{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Jacobi MDA\n\nA smart MDA that solves only strongly coupled disciplines\nand then executes the weakly coupled ones\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import absolute_import, division, unicode_literals\n\nfrom os import name as os_name\n\nfrom gemseo.api import configure_logger, create_discipline, create_mda\n\nIS_NT = os_name == \"nt\"\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    \"\"\"\n    Display coupling and output variables in logger\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_var in output_names:\n        print(\n            \"{}, output variable {}: {}\".format(mda_name, output_var, res[output_var]),\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)\nuse_threading = False\nif IS_NT:\n    use_threading = True\nmda = create_mda(\n    \"MDAJacobi\",\n    disciplines,\n    acceleration=\"m2d\",\n    warm_start=True,\n    n_processes=4,\n    use_threading=use_threading,\n)\nres = mda.execute()\ndisplay_result(res, mda.name)\nmda.plot_residual_history(\n    n_iterations=10, logscale=[1e-8, 10.0], show=True, save=False, figsize=(10, 2)\n)"
      ]
    }
  ],
  "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.8.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}