.. 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-29 .. code-block:: default from __future__ import division, unicode_literals .. GENERATED FROM PYTHON SOURCE LINES 30-32 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 32-39 .. code-block:: default from numpy import array, empty 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 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:: default 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 :meth:`~gemseo.api.create_discipline` API function with :code:`'AutoPyDiscipline'` as first argument: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: default disc = create_discipline("AutoPyDiscipline", py_func=f) .. GENERATED FROM PYTHON SOURCE LINES 62-65 Execute the discipline ---------------------- Then, we can execute it easily, either considering default inputs: .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. 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 68-69 or using new inputs: .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. 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 72-82 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 84-87 Define the jacobian function ---------------------------- Here is an example of jacobian function: .. GENERATED FROM PYTHON SOURCE LINES 87-97 .. 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 98-99 that we can execute with default inputs for example: .. GENERATED FROM PYTHON SOURCE LINES 99-100 .. 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.009 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 `_