def test_vectorized(self): """Check against the scipy solver.""" model = Group() x = np.array([[1, 2, -3], [2, -1, 4]]) A = np.array([[5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]]) b = np.einsum('jk,ik->ij', A, x) model.add_subsystem('p1', IndepVarComp('A', A)) model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem('lin', LinearSystemComp(size=3, vec_size=2)) model.connect('p1.A', 'lin.A') model.connect('p2.b', 'lin.b') prob = Problem(model) prob.setup() lingrp.linear_solver = ScipyKrylov() prob.set_solver_print(level=0) prob.run_model() assert_rel_error(self, prob['lin.x'], x, .0001) assert_rel_error(self, prob.model._residuals.get_norm(), 0.0, 1e-10) model.run_apply_nonlinear() with model._scaled_context_all(): val = model.lingrp.lin._residuals['x'] assert_rel_error(self, val, np.zeros((2, 3)), tolerance=1e-8)
def test_feature_vectorized(self): import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.api import LinearSystemComp, ScipyKrylov model = Group() A = np.array([[5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]]) b = np.array([[2.0, -3.0, 4.0], [1.0, 0.0, -1.0]]) model.add_subsystem('p1', IndepVarComp('A', A)) model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem('lin', LinearSystemComp(size=3, vec_size=2)) model.connect('p1.A', 'lin.A') model.connect('p2.b', 'lin.b') prob = Problem(model) prob.setup() lingrp.linear_solver = ScipyKrylov() prob.run_model() assert_rel_error( self, prob['lin.x'], np.array([[0.10596026, -0.16556291, 0.48675497], [0.19205298, -0.11258278, -0.14900662]]), .0001)
def test_feature_vectorized_A(self): import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.api import LinearSystemComp, ScipyKrylov model = Group() A = np.array([[[5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]], [[2.0, 3.0, 4.0], [1.0, -1.0, -2.0], [3.0, 2.0, -2.0]]]) b = np.array([[-5.0, 2.0, 3.0], [-1.0, 1.0, -3.0]]) model.add_subsystem('p1', IndepVarComp('A', A)) model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem( 'lin', LinearSystemComp(size=3, vec_size=2, vectorize_A=True)) model.connect('p1.A', 'lin.A') model.connect('p2.b', 'lin.b') prob = Problem(model) prob.setup() lingrp.linear_solver = ScipyKrylov() prob.run_model() assert_rel_error( self, prob['lin.x'], np.array([[-0.78807947, 0.66887417, 0.47350993], [0.7, -1.8, 0.75]]), .0001)
def test_solve_linear_vectorized(self): """Check against solve_linear.""" x = np.array([[1, 2, -3], [2, -1, 4]]) A = np.array([[1., 1., 1.], [1., 2., 3.], [0., 1., 3.]]) b = np.einsum('jk,ik->ij', A, x) b_T = np.einsum('jk,ik->ij', A.T, x) lin_sys_comp = LinearSystemComp(size=3, vec_size=2) prob = Problem() prob.model.add_subsystem('p1', IndepVarComp('A', A)) prob.model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = prob.model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem('lin', lin_sys_comp) prob.model.connect('p1.A', 'lin.A') prob.model.connect('p2.b', 'lin.b') prob.setup(check=False) prob.set_solver_print(level=0) prob.run_model() prob.model.run_linearize() # Compare against calculated derivs Ainv = np.array([[3., -2., 1.], [-3., 3., -2.], [1., -1., 1.]]) dx_dA0 = np.outer(Ainv, -x[0]).reshape(3, 9) dx_dA1 = np.outer(Ainv, -x[1]).reshape(3, 9) dx_dA = np.vstack((dx_dA0, dx_dA1)) dx_db = np.kron(np.eye(2), Ainv) d_inputs, d_outputs, d_residuals = lingrp.get_linear_vectors() # Forward mode with RHS of self.b d_residuals['lin.x'] = b lingrp.run_solve_linear(['linear'], 'fwd') sol = d_outputs['lin.x'] assert_rel_error(self, sol, x, .0001) # Reverse mode with RHS of self.b_T d_outputs['lin.x'] = b_T lingrp.run_solve_linear(['linear'], 'rev') sol = d_residuals['lin.x'] assert_rel_error(self, sol, x, .0001) J = prob.compute_totals(['lin.x'], ['p1.A', 'p2.b'], return_format='flat_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) data = prob.check_partials(out_stream=None) abs_errors = data['lingrp.lin'][('x', 'x')]['abs error'] self.assertTrue(len(abs_errors) > 0) for match in abs_errors: abs_error = float(match)
def test_feature_basic(self): import numpy as np from openmdao.api import Group, Problem, IndepVarComp from openmdao.api import LinearSystemComp, ScipyKrylov model = Group() A = np.array([[5.0, -3.0, 2.0], [1.0, 7.0, -4.0], [1.0, 0.0, 8.0]]) b = np.array([1.0, 2.0, -3.0]) model.add_subsystem('p1', IndepVarComp('A', A)) model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem('lin', LinearSystemComp(size=3)) model.connect('p1.A', 'lin.A') model.connect('p2.b', 'lin.b') prob = Problem(model) prob.setup() lingrp.linear_solver = ScipyKrylov() prob.run_model() assert_rel_error(self, prob['lin.x'], np.array([0.36423841, -0.00662252, -0.4205298]), .0001)
def test_linear_system(self): """Check against the scipy solver.""" model = Group() 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) model.add_subsystem('p1', IndepVarComp('A', A)) model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem( 'lin', LinearSystemComp(size=3, partial_type="matrix_free")) model.connect('p1.A', 'lin.A') model.connect('p2.b', 'lin.b') prob = Problem(model) prob.setup() lingrp.linear_solver = ScipyKrylov() prob.set_solver_print(level=0) prob.run_model() assert_rel_error(self, prob['lin.x'], x, .0001) assert_rel_error(self, prob.model._residuals.get_norm(), 0.0, 1e-10)
def setup(self): thermo = self.options['thermo'] num_element = thermo.num_element self.add_subsystem('TP2ls', PropsRHS(thermo), promotes_inputs=('T', 'n', 'n_moles', 'b0')) ne1 = num_element + 1 self.add_subsystem('ls2t', LinearSystemComp(size=ne1)) self.add_subsystem('ls2p', LinearSystemComp(size=ne1)) self.add_subsystem( 'tp2props', PropsCalcs(thermo=thermo), promotes_inputs=['n', 'n_moles', 'T', 'P'], promotes_outputs=['h', 'S', 'gamma', 'Cp', 'Cv', 'rho', 'R']) self.connect('TP2ls.lhs_TP', ['ls2t.A', 'ls2p.A']) self.connect('TP2ls.rhs_T', 'ls2t.b') self.connect('TP2ls.rhs_P', 'ls2p.b') self.connect('ls2t.x', 'tp2props.result_T') self.connect('ls2p.x', 'tp2props.result_P')
p = Problem() p.model = Group() indeps = p.model.add_subsystem('indeps', IndepVarComp(), promotes=['*']) indeps.add_output('T', val=2761.56784655, units='degK') indeps.add_output('n', val=np.array([2.272e-02, 1.000e-10, 1.136e-02])) indeps.add_output('composition', val=np.array([0.023, 0.045])) indeps.add_output('n_moles', val=0.0340831628675) props_rhs = p.model.add_subsystem('props_rhs', PropsRHS(thermo=thermo), promotes=["*"]) p.model.add_subsystem('ln_T', LinearSystemComp(size=thermo.num_element + 1)) p.model.connect('lhs_TP', 'ln_T.A') p.model.connect('rhs_T', 'ln_T.b') p.model.add_subsystem('ln_P', LinearSystemComp(size=thermo.num_element + 1)) p.model.connect('lhs_TP', 'ln_P.A') p.model.connect('rhs_P', 'ln_P.b') p.setup() p.run_model() print("outputs") print("rhs_t", repr(p['rhs_T'])) print("rhs_P", repr(p['rhs_P'])) print("lhs_TP", repr(p['lhs_TP']))
def test_solve_linear_vectorized_A(self): """Check against solve_linear.""" x = np.array([[1, 2, -3], [2, -1, 4]]) A = np.array([[[1., 1., 1.], [1., 2., 3.], [0., 1., 3.]], [[2.0, 3.0, 4.0], [1.0, -1.0, -2.0], [3.0, 2.0, -2.0]]]) b = np.einsum('ijk,ik->ij', A, x) b_T = np.einsum('ijk,ik->ij', A.transpose(0, 2, 1), x) lin_sys_comp = LinearSystemComp(size=3, vec_size=2, vectorize_A=True) prob = Problem() prob.model.add_subsystem('p1', IndepVarComp('A', A)) prob.model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = prob.model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem('lin', lin_sys_comp) prob.model.connect('p1.A', 'lin.A') prob.model.connect('p2.b', 'lin.b') prob.setup(check=False) prob.set_solver_print(level=0) prob.run_model() prob.model.run_linearize() # Compare against calculated derivs Ainv1 = np.array([[3., -2., 1.], [-3., 3., -2.], [1., -1., 1.]]) Ainv2 = np.array([[0.3, 0.7, -0.1], [-0.2, -0.8, 0.4], [0.25, 0.25, -0.25]]) dx_dA0 = np.outer(Ainv1, -x[0]).reshape(3, 9) dx_dA1 = np.outer(Ainv2, -x[1]).reshape(3, 9) dx_dA = np.zeros((6, 18)) dx_dA[:3, :9] = dx_dA0 dx_dA[3:, 9:] = dx_dA1 dx_db = np.zeros((6, 6)) dx_db[:3, :3] = Ainv1 dx_db[3:, 3:] = Ainv2 d_inputs, d_outputs, d_residuals = lingrp.get_linear_vectors() # Forward mode with RHS of self.b d_residuals['lin.x'] = b lingrp.run_solve_linear(['linear'], 'fwd') sol = d_outputs['lin.x'] assert_rel_error(self, sol, x, .0001) # Reverse mode with RHS of self.b_T d_outputs['lin.x'] = b_T lingrp.run_solve_linear(['linear'], 'rev') sol = d_residuals['lin.x'] assert_rel_error(self, sol, x, .0001) J = prob.compute_totals(['lin.x'], ['p1.A', 'p2.b'], return_format='flat_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) data = prob.check_partials(out_stream=None) abs_errors = data['lingrp.lin'][('x', 'x')]['abs error'] self.assertTrue(len(abs_errors) > 0) for match in abs_errors: abs_error = float(match)
def setup(self): mesh = self.options['mesh'] E = self.options['E'] v = self.options['v'] problem_type = self.options['problem_type'] ng = self.options['ng'] be = self.options['be'] le = self.options['le'] A = self.options['A'] f = self.options['f'] constraints = self.options['constraints'] pN = mesh.pN W = mesh.W ENT = mesh.ENT Elem_Coords = mesh.Elem_Coords NDOF = mesh.NDOF NEL = mesh.NEL NDIM = mesh.NDIM max_nn = mesh.max_nn max_ng = mesh.max_ng max_edof = mesh.max_edof NN = mesh.NN S = mesh.S.ind # comp = ExplicitComponent() # comp.add_output('d', shape = (NDOF)) # self.add_subsystem('d_comp', comp, promotes=['*']) if problem_type == 'plane_stress' or 'plane_strain': n_D = 3 if problem_type == 'truss': n_D = 1 comp = IndepVarComp() comp.add_output('t', shape=(NEL)) self.add_subsystem('t_comp', comp, promotes=['*']) comp = DComp(E=E, v=v, problem_type=problem_type) self.add_subsystem('D_comp', comp, promotes=['*']) comp = JacobianComp(pN=pN, Elem_Coords=Elem_Coords) self.add_subsystem('J_comp', comp, promotes=['*']) comp = BComp(pN=pN, problem_type=problem_type, max_edof=max_edof) self.add_subsystem('B_comp', comp, promotes=['*']) comp = Kel_localComp(W=W, max_edof=max_edof, n_D=n_D) self.add_subsystem('Kl_comp', comp, promotes=['*']) comp = KglobalComp(S=S, max_edof=max_edof, NEL=NEL, NDOF=NDOF) self.add_subsystem('Kg_comp', comp, promotes=['*']) comp = KKTComp(NDOF=NDOF, A=A, f=f, constraints=constraints) self.add_subsystem('KKT_comp', comp, promotes=['*']) self.add_subsystem('Solve_comp', LinearSystemComp(size=(NDOF + len(constraints)))) comp = DisplacementsComp(NDOF=NDOF, constraints=constraints) self.add_subsystem('Displacements_comp', comp, promotes=['*']) comp = ComplianceComp(NDOF=NDOF, f=f) self.add_subsystem('Compliance_comp', comp, promotes=['*']) comp = VolumeComp(NEL=NEL, be=be, le=le) self.add_subsystem('Volume_comp', comp, promotes=['*'])
def test_linear_system_solve_linear(self): """Check against solve_linear.""" x = np.array([1, 2, -3]) A = np.array([[1., 1., 1.], [1., 2., 3.], [0., 1., 3.]]) b = A.dot(x) b_T = A.T.dot(x) def check_derivs(lin_sys_comp): prob = Problem() prob.model.add_subsystem('p1', IndepVarComp('A', A)) prob.model.add_subsystem('p2', IndepVarComp('b', b)) lingrp = prob.model.add_subsystem('lingrp', Group(), promotes=['*']) lingrp.add_subsystem('lin', lin_sys_comp) prob.model.connect('p1.A', 'lin.A') prob.model.connect('p2.b', 'lin.b') prob.setup(check=False) prob.set_solver_print(level=0) prob.run_model() prob.model.run_linearize() # prob.check_partials() # Compare against calculated derivs # Ainv = np.linalg.inv(A) # Don't use linalg.inv or a mathematician will die Ainv = np.array([[3., -2., 1.], [-3., 3., -2.], [1., -1., 1.]]) dx_dA = np.outer(Ainv, -x).reshape(3, 9) dx_db = Ainv d_inputs, d_outputs, d_residuals = lingrp.get_linear_vectors() # Forward mode with RHS of self.b d_residuals['lin.x'] = b lingrp.run_solve_linear(['linear'], 'fwd') sol = d_outputs['lin.x'] assert_rel_error(self, sol, x, .0001) # Reverse mode with RHS of self.b_T d_outputs['lin.x'] = b_T lingrp.run_solve_linear(['linear'], 'rev') sol = d_residuals['lin.x'] assert_rel_error(self, sol, x, .0001) J = prob.compute_totals(['lin.x'], ['p1.A', 'p2.b'], return_format='flat_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) # print lin_sys_comp = LinearSystemComp(size=3, partial_type="matrix_free") check_derivs(lin_sys_comp) lin_sys_comp = LinearSystemComp(size=3, partial_type="dense") check_derivs(lin_sys_comp) lin_sys_comp = LinearSystemComp(size=3, partial_type="sparse") check_derivs(lin_sys_comp)