def test_dimensions(self): bm = BlockMatrix(2, 2) self.assertTrue(bm.has_undefined_row_sizes()) self.assertTrue(bm.has_undefined_col_sizes()) with self.assertRaises(NotFullyDefinedBlockMatrixError): shape = bm.shape with self.assertRaises(NotFullyDefinedBlockMatrixError): bm.set_block(0, 0, BlockMatrix(2, 2)) with self.assertRaises(NotFullyDefinedBlockMatrixError): row_sizes = bm.row_block_sizes() with self.assertRaises(NotFullyDefinedBlockMatrixError): col_sizes = bm.col_block_sizes() bm2 = BlockMatrix(2, 2) bm2.set_block(0, 0, coo_matrix((2, 2))) bm2.set_block(1, 1, coo_matrix((2, 2))) bm3 = bm2.copy() bm.set_block(0, 0, bm2) bm.set_block(1, 1, bm3) self.assertFalse(bm.has_undefined_row_sizes()) self.assertFalse(bm.has_undefined_col_sizes()) self.assertEqual(bm.shape, (8, 8)) bm.set_block(0, 0, None) self.assertFalse(bm.has_undefined_row_sizes()) self.assertFalse(bm.has_undefined_col_sizes()) self.assertEqual(bm.shape, (8, 8)) self.assertTrue(np.all(bm.row_block_sizes() == np.ones(2) * 4)) self.assertTrue(np.all(bm.col_block_sizes() == np.ones(2) * 4)) self.assertTrue( np.all(bm.row_block_sizes(copy=False) == np.ones(2) * 4)) self.assertTrue( np.all(bm.col_block_sizes(copy=False) == np.ones(2) * 4))
def test_lt(self): v = BlockVector(2) a = np.ones(5) b = np.zeros(9) v.set_block(0, a) v.set_block(1, b) flags = v < 1 v.set_block(0, a - 1) v.set_block(1, b + 1) self.assertEqual(v.nblocks, flags.nblocks) for bid, blk in enumerate(flags): self.assertTrue(np.allclose(blk, v.get_block(bid))) v.set_block(0, a + 1) v.set_block(1, b - 1) flags = v < np.ones(v.size) v.set_block(0, a - 1) v.set_block(1, b + 1) self.assertEqual(v.nblocks, flags.nblocks) for bid, blk in enumerate(flags): self.assertTrue(np.allclose(blk, v.get_block(bid))) v.set_block(0, a + 1) v.set_block(1, b - 1) vv = v.copy() vv.fill(1.0) flags = v < vv v.set_block(0, a - 1) v.set_block(1, b + 1) self.assertEqual(v.nblocks, flags.nblocks) for bid, blk in enumerate(flags): self.assertTrue(np.allclose(blk, v.get_block(bid)))
def test_compress(self): v = self.ones v = BlockVector(2) a = np.ones(5) b = np.zeros(9) v.set_block(0, a) v.set_block(1, b) c = v.compress(v < 1) v2 = BlockVector(2) b = np.zeros(9) v2.set_block(0, np.ones(0)) v2.set_block(1, b) self.assertEqual(c.nblocks, v.nblocks) for bid, blk in enumerate(c): self.assertTrue(np.allclose(blk, v2.get_block(bid))) flags = v < 1 c = v.compress(flags.flatten()) self.assertEqual(c.nblocks, v.nblocks) for bid, blk in enumerate(c): self.assertTrue(np.allclose(blk, v2.get_block(bid))) with self.assertRaises(Exception) as context: v.compress(1.0)
def __init__(self, A1, A2, c1, c2, N, dt): self._A1 = A1 self._A2 = A2 self._c1 = c1 self._c2 = c2 self._N = N self._dt = dt self._input_names = ['F1_{}'.format(t) for t in range(1, N)] self._input_names.extend(['F2_{}'.format(t) for t in range(1, N)]) self._input_names.extend(['h1_{}'.format(t) for t in range(0, N)]) self._input_names.extend(['h2_{}'.format(t) for t in range(0, N)]) self._output_names = ['F12_{}'.format(t) for t in range(0, N)] self._output_names.extend(['Fo_{}'.format(t) for t in range(0, N)]) self._equality_constraint_names = [ 'h1bal_{}'.format(t) for t in range(1, N) ] self._equality_constraint_names.extend( ['h2bal_{}'.format(t) for t in range(1, N)]) # inputs self._F1 = np.zeros(N) # we don't use the first one self._F2 = np.zeros(N) # we don't use the first one self._h1 = np.zeros(N) self._h2 = np.zeros(N) # outputs self._F12 = np.zeros(N) self._Fo = np.zeros(N) # multipliers self._eq_con_mult_values = np.ones(2 * (N - 1)) self._output_con_mult_values = np.ones(2 * N)
def test_set_blocks(self): v = self.ones blocks = [np.ones(s) * i for i, s in enumerate(self.list_sizes_ones)] v.set_blocks(blocks) for i, s in enumerate(self.list_sizes_ones): self.assertEqual(v.get_block(i).size, s) self.assertEqual(v.get_block(i).shape, (s, )) res = np.ones(s) * i self.assertListEqual(v.get_block(i).tolist(), res.tolist())
def test_has_none(self): v = self.ones self.assertFalse(v.has_none) v = BlockVector(3) v.set_block(0, np.ones(2)) v.set_block(2, np.ones(3)) self.assertTrue(v.has_none) v.set_block(1, np.ones(2)) self.assertFalse(v.has_none)
def test_reduce_ufuncs(self): v = BlockVector(2) a = np.ones(3) * 0.5 b = np.ones(2) * 0.8 v.set_block(0, a) v.set_block(1, b) reduce_funcs = [np.sum, np.max, np.min, np.prod, np.mean] for fun in reduce_funcs: self.assertAlmostEqual(fun(v), fun(v.flatten())) other_funcs = [np.all, np.any, np.std, np.ptp] for fun in other_funcs: self.assertAlmostEqual(fun(v), fun(v.flatten()))
def test_all(self): v = BlockVector(2) a = np.ones(5) b = np.ones(3) v.set_block(0, a) v.set_block(1, b) self.assertTrue(v.all()) v = BlockVector(2) a = np.zeros(5) b = np.zeros(3) v.set_block(0, a) v.set_block(1, b) self.assertFalse(v.all())
def test_matvec_1(self): rank = comm.Get_rank() rank_ownership = np.array([[0, -1, -1, 0], [-1, 1, -1, 1], [-1, -1, 2, 2], [0, 1, 2, -1]]) m = MPIBlockMatrix(4, 4, rank_ownership, comm) sub_m = np.array([[1, 0], [0, 1]]) sub_m = coo_matrix(sub_m) m.set_block(rank, rank, sub_m.copy()) m.set_block(rank, 3, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) m.set_block(3, 3, sub_m.copy()) rank_ownership = np.array([0, 1, 2, -1]) v = MPIBlockVector(4, rank_ownership, comm) sub_v = np.ones(2) v.set_block(rank, sub_v.copy()) v.set_block(3, sub_v.copy()) res = m.dot(v) self.assertIsInstance(res, MPIBlockVector) self.assertTrue(np.array_equal(res.get_block(rank), sub_v * 2)) self.assertTrue(np.array_equal(res.get_block(3), sub_v * 4)) self.assertTrue( np.array_equal(res.rank_ownership, np.array([0, 1, 2, -1]))) self.assertFalse(res.has_none)
def test_get_block_vector_for_dot_product_5(self): rank = comm.Get_rank() rank_ownership = np.array([[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]) m = MPIBlockMatrix(4, 3, rank_ownership, comm) sub_m = np.array([[1, 0], [0, 1]]) sub_m = coo_matrix(sub_m) if rank == 0: m.set_block(3, rank, sub_m.copy()) elif rank == 1: m.set_block(0, 0, sub_m.copy()) m.set_block(rank, rank, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) else: m.set_block(rank, rank, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) v = BlockVector(3) sub_v = np.ones(2) for ndx in range(3): v.set_block(ndx, sub_v.copy()) res = m._get_block_vector_for_dot_product(v) self.assertIs(res, v) v_flat = v.flatten() res = m._get_block_vector_for_dot_product(v_flat) self.assertIsInstance(res, BlockVector) for ndx in range(3): block = res.get_block(ndx) self.assertTrue(np.array_equal(block, sub_v))
def test_round(self): v = BlockVector(2) a = np.ones(5) * 1.1 b = np.ones(9) * 1.1 v.set_block(0, a) v.set_block(1, b) vv = v.round() self.assertEqual(vv.nblocks, v.nblocks) a = np.ones(5) b = np.ones(9) v.set_block(0, a) v.set_block(1, b) for bid, blk in enumerate(vv): self.assertTrue(np.allclose(blk, v.get_block(bid)))
def test_get_block_vector_for_dot_product_2(self): rank = comm.Get_rank() rank_ownership = np.array([[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]) m = MPIBlockMatrix(4, 3, rank_ownership, comm) sub_m = np.array([[1, 0], [0, 1]]) sub_m = coo_matrix(sub_m) if rank == 0: m.set_block(3, rank, sub_m.copy()) elif rank == 1: m.set_block(0, 0, sub_m.copy()) m.set_block(rank, rank, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) else: m.set_block(rank, rank, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) rank_ownership = np.array([-1, 1, 2]) v = MPIBlockVector(3, rank_ownership, comm) sub_v = np.ones(2) v.set_block(0, sub_v.copy()) if rank != 0: v.set_block(rank, sub_v.copy()) res = m._get_block_vector_for_dot_product(v) self.assertIs(res, v)
def test_jacobian_d(self): nlp = self.nlp x = nlp.create_vector_x() self.assertEqual(nlp.jacobian_d(x).shape, (0, 3)) x = self.nlp3.create_vector_x() x.fill(1.0) jac = self.nlp3.jacobian_d(x) self.assertEqual(3, jac.shape[1]) self.assertEqual(3, jac.shape[0]) values = np.ones(7) self.assertTrue(np.allclose(values, jac.data)) new_jac = self.nlp3.jacobian_d(x) new_jac.data.fill(0.0) self.nlp3.jacobian_d(x, out=new_jac) self.assertTrue(np.allclose(values, new_jac.data)) jac_g = self.nlp3.jacobian_g(x) new_jac = self.nlp3.jacobian_d(x, evaluated_jac_g=jac_g) self.assertTrue(np.allclose(values, new_jac.data)) new_jac = self.nlp3.jacobian_d(x) new_jac.data.fill(0.0) self.nlp3.jacobian_d(x, out=new_jac, evaluated_jac_g=jac_g) self.assertTrue(np.allclose(values, new_jac.data))
def test_get_block_vector_for_dot_product_3(self): rank = comm.Get_rank() rank_ownership = np.array([[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]) m = MPIBlockMatrix(4, 3, rank_ownership, comm) sub_m = np.array([[1, 0], [0, 1]]) sub_m = coo_matrix(sub_m) if rank == 0: m.set_block(3, rank, sub_m.copy()) elif rank == 1: m.set_block(0, 0, sub_m.copy()) m.set_block(rank, rank, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) else: m.set_block(rank, rank, sub_m.copy()) m.set_block(3, rank, sub_m.copy()) rank_ownership = np.array([0, 1, 2]) v = MPIBlockVector(3, rank_ownership, comm) sub_v = np.ones(2) v.set_block(rank, sub_v.copy()) res = m._get_block_vector_for_dot_product(v) self.assertIsNot(res, v) self.assertTrue(np.array_equal(res.get_block(0), sub_v)) if rank == 0: self.assertIsNone(res.get_block(1)) self.assertIsNone(res.get_block(2)) elif rank == 1: self.assertTrue(np.array_equal(res.get_block(1), sub_v)) self.assertIsNone(res.get_block(2)) elif rank == 2: self.assertTrue(np.array_equal(res.get_block(2), sub_v)) self.assertIsNone(res.get_block(1))
def test_eval_d(self): x = np.ones(self.nlp3.nx) res = np.array([2.0, 2.0, 3.0]) self.assertTrue(np.allclose(self.nlp3.evaluate_d(x), res)) res_ = self.nlp3.create_vector_y(subset='d') self.nlp3.evaluate_d(x, out=res_) self.assertTrue(np.allclose(res_, res)) x = np.ones(self.nlp3.nx) res = np.array([2.0, 2.0, 3.0]) g = self.nlp3.evaluate_g(x) res_eval = self.nlp3.evaluate_d(x, evaluated_g=g) self.assertTrue(np.allclose(res_eval, res)) res_ = self.nlp3.create_vector_y(subset='d') self.nlp3.evaluate_d(x, out=res_, evaluated_g=g) self.assertTrue(np.allclose(res_, res))
def test_eval_g(self): x = np.ones(self.nlp3.nx) res = np.array([-1.0, -0.5, 2, 2, 3]) self.assertTrue(np.allclose(self.nlp3.evaluate_g(x), res)) res_ = self.nlp3.create_vector_y() self.nlp3.evaluate_g(x, out=res_) self.assertTrue(np.allclose(res_, res))
def test_copyto(self): bm0 = self.basic_m.copy() coo = bm0.tocoo() coo.data.fill(1.0) csr = coo.tocsr() csc = coo.tocsc() self.assertFalse(np.allclose(coo.toarray(), self.dense)) self.assertFalse(np.allclose(csr.toarray(), self.dense)) self.assertFalse(np.allclose(csc.toarray(), self.dense)) bm0.copyto(coo) bm0.copyto(csr) bm0.copyto(csc) self.assertTrue(np.allclose(coo.toarray(), self.dense)) self.assertTrue(np.allclose(csr.toarray(), self.dense)) self.assertTrue(np.allclose(csc.toarray(), self.dense)) flat = np.ones((8, 8)) bm0.copyto(flat) self.assertTrue(np.allclose(flat, self.dense)) bm = bm0.copy_structure() bm0.copyto(bm) self.assertTrue(np.allclose(bm.toarray(), self.dense)) bm.get_block(0, 0).data.fill(1.0) self.assertAlmostEqual(bm0.toarray()[0, 0], 2) # this tests that a deep copy was done self.assertAlmostEqual(bm.toarray()[0, 0], 1) bm0.copyto(bm, deep=False) bm.get_block(0, 0).data.fill(1.0) self.assertAlmostEqual(bm0.toarray()[0, 0], 1) # this tests that a shallow copy was done self.assertAlmostEqual(bm.toarray()[0, 0], 1)
def test_mean(self): flat_v = np.ones(self.ones.size) v = self.ones self.assertEqual(v.mean(), flat_v.mean()) v = BlockVector(2) with self.assertRaises(NotFullyDefinedBlockVectorError): v_mean = v.mean()
def test_constructor(self): v = BlockVector(2) self.assertEqual(v.nblocks, 2) self.assertEqual(v.bshape, (2, )) with self.assertRaises(NotFullyDefinedBlockVectorError): v_size = v.size v.set_block(0, np.ones(2)) v.set_block(1, np.ones(4)) self.assertEqual(v.size, 6) self.assertEqual(v.shape, (6, )) with self.assertRaises(AssertionError): v.set_block(0, None) with self.assertRaises(Exception) as context: BlockVector('hola')
def test_copy(self): v = BlockVector(2) a = np.ones(5) b = np.zeros(9) v.set_block(0, a) v.set_block(1, b) v2 = v.copy() self.assertTrue(np.allclose(v.flatten(), v2.flatten()))
def test_multiply(self): # check scalar multiplication block = self.block_m m = self.basic_m * 5.0 scipy_mat = bmat([[block, block], [None, block]], format='coo') mulscipy_mat = scipy_mat * 5.0 dinopy_mat = m.tocoo() drow = np.sort(dinopy_mat.row) dcol = np.sort(dinopy_mat.col) ddata = np.sort(dinopy_mat.data) srow = np.sort(mulscipy_mat.row) scol = np.sort(mulscipy_mat.col) sdata = np.sort(mulscipy_mat.data) self.assertListEqual(drow.tolist(), srow.tolist()) self.assertListEqual(dcol.tolist(), scol.tolist()) self.assertListEqual(ddata.tolist(), sdata.tolist()) m = 5.0 * self.basic_m dinopy_mat = m.tocoo() drow = np.sort(dinopy_mat.row) dcol = np.sort(dinopy_mat.col) ddata = np.sort(dinopy_mat.data) self.assertListEqual(drow.tolist(), srow.tolist()) self.assertListEqual(dcol.tolist(), scol.tolist()) self.assertListEqual(ddata.tolist(), sdata.tolist()) # check dot product with block vector block = self.block_m m = self.basic_m scipy_mat = bmat([[block, block], [None, block]], format='coo') x = BlockVector(2) x.set_block(0, np.ones(block.shape[1], dtype=np.float64)) x.set_block(1, np.ones(block.shape[1], dtype=np.float64)) res_scipy = scipy_mat.dot(x.flatten()) res_dinopy = m * x res_dinopy_flat = m * x.flatten() self.assertListEqual(res_dinopy.tolist(), res_scipy.tolist()) self.assertListEqual(res_dinopy_flat.tolist(), res_scipy.tolist()) dense_mat = dinopy_mat.toarray() self.basic_m *= 5.0 self.assertTrue(np.allclose(dense_mat, self.basic_m.toarray()))
def test_fill(self): v = BlockVector(2) a = np.arange(5) b = np.arange(9) v.set_block(0, a) v.set_block(1, b) v.fill(1.0) c = np.ones(v.size) self.assertListEqual(v.tolist(), c.tolist())
def test_copy_structure(self): v = BlockVector(2) a = np.ones(5) b = np.zeros(9) v.set_block(0, a) v.set_block(1, b) v2 = v.copy_structure() self.assertEqual(v.get_block(0).size, v2.get_block(0).size) self.assertEqual(v.get_block(1).size, v2.get_block(1).size)
def test_itruediv(self): v = self.ones v /= 3 self.assertTrue(np.allclose(v.flatten(), np.ones(v.size) / 3)) v.fill(1.0) v /= v self.assertTrue(np.allclose(v.flatten(), np.ones(v.size))) v.fill(1.0) v /= np.ones(v.size) * 2 self.assertTrue(np.allclose(v.flatten(), np.ones(v.size) / 2)) v = BlockVector(2) a = np.ones(5) b = np.arange(9, dtype=np.float64) a_copy = a.copy() b_copy = b.copy() v.set_block(0, a) v.set_block(1, b) v /= 2.0 self.assertTrue(np.allclose(v.get_block(0), a_copy / 2.0)) self.assertTrue(np.allclose(v.get_block(1), b_copy / 2.0)) v = BlockVector(2) a = np.ones(5) b = np.zeros(9) a_copy = a.copy() b_copy = b.copy() v.set_block(0, a) v.set_block(1, b) v2 = BlockVector(2) v2.set_block(0, np.ones(5) * 2) v2.set_block(1, np.ones(9) * 2) v /= v2 self.assertTrue(np.allclose(v.get_block(0), a_copy / 2)) self.assertTrue(np.allclose(v.get_block(1), b_copy / 2)) self.assertTrue(np.allclose(v2.get_block(0), np.ones(5) * 2)) self.assertTrue(np.allclose(v2.get_block(1), np.ones(9) * 2)) with self.assertRaises(Exception) as context: v *= 'hola'
def test_contains(self): v = BlockVector(2) a = np.ones(5) b = np.zeros(9) v.set_block(0, a) v.set_block(1, b) self.assertTrue(0 in v) self.assertFalse(3 in v)
def test_clip(self): v = BlockVector(3) v2 = BlockVector(3) a = np.zeros(5) b = np.ones(3) * 5.0 c = np.ones(3) * 10.0 v.set_block(0, a) v.set_block(1, b) v.set_block(2, c) v2.set_block(0, np.ones(5) * 4.0) v2.set_block(1, np.ones(3) * 5.0) v2.set_block(2, np.ones(3) * 9.0) vv = v.clip(4.0, 9.0) self.assertEqual(vv.nblocks, v.nblocks) for bid, blk in enumerate(vv): self.assertTrue(np.allclose(blk, v2.get_block(bid)))
def test_clone(self): v = self.ones w = v.clone() self.assertListEqual(w.tolist(), v.tolist()) x = v.clone(4) self.assertListEqual(x.tolist(), [4] * v.size) y = x.clone(copy=False) y.get_block(2)[-1] = 6 d = np.ones(y.size) * 4 d[-1] = 6 self.assertListEqual(y.tolist(), d.tolist()) self.assertListEqual(x.tolist(), d.tolist())
def test_interface(self): # weird, this is really a test of the test class above # but we could add code later, so... iom = PressureDropModel() iom.set_inputs(np.ones(4)) o = iom.evaluate_outputs() expected_o = np.asarray([0.0, -1.0], dtype=np.float64) self.assertTrue(np.array_equal(o, expected_o)) jac = iom.evaluate_derivatives() expected_jac = np.asarray([[1, -1, 0, -2], [1, -1, -1, -4]], dtype=np.float64) self.assertTrue(np.array_equal(jac.todense(), expected_jac))
def test_dot(self): A_dense = self.basic_m.toarray() A_block = self.basic_m x = np.ones(A_dense.shape[1]) block_x = BlockVector(2) block_x.set_block(0, np.ones(self.block_m.shape[1])) block_x.set_block(1, np.ones(self.block_m.shape[1])) flat_res = A_block.dot(x).flatten() block_res = A_block.dot(block_x) self.assertTrue(np.allclose(A_dense.dot(x), flat_res)) self.assertTrue(np.allclose(A_dense.dot(x), block_res.flatten())) self.assertEqual(block_res.bshape[0], 2) m = BlockMatrix(2, 2) sub_m = np.array([[1, 0], [0, 1]]) sub_m = coo_matrix(sub_m) m.set_block(0, 1, sub_m.copy()) m.set_block(1, 0, sub_m.copy()) x = np.arange(4) res = m * x self.assertTrue(np.allclose(res.flatten(), np.array([2, 3, 0, 1])))
def test_unary_ufuncs(self): v = BlockVector(2) a = np.ones(3) * 0.5 b = np.ones(2) * 0.8 v.set_block(0, a) v.set_block(1, b) v2 = BlockVector(2) unary_funcs = [ np.log10, np.sin, np.cos, np.exp, np.ceil, np.floor, np.tan, np.arctan, np.arcsin, np.arccos, np.sinh, np.cosh, np.abs, np.tanh, np.arcsinh, np.arctanh, np.fabs, np.sqrt, np.log, np.log2, np.absolute, np.isfinite, np.isinf, np.isnan, np.log1p, np.logical_not, np.exp2, np.expm1, np.sign, np.rint, np.square, np.positive, np.negative, np.rad2deg, np.deg2rad, np.conjugate, np.reciprocal ] for fun in unary_funcs: v2.set_block(0, fun(v.get_block(0))) v2.set_block(1, fun(v.get_block(1))) res = fun(v) self.assertIsInstance(res, BlockVector) self.assertEqual(res.nblocks, 2) for i in range(2): self.assertTrue(np.allclose(res.get_block(i), v2.get_block(i))) other_funcs = [np.cumsum, np.cumprod, np.cumproduct] for fun in other_funcs: res = fun(v) self.assertIsInstance(res, BlockVector) self.assertEqual(res.nblocks, 2) self.assertTrue(np.allclose(fun(v.flatten()), res.flatten())) with self.assertRaises(Exception) as context: np.cbrt(v)