.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/scenario/plot_store_observables.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_scenario_plot_store_observables.py: Store observables ================= .. GENERATED FROM PYTHON SOURCE LINES 25-43 Introduction ------------ In this example, we will learn how to store the history of state variables using the :meth:`~gemseo.core.scenario.Scenario.add_observable` method. This is useful in situations where we wish to access, post-process, or save the values of discipline outputs that are not design variables, constraints or objective functions. The Sellar problem ------------------ We will consider in this example the Sellar problem: .. include:: /problems/sellar_problem_definition.inc Imports ------- All the imports needed for the tutorials are performed here. .. GENERATED FROM PYTHON SOURCE LINES 43-55 .. code-block:: default from __future__ import annotations from gemseo import configure_logger from gemseo import create_discipline from gemseo import create_scenario from gemseo.algos.design_space import DesignSpace from numpy import array from numpy import ones configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 56-64 Create the problem disciplines ------------------------------ In this section, we use the available classes :class:`.Sellar1`, :class:`.Sellar2` and :class:`.SellarSystem` to define the disciplines of the problem. The :func:`.create_discipline` API function allows us to carry out this task easily, as well as store the instances in a list to be used later on. .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: default disciplines = create_discipline(["Sellar1", "Sellar2", "SellarSystem"]) .. GENERATED FROM PYTHON SOURCE LINES 67-74 Create and execute the scenario ------------------------------- Create the design space ^^^^^^^^^^^^^^^^^^^^^^^ In this section, we define the design space which will be used for the creation of the MDOScenario. .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: default design_space = DesignSpace() design_space.add_variable("x_local", l_b=0.0, u_b=10.0, value=ones(1)) design_space.add_variable( "x_shared", 2, l_b=(-10, 0.0), u_b=(10.0, 10.0), value=array([4.0, 3.0]) ) .. GENERATED FROM PYTHON SOURCE LINES 81-86 Create the scenario ^^^^^^^^^^^^^^^^^^^ In this section, we build the MDO scenario which links the disciplines with the formulation, the design space and the objective function. .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: default scenario = create_scenario( disciplines, formulation="MDF", objective_name="obj", design_space=design_space ) .. GENERATED FROM PYTHON SOURCE LINES 91-95 Add the constraints ^^^^^^^^^^^^^^^^^^^ Then, we have to set the design constraints .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: default scenario.add_constraint("c_1", "ineq") scenario.add_constraint("c_2", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 99-108 Add the observables ^^^^^^^^^^^^^^^^^^^ Only the design variables, objective function and constraints are stored by default. In order to be able to recover the data from the state variables, y1 and y2, we have to add them as observables. All we have to do is enter the variable name as a string to the :meth:`~gemseo.core.scenario.Scenario.add_observable` method. If more than one output name is provided (as a list of strings), the observable function returns a concatenated array of the output values. .. GENERATED FROM PYTHON SOURCE LINES 108-109 .. code-block:: default scenario.add_observable("y_1") .. GENERATED FROM PYTHON SOURCE LINES 110-112 It is also possible to add the observable with a custom name, using the option `observable_name`. Let us store the variable `y_2` as `y2`. .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: default scenario.add_observable("y_2", observable_name="y2") .. GENERATED FROM PYTHON SOURCE LINES 115-121 Execute the scenario ^^^^^^^^^^^^^^^^^^^^ Then, we execute the MDO scenario with the inputs of the MDO scenario as a dictionary. In this example, the gradient-based `SLSQP` optimizer is selected, with 10 iterations at maximum: .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: default scenario.execute(input_data={"max_iter": 10, "algo": "SLSQP"}) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:25:55: INFO - 16:25:55: *** Start MDOScenario execution *** INFO - 16:25:55: MDOScenario INFO - 16:25:55: Disciplines: Sellar1 Sellar2 SellarSystem INFO - 16:25:55: MDO formulation: MDF INFO - 16:25:55: Optimization problem: INFO - 16:25:55: minimize obj(x_local, x_shared) INFO - 16:25:55: with respect to x_local, x_shared INFO - 16:25:55: subject to constraints: INFO - 16:25:55: c_1(x_local, x_shared) <= 0.0 INFO - 16:25:55: c_2(x_local, x_shared) <= 0.0 INFO - 16:25:55: over the design space: INFO - 16:25:55: +-------------+-------------+-------+-------------+-------+ INFO - 16:25:55: | name | lower_bound | value | upper_bound | type | INFO - 16:25:55: +-------------+-------------+-------+-------------+-------+ INFO - 16:25:55: | x_local | 0 | 1 | 10 | float | INFO - 16:25:55: | x_shared[0] | -10 | 4 | 10 | float | INFO - 16:25:55: | x_shared[1] | 0 | 3 | 10 | float | INFO - 16:25:55: +-------------+-------------+-------+-------------+-------+ INFO - 16:25:55: Solving optimization problem with algorithm SLSQP: INFO - 16:25:55: ... 0%| | 0/10 [00:00 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.003 seconds) .. _sphx_glr_download_examples_scenario_plot_store_observables.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_store_observables.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_store_observables.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_