Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor multipoint groups to not use multiple inheritance. #64

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/basics/model_hierarchy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ In the ``setup`` method of the Multipoint group, the following steps must be don
Additionally, the Multipoint group can hold the design variables or other inputs and subsystems to be evaluated after the scenarios.
These extra subsystem can then be connected to the scenarios by the user.

.. automodule:: mphys.multipoint

.. autoclass:: Multipoint
:members:
:exclude-members: configure

------------------
MultipointParallel
------------------
Expand All @@ -85,3 +91,7 @@ In the ``setup`` method of the ``MultipointParallel`` group, the following steps

Because this is a ParallelGroup, other subsystems that hold the inputs outside the scenario and subsystems to be evaluated after the scenarios cannot be added directly to the MultipointParallel group.
These extra subsystem should be added to a higher level of the model and then connected to the scenarios by the user.

.. autoclass:: MultipointParallel
:members:
:exclude-members: configure
108 changes: 79 additions & 29 deletions mphys/multipoint.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
import openmdao.api as om

class MultipointBase:
def __init__(self):
self.mphys_coupling_solvers = []

def mphys_add_scenario(self, name, scenario, coupling_nonlinear_solver=None,
coupling_linear_solver=None):
solver_tuple = (coupling_nonlinear_solver, coupling_linear_solver)
self.mphys_coupling_solvers.append((scenario,solver_tuple))
return self.add_subsystem(name,scenario)
def set_coupling_algorithms_in_scenarios(multipoint_group):
"""
Set the stored linear and nonlinear solver into the coupling group if the
scenario exists on this proc.
Shared method between multipoint and multipoint parallel groups.
Called during configure().
"""
for scenario, solvers in multipoint_group.mphys_coupling_solvers:
if solvers[0] is not None and scenario.comm:
scenario.coupling.nonlinear_solver = solvers[0]

def configure(self):
self._mphys_set_coupling_algorithms_in_scenarios()
if solvers[1] is not None and scenario.comm:
scenario.coupling.linear_solver = solvers[1]

def _mphys_set_coupling_algorithms_in_scenarios(self):
for scenario, solvers in self.mphys_coupling_solvers:
if solvers[0] is not None and scenario.comm:
scenario.coupling.nonlinear_solver = solvers[0]

if solvers[1] is not None and scenario.comm:
scenario.coupling.linear_solver = solvers[1]
class Multipoint(om.Group):
"""
An extension of the standard OpenMDAO group that adds the :func:`~mphys_add_scenario` method.
For sequential evaluations of the Mphys scenarios.
"""

class Multipoint(om.Group,MultipointBase):
def __init__(self, **kwargs):
MultipointBase.__init__(self)
om.Group.__init__(self, **kwargs)
self.mphys_coupling_solvers = []
super().__init__(**kwargs)

def mphys_add_scenario(self, name, scenario, coupling_nonlinear_solver=None,
coupling_linear_solver=None):
"""
Add an Mphys scenario

Parameters
----------
name : str
The name of the scenario
Scenario: :class:`~mphys.scenario.Scenario`
The scenario object
coupling_nonlinear_solver: openmdao.solvers.solver.NonlinearSolver
The nonlinear solver to assign to the coupling group primal problem
coupling_linear_solver: openmdao.solvers.solver.LinearSolver
The linear solver to to assign to the coupling group sensitivity problem
"""
solver_tuple = (coupling_nonlinear_solver, coupling_linear_solver)
self.mphys_coupling_solvers.append((scenario, solver_tuple))
return self.add_subsystem(name, scenario)

def mphys_connect_scenario_coordinate_source(self, source, scenarios, disciplines):
"""
A helper method to aid in connecting mesh coordinate sources to the scenarios
in this multipoint group.
The source and target variable names are assumed to be `'x_{discipline}0'`

Parameters
----------
disciplines : str or list[str]
The extension of the after the underscore in x_ for
source: openmdao.api.Group or openmdao.api.Component

scenario_list : str or list[str]
The source subsystem for the mesh coordinate variables
scenarios : str or list[str]
The names of the scenarios to be the target of the connections
disciplines : str or list[str]
The names of the disciplines for the mesh coordinates.

"""
scenarios_list = scenarios if type(scenarios) == list else [scenarios]
Expand All @@ -45,15 +69,41 @@ def mphys_connect_scenario_coordinate_source(self, source, scenarios, discipline
for discipline in disciplines_list:
src = f'{source}.x_{discipline}0'
target = f'{scenario}.x_{discipline}0'
self.connect(src,target)
self.connect(src, target)

def configure(self):
return MultipointBase.configure(self)
return set_coupling_algorithms_in_scenarios(self)


class MultipointParallel(om.ParallelGroup):
"""
An OpenMDAO parallel group that adds the :func:`~mphys_add_scenario` method.
For simultaneous evaluations of the Mphys scenarios.
"""

class MultipointParallel(om.ParallelGroup, MultipointBase):
def __init__(self, **kwargs):
MultipointBase.__init__(self)
om.ParallelGroup.__init__(self, **kwargs)
self.mphys_coupling_solvers = []
super().__init__(**kwargs)

def mphys_add_scenario(self, name, scenario, coupling_nonlinear_solver=None,
coupling_linear_solver=None):
"""
Add an Mphys scenario

Parameters
----------
name : str
The name of the scenario
Scenario: :class:`~mphys.scenario.Scenario`
The scenario object
coupling_nonlinear_solver: openmdao.solvers.solver.NonlinearSolver
The nonlinear solver to assign to the coupling group primal problem
coupling_linear_solver: openmdao.solvers.solver.LinearSolver
The linear solver to to assign to the coupling group sensitivity problem
"""
solver_tuple = (coupling_nonlinear_solver, coupling_linear_solver)
self.mphys_coupling_solvers.append((scenario, solver_tuple))
return self.add_subsystem(name, scenario)

def configure(self):
return MultipointBase.configure(self)
return set_coupling_algorithms_in_scenarios(self)