{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Hybrid Jacobi/Newton MDA\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from gemseo.api import configure_logger\nfrom gemseo.api import create_discipline\nfrom gemseo.api import create_mda\nfrom matplotlib import pyplot as plt\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Define a way to display results\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def display_result(res, mda_name):\n    \"\"\"Display coupling and output variables in logger.\n\n    @param res: result (dict) of MDA\n    @param mda_name: name of the current MDA\n    \"\"\"\n    # names of the coupling variables\n    coupling_names = [\n        \"y_11\",\n        \"y_12\",\n        \"y_14\",\n        \"y_21\",\n        \"y_23\",\n        \"y_24\",\n        \"y_31\",\n        \"y_32\",\n        \"y_34\",\n    ]\n    for coupling_var in coupling_names:\n        print(\n            \"{}, coupling variable {}: {}\".format(\n                mda_name, coupling_var, res[coupling_var]\n            ),\n        )\n\n    # names of the output variables\n    output_names = [\"y_1\", \"y_2\", \"y_3\", \"y_4\", \"g_1\", \"g_2\", \"g_3\"]\n    for output_name in output_names:\n        print(\n            f\"{mda_name}, output variable {output_name}: {res[output_name]}\",\n        )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create, execute and post-process MDA\nWe do not need to specify the inputs, the default inputs\nof the MDA will be used and computed from the\nDefault inputs of the disciplines\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "disciplines = create_discipline(\n    [\n        \"SobieskiStructure\",\n        \"SobieskiPropulsion\",\n        \"SobieskiAerodynamics\",\n        \"SobieskiMission\",\n    ]\n)\nmda1 = create_mda(\"MDAJacobi\", disciplines, max_mda_iter=1)\nmda2 = create_mda(\"MDANewtonRaphson\", disciplines)\nmda_sequence = [mda1, mda2]\nmda = create_mda(\"MDASequential\", disciplines, mda_sequence=mda_sequence)\nres = mda.execute()\ndisplay_result(res, mda.name)\nmda.plot_residual_history(\n    n_iterations=10, logscale=[1e-8, 10.0], show=False, save=False, fig_size=(10, 2)\n)\n# Workaround for HTML rendering, instead of ``show=True``\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "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.9.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}