.. 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 Click :ref:`here ` 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:: /tutorials/_description/sellar_problem_definition.inc ############################################################################# Imports ------- All the imports needed for the tutorials are performed here. .. GENERATED FROM PYTHON SOURCE LINES 43-54 .. code-block:: default from gemseo.algos.design_space import DesignSpace from gemseo.api import configure_logger from gemseo.api import create_discipline from gemseo.api import create_scenario from matplotlib import pyplot as plt from numpy import array from numpy import ones configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 55-63 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 :meth:`~gemseo.api.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 63-65 .. code-block:: default disciplines = create_discipline(["Sellar1", "Sellar2", "SellarSystem"]) .. GENERATED FROM PYTHON SOURCE LINES 66-73 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 73-79 .. code-block:: default design_space = DesignSpace() design_space.add_variable("x_local", 1, 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 80-85 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 85-89 .. code-block:: default scenario = create_scenario( disciplines, formulation="MDF", objective_name="obj", design_space=design_space ) .. GENERATED FROM PYTHON SOURCE LINES 90-94 Add the constraints ^^^^^^^^^^^^^^^^^^^ Then, we have to set the design constraints .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: default scenario.add_constraint("c_1", "ineq") scenario.add_constraint("c_2", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 98-107 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 107-108 .. code-block:: default scenario.add_observable("y_1") .. GENERATED FROM PYTHON SOURCE LINES 109-111 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 111-113 .. code-block:: default scenario.add_observable("y_2", observable_name="y2") .. GENERATED FROM PYTHON SOURCE LINES 114-120 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 120-122 .. code-block:: default scenario.execute(input_data={"max_iter": 10, "algo": "SLSQP"}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 07:17:21: INFO - 07:17:21: *** Start MDOScenario execution *** INFO - 07:17:21: MDOScenario INFO - 07:17:21: Disciplines: Sellar1 Sellar2 SellarSystem INFO - 07:17:21: MDO formulation: MDF INFO - 07:17:21: Optimization problem: INFO - 07:17:21: minimize obj(x_local, x_shared) INFO - 07:17:21: with respect to x_local, x_shared INFO - 07:17:21: subject to constraints: INFO - 07:17:21: c_1(x_local, x_shared) <= 0.0 INFO - 07:17:21: c_2(x_local, x_shared) <= 0.0 INFO - 07:17:21: over the design space: INFO - 07:17:21: +----------+-------------+-------+-------------+-------+ INFO - 07:17:21: | name | lower_bound | value | upper_bound | type | INFO - 07:17:21: +----------+-------------+-------+-------------+-------+ INFO - 07:17:21: | x_local | 0 | 1 | 10 | float | INFO - 07:17:21: | x_shared | -10 | 4 | 10 | float | INFO - 07:17:21: | x_shared | 0 | 3 | 10 | float | INFO - 07:17:21: +----------+-------------+-------+-------------+-------+ INFO - 07:17:21: Solving optimization problem with algorithm SLSQP: INFO - 07:17:21: ... 0%| | 0/10 [00:00` .. 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 `_