def test_non_diff(self): # Test grouping comps with non-differentiable connections. model = set_as_top(Assembly()) model.add('driver', SimpleDriver()) model.add('comp1', ND_Send()) model.add('comp2', ND_Receive()) model.connect('comp1.y', 'comp2.x') model.connect('comp1.n', 'comp2.n') model.driver.workflow.add(['comp1', 'comp2']) model.run() inputs = ['comp1.x'] outputs = ['comp2.y'] J = model.driver.calc_gradient(inputs, outputs, mode='forward') self.assertAlmostEqual(J[0, 0], 2.5) msystem = model.driver.workflow._system self.assertTrue(len(msystem.subsystems()) == 2) self.assertTrue( len(msystem.subsystems()[1]._inner_system.subsystems()) == 3) self.assertTrue(msystem.subsystems()[1]._inner_system.subsystems() [1].name == 'comp1') self.assertTrue(msystem.subsystems()[1]._inner_system.subsystems() [2].name == 'comp2') J = model.driver.calc_gradient(inputs, outputs, mode='fd')
def test_derivative_no_deriv(self): model = set_as_top(Assembly()) model.add('driver', SimpleDriver()) model.add('comp', MyComp_No_Deriv()) model.driver.workflow.add('comp') model.comp.eval_only = False model.run() J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp.y_out']) #print J assert_rel_error(self, J[0][0], 0.75, 2e-3) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp.y_out'], mode='fd') #print J assert_rel_error(self, J[0][0], 0.75, 2e-3) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp.y_out'], mode='adjoint') #print J assert_rel_error(self, J[0][0], 0.75, 2e-3)
def configure(self): self.add('driver', SimpleDriver()) self.driver.gradient_options.force_fd = True self.driver.add_parameter('x', low=array([-10, -10]), high=array([10, 10])) self.driver.add_objective('(x[0]-1)**2 + (x[1]-1)**2')
def configure(self): self.add('d1', Discipline1_WithDerivatives()) self.d1.x1 = 1.0 self.d1.y1 = 1.0 self.d1.y2 = 1.0 self.d1.z1 = 5.0 self.d1.z2 = 2.0 self.add('d2', Discipline2_WithDerivatives()) self.d2.y1 = 1.0 self.d2.y2 = 1.0 self.d2.z1 = 5.0 self.d2.z2 = 2.0 self.add('P1', Paraboloid()) self.add('P2', Paraboloid()) self.connect('d1.y1', 'd2.y1') self.connect('P1.f_xy', 'd1.x1') self.connect('d1.y1', 'P2.x') self.connect('d2.y2', 'P2.y') self.add('driver', SimpleDriver()) self.add('driver2', Driver()) self.add('subdriver', NewtonSolver()) self.driver.workflow.add(['P1', 'subdriver', 'P2']) self.subdriver.workflow.add(['d1', 'd2']) self.subdriver.add_parameter('d1.y2', low=-1e99, high=1e99) self.subdriver.add_constraint('d1.y2 = d2.y2') self.driver.add_parameter('P1.x', low=-1e99, high=1e99) self.driver.add_constraint('P2.f_xy < 0')
def test_derivative_state_connection_internal_solve_apply_deriv(self): model = set_as_top(Assembly()) model.add('driver', SimpleDriver()) model.add('comp', MyComp_Deriv()) model.comp.add('c', Float(2.0, iotype="in")) model.comp.eval_only = False model.add('comp2', ExecCompWithDerivatives(["y=2*x"], ["dy_dx=2"])) model.driver.workflow.add(['comp', 'comp2']) model.connect('comp.z', 'comp2.x') model.run() #print model.comp.x, model.comp.y, model.comp.z, model.comp.res J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp2.y']) assert_rel_error(self, J[0][0], -0.1666, 2e-3) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp2.y'], mode='adjoint') assert_rel_error(self, J[0][0], -0.1666, 2e-3) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp2.y'], mode='fd') assert_rel_error(self, J[0][0], -0.1666, 2e-3)
def test_linearGS_single_comp(self): top = set_as_top(Assembly()) top.add('comp', Paraboloid()) top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp']) top.driver.add_parameter('comp.x', low=-1000, high=1000) top.driver.add_parameter('comp.y', low=-1000, high=1000) top.driver.add_objective('comp.f_xy') top.driver.gradient_options.lin_solver = 'linear_gs' top.driver.gradient_options.maxiter = 1 top.comp.x = 3 top.comp.y = 5 top.run() self.assertEqual(top.comp.f_xy, 93.) self.assertEqual(top._pseudo_0.out0, 93.) J = top.driver.workflow.calc_gradient(inputs=['comp.x', 'comp.y'], outputs=['comp.f_xy'], mode='forward') assert_rel_error(self, J[0, 0], 5.0, 0.0001) assert_rel_error(self, J[0, 1], 21.0, 0.0001) J = top.driver.workflow.calc_gradient(inputs=['comp.x', 'comp.y'], mode='adjoint') assert_rel_error(self, J[0, 0], 5.0, 0.0001) assert_rel_error(self, J[0, 1], 21.0, 0.0001)
def test_linearGS_simul_element_and_full_connection(self): # Added because of a bug with array slices for Linear GS top = Assembly() top.add('comp1', ArrayComp2D()) top.add('comp2', ArrayComp2D()) top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp1', 'comp2']) top.connect('comp1.y', 'comp2.x') top.driver.add_parameter('comp1.x[0][0]', low=-10, high=10) top.driver.add_objective('comp1.y[0][0]') top.driver.add_constraint('comp2.y[0][1] < 0') top.driver.gradient_options.lin_solver = 'linear_gs' top.driver.gradient_options.maxiter = 1 top.run() J = top.driver.calc_gradient(mode='forward') assert_rel_error(self, J[0, 0], 2.0, .000001) assert_rel_error(self, J[1, 0], 39.0, .000001) J = top.driver.calc_gradient(mode='adjoint') assert_rel_error(self, J[0, 0], 2.0, .000001) assert_rel_error(self, J[1, 0], 39.0, .000001)
def test_nested_2Darray_simul_element_and_full_connection2(self): # Slightly different config top = Assembly() top.add('nest', Assembly()) top.nest.add('comp1', ArrayComp2D()) top.nest.add('comp2', ArrayComp2D()) top.driver.gradient_options.lin_solver = 'petsc_ksp' top.nest.driver.gradient_options.lin_solver = 'petsc_ksp' top.nest.driver.workflow.add(['comp1', 'comp2']) top.nest.connect('comp1.y', 'comp2.x') top.nest.create_passthrough('comp1.x') top.nest.create_passthrough('comp1.y') top.nest.add('yy', Array(iotype='out')) top.nest.connect('comp2.y', 'yy') top.add('driver', SimpleDriver()) top.driver.workflow.add(['nest']) top.driver.add_parameter('nest.x[0][0]', low=-10, high=10) top.driver.add_objective('nest.yy[0][0]') top.driver.add_constraint('nest.y[0][1] < 0') top.run() J = top.driver.calc_gradient(mode='forward') assert_rel_error(self, J[0, 0], 24.0, .000001) assert_rel_error(self, J[1, 0], 4.0, .000001) J = top.driver.calc_gradient(mode='adjoint') assert_rel_error(self, J[0, 0], 24.0, .000001) assert_rel_error(self, J[1, 0], 4.0, .000001) J = top.driver.calc_gradient(mode='fd') assert_rel_error(self, J[0, 0], 24.0, .000001) assert_rel_error(self, J[1, 0], 4.0, .000001)
def test_fan_in_simpledriver(self): size = 5 # 2 parallel comps feeding another comp top = set_as_top(Assembly()) top.add('driver', SimpleDriver()) top.add("C1", ABCDArrayComp(size)) top.add("C2", ABCDArrayComp(size)) top.add("C3", ABCDArrayComp(size)) top.driver.workflow.add(['C1', 'C2', 'C3']) top.connect('C1.c', 'C3.a') top.connect('C2.d', 'C3.b') top.C1.a = np.ones(size, float) * 3.0 top.C1.b = np.ones(size, float) * 7.0 top.C2.a = np.ones(size, float) * 4.0 top.C2.b = np.ones(size, float) * 5.0 top.driver.add_parameter('C1.a', low=-1000, high=1000) top.driver.add_parameter('C2.a', low=-1000, high=1000) top.driver.add_objective('C3.d') top.run() # top.C1.dump(self.comm) # top.C2.dump(self.comm) if self.comm.rank == 0: self.assertTrue(all(top.C3.a==np.ones(size, float)*10.)) self.assertTrue(all(top.C3.b==np.ones(size, float)*-1.)) self.assertTrue(all(top.C3.c==np.ones(size, float)*9.)) self.assertTrue(all(top.C3.d==np.ones(size, float)*11.))
def test_one_to_two_fd(self): top = set_as_top(Assembly()) exp1 = ["y1 = 3.0*x", "y2 = 4.0*x"] exp2 = ["y = -2.0*x"] exp3 = ["y = 5.0*x"] deriv1 = ["dy1_dx = 3.0", "dy2_dx = 4.0"] deriv2 = ["dy_dx = -2.0"] deriv3 = ["dy_dx = 5.0"] top.add('comp1', ExecCompWithDerivatives(exp1, deriv1)) top.add('comp2', ExecCompWithDerivatives(exp2, deriv2)) top.add('comp3', ExecCompWithDerivatives(exp3, deriv3)) top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp1', 'comp2', 'comp3']) top.connect('comp1.y1', 'comp2.x') top.connect('comp1.y2', 'comp3.x') top.driver.add_parameter('comp1.x', low=-100, high=100) top.driver.add_constraint('comp2.y < 1000') top.driver.add_constraint('comp3.y < 1000') top.run() J = top.driver.workflow.calc_gradient(mode='fd', return_format='dict') J = top.driver.workflow._system.get_combined_J(J) collective_assert_rel_error(self, J['_pseudo_0.out0']['comp1.x'][0][0], -6.0, 0.0001) collective_assert_rel_error(self, J['_pseudo_1.out0']['comp1.x'][0][0], 20.0, 0.0001)
def configure(self): self.add('src', SrcComp()) self.add('tgt', TargComp()) self.add('sub', SimpleDriver()) self.connect('src.y', 'tgt.x') self.sub.workflow.add('src') self.driver.workflow.add(['sub', 'tgt'])
def test_custom_jacobian(self): class AComp(Component): x = Array([[1.0, 3.0], [-2.0, 4.0]], iotype='in') y = Array(np.zeros((2, 2)), iotype='out') def __init__(self): super(AComp, self).__init__() self.J = np.array([[3.5, -2.5, 1.5, 4.0], [4.0, 2.0, -1.1, 3.4], [7.7, 6.6, 4.4, 1.1], [0.1, 3.3, 6.8, -5.5]]) def execute(self): """ Run arraycomp""" y = self.J.dot(self.x.flatten()) self.y = y.reshape((2, 2)) def list_deriv_vars(self): """ x and y """ input_keys = ('x', ) output_keys = ('y', ) return input_keys, output_keys def provideJ(self): """Analytical first derivatives""" return self.J def fake_jac(): """ Returns a User-defined Jacobian. The values are totally wrong to facilitate testing. """ jacs = {} jacs['comp.x'] = np.array([[100.0, 101, 102, 103], [104, 105, 106, 107], [108, 109, 110, 111], [112, 113, 114, 115]]) return jacs top = set_as_top(Assembly()) top.add('driver', SimpleDriver()) top.add('comp', AComp()) top.driver.workflow.add('comp') top.driver.add_parameter('comp.x', low=10, high=10) top.driver.add_constraint('comp.y < 1', jacs=fake_jac) # petsc KSP, inequality constraints top.run() J = top.driver.calc_gradient(mode='forward', return_format='dict') J = J['_pseudo_0.out0']['comp.x'] diff = np.abs(J - top.comp.J) assert_rel_error(self, diff.max(), 0.0, 1e-4) J = top.driver.calc_gradient(mode='adjoint', return_format='dict') J = J['_pseudo_0.out0']['comp.x'] diff = np.abs(J - fake_jac()['comp.x']) assert_rel_error(self, diff.max(), 0.0, 1e-4)
def test_solver_nested_under_double_nested_driver_no_deriv(self): model = set_as_top(Assembly()) model.add('driver', SimpleDriver()) model.add('comp', MyComp_No_Deriv()) model.add('subdriver', SimpleDriver()) model.add('solver', BroydenSolver()) model.driver.workflow.add('subdriver') model.subdriver.workflow.add('solver') model.solver.workflow.add('comp') model.solver.tol = 0.0000001 model.solver.add_parameter('comp.x', low=-100, high=100) model.solver.add_parameter('comp.y', low=-100, high=100) model.solver.add_parameter('comp.z', low=-100, high=100) model.solver.add_constraint('comp.res[0] = 0') model.solver.add_constraint('comp.res[1] = 0') model.solver.add_constraint('comp.res[2] = 0') model.driver.add_parameter('comp.c', low=-100, high=100) model.driver.add_objective('comp.y_out') model.comp.eval_only = True model.run() J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp.y_out']) #print J assert_rel_error(self, J[0][0], 0.75, 1e-5) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp.y_out'], mode='adjoint') #print J assert_rel_error(self, J[0][0], 0.75, 1e-5) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp.y_out'], mode='fd') #print J assert_rel_error(self, J[0][0], 0.75, 1e-5)
def test_solver_nested_under_double_nested_driver_boundary_var_no_deriv( self): model = set_as_top(Assembly()) model.add('comp', MyComp_No_Deriv()) model.add('bvar', Float(0.0, iotype='in')) model.add('driver', SimpleDriver()) model.add('subdriver', SimpleDriver()) model.driver.workflow.add('subdriver') model.subdriver.workflow.add('comp') model.subdriver.add_parameter('comp.c', low=-100, high=100) model.subdriver.add_objective('comp.y_out - bvar') model.driver.add_parameter('bvar', low=-100, high=100) model.driver.add_objective('bvar - comp.y_out') model.comp.eval_only = True model.run() J = model.driver.calc_gradient()
def setUp(self): # this model mimics the one in test_derivatives, test_single_comp self.top = top = set_as_top(Assembly()) top.add('comp', Paraboloid()) top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp']) top.driver.add_parameter('comp.x', low=-1000, high=1000) top.driver.add_parameter('comp.y', low=-1000, high=1000) top.driver.add_objective('comp.f_xy') top.comp.x = 3 top.comp.y = 5
def test_newton_nested(self): # Make sure derivatives across the newton-solved system are correct. top = set_as_top(Assembly()) top.add('driver', SimpleDriver()) top.add('d1', Discipline1_WithDerivatives()) top.d1.x1 = 1.0 top.d1.y1 = 1.0 top.d1.y2 = 1.0 top.d1.z1 = 5.0 top.d1.z2 = 2.0 top.add('d2', Discipline2_WithDerivatives()) top.d2.y1 = 1.0 top.d2.y2 = 1.0 top.d2.z1 = 5.0 top.d2.z2 = 2.0 top.connect('d1.y1', 'd2.y1') top.add('solver', NewtonSolver()) top.solver.atol = 1e-9 top.solver.workflow.add(['d1', 'd2']) top.solver.add_parameter('d1.y2', low=-1e99, high=1e99) top.solver.add_constraint('d1.y2 = d2.y2') top.driver.workflow.add(['solver']) top.driver.add_parameter('d1.z1', low=-100, high=100) top.driver.add_objective('d1.y1 + d1.y2') top.run() J = top.driver.calc_gradient(mode='forward') print J assert_rel_error(self, J[0][0], 10.77542099, 1e-5) J = top.driver.calc_gradient(mode='adjoint') print J assert_rel_error(self, J[0][0], 10.77542099, 1e-5) top.driver.gradient_options.fd_step = 1e-7 top.driver.gradient_options.fd_form = 'central' J = top.driver.calc_gradient(mode='fd') print J assert_rel_error(self, J[0][0], 10.77542099, 1e-5)
def test_petsc_ksp_single_comp_dict(self): top = set_as_top(Assembly()) top.add('comp', Paraboloid()) top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp']) top.driver.add_parameter('comp.x', low=-1000, high=1000) top.driver.add_parameter('comp.y', low=-1000, high=1000) top.driver.add_objective('comp.f_xy') top.driver.gradient_options.lin_solver = 'petsc_ksp' top.comp.x = 3 top.comp.y = 5 top.run() self.assertEqual(top.comp.f_xy, 93.) self.assertEqual(top._pseudo_0.out0, 93.) J = top.driver.workflow.calc_gradient(inputs=['comp.x', 'comp.y'], outputs=['comp.f_xy'], mode='forward', return_format='dict') assert_rel_error(self, J['comp.f_xy']['comp.x'][0][0], 5.0, 0.0001) assert_rel_error(self, J['comp.f_xy']['comp.y'][0][0], 21.0, 0.0001) J = top.driver.workflow.calc_gradient(inputs=['comp.x', 'comp.y'], mode='adjoint', return_format='dict') assert_rel_error(self, J['_pseudo_0.out0']['comp.x'][0][0], 5.0, 0.0001) assert_rel_error(self, J['_pseudo_0.out0']['comp.y'][0][0], 21.0, 0.0001) # Make sure we aren't add-scattering out p vector top.run() J = top.driver.workflow.calc_gradient(inputs=['comp.x', 'comp.y'], mode='forward', return_format='dict') assert_rel_error(self, J['_pseudo_0.out0']['comp.x'][0][0], 5.0, 0.0001) assert_rel_error(self, J['_pseudo_0.out0']['comp.y'][0][0], 21.0, 0.0001)
def test_cascade_opt(self): raise SkipTest( "We currently don't allow a component instance in multiple workflows." ) top = set_as_top(Assembly()) eq = ['f = (x-3)**2 + x*y + (y+4)**2 - 3'] deriv = ['df_dx = 2.0*x - 6.0 + y', 'df_dy = 2.0*y + 8.0 + x'] top.add('comp', ExecCompWithDerivatives(eq, deriv)) top.add('driver', SimpleDriver()) top.add('opt1', SLSQPdriver()) top.add('opt2', SLSQPdriver()) top.opt1.workflow.add(['comp']) top.opt2.workflow.add(['comp']) top.driver.workflow.add(['opt1', 'opt2']) top.opt1.add_parameter('comp.x', low=-100, high=100) top.opt1.add_parameter('comp.y', low=-100, high=100) top.opt1.add_objective('comp.f') top.opt1.maxiter = 2 top.opt2.add_parameter('comp.x', low=-100, high=100) top.opt2.add_parameter('comp.y', low=-100, high=100) top.opt2.add_objective('comp.f') top.opt2.maxiter = 50 top.run() assert_rel_error(self, top.comp.x, 6.666309, 0.01) assert_rel_error(self, top.comp.y, -7.333026, 0.01) J = top.driver.calc_gradient(inputs=['comp.x', 'comp.y'], outputs=['comp.f']) edges = top.driver.workflow._edges print edges self.assertEqual(set(edges['@in0']), set(['~opt1.comp|x', '~opt2.comp|x'])) self.assertEqual(set(edges['@in1']), set(['~opt1.comp|y', '~opt2.comp|y'])) self.assertEqual(set(edges['~opt1.comp|f']), set(['@out0'])) self.assertEqual(set(edges['~opt2.comp|f']), set(['@out0']))
def test_derivative_state_connection_external_solve_ProvideJ(self): model = set_as_top(Assembly()) model.add('driver', SimpleDriver()) model.add('comp', MyComp_Deriv_ProvideJ()) model.comp.add('c', Float(2.0, iotype="in", fd_step=.001)) model.add('comp2', ExecCompWithDerivatives(["y=2*x"], ["dy_dx=2"])) model.add('solver', BroydenSolver()) model.solver.workflow.add(['comp', 'comp2']) model.driver.workflow.add(['solver']) model.connect('comp.z', 'comp2.x') model.solver.add_parameter('comp.x', low=-100, high=100) model.solver.add_parameter('comp.y', low=-100, high=100) model.solver.add_parameter('comp.z', low=-100, high=100) model.solver.add_constraint('comp.res[0] = 0') model.solver.add_constraint('comp.res[1] = 0') model.solver.add_constraint('comp.res[2] = 0') model.comp.eval_only = True model.run() #print model.comp.x, model.comp.y, model.comp.z, model.comp.res J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp2.y'], mode='forward') assert_rel_error(self, J[0][0], -0.1666, 2e-3) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp2.y'], mode='adjoint') assert_rel_error(self, J[0][0], -0.1666, 2e-3) J = model.driver.calc_gradient(inputs=['comp.c'], outputs=['comp2.y'], mode='fd') assert_rel_error(self, J[0][0], -0.1666, 2e-3)
def test_scale_gradients(self): opt_problem = OptimizationUnconstrainedScale() opt_problem.replace('driver', SimpleDriver()) opt_problem.run() J = opt_problem.driver.calc_gradient() Jdict = opt_problem.driver.calc_gradient(return_format='dict') # Make sure untransforming works for dicts too self.assertTrue(J[0][0] == Jdict['_pseudo_0.out0']['paraboloid.x']) self.assertTrue(J[0][1] == Jdict['_pseudo_0.out0']['paraboloid.y']) Jfddict = opt_problem.driver.calc_gradient(mode='fd', return_format='dict') opt_problem.driver.run_iteration() Jfd = opt_problem.driver.calc_gradient(mode='fd') # Make sure untransforming works for dicts too self.assertTrue(Jfd[0][0] == Jfddict['_pseudo_0.out0']['paraboloid.x']) self.assertTrue(Jfd[0][1] == Jfddict['_pseudo_0.out0']['paraboloid.y'])
def test_three_comp_diamond_forward(self): self.top = set_as_top(Assembly()) exp1 = ['y1 = 50.0*x1', 'y2 = 1.0*x1'] deriv1 = ['dy1_dx1 = 50.0', 'dy2_dx1 = 1.0'] exp2 = ['y1 = 1.2*x1'] deriv2 = ['dy1_dx1 = 1.2'] exp3 = ['y1 = 100.0*x1*x2 + 30*x1 + 0.3*x2'] deriv3 = ['dy1_dx1 = 100.0*x2 + 30', 'dy1_dx2 = 100.0*x1 + 0.3'] self.top.add('comp1', ExecCompWithDerivatives(exp1, deriv1)) self.top.add('comp2', ExecCompWithDerivatives(exp2, deriv2)) self.top.add('comp3', ExecCompWithDerivatives(exp3, deriv3)) self.top.add('driver', SimpleDriver()) self.top.driver.workflow.add(['comp1', 'comp2', 'comp3']) self.top.connect('comp1.y1', 'comp2.x1') self.top.connect('comp1.y2', 'comp3.x1') self.top.connect('comp2.y1', 'comp3.x2') self.top.driver.add_parameter('comp1.x1', low=-100, high=100) self.top.driver.add_objective('comp3.y1') self.top.comp1.x1 = 2.0 self.top.run() J = self.top.driver.calc_gradient(inputs=['comp1.x1'], outputs=['comp3.y1'], mode='forward', return_format='dict') if self.comm.rank == 0: assert_rel_error(self, J['comp3.y1']['comp1.x1'][0][0], 24048.0, 0.0001)
def test_simple_opaque(self): size = 5 # 2 parallel comps feeding another comp top = set_as_top(Assembly()) top.add('driver', SimpleDriver()) top.add("C1", ABCDArrayComp(size)) top.add("C2", ABCDArrayComp(size)) top.driver.workflow.add(['C1', 'C2']) top.connect('C1.c', 'C2.a') top.C1.a = np.ones(size, float) * 3.0 top.C1.b = np.ones(size, float) * 7.0 top.C2.b = np.ones(size, float) * 5.0 top.run() if self.comm.rank == 0: self.assertTrue(all(top.C2.a==np.ones(size, float)*10.)) self.assertTrue(all(top.C2.b==np.ones(size, float)*5.)) self.assertTrue(all(top.C2.c==np.ones(size, float)*15.)) self.assertTrue(all(top.C2.d==np.ones(size, float)*5.))
def configure(self): self.add('driver', SimpleDriver()) self.driver.iout = 1 self.driver.iprint = 3 self.driver.gradient_options.force_fd = True self.driver.gradient_options.fd_step = 1.e-20 self.driver.gradient_options.fd_form = 'complex_step' self.add('c', Asym()) self.driver.workflow.add('c') # this works # self.driver.add_parameter('c.x', start=numpy.array([1., 1., 1., 1.])) # this doesn't work self.driver.add_parameter('c.x[0]', start=1.) self.driver.add_parameter('c.x[1]', start=1.) self.driver.add_parameter('c.x[2]', start=1.) self.driver.add_parameter('c.x[3]', start=1.) self.driver.add_constraint('c.g < 0.') self.driver.add_objective('c.result')
def test_simple_array(self): model = set_as_top(Assembly()) model.add('comp', SimpleCompArray()) model.driver.workflow.add('comp') model.driver.gradient_options.fd_form = 'complex_step' model.run() J = model.driver.calc_gradient(inputs=['comp.x'], outputs=['comp.y']) diff = abs(J - model.comp.J).max() assert_rel_error(self, diff, 0.0, .0001) self.assertTrue(J[0, 0] is not complex) model.add('driver', SimpleDriver()) model.driver.add_parameter('comp.x', low=-10, high=10) model.driver.add_objective('comp.y - comp.x') model.run() J = model.driver.calc_gradient(mode='fd') diff = abs(J + eye(4) - model.comp.J).max() assert_rel_error(self, diff, 0.0, .0001) self.assertTrue(J[0, 0] is not complex)
def test_two_to_one_adjoint_bcast(self): top = set_as_top(Assembly()) exp1 = ["y = 3.0*x"] exp2 = ["y = -2.0*x"] exp3 = ["y = 5.0*x1 + 4.0*x2"] deriv1 = ["dy_dx = 3.0"] deriv2 = ["dy_dx = -2.0"] deriv3 = ["dy_dx1 = 5.0", "dy_dx2 = 4.0"] top.add('comp1', ExecCompWithDerivatives(exp1, deriv1)) top.add('comp2', ExecCompWithDerivatives(exp2, deriv2)) top.add('comp3', ExecCompWithDerivatives(exp3, deriv3)) top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp1', 'comp2', 'comp3']) top.connect('comp1.y', 'comp3.x1') top.connect('comp2.y', 'comp3.x2') top.driver.add_parameter(('comp1.x', 'comp2.x'), low=-100, high=100) top.driver.add_constraint('comp3.y < 1000') top.run() J = top.driver.workflow.calc_gradient(mode='adjoint', return_format='dict') J = top.driver.workflow._system.get_combined_J(J) #from openmdao.util.dotgraph import plot_system_tree #plot_system_tree(top._system) collective_assert_rel_error(self, J['_pseudo_0.out0']['comp1.x'][0][0], 7.0, 0.0001)
def test_nested_array_full_and_partial_passthrough(self): top = Assembly() top.add('nest', Assembly()) top.nest.add('comp1', ArrayComp2D()) top.nest.add('comp2', ArrayComp2D()) top.driver.gradient_options.lin_solver = 'petsc_ksp' top.nest.driver.gradient_options.lin_solver = 'petsc_ksp' top.nest.driver.workflow.add(['comp1', 'comp2']) top.nest.create_passthrough('comp1.x', 'x') top.nest.create_passthrough('comp1.y', 'y1') top.nest.create_passthrough('comp2.y', 'y2') top.nest.connect('x[-1, -1]', 'comp2.x[-1, -1]') top.add('driver', SimpleDriver()) top.driver.workflow.add(['nest']) top.run() Jbase = top.nest.comp1.provideJ() J = top.driver.calc_gradient(inputs=['nest.x'], outputs=['nest.y1', 'nest.y2'], mode='fd') diff = abs(J[0:4, :] - Jbase) assert_rel_error(self, diff.max(), 0.0, .00001) diff = abs(J[0:4, -1] - Jbase[:, -1]) assert_rel_error(self, diff.max(), 0.0, .00001) diff = abs(J[4:, :-1]) assert_rel_error(self, diff.max(), 0.0, .00001) J = top.driver.calc_gradient(inputs=['nest.x'], outputs=['nest.y1', 'nest.y2'], mode='forward') diff = abs(J[0:4, :] - Jbase) assert_rel_error(self, diff.max(), 0.0, .00001) diff = abs(J[0:4, -1] - Jbase[:, -1]) assert_rel_error(self, diff.max(), 0.0, .00001) diff = abs(J[4:, :-1]) assert_rel_error(self, diff.max(), 0.0, .00001) Jdict = top.driver.calc_gradient(inputs=['nest.x'], outputs=['nest.y1', 'nest.y2'], mode='forward', return_format='dict') diff = Jdict['nest.y1']['nest.x'] - Jbase assert_rel_error(self, diff.max(), 0.0, .00001) diff = Jdict['nest.y2']['nest.x'] - J[4:, :] assert_rel_error(self, diff.max(), 0.0, .00001) J = top.driver.calc_gradient(inputs=['nest.x'], outputs=['nest.y1', 'nest.y2'], mode='adjoint') diff = abs(J[0:4, :] - Jbase) assert_rel_error(self, diff.max(), 0.0, .00001) diff = abs(J[0:4, -1] - Jbase[:, -1]) assert_rel_error(self, diff.max(), 0.0, .00001) diff = abs(J[4:, :-1]) assert_rel_error(self, diff.max(), 0.0, .00001) Jdict = top.driver.calc_gradient(inputs=['nest.x'], outputs=['nest.y1', 'nest.y2'], mode='adjoint', return_format='dict') diff = Jdict['nest.y1']['nest.x'] - Jbase assert_rel_error(self, diff.max(), 0.0, .00001) diff = Jdict['nest.y2']['nest.x'] - J[4:, :] assert_rel_error(self, diff.max(), 0.0, .00001)
def test_input_as_output(self): top = set_as_top(Assembly()) top.add( 'comp1', ExecCompWithDerivatives(['y=2.0*x + 3.0*x2'], ['dy_dx = 2.0', 'dy_dx2 = 3.0'])) top.add('comp2', ExecCompWithDerivatives(['y=3.0*x'], ['dy_dx = 3.0'])) top.connect('comp1.y', 'comp2.x') top.add('driver', SimpleDriver()) top.driver.workflow.add(['comp1', 'comp2']) #top.driver.add_parameter('comp1.x', low=-100, high=100) top.driver.add_objective('comp1.y + comp2.y + 5*comp1.x') top.driver.gradient_options.lin_solver = 'petsc_ksp' objs = top.driver.get_objectives().values() obj = '%s.out0' % objs[0].pcomp_name top.comp1.x = 1.0 top.run() J = top.driver.calc_gradient(inputs=['comp1.x'], outputs=[obj], mode='forward') assert_rel_error(self, J[0, 0], 13.0, 0.0001) J = top.driver.calc_gradient(inputs=['comp1.x'], outputs=[obj], mode='fd') assert_rel_error(self, J[0, 0], 13.0, 0.0001) top.driver.run() J = top.driver.calc_gradient(inputs=['comp1.x'], outputs=[obj], mode='adjoint') assert_rel_error(self, J[0, 0], 13.0, 0.0001) J = top.driver.calc_gradient(inputs=['comp1.x', 'comp1.x2'], outputs=[obj], mode='forward') assert_rel_error(self, J[0, 0], 13.0, 0.0001) assert_rel_error(self, J[0, 1], 12.0, 0.0001) J = top.driver.calc_gradient(inputs=['comp1.x', 'comp1.x2'], outputs=[obj], mode='adjoint') assert_rel_error(self, J[0, 0], 13.0, 0.0001) assert_rel_error(self, J[0, 1], 12.0, 0.0001) J = top.driver.calc_gradient(inputs=[('comp1.x', ), ('comp1.x2', )], outputs=[obj], mode='forward') assert_rel_error(self, J[0, 0], 13.0, 0.0001) assert_rel_error(self, J[0, 1], 12.0, 0.0001) J = top.driver.calc_gradient(inputs=[('comp1.x', ), ('comp1.x2', )], outputs=[obj], mode='adjoint') assert_rel_error(self, J[0, 0], 13.0, 0.0001) assert_rel_error(self, J[0, 1], 12.0, 0.0001)
# Flag for making sure we run serial if we do an mpirun model.seg3.driver.system_type = 'serial' model.seg3.coupled_solver.system_type = 'serial' #---------------------- # Prepare to Run #---------------------- model.driver.workflow.add(['seg1', 'seg2', 'seg3']) #model._setup() #from openmdao.util.dotgraph import plot_system_tree #plot_system_tree(model._system) model.replace('driver', SimpleDriver()) model.driver.add_objective('seg1.fuelburn + seg2.fuelburn + seg3.fuelburn') model.driver.add_constraint('seg1.h_i = 0.0') model.driver.add_constraint('seg2.h_i = 0.0') model.driver.add_constraint('seg3.h_i = 0.0') model.driver.add_constraint('seg1.h_f = 0.0') model.driver.add_constraint('seg2.h_f = 0.0') model.driver.add_constraint('seg3.h_f = 0.0') model.driver.add_constraint('seg1.Tmin < 0.0') model.driver.add_constraint('seg2.Tmin < 0.0') model.driver.add_constraint('seg3.Tmin < 0.0') model.driver.add_constraint('seg1.Tmax < 0.0') model.driver.add_constraint('seg2.Tmax < 0.0') model.driver.add_constraint('seg3.Tmax < 0.0') model.driver.add_parameter('seg1.h_pt', low=0.0, high=14.1) model.driver.add_parameter('seg2.h_pt', low=0.0, high=14.1)
def test_diverge_converge_nondiff_comp3_forward(self): self.top = set_as_top(Assembly()) exp1 = ['y1 = 2.0*x1**2', 'y2 = 3.0*x1'] deriv1 = ['dy1_dx1 = 4.0*x1', 'dy2_dx1 = 3.0'] exp2 = ['y1 = 0.5*x1'] deriv2 = ['dy1_dx1 = 0.5'] exp3 = ['y1 = 3.5*x1'] deriv3 = ['dy1_dx1 = 3.5'] exp4 = ['y1 = x1 + 2.0*x2', 'y2 = 3.0*x1', 'y3 = x1*x2'] deriv4 = ['dy1_dx1 = 1.0', 'dy1_dx2 = 2.0', 'dy2_dx1 = 3.0', 'dy2_dx2 = 0.0', 'dy3_dx1 = x2', 'dy3_dx2 = x1'] exp5 = ['y1 = x1 + 3.0*x2 + 2.0*x3'] deriv5 = ['dy1_dx1 = 1.0', 'dy1_dx2 = 3.0', 'dy1_dx3 = 2.0'] self.top.add('driver', SimpleDriver()) self.top.add('comp1', ExecCompWithDerivatives(exp1, deriv1)) self.top.add('comp2', ExecCompWithDerivatives(exp2, deriv2)) self.top.add('comp3', ExecComp(exp3)) self.top.add('comp4', ExecCompWithDerivatives(exp4, deriv4)) self.top.add('comp5', ExecCompWithDerivatives(exp5, deriv5)) self.top.driver.workflow.add(['comp1', 'comp2', 'comp3', 'comp4', 'comp5']) self.top.connect('comp1.y1', 'comp2.x1') self.top.connect('comp1.y2', 'comp3.x1') self.top.connect('comp2.y1', 'comp4.x1') self.top.connect('comp3.y1', 'comp4.x2') self.top.connect('comp4.y1', 'comp5.x1') self.top.connect('comp4.y2', 'comp5.x2') self.top.connect('comp4.y3', 'comp5.x3') self.top.driver.add_parameter('comp1.x1', low=-100, high=100) self.top.driver.add_objective('comp5.y1') self.top.comp1.x1 = 2.0 self.top.run() J = self.top.driver.calc_gradient(inputs=['comp1.x1'], outputs=['comp5.y1'], mode='forward', return_format='dict') collective_assert_rel_error(self, J['comp5.y1']['comp1.x1'][0][0], 313.0, 0.0001)
def configure(self): self.add('driver', SimpleDriver()) self.driver.gradient_options.force_fd = True self.driver.add_parameter('x', low=-10., high=10.) self.driver.add_objective('x**2')