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 self.plex.metricSetIsotropic(False) 146 self.plex.metricSetRestrictAnisotropyFirst(False) 147 self.plex.metricSetNoInsertion(False) 148 self.plex.metricSetNoSwapping(False) 149 self.plex.metricSetNoMovement(False) 150 self.plex.metricSetVerbosity(-1) 151 self.plex.metricSetNumIterations(3) 152 self.plex.metricSetMinimumMagnitude(h_min) 153 self.plex.metricSetMaximumMagnitude(h_max) 154 self.plex.metricSetMaximumAnisotropy(a_max) 155 self.plex.metricSetTargetComplexity(target) 156 self.plex.metricSetNormalizationOrder(p) 157 self.plex.metricSetGradationFactor(beta) 158 159 self.assertFalse(self.plex.metricIsIsotropic()) 160 self.assertFalse(self.plex.metricRestrictAnisotropyFirst()) 161 self.assertFalse(self.plex.metricNoInsertion()) 162 self.assertFalse(self.plex.metricNoSwapping()) 163 self.assertFalse(self.plex.metricNoMovement()) 164 assert self.plex.metricGetVerbosity() == -1 165 assert self.plex.metricGetNumIterations() == 3 166 assert np.isclose(self.plex.metricGetMinimumMagnitude(), h_min) 167 assert np.isclose(self.plex.metricGetMaximumMagnitude(), h_max) 168 assert np.isclose(self.plex.metricGetMaximumAnisotropy(), a_max) 169 assert np.isclose(self.plex.metricGetTargetComplexity(), target) 170 assert np.isclose(self.plex.metricGetNormalizationOrder(), p) 171 assert np.isclose(self.plex.metricGetGradationFactor(), beta) 172 173 metric1 = self.plex.metricCreateUniform(1.0) 174 metric2 = self.plex.metricCreateUniform(2.0) 175 metric = self.plex.metricAverage2(metric1, metric2) 176 metric2.array[:] *= 0.75 177 assert np.allclose(metric.array, metric2.array) 178 metric = self.plex.metricIntersection2(metric1, metric2) 179 assert np.allclose(metric.array, metric1.array) 180 metric = self.plex.metricEnforceSPD(metric) 181 assert np.allclose(metric.array, metric1.array) 182 nMetric = self.plex.metricNormalize(metric, restrictSizes=False, restrictAnisotropy=False) 183 metric.scale(pow(target, 2.0/self.DIM)) 184 assert np.allclose(metric.array, nMetric.array) 185 186 def testAdapt(self): 187 if self.DIM == 1: return 188 self.plex.distribute() 189 if self.CELLS is None and not self.plex.isSimplex(): return 190 if sum(self.DOFS) > 1: return 191 metric = self.plex.metricCreateUniform(9.0) 192 try: 193 newplex = self.plex.adaptMetric(metric,"") 194 except PETSc.Error as exc: 195 if exc.ierr != ERR_ARG_OUTOFRANGE: raise 196 197 198# -------------------------------------------------------------------- 199 200class BaseTestPlex_2D(BaseTestPlex): 201 DIM = 2 202 CELLS = [[0, 1, 3], [1, 3, 4], [1, 2, 4], [2, 4, 5], 203 [3, 4, 6], [4, 6, 7], [4, 5, 7], [5, 7, 8]] 204 COORDS = [[0.0, 0.0], [0.5, 0.0], [1.0, 0.0], 205 [0.0, 0.5], [0.5, 0.5], [1.0, 0.5], 206 [0.0, 1.0], [0.5, 1.0], [1.0, 1.0]] 207 DOFS = [1, 0, 0] 208 209class BaseTestPlex_3D(BaseTestPlex): 210 DIM = 3 211 CELLS = [[0, 2, 3, 7], [0, 2, 6, 7], [0, 4, 6, 7], 212 [0, 1, 3, 7], [0, 1, 5, 7], [0, 4, 5, 7]] 213 COORDS = [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [1., 1., 0.], 214 [0., 0., 1.], [1., 0., 1.], [0., 1., 1.], [1., 1., 1.]] 215 DOFS = [1, 0, 0, 0] 216 217# -------------------------------------------------------------------- 218 219class TestPlex_1D(BaseTestPlex, unittest.TestCase): 220 pass 221 222class TestPlex_2D(BaseTestPlex_2D, unittest.TestCase): 223 pass 224 225class TestPlex_3D(BaseTestPlex_3D, unittest.TestCase): 226 pass 227 228class TestPlex_2D_P3(BaseTestPlex_2D, unittest.TestCase): 229 DOFS = [1, 2, 1] 230 231class TestPlex_3D_P3(BaseTestPlex_3D, unittest.TestCase): 232 DOFS = [1, 2, 1, 0] 233 234class TestPlex_3D_P4(BaseTestPlex_3D, unittest.TestCase): 235 DOFS = [1, 3, 3, 1] 236 237class TestPlex_2D_BoxTensor(BaseTestPlex_2D, unittest.TestCase): 238 CELLS = None 239 COORDS = None 240 def setUp(self): 241 self.plex = PETSc.DMPlex().createBoxMesh([3,3], simplex=False) 242 243class TestPlex_3D_BoxTensor(BaseTestPlex_3D, unittest.TestCase): 244 CELLS = None 245 COORDS = None 246 def setUp(self): 247 self.plex = PETSc.DMPlex().createBoxMesh([3,3,3], simplex=False) 248 249try: 250 raise PETSc.Error 251 PETSc.DMPlex().createBoxMesh([2,2], simplex=True, comm=PETSc.COMM_SELF).destroy() 252except PETSc.Error: 253 pass 254else: 255 class TestPlex_2D_Box(BaseTestPlex_2D, unittest.TestCase): 256 CELLS = None 257 COORDS = None 258 def setUp(self): 259 self.plex = PETSc.DMPlex().createBoxMesh([1,1], simplex=True) 260 261 class TestPlex_2D_Boundary(BaseTestPlex_2D, unittest.TestCase): 262 CELLS = None 263 COORDS = None 264 def setUp(self): 265 boundary = PETSc.DMPlex().create(self.COMM) 266 boundary.createSquareBoundary([0., 0.], [1., 1.], [2, 2]) 267 boundary.setDimension(self.DIM-1) 268 self.plex = PETSc.DMPlex().generate(boundary) 269 270 class TestPlex_3D_Box(BaseTestPlex_3D, unittest.TestCase): 271 CELLS = None 272 COORDS = None 273 def setUp(self): 274 self.plex = PETSc.DMPlex().createBoxMesh([1,1,1], simplex=True) 275 276 class TestPlex_3D_Boundary(BaseTestPlex_3D, unittest.TestCase): 277 CELLS = None 278 COORDS = None 279 def setUp(self): 280 boundary = PETSc.DMPlex().create(self.COMM) 281 boundary.createCubeBoundary([0., 0., 0.], [1., 1., 1.], [1, 1, 1]) 282 boundary.setDimension(self.DIM-1) 283 self.plex = PETSc.DMPlex().generate(boundary) 284 285# -------------------------------------------------------------------- 286 287PETSC_DIR = petsc4py.get_config()['PETSC_DIR'] 288 289def check_dtype(method): 290 def wrapper(self, *args, **kwargs): 291 if PETSc.ScalarType is PETSc.ComplexType: 292 return 293 else: 294 return method(self, *args, **kwargs) 295 return wrapper 296 297def check_package(method): 298 def wrapper(self, *args, **kwargs): 299 if not PETSc.Sys.hasExternalPackage("hdf5"): 300 return 301 elif self.PARTITIONERTYPE != "simple" and \ 302 not PETSc.Sys.hasExternalPackage(self.PARTITIONERTYPE): 303 return 304 else: 305 return method(self, *args, **kwargs) 306 return wrapper 307 308def check_nsize(method): 309 def wrapper(self, *args, **kwargs): 310 if PETSc.COMM_WORLD.size != self.NSIZE: 311 return 312 else: 313 return method(self, *args, **kwargs) 314 return wrapper 315 316class BaseTestPlexHDF5(object): 317 NSIZE = 4 318 NTIMES = 3 319 320 def setUp(self): 321 self.txtvwr = PETSc.Viewer() 322 323 def tearDown(self): 324 if not PETSc.COMM_WORLD.rank: 325 if os.path.exists(self.outfile()): 326 os.remove(self.outfile()) 327 if os.path.exists(self.tmp_output_file()): 328 os.remove(self.tmp_output_file()) 329 self.txtvwr = None 330 331 def _name(self): 332 return "%s_outformat-%s_%s" % (self.SUFFIX, 333 self.OUTFORMAT, 334 self.PARTITIONERTYPE) 335 336 def infile(self): 337 return os.path.join(PETSC_DIR, "share/petsc/datafiles/", 338 "meshes/blockcylinder-50.h5") 339 340 def outfile(self): 341 return os.path.join("./temp_test_dmplex_%s.h5" % self._name()) 342 343 def informat(self): 344 return PETSc.Viewer.Format.HDF5_XDMF 345 346 def outformat(self): 347 d = {"hdf5_petsc": PETSc.Viewer.Format.HDF5_PETSC, 348 "hdf5_xdmf": PETSc.Viewer.Format.HDF5_XDMF} 349 return d[self.OUTFORMAT] 350 351 def partitionerType(self): 352 d = {"simple": PETSc.Partitioner.Type.SIMPLE, 353 "ptscotch": PETSc.Partitioner.Type.PTSCOTCH, 354 "parmetis": PETSc.Partitioner.Type.PARMETIS} 355 return d[self.PARTITIONERTYPE] 356 357 def ref_output_file(self): 358 return os.path.join(PETSC_DIR, "src/dm/impls/plex/tutorials/", 359 "output/ex5_%s.out" % self._name()) 360 361 def tmp_output_file(self): 362 return os.path.join("./temp_test_dmplex_%s.out" % self._name()) 363 364 def outputText(self, msg, comm): 365 if not comm.rank: 366 with open(self.tmp_output_file(), 'a') as f: 367 f.write(msg) 368 369 def outputPlex(self, plex): 370 self.txtvwr.createASCII(self.tmp_output_file(), 371 mode='a', comm=plex.comm) 372 plex.view(viewer=self.txtvwr) 373 self.txtvwr.destroy() 374 375 @check_dtype 376 @check_package 377 @check_nsize 378 def testViewLoadCycle(self): 379 grank = PETSc.COMM_WORLD.rank 380 for i in range(self.NTIMES): 381 if i == 0: 382 infname = self.infile() 383 informt = self.informat() 384 else: 385 infname = self.outfile() 386 informt = self.outformat() 387 if self.HETEROGENEOUS: 388 mycolor = (grank > self.NTIMES - i) 389 else: 390 mycolor = 0 391 try: 392 import mpi4py 393 except ImportError: 394 self.skipTest('mpi4py') # throws special exception to signal test skip 395 mpicomm = PETSc.COMM_WORLD.tompi4py() 396 comm = PETSc.Comm(comm=mpicomm.Split(color=mycolor, key=grank)) 397 if mycolor == 0: 398 self.outputText("Begin cycle %d\n" % i, comm) 399 plex = PETSc.DMPlex() 400 vwr = PETSc.ViewerHDF5() 401 # Create plex 402 plex.create(comm=comm) 403 plex.setName("DMPlex Object") 404 # Load data from XDMF into dm in parallel 405 vwr.create(infname, mode='r', comm=comm) 406 vwr.pushFormat(format=informt) 407 plex.load(viewer=vwr) 408 plex.setOptionsPrefix("loaded_") 409 plex.setFromOptions() 410 vwr.popFormat() 411 vwr.destroy() 412 self.outputPlex(plex) 413 # Test DM is indeed distributed 414 flg = plex.isDistributed() 415 self.outputText("Loaded mesh distributed? %s\n" % 416 str(flg).upper(), comm) 417 # Interpolate 418 plex.interpolate() 419 plex.setOptionsPrefix("interpolated_") 420 plex.setFromOptions() 421 self.outputPlex(plex) 422 # Redistribute 423 part = plex.getPartitioner() 424 part.setType(self.partitionerType()) 425 _ = plex.distribute(overlap=0) 426 plex.setOptionsPrefix("redistributed_") 427 plex.setFromOptions() 428 self.outputPlex(plex) 429 # Save redistributed dm to XDMF in parallel 430 vwr.create(self.outfile(), mode='w', comm=comm) 431 vwr.pushFormat(format=self.outformat()) 432 plex.setName("DMPlex Object") 433 plex.view(viewer=vwr) 434 vwr.popFormat() 435 vwr.destroy() 436 # Destroy plex 437 plex.destroy() 438 self.outputText("End cycle %d\n--------\n" % i, comm) 439 PETSc.COMM_WORLD.Barrier() 440 # Check that the output is identical to that of plex/tutorial/ex5.c. 441 self.assertTrue(filecmp.cmp(self.tmp_output_file(), 442 self.ref_output_file(), shallow=False), 443 'Contents of the files not the same.') 444 PETSc.COMM_WORLD.Barrier() 445 446class BaseTestPlexHDF5Homogeneous(BaseTestPlexHDF5): 447 """Test save on N / load on N.""" 448 SUFFIX = 0 449 HETEROGENEOUS = False 450 451class BaseTestPlexHDF5Heterogeneous(BaseTestPlexHDF5): 452 """Test save on N / load on M.""" 453 SUFFIX = 1 454 HETEROGENEOUS = True 455 456class TestPlexHDF5PETSCSimpleHomogeneous(BaseTestPlexHDF5Homogeneous, 457 unittest.TestCase): 458 OUTFORMAT = "hdf5_petsc" 459 PARTITIONERTYPE = "simple" 460 461""" 462Skipping. PTScotch produces different distributions when run 463in a sequence in a single session. 464 465class TestPlexHDF5PETSCPTScotchHomogeneous(BaseTestPlexHDF5Homogeneous, 466 unittest.TestCase): 467 OUTFORMAT = "hdf5_petsc" 468 PARTITIONERTYPE = "ptscotch" 469""" 470 471class TestPlexHDF5PETSCParmetisHomogeneous(BaseTestPlexHDF5Homogeneous, 472 unittest.TestCase): 473 OUTFORMAT = "hdf5_petsc" 474 PARTITIONERTYPE = "parmetis" 475 476class TestPlexHDF5XDMFSimpleHomogeneous(BaseTestPlexHDF5Homogeneous, 477 unittest.TestCase): 478 OUTFORMAT = "hdf5_xdmf" 479 PARTITIONERTYPE = "simple" 480 481""" 482Skipping. PTScotch produces different distributions when run 483in a sequence in a single session. 484 485class TestPlexHDF5XDMFPTScotchHomogeneous(BaseTestPlexHDF5Homogeneous, 486 unittest.TestCase): 487 OUTFORMAT = "hdf5_xdmf" 488 PARTITIONERTYPE = "ptscotch" 489""" 490 491class TestPlexHDF5XDMFParmetisHomogeneous(BaseTestPlexHDF5Homogeneous, 492 unittest.TestCase): 493 OUTFORMAT = "hdf5_xdmf" 494 PARTITIONERTYPE = "parmetis" 495 496class TestPlexHDF5PETSCSimpleHeterogeneous(BaseTestPlexHDF5Heterogeneous, 497 unittest.TestCase): 498 OUTFORMAT = "hdf5_petsc" 499 PARTITIONERTYPE = "simple" 500 501""" 502Skipping. PTScotch produces different distributions when run 503in a sequence in a single session. 504 505class TestPlexHDF5PETSCPTScotchHeterogeneous(BaseTestPlexHDF5Heterogeneous, 506 unittest.TestCase): 507 OUTFORMAT = "hdf5_petsc" 508 PARTITIONERTYPE = "ptscotch" 509""" 510 511class TestPlexHDF5PETSCParmetisHeterogeneous(BaseTestPlexHDF5Heterogeneous, 512 unittest.TestCase): 513 OUTFORMAT = "hdf5_petsc" 514 PARTITIONERTYPE = "parmetis" 515 516class TestPlexHDF5XDMFSimpleHeterogeneous(BaseTestPlexHDF5Heterogeneous, 517 unittest.TestCase): 518 OUTFORMAT = "hdf5_xdmf" 519 PARTITIONERTYPE = "simple" 520 521class TestPlexHDF5XDMFPTScotchHeterogeneous(BaseTestPlexHDF5Heterogeneous, 522 unittest.TestCase): 523 OUTFORMAT = "hdf5_xdmf" 524 PARTITIONERTYPE = "ptscotch" 525 526class TestPlexHDF5XDMFParmetisHeterogeneous(BaseTestPlexHDF5Heterogeneous, 527 unittest.TestCase): 528 OUTFORMAT = "hdf5_xdmf" 529 PARTITIONERTYPE = "parmetis" 530 531# -------------------------------------------------------------------- 532 533if __name__ == '__main__': 534 unittest.main() 535