.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/types/plot_analytic_discipline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_disciplines_types_plot_analytic_discipline.py: Create a discipline from analytical expressions =============================================== .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo import configure_logger from gemseo import create_discipline .. GENERATED FROM PYTHON SOURCE LINES 34-36 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 40-46 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 49-54 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 ``string`` format: .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python expressions = {"y_1": "2*x**2", "y_2": "5+3*x**2+z**3"} .. GENERATED FROM PYTHON SOURCE LINES 57-69 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 :func:`.create_discipline` with: - ``discipline_name="AnalyticDiscipline"``, - ``name="analytic"``, - ``expressions=expr_dict``. In practice, we write: .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python disc = create_discipline("AnalyticDiscipline", expressions=expressions) .. GENERATED FROM PYTHON SOURCE LINES 72-80 .. note:: |g| takes care of the grammars and :meth:`!MDODiscipline._run` method generation from the ``expressions`` argument. In the background, |g| considers that ``x`` is a monodimensional float input parameter and ``y_1`` and ``y_2`` are monodimensional float output parameters. .. GENERATED FROM PYTHON SOURCE LINES 82-85 Execute the discipline ---------------------- Lastly, we can execute this discipline any other: .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python input_data = {"x": array([2.0]), "z": array([3.0])} disc.execute(input_data) .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': array([2.]), 'z': array([3.]), 'y_2': array([44.]), 'y_1': array([8.])} .. GENERATED FROM PYTHON SOURCE LINES 89-96 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 96-102 .. code-block:: Python disc.check_jacobian( input_data, derr_approx=disc.ApproximationMode.FINITE_DIFFERENCES, step=1e-5, threshold=1e-3, ) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 09:00:36: Jacobian: dp y_1/dp x succeeded. INFO - 09:00:36: Jacobian: dp y_1/dp z succeeded. INFO - 09:00:36: Jacobian: dp y_2/dp x succeeded. INFO - 09:00:36: Jacobian: dp y_2/dp z succeeded. INFO - 09:00:36: Linearization of MDODiscipline: AnalyticDiscipline is correct. True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.020 seconds) .. _sphx_glr_download_examples_disciplines_types_plot_analytic_discipline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_analytic_discipline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_analytic_discipline.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_