{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fcf57770",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:14.225266Z",
     "iopub.status.busy": "2026-10-02T14:43:14.225066Z",
     "iopub.status.idle": "2026-10-02T14:43:14.229253Z",
     "shell.execute_reply": "2026-10-02T14:43:14.228503Z"
    },
    "papermill": {
     "duration": 0.007689,
     "end_time": "2026-10-02T14:43:14.229741+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:14.222052+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]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfe2fb41",
   "metadata": {
    "papermill": {
     "duration": 0.032386,
     "end_time": "2026-10-02T14:43:14.264367+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:14.231981+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# System Options (Arguments to Components and Groups)\n",
    "\n",
    "The primary jobs of a component, whether explicit or implicit, are to define inputs and outputs and to do the mapping that computes the outputs given the inputs. Often, however, there are incidental parameters that affect the behavior of the component, but which are not considered input variables in the sense of being computed as an output of another component.\n",
    "\n",
    "OpenMDAO provides a way of declaring these parameters, which are contained in an *OptionsDictionary* named `options` that is available in every system. Options associated with a particular component or group must be declared in the *initialize* method of the system definition. A default value can be provided as well as various checks for validity, such as a list of acceptable values or types.\n",
    "\n",
    "The attributes that can be specified when declaring an option are enumerated and described below:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.utils.options_dictionary.OptionsDictionary.declare\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "When using the `check_valid` argument, the expected function signature is:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.utils.options_dictionary.check_valid\n",
    "        :noindex:\n",
    "```\n",
    "Option values are typically passed at component instantiation time as keyword arguments, which are automatically assigned into the option dictionary. The options are then available for use in the component’s other methods, such as *setup* and *compute*.\n",
    "\n",
    "Alternatively, values can be set at a later time, in another method of the component (except for *initialize*) or outside of the component definition after the component is instantiated.\n",
    "\n",
    "## A Simple Example\n",
    "\n",
    "Options are commonly used to specify the shape or size of the component’s input and output variables, such as in this simple example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5e2c2bf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:14.291248Z",
     "iopub.status.busy": "2026-10-02T14:43:14.291085Z",
     "iopub.status.idle": "2026-10-02T14:43:15.599239Z",
     "shell.execute_reply": "2026-10-02T14:43:15.598635Z"
    },
    "papermill": {
     "duration": 1.334283,
     "end_time": "2026-10-02T14:43:15.600400+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:14.266117+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "A component that multiplies a vector by 2, where the\n",
    "size of the vector is given as an option of type 'int'.\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class VectorDoublingComp(om.ExplicitComponent):\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('size', types=int)\n",
    "\n",
    "    def setup(self):\n",
    "        size = self.options['size']\n",
    "\n",
    "        self.add_input('x', shape=size)\n",
    "        self.add_output('y', shape=size)\n",
    "\n",
    "    def setup_partials(self):\n",
    "        size = self.options['size']\n",
    "        self.declare_partials('y', 'x', val=2.,\n",
    "                              rows=np.arange(size),\n",
    "                              cols=np.arange(size))\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = 2 * inputs['x']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6e1e8fa1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:15.607447Z",
     "iopub.status.busy": "2026-10-02T14:43:15.607010Z",
     "iopub.status.idle": "2026-10-02T14:43:16.824775Z",
     "shell.execute_reply": "2026-10-02T14:43:16.824211Z"
    },
    "papermill": {
     "duration": 1.222025,
     "end_time": "2026-10-02T14:43:16.825399+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:15.603374+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1790952196.814914] [runnervm8df0l:7646 :0]        ib_iface.c:1269 UCX  ERROR mana_0: iface 0x5572d4108840 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",
      "[1790952196.815165] [runnervm8df0l:7646 :0]      ucp_worker.c:1412 UCX  ERROR uct_iface_open(ud_verbs/mana_0:1) failed: Input/output error\n",
      "[2. 4. 6.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[runnervm8df0l:07646] pml_ucx.c:313  Error: Failed to create UCP worker\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "prob.model.add_subsystem('double', VectorDoublingComp(size=3))  # 'size' is an option\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('double.x', [1., 2., 3.])\n",
    "\n",
    "prob.run_model()\n",
    "print(prob.get_val('double.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ce702e58",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.829988Z",
     "iopub.status.busy": "2026-10-02T14:43:16.829833Z",
     "iopub.status.idle": "2026-10-02T14:43:16.834799Z",
     "shell.execute_reply": "2026-10-02T14:43:16.834011Z"
    },
    "papermill": {
     "duration": 0.008118,
     "end_time": "2026-10-02T14:43:16.835552+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.827434+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "assert_near_equal(prob.get_val('double.y'), [2., 4., 6.])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5e28437",
   "metadata": {
    "papermill": {
     "duration": 0.002561,
     "end_time": "2026-10-02T14:43:16.840901+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.838340+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Not setting a default value when declaring an option implies that the value must be set by the user.\n",
    "\n",
    "In this example, ‘size’ is required; We would have gotten an error if we:\n",
    "\n",
    "1. Did not pass in ‘size’ when instantiating *VectorDoublingComp* and\n",
    "2. Did not set its value in the code for *VectorDoublingComp*.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4da6610c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.846834Z",
     "iopub.status.busy": "2026-10-02T14:43:16.846690Z",
     "iopub.status.idle": "2026-10-02T14:43:16.850689Z",
     "shell.execute_reply": "2026-10-02T14:43:16.850036Z"
    },
    "papermill": {
     "duration": 0.007552,
     "end_time": "2026-10-02T14:43:16.851137+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.843585+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'double' <class VectorDoublingComp>: Option 'size' is required but has not been set.\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.options_feature_vector import VectorDoublingComp\n",
    "\n",
    "prob = om.Problem()\n",
    "prob.model.add_subsystem('double', VectorDoublingComp())  # 'size' not specified\n",
    "\n",
    "try:\n",
    "    prob.setup()\n",
    "except RuntimeError as err:\n",
    "    print(str(err))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4179545e",
   "metadata": {
    "papermill": {
     "duration": 0.002785,
     "end_time": "2026-10-02T14:43:16.900650+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.897865+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Option Names\n",
    "\n",
    "An option name can be any valid string. It is recommended that you use clear concise names. It is also recommended that you restrict the characters to those permitted in valid python names so that they can be passed into the system's initialization arguments, but it is not required.\n",
    "\n",
    "## Option Types\n",
    "\n",
    "Options are not limited to simple types like `int`. In the following example, the component takes a *Numpy* array as an option:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "49265cf9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.906631Z",
     "iopub.status.busy": "2026-10-02T14:43:16.906447Z",
     "iopub.status.idle": "2026-10-02T14:43:16.909966Z",
     "shell.execute_reply": "2026-10-02T14:43:16.909353Z"
    },
    "papermill": {
     "duration": 0.007209,
     "end_time": "2026-10-02T14:43:16.910388+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.903179+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "A component that multiplies an array by an input value, where\n",
    "the array is given as an option of type 'numpy.ndarray'.\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class ArrayMultiplyComp(om.ExplicitComponent):\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('array', types=np.ndarray)\n",
    "\n",
    "    def setup(self):\n",
    "        array = self.options['array']\n",
    "\n",
    "        self.add_input('x', 1.)\n",
    "        self.add_output('y', shape=array.shape)\n",
    "\n",
    "    def setup_partials(self):\n",
    "        self.declare_partials(of='*', wrt='*')\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = self.options['array'] * inputs['x']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "44d80fe1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.914730Z",
     "iopub.status.busy": "2026-10-02T14:43:16.914540Z",
     "iopub.status.idle": "2026-10-02T14:43:16.920223Z",
     "shell.execute_reply": "2026-10-02T14:43:16.919535Z"
    },
    "papermill": {
     "duration": 0.008472,
     "end_time": "2026-10-02T14:43:16.920680+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.912208+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5. 10. 15.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "prob.model.add_subsystem('a_comp', ArrayMultiplyComp(array=np.array([1, 2, 3])))\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('a_comp.x', 5.)\n",
    "\n",
    "prob.run_model()\n",
    "print(prob.get_val('a_comp.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b61931bf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.924975Z",
     "iopub.status.busy": "2026-10-02T14:43:16.924852Z",
     "iopub.status.idle": "2026-10-02T14:43:16.927757Z",
     "shell.execute_reply": "2026-10-02T14:43:16.927236Z"
    },
    "papermill": {
     "duration": 0.005652,
     "end_time": "2026-10-02T14:43:16.928157+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.922505+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('a_comp.y'), [5., 10., 15.])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a62cbd1c",
   "metadata": {
    "papermill": {
     "duration": 0.002008,
     "end_time": "2026-10-02T14:43:16.931985+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.929977+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "It is even possible to provide a function as an option:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "426beb85",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.937102Z",
     "iopub.status.busy": "2026-10-02T14:43:16.936939Z",
     "iopub.status.idle": "2026-10-02T14:43:16.940677Z",
     "shell.execute_reply": "2026-10-02T14:43:16.940128Z"
    },
    "papermill": {
     "duration": 0.007319,
     "end_time": "2026-10-02T14:43:16.941281+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.933962+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "A component that computes y = func(x), where func\n",
    "is a function given as an option.\n",
    "\"\"\"\n",
    "\n",
    "from types import FunctionType\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class UnitaryFunctionComp(om.ExplicitComponent):\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('func', types=FunctionType, recordable=False)\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_input('x')\n",
    "        self.add_output('y')\n",
    "\n",
    "    def setup_partials(self):\n",
    "        self.declare_partials('y', 'x', method='fd')\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        func = self.options['func']\n",
    "        outputs['y'] = func(inputs['x'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b13f1b80",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.980658Z",
     "iopub.status.busy": "2026-10-02T14:43:16.980491Z",
     "iopub.status.idle": "2026-10-02T14:43:16.985939Z",
     "shell.execute_reply": "2026-10-02T14:43:16.985320Z"
    },
    "papermill": {
     "duration": 0.009139,
     "end_time": "2026-10-02T14:43:16.986337+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.977198+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10.]\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "def my_func(x):\n",
    "    return x*2\n",
    "\n",
    "prob = om.Problem()\n",
    "prob.model.add_subsystem('f_comp', UnitaryFunctionComp(func=my_func))\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('f_comp.x', 5.)\n",
    "\n",
    "prob.run_model()\n",
    "print(prob.get_val('f_comp.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "db5f62b2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:16.992661Z",
     "iopub.status.busy": "2026-10-02T14:43:16.992498Z",
     "iopub.status.idle": "2026-10-02T14:43:16.995505Z",
     "shell.execute_reply": "2026-10-02T14:43:16.994973Z"
    },
    "papermill": {
     "duration": 0.006906,
     "end_time": "2026-10-02T14:43:16.996010+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.989104+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('f_comp.y'), 10.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61ca5bbb",
   "metadata": {
    "papermill": {
     "duration": 0.00307,
     "end_time": "2026-10-02T14:43:17.001979+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:16.998909+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Providing Default Values\n",
    "\n",
    "One reason why using options is convenient is that a default value can be specified, making it optional to pass the value in during component instantiation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b519d880",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.025392Z",
     "iopub.status.busy": "2026-10-02T14:43:17.025229Z",
     "iopub.status.idle": "2026-10-02T14:43:17.028850Z",
     "shell.execute_reply": "2026-10-02T14:43:17.028196Z"
    },
    "papermill": {
     "duration": 0.007482,
     "end_time": "2026-10-02T14:43:17.029235+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.021753+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "A component that computes y = a*x + b, where a and b\n",
    "are given as an option of type 'numpy.ScalarType'.\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class LinearCombinationComp(om.ExplicitComponent):\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('a', default=1., types=np.ScalarType)\n",
    "        self.options.declare('b', default=1., types=np.ScalarType)\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_input('x')\n",
    "        self.add_output('y')\n",
    "\n",
    "    def setup_partials(self):\n",
    "        self.declare_partials('y', 'x', val=self.options['a'])\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = self.options['a'] * inputs['x'] + self.options['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f7381877",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.035601Z",
     "iopub.status.busy": "2026-10-02T14:43:17.035440Z",
     "iopub.status.idle": "2026-10-02T14:43:17.040396Z",
     "shell.execute_reply": "2026-10-02T14:43:17.039982Z"
    },
    "papermill": {
     "duration": 0.008761,
     "end_time": "2026-10-02T14:43:17.040809+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.032048+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7.]\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "prob.model.add_subsystem('linear', LinearCombinationComp(a=2.))  # 'b' not specified\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('linear.x', 3)\n",
    "\n",
    "prob.run_model()\n",
    "print(prob.get_val('linear.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "633f8f41",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.048275Z",
     "iopub.status.busy": "2026-10-02T14:43:17.048132Z",
     "iopub.status.idle": "2026-10-02T14:43:17.050167Z",
     "shell.execute_reply": "2026-10-02T14:43:17.049776Z"
    },
    "papermill": {
     "duration": 0.005826,
     "end_time": "2026-10-02T14:43:17.050557+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.044731+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(prob.get_val('linear.y') == 7.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06bed138",
   "metadata": {
    "papermill": {
     "duration": 0.002966,
     "end_time": "2026-10-02T14:43:17.152766+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.149800+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "In this example, both ‘a’ and ‘b’ are optional, so it is valid to pass in ‘a’, but not ‘b’.\n",
    "\n",
    "## Specifying Values or Types\n",
    "\n",
    "The parameters available when declaring an option allow a great deal of flexibility in specifying exactly what types and values are acceptable.\n",
    "\n",
    "As seen above, the allowed types can be specified using the `types` parameter. If an option is more limited, then the set of allowed values can be given with `values`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ea0ab308",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.159493Z",
     "iopub.status.busy": "2026-10-02T14:43:17.159298Z",
     "iopub.status.idle": "2026-10-02T14:43:17.166661Z",
     "shell.execute_reply": "2026-10-02T14:43:17.166024Z"
    },
    "papermill": {
     "duration": 0.011727,
     "end_time": "2026-10-02T14:43:17.167240+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.155513+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 4. 6. 8.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import openmdao.api as om\n",
    "\n",
    "class VectorDoublingComp(om.ExplicitComponent):\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('size', values=[2, 4, 6, 8])\n",
    "\n",
    "    def setup(self):\n",
    "        size = self.options['size']\n",
    "\n",
    "        self.add_input('x', shape=size)\n",
    "        self.add_output('y', shape=size)\n",
    "        self.declare_partials('y', 'x', val=2.,\n",
    "                              rows=np.arange(size),\n",
    "                              cols=np.arange(size))\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = 2 * inputs['x']\n",
    "\n",
    "prob = om.Problem()\n",
    "prob.model.add_subsystem('double', VectorDoublingComp(size=4))\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('double.x', [1., 2., 3., 4.])\n",
    "\n",
    "prob.run_model()\n",
    "print(prob.get_val('double.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d54a1204",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.173670Z",
     "iopub.status.busy": "2026-10-02T14:43:17.173508Z",
     "iopub.status.idle": "2026-10-02T14:43:17.178105Z",
     "shell.execute_reply": "2026-10-02T14:43:17.177511Z"
    },
    "papermill": {
     "duration": 0.008951,
     "end_time": "2026-10-02T14:43:17.178980+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.170029+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('double.y'), [2., 4., 6., 8.])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ad81366",
   "metadata": {
    "papermill": {
     "duration": 0.002797,
     "end_time": "2026-10-02T14:43:17.230467+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.227670+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "```{warning}\n",
    "It is an error to attempt to specify both a list of acceptable values and a list of acceptable types.\n",
    "```\n",
    "\n",
    "\n",
    "Alternatively, the allowable values can be set using bounds and/or a validation function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a545b20a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.236837Z",
     "iopub.status.busy": "2026-10-02T14:43:17.236643Z",
     "iopub.status.idle": "2026-10-02T14:43:17.241192Z",
     "shell.execute_reply": "2026-10-02T14:43:17.240416Z"
    },
    "papermill": {
     "duration": 0.008497,
     "end_time": "2026-10-02T14:43:17.241638+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.233141+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Option 'size' with value 5 must be an even number.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import openmdao.api as om\n",
    "\n",
    "def check_even(name, value):\n",
    "    if value % 2 != 0:\n",
    "        raise ValueError(f\"Option '{name}' with value {value} must be an even number.\")\n",
    "\n",
    "class VectorDoublingComp(om.ExplicitComponent):\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('size', types=int, lower=2, upper=8, check_valid=check_even)\n",
    "\n",
    "    def setup(self):\n",
    "        size = self.options['size']\n",
    "\n",
    "        self.add_input('x', shape=size)\n",
    "        self.add_output('y', shape=size)\n",
    "        self.declare_partials('y', 'x', val=2.,\n",
    "                              rows=np.arange(size),\n",
    "                              cols=np.arange(size))\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = 2 * inputs['x']\n",
    "\n",
    "try:\n",
    "    comp = VectorDoublingComp(size=5)\n",
    "except Exception as err:\n",
    "    msg = str(err)\n",
    "    print(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "66647f55",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.248643Z",
     "iopub.status.busy": "2026-10-02T14:43:17.248470Z",
     "iopub.status.idle": "2026-10-02T14:43:17.250783Z",
     "shell.execute_reply": "2026-10-02T14:43:17.250217Z"
    },
    "papermill": {
     "duration": 0.006404,
     "end_time": "2026-10-02T14:43:17.251227+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.244823+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(msg == \"Option 'size' with value 5 must be an even number.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8f0d113",
   "metadata": {
    "papermill": {
     "duration": 0.003992,
     "end_time": "2026-10-02T14:43:17.289059+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.285067+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Setting Multiple Option Values Simultaneously\n",
    "\n",
    "The *OptionsDictionary* class is versatile and can be used in a variety of situations.\n",
    "While all systems contain an `options` attribute, users may wish to introduce other options for their systems or other classes using OpenMDAO's *OptionsDictionary*.\n",
    "\n",
    "After initialization, users might want the ability to modify more than one option at a time.\n",
    "For this purpose, the `set` method on *OptionsDictionary* exists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "85a0d81e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.308799Z",
     "iopub.status.busy": "2026-10-02T14:43:17.308583Z",
     "iopub.status.idle": "2026-10-02T14:43:17.315112Z",
     "shell.execute_reply": "2026-10-02T14:43:17.314256Z"
    },
    "papermill": {
     "duration": 0.01093,
     "end_time": "2026-10-02T14:43:17.315662+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.304732+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[14.]\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "linear_comp = prob.model.add_subsystem('linear', LinearCombinationComp())\n",
    "\n",
    "# Set options a and b at the same time.\n",
    "linear_comp.options.set(a=3., b=5.)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('linear.x', 3)\n",
    "\n",
    "prob.run_model()\n",
    "print(prob.get_val('linear.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0d16cd70",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.360564Z",
     "iopub.status.busy": "2026-10-02T14:43:17.360383Z",
     "iopub.status.idle": "2026-10-02T14:43:17.363000Z",
     "shell.execute_reply": "2026-10-02T14:43:17.362403Z"
    },
    "papermill": {
     "duration": 0.044677,
     "end_time": "2026-10-02T14:43:17.363478+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.318801+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(prob.get_val('linear.y') == 14.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d488b4e",
   "metadata": {
    "papermill": {
     "duration": 0.002905,
     "end_time": "2026-10-02T14:43:17.385550+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.382645+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Note that the option name must be a valid python name in order to use this `set` function. If your option has a name that is not valid in python (e.g., \"aircraft:propulsion:NUM_ENGINES\"), then you will have to set them individually with the dictionary interface.\n",
    "\n",
    "## Setting options throughout a problem model (`Problem.model_options`)\n",
    "\n",
    "It is common for options to be passed down through a model tree from parent Group to child Group or Component.\n",
    "This can make the addition of a new option deep in the model a tedious procedure, since each parent system may need to have an option added to itself in order to know how to instantiate its children, and then actually take the given input option and pass it to the child.\n",
    "\n",
    "There are a few ways to address this. For example, you might implement a function to declare the common options and call it in the relevant subsystems' `initialize` method.\n",
    "Alternatively, those classes can subclass from a class which automatically adds the appropriate options.\n",
    "\n",
    "For passing options to components nested deeply within the model, the OpenMDAO `Problem` object provides an attribute named `model_options`. `Problem.model_options` is a standard dictionary, keyed by a string that serves as a [glob](https://en.wikipedia.org/wiki/Glob_(programming)) filter for system pathnames.\n",
    "\n",
    "For each corresponding value, a sub dictionary provides string keys of option names, with corresponding option values as the associated value.\n",
    "\n",
    "When OpenMDAO begins `setup`, these options are passed down to each system in the model\n",
    "If a system's pathname matches the key in model options, it will accept the value for each option which it possesses.\n",
    "If it does not possess a particular option, that option value is ignored by that particular system.\n",
    "\n",
    "Dictionaries in Python are ordered, so if a particular component matches multiple glob patterns specifying the same options, the last match will take precedence.\n",
    "\n",
    "The following code demonstrates a model with several levels of depth where the component leaves accept options `a` and `b`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1be3e7b7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.574220Z",
     "iopub.status.busy": "2026-10-02T14:43:17.573966Z",
     "iopub.status.idle": "2026-10-02T14:43:17.582936Z",
     "shell.execute_reply": "2026-10-02T14:43:17.581924Z"
    },
    "papermill": {
     "duration": 0.194969,
     "end_time": "2026-10-02T14:43:17.583531+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.388562+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "\n",
    "c1 = prob.model.add_subsystem('c1', LinearCombinationComp())\n",
    "g1 = prob.model.add_subsystem('g1', om.Group())\n",
    "\n",
    "c2 = g1.add_subsystem('c2', LinearCombinationComp())\n",
    "g2 = g1.add_subsystem('g2', om.Group())\n",
    "\n",
    "c3 = g2.add_subsystem('c3', LinearCombinationComp())\n",
    "g3 = g2.add_subsystem('g3', om.Group())\n",
    "\n",
    "prob.model.connect('c1.y', 'g1.c2.x')\n",
    "prob.model.connect('g1.c2.y', 'g1.g2.c3.x')\n",
    "\n",
    "# Any component with options 'a' or 'b' accepts these values.\n",
    "prob.model_options['*'] = {'a': 3., 'b': 5.}\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('c1.x', 3)\n",
    "\n",
    "prob.run_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "83666d52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.590803Z",
     "iopub.status.busy": "2026-10-02T14:43:17.590595Z",
     "iopub.status.idle": "2026-10-02T14:43:17.593454Z",
     "shell.execute_reply": "2026-10-02T14:43:17.592813Z"
    },
    "papermill": {
     "duration": 0.007074,
     "end_time": "2026-10-02T14:43:17.593930+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.586856+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(c1.options['a'] == c2.options['a'] == c3.options['a'])\n",
    "assert(c1.options['b'] == c2.options['b'] == c3.options['b'])\n",
    "assert(prob.get_val('g1.g2.c3.y') == ((3 * 3 + 5) * 3 + 5) * 3 + 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09cedce4",
   "metadata": {
    "papermill": {
     "duration": 0.002837,
     "end_time": "2026-10-02T14:43:17.599572+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.596735+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Showing detailed node information for the components below will indicate that options `a` and `b` have the correct values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7ac11228",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.860769Z",
     "iopub.status.busy": "2026-10-02T14:43:17.860392Z",
     "iopub.status.idle": "2026-10-02T14:43:17.922822Z",
     "shell.execute_reply": "2026-10-02T14:43:17.922233Z"
    },
    "papermill": {
     "duration": 0.068127,
     "end_time": "2026-10-02T14:43:17.923338+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.855211+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"700\"\n",
       "            src=\"n2.html\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x7f2a54de6660>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "om.n2(prob)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c3625a3",
   "metadata": {
    "papermill": {
     "duration": 0.003212,
     "end_time": "2026-10-02T14:43:17.930329+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.927117+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Using glob patterns to set different option values in different systems."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "949c12f7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.938158Z",
     "iopub.status.busy": "2026-10-02T14:43:17.937969Z",
     "iopub.status.idle": "2026-10-02T14:43:17.946139Z",
     "shell.execute_reply": "2026-10-02T14:43:17.945650Z"
    },
    "papermill": {
     "duration": 0.013208,
     "end_time": "2026-10-02T14:43:17.946548+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.933340+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[254.]\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "\n",
    "c1 = prob.model.add_subsystem('c1', LinearCombinationComp())\n",
    "g1 = prob.model.add_subsystem('g1', om.Group())\n",
    "\n",
    "c2 = g1.add_subsystem('c2', LinearCombinationComp())\n",
    "g2 = g1.add_subsystem('g2', om.Group())\n",
    "\n",
    "c3 = g2.add_subsystem('c3', LinearCombinationComp())\n",
    "g3 = g2.add_subsystem('g3', om.Group())\n",
    "\n",
    "prob.model.connect('c1.y', 'g1.c2.x')\n",
    "prob.model.connect('g1.c2.y', 'g1.g2.c3.x')\n",
    "\n",
    "# Only component 'c1' will accept these values.\n",
    "prob.model_options['c1'] = {'a': 3., 'b': 5.}\n",
    "\n",
    "# Any component that is not at the top of the model and whose name matches\n",
    "# pattern `c?` will accept these values.\n",
    "prob.model_options['*.c?'] = {'a': 4., 'b': 6.}\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('c1.x', 3)\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('g1.g2.c3.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cba56a9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.952392Z",
     "iopub.status.busy": "2026-10-02T14:43:17.952089Z",
     "iopub.status.idle": "2026-10-02T14:43:17.954982Z",
     "shell.execute_reply": "2026-10-02T14:43:17.954480Z"
    },
    "papermill": {
     "duration": 0.00627,
     "end_time": "2026-10-02T14:43:17.955531+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.949261+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(c1.options['a'] == 3)\n",
    "assert(c2.options['a'] == c3.options['a'] == 4.)\n",
    "assert(c1.options['b'] == 5.)\n",
    "assert(c2.options['b'] == c3.options['b'] == 6.)\n",
    "assert(prob.get_val('g1.g2.c3.y') == ((3 * 3 + 5) * 4 + 6) * 4 + 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c5ff4f9",
   "metadata": {
    "papermill": {
     "duration": 0.00204,
     "end_time": "2026-10-02T14:43:17.959672+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.957632+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Modifying `model_options` at the Group level\n",
    "\n",
    "Sometimes it may be desirable to have a Group modify the contents of `model_options` during setup so that it can specify options for its children.  For this purpose, group has a view of `model_options` that is accessible via the `Group.model_options` attribute.\n",
    "\n",
    "During setup, a Group can set options for all of its children by appending something to `model_options` with the appropriate path.\n",
    "\n",
    "Remember that `setup` is a top-down process, so `model_options` must be modified no later than `setup` in order for the appropriate information to be sent to the Group's descendents.\n",
    "The user should be cautious when using this functionality.\n",
    "They have the ability to modify model options for other systems in the model tree (such as \"sibling\" systems and their descendents), but doing so may result in undefined behavior that will work sometimes and othertimes not depending on the specific ordering of the systems in the model tree.\n",
    "In short, it's good practice to prefix the model options with `'self.pathname'` when using this feature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "09062fa2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.964518Z",
     "iopub.status.busy": "2026-10-02T14:43:17.964351Z",
     "iopub.status.idle": "2026-10-02T14:43:17.972256Z",
     "shell.execute_reply": "2026-10-02T14:43:17.971577Z"
    },
    "papermill": {
     "duration": 0.010889,
     "end_time": "2026-10-02T14:43:17.972621+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.961732+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[173.]\n"
     ]
    }
   ],
   "source": [
    "class MyGroup(om.Group):\n",
    "\n",
    "    def setup(self):\n",
    "        g1 = self.add_subsystem('g1', om.Group())\n",
    "        g1.add_subsystem('c1', LinearCombinationComp())\n",
    "        g1.add_subsystem('c2', LinearCombinationComp())\n",
    "        g1.add_subsystem('c3', LinearCombinationComp())\n",
    "\n",
    "        # Send options a and b to all descendents of this model.\n",
    "        self.model_options[f'{self.pathname}.*'] = {'a': 3., 'b': 5.}\n",
    "\n",
    "        g1.connect('c1.y', 'c2.x')\n",
    "        g1.connect('c2.y', 'c3.x')\n",
    "\n",
    "p = om.Problem()\n",
    "p.model.add_subsystem('my_group', MyGroup())\n",
    "p.setup()\n",
    "\n",
    "p.set_val('my_group.g1.c1.x', 4)\n",
    "\n",
    "p.run_model()\n",
    "\n",
    "print(p.get_val('my_group.g1.c3.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6e11a676",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:17.977611Z",
     "iopub.status.busy": "2026-10-02T14:43:17.977458Z",
     "iopub.status.idle": "2026-10-02T14:43:17.981124Z",
     "shell.execute_reply": "2026-10-02T14:43:17.980404Z"
    },
    "papermill": {
     "duration": 0.006744,
     "end_time": "2026-10-02T14:43:17.981541+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.974797+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c3y = p.get_val('my_group.g1.c3.y')\n",
    "expected = ((4 * 3 + 5) * 3 + 5) * 3 + 5.\n",
    "assert_near_equal(expected, c3y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "481948e6",
   "metadata": {
    "papermill": {
     "duration": 0.002097,
     "end_time": "2026-10-02T14:43:17.985890+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.983793+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Listing Options\n",
    "\n",
    "You can list all options in a `Group` or `Component` and their associated linear and nonlinear solvers by using the `list_options` method. This will return the options in a list of tuples of (system pathname, system dictionary of options, nonlinear_solver dictionary of options, linear_solver dictionary of options), and will also print them to stdout or any output stream that you request."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bfc75163",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:18.013316Z",
     "iopub.status.busy": "2026-10-02T14:43:18.013148Z",
     "iopub.status.idle": "2026-10-02T14:43:18.016126Z",
     "shell.execute_reply": "2026-10-02T14:43:18.015595Z"
    },
    "papermill": {
     "duration": 0.028933,
     "end_time": "2026-10-02T14:43:18.016959+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:17.988026+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Options for 6 components in 'model\n",
      "\n",
      "option name                  option value\n",
      "---------------------------  ------------------------------\n",
      "model\n",
      "  assembled_jac_type           <class 'NoneType'>\n",
      "  derivs_method                <class 'NoneType'>\n",
      "  auto_order                   False\n",
      "  nonlinear_solver\n",
      "    iprint                     1\n",
      "    debug_print                False\n",
      "  linear_solver\n",
      "    iprint                     1\n",
      "    assemble_jac               False\n",
      "    use_aitken                 False\n",
      "    aitken_min_factor          0.1\n",
      "    aitken_max_factor          1.5\n",
      "    aitken_initial_factor      1.0\n",
      "  my_group\n",
      "    assembled_jac_type         <class 'NoneType'>\n",
      "    derivs_method              <class 'NoneType'>\n",
      "    auto_order                 False\n",
      "    nonlinear_solver\n",
      "      iprint                   1\n",
      "      debug_print              False\n",
      "    linear_solver\n",
      "      iprint                   1\n",
      "      assemble_jac             False\n",
      "      use_aitken               False\n",
      "      aitken_min_factor        0.1\n",
      "      aitken_max_factor        1.5\n",
      "      aitken_initial_factor    1.0\n",
      "    g1\n",
      "      assembled_jac_type       <class 'NoneType'>\n",
      "      derivs_method            <class 'NoneType'>\n",
      "      auto_order               False\n",
      "      nonlinear_solver\n",
      "        iprint                 1\n",
      "        debug_print            False\n",
      "      linear_solver\n",
      "        iprint                 1\n",
      "        assemble_jac           False\n",
      "        use_aitken             False\n",
      "        aitken_min_factor      0.1\n",
      "        aitken_max_factor      1.5\n",
      "        aitken_initial_factor  1.0\n",
      "      c1\n",
      "        derivs_method          <class 'NoneType'>\n",
      "        distributed            False\n",
      "        run_root_only          False\n",
      "        always_opt             False\n",
      "        use_jit                True\n",
      "        default_shape          (1,)\n",
      "        a                      3.0\n",
      "        b                      5.0\n",
      "      c2\n",
      "        derivs_method          <class 'NoneType'>\n",
      "        distributed            False\n",
      "        run_root_only          False\n",
      "        always_opt             False\n",
      "        use_jit                True\n",
      "        default_shape          (1,)\n",
      "        a                      3.0\n",
      "        b                      5.0\n",
      "      c3\n",
      "        derivs_method          <class 'NoneType'>\n",
      "        distributed            False\n",
      "        run_root_only          False\n",
      "        always_opt             False\n",
      "        use_jit                True\n",
      "        default_shape          (1,)\n",
      "        a                      3.0\n",
      "        b                      5.0\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "myoptions = p.model.list_options(include_solvers=True, include_default=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b251767b",
   "metadata": {
    "papermill": {
     "duration": 0.002325,
     "end_time": "2026-10-02T14:43:18.021528+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:18.019203+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Adding a function to pre-process setting an option\n",
    "\n",
    "The `OptionsDictionary` has support for custom pre-processing the value before it is set. One potential use for this is to provide a way to convert units while setting an option. The following example shows how to do this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f3d3e404",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:18.026759Z",
     "iopub.status.busy": "2026-10-02T14:43:18.026560Z",
     "iopub.status.idle": "2026-10-02T14:43:18.034472Z",
     "shell.execute_reply": "2026-10-02T14:43:18.033712Z"
    },
    "papermill": {
     "duration": 0.011254,
     "end_time": "2026-10-02T14:43:18.035057+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:18.023803+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The following should be 72 if the units convert correctly.\n",
      "[72.]\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.utils.units import convert_units\n",
    "\n",
    "\n",
    "# TODO: Turn this into a test.\n",
    "\n",
    "def units_setter(opt_meta, value):\n",
    "    \"\"\"\n",
    "    Check and convert new units tuple into\n",
    "\n",
    "    Parameters\n",
    "    ----------\n",
    "    opt_meta : dict\n",
    "        Dictionary of entries for the option.\n",
    "    value : any\n",
    "        New value for the option.\n",
    "\n",
    "    Returns\n",
    "    -------\n",
    "    any\n",
    "        Post processed value to set into the option.\n",
    "    \"\"\"\n",
    "    new_val, new_units = value\n",
    "    old_val, units = opt_meta['val']\n",
    "\n",
    "    converted_val = convert_units(new_val, new_units, units)\n",
    "    return (converted_val, units)\n",
    "\n",
    "\n",
    "class MyComp(om.ExplicitComponent):\n",
    "\n",
    "    def setup(self):\n",
    "\n",
    "        self.add_input('x', 3.0)\n",
    "        self.add_output('y', 3.0)\n",
    "\n",
    "    def initialize(self):\n",
    "        self.options.declare('length', default=(12.0, 'inch'),\n",
    "                             set_function=units_setter)\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        length = self.options['length'][0]\n",
    "\n",
    "        x = inputs['x']\n",
    "        outputs['y'] = length * x\n",
    "\n",
    "\n",
    "class MySubgroup(om.Group):\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_subsystem('mass', MyComp())\n",
    "\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('statics', MySubgroup())\n",
    "\n",
    "prob.model_options['*'] = {'length': (2.0, 'ft')}\n",
    "prob.setup()\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print('The following should be 72 if the units convert correctly.')\n",
    "print(prob.get_val('statics.mass.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "42b5fc9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:43:18.042286Z",
     "iopub.status.busy": "2026-10-02T14:43:18.042097Z",
     "iopub.status.idle": "2026-10-02T14:43:18.045271Z",
     "shell.execute_reply": "2026-10-02T14:43:18.044671Z"
    },
    "papermill": {
     "duration": 0.007383,
     "end_time": "2026-10-02T14:43:18.045661+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:43:18.038278+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-output",
     "remove-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(1.9737298215558337e-16)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "assert_near_equal(prob.get_val('statics.mass.y'), 72, 1e-6)\n"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 5.003755,
   "end_time": "2026-10-02T14:43:18.564396+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/features/core_features/options/options.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/features/core_features/options/options.ipynb",
   "parameters": {},
   "start_time": "2026-10-02T14:43:13.560641+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}