.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/basics/plot_compute_jacobian_fd.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_basics_plot_compute_jacobian_fd.py: Compute the Jacobian of a discipline with finite differences ============================================================ In this example, we will compute the Jacobians of some outputs of an :class:`.Discipline` with respect to some inputs using the finite difference method. .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo.disciplines.auto_py import AutoPyDiscipline .. GENERATED FROM PYTHON SOURCE LINES 38-40 First, we create a discipline, e.g. an :class:`.AutoPyDiscipline`: .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: Python def f(a=0.0, b=0.0): y = a**2 + b z = a**3 + b**2 return y, z discipline = AutoPyDiscipline(f) .. GENERATED FROM PYTHON SOURCE LINES 49-50 We can execute it with its default input values: .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python discipline.execute() discipline.local_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'a': array([0.]), 'b': array([0.]), 'y': array([0.]), 'z': array([0.])} .. GENERATED FROM PYTHON SOURCE LINES 54-55 or with custom ones: .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python discipline.execute({"a": array([1.0])}) discipline.local_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'a': array([1.]), 'b': array([0.]), 'y': array([1.]), 'z': array([1.])} .. GENERATED FROM PYTHON SOURCE LINES 59-61 Then, we use the method :meth:`.Discipline.linearize` to compute the derivatives: .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python jacobian_data = discipline.linearize() jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none {} .. GENERATED FROM PYTHON SOURCE LINES 65-74 There is no Jacobian data because we need to set the input variables with respect to which to compute the Jacobian of the output ones. For that, we use the method :meth:`~.Discipline.add_differentiated_inputs`. We also need to set these output variables: with the method :meth:`~.Discipline.add_differentiated_outputs`. For instance, we may want to compute the derivative of ``"z"`` with respect to ``"a"`` only: .. GENERATED FROM PYTHON SOURCE LINES 74-79 .. code-block:: Python discipline.add_differentiated_inputs(["a"]) discipline.add_differentiated_outputs(["z"]) jacobian_data = discipline.linearize() jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'z': {'a': array([[1.e-14]])}} .. GENERATED FROM PYTHON SOURCE LINES 80-88 We can have a quick look at the values of these derivatives and verify that they are equal to the analytical results, up to the numerical precision. By default, |g| uses :attr:`.Discipline.default_input_data` as input data for which to compute the Jacobian one. We can change them with ``input_data``: .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. code-block:: Python jacobian_data = discipline.linearize({"a": array([1.0])}) jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'z': {'a': array([[3.0000003]])}} .. GENERATED FROM PYTHON SOURCE LINES 92-94 We can also force the discipline to compute the derivatives of all the outputs with respect to all the inputs: .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python jacobian_data = discipline.linearize(compute_all_jacobians=True) jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'y': {'a': array([[1.e-07]]), 'b': array([[1.]])}, 'z': {'a': array([[1.e-14]]), 'b': array([[1.e-07]])}} .. GENERATED FROM PYTHON SOURCE LINES 98-99 we can change the approximation type to complex step and compare the results: .. GENERATED FROM PYTHON SOURCE LINES 99-105 .. code-block:: Python discipline.set_jacobian_approximation( jac_approx_type=discipline.ApproximationMode.COMPLEX_STEP ) jacobian_data_complex_step = discipline.linearize(compute_all_jacobians=True) jacobian_data_complex_step .. rst-class:: sphx-glr-script-out .. code-block:: none {'y': {'a': array([[0.]]), 'b': array([[0.]])}, 'z': {'a': array([[0.]]), 'b': array([[0.]])}} .. GENERATED FROM PYTHON SOURCE LINES 106-108 Lastly, We can change the approximation type to centered differences and compare the results: .. GENERATED FROM PYTHON SOURCE LINES 108-113 .. code-block:: Python discipline.set_jacobian_approximation( jac_approx_type=discipline.ApproximationMode.CENTERED_DIFFERENCES ) jacobian_data_centered_differences = discipline.linearize(compute_all_jacobians=True) jacobian_data_centered_differences .. rst-class:: sphx-glr-script-out .. code-block:: none {'y': {'a': array([[0.]]), 'b': array([[1.]])}, 'z': {'a': array([[1.e-14]]), 'b': array([[0.]])}} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.008 seconds) .. _sphx_glr_download_examples_disciplines_basics_plot_compute_jacobian_fd.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_compute_jacobian_fd.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_compute_jacobian_fd.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_compute_jacobian_fd.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_