-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftle_cpu.py
1051 lines (900 loc) · 41.4 KB
/
ftle_cpu.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import glob
import gc
from concurrent.futures import ThreadPoolExecutor as Executor
from typing import Callable, Tuple, List
import h5py
import time
import numpy as np
from numba import uint32, uint64, float32, float64, njit, prange
from math import sqrt, log, ceil, floor
from scipy.spatial import KDTree
from progressbar import ProgressBar
from pyevtk.hl import pointsToVTK, gridToVTK
from mpi4py import MPI
REAL_NP = np.float32
REAL_CP = float32
CUDA_INT = uint64
FAST_MATH = True
def master_print(msg):
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
print(msg)
sys.stdout.flush()
def read_UVW(filename, gen_2d=False):
if not gen_2d:
with h5py.File(filename, "r", driver='mpio', comm=MPI.COMM_WORLD) as f:
data = f["dataset"]
Nx, Ny, _, Nz = data.shape
U = np.empty((Nx, Ny, Nz), dtype=REAL_NP)
V = np.empty((Nx, Ny, Nz), dtype=REAL_NP)
W = np.empty((Nx, Ny, Nz), dtype=REAL_NP)
data.read_direct(U, np.s_[:, :, 1, :], np.s_[:, :, :])
data.read_direct(V, np.s_[:, :, 2, :], np.s_[:, :, :])
data.read_direct(W, np.s_[:, :, 3, :], np.s_[:, :, :])
else:
#with h5py.File(filename, "r", driver='mpio', comm=MPI.COMM_WORLD) as f:
with h5py.File(filename, "r") as f:
data = f["dataset"]
#with data.collective:
Nx, Ny, _, Nz = data.shape
U = data.astype(REAL_NP)[:, :, 1, Nz//2].reshape((Nx, Ny, 1))
V = data.astype(REAL_NP)[:, :, 2, Nz//2].reshape((Nx, Ny, 1))
W = np.zeros_like(U)
return U, V, W
@njit(fastmath=True, parallel=True, nogil=True)
def interp_UVW_flat(U0, V0, W0, U1, V1, W1, Un, Vn, Wn, dt, fraction=0.1, N=0):
tm = REAL_CP(dt / 2.)
t_target = REAL_CP(fraction * N * dt)
dt_inv = REAL_CP(1./dt)
N = U0.shape[0]
for ii in prange(N):
Um = REAL_CP(REAL_CP(0.5) * (U0[ii] + U1[ii]))
Vm = REAL_CP(REAL_CP(0.5) * (V0[ii] + V1[ii]))
Wm = REAL_CP(REAL_CP(0.5) * (W0[ii] + W1[ii]))
dUdt = REAL_CP((U1[ii] - U0[ii]) * dt_inv)
dVdt = REAL_CP((V1[ii] - V0[ii]) * dt_inv)
dWdt = REAL_CP((W1[ii] - W0[ii]) * dt_inv)
deltaT = REAL_CP((t_target - tm))
Un[ii] = REAL_CP(Um + dUdt * deltaT)
Vn[ii] = REAL_CP(Vm + dVdt * deltaT)
Wn[ii] = REAL_CP(Wm + dWdt * deltaT)
@njit(fastmath=True, parallel=True, nogil=True)
def interp_UVW(U0, V0, W0, U1, V1, W1, Un, Vn, Wn, dt, fraction=0.1, N=0):
tm = REAL_CP(dt / 2.)
t_target = REAL_CP(fraction * N * dt)
dt_inv = REAL_CP(1./dt)
Nx, Ny, Nz = U0.shape
for ii in prange(Nx):
for jj in range(Ny):
for kk in range(Nz):
Um = REAL_CP(REAL_CP(0.5) * (U0[ii, jj, kk] + U1[ii, jj, kk]))
Vm = REAL_CP(REAL_CP(0.5) * (V0[ii, jj, kk] + V1[ii, jj, kk]))
Wm = REAL_CP(REAL_CP(0.5) * (W0[ii, jj, kk] + W1[ii, jj, kk]))
dUdt = REAL_CP((U1[ii, jj, kk] - U0[ii, jj, kk]) * dt_inv)
dVdt = REAL_CP((V1[ii, jj, kk] - V0[ii, jj, kk]) * dt_inv)
dWdt = REAL_CP((W1[ii, jj, kk] - W0[ii, jj, kk]) * dt_inv)
deltaT = REAL_CP((t_target - tm))
Un[ii, jj, kk] = REAL_CP(Um + dUdt * deltaT)
Vn[ii, jj, kk] = REAL_CP(Vm + dVdt * deltaT)
Wn[ii, jj, kk] = REAL_CP(Wm + dWdt * deltaT)
@njit(fastmath=True, inline="always", parallel=True)
def max_cube(Uc):
U000 = Uc[0,0,0]
U100 = Uc[1,0,0]
U010 = Uc[0,1,0]
U001 = Uc[0,0,1]
U110 = Uc[1,1,0]
U011 = Uc[0,1,1]
U101 = Uc[1,0,1]
U111 = Uc[1,1,1]
return max(U000, U100, U010, U001, U110, U011, U101, U111)
@njit(fastmath=True, inline="always", parallel=True)
def min_cube(Uc):
U000 = Uc[0,0,0]
U100 = Uc[1,0,0]
U010 = Uc[0,1,0]
U001 = Uc[0,0,1]
U110 = Uc[1,1,0]
U011 = Uc[0,1,1]
U101 = Uc[1,0,1]
U111 = Uc[1,1,1]
return min(U000, U100, U010, U001, U110, U011, U101, U111)
@njit(fastmath=True, inline="always", parallel=True)
def minmax_cube(Uc):
U000 = Uc[0,0,0]
U100 = Uc[1,0,0]
U010 = Uc[0,1,0]
U001 = Uc[0,0,1]
U110 = Uc[1,1,0]
U011 = Uc[0,1,1]
U101 = Uc[1,0,1]
U111 = Uc[1,1,1]
mini = min(U000, U100, U010, U001, U110, U011, U101, U111)
maxi = max(U000, U100, U010, U001, U110, U011, U101, U111)
return mini, maxi
@njit(fastmath=True, inline="always", parallel=True)
def clip(v, minimum, maximum):
return max(min(maximum, v), minimum)
@njit(fastmath=True, inline="always", parallel=True)
def inner_trilinear_interpolate(Uc, dx, dy, dz):
dxdy = dx * dy
dxdz = dx * dz
dydz = dy * dz
dxdydz = dx * dy * dz
mini, maxi = minmax_cube(Uc)
U000 = Uc[0,0,0]
U100 = Uc[1,0,0]
U010 = Uc[0,1,0]
U001 = Uc[0,0,1]
U110 = Uc[1,1,0]
U011 = Uc[0,1,1]
U101 = Uc[1,0,1]
U111 = Uc[1,1,1]
c0u = (U000)
c1u = (U100 - U000)
c2u = (U010 - U000)
c3u = (U001 - U000)
c4u = (U110 - U010 - U100 + U000)
c5u = (U011 - U001 - U010 + U000)
c6u = (U101 - U001 - U100 + U000)
c7u = (U111 - U011 - U101 - U110 + U100 + U001 + U010 - U000)
return clip(c0u + c1u * dx + c2u * dy + c3u * dz + c4u * dxdy + c5u * dydz + c6u * dxdz + c7u * dxdydz, mini, maxi) # 80
@njit(fastmath=True, inline="always", parallel=True)
def cpu_nns(x, y, z, xp, yp, zp, coarse_levels):
Nx, Ny, Nz = x.shape
COARSE_FACTOR = ((2)**coarse_levels)
best_x = 0
best_y = 0
best_z = 0
distance = float32(1e6)
for ii in range((COARSE_FACTOR), (Nx-COARSE_FACTOR), (COARSE_FACTOR//2)):
for jj in range((COARSE_FACTOR), (Ny-COARSE_FACTOR), (COARSE_FACTOR//2)):
for kk in range((COARSE_FACTOR), (Nz-COARSE_FACTOR), (COARSE_FACTOR//2)):
current_distance = sqrt(
(x[ii,jj,kk] - xp)*(x[ii,jj,kk] - xp) +
(y[ii,jj,kk] - yp)*(y[ii,jj,kk] - yp) +
(z[ii,jj,kk] - zp)*(z[ii,jj,kk] - zp)
)
if (current_distance <= distance):
best_x = (ii)
best_y = (jj)
best_z = (kk)
distance = current_distance
while COARSE_FACTOR > (1):
for ii in range((best_x - COARSE_FACTOR), (best_x + COARSE_FACTOR), (COARSE_FACTOR//2)):
for jj in range((best_y - COARSE_FACTOR), (best_y + COARSE_FACTOR), (COARSE_FACTOR//2)):
for kk in range((best_z - COARSE_FACTOR), (best_z + COARSE_FACTOR), (COARSE_FACTOR//2)):
current_distance = sqrt(
(x[ii,jj,kk] - xp)*(x[ii,jj,kk] - xp) +
(y[ii,jj,kk] - yp)*(y[ii,jj,kk] - yp) +
(z[ii,jj,kk] - zp)*(z[ii,jj,kk] - zp)
)
if (current_distance <= distance):
best_x = (ii)
best_y = (jj)
best_z = (kk)
distance = current_distance
COARSE_FACTOR = (COARSE_FACTOR // 2)
return CUDA_INT(best_x), CUDA_INT(best_y), CUDA_INT(best_z)
@njit(fastmath=True, parallel=True, nogil=True)
def get_particle_velocity_with_integrated_nns(x, y, z, xp, yp, zp, U, V, W, dt):
NParticles = xp.size
Nx, Ny, Nz = x.shape
SMALLEST_DIM = min(Nx, Ny, Nz)
P = 1
while (2**(P+1)) < (SMALLEST_DIM//2):
P+=1
for particle in prange(NParticles):
xp_local = REAL_CP(xp[particle])
yp_local = REAL_CP(yp[particle])
zp_local = REAL_CP(zp[particle])
i, j, k = cpu_nns(x, y, z, xp_local, yp_local, zp_local, P)
i -= 1 * ((xp_local <= x[i,j,k]))
j -= 1 * ((yp_local <= y[i,j,k]))
k -= 1 * ((zp_local <= z[i,j,k]) and (zp_local >= z[i,j,max(0, k-1)]))
in_domain = (0 <= i) and (i < Nx - 1) and (0 <= j) and (j < Ny - 1) and (0 <= k) and (k < Nz - 1)
i = clip(i, 0, Nx - 2)
j = clip(j, 0, Ny - 2)
k = clip(k, 0, Nz - 2)
xc = (x[i:i+2, j:j+2, k:k+2])
yc = (y[i:i+2, j:j+2, k:k+2])
zc = (z[i:i+2, j:j+2, k:k+2])
minX, maxX = minmax_cube(xc)
minY, maxY = minmax_cube(yc)
minZ, maxZ = minmax_cube(zc)
dx = (xp_local - minX)/(maxX - minX)
dy = (yp_local - minY)/(maxY - minY)
dz = (zp_local - minZ)/(maxZ - minZ)
Uc = (U[i:i+2, j:j+2, k:k+2])
Vc = (V[i:i+2, j:j+2, k:k+2])
Wc = (W[i:i+2, j:j+2, k:k+2])
Up = REAL_CP(inner_trilinear_interpolate(Uc, dx, dy, dz)) * REAL_CP(in_domain) + REAL_CP(not in_domain) * REAL_CP(U[i, j, k])
Vp = REAL_CP(inner_trilinear_interpolate(Vc, dx, dy, dz)) * REAL_CP(in_domain) + REAL_CP(not in_domain) * REAL_CP(V[i, j, k])
Wp = REAL_CP(inner_trilinear_interpolate(Wc, dx, dy, dz)) * REAL_CP(in_domain) + REAL_CP(not in_domain) * REAL_CP(W[i, j, k])
xp[particle] = xp_local + REAL_CP(dt * Up)
yp[particle] = yp_local + REAL_CP(dt * Vp)
zp[particle] = zp_local + REAL_CP(dt * Wp)
def update_particle_velocity(
point: Tuple[np.ndarray, np.ndarray, np.ndarray],
x: np.ndarray,
y: np.ndarray,
z: np.ndarray,
U: np.ndarray,
V: np.ndarray,
W: np.ndarray,
is_2d: bool = False,
dt: float = None,
):
xtarget, ytarget, ztarget = point
get_particle_velocity_with_integrated_nns(
x,
y,
z,
xtarget,
ytarget,
ztarget,
U,
V,
W,
REAL_CP(dt)
)
return
@njit(fastmath=True, parallel=True, nogil=True)
def step_particles_euler(
Up, Vp, Wp, xp, yp, zp, dt, maxz=0.0, minz=0.0
):
NParticles = xp.size
for particle in prange(NParticles):
xp[particle] = REAL_CP(dt * Up[particle] + xp[particle])
yp[particle] = REAL_CP(dt * Vp[particle] + yp[particle])
zp[particle] = REAL_CP(dt * Wp[particle] + zp[particle])
def particle_first_step(
U: np.ndarray,
V: np.ndarray,
W: np.ndarray,
x: np.ndarray,
y: np.ndarray,
z: np.ndarray,
dt: float,
mask: List[int],
step_particles: Callable,
is_2d: bool,
newx: np.ndarray,
newy: np.ndarray,
newz: np.ndarray,
):
N = len(mask) - 1
xp = np.ascontiguousarray(newx.reshape((-1,))[mask[0]:mask[-1]])
yp = np.ascontiguousarray(newy.reshape((-1,))[mask[0]:mask[-1]])
zp = np.ascontiguousarray(newz.reshape((-1,))[mask[0]:mask[-1]])
update_particle_velocity(
(xp, yp, zp), x, y, z, U, V, W, is_2d, dt
)
return xp, yp, zp
def particle_update_velocities(
U: np.ndarray,
V: np.ndarray,
W: np.ndarray,
x: np.ndarray,
y: np.ndarray,
z: np.ndarray,
dt: float,
xp: np.ndarray,
yp: np.ndarray,
zp: np.ndarray,
step_particles: Callable,
is_2d: bool = False
):
N = xp.size
update_particle_velocity(
(xp, yp, zp), x, y, z, U, V, W, is_2d, dt
)
return xp, yp, zp
def write_particles_to_file(
h5file: h5py.File,
xp: np.ndarray,
yp: np.ndarray,
zp: np.ndarray,
timestep: int,
mask: List[int]
):
print(f"Writing timestep: # {timestep}")
sys.stdout.flush()
dset_1 = h5file["Particle_xyz"]
dset_1[timestep, 0, mask[0]:mask[-1]] = np.array(xp, copy=False)
dset_1[timestep, 1, mask[0]:mask[-1]] = np.array(yp, copy=False)
dset_1[timestep, 2, mask[0]:mask[-1]] = np.array(zp, copy=False)
return
@njit(parallel=True, fastmath=False, nogil=True)
def __irregular_mesh_refine(x, y, z, ref_x, ref_y, ref_z):
Nx, Ny, Nz = x.shape
newx = np.zeros((Nx * ref_x, Ny * ref_y, Nz*ref_z), dtype = x.dtype) + np.min(x)
newy = np.zeros((Nx * ref_x, Ny * ref_y, Nz*ref_z), dtype = y.dtype) + np.min(y)
newz = np.zeros((Nx * ref_x, Ny * ref_y, Nz*ref_z), dtype = z.dtype) + np.min(z)
for ii in prange(1, Nx+1):
for jj in range(1, Ny+1):
for kk in range(1, Nz+1):
for io in range(ref_x):
for jo in range(ref_y):
for ko in range(ref_z):
idx = (ii - 1)*ref_x + io
idy = (jj - 1)*ref_y + jo
idz = (kk - 1)*ref_z + ko
xcube = x[max(0, ii-1):min(Nx, ii+1), max(0, jj-1):min(Ny, jj+1), max(0, kk-1):min(Nz, kk+1)]
ycube = y[max(0, ii-1):min(Nx, ii+1), max(0, jj-1):min(Ny, jj+1), max(0, kk-1):min(Nz, kk+1)]
zcube = z[max(0, ii-1):min(Nx, ii+1), max(0, jj-1):min(Ny, jj+1), max(0, kk-1):min(Nz, kk+1)]
dx = (io) / (ref_x)
dy = (jo) / (ref_y)
dz = (ko) / (ref_z)
Uc = xcube
Vc = ycube
Wc = zcube
c0u = (Uc[0,0,0])
c1u = (Uc[-1,0,0] - Uc[0,0,0])
c2u = (Uc[0,-1,0] - Uc[0,0,0])
c3u = (Uc[0,0,-1] - Uc[0,0,0])
c4u = (Uc[-1,-1,0] - Uc[0,-1,0] - Uc[-1,0,0] + Uc[0,0,0])
c5u = (Uc[0,-1,-1] - Uc[0,0,-1] - Uc[0,-1,0] + Uc[0,0,0])
c6u = (Uc[-1,0,-1] - Uc[0,0,-1] - Uc[-1,0,0] + Uc[0,0,0])
c7u = (Uc[-1,-1,-1] - Uc[0,-1,-1] - Uc[-1,0,-1] - Uc[-1,-1,0] + Uc[-1,0,0] + Uc[0,0,-1] + Uc[0,-1,0] - Uc[0,0,0])
c0v = (Vc[0,0,0])
c1v = (Vc[-1,0,0] - Vc[0,0,0])
c2v = (Vc[0,-1,0] - Vc[0,0,0])
c3v = (Vc[0,0,-1] - Vc[0,0,0])
c4v = (Vc[-1,-1,0] - Vc[0,-1,0] - Vc[-1,0,0] + Vc[0,0,0])
c5v = (Vc[0,-1,-1] - Vc[0,0,-1] - Vc[0,-1,0] + Vc[0,0,0])
c6v = (Vc[-1,0,-1] - Vc[0,0,-1] - Vc[-1,0,0] + Vc[0,0,0])
c7v = (Vc[-1,-1,-1] - Vc[0,-1,-1] - Vc[-1,0,-1] - Vc[-1,-1,0] + Vc[-1,0,0] + Vc[0,0,-1] + Vc[0,-1,0] - Vc[0,0,0])
c0w = (Wc[0,0,0])
c1w = (Wc[-1,0,0] - Wc[0,0,0])
c2w = (Wc[0,-1,0] - Wc[0,0,0])
c3w = (Wc[0,0,-1] - Wc[0,0,0])
c4w = (Wc[-1,-1,0] - Wc[0,-1,0] - Wc[-1,0,0] + Wc[0,0,0])
c5w = (Wc[0,-1,-1] - Wc[0,0,-1] - Wc[0,-1,0] + Wc[0,0,0])
c6w = (Wc[-1,0,-1] - Wc[0,0,-1] - Wc[-1,0,0] + Wc[0,0,0])
c7w = (Wc[-1,-1,-1] - Wc[0,-1,-1] - Wc[-1,0,-1] - Wc[-1,-1,0] + Wc[-1,0,0] + Wc[0,0,-1] + Wc[0,-1,0] - Wc[0,0,0])
newx[idx,idy,idz] = c0u + c1u * dx + c2u * dy + c3u * dz + c4u * dx * dy + c5u * dy * dz + c6u * dz * dx + c7u * dx * dy * dz
newy[idx,idy,idz] = c0v + c1v * dx + c2v * dy + c3v * dz + c4v * dx * dy + c5v * dy * dz + c6v * dz * dx + c7v * dx * dy * dz
newz[idx,idy,idz] = c0w + c1w * dx + c2w * dy + c3w * dz + c4w * dx * dy + c5w * dy * dz + c6w * dz * dx + c7w * dx * dy * dz
return newx, newy, newz
def refine_mesh(
x, y, z, ref_x = 10, ref_y = 10, ref_z = 1, regular_mesh=False
):
Nx, Ny, Nz = x.shape
if regular_mesh:
min_x = np.min(x)
min_y = np.min(y)
max_x = np.max(x)
max_y = np.max(y)
min_z = np.min(z)
max_z = np.max(z)
newx, newy, newz = np.meshgrid(
np.linspace(min_x, max_x, num=Nx * ref_x, endpoint=True),
np.linspace(min_y, max_y, num=Ny * ref_y, endpoint=True),
np.linspace(min_z, max_z, num=Nz * ref_z, endpoint=True),
indexing='ij'
)
else:
newx, newy, newz = __irregular_mesh_refine(x, y, z, ref_x, ref_y, ref_z)
return newx.astype(x.dtype), newy.astype(x.dtype), newz.astype(x.dtype)
def particle_simulator(
filenames: str,
x: np.ndarray,
y: np.ndarray,
z: np.ndarray,
dt: float,
output_filename: str,
upscale: int = 10,
make_2d_simulation: bool = False,
refinement_parameters: Tuple[int, int, int] = (1, 1, 1),
write_all_time_steps: bool = True,
number_of_time_steps=-1,
initial_time_step=0,
):
comm = MPI.COMM_WORLD
my_rank = comm.rank
total_ranks = comm.size
print("Creating output HDF5 files...")
h5Out = h5py.File(output_filename, "w", driver='mpio', comm=MPI.COMM_WORLD)
#filenames = sorted(filenames)
U, V, W = read_UVW(filenames[0], gen_2d=make_2d_simulation)
newx, newy, newz = refine_mesh(
x, y, z, refinement_parameters[0], refinement_parameters[1], refinement_parameters[2]
)
Nx, Ny, Nz = newy.shape
TotalElements = Nx * Ny * Nz
localN = -(TotalElements // -total_ranks)
MyIndexList = [ii for ii in range(my_rank*localN, min((my_rank+1)*localN, TotalElements))]
print(f" * Rank {my_rank} to process {len(MyIndexList)} particles out of {TotalElements} in the range: [{MyIndexList[0]}, {MyIndexList[-1]})")
print(f"Allocating HDF5 datasets for {TotalElements} particles...")
sys.stdout.flush()
h5Out.create_dataset(
f"refined_x",
data = newx
)
h5Out.create_dataset(
f"refined_y",
data = newy
)
h5Out.create_dataset(
f"refined_z",
data = newz
)
filenames = filenames[initial_time_step:(initial_time_step+number_of_time_steps)]
if write_all_time_steps:
length_of_files = len(filenames) + 1
else:
length_of_files = 2
h5Out.create_dataset(
f"Particle_xyz",
shape=(length_of_files, 3, TotalElements),
dtype=REAL_NP,
)
#h5Out.flush()
timestep = 0
write_particles_to_file(
h5Out,
newx.ravel()[MyIndexList[0]:MyIndexList[-1]],
newy.ravel()[MyIndexList[0]:MyIndexList[-1]],
newz.ravel()[MyIndexList[0]:MyIndexList[-1]],
timestep,
MyIndexList
)
d_U = np.ascontiguousarray(U)
d_V = np.ascontiguousarray(V)
d_W = np.ascontiguousarray(W)
d_x = np.ascontiguousarray(x)
d_y = np.ascontiguousarray(y)
d_z = np.ascontiguousarray(z)
d_U0 = np.zeros(U.shape, dtype=REAL_NP)
d_V0 = np.zeros(V.shape, dtype=REAL_NP)
d_W0 = np.zeros(W.shape, dtype=REAL_NP)
d_Un = np.zeros(U.shape, dtype=REAL_NP)
d_Vn = np.zeros(V.shape, dtype=REAL_NP)
d_Wn = np.zeros(W.shape, dtype=REAL_NP)
print("Initializing the simulation and allocating additional arrays...")
xp, yp, zp = particle_first_step(
d_U,
d_V,
d_W,
d_x,
d_y,
d_z,
dt/upscale,
MyIndexList,
step_particles_euler,
make_2d_simulation,
newx,
newy,
newz
)
timestep += 1
if write_all_time_steps:
write_particles_to_file(h5Out, xp, yp, zp, timestep, MyIndexList)
with ProgressBar(max_value=len(filenames)) as bar, Executor(1) as pool:
FRACTION = 1./upscale
TIMER_LEN = len(filenames[1:]) - 1
for ii, (U, V, W) in enumerate(pool.map(read_UVW, filenames[1:])):
if True:#(ii % TIMER_LEN//4) == 0:
gc.collect()
bar.update(ii)
d_U0, d_U = d_U, d_U0
d_V0, d_V = d_V, d_V0
d_W0, d_W = d_W, d_W0
d_U = np.ascontiguousarray(U)
d_V = np.ascontiguousarray(V)
d_W = np.ascontiguousarray(W)
for N in range(1, upscale):
interp_UVW_flat(
d_U0.reshape(-1), d_V0.reshape(-1), d_W0.reshape(-1),
d_U.reshape(-1), d_V.reshape(-1), d_W.reshape(-1),
d_Un.reshape(-1), d_Vn.reshape(-1), d_Wn.reshape(-1),
dt, FRACTION, N
)
xp, yp, zp = particle_update_velocities(
d_Un, d_Vn, d_Wn, d_x, d_y, d_z, dt/upscale, xp, yp, zp, step_particles_euler, make_2d_simulation
)
xp, yp, zp = particle_update_velocities(
d_U, d_V, d_W, d_x, d_y, d_z, dt/upscale, xp, yp, zp, step_particles_euler, make_2d_simulation
)
timestep += 1
if write_all_time_steps:
write_particles_to_file(h5Out, xp, yp, zp, timestep, MyIndexList)
if not write_all_time_steps:
write_particles_to_file(h5Out, xp, yp, zp, 1, MyIndexList)
bar.update(ii+1)
h5Out.close()
return xp, yp, zp
@njit(fastmath=True, inline="always", parallel=True)
def power_iteration_3d(A, b_k, b_k1):
b_k[0] = 0.801784
b_k[1] = 0.534522
b_k[2] = -0.267261
bkp_0 = b_k[0]
bkp_1 = b_k[1]
bkp_2 = b_k[2]
difference = 100
while difference > 1e-6:
b_k1[0] = A[0, 0] * b_k[0] + A[0, 1] * b_k[1] + A[0, 2] * b_k[2]
b_k1[1] = A[1, 0] * b_k[0] + A[1, 1] * b_k[1] + A[1, 2] * b_k[2]
b_k1[2] = A[2, 0] * b_k[0] + A[2, 1] * b_k[1] + A[2, 2] * b_k[2]
b_k1_norm_inv = 1./sqrt(b_k1[0] * b_k1[0] + b_k1[1] * b_k1[1] + b_k1[2] * b_k1[2])
bkp_0 = b_k[0]
bkp_1 = b_k[1]
bkp_2 = b_k[2]
b_k[0] = b_k1[0] * b_k1_norm_inv
b_k[1] = b_k1[1] * b_k1_norm_inv
b_k[2] = b_k1[2] * b_k1_norm_inv
difference = sqrt((bkp_0 - b_k[0])*(bkp_0 - b_k[0]) + (bkp_1 - b_k[1])*(bkp_1 - b_k[1]) + (bkp_2 - b_k[2])*(bkp_2 - b_k[2]))
rho = (b_k1[0]* b_k[0] + b_k1[1]* b_k[1] + b_k1[2] * b_k[2]) / (b_k[0]* b_k[0] + b_k[1]* b_k[1] + b_k[2] * b_k[2])
return rho
@njit(fastmath=True, inline="always", parallel=True)
def power_iteration_2d(A, b_k, b_k1):
# 0.81901029, 0.57377882
b_k[0] = 0.81901029
b_k[1] = 0.57377882
rho_k = 1024.
for _ in range(100000):
b_k1[0] = A[0, 0] * b_k[0] + A[0, 1] * b_k[1]
b_k1[1] = A[1, 0] * b_k[0] + A[1, 1] * b_k[1]
b_k1_norm_inv = 1./sqrt(b_k1[0] * b_k1[0] + b_k1[1] * b_k1[1])
b_k[0] = b_k1[0] * b_k1_norm_inv
b_k[1] = b_k1[1] * b_k1_norm_inv
rho = (b_k1[0]* b_k[0] + b_k1[1]* b_k[1]) / (b_k[0]* b_k[0] + b_k[1]* b_k[1])
if abs(rho - rho_k) < 1.192093e-07:
break
rho_k = rho
return rho
@njit(fastmath=True, inline="always", parallel=True)
def device_transposed_matmul_and_make_symmetric_2d(J, C):
# C = J.T @ J
for ii in range(2):
for jj in range(2):
C[ii,jj] = REAL_CP(0.)
for kk in range(2):
C[ii,jj] += J[kk,ii] * J[kk,jj]
# C = (C + C.T)/2.
for ii in range(2):
for jj in range(ii, 2):
C[jj,ii] = REAL_CP(0.5) * (C[ii,jj] + C[jj,ii])
C[ii,jj] = C[jj,ii]
return C
@njit(fastmath=True, inline="always", parallel=True)
def device_transposed_matmul_and_make_symmetric_3d(J, C):
# C = J.T @ J
for ii in range(3):
for jj in range(3):
C[ii,jj] = REAL_CP(0.)
for kk in range(3):
C[ii,jj] += J[kk,ii] * J[kk,jj]
# C = (C + C.T)/2.
for ii in range(3):
for jj in range(ii, 3):
C[jj,ii] = REAL_CP(0.5) * (C[ii,jj] + C[jj,ii])
C[ii,jj] = C[jj,ii]
return C
@njit(fastmath=True, parallel=True, nogil=True)
def calculate_particle_ftle_and_fsle(xp0, yp0, zp0, xp1, yp1, zp1, ftle, fsle, T):
EPS = REAL_CP(1.192093e-07)
Nx, Ny, Nz = xp0.shape
dz = abs(zp0[0,0,1] - zp0[0,0,0])
sp0 = xp0 #np.zeros_like(xp0)
np0 = yp0 #np.zeros_like(yp0)
T_inv = REAL_CP(REAL_CP(1.)/REAL_CP(T))
distance_mean_factor = REAL_CP(0.038461538461538)
for ii in prange(Nx):
J = np.empty((3, 3), REAL_CP)
for jj in range(Ny):
for kk in range(Nz):
# X Derivatives
if ii == 0:
J[0, 0] = (xp1[ii+1,jj,kk] - xp1[ii,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii,jj,kk] + EPS)
J[1, 0] = (yp1[ii+1,jj,kk] - yp1[ii,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii,jj,kk] + EPS)
J[2, 0] = (zp1[ii+1,jj,kk] - zp1[ii,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii,jj,kk] + EPS)
elif ii == Nx - 1:
J[0, 0] = (xp1[ii,jj,kk] - xp1[ii-1,jj,kk]) / (sp0[ii,jj,kk] - sp0[ii-1,jj,kk] + EPS)
J[1, 0] = (yp1[ii,jj,kk] - yp1[ii-1,jj,kk]) / (sp0[ii,jj,kk] - sp0[ii-1,jj,kk] + EPS)
J[2, 0] = (zp1[ii,jj,kk] - zp1[ii-1,jj,kk]) / (sp0[ii,jj,kk] - sp0[ii-1,jj,kk] + EPS)
else:
J[0, 0] = (xp1[ii+1,jj,kk] - xp1[ii-1,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii-1,jj,kk] + EPS)
J[1, 0] = (yp1[ii+1,jj,kk] - yp1[ii-1,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii-1,jj,kk] + EPS)
J[2, 0] = (zp1[ii+1,jj,kk] - zp1[ii-1,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii-1,jj,kk] + EPS)
# Y Derivatives
if jj == 0:
J[0, 1] = (xp1[ii,jj+1,kk] - xp1[ii,jj,kk]) / (np0[ii,jj+1,kk] - np0[ii,jj,kk] + EPS)
J[1, 1] = (yp1[ii,jj+1,kk] - yp1[ii,jj,kk]) / (np0[ii,jj+1,kk] - np0[ii,jj,kk] + EPS)
J[2, 1] = (zp1[ii,jj+1,kk] - zp1[ii,jj,kk]) / (np0[ii,jj+1,kk] - np0[ii,jj,kk] + EPS)
elif jj == Ny - 1:
J[0, 1] = (xp1[ii,jj,kk] - xp1[ii,jj-1,kk]) / (np0[ii,jj,kk] - np0[ii,jj-1,kk] + EPS)
J[1, 1] = (yp1[ii,jj,kk] - yp1[ii,jj-1,kk]) / (np0[ii,jj,kk] - np0[ii,jj-1,kk] + EPS)
J[2, 1] = (zp1[ii,jj,kk] - zp1[ii,jj-1,kk]) / (np0[ii,jj,kk] - np0[ii,jj-1,kk] + EPS)
else:
h1 = (np0[ii,jj+1,kk] - np0[ii,jj,kk])
h2 = (np0[ii,jj,kk] - np0[ii,jj-1,kk])
alpha = h1 / h2
dy = h1 * (1. + alpha) + EPS
J[0, 1] = (xp1[ii,jj+1,kk] - alpha**2 * xp1[ii,jj-1,kk] - (1 - alpha**2) * xp1[ii,jj,kk]) / dy
J[1, 1] = (yp1[ii,jj+1,kk] - alpha**2 * yp1[ii,jj-1,kk] - (1 - alpha**2) * yp1[ii,jj,kk]) / dy
J[2, 1] = (zp1[ii,jj+1,kk] - alpha**2 * zp1[ii,jj-1,kk] - (1 - alpha**2) * zp1[ii,jj,kk]) / dy
# Z Derivatives
if kk == 0:
J[0, 2] = (xp1[ii,jj,1] - xp1[ii,jj,Nz - 1]) / (2 * dz + EPS)
J[1, 2] = (yp1[ii,jj,1] - yp1[ii,jj,Nz - 1]) / (2 * dz + EPS)
J[2, 2] = (zp1[ii,jj,kk+1] - zp1[ii,jj,kk]) / (dz + EPS)
elif kk == Nz - 1:
J[0, 2] = (xp1[ii,jj,0] - xp1[ii,jj,kk-1]) / (2 * dz + EPS)
J[1, 2] = (yp1[ii,jj,0] - yp1[ii,jj,kk-1]) / (2 * dz + EPS)
J[2, 2] = (zp1[ii,jj,kk] - zp1[ii,jj,kk-1]) / (dz + EPS)
else:
J[0, 2] = (xp1[ii,jj,kk+1] - xp1[ii,jj,kk-1]) / (2 * dz + EPS)
J[1, 2] = (yp1[ii,jj,kk+1] - yp1[ii,jj,kk-1]) / (2 * dz + EPS)
J[2, 2] = (zp1[ii,jj,kk+1] - zp1[ii,jj,kk-1]) / (2 * dz + EPS)
if (0 < ii) and (ii < Nx - 1) and (0 < jj) and (jj < Ny - 1) and (0 < kk) and (kk < Nz - 1):
for _i in range(-1, 2):
for _j in range(-1, 2):
for _k in range(-1, 2):
if (_i != 0) or (_j != 0) or (_k != 0):
d0 = sqrt(
(xp0[ii+_i,jj+_j,kk+_k] - xp0[ii,jj,kk])*(xp0[ii+_i,jj+_j,kk+_k] - xp0[ii,jj,kk]) +
(yp0[ii+_i,jj+_j,kk+_k] - yp0[ii,jj,kk])*(yp0[ii+_i,jj+_j,kk+_k] - yp0[ii,jj,kk]) +
(zp0[ii+_i,jj+_j,kk+_k] - zp0[ii,jj,kk])*(zp0[ii+_i,jj+_j,kk+_k] - zp0[ii,jj,kk])
)
d1 = sqrt(
(xp1[ii+_i,jj+_j,kk+_k] - xp1[ii,jj,kk])*(xp1[ii+_i,jj+_j,kk+_k] - xp1[ii,jj,kk]) +
(yp1[ii+_i,jj+_j,kk+_k] - yp1[ii,jj,kk])*(yp1[ii+_i,jj+_j,kk+_k] - yp1[ii,jj,kk]) +
(zp1[ii+_i,jj+_j,kk+_k] - zp1[ii,jj,kk])*(zp1[ii+_i,jj+_j,kk+_k] - zp1[ii,jj,kk])
)
fsle[ii, jj, kk] += distance_mean_factor * T_inv * log(d1 / d0)
#_C = device_transposed_matmul_and_make_symmetric_3d(J, _C)
C = J.T @ J #np.matmul(J.T, J, out=_C)
C = (C + C.T) * REAL_CP(0.5)
# power_iteration_3d(_C, b_k, b_k1)
ftle[ii, jj, kk] = log( sqrt( np.max(np.linalg.eigvalsh(C)) )) * T_inv
@njit(fastmath=True, parallel=True, nogil=True)
def calculate_particle_ftle_2d(xp0, yp0, zp0, xp1, yp1, zp1, C, N, dt):
EPS = REAL_CP(1e-6)
Nx, Ny, Nz = xp0.shape
sp0 = xp0 #np.zeros_like(xp0)
np0 = yp0 #np.zeros_like(yp0)
T = (N * dt)
kk = 0
J = np.empty((2, 2), REAL_CP)
_C = np.empty((2, 2), REAL_CP)
for ii in prange(Nx):
for jj in range(Ny):
# X Derivatives
if ii == 0:
J[0, 0] = (xp1[ii+1,jj,kk] - xp1[ii,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii,jj,kk] + EPS)
J[1, 0] = (yp1[ii+1,jj,kk] - yp1[ii,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii,jj,kk] + EPS)
elif ii == Nx - 1:
J[0, 0] = (xp1[ii,jj,kk] - xp1[ii-1,jj,kk]) / (sp0[ii,jj,kk] - sp0[ii-1,jj,kk] + EPS)
J[1, 0] = (yp1[ii,jj,kk] - yp1[ii-1,jj,kk]) / (sp0[ii,jj,kk] - sp0[ii-1,jj,kk] + EPS)
else:
J[0, 0] = (xp1[ii+1,jj,kk] - xp1[ii-1,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii-1,jj,kk] + EPS)
J[1, 0] = (yp1[ii+1,jj,kk] - yp1[ii-1,jj,kk]) / (sp0[ii+1,jj,kk] - sp0[ii-1,jj,kk] + EPS)
# Y Derivatives
if jj == 0:
J[0, 1] = (xp1[ii,jj+1,kk] - xp1[ii,jj,kk]) / (np0[ii,jj+1,kk] - np0[ii,jj,kk] + EPS)
J[1, 1] = (yp1[ii,jj+1,kk] - yp1[ii,jj,kk]) / (np0[ii,jj+1,kk] - np0[ii,jj,kk] + EPS)
elif jj == Ny - 1:
J[0, 1] = (xp1[ii,jj,kk] - xp1[ii,jj-1,kk]) / (np0[ii,jj,kk] - np0[ii,jj-1,kk] + EPS)
J[1, 1] = (yp1[ii,jj,kk] - yp1[ii,jj-1,kk]) / (np0[ii,jj,kk] - np0[ii,jj-1,kk] + EPS)
else:
h1 = (np0[ii,jj+1,kk] - np0[ii,jj,kk])
h2 = (np0[ii,jj,kk] - np0[ii,jj-1,kk])
alpha = h1 / h2
dy = h1 * (1. + alpha) + EPS
J[0, 1] = (xp1[ii,jj+1,kk] - alpha**2 * xp1[ii,jj-1,kk] - (1 - alpha**2) * xp1[ii,jj,kk]) / dy
J[1, 1] = (yp1[ii,jj+1,kk] - alpha**2 * yp1[ii,jj-1,kk] - (1 - alpha**2) * yp1[ii,jj,kk]) / dy
_C = device_transposed_matmul_and_make_symmetric_2d(J, _C)
for i in range(2):
for j in range(2):
C[ii, jj, 0, i, j] = _C[i,j]
def write_viz_files(xp0, yp0, zp0, xp1, yp1, zp1, ftle, fsle, N, filename):
filename_ftle = filename + f".ftle"
filename_particles = filename + f".particles.{N}"
gridToVTK(filename_ftle, xp0, yp0, zp0, pointData={'FTLE': ftle, 'FSLE': fsle})
pointsToVTK(
filename_particles,
np.array(xp1.reshape((-1))),
np.array(yp1.reshape((-1))),
np.array(zp1.reshape((-1)))
)
return
@njit(parallel=True, fastmath=False, nogil=True)
def correct_ftle(U, ftle):
Nx, Ny, Nz = U.shape
for ii in prange(1, Nx-1):
for jj in range(1, Ny - 1):
for kk in range(Nz):
if np.abs(ftle[ii,jj,kk]) > 0.5:
ftle[ii-1:ii+1, jj-1:jj+2, kk] = 0.0
return ftle
def process_multiple_parallel_final_particle_info(filename, dt, output_file, viz_filename, x, y, z, make_2d_simulation = False, NFlowFields = None):
with h5py.File(filename, 'r') as f, \
h5py.File(output_file, 'w') as h5Out:
xyz_particle = f["Particle_xyz"]
x = f["refined_x"][...]
y = f["refined_y"][...]
z = f["refined_z"][...]
Nx, Ny, Nz = x.shape
NTimeSteps = xyz_particle.shape[0]
xp0 = xyz_particle[0, 0, :].reshape((Nx, Ny, Nz))
yp0 = xyz_particle[0, 1, :].reshape((Nx, Ny, Nz))
zp0 = xyz_particle[0, 2, :].reshape((Nx, Ny, Nz))
h5Out.create_dataset(
f"FTLE",
shape=(NTimeSteps-1, Nx, Ny, Nz),
dtype=REAL_NP,
)
h5Out.create_dataset(
f"FSLE",
shape=(NTimeSteps-1, Nx, Ny, Nz),
dtype=REAL_NP,
)
FTLE = h5Out['FTLE']
FSLE = h5Out['FSLE']
with ProgressBar(max_value=len(list(range(1, NTimeSteps))) + 1) as bar:
iteration = 0
for N in range(1,NTimeSteps):
bar.update(iteration)
sys.stdout.flush()
xp1 = xyz_particle[N, 0, :].reshape((Nx, Ny, Nz))
yp1 = xyz_particle[N, 1, :].reshape((Nx, Ny, Nz))
zp1 = xyz_particle[N, 2, :].reshape((Nx, Ny, Nz))
if make_2d_simulation:
C = np.zeros((Nx, Ny, 1, 2, 2), dtype=REAL_NP)
calculate_ftle = calculate_particle_ftle_2d
else:
ftle = np.zeros((Nx, Ny, Nz), dtype=REAL_NP)
fsle = np.zeros((Nx, Ny, Nz), dtype=REAL_NP)
calculate_ftle_and_fsle = calculate_particle_ftle_and_fsle
T = REAL_NP(1.0 / (N * NFlowFields * dt))
calculate_ftle_and_fsle(xp0, yp0, zp0, xp1, yp1, zp1, ftle, fsle, T)
ftle = np.array(ftle)
fsle = np.array(fsle)
ftle = np.nan_to_num(ftle, nan=0.0, posinf=0.0, neginf=0.0)
fsle = np.nan_to_num(fsle, nan=0.0, posinf=0.0, neginf=0.0)
if T > 0:
ftle -= np.min(ftle)
ftle /= np.max(ftle)
fsle -= np.min(fsle)
fsle /= np.max(fsle)
else:
ftle -= np.max(ftle)
ftle /= abs(np.min(ftle))
fsle -= np.max(fsle)
fsle /= abs(np.min(fsle))
ftle[:, -3:, :] = ftle[:, -3, np.newaxis, :]
ftle[:, :, -3:] = ftle[:, :, np.newaxis, -3]
ftle[:, :, :3] = ftle[:, :, np.newaxis, 3]
fsle[:, -3:, :] = fsle[:, -3, np.newaxis, :]
fsle[:, :, -3:] = fsle[:, :, np.newaxis, -3]
fsle[:, :, :3] = fsle[:, :, np.newaxis, 3]
FTLE[N-1,...] = ftle
FSLE[N-1,...] = fsle
write_viz_files(x, y, z, xp1, yp1, zp1, ftle, fsle, int(N*NFlowFields), viz_filename)
iteration += 1
bar.update(iteration)
sys.stdout.flush()
return
if __name__ == "__main__":
RUNSIM = True
CALCULATE_FTLE = True
NFIELDS = 11 # Number of Flow Fields to reach ~t^+ = 40
UPSCALE = 100 #20 # 8 additional flow fields between every real field.
# Set this value to 1 to avoid upscaling.
FLOWFIELDS = 1 # For a dynamic FTLE, set this number to the desired number of FTLEs
SKIP = 1 # If you require skipping underlying flow fields, set this number to the desired value.
MAKE_2D_SIMULATION = False # Particle advection can neglect spanwise valocity.
WRITE_ALL_TIME_STEPS = False # Write intermediate steps for particle advection visualization.
# CONFIGS Allows for a list of refinement values along x, y, z directions
CONFIGS = [
(4, 4, 4), # 415.8M particles
]
FCENTERS = list(range(0, FLOWFIELDS, SKIP))
DIRECTIONS = [-1,1] # Backward and Forw
for X_UP, Y_UP, Z_UP in CONFIGS:
master_print("*"*100)
master_print("*"*100)
master_print("-"*100)
master_print("-"*100)
master_print("Configuration:")
master_print(f" + ({X_UP}, {Y_UP}, {Z_UP})")
# Configure Base Directory
BASE_DIR = "./SUBSET_30"
CASE_NAME = "ZPG_LOW_RE"
MACH_MOD = "INCOMPRESSIBLE"
WALL_CONDITION = "Adiabatic"
coord_path = f"{BASE_DIR}/coord_1_440.txt.h5"
filenames_full = sorted(
glob.glob(f"{BASE_DIR}/PUVWT*.h5")
)
Ntime = len(filenames_full)
t1 = time.time()
with h5py.File(coord_path, "r", driver='mpio', comm=MPI.COMM_WORLD) as hf:
dataset = hf["dataset"]
Nx, Ny, _, Nz = dataset.shape
x = dataset.astype(REAL_NP)[:,:,0,:].reshape((Nx,Ny,Nz))
y = dataset.astype(REAL_NP)[:,:,1,:].reshape((Nx,Ny,Nz))
z = dataset.astype(REAL_NP)[:,:,2,:].reshape((Nx,Ny,Nz))
master_print(f"(Nx, Ny, Nz) = ({Nx}, {Ny}, {Nz})")
PARTICLE_COUNT = X_UP * Y_UP * Z_UP * Nx * Ny * Nz
master_print(f"Particle Count = {PARTICLE_COUNT}")
t2 = time.time()
master_print(f"Time Elapsed Reading Coordinates: {t2 - t1} s")
sys.stdout.flush()
for DIRECTION in DIRECTIONS:
for FLOW_CENTER in FCENTERS:
master_print(f"Global Step: {FLOW_CENTER}")
gc.collect()
dt = DIRECTION * 2.00E-03 * 10
FIELDS = Ntime // 2 - 2
filenames = filenames_full[Ntime//2 : (Ntime//2) * (DIRECTION * FIELDS) : DIRECTION]
CURRENT_ID = os.path.split(filenames[FLOW_CENTER])[1][:-3]
if DIRECTION == 1:
output_filename = f"./Temporal/ParticleTracking_{CASE_NAME}_{X_UP}x_{Y_UP}x_{Z_UP}x_{PARTICLE_COUNT}P_{NFIELDS}Fields_TEMPORAL_UPS{UPSCALE}_{MACH_MOD}_{WALL_CONDITION}.forward.gpu.skip.step.{CURRENT_ID}.h5"
ftle_filename = f"./Temporal/ParticleTracking_{CASE_NAME}_{X_UP}x_{Y_UP}x_{Z_UP}x_{PARTICLE_COUNT}P_{NFIELDS}Fields_TEMPORAL_UPS{UPSCALE}_{MACH_MOD}_{WALL_CONDITION}.forward.ftle.gpu.skip.step.{CURRENT_ID}.h5"
viz_filename = f"./Temporal/PV_VIZ/ParticleTracking_{CASE_NAME}_{X_UP}x_{Y_UP}x_{Z_UP}x_{PARTICLE_COUNT}P_{NFIELDS}Fields_TEMPORAL_UPS{UPSCALE}_{MACH_MOD}_{WALL_CONDITION}.forward.gpu.skip.step.{CURRENT_ID}"
elif DIRECTION == -1:
output_filename = f"./Temporal/ParticleTracking_{CASE_NAME}_{X_UP}x_{Y_UP}x_{Z_UP}x_{PARTICLE_COUNT}P_{NFIELDS}Fields_TEMPORAL_UPS{UPSCALE}_{MACH_MOD}_{WALL_CONDITION}.backward.gpu.skip.step.{CURRENT_ID}.h5"
ftle_filename = f"./Temporal/ParticleTracking_{CASE_NAME}_{X_UP}x_{Y_UP}x_{Z_UP}x_{PARTICLE_COUNT}P_{NFIELDS}Fields_TEMPORAL_UPS{UPSCALE}_{MACH_MOD}_{WALL_CONDITION}.backward.ftle.gpu.skip.step.{CURRENT_ID}.h5"
viz_filename = f"./Temporal/PV_VIZ/ParticleTracking_{CASE_NAME}_{X_UP}x_{Y_UP}x_{Z_UP}x_{PARTICLE_COUNT}P_{NFIELDS}Fields_TEMPORAL_UPS{UPSCALE}_{MACH_MOD}_{WALL_CONDITION}.backward.gpu.skip.step.{CURRENT_ID}"
if RUNSIM:
t3 = time.time()
particle_simulator(
filenames,