.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mdo/plot_gemseo_in_10_minutes.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_mdo_plot_gemseo_in_10_minutes.py: GEMSEO in 10 minutes ==================== .. GENERATED FROM PYTHON SOURCE LINES 27-38 Introduction ------------ This is a short introduction to |g|, geared mainly for new users. In this example, we will set up a simple Multi-disciplinary Design Optimization (:term:`MDO`) problem based on a simple analytic problem. Imports ------- First, we will import all the classes and functions needed for the tutorials. .. GENERATED FROM PYTHON SOURCE LINES 39-52 .. code-block:: Python from __future__ import annotations from math import exp from numpy import array from numpy import ones from gemseo import create_design_space from gemseo import create_discipline from gemseo import create_scenario from gemseo import generate_n2_plot from gemseo.settings.mda import MDAChain_Settings .. GENERATED FROM PYTHON SOURCE LINES 53-74 These imports are needed to compute mathematical expressions and to instantiate NumPy arrays. NumPy arrays are used to store numerical data in |g| at a low level. If you are not comfortable using NumPy, please have a look at the `Numpy Quickstart tutorial `_. A simple MDO test case: the Sellar Problem ------------------------------------------ We will consider in this example the Sellar's problem: .. include:: /problems/sellar_problem_definition.inc Definition of the disciplines using Python functions ---------------------------------------------------- The Sellar's problem is composed of two :term:`disciplines ` and an :term:`objective function`. As they are expressed analytically, it is possible to write them as simple Python functions which take as parameters the :term:`design variables` and the :term:`coupling variables`. The returned values may be the outputs of a discipline, the values of the :term:`constraints` or the value of the objective function. Their definitions read: .. GENERATED FROM PYTHON SOURCE LINES 74-125 .. code-block:: Python def f_sellar_system(x_local=1.0, x_shared_2=3.0, y_1=1.0, y_2=1.0): """Compute the values of the objective and constraints. Args: x_local: The value of the local design variable. x_shared_2: The value of the second shared design variable. y_1: The value of the first coupling variable. y_2: The value of the second coupling variable. Returns: The values of the objective and constraints. """ obj = x_local**2 + x_shared_2 + y_1**2 + exp(-y_2) c_1 = 3.16 - y_1**2 c_2 = y_2 - 24.0 return obj, c_1, c_2 def f_sellar_1(x_local=1.0, y_2=1.0, x_shared_1=1.0, x_shared_2=3.0): """Compute the value of the first coupling variable. Args: x_local: The value of the local design variable. y_2: The value of the second coupling variable. x_shared_1: The value of the first shared design variable. x_shared_2: The value of the second shared design variable. Returns: The value of the first coupling variable. """ y_1 = (x_shared_1**2 + x_shared_2 + x_local - 0.2 * y_2) ** 0.5 return y_1 def f_sellar_2(y_1=1.0, x_shared_1=1.0, x_shared_2=3.0): """Compute the value of the second coupling variable. Args: y_1: The value of the first coupling variable. x_shared_1: The value of the first shared design variable. x_shared_2: The value of the second shared design variable. Returns: The value of the second coupling variable. """ y_2 = abs(y_1) + x_shared_1 + x_shared_2 return y_2 .. GENERATED FROM PYTHON SOURCE LINES 126-134 These Python functions can be easily converted into |g| :class:`.Discipline` objects by using the :class:`.AutoPyDiscipline` discipline. It enables the automatic wrapping of a Python function into a |g| :class:`.Discipline` by only passing a reference to the function to be wrapped. |g| handles the wrapping and the grammar creation under the hood. The :class:`.AutoPyDiscipline` discipline can be instantiated using the :func:`.create_discipline` function from the |g| :term:`API`: .. GENERATED FROM PYTHON SOURCE LINES 134-141 .. code-block:: Python disc_sellar_system = create_discipline("AutoPyDiscipline", py_func=f_sellar_system) disc_sellar_1 = create_discipline("AutoPyDiscipline", py_func=f_sellar_1) disc_sellar_2 = create_discipline("AutoPyDiscipline", py_func=f_sellar_2) .. GENERATED FROM PYTHON SOURCE LINES 142-147 Note that it is possible to define the Sellar disciplines by subclassing the :class:`.Discipline` class and implementing the constuctor and the _run method by hand. Although it would take more time, it may also provide more flexibility and more options. This method is illustrated in the :ref:`Sellar from scratch tutorial `. .. GENERATED FROM PYTHON SOURCE LINES 149-151 We then create a list of disciplines, which will be used later to create an :class:`.MDOScenario`: .. GENERATED FROM PYTHON SOURCE LINES 151-153 .. code-block:: Python disciplines = [disc_sellar_system, disc_sellar_1, disc_sellar_2] .. GENERATED FROM PYTHON SOURCE LINES 154-156 We can quickly access the most relevant information of any discipline (name, inputs, and outputs) with their string representations: .. GENERATED FROM PYTHON SOURCE LINES 156-158 .. code-block:: Python disc_sellar_1 .. raw:: html
f_sellar_1
  • Inputs: x_local, x_shared_1, x_shared_2, y_2
  • Outputs: y_1


