{
 "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": [
    "# ResponseSurface\n",
    "\n",
    "Surrogate Model based on second order response surface equations.\n",
    "\n",
    "Here is a simple example where the *ResponseSurface* surrogate is used to approximate the output of a sinusoidal component."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "\n",
    "sin_mm = om.MetaModelUnStructuredComp()\n",
    "sin_mm.add_input('x', 2.1)\n",
    "sin_mm.add_output('f_x', 0., surrogate=om.ResponseSurface())\n",
    "\n",
    "prob.model.add_subsystem('sin_mm', sin_mm)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "# train the surrogate and check predicted value\n",
    "sin_mm.options['train_x'] = np.linspace(0, 3.14, 20)\n",
    "sin_mm.options['train_f_x'] = .5*np.sin(sin_mm.options['train_x'])\n",
    "\n",
    "prob.set_val('sin_mm.x', 2.1)\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('sin_mm.f_x'))"
   ]
  },
  {
   "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('sin_mm.f_x'), .5*np.sin(prob.get_val('sin_mm.x')), 2e-3)"
   ]
  }
 ],
 "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
}
