Skip to content

Commit

Permalink
Merge pull request OpenMDAO#536 from Kenneth-T-Moore/constraint
Browse files Browse the repository at this point in the history
Add support for constraint aliases to the phase info.
  • Loading branch information
Kenneth-T-Moore authored Sep 26, 2024
2 parents e7bacd9 + 16fdba2 commit b342170
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
44 changes: 44 additions & 0 deletions aviary/interface/test/test_height_energy_mission.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
import os
import unittest
import subprocess
Expand All @@ -7,6 +8,7 @@
from openmdao.utils.reports_system import clear_reports

from aviary.interface.methods_for_level1 import run_aviary
from aviary.interface.methods_for_level2 import AviaryProblem
from aviary.subsystems.test.test_dummy_subsystem import ArrayGuessSubsystemBuilder
from aviary.mission.energy_phase import EnergyPhase
from aviary.variable_info.variables import Dynamic
Expand Down Expand Up @@ -252,6 +254,48 @@ def test_custom_phase_builder_error(self):
optimizer='SLSQP',
)

def test_support_constraint_aliases(self):
# Test specification of multiple constraints on a single variable.
modified_phase_info = deepcopy(self.phase_info)
modified_phase_info['climb']['user_options']['constraints'] = {
'throttle_1': {
'target': Dynamic.Mission.THROTTLE,
'equals': 0.2,
'loc': 'initial',
'type': 'boundary',
},
'throttle_2': {
'target': Dynamic.Mission.THROTTLE,
'equals': 0.8,
'loc': 'final',
'type': 'boundary',
},
}

prob = AviaryProblem()

csv_path = "models/test_aircraft/aircraft_for_bench_FwFm.csv"

prob.load_inputs(csv_path, modified_phase_info)
prob.check_and_preprocess_inputs()
prob.add_pre_mission_systems()
prob.add_phases()
prob.add_post_mission_systems()
prob.link_phases()

prob.setup()
prob.set_initial_guesses()

prob.run_model()

prob_vars = prob.list_problem_vars()
cons = {key: val for (key, val) in prob_vars['constraints']}
con1 = cons['traj.phases.climb->initial_boundary_constraint->throttle_1']
con2 = cons['traj.phases.climb->final_boundary_constraint->throttle_2']

self.assertEqual(con1['name'], 'timeseries.throttle_1')
self.assertEqual(con2['name'], 'timeseries.throttle_2')


if __name__ == '__main__':
unittest.main()
10 changes: 9 additions & 1 deletion aviary/mission/phase_builder_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,15 @@ def _add_user_defined_constraints(self, phase, constraints):
for constraint_name, kwargs in constraints.items():
if kwargs['type'] == 'boundary':
kwargs.pop('type')
phase.add_boundary_constraint(constraint_name, **kwargs)

if 'target' in kwargs:
# Support for constraint aliases.
target = kwargs.pop('target')
kwargs['constraint_name'] = constraint_name
phase.add_boundary_constraint(target, **kwargs)
else:
phase.add_boundary_constraint(constraint_name, **kwargs)

elif kwargs['type'] == 'path':
kwargs.pop('type')
phase.add_path_constraint(constraint_name, **kwargs)
Expand Down

0 comments on commit b342170

Please sign in to comment.