File size: 10,485 Bytes
66c9c8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import warp as wp
import numpy as np

from warp.fem.types import ElementIndex, Coords
from warp.fem.polynomial import Polynomial, is_closed
from warp.fem.geometry import Grid3D
from warp.fem import cache

from .topology import SpaceTopology, DiscontinuousSpaceTopologyMixin, forward_base_topology
from .basis_space import ShapeBasisSpace, TraceBasisSpace

from .shape import ShapeFunction, ConstantShapeFunction
from .shape.cube_shape_function import (
    CubeTripolynomialShapeFunctions,
    CubeSerendipityShapeFunctions,
    CubeNonConformingPolynomialShapeFunctions,
)


class Grid3DSpaceTopology(SpaceTopology):
    def __init__(self, grid: Grid3D, shape: ShapeFunction):
        super().__init__(grid, shape.NODES_PER_ELEMENT)
        self._shape = shape

    @wp.func
    def _vertex_coords(vidx_in_cell: int):
        x = vidx_in_cell // 4
        y = (vidx_in_cell - 4 * x) // 2
        z = vidx_in_cell - 4 * x - 2 * y
        return wp.vec3i(x, y, z)

    @wp.func
    def _vertex_index(cell_arg: Grid3D.CellArg, cell_index: ElementIndex, vidx_in_cell: int):
        res = cell_arg.res
        strides = wp.vec2i((res[1] + 1) * (res[2] + 1), res[2] + 1)

        corner = Grid3D.get_cell(res, cell_index) + Grid3DSpaceTopology._vertex_coords(vidx_in_cell)
        return Grid3D._from_3d_index(strides, corner)


class Grid3DDiscontinuousSpaceTopology(
    DiscontinuousSpaceTopologyMixin,
    Grid3DSpaceTopology,
):
    pass


class Grid3DBasisSpace(ShapeBasisSpace):
    def __init__(self, topology: Grid3DSpaceTopology, shape: ShapeFunction):
        super().__init__(topology, shape)

        self._grid: Grid3D = topology.geometry


class Grid3DPiecewiseConstantBasis(Grid3DBasisSpace):
    def __init__(self, grid: Grid3D):
        shape = ConstantShapeFunction(grid.reference_cell(), space_dimension=3)
        topology = Grid3DDiscontinuousSpaceTopology(grid, shape)
        super().__init__(shape=shape, topology=topology)

        if isinstance(grid, Grid3D):
            self.node_grid = self._node_grid

    def _node_grid(self):
        X = (np.arange(0, self.geometry.res[0], dtype=float) + 0.5) * self._grid.cell_size[0] + self._grid.bounds_lo[0]
        Y = (np.arange(0, self.geometry.res[1], dtype=float) + 0.5) * self._grid.cell_size[1] + self._grid.bounds_lo[1]
        Z = (np.arange(0, self.geometry.res[2], dtype=float) + 0.5) * self._grid.cell_size[2] + self._grid.bounds_lo[2]
        return np.meshgrid(X, Y, Z, indexing="ij")

    class Trace(TraceBasisSpace):
        @wp.func
        def _node_coords_in_element(
            side_arg: Grid3D.SideArg,
            basis_arg: Grid3DBasisSpace.BasisArg,
            element_index: ElementIndex,
            node_index_in_element: int,
        ):
            return Coords(0.5, 0.5, 0.0)

        def make_node_coords_in_element(self):
            return self._node_coords_in_element

    def trace(self):
        return Grid3DPiecewiseConstantBasis.Trace(self)


