.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/discipline/plot_analyticdiscipline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_discipline_plot_analyticdiscipline.py: Create a discipline from analytical expressions =============================================== .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: default from gemseo.api import configure_logger from gemseo.api import create_discipline from numpy import array .. GENERATED FROM PYTHON SOURCE LINES 30-32 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: default configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-42 Introduction ------------ A simple :class:`.MDODiscipline` can be created using analytic formulas, e.g. :math:`y_1=2x^2` and :math:`y_2=5+3x^2z^3`, thanks to the :class:`.AnalyticDiscipline` class which is a quick alternative to model a simple analytic MDO problem. .. GENERATED FROM PYTHON SOURCE LINES 45-50 Create the dictionary of analytic outputs ----------------------------------------- First of all, we have to define the output expressions in a dictionary where keys are output names and values are formula with :code:`string` format: .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: default expressions = {"y_1": "2*x**2", "y_2": "5+3*x**2+z**3"} .. GENERATED FROM PYTHON SOURCE LINES 53-65 Create the discipline --------------------- Then, we create and instantiate the corresponding :class:`.AnalyticDiscipline`, which is a particular :class:`.MDODiscipline`. For that, we use the API function :meth:`~gemseo.api.create_discipline` with: - :code:`discipline_name="AnalyticDiscipline"`, - :code:`name="analytic"`, - :code:`expressions=expr_dict`. In practice, we write: .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: default disc = create_discipline("AnalyticDiscipline", expressions=expressions) .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. note:: |g| takes care of the grammars and :meth:`!MDODiscipline._run` method generation from the :code:`expressions` argument. In the background, |g| considers that :code:`x` is a monodimensional float input parameter and :code:`y_1` and :code:`y_2` are monodimensional float output parameters. .. GENERATED FROM PYTHON SOURCE LINES 78-81 Execute the discipline ---------------------- Lastly, we can execute this discipline any other: .. GENERATED FROM PYTHON SOURCE LINES 81-86 .. code-block:: default input_data = {"x": array([2.0]), "z": array([3.0])} out = disc.execute(input_data) print(("y_1 =", out["y_1"])) print(("y_2 =", out["y_2"])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ('y_1 =', array([8.])) ('y_2 =', array([44.])) .. GENERATED FROM PYTHON SOURCE LINES 87-94 About the analytic jacobian --------------------------- The discipline will provide analytic derivatives (Jacobian) automatically using the `sympy library `_. This can be checked easily using :meth:`.MDODiscipline.check_jacobian`: .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: default disc.check_jacobian( input_data, derr_approx=disc.FINITE_DIFFERENCES, step=1e-5, threshold=1e-3 ) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 07:19:10: Jacobian: dp y_1/dp x succeeded. INFO - 07:19:10: Jacobian: dp y_1/dp z succeeded. INFO - 07:19:10: Jacobian: dp y_2/dp x succeeded. INFO - 07:19:10: Jacobian: dp y_2/dp z succeeded. INFO - 07:19:10: Linearization of MDODiscipline: AnalyticDiscipline is correct. True .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.029 seconds) .. _sphx_glr_download_examples_discipline_plot_analyticdiscipline.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_analyticdiscipline.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_analyticdiscipline.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_