.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/types/plot_auto_py_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_auto_py_discipline.py: Create a discipline from a Python function ========================================== .. GENERATED FROM PYTHON SOURCE LINES 27-29 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 29-39 .. code-block:: Python from __future__ import annotations from numpy import array from numpy import empty from gemseo import configure_logger from gemseo import create_discipline configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 40-43 Build a discipline from a simple Python function ------------------------------------------------ Let's consider a simple Python function, e.g.: .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python def f(x=0.0, y=0.0): """A simple Python function.""" z = x + 2 * y return z .. GENERATED FROM PYTHON SOURCE LINES 52-59 Create and instantiate the discipline ------------------------------------- Then, we can consider the :class:`.AutoPyDiscipline` class to convert it into an :class:`.MDODiscipline`. For that, we can use the :func:`.create_discipline` API function with ``'AutoPyDiscipline'`` as first argument: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python disc = create_discipline("AutoPyDiscipline", py_func=f) .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 13:58:15: Discipline f: py_func has inconsistent type hints: either both the signature arguments and the return values shall have type hints or none. The grammars will not use the type hints at all. .. GENERATED FROM PYTHON SOURCE LINES 62-67 The original Python function may or may not include default values for input arguments, however, if the resulting :class:`.AutoPyDiscipline` is going to be placed inside an :class:`.MDF`, a :class:`.BiLevel` formulation or an :class:`.MDA` with strong couplings, then the Python function **must** assign default values for its input arguments. .. GENERATED FROM PYTHON SOURCE LINES 69-72 Execute the discipline ---------------------- Then, we can execute it easily, either considering default inputs: .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python disc.execute() .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': array([0.]), 'y': array([0.]), 'z': array([0.])} .. GENERATED FROM PYTHON SOURCE LINES 75-76 or using new inputs: .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python disc.execute({"x": array([1.0]), "y": array([-3.2])}) .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': array([1.]), 'y': array([-3.2]), 'z': array([-5.4])} .. GENERATED FROM PYTHON SOURCE LINES 79-89 Optional arguments ------------------ The optional arguments passed to the constructor are: - ``py_jac=None``: pointer to the jacobian function which must returned a 2D numpy array (see below), - ``use_arrays=False``: if ``True``, the function is expected to take arrays as inputs and give outputs as arrays, - ``write_schema=False``: if ``True``, write the json schema on the disk. .. GENERATED FROM PYTHON SOURCE LINES 91-94 Define the jacobian function ---------------------------- Here is an example of jacobian function: .. GENERATED FROM PYTHON SOURCE LINES 94-104 .. code-block:: Python def dfdxy(x=0.0, y=0.0): """Jacobian function of f.""" jac = empty((2, 1)) jac[0, 0] = 1 jac[1, 0] = 2 return jac .. GENERATED FROM PYTHON SOURCE LINES 105-106 that we can execute with default inputs for example: .. GENERATED FROM PYTHON SOURCE LINES 106-107 .. code-block:: Python dfdxy() .. rst-class:: sphx-glr-script-out .. code-block:: none array([[1.], [2.]]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.008 seconds) .. _sphx_glr_download_examples_disciplines_types_plot_auto_py_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_auto_py_discipline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_auto_py_discipline.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_