class GridTripolynomialSpaceTopology(Grid3DSpaceTopology):
    def __init__(self, grid: Grid3D, shape: CubeTripolynomialShapeFunctions):
        super().__init__(grid, shape)

        self.element_node_index = self._make_element_node_index()

    def node_count(self) -> int:
        return (
            (self.geometry.res[0] * self._shape.ORDER + 1)
            * (self.geometry.res[1] * self._shape.ORDER + 1)
            * (self.geometry.res[2] * self._shape.ORDER + 1)
        )

    def _make_element_node_index(self):
        ORDER = self._shape.ORDER

        @cache.dynamic_func(suffix=self.name)
        def element_node_index(
            cell_arg: Grid3D.CellArg,
            topo_arg: Grid3DSpaceTopology.TopologyArg,
            element_index: ElementIndex,
            node_index_in_elt: int,
        ):
            res = cell_arg.res
            cell = Grid3D.get_cell(res, element_index)

            node_i, node_j, node_k = self._shape._node_ijk(node_index_in_elt)

            node_x = ORDER * cell[0] + node_i
            node_y = ORDER * cell[1] + node_j
            node_z = ORDER * cell[2] + node_k

            node_pitch_y = (res[2] * ORDER) + 1
            node_pitch_x = node_pitch_y * ((res[1] * ORDER) + 1)
            node_index = node_pitch_x * node_x + node_pitch_y * node_y + node_z

            return node_index

        return element_node_index


class GridTripolynomialBasisSpace(Grid3DBasisSpace):
    def __init__(
        self,
        grid: Grid3D,
        degree: int,
        family: Polynomial,
    ):
        if family is None:
            family = Polynomial.LOBATTO_GAUSS_LEGENDRE

        if not is_closed(family):
            raise ValueError("A closed polynomial family is required to define a continuous function space")

        shape = CubeTripolynomialShapeFunctions(degree, family=family)
        topology = forward_base_topology(GridTripolynomialSpaceTopology, grid, shape)

        super().__init__(topology, shape)

        if isinstance(grid, Grid3D):
            self.node_grid = self._node_grid

    def _node_grid(self):
        res = self._grid.res

        cell_coords = np.array(self._shape.LOBATTO_COORDS)[:-1]

        grid_coords_x = np.repeat(np.arange(0, res[0], dtype=float), len(cell_coords)) + np.tile(
            cell_coords, reps=res[0]
        )
        grid_coords_x = np.append(grid_coords_x, res[0])
        X = grid_coords_x * self._grid.cell_size[0] + self._grid.origin[0]

        grid_coords_y = np.repeat(np.arange(0, res[1], dtype=float), len(cell_coords)) + np.tile(
            cell_coords, reps=res[1]
        )
        grid_coords_y = np.append(grid_coords_y, res[1])
        Y = grid_coords_y * self._grid.cell_size[1] + self._grid.origin[1]

        grid_coords_z = np.repeat(np.arange(0, res[2], dtype=float), len(cell_coords)) + np.tile(
            cell_coords, reps=res[2]
        )
        grid_coords_z = np.append(grid_coords_z, res[2])
        Z = grid_coords_z * self._grid.cell_size[2] + self._grid.origin[2]

        return np.meshgrid(X, Y, Z, indexing="ij")


class GridDGTripolynomialBasisSpace(Grid3DBasisSpace):
    def __init__(
        self,
        grid: Grid3D,
        degree: int,
        family: Polynomial,
    ):
        if family is None:
            family = Polynomial.LOBATTO_GAUSS_LEGENDRE

        shape = CubeTripolynomialShapeFunctions(degree, family=family)
        topology = Grid3DDiscontinuousSpaceTopology(grid, shape)

        super().__init__(shape=shape, topology=topology)

    def node_grid(self):
        res = self._grid.res

        cell_coords = np.array(self._shape.LOBATTO_COORDS)

        grid_coords_x = np.repeat(np.arange(0, res[0], dtype=float), len(cell_coords)) + np.tile(
            cell_coords, reps=res[0]
        )
        X = grid_coords_x * self._grid.cell_size[0] + self._grid.origin[0]

        grid_coords_y = np.repeat(np.arange(0, res[1], dtype=float), len(cell_coords)) + np.tile(
            cell_coords, reps=res[1]
        )
        Y = grid_coords_y * self._grid.cell_size[1] + self._grid.origin[1]

        grid_coords_z = np.repeat(np.arange(0, res[2], dtype=float), len(cell_coords)) + np.tile(
            cell_coords, reps=res[2]
        )
        Z = grid_coords_z * self._grid.cell_size[2] + self._grid.origin[2]

        return np.meshgrid(X, Y, Z, indexing="ij")


