{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "523e70f1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:09.489200Z",
     "iopub.status.busy": "2026-10-02T14:41:09.488975Z",
     "iopub.status.idle": "2026-10-02T14:41:09.494118Z",
     "shell.execute_reply": "2026-10-02T14:41:09.493527Z"
    },
    "papermill": {
     "duration": 0.008412,
     "end_time": "2026-10-02T14:41:09.495168+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:09.486756+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1231aa1d",
   "metadata": {
    "papermill": {
     "duration": 0.000926,
     "end_time": "2026-10-02T14:41:09.497337+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:09.496411+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# AddSubtractComp\n",
    "\n",
    "[AddSubtractComp](../../../_srcdocs/packages/components/add_subtract_comp) 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": 2,
   "id": "2246e962",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:09.500365Z",
     "iopub.status.busy": "2026-10-02T14:41:09.500168Z",
     "iopub.status.idle": "2026-10-02T14:41:12.171240Z",
     "shell.execute_reply": "2026-10-02T14:41:12.170581Z"
    },
    "papermill": {
     "duration": 2.673657,
     "end_time": "2026-10-02T14:41:12.171999+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:09.498342+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1790952072.158905] [runnervm8df0l:6335 :0]        ib_iface.c:1269 UCX  ERROR mana_0: iface 0x55f0f24a10c0 failed to create UD QP TX wr:256 sge:6 inl:64 resp:0 RX wr:4096 sge:1 resp:0 failed: Operation not supported\n",
      "[1790952072.159198] [runnervm8df0l:6335 :0]      ucp_worker.c:1412 UCX  ERROR uct_iface_open(ud_verbs/mana_0:1) failed: Input/output error\n",
      "[[100.   0.]\n",
      " [200.  -1.]\n",
      " [300.  -2.]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[runnervm8df0l:06335] pml_ucx.c:313  Error: Failed to create UCP worker\n"
     ]
    }
   ],
   "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": 3,
   "id": "fa26eea4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:12.174471Z",
     "iopub.status.busy": "2026-10-02T14:41:12.174236Z",
     "iopub.status.idle": "2026-10-02T14:41:12.179022Z",
     "shell.execute_reply": "2026-10-02T14:41:12.178400Z"
    },
    "papermill": {
     "duration": 0.006465,
     "end_time": "2026-10-02T14:41:12.179421+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:12.172956+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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.13.14"
  },
  "orphan": true,
  "papermill": {
   "default_parameters": {},
   "duration": 3.813621,
   "end_time": "2026-10-02T14:41:12.695261+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/features/building_blocks/components/add_subtract_comp.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/features/building_blocks/components/add_subtract_comp.ipynb",
   "parameters": {},
   "start_time": "2026-10-02T14:41:08.881640+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}