Note
Go to the end to download the full example code.
Formulation#
In this example, we will discover the different functions of the API related to MDO formulations: their names, their options and their sub-options.
from __future__ import annotations
from gemseo import configure_logger
from gemseo import get_available_formulations
from gemseo import get_formulation_options_schema
from gemseo import get_formulation_sub_options_schema
from gemseo import get_formulations_options_defaults
from gemseo import get_formulations_sub_options_defaults
configure_logger()
<RootLogger root (INFO)>
Get available formulations#
The get_available_formulations()
function returns the list
of MDO formulations available in GEMSEO or in external modules
get_available_formulations()
['BiLevel', 'BiLevelBCD', 'DisciplinaryOpt', 'IDF', 'MDF']
Get formulation schemas for (sub-)options#
For a given MDO formulation, e.g. "MDF"
, we can:
get the options of an MDO formulation using the
get_formulation_options_schema()
function; e.g.
get_formulation_options_schema("MDF")
{'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'differentiated_input_names_substitute': {'type': 'array', 'description': 'The names of the discipline inputs with respect to which to differentiate the discipline outputs used as objective, constraints and observables. If empty, consider the inputs of these functions. More precisely, for each function, an :class:`.MDOFunction` is built from the ``disciplines``, which depend on input variables :math:`x_1,\\ldots,x_d,x_{d+1}`, and over an input space spanned by the input variables :math:`x_1,\\ldots,x_d` and depending on both the MDO formulation and the ``design_space``. Then, the methods :meth:`.MDOFunction.evaluate` and :meth:`.MDOFunction.jac` are called at a given point of the input space and return the output value and the Jacobian matrix, i.e. the matrix concatenating the partial derivatives with respect to the inputs :math:`x_1,\\ldots,x_d` at this point of the input space. This argument can be used to compute the matrix concatenating the partial derivatives at the same point of the input space but with respect to custom inputs, e.g. :math:`x_{d-1}` and :math:`x_{d+1}`. Mathematically speaking, this matrix returned by :meth:`.MDOFunction.jac` is no longer a Jacobian.'}, 'main_mda_name': {'type': 'string', 'description': 'The name of the class of the main MDA. Typically the :class:`.MDAChain`, but one can force to use :class:`.MDAGaussSeidel` for instance. This field is ignored when ``main_mda_settings`` is a Pydantic model.'}, 'main_mda_settings': {'type': 'object', 'description': 'The settings of the main MDA. These settings may include those of the inner-MDA.'}}}
get the default option values using the
get_formulations_options_defaults()
function; e.g.
get_formulations_options_defaults("MDF")
{'differentiated_input_names_substitute': (), 'main_mda_name': 'MDAChain', 'main_mda_settings': {}}
get sub-options of an MDO formulation using the
get_formulation_sub_options_schema()
function; e.g.
get_formulation_sub_options_schema("MDF", main_mda_name="MDAGaussSeidel")
{'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'coupling_structure': {'type': 'null', 'description': 'The coupling structure to be used by the MDA. If ``None``, the coupling structure is created from the disciplines.'}, 'linear_solver': {'type': 'string', 'description': 'The name of the linear solver. This field is ignored when ``linear_solver_settings`` is a Pydantic model.'}, 'linear_solver_settings': {'type': 'object', 'description': 'The settings of the linear solver.'}, 'linear_solver_tolerance': {'type': 'number', 'description': 'The linear solver tolerance.'}, 'log_convergence': {'type': 'boolean', 'description': 'Whether to log the MDA convergence. The log displays the normalized residual norm.'}, 'max_mda_iter': {'type': 'integer', 'description': 'The maximum number of iterations for the MDA algorithm. If 0, evaluate the coupling variables without trying to solve the coupling equations.'}, 'max_consecutive_unsuccessful_iterations': {'type': 'integer', 'description': 'The maximum number of consecutive unsuccessful iterations. Iterations are considered unsuccessful if the normalized residual norm increases.'}, 'name': {'type': 'string', 'description': 'The name to be given to the MDA. If empty, use the name of the class.'}, 'tolerance': {'type': 'number', 'description': 'The tolerance for the MDA algorithm residual. The available normalization strategies for the residual are described in :attr:`.BaseMDA.ResidualScaling`.'}, 'use_lu_fact': {'type': 'boolean', 'description': 'Whether to perform an LU factorization. The factorization is used to solve the linear systems arising in the computation of the total derivatives. Since there are possibly several right-hand side, if affordable, such a factorization may improve the solution time.'}, 'warm_start': {'type': 'boolean', 'description': 'Whether to warm start the execution of the MDA algorithm. The warm start strategy consists in using the last cached values for the coupling variables as an initial guess. This is expected to reduce the number of iteration of the MDA algorithm required to reach convergence.'}, 'acceleration_method': {'type': 'string', 'description': 'The acceleration method used within the fixed point iterations.'}, 'over_relaxation_factor': {'type': 'number', 'description': 'The over-relaxation factor.'}}}
get the sub-option values using the
get_formulations_sub_options_defaults()
function; e.g.
get_formulations_sub_options_defaults("MDF", main_mda_name="MDAGaussSeidel")
{'coupling_structure': None, 'linear_solver': <LinearSolver.DEFAULT: 'DEFAULT'>, 'linear_solver_settings': {}, 'linear_solver_tolerance': 1e-12, 'log_convergence': False, 'max_mda_iter': 20, 'max_consecutive_unsuccessful_iterations': 8, 'name': '', 'tolerance': 1e-06, 'use_lu_fact': False, 'warm_start': False, 'acceleration_method': <AccelerationMethod.NONE: 'NoTransformation'>, 'over_relaxation_factor': 1.0}
Or import its settings model and pass it directly with the keyword "formulation_settings_model".
from gemseo.settings.formulations import MDF_Settings # noqa: E402
settings_model = MDF_Settings()
See Formulation Settings for more information on how to use settings models.
Total running time of the script: (0 minutes 0.003 seconds)