.. GENERATED FROM PYTHON SOURCE LINES 159-162 Moreover, we can get the default input values of a discipline with the attribute :attr:`.Discipline.default_input_data`: .. GENERATED FROM PYTHON SOURCE LINES 162-164 .. code-block:: Python disc_sellar_1.default_input_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'x_local': array([1.]), 'y_2': array([1.]), 'x_shared_1': array([1.]), 'x_shared_2': array([3.])} .. GENERATED FROM PYTHON SOURCE LINES 165-169 You may also be interested in plotting the couplings of your disciplines. A quick way of getting this information is the high-level function :func:`.generate_n2_plot`. A much more detailed explanation of coupling visualization is available :ref:`here `. .. GENERATED FROM PYTHON SOURCE LINES 169-171 .. code-block:: Python generate_n2_plot(disciplines, save=False, show=True) .. image-sg:: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_001.png :alt: plot gemseo in 10 minutes :srcset: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 172-179 .. note:: For the sake of clarity, these disciplines are overly simple. Yet, |g| enables the definition of much more complex disciplines, such as wrapping complex :term:`COTS`. Check out the other :ref:`tutorials ` and our :ref:`publications list ` for more information. .. GENERATED FROM PYTHON SOURCE LINES 181-186 Definition of the design space ------------------------------ In order to define :class:`.MDOScenario`, a :term:`design space` has to be defined by creating a :class:`.DesignSpace` object. The design space definition reads: .. GENERATED FROM PYTHON SOURCE LINES 186-199 .. code-block:: Python design_space = create_design_space() design_space.add_variable("x_local", lower_bound=0.0, upper_bound=10.0, value=ones(1)) design_space.add_variable( "x_shared_1", lower_bound=-10, upper_bound=10.0, value=array([4.0]) ) design_space.add_variable( "x_shared_2", lower_bound=0.0, upper_bound=10.0, value=array([3.0]) ) design_space.add_variable("y_1", lower_bound=-100.0, upper_bound=100.0, value=ones(1)) design_space.add_variable("y_2", lower_bound=-100.0, upper_bound=100.0, value=ones(1)) design_space .. raw:: html
Design space:
Name Lower bound Value Upper bound Type
x_local 0 1 10 float
x_shared_1 -10 4 10 float
x_shared_2 0 3 10 float
y_1 -100 1 100 float
y_2 -100 1 100 float


