Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if MPI:
expected = [('c2.y', 'c3.y')]
else:
expected = [('c2.y',), ('c3.y',)]
self.assertEqual(prob.driver.outputs_of_interest(),
expected)
prob.setup(check=False)
prob.run()
unknown_list = ['c2.y', 'c3.y']
indep_list = ['p.x']
J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict')
assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6)
assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6)
def test_fan_in(self):
prob = Problem(impl=impl)
prob.root = FanInGrouped()
prob.root.ln_solver = PetscKSP()
prob.setup(check=False)
indep_list = ['p1.x1', 'p2.x2']
unknown_list = ['comp3.y']
J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict')
assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6)
assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict')
assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6)
assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
prob.driver.add_objective('sub.obj')
prob.driver.add_constraint('sub.con1', upper=0.0)
prob.driver.add_constraint('sub.con2', upper=0.0)
#prob.driver.recorders.append(SqliteRecorder("sellar.db"))
prob.setup(check=False)
prob.run()
tol = 1.e-3
assert_rel_error(self, prob['sub.obj'], 3.1833940, tol)
assert_rel_error(self, prob['z'][0], 1.977639, tol)
assert_rel_error(self, prob['z'][1], 0.0, tol)
assert_rel_error(self, prob['x'], 0.0, tol)
def test_single_diamond(self):
prob = Problem(impl=impl)
prob.root = SingleDiamondPar()
prob.root.ln_solver = PetscKSP()
prob.setup(check=False)
prob.run()
indep_list = ['p.x']
unknown_list = ['comp4.y1', 'comp4.y2']
J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict')
assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6)
assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6)
J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict')
assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6)
assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6)
prob = Problem(impl=impl)
prob.root = ConvergeDivergePar()
prob.root.ln_solver = PetscKSP()
prob.setup(check=False)
prob.run()
# Make sure value is fine.
assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6)
indep_list = ['p.x']
unknown_list = ['comp7.y1']
J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict')
assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict')
assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='dict')
assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
root.connect('par.c2.y', 'sumcomp.x2')
driver = model.driver = pyOptSparseDriver()
driver.options['optimizer'] = OPTIMIZER
driver.options['print_results'] = False
driver.add_desvar('p1.x', lower=-100, upper=100)
driver.add_desvar('p2.x', lower=-100, upper=100)
driver.add_objective('sumcomp.sum')
root.fd_options['force_fd'] = True
model.setup(check=False)
model.run()
if not MPI or self.comm.rank == 0:
assert_rel_error(self, model['p1.x'], 2.0, 1.e-6)
assert_rel_error(self, model['p2.x'], 3.0, 1.e-6)
if MPI:
expected = [('c2.y', 'c3.y')]
else:
expected = [('c2.y',), ('c3.y',)]
self.assertEqual(prob.driver.outputs_of_interest(),
expected)
prob.setup(check=False)
prob.run()
unknown_list = ['c2.y', 'c3.y']
indep_list = ['p.x']
J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict')
assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6)
assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6)
top.driver.options['optimizer'] = 'SLSQP'
top.driver.options['tol'] = 1.0e-8
top.driver.options['disp'] = False
top.driver.add_desvar('z', lower=np.array([-10.0, 0.0]),
upper=np.array([10.0, 10.0]))
top.driver.add_desvar('x', lower=0.0, upper=10.0)
top.driver.add_objective('obj')
top.driver.add_constraint('con1', upper=0.0)
top.driver.add_constraint('con2', upper=0.0)
top.setup(check=False)
top.run()
assert_rel_error(self, top['z'][0], 1.977639, 1e-5)
assert_rel_error(self, top['z'][1], 0.0, 1e-5)
assert_rel_error(self, top['x'], 0.0, 1e-5)
assert_rel_error(self, top['obj'], 3.1833940, 1e-5)
root.connect('parabola.y', 'bal.y2')
root.connect('bal.x', 'line.x')
root.connect('bal.x', 'parabola.x')
root.nl_solver = Newton()
root.ln_solver = ScipyGMRES()
top.setup(check=False)
stream = cStringIO()
# Positive solution
top['bal.x'] = 7.0
root.list_states(stream)
top.run()
assert_rel_error(self, top['bal.x'], 1.430501, 1e-5)
assert_rel_error(self, top['line.y'], 1.138998, 1e-5)
# Negative solution
top['bal.x'] = -7.0
root.list_states(stream)
top.run()
assert_rel_error(self, top['bal.x'], -2.097168, 1e-5)
assert_rel_error(self, top['line.y'], 8.194335, 1e-5)
def test_parallel_array_comps_fwd(self):
prob = self.prob
prob.root.ln_solver.options['mode'] = 'fwd'
prob.root.par.ln_solver.options['mode'] = 'fwd'
prob.root.par.ser1.ln_solver.options['mode'] = 'fwd'
prob.root.par.ser2.ln_solver.options['mode'] = 'fwd'
prob.setup(check=False)
prob.run()
assert_rel_error(self, prob['total.obj'], 50.0, 1e-6)