.. 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 28-36 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. .. GENERATED FROM PYTHON SOURCE LINES 38-43 The Sellar problem ------------------ We will consider in this example the Sellar problem: .. include:: /tutorials/_description/sellar_problem_definition.inc .. GENERATED FROM PYTHON SOURCE LINES 45-49 Imports ------- All the imports needed for the tutorials are performed here. Note that some of the imports are related to the Python 2/3 compatibility. .. GENERATED FROM PYTHON SOURCE LINES 49-61 .. code-block:: default from __future__ import division, unicode_literals from matplotlib import pyplot as plt from numpy import array, ones from gemseo.algos.design_space import DesignSpace from gemseo.api import configure_logger, create_discipline, create_scenario configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 62-70 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 70-72 .. code-block:: default disciplines = create_discipline(["Sellar1", "Sellar2", "SellarSystem"]) .. GENERATED FROM PYTHON SOURCE LINES 73-80 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 80-86 .. 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 87-92 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 92-96 .. code-block:: default scenario = create_scenario( disciplines, formulation="MDF", objective_name="obj", design_space=design_space ) .. GENERATED FROM PYTHON SOURCE LINES 97-101 Add the constraints ^^^^^^^^^^^^^^^^^^^ Then, we have to set the design constraints .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: default scenario.add_constraint("c_1", "ineq") scenario.add_constraint("c_2", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 105-114 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 114-115 .. code-block:: default scenario.add_observable("y_1") .. GENERATED FROM PYTHON SOURCE LINES 116-118 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 118-120 .. code-block:: default scenario.add_observable("y_2", observable_name="y2") .. GENERATED FROM PYTHON SOURCE LINES 121-127 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 127-129 .. code-block:: default scenario.execute(input_data={"max_iter": 10, "algo": "SLSQP"}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 21:50:03: INFO - 21:50:03: *** Start MDO Scenario execution *** INFO - 21:50:03: MDOScenario INFO - 21:50:03: Disciplines: Sellar1 Sellar2 SellarSystem INFO - 21:50:03: MDOFormulation: MDF INFO - 21:50:03: Algorithm: SLSQP INFO - 21:50:03: Optimization problem: INFO - 21:50:03: Minimize: obj(x_local, x_shared) INFO - 21:50:03: With respect to: x_local, x_shared INFO - 21:50:03: Subject to constraints: INFO - 21:50:03: c_1(x_local, x_shared) <= 0.0 INFO - 21:50:03: c_2(x_local, x_shared) <= 0.0 INFO - 21:50:03: Design space: INFO - 21:50:03: +----------+-------------+-------+-------------+-------+ INFO - 21:50:03: | name | lower_bound | value | upper_bound | type | INFO - 21:50:03: +----------+-------------+-------+-------------+-------+ INFO - 21:50:03: | x_local | 0 | 1 | 10 | float | INFO - 21:50:03: | x_shared | -10 | 4 | 10 | float | INFO - 21:50:03: | x_shared | 0 | 3 | 10 | float | INFO - 21:50:03: +----------+-------------+-------+-------------+-------+ INFO - 21:50:03: Optimization: 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 `_