.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/discipline/plot_autopydiscipline.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_autopydiscipline.py: Create a discipline from a python function ========================================== .. 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-42 .. code-block:: default from numpy import array, empty from gemseo.api import configure_logger, create_discipline configure_logger() standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 43-46 Build a discipline from a simple python function ------------------------------------------------ Let's consider a simple python function, e.g.: .. GENERATED FROM PYTHON SOURCE LINES 46-54 .. code-block:: default def f(x=0.0, y=0.0): """A simple python function""" z = x + 2 * y return z .. GENERATED FROM PYTHON SOURCE LINES 55-62 Create and instantiate the discipline ------------------------------------- Then, we can consider the :class:`.AutoPyDiscipline` class to convert it into a :class:`.MDODiscipline`. For that, we can use the :meth:`~gemseo.api.create_discipline` API function with :code:`'AutoPyDiscipline'` as first argument: .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: default disc = create_discipline("AutoPyDiscipline", py_func=f) .. GENERATED FROM PYTHON SOURCE LINES 65-68 Execute the discipline ---------------------- Then, we can execute it easily, either considering default inputs: .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: default print(disc.execute()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': array([0.]), 'y': array([0.]), 'z': array([0.])} .. GENERATED FROM PYTHON SOURCE LINES 71-72 or using new inputs: .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: default print(disc.execute({"x": array([1.0]), "y": array([-3.2])})) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': array([1.]), 'y': array([-3.2]), 'z': array([-5.4])} .. GENERATED FROM PYTHON SOURCE LINES 75-85 Optional arguments ------------------ The optional arguments passed to the constructor are: - :code:`py_jac=None`: pointer to the jacobian function which must returned a 2D numpy array (see below), - :code:`use_arrays=False`: if :code:`True`, the function is expected to take arrays as inputs and give outputs as arrays, - :code:`write_schema=False`: if :code:`True`, write the json schema on the disk. .. GENERATED FROM PYTHON SOURCE LINES 87-90 Define the jacobian function ---------------------------- Here is an example of jacobian function: .. GENERATED FROM PYTHON SOURCE LINES 90-100 .. code-block:: default 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 101-102 that we can execute with default inputs for example: .. GENERATED FROM PYTHON SOURCE LINES 102-103 .. code-block:: default print(dfdxy()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[1.] [2.]] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.511 seconds) .. _sphx_glr_download_examples_discipline_plot_autopydiscipline.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_autopydiscipline.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_autopydiscipline.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_