.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/scalable/scalable_tm.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_scalable_scalable_tm.py: Scalable problem of Tedford and Martins, 2010 ============================================= .. GENERATED FROM PYTHON SOURCE LINES 29-47 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library from numpy import array from numpy.random import rand from gemseo.api import generate_n2_plot from gemseo.problems.scalable.scalable_tm.core import ( TMParamSS, TMParamSSPost, TMScalableProblem, TMScalableStudy, ) from gemseo.problems.scalable.scalable_tm.design_space import TMDesignSpace from gemseo.problems.scalable.scalable_tm.disciplines import TMDiscipline, TMSystem standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 48-58 Disciplines ----------- We define two strongly coupled disciplines and a weakly coupled discipline, with: - 2 shared design parameters, - 2 local design parameters for the first discipline, - 3 local design parameters for the second discipline, - 3 coupling variables for the first discipline, - 2 coupling variables for the second discipline. .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: default sizes = {"x_shared": 2, "x_local_0": 2, "x_local_1": 3, "y_0": 3, "y_1": 2} .. GENERATED FROM PYTHON SOURCE LINES 62-68 We use any values for the coefficients and the default values of the design parameters and coupling variables. Strongly coupled disciplines ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Here is the first strongly coupled discipline. .. GENERATED FROM PYTHON SOURCE LINES 68-83 .. code-block:: default default_inputs = { "x_shared": rand(sizes["x_shared"]), "x_local_0": rand(sizes["x_local_0"]), "y_1": rand(sizes["y_1"]), } index = 0 c_shared = rand(sizes["y_0"], sizes["x_shared"]) c_local = rand(sizes["y_0"], sizes["x_local_0"]) c_coupling = {"y_1": rand(sizes["y_0"], sizes["y_1"])} disc0 = TMDiscipline(index, c_shared, c_local, c_coupling, default_inputs) print(disc0.name) print(disc0.get_input_data_names()) print(disc0.get_output_data_names()) .. GENERATED FROM PYTHON SOURCE LINES 84-89 .. code-block:: console TM_Discipline_0 dict_keys(['x_shared', 'x_local_0', 'y_1']) dict_keys(['y_0']) .. GENERATED FROM PYTHON SOURCE LINES 92-93 Here is the second one, strongly coupled with the first one. .. GENERATED FROM PYTHON SOURCE LINES 93-108 .. code-block:: default default_inputs = { "x_shared": rand(sizes["x_shared"]), "x_local_1": rand(sizes["x_local_1"]), "y_0": rand(sizes["y_0"]), } index = 1 c_shared = rand(sizes["y_1"], sizes["x_shared"]) c_local = rand(sizes["y_1"], sizes["x_local_1"]) c_coupling = {"y_0": rand(sizes["y_1"], sizes["y_0"])} disc1 = TMDiscipline(index, c_shared, c_local, c_coupling, default_inputs) print(disc1.name) print(disc1.get_input_data_names()) print(disc1.get_output_data_names()) .. GENERATED FROM PYTHON SOURCE LINES 109-114 .. code-block:: console TM_Discipline_1 dict_keys(['x_shared', 'x_local_1', 'y_0']) dict_keys(['y_1']) .. GENERATED FROM PYTHON SOURCE LINES 116-119 Weakly coupled discipline ^^^^^^^^^^^^^^^^^^^^^^^^^ Here is the discipline weakly coupled to the previous ones. .. GENERATED FROM PYTHON SOURCE LINES 119-131 .. code-block:: default c_constraint = [array([1.0, 2.0]), array([3.0, 4.0, 5.0])] default_inputs = { "x_shared": array([0.5]), "y_0": array([2.0, 3.0]), "y_1": array([4.0, 5.0, 6.0]), } system = TMSystem(c_constraint, default_inputs) print(system.name) print(system.get_input_data_names()) print(system.get_output_data_names()) .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: console TM_System dict_keys(['x_shared', 'y_0', 'y_1']) dict_keys(['obj', 'cstr_0', 'cstr_1']) .. GENERATED FROM PYTHON SOURCE LINES 139-142 Coupling chart ^^^^^^^^^^^^^^ We can represent these three disciplines by means of a N2 chart. .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: default generate_n2_plot([disc0, disc1, system], save=False, show=True) .. GENERATED FROM PYTHON SOURCE LINES 145-146 .. image:: /_images/scalable_tm/N2chart.png .. GENERATED FROM PYTHON SOURCE LINES 148-152 Design space ------------ We define the design space from the sizes of the shared design parameters, local parameters and coupling variables. .. GENERATED FROM PYTHON SOURCE LINES 152-159 .. code-block:: default n_shared = sizes["x_shared"] n_local = [sizes["x_local_0"], sizes["x_local_1"]] n_coupling = [sizes["y_0"], sizes["y_1"]] design_space = TMDesignSpace(n_shared, n_local, n_coupling) print(design_space) .. GENERATED FROM PYTHON SOURCE LINES 160-179 .. code-block:: console Design Space: +-----------+-------------+-------+-------------+-------+ | name | lower_bound | value | upper_bound | type | +-----------+-------------+-------+-------------+-------+ | x_local_0 | 0 | 0.5 | 1 | float | | x_local_0 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_shared | 0 | 0.5 | 1 | float | | x_shared | 0 | 0.5 | 1 | float | | y_0 | 0 | 0.5 | 1 | float | | y_0 | 0 | 0.5 | 1 | float | | y_0 | 0 | 0.5 | 1 | float | | y_1 | 0 | 0.5 | 1 | float | | y_1 | 0 | 0.5 | 1 | float | +-----------+-------------+-------+-------------+-------+ .. GENERATED FROM PYTHON SOURCE LINES 181-191 Scalable problem ---------------- We define a scalable problem based on two strongly coupled disciplines and a weakly one, with the following properties: - 3 shared design parameters, - 2 local design parameters for the first strongly coupled discipline, - 2 coupling variables for the first strongly coupled discipline, - 4 local design parameters for the second strongly coupled discipline, - 3 coupling variables for the second strongly coupled discipline. .. GENERATED FROM PYTHON SOURCE LINES 191-199 .. code-block:: default problem = TMScalableProblem(3, [2, 4], [2, 3]) print(problem) print(problem.get_design_space()) print(problem.get_default_inputs()) .. GENERATED FROM PYTHON SOURCE LINES 200-247 .. code-block:: console Scalable problem > TM_System >> Inputs: | x_shared (3) | y_0 (2) | y_1 (3) >> Outputs: | cstr_0 (2) | cstr_1 (3) | obj (1) > TM_Discipline_0 >> Inputs: | x_local_0 (2) | x_shared (3) | y_1 (3) >> Outputs: | y_0 (2) > TM_Discipline_1 >> Inputs: | x_local_1 (4) | x_shared (3) | y_0 (2) >> Outputs: | y_1 (3) Design Space: +-----------+-------------+-------+-------------+-------+ | name | lower_bound | value | upper_bound | type | +-----------+-------------+-------+-------------+-------+ | x_local_0 | 0 | 0.5 | 1 | float | | x_local_0 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_local_1 | 0 | 0.5 | 1 | float | | x_shared | 0 | 0.5 | 1 | float | | x_shared | 0 | 0.5 | 1 | float | | x_shared | 0 | 0.5 | 1 | float | | y_0 | 0 | 0.5 | 1 | float | | y_0 | 0 | 0.5 | 1 | float | | y_1 | 0 | 0.5 | 1 | float | | y_1 | 0 | 0.5 | 1 | float | | y_1 | 0 | 0.5 | 1 | float | +-----------+-------------+-------+-------------+-------+ {'x_shared': array([0.5, 0.5, 0.5]), 'x_local_0': array([0.5, 0.5]), 'y_0': array([0.5, 0.5]), 'cstr_0': array([0.5]), 'x_local_1': array([0.5, 0.5, 0.5, 0.5]), 'y_1': array([0.5, 0.5, 0.5]), 'cstr_1': array([0.5])} .. GENERATED FROM PYTHON SOURCE LINES 249-257 Scalable study -------------- We define a scalable study based on two strongly coupled disciplines and a weakly one, with the following properties: - 3 shared design parameters, - 2 local design parameters for each strongly coupled discipline, - 3 coupling variables for each strongly coupled discipline. .. GENERATED FROM PYTHON SOURCE LINES 257-260 .. code-block:: default study = TMScalableStudy(n_disciplines=2, n_shared=3, n_local=2, n_coupling=3) print(study) .. GENERATED FROM PYTHON SOURCE LINES 261-268 .. code-block:: console Scalable study > 2 disciplines > 3 shared design parameters > 2 local design parameters per discipline > 3 coupling variables per discipline .. GENERATED FROM PYTHON SOURCE LINES 270-271 Then, we run MDF and IDF formulations: .. GENERATED FROM PYTHON SOURCE LINES 271-274 .. code-block:: default study.run_formulation("MDF") study.run_formulation("IDF") .. GENERATED FROM PYTHON SOURCE LINES 275-276 We can look at the result in the console: .. GENERATED FROM PYTHON SOURCE LINES 276-278 .. code-block:: default print(study) .. GENERATED FROM PYTHON SOURCE LINES 279-304 .. code-block:: console Scalable study > 2 disciplines > 3 shared design parameters > 2 local design parameters per discipline > 3 coupling variables per discipline MDO formulations > MDF >> TM_System = 9 calls / 7 linearizations / 3.29e-03 seconds >> TM_Discipline_0 = 132 calls / 7 linearizations / 2.19e-02 seconds >> TM_Discipline_1 = 124 calls / 7 linearizations / 2.04e-02 seconds >> mda = 7 calls / 7 linearizations / 2.68e-01 seconds >> mdo_chain = 7 calls / 0 linearizations / 1.20e-01 seconds >> sub_mda = 7 calls / 0 linearizations / 1.16e-01 seconds >> scenario = 1 calls / 0 linearizations / 3.35e-01 seconds > IDF >> TM_System = 12 calls / 9 linearizations / 2.98e-03 seconds >> TM_Discipline_0 = 12 calls / 9 linearizations / 2.19e-03 seconds >> TM_Discipline_1 = 11 calls / 9 linearizations / 2.01e-03 seconds >> mda = 0 calls / 0 linearizations / 0.00e+00 seconds >> mdo_chain = 0 calls / 0 linearizations / 0.00e+00 seconds >> sub_mda = 0 calls / 0 linearizations / 0.00e+00 seconds >> scenario = 1 calls / 0 linearizations / 7.60e-02 seconds .. GENERATED FROM PYTHON SOURCE LINES 306-307 or plot the execution time: .. GENERATED FROM PYTHON SOURCE LINES 307-309 .. code-block:: default study.plot_exec_time() .. GENERATED FROM PYTHON SOURCE LINES 310-311 .. image:: /_images/scalable_tm/exec_time.png .. GENERATED FROM PYTHON SOURCE LINES 313-322 Parametric scalability study ---------------------------- We define a parametric scalability study based on two strongly coupled disciplines and a weakly one, with the following properties: - 3 shared design parameters, - 2 coupling variables for each strongly coupled discipline, - 1, 5 or 25 local design parameters for each strongly coupled discipline, .. GENERATED FROM PYTHON SOURCE LINES 322-325 .. code-block:: default study = TMParamSS(n_disciplines=2, n_shared=3, n_local=[1, 5, 25], n_coupling=2) print(study) .. GENERATED FROM PYTHON SOURCE LINES 326-333 .. code-block:: console Parametric scalable study > 2 disciplines > 3 shared design parameters > 1, 5 or 25 local design parameters per discipline > 2 coupling variables per discipline .. GENERATED FROM PYTHON SOURCE LINES 335-336 Then, we run MDF and IDF formulations: .. GENERATED FROM PYTHON SOURCE LINES 336-339 .. code-block:: default study.run_formulation("MDF") study.run_formulation("IDF") .. GENERATED FROM PYTHON SOURCE LINES 340-341 and save the results in a pickle file: .. GENERATED FROM PYTHON SOURCE LINES 341-343 .. code-block:: default study.save("results.pkl") .. GENERATED FROM PYTHON SOURCE LINES 344-346 We can plot these results and compare MDF and IDF formulations in terms of execution time for different number of local design variables. .. GENERATED FROM PYTHON SOURCE LINES 346-349 .. code-block:: default results = TMParamSSPost("results.pkl") results.plot("Comparison of MDF and IDF formulations") .. GENERATED FROM PYTHON SOURCE LINES 350-351 .. image:: /_images/scalable_tm/mdf_idf.png .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_examples_scalable_scalable_tm.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: scalable_tm.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: scalable_tm.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_