{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AddSubtractComp\n",
    "\n",
    "[AddSubtractComp](../../../_srcdocs/packages/components/add_subtract_comp.html#openmdao-components-add-subtract-comp-py) performs elementwise addition or subtraction between two or more compatible inputs.  It may be vectorized to provide the result at one or more points simultaneously.\n",
    "\n",
    "$$\n",
    "    result = a * \\textrm{scaling factor}_a + b * \\textrm{scaling factor}_b + c * \\textrm{scaling factor}_c + ...\n",
    "$$\n",
    "\n",
    "## AddSubtractComp Constructor\n",
    "\n",
    "The call signature for the `AddSubtractComp` constructor is:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.add_subtract_comp.AddSubtractComp.__init__\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "## Using the AddSubtractComp\n",
    "\n",
    "The `add_equation` method is used to set up a system of inputs to be added/subtracted (with scaling factors).\n",
    "Each time the user adds an equation, all of the inputs and outputs must be of identical shape (this is a requirement for element-wise addition/subtraction).\n",
    "The units must also be compatible between all inputs and the output of each equation.\n",
    "\n",
    "## Method Signature\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.add_subtract_comp.AddSubtractComp.add_equation\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "## AddSubtractComp Example\n",
    "\n",
    "In the following example AddSubtractComp is used to add thrust, drag, lift, and weight forces. Note the scaling factor of -1 for the drag force and weight force."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import openmdao.api as om\n",
    "\n",
    "n = 3\n",
    "\n",
    "p = om.Problem()\n",
    "model = p.model\n",
    "\n",
    "# Construct an adder/subtracter here. create a relationship through the add_equation method\n",
    "adder = om.AddSubtractComp()\n",
    "adder.add_equation('total_force', input_names=['thrust', 'drag', 'lift', 'weight'],\n",
    "                   vec_size=n, length=2, scaling_factors=[1, -1, 1, -1], units='kN')\n",
    "# Note the scaling factors. we assume all forces are positive sign upstream\n",
    "\n",
    "# The vector represents forces at 3 time points (rows) in 2 dimensional plane (cols)\n",
    "p.model.add_subsystem(name='totalforcecomp', subsys=adder,\n",
    "                      promotes_inputs=['thrust', 'drag', 'lift', 'weight'])\n",
    "\n",
    "p.setup()\n",
    "\n",
    "# Set thrust to exceed drag, weight to equal lift for this scenario\n",
    "p['thrust'][:, 0] = [500, 600, 700]\n",
    "p['drag'][:, 0] = [400, 400, 400]\n",
    "p['weight'][:, 1] = [1000, 1001, 1002]\n",
    "p['lift'][:, 1] = [1000, 1000, 1000]\n",
    "\n",
    "p.run_model()\n",
    "\n",
    "# Verify the results\n",
    "expected_i = np.array([[100, 200, 300], [0, -1, -2]]).T\n",
    "print(p.get_val('totalforcecomp.total_force', units='kN'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "assert_near_equal(p.get_val('totalforcecomp.total_force', units='kN'), expected_i)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
