-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add mphys helper function for adding constraints * Alter composite optimisation example to use new mphys helper function * Add a function decorator for logging function calls to TACS MPhys components * Revert "Add a function decorator for logging function calls to TACS MPhys components" This reverts commit e4e9363. * Remove unnecessary model input * Test `add_tacs_constraints` in mphys integration test * Need to add a nonlinear constraint to get around OpenMDAO bug * Add an objective too for realism
- Loading branch information
Showing
4 changed files
with
116 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,4 @@ | ||
from tacs.mphys.builder import TacsBuilder | ||
import tacs.mphys.utils | ||
|
||
__all__ = ["TacsBuilder"] | ||
__all__ = ["TacsBuilder", "utils"] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import warnings | ||
import tacs | ||
|
||
|
||
def add_tacs_constraints(scenario): | ||
"""Call this in the configure method to add all TACS constraints functions | ||
in a TacsPostcouplingGroup as constraints in an OpenMDAO model. This saves | ||
you having to call OpenMDAO's `add_constraint` method for each constraint | ||
function yourself. | ||
This function relies on you having defined the lower and/or upper bounds | ||
of the constraints when you created them in your `constraint_setup` | ||
function. | ||
Parameters | ||
---------- | ||
scenario : MPhys scenario | ||
Scenario containing a TACS postcoupling group with constraints | ||
""" | ||
# First, verify that the model has a struct_post component that is a | ||
# TacsPostcouplingGroup | ||
if not hasattr(scenario, "struct_post"): | ||
raise ValueError( | ||
f"Scenario {scenario.name} does not have a struct_post component" | ||
) | ||
if not isinstance( | ||
scenario.struct_post, tacs.mphys.postcoupling.TacsPostcouplingGroup | ||
): | ||
raise ValueError( | ||
f"{scenario.name}.struct_post is not a TacsPostcouplingGroup, cannot add TACS constraints" | ||
) | ||
# If the provided scenario has no constraints component, warn the user and return | ||
if not hasattr(scenario.struct_post, "constraints"): | ||
warnings.warn( | ||
f"Scenario {scenario.name} contains no TACS constraints to add", | ||
stacklevel=2, | ||
) | ||
return | ||
else: | ||
for system in scenario.struct_post.constraints.system_iter(): | ||
constraint = system.constr | ||
constraintFuncNames = constraint.getConstraintKeys() | ||
bounds = {} | ||
constraint.getConstraintBounds(bounds) | ||
for conName in constraintFuncNames: | ||
if scenario.comm.rank == 0: | ||
print("Adding TACS constraint: ", conName) | ||
name = f"{system.name}.{conName}" | ||
# Panel length constraints are nonlinear, all other constrain | ||
# types (DV and adjacency) are linear | ||
if isinstance( | ||
constraint, | ||
tacs.constraints.panel_length.PanelLengthConstraint, | ||
): | ||
scenario.add_constraint(name, equals=0.0, scaler=1.0) | ||
else: | ||
lb = bounds[f"{system.name}_{conName}"][0] | ||
ub = bounds[f"{system.name}_{conName}"][1] | ||
if all(lb == ub): | ||
scenario.add_constraint(name, equals=lb, linear=True) | ||
else: | ||
scenario.add_constraint(name, lower=lb, upper=ub, linear=True) |
This file contains 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