 {
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting Started\n",
    "\n",
    "Installation Instructions:\n",
    "\n",
    "From your python environment (we recommend [Anaconda](https://www.anaconda.com/distribution)), just type:\n",
    "\n",
    "``` bash\n",
    "pip install 'openmdao[all]'\n",
    "```\n",
    "\n",
    "```{note}\n",
    "\n",
    "The [all] suffix to the install command ensures that you get all the optional dependencies\n",
    "(e.g. for testing and visualization).  You can omit this for a minimal installation.\n",
    "\n",
    "The quotation marks are required to prevent some command shells (e.g. zsh) from trying to interpret\n",
    "the square brackets.\n",
    "```\n",
    "\n",
    "Alternatively, in a Jupyter notebook environment such as [Google Colab](https://colab.research.google.com),\n",
    "you can install OpenMDAO by running the above as a shell command (precede it with !).\n",
    "\n",
    "The examples in this documentation will do this for you using the following block. This simply installs OpenMDAO if it is not already available. This is not typically necessary if you're running OpenMDAO on your local computer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "tags": [
     "active-ipynb",
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    import openmdao.api as om\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]\n",
    "    import openmdao.api as om\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sample Optimization File\n",
    "\n",
    "With OpenMDAO installed, let's try out a simple example, to get you started running your first optimization.\n",
    "Copy the following code into a file named paraboloid_min.py:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "# build the model\n",
    "prob = om.Problem()\n",
    "\n",
    "prob.model.add_subsystem('paraboloid', om.ExecComp('f = (x-3)**2 + x*y + (y+4)**2 - 3'))\n",
    "\n",
    "# setup the optimization\n",
    "prob.driver = om.ScipyOptimizeDriver()\n",
    "prob.driver.options['optimizer'] = 'SLSQP'\n",
    "\n",
    "prob.model.add_design_var('paraboloid.x', lower=-50, upper=50)\n",
    "prob.model.add_design_var('paraboloid.y', lower=-50, upper=50)\n",
    "prob.model.add_objective('paraboloid.f')\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "# Set initial values.\n",
    "prob.set_val('paraboloid.x', 3.0)\n",
    "prob.set_val('paraboloid.y', -4.0)\n",
    "\n",
    "# run the optimization\n",
    "prob.run_driver();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, to run the file, simply type:\n",
    "\n",
    "``` bash\n",
    ">> python paraboloid_min.py\n",
    "```\n",
    "\n",
    "If all works as planned, results should appear as such:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "# build the model\n",
    "prob = om.Problem()\n",
    "\n",
    "prob.model.add_subsystem('paraboloid', om.ExecComp('f = (x-3)**2 + x*y + (y+4)**2 - 3'))\n",
    "\n",
    "# setup the optimization\n",
    "prob.driver = om.ScipyOptimizeDriver()\n",
    "prob.driver.options['optimizer'] = 'SLSQP'\n",
    "\n",
    "prob.model.add_design_var('paraboloid.x', lower=-50, upper=50)\n",
    "prob.model.add_design_var('paraboloid.y', lower=-50, upper=50)\n",
    "prob.model.add_objective('paraboloid.f')\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "# Set initial values.\n",
    "prob.set_val('paraboloid.x', 3.0)\n",
    "prob.set_val('paraboloid.y', -4.0)\n",
    "\n",
    "# run the optimization\n",
    "prob.run_driver();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "# This block of code tests that the documented code functions as\n",
    "# expected and is hidden by default.\n",
    "\n",
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "# minimum value\n",
    "assert_near_equal(prob.get_val('paraboloid.f'), -27.33333, 1e-6)\n",
    "\n",
    "# location of the minimum\n",
    "assert_near_equal(prob.get_val('paraboloid.x'), 6.6667, 1e-4)\n",
    "assert_near_equal(prob.get_val('paraboloid.y'), -7.33333, 1e-4)"
   ]
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
