.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mda/plot_mda_settings.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_mda_plot_mda_settings.py: Create MDAs with specific settings ---------------------------------- In this example, we show how to create MDAs with specific parameters, such as the maximum number of iterations or the convergence tolerance. Let us first create two coupled disciplines, namely the :class:`.Sellar1` and :class:`.Sellar2` disciplines: .. GENERATED FROM PYTHON SOURCE LINES 27-35 .. code-block:: Python from __future__ import annotations from gemseo import create_discipline from gemseo import create_mda sellar_1, sellar_2 = create_discipline(["Sellar1", "Sellar2"]) .. GENERATED FROM PYTHON SOURCE LINES 36-43 Using key/value pairs ^^^^^^^^^^^^^^^^^^^^^ A first possibility is to use key/value pairs. In the following code, the ``tolerance`` and ``max_mda_iter`` settings are passed as key/value pairs. However, this method requires to know the right keyword for the settings, otherwise the ``create_mda`` function will raise an error. It is nevertheless relevant for users already aware of the keywords. .. GENERATED FROM PYTHON SOURCE LINES 43-52 .. code-block:: Python mda_jacobi = create_mda( "MDAJacobi", disciplines=[sellar_1, sellar_2], max_mda_iter=15, tolerance=1e-8, ) .. GENERATED FROM PYTHON SOURCE LINES 53-58 Using Pydantic settings model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The second way of providing settings for the MDA is to use the corresponding Pydantic settings model. In our example, we intend to create an :class:`.MDAJacobi`, so one needs to import the corresponding model: .. GENERATED FROM PYTHON SOURCE LINES 58-69 .. code-block:: Python from gemseo.settings.mda import MDAJacobi_Settings # noqa: E402 mda_jacobi_settings = MDAJacobi_Settings(max_mda_iter=15, tolerance=1e-8) mda_jacobi = create_mda( "MDAJacobi", disciplines=[sellar_1, sellar_2], settings_model=mda_jacobi_settings, ) .. GENERATED FROM PYTHON SOURCE LINES 70-76 Using Pydantic model to provide settings has to be prefered for two main reasons: - The settings are validated (type, value, etc) when the model is created. - The auto-complementation and documentation of the model shows all the settings available for the MDA of interest and their **default values**. The name of the Pydantic model associated with an MDA class is available via the class attribute :attr:`~.BaseMDA.Settings`. For instance: .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: Python print(mda_jacobi.Settings) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 81-86 Updating the settings --------------------- It is also possible to update the settings after the MDA object has been created. To do so, one must access the settings model attached to each mda via :attr:`~.BaseMDA.settings`. Then modifications are as simple as: .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: Python new_tolerance = 1e-12 mda_jacobi.settings.tolerance = new_tolerance .. GENERATED FROM PYTHON SOURCE LINES 91-94 It is worth knowing that the settings are validated when updated, so the following update of ``max_mda_iter`` will raise an error, as the maximum number of iterations must be a non-negative integer. .. GENERATED FROM PYTHON SOURCE LINES 94-103 .. code-block:: Python from pydantic_core import ValidationError # noqa: E402 try: mda_jacobi.settings.max_mda_iter = -2 except ValidationError as error: print(error) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 validation error for MDAJacobi_Settings max_mda_iter Input should be greater than or equal to 0 [type=greater_than_equal, input_value=-2, input_type=int] For further information visit https://errors.pydantic.dev/2.10/v/greater_than_equal .. GENERATED FROM PYTHON SOURCE LINES 104-112 Settings for composed MDAs -------------------------- In |g|, there are two MDAs that are said to be composed because they use or create inner MDAs internally, namely :class:`.MDAChain` and :class:`.MDASequential`. For such MDAs, the settings are nested, since there are settings for these two classes, and settings for the inner-MDAs. Nevertheless, there is a **cascading mechanism** that allows to update the settings of the inner-MDAs easily. Let us for instance create an ``MDAChain``: .. GENERATED FROM PYTHON SOURCE LINES 112-115 .. code-block:: Python mda_chain = create_mda("MDAChain", disciplines=[sellar_1, sellar_2]) .. GENERATED FROM PYTHON SOURCE LINES 116-118 By default, the ``MDAChain`` creates ``MDAJacobi`` instances internally. To set the tolerance of the inner-MDAs, simply do the following: .. GENERATED FROM PYTHON SOURCE LINES 118-123 .. code-block:: Python inner_mda = mda_chain.inner_mdas[0] print(f"The tolerance of the inner-MDA is {inner_mda.settings.tolerance}.") mda_chain.settings.tolerance = 1e-4 print(f"The tolerance of the inner-MDA is now {inner_mda.settings.tolerance}.") .. rst-class:: sphx-glr-script-out .. code-block:: none The tolerance of the inner-MDA is 1e-06. The tolerance of the inner-MDA is now 0.0001. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.007 seconds) .. _sphx_glr_download_examples_mda_plot_mda_settings.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mda_settings.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mda_settings.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mda_settings.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_