1from petsc4py import PETSc 2import unittest 3 4import numpy as N 5import numpy as np 6 7def mkgraph(comm, m, n): 8 start = m*n * comm.rank 9 end = start + m*n 10 idt = PETSc.IntType 11 rows = [] 12 for I in range(start, end) : 13 rows.append([]) 14 adj = rows[-1] 15 i = I//n; j = I - i*n 16 if i> 0 : J = I-n; adj.append(J) 17 if j> 0 : J = I-1; adj.append(J) 18 adj.append(I) 19 if j< n-1: J = I+1; adj.append(J) 20 if i< m-1: J = I+n; adj.append(J) 21 nods = N.array(range(start, end), dtype=idt) 22 xadj = N.array([0]*(len(rows)+1), dtype=idt) 23 xadj[0] = 0 24 xadj[1:] = N.cumsum([len(r) for r in rows], dtype=idt) 25 if not rows: adjy = N.array([],dtype=idt) 26 else: adjy = N.concatenate(rows).astype(idt) 27 return nods, xadj, adjy 28 29 30class BaseTestMatAnyAIJ(object): 31 32 COMM = PETSc.COMM_NULL 33 TYPE = None 34 GRID = 0, 0 35 BSIZE = None 36 37 def setUp(self): 38 COMM = self.COMM 39 GM, GN = self.GRID 40 BS = self.BSIZE 41 # 42 try: 43 rbs, cbs = BS 44 rbs = rbs or 1 45 cbs = cbs or 1 46 except (TypeError, ValueError): 47 rbs = cbs = BS or 1 48 sdt = dtype = PETSc.ScalarType 49 self.rows, self.xadj, self.adjy = mkgraph(COMM, GM, GN) 50 self.vals = N.array(range(1, 1 + len(self.adjy)*rbs*cbs), dtype=sdt) 51 self.vals.shape = (-1, rbs, cbs) 52 # 53 m, n = GM, GN 54 rowsz = (m*n*rbs, None) 55 colsz = (m*n*cbs, None) 56 A = self.A = PETSc.Mat().create(comm=COMM) 57 A.setType(self.TYPE) 58 A.setSizes([rowsz, colsz], BS) 59 60 def tearDown(self): 61 self.A.destroy() 62 self.A = None 63 PETSc.garbage_cleanup() 64 65 def testSetPreallocNNZ(self): 66 nnz = [5, 2] 67 self.A.setPreallocationNNZ(nnz) 68 self._chk_bs(self.A, self.BSIZE) 69 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 70 self.A.setOption(opt, True) 71 ai, aj, av = self._set_values() 72 self.A.assemble() 73 self._chk_aij(self.A, ai, aj) 74 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 75 self.A.setOption(opt, True) 76 ai, aj, av = self._set_values_ijv() 77 self.A.assemble() 78 self._chk_aij(self.A, ai, aj) 79 80 def testSetPreallocNNZ_2(self): 81 _, ai, _, _ =self._get_aijv() 82 d_nnz = N.diff(ai) 83 nnz = [d_nnz, 3] 84 self.A.setPreallocationNNZ(nnz) 85 self._chk_bs(self.A, self.BSIZE) 86 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 87 self.A.setOption(opt, True) 88 ai, aj, av = self._set_values() 89 self.A.assemble() 90 self._chk_aij(self.A, ai, aj) 91 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 92 self.A.setOption(opt, True) 93 ai, aj, av =self._set_values_ijv() 94 self.A.assemble() 95 self._chk_aij(self.A, ai, aj) 96 97 def testSetPreallocCSR(self): 98 if 'is' in self.A.getType(): return # XXX 99 _, ai, aj, _ = self._get_aijv() 100 csr = [ai, aj] 101 self.A.setPreallocationCSR(csr) 102 self._chk_bs(self.A, self.BSIZE) 103 self._chk_aij(self.A, ai, aj) 104 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 105 self.A.setOption(opt, True) 106 self._set_values() 107 self.A.assemble() 108 self._chk_aij(self.A, ai, aj) 109 self._set_values_ijv() 110 self.A.assemble() 111 self._chk_aij(self.A, ai, aj) 112 113 def testSetPreallocCSR_2(self): 114 if 'is' in self.A.getType(): return # XXX 115 _, ai, aj, av =self._get_aijv() 116 csr = [ai, aj, av] 117 self.A.setPreallocationCSR(csr) 118 self._chk_bs(self.A, self.BSIZE) 119 self._chk_aij(self.A, ai, aj) 120 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 121 self.A.setOption(opt, True) 122 self._set_values() 123 self.A.assemble() 124 self._chk_aij(self.A, ai, aj) 125 self._set_values_ijv() 126 self.A.assemble() 127 self._chk_aij(self.A, ai, aj) 128 129 def testSetValues(self): 130 self._preallocate() 131 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 132 self.A.setOption(opt, True) 133 ai, aj, av = self._set_values() 134 self.A.assemble() 135 self._chk_aij(self.A, ai, aj) 136 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 137 self.A.setOption(opt, True) 138 ai, aj, av = self._set_values() 139 self.A.assemble() 140 self._chk_aij(self.A, ai, aj) 141 142 def testSetValuesIJV(self): 143 self._preallocate() 144 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 145 self.A.setOption(opt, True) 146 ai, aj, av = self._set_values_ijv() 147 self.A.assemble() 148 self._chk_aij(self.A, ai, aj) 149 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 150 self.A.setOption(opt, True) 151 ai, aj, av = self._set_values_ijv() 152 self.A.assemble() 153 self._chk_aij(self.A, ai, aj) 154 155 def testGetValuesCSR(self): 156 if 'is' in self.A.getType(): return # XXX 157 self._preallocate() 158 self._set_values_ijv() 159 A = self.A 160 A.assemble() 161 if 'sbaij' in A.getType(): 162 opt = PETSc.Mat.Option.GETROW_UPPERTRIANGULAR 163 self.A.setOption(opt, True) 164 ai, aj, av = A.getValuesCSR() 165 rstart, rend = A.getOwnershipRange() 166 for row in range(rstart, rend): 167 cols, vals = A.getRow(row) 168 i = row - rstart 169 self.assertTrue(N.allclose(aj[ai[i]:ai[i+1]], cols)) 170 self.assertTrue(N.allclose(av[ai[i]:ai[i+1]], vals)) 171 172 def testConvertToSAME(self): 173 self._preallocate() 174 self._set_values_ijv() 175 A = self.A 176 A.assemble() 177 A.convert('same') 178 179 def testConvertToDENSE(self): 180 self._preallocate() 181 self._set_values_ijv() 182 A = self.A 183 A.assemble() 184 x, y = A.getVecs() 185 x.setRandom() 186 z = y.duplicate() 187 A.mult(x, y) 188 if A.type.endswith('sbaij'): return 189 B = PETSc.Mat() 190 A.convert('dense', B) # initial 191 B.mult(x, z) 192 self.assertTrue(np.allclose(y.array, z.array)) 193 A.convert('dense', B) # reuse 194 B.mult(x, z) 195 self.assertTrue(np.allclose(y.array, z.array)) 196 A.convert('dense') # inplace 197 A.mult(x, z) 198 self.assertTrue(np.allclose(y.array, z.array)) 199 200 def testConvertToAIJ(self): 201 self._preallocate() 202 self._set_values_ijv() 203 A = self.A 204 A.assemble() 205 x, y = A.getVecs() 206 x.setRandom() 207 z = y.duplicate() 208 A.mult(x, y) 209 if A.type.endswith('sbaij'): return 210 B = PETSc.Mat() 211 A.convert('aij', B) # initial 212 B.mult(x, z) 213 self.assertTrue(np.allclose(y.array, z.array)) 214 A.convert('aij', B) # reuse 215 B.mult(x, z) 216 self.assertTrue(np.allclose(y.array, z.array)) 217 A.convert('aij') # inplace 218 A.mult(x, z) 219 self.assertTrue(np.allclose(y.array, z.array)) 220 221 def testGetDiagonalBlock(self): 222 if 'is' in self.A.getType(): return # XXX 223 self._preallocate() 224 self._set_values_ijv() 225 self.A.assemble() 226 B = self.A.getDiagonalBlock() 227 self.assertEqual(self.A.getLocalSize(), B.getSize()) 228 B.destroy() 229 230 def testInvertBlockDiagonal(self): 231 if 'is' in self.A.getType(): return # XXX 232 try: 233 _ = len(self.BSIZE) 234 return 235 except (TypeError, ValueError): 236 pass 237 self._preallocate() 238 rbs, cbs = self.A.getBlockSizes() 239 if rbs != cbs: return 240 self._set_values_ijv() 241 self.A.assemble() 242 self.A.shift(1000) # Make nonsingular 243 ibdiag = self.A.invertBlockDiagonal() 244 bs = self.A.getBlockSize() 245 m, _ = self.A.getLocalSize() 246 self.assertEqual(ibdiag.shape, (m//bs, bs, bs)) 247 tmp = N.empty((m//bs, bs, bs), dtype=PETSc.ScalarType) 248 rstart, rend = self.A.getOwnershipRange() 249 s, e = rstart//bs, rend//bs 250 for i in range(s, e): 251 rows = cols = N.arange(i*bs,(i+1)*bs, dtype=PETSc.IntType) 252 vals = self.A.getValues(rows,cols) 253 tmp[i-s,:,:] = N.linalg.inv(vals) 254 self.assertTrue(N.allclose(ibdiag, tmp)) 255 256 def testCreateSubMatrix(self): 257 if 'baij' in self.A.getType(): return # XXX 258 self._preallocate() 259 self._set_values_ijv() 260 self.A.assemble() 261 # 262 rank = self.A.getComm().getRank() 263 rs, re = self.A.getOwnershipRange() 264 cs, ce = self.A.getOwnershipRangeColumn() 265 rows = N.array(range(rs, re), dtype=PETSc.IntType) 266 cols = N.array(range(cs, ce), dtype=PETSc.IntType) 267 rows = PETSc.IS().createGeneral(rows, comm=self.A.getComm()) 268 cols = PETSc.IS().createGeneral(cols, comm=self.A.getComm()) 269 # 270 S = self.A.createSubMatrix(rows, None) 271 S.zeroEntries() 272 self.A.createSubMatrix(rows, None, S) 273 S.destroy() 274 # 275 S = self.A.createSubMatrix(rows, cols) 276 S.zeroEntries() 277 self.A.createSubMatrix(rows, cols, S) 278 S.destroy() 279 280 def testCreateSubMatrices(self): 281 if 'baij' in self.A.getType(): return # XXX 282 if 'is' in self.A.getType(): return # XXX 283 self._preallocate() 284 self._set_values_ijv() 285 self.A.assemble() 286 # 287 rs, re = self.A.getOwnershipRange() 288 cs, ce = self.A.getOwnershipRangeColumn() 289 rows = N.array(range(rs, re), dtype=PETSc.IntType) 290 cols = N.array(range(cs, ce), dtype=PETSc.IntType) 291 rows = PETSc.IS().createGeneral(rows, comm=self.A.getComm()) 292 cols = PETSc.IS().createGeneral(cols, comm=self.A.getComm()) 293 # 294 (S,) = self.A.createSubMatrices(rows, cols) 295 S.zeroEntries() 296 self.A.createSubMatrices(rows, cols, submats=[S]) 297 S.destroy() 298 # 299 (S1,) = self.A.createSubMatrices([rows], [cols]) 300 (S2,) = self.A.createSubMatrices([rows], [cols]) 301 self.assertTrue(S1.equal(S2)) 302 S2.zeroEntries() 303 self.A.createSubMatrices([rows], [cols], [S2]) 304 self.assertTrue(S1.equal(S2)) 305 S1.destroy() 306 S2.destroy() 307 # 308 if 'seq' not in self.A.getType(): return # XXX 309 S1, S2 = self.A.createSubMatrices([rows, rows], [cols, cols]) 310 self.assertTrue(S1.equal(S2)) 311 S1.zeroEntries() 312 S2.zeroEntries() 313 self.A.createSubMatrices([rows, rows], [cols, cols], [S1, S2]) 314 self.assertTrue(S1.equal(S2)) 315 S1.destroy() 316 S2.destroy() 317 318 def testGetRedundantMatrix(self): 319 if 'aijcrl' in self.A.getType(): return # duplicate not supported 320 if 'mpisbaij' in self.A.getType(): return # not working 321 if 'is' in self.A.getType(): return # XXX 322 self._preallocate() 323 self._set_values_ijv() 324 self.A.assemble() 325 #Test the most simple case 326 sizecommA = self.A.getComm().getSize() 327 Ared = self.A.getRedundantMatrix(sizecommA) 328 sizecommAred = Ared.getComm().getSize() 329 self.assertEqual(1, sizecommAred) 330 Ared.destroy() 331 332 def testCreateTranspose(self): 333 self._preallocate() 334 self._set_values_ijv() 335 self.A.assemble() 336 A = self.A 337 AT = PETSc.Mat().createTranspose(A) 338 x, y = A.createVecs() 339 xt, yt = AT.createVecs() 340 # 341 y.setRandom() 342 A.multTranspose(y, x) 343 y.copy(xt) 344 AT.mult(xt, yt) 345 self.assertTrue(yt.equal(x)) 346 # 347 x.setRandom() 348 A.mult(x, y) 349 x.copy(yt) 350 AT.multTranspose(yt, xt) 351 self.assertTrue(xt.equal(y)) 352 353 def _get_aijv(self): 354 return (self.rows, self.xadj, self.adjy, self.vals,) 355 356 def _preallocate(self): 357 self.A.setPreallocationNNZ([5, 2]) 358 359 def _set_values(self): 360 import sys 361 if hasattr(sys, 'gettotalrefcount'): 362 return self._set_values_ijv() 363 # XXX Why the code below leak refs as a beast ??? 364 row, ai, aj, av =self._get_aijv() 365 if not self.BSIZE: 366 setvalues = self.A.setValues 367 else: 368 setvalues = self.A.setValuesBlocked 369 for i, r in enumerate(row): 370 s, e = ai[i], ai[i+1] 371 setvalues(r, aj[s:e], av[s:e]) 372 return ai, aj, av 373 374 def _set_values_ijv(self): 375 row, ai, aj, av =self._get_aijv() 376 if not self.BSIZE: 377 setvalues = self.A.setValuesIJV 378 else: 379 setvalues = self.A.setValuesBlockedIJV 380 setvalues(ai, aj, av, rowmap=row) 381 setvalues(ai, aj, av, rowmap=None) 382 return ai, aj, av 383 384 def _chk_bs(self, A, bs): 385 self.assertEqual(A.getBlockSize(), bs or 1) 386 387 def _chk_bsizes(self, A, bsizes): 388 try: 389 rbs, cbs = bsizes 390 except (TypeError, ValueError): 391 rbs = cbs = bsizes 392 self.assertEqual(A.getBlockSizes(), (rbs, cbs)) 393 394 def _chk_aij(self, A, i, j): 395 compressed = bool(self.BSIZE) 396 ai, aj = A.getRowIJ(compressed=compressed) 397 if ai is not None and aj is not None: 398 self.assertTrue(N.all(i==ai)) 399 self.assertTrue(N.all(j==aj)) 400 ai, aj = A.getColumnIJ(compressed=compressed) 401 if ai is not None and aj is not None: 402 self.assertTrue(N.all(i==ai)) 403 self.assertTrue(N.all(j==aj)) 404 405# -- AIJ --------------------- 406 407class BaseTestMatAIJ(BaseTestMatAnyAIJ, unittest.TestCase): 408 COMM = PETSc.COMM_WORLD 409 TYPE = PETSc.Mat.Type.AIJ 410 GRID = 0, 0 411 BSIZE = None 412 413# -- Seq AIJ -- 414 415class TestMatSeqAIJ(BaseTestMatAIJ): 416 COMM = PETSc.COMM_SELF 417 TYPE = PETSc.Mat.Type.SEQAIJ 418class TestMatSeqAIJ_G23(TestMatSeqAIJ): 419 GRID = 2, 3 420class TestMatSeqAIJ_G45(TestMatSeqAIJ): 421 GRID = 4, 5 422class TestMatSeqAIJ_G89(TestMatSeqAIJ): 423 GRID = 8, 9 424 425# -- MPI AIJ -- 426 427class TestMatMPIAIJ(BaseTestMatAIJ): 428 COMM = PETSc.COMM_WORLD 429 TYPE = PETSc.Mat.Type.MPIAIJ 430class TestMatMPIAIJ_G23(TestMatMPIAIJ): 431 GRID = 2, 3 432class TestMatMPIAIJ_G45(TestMatMPIAIJ): 433 GRID = 4, 5 434class TestMatMPIAIJ_G89(TestMatMPIAIJ): 435 GRID = 8, 9 436 437 438# -- Block AIJ --------------- 439 440class BaseTestMatBAIJ(BaseTestMatAnyAIJ, unittest.TestCase): 441 COMM = PETSc.COMM_WORLD 442 TYPE = PETSc.Mat.Type.BAIJ 443 GRID = 0, 0 444 BSIZE = 1 445 446# -- Seq Block AIJ -- 447 448class TestMatSeqBAIJ(BaseTestMatBAIJ): 449 COMM = PETSc.COMM_SELF 450 TYPE = PETSc.Mat.Type.SEQBAIJ 451# bs = 1 452class TestMatSeqBAIJ_G23(TestMatSeqBAIJ): 453 GRID = 2, 3 454class TestMatSeqBAIJ_G45(TestMatSeqBAIJ): 455 GRID = 4, 5 456class TestMatSeqBAIJ_G89(TestMatSeqBAIJ): 457 GRID = 8, 9 458# bs = 2 459class TestMatSeqBAIJ_G23_B2(TestMatSeqBAIJ_G23): 460 BSIZE = 2 461class TestMatSeqBAIJ_G45_B2(TestMatSeqBAIJ_G45): 462 BSIZE = 2 463class TestMatSeqBAIJ_G89_B2(TestMatSeqBAIJ_G89): 464 BSIZE = 2 465# bs = 3 466class TestMatSeqBAIJ_G23_B3(TestMatSeqBAIJ_G23): 467 BSIZE = 3 468class TestMatSeqBAIJ_G45_B3(TestMatSeqBAIJ_G45): 469 BSIZE = 3 470class TestMatSeqBAIJ_G89_B3(TestMatSeqBAIJ_G89): 471 BSIZE = 3 472# bs = 4 473class TestMatSeqBAIJ_G23_B4(TestMatSeqBAIJ_G23): 474 BSIZE = 4 475class TestMatSeqBAIJ_G45_B4(TestMatSeqBAIJ_G45): 476 BSIZE = 4 477class TestMatSeqBAIJ_G89_B4(TestMatSeqBAIJ_G89): 478 BSIZE = 4 479# bs = 5 480class TestMatSeqBAIJ_G23_B5(TestMatSeqBAIJ_G23): 481 BSIZE = 5 482class TestMatSeqBAIJ_G45_B5(TestMatSeqBAIJ_G45): 483 BSIZE = 5 484class TestMatSeqBAIJ_G89_B5(TestMatSeqBAIJ_G89): 485 BSIZE = 5 486 487 488# -- MPI Block AIJ -- 489 490class TestMatMPIBAIJ(BaseTestMatBAIJ): 491 COMM = PETSc.COMM_WORLD 492 TYPE = PETSc.Mat.Type.MPIBAIJ 493# bs = 1 494class TestMatMPIBAIJ_G23(TestMatMPIBAIJ): 495 GRID = 2, 3 496class TestMatMPIBAIJ_G45(TestMatMPIBAIJ): 497 GRID = 4, 5 498class TestMatMPIBAIJ_G89(TestMatMPIBAIJ): 499 GRID = 8, 9 500# bs = 2 501class TestMatMPIBAIJ_G23_B2(TestMatMPIBAIJ_G23): 502 BSIZE = 2 503class TestMatMPIBAIJ_G45_B2(TestMatMPIBAIJ_G45): 504 BSIZE = 2 505class TestMatMPIBAIJ_G89_B2(TestMatMPIBAIJ_G89): 506 BSIZE = 2 507# bs = 3 508class TestMatMPIBAIJ_G23_B3(TestMatMPIBAIJ_G23): 509 BSIZE = 3 510class TestMatMPIBAIJ_G45_B3(TestMatMPIBAIJ_G45): 511 BSIZE = 3 512class TestMatMPIBAIJ_G89_B3(TestMatMPIBAIJ_G89): 513 BSIZE = 3 514# bs = 4 515class TestMatMPIBAIJ_G23_B4(TestMatMPIBAIJ_G23): 516 BSIZE = 4 517class TestMatMPIBAIJ_G45_B4(TestMatMPIBAIJ_G45): 518 BSIZE = 4 519class TestMatMPIBAIJ_G89_B4(TestMatMPIBAIJ_G89): 520 BSIZE = 4 521# bs = 5 522class TestMatMPIBAIJ_G23_B5(TestMatMPIBAIJ_G23): 523 BSIZE = 5 524class TestMatMPIBAIJ_G45_B5(TestMatMPIBAIJ_G45): 525 BSIZE = 5 526class TestMatMPIBAIJ_G89_B5(TestMatMPIBAIJ_G89): 527 BSIZE = 5 528 529# -- SymmBlock AIJ --------------- 530 531class BaseTestMatSBAIJ(BaseTestMatAnyAIJ, unittest.TestCase): 532 COMM = PETSc.COMM_WORLD 533 TYPE = PETSc.Mat.Type.SBAIJ 534 GRID = 0, 0 535 BSIZE = 1 536 def testInvertBlockDiagonal(self): pass 537 def _chk_aij(self, A, i, j): 538 ai, aj = A.getRowIJ(compressed=True) 539 if ai is not None and aj is not None: 540 if 0: # XXX Implement 541 self.assertTrue(N.all(i==ai)) 542 self.assertTrue(N.all(j==aj)) 543 ai, aj = A.getColumnIJ(compressed=True) 544 if ai is not None and aj is not None: 545 if 0: # XXX Implement 546 self.assertTrue(N.all(i==ai)) 547 self.assertTrue(N.all(j==aj)) 548 549# -- Seq SymmBlock AIJ -- 550 551class TestMatSeqSBAIJ(BaseTestMatSBAIJ): 552 COMM = PETSc.COMM_SELF 553 TYPE = PETSc.Mat.Type.SEQSBAIJ 554# bs = 1 555class TestMatSeqSBAIJ_G23(TestMatSeqSBAIJ): 556 GRID = 2, 3 557class TestMatSeqSBAIJ_G45(TestMatSeqSBAIJ): 558 GRID = 4, 5 559class TestMatSeqSBAIJ_G89(TestMatSeqSBAIJ): 560 GRID = 8, 9 561# bs = 2 562class TestMatSeqSBAIJ_G23_B2(TestMatSeqSBAIJ_G23): 563 BSIZE = 2 564class TestMatSeqSBAIJ_G45_B2(TestMatSeqSBAIJ_G45): 565 BSIZE = 2 566class TestMatSeqSBAIJ_G89_B2(TestMatSeqSBAIJ_G89): 567 BSIZE = 2 568# bs = 3 569class TestMatSeqSBAIJ_G23_B3(TestMatSeqSBAIJ_G23): 570 BSIZE = 3 571class TestMatSeqSBAIJ_G45_B3(TestMatSeqSBAIJ_G45): 572 BSIZE = 3 573class TestMatSeqSBAIJ_G89_B3(TestMatSeqSBAIJ_G89): 574 BSIZE = 3 575# bs = 4 576class TestMatSeqSBAIJ_G23_B4(TestMatSeqSBAIJ_G23): 577 BSIZE = 4 578class TestMatSeqSBAIJ_G45_B4(TestMatSeqSBAIJ_G45): 579 BSIZE = 4 580class TestMatSeqSBAIJ_G89_B4(TestMatSeqSBAIJ_G89): 581 BSIZE = 4 582# bs = 5 583class TestMatSeqSBAIJ_G23_B5(TestMatSeqSBAIJ_G23): 584 BSIZE = 5 585class TestMatSeqSBAIJ_G45_B5(TestMatSeqSBAIJ_G45): 586 BSIZE = 5 587class TestMatSeqSBAIJ_G89_B5(TestMatSeqSBAIJ_G89): 588 BSIZE = 5 589 590 591# -- MPI SymmBlock AIJ -- 592 593class TestMatMPISBAIJ(BaseTestMatSBAIJ): 594 COMM = PETSc.COMM_WORLD 595 TYPE = PETSc.Mat.Type.MPISBAIJ 596# bs = 1 597class TestMatMPISBAIJ_G23(TestMatMPISBAIJ): 598 GRID = 2, 3 599class TestMatMPISBAIJ_G45(TestMatMPISBAIJ): 600 GRID = 4, 5 601class TestMatMPISBAIJ_G89(TestMatMPISBAIJ): 602 GRID = 8, 9 603# bs = 2 604class TestMatMPISBAIJ_G23_B2(TestMatMPISBAIJ_G23): 605 BSIZE = 2 606class TestMatMPISBAIJ_G45_B2(TestMatMPISBAIJ_G45): 607 BSIZE = 2 608class TestMatMPISBAIJ_G89_B2(TestMatMPISBAIJ_G89): 609 BSIZE = 2 610# bs = 3 611class TestMatMPISBAIJ_G23_B3(TestMatMPISBAIJ_G23): 612 BSIZE = 3 613class TestMatMPISBAIJ_G45_B3(TestMatMPISBAIJ_G45): 614 BSIZE = 3 615class TestMatMPISBAIJ_G89_B3(TestMatMPISBAIJ_G89): 616 BSIZE = 3 617# bs = 4 618class TestMatMPISBAIJ_G23_B4(TestMatMPISBAIJ_G23): 619 BSIZE = 4 620class TestMatMPISBAIJ_G45_B4(TestMatMPISBAIJ_G45): 621 BSIZE = 4 622class TestMatMPISBAIJ_G89_B4(TestMatMPISBAIJ_G89): 623 BSIZE = 4 624# bs = 5 625class TestMatMPISBAIJ_G23_B5(TestMatMPISBAIJ_G23): 626 BSIZE = 5 627class TestMatMPISBAIJ_G45_B5(TestMatMPISBAIJ_G45): 628 BSIZE = 5 629class TestMatMPISBAIJ_G89_B5(TestMatMPISBAIJ_G89): 630 BSIZE = 5 631 632# -- AIJ + Block --------------- 633 634class BaseTestMatAIJ_B(BaseTestMatAnyAIJ, unittest.TestCase): 635 COMM = PETSc.COMM_WORLD 636 TYPE = PETSc.Mat.Type.AIJ 637 GRID = 0, 0 638 BSIZE = 1 639 640 def testSetPreallocNNZ(self):pass 641 def testSetPreallocNNZ_2(self):pass 642 def testSetPreallocCSR(self):pass 643 def testSetPreallocCSR_2(self):pass 644 def testSetValues(self): 645 self._preallocate() 646 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 647 self.A.setOption(opt, True) 648 ai, aj, av = self._set_values() 649 self.A.assemble() 650 self._chk_aij(self.A, ai, aj) 651 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 652 self.A.setOption(opt, True) 653 ai, aj, av = self._set_values() 654 self.A.assemble() 655 self._chk_aij(self.A, ai, aj) 656 def testSetValuesIJV(self): 657 self._preallocate() 658 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 659 self.A.setOption(opt, True) 660 ai, aj, av = self._set_values_ijv() 661 self.A.assemble() 662 self._chk_aij(self.A, ai, aj) 663 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 664 self.A.setOption(opt, True) 665 ai, aj, av = self._set_values_ijv() 666 self.A.assemble() 667 self._chk_aij(self.A, ai, aj) 668 def _preallocate(self): 669 self.A.setPreallocationNNZ([5*self.BSIZE, 3*self.BSIZE]) 670 self._chk_bs(self.A, self.BSIZE) 671 def _chk_aij(self, A, i, j): 672 bs = self.BSIZE or 1 673 ai, aj = A.getRowIJ() 674 if ai is not None and aj is not None: ## XXX map and check !! 675 #self.assertTrue(N.all(i==ai)) 676 #self.assertTrue(N.all(j==aj)) 677 pass 678 ai, aj = A.getColumnIJ(compressed=bool(self.BSIZE)) 679 if ai is not None and aj is not None: ## XXX map and check !! 680 #self.assertTrue(N.all(i==ai)) 681 #self.assertTrue(N.all(j==aj)) 682 pass 683 684# -- Seq AIJ + Block -- 685 686class TestMatSeqAIJ_B(BaseTestMatAIJ_B): 687 COMM = PETSc.COMM_SELF 688 TYPE = PETSc.Mat.Type.SEQAIJ 689# bs = 1 690class TestMatSeqAIJ_B_G23(TestMatSeqAIJ_B): 691 GRID = 2, 3 692class TestMatSeqAIJ_B_G45(TestMatSeqAIJ_B): 693 GRID = 4, 5 694class TestMatSeqAIJ_B_G89(TestMatSeqAIJ_B): 695 GRID = 8, 9 696# bs = 2 697class TestMatSeqAIJ_B_G23_B2(TestMatSeqAIJ_B_G23): 698 BSIZE = 2 699class TestMatSeqAIJ_B_G45_B2(TestMatSeqAIJ_B_G45): 700 BSIZE = 2 701class TestMatSeqAIJ_B_G89_B2(TestMatSeqAIJ_B_G89): 702 BSIZE = 2 703# bs = 3 704class TestMatSeqAIJ_B_G23_B3(TestMatSeqAIJ_B_G23): 705 BSIZE = 3 706class TestMatSeqAIJ_B_G45_B3(TestMatSeqAIJ_B_G45): 707 BSIZE = 3 708class TestMatSeqAIJ_B_G89_B3(TestMatSeqAIJ_B_G89): 709 BSIZE = 3 710# bs = 4 711class TestMatSeqAIJ_B_G23_B4(TestMatSeqAIJ_B_G23): 712 BSIZE = 4 713class TestMatSeqAIJ_B_G45_B4(TestMatSeqAIJ_B_G45): 714 BSIZE = 4 715class TestMatSeqAIJ_B_G89_B4(TestMatSeqAIJ_B_G89): 716 BSIZE = 4 717# bs = 5 718class TestMatSeqAIJ_B_G23_B5(TestMatSeqAIJ_B_G23): 719 BSIZE = 5 720class TestMatSeqAIJ_B_G45_B5(TestMatSeqAIJ_B_G45): 721 BSIZE = 5 722class TestMatSeqAIJ_B_G89_B5(TestMatSeqAIJ_B_G89): 723 BSIZE = 5 724 725 726# -- MPI AIJ + Block -- 727 728class TestMatMPIAIJ_B(BaseTestMatAIJ_B): 729 COMM = PETSc.COMM_WORLD 730 TYPE = PETSc.Mat.Type.MPIAIJ 731# bs = 1 732class TestMatMPIAIJ_B_G23(TestMatMPIAIJ_B): 733 GRID = 2, 3 734class TestMatMPIAIJ_B_G45(TestMatMPIAIJ_B): 735 GRID = 4, 5 736class TestMatMPIAIJ_B_G89(TestMatMPIAIJ_B): 737 GRID = 8, 9 738# bs = 2 739class TestMatMPIAIJ_B_G23_B2(TestMatMPIAIJ_B_G23): 740 BSIZE = 2 741class TestMatMPIAIJ_B_G45_B2(TestMatMPIAIJ_B_G45): 742 BSIZE = 2 743class TestMatMPIAIJ_B_G89_B2(TestMatMPIAIJ_B_G89): 744 BSIZE = 2 745# bs = 3 746class TestMatMPIAIJ_B_G23_B3(TestMatMPIAIJ_B_G23): 747 BSIZE = 3 748class TestMatMPIAIJ_B_G45_B3(TestMatMPIAIJ_B_G45): 749 BSIZE = 3 750class TestMatMPIAIJ_B_G89_B3(TestMatMPIAIJ_B_G89): 751 BSIZE = 3 752# bs = 4 753class TestMatMPIAIJ_B_G23_B4(TestMatMPIAIJ_B_G23): 754 BSIZE = 4 755class TestMatMPIAIJ_B_G45_B4(TestMatMPIAIJ_B_G45): 756 BSIZE = 4 757class TestMatMPIAIJ_B_G89_B4(TestMatMPIAIJ_B_G89): 758 BSIZE = 4 759# bs = 5 760class TestMatMPIAIJ_B_G23_B5(TestMatMPIAIJ_B_G23): 761 BSIZE = 5 762class TestMatMPIAIJ_B_G45_B5(TestMatMPIAIJ_B_G45): 763 BSIZE = 5 764class TestMatMPIAIJ_B_G89_B5(TestMatMPIAIJ_B_G89): 765 BSIZE = 5 766 767# -- Non-square blocks -- 768class BaseTestMatAIJ_B(BaseTestMatAnyAIJ, unittest.TestCase): 769 COMM = PETSc.COMM_WORLD 770 TYPE = PETSc.Mat.Type.AIJ 771 GRID = 0, 0 772 BSIZE = 4, 2 773 774 def _preallocate(self): 775 try: 776 rbs, cbs = self.BSIZE 777 except (TypeError, ValueError): 778 rbs = cbs = self.BSIZE 779 self.A.setPreallocationNNZ([5*rbs, 3*cbs]) 780 self._chk_bsizes(self.A, self.BSIZE) 781 def testSetPreallocNNZ(self):pass 782 def testSetPreallocNNZ_2(self):pass 783 def testSetPreallocCSR(self):pass 784 def testSetPreallocCSR_2(self):pass 785 def testSetValues(self): 786 self._preallocate() 787 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 788 self.A.setOption(opt, True) 789 ai, aj, av = self._set_values() 790 self.A.assemble() 791 self._chk_aij(self.A, ai, aj) 792 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 793 self.A.setOption(opt, True) 794 ai, aj, av = self._set_values() 795 self.A.assemble() 796 self._chk_aij(self.A, ai, aj) 797 def testSetValuesIJV(self): 798 self._preallocate() 799 opt = PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR 800 self.A.setOption(opt, True) 801 ai, aj, av = self._set_values_ijv() 802 self.A.assemble() 803 self._chk_aij(self.A, ai, aj) 804 opt = PETSc.Mat.Option.NEW_NONZERO_LOCATION_ERR 805 self.A.setOption(opt, True) 806 ai, aj, av = self._set_values_ijv() 807 self.A.assemble() 808 self._chk_aij(self.A, ai, aj) 809 def _chk_aij(self, A, i, j): 810 bs = self.BSIZE or 1 811 ai, aj = A.getRowIJ() 812 if ai is not None and aj is not None: ## XXX map and check !! 813 #self.assertTrue(N.all(i==ai)) 814 #self.assertTrue(N.all(j==aj)) 815 pass 816 ai, aj = A.getColumnIJ() 817 if ai is not None and aj is not None: ## XXX map and check !! 818 #self.assertTrue(N.all(i==ai)) 819 #self.assertTrue(N.all(j==aj)) 820 pass 821 822# -- AIJCRL --------------------- 823 824class BaseTestMatAIJCRL(BaseTestMatAIJ, unittest.TestCase): 825 TYPE = PETSc.Mat.Type.AIJCRL 826 827# -- Seq AIJCRL -- 828 829class TestMatSeqAIJCRL(BaseTestMatAIJCRL): 830 COMM = PETSc.COMM_SELF 831 TYPE = PETSc.Mat.Type.SEQAIJCRL 832class TestMatSeqAIJCRL_G23(TestMatSeqAIJCRL): 833 GRID = 2, 3 834class TestMatSeqAIJCRL_G45(TestMatSeqAIJCRL): 835 GRID = 4, 5 836class TestMatSeqAIJCRL_G89(TestMatSeqAIJCRL): 837 GRID = 8, 9 838 839# -- MPI AIJCRL -- 840 841class TestMatMPIAIJCRL(BaseTestMatAIJCRL): 842 COMM = PETSc.COMM_WORLD 843 TYPE = PETSc.Mat.Type.MPIAIJCRL 844class TestMatMPIAIJCRL_G23(TestMatMPIAIJCRL): 845 GRID = 2, 3 846class TestMatMPIAIJCRL_G45(TestMatMPIAIJCRL): 847 GRID = 4, 5 848class TestMatMPIAIJCRL_G89(TestMatMPIAIJCRL): 849 GRID = 8, 9 850 851# -- AIJCRL + Block ------------- 852 853class BaseTestMatAIJCRL_B(BaseTestMatAIJ_B, unittest.TestCase): 854 TYPE = PETSc.Mat.Type.AIJCRL 855 856# -- Seq AIJCRL + Block -- 857 858class TestMatSeqAIJCRL_B(BaseTestMatAIJCRL_B): 859 COMM = PETSc.COMM_SELF 860 TYPE = PETSc.Mat.Type.SEQAIJCRL 861# bs = 1 862class TestMatSeqAIJCRL_B_G23(TestMatSeqAIJCRL_B): 863 GRID = 2, 3 864class TestMatSeqAIJCRL_B_G45(TestMatSeqAIJCRL_B): 865 GRID = 4, 5 866class TestMatSeqAIJCRL_B_G89(TestMatSeqAIJCRL_B): 867 GRID = 8, 9 868# bs = 2 869class TestMatSeqAIJCRL_B_G23_B2(TestMatSeqAIJCRL_B_G23): 870 BSIZE = 2 871class TestMatSeqAIJCRL_B_G45_B2(TestMatSeqAIJCRL_B_G45): 872 BSIZE = 2 873class TestMatSeqAIJCRL_B_G89_B2(TestMatSeqAIJCRL_B_G89): 874 BSIZE = 2 875# bs = 3 876class TestMatSeqAIJCRL_B_G23_B3(TestMatSeqAIJCRL_B_G23): 877 BSIZE = 3 878class TestMatSeqAIJCRL_B_G45_B3(TestMatSeqAIJCRL_B_G45): 879 BSIZE = 3 880class TestMatSeqAIJCRL_B_G89_B3(TestMatSeqAIJCRL_B_G89): 881 BSIZE = 3 882# bs = 4 883class TestMatSeqAIJCRL_B_G23_B4(TestMatSeqAIJCRL_B_G23): 884 BSIZE = 4 885class TestMatSeqAIJCRL_B_G45_B4(TestMatSeqAIJCRL_B_G45): 886 BSIZE = 4 887class TestMatSeqAIJCRL_B_G89_B4(TestMatSeqAIJCRL_B_G89): 888 BSIZE = 4 889# bs = 5 890class TestMatSeqAIJCRL_B_G23_B5(TestMatSeqAIJCRL_B_G23): 891 BSIZE = 5 892class TestMatSeqAIJCRL_B_G45_B5(TestMatSeqAIJCRL_B_G45): 893 BSIZE = 5 894class TestMatSeqAIJCRL_B_G89_B5(TestMatSeqAIJCRL_B_G89): 895 BSIZE = 5 896 897 898# -- MPI AIJCRL + Block -- 899 900class TestMatMPIAIJCRL_B(BaseTestMatAIJCRL_B): 901 COMM = PETSc.COMM_WORLD 902 TYPE = PETSc.Mat.Type.MPIAIJCRL 903# bs = 1 904class TestMatMPIAIJCRL_B_G23(TestMatMPIAIJCRL_B): 905 GRID = 2, 3 906class TestMatMPIAIJCRL_B_G45(TestMatMPIAIJCRL_B): 907 GRID = 4, 5 908class TestMatMPIAIJCRL_B_G89(TestMatMPIAIJCRL_B): 909 GRID = 8, 9 910# bs = 2 911class TestMatMPIAIJCRL_B_G23_B2(TestMatMPIAIJCRL_B_G23): 912 BSIZE = 2 913class TestMatMPIAIJCRL_B_G45_B2(TestMatMPIAIJCRL_B_G45): 914 BSIZE = 2 915class TestMatMPIAIJCRL_B_G89_B2(TestMatMPIAIJCRL_B_G89): 916 BSIZE = 2 917# bs = 3 918class TestMatMPIAIJCRL_B_G23_B3(TestMatMPIAIJCRL_B_G23): 919 BSIZE = 3 920class TestMatMPIAIJCRL_B_G45_B3(TestMatMPIAIJCRL_B_G45): 921 BSIZE = 3 922class TestMatMPIAIJCRL_B_G89_B3(TestMatMPIAIJCRL_B_G89): 923 BSIZE = 3 924# bs = 4 925class TestMatMPIAIJCRL_B_G23_B4(TestMatMPIAIJCRL_B_G23): 926 BSIZE = 4 927class TestMatMPIAIJCRL_B_G45_B4(TestMatMPIAIJCRL_B_G45): 928 BSIZE = 4 929class TestMatMPIAIJCRL_B_G89_B4(TestMatMPIAIJCRL_B_G89): 930 BSIZE = 4 931# bs = 5 932class TestMatMPIAIJCRL_B_G23_B5(TestMatMPIAIJCRL_B_G23): 933 BSIZE = 5 934class TestMatMPIAIJCRL_B_G45_B5(TestMatMPIAIJCRL_B_G45): 935 BSIZE = 5 936class TestMatMPIAIJCRL_B_G89_B5(TestMatMPIAIJCRL_B_G89): 937 BSIZE = 5 938 939# -- MATIS -- 940 941class TestMatIS(BaseTestMatAIJ): 942 COMM = PETSc.COMM_WORLD 943 TYPE = PETSc.Mat.Type.IS 944class TestMatIS_G23(TestMatIS): 945 GRID = 2, 3 946class TestMatIS_G45(TestMatIS): 947 GRID = 4, 5 948class TestMatIS_G89(TestMatIS): 949 GRID = 8, 9 950 951# ----- 952 953 954 955if __name__ == '__main__': 956 unittest.main() 957