def test_complex_array_data_passing(self): model = set_as_top(Assembly()) model.add('comp1', ComplexArray()) model.add('comp2', ComplexArray()) model.driver.workflow.add(['comp1', 'comp2']) model.connect('comp1.y', 'comp2.x') model.driver.gradient_options.fd_form = 'complex_step' model.run() J = model.driver.calc_gradient(inputs=['comp1.x'], outputs=['comp2.y']) assert_rel_error(self, J[0, 0], 7.0, .0001) assert_rel_error(self, J[0, 1], 0.0, .0001) assert_rel_error(self, J[1, 1], 7.0, .0001) assert_rel_error(self, J[1, 0], 0.0, .0001)
def test_sequential(self): # verify that if components aren't connected they should execute in the # order that they were added to the workflow instead of hash order global exec_order top = set_as_top(Assembly()) top.add('c2', Simple()) top.add('c1', Simple()) top.add('c3', Simple()) top.add('c4', Simple()) top.driver.workflow.add(['c1','c2','c3','c4']) top.run() self.assertEqual(exec_order, ['c1','c2','c3','c4']) top.connect('c4.c', 'c3.a') # now make c3 depend on c4 exec_order = [] top.c4.a = 2 # makes c4 run again top.run() self.assertEqual(exec_order, ['c4','c3'])
def test_eval_gradient_lots_of_vars(self): top = set_as_top(Assembly()) top.add('comp1', B()) #build expr expr = "2*comp1.in1 + 3*comp1.in11" exp = ExprEvaluator(expr, top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.in1'], 2.0, 0.00001) assert_rel_error(self, grad['comp1.in11'], 3.0, 0.00001) expr = "asin(comp1.in1)" exp = ExprEvaluator(expr, top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.in1'], 1.0, 0.00001)
def test_errors(self): a = set_as_top(Assembly()) comp = a.add('comp', ExecComp(exprs=["f=x"])) driver = a.add('driver', Brent()) driver.add_parameter('comp.x') driver.add_constraint('comp.f=0') comp.n = 1.0 comp.c = 0 driver.lower_bound = 1.0 try: a.run() except Exception as err: self.assertEqual( str(err), "driver: bounds (low=1.0, high=100.0) do not bracket a root") else: self.fail("Exception expected")
def test_expr_deps(self): top = set_as_top(Assembly()) driver1 = top.add('driver1', DumbDriver()) driver2 = top.add('driver2', DumbDriver()) top.add('c1', Simple()) top.add('c2', Simple()) top.add('c3', Simple()) top.driver.workflow.add(['driver1','driver2','c3']) top.driver1.workflow.add('c2') top.driver2.workflow.add('c1') top.connect('c1.c', 'c2.a') top.driver1.add_objective("c2.c*c2.d") top.driver2.add_objective("c1.c") top.run() self.assertEqual(exec_order, ['driver2','c1','driver1','c2','c3'])
def setUp(self): self.top = set_as_top(Assembly()) self.top.add('comp', RosenSuzukiMixed()) driver = self.top.add('driver', CONMINdriver()) driver.workflow.add('comp') driver.iprint = 0 driver.itmax = 30 driver.add_objective('10*comp.result') map(driver.add_parameter, ['comp.x0', 'comp.x12', 'comp.x3']) # pylint: disable=C0301 map(driver.add_constraint, [ 'comp.x0**2+comp.x0+comp.x12[0]**2-comp.x12[0]+comp.x12[1]**2+comp.x12[1]+comp.x3**2-comp.x3 < 8', 'comp.x0**2-comp.x0+2*comp.x12[0]**2+comp.x12[1]**2+2*comp.x3**2-comp.x3 < 10', '2*comp.x0**2+2*comp.x0+comp.x12[0]**2-comp.x12[0]+comp.x12[1]**2-comp.x3 < 5' ])
def rosen_setUp(self): # Chop up the equations for the Rosen-Suzuki optimization problem # into 4 ExprComp components and some Adders so that our driver # will iterate over more than one compnent top = set_as_top(Assembly()) self.top = top # create the first driver drv = top.add('driver1', CONMINdriver()) top.add('comp1', ExprComp(expr='x**2 - 5.0*x')) top.add('comp2', ExprComp(expr='x**2 - 5.0*x')) top.add('comp3', ExprComp(expr='2.0*x**2 - 21.0*x')) top.add('comp4', ExprComp(expr='x**2 + 7.0*x')) top.add('adder1', Adder()) top.add('adder2', Adder()) top.add('adder3', Adder()) top.connect('comp1.f_x', 'adder1.x1') top.connect('comp2.f_x', 'adder1.x2') top.connect('comp3.f_x', 'adder2.x1') top.connect('comp4.f_x', 'adder2.x2') top.connect('adder1.sum', 'adder3.x1') top.connect('adder2.sum', 'adder3.x2') top.driver.workflow.add('driver1') drv.workflow.add( ['comp1', 'comp2', 'comp3', 'comp4', 'adder1', 'adder2', 'adder3']) drv.itmax = 30 #drv.conmin_diff = True drv.add_objective('adder3.sum+50.') drv.add_parameter('comp1.x', -10., 99.) drv.add_parameter('comp2.x', -10., 99.) drv.add_parameter('comp3.x', -10., 99.) drv.add_parameter('comp4.x', -10., 99.) map(drv.add_constraint, [ 'comp1.x**2 + comp2.x**2 + comp3.x**2 + comp4.x**2 + comp1.x-comp2.x+comp3.x-comp4.x < 8.0', 'comp1.x**2 + 2.*comp2.x**2 + comp3.x**2 + 2.*comp4.x**2 - comp1.x - comp4.x < 10.', '2.0*comp1.x**2 + comp2.x**2 + comp3.x**2 + 2.0*comp1.x - comp2.x - comp4.x < 5.0', ]) # expected optimal values self.opt_objective = 6. self.opt_design_vars = [0., 1., 2., -1.]
def test_varTree_connections_whole_tree(self): top = set_as_top(Assembly()) top.add('driver', SimpleDriver()) top.add('comp1', CompWithVarTreeSubTree()) top.add('comp2', CompWithVarTree()) top.driver.workflow.add(['comp1', 'comp2']) inputs = ['comp1.ins.x.x1', 'comp1.ins.x.x2', 'comp1.ins.y'] outputs = ['comp2.z', 'comp1.outs.z', 'comp1.ins.x.x1'] top.driver.add_parameter('comp1.ins.x.x1', low=-1000, high=1000) top.driver.add_parameter('comp1.ins.x.x2', low=-1000, high=1000) top.driver.add_parameter('comp1.ins.y', low=-1000, high=1000) top.connect('comp1.outs', 'comp2.ins') top.driver.add_objective('comp2.z') top.driver.add_constraint('comp1.outs.z+comp1.ins.x.x1 < 0') top.run() J_true = array([ [8., 12., 16.], #obj [3., 3., 4.] ]) #c1 obj = ["%s.out0" % item.pcomp_name for item in \ top.driver.get_objectives().values()] con = ["%s.out0" % item.pcomp_name for item in \ top.driver.get_constraints().values()] top.driver.workflow.config_changed() J_fd = top.driver.workflow.calc_gradient(inputs, obj + con, mode='fd') top.driver.workflow.config_changed() J_forward = top.driver.workflow.calc_gradient(inputs, obj + con, mode="forward") top.driver.workflow.config_changed() J_reverse = top.driver.workflow.calc_gradient(inputs, obj + con, mode="adjoint") assert_rel_error(self, linalg.norm(J_true - J_fd), 0, .00001) assert_rel_error(self, linalg.norm(J_true - J_forward), 0, .00001) assert_rel_error(self, linalg.norm(J_true - J_reverse), 0, .00001)
def test_array(self): model = set_as_top(Assembly()) model.add('comp', SimpleCompArray()) model.driver.workflow.add('comp') model.comp.x = model.comp.x.astype('complex') model.comp.x[1, 1] = 3 + 4j model.run() print model.comp.y y_check = array([[31.0 + 20.0j, 2.0 - 20.0j], [18.0 - 12.0j, 31.5 + 8.0j]]) self.assertEqual(model.comp.x[1, 1], 3 + 4j) self.assertEqual(model.comp.y[0, 0], y_check[0, 0]) self.assertEqual(model.comp.y[0, 1], y_check[0, 1]) self.assertEqual(model.comp.y[1, 0], y_check[1, 0]) self.assertEqual(model.comp.y[1, 1], y_check[1, 1])
def test_connections(self): logging.debug('') logging.debug('test_connections') top = Assembly() top.add('generator', Generator()) top.add('cid', CaseIteratorDriver()) top.add('driven', DrivenComponent()) top.add('verifier', Verifier()) top.driver.workflow.add(('generator', 'cid', 'verifier')) top.cid.workflow.add('driven') top.cid.printvars = ['driven.extra'] top.connect('generator.cases', 'cid.iterator') top.connect('cid.evaluated', 'verifier.cases') top.run()
def test_basic(self): top = set_as_top(Assembly()) box = top.add('box', Component()) box.add('geo', VarTree(GeomData(13, 17, 3), iotype='in')) self.assertTrue(box.geo.points.shape == (13, 3)) self.assertTrue(box.geo.facets.shape == (17, 3)) self.assertTrue(issubclass(box.geo.facets.dtype.type, np.int)) box.add('geo2', VarTree(GeomData(13, 17, 4), iotype='out')) self.assertTrue(box.geo2.facets.shape == (17, 4)) try: box.geo = GeomData(3, 4, 5) except ValueError, err: msg = 'facet size must be either 3 or 4' self.assertEqual(str(err), msg)
def test_expr_deps(self): top = set_as_top(Assembly()) top.add('driver1', DumbDriver()) top.add('driver2', DumbDriver()) top.add('c1', Simple()) top.add('c2', Simple()) top.add('c3', Simple()) top.driver.workflow.add(['driver1', 'driver2', 'c3']) top.driver1.workflow.add('c2') top.driver2.workflow.add('c1') top.connect('c1.c', 'c2.a') top.driver1.add_objective("c2.c*c2.d") top.driver2.add_objective("c1.c") top.run() # FIXME: without lazy evaluation, c1 runs in the wrong order self.assertEqual(exec_order, ['driver1', 'c2', 'driver2', 'c1', 'c3'])
def test_SLSQP(self): top = set_as_top(Assembly()) top.add('parab1', Parab1()) top.add('parab2', Parab2()) top.add('driver', SLSQPdriver()) top.driver.workflow.add(['parab1', 'parab2']) top.driver.add_parameter(['parab1.x', 'parab2.x'], low=-100, high=100) top.driver.add_parameter(['parab1.y', 'parab2.y'], low=-100, high=100) top.driver.add_objective('parab1.f_xy + parab2.f_xy') top.driver.add_constraint('parab1.x-parab1.y >= 15.0') top.run() assert_rel_error(self, top.parab1.x, 7.166, 0.001) assert_rel_error(self, top.parab1.y, -7.833, 0.001) assert_rel_error(self, top._pseudo_0.out0, -27.083, 0.001)
def test_remove(self): top = Assembly() g = top._depgraph.component_graph() comps = [name for name in g] self.assertEqual(comps, ['driver']) top.add('comp', Component()) g = top._depgraph.component_graph() comps = [name for name in g] self.assertEqual(set(comps), set(['driver', 'comp'])) top.remove('comp') g = top._depgraph.component_graph() comps = [name for name in g] self.assertEqual(comps, ['driver'])
def setUp(self): self.top = set_as_top(Assembly()) self.top.add('comp1', Simple()) self.top.add('comp2', Simple()) self.top.connect('comp1.c', 'comp2.a') self.top.connect('comp1.d', 'comp2.b') self.top.connect('comp1.c_lst', 'comp2.a_lst') self.top.driver.workflow.add(['comp1', 'comp2']) self.inputs = [('comp1.a', 2), ('comp1.b', 4), ('comp1.a_lst', [4, 5, 6])] self.outputs = ['comp2.c+comp2.d', 'comp2.c_lst[2]', 'comp2.d'] self.case = case = Case(inputs=self.inputs, outputs=self.outputs, label='blah blah') case.apply_inputs(self.top) self.top.run() case.update_outputs(self.top)
def setUp(self): self.top = top = set_as_top(Assembly()) driver = top.add('driver', SimpleCaseIterDriver()) top.add('comp1', ExecComp(exprs=['z=x+y'])) top.add('comp2', ExecComp(exprs=['z=x+1'])) top.connect('comp1.z', 'comp2.x') driver.workflow.add(['comp1', 'comp2']) # now create some Cases outputs = ['comp1.z', 'comp2.z'] cases = [] for i in range(10): i = float(i) inputs = [('comp1.x', i), ('comp1.y', i * 2)] cases.append(Case(inputs=inputs, outputs=outputs)) Case.set_vartree_inputs(driver, cases) driver.add_responses(outputs)
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_simplest(self): top = set_as_top(Assembly()) top.add('comp', ExecComp(['y=4.0*x'])) top.driver.workflow.add('comp') top.run() top.driver.gradient_options.directional_fd = True J = top.driver.calc_gradient(inputs=['comp.x'], outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.0, 0.0001) top.driver.gradient_options.fd_form = 'backward' J = top.driver.calc_gradient(inputs=['comp.x'], outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.0, 0.0001) top.driver.gradient_options.fd_form = 'central' J = top.driver.calc_gradient(inputs=['comp.x'], outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.0, 0.0001) top.driver.gradient_options.fd_form = 'complex_step' J = top.driver.calc_gradient(inputs=['comp.x'], outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.0, 0.0001) top.driver.gradient_options.directional_fd = True try: J = top.driver.calc_gradient(inputs=['comp.x'], outputs=['comp.y'], mode = 'adjoint') except RuntimeError, err: msg = "Directional derivatives can only be used with forward " msg += "mode." self.assertEqual(str(err), msg)
def test_simple_coupled(self): self.top = set_as_top(Assembly()) exp1 = ['y2 = 0.5*sin(x1) - 0.5*x1*y1'] deriv1 = ['dy2_dx1 = 0.5*cos(x1) - 0.5*y1', 'dy2_dy1 = -0.5*x1'] exp2 = ['y1 = x2*x2*y2'] deriv2 = ['dy1_dx2 = 2.0*y2*x2', 'dy1_dy2 = x2*x2'] self.top.add('driver', Driv()) self.top.add('comp1', ExecCompWithDerivatives(exp1, deriv1)) self.top.add('comp2', ExecCompWithDerivatives(exp2, deriv2)) #self.top.add('comp1', ExecComp(exp1)) #self.top.add('comp2', ExecComp(exp2)) self.top.add('solver', BroydenSolver()) self.top.driver.workflow.add(['solver']) self.top.solver.workflow.add(['comp1', 'comp2']) self.top.connect('comp1.y2', 'comp2.y2') # Solver setup self.top.solver.add_parameter('comp1.y1', low=-1.e99, high=1.e99) self.top.solver.add_constraint('comp2.y1 = comp1.y1') # Top driver setup #self.top.driver.differentiator = FiniteDifference() self.top.driver.differentiator = Analytic() obj = 'comp2.y1' self.top.driver.add_parameter('comp1.x1', low=-100., high=100., fd_step=.001) #self.top.driver.add_parameter('comp2.x2', low=-100., high=100., fd_step=.0001) self.top.driver.add_objective(obj) self.top.comp1.x1 = 1.0 self.top.comp2.x2 = 1.0 self.top.run() self.top.driver.differentiator.calc_gradient() grad = self.top.driver.differentiator.get_gradient(obj) assert_rel_error(self, grad[0], 0.08660, .001)
def test_force_fd(self): model = set_as_top(Assembly()) model.add('comp', MyCompDerivs()) model.driver.workflow.add(['comp']) model.x1 = 1.0 model.x2 = 1.0 model.run() J = model.driver.workflow.calc_gradient(inputs=['comp.x1', 'comp.x2'], outputs=['comp.y']) self.assertEqual(model.comp.exec_count, 1) self.assertEqual(model.comp.derivative_exec_count, 1) # Component-wise force FD model.comp.force_fd = True model.driver.workflow.config_changed() J = model.driver.workflow.calc_gradient(inputs=['comp.x1', 'comp.x2'], outputs=['comp.y']) self.assertEqual(model.comp.exec_count, 3) self.assertEqual(model.comp.derivative_exec_count, 1) model.check_gradient(inputs=['comp.x1', 'comp.x2'], outputs=['comp.y'], stream=None) model.check_gradient(inputs=['comp.x1', 'comp.x2'], outputs=['comp.y'], fd_form='central', stream=None) model.check_gradient(inputs=['comp.x1', 'comp.x2'], outputs=['comp.y'], fd_step_type='relative', stream=None) # Full model force FD model.comp.force_fd = False model.driver.gradient_options.force_fd = True old_count = model.comp.exec_count model.driver.workflow.config_changed() J = model.driver.workflow.calc_gradient(inputs=['comp.x1', 'comp.x2'], outputs=['comp.y']) self.assertEqual(model.comp.exec_count - old_count, 2) self.assertEqual(model.comp.derivative_exec_count, 1)
def test_simple(self): top = set_as_top(Assembly()) top.add('comp1', Simple()) top.driver.workflow.add('comp1') vars = ['comp1.a', 'comp1.b', 'comp1.c', 'comp1.d'] self.assertEqual(top.comp1.exec_count, 0) valids = [top._depgraph.node[n]['valid'] for n in vars] self.assertEqual(valids, [True, True, False, False]) top.run() self.assertEqual(top.comp1.exec_count, 1) self.assertEqual(top.comp1.c, 3) self.assertEqual(top.comp1.d, -1) valids = [top._depgraph.node[n]['valid'] for n in vars] self.assertEqual(valids, [True, True, True, True]) top.set('comp1.a', 5) valids = [top._depgraph.node[n]['valid'] for n in vars] self.assertEqual(valids, [True, True, False, False]) top.run() self.assertEqual(top.comp1.exec_count, 2) self.assertEqual(top.comp1.c, 7) self.assertEqual(top.comp1.d, 3) top.run() self.assertEqual(top.comp1.exec_count, 2) # exec_count shouldn't change valids = [top._depgraph.node[n]['valid'] for n in vars] self.assertEqual(valids, [True, True, True, True]) # now add another comp and connect them top.add('comp2', Simple()) top.driver.workflow.add('comp2') top.connect('comp1.c', 'comp2.a') self.assertEqual(top.comp2.exec_count, 0) self.assertEqual(top.comp2.c, 3) self.assertEqual(top.comp2.d, -1) vars = ['comp2.a', 'comp2.b', 'comp2.c', 'comp2.d'] valids = [top._depgraph.node[n]['valid'] for n in vars] self.assertEqual(valids, [False, True, False, False]) top.run() self.assertEqual(top.comp1.exec_count, 2) self.assertEqual(top.comp2.exec_count, 1) self.assertEqual(top.comp2.c, 9) self.assertEqual(top.comp2.d, 5) valids = [top._depgraph.node[n]['valid'] for n in vars] self.assertEqual(valids, [True, True, True, True])
def test_fd_step_type_bounds_scaled(self): model = set_as_top(Assembly()) model.add('comp', MyComp()) model.driver.workflow.add(['comp']) model.run() model.driver.workflow.config_changed() J = model.driver.workflow.calc_gradient(inputs=['comp.x5'], outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.2, 0.0001) model.driver.gradient_options.fd_step_type = 'bounds_scaled' model.run() model.driver.workflow.config_changed() J = model.driver.workflow.calc_gradient(inputs=['comp.x6'], outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.2, 0.0001) model.run() model.driver.workflow.config_changed() try: J = model.driver.workflow.calc_gradient(inputs=['comp.x7'], outputs=['comp.y']) except RuntimeError as err: self.assertEqual( str(err), "For variable 'comp.x7', a finite " "difference step type of bounds_scaled " "is used but required low and high " "values are not set") else: self.fail("Exception expected because low " "and high not set for comp.x7") # test add_parameter's fdstep model.add('driver', SimpleDriver()) model.driver.workflow.add(['comp']) model.driver.gradient_options.fd_step_type = 'bounds_scaled' model.driver.add_parameter('comp.x2', low=0.0, high=1000.0, fd_step=.0001) J = model.driver.workflow.calc_gradient(outputs=['comp.y']) assert_rel_error(self, J[0, 0], 4.2, 0.0001)
def setUp(self): self.top = top = set_as_top(Assembly()) driver = top.add('driver', SimpleCaseIterDriver()) top.add('comp1', ExecComp(exprs=['z=x+y'])) top.add('comp2', ExecComp(exprs=['z=x+1'])) top.comp1.add('a_dict', Dict({}, iotype='in')) top.comp1.add('a_list', List([], iotype='in')) top.connect('comp1.z', 'comp2.x') driver.workflow.add(['comp1', 'comp2']) # now create some Cases outputs = ['comp1.z', 'comp2.z'] cases = [] for i in range(10): inputs = [('comp1.x', i), ('comp1.y', i*2), ('comp1.a_dict', {'a': 'b'}), ('comp1.a_list', ['a', 'b'])] cases.append(Case(inputs=inputs, outputs=outputs, label='case%s' % i)) driver.iterator = ListCaseIterator(cases)
def test_brent_converge(self): a = set_as_top(Assembly()) comp = a.add('comp', ExecComp(exprs=["f=a * x**n + b * x - c"])) comp.n = 77.0/27.0 comp.a = 1.0 comp.b = 1.0 comp.c = 10.0 driver = a.add('driver', Brent()) driver.add_parameter('comp.x', 0, 100) driver.add_constraint('comp.f=0') a.run() assert_rel_error(self, a.comp.x, 2.06720359226, .0001) assert_rel_error(self, a.comp.f, 0, .0001) self.assertTrue(has_interface(driver, ISolver))
def test_fan_out_in(self): size = 5 # array var size # a comp feeds two parallel comps which feed # another comp top = set_as_top(Assembly()) top.add("C1", ABCDArrayComp(size)) top.add("C2", ABCDArrayComp(size)) top.add("C3", ABCDArrayComp(size)) top.add("C4", ABCDArrayComp(size)) top.driver.workflow.add(['C1', 'C2', 'C3', 'C4']) top.connect('C1.c', 'C2.a') top.connect('C1.d', 'C3.b') top.connect('C2.c', 'C4.a') top.connect('C3.d', 'C4.b') top.C1.a = np.ones(size, float) * 3.0 top.C1.b = np.ones(size, float) * 7.0 top.run() with MPIContext(): self.assertTrue(all(top.C4.a==np.ones(size, float)*11.)) self.assertTrue(all(top.C4.b==np.ones(size, float)*5.)) # Piggyback testing of the is_variable_local function. system = top.driver.workflow._system # Only lowest rank has vars that are on all proceses if self.comm.rank == 0: self.assertTrue(system.is_variable_local('C1.c')) self.assertTrue(system.is_variable_local('C2.a')) self.assertTrue(system.is_variable_local('C3.b')) self.assertTrue(system.is_variable_local('C1.exec_count')) else: self.assertFalse(system.is_variable_local('C1.c')) self.assertFalse(system.is_variable_local('C2.a')) self.assertFalse(system.is_variable_local('C3.b')) self.assertFalse(system.is_variable_local('C1.exec_count')) # Exclusive or - you either got C2 or C3 on a given process. self.assertTrue(system.is_variable_local('C2.c') != system.is_variable_local('C3.d')) self.assertTrue(system.is_variable_local('C4.a') != system.is_variable_local('C4.b'))
def test_nondistrib_gather(self): # regular comp --> distrib comp --> regular comp. last comp should # automagically gather the full vector without declaring distrib_idxs size = 11 top = set_as_top(Assembly()) top.add("C1", InOutArrayComp(size)) top.add("C2", DistribInputDistribOutputComp(size)) top.add("C3", NonDistribGatherComp()) top.driver.workflow.add(['C1', 'C2', 'C3']) top.connect('C1.outvec', 'C2.invec') top.connect('C2.outvec', 'C3.invec') top.C1.invec = np.array(range(size, 0, -1), float) top.run() if self.comm.rank == 0: self.assertTrue(all(top.C3.outvec==np.array(range(size, 0, -1), float)*4))
def test_replace_parameter_objective(self): top = set_as_top(Assembly()) top.add('driver', EqInEqdriver()) top.add('comp1', Simple()) top.add('comp2', Simple()) top.connect('comp1.c','comp2.a') top.driver.add_parameter('comp1.a', low=-10, high=10) top.driver.add_objective('comp2.d') top.comp1.a = 10. top.comp2.b = -5. top.replace('comp1', Simple()) top.replace('comp2', Simple()) self.assertEqual(top.comp1.a, 10.) self.assertEqual(top.comp2.b, -5.)
def test_varTree_with_Array(self): top = set_as_top(Assembly()) top.add('driver', SimpleDriver()) top.add('comp', CompWithArrayVarTree()) top.driver.workflow.add(['comp']) top.run() inputs = ['comp.ins.x'] outputs = ['comp.outs.x'] J = top.driver.calc_gradient(inputs, outputs, mode="forward") J_true = top.comp.J assert_rel_error(self, linalg.norm(J_true - J), 0, .00001) J = top.driver.calc_gradient(inputs, outputs, mode="adjoint") assert_rel_error(self, linalg.norm(J_true - J), 0, .00001) J = top.driver.calc_gradient(inputs, outputs, mode='fd') assert_rel_error(self, linalg.norm(J_true - J), 0, .00001)
def test_2drivers_discon_same_iterset(self): # # D1---> # | | # | C1--------->| # | | # |<----------C2 | # | | # |<---D2 # global exec_order print "*** test_2drivers_discon_same_iterset ***" top = set_as_top(Assembly()) top.add('D1', Summer()) top.add('D2', Summer()) top.add('C1', ExprComp(expr='x+1')) top.add('C2', ExprComp(expr='x+1')) top.D1.add_objective('C2.f_x') top.D1.add_parameter('C1.x', low=-999, high=999) top.D1.max_iterations = 2 top.D2.add_objective('C1.f_x') top.D2.add_parameter('C2.x', low=-999, high=999) top.D2.max_iterations = 3 top.driver.workflow.add(['D1', 'D2']) top.D1.workflow.add(['C1', 'C2']) top.D2.workflow.add(['C1', 'C2']) top.run() self.assertEqual(top.D2.runcount, 1) self.assertEqual(top.D1.runcount, 1) self.assertEqual(top.C1.runcount, top.D1.max_iterations) self.assertEqual(top.C2.runcount, top.D2.max_iterations+1) # since C1 and C2 are not dependent on each other, they could # execute in any order (depending on dict hash value which can differ per platform) # so need two possible exec orders order1 = ['D1', 'C1', 'C2', 'C1', 'D2', 'C2', 'C2', 'C2'] order2 = ['D1', 'C2', 'C1', 'C1', 'D2', 'C2', 'C2', 'C2'] self.assertTrue(exec_order==order1 or exec_order==order2)
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']))