class Grid3DSerendipitySpaceTopology(Grid3DSpaceTopology):
    def __init__(self, grid: Grid3D, shape: CubeSerendipityShapeFunctions):
        super().__init__(grid, shape)

        self.element_node_index = self._make_element_node_index()

    def node_count(self) -> int:
        return self.geometry.vertex_count() + (self._shape.ORDER - 1) * self.geometry.edge_count()

    def _make_element_node_index(self):
        ORDER = self._shape.ORDER

        @cache.dynamic_func(suffix=self.name)
        def element_node_index(
            cell_arg: Grid3D.CellArg,
            topo_arg: Grid3DSpaceTopology.TopologyArg,
            element_index: ElementIndex,
            node_index_in_elt: int,
        ):
            res = cell_arg.res
            cell = Grid3D.get_cell(res, element_index)

            node_type, type_index = self._shape.node_type_and_type_index(node_index_in_elt)

            if node_type == CubeSerendipityShapeFunctions.VERTEX:
                return Grid3DSpaceTopology._vertex_index(cell_arg, element_index, type_index)

            axis = CubeSerendipityShapeFunctions._edge_axis(node_type)
            node_all = CubeSerendipityShapeFunctions._edge_coords(type_index)

            res = cell_arg.res

            edge_index = 0
            if axis > 0:
                edge_index += (res[1] + 1) * (res[2] + 1) * res[0]
            if axis > 1:
                edge_index += (res[0] + 1) * (res[2] + 1) * res[1]

            res_loc = Grid3D._world_to_local(axis, res)
            cell_loc = Grid3D._world_to_local(axis, cell)

            edge_index += (res_loc[1] + 1) * (res_loc[2] + 1) * cell_loc[0]
            edge_index += (res_loc[2] + 1) * (cell_loc[1] + node_all[1])
            edge_index += cell_loc[2] + node_all[2]

            vertex_count = (res[0] + 1) * (res[1] + 1) * (res[2] + 1)

            return vertex_count + (ORDER - 1) * edge_index + (node_all[0] - 1)

        return element_node_index


class Grid3DSerendipityBasisSpace(Grid3DBasisSpace):
    def __init__(
        self,
        grid: Grid3D,
        degree: int,
        family: Polynomial,
    ):
        if family is None:
            family = Polynomial.LOBATTO_GAUSS_LEGENDRE

        shape = CubeSerendipityShapeFunctions(degree, family=family)
        topology = forward_base_topology(Grid3DSerendipitySpaceTopology, grid, shape=shape)

        super().__init__(topology=topology, shape=shape)


class Grid3DDGSerendipityBasisSpace(Grid3DBasisSpace):
    def __init__(
        self,
        grid: Grid3D,
        degree: int,
        family: Polynomial,
    ):
        if family is None:
            family = Polynomial.LOBATTO_GAUSS_LEGENDRE

        shape = CubeSerendipityShapeFunctions(degree, family=family)
        topology = Grid3DDiscontinuousSpaceTopology(grid, shape=shape)

        super().__init__(topology=topology, shape=shape)


class Grid3DDGPolynomialBasisSpace(Grid3DBasisSpace):
    def __init__(
        self,
        grid: Grid3D,
        degree: int,
    ):
        shape = CubeNonConformingPolynomialShapeFunctions(degree)
        topology = Grid3DDiscontinuousSpaceTopology(grid, shape=shape)

        super().__init__(topology=topology, shape=shape)