Skip to content

Commit

Permalink
Merge 255b652 into 5c82972
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth-T-Moore committed Jul 16, 2020
2 parents 5c82972 + 255b652 commit 0dd0952
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
65 changes: 33 additions & 32 deletions openmdao/core/tests/test_coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,47 +698,40 @@ def compute(self, inputs, outputs):
SIZE = 10

p = om.Problem()

indeps = p.model.add_subsystem('indeps', om.IndepVarComp(), promotes_outputs=['*'])

# the following were randomly generated using np.random.random(10)*2-1 to randomly
# disperse them within a unit circle centered at the origin.
indeps.add_output('x', np.array([ 0.55994437, -0.95923447, 0.21798656, -0.02158783, 0.62183717,
0.04007379, 0.46044942, -0.10129622, 0.27720413, -0.37107886]))
indeps.add_output('y', np.array([ 0.52577864, 0.30894559, 0.8420792 , 0.35039912, -0.67290778,
-0.86236787, -0.97500023, 0.47739414, 0.51174103, 0.10052582]))
indeps.add_output('r', .7)
model = p.model

########################################################################
# DynamicPartialsComp is set up to do dynamic partial coloring
arctan_yox = p.model.add_subsystem('arctan_yox', DynamicPartialsComp(SIZE))
arctan_yox = model.add_subsystem('arctan_yox', DynamicPartialsComp(SIZE),
promotes_inputs=['x', 'y'])
########################################################################

p.model.add_subsystem('circle', om.ExecComp('area=pi*r**2'))
model.add_subsystem('circle', om.ExecComp('area=pi*r**2'),
promotes_inputs=['r'])

p.model.add_subsystem('r_con', om.ExecComp('g=x**2 + y**2 - r', has_diag_partials=True,
g=np.ones(SIZE), x=np.ones(SIZE), y=np.ones(SIZE)))
model.add_subsystem('r_con', om.ExecComp('g=x**2 + y**2 - r', has_diag_partials=True,
g=np.ones(SIZE), x=np.ones(SIZE), y=np.ones(SIZE)),
promotes_inputs=['x', 'y', 'r'])

