{
 "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": [
    "# IndepVarComp\n",
    "\n",
    "An *IndepVarComp* is used to define independent variables.\n",
    "\n",
    "Independent variables are those that are set externally to the model—therefore, they are called model inputs. From the perspective of a component, they are component outputs that do not depend on any component inputs. From the perspective of a model, they can be viewed as design variables or model parameters that are set by the user or driver, prior to running the model.\n",
    "\n",
    "In general, you no longer have to define these because OpenMDAO defines and uses them automatically for all unconnected inputs in your model. However, there are some special cases where an IndepVarComp is required (see [Distributed Components](distributed_components.ipynb).\n",
    "\n",
    "The *IndepVarComp* class is instantiated directly (without defining a subclass). The name, initial value, and other options of the independent variable(s) to be declared can be either passed in during instantiation, or declared via the `add_output` method.\n",
    "\n",
    "## IndepVarComp Constructor\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.core.indepvarcomp.IndepVarComp.__init__\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "## Method Signature\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.core.indepvarcomp.IndepVarComp.add_output\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "## Usage\n",
    "\n",
    "1\\. Define one independent variable and set its value.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Define one independent variable and set its value.\"\"\"\n",
    "import openmdao.api as om\n",
    "\n",
    "comp = om.IndepVarComp('indep_var')\n",
    "prob = om.Problem(comp).setup()\n",
    "\n",
    "print(prob.get_val('indep_var'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prob.set_val('indep_var', 2.0)\n",
    "print(prob.get_val('indep_var'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "assert_near_equal(prob.get_val('indep_var'), 2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2\\. Define one independent variable with a default value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Define one independent variable with a default value.\"\"\"\n",
    "import openmdao.api as om\n",
    "\n",
    "comp = om.IndepVarComp('indep_var', val=2.0)\n",
    "prob = om.Problem(comp).setup()\n",
    "\n",
    "print(prob.get_val('indep_var'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_near_equal(prob.get_val('indep_var'), 2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3\\. Define one independent variable with a default value and additional options."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Define one independent variable with a default value and additional options.\"\"\"\n",
    "import openmdao.api as om\n",
    "\n",
    "comp = om.IndepVarComp('indep_var', val=2.0, units='m', lower=0, upper=10)\n",
    "prob = om.Problem(comp).setup()\n",
    "\n",
    "print(prob.get_val('indep_var'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_near_equal(prob.get_val('indep_var'), 2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4\\. Define one independent array variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Define one independent array variable.\"\"\"\n",
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "array = np.array([\n",
    "    [1., 2.],\n",
    "    [3., 4.],\n",
    "])\n",
    "\n",
    "comp = om.IndepVarComp('indep_var', val=array)\n",
    "prob = om.Problem(comp).setup()\n",
    "\n",
    "print(prob.get_val('indep_var'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_near_equal(prob.get_val('indep_var'), array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5\\. Define two independent variables using the `add_output` method with additional options."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Define two independent variables using the add_output method.\"\"\"\n",
    "import openmdao.api as om\n",
    "\n",
    "comp = om.IndepVarComp()\n",
    "comp.add_output('indep_var_1', val=1.0)\n",
    "comp.add_output('indep_var_2', val=2.0)\n",
    "\n",
    "prob = om.Problem(comp).setup()\n",
    "\n",
    "print(prob.get_val('indep_var_1'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(prob.get_val('indep_var_2'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_near_equal(prob.get_val('indep_var_1'), 1.0)\n",
    "assert_near_equal(prob.get_val('indep_var_2'), 2.0)"
   ]
  }
 ],
 "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
}
