{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Scalable problem of Tedford and Martins, 2010\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom future import standard_library\nfrom numpy import array\nfrom numpy.random import rand\n\nfrom gemseo.api import generate_n2_plot\nfrom gemseo.problems.scalable.scalable_tm.core import (\n TMParamSS,\n TMParamSSPost,\n TMScalableProblem,\n TMScalableStudy,\n)\nfrom gemseo.problems.scalable.scalable_tm.design_space import TMDesignSpace\nfrom gemseo.problems.scalable.scalable_tm.disciplines import TMDiscipline, TMSystem\n\nstandard_library.install_aliases()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Disciplines\nWe define two strongly coupled disciplines and a weakly coupled discipline,\nwith:\n\n- 2 shared design parameters,\n- 2 local design parameters for the first discipline,\n- 3 local design parameters for the second discipline,\n- 3 coupling variables for the first discipline,\n- 2 coupling variables for the second discipline.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sizes = {\"x_shared\": 2, \"x_local_0\": 2, \"x_local_1\": 3, \"y_0\": 3, \"y_1\": 2}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use any values for the coefficients and the default values of the design\nparameters and coupling variables.\n\n### Strongly coupled disciplines\nHere is the first strongly coupled discipline.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"default_inputs = {\n \"x_shared\": rand(sizes[\"x_shared\"]),\n \"x_local_0\": rand(sizes[\"x_local_0\"]),\n \"y_1\": rand(sizes[\"y_1\"]),\n}\nindex = 0\nc_shared = rand(sizes[\"y_0\"], sizes[\"x_shared\"])\nc_local = rand(sizes[\"y_0\"], sizes[\"x_local_0\"])\nc_coupling = {\"y_1\": rand(sizes[\"y_0\"], sizes[\"y_1\"])}\ndisc0 = TMDiscipline(index, c_shared, c_local, c_coupling, default_inputs)\n\nprint(disc0.name)\nprint(disc0.get_input_data_names())\nprint(disc0.get_output_data_names())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n TM_Discipline_0\n dict_keys(['x_shared', 'x_local_0', 'y_1'])\n dict_keys(['y_0'])\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the second one, strongly coupled with the first one.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"default_inputs = {\n \"x_shared\": rand(sizes[\"x_shared\"]),\n \"x_local_1\": rand(sizes[\"x_local_1\"]),\n \"y_0\": rand(sizes[\"y_0\"]),\n}\nindex = 1\nc_shared = rand(sizes[\"y_1\"], sizes[\"x_shared\"])\nc_local = rand(sizes[\"y_1\"], sizes[\"x_local_1\"])\nc_coupling = {\"y_0\": rand(sizes[\"y_1\"], sizes[\"y_0\"])}\ndisc1 = TMDiscipline(index, c_shared, c_local, c_coupling, default_inputs)\n\nprint(disc1.name)\nprint(disc1.get_input_data_names())\nprint(disc1.get_output_data_names())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n TM_Discipline_1\n dict_keys(['x_shared', 'x_local_1', 'y_0'])\n dict_keys(['y_1'])\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Weakly coupled discipline\nHere is the discipline weakly coupled to the previous ones.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c_constraint = [array([1.0, 2.0]), array([3.0, 4.0, 5.0])]\ndefault_inputs = {\n \"x_shared\": array([0.5]),\n \"y_0\": array([2.0, 3.0]),\n \"y_1\": array([4.0, 5.0, 6.0]),\n}\nsystem = TMSystem(c_constraint, default_inputs)\n\nprint(system.name)\nprint(system.get_input_data_names())\nprint(system.get_output_data_names())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n TM_System\n dict_keys(['x_shared', 'y_0', 'y_1'])\n dict_keys(['obj', 'cstr_0', 'cstr_1'])\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Coupling chart\nWe can represent these three disciplines by means of a N2 chart.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"generate_n2_plot([disc0, disc1, system], save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Design space\nWe define the design space from the sizes of the shared design parameters,\nlocal parameters and coupling variables.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"n_shared = sizes[\"x_shared\"]\nn_local = [sizes[\"x_local_0\"], sizes[\"x_local_1\"]]\nn_coupling = [sizes[\"y_0\"], sizes[\"y_1\"]]\ndesign_space = TMDesignSpace(n_shared, n_local, n_coupling)\n\nprint(design_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n Design Space:\n +-----------+-------------+-------+-------------+-------+\n | name | lower_bound | value | upper_bound | type |\n +-----------+-------------+-------+-------------+-------+\n | x_local_0 | 0 | 0.5 | 1 | float |\n | x_local_0 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_shared | 0 | 0.5 | 1 | float |\n | x_shared | 0 | 0.5 | 1 | float |\n | y_0 | 0 | 0.5 | 1 | float |\n | y_0 | 0 | 0.5 | 1 | float |\n | y_0 | 0 | 0.5 | 1 | float |\n | y_1 | 0 | 0.5 | 1 | float |\n | y_1 | 0 | 0.5 | 1 | float |\n +-----------+-------------+-------+-------------+-------+\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scalable problem\nWe define a scalable problem based on two strongly coupled disciplines\nand a weakly one, with the following properties:\n\n- 3 shared design parameters,\n- 2 local design parameters for the first strongly coupled discipline,\n- 2 coupling variables for the first strongly coupled discipline,\n- 4 local design parameters for the second strongly coupled discipline,\n- 3 coupling variables for the second strongly coupled discipline.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem = TMScalableProblem(3, [2, 4], [2, 3])\n\nprint(problem)\n\nprint(problem.get_design_space())\n\nprint(problem.get_default_inputs())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n Scalable problem\n > TM_System\n >> Inputs:\n | x_shared (3)\n | y_0 (2)\n | y_1 (3)\n >> Outputs:\n | cstr_0 (2)\n | cstr_1 (3)\n | obj (1)\n > TM_Discipline_0\n >> Inputs:\n | x_local_0 (2)\n | x_shared (3)\n | y_1 (3)\n >> Outputs:\n | y_0 (2)\n > TM_Discipline_1\n >> Inputs:\n | x_local_1 (4)\n | x_shared (3)\n | y_0 (2)\n >> Outputs:\n | y_1 (3)\n\n Design Space:\n +-----------+-------------+-------+-------------+-------+\n | name | lower_bound | value | upper_bound | type |\n +-----------+-------------+-------+-------------+-------+\n | x_local_0 | 0 | 0.5 | 1 | float |\n | x_local_0 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_local_1 | 0 | 0.5 | 1 | float |\n | x_shared | 0 | 0.5 | 1 | float |\n | x_shared | 0 | 0.5 | 1 | float |\n | x_shared | 0 | 0.5 | 1 | float |\n | y_0 | 0 | 0.5 | 1 | float |\n | y_0 | 0 | 0.5 | 1 | float |\n | y_1 | 0 | 0.5 | 1 | float |\n | y_1 | 0 | 0.5 | 1 | float |\n | y_1 | 0 | 0.5 | 1 | float |\n +-----------+-------------+-------+-------------+-------+\n {'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])}\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scalable study\nWe define a scalable study based on two strongly coupled disciplines\nand a weakly one, with the following properties:\n\n- 3 shared design parameters,\n- 2 local design parameters for each strongly coupled discipline,\n- 3 coupling variables for each strongly coupled discipline.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study = TMScalableStudy(n_disciplines=2, n_shared=3, n_local=2, n_coupling=3)\nprint(study)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n Scalable study\n > 2 disciplines\n > 3 shared design parameters\n > 2 local design parameters per discipline\n > 3 coupling variables per discipline\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we run MDF and IDF formulations:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.run_formulation(\"MDF\")\nstudy.run_formulation(\"IDF\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can look at the result in the console:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(study)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n Scalable study\n > 2 disciplines\n > 3 shared design parameters\n > 2 local design parameters per discipline\n > 3 coupling variables per discipline\n\n MDO formulations\n > MDF\n >> TM_System = 9 calls / 7 linearizations / 3.29e-03 seconds\n >> TM_Discipline_0 = 132 calls / 7 linearizations / 2.19e-02 seconds\n >> TM_Discipline_1 = 124 calls / 7 linearizations / 2.04e-02 seconds\n >> mda = 7 calls / 7 linearizations / 2.68e-01 seconds\n >> mdo_chain = 7 calls / 0 linearizations / 1.20e-01 seconds\n >> sub_mda = 7 calls / 0 linearizations / 1.16e-01 seconds\n >> scenario = 1 calls / 0 linearizations / 3.35e-01 seconds\n > IDF\n >> TM_System = 12 calls / 9 linearizations / 2.98e-03 seconds\n >> TM_Discipline_0 = 12 calls / 9 linearizations / 2.19e-03 seconds\n >> TM_Discipline_1 = 11 calls / 9 linearizations / 2.01e-03 seconds\n >> mda = 0 calls / 0 linearizations / 0.00e+00 seconds\n >> mdo_chain = 0 calls / 0 linearizations / 0.00e+00 seconds\n >> sub_mda = 0 calls / 0 linearizations / 0.00e+00 seconds\n >> scenario = 1 calls / 0 linearizations / 7.60e-02 seconds\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or plot the execution time:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.plot_exec_time()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Parametric scalability study\nWe define a parametric scalability study\nbased on two strongly coupled disciplines\nand a weakly one, with the following properties:\n\n- 3 shared design parameters,\n- 2 coupling variables for each strongly coupled discipline,\n- 1, 5 or 25 local design parameters for each strongly coupled discipline,\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study = TMParamSS(n_disciplines=2, n_shared=3, n_local=[1, 5, 25], n_coupling=2)\nprint(study)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. code-block:: console\n\n Parametric scalable study\n > 2 disciplines\n > 3 shared design parameters\n > 1, 5 or 25 local design parameters per discipline\n > 2 coupling variables per discipline\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we run MDF and IDF formulations:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.run_formulation(\"MDF\")\nstudy.run_formulation(\"IDF\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and save the results in a pickle file:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.save(\"results.pkl\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot these results and compare MDF and IDF formulations in terms\nof execution time for different number of local design variables.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"results = TMParamSSPost(\"results.pkl\")\nresults.plot(\"Comparison of MDF and IDF formulations\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}