1import petsc4py 2from petsc4py import PETSc 3import unittest 4import os 5import filecmp 6import numpy as np 7 8# -------------------------------------------------------------------- 9 10ERR_ARG_OUTOFRANGE = 63 11 12class BaseTestPlex(object): 13 14 COMM = PETSc.COMM_WORLD 15 DIM = 1 16 CELLS = [[0, 1], [1, 2]] 17 COORDS = [[0.], [0.5], [1.]] 18 COMP = 1 19 DOFS = [1, 0] 20 21 def setUp(self): 22 self.plex = PETSc.DMPlex().createFromCellList(self.DIM, 23 self.CELLS, 24 self.COORDS, 25 comm=self.COMM) 26 27 def tearDown(self): 28 self.plex.destroy() 29 self.plex = None 30 31 def testTopology(self): 32 rank = self.COMM.rank 33 dim = self.plex.getDimension() 34 pStart, pEnd = self.plex.getChart() 35 cStart, cEnd = self.plex.getHeightStratum(0) 36 vStart, vEnd = self.plex.getDepthStratum(0) 37 numDepths = self.plex.getLabelSize("depth") 38 coords_raw = self.plex.getCoordinates().getArray() 39 coords = np.reshape(coords_raw, (vEnd - vStart, dim)) 40 self.assertEqual(dim, self.DIM) 41 self.assertEqual(numDepths, self.DIM+1) 42 if rank == 0 and self.CELLS is not None: 43 self.assertEqual(cEnd-cStart, len(self.CELLS)) 44 if rank == 0 and self.COORDS is not None: 45 self.assertEqual(vEnd-vStart, len(self.COORDS)) 46 self.assertTrue((coords == self.COORDS).all()) 47 48 def testClosure(self): 49 pStart, pEnd = self.plex.getChart() 50 for p in range(pStart, pEnd): 51 closure = self.plex.getTransitiveClosure(p)[0] 52 for c in closure: 53 cone = self.plex.getCone(c) 54 self.assertEqual(self.plex.getConeSize(c), len(cone)) 55 for i in cone: 56 self.assertIn(i, closure) 57 star = self.plex.getTransitiveClosure(p, useCone=False)[0] 58 for s in star: 59 support = self.plex.getSupport(s) 60 self.assertEqual(self.plex.getSupportSize(s), len(support)) 61 for i in support: 62 self.assertIn(i, star) 63 64 def testAdjacency(self): 65 PETSc.DMPlex.setAdjacencyUseAnchors(self.plex, False) 66 flag = PETSc.DMPlex.getAdjacencyUseAnchors(self.plex) 67 self.assertFalse(flag) 68 PETSc.DMPlex.setAdjacencyUseAnchors(self.plex, True) 69 flag = PETSc.DMPlex.getAdjacencyUseAnchors(self.plex) 70 self.assertTrue(flag) 71 PETSc.DMPlex.setBasicAdjacency(self.plex, False, False) 72 flagA, flagB = PETSc.DMPlex.getBasicAdjacency(self.plex) 73 self.assertFalse(flagA) 74 self.assertFalse(flagB) 75 PETSc.DMPlex.setBasicAdjacency(self.plex, True, True) 76 flagA, flagB = PETSc.DMPlex.getBasicAdjacency(self.plex) 77 self.assertTrue(flagA) 78 self.assertTrue(flagB) 79 pStart, pEnd = self.plex.getChart() 80 for p in range(pStart, pEnd): 81 adjacency = self.plex.getAdjacency(p) 82 self.assertTrue(p in adjacency) 83 self.assertTrue(len(adjacency) > 1) 84 85 def testSectionDofs(self): 86 self.plex.setNumFields(1) 87 section = self.plex.createSection([self.COMP], [self.DOFS]) 88 size = section.getStorageSize() 89 entity_dofs = [self.plex.getStratumSize("depth", d) * 90 self.DOFS[d] for d in range(self.DIM+1)] 91 self.assertEqual(sum(entity_dofs), size) 92 93 def testSectionClosure(self): 94 section = self.plex.createSection([self.COMP], [self.DOFS]) 95 self.plex.setSection(section) 96 vec = self.plex.createLocalVec() 97 pStart, pEnd = self.plex.getChart() 98 for p in range(pStart, pEnd): 99 for i in range(section.getDof(p)): 100 off = section.getOffset(p) 101 vec.setValue(off+i, p) 102 103 for p in range(pStart, pEnd): 104 point_closure = self.plex.getTransitiveClosure(p)[0] 105 dof_closure = self.plex.vecGetClosure(section, vec, p) 106 for p in dof_closure: 107 self.assertIn(p, point_closure) 108 109 def testBoundaryLabel(self): 110 pStart, pEnd = self.plex.getChart() 111 if (pEnd - pStart == 0): return 112 113 self.assertFalse(self.plex.hasLabel("boundary")) 114 self.plex.markBoundaryFaces("boundary") 115 self.assertTrue(self.plex.hasLabel("boundary")) 116 117 faces = self.plex.getStratumIS("boundary", 1) 118 for f in faces.getIndices(): 119 points, orient = self.plex.getTransitiveClosure(f, useCone=True) 120 for p in points: 121 self.plex.setLabelValue("boundary", p, 1) 122 123 for p in range(pStart, pEnd): 124 if self.plex.getLabelValue("boundary", p) != 1: 125 self.plex.setLabelValue("boundary", p, 2) 126 127 numBoundary = self.plex.getStratumSize("boundary", 1) 128 numInterior = self.plex.getStratumSize("boundary", 2) 129 self.assertNotEqual(numBoundary, pEnd - pStart) 130 self.assertNotEqual(numInterior, pEnd - pStart) 131 self.assertEqual(numBoundary + numInterior, pEnd - pStart) 132 133 def testMetric(self): 134 if self.DIM == 1: return 135 self.plex.distribute() 136 if self.CELLS is None and not self.plex.isSimplex(): return 137 self.plex.orient() 138 139 h_min = 1.0e-30 140 h_max = 1.0e+30 141 a_max = 1.0e+10 142 target = 10.0 143 p = 1.0 144 beta = 1.3 145 hausd = 0.01 146 self.plex.metricSetUniform(False) 147 self.plex.metricSetIsotropic(False) 148 self.plex.metricSetRestrictAnisotropyFirst(False) 149 self.plex.metricSetNoInsertion(False) 150 self.plex.metricSetNoSwapping(False) 151 self.plex.metricSetNoMovement(False) 152 self.plex.metricSetNoSurf(False) 153 self.plex.metricSetVerbosity(-1) 154 self.plex.metricSetNumIterations(3) 155 self.plex.metricSetMinimumMagnitude(h_min) 156 self.plex.metricSetMaximumMagnitude(h_max) 157 self.plex.metricSetMaximumAnisotropy(a_max) 158 self.plex.metricSetTargetComplexity(target) 159 self.plex.metricSetNormalizationOrder(p) 160 self.plex.metricSetGradationFactor(beta) 161 self.plex.metricSetHausdorffNumber(hausd) 162 163 self.assertFalse(self.plex.metricIsUniform()) 164 self.assertFalse(self.plex.metricIsIsotropic()) 165 self.assertFalse(self.plex.metricRestrictAnisotropyFirst()) 166 self.assertFalse(self.plex.metricNoInsertion()) 167 self.assertFalse(self.plex.metricNoSwapping()) 168 self.assertFalse(self.plex.metricNoMovement()) 169 self.assertFalse(self.plex.metricNoSurf()) 170 assert self.plex.metricGetVerbosity() == -1 171 assert self.plex.metricGetNumIterations() == 3 172 assert np.isclose(self.plex.metricGetMinimumMagnitude(), h_min) 173 assert np.isclose(self.plex.metricGetMaximumMagnitude(), h_max) 174 assert np.isclose(self.plex.metricGetMaximumAnisotropy(), a_max) 175 assert np.isclose(self.plex.metricGetTargetComplexity(), target) 176 assert np.isclose(self.plex.metricGetNormalizationOrder(), p) 177 assert np.isclose(self.plex.metricGetGradationFactor(), beta) 178 assert np.isclose(self.plex.metricGetHausdorffNumber(), hausd) 179 180 metric1 = self.plex.metricCreateUniform(1.0) 181 metric2 = self.plex.metricCreateUniform(2.0) 182 metric = self.plex.metricAverage2(metric1, metric2) 183 metric2.array[:] *= 0.75 184 assert np.allclose(metric.array, metric2.array) 185 metric = self.plex.metricIntersection2(metric1, metric2) 186 assert np.allclose(metric.array, metric1.array) 187 metric = self.plex.metricEnforceSPD(metric) 188 assert np.allclose(metric.array, metric1.array) 189 nMetric = self.plex.metricNormalize(metric, restrictSizes=False, restrictAnisotropy=False) 190 metric.scale(pow(target, 2.0/self.DIM)) 191 assert np.allclose(metric.array, nMetric.array) 192 193 def testAdapt(self): 194 if self.DIM == 1: return 195 self.plex.orient() 196 plex = self.plex.refine() 197 plex.distribute() 198 if self.CELLS is None and not plex.isSimplex(): return 199 if sum(self.DOFS) > 1: return 200 metric = plex.metricCreateUniform(9.0) 201 try: 202 newplex = plex.adaptMetric(metric,"") 203 except PETSc.Error as exc: 204 if exc.ierr != ERR_ARG_OUTOFRANGE: raise 205 206 207# -------------------------------------------------------------------- 208 209class BaseTestPlex_2D(BaseTestPlex): 210 DIM = 2 211 CELLS = [[0, 1, 3], [1, 3, 4], [1, 2, 4], [2, 4, 5], 212 [3, 4, 6], [4, 6, 7], [4, 5, 7], [5, 7, 8]] 213 COORDS = [[0.0, 0.0], [0.5, 0.0], [1.0, 0.0], 214 [0.0, 0.5], [0.5, 0.5], [1.0, 0.5], 215 [0.0, 1.0], [0.5, 1.0], [1.0, 1.0]] 216 DOFS = [1, 0, 0] 217 218class BaseTestPlex_3D(BaseTestPlex): 219 DIM = 3 220 CELLS = [[0, 2, 3, 7], [0, 2, 6, 7], [0, 4, 6, 7], 221 [0, 1, 3, 7], [0, 1, 5, 7], [0, 4, 5, 7]] 222 COORDS = [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [1., 1., 0.], 223 [0., 0., 1.], [1., 0., 1.], [0., 1., 1.], [1., 1., 1.]] 224 DOFS = [1, 0, 0, 0] 225 226# -------------------------------------------------------------------- 227 228class TestPlex_1D(BaseTestPlex, unittest.TestCase): 229 pass 230 231class TestPlex_2D(BaseTestPlex_2D, unittest.TestCase): 232 pass 233 234class TestPlex_3D(BaseTestPlex_3D, unittest.TestCase): 235 pass 236 237class TestPlex_2D_P3(BaseTestPlex_2D, unittest.TestCase): 238 DOFS = [1, 2, 1] 239 240class TestPlex_3D_P3(BaseTestPlex_3D, unittest.TestCase): 241 DOFS = [1, 2, 1, 0] 242 243class TestPlex_3D_P4(BaseTestPlex_3D, unittest.TestCase): 244 DOFS = [1, 3, 3, 1] 245 246class TestPlex_2D_BoxTensor(BaseTestPlex_2D, unittest.TestCase): 247 CELLS = None 248 COORDS = None 249 def setUp(self): 250 self.plex = PETSc.DMPlex().createBoxMesh([3,3], simplex=False) 251 252class TestPlex_3D_BoxTensor(BaseTestPlex_3D, unittest.TestCase): 253 CELLS = None 254 COORDS = None 255 def setUp(self): 256 self.plex = PETSc.DMPlex().createBoxMesh([3,3,3], simplex=False) 257 258try: 259 raise PETSc.Error 260 PETSc.DMPlex().createBoxMesh([2,2], simplex=True, comm=PETSc.COMM_SELF).destroy() 261except PETSc.Error: 262 pass 263else: 264 class TestPlex_2D_Box(BaseTestPlex_2D, unittest.TestCase): 265 CELLS = None 266 COORDS = None 267 def setUp(self): 268 self.plex = PETSc.DMPlex().createBoxMesh([1,1], simplex=True) 269 270 class TestPlex_2D_Boundary(BaseTestPlex_2D, unittest.TestCase): 271 CELLS = None 272 COORDS = None 273 def setUp(self): 274 boundary = PETSc.DMPlex().create(self.COMM) 275 boundary.createSquareBoundary([0., 0.], [1., 1.], [2, 2]) 276 boundary.setDimension(self.DIM-1) 277 self.plex = PETSc.DMPlex().generate(boundary) 278 279 class TestPlex_3D_Box(BaseTestPlex_3D, unittest.TestCase): 280 CELLS = None 281 COORDS = None 282 def setUp(self): 283 self.plex = PETSc.DMPlex().createBoxMesh([1,1,1], simplex=True) 284 285 class TestPlex_3D_Boundary(BaseTestPlex_3D, unittest.TestCase): 286 CELLS = None 287 COORDS = None 288 def setUp(self): 289 boundary = PETSc.DMPlex().create(self.COMM) 290 boundary.createCubeBoundary([0., 0., 0.], [1., 1., 1.], [1, 1, 1]) 291 boundary.setDimension(self.DIM-1) 292 self.plex = PETSc.DMPlex().generate(boundary) 293 294# -------------------------------------------------------------------- 295 296PETSC_DIR = petsc4py.get_config()['PETSC_DIR'] 297 298def check_dtype(method): 299 def wrapper(self, *args, **kwargs): 300 if PETSc.ScalarType is PETSc.ComplexType: 301 return 302 else: 303 return method(self, *args, **kwargs) 304 return wrapper 305 306def check_package(method): 307 def wrapper(self, *args, **kwargs): 308 if not PETSc.Sys.hasExternalPackage("hdf5"): 309 return 310 elif self.PARTITIONERTYPE != "simple" and \ 311 not PETSc.Sys.hasExternalPackage(self.PARTITIONERTYPE): 312 return 313 else: 314 return method(self, *args, **kwargs) 315 return wrapper 316 317def check_nsize(method): 318 def wrapper(self, *args, **kwargs): 319 if PETSc.COMM_WORLD.size != self.NSIZE: 320 return 321 else: 322 return method(self, *args, **kwargs) 323 return wrapper 324 325class BaseTestPlexHDF5(object): 326 NSIZE = 4 327 NTIMES = 3 328 329 def setUp(self): 330 self.txtvwr = PETSc.Viewer() 331 332 def tearDown(self): 333 if not PETSc.COMM_WORLD.rank: 334 if os.path.exists(self.outfile()): 335 os.remove(self.outfile()) 336 if os.path.exists(self.tmp_output_file()): 337 os.remove(self.tmp_output_file()) 338 self.txtvwr = None 339 340 def _name(self): 341 return "%s_outformat-%s_%s" % (self.SUFFIX, 342 self.OUTFORMAT, 343 self.PARTITIONERTYPE) 344 345 def infile(self): 346 return os.path.join(PETSC_DIR, "share/petsc/datafiles/", 347 "meshes/blockcylinder-50.h5") 348 349 def outfile(self): 350 return os.path.join("./temp_test_dmplex_%s.h5" % self._name()) 351 352 def informat(self): 353 return PETSc.Viewer.Format.HDF5_XDMF 354 355 def outformat(self): 356 d = {"hdf5_petsc": PETSc.Viewer.Format.HDF5_PETSC, 357 "hdf5_xdmf": PETSc.Viewer.Format.HDF5_XDMF} 358 return d[self.OUTFORMAT] 359 360 def partitionerType(self): 361 d = {"simple": PETSc.Partitioner.Type.SIMPLE, 362 "ptscotch": PETSc.Partitioner.Type.PTSCOTCH, 363 "parmetis": PETSc.Partitioner.Type.PARMETIS} 364 return d[self.PARTITIONERTYPE] 365 366 def ref_output_file(self): 367 return os.path.join(PETSC_DIR, "src/dm/impls/plex/tutorials/", 368 "output/ex5_%s.out" % self._name()) 369 370 def tmp_output_file(self): 371 return os.path.join("./temp_test_dmplex_%s.out" % self._name()) 372 373 def outputText(self, msg, comm): 374 if not comm.rank: 375 with open(self.tmp_output_file(), 'a') as f: 376 f.write(msg) 377 378 def outputPlex(self, plex): 379 self.txtvwr.createASCII(self.tmp_output_file(), 380 mode='a', comm=plex.comm) 381 plex.view(viewer=self.txtvwr) 382 self.txtvwr.destroy() 383 384 @check_dtype 385 @check_package 386 @check_nsize 387 def testViewLoadCycle(self): 388 grank = PETSc.COMM_WORLD.rank 389 for i in range(self.NTIMES): 390 if i == 0: 391 infname = self.infile() 392 informt = self.informat() 393 else: 394 infname = self.outfile() 395 informt = self.outformat() 396 if self.HETEROGENEOUS: 397 mycolor = (grank > self.NTIMES - i) 398 else: 399 mycolor = 0 400 try: 401 import mpi4py 402 except ImportError: 403 self.skipTest('mpi4py') # throws special exception to signal test skip 404 mpicomm = PETSc.COMM_WORLD.tompi4py() 405 comm = PETSc.Comm(comm=mpicomm.Split(color=mycolor, key=grank)) 406 if mycolor == 0: 407 self.outputText("Begin cycle %d\n" % i, comm) 408 plex = PETSc.DMPlex() 409 vwr = PETSc.ViewerHDF5() 410 # Create plex 411 plex.create(comm=comm) 412 plex.setName("DMPlex Object") 413 # Load data from XDMF into dm in parallel 414 vwr.create(infname, mode='r', comm=comm) 415 vwr.pushFormat(format=informt) 416 plex.load(viewer=vwr) 417 plex.setOptionsPrefix("loaded_") 418 plex.distributeSetDefault(False) 419 plex.setFromOptions() 420 vwr.popFormat() 421 vwr.destroy() 422 self.outputPlex(plex) 423 # Test DM is indeed distributed 424 flg = plex.isDistributed() 425 self.outputText("Loaded mesh distributed? %s\n" % 426 str(flg).upper(), comm) 427 # Interpolate 428 plex.interpolate() 429 plex.setOptionsPrefix("interpolated_") 430 plex.setFromOptions() 431 self.outputPlex(plex) 432 # Redistribute 433 part = plex.getPartitioner() 434 part.setType(self.partitionerType()) 435 _ = plex.distribute(overlap=0) 436 plex.setName("DMPlex Object") 437 plex.setOptionsPrefix("redistributed_") 438 plex.setFromOptions() 439 self.outputPlex(plex) 440 # Save redistributed dm to XDMF in parallel 441 vwr.create(self.outfile(), mode='w', comm=comm) 442 vwr.pushFormat(format=self.outformat()) 443 plex.setName("DMPlex Object") 444 plex.view(viewer=vwr) 445 vwr.popFormat() 446 vwr.destroy() 447 # Destroy plex 448 plex.destroy() 449 self.outputText("End cycle %d\n--------\n" % i, comm) 450 PETSc.COMM_WORLD.Barrier() 451 # Check that the output is identical to that of plex/tutorial/ex5.c. 452 self.assertTrue(filecmp.cmp(self.tmp_output_file(), 453 self.ref_output_file(), shallow=False), 454 'Contents of the files not the same.') 455 PETSc.COMM_WORLD.Barrier() 456 457class BaseTestPlexHDF5Homogeneous(BaseTestPlexHDF5): 458 """Test save on N / load on N.""" 459 SUFFIX = 0 460 HETEROGENEOUS = False 461 462class BaseTestPlexHDF5Heterogeneous(BaseTestPlexHDF5): 463 """Test save on N / load on M.""" 464 SUFFIX = 1 465 HETEROGENEOUS = True 466 467class TestPlexHDF5PETSCSimpleHomogeneous(BaseTestPlexHDF5Homogeneous, 468 unittest.TestCase): 469 OUTFORMAT = "hdf5_petsc" 470 PARTITIONERTYPE = "simple" 471 472""" 473Skipping. PTScotch produces different distributions when run 474in a sequence in a single session. 475 476class TestPlexHDF5PETSCPTScotchHomogeneous(BaseTestPlexHDF5Homogeneous, 477 unittest.TestCase): 478 OUTFORMAT = "hdf5_petsc" 479 PARTITIONERTYPE = "ptscotch" 480""" 481 482class TestPlexHDF5PETSCParmetisHomogeneous(BaseTestPlexHDF5Homogeneous, 483 unittest.TestCase): 484 OUTFORMAT = "hdf5_petsc" 485 PARTITIONERTYPE = "parmetis" 486 487class TestPlexHDF5XDMFSimpleHomogeneous(BaseTestPlexHDF5Homogeneous, 488 unittest.TestCase): 489 OUTFORMAT = "hdf5_xdmf" 490 PARTITIONERTYPE = "simple" 491 492""" 493Skipping. PTScotch produces different distributions when run 494in a sequence in a single session. 495 496class TestPlexHDF5XDMFPTScotchHomogeneous(BaseTestPlexHDF5Homogeneous, 497 unittest.TestCase): 498 OUTFORMAT = "hdf5_xdmf" 499 PARTITIONERTYPE = "ptscotch" 500""" 501 502class TestPlexHDF5XDMFParmetisHomogeneous(BaseTestPlexHDF5Homogeneous, 503 unittest.TestCase): 504 OUTFORMAT = "hdf5_xdmf" 505 PARTITIONERTYPE = "parmetis" 506 507class TestPlexHDF5PETSCSimpleHeterogeneous(BaseTestPlexHDF5Heterogeneous, 508 unittest.TestCase): 509 OUTFORMAT = "hdf5_petsc" 510 PARTITIONERTYPE = "simple" 511 512""" 513Skipping. PTScotch produces different distributions when run 514in a sequence in a single session. 515 516class TestPlexHDF5PETSCPTScotchHeterogeneous(BaseTestPlexHDF5Heterogeneous, 517 unittest.TestCase): 518 OUTFORMAT = "hdf5_petsc" 519 PARTITIONERTYPE = "ptscotch" 520""" 521 522class TestPlexHDF5PETSCParmetisHeterogeneous(BaseTestPlexHDF5Heterogeneous, 523 unittest.TestCase): 524 OUTFORMAT = "hdf5_petsc" 525 PARTITIONERTYPE = "parmetis" 526 527class TestPlexHDF5XDMFSimpleHeterogeneous(BaseTestPlexHDF5Heterogeneous, 528 unittest.TestCase): 529 OUTFORMAT = "hdf5_xdmf" 530 PARTITIONERTYPE = "simple" 531 532class TestPlexHDF5XDMFPTScotchHeterogeneous(BaseTestPlexHDF5Heterogeneous, 533 unittest.TestCase): 534 OUTFORMAT = "hdf5_xdmf" 535 PARTITIONERTYPE = "ptscotch" 536 537class TestPlexHDF5XDMFParmetisHeterogeneous(BaseTestPlexHDF5Heterogeneous, 538 unittest.TestCase): 539 OUTFORMAT = "hdf5_xdmf" 540 PARTITIONERTYPE = "parmetis" 541 542# -------------------------------------------------------------------- 543 544if __name__ == '__main__': 545 unittest.main() 546