.. 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 27-31 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library .. GENERATED FROM PYTHON SOURCE LINES 32-34 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 34-40 .. code-block:: default from numpy import array from gemseo.api import configure_logger, create_discipline configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 41-47 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 47-51 .. code-block:: default standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 52-57 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 57-59 .. code-block:: default expressions_dict = {"y_1": "2*x**2", "y_2": "5+3*x**2+z**3"} .. GENERATED FROM PYTHON SOURCE LINES 60-72 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_dict=expr_dict`. In practice, we write: .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: default disc = create_discipline("AnalyticDiscipline", expressions_dict=expressions_dict) .. GENERATED FROM PYTHON SOURCE LINES 75-83 .. note:: |g| takes care of the grammars and :meth:`!MDODiscipline._run` method generation from the :code:`expressions_dict` 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 85-88 Execute the discipline ---------------------- Lastly, we can execute this discipline any other: .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. 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 94-101 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 101-104 .. 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 True .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.060 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 `_