def test_distributed_comp_states(self): prob = om.Problem() model = prob.model sub = model.add_subsystem('sub', om.Group(), promotes=['*']) sub.add_subsystem('d1', DistribExecComp( ['y1 = 28 - 0.2*y2', 'y1 = 18 - 0.2*y2'], arr_size=2), promotes=['y1', 'y2']) sub.add_subsystem('d2', DistribExecComp( ['y2 = y1**.5 + 7', 'y2 = y1**.5 - 3'], arr_size=2), promotes=['y1', 'y2']) sub.nonlinear_solver = om.BroydenSolver(state_vars=['y1', 'y2']) sub.linear_solver = om.DirectSolver() model.linear_solver = om.DirectSolver() prob.setup(check=False, force_alloc_complex=True) prob.set_solver_print(level=2) prob.run_model() # TODO - prob.get not working correctly on distributed vars. Once this is fixed, we can # test all values on all ranks. if model.comm.rank == 0: assert_rel_error(self, prob.get_val('y1', get_remote=True), 25.58830237, .00001) elif model.comm.rank == 1: assert_rel_error(self, prob.get_val('y1', get_remote=True), 17.75721382, .00001)
def test_distributed_comp_states(self): prob = om.Problem() model = prob.model sub = model.add_subsystem('sub', om.Group(), promotes=['*']) sub.add_subsystem('d1', DistribExecComp( ['y1 = 28 - 0.2*y2', 'y1 = 18 - 0.2*y2'], arr_size=2), promotes=['y1', 'y2']) sub.add_subsystem('d2', DistribExecComp( ['y2 = y1**.5 + 7', 'y2 = y1**.5 - 3'], arr_size=2), promotes=['y1', 'y2']) sub.nonlinear_solver = om.BroydenSolver(state_vars=['y1', 'y2']) sub.linear_solver = om.DirectSolver() model.linear_solver = om.DirectSolver() prob.setup(check=False, force_alloc_complex=True) prob.set_solver_print(level=2) prob.run_model() np.testing.assert_allclose(prob.get_val('y1', get_remote=True), np.array([25.58830237, 17.75721382]), .00001)
def test_distributed_comp(self): prob = om.Problem() model = prob.model sub = model.add_subsystem('sub', om.Group(), promotes=['*']) sub.add_subsystem('d1', DistribExecComp( ['y1 = 28 - 0.2*y2', 'y1 = 18 - 0.2*y2'], arr_size=2), promotes=['y1', 'y2']) sub.add_subsystem('d2', DistribExecComp( ['y2 = y1**.5 + 7', 'y2 = y1**.5 - 3'], arr_size=2), promotes=['y1', 'y2']) sub.nonlinear_solver = om.BroydenSolver() sub.linear_solver = om.LinearBlockGS() model.linear_solver = om.LinearBlockGS() prob.setup(check=False, force_alloc_complex=True) with self.assertRaises(Exception) as cm: prob.run_model() self.assertEqual( str(cm.exception), "Group (sub) has a BroydenSolver solver and contains a distributed system." )
def test_dup_dist(self, mode): # duplicated output, parallel input prob = om.Problem() model = prob.model size = 3 sizes = [2, 1] rank = prob.comm.rank model.add_subsystem('indep', om.IndepVarComp('x', np.ones(size))) model.add_subsystem( 'C1', DistribExecComp(['y=2.5*x', 'y=3.5*x'], arr_size=size)) model.connect('indep.x', 'C1.x') of = ['C1.y'] wrt = ['indep.x'] prob.model.linear_solver = om.LinearRunOnce() prob.setup(check=False, mode=mode) prob.set_solver_print(level=0) prob.run_model() assert_rel_error(self, prob.get_val('C1.y', get_remote=True), np.array([2.5, 2.5, 3.5], dtype=float), 1e-6) J = prob.compute_totals(of=of, wrt=wrt) expected = np.array([[2.5, 0, 0], [0, 2.5, 0], [0, 0, 3.5]], dtype=float) assert_rel_error(self, J['C1.y', 'indep.x'], expected, 1e-6) assert_rel_error(self, prob.get_val('C1.y', get_remote=True), np.array([2.5, 2.5, 3.5], dtype=float), 1e-6)
def test_distrib_direct_subbed(self): size = 3 prob = om.Problem() group = prob.model = om.Group() group.add_subsystem('P', om.IndepVarComp('x', np.arange(size))) sub = group.add_subsystem('sub', om.Group()) sub.add_subsystem( 'C1', DistribExecComp(['y=2.0*x', 'y=3.0*x'], arr_size=size, x=np.zeros(size), y=np.zeros(size))) sub.add_subsystem( 'C2', om.ExecComp(['z=3.0*y'], y=np.zeros(size), z=np.zeros(size))) prob.model.linear_solver = om.DirectSolver() group.connect('P.x', 'sub.C1.x') group.connect('sub.C1.y', 'sub.C2.y') prob.setup(check=False, mode='fwd') with self.assertRaises(Exception) as cm: prob.run_model() msg = "DirectSolver linear solver in Group (<model>) cannot be used in or above a ParallelGroup or a " + \ "distributed component." self.assertEqual(str(cm.exception), msg)
def test_distrib_direct(self): size = 3 group = om.Group() group.add_subsystem('P', om.IndepVarComp('x', np.arange(size))) group.add_subsystem( 'C1', DistribExecComp(['y=2.0*x', 'y=3.0*x'], arr_size=size, x=np.zeros(size), y=np.zeros(size))) group.add_subsystem( 'C2', om.ExecComp(['z=3.0*y'], y=np.zeros(size), z=np.zeros(size))) prob = om.Problem() prob.model = group prob.model.linear_solver = om.DirectSolver() prob.model.connect('P.x', 'C1.x') prob.model.connect('C1.y', 'C2.y') prob.setup(check=False, mode='fwd') with self.assertRaises(Exception) as cm: prob.run_model() self.assertEqual( str(cm.exception), "Group (<model>) has a DirectSolver solver and contains a distributed system." )
def test_distributed_comp(self): prob = om.Problem() model = prob.model sub = model.add_subsystem('sub', om.Group(), promotes=['*']) sub.add_subsystem('d1', DistribExecComp(['y1 = 28 - 0.2*y2', 'y1 = 18 - 0.2*y2'], arr_size=2), promotes=['y1', 'y2']) sub.add_subsystem('d2', DistribExecComp(['y2 = y1**.5 + 7', 'y2 = y1**.5 - 3'], arr_size=2), promotes=['y1', 'y2']) sub.nonlinear_solver = om.BroydenSolver() sub.linear_solver = om.LinearBlockGS() model.linear_solver = om.LinearBlockGS() prob.setup(check=False, force_alloc_complex=True) with self.assertRaises(Exception) as cm: prob.run_model() msg = "BroydenSolver linear solver in Group (sub) cannot be used in or above a ParallelGroup or a " + \ "distributed component." self.assertEqual(str(cm.exception), msg)
def test_par_dist(self, mode, auto): # non-distributed output, parallel input prob = om.Problem() model = prob.model size = 3 sizes = [2, 1] rank = prob.comm.rank model.add_subsystem('indep', om.IndepVarComp('x', np.ones(size))) par = model.add_subsystem('par', om.ParallelGroup()) par.add_subsystem( 'C1', om.ExecComp('y = 3 * x', x=np.zeros(size), y=np.zeros(size))) par.add_subsystem( 'C2', om.ExecComp('y = 5 * x', x=np.zeros(size), y=np.zeros(size))) model.add_subsystem( 'C3', DistribExecComp(['y=1.5*x1+2.5*x2', 'y=2.5*x1-.5*x2'], arr_size=size)) model.connect('indep.x', 'par.C1.x') model.connect('indep.x', 'par.C2.x') model.connect('par.C1.y', 'C3.x1') model.connect('par.C2.y', 'C3.x2') of = ['C3.y'] wrt = ['indep.x'] prob.model.linear_solver = om.LinearRunOnce() prob.setup(check=False, mode=mode) prob.set_solver_print(level=0) prob.run_model() assert_near_equal(prob.get_val('C3.y', get_remote=True), np.array([17, 17, 5], dtype=float), 1e-6) J = prob.compute_totals(of=of, wrt=wrt) expected = np.array([[17, 0, 0], [0, 17, 0], [0, 0, 5]], dtype=float) assert_near_equal(J['C3.y', 'indep.x'], expected, 1e-6) assert_near_equal(prob.get_val('C3.y', get_remote=True), np.array([17, 17, 5], dtype=float), 1e-6)
def test_dist_dup(self, mode, auto): # non-distributed output, parallel input # Note: Auto-ivc not supported for distributed inputs. prob = om.Problem() model = prob.model size = 3 rank = prob.comm.rank if not auto: model.add_subsystem('indep', om.IndepVarComp('x', np.ones(size)), promotes=['x']) model.add_subsystem('C1', DistribExecComp(['y=2.5*x', 'y=3.5*x'], arr_size=size), promotes=['x']) model.add_subsystem( 'sink', om.ExecComp('y=-1.5 * x', x=np.zeros(size), y=np.zeros(size))) model.connect('C1.y', 'sink.x', src_indices=om.slicer[:]) of = ['sink.y'] wrt = ['x'] prob.model.linear_solver = om.LinearRunOnce() prob.setup(check=False, mode=mode) prob.set_solver_print(level=0) prob.run_model() assert_near_equal(prob.get_val('sink.y', get_remote=True), np.array([-3.75, -3.75, -5.25], dtype=float), 1e-6) J = prob.compute_totals(of=of, wrt=wrt) expected = np.array([[-3.75, 0, 0], [0, -3.75, 0], [0, 0, -5.25]], dtype=float) assert_near_equal(J['sink.y', 'x'], expected, 1e-6) assert_near_equal(prob.get_val('sink.y', get_remote=True), np.array([-3.75, -3.75, -5.25], dtype=float), 1e-6)
def test_dup_dist(self, mode, auto): # Note: Auto-ivc not supported for distributed inputs. # non-distributed output, parallel input prob = om.Problem() model = prob.model size = 3 sizes = [2, 1] rank = prob.comm.rank if not auto: model.add_subsystem('indep', om.IndepVarComp('x', np.ones(size)), promotes=['x']) model.add_subsystem('C1', DistribExecComp(['y=2.5*x', 'y=3.5*x'], arr_size=size), promotes=['x']) of = ['C1.y'] wrt = ['x'] prob.model.linear_solver = om.LinearRunOnce() prob.setup(check=False, mode=mode) prob.set_solver_print(level=0) prob.run_model() assert_near_equal(prob.get_val('C1.y', get_remote=True), np.array([2.5, 2.5, 3.5], dtype=float), 1e-6) J = prob.compute_totals(of=of, wrt=wrt) expected = np.array([[2.5, 0, 0], [0, 2.5, 0], [0, 0, 3.5]], dtype=float) assert_near_equal(J['C1.y', 'x'], expected, 1e-6) assert_near_equal(prob.get_val('C1.y', get_remote=True), np.array([2.5, 2.5, 3.5], dtype=float), 1e-6)
def test_dist_dup(self, mode): # duplicated output, parallel input prob = om.Problem() model = prob.model size = 3 rank = prob.comm.rank model.add_subsystem('indep', om.IndepVarComp('x', np.ones(size))) model.add_subsystem( 'C1', DistribExecComp(['y=2.5*x', 'y=3.5*x'], arr_size=size)) model.add_subsystem( 'sink', om.ExecComp('y=-1.5 * x', x=np.zeros(size), y=np.zeros(size))) model.connect('indep.x', 'C1.x') model.connect('C1.y', 'sink.x') of = ['sink.y'] wrt = ['indep.x'] prob.model.linear_solver = om.LinearRunOnce() #import wingdbstub prob.setup(check=False, mode=mode) prob.set_solver_print(level=0) prob.run_model() assert_near_equal(prob.get_val('sink.y', get_remote=True), np.array([-3.75, -3.75, -5.25], dtype=float), 1e-6) J = prob.compute_totals(of=of, wrt=wrt) expected = np.array([[-3.75, 0, 0], [0, -3.75, 0], [0, 0, -5.25]], dtype=float) assert_near_equal(J['sink.y', 'indep.x'], expected, 1e-6) assert_near_equal(prob.get_val('sink.y', get_remote=True), np.array([-3.75, -3.75, -5.25], dtype=float), 1e-6)