Skip to content

GraphProblemBuilder

mdo_framework.core.translator.GraphProblemBuilder

Builds a GEMSEO MDA from a typed study schema.

Source code in src/mdo_framework/core/translator.py
 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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 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
141
142
143
144
145
146
147
148
149
class GraphProblemBuilder:
    """Builds a GEMSEO MDA from a typed study schema."""

    def __init__(self, schema: StudySchema):
        """Initializes the builder with the given study schema.

        Args:
            schema: Typed description of the variables and tools, as returned
                by ``GraphManager.get_study_schema()``.
        """
        self.schema = schema
        self.variable_specs = build_variable_specs(schema)

    def build_problem(
        self,
        tool_registry: Mapping[str, Callable],
        mda_settings: MDASettings | None = None,
    ) -> StrictMDAChain:
        """Constructs a GEMSEO MDA from the study schema.

        Each tool gets its own defaults: the value of its fixed parameters, the
        initial guess of its state variables, and 0.0 for a coupling variable
        without an initial guess. Design variables have no default. The MDA
        gathers the defaults of its disciplines.

        The coupled tools are solved one after the other (Gauss-Seidel) unless
        ``mda_settings`` says otherwise. Evaluating the returned MDA raises
        ``MDANotConvergedError`` if a coupled group does not converge. If any
        tool is not deterministic, no process around the tools caches.

        Args:
            tool_registry: Dictionary mapping tool names to Python functions.
            mda_settings: How to solve the coupled tools; sequential defaults
                when omitted. Evaluating them in parallel requires every coupled
                tool to be declared ``thread_safe``.

        Returns:
            An instantiated GEMSEO MDA Discipline object.

        Raises:
            StudyValidationError: If a tool has no registered function or the
                registered function cannot take the tool's inputs by keyword, or
                if the MDA runs in parallel and a coupled tool is not declared
                ``thread_safe``. Every bad tool is reported in the same error.
            ValueError: If two tools produce the same output.
        """
        settings = mda_settings or MDASettings()
        report = validate_registry(self.schema, tool_registry)
        couplings = set(walk_dependencies(self.schema, []).couplings)
        errors = list(report.errors)
        if settings.n_processes > 1:
            errors.extend(thread_safety_findings(self.schema, couplings))
        if errors:
            raise StudyValidationError(
                ValidationReport(errors=tuple(errors), warnings=report.warnings)
            )

        disciplines = [
            ToolComponent(
                name=tool.name,
                func=tool_registry[tool.name],
                inputs=tool.inputs,
                outputs=tool.outputs,
                specs={
                    in_name: self.variable_specs[in_name]
                    for in_name in tool.inputs
                    if in_name in self.variable_specs
                },
                defaults=self._tool_defaults(tool, couplings),
                deterministic=tool.deterministic,
                arg_map=tool.arg_map,
            )
            for tool in self.schema.tools
        ]
        check_disciplines_consistency(disciplines, False, True)

        chain = build_mda(disciplines, settings)
        if not all(tool.deterministic for tool in self.schema.tools):
            chain.disable_caches()
        return chain

    def _tool_defaults(self, tool: ToolNode, couplings: set[str]) -> dict[str, Any]:
        variables = {variable.name: variable for variable in self.schema.variables}
        defaults: dict[str, Any] = {}
        for name in tool.inputs:
            variable = variables[name]
            if isinstance(variable, FixedParam):
                defaults[name] = to_design_value(
                    self.variable_specs.get(name), variable.value
                )
            elif isinstance(variable, StateVar):
                if variable.initial_guess is not None:
                    defaults[name] = np.asarray(variable.initial_guess, dtype=float)
                elif name in couplings:
                    defaults[name] = 0.0
        return defaults

__init__(schema)

Initializes the builder with the given study schema.

Parameters:

Name Type Description Default
schema StudySchema

Typed description of the variables and tools, as returned by GraphManager.get_study_schema().

