forked from ludwig-cf/ludwig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollision.c
3138 lines (2620 loc) · 115 KB
/
collision.c
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
/*****************************************************************************
*
* collision.c
*
* Collision stage routines and associated data.
*
* Isothermal fluctuations following Adhikari et al., Europhys. Lett
* (2005).
*
* The relaxation times can be set to give either 'm10', BGK or
* 'two-relaxation' time (TRT) models.
*
* Edinburgh Soft Matter and Statistical Physics Group and
* Edinburgh Parallel Computing Centre
*
* (c) 2011-2024 The University of Edinburgh
*
* Contributing authors:
* Kevin Stratford ([email protected])
* Alan Gray ([email protected])
*
*****************************************************************************/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "pe.h"
#include "util.h"
#include "physics.h"
#include "free_energy.h"
#include "visc.h"
#include "control.h"
#include "collision.h"
#include "kernel.h"
#include "timer.h"
#include "symmetric.h"
__global__ void lb_collision_mrt1(kernel_3d_v_t k3v, lb_t * lb,
hydro_t * hydro,
map_t * map, noise_t * noise, fe_t * fe);
__global__ void lb_collision_mrt2(kernel_3d_v_t k3d, lb_t * lb,
hydro_t * hydro,
fe_symm_t * fe, noise_t * noise);
int lb_collision_mrt(lb_t * lb, hydro_t * hydro, map_t * map,
noise_t * noise, fe_t * fe, visc_t * visc);
int lb_collision_binary(lb_t * lb, hydro_t * hydro, noise_t * noise,
fe_symm_t * fe, visc_t * visc);
static __host__ __device__
void lb_collision_fluctuations(lb_t * lb, noise_t * noise, int index,
double kt,
double shat[3][3], double ghat[NVEL]);
int lb_collision_noise_var_set(lb_t * lb, noise_t * noise);
static __host__ int lb_collision_parameters_commit(lb_t * lb, visc_t * visc);
static __device__
void lb_collision_mrt1_site(lb_t * lb, hydro_t * hydro, map_t * map,
noise_t * noise, fe_t * fe, const int index0);
static __device__
void lb_collision_mrt2_site(lb_t * lb, hydro_t * hydro, fe_symm_t * fe,
noise_t * noise, const int index0);
__device__ void d3q19_f2mode_chunk(double* mode, const double* __restrict__ fchunk);
__device__ void d3q19_mode2f_chunk(double* mode, double* fchunk);
__device__ void d3q19_mode2f_phi(double jdotc[NSIMDVL],
double sphidotq[NSIMDVL],
double sphi[3][3][NSIMDVL],
double phi[NSIMDVL],
double jphi[3][NSIMDVL],
double * f, int baseIndex);
/* Additional file scope collide time constants */
typedef struct collide_param_s collide_param_t;
struct collide_param_s {
double force_global[3];
double kt;
double mobility;
double rtau2;
double eta_shear;
double eta_bulk;
int have_visc_model;
};
static __constant__ lb_collide_param_t _lbp;
static __constant__ collide_param_t _cp;
/* Todo. Better unit tests required for these functions. */
__host__ __device__ int lb_nrelax_valid(lb_relaxation_enum_t nrelax);
__host__ __device__ int lb_relaxation_time_shear(lb_t * lb,
double eta,
double * rtau);
__host__ __device__ int lb_relaxation_time_bulk(lb_t * lb,
double eta,
double eta_bulk,
double * rtau_bulk);
__host__ __device__ int lb_relaxation_time_ghosts(lb_t * lb,
double eta,
double * rtau);
__host__ __device__ double lb_fluctuations_var_eta(double tau, double kt);
__host__ __device__ double lb_fluctuations_var_bulk(double tau, double kt);
__host__ __device__ int lb_fluctuations_var_ghost(double * rna, double * rtau,
double kt,
double * var);
__host__ __device__ int lb_fluctuations_stress(noise_t * noise, int index,
double var_eta,
double var_eta_bulk,
double shat[3][3]);
__host__ __device__ int lb_fluctuations_ghosts(noise_t * noise, int index,
double * var_ghost,
double ghat[NVEL]);
/* Some vectorised versions */
__host__ __device__ int lb_relaxation_time_shear_v(lb_t * lb,
const double eta[NSIMDVL],
double rtau[NSIMDVL]);
__host__ __device__ int lb_relaxation_time_bulk_v(lb_t * lb,
const double eta[NSIMDVL],
const double eta_nu[NSIMDVL],
double rtau[NSIMDVL]);
__host__ __device__ int lb_relaxation_time_ghosts_v(lb_t * lb,
const double eta[NSIMDVL],
double rtau[NVEL][NSIMDVL]);
/*****************************************************************************
*
* lb_collide
*
* Driver routine for the collision stage.
*
* We allow hydro to be NULL, in which case there is no hydrodynamics!
*
*****************************************************************************/
__host__
int lb_collide(lb_t * lb, hydro_t * hydro, map_t * map, noise_t * noise,
fe_t * fe, visc_t * visc) {
int ndist;
if (hydro == NULL) return 0;
assert(lb);
assert(map);
lb_ndist(lb, &ndist);
lb_collision_relaxation_times_set(lb);
lb_collision_noise_var_set(lb, noise);
lb_collide_param_commit(lb);
if (ndist == 1) lb_collision_mrt(lb, hydro, map, noise, fe, visc);
if (ndist == 2) lb_collision_binary(lb, hydro, noise, (fe_symm_t *) fe, visc);
return 0;
}
/*****************************************************************************
*
* lb_collision_mrt_site
*
* Single fluid collision driver (multiple relaxation time).
*
*****************************************************************************/
__host__ int lb_collision_mrt(lb_t * lb, hydro_t * hydro, map_t * map,
noise_t * noise, fe_t * fe, visc_t * visc) {
int nlocal[3] = {0};
fe_t * fetarget = NULL;
noise_t * noisetarget = NULL;
assert(lb);
assert(hydro);
assert(map);
cs_nlocal(lb->cs, nlocal);
/* Local extent */
{
dim3 nblk = {};
dim3 ntpb = {};
cs_limits_t lim = {1, nlocal[X], 1, nlocal[Y], 1, nlocal[Z]};
kernel_3d_v_t k3v = kernel_3d_v(lb->cs, lim, NSIMDVL);
kernel_3d_launch_param(k3v.kiterations, &nblk, &ntpb);
lb_collision_parameters_commit(lb, visc);
if (fe) fe->func->target(fe, &fetarget);
if (noise) noisetarget = noise->target;
TIMER_start(TIMER_COLLIDE_KERNEL);
tdpLaunchKernel(lb_collision_mrt1, nblk, ntpb, 0, 0,
k3v, lb->target, hydro->target, map->target,
noisetarget, fetarget);
tdpAssert(tdpPeekAtLastError());
tdpAssert(tdpDeviceSynchronize());
TIMER_stop(TIMER_COLLIDE_KERNEL);
}
return 0;
}
/*****************************************************************************
*
* lb_collision_mrt1
*
* Kernel driver for thread-decomposed collision routine; this generates
* a loop over all lattice sites.
*
*****************************************************************************/
__global__ void lb_collision_mrt1(kernel_3d_v_t k3v, lb_t * lb,
hydro_t * hydro,
map_t * map, noise_t * noise, fe_t * fe) {
int kindex = 0;
for_simt_parallel(kindex, k3v.kiterations, NSIMDVL) {
int index0 = k3v.kindex0 + kindex;
lb_collision_mrt1_site(lb, hydro, map, noise, fe, index0);
}
return;
}
/*****************************************************************************
*
* lb_collision_mrt1_site
*
* Collision with (potentially) different relaxation times for each
* different mode.
*
* This code is per lattice site. To be called from
* lb_collision_mrt1().
*
* The matrices ma and mi project the distributions onto the
* modes, and vice-versa, respectively, for the current LB model.
*
* The collision conserves density, and momentum (to within any
* body force present). The stress modes, and ghost modes, are
* relaxed toward their equilibrium values.
*
*****************************************************************************/
static __device__
void lb_collision_mrt1_site(lb_t * lb, hydro_t * hydro, map_t * map,
noise_t * noise, fe_t * fe, const int index0) {
int p, m; /* velocity index */
int ia, ib; /* indices ("alphabeta") */
int iv=0; /* SIMD loop counter */
double mode[NVEL*NSIMDVL]; /* Modes; hydrodynamic + ghost */
double rho[NSIMDVL], rrho[NSIMDVL]; /* Density, reciprocal density */
double u[3][NSIMDVL]; /* Velocity */
double s[3][3][NSIMDVL]; /* Stress */
double seq[3][3][NSIMDVL]; /* Equilibrium stress */
double shat[3][3][NSIMDVL]; /* random stress */
double ghat[NVEL][NSIMDVL]; /* noise for ghosts */
double eta[NSIMDVL]; /* Local shear viscosity */
double eta_bulk[NSIMDVL]; /* Local bulk viscosity */
double rtau[NSIMDVL]; /* Local 1/shear relaxation tau */
double rtau_bulk[NSIMDVL]; /* Local 1/bulk relaxation tau */
double rtau_ghost[NVEL][NSIMDVL]; /* Ghost relaxation times rtau */
double force[3][NSIMDVL]; /* External force */
double tr_s[NSIMDVL], tr_seq[NSIMDVL]; /* Vectors for stress trace */
double fchunk[NVEL*NSIMDVL]; /* 1-d SIMD distribution vector */
char fullchunk=1;
char includeSite[NSIMDVL];
const double rdim = (1.0/NDIM); /* 1 / dimension */
KRONECKER_DELTA_CHAR(d); /* delta_ab */
assert(lb);
assert(lb->param);
assert(hydro);
/* Determine whether this chunk of lattice sites are all active
* and if not, which should be included */
for_simd_v(iv, NSIMDVL) includeSite[iv] = 1;
for_simd_v(iv, NSIMDVL) {
if (map->status[index0+iv] != MAP_FLUID) {
includeSite[iv] = 0;
fullchunk = 0;
}
}
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) u[ia][iv] = 0.0;
}
/* Default to fluctuations off; shat, ghat are zero */
for (ia = 0; ia < 3; ia++) {
for (ib = 0; ib < 3; ib++) {
for_simd_v(iv, NSIMDVL) shat[ia][ib][iv] = 0.0;
}
}
for (ia = 0; ia < NVEL; ia++) {
for_simd_v(iv, NSIMDVL) ghat[ia][iv] = 0.0;
}
/* Load SIMD vectors for distribution and force */
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) fchunk[p*NSIMDVL+iv] =
lb->f[ LB_ADDR(_lbp.nsite, 1, NVEL, index0 + iv, LB_RHO, p) ];
}
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) {
force[ia][iv] = _cp.force_global[ia]
+ hydro->force->data[addr_rank1(hydro->nsite, 3, index0+iv, ia)];
}
}
/* Compute all the modes */
#ifdef _D3Q19_
d3q19_f2mode_chunk(mode, fchunk);
#else
for (m = 0; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) mode[m*NSIMDVL+iv] = 0.0;
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) {
mode[m*NSIMDVL+iv] += fchunk[p*NSIMDVL+iv]*_lbp.ma[m][p];
}
}
}
#endif
/* For convenience, write out the physical modes, that is,
* rho, NDIM components of velocity, independent components
* of stress (upper triangle), and lower triangle. */
for_simd_v(iv, NSIMDVL) rho[iv] = mode[0*NSIMDVL+iv];
for (ia = 0; ia < NDIM; ia++) {
for_simd_v(iv, NSIMDVL) u[ia][iv] = mode[(1 + ia)*NSIMDVL+iv];
}
m = 0;
for (ia = 0; ia < NDIM; ia++) {
for (ib = ia; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) s[ia][ib][iv] = mode[(1 + NDIM + m)*NSIMDVL+iv];
m++;
}
}
for (ia = 1; ia < NDIM; ia++) {
for (ib = 0; ib < ia; ib++) {
for_simd_v(iv, NSIMDVL) s[ia][ib][iv] = s[ib][ia][iv];
}
}
/* Compute the local velocity, taking account of any body force */
for_simd_v(iv, NSIMDVL) rrho[iv] = 1.0/rho[iv];
for (ia = 0; ia < NDIM; ia++) {
for_simd_v(iv, NSIMDVL) {
u[ia][iv] = rrho[iv]*(u[ia][iv] + 0.5*force[ia][iv]);
}
}
/* Local relaxation times */
if (_cp.have_visc_model == 0) {
/* Fixed viscosity */
for_simd_v(iv, NSIMDVL) {
eta[iv] = _cp.eta_shear;
eta_bulk[iv] = _cp.eta_bulk;
}
}
else {
/* Use viscosity model values hydro->eta */
/* Bulk viscosity will be (eta_bulk/eta_shear)_newtonian*eta_local */
for_simd_v(iv, NSIMDVL) {
eta[iv] = hydro->eta->data[addr_rank0(hydro->nsite, index0+iv)];
eta_bulk[iv] = (_cp.eta_bulk/_cp.eta_shear)*eta[iv];
}
}
lb_relaxation_time_shear_v(lb, eta, rtau);
lb_relaxation_time_bulk_v(lb, eta, eta_bulk, rtau_bulk);
lb_relaxation_time_ghosts_v(lb, eta, rtau_ghost);
/* Relax stress with different shear and bulk viscosity */
for_simd_v(iv, NSIMDVL) {
tr_s[iv] = 0.0;
tr_seq[iv] = 0.0;
}
if (fe && fe->use_stress_relaxation) {
double symm[3][3][NSIMDVL];
fe->func->str_symm_v(fe, index0, symm);
for (ia = 0; ia < NDIM; ia++) {
/* Set equilibrium stress */
for (ib = 0; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) {
seq[ia][ib][iv] = rho[iv]*u[ia][iv]*u[ib][iv] + symm[ia][ib][iv];
}
}
/* Compute trace */
for_simd_v(iv, NSIMDVL){
tr_s[iv] += s[ia][ia][iv];
tr_seq[iv] += seq[ia][ia][iv];
}
}
}
else {
for (ia = 0; ia < NDIM; ia++) {
/* Set equilibrium stress */
for (ib = 0; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) {
seq[ia][ib][iv] = rho[iv]*u[ia][iv]*u[ib][iv];
}
}
/* Compute trace */
for_simd_v(iv, NSIMDVL){
tr_s[iv] += s[ia][ia][iv];
tr_seq[iv] += seq[ia][ia][iv];
}
}
}
/* Form traceless parts */
for (ia = 0; ia < NDIM; ia++) {
for_simd_v(iv, NSIMDVL){
s[ia][ia][iv] -= rdim*tr_s[iv];
seq[ia][ia][iv] -= rdim*tr_seq[iv];
}
}
/* Relax each mode */
for_simd_v(iv, NSIMDVL) {
tr_s[iv] = tr_s[iv] - rtau_bulk[iv]*(tr_s[iv] - tr_seq[iv]);
}
for (ia = 0; ia < NDIM; ia++) {
for (ib = 0; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) {
s[ia][ib][iv] -= rtau[iv]*(s[ia][ib][iv] - seq[ia][ib][iv]);
s[ia][ib][iv] += d[ia][ib]*rdim*tr_s[iv];
/* Correction from body force (assumes equal relaxation times) */
s[ia][ib][iv] += (2.0 - rtau[iv])
*(u[ia][iv]*force[ib][iv] + force[ia][iv]*u[ib][iv]);
}
}
}
if (lb->param->noise) {
double shat1[3][3];
double ghat1[NVEL];
double var, var_bulk;
double rtau_ghost_tmp[NVEL];
double var_ghost[NVEL];
/* This does not vectorise at the moment. Needs revisiting.
* Note that there is a mask here to prevent random number
* generation at solid sites, which is to maintain results
* in regression tests. Not strictly necessary. */
for (iv = 0; iv < NSIMDVL; iv++) {
if (includeSite[iv]) {
var = lb_fluctuations_var_eta(1.0/rtau[iv], _cp.kt);
var_bulk = lb_fluctuations_var_bulk(1.0/rtau_bulk[iv], _cp.kt);
lb_fluctuations_stress(noise, index0 + iv, var, var_bulk, shat1);
for (ia = 0; ia < NDIM; ia++) {
for (ib = 0; ib < NDIM; ib++) {
shat[ia][ib][iv] = shat1[ia][ib];
}
}
if (lb->param->isghost == LB_GHOST_ON) {
/* TODO rtau_ghost to be vectorised */
for (ia = 0; ia < NVEL; ia++) {
rtau_ghost_tmp[ia] = rtau_ghost[ia][iv];
}
lb_fluctuations_var_ghost(lb->param->rna,
rtau_ghost_tmp, _cp.kt, var_ghost);
lb_fluctuations_ghosts(noise, index0 + iv, var_ghost, ghat1);
for (ia = 0; ia < NVEL; ia++) {
ghat[ia][iv] = ghat1[ia];
}
}
}
}
}
/* Now reset the hydrodynamic modes to post-collision values:
* rho is unchanged, velocity unchanged if no force,
* independent components of stress, and ghosts. */
for (ia = 0; ia < NDIM; ia++) {
for_simd_v(iv, NSIMDVL) mode[(1 + ia)*NSIMDVL+iv] += force[ia][iv];
}
m = 0;
for (ia = 0; ia < NDIM; ia++) {
for (ib = ia; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) {
mode[(1 + NDIM + m)*NSIMDVL+iv] = s[ia][ib][iv] + shat[ia][ib][iv];
}
m++;
}
}
/* Ghost modes are relaxed toward zero equilibrium. */
for (m = NHYDRO; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) {
mode[m*NSIMDVL+iv] = mode[m*NSIMDVL+iv]
- rtau_ghost[m][iv]*(mode[m*NSIMDVL+iv] - 0.0) + ghat[m][iv];
}
}
/* Project post-collision modes back onto the distribution */
#ifdef _D3Q19_
d3q19_mode2f_chunk(mode, fchunk);
#else
for (p = 0; p < NVEL; p++) {
double ftmp[NSIMDVL];
for_simd_v(iv, NSIMDVL) ftmp[iv] = 0.0;
for (m = 0; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) ftmp[iv] += _lbp.mi[p][m]*mode[m*NSIMDVL+iv];
}
for_simd_v(iv, NSIMDVL) fchunk[p*NSIMDVL+iv] = ftmp[iv];
}
#endif
/* Write SIMD chunks back to main arrays. */
if (fullchunk) {
/* distribution */
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) {
lb->f[LB_ADDR(_lbp.nsite, _lbp.ndist, NVEL, index0+iv, LB_RHO, p)] = fchunk[p*NSIMDVL+iv];
}
}
/* density */
for_simd_v(iv, NSIMDVL) {
hydro->rho->data[addr_rank0(hydro->nsite, index0+iv)] = rho[iv];
}
/* velocity */
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) {
hydro->u->data[addr_rank1(hydro->nsite, 3, index0+iv, ia)] = u[ia][iv];
}
}
}
else {
for_simd_v(iv, NSIMDVL) {
if (includeSite[iv]) {
/* distribution */
for (p = 0; p < NVEL; p++) {
lb->f[LB_ADDR(_lbp.nsite, _lbp.ndist, NVEL, index0 + iv, LB_RHO, p)]
= fchunk[p*NSIMDVL+iv];
}
/* velocity */
for (ia = 0; ia < 3; ia++) {
int haddr = addr_rank1(hydro->nsite, 3, index0 + iv, ia);
hydro->u->data[haddr] = u[ia][iv];
}
}
}
}
return;
}
/*****************************************************************************
*
* lb_collision_binary
*
* Driver for 2-distribution binary fluid collision retained as an
* option for symmetric free energy (only).
*
*****************************************************************************/
__host__ int lb_collision_binary(lb_t * lb, hydro_t * hydro, noise_t * noise,
fe_symm_t * fe, visc_t * visc) {
int nlocal[3] = {0};
assert (NDIM == 3); /* NDIM = 2 warrants additional tests here. */
assert(lb);
assert(hydro);
assert(fe);
cs_nlocal(lb->cs, nlocal);
/* Kernel extent */
{
dim3 nblk = {};
dim3 ntpb = {};
cs_limits_t lim = {1, nlocal[X], 1, nlocal[Y], 1, nlocal[Z]};
kernel_3d_v_t k3v = kernel_3d_v(lb->cs, lim, NSIMDVL);
kernel_3d_launch_param(k3v.kiterations, &nblk, &ntpb);
lb_collision_parameters_commit(lb, visc);
TIMER_start(TIMER_COLLIDE_KERNEL);
tdpLaunchKernel(lb_collision_mrt2, nblk, ntpb, 0, 0,
k3v, lb->target, hydro->target, fe->target,
(noise) ? noise->target : NULL);
tdpAssert(tdpPeekAtLastError());
tdpAssert(tdpDeviceSynchronize());
TIMER_stop(TIMER_COLLIDE_KERNEL);
}
return 0;
}
/*****************************************************************************
*
* lb_collision_mrt2
*
* This is the kernel function. The actual work is on a per-site
* basis in lb_collision_mrt2_site as a convenience.
*
*****************************************************************************/
__global__ void lb_collision_mrt2(kernel_3d_v_t k3v, lb_t * lb,
hydro_t * hydro, fe_symm_t * fe,
noise_t * noise) {
int kindex = 0;
for_simt_parallel(kindex, k3v.kiterations, NSIMDVL) {
int index0 = k3v.kindex0 + kindex;
lb_collision_mrt2_site(lb, hydro, fe, noise, index0);
}
return;
}
/*****************************************************************************
*
* lb_collision_mrt2_site
*
* Binary LB collision stage.
*
* This follows the single fluid version above, with the addition
* that the equilibrium stress includes the thermodynamic term
* following Swift etal.
*
* We also have to update the second distribution g from the
* order parameter modes phi, jphi[3], sphi[3][3].
*
* There are two choices:
* 1. relax jphi[i] toward equilibrium phi*u[i] at rate rtau2
* AND
* fix sphi[i][j] = phi*u[i]*u[j] + mu*d_[i][j]
* so the mobility enters through rtau2 (J. Stat. Phys. 2005).
* 2.
* fix jphi[i] = phi*u[i] (i.e. relaxation time == 1.0)
* AND
* fix sphi[i][j] = phi*u[i]*u[j] + mobility*mu*d_[i][j]
* so the mobility enters with chemical potential (Kendon etal 2001).
*
* Note there is an extra factor of cs^2 before the mobility,
* which should be taken into account if quoting the actual
* mobility. The factor is somewhat arbitrary and is there
* to try to ensure both methods are stable for given input
* mobility.
*
* As there seems to be little to choose between the two in terms of
* results, I prefer 2, as it avoids the calculation of jphi[i] from
* from the distributions g. However, keep 1 so tests don't break!
*
* However, for asymmetric quenches version 1 may be preferred.
*
* The reprojection of g moves phi (mostly) into the non-propagating
* distribution following J. Stat. Phys. (2005).
*
*****************************************************************************/
#define NDIST 2 /* for binary collision */
__device__ void lb_collision_mrt2_site(lb_t * lb, hydro_t * hydro,
fe_symm_t * fe, noise_t * noise,
const int index0) {
int ia, ib, m, p;
double f[NVEL*NSIMDVL];
double mode[NVEL*NSIMDVL]; /* Modes; hydrodynamic + ghost */
double rho[NSIMDVL];
double rrho[NSIMDVL]; /* Density, reciprocal density */
double u[3][NSIMDVL]; /* Velocity */
double s[3][3][NSIMDVL]; /* Stress */
double seq[3][3][NSIMDVL]; /* equilibrium stress */
double shat[3][3][NSIMDVL]; /* random stress */
double ghat[NVEL][NSIMDVL]; /* noise for ghosts */
double force[3][NSIMDVL]; /* External force */
double tr_s[NSIMDVL]; /* Trace of stress */
double tr_seq[NSIMDVL]; /* Equilibrium value thereof */
double phi[NSIMDVL]; /* phi */
double jphi[3][NSIMDVL]; /* phi flux */
double jdotc[NSIMDVL]; /* Contraction jphi_a cv_ia */
double sphidotq[NSIMDVL]; /* phi second moment */
double sth[3][3][NSIMDVL]; /* stress */
double sphi[3][3][NSIMDVL]; /* stress */
double mu[NSIMDVL]; /* Chemical potential */
const double r3 = 1.0/3.0;
KRONECKER_DELTA_CHAR(d);
/* index for SIMD vectors */
int iv=0;
assert(lb);
assert(hydro);
/* switch fluctuations off */
for (ia = 0; ia < 3; ia++) {
for (ib = 0; ib < 3; ib++) {
for_simd_v(iv, NSIMDVL) shat[ia][ib][iv] = 0.0;
}
}
for (m = NHYDRO; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) ghat[m][iv] = 0.0;
}
#ifdef _D3Q19_
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) {
f[p*NSIMDVL+iv]
= lb->f[LB_ADDR(_lbp.nsite, _lbp.ndist, NVEL, index0 + iv, LB_RHO, p)];
}
}
d3q19_f2mode_chunk(mode, f);
#else
/* Compute all the modes */
for (m = 0; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) {
mode[m*NSIMDVL+iv] = 0.0;
}
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) {
mode[m*NSIMDVL+iv] += _lbp.ma[m][p]
*lb->f[LB_ADDR(_lbp.nsite, _lbp.ndist, NVEL, index0 + iv, LB_RHO, p)];
}
}
}
#endif
/* For convenience, write out the physical modes. */
for_simd_v(iv, NSIMDVL) rho[iv] = mode[0*NSIMDVL+iv];
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) u[ia][iv] = mode[(1 + ia)*NSIMDVL+iv];
}
m = 0;
for (ia = 0; ia < NDIM; ia++) {
for (ib = ia; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) s[ia][ib][iv] = mode[(1 + NDIM + m)*NSIMDVL+iv];
m++;
}
}
for (ia = 1; ia < NDIM; ia++) {
for (ib = 0; ib < ia; ib++) {
for_simd_v(iv, NSIMDVL) s[ia][ib][iv] = s[ib][ia][iv];
}
}
/* Compute the local velocity, taking account of any body force */
for_simd_v(iv, NSIMDVL) rrho[iv] = 1.0/rho[iv];
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) {
int haddr = addr_rank1(hydro->nsite, 3, index0 + iv, ia);
force[ia][iv] = _cp.force_global[ia] + hydro->force->data[haddr];
u[ia][iv] = rrho[iv]*(u[ia][iv] + 0.5*force[ia][iv]);
}
}
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) {
int haddr = addr_rank1(hydro->nsite, 3, index0 + iv, ia);
hydro->u->data[haddr] = u[ia][iv];
}
}
/* Compute the thermodynamic component of the stress */
fe_symm_str_v(fe, index0, sth);
/* Relax stress with different shear and bulk viscosity */
for_simd_v(iv, NSIMDVL) {
tr_s[iv] = 0.0;
tr_seq[iv] = 0.0;
}
for (ia = 0; ia < 3; ia++) {
/* Set equilibrium stress, which includes thermodynamic part */
for (ib = 0; ib < 3; ib++) {
for_simd_v(iv, NSIMDVL) {
seq[ia][ib][iv] = rho[iv]*u[ia][iv]*u[ib][iv] + sth[ia][ib][iv];
}
}
/* Compute trace */
for_simd_v(iv, NSIMDVL) {
tr_s[iv] += s[ia][ia][iv];
tr_seq[iv] += seq[ia][ia][iv];
}
}
/* Form traceless parts */
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) {
s[ia][ia][iv] -= r3*tr_s[iv];
seq[ia][ia][iv] -= r3*tr_seq[iv];
}
}
/* Relax each mode */
for_simd_v(iv, NSIMDVL)
tr_s[iv] = tr_s[iv] - _lbp.rtau[LB_TAU_BULK]*(tr_s[iv] - tr_seq[iv]);
for (ia = 0; ia < 3; ia++) {
for (ib = 0; ib < 3; ib++) {
for_simd_v(iv, NSIMDVL) {
s[ia][ib][iv] -= _lbp.rtau[LB_TAU_SHEAR]*(s[ia][ib][iv] - seq[ia][ib][iv]);
s[ia][ib][iv] += d[ia][ib]*r3*tr_s[iv];
/* Correction from body force (assumes equal relaxation times) */
s[ia][ib][iv] += (2.0 - _lbp.rtau[LB_TAU_SHEAR])
*(u[ia][iv]*force[ib][iv] + force[ia][iv]*u[ib][iv]);
}
}
}
if (lb->param->noise) {
/* Not vectorised */
for (iv = 0; iv < NSIMDVL; iv++) {
double shat1[3][3];
double ghat1[NVEL];
lb_collision_fluctuations(lb, noise, index0 + iv, _cp.kt, shat1, ghat1);
for (ia = 0; ia < 3; ia++) {
for (ib = 0; ib < 3; ib++) {
shat[ia][ib][iv] = shat1[ia][ib];
}
}
for (p = 0; p < NVEL; p++) {
ghat[p][iv] = ghat1[p];
}
}
}
/* Now reset the hydrodynamic modes to post-collision values:
* rho is unchanged, velocity unchanged if no force,
* independent components of stress, and ghosts. */
for (ia = 0; ia < NDIM; ia++) {
for_simd_v(iv, NSIMDVL) mode[(1 + ia)*NSIMDVL+iv] += force[ia][iv];
}
m = 0;
for (ia = 0; ia < NDIM; ia++) {
for (ib = ia; ib < NDIM; ib++) {
for_simd_v(iv, NSIMDVL) {
mode[(1 + NDIM + m)*NSIMDVL+iv] = s[ia][ib][iv] + shat[ia][ib][iv];
}
m++;
}
}
/* Ghost modes are relaxed toward zero equilibrium. */
for (m = NHYDRO; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) {
mode[m*NSIMDVL+iv] = mode[m*NSIMDVL+iv]
- lb->param->rtau[m]*(mode[m*NSIMDVL+iv] - 0.0) + ghat[m][iv];
}
}
/* Project post-collision modes back onto the distribution */
#ifdef _D3Q19_
d3q19_mode2f_chunk(mode, f);
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) {
lb->f[LB_ADDR(_lbp.nsite, _lbp.ndist, NVEL, index0 + iv, LB_RHO, p)] =
f[p*NSIMDVL+iv];
}
}
#else
for (p = 0; p < NVEL; p++) {
for_simd_v(iv, NSIMDVL) f[p*NSIMDVL+iv] = 0.0;
for (m = 0; m < NVEL; m++) {
for_simd_v(iv, NSIMDVL) f[p*NSIMDVL+iv] += _lbp.mi[p][m]*mode[m*NSIMDVL+iv];
}
for_simd_v(iv, NSIMDVL) {
lb->f[LB_ADDR(_lbp.nsite, NDIST, NVEL, index0+iv, LB_RHO, p)] = f[p*NSIMDVL+iv];
}
}
#endif
/* Now, the order parameter distribution */
for_simd_v(iv, NSIMDVL) {
phi[iv] = fe->phi->data[addr_rank0(fe->phi->nsites, index0 + iv)];
}
for_simd_v(iv, NSIMDVL) {
fe_symm_mu(fe, index0 + iv, mu + iv);
jphi[X][iv] = 0.0;
jphi[Y][iv] = 0.0;
jphi[Z][iv] = 0.0;
}
for (p = 1; p < NVEL; p++) {
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) {
jphi[ia][iv] += _lbp.cv[p][ia]*
lb->f[ LB_ADDR(_lbp.nsite, _lbp.ndist, NVEL, index0+iv, LB_PHI, p) ];
}
}
}
/* Relax order parameter modes. See the comments above. */
for (ia = 0; ia < 3; ia++) {
for (ib = 0; ib < 3; ib++) {
for_simd_v(iv, NSIMDVL) {
sphi[ia][ib][iv] = phi[iv]*u[ia][iv]*u[ib][iv] + mu[iv]*d[ia][ib];
/* The alternate form would be:
sphi[ia][ib] = phi*u[ia]*u[ib] + cs2*mobility*mu*d_[ia][ib]; */
}
}
for_simd_v(iv, NSIMDVL) {
jphi[ia][iv] = jphi[ia][iv] - _cp.rtau2*(jphi[ia][iv] - phi[iv]*u[ia][iv]);
/* The alternate form would be: "jphi[ia] = phi*u[ia];" */
}
}
/* Now update the distribution */
#ifdef _D3Q19_
d3q19_mode2f_phi(jdotc,sphidotq,sphi,phi,jphi, lb->f, index0);
#else
for (p = 0; p < NVEL; p++) {
LB_CS2_DOUBLE(cs2);
int dp0 = (p == 0);
for_simd_v(iv, NSIMDVL) {
jdotc[iv] = 0.0;
sphidotq[iv] = 0.0;
}
for (ia = 0; ia < 3; ia++) {
for_simd_v(iv, NSIMDVL) jdotc[iv] += jphi[ia][iv]*_lbp.cv[p][ia];
for (ib = 0; ib < 3; ib++) {
for_simd_v(iv, NSIMDVL) {
sphidotq[iv] += sphi[ia][ib][iv]*(_lbp.cv[p][ia]*_lbp.cv[p][ib] - cs2*d[ia][ib]);