thetas = np.linspace(0, np.pi/4, SIZE)
p.model.add_subsystem('theta_con', om.ExecComp('g = x - theta', has_diag_partials=True,
model.add_subsystem('theta_con', om.ExecComp('g = x - theta', has_diag_partials=True,
g=np.ones(SIZE), x=np.ones(SIZE),
theta=thetas))
p.model.add_subsystem('delta_theta_con', om.ExecComp('g = even - odd', has_diag_partials=True,
model.add_subsystem('delta_theta_con', om.ExecComp('g = even - odd', has_diag_partials=True,
g=np.ones(SIZE//2), even=np.ones(SIZE//2),
odd=np.ones(SIZE//2)))

p.model.add_subsystem('l_conx', om.ExecComp('g=x-1', has_diag_partials=True, g=np.ones(SIZE), x=np.ones(SIZE)))
model.add_subsystem('l_conx', om.ExecComp('g=x-1', has_diag_partials=True,
g=np.ones(SIZE), x=np.ones(SIZE)),
promotes_inputs=['x'])

IND = np.arange(SIZE, dtype=int)
ODD_IND = IND[1::2] # all odd indices
EVEN_IND = IND[0::2] # all even indices

p.model.connect('r', ('circle.r', 'r_con.r'))
p.model.connect('x', ['r_con.x', 'arctan_yox.x', 'l_conx.x'])
p.model.connect('y', ['r_con.y', 'arctan_yox.y'])
p.model.connect('arctan_yox.g', 'theta_con.x')
p.model.connect('arctan_yox.g', 'delta_theta_con.even', src_indices=EVEN_IND)
p.model.connect('arctan_yox.g', 'delta_theta_con.odd', src_indices=ODD_IND)
model.connect('arctan_yox.g', 'theta_con.x')
model.connect('arctan_yox.g', 'delta_theta_con.even', src_indices=EVEN_IND)
model.connect('arctan_yox.g', 'delta_theta_con.odd', src_indices=ODD_IND)

p.driver = om.ScipyOptimizeDriver()
p.driver.options['optimizer'] = 'SLSQP'
Expand All @@ -749,26 +742,34 @@ def compute(self, inputs, outputs):
p.driver.declare_coloring(show_summary=True, show_sparsity=True)
#####################################

p.model.add_design_var('x')
p.model.add_design_var('y')
p.model.add_design_var('r', lower=.5, upper=10)
model.add_design_var('x')
model.add_design_var('y')
model.add_design_var('r', lower=.5, upper=10)

# nonlinear constraints
p.model.add_constraint('r_con.g', equals=0)
model.add_constraint('r_con.g', equals=0)

p.model.add_constraint('theta_con.g', lower=-1e-5, upper=1e-5, indices=EVEN_IND)
p.model.add_constraint('delta_theta_con.g', lower=-1e-5, upper=1e-5)
model.add_constraint('theta_con.g', lower=-1e-5, upper=1e-5, indices=EVEN_IND)
model.add_constraint('delta_theta_con.g', lower=-1e-5, upper=1e-5)

# this constrains x[0] to be 1 (see definition of l_conx)
p.model.add_constraint('l_conx.g', equals=0, linear=False, indices=[0,])
model.add_constraint('l_conx.g', equals=0, linear=False, indices=[0,])

# linear constraint
p.model.add_constraint('y', equals=0, indices=[0,], linear=True)
model.add_constraint('y', equals=0, indices=[0,], linear=True)

p.model.add_objective('circle.area', ref=-1)
model.add_objective('circle.area', ref=-1)

p.setup(mode='fwd')

# the following were randomly generated using np.random.random(10)*2-1 to randomly
# disperse them within a unit circle centered at the origin.
p.set_val('x', np.array([ 0.55994437, -0.95923447, 0.21798656, -0.02158783, 0.62183717,
0.04007379, 0.46044942, -0.10129622, 0.27720413, -0.37107886]))
p.set_val('y', np.array([ 0.52577864, 0.30894559, 0.8420792 , 0.35039912, -0.67290778,
-0.86236787, -0.97500023, 0.47739414, 0.51174103, 0.10052582]))
p.set_val('r', .7)

# coloring info will be displayed during run_driver. The number of colors in the
# partial coloring of arctan_yox should be 2 and the number of colors in the
# total coloring should be 5.
Expand Down
12 changes: 6 additions & 6 deletions openmdao/core/tests/test_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def __imul__(self, val):
val = val.getval()
self._val *= val
return self

def __eq__(self, val):
if isinstance(val, _DiscreteVal):
return self._val == val.getval()
Expand Down Expand Up @@ -814,6 +814,7 @@ def _var_iter(obj):


class DiscreteFeatureTestCase(unittest.TestCase):

def test_feature_discrete(self):
import numpy as np
import openmdao.api as om
Expand Down Expand Up @@ -841,16 +842,15 @@ def compute(self, inputs, outputs, discrete_inputs, discrete_outputs):

# build the model
prob = om.Problem()
indeps = prob.model.add_subsystem('indeps', om.IndepVarComp(), promotes=['*'])
indeps.add_output('r_m', 3.2, units="ft")
indeps.add_output('chord', .3, units='ft')

prob.model.add_subsystem('SolidityComp', BladeSolidity(),
promotes_inputs=['r_m', 'chord', 'num_blades'])

prob.setup()

prob['num_blades'] = 2
prob.set_val('num_blades', 2)
prob.set_val('r_m', 3.2)
prob.set_val('chord', .3)

prob.run_model()

Expand Down Expand Up @@ -905,7 +905,7 @@ def guess_nonlinear(self, inputs, outputs, resids, discrete_inputs, discrete_out
prob.setup()
prob.run_model()

assert_near_equal(prob['comp.x'], 3., 1e-4)
assert_near_equal(prob.get_val('comp.x'), 3., 1e-4)


if __name__ == "__main__":
Expand Down

0 comments on commit 0dd0952

Please sign in to comment.