-
Notifications
You must be signed in to change notification settings - Fork 157
/
mesh.py
1225 lines (976 loc) · 45.9 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
import numpy as np
import ctypes
import os
import ufl
import weakref
from pyop2 import op2
from pyop2.logger import info_red
from pyop2.profiling import timed_function, timed_region, profile
from pyop2.utils import as_tuple
import coffee.base as ast
import firedrake.dmplex as dmplex
import firedrake.extrusion_utils as eutils
import firedrake.spatialindex as spatialindex
import firedrake.utils as utils
from firedrake.parameters import parameters
from firedrake.petsc import PETSc
__all__ = ['Mesh', 'ExtrudedMesh', 'SubDomainData']
_cells = {
1: {2: "interval"},
2: {3: "triangle", 4: "quadrilateral"},
3: {4: "tetrahedron"}
}
class _Facets(object):
"""Wrapper class for facet interation information on a :func:`Mesh`
.. warning::
The unique_markers argument **must** be the same on all processes."""
def __init__(self, mesh, classes, kind, facet_cell, local_facet_number, markers=None,
unique_markers=None):
self.mesh = mesh
classes = as_tuple(classes, int, 4)
self.classes = classes
self.kind = kind
assert(kind in ["interior", "exterior"])
if kind == "interior":
self._rank = 2
else:
self._rank = 1
self.facet_cell = facet_cell
self.local_facet_number = local_facet_number
# assert that markers is a proper subset of unique_markers
if markers is not None:
for marker in markers:
assert (marker in unique_markers), \
"Every marker has to be contained in unique_markers"
self.markers = markers
self.unique_markers = [] if unique_markers is None else unique_markers
self._subsets = {}
@utils.cached_property
def set(self):
size = self.classes
halo = None
if isinstance(self.mesh, ExtrudedMeshTopology):
if self.kind == "interior":
base = self.mesh._base_mesh.interior_facets.set
else:
base = self.mesh._base_mesh.exterior_facets.set
return op2.ExtrudedSet(base, layers=self.mesh.layers)
return op2.Set(size, "%s_%s_facets" % (self.mesh.name, self.kind), halo=halo)
@property
def bottom_set(self):
'''Returns the bottom row of cells.'''
return self.mesh.cell_set
@utils.cached_property
def _null_subset(self):
'''Empty subset for the case in which there are no facets with
a given marker value. This is required because not all
markers need be represented on all processors.'''
return op2.Subset(self.set, [])
def measure_set(self, integral_type, subdomain_id):
'''Return the iteration set appropriate to measure. This will
either be for all the interior or exterior (as appropriate)
facets, or for a particular numbered subdomain.'''
# ufl.Measure doesn't have enums for these any more :(
if subdomain_id in ["everywhere", "otherwise"]:
if integral_type == "exterior_facet_bottom":
return [(op2.ON_BOTTOM, self.bottom_set)]
elif integral_type == "exterior_facet_top":
return [(op2.ON_TOP, self.bottom_set)]
elif integral_type == "interior_facet_horiz":
return self.bottom_set
else:
return self.set
else:
return self.subset(subdomain_id)
def subset(self, markers):
"""Return the subset corresponding to a given marker value.
:param markers: integer marker id or an iterable of marker ids"""
if self.markers is None:
return self._null_subset
markers = as_tuple(markers, int)
try:
return self._subsets[markers]
except KeyError:
# check that the given markers are valid
for marker in markers:
if marker not in self.unique_markers:
raise LookupError(
'{0} is not a valid marker'.
format(marker))
# build a list of indices corresponding to the subsets selected by
# markers
indices = np.concatenate([np.nonzero(self.markers == i)[0]
for i in markers])
self._subsets[markers] = op2.Subset(self.set, indices)
return self._subsets[markers]
@utils.cached_property
def local_facet_dat(self):
"""Dat indicating which local facet of each adjacent
cell corresponds to the current facet."""
return op2.Dat(op2.DataSet(self.set, self._rank), self.local_facet_number,
np.uintc, "%s_%s_local_facet_number" % (self.mesh.name, self.kind))
@utils.cached_property
def facet_cell_map(self):
"""Map from facets to cells."""
return op2.Map(self.set, self.bottom_set, self._rank, self.facet_cell,
"facet_to_cell_map")
def _from_gmsh(filename):
"""Read a Gmsh .msh file from `filename`"""
# Create a read-only PETSc.Viewer
gmsh_viewer = PETSc.Viewer().create()
gmsh_viewer.setType("ascii")
gmsh_viewer.setFileMode("r")
gmsh_viewer.setFileName(filename)
gmsh_plex = PETSc.DMPlex().createGmsh(gmsh_viewer)
if gmsh_plex.hasLabel("Face Sets"):
boundary_ids = gmsh_plex.getLabelIdIS("Face Sets").getIndices()
gmsh_plex.createLabel("boundary_ids")
for bid in boundary_ids:
faces = gmsh_plex.getStratumIS("Face Sets", bid).getIndices()
for f in faces:
gmsh_plex.setLabelValue("boundary_ids", f, bid)
return gmsh_plex
def _from_exodus(filename):
"""Read an Exodus .e or .exo file from `filename`"""
plex = PETSc.DMPlex().createExodusFromFile(filename)
boundary_ids = dmplex.getLabelIdIS("Face Sets").getIndices()
plex.createLabel("boundary_ids")
for bid in boundary_ids:
faces = plex.getStratumIS("Face Sets", bid).getIndices()
for f in faces:
plex.setLabelValue("boundary_ids", f, bid)
return plex
def _from_cgns(filename):
"""Read a CGNS .cgns file from `filename`"""
plex = PETSc.DMPlex().createCGNSFromFile(filename)
# TODO: Add boundary IDs
return plex
def _from_triangle(filename, dim):
"""Read a set of triangle mesh files from `filename`"""
basename, ext = os.path.splitext(filename)
if op2.MPI.comm.rank == 0:
try:
facetfile = open(basename+".face")
tdim = 3
except:
try:
facetfile = open(basename+".edge")
tdim = 2
except:
facetfile = None
tdim = 1
if dim is None:
dim = tdim
op2.MPI.comm.bcast(tdim, root=0)
with open(basename+".node") as nodefile:
header = np.fromfile(nodefile, dtype=np.int32, count=2, sep=' ')
nodecount = header[0]
nodedim = header[1]
assert nodedim == dim
coordinates = np.loadtxt(nodefile, usecols=range(1, dim+1), skiprows=1)
assert nodecount == coordinates.shape[0]
with open(basename+".ele") as elefile:
header = np.fromfile(elefile, dtype=np.int32, count=2, sep=' ')
elecount = header[0]
eledim = header[1]
eles = np.loadtxt(elefile, usecols=range(1, eledim+1), dtype=np.int32, skiprows=1)
assert elecount == eles.shape[0]
cells = map(lambda c: c-1, eles)
else:
tdim = op2.MPI.comm.bcast(None, root=0)
cells = None
coordinates = None
plex = _from_cell_list(tdim, cells, coordinates, comm=op2.MPI.comm)
# Apply boundary IDs
if op2.MPI.comm.rank == 0:
facets = None
try:
header = np.fromfile(facetfile, dtype=np.int32, count=2, sep=' ')
edgecount = header[0]
facets = np.loadtxt(facetfile, usecols=range(1, tdim+2), dtype=np.int32, skiprows=0)
assert edgecount == facets.shape[0]
finally:
facetfile.close()
if facets is not None:
vStart, vEnd = plex.getDepthStratum(0) # vertices
for facet in facets:
bid = facet[-1]
vertices = map(lambda v: v + vStart - 1, facet[:-1])
join = plex.getJoin(vertices)
plex.setLabelValue("boundary_ids", join[0], bid)
return plex
def _from_cell_list(dim, cells, coords, comm=None):
"""
Create a DMPlex from a list of cells and coords.
:arg dim: The topological dimension of the mesh
:arg cells: The vertices of each cell
:arg coords: The coordinates of each vertex
:arg comm: An optional MPI communicator to build the plex on
(defaults to ``COMM_WORLD``)
"""
if comm is None:
comm = op2.MPI.comm
if comm.rank == 0:
cells = np.asarray(cells, dtype=PETSc.IntType)
coords = np.asarray(coords, dtype=float)
comm.bcast(cells.shape, root=0)
comm.bcast(coords.shape, root=0)
# Provide the actual data on rank 0.
return PETSc.DMPlex().createFromCellList(dim, cells, coords, comm=comm)
cell_shape = list(comm.bcast(None, root=0))
coord_shape = list(comm.bcast(None, root=0))
cell_shape[0] = 0
coord_shape[0] = 0
# Provide empty plex on other ranks
# A subsequent call to plex.distribute() takes care of parallel partitioning
return PETSc.DMPlex().createFromCellList(dim,
np.zeros(cell_shape, dtype=PETSc.IntType),
np.zeros(coord_shape, dtype=float),
comm=comm)
class MeshTopology(object):
"""A representation of mesh topology."""
def __init__(self, plex, name, reorder, distribute):
"""Half-initialise a mesh topology.
:arg plex: :class:`DMPlex` representing the mesh topology
:arg name: name of the mesh
:arg reorder: whether to reorder the mesh (bool)
:arg distribute: whether to distribute the mesh to parallel processes
"""
# Do some validation of the input mesh
dmplex.validate_mesh(plex)
utils._init()
self._plex = plex
self.name = name
# A cache of function spaces that have been built on this mesh
self._cache = {}
# Mark exterior and interior facets
# Note. This must come before distribution, because otherwise
# DMPlex will consider facets on the domain boundary to be
# exterior, which is wrong.
with timed_region("Mesh: label facets"):
label_boundary = (op2.MPI.comm.size == 1) or distribute
dmplex.label_facets(plex, label_boundary=label_boundary)
# Distribute the dm to all ranks
if op2.MPI.comm.size > 1 and distribute:
# We distribute with overlap zero, in case we're going to
# refine this mesh in parallel. Later, when we actually use
# it, we grow the halo.
plex.distribute(overlap=0)
dim = plex.getDimension()
cStart, cEnd = plex.getHeightStratum(0) # cells
cell_nfacets = plex.getConeSize(cStart)
self._grown_halos = False
self._ufl_cell = ufl.Cell(_cells[dim][cell_nfacets])
def callback(self):
"""Finish initialisation."""
del self._callback
if op2.MPI.comm.size > 1:
self._plex.distributeOverlap(1)
self._grown_halos = True
if reorder:
with timed_region("Mesh: reorder"):
old_to_new = self._plex.getOrdering(PETSc.Mat.OrderingType.RCM).indices
reordering = np.empty_like(old_to_new)
reordering[old_to_new] = np.arange(old_to_new.size, dtype=old_to_new.dtype)
else:
# No reordering
reordering = None
self._did_reordering = bool(reorder)
# Mark OP2 entities and derive the resulting Plex renumbering
with timed_region("Mesh: renumbering"):
dmplex.mark_entity_classes(self._plex)
self._entity_classes = dmplex.get_entity_classes(self._plex)
self._plex_renumbering = dmplex.plex_renumbering(self._plex,
self._entity_classes,
reordering)
with timed_region("Mesh: cell numbering"):
# Derive a cell numbering from the Plex renumbering
entity_dofs = np.zeros(dim+1, dtype=np.int32)
entity_dofs[-1] = 1
self._cell_numbering = self._plex.createSection([1], entity_dofs,
perm=self._plex_renumbering)
entity_dofs[:] = 0
entity_dofs[0] = 1
self._vertex_numbering = self._plex.createSection([1], entity_dofs,
perm=self._plex_renumbering)
self._callback = callback
def init(self):
"""Finish the initialisation of the mesh."""
if hasattr(self, '_callback'):
self._callback(self)
@property
def topology(self):
"""The underlying mesh topology object."""
return self
@property
def topological(self):
"""Alias of topology.
This is to ensure consistent naming for some multigrid codes."""
return self
@property
def layers(self):
return None
def ufl_cell(self):
"""The UFL :class:`~ufl.classes.Cell` associated with the mesh."""
return self._ufl_cell
@utils.cached_property
def cell_closure(self):
"""2D array of ordered cell closures
Each row contains ordered cell entities for a cell, one row per cell.
"""
plex = self._plex
dim = plex.getDimension()
# Cell numbering and global vertex numbering
cell_numbering = self._cell_numbering
vertex_numbering = self._vertex_numbering.createGlobalSection(plex.getPointSF())
cell = self.ufl_cell()
if cell.is_simplex():
# Simplex mesh
cStart, cEnd = plex.getHeightStratum(0)
a_closure = plex.getTransitiveClosure(cStart)[0]
entity_per_cell = np.zeros(dim + 1, dtype=np.int32)
for dim in xrange(dim + 1):
start, end = plex.getDepthStratum(dim)
entity_per_cell[dim] = sum(map(lambda idx: start <= idx < end,
a_closure))
return dmplex.closure_ordering(plex, vertex_numbering,
cell_numbering, entity_per_cell)
elif cell.cellname() == "quadrilateral":
from firedrake.citations import Citations
Citations().register("Homolya2016")
Citations().register("McRae2014")
# Quadrilateral mesh
cell_ranks = dmplex.get_cell_remote_ranks(plex)
facet_orientations = dmplex.quadrilateral_facet_orientations(
plex, vertex_numbering, cell_ranks)
cell_orientations = dmplex.orientations_facet2cell(
plex, vertex_numbering, cell_ranks,
facet_orientations, cell_numbering)
dmplex.exchange_cell_orientations(plex,
cell_numbering,
cell_orientations)
return dmplex.quadrilateral_closure_ordering(
plex, vertex_numbering, cell_numbering, cell_orientations)
else:
raise NotImplementedError("Cell type '%s' not supported." % cell)
@utils.cached_property
def exterior_facets(self):
if self._plex.getStratumSize("exterior_facets", 1) > 0:
# Compute the facet_numbering
# Order exterior facets by OP2 entity class
exterior_facets, exterior_facet_classes = \
dmplex.get_facets_by_class(self._plex, "exterior_facets")
# Derive attached boundary IDs
if self._plex.hasLabel("boundary_ids"):
boundary_ids = np.zeros(exterior_facets.size, dtype=np.int32)
for i, facet in enumerate(exterior_facets):
boundary_ids[i] = self._plex.getLabelValue("boundary_ids", facet)
unique_ids = np.sort(self._plex.getLabelIdIS("boundary_ids").indices)
else:
boundary_ids = None
unique_ids = None
exterior_local_facet_number, exterior_facet_cell = \
dmplex.facet_numbering(self._plex, "exterior",
exterior_facets,
self._cell_numbering,
self.cell_closure)
return _Facets(self, exterior_facet_classes, "exterior",
exterior_facet_cell, exterior_local_facet_number,
boundary_ids, unique_markers=unique_ids)
else:
if self._plex.hasLabel("boundary_ids"):
unique_ids = np.sort(self._plex.getLabelIdIS("boundary_ids").indices)
else:
unique_ids = None
return _Facets(self, 0, "exterior", None, None,
unique_markers=unique_ids)
@utils.cached_property
def interior_facets(self):
if self._plex.getStratumSize("interior_facets", 1) > 0:
# Compute the facet_numbering
# Order interior facets by OP2 entity class
interior_facets, interior_facet_classes = \
dmplex.get_facets_by_class(self._plex, "interior_facets")
interior_local_facet_number, interior_facet_cell = \
dmplex.facet_numbering(self._plex, "interior",
interior_facets,
self._cell_numbering,
self.cell_closure)
return _Facets(self, interior_facet_classes, "interior",
interior_facet_cell, interior_local_facet_number)
else:
return _Facets(self, 0, "interior", None, None)
def make_cell_node_list(self, global_numbering, entity_dofs):
"""Builds the DoF mapping.
:arg global_numbering: Section describing the global DoF numbering
:arg fiat_element: The FIAT element for the cell
"""
return dmplex.get_cell_nodes(global_numbering,
self.cell_closure,
entity_dofs)
def make_dofs_per_plex_entity(self, entity_dofs):
"""Returns the number of DoFs per plex entity for each stratum,
i.e. [#dofs / plex vertices, #dofs / plex edges, ...].
:arg entity_dofs: FIAT element entity DoFs
"""
return [len(entity_dofs[d][0]) for d in sorted(entity_dofs)]
def make_offset(self, entity_dofs, ndofs):
"""Returns None (only for extruded use)."""
return None
def _order_data_by_cell_index(self, column_list, cell_data):
return cell_data[column_list]
def cell_orientations(self):
"""Return the orientation of each cell in the mesh.
Use :func:`init_cell_orientations` on the mesh *geometry* to initialise."""
if not hasattr(self, '_cell_orientations'):
raise RuntimeError("No cell orientations found, did you forget to call init_cell_orientations?")
return self._cell_orientations
def num_cells(self):
cStart, cEnd = self._plex.getHeightStratum(0)
return cEnd - cStart
def num_facets(self):
fStart, fEnd = self._plex.getHeightStratum(1)
return fEnd - fStart
def num_faces(self):
fStart, fEnd = self._plex.getDepthStratum(2)
return fEnd - fStart
def num_edges(self):
eStart, eEnd = self._plex.getDepthStratum(1)
return eEnd - eStart
def num_vertices(self):
vStart, vEnd = self._plex.getDepthStratum(0)
return vEnd - vStart
def num_entities(self, d):
eStart, eEnd = self._plex.getDepthStratum(d)
return eEnd - eStart
def size(self, d):
return self.num_entities(d)
def cell_dimension(self):
"""Returns the cell dimension."""
return self.ufl_cell().topological_dimension()
def facet_dimension(self):
"""Returns the facet dimension."""
# Facets have co-dimension 1
return self.ufl_cell().topological_dimension() - 1
@utils.cached_property
def cell_set(self):
size = list(self._entity_classes[self.cell_dimension(), :])
return op2.Set(size, "%s_cells" % self.name)
class ExtrudedMeshTopology(MeshTopology):
"""Representation of an extruded mesh topology."""
def __init__(self, mesh, layers):
"""Build an extruded mesh topology from an input mesh topology
:arg mesh: the unstructured base mesh topology
:arg layers: number of extruded cell layers in the "vertical"
direction.
"""
from firedrake.citations import Citations
Citations().register("McRae2014")
# A cache of function spaces that have been built on this mesh
self._cache = {}
mesh.init()
self._base_mesh = mesh
if layers < 1:
raise RuntimeError("Must have at least one layer of extruded cells (not %d)" % layers)
# All internal logic works with layers of base mesh (not layers of cells)
self._layers = layers + 1
self._ufl_cell = ufl.OuterProductCell(mesh.ufl_cell(), ufl.interval)
# TODO: These attributes are copied so that FunctionSpaceBase can
# access them directly. Eventually we would want a better refactoring
# of responsibilities between mesh and function space.
self._plex = mesh._plex
self._plex_renumbering = mesh._plex_renumbering
self._entity_classes = mesh._entity_classes
@property
def name(self):
return self._base_mesh.name
@property
def cell_closure(self):
"""2D array of ordered cell closures
Each row contains ordered cell entities for a cell, one row per cell.
"""
return self._base_mesh.cell_closure
@utils.cached_property
def exterior_facets(self):
exterior_facets = self._base_mesh.exterior_facets
return _Facets(self, exterior_facets.classes,
"exterior",
exterior_facets.facet_cell,
exterior_facets.local_facet_number,
exterior_facets.markers,
unique_markers=exterior_facets.unique_markers)
@utils.cached_property
def interior_facets(self):
interior_facets = self._base_mesh.interior_facets
return _Facets(self, interior_facets.classes,
"interior",
interior_facets.facet_cell,
interior_facets.local_facet_number)
def make_cell_node_list(self, global_numbering, entity_dofs):
"""Builds the DoF mapping.
:arg global_numbering: Section describing the global DoF numbering
:arg fiat_element: The FIAT element for the cell
"""
flat_entity_dofs = {}
for b, v in entity_dofs:
# v in [0, 1]. Only look at the ones, then grab the data from zeros.
if v == 0:
continue
flat_entity_dofs[b] = {}
for i in entity_dofs[(b, v)]:
# This line is fairly magic.
# It works because an interval has two points.
# We pick up the DoFs from the bottom point,
# then the DoFs from the interior of the interval,
# then finally the DoFs from the top point.
flat_entity_dofs[b][i] = \
entity_dofs[(b, 0)][2*i] + entity_dofs[(b, 1)][i] + entity_dofs[(b, 0)][2*i+1]
return dmplex.get_cell_nodes(global_numbering,
self.cell_closure,
flat_entity_dofs)
def make_dofs_per_plex_entity(self, entity_dofs):
"""Returns the number of DoFs per plex entity for each stratum,
i.e. [#dofs / plex vertices, #dofs / plex edges, ...].
:arg entity_dofs: FIAT element entity DoFs
"""
dofs_per_entity = [0] * (1 + self._base_mesh.cell_dimension())
for (b, v), entities in entity_dofs.iteritems():
dofs_per_entity[b] += (self.layers - v) * len(entities[0])
return dofs_per_entity
def make_offset(self, entity_dofs, ndofs):
"""Returns the offset between the neighbouring cells of a
column for each DoF.
:arg entity_dofs: FIAT element entity DoFs
:arg ndofs: number of DoFs in the FIAT element
"""
entity_offset = [0] * (1 + self._base_mesh.cell_dimension())
for (b, v), entities in entity_dofs.iteritems():
entity_offset[b] += len(entities[0])
dof_offset = np.zeros(ndofs, dtype=np.int32)
for (b, v), entities in entity_dofs.iteritems():
for dof_indices in entities.itervalues():
for i in dof_indices:
dof_offset[i] = entity_offset[b]
return dof_offset
@property
def layers(self):
"""Return the number of layers of the extruded mesh
represented by the number of occurences of the base mesh."""
return self._layers
def cell_dimension(self):
"""Returns the cell dimension."""
return (self._base_mesh.cell_dimension(), 1)
def facet_dimension(self):
"""Returns the facet dimension.
.. note::
This only returns the dimension of the "side" (vertical) facets,
not the "top" or "bottom" (horizontal) facets.
"""
return (self._base_mesh.facet_dimension(), 1)
@utils.cached_property
def cell_set(self):
return op2.ExtrudedSet(self._base_mesh.cell_set, layers=self.layers)
def _order_data_by_cell_index(self, column_list, cell_data):
cell_list = []
for col in column_list:
cell_list += range(col, col + (self.layers - 1))
return cell_data[cell_list]
class MeshGeometry(ufl.Mesh):
"""A representation of mesh topology and geometry."""
def __new__(cls, element):
"""Create mesh geometry object."""
utils._init()
mesh = super(MeshGeometry, cls).__new__(cls)
mesh.uid = utils._new_uid()
assert isinstance(element, ufl.FiniteElementBase)
ufl.Mesh.__init__(mesh, element, ufl_id=mesh.uid)
return mesh
def __init__(self, coordinates):
"""Initialise a mesh geometry from coordinates.
:arg coordinates: a coordinateless function containing the coordinates
"""
# Direct link to topology
self._topology = coordinates.function_space().mesh()
# Cache mesh object on the coordinateless coordinates function
coordinates._as_mesh_geometry = weakref.ref(self)
self._coordinates = coordinates
def init(self):
"""Finish the initialisation of the mesh. Most of the time
this is carried out automatically, however, in some cases (for
example accessing a property of the mesh directly after
constructing it) you need to call this manually."""
if hasattr(self, '_callback'):
self._callback(self)
@property
def topology(self):
"""The underlying mesh topology object."""
return self._topology
@property
def topological(self):
"""Alias of topology.
This is to ensure consistent naming for some multigrid codes."""
return self._topology
@utils.cached_property
def _coordinates_function(self):
"""The :class:`.Function` containing the coordinates of this mesh."""
import firedrake.functionspace as functionspace
import firedrake.function as function
self.init()
coordinates_fs = self._coordinates.function_space()
V = functionspace.WithGeometry(coordinates_fs, self)
f = function.Function(V, val=self._coordinates)
return f
@property
def coordinates(self):
"""The :class:`.Function` containing the coordinates of this mesh."""
return self._coordinates_function
@coordinates.setter
def coordinates(self, value):
message = """Cannot re-assign the coordinates.
You are free to change the coordinate values, but if you need a
different coordinate function space, use Mesh(f) to create a new mesh
object whose coordinates are f's values. (This will not copy the
values from f.)"""
raise AttributeError(message)
@utils.cached_property
def spatial_index(self):
"""Spatial index to quickly find which cell contains a given point."""
from firedrake import function, functionspace
from firedrake.parloops import par_loop, READ, RW
gdim = self.ufl_cell().geometric_dimension()
if gdim <= 1:
info_red("libspatialindex does not support 1-dimension, falling back on brute force.")
return None
# Calculate the bounding boxes for all cells by running a kernel
V = functionspace.VectorFunctionSpace(self, "DG", 0, dim=gdim)
coords_min = function.Function(V)
coords_max = function.Function(V)
coords_min.dat.data.fill(np.inf)
coords_max.dat.data.fill(-np.inf)
kernel = """
for (int d = 0; d < gdim; d++) {
for (int i = 0; i < nodes_per_cell; i++) {
f_min[0][d] = fmin(f_min[0][d], f[i][d]);
f_max[0][d] = fmax(f_max[0][d], f[i][d]);
}
}
"""
cell_node_list = self.coordinates.function_space().cell_node_list
nodes_per_cell = len(cell_node_list[0])
kernel = kernel.replace("gdim", str(gdim))
kernel = kernel.replace("nodes_per_cell", str(nodes_per_cell))
par_loop(kernel, ufl.dx, {'f': (self.coordinates, READ),
'f_min': (coords_min, RW),
'f_max': (coords_max, RW)})
# Reorder bounding boxes according to the cell indices we use
column_list = V.cell_node_list.reshape(-1)
coords_min = self._order_data_by_cell_index(column_list, coords_min.dat.data_ro_with_halos)
coords_max = self._order_data_by_cell_index(column_list, coords_max.dat.data_ro_with_halos)
# Build spatial index
return spatialindex.from_regions(coords_min, coords_max)
def locate_cell(self, x):
"""Locate cell containg given point.
:arg x: point coordinates
:returns: cell number (int), or None (if the point is not in the domain)
"""
x = np.asarray(x, dtype=np.float)
cell = self._c_locator(self.coordinates._ctypes,
x.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))
if cell == -1:
return None
else:
return cell
@utils.cached_property
def _c_locator(self):
from pyop2 import compilation
import firedrake.function as function
import firedrake.pointquery_utils as pq_utils
src = pq_utils.src_locate_cell(self)
src += """
extern "C" int locator(struct Function *f, double *x)
{
struct ReferenceCoords reference_coords;
return locate_cell(f, x, %(geometric_dimension)d, &to_reference_coords, &reference_coords);
}
""" % dict(geometric_dimension=self.geometric_dimension())
locator = compilation.load(src, "cpp", "locator",
cppargs=["-I%s" % os.path.dirname(__file__)],
ldargs=["-lspatialindex"])
locator.argtypes = [ctypes.POINTER(function._CFunction),
ctypes.POINTER(ctypes.c_double)]
locator.restype = ctypes.c_int
return locator
def init_cell_orientations(self, expr):
"""Compute and initialise :attr:`cell_orientations` relative to a specified orientation.
:arg expr: an :class:`.Expression` evaluated to produce a
reference normal direction.
"""
import firedrake.function as function
import firedrake.functionspace as functionspace
if expr.value_shape()[0] != 3:
raise NotImplementedError('Only implemented for 3-vectors')
if self.ufl_cell() not in (ufl.Cell('triangle', 3), ufl.Cell("quadrilateral", 3), ufl.OuterProductCell(ufl.Cell('interval'), ufl.Cell('interval'), gdim=3)):
raise NotImplementedError('Only implemented for triangles and quadrilaterals embedded in 3d')
if hasattr(self.topology, '_cell_orientations'):
raise RuntimeError("init_cell_orientations already called, did you mean to do so again?")
v0 = lambda x: ast.Symbol("v0", (x,))
v1 = lambda x: ast.Symbol("v1", (x,))
n = lambda x: ast.Symbol("n", (x,))
x = lambda x: ast.Symbol("x", (x,))
coords = lambda x, y: ast.Symbol("coords", (x, y))
body = []
body += [ast.Decl("double", v(3)) for v in [v0, v1, n, x]]
body.append(ast.Decl("double", "dot"))
body.append(ast.Assign("dot", 0.0))
body.append(ast.Decl("int", "i"))
# if triangle, use v0 = x1 - x0, v1 = x2 - x0
# otherwise, for the various quads, use v0 = x2 - x0, v1 = x1 - x0
# recall reference element ordering:
# triangle: 2 quad: 1 3
# 0 1 0 2
if self.ufl_cell() == ufl.Cell('triangle', 3):
body.append(ast.For(ast.Assign("i", 0), ast.Less("i", 3), ast.Incr("i", 1),
[ast.Assign(v0("i"), ast.Sub(coords(1, "i"), coords(0, "i"))),
ast.Assign(v1("i"), ast.Sub(coords(2, "i"), coords(0, "i"))),
ast.Assign(x("i"), 0.0)]))
else:
body.append(ast.For(ast.Assign("i", 0), ast.Less("i", 3), ast.Incr("i", 1),
[ast.Assign(v0("i"), ast.Sub(coords(2, "i"), coords(0, "i"))),
ast.Assign(v1("i"), ast.Sub(coords(1, "i"), coords(0, "i"))),
ast.Assign(x("i"), 0.0)]))
# n = v0 x v1
body.append(ast.Assign(n(0), ast.Sub(ast.Prod(v0(1), v1(2)), ast.Prod(v0(2), v1(1)))))
body.append(ast.Assign(n(1), ast.Sub(ast.Prod(v0(2), v1(0)), ast.Prod(v0(0), v1(2)))))
body.append(ast.Assign(n(2), ast.Sub(ast.Prod(v0(0), v1(1)), ast.Prod(v0(1), v1(0)))))
body.append(ast.For(ast.Assign("i", 0), ast.Less("i", 3), ast.Incr("i", 1),
[ast.Incr(x(j), coords("i", j)) for j in range(3)]))
body.extend([ast.FlatBlock("dot += (%(x)s) * n[%(i)d];\n" % {"x": x_, "i": i})
for i, x_ in enumerate(expr.code)])
body.append(ast.Assign("orientation[0][0]", ast.Ternary(ast.Less("dot", 0), 1, 0)))
kernel = op2.Kernel(ast.FunDecl("void", "cell_orientations",
[ast.Decl("int**", "orientation"),
ast.Decl("double**", "coords")],
ast.Block(body)),
"cell_orientations")
# Build the cell orientations as a DG0 field (so that we can
# pass it in for facet integrals and the like)
fs = functionspace.FunctionSpace(self, 'DG', 0)
cell_orientations = function.Function(fs, name="cell_orientations", dtype=np.int32)
op2.par_loop(kernel, self.cell_set,
cell_orientations.dat(op2.WRITE, cell_orientations.cell_node_map()),
self.coordinates.dat(op2.READ, self.coordinates.cell_node_map()))
self.topology._cell_orientations = cell_orientations
def __getattr__(self, name):
return getattr(self._topology, name)
def make_mesh_from_coordinates(coordinates):
"""Given a coordinate field build a new mesh, using said coordinate field.
:arg coordinates: A :class:`~.Function`.
"""
import firedrake.functionspace as functionspace
from firedrake.ufl_expr import reconstruct_element
if hasattr(coordinates, '_as_mesh_geometry'):
mesh = coordinates._as_mesh_geometry()
if mesh is not None:
return mesh
coordinates_fs = coordinates.function_space()
if not isinstance(coordinates_fs, functionspace.VectorFunctionSpace):
raise ValueError("Coordinates must have a VectorFunctionSpace.")
assert coordinates_fs.mesh().ufl_cell().topological_dimension() <= coordinates_fs.dim
# Build coordinate element
element = coordinates.ufl_element()
cell = element.cell().reconstruct(geometric_dimension=coordinates_fs.dim)
element = reconstruct_element(element, cell=cell)
mesh = MeshGeometry.__new__(MeshGeometry, element)
mesh.__init__(coordinates)
return mesh
@timed_function("Build mesh")
@profile
def Mesh(meshfile, **kwargs):
"""Construct a mesh object.
Meshes may either be created by reading from a mesh file, or by
providing a PETSc DMPlex object defining the mesh topology.
:param meshfile: Mesh file name (or DMPlex object) defining
mesh topology. See below for details on supported mesh
formats.
:param dim: optional specification of the geometric dimension