From 63163ebbbced363fde788c560b479569470705bd Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 15 Jun 2019 21:39:42 +0200 Subject: Split descriptors and symbolic formulation --- implosion.py | 9 ++-- lbm.py | 137 -------------------------------------------------- ldc_2d.py | 9 ++-- ldc_3d.py | 9 ++-- simulation.py | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ symbolic/D2Q9.py | 41 --------------- symbolic/D3Q19.py | 41 --------------- symbolic/generator.py | 46 +++++++++++++++++ 8 files changed, 201 insertions(+), 228 deletions(-) delete mode 100644 lbm.py create mode 100644 simulation.py create mode 100644 symbolic/generator.py diff --git a/implosion.py b/implosion.py index 05f3644..2ba831f 100644 --- a/implosion.py +++ b/implosion.py @@ -5,7 +5,8 @@ import matplotlib import matplotlib.pyplot as plt matplotlib.use('AGG') -from lbm import Lattice, Geometry +from simulation import Lattice, Geometry +from symbolic.generator import LBM import symbolic.D2Q9 as D2Q9 @@ -58,12 +59,14 @@ moments = [] print("Initializing simulation...\n") +lbm = LBM(D2Q9) + lattice = Lattice( descriptor = D2Q9, geometry = Geometry(1024, 1024), - moments = D2Q9.moments(optimize = False), - collide = D2Q9.bgk(tau = 0.8), + moments = lbm.moments(optimize = False), + collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = 0.8), pop_eq_src = pop_eq, boundary_src = boundary) diff --git a/lbm.py b/lbm.py deleted file mode 100644 index ef05e4f..0000000 --- a/lbm.py +++ /dev/null @@ -1,137 +0,0 @@ -import pyopencl as cl -mf = cl.mem_flags - -import numpy -import sympy - -from mako.template import Template -from pathlib import Path - -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): - if self.size_z == 1: - for y in range(1,self.size_y-1): - for x in range(1,self.size_x-1): - yield x, y - else: - for z in range(1,self.size_z-1): - for y in range(1,self.size_y-1): - for x in range(1,self.size_x-1): - yield x, y, z - - def span(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_span(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) - - -class Lattice: - def __init__(self, descriptor, geometry, moments, collide, pop_eq_src = '', boundary_src = ''): - self.descriptor = descriptor - self.geometry = geometry - - self.moments = moments - self.collide = collide - - self.pop_eq_src = pop_eq_src - self.boundary_src = boundary_src - - self.platform = cl.get_platforms()[0] - self.context = cl.Context(properties=[(cl.context_properties.PLATFORM, self.platform)]) - self.queue = cl.CommandQueue(self.context) - - self.np_material = numpy.ndarray(shape=(self.geometry.volume, 1), dtype=numpy.int32) - - self.tick = True - - self.pop_size = descriptor.q * self.geometry.volume * numpy.float32(0).nbytes - self.moments_size = (descriptor.d+1) * self.geometry.volume * numpy.float32(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_ONLY | mf.USE_HOST_PTR, hostbuf=self.np_material) - - self.build_kernel() - - if descriptor.d == 2: - self.layout = (32,1) - elif descriptor.d == 3: - self.layout = (32,1,1) - - self.program.equilibrilize( - self.queue, self.geometry.span(), self.layout, self.cl_pop_a, self.cl_pop_b).wait() - - def idx(self, x, y, z = 0): - return z * (self.geometry.size_x*self.geometry.size_y) + y * self.geometry.size_x + x; - - def setup_geometry(self, material_at): - if self.descriptor.d == 2: - for x, y in self.geometry.inner_cells(): - self.np_material[self.idx(x,y)] = material_at(self.geometry, x, y) - elif self.descriptor.d == 3: - for x, y, z in self.geometry.inner_cells(): - self.np_material[self.idx(x,y,z)] = material_at(self.geometry, x, y, z) - - cl.enqueue_copy(self.queue, self.cl_material, self.np_material).wait(); - - def build_kernel(self): - program_src = Template(filename = str(Path(__file__).parent/'template/kernel.mako')).render( - descriptor = self.descriptor, - geometry = self.geometry, - - moments_subexpr = self.moments[0], - moments_assignment = self.moments[1], - collide_subexpr = self.collide[0], - collide_assignment = self.collide[1], - - pop_eq_src = Template(self.pop_eq_src).render( - descriptor = self.descriptor, - geometry = self.geometry - ), - boundary_src = Template(self.boundary_src).render( - descriptor = self.descriptor, - geometry = self.geometry - ), - - ccode = sympy.ccode - ) - self.program = cl.Program(self.context, program_src).build('-cl-single-precision-constant -cl-fast-relaxed-math') - - def evolve(self): - if self.tick: - self.tick = False - self.program.collide_and_stream( - self.queue, self.geometry.span(), self.layout, self.cl_pop_a, self.cl_pop_b, self.cl_material) - else: - self.tick = True - self.program.collide_and_stream( - self.queue, self.geometry.span(), self.layout, self.cl_pop_b, self.cl_pop_a, self.cl_material) - - def sync(self): - self.queue.finish() - - def get_moments(self): - moments = numpy.ndarray(shape=(self.descriptor.d+1, self.geometry.volume), dtype=numpy.float32) - if self.tick: - self.program.collect_moments( - self.queue, self.geometry.span(), self.layout, self.cl_pop_b, self.cl_moments) - else: - self.program.collect_moments( - self.queue, self.geometry.span(), self.layout, self.cl_pop_a, self.cl_moments) - cl.enqueue_copy(self.queue, moments, self.cl_moments).wait(); - return moments diff --git a/ldc_2d.py b/ldc_2d.py index 0aa1e71..979f7be 100644 --- a/ldc_2d.py +++ b/ldc_2d.py @@ -5,7 +5,8 @@ import matplotlib import matplotlib.pyplot as plt matplotlib.use('AGG') -from lbm import Lattice, Geometry +from simulation import Lattice, Geometry +from symbolic.generator import LBM import symbolic.D2Q9 as D2Q9 @@ -50,12 +51,14 @@ moments = [] print("Initializing simulation...\n") +lbm = LBM(D2Q9) + lattice = Lattice( descriptor = D2Q9, geometry = Geometry(256, 256), - moments = D2Q9.moments(optimize = False), - collide = D2Q9.bgk(tau = 0.52), + moments = lbm.moments(optimize = False), + collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = 0.52), boundary_src = boundary) diff --git a/ldc_3d.py b/ldc_3d.py index 62f820e..c008b06 100644 --- a/ldc_3d.py +++ b/ldc_3d.py @@ -5,7 +5,8 @@ import matplotlib import matplotlib.pyplot as plt matplotlib.use('AGG') -from lbm import Lattice, Geometry +from simulation import Lattice, Geometry +from symbolic.generator import LBM import symbolic.D3Q19 as D3Q19 @@ -68,12 +69,14 @@ moments = [] print("Initializing simulation...\n") +lbm = LBM(D3Q19) + lattice = Lattice( descriptor = D3Q19, geometry = Geometry(128, 128, 128), - moments = D3Q19.moments(optimize = False), - collide = D3Q19.bgk(tau = 0.52), + moments = lbm.moments(optimize = False), + collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = 0.52), boundary_src = boundary) diff --git a/simulation.py b/simulation.py new file mode 100644 index 0000000..ef05e4f --- /dev/null +++ b/simulation.py @@ -0,0 +1,137 @@ +import pyopencl as cl +mf = cl.mem_flags + +import numpy +import sympy + +from mako.template import Template +from pathlib import Path + +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): + if self.size_z == 1: + for y in range(1,self.size_y-1): + for x in range(1,self.size_x-1): + yield x, y + else: + for z in range(1,self.size_z-1): + for y in range(1,self.size_y-1): + for x in range(1,self.size_x-1): + yield x, y, z + + def span(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_span(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) + + +class Lattice: + def __init__(self, descriptor, geometry, moments, collide, pop_eq_src = '', boundary_src = ''): + self.descriptor = descriptor + self.geometry = geometry + + self.moments = moments + self.collide = collide + + self.pop_eq_src = pop_eq_src + self.boundary_src = boundary_src + + self.platform = cl.get_platforms()[0] + self.context = cl.Context(properties=[(cl.context_properties.PLATFORM, self.platform)]) + self.queue = cl.CommandQueue(self.context) + + self.np_material = numpy.ndarray(shape=(self.geometry.volume, 1), dtype=numpy.int32) + + self.tick = True + + self.pop_size = descriptor.q * self.geometry.volume * numpy.float32(0).nbytes + self.moments_size = (descriptor.d+1) * self.geometry.volume * numpy.float32(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_ONLY | mf.USE_HOST_PTR, hostbuf=self.np_material) + + self.build_kernel() + + if descriptor.d == 2: + self.layout = (32,1) + elif descriptor.d == 3: + self.layout = (32,1,1) + + self.program.equilibrilize( + self.queue, self.geometry.span(), self.layout, self.cl_pop_a, self.cl_pop_b).wait() + + def idx(self, x, y, z = 0): + return z * (self.geometry.size_x*self.geometry.size_y) + y * self.geometry.size_x + x; + + def setup_geometry(self, material_at): + if self.descriptor.d == 2: + for x, y in self.geometry.inner_cells(): + self.np_material[self.idx(x,y)] = material_at(self.geometry, x, y) + elif self.descriptor.d == 3: + for x, y, z in self.geometry.inner_cells(): + self.np_material[self.idx(x,y,z)] = material_at(self.geometry, x, y, z) + + cl.enqueue_copy(self.queue, self.cl_material, self.np_material).wait(); + + def build_kernel(self): + program_src = Template(filename = str(Path(__file__).parent/'template/kernel.mako')).render( + descriptor = self.descriptor, + geometry = self.geometry, + + moments_subexpr = self.moments[0], + moments_assignment = self.moments[1], + collide_subexpr = self.collide[0], + collide_assignment = self.collide[1], + + pop_eq_src = Template(self.pop_eq_src).render( + descriptor = self.descriptor, + geometry = self.geometry + ), + boundary_src = Template(self.boundary_src).render( + descriptor = self.descriptor, + geometry = self.geometry + ), + + ccode = sympy.ccode + ) + self.program = cl.Program(self.context, program_src).build('-cl-single-precision-constant -cl-fast-relaxed-math') + + def evolve(self): + if self.tick: + self.tick = False + self.program.collide_and_stream( + self.queue, self.geometry.span(), self.layout, self.cl_pop_a, self.cl_pop_b, self.cl_material) + else: + self.tick = True + self.program.collide_and_stream( + self.queue, self.geometry.span(), self.layout, self.cl_pop_b, self.cl_pop_a, self.cl_material) + + def sync(self): + self.queue.finish() + + def get_moments(self): + moments = numpy.ndarray(shape=(self.descriptor.d+1, self.geometry.volume), dtype=numpy.float32) + if self.tick: + self.program.collect_moments( + self.queue, self.geometry.span(), self.layout, self.cl_pop_b, self.cl_moments) + else: + self.program.collect_moments( + self.queue, self.geometry.span(), self.layout, self.cl_pop_a, self.cl_moments) + cl.enqueue_copy(self.queue, moments, self.cl_moments).wait(); + return moments diff --git a/symbolic/D2Q9.py b/symbolic/D2Q9.py index 8d42245..22f7ed5 100644 --- a/symbolic/D2Q9.py +++ b/symbolic/D2Q9.py @@ -1,5 +1,4 @@ from sympy import * -from sympy.codegen.ast import Assignment q = 9 d = 2 @@ -8,43 +7,3 @@ c = [ Matrix(x) for x in [(-1, 1), ( 0, 1), ( 1, 1), (-1, 0), ( 0, 0), ( 1, 0), w = [ Rational(*x) for x in [(1,36), (1,9), (1,36), (1,9), (4,9), (1,9), (1,36), (1,9), (1,36)] ] c_s = sqrt(Rational(1,3)) - -f_next = symarray('f_next', q) -f_curr = symarray('f_curr', q) - -def moments(f = f_curr, optimize = True): - rho = symbols('rho') - u = Matrix(symarray('u', d)) - - exprs = [ Assignment(rho, sum(f)) ] - - for i, u_i in enumerate(u): - exprs.append(Assignment(u_i, sum([ (c_j*f[j])[i] for j, c_j in enumerate(c) ]) / sum(f))) - - if optimize: - return cse(exprs, optimizations='basic', symbols=numbered_symbols(prefix='m')) - else: - return ([], exprs) - -def equilibrium(): - rho = symbols('rho') - u = Matrix(symarray('u', d)) - - f_eq = [] - - for i, c_i in enumerate(c): - f_eq_i = w[i] * rho * ( 1 - + c_i.dot(u) / c_s**2 - + c_i.dot(u)**2 / (2*c_s**4) - - u.dot(u) / (2*c_s**2) ) - f_eq.append(f_eq_i) - - return f_eq - -def bgk(tau, f_eq = equilibrium(), optimize = True): - exprs = [ Assignment(f_next[i], f_curr[i] + 1/tau * ( f_eq_i - f_curr[i] )) for i, f_eq_i in enumerate(f_eq) ] - - if optimize: - return cse(exprs, optimizations='basic') - else: - return ([], exprs) diff --git a/symbolic/D3Q19.py b/symbolic/D3Q19.py index 789b083..4e84908 100644 --- a/symbolic/D3Q19.py +++ b/symbolic/D3Q19.py @@ -1,5 +1,4 @@ from sympy import * -from sympy.codegen.ast import Assignment q = 19 d = 3 @@ -17,43 +16,3 @@ w = [Rational(*x) for x in [ ]] c_s = sqrt(Rational(1,3)) - -f_next = symarray('f_next', q) -f_curr = symarray('f_curr', q) - -def moments(f = f_curr, optimize = True): - rho = symbols('rho') - u = Matrix(symarray('u', d)) - - exprs = [ Assignment(rho, sum(f)) ] - - for i, u_i in enumerate(u): - exprs.append(Assignment(u_i, sum([ (c_j*f[j])[i] for j, c_j in enumerate(c) ]) / sum(f))) - - if optimize: - return cse(exprs, optimizations='basic', symbols=numbered_symbols(prefix='m')) - else: - return ([], exprs) - -def equilibrium(): - rho = symbols('rho') - u = Matrix(symarray('u', d)) - - f_eq = [] - - for i, c_i in enumerate(c): - f_eq_i = w[i] * rho * ( 1 - + c_i.dot(u) / c_s**2 - + c_i.dot(u)**2 / (2*c_s**4) - - u.dot(u) / (2*c_s**2) ) - f_eq.append(f_eq_i) - - return f_eq - -def bgk(tau, f_eq = equilibrium(), optimize = True): - exprs = [ Assignment(f_next[i], f_curr[i] + 1/tau * ( f_eq_i - f_curr[i] )) for i, f_eq_i in enumerate(f_eq) ] - - if optimize: - return cse(exprs, optimizations='basic') - else: - return ([], exprs) diff --git a/symbolic/generator.py b/symbolic/generator.py new file mode 100644 index 0000000..e57154c --- /dev/null +++ b/symbolic/generator.py @@ -0,0 +1,46 @@ +from sympy import * +from sympy.codegen.ast import Assignment + +class LBM(): + def __init__(self, descriptor): + self.descriptor = descriptor + self.f_next = symarray('f_next', descriptor.q) + self.f_curr = symarray('f_curr', descriptor.q) + + def moments(self, optimize = True): + rho = symbols('rho') + u = Matrix(symarray('u', self.descriptor.d)) + + exprs = [ Assignment(rho, sum(self.f_curr)) ] + + for i, u_i in enumerate(u): + exprs.append( + Assignment(u_i, sum([ (c_j*self.f_curr[j])[i] for j, c_j in enumerate(self.descriptor.c) ]) / sum(self.f_curr))) + + if optimize: + return cse(exprs, optimizations='basic', symbols=numbered_symbols(prefix='m')) + else: + return ([], exprs) + + def equilibrium(self): + rho = symbols('rho') + u = Matrix(symarray('u', self.descriptor.d)) + + f_eq = [] + + for i, c_i in enumerate(self.descriptor.c): + f_eq_i = self.descriptor.w[i] * rho * ( 1 + + c_i.dot(u) / self.descriptor.c_s**2 + + c_i.dot(u)**2 / (2*self.descriptor.c_s**4) + - u.dot(u) / (2*self.descriptor.c_s**2) ) + f_eq.append(f_eq_i) + + return f_eq + + def bgk(self, tau, f_eq, optimize = True): + exprs = [ Assignment(self.f_next[i], self.f_curr[i] + 1/tau * (f_eq_i - self.f_curr[i])) for i, f_eq_i in enumerate(f_eq) ] + + if optimize: + return cse(exprs, optimizations='basic') + else: + return ([], exprs) -- cgit v1.2.3