def test_fan_out_grouped(self, solv_tup, nlsolver): prob = om.Problem(FanOutGrouped()) of = ['c2.y', "c3.y"] wrt = ['iv.x'] solver, jactype = solv_tup prob.model.linear_solver = solver() if jactype is not None: prob.model.options['assembled_jac_type'] = jactype prob.model.nonlinear_solver = nlsolver() prob.setup(check=False, mode='fwd') prob.set_solver_print(level=0) prob.run_model() J = prob.compute_totals(of=['c2.y', "c3.y"], wrt=['iv.x']) assert_near_equal(J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_near_equal(J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) assert_near_equal(prob['c2.y'], -6.0, 1e-6) assert_near_equal(prob['c3.y'], 15.0, 1e-6) prob.setup(check=False, mode='rev') prob.run_model() J = prob.compute_totals(of=['c2.y', "c3.y"], wrt=['iv.x']) assert_near_equal(J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_near_equal(J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) assert_near_equal(prob['c2.y'], -6.0, 1e-6) assert_near_equal(prob['c3.y'], 15.0, 1e-6)
def test_fan_out_parallel_sets_rev(self): prob = om.Problem() prob.model = FanOutGrouped() prob.model.linear_solver = om.LinearBlockGS() prob.model.sub.linear_solver = om.LinearBlockGS() prob.model.add_design_var('iv.x') prob.model.add_constraint('c2.y', upper=0.0, parallel_deriv_color='par_resp') prob.model.add_constraint('c3.y', upper=0.0, parallel_deriv_color='par_resp') prob.setup(check=False, mode='rev') prob.run_driver() unknown_list = ['c2.y', 'c3.y'] indep_list = ['iv.x'] J = prob.compute_totals(unknown_list, indep_list, return_format='flat_dict') assert_near_equal(J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_near_equal(J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) # Piggyback to make sure the distributed norm calculation is correct. vec = prob.model._vectors['residual']['c2.y'] norm_val = vec.get_norm() # NOTE: BAN updated the norm value for the PR that added seed splitting, i.e. # the seed, c2.y in this case, is half what it was before (-.5 vs. -1). assert_near_equal(norm_val, 6.422616289332565, 1e-6)
def test_fan_out_parallel_sets_rev(self): prob = Problem() prob.model = FanOutGrouped() prob.model.linear_solver = LinearBlockGS() prob.model.sub.linear_solver = LinearBlockGS() prob.model.add_design_var('iv.x') prob.model.add_constraint('c2.y', upper=0.0, parallel_deriv_color='par_resp') prob.model.add_constraint('c3.y', upper=0.0, parallel_deriv_color='par_resp') prob.setup(vector_class=vector_class, check=False, mode='rev') prob.run_driver() unknown_list = ['c2.y', 'c3.y'] indep_list = ['iv.x'] J = prob.compute_totals(unknown_list, indep_list, return_format='flat_dict') assert_rel_error(self, J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y', 'iv.x'][0][0], 15.0, 1e-6)
def test_fan_out_grouped(self): prob = Problem(FanOutGrouped()) of = ['c2.y', "c3.y"] wrt = ['iv.x'] prob.setup(check=False, mode='fwd') prob.set_solver_print(level=0) prob.run_model() J = prob.compute_totals(of=['c2.y', "c3.y"], wrt=['iv.x']) assert_rel_error(self, J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) assert_rel_error(self, prob['c2.y'], -6.0, 1e-6) assert_rel_error(self, prob['c3.y'], 15.0, 1e-6) prob.setup(check=False, mode='rev') prob.run_model() J = prob.compute_totals(of=['c2.y', "c3.y"], wrt=['iv.x']) assert_rel_error(self, J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) assert_rel_error(self, prob['c2.y'], -6.0, 1e-6) assert_rel_error(self, prob['c3.y'], 15.0, 1e-6)
def test_fan_out_parallel_sets_rev(self): prob = om.Problem() prob.model = FanOutGrouped() prob.model.linear_solver = om.LinearBlockGS() prob.model.sub.linear_solver = om.LinearBlockGS() prob.model.add_design_var('iv.x') prob.model.add_constraint('c2.y', upper=0.0, parallel_deriv_color='par_resp') prob.model.add_constraint('c3.y', upper=0.0, parallel_deriv_color='par_resp') prob.setup(check=False, mode='rev') prob.run_driver() unknown_list = ['c2.y', 'c3.y'] indep_list = ['iv.x'] J = prob.compute_totals(unknown_list, indep_list, return_format='flat_dict') assert_near_equal(J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_near_equal(J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) # Piggyback to make sure the distributed norm calculation is correct. vec = prob.model._vectors['residual']['linear'] norm_val = vec.get_norm() # NOTE: BAN updated the norm value for the PR that removed vec_names vectors. # the seeds for the constraints are now back to -1 instead of -.5 assert_near_equal(norm_val, 6.557438524302, 1e-6)
def test_fan_out_parallel_sets_rev(self): prob = om.Problem() prob.model = FanOutGrouped() prob.model.linear_solver = om.LinearBlockGS() prob.model.sub.linear_solver = om.LinearBlockGS() prob.model.add_design_var('iv.x') prob.model.add_constraint('c2.y', upper=0.0, parallel_deriv_color='par_resp') prob.model.add_constraint('c3.y', upper=0.0, parallel_deriv_color='par_resp') prob.setup(check=False, mode='rev') prob.run_driver() unknown_list = ['c2.y', 'c3.y'] indep_list = ['iv.x'] J = prob.compute_totals(unknown_list, indep_list, return_format='flat_dict') assert_rel_error(self, J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y', 'iv.x'][0][0], 15.0, 1e-6) # Piggyback to make sure the distributed norm calculation is correct. vec = prob.model._vectors['residual']['c2.y'] norm_val = vec.get_norm() assert_rel_error(self, norm_val, 6.480740698, 1e-6)
def test_fan_out_grouped(self): # Test derivatives for fan-out-grouped topology. prob = Problem() prob.model = FanOutGrouped() prob.model.linear_solver = self.linear_solver_class() prob.set_solver_print(level=0) prob.setup(check=False, mode='fwd') prob.run_model() wrt = ['iv.x'] of = ['sub.c2.y', "sub.c3.y"] J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_near_equal(J['sub.c2.y', 'iv.x'], [[-6.0]], 1e-6) assert_near_equal(J['sub.c3.y', 'iv.x'], [[15.0]], 1e-6) J = prob.compute_totals(of=of, wrt=wrt, return_format='flat_dict') assert_near_equal(J['sub.c2.y', 'iv.x'], [[-6.0]], 1e-6) assert_near_equal(J['sub.c3.y', 'iv.x'], [[15.0]], 1e-6)
def test_fan_out_serial_sets_rev(self): prob = om.Problem() prob.model = FanOutGrouped() prob.model.linear_solver = om.LinearBlockGS() prob.model.sub.linear_solver = om.LinearBlockGS() prob.model.add_design_var('iv.x') prob.model.add_constraint('c2.y', upper=0.0) prob.model.add_constraint('c3.y', upper=0.0) prob.setup(check=False, mode='rev') prob.run_driver() unknown_list = ['c3.y','c2.y'] #['c2.y', 'c3.y'] indep_list = ['iv.x'] J = prob.compute_totals(unknown_list, indep_list, return_format='flat_dict') assert_near_equal(J['c2.y', 'iv.x'][0][0], -6.0, 1e-6) assert_near_equal(J['c3.y', 'iv.x'][0][0], 15.0, 1e-6)
def test_parallel_list_vars(self): print_opts = {'linewidth': 1024, 'precision': 1} if LooseVersion(np.__version__) >= LooseVersion("1.14"): print_opts['legacy'] = '1.13' prob = om.Problem(FanOutGrouped()) # add another subsystem with similar prefix prob.model.add_subsystem('sub2', om.ExecComp(['y=x'])) prob.model.connect('iv.x', 'sub2.x') prob.setup() prob.run_model() # # list inputs, not hierarchical # stream = StringIO() with printoptions(**print_opts): prob.model.list_inputs(values=True, hierarchical=False, out_stream=stream) with multi_proc_exception_check(prob.comm): if prob.comm.rank == 0: # Only rank 0 prints text = stream.getvalue().split('\n') expected = [ "6 Input(s) in 'model'", '', 'varname val', '-------- -----', 'c1.x', 'sub.c2.x', 'sub.c3.x', 'c2.x', 'c3.x', 'sub2.x' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertTrue(text[i].startswith(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) # # list inputs, hierarchical # stream = StringIO() with printoptions(**print_opts): prob.model.list_inputs(values=True, hierarchical=True, out_stream=stream) with multi_proc_exception_check(prob.comm): if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "6 Input(s) in 'model'", '', 'varname val', '------- -----', 'c1', ' x', 'sub', ' c2', ' x', ' c3', ' x', 'c2', ' x', 'c3', ' x', 'sub2', ' x' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertTrue(text[i].startswith(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) # # list outputs, not hierarchical # stream = StringIO() with printoptions(**print_opts): prob.model.list_outputs(values=True, residuals=True, hierarchical=False, out_stream=stream) with multi_proc_exception_check(prob.comm): if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "7 Explicit Output(s) in 'model'", '', 'varname val resids', '-------- ---- ------', 'iv.x', 'c1.y', 'sub.c2.y', 'sub.c3.y', 'c2.y', 'c3.y', 'sub2.y', '', '', "0 Implicit Output(s) in 'model'", ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertTrue(text[i].startswith(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) # # list outputs, hierarchical # stream = StringIO() with printoptions(**print_opts): prob.model.list_outputs(values=True, residuals=True, hierarchical=True, out_stream=stream) with multi_proc_exception_check(prob.comm): if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "7 Explicit Output(s) in 'model'", '', 'varname val resids', '------- ----- ------', 'iv', ' x', 'c1', ' y', 'sub', ' c2', ' y', ' c3', ' y', 'c2', ' y', 'c3', ' y', 'sub2', ' y', '', '', "0 Implicit Output(s) in 'model'", ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertTrue(text[i].startswith(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i]))
def test_parallel_list_vars(self): prob = Problem(FanOutGrouped()) # add another subsystem with similar prefix prob.model.add_subsystem('sub2', ExecComp(['y=x'])) prob.model.connect('iv.x', 'sub2.x') prob.setup() prob.run_model() # # list inputs, not hierarchical # stream = cStringIO() prob.model.list_inputs(values=True, hierarchical=False, out_stream=stream) if prob.comm.rank == 0: # Only rank 0 prints text = stream.getvalue().split('\n') expected = [ "6 Input(s) in 'model'", '---------------------', '', 'varname value', '-------- -----', 'c1.x', 'sub.c2.x', 'sub.c3.x', 'c2.x', 'c3.x', 'sub2.x' ] for i in range(len(expected)): self.assertTrue(text[i].startswith(expected[i]), '\nExpected: %s\nReceived: %s\n' % (expected[i], text[i])) # # list inputs, hierarchical # stream = cStringIO() prob.model.list_inputs(values=True, hierarchical=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "6 Input(s) in 'model'", '---------------------', '', 'varname value', '------- -----', 'top', ' c1', ' x', ' sub', ' c2', ' x', ' c3', ' x', ' c2', ' x', ' c3', ' x', ' sub2', ' x' ] for i in range(len(expected)): self.assertTrue(text[i].startswith(expected[i]), '\nExpected: %s\nReceived: %s\n' % (expected[i], text[i])) # # list outputs, not hierarchical # stream = cStringIO() prob.model.list_outputs(values=True, residuals=True, hierarchical=False, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "7 Explicit Output(s) in 'model'", '-------------------------------', '', 'varname value resids', '-------- ----- ------', 'iv.x', 'c1.y', 'sub.c2.y', 'sub.c3.y', 'c2.y', 'c3.y', 'sub2.y', '', '', "0 Implicit Output(s) in 'model'", '-------------------------------', ] for i in range(len(expected)): self.assertTrue(text[i].startswith(expected[i]), '\nExpected: %s\nReceived: %s\n' % (expected[i], text[i])) # # list outputs, hierarchical # stream = cStringIO() prob.model.list_outputs(values=True, residuals=True, hierarchical=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "7 Explicit Output(s) in 'model'", '-------------------------------', '', 'varname value resids', '------- ----- ------', 'top', ' iv', ' x', ' c1', ' y', ' sub', ' c2', ' y', ' c3', ' y', ' c2', ' y', ' c3', ' y', ' sub2', ' y', '', '', "0 Implicit Output(s) in 'model'", '-------------------------------', ] for i in range(len(expected)): self.assertTrue(text[i].startswith(expected[i]), '\nExpected: %s\nReceived: %s\n' % (expected[i], text[i]))