mda_chain module¶
An advanced MDA splitting algorithm based on graphs.
- class gemseo.mda.mda_chain.MDAChain(disciplines, inner_mda_name='MDAJacobi', max_mda_iter=20, name=None, n_processes=2, chain_linearize=False, tolerance=1e-06, linear_solver_tolerance=1e-12, use_lu_fact=False, grammar_type=GrammarType.JSON, coupling_structure=None, sub_coupling_structures=None, log_convergence=False, linear_solver='DEFAULT', linear_solver_options=None, mdachain_parallelize_tasks=False, mdachain_parallel_options=None, **inner_mda_options)[source]
Bases:
MDA
A chain of MDAs.
The execution sequence is provided by the
DependencyGraph
.Initialize self. See help(type(self)) for accurate signature.
- Parameters:
disciplines (Sequence[MDODiscipline]) – The disciplines from which to compute the MDA.
inner_mda_name (str) –
The class name of the inner-MDA.
By default it is set to “MDAJacobi”.
max_mda_iter (int) –
The maximum iterations number for the MDA algorithm.
By default it is set to 20.
name (str | None) – The name to be given to the MDA. If
None
, use the name of the class.n_processes (int) –
The maximum simultaneous number of threads if
use_threading
is set to True, otherwise processes, used to parallelize the execution.By default it is set to 2.
chain_linearize (bool) –
Whether to linearize the chain of execution. Otherwise, linearize the overall MDA with base class method. This last option is preferred to minimize computations in adjoint mode, while in direct mode, linearizing the chain may be cheaper.
By default it is set to False.
tolerance (float) –
The tolerance of the iterative direct coupling solver; the norm of the current residuals divided by initial residuals norm shall be lower than the tolerance to stop iterating.
By default it is set to 1e-06.
linear_solver_tolerance (float) –
The tolerance of the linear solver in the adjoint equation.
By default it is set to 1e-12.
use_lu_fact (bool) –
Whether to store a LU factorization of the matrix when using adjoint/forward differentiation. to solve faster multiple RHS problem.
By default it is set to False.
grammar_type (MDODiscipline.GrammarType) –
The type of the input and output grammars.
By default it is set to “JSONGrammar”.
coupling_structure (MDOCouplingStructure | None) – The coupling structure to be used by the MDA. If
None
, it is created from disciplines.sub_coupling_structures (Iterable[MDOCouplingStructure | None] | None) – The coupling structures to be used by the inner-MDAs. If
None
, they are created from the sub-disciplines.log_convergence (bool) –
Whether to log the MDA convergence, expressed in terms of normed residuals.
By default it is set to False.
linear_solver (str) –
The name of the linear solver.
By default it is set to “DEFAULT”.
linear_solver_options (Mapping[str, Any] | None) – The options passed to the linear solver factory.
mdachain_parallelize_tasks (bool) –
Whether to parallelize the parallel tasks, if any.
By default it is set to False.
mdachain_parallel_options (Mapping[str, int | bool] | None) – The options of the MDOParallelChain instances, if any.
**inner_mda_options (float | int | bool | str | None) – The options of the inner-MDAs.
- add_differentiated_inputs(inputs=None)[source]
Add the inputs against which to differentiate the outputs.
If the discipline grammar type is
MDODiscipline.GrammarType.JSON
and an input is either a non-numeric array or not an array, it will be ignored. If an input is declared as an array but the type of its items is not defined, it is assumed as a numeric array.If the discipline grammar type is
MDODiscipline.GrammarType.SIMPLE
and an input is not an array, it will be ignored. Keep in mind that in this case the array subtype is not checked.- Parameters:
inputs (Iterable[str] | None) – The input variables against which to differentiate the outputs. If
None
, all the inputs of the discipline are used.- Raises:
ValueError – When the inputs wrt which differentiate the discipline are not inputs of the latter.
- Return type:
None
- add_differentiated_outputs(outputs=None)[source]
Add the outputs to be differentiated.
If the discipline grammar type is
MDODiscipline.GrammarType.JSON
and an output is either a non-numeric array or not an array, it will be ignored. If an output is declared as an array but the type of its items is not defined, it is assumed as a numeric array.If the discipline grammar type is
MDODiscipline.GrammarType.SIMPLE
and an output is not an array, it will be ignored. Keep in mind that in this case the array subtype is not checked.- Parameters:
outputs (Iterable[str] | None) – The output variables to be differentiated. If
None
, all the outputs of the discipline are used.- Raises:
ValueError – When the outputs to differentiate are not discipline outputs.
- Return type:
None
- get_expected_dataflow()[source]
Return the expected data exchange sequence.
This method is used for the XDSM representation.
The default expected data exchange sequence is an empty list.
See also
MDOFormulation.get_expected_dataflow
- Returns:
The data exchange arcs.
- Return type:
list[tuple[gemseo.core.discipline.MDODiscipline, gemseo.core.discipline.MDODiscipline, list[str]]]
- get_expected_workflow()[source]
Return the expected execution sequence.
This method is used for the XDSM representation.
The default expected execution sequence is the execution of the discipline itself.
See also
MDOFormulation.get_expected_workflow
- Returns:
The expected execution sequence.
- Return type:
- plot_residual_history(show=False, save=True, n_iterations=None, logscale=None, filename=None, fig_size=(50.0, 10.0))[source]
Generate a plot of the residual history.
The first iteration of each new execution is marked with a red dot.
- Parameters:
show (bool) –
Whether to display the plot on screen.
By default it is set to False.
save (bool) –
Whether to save the plot as a PDF file.
By default it is set to True.
n_iterations (int | None) – The number of iterations on the x axis. If
None
, use all the iterations.logscale (tuple[int, int] | None) – The limits of the y axis. If
None
, do not change the limits of the y axis.filename (str | None) – The name of the file to save the figure. If
None
, use “{mda.name}_residual_history.pdf”.fig_size (FigSizeType) –
The width and height of the figure in inches, e.g. (w, h).
By default it is set to (50.0, 10.0).
- Returns:
The figure, to be customized if not closed.
- Return type:
None
- reset_statuses_for_run()[source]
Set all the statuses to
MDODiscipline.ExecutionStatus.PENDING
.- Raises:
ValueError – When the discipline cannot be run because of its status.
- Return type:
None
- assembly: JacobianAssembly
- cache: AbstractCache | None
The cache containing one or several executions of the discipline according to the cache policy.
- coupling_structure: MDOCouplingStructure
The coupling structure to be used by the MDA.
- data_processor: DataProcessor
A tool to pre- and post-process discipline data.
- exec_for_lin: bool
Whether the last execution was due to a linearization.
- input_grammar: BaseGrammar
The input grammar.
- jac: dict[str, dict[str, ndarray]]
The Jacobians of the outputs wrt inputs.
The structure is
{output: {input: matrix}}
.
- lin_cache_tol_fact: float
The tolerance factor to cache the Jacobian.
- linear_solver: str
The name of the linear solver.
- linear_solver_options: Mapping[str, Any]
The options of the linear solver.
- linear_solver_tolerance: float
The tolerance of the linear solver in the adjoint equation.
- property log_convergence: bool
Whether to log the MDA convergence.
- matrix_type: JacobianAssembly.JacobianType
The type of the matrix.
- property max_mda_iter: int
The maximum iterations number of each of the inner MDA algorithms.
- name: str
The name of the discipline.
- property normed_residual: float
The normed_residuals, computed from the sub-MDAs residuals.
- output_grammar: BaseGrammar
The output grammar.
- re_exec_policy: ReExecutionPolicy
The policy to re-execute the same discipline.
- reset_history_each_run: bool
Whether to reset the history of MDA residuals before each run.
- residual_variables: Mapping[str, str]
The output variables mapping to their inputs, to be considered as residuals; they shall be equal to zero.
- run_solves_residuals: bool
Whether the run method shall solve the residuals.
- scaling: ResidualScaling
The scaling method applied to MDA residuals for convergence monitoring.
- tolerance: float
The tolerance of the iterative direct coupling solver.
- use_lu_fact: bool
Whether to store a LU factorization of the matrix.
- warm_start: bool
Whether the second iteration and ongoing start from the previous solution.
Examples using MDAChain¶
MDAChain with independent parallel MDAs