-
Notifications
You must be signed in to change notification settings - Fork 0
/
openclexample.py
276 lines (149 loc) · 6.12 KB
/
openclexample.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
import matplotlib.pyplot as plt
import time
import numpy as np
import pyopencl as cl
import math
import cmath
imsized = 10 #4 - 10
List_Strengh_Vortexes = np.array([1.0, -1.0, 1.0, -1.0], dtype = np.float64)
List_of_Vort = np.array([-1.0 - 0.1j, -1.0 + 0.1j, -0.5 - 0.5j, -0.5 + 0.5j], dtype=np.complex128)
kvortexes = len(List_of_Vort)
rcore = 0.1
tmax = 0.7
steps = 70
dt = tmax/steps
actSteps = steps
actualT = steps*dt
print(actualT)
w = 500
h = 500
def calc_frame_opencl(q, vort, vortstrength = List_Strengh_Vortexes):
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
output = np.empty(q.shape, dtype=np.complex128)
#vort = np.array([1.0 + 1j,1.0, 100.0,100.0, 1000.0,1000.0, 100.0,1.0], dtype=np.complex128)
#vortstrength = np.array([-1, 1 , -1 , 1], dtype=np.uint16)
mf = cl.mem_flags
q_opencl = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf = q)
output_opencl = cl.Buffer(ctx, mf.WRITE_ONLY, output.nbytes)
vort4_opencl = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=vort)
vortstrength_opencl = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=vortstrength)
prg = cl.Program(ctx,
"""
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
constant double rcore = 0.1;
#define vortN 4
double2 fValue( double K, double2 Z, double2 Z0){
double r2 = pow( sqrt( pow((Z.x - Z0.x), 2) + pow((Z.y - Z0.y), 2) ), 2);
double constM = K*( 1.0 - exp( (-r2)/(pow(rcore, 2)) ) ) / r2 ;
double Xk = - constM*(Z0.y - Z.y);
double Yk = constM*(Z0.x - Z.x);
double2 T1 = (double2) (Xk, Yk);
return(T1);
}
__kernel void runge(__global double2 *q,
__global double2 *output,
__global double2 *vortlist,
__global double *vortstrength,
double const dt)
{
int gid = get_global_id(0);
double2 k1 = (double2) (0, 0);
for (int iter = 0; iter < vortN; iter++){
k1 += fValue( vortstrength[iter], q[gid], vortlist[iter] );
}
k1 *= dt;
double2 k2 = (double2) (0, 0);
for (int iter = 0; iter < vortN; iter++){
k2 += fValue( vortstrength[iter], q[gid] + k1 /2, vortlist[iter] );
}
k2 *= dt;
double2 k3 = (double2) (0, 0);
for (int iter = 0; iter < vortN; iter++){
k3 += fValue( vortstrength[iter], q[gid] + k2 /2, vortlist[iter] );
}
k3 *= dt;
double2 k4 = (double2) (0, 0);
for (int iter = 0; iter < vortN; iter++){
k3 += fValue( vortstrength[iter], q[gid] + k3, vortlist[iter] );
}
k4 *= dt;
output[gid] = q[gid] - ((k1 + 2*k2 + 2*k3 + k4)/6) ;
}
""",
).build()
prg.runge(
queue, output.shape, None, q_opencl, output_opencl, vort4_opencl, vortstrength_opencl, np.float64(dt)
)
cl.enqueue_copy(queue, output, output_opencl).wait()
return output
def display(mesh):
#imsized = 10
cmap = 'copper_r'
plt.figure(num = None, figsize=(imsized, imsized), dpi=300)
plt.axis('off')
#plot = plt.imshow(mesh, cmap = cmap, interpolation='lanczos' )
plot = plt.imshow(mesh, cmap = cmap, interpolation='lanczos')
####
filenameImage = f'test{h}_{w}_{dt}_{actSteps}_{actualT}_{cmap}_{List_of_Vort[0]}_{List_of_Vort[1]}_{List_of_Vort[2]}_{List_of_Vort[3]}.png'
plt.savefig(filenameImage, bbox_inches = 'tight')
####
plt.show()
plt.close()
def caclulateMatrixVortexesInTime():
matrixVortInTime = np.zeros((steps +1, kvortexes), dtype=np.complex128)
matrixVortInTime[0, :] = List_of_Vort
for i in range(1, steps + 1):
matrixVortInTime[i, :] = RungeUpdateVortInTime(matrixVortInTime[i - 1, :])
return matrixVortInTime
def RungeUpdateVortInTime(listofz):
#listofz - list of vortices
k1 = dt * value_of_interaction_matrix(listofz)
k2 = dt * value_of_interaction_matrix(listofz + k1/2)
k3 = dt * value_of_interaction_matrix(listofz + k2/2)
k4 = dt * value_of_interaction_matrix(listofz + k3)
#return list [z1 n+1, z2 n+1, z3 n+1, z4 n+1]
return (k1 + 2*k2 + 2*k3 + k4)/6 + listofz
def value_of_interaction_matrix(Zlist):
#matrixValues = np.zeros((kvortexes, kvortexes), dtype=np.complex128)
matrixSum = np.zeros((kvortexes), dtype=np.complex128)
for i in range(kvortexes):
for j in range(kvortexes):
if i != j:
matrixSum[i] += functionValue(List_Strengh_Vortexes[j], Zlist[i], Zlist[j])
return matrixSum
def functionValue(K, z, z0):
r2 = abs(z - z0)**2
temp = K * (z0 - z) / r2 * (1 - math.exp((-r2)/(rcore**2))) * complex(0, 1)
#temp = K * (z0 - z) / (r2 * (1 - cmath.exp((-r2)/(rcore**2)))) * complex(0, 1)
return temp
class FluidSimulation:
def draw(self, x1, x2, y1, y2):
xx = np.linspace(x1, x2, num = w)
yy = np.linspace(y2, y1, num = h) * 1j
q = np.ravel(xx + yy[:, np.newaxis]).astype(np.complex128)
#print(q)
start_main = time.time()
VortMesh = caclulateMatrixVortexesInTime()
#print(VortMesh)
output = q
for i in range(steps, 0, -1):
#output -= calc_frame_opencl(output, VortMesh [i, :] )
output = calc_frame_opencl(output, VortMesh [i, :] )
end_main = time.time()
secs = end_main - start_main
print(f"Main took: {secs}")
self.mandel = np.zeros((h*w))
for index, z in np.ndenumerate(output):
#self.mandel[index] = cmath.phase(z)
self.mandel[index] = cmath.phase(z) * math.copysign(1, z.imag)
self.mandel = np.reshape(self.mandel, (h,w))
def create_image(self):
posx = 0
posy = 0
r = 2.2
self.draw(posx - r, posx + r, posy - r, posy + r)
display(self.mandel)
if __name__ == "__main__":
test = FluidSimulation()
test.create_image()