Skip to content

MDA settings

mdo_framework.core.mda

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

MDASettings

Bases: BaseModel

How the strongly coupled tools are solved.

The default runs the coupled tools one after the other (Gauss-Seidel). Running them in parallel requires inner_mda_name="MDAJacobi" and tools declared thread_safe.

Attributes:

Name Type Description
inner_mda_name Literal['MDAGaussSeidel', 'MDAJacobi', 'MDANewtonRaphson']

GEMSEO algorithm that solves each coupled group.

tolerance PositiveFloat

Normalized residual at which the coupled group has converged.

max_mda_iter PositiveInt

Maximum number of iterations of the algorithm.

max_consecutive_unsuccessful_iterations PositiveInt

Number of consecutive iterations without residual decrease after which the algorithm stops.

n_processes PositiveInt

Number of threads running the coupled tools at the same time. Above 1 only for MDAJacobi.

accept_tolerance PositiveFloat | None

Largest normalized residual of a stopped algorithm that is still accepted. None accepts only tolerance.

Source code in src/mdo_framework/core/mda.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class MDASettings(BaseModel):
    """How the strongly coupled tools are solved.

    The default runs the coupled tools one after the other (Gauss-Seidel).
    Running them in parallel requires ``inner_mda_name="MDAJacobi"`` and tools
    declared ``thread_safe``.

    Attributes:
        inner_mda_name: GEMSEO algorithm that solves each coupled group.
        tolerance: Normalized residual at which the coupled group has converged.
        max_mda_iter: Maximum number of iterations of the algorithm.
        max_consecutive_unsuccessful_iterations: Number of consecutive
            iterations without residual decrease after which the algorithm
            stops.
        n_processes: Number of threads running the coupled tools at the same
            time. Above 1 only for ``MDAJacobi``.
        accept_tolerance: Largest normalized residual of a stopped algorithm
            that is still accepted. ``None`` accepts only ``tolerance``.
    """

    model_config = ConfigDict(frozen=True, extra="forbid")

    inner_mda_name: Literal["MDAGaussSeidel", "MDAJacobi", "MDANewtonRaphson"] = (
        "MDAGaussSeidel"
    )
    tolerance: PositiveFloat = 1e-6
    max_mda_iter: PositiveInt = 20
    max_consecutive_unsuccessful_iterations: PositiveInt = 8
    n_processes: PositiveInt = 1
    accept_tolerance: PositiveFloat | None = None

    @model_validator(mode="after")
    def _check_consistency(self) -> Self:
        if self.n_processes > 1 and self.inner_mda_name != PARALLEL_INNER_MDA:
            raise ValueError(
                f"n_processes={self.n_processes} requires "
                f"inner_mda_name='{PARALLEL_INNER_MDA}', not "
                f"'{self.inner_mda_name}'"
            )
        if self.accept_tolerance is not None and self.accept_tolerance < self.tolerance:
            raise ValueError(
                f"accept_tolerance ({self.accept_tolerance:g}) cannot be below "
                f"tolerance ({self.tolerance:g})"
            )
        return self

    @property
    def residual_limit(self) -> float:
        """Largest normalized residual of an accepted coupled group."""
        if self.accept_tolerance is None:
            return self.tolerance
        return self.accept_tolerance

residual_limit property

Largest normalized residual of an accepted coupled group.

StrictMDAChain

Bases: MDAChain

MDAChain that raises instead of returning unconverged states.

GEMSEO only logs a warning when an algorithm stops above its tolerance and returns the last iterate. Every coupled group of this chain is checked once it has run, so each evaluation of the discipline either converged or raised MDANotConvergedError.

