def test_double_arraycomp(self): # Mainly testing a bug in the array return for multiple arrays group = Group() group.add('x_param1', ParamComp('x1', np.ones((2))), promotes=['*']) group.add('x_param2', ParamComp('x2', np.ones((2))), promotes=['*']) group.add('mycomp', DoubleArrayComp(), promotes=['*']) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = PetscKSP() prob.setup(check=False) prob.run() Jbase = group.mycomp.JJ J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fwd', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fd', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='rev', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8)
def test_double_arraycomp(self): # Mainly testing a bug in the array return for multiple arrays group = Group() group.add('x_param1', IndepVarComp('x1', np.ones((2))), promotes=['*']) group.add('x_param2', IndepVarComp('x2', np.ones((2))), promotes=['*']) group.add('mycomp', DoubleArrayComp(), promotes=['*']) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = PetscKSP() prob.setup(check=False) prob.run() Jbase = group.mycomp.JJ J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fwd', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fd', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='rev', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8)
def test_simple_jac(self): group = Group() group.add('x_param', ParamComp('x', 1.0), promotes=['*']) group.add('mycomp', ExecComp(['y=2.0*x']), promotes=['x', 'y']) prob = Problem() prob.root = group prob.root.ln_solver = ExplicitSolver() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)
def test_simple(self): group = Group() group.add('x_param', ParamComp('x', 1.0), promotes=['*']) group.add('mycomp', SimpleCompDerivMatVec(), promotes=['x', 'y']) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = PetscKSP() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)
def test_simple(self): group = Group() group.add('x_param', IndepVarComp('x', 1.0), promotes=['*']) group.add('mycomp', SimpleCompDerivMatVec(), promotes=['x', 'y']) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = PetscKSP() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)
def test_simple_jac(self): group = Group() group.add('x_param', IndepVarComp('x', 1.0), promotes=['*']) group.add('mycomp', ExecComp(['y=2.0*x']), promotes=['x', 'y']) prob = Problem() prob.root = group prob.root.ln_solver = DirectSolver() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)
def test_simple_in_group_matvec(self): group = Group() sub = group.add('sub', Group(), promotes=['x', 'y']) group.add('x_param', ParamComp('x', 1.0), promotes=['*']) sub.add('mycomp', SimpleCompDerivMatVec(), promotes=['x', 'y']) prob = Problem() prob.root = group prob.root.ln_solver = ExplicitSolver() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') assert_rel_error(self, J['y']['x'][0][0], 2.0, 1e-6)
def test_linear_system(self): root = Group() root.add('lin', LinearSystem(3)) x = np.array([1, 2, -3]) A = np.array([[5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]]) b = A.dot(x) root.add('p1', ParamComp('A', A)) root.add('p2', ParamComp('b', b)) root.connect('p1.A', 'lin.A') root.connect('p2.b', 'lin.b') prob = Problem(root) prob.setup(check=False) prob.run() # Make sure it gets the right answer assert_rel_error(self, prob['lin.x'], x, .0001) assert_rel_error(self, np.linalg.norm(prob.root.resids.vec), 0.0, 1e-10) # Compare against calculated derivs Ainv = np.linalg.inv(A) dx_dA = np.outer(Ainv, -x).reshape(3, 9) dx_db = Ainv J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='fwd', return_format='dict') assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001) assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001) J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='rev', return_format='dict') assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001) assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001) J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='fd', return_format='dict') assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001) assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001)
def test_array2D(self): group = Group() group.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*']) group.add('mycomp', ArrayComp2D(), promotes=['x', 'y']) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = PetscKSP() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') Jbase = prob.root.mycomp._jacobian_cache diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x']) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x']) assert_rel_error(self, diff, 0.0, 1e-8)
def test_array2D(self): group = Group() group.add('x_param', ParamComp('x', np.ones((2, 2))), promotes=['*']) group.add('mycomp', ArrayComp2D(), promotes=['x', 'y']) prob = Problem() prob.root = group prob.root.ln_solver = ExplicitSolver() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['y'], mode='fwd', return_format='dict') Jbase = prob.root.mycomp._jacobian_cache diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x']) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x'], ['y'], mode='rev', return_format='dict') diff = np.linalg.norm(J['y']['x'] - Jbase['y', 'x']) assert_rel_error(self, diff, 0.0, 1e-8)
def test_indices(self): size = 10 root = Group() root.add('P1', ParamComp('x', np.zeros(size))) root.add( 'C1', ExecComp('y = x * 2.', y=np.zeros(size // 2), x=np.zeros(size // 2))) root.add( 'C2', ExecComp('y = x * 3.', y=np.zeros(size // 2), x=np.zeros(size // 2))) root.connect('P1.x', "C1.x", src_indices=list(range(size // 2))) root.connect('P1.x', "C2.x", src_indices=list(range(size // 2, size))) prob = Problem(root) prob.setup(check=False) root.P1.unknowns['x'][0:size // 2] += 1.0 root.P1.unknowns['x'][size // 2:size] -= 1.0 prob.run() assert_rel_error(self, root.C1.params['x'], np.ones(size // 2), 0.0001) assert_rel_error(self, root.C2.params['x'], -np.ones(size // 2), 0.0001)
def test_indices(self): size = 10 root = Group() root.add('P1', ParamComp('x', np.zeros(size))) root.add('C1', ExecComp('y = x * 2.', y=np.zeros(size//2), x=np.zeros(size//2))) root.add('C2', ExecComp('y = x * 3.', y=np.zeros(size//2), x=np.zeros(size//2))) root.connect('P1.x', "C1.x", src_indices=list(range(size//2))) root.connect('P1.x', "C2.x", src_indices=list(range(size//2, size))) prob = Problem(root) prob.setup(check=False) root.P1.unknowns['x'][0:size//2] += 1.0 root.P1.unknowns['x'][size//2:size] -= 1.0 prob.run() assert_rel_error(self, root.C1.params['x'], np.ones(size//2), 0.0001) assert_rel_error(self, root.C2.params['x'], -np.ones(size//2), 0.0001)
def test_linear_system(self): root = Group() root.add('lin', LinearSystem(3)) x = np.array([1, 2, -3]) A = np.array([[ 5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]]) b = A.dot(x) root.add('p1', IndepVarComp('A', A)) root.add('p2', IndepVarComp('b', b)) root.connect('p1.A', 'lin.A') root.connect('p2.b', 'lin.b') prob = Problem(root) prob.setup(check=False) prob.run() # Make sure it gets the right answer assert_rel_error(self, prob['lin.x'], x, .0001) assert_rel_error(self, np.linalg.norm(prob.root.resids.vec), 0.0, 1e-10) # Compare against calculated derivs Ainv = np.linalg.inv(A) dx_dA = np.outer(Ainv, -x).reshape(3, 9) dx_db = Ainv J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='fwd', return_format='dict') assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001) assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001) J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='rev', return_format='dict') assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001) assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001) J = prob.calc_gradient(['p1.A', 'p2.b'], ['lin.x'], mode='fd', return_format='dict') assert_rel_error(self, J['lin.x']['p1.A'], dx_dA, .0001) assert_rel_error(self, J['lin.x']['p2.b'], dx_db, .0001)
def test_array_to_scalar(self): root = Group() root.add('P1', ParamComp('x', np.array([2., 3.]))) root.add('C1', SimpleComp()) root.add('C2', ExecComp('y = x * 3.', y=0., x=0.)) root.connect('P1.x', 'C1.x', src_indices=[0,]) root.connect('P1.x', 'C2.x', src_indices=[1,]) prob = Problem(root) prob.setup(check=False) prob.run() self.assertAlmostEqual(root.C1.params['x'], 2.) self.assertAlmostEqual(root.C2.params['x'], 3.)
def test_subarray_to_promoted_var(self): root = Group() P = root.add('P', IndepVarComp('x', np.array([1., 2., 3., 4., 5.]))) G = root.add('G', Group()) C = root.add('C', SimpleComp()) A = G.add('A', SimpleArrayComp()) G2 = G.add('G2', Group()) A2 = G2.add('A2', SimpleArrayComp()) root.connect('P.x', 'G.A.x', src_indices=[0,1]) root.connect('P.x', 'C.x', src_indices=[2,]) root.connect('P.x', 'G.G2.A2.x', src_indices=[3, 4]) prob = Problem(root) prob.setup(check=False) prob.run() assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]), 0.0001) self.assertAlmostEqual(root.C.params['x'], 3.) assert_rel_error(self, root.G.G2.A2.params['x'], np.array([4., 5.]), 0.0001) # now try the same thing with promoted var root = Group() P = root.add('P', IndepVarComp('x', np.array([1., 2., 3., 4., 5.]))) G = root.add('G', Group()) C = root.add('C', SimpleComp()) A = G.add('A', SimpleArrayComp(), promotes=['x', 'y']) G2 = G.add('G2', Group()) A2 = G2.add('A2', SimpleArrayComp(), promotes=['x', 'y']) root.connect('P.x', 'G.x', src_indices=[0,1]) root.connect('P.x', 'C.x', src_indices=[2,]) root.connect('P.x', 'G.G2.x', src_indices=[3, 4]) prob = Problem(root) prob.setup(check=False) prob.run() assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]), 0.0001) self.assertAlmostEqual(root.C.params['x'], 3.) assert_rel_error(self, root.G.G2.A2.params['x'], np.array([4., 5.]), 0.0001)
def test_subarray_to_promoted_var(self): root = Group() P = root.add('P', ParamComp('x', np.array([1., 2., 3.]))) G = root.add('G', Group()) C = root.add('C', SimpleComp()) A = G.add('A', SimpleArrayComp()) # , promotes=['x', 'y']) root.connect('P.x', 'G.A.x', src_indices=[0, 1]) root.connect('P.x', 'C.x', src_indices=[ 2, ]) prob = Problem(root) prob.setup(check=False) prob.run() assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]), 0.0001) self.assertAlmostEqual(root.C.params['x'], 3.) # no try the same thing with promoted var root = Group() P = root.add('P', ParamComp('x', np.array([1., 2., 3.]))) G = root.add('G', Group()) C = root.add('C', SimpleComp()) A = G.add('A', SimpleArrayComp(), promotes=['x', 'y']) root.connect('P.x', 'G.x', src_indices=[0, 1]) root.connect('P.x', 'C.x', src_indices=[ 2, ]) prob = Problem(root) prob.setup(check=False) prob.run() assert_rel_error(self, root.G.A.params['x'], np.array([1., 2.]), 0.0001) self.assertAlmostEqual(root.C.params['x'], 3.)
def test_array_to_scalar(self): root = Group() root.add('P1', ParamComp('x', np.array([2., 3.]))) root.add('C1', SimpleComp()) root.add('C2', ExecComp('y = x * 3.', y=0., x=0.)) root.connect('P1.x', 'C1.x', src_indices=[ 0, ]) root.connect('P1.x', 'C2.x', src_indices=[ 1, ]) prob = Problem(root) prob.setup(check=False) prob.run() self.assertAlmostEqual(root.C1.params['x'], 2.) self.assertAlmostEqual(root.C2.params['x'], 3.)
def test_indices_connect_error(self): root = Group() P = root.add('P', IndepVarComp('x', np.array([1., 2., 3., 4., 5.]))) G = root.add('G', Group()) C = root.add('C', SimpleComp()) A = G.add('A', SimpleArrayComp()) root.connect('P.x', 'G.A.x', src_indices=[0]) root.connect('P.x', 'C.x', src_indices=[2,]) expected_error_message = py3fix("Size 1 of the indexed sub-part of " "source 'P.x' must match the size " "'2' of the target 'G.A.x'") prob = Problem(root) with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message) # now try the same thing with promoted var root = Group() P = root.add('P', IndepVarComp('x', np.array([1., 2., 3., 4., 5.]))) G = root.add('G', Group()) C = root.add('C', SimpleComp()) A = G.add('A', SimpleArrayComp(), promotes=['x', 'y']) root.connect('P.x', 'G.x', src_indices=[0,1,2]) root.connect('P.x', 'C.x', src_indices=[2,]) expected_error_message = py3fix("Size 3 of the indexed sub-part of " "source 'P.x' must match the size " "'2' of the target 'G.x'") prob = Problem(root) with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message)
self.connect("nozzle_air.Fl_O:tot:Cp","tm.nozzle_air_Cp") self.connect("nozzle_air.Fl_O:stat:W","tm.nozzle_air_W") self.connect("bearing_air.Fl_O:tot:T","tm.bearing_air_Tt") self.connect("bearing_air.Fl_O:tot:Cp","tm.bearing_air_Cp") self.connect("bearing_air.Fl_O:stat:W","tm.bearing_air_W") self.connect('tm.ss_temp_residual','tmp_balance.ss_temp_residual') self.connect('tmp_balance.temp_boundary','tm.temp_boundary') #run stand-alone component if __name__ == "__main__": root = Group() root.add('fs', FlowStuff()) prob = Problem(root) prob.root.nl_solver = Newton() prob.root.nl_solver.options['atol'] = 1e-5 prob.root.nl_solver.options['iprint'] = 1 prob.root.nl_solver.options['rtol'] = 1e-5 prob.root.nl_solver.options['maxiter'] = 50 params = ( ('P', 0.3, {'units':'psi'}), ('T', 1500.0, {'units':'degR'}), ('W', 1.0, {'units':'lbm/s'}) ) #nozzle