def test(self): nn = 2 p = om.Problem() p.model.add_subsystem(name='mat_vec_product_comp', subsys=om.MatrixVectorProductComp(A_name='Mat', vec_size=nn, b_name='y', b_units='m', x_units='m'), promotes_inputs=['Mat', 'x']) p.setup() p.set_val('Mat', np.random.rand(nn, 3, 3)) p.set_val('x', np.random.rand(nn, 3)) p.run_model() assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[0, :], np.dot(p['Mat'][0, :, :], p['x'][0, :]) * 3.2808399, tolerance=1.0E-8) assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[1, :], np.dot(p['Mat'][1, :, :], p['x'][1, :]) * 3.2808399, tolerance=1.0E-8)
def setUp(self): self.nn = 5 self.p = om.Problem() ivc = om.IndepVarComp() ivc.add_output(name='A', shape=(self.nn, 6, 4)) ivc.add_output(name='x', shape=(self.nn, 4)) self.p.model.add_subsystem(name='ivc', subsys=ivc, promotes_outputs=['A', 'x']) self.p.model.add_subsystem(name='mat_vec_product_comp', subsys=om.MatrixVectorProductComp( vec_size=self.nn, A_shape=(6, 4))) self.p.model.connect('A', 'mat_vec_product_comp.A') self.p.model.connect('x', 'mat_vec_product_comp.x') self.p.setup(force_alloc_complex=True) self.p['A'] = np.random.rand(self.nn, 6, 4) self.p['x'] = np.random.rand(self.nn, 4) self.p.run_model()
def test_duplicate_outputs(self): mvp = om.MatrixVectorProductComp() with self.assertRaises(NameError) as ctx: mvp.add_product('b', 'B', 'y') self.assertEqual( str(ctx.exception), "<class MatrixVectorProductComp>: " "Multiple definition of output 'b'.")
def test_x_units_mismatch(self): mvp = om.MatrixVectorProductComp() with self.assertRaises(ValueError) as ctx: mvp.add_product('c', 'A', 'x', x_units='ft') self.assertEqual( str(ctx.exception), "<class MatrixVectorProductComp>: " "Conflicting units 'ft' specified for vector 'x', " "which has already been defined with units 'None'.")
def test_x_vec_size_mismatch(self): mvp = om.MatrixVectorProductComp() with self.assertRaises(ValueError) as ctx: mvp.add_product('c', 'B', 'x', A_shape=(5, 5)) self.assertEqual( str(ctx.exception), "<class MatrixVectorProductComp>: " "Matrix shape (5, 5) is incompatible with vector 'x', " "which has already been defined with 3 column(s).")
def test_x_vec_size_mismatch(self): mvp = om.MatrixVectorProductComp() with self.assertRaises(ValueError) as ctx: mvp.add_product('c', 'B', 'x', vec_size=10) self.assertEqual( str(ctx.exception), "<class MatrixVectorProductComp>: " "Conflicting vec_size=10 specified for vector 'x', " "which has already been defined with vec_size=1.")
def test_A_shape_mismatch(self): mvp = om.MatrixVectorProductComp() with self.assertRaises(ValueError) as ctx: mvp.add_product('c', 'A', 'y', A_shape=(5, 5)) self.assertEqual( str(ctx.exception), "<class MatrixVectorProductComp>: " "Conflicting shape (5, 5) specified for matrix 'A', " "which has already been defined with shape (3, 3).")
def test_output_as_input_x(self): mvp = om.MatrixVectorProductComp() with self.assertRaises(NameError) as ctx: mvp.add_product('c', 'A', 'b') self.assertEqual( str(ctx.exception), "<class MatrixVectorProductComp>: " "'b' specified as an input, but it has already been " "defined as an output.")
def test_multiple(self): import numpy as np import openmdao.api as om nn = 2 p = om.Problem() mvp = om.MatrixVectorProductComp(A_name='Mat', vec_size=nn, b_name='y', b_units='m', x_units='m') mvp.add_product(A_name='Mat', vec_size=nn, b_name='z', b_units='m', x_name='w', x_units='m') p.model.add_subsystem(name='mat_vec_product_comp', subsys=mvp, promotes_inputs=['Mat', 'x', 'w']) p.setup() p.set_val('Mat', np.random.rand(nn, 3, 3)) p.set_val('x', np.random.rand(nn, 3)) p.set_val('w', np.random.rand(nn, 3)) p.run_model() assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[0, :], np.dot(p['Mat'][0, :, :], p['x'][0, :]) * 3.2808399, tolerance=1.0E-8) assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[1, :], np.dot(p['Mat'][1, :, :], p['x'][1, :]) * 3.2808399, tolerance=1.0E-8) assert_near_equal(p.get_val('mat_vec_product_comp.z', units='ft')[0, :], np.dot(p['Mat'][0, :, :], p['w'][0, :]) * 3.2808399, tolerance=1.0E-8) assert_near_equal(p.get_val('mat_vec_product_comp.z', units='ft')[1, :], np.dot(p['Mat'][1, :, :], p['w'][1, :]) * 3.2808399, tolerance=1.0E-8)
def setup(self): ivc = om.IndepVarComp() ivc.add_output(name='A', shape=(2, 5, 3), units='ft') ivc.add_output(name='x', shape=(2, 3), units='lbf') mvp = om.MatrixVectorProductComp(vec_size=2, A_shape=(5, 3), A_units='m', x_units='N', b_units='N*m') self.add_subsystem('ivc', ivc, promotes_outputs=['*']) self.add_subsystem('mvp', mvp, promotes=['*'])
def test(self): import numpy as np import openmdao.api as om nn = 2 p = om.Problem() ivc = om.IndepVarComp() ivc.add_output(name='Mat', shape=(nn, 3, 3)) ivc.add_output(name='x', shape=(nn, 3), units='m') p.model.add_subsystem(name='ivc', subsys=ivc, promotes_outputs=['Mat', 'x']) p.model.add_subsystem(name='mat_vec_product_comp', subsys=om.MatrixVectorProductComp(A_name='Mat', vec_size=nn, b_name='y', b_units='m', x_units='m')) p.model.connect('Mat', 'mat_vec_product_comp.Mat') p.model.connect('x', 'mat_vec_product_comp.x') p.setup() p['Mat'] = np.random.rand(nn, 3, 3) p['x'] = np.random.rand(nn, 3) p.run_model() Mat_i = p['Mat'][0, :, :] x_i = p['x'][0, :] expected_i = np.dot(Mat_i, x_i) * 3.2808399 assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[0, :], expected_i, tolerance=1.0E-8) Mat_i = p['Mat'][1, :, :] x_i = p['x'][1, :] expected_i = np.dot(Mat_i, x_i) * 3.2808399 assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[1, :], expected_i, tolerance=1.0E-8)
def setUp(self): self.nn = 2 ivc = om.IndepVarComp() ivc.add_output(name='A', shape=(self.nn, 5, 3), units='ft') ivc.add_output(name='x', shape=(self.nn, 3), units='lbf') ivc.add_output(name='B', shape=(self.nn, 5, 3), units='m') ivc.add_output(name='y', shape=(self.nn, 3), units='N') mvp = om.MatrixVectorProductComp(vec_size=self.nn, A_shape=(5, 3), A_units='m', x_units='N', b_units='N*m') mvp.add_product('c', A_name='B', x_name='y', A_shape=(5, 3), vec_size=self.nn, A_units='m', x_units='N', b_units='N*m') model = om.Group() model.add_subsystem(name='ivc', subsys=ivc, promotes_outputs=['*']) model.add_subsystem(name='mat_vec_product_comp', subsys=mvp, promotes=['*']) self.p = om.Problem(model) self.p.setup(force_alloc_complex=True) A = np.random.rand(self.nn, 5, 3) x = np.random.rand(self.nn, 3) self.p['A'] = A self.p['x'] = x self.p['B'] = convert_units(A, 'ft', 'm') self.p['y'] = convert_units(x, 'lbf', 'N') self.p.run_model()
def test(self): import numpy as np import openmdao.api as om from openmdao.utils.assert_utils import assert_rel_error nn = 100 p = om.Problem() ivc = om.IndepVarComp() ivc.add_output(name='A', shape=(nn, 3, 3)) ivc.add_output(name='x', shape=(nn, 3)) p.model.add_subsystem(name='ivc', subsys=ivc, promotes_outputs=['A', 'x']) p.model.add_subsystem(name='mat_vec_product_comp', subsys=om.MatrixVectorProductComp(A_name='M', vec_size=nn, b_name='y', b_units='m')) p.model.connect('A', 'mat_vec_product_comp.M') p.model.connect('x', 'mat_vec_product_comp.x') p.setup() p['A'] = np.random.rand(nn, 3, 3) p['x'] = np.random.rand(nn, 3) p.run_model() for i in range(nn): A_i = p['A'][i, :, :] x_i = p['x'][i, :] expected_i = np.dot(A_i, x_i) * 3.2808399 assert_rel_error(self, p.get_val('mat_vec_product_comp.y', units='ft')[i, :], expected_i, tolerance=1.0E-8)