.. 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-50 .. code-block:: default from __future__ import division, unicode_literals from numpy import array from numpy.random import rand from gemseo.api import configure_logger, generate_n2_plot from gemseo.problems.scalable.parametric.core.design_space import TMDesignSpace from gemseo.problems.scalable.parametric.disciplines import ( TMMainDiscipline, TMSubDiscipline, ) from gemseo.problems.scalable.parametric.problem import TMScalableProblem from gemseo.problems.scalable.parametric.study import ( TMParamSS, TMParamSSPost, TMScalableStudy, ) configure_logger() .. GENERATED FROM PYTHON SOURCE LINES 51-61 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 61-64 .. 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 65-71 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 71-86 .. 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 = TMSubDiscipline(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 87-92 .. code-block:: console TM_Discipline_0 dict_keys(['x_shared', 'x_local_0', 'y_1']) dict_keys(['y_0']) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Here is the second one, strongly coupled with the first one. .. GENERATED FROM PYTHON SOURCE LINES 96-111 .. 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 = TMSubDiscipline(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 112-117 .. code-block:: console TM_Discipline_1 dict_keys(['x_shared', 'x_local_1', 'y_0']) dict_keys(['y_1']) .. GENERATED FROM PYTHON SOURCE LINES 119-122 Weakly coupled discipline ^^^^^^^^^^^^^^^^^^^^^^^^^ Here is the discipline weakly coupled to the previous ones. .. GENERATED FROM PYTHON SOURCE LINES 122-134 .. 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 = TMMainDiscipline(c_constraint, default_inputs) print(system.name) print(system.get_input_data_names()) print(system.get_output_data_names()) .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. 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 142-145 Coupling chart ^^^^^^^^^^^^^^ We can represent these three disciplines by means of an N2 chart. .. GENERATED FROM PYTHON SOURCE LINES 145-147 .. code-block:: default generate_n2_plot([disc0, disc1, system], save=False, show=True) .. GENERATED FROM PYTHON SOURCE LINES 148-149 .. image:: /_images/scalable_tm/N2chart.png .. GENERATED FROM PYTHON SOURCE LINES 151-155 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 155-162 .. 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 163-182 .. 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 184-194 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 194-202 .. 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 203-253 .. 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 255-263 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 263-266 .. code-block:: default study = TMScalableStudy(n_disciplines=2, n_shared=3, n_local=2, n_coupling=3) print(study) .. GENERATED FROM PYTHON SOURCE LINES 267-274 .. 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 276-277 Then, we run MDF and IDF formulations: .. GENERATED FROM PYTHON SOURCE LINES 277-280 .. code-block:: default study.run_formulation("MDF") study.run_formulation("IDF") .. GENERATED FROM PYTHON SOURCE LINES 281-282 We can look at the result in the console: .. GENERATED FROM PYTHON SOURCE LINES 282-284 .. code-block:: default print(study) .. GENERATED FROM PYTHON SOURCE LINES 285-310 .. 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 312-313 or plot the execution time: .. GENERATED FROM PYTHON SOURCE LINES 313-315 .. code-block:: default study.plot_exec_time() .. GENERATED FROM PYTHON SOURCE LINES 316-317 .. image:: /_images/scalable_tm/exec_time.png .. GENERATED FROM PYTHON SOURCE LINES 319-328 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 328-331 .. 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 332-339 .. 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 341-342 Then, we run MDF and IDF formulations: .. GENERATED FROM PYTHON SOURCE LINES 342-345 .. code-block:: default study.run_formulation("MDF") study.run_formulation("IDF") .. GENERATED FROM PYTHON SOURCE LINES 346-347 and save the results in a pickle file: .. GENERATED FROM PYTHON SOURCE LINES 347-349 .. code-block:: default study.save("results.pkl") .. GENERATED FROM PYTHON SOURCE LINES 350-352 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 352-355 .. code-block:: default results = TMParamSSPost("results.pkl") results.plot("Comparison of MDF and IDF formulations") .. GENERATED FROM PYTHON SOURCE LINES 356-357 .. 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 `_