Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import os
import sys
COLOR_TYPE = os.environ.get('COLOR_TYPE', 'auto')
from openmdao.api import ScipyOptimizeDriver
from openmdao.core.tests.test_coloring import run_opt
if __name__ == '__main__':
if len(sys.argv) != 3 or sys.argv[1] != '-f':
print("usage: python circle_coloring_needs_args.py -f bar")
sys.exit(2)
p_color = run_opt(ScipyOptimizeDriver, COLOR_TYPE, optimizer='SLSQP', disp=False,
dynamic_total_coloring=True, partial_coloring=False)
z=np.array([0.0, 0.0]), x=0.0))
self.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'))
self.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'))
self.connect('indeps.x', ['cycle.d1.x', 'obj_cmp.x'])
self.connect('indeps.z', ['cycle.d1.z', 'cycle.d2.z', 'obj_cmp.z'])
self.connect('cycle.d1.y1', ['obj_cmp.y1', 'con_cmp1.y1'])
self.connect('cycle.d2.y2', ['obj_cmp.y2', 'con_cmp2.y2'])
prob = om.Problem()
prob.model = SellarMDAConnect()
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
# prob.driver.options['maxiter'] = 100
prob.driver.options['tol'] = 1e-8
prob.set_solver_print(level=0)
prob.model.add_design_var('indeps.x', lower=0, upper=10)
prob.model.add_design_var('indeps.z', lower=0, upper=10)
prob.model.add_objective('obj_cmp.obj')
prob.model.add_constraint('con_cmp1.con1', upper=0)
prob.model.add_constraint('con_cmp2.con2', upper=0)
prob.setup()
prob['indeps.x'] = 2.
prob['indeps.z'] = [-1., -1.]
J['power', 'Vu'] = 6.0 * Area * Vu**2 * a * rho * one_minus_a**2
# build the model
prob = om.Problem()
indeps = prob.model.add_subsystem('indeps', om.IndepVarComp(), promotes=['*'])
indeps.add_output('a', .5)
indeps.add_output('Area', 10.0, units='m**2')
indeps.add_output('rho', 1.225, units='kg/m**3')
indeps.add_output('Vu', 10.0, units='m/s')
prob.model.add_subsystem('a_disk', ActuatorDisc(),
promotes_inputs=['a', 'Area', 'rho', 'Vu'])
# setup the optimization
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.model.add_design_var('a', lower=0., upper=1.)
prob.model.add_design_var('Area', lower=0., upper=1.)
# negative one so we maximize the objective
prob.model.add_objective('a_disk.Cp', scaler=-1)
prob.setup()
prob.run_driver()
# minimum value
assert_rel_error(self, prob['a_disk.Cp'], 16./27., 1e-4)
assert_rel_error(self, prob['a'], 0.33333, 1e-4)
# There is a bug in scipy version < 1.0 that causes this value to be wrong.
promotes=['delta_v'])
model.connect('dv1.delta_v', 'dv_total.dv1')
model.connect('dv2.delta_v', 'dv_total.dv2')
model.add_subsystem('dinc_total',
subsys=ExecComp('dinc=dinc1+dinc2',
dinc={'units': 'deg'},
dinc1={'units': 'deg'},
dinc2={'units': 'deg'}),
promotes=['dinc'])
model.connect('dinc1', 'dinc_total.dinc1')
model.connect('dinc2', 'dinc_total.dinc2')
prob.driver = ScipyOptimizeDriver()
model.add_design_var('dinc1', lower=0, upper=28.5)
model.add_design_var('dinc2', lower=0, upper=28.5)
model.add_constraint('dinc', lower=28.5, upper=28.5, scaler=1.0)
model.add_objective('delta_v', scaler=1.0)
# Setup the problem
prob.setup()
prob['mu'] = 398600.4418
prob['r1'] = 6778.137
prob['r2'] = 42164.0
prob['dinc1'] = 0
prob['dinc2'] = 28.5
if __name__ == "__main__":
model = om.Group()
ivc = om.IndepVarComp()
ivc.add_output('x', 3.0)
ivc.add_output('y', -4.0)
model.add_subsystem('des_vars', ivc)
model.add_subsystem('parab_comp', Paraboloid())
model.connect('des_vars.x', 'parab_comp.x')
model.connect('des_vars.y', 'parab_comp.y')
prob = om.Problem(model)
prob.driver = om.ScipyOptimizeDriver() # so 'openmdao cite' will report it for cite docs
prob.setup()
prob.run_model()
print(prob['parab_comp.f_xy'])
prob['des_vars.x'] = 5.0
prob['des_vars.y'] = -2.0
prob.run_model()
print(prob['parab_comp.f_xy'])
def test_ivp_driver_4_segs_7_order(self):
import openmdao.api as om
import dymos as dm
import matplotlib.pyplot as plt
plt.switch_backend('Agg') # disable plotting to the screen
from oscillator_ode import OscillatorODE
# Instantiate an OpenMDAO Problem instance.
prob = om.Problem()
# We need an optimization driver. To solve this simple problem ScipyOptimizerDriver will work.
prob.driver = om.ScipyOptimizeDriver()
# Instantiate a Dymos Trajectory and add it to the Problem model.
traj = dm.Trajectory()
prob.model.add_subsystem('traj', traj)
# Instantiate a Phase and add it to the Trajectory.
phase = dm.Phase(ode_class=OscillatorODE, transcription=dm.Radau(num_segments=4, order=7))
traj.add_phase('phase0', phase)
# Tell Dymos that the duration of the phase is bounded.
phase.set_time_options(fix_initial=True, fix_duration=True)
# Tell Dymos the states to be propagated using the given ODE.
phase.add_state('x', fix_initial=True, rate_source='v', targets=['x'], units='m')
phase.add_state('v', fix_initial=True, rate_source='v_dot', targets=['v'], units='m/s')
def test_ivp_solve_segments(self):
import openmdao.api as om
import dymos as dm
import matplotlib.pyplot as plt
plt.switch_backend('Agg') # disable plotting to the screen
from projectile_ode import ProjectileODE
# Instnatiate an OpenMDAO Problem instance.
prob = om.Problem()
# We need an optimization driver. To solve this simple problem ScipyOptimizerDriver will work.
prob.driver = om.ScipyOptimizeDriver()
# Instantiate a Dymos Trajectory and add it to the Problem model.
traj = dm.Trajectory()
prob.model.add_subsystem('traj', traj)
# Instantiate a Phase and add it to the Trajectory.
# Here the transcription is necessary but not particularly relevant.
phase = dm.Phase(ode_class=ProjectileODE, transcription=dm.Radau(num_segments=10, solve_segments=True))
traj.add_phase('phase0', phase)
# Tell Dymos that the time duration of the Phase should not be fixed (its a design variable for
# the optimization). The duration of a phase may be negative but it should never be zero.
phase.set_time_options(fix_initial=True, duration_bounds=(5, 50))
# Tell Dymos the states to be propagated using the given ODE.
phase.add_state('x', fix_initial=True, targets=None, rate_source='x_dot', units='m')
def vanderpol(transcription='gauss-lobatto', num_segments=8, transcription_order=3,
compressed=True, optimizer='SLSQP', use_pyoptsparse=False, delay=None):
"""Dymos problem definition for optimal control of a Van der Pol oscillator"""
# define the OpenMDAO problem
p = om.Problem(model=om.Group())
if not use_pyoptsparse:
p.driver = om.ScipyOptimizeDriver()
else:
p.driver = om.pyOptSparseDriver()
p.driver.options['optimizer'] = optimizer
if use_pyoptsparse and optimizer == 'SNOPT':
p.driver.opt_settings['iSumm'] = 6 # show detailed SNOPT output
p.driver.declare_coloring()
# define a Trajectory object and add to model
traj = dm.Trajectory()
p.model.add_subsystem('traj', subsys=traj)
# define a Transcription
if transcription == 'gauss-lobatto':
t = dm.GaussLobatto(num_segments=num_segments,
order=transcription_order,
compressed=compressed)
newton.linesearch.options['iprint'] = -1
# newton.linesearch.options['print_bound_enforce'] = False
# newton.linesearch.options['alpha'] = 0.5
prob.model.linear_solver = om.DirectSolver(assemble_jac=True)
# prob.model.jacobian = CSCJacobian()
# recorder = SqliteRecorder('N3_MDP.sql')
# setup the optimization
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['debug_print'] = ['desvars', 'nl_cons', 'objs']
prob.driver.opt_settings={'Major step limit': 0.05}
# prob.driver.recording_options['record_n2_data'] = False
prob.model.add_design_var('fan:PRdes', lower=1.20, upper=1.4)
prob.model.add_design_var('lpc:PRdes', lower=2.5, upper=4.0)
prob.model.add_design_var('OPR_simple', lower=40.0, upper=70.0, ref0=40.0, ref=70.0)
prob.model.add_design_var('RTO:T4max', lower=3000.0, upper=3600.0, ref0=3000.0, ref=3600.0)
prob.model.add_design_var('CRZ:VjetRatio', lower=1.35, upper=1.45, ref0=1.35, ref=1.45)
prob.model.add_design_var('TR', lower=0.5, upper=0.95, ref0=0.5, ref=0.95)
# prob.model.add_objective('CRZ.perf.TSFC')
prob.model.add_objective('CRZ.perf.TSFC', ref0=0.4, ref=0.5)