.. GENERATED FROM PYTHON SOURCE LINES 200-209 Definition of the MDO scenario ------------------------------ Once the disciplines and the design space have been defined, we can create our MDO scenario by using the high-level function :func:`.create_scenario`. In this simple example, we are using a Multiple Disciplinary Feasible (:term:`MDF`) strategy. The Multiple Disciplinary Analyses (:term:`MDA`) are carried out using the Gauss-Seidel method. The scenario definition reads: .. GENERATED FROM PYTHON SOURCE LINES 209-218 .. code-block:: Python scenario = create_scenario( disciplines, "obj", design_space, formulation_name="MDF", main_mda_settings=MDAChain_Settings(inner_mda_name="MDAGaussSeidel"), ) .. GENERATED FROM PYTHON SOURCE LINES 219-243 It can be noted that neither a :term:`workflow ` nor a :term:`dataflow ` has been defined. By design, there is no need to explicitly define the workflow and the dataflow in |g|: - the workflow is determined from the MDO formulation used. - the dataflow is determined from the variable names used in the disciplines. Then, it is of uttermost importance to be consistent while choosing and using the variable names in the disciplines. .. warning:: As the workflow and the dataflow are implicitly determined by |g|, set-up errors may easily occur. Although it is not performed in this example, it is strongly advised to - check the interfaces between the several disciplines using an N2 diagram, - check the MDO process using an XDSM representation Setting the constraints ----------------------- Most of the MDO problems are under :term:`constraints`. In our problem, we have two inequality constraints, and their declaration reads: .. GENERATED FROM PYTHON SOURCE LINES 243-247 .. code-block:: Python scenario.add_constraint("c_1", constraint_type="ineq") scenario.add_constraint("c_2", constraint_type="ineq") .. GENERATED FROM PYTHON SOURCE LINES 248-255 Execution of the scenario ------------------------- The scenario is now complete and ready to be executed. When running the optimization process, the user can choose the optimization algorithm and the maximum number of iterations to perform. The execution of the scenario reads: .. GENERATED FROM PYTHON SOURCE LINES 255-258 .. code-block:: Python scenario.execute(algo_name="SLSQP", max_iter=10) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:25:05: *** Start MDOScenario execution *** INFO - 16:25:05: MDOScenario INFO - 16:25:05: Disciplines: f_sellar_1 f_sellar_2 f_sellar_system INFO - 16:25:05: MDO formulation: MDF INFO - 16:25:05: Optimization problem: INFO - 16:25:05: minimize obj(x_local, x_shared_1, x_shared_2) INFO - 16:25:05: with respect to x_local, x_shared_1, x_shared_2 INFO - 16:25:05: under the inequality constraints INFO - 16:25:05: c_1(x_local, x_shared_1, x_shared_2) <= 0 INFO - 16:25:05: c_2(x_local, x_shared_1, x_shared_2) <= 0 INFO - 16:25:05: over the design space: INFO - 16:25:05: +------------+-------------+-------+-------------+-------+ INFO - 16:25:05: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:25:05: +------------+-------------+-------+-------------+-------+ INFO - 16:25:05: | x_local | 0 | 1 | 10 | float | INFO - 16:25:05: | x_shared_1 | -10 | 4 | 10 | float | INFO - 16:25:05: | x_shared_2 | 0 | 3 | 10 | float | INFO - 16:25:05: +------------+-------------+-------+-------------+-------+ INFO - 16:25:05: Solving optimization problem with algorithm SLSQP: INFO - 16:25:05: 10%|█ | 1/10 [00:00<00:00, 66.91 it/sec, feas=True, obj=21.8] INFO - 16:25:05: 20%|██ | 2/10 [00:00<00:00, 88.83 it/sec, feas=True, obj=5.39] INFO - 16:25:05: 30%|███ | 3/10 [00:00<00:00, 100.65 it/sec, feas=True, obj=3.41] INFO - 16:25:05: 40%|████ | 4/10 [00:00<00:00, 107.06 it/sec, feas=True, obj=3.19] INFO - 16:25:05: 50%|█████ | 5/10 [00:00<00:00, 111.32 it/sec, feas=True, obj=3.18] INFO - 16:25:05: 60%|██████ | 6/10 [00:00<00:00, 114.45 it/sec, feas=True, obj=3.18] INFO - 16:25:05: 70%|███████ | 7/10 [00:00<00:00, 113.47 it/sec, feas=True, obj=3.18] INFO - 16:25:05: 80%|████████ | 8/10 [00:00<00:00, 126.04 it/sec, feas=True, obj=3.18] INFO - 16:25:05: Optimization result: INFO - 16:25:05: Optimizer info: INFO - 16:25:05: Status: None INFO - 16:25:05: Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO stopped the driver. INFO - 16:25:05: Solution: INFO - 16:25:05: The solution is feasible. INFO - 16:25:05: Objective: 3.1833939510323312 INFO - 16:25:05: Standardized constraints: INFO - 16:25:05: c_1 = 1.1344365447030214e-09 INFO - 16:25:05: c_2 = -20.24472223811599 INFO - 16:25:05: Design space: INFO - 16:25:05: +------------+-------------+-----------------------+-------------+-------+ INFO - 16:25:05: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:25:05: +------------+-------------+-----------------------+-------------+-------+ INFO - 16:25:05: | x_local | 0 | 0 | 10 | float | INFO - 16:25:05: | x_shared_1 | -10 | 1.97763887833178 | 10 | float | INFO - 16:25:05: | x_shared_2 | 0 | 4.081962102940113e-10 | 10 | float | INFO - 16:25:05: +------------+-------------+-----------------------+-------------+-------+ INFO - 16:25:05: *** End MDOScenario execution *** .. GENERATED FROM PYTHON SOURCE LINES 259-267 The scenario converged after 7 iterations. Useful information can be found in the standard output, as seen above. .. note:: |g| provides the user with a lot of optimization algorithms and options. An exhaustive list of the algorithms available in |g| can be found in the :ref:`gen_opt_algos` section. .. GENERATED FROM PYTHON SOURCE LINES 269-277 Post-processing the results --------------------------- Post-processors such as plots exhibiting the evolutions of the objective function, the design variables or the constraints can be extremely useful. The convergence of the objective function, design variables and of the inequality constraints can be observed in the following plots. Many other post-processors are available in |g| and are described in :ref:`Post-processing `. .. GENERATED FROM PYTHON SOURCE LINES 277-280 .. code-block:: Python scenario.post_process(post_name="OptHistoryView", save=False, show=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_002.png :alt: Evolution of the optimization variables :srcset: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_003.png :alt: Evolution of the objective value :srcset: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_004.png :alt: Evolution of the distance to the optimum :srcset: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_005.png :alt: Evolution of the inequality constraints :srcset: /examples/mdo/images/sphx_glr_plot_gemseo_in_10_minutes_005.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 281-286 .. note:: Such post-processors can be exported in PDF format, by setting ``save`` to ``True`` and potentially additional settings (see the :meth:`.Scenario.post_process` options). .. GENERATED FROM PYTHON SOURCE LINES 288-293 Exporting the problem data. --------------------------- After the execution of the scenario, you may want to export your data to use it elsewhere. The :meth:`.Scenario.to_dataset` will allow you to export your results to a :class:`.Dataset`, the basic |g| class to store data. .. GENERATED FROM PYTHON SOURCE LINES 293-295 .. code-block:: Python dataset = scenario.to_dataset("a_name_for_my_dataset") .. GENERATED FROM PYTHON SOURCE LINES 296-302 What's next? ------------ You have completed a short introduction to |g|. You can now look at the :ref:`tutorials ` which exhibit more complex use-cases. You can also have a look at the documentation to discover the several features and options of |g|. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.527 seconds) .. _sphx_glr_download_examples_mdo_plot_gemseo_in_10_minutes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gemseo_in_10_minutes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gemseo_in_10_minutes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gemseo_in_10_minutes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_