Source code in src/mdo_framework/core/mda.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class StrictMDAChain(MDAChain):
    """MDAChain that raises instead of returning unconverged states.

    GEMSEO only logs a warning when an algorithm stops above its tolerance and
    returns the last iterate. Every coupled group of this chain is checked
    once it has run, so each evaluation of the discipline either converged or
    raised ``MDANotConvergedError``.
    """

    def __init__(self, disciplines: Sequence[Discipline], settings: MDASettings):
        """Build the chain.

        Args:
            disciplines: The disciplines to chain; the coupled ones are solved
                together by the algorithm of ``settings``.
            settings: How the coupled groups are solved and accepted.
        """
        super().__init__(
            disciplines,
            inner_mda_name=settings.inner_mda_name,
            inner_mda_settings=_inner_mda_settings(settings),
        )
        self._residual_limit = settings.residual_limit

    def _solve(self) -> None:
        super()._solve()
        for mda in self.inner_mdas:
            last = float(mda.io.data[self.NORMALIZED_RESIDUAL_NORM][-1])
            # A NaN residual compares false, so it is rejected too.
            if not last <= self._residual_limit:
                couplings = ", ".join(sorted(mda.coupling_structure.strong_couplings))
                raise MDANotConvergedError(
                    f"MDA did not converge: residual {last:.1e} > "
                    f"{self._residual_limit:.1e} (couplings: {couplings})"
                )

    def disable_caches(self) -> None:
        """Stop the chain, its algorithms and nested processes from caching.

        A cached process replays its outputs for inputs it has already seen,
        so a stochastic tool inside it would be called only once. The caches
        of the tools themselves are left to the tools.
        """
        self.set_cache(CacheType.NONE)
        _disable_process_caches(self.mdo_chain)

__init__(disciplines, settings)

Build the chain.

Parameters:

Name Type Description Default
disciplines Sequence[Discipline]

The disciplines to chain; the coupled ones are solved together by the algorithm of settings.

required
settings MDASettings

How the coupled groups are solved and accepted.

required
Source code in src/mdo_framework/core/mda.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def __init__(self, disciplines: Sequence[Discipline], settings: MDASettings):
    """Build the chain.

    Args:
        disciplines: The disciplines to chain; the coupled ones are solved
            together by the algorithm of ``settings``.
        settings: How the coupled groups are solved and accepted.
    """
    super().__init__(
        disciplines,
        inner_mda_name=settings.inner_mda_name,
        inner_mda_settings=_inner_mda_settings(settings),
    )
    self._residual_limit = settings.residual_limit

disable_caches()

Stop the chain, its algorithms and nested processes from caching.

A cached process replays its outputs for inputs it has already seen, so a stochastic tool inside it would be called only once. The caches of the tools themselves are left to the tools.

Source code in src/mdo_framework/core/mda.py
132
133
134
135
136
137
138
139
140
def disable_caches(self) -> None:
    """Stop the chain, its algorithms and nested processes from caching.

    A cached process replays its outputs for inputs it has already seen,
    so a stochastic tool inside it would be called only once. The caches
    of the tools themselves are left to the tools.
    """
    self.set_cache(CacheType.NONE)
    _disable_process_caches(self.mdo_chain)

build_mda(disciplines, settings=None)

Chain the disciplines, solving the coupled ones as the settings say.

Parameters:

Name Type Description Default
disciplines Sequence[Discipline]

The disciplines of the study.

required
settings MDASettings | None

How to solve and accept the coupled groups; the sequential Gauss-Seidel defaults when omitted.

None

Returns:

Type Description
StrictMDAChain

A discipline whose evaluation raises MDANotConvergedError if a

StrictMDAChain

coupled group stops above its accepted residual.

Source code in src/mdo_framework/core/mda.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def build_mda(
    disciplines: Sequence[Discipline], settings: MDASettings | None = None
) -> StrictMDAChain:
    """Chain the disciplines, solving the coupled ones as the settings say.

    Args:
        disciplines: The disciplines of the study.
        settings: How to solve and accept the coupled groups; the sequential
            Gauss-Seidel defaults when omitted.

    Returns:
        A discipline whose evaluation raises ``MDANotConvergedError`` if a
        coupled group stops above its accepted residual.
    """
    return StrictMDAChain(disciplines, settings or MDASettings())