aboutsummaryrefslogtreecommitdiff
path: root/simulation.py
blob: e2174eefdd446a41276809ba1e034bcb34b3df71 (plain)
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
import pyopencl as cl
mf = cl.mem_flags

import numpy
from utility.ndindex import ndindex

import sympy

from mako.template import Template
from mako.lookup import TemplateLookup

from pathlib import Path

from pyopencl.tools import get_gl_sharing_context_properties

class Geometry:
    def __init__(self, size_x, size_y, size_z = 1):
        self.size_x = size_x
        self.size_y = size_y
        self.size_z = size_z
        self.volume = size_x * size_y * size_z

    def inner_cells(self):
        for idx in numpy.ndindex(self.inner_size()):
            yield tuple(map(lambda i: i + 1, idx))

    def size(self):
        if self.size_z == 1:
            return (self.size_x, self.size_y)
        else:
            return (self.size_x, self.size_y, self.size_z)

    def inner_size(self):
        if self.size_z == 1:
            return (self.size_x-2, self.size_y-2)
        else:
            return (self.size_x-2, self.size_y-2, self.size_z-2)

    def wireframe(self):
        if self.size_z > 1:
            return ([
                [0          , 0          , 0          ],
                [self.size_x, 0          , 0          ],
                [self.size_x, self.size_y, 0          ],
                [0          , self.size_y, 0          ],
                [0          , 0          , self.size_z],
                [self.size_x, 0          , self.size_z],
                [self.size_x, self.size_y, self.size_z],
                [0          , self.size_y, self.size_z]
            ],
            [
                (0,1), (1,2), (2,3), (3,0),
                (4,5), (5,6), (6,7), (7,4),
                (0,4), (1,5), (2,6), (3,7)
            ])
        else:
            return ([
                [0          , 0          ],
                [self.size_x, 0          ],
                [self.size_x, self.size_y],
                [0          , self.size_y],
            ],
            [
                (0,1), (1,2), (2,3), (3,0)
            ])

