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