.. 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 26-41 .. code-block:: default from gemseo.api import configure_logger from gemseo.api import generate_n2_plot from gemseo.problems.scalable.parametric.core.design_space import TMDesignSpace from gemseo.problems.scalable.parametric.disciplines import TMMainDiscipline from gemseo.problems.scalable.parametric.disciplines import TMSubDiscipline from gemseo.problems.scalable.parametric.problem import TMScalableProblem from gemseo.problems.scalable.parametric.study import TMParamSS from gemseo.problems.scalable.parametric.study import TMParamSSPost from gemseo.problems.scalable.parametric.study import TMScalableStudy from numpy import array from numpy.random import rand configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 42-52 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 52-55 .. 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 56-62 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 62-77 .. 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()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none SubModel_0 KeysView(Grammar name: SubModel_0_input, schema: { "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "x_local_0": { "type": "array", "items": { "type": "number" } }, "x_shared": { "type": "array", "items": { "type": "number" } }, "y_1": { "type": "array", "items": { "type": "number" } } }, "required": [ "x_local_0", "x_shared", "y_1" ] }) KeysView(Grammar name: SubModel_0_output, schema: { "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "y_0": { "type": "array", "items": { "type": "number" } } }, "required": [ "y_0" ] }) .. GENERATED FROM PYTHON SOURCE LINES 78-83 .. code-block:: console TM_Discipline_0 dict_keys(['x_shared', 'x_local_0', 'y_1']) dict_keys(['y_0']) .. GENERATED FROM PYTHON SOURCE LINES 86-87 Here is the second one, strongly coupled with the first one. .. GENERATED FROM PYTHON SOURCE LINES 87-102 .. 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()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none SubModel_1 KeysView(Grammar name: SubModel_1_input, schema: { "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "x_local_1": { "type": "array", "items": { "type": "number" } }, "x_shared": { "type": "array", "items": { "type": "number" } }, "y_0": { "type": "array", "items": { "type": "number" } } }, "required": [ "x_local_1", "x_shared", "y_0" ] }) KeysView(Grammar name: SubModel_1_output, schema: { "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "y_1": { "type": "array", "items": { "type": "number" } } }, "required": [ "y_1" ] }) .. GENERATED FROM PYTHON SOURCE LINES 103-108 .. code-block:: console TM_Discipline_1 dict_keys(['x_shared', 'x_local_1', 'y_0']) dict_keys(['y_1']) .. GENERATED FROM PYTHON SOURCE LINES 110-113 Weakly coupled discipline ^^^^^^^^^^^^^^^^^^^^^^^^^ Here is the discipline weakly coupled to the previous ones. .. GENERATED FROM PYTHON SOURCE LINES 113-125 .. 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()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none MainModel KeysView(Grammar name: MainModel_input, schema: { "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "x_shared": { "type": "array", "items": { "type": "number" } }, "y_0": { "type": "array", "items": { "type": "number" } }, "y_1": { "type": "array", "items": { "type": "number" } } }, "required": [ "x_shared", "y_0", "y_1" ] }) KeysView(Grammar name: MainModel_output, schema: { "$schema": "http://json-schema.org/schema#", "type": "object", "properties": { "cstr_0": { "type": "array", "items": { "type": "number" } }, "cstr_1": { "type": "array", "items": { "type": "number" } }, "obj": { "type": "array", "items": { "type": "number" } } }, "required": [ "cstr_0", "cstr_1", "obj" ] }) .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. 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 133-136 Coupling chart ^^^^^^^^^^^^^^ We can represent these three disciplines by means of an N2 chart. .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: default generate_n2_plot([disc0, disc1, system], save=False, show=True) .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_001.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 139-140 .. image:: /_images/scalable_tm/N2chart.png .. GENERATED FROM PYTHON SOURCE LINES 142-146 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 146-153 .. 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) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 154-173 .. 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 175-185 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 185-193 .. code-block:: default problem = TMScalableProblem(3, [2, 4], [2, 3]) print(problem) print(problem.get_design_space()) print(problem.get_default_inputs()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Scalable problem .... MainModel ........ Inputs: ............ x_shared (3) ............ y_0 (2) ............ y_1 (3) ........ Outputs: ............ cstr_0 (2) ............ cstr_1 (3) ............ obj (1) .... SubModel_0 ........ Inputs: ............ x_local_0 (2) ............ x_shared (3) ............ y_1 (3) ........ Outputs: ............ y_0 (2) .... SubModel_1 ........ Inputs: ............ x_local_1 (4) ............ x_shared (3) ............ y_0 (2) ........ Outputs: ............ y_1 (3) +------------------------------------------------------------------------------+ | Parameter space | +-----------+-------------+-------+-------------+-------+----------------------+ | name | lower_bound | value | upper_bound | type | Initial distribution | +-----------+-------------+-------+-------------+-------+----------------------+ | 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, 0.5]), 'u_local_0': array([0., 0.]), '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, 0.5, 0.5]), 'u_local_1': array([0., 0., 0.])} .. GENERATED FROM PYTHON SOURCE LINES 194-244 .. 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 246-254 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 254-257 .. code-block:: default study = TMScalableStudy(n_disciplines=2, n_shared=3, n_local=2, n_coupling=3) print(study) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Scalable study .... 2 disciplines .... 3 shared design parameters .... 2 local design parameters per discipline .... 3 coupling variables per discipline .. GENERATED FROM PYTHON SOURCE LINES 258-265 .. 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 267-268 Then, we run MDF and IDF formulations: .. GENERATED FROM PYTHON SOURCE LINES 268-271 .. code-block:: default study.run_formulation("MDF", xdsm_pdf=False) study.run_formulation("IDF", xdsm_pdf=False) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_002.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_003.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 10:04:00: Make the starting point feasible. INFO - 10:04:00: INFO - 10:04:00: *** Start MDOScenario execution *** INFO - 10:04:00: MDOScenario INFO - 10:04:00: Disciplines: MainModel SubModel_0 SubModel_1 INFO - 10:04:00: MDO formulation: MDF INFO - 10:04:00: Optimization problem: INFO - 10:04:00: minimize obj(x_local_0, x_local_1, x_shared) INFO - 10:04:00: with respect to x_local_0, x_local_1, x_shared INFO - 10:04:00: subject to constraints: INFO - 10:04:00: cstr_0(x_local_0, x_local_1, x_shared) <= [ 0.65250671 -2.8453087 -4.16343355] INFO - 10:04:00: cstr_1(x_local_0, x_local_1, x_shared) <= [0.85268891 0.52747299 0.62341835] INFO - 10:04:00: over the design space: INFO - 10:04:00: | Parameter space | INFO - 10:04:00: +-----------+-------------+-------+-------------+-------+----------------------+ INFO - 10:04:00: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 10:04:00: +-----------+-------------+-------+-------------+-------+----------------------+ INFO - 10:04:00: | x_local_0 | 0 | 0.5 | 1 | float | | INFO - 10:04:00: | x_local_0 | 0 | 0.5 | 1 | float | | INFO - 10:04:00: | x_local_1 | 0 | 0.5 | 1 | float | | INFO - 10:04:00: | x_local_1 | 0 | 0.5 | 1 | float | | INFO - 10:04:00: | x_shared | 0 | 0.5 | 1 | float | | INFO - 10:04:00: | x_shared | 0 | 0.5 | 1 | float | | INFO - 10:04:00: | x_shared | 0 | 0.5 | 1 | float | | INFO - 10:04:00: +-----------+-------------+-------+-------------+-------+----------------------+ INFO - 10:04:00: Solving optimization problem with algorithm NLOPT_SLSQP: INFO - 10:04:00: ... 0%| | 0/100 [00:00 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 303-304 or plot the execution time: .. GENERATED FROM PYTHON SOURCE LINES 304-306 .. code-block:: default study.plot_exec_time() .. GENERATED FROM PYTHON SOURCE LINES 307-308 .. image:: /_images/scalable_tm/exec_time.png .. GENERATED FROM PYTHON SOURCE LINES 310-319 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 319-322 .. code-block:: default study = TMParamSS(n_disciplines=2, n_shared=3, n_local=[1, 5, 25], n_coupling=2) print(study) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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 323-330 .. 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 332-333 Then, we run MDF and IDF formulations: .. GENERATED FROM PYTHON SOURCE LINES 333-336 .. code-block:: default study.run_formulation("MDF") study.run_formulation("IDF") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_004.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_005.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_005.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_006.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_006.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_007.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_007.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_008.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_008.png :class: sphx-glr-multi-img * .. image-sg:: /examples/scalable/images/sphx_glr_scalable_tm_009.png :alt: scalable tm :srcset: /examples/scalable/images/sphx_glr_scalable_tm_009.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 10:04:09: Make the starting point feasible. INFO - 10:04:09: INFO - 10:04:09: *** Start MDOScenario execution *** INFO - 10:04:09: MDOScenario INFO - 10:04:09: Disciplines: MainModel SubModel_0 SubModel_1 INFO - 10:04:09: MDO formulation: MDF INFO - 10:04:09: Optimization problem: INFO - 10:04:09: minimize obj(x_local_0, x_local_1, x_shared) INFO - 10:04:09: with respect to x_local_0, x_local_1, x_shared INFO - 10:04:09: subject to constraints: INFO - 10:04:09: cstr_0(x_local_0, x_local_1, x_shared) <= [0.88589544 0.88821903] INFO - 10:04:09: cstr_1(x_local_0, x_local_1, x_shared) <= [-0.17585898 -1.56050583] INFO - 10:04:09: over the design space: INFO - 10:04:09: | Parameter space | INFO - 10:04:09: +-----------+-------------+-------+-------------+-------+----------------------+ INFO - 10:04:09: | name | lower_bound | value | upper_bound | type | Initial distribution | INFO - 10:04:09: +-----------+-------------+-------+-------------+-------+----------------------+ INFO - 10:04:09: | x_local_0 | 0 | 0.5 | 1 | float | | INFO - 10:04:09: | x_local_1 | 0 | 0.5 | 1 | float | | INFO - 10:04:09: | x_shared | 0 | 0.5 | 1 | float | | INFO - 10:04:09: | x_shared | 0 | 0.5 | 1 | float | | INFO - 10:04:09: | x_shared | 0 | 0.5 | 1 | float | | INFO - 10:04:09: +-----------+-------------+-------+-------------+-------+----------------------+ INFO - 10:04:09: Solving optimization problem with algorithm NLOPT_SLSQP: INFO - 10:04:09: ... 0%| | 0/100 [00:00` .. 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 `_