def pad(n, m):
    return (n // m + min(1,n % m)) * m

class Grid:
    def __init__(self, geometry, padding = None):
        if padding == None:
            self.size_x = geometry.size_x
            self.size_y = geometry.size_y
            self.size_z = geometry.size_z
        else:
            self.size_x = pad(geometry.size_x, padding[0])
            self.size_y = pad(geometry.size_y, padding[1])
            if geometry.size_z == 1:
                self.size_z = geometry.size_z
            else:
                self.size_z = pad(geometry.size_z, padding[2])

        self.volume = self.size_x * self.size_y * self.size_z

    def size(self):
        if self.size_z == 1:
            return (self.size_x, self.size_y)
        else:
            return (self.size_x, self.size_y, self.size_z)

class Memory:
    def __init__(self, descriptor, grid, context, float_type, align, opengl):
        self.descriptor = descriptor
        self.context    = context
        self.float_type = float_type

        if align:
            self.size_x = pad(grid.size_x, {
                numpy.float32: 32,
                numpy.float64: 16
            }.get(float_type, None))
        else:
            self.size_x = grid.size_x

        self.size_y = grid.size_y
        self.size_z = grid.size_z

        self.volume = self.size_x * self.size_y * self.size_z

        self.pop_size     = descriptor.q     * self.volume * self.float_type(0).nbytes
        self.moments_size = (descriptor.d+1) * self.volume * self.float_type(0).nbytes

        self.cl_pop_a = cl.Buffer(self.context, mf.READ_WRITE, size=self.pop_size)
        self.cl_pop_b = cl.Buffer(self.context, mf.READ_WRITE, size=self.pop_size)

        self.cl_moments  = cl.Buffer(self.context, mf.WRITE_ONLY, size=self.moments_size)
        self.cl_material = cl.Buffer(self.context, mf.READ_WRITE, size=self.volume * numpy.int32(0).nbytes)

    def gid(self, x, y, z = 0):
        return z * (self.size_x*self.size_y) + y * self.size_x + x;

    def size(self):
        if self.size_z == 1:
            return (self.size_x, self.size_y)
        else:
            return (self.size_x, self.size_y, self.size_z)

    def cells(self):
        return ndindex(self.size(), order='F')

class Lattice:
    def __init__(self,
        descriptor, geometry, moments, collide,
        pop_eq_src = '', boundary_src = '',
        platform = 0, precision = 'single', layout = None, padding = None, align = False, opengl = False
    ):
        self.descriptor = descriptor
        self.geometry   = geometry
        self.grid       = Grid(self.geometry, padding)

        self.time = 0

        self.float_type = {
            'single': (numpy.float32, 'float'),
            'double': (numpy.float64, 'double'),
        }.get(precision, None)

        self.mako_lookup = TemplateLookup(directories = [
            Path(__file__).parent
        ])

        self.platform = cl.get_platforms()[platform]

        if opengl:
            try:
                self.context = cl.Context(
                    properties = [
                        (cl.context_properties.PLATFORM, self.platform)
                    ] + get_gl_sharing_context_properties())
            except:
                self.context = cl.Context(
                    properties = [
                        (cl.context_properties.PLATFORM, self.platform)
                    ] + get_gl_sharing_context_properties(),
                    devices = [ self.platform.get_devices()[0] ])
        else:
            self.context = cl.Context(
                properties=[(cl.context_properties.PLATFORM, self.platform)])

        self.queue = cl.CommandQueue(self.context)

        self.memory = Memory(self.descriptor, self.grid, self.context, self.float_type[0], align, opengl)
        self.tick = False

        self.moments = moments
        self.collide = collide

        self.pop_eq_src = pop_eq_src
        self.boundary_src = boundary_src

        self.layout = layout

        self.compiler_args = {
            'single': '-cl-single-precision-constant -cl-fast-relaxed-math',
            'double': '-cl-fast-relaxed-math'
        }.get(precision, None)

        self.build_kernel()

        self.program.equilibrilize(
            self.queue, self.grid.size(), self.layout, self.memory.cl_pop_a, self.memory.cl_pop_b).wait()

        self.material = numpy.ndarray(shape=(self.memory.volume, 1), dtype=numpy.int32)

    def apply_material_map(self, material_map):
        for primitive, material in material_map:
            if callable(primitive):
                self.material[[primitive(*idx) for idx in self.memory.cells()]] = material
            else:
                indicator = primitive.indicator()
                self.material[[indicator(*idx) for idx in self.memory.cells()]] = material

    def setup_channel_with_sdf_obstacle(self, sdf_src):
        sdf_kernel_src = Template(
            filename = 'template/sdf.cl.mako',
            lookup   = self.mako_lookup
        ).render(
            geometry = self.memory,
            sdf_src  = sdf_src
        )

        sdf_program = cl.Program(self.context, sdf_kernel_src).build(self.compiler_args)
        sdf_program.setup_channel_with_sdf_obstacle(self.queue, self.memory.size(), None, self.memory.cl_material)
        cl.enqueue_copy(self.queue, self.material, self.memory.cl_material).wait()

    def sync_material(self):
        cl.enqueue_copy(self.queue, self.memory.cl_material, self.material).wait()

    def build_kernel(self):
        program_src = Template(filename = str(Path(__file__).parent/'template/kernel.mako')).render(
            descriptor = self.descriptor,
            geometry   = self.geometry,
            memory     = self.memory,

            moments_subexpr    = self.moments[0],
            moments_assignment = self.moments[1],
            collide_subexpr    = self.collide[0],
            collide_assignment = self.collide[1],

            float_type = self.float_type[1],

            pop_eq_src = Template(self.pop_eq_src).render(
                descriptor = self.descriptor,
                geometry   = self.geometry,
                memory     = self.memory,
                float_type = self.float_type[1],
            ),
            boundary_src = Template(self.boundary_src).render(
                descriptor = self.descriptor,
                geometry   = self.geometry,
                memory     = self.memory,
                float_type = self.float_type[1],
            ),

            ccode = sympy.ccode
        )
        self.program = cl.Program(self.context, program_src).build(self.compiler_args)

    def evolve(self):
        self.time += 1
        if self.tick:
            self.tick = False
            self.program.collide_and_stream(
                self.queue, self.grid.size(), self.layout