required
Source code in src/mdo_framework/core/translator.py
57
58
59
60
61
62
63
64
65
def __init__(self, schema: StudySchema):
    """Initializes the builder with the given study schema.

    Args:
        schema: Typed description of the variables and tools, as returned
            by ``GraphManager.get_study_schema()``.
    """
    self.schema = schema
    self.variable_specs = build_variable_specs(schema)

build_problem(tool_registry, mda_settings=None)

Constructs a GEMSEO MDA from the study schema.

Each tool gets its own defaults: the value of its fixed parameters, the initial guess of its state variables, and 0.0 for a coupling variable without an initial guess. Design variables have no default. The MDA gathers the defaults of its disciplines.

The coupled tools are solved one after the other (Gauss-Seidel) unless mda_settings says otherwise. Evaluating the returned MDA raises MDANotConvergedError if a coupled group does not converge. If any tool is not deterministic, no process around the tools caches.

Parameters:

Name Type Description Default
tool_registry Mapping[str, Callable]

Dictionary mapping tool names to Python functions.

required
mda_settings MDASettings | None

How to solve the coupled tools; sequential defaults when omitted. Evaluating them in parallel requires every coupled tool to be declared thread_safe.

None

Returns:

Type Description
StrictMDAChain

An instantiated GEMSEO MDA Discipline object.

Raises:

Type Description
StudyValidationError

If a tool has no registered function or the registered function cannot take the tool's inputs by keyword, or if the MDA runs in parallel and a coupled tool is not declared thread_safe. Every bad tool is reported in the same error.

ValueError

If two tools produce the same output.

Source code in src/mdo_framework/core/translator.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 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
def build_problem(
    self,
    tool_registry: Mapping[str, Callable],
    mda_settings: MDASettings | None = None,
) -> StrictMDAChain:
    """Constructs a GEMSEO MDA from the study schema.

    Each tool gets its own defaults: the value of its fixed parameters, the
    initial guess of its state variables, and 0.0 for a coupling variable
    without an initial guess. Design variables have no default. The MDA
    gathers the defaults of its disciplines.

    The coupled tools are solved one after the other (Gauss-Seidel) unless
    ``mda_settings`` says otherwise. Evaluating the returned MDA raises
    ``MDANotConvergedError`` if a coupled group does not converge. If any
    tool is not deterministic, no process around the tools caches.

    Args:
        tool_registry: Dictionary mapping tool names to Python functions.
        mda_settings: How to solve the coupled tools; sequential defaults
            when omitted. Evaluating them in parallel requires every coupled
            tool to be declared ``thread_safe``.

    Returns:
        An instantiated GEMSEO MDA Discipline object.

    Raises:
        StudyValidationError: If a tool has no registered function or the
            registered function cannot take the tool's inputs by keyword, or
            if the MDA runs in parallel and a coupled tool is not declared
            ``thread_safe``. Every bad tool is reported in the same error.
        ValueError: If two tools produce the same output.
    """
    settings = mda_settings or MDASettings()
    report = validate_registry(self.schema, tool_registry)
    couplings = set(walk_dependencies(self.schema, []).couplings)
    errors = list(report.errors)
    if settings.n_processes > 1:
        errors.extend(thread_safety_findings(self.schema, couplings))
    if errors:
        raise StudyValidationError(
            ValidationReport(errors=tuple(errors), warnings=report.warnings)
        )

    disciplines = [
        ToolComponent(
            name=tool.name,
            func=tool_registry[tool.name],
            inputs=tool.inputs,
            outputs=tool.outputs,
            specs={
                in_name: self.variable_specs[in_name]
                for in_name in tool.inputs
                if in_name in self.variable_specs
            },
            defaults=self._tool_defaults(tool, couplings),
            deterministic=tool.deterministic,
            arg_map=tool.arg_map,
        )
        for tool in self.schema.tools
    ]
    check_disciplines_consistency(disciplines, False, True)

    chain = build_mda(disciplines, settings)
    if not all(tool.deterministic for tool in self.schema.tools):
        chain.disable_caches()
    return chain