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
|
<%
def gid():
return {
2: 'get_global_id(1)*%d + get_global_id(0)' % memory.size_x,
3: 'get_global_id(2)*%d + get_global_id(1)*%d + get_global_id(0)' % (memory.size_x*memory.size_y, memory.size_x)
}.get(descriptor.d)
def pop_offset(i):
return i * memory.volume
%>
__kernel void equilibrilize(__global __write_only ${float_type}* f_next,
__global __write_only ${float_type}* f_prev)
{
const unsigned int gid = ${gid()};
__global __write_only ${float_type}* preshifted_f_next = f_next + gid;
__global __write_only ${float_type}* preshifted_f_prev = f_prev + gid;
% if pop_eq_src == '':
% for i, w_i in enumerate(descriptor.w):
preshifted_f_next[${pop_offset(i)}] = ${w_i}.f;
preshifted_f_prev[${pop_offset(i)}] = ${w_i}.f;
% endfor
% else:
${pop_eq_src}
% endif
}
<%
def neighbor_offset(c_i):
return {
2: lambda: c_i[1]*memory.size_x + c_i[0],
3: lambda: c_i[2]*memory.size_x*memory.size_y + c_i[1]*memory.size_x + c_i[0]
}.get(descriptor.d)()
%>
__kernel void collide_and_stream(__global __write_only ${float_type}* f_next,
__global __read_only ${float_type}* f_prev,
__global __read_only int* material,
unsigned int time)
{
const unsigned int gid = ${gid()};
const int m = material[gid];
if ( m == 0 ) {
return;
}
__global __write_only ${float_type}* preshifted_f_next = f_next + gid;
__global __read_only ${float_type}* preshifted_f_prev = f_prev + gid;
% for i, c_i in enumerate(descriptor.c):
const ${float_type} f_curr_${i} = preshifted_f_prev[${pop_offset(i) + neighbor_offset(-c_i)}];
% endfor
% for i, expr in enumerate(moments_subexpr):
const ${float_type} ${expr[0]} = ${ccode(expr[1])};
% endfor
% for i, expr in enumerate(moments_assignment):
${float_type} ${ccode(expr)}
% endfor
${boundary_src}
% for i, expr in enumerate(collide_subexpr):
const ${float_type} ${expr[0]} = ${ccode(expr[1])};
% endfor
% for i, expr in enumerate(collide_assignment):
const ${float_type} ${ccode(expr)}
% endfor
% for i in range(0,descriptor.q):
preshifted_f_next[${pop_offset(i)}] = f_next_${i};
% endfor
}
__kernel void collect_moments(__global __read_only ${float_type}* f,
__global __write_only ${float_type}* moments)
{
const unsigned int gid = ${gid()};
__global __read_only ${float_type}* preshifted_f = f + gid;
% for i in range(0,descriptor.q):
const ${float_type} f_curr_${i} = preshifted_f[${pop_offset(i)}];
% endfor
% for i, expr in enumerate(moments_subexpr):
const ${float_type} ${expr[0]} = ${ccode(expr[1])};
% endfor
% for i, expr in enumerate(moments_assignment):
moments[${pop_offset(i)} + gid] = ${ccode(expr.rhs)};
% endfor
}
__kernel void collect_gl_moments(__global __read_only ${float_type}* f,
__global __read_only int* material,
__global __write_only float4* moments)
{
const unsigned int gid = ${gid()};
__global __read_only ${float_type}* preshifted_f = f + gid;
% for i in range(0,descriptor.q):
const ${float_type} f_curr_${i} = preshifted_f[${pop_offset(i)}];
% endfor
% for i, expr in enumerate(moments_subexpr):
const ${float_type} ${expr[0]} = ${ccode(expr[1])};
% endfor
float4 data;
if (material[gid] == 1) {
% if descriptor.d == 2:
data.x = ${ccode(moments_assignment[0].rhs)};
data.y = ${ccode(moments_assignment[1].rhs)};
data.z = ${ccode(moments_assignment[2].rhs)};
data.w = sqrt(data.y*data.y + data.z*data.z);
% elif descriptor.d == 3:
data.x = ${ccode(moments_assignment[0].rhs)};
data.y = ${ccode(moments_assignment[1].rhs)};
data.z = ${ccode(moments_assignment[2].rhs)};
data.w = ${ccode(moments_assignment[3].rhs)};
% endif
} else {
data.x = 0.0;
data.y = 0.0;
data.z = 0.0;
data.w = -material[gid];
}
moments[gid] = data;
}
__kernel void update_particles(__global __read_only float4* moments,
__global __read_only int* material,
__global __write_only float4* particles,
__global __read_only float4* init_particles,
float aging)
{
const unsigned int pid = get_global_id(0);
float4 particle = particles[pid];
% if descriptor.d == 2:
const unsigned int gid = floor(particle.y)*${memory.size_x} + floor(particle.x);
% elif descriptor.d == 3:
const unsigned int gid = floor(particle.z)*${memory.size_x*memory.size_y} + floor(particle.y)*${memory.size_x} + floor(particle.x);
% endif
const float4 moment = moments[gid];
if (material[gid] == 1 && particle.w < 1.0) {
particle.x += moment.y;
particle.y += moment.z;
% if descriptor.d == 2:
particle.w += min(particle.x, particle.y) * aging;
% elif descriptor.d == 3:
particle.z += moment.w;
particle.w += min(min(particle.x, particle.y), particle.z) * aging;
% endif
} else {
particle.xyz = init_particles[pid].xyz;
particle.w = particle.w-1.0;
}
particles[pid] = particle;
}
|