-
Notifications
You must be signed in to change notification settings - Fork 61
MPhys wrapper improvements #279
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
Open
A-CGray
wants to merge
9
commits into
main
Choose a base branch
from
mphysError
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−17
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
885b72d
Add helpful error message if `nom_getDVGeo` is called before `setup`
A-CGray 1956cd8
Ignore MPhys testing output
A-CGray 3b7143e
Improve `nom_addPointSet`
A-CGray 7bba4ea
Add docstring for `nom_addPointSet`
A-CGray d3ffb46
Turn off OpenMDAO reports in MPhys tests
A-CGray 01cfee5
Initialize `self.update_jac` in `initialize`
A-CGray 74d2e66
Fix derivatives for non-distributed pointsets
A-CGray 9ef2fa3
Add test for non-distributed pointset with parallel MPhys
A-CGray f3e7bc8
isort
A-CGray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ doc/_build | |
input_files | ||
.isort.cfg | ||
*.vscode | ||
tests/reg_tests/reports/ | ||
tests/reg_tests/test_MPhysGeo*_out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Standard Python modules | ||
import inspect | ||
|
||
# External modules | ||
from mpi4py import MPI | ||
import numpy as np | ||
|
@@ -36,6 +39,8 @@ def initialize(self): | |
# since `nom_add_discipline_coords` can be called before `setup` | ||
self.omPtInOutDict = {} | ||
|
||
self.update_jac = True | ||
|
||
def setup(self): | ||
# create a constraints object to go with this DVGeo(s) | ||
self.DVCon = DVConstraints() | ||
|
@@ -97,7 +102,7 @@ def setup(self): | |
for _, DVGeo in self.DVGeos.items(): | ||
self.DVCon.setDVGeo(DVGeo, name=DVConName) | ||
|
||
self.omPtSetList = [] | ||
self.omPtSetList = {} | ||
|
||
def compute(self, inputs, outputs): | ||
# check for inputs that have been added but the points have not been added to dvgeo | ||
|
@@ -221,26 +226,47 @@ def nom_add_discipline_coords(self, discipline, points=None, DVGeoName=None, **k | |
|
||
else: | ||
# we are provided with points. we can do the full initialization now | ||
self.nom_addPointSet(points, outputName, add_output=False, DVGeoName=DVGeoName, **kwargs) | ||
self.nom_addPointSet(points, outputName, add_output=False, DVGeoName=DVGeoName, distributed=True, **kwargs) | ||
self.add_input(inputName, distributed=True, val=points.flatten()) | ||
self.add_output(outputName, distributed=True, val=points.flatten()) | ||
|
||
def nom_addPointSet(self, points, ptName, add_output=True, DVGeoName=None, **kwargs): | ||
def nom_addPointSet(self, points, ptName, add_output=True, DVGeoName=None, distributed=True, **kwargs): | ||
"""Add a pointset to the DVGeo object and create an output for it in the OpenMDAO component. | ||
|
||
Parameters | ||
---------- | ||
points : numpy array | ||
3D points to add to the DVGeo object, shape (N,3) or (3N,) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is "3N," a typo or intentional? |
||
ptName : str | ||
Name for the pointset | ||
add_output : bool, optional | ||
Whether to add the deformed points as an output of the component, by default True | ||
DVGeoName : str, optional | ||
The name of the DVGeo to add the points to, necessary if there are multiple DVGeo objects. By default `None`. | ||
distributed : bool, optional | ||
Whether the output of the component should be a distributed variable, by default True | ||
|
||
Returns | ||
------- | ||
None or float | ||
If using DVGeometryESP or DVGeometryVSP, returns the maximum distance between pointset and the CAD model | ||
""" | ||
# if we have multiple DVGeos use the one specified by name | ||
DVGeo = self.nom_getDVGeo(DVGeoName=DVGeoName) | ||
|
||
# add the points to the dvgeo object | ||
dMaxGlobal = None | ||
if isinstance(DVGeo, DVGeometryESP): | ||
# DVGeoESP can return a value to check the pointset distribution | ||
dMaxGlobal = DVGeo.addPointSet(points.reshape(len(points) // 3, 3), ptName, **kwargs) | ||
# DVGeoESP and DVGeoVSP can return a value to check the pointset distribution | ||
# Also, the addPointSet method for some DVGeos takes a distributed kwarg, in which case we can pass it | ||
sig = inspect.signature(DVGeo.addPointSet) | ||
if "distributed" in sig.parameters: | ||
dMaxGlobal = DVGeo.addPointSet(points.reshape(-1, 3), ptName, distributed=distributed, **kwargs) | ||
else: | ||
DVGeo.addPointSet(points.reshape(len(points) // 3, 3), ptName, **kwargs) | ||
self.omPtSetList.append(ptName) | ||
dMaxGlobal = DVGeo.addPointSet(points.reshape(-1, 3), ptName, **kwargs) | ||
self.omPtSetList[ptName] = {"distributed": distributed} | ||
|
||
if add_output: | ||
# add an output to the om component | ||
self.add_output(ptName, distributed=True, val=points.flatten()) | ||
self.add_output(ptName, distributed=distributed, val=points.flatten()) | ||
|
||
return dMaxGlobal | ||
|
||
|
@@ -269,6 +295,12 @@ def nom_getDVGeo(self, childName=None, comp=None, DVGeoName=None): | |
DVGeometry object | ||
DVGeometry object held by this geometry component | ||
""" | ||
# Calling this function before setup is not allowed because the DVGeo object(s) do not exist yet | ||
if not hasattr(self, "DVGeos"): | ||
raise RuntimeError( | ||
"Cannot call `nom_getDVGeo` before OM_DVGEOCOMP's `setup` method has been called. If you are calling this function in the `setup` method of a group containing an OM_DVGEOCOMP, move the call to `configure` instead." | ||
) from None | ||
|
||
# if we have multiple DVGeos use the one specified by name | ||
if self.multDVGeo: | ||
DVGeo = self.DVGeos[DVGeoName] | ||
|
@@ -1100,8 +1132,11 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode): | |
for k in xdot: | ||
# check if this dv is present | ||
if k in d_inputs: | ||
# do the allreduce | ||
xdotg[k] = self.comm.allreduce(xdot[k], op=MPI.SUM) | ||
# do the allreduce if the pointset is distributed | ||
if self.omPtSetList[ptSetName]["distributed"]: | ||
xdotg[k] = self.comm.allreduce(xdot[k], op=MPI.SUM) | ||
else: | ||
xdotg[k] = xdot[k] | ||
|
||
# accumulate in the dict | ||
d_inputs[k] += xdotg[k][0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change the variable name too now that it is a dictionary?