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='', 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, initialize_defaults=False, **inner_mda_options)[source]
Bases:
BaseMDA
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) –
The name to be given to the MDA. If
None
, use the name of the class.By default it is set to “”.
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 (StrKeyMapping | 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.
initialize_defaults (bool) –
Whether to create a
MDOInitializationChain
to compute the eventually missingdefault_inputs
at the first execution.By default it is set to False.
**inner_mda_options (float | int | bool | str | None) – The options of the inner-MDAs.
- add_differentiated_inputs(inputs=None)[source]
Add the inputs for differentiation.
The inputs that do not represent continuous numbers are filtered out.
- 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 ``inputs `` are not in the input grammar.
- Return type:
None
- add_differentiated_outputs(outputs=None)[source]
Add the outputs for differentiation.
The outputs that do not represent continuous numbers are filtered out.
- Parameters:
outputs (Iterable[str] | None) – The output variables to be differentiated. If
None
, all the outputs of the discipline are used.- Raises:
ValueError – When ``outputs `` are not in the output grammar.
- Return type:
None
- execute(input_data=None)[source]
Execute the discipline.
This method executes the discipline:
Adds the default inputs to the
input_data
if some inputs are not defined in input_data but exist inMDODiscipline.default_inputs
.Checks whether the last execution of the discipline was called with identical inputs, i.e. cached in
MDODiscipline.cache
; if so, directly returnsself.cache.get_output_cache(inputs)
.Caches the inputs.
Checks the input data against
MDODiscipline.input_grammar
.If
MDODiscipline.data_processor
is not None, runs the preprocessor.Updates the status to
MDODiscipline.ExecutionStatus.RUNNING
.Calls the
MDODiscipline._run()
method, that shall be defined.If
MDODiscipline.data_processor
is not None, runs the postprocessor.Checks the output data.
Caches the outputs.
Updates the status to
MDODiscipline.ExecutionStatus.DONE
orMDODiscipline.ExecutionStatus.FAILED
.Updates summed execution time.
- Parameters:
input_data (StrKeyMapping | None) – The input data needed to execute the discipline according to the discipline input grammar. If
None
, use theMDODiscipline.default_inputs
.- Returns:
The discipline local data after execution.
- Return type:
- get_disciplines_in_dataflow_chain()[source]
Return the disciplines that must be shown as blocks in the XDSM.
By default, only the discipline itself is shown. This function can be differently implemented for any type of inherited discipline.
- Returns:
The disciplines shown in the XDSM chain.
- Return type:
- 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:
- 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='', 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 empty, do not change the limits of the y axis.
filename (Path | str) –
The name of the file to save the figure. If empty, use “{mda.name}_residual_history.pdf”.
By default it is set to “”.
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: BaseCache | 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: MutableMapping[str, MutableMapping[str, ndarray | csr_array | JacobianOperator]]
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: StrKeyMapping
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 the MDA algorithm.
- 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: dict[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