-
Notifications
You must be signed in to change notification settings - Fork 8
/
fix_lb_fluid.cpp
3480 lines (3025 loc) · 138 KB
/
fix_lb_fluid.cpp
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
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, [email protected]
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
Contributing authors: Frances Mackay, Santtu Ollila, Colin Denniston (UWO)
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
Function of Write_VTK_Fluid_File is completed by Huilin Ye in Sep. 19, 2016.
------------------------------------------------------------------------ */
#include "fix_lb_fluid.h"
#include <math.h>
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "comm.h"
#include "memory.h"
#include "error.h"
#include "domain.h"
#include "atom.h"
#include <iostream>
//================================= By Huilin Ye
#include <fstream>
#include <sstream>
//=================================
#include <iomanip>
#include "group.h"
#include "random_mars.h"
#include "update.h"
#include "force.h"
#include "modify.h"
using namespace LAMMPS_NS;
using namespace FixConst;
//================================= By Huilin Ye
using namespace std;
//=================================
static const double kappa_lb=0.0;
FixLbFluid::FixLbFluid(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg)
{
//=====================================================================================================
// Sample inputfile call:
// fix # group lb/fluid nevery typeLB viscosity densityinit_real
//
// where: nevery: call this fix every nevery timesteps.
// (keep this set to 1 for now).
// typeLB: there are two different integrators
// in the code labelled "1" and "2".
// viscosity: the viscosity of the fluid.
// densityinit_real: the density of the fluid.
//
// optional arguments:
// "setArea" type node_area: set the surface area per node associated with a
// given atom type. By default the surface area
// is set at 1.0*dx_lb^2.
// "setGamma" gamma: specify a user-defined value for the force
// coupling constant, instead of using the default
// value.
// "scaleGamma" type scale_factor: scale the user provided force coupling constant
// by the factor, scale_factor, for the given atom
// type.
// "dx" dx_lb: the lattice-Boltzmann grid spacing.
// "dm" dm_lb: the lattice-Boltzmann mass unit.
// "a0" a_0_real: the square of the sound speed in the fluid.
// "noise" Temperature seed: include noise in the system.
// Temperature is the temperature for the fluid.
// seed is the seed for the random number generator.
// "calcforce" N group: print the force acting on a given group every
// N timesteps.
// "trilinear": use the trilinear interpolation stencil.
// "read_restart" restart_file: restart a fluid run from restart_file.
// "write_restart" N: write a fluid restart file every N timesteps.
// "zwall_velocity" velocity_bottom velocity_top: assign velocities to the z-walls
// in the system.
// "bodyforce" bodyforcex bodyforcey bodyforcez: add a constant body force to the
// fluid.
// "printfluid" N: print the fluid density and velocity at each
// grid point every N timesteps.
// "D3Q19": use the 19 velocity D3Q19 model. By default,
// the 15 velocity D3Q15 model is used.
//=====================================================================================================
if(narg <7) error->all(FLERR,"Illegal fix lb/fluid command");
if (comm->style != 0)
error->universe_all(FLERR,"Fix lb/fluid can only currently be used with "
"comm_style brick");
MPI_Comm_rank(world,&me);
MPI_Comm_size(world,&nprocs);
nevery = atoi(arg[3]);
typeLB = atoi(arg[4]);
viscosity = atof(arg[5]);
densityinit_real = atof(arg[6]);
// Default values for optional arguments:
force_diagnostic=0;
noisestress = 0;
trilinear_stencil = 0;
readrestart = 0;
printrestart = 0;
bodyforcex = bodyforcey = bodyforcez = 0.0;
vwtp = vwbt = 0.0;
printfluid = 2000;
T = 300.0;
dm_lb = 1.0;
fixviscouslb = 0;
setdx = 1;
seta0 = 1;
setGamma = 0;
setArea = 0;
numvel = 15;
Gamma = NULL;
NodeArea = NULL;
int iarg = 7;
while (iarg < narg){
if(strcmp(arg[iarg],"setArea")==0){
if(setGamma == 1)
error->all(FLERR,"Illegal fix lb/fluid command: cannot use a combination of default and user-specified gamma values");
setArea = 1;
int itype = atoi(arg[iarg+1]);
double areafactor = atof(arg[iarg+2]);
if(itype <= 0 || itype > atom->ntypes || areafactor < 0.0)
error->all(FLERR,"Illegal fix lb/fluid command: setArea");
if(NodeArea == NULL){
NodeArea = new double[atom->ntypes+1];
for(int i=0; i<=atom->ntypes; i++) NodeArea[i] = -1.0;
}
NodeArea[itype] = areafactor;
iarg += 3;
}
else if(strcmp(arg[iarg],"setGamma")==0){
if(setArea == 1)
error->all(FLERR,"Illegal fix lb/fluid command: cannot use a combination of default and user-specified gamma values");
setGamma = 1;
double Gammaone;
Gammaone = atof(arg[iarg+1]);
if(Gamma == NULL)
Gamma = new double[atom->ntypes+1];
for(int i=0; i<=atom->ntypes; i++) Gamma[i] = Gammaone;
iarg += 2;
}
else if(strcmp(arg[iarg],"scaleGamma")==0){
if(setGamma == 0)
error->all(FLERR,"Illegal fix lb/fluid command: must set a value for Gamma before scaling it");
int itype = atoi(arg[iarg+1]);
double scalefactor = atof(arg[iarg+2]);
if(itype <= 0 || itype > atom->ntypes || scalefactor < 0.0)
error->all(FLERR,"Illegal fix lb/fluid command: scaleGamma");
Gamma[itype] *= scalefactor;
iarg += 3;
}
else if(strcmp(arg[iarg],"dx")==0){
dx_lb = atof(arg[iarg+1]);
iarg += 2;
setdx = 0;
}
else if(strcmp(arg[iarg],"dm")==0){
dm_lb = atof(arg[iarg+1]);
iarg += 2;
}
else if(strcmp(arg[iarg],"a0")==0){
a_0_real = atof(arg[iarg+1]);
iarg += 2;
seta0 = 0;
}
else if(strcmp(arg[iarg],"noise")== 0){
noisestress = 1;
T = atof(arg[iarg+1]);
seed = atoi(arg[iarg+2]);
iarg += 3;
}
else if(strcmp(arg[iarg],"calcforce")==0){
force_diagnostic = atoi(arg[iarg+1]);
igroupforce=group->find(arg[iarg+2]);
iarg += 3;
}
else if(strcmp(arg[iarg],"trilinear")==0){
trilinear_stencil = 1;
iarg += 1;
}
else if(strcmp(arg[iarg],"read_restart")==0){
readrestart = 1;
int nlength = strlen(arg[iarg+1]) + 16;
char *filename = new char[nlength];
strcpy(filename,arg[iarg+1]);
MPI_File_open(world,filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&pFileRead);
delete [] filename;
iarg += 2;
}
else if(strcmp(arg[iarg],"write_restart")==0){
printrestart = atoi(arg[iarg+1]);
iarg += 2;
}
else if(strcmp(arg[iarg],"zwall_velocity")==0){
if(domain->periodicity[2]!=0) error->all(FLERR,"fix lb/fluid error: setting \
a z wall velocity without implementing fixed BCs in z");
vwbt = atof(arg[iarg+1]);
vwtp = atof(arg[iarg+2]);
iarg += 3;
}
else if(strcmp(arg[iarg],"bodyforce")==0){
bodyforcex = atof(arg[iarg+1]);
bodyforcey = atof(arg[iarg+2]);
bodyforcez = atof(arg[iarg+3]);
iarg += 4;
}
else if(strcmp(arg[iarg],"printfluid")==0){
printfluid = atoi(arg[iarg+1]);
iarg += 2;
}
else if(strcmp(arg[iarg],"D3Q19")==0){
numvel = 19;
iarg += 1;
}
else error->all(FLERR,"Illegal fix lb/fluid command");
}
//--------------------------------------------------------------------------
//Choose between D3Q15 and D3Q19 functions:
//--------------------------------------------------------------------------
if(numvel == 15){
initializeLB = &FixLbFluid::initializeLB15;
equilibriumdist = &FixLbFluid::equilibriumdist15;
update_full = &FixLbFluid::update_full15;
}else{
initializeLB = &FixLbFluid::initializeLB19;
equilibriumdist = &FixLbFluid::equilibriumdist19;
update_full = &FixLbFluid::update_full19;
}
//--------------------------------------------------------------------------
// perform initial allocation of atom-based array register
// with Atom class
//--------------------------------------------------------------------------
hydroF = NULL;
grow_arrays(atom->nmax);
atom->add_callback(0);
for(int i=0; i<atom->nmax; i++)
for(int j=0; j<3; j++)
hydroF[i][j] = 0.0;
Ng_lb = NULL;
w_lb = NULL;
mg_lb = NULL;
e = NULL;
feq = NULL;
feqold = NULL;
feqn = NULL;
feqoldn = NULL;
f_lb = NULL;
fnew = NULL;
density_lb = NULL;
u_lb = NULL;
altogether = NULL;
buf = NULL;
Ff = NULL;
Fftempx = NULL;
Fftempy = NULL;
Fftempz = NULL;
//--------------------------------------------------------------------------
// Set the lattice Boltzmann dt.
//--------------------------------------------------------------------------
dt_lb=nevery*(update->dt);
//--------------------------------------------------------------------------
// Set the lattice Boltzmann dx if it wasn't specified in the
// input.
//--------------------------------------------------------------------------
if(setdx == 1){
double dx_lb1 = sqrt(3.0*viscosity*dt_lb/densityinit_real);
double mindomain = std::min(std::min(domain->xprd/comm->procgrid[0],domain->yprd/comm->procgrid[1]),domain->zprd/comm->procgrid[2]);
dx_lb = mindomain/floor(mindomain/dx_lb1);
if(comm->me==0){
char str[128];
sprintf(str,"Setting the lattice-Boltzmann dx to %10.6f",dx_lb);
error->message(FLERR,str);
}
}
//--------------------------------------------------------------------------
// If the area per node has not been set by the user, set to the
// default value of dx_lb*dx_lb.
//--------------------------------------------------------------------------
if(setGamma == 0){
if(setArea == 0){
if(comm->me==0){
error->message(FLERR,"Assuming an area per node of dx*dx for all of the MD particles. This should only be used if these all correspond to point particles; otherwise, change using the setArea keyword");
}
NodeArea = new double[atom->ntypes+1];
for(int i=0; i<=atom->ntypes; i++) NodeArea[i] = -1.0;
}
for(int i=0; i<=atom->ntypes; i++)
if(NodeArea[i] < 0.0) NodeArea[i] = dx_lb*dx_lb;
}
//--------------------------------------------------------------------------
// Set a0 if it wasn't specified in the input
//--------------------------------------------------------------------------
if(seta0 == 1)
a_0_real = 0.33333333*dx_lb*dx_lb/dt_lb/dt_lb;
//--------------------------------------------------------------------------
// Check to make sure that the total number of grid points in each direction
// divides evenly among the processors in that direction.
// Shrink-wrapped boundary conditions (which are not permitted by this fix)
// might cause a problem, so check for this. A full check of the boundary
// conditions is performed in the init routine, rather than here, as it is
// possible to change the BCs between runs.
//--------------------------------------------------------------------------
double aa;
double eps=1.0e-8;
aa = (domain->xprd/comm->procgrid[0])/dx_lb;
if(fabs(aa - floor(aa+0.5)) > eps){
if(domain->boundary[0][0] != 0){
error->all(FLERR,"the x-direction must be periodic");
}
char errormessage[200];
sprintf(errormessage,"With dx= %f, and the simulation domain divided by %i processors in the x direction, the simulation domain in the x direction must be a multiple of %f",dx_lb,comm->procgrid[0],comm->procgrid[0]*dx_lb);
error->all(FLERR,errormessage);
}
aa = (domain->yprd/comm->procgrid[1])/dx_lb;
if(fabs(aa - floor(aa+0.5)) > eps){
if(domain->boundary[1][0] != 0){
error->all(FLERR,"the y-direction must be periodic");
}
char errormessage[200];
sprintf(errormessage,"With dx= %f, and the simulation domain divided by %i processors in the y direction, the simulation domain in the y direction must be a multiple of %f",dx_lb,comm->procgrid[1],comm->procgrid[1]*dx_lb);
error->all(FLERR,errormessage);
}
aa = (domain->zprd/comm->procgrid[2])/dx_lb;
if(fabs(aa - floor(aa+0.5)) > eps){
if(domain->boundary[2][0] == 2 || domain->boundary[2][0] == 3){
error->all(FLERR,"the z-direction can not have shrink-wrap boundary conditions");
}
char errormessage[200];
sprintf(errormessage,"With dx= %f, and the simulation domain divided by %i processors in the z direction, the simulation domain in the z direction must be a multiple of %f",dx_lb,comm->procgrid[2],comm->procgrid[2]*dx_lb);
error->all(FLERR,errormessage);
}
//--------------------------------------------------------------------------
// Set the total number of grid points in each direction.
//--------------------------------------------------------------------------
Nbx = (int)(domain->xprd/dx_lb + 0.5);
Nby = (int)(domain->yprd/dx_lb + 0.5);
Nbz = (int)(domain->zprd/dx_lb + 0.5);
//--------------------------------------------------------------------------
// Set the number of grid points in each dimension for the local subgrids.
//--------------------------------------------------------------------------
subNbx= Nbx/comm->procgrid[0] + 2;
subNby= Nby/comm->procgrid[1] + 2;
subNbz= Nbz/comm->procgrid[2] + 2;
//--------------------------------------------------------------------------
// In order to calculate the fluid forces correctly, need to have atleast
// 5 grid points in each direction per processor.
//--------------------------------------------------------------------------
if(subNbx<7 || subNby < 7 || subNbz<7)
error->all(FLERR,"Need at least 5 grid points in each direction per processor");
// If there are walls in the z-direction add an extra grid point.
if(domain->periodicity[2]==0){
Nbz += 1;
if(comm->myloc[2]==comm->procgrid[2]-1)
subNbz += 1;
}
if(comm->me==0){
char str[128];
if(setdx == 1){
sprintf(str,"Using a lattice-Boltzmann grid of %i by %i by %i total grid points. To change, use the dx keyword",Nbx,Nby,Nbz);
}else{
sprintf(str,"Using a lattice-Boltzmann grid of %i by %i by %i total grid points.",Nbx,Nby,Nbz);
}
error->message(FLERR,str);
}
//--------------------------------------------------------------------------
// Store the largest value of subNbz, which is needed for allocating the
// buf array (since a processor with comm->myloc[2] == comm->procgrid[2]-1
// may have an additional subNbz point as compared with the rest).
//--------------------------------------------------------------------------
int subNbzmax;
MPI_Allreduce(&subNbz,&subNbzmax,1,MPI_INT,MPI_MAX,world);
//--------------------------------------------------------------------------
// Create the MPI datatypes used to pass portions of arrays:
// datatypes to pass the f and feq arrays.
//--------------------------------------------------------------------------
MPI_Aint lb,sizeofdouble;
MPI_Type_get_extent(MPI_DOUBLE,&lb,&sizeofdouble);
MPI_Type_vector(subNbz-2,numvel,numvel,MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNby-2,1,numvel*subNbz*sizeofdouble,oneslice,&passxf);
MPI_Type_commit(&passxf);
MPI_Type_create_hvector(subNbx,1,numvel*subNbz*subNby*sizeofdouble,oneslice,&passyf);
MPI_Type_commit(&passyf);
MPI_Type_free(&oneslice);
MPI_Type_vector(subNby,numvel,numvel*subNbz,MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNbx,1,numvel*subNbz*subNby*sizeofdouble,oneslice,&passzf);
MPI_Type_commit(&passzf);
// datatypes to pass the u array, and the Ff array.
MPI_Type_free(&oneslice);
MPI_Type_vector(subNbz+3,3,3,MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNby+3,1,3*(subNbz+3)*sizeofdouble,oneslice,&passxu);
MPI_Type_commit(&passxu);
MPI_Type_create_hvector(subNbx+3,1,3*(subNbz+3)*(subNby+3)*sizeofdouble,oneslice,&passyu);
MPI_Type_commit(&passyu);
MPI_Type_free(&oneslice);
MPI_Type_vector(subNby+3,3,3*(subNbz+3),MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNbx+3,1,3*(subNbz+3)*(subNby+3)*sizeofdouble,oneslice,&passzu);
MPI_Type_commit(&passzu);
// datatypes to pass the density array.
MPI_Type_free(&oneslice);
MPI_Type_vector(subNbz+3,1,1,MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNby+3,1,1*(subNbz+3)*sizeofdouble,oneslice,&passxrho);
MPI_Type_commit(&passxrho);
MPI_Type_create_hvector(subNbx+3,1,1*(subNbz+3)*(subNby+3)*sizeofdouble,oneslice,&passyrho);
MPI_Type_commit(&passyrho);
MPI_Type_free(&oneslice);
MPI_Type_vector(subNby+3,1,1*(subNbz+3),MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNbx+3,1,1*(subNbz+3)*(subNby+3)*sizeofdouble,oneslice,&passzrho);
MPI_Type_commit(&passzrho);
// datatypes to receive a portion of the Ff array.
MPI_Type_free(&oneslice);
MPI_Type_vector(subNbz+3,3,3,MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNby+3,1,3*(subNbz+3)*sizeofdouble,oneslice,&passxtemp);
MPI_Type_commit(&passxtemp);
MPI_Type_create_hvector(subNbx+3,1,3*(subNbz+3)*5*sizeofdouble,oneslice,&passytemp);
MPI_Type_commit(&passytemp);
MPI_Type_free(&oneslice);
MPI_Type_vector(subNby+3,3,3*5,MPI_DOUBLE,&oneslice);
MPI_Type_commit(&oneslice);
MPI_Type_create_hvector(subNbx+3,1,3*5*(subNby+3)*sizeofdouble,oneslice,&passztemp);
MPI_Type_commit(&passztemp);
MPI_Type_free(&oneslice);
//--------------------------------------------------------------------------
// Allocate the necessary arrays.
//--------------------------------------------------------------------------
memory->create(Ng_lb,numvel,"FixLbFluid:Ng_lb");
memory->create(w_lb,numvel,"FixLbFluid:w_lb");
memory->create(mg_lb,numvel,numvel,"FixLbFluid:mg_lb");
memory->create(e,numvel,3,"FixLbFluid:e");
memory->create(feq,subNbx,subNby,subNbz,numvel,"FixLbFluid:feq");
if(typeLB == 2){
memory->create(feqold,subNbx,subNby,subNbz,numvel,"FixLbFluid:feqold");
memory->create(feqn,subNbx,subNby,subNbz,numvel,"FixLbFluid:feqn");
memory->create(feqoldn,subNbx,subNby,subNbz,numvel,"FixLbFluid:feqoldn");
}
memory->create(f_lb,subNbx,subNby,subNbz,numvel,"FixLbFluid:f_lb");
memory->create(fnew,subNbx,subNby,subNbz,numvel,"FixLbFluid:fnew");
memory->create(density_lb,subNbx+3,subNby+3,subNbz+3,"FixLbFluid:density_lb");
memory->create(u_lb,subNbx+3,subNby+3,subNbz+3,3,"FixLbFluid:u_lb");
if(printfluid > 0){
memory->create(buf,subNbx,subNby,subNbzmax,4,"FixLbFluid:buf");
if(me==0)
memory->create(altogether,Nbx,Nby,Nbz,4,"FixLbFluid:altogether");
}
memory->create(Ff,subNbx+3,subNby+3,subNbz+3,3,"FixLbFluid:Ff");
memory->create(Fftempx,5,subNby+3,subNbz+3,3,"FixLbFluid:Fftempx");
memory->create(Fftempy,subNbx+3,5,subNbz+3,3,"FixLbFluid:Fftempy");
memory->create(Fftempz,subNbx+3,subNby+3,5,3,"FixLbFluid:Fftempz");
if(noisestress==1){
random = new RanMars(lmp,seed + comm->me);
}
//--------------------------------------------------------------------------
// Rescale the variables to Lattice Boltzmann dimensionless units.
//--------------------------------------------------------------------------
rescale();
//-------------------------
//output the rescaled parameters
//-------------------------
if(update->ntimestep == 0){
stringstream output_filename;
output_filename << "FluidParameters.dat";
ofstream output_file;
output_file.open(output_filename.str().c_str());
output_file << "Upper velocity " << vwtp <<"\n";
output_file << "Bottom velocity " << vwbt <<"\n";
output_file << "Relaxition time " << tau <<"\n";
output_file << "Initial Densitity " << densityinit <<"\n";
output_file << "Sound speed " << a_0 <<"\n";
}
//--------------------------------------------------------------------------
// Initialize the arrays.
//--------------------------------------------------------------------------
(*this.*initializeLB)();
initialize_feq();
}
FixLbFluid::~FixLbFluid()
{
atom->delete_callback(id,0);
memory->destroy(hydroF);
memory->destroy(Ng_lb);
memory->destroy(w_lb);
memory->destroy(mg_lb);
memory->destroy(e);
memory->destroy(feq);
if(typeLB == 2){
memory->destroy(feqold);
memory->destroy(feqn);
memory->destroy(feqoldn);
}
memory->destroy(f_lb);
memory->destroy(fnew);
memory->destroy(density_lb);
memory->destroy(u_lb);
if(printfluid>0){
if(me==0)
memory->destroy(altogether);
memory->destroy(buf);
}
memory->destroy(Ff);
memory->destroy(Fftempx);
memory->destroy(Fftempy);
memory->destroy(Fftempz);
if(noisestress==1){
delete random;
}
if(setGamma == 1){
delete [] Gamma;
}else{
delete [] NodeArea;
}
}
int FixLbFluid::setmask()
{
int mask =0;
mask |= INITIAL_INTEGRATE;
mask |= POST_FORCE;
mask |= END_OF_STEP;
return mask;
}
void FixLbFluid::init(void)
{
int i,j;
if (comm->style != 0)
error->universe_all(FLERR,"Fix lb/fluid can only currently be used with "
"comm_style brick");
//--------------------------------------------------------------------------
// Check to see if the MD timestep has changed between runs.
//--------------------------------------------------------------------------
double dt_lb_now;
dt_lb_now=nevery*(update->dt);
if(fabs(dt_lb_now - dt_lb) > 1.0e-12){
error->warning(FLERR,"Timestep has changed between runs with the same lb/fluid. Unphysical results may occur");
}
//--------------------------------------------------------------------------
// Make sure the size of the simulation domain has not changed
// between runs.
//--------------------------------------------------------------------------
int Nbx_now,Nby_now,Nbz_now;
Nbx_now = (int)(domain->xprd/dx_lb + 0.5);
Nby_now = (int)(domain->yprd/dx_lb + 0.5);
Nbz_now = (int)(domain->zprd/dx_lb + 0.5);
// If there are walls in the z-direction add an extra grid point.
if(domain->periodicity[2]==0){
Nbz_now += 1;
}
if(Nbx_now != Nbx || Nby_now != Nby || Nbz_now != Nbz){
error->all(FLERR,"the simulation domain can not change shape between runs with the same lb/fluid");
}
//--------------------------------------------------------------------------
// Check to make sure that the chosen LAMMPS boundary types are compatible
// with this fix.
// shrink-wrap is not compatible in any dimension.
// fixed only works in the z-direction.
//--------------------------------------------------------------------------
if(domain->boundary[0][0] != 0){
error->all(FLERR,"the x-direction must be periodic");
}
if(domain->boundary[1][0] != 0){
error->all(FLERR,"the y-direction must be periodic");
}
if(domain->boundary[2][0] == 2 || domain->boundary[2][0] == 3){
error->all(FLERR,"the z-direction can not have shrink-wrap boundary conditions");
}
//--------------------------------------------------------------------------
// Check if the lb/viscous fix is also called:
//--------------------------------------------------------------------------
groupbit_viscouslb = groupbit_pc = groupbit_rigid_pc_sphere = 0;
for (i = 0; i < modify->nfix; i++){
if (strcmp(modify->fix[i]->style,"lb/viscous") == 0){
fixviscouslb = 1;
groupbit_viscouslb = group->bitmask[modify->fix[i]->igroup];
}
if(strcmp(modify->fix[i]->style,"lb/pc")==0){
groupbit_pc = group->bitmask[modify->fix[i]->igroup];
}
if(strcmp(modify->fix[i]->style,"lb/rigid/pc/sphere")==0){
groupbit_rigid_pc_sphere = group->bitmask[modify->fix[i]->igroup];
}
}
// Warn if the fluid force is not applied to any of the particles.
if(!(groupbit_viscouslb || groupbit_pc || groupbit_rigid_pc_sphere) && comm->me==0){
error->message(FLERR,"Not adding the fluid force to any of the MD particles. To add this force use one of the lb/viscous, lb/pc, or lb/rigid/pc/sphere fixes");
}
// If fix lb/viscous is called for a particular atom, make sure
// lb/pc or lb/rigid/pc/sphere are not:
if(fixviscouslb == 1){
int *mask = atom->mask;
int nlocal = atom->nlocal;
for(j=0; j<nlocal; j++){
if((mask[j] & groupbit) && (mask[j] & groupbit_viscouslb) && (mask[j] & groupbit_pc))
error->one(FLERR,"should not use the lb/viscous command when integrating with the lb/pc fix");
if((mask[j] & groupbit) && (mask[j] & groupbit_viscouslb) && (mask[j] & groupbit_rigid_pc_sphere))
error->one(FLERR,"should not use the lb/viscous command when integrating with the lb/rigid/pc/sphere fix");
}
}
}
void FixLbFluid::setup(int vflag)
{
//--------------------------------------------------------------------------
// Need to calculate the force on the fluid for a restart run.
//--------------------------------------------------------------------------
if(step > 0)
calc_fluidforce();
}
void FixLbFluid::initial_integrate(int vflag)
{
//--------------------------------------------------------------------------
// Print a header labelling any output printed to the screen.
//--------------------------------------------------------------------------
static int printheader = 1;
if(printheader == 1){
if(force_diagnostic > 0 && me == 0){
printf("-------------------------------------------------------------------------------\n");
printf(" F_x F_y F_z T_x T_y T_z\n");
printf("-------------------------------------------------------------------------------\n");
}
if(printfluid > 0 && me == 0){
printf("---------------------------------------------------------------------\n");
printf(" density u_x u_y u_z \n");
printf("---------------------------------------------------------------------\n");
}
printheader = 0;
}
//--------------------------------------------------------------------------
// Determine the equilibrium distribution on the local subgrid.
//--------------------------------------------------------------------------
(*this.*equilibriumdist)(1,subNbx-1,1,subNby-1,1,subNbz-1);
//--------------------------------------------------------------------------
// Using the equilibrium distribution, calculate the new
// distribution function.
//--------------------------------------------------------------------------
(*this.*update_full)();
std::swap(f_lb,fnew);
//--------------------------------------------------------------------------
// Calculate moments of the distribution function.
//--------------------------------------------------------------------------
parametercalc_full();
//--------------------------------------------------------------------------
// Store the equilibrium distribution function, it is needed in
// the next time step by the update routine.
//--------------------------------------------------------------------------
if(typeLB == 2){
std::swap(feqold,feq);
std::swap(feqoldn,feqn);
}
//--------------------------------------------------------------------------
// Perform diagnostics, and print output for the graphics program
//--------------------------------------------------------------------------
if(printfluid > 0 && update->ntimestep > 0 && (update->ntimestep % printfluid == 0))
streamout();
}
void FixLbFluid::post_force(int vflag)
{
//if(fixviscouslb==1)
calc_fluidforce();
}
void FixLbFluid::end_of_step()
{
if(fixviscouslb==0)
calc_fluidforce();
if(printrestart>0){
if((update->ntimestep)%printrestart == 0){
write_restartfile();
}
}
}
//==========================================================================
// allocate atom-based array
//==========================================================================
void FixLbFluid::grow_arrays(int nmax)
{
memory->grow(hydroF,nmax,3,"FixLbFluid:hydroF");
}
//==========================================================================
// copy values within local atom-based array
//==========================================================================
void FixLbFluid::copy_arrays(int i, int j, int delflag)
{
hydroF[j][0] = hydroF[i][0];
hydroF[j][1] = hydroF[i][1];
hydroF[j][2] = hydroF[i][2];
}
//==========================================================================
// pack values in local atom-based array for exchange with another proc
//==========================================================================
int FixLbFluid::pack_exchange(int i, double *buf)
{
buf[0] = hydroF[i][0];
buf[1] = hydroF[i][1];
buf[2] = hydroF[i][2];
return 3;
}
//==========================================================================
// unpack values in local atom-based array from exchange with another proc
//==========================================================================
int FixLbFluid::unpack_exchange(int nlocal, double *buf)
{
hydroF[nlocal][0] = buf[0];
hydroF[nlocal][1] = buf[1];
hydroF[nlocal][2] = buf[2];
return 3;
}
//==========================================================================
// calculate the force from the local atoms acting on the fluid.
//==========================================================================
void FixLbFluid::calc_fluidforce(void)
{
int *mask = atom->mask;
int nlocal = atom->nlocal;
double **x = atom->x;
int i,j,k,m;
MPI_Request requests[20];
double forceloc[3],force[3];
double torqueloc[3],torque[3];
//--------------------------------------------------------------------------
// Zero out arrays
//--------------------------------------------------------------------------
std::fill(&Ff[0][0][0][0],&Ff[0][0][0][0] + (subNbx+3)*(subNby+3)*(subNbz+3)*3,0.0);
std::fill(&Fftempx[0][0][0][0],&Fftempx[0][0][0][0] + 5*(subNby+3)*(subNbz+3)*3,0.0);
std::fill(&Fftempy[0][0][0][0],&Fftempy[0][0][0][0] + (subNbx+3)*5*(subNbz+3)*3,0.0);
std::fill(&Fftempz[0][0][0][0],&Fftempz[0][0][0][0] + (subNbx+3)*(subNby+3)*5*3,0.0);
forceloc[0] = forceloc[1] = forceloc[2] = 0.0;
torqueloc[0] = torqueloc[1] = torqueloc[2] = 0.0;
for(i=0; i<atom->nmax; i++)
for(j=0; j<3; j++)
hydroF[i][j] = 0.0;
double unwrap[3];
double dx,dy,dz;
double massone;
imageint *image = atom->image;
double *rmass = atom->rmass;
double *mass = atom->mass;
int *type = atom->type;
double sum[4],xcm[4];
if(force_diagnostic > 0 && update->ntimestep > 0 && (update->ntimestep % force_diagnostic == 0)){
//Calculate the center of mass of the particle group
//(needed to calculate the torque).
sum[0] = sum[1] = sum[2] = sum[3] = 0.0;
for(i=0; i<nlocal; i++){
if(mask[i] & group->bitmask[igroupforce]){
domain->unmap(x[i],image[i],unwrap);
if(rmass) massone = rmass[i];
else massone = mass[type[i]];
sum[0] += unwrap[0]*massone;
sum[1] += unwrap[1]*massone;
sum[2] += unwrap[2]*massone;
sum[3] += massone;
}
}
MPI_Allreduce(&sum[0],&xcm[0],4,MPI_DOUBLE,MPI_SUM,world);
xcm[0] = xcm[0]/xcm[3];
xcm[1] = xcm[1]/xcm[3];
xcm[2] = xcm[2]/xcm[3];
}
//--------------------------------------------------------------------------
//Calculate the contribution to the force on the fluid.
//--------------------------------------------------------------------------
for(i=0; i<nlocal; i++){
if(mask[i] & groupbit){
if(trilinear_stencil==1) {
trilinear_interpolation(i);
}else{
peskin_interpolation(i);
}
if(force_diagnostic > 0 && update->ntimestep > 0 && (update->ntimestep % force_diagnostic == 0)){
if(mask[i] & group->bitmask[igroupforce]){
domain->unmap(x[i],image[i],unwrap);
dx = unwrap[0] - xcm[0];
dy = unwrap[1] - xcm[1];
dz = unwrap[2] - xcm[2];
forceloc[0] += hydroF[i][0];
forceloc[1] += hydroF[i][1];
forceloc[2] += hydroF[i][2];
torqueloc[0] += dy*hydroF[i][2] - dz*hydroF[i][1];
torqueloc[1] += dz*hydroF[i][0] - dx*hydroF[i][2];
torqueloc[2] += dx*hydroF[i][1] - dy*hydroF[i][0];
}
}
}
}
//--------------------------------------------------------------------------
//Communicate the force contributions which lie outside the local processor
//sub domain.
//--------------------------------------------------------------------------
for(i=0; i<10; i++)
requests[i]=MPI_REQUEST_NULL;
MPI_Isend(&Ff[0][0][0][0],1,passxu,comm->procneigh[0][0],10,world,&requests[0]);
MPI_Isend(&Ff[subNbx+2][0][0][0],1,passxu,comm->procneigh[0][0],20,world,&requests[1]);
MPI_Isend(&Ff[subNbx-1][0][0][0],1,passxu,comm->procneigh[0][1],30,world,&requests[2]);
MPI_Isend(&Ff[subNbx][0][0][0],1,passxu,comm->procneigh[0][1],40,world,&requests[3]);
MPI_Isend(&Ff[subNbx+1][0][0][0],1,passxu,comm->procneigh[0][1],50,world,&requests[4]);
MPI_Irecv(&Fftempx[0][0][0][0],1,passxtemp,comm->procneigh[0][1],10,world,&requests[5]);
MPI_Irecv(&Fftempx[1][0][0][0],1,passxtemp,comm->procneigh[0][1],20,world,&requests[6]);
MPI_Irecv(&Fftempx[2][0][0][0],1,passxtemp,comm->procneigh[0][0],30,world,&requests[7]);
MPI_Irecv(&Fftempx[3][0][0][0],1,passxtemp,comm->procneigh[0][0],40,world,&requests[8]);
MPI_Irecv(&Fftempx[4][0][0][0],1,passxtemp,comm->procneigh[0][0],50,world,&requests[9]);
MPI_Waitall(10,requests,MPI_STATUS_IGNORE);
for(j=0; j<subNby+3; j++){
for(k=0; k<subNbz+3; k++){
for(m=0; m<3; m++){
Ff[subNbx-2][j][k][m] += Fftempx[0][j][k][m];
Ff[subNbx-3][j][k][m] += Fftempx[1][j][k][m];
Ff[1][j][k][m] += Fftempx[2][j][k][m];
Ff[2][j][k][m] += Fftempx[3][j][k][m];
Ff[3][j][k][m] += Fftempx[4][j][k][m];
}
}
}
for(i=0; i<10; i++)
requests[i]=MPI_REQUEST_NULL;
MPI_Isend(&Ff[0][0][0][0],1,passyu,comm->procneigh[1][0],10,world,&requests[0]);
MPI_Isend(&Ff[0][subNby+2][0][0],1,passyu,comm->procneigh[1][0],20,world,&requests[1]);
MPI_Isend(&Ff[0][subNby-1][0][0],1,passyu,comm->procneigh[1][1],30,world,&requests[2]);
MPI_Isend(&Ff[0][subNby][0][0],1,passyu,comm->procneigh[1][1],40,world,&requests[3]);
MPI_Isend(&Ff[0][subNby+1][0][0],1,passyu,comm->procneigh[1][1],50,world,&requests[4]);
MPI_Irecv(&Fftempy[0][0][0][0],1,passytemp,comm->procneigh[1][1],10,world,&requests[5]);
MPI_Irecv(&Fftempy[0][1][0][0],1,passytemp,comm->procneigh[1][1],20,world,&requests[6]);
MPI_Irecv(&Fftempy[0][2][0][0],1,passytemp,comm->procneigh[1][0],30,world,&requests[7]);
MPI_Irecv(&Fftempy[0][3][0][0],1,passytemp,comm->procneigh[1][0],40,world,&requests[8]);
MPI_Irecv(&Fftempy[0][4][0][0],1,passytemp,comm->procneigh[1][0],50,world,&requests[9]);
MPI_Waitall(10,requests,MPI_STATUS_IGNORE);
for(i=0; i<subNbx+3; i++){
for(k=0; k<subNbz+3; k++){
for(m=0; m<3; m++){
Ff[i][subNby-2][k][m] += Fftempy[i][0][k][m];
Ff[i][subNby-3][k][m] += Fftempy[i][1][k][m];
Ff[i][1][k][m] += Fftempy[i][2][k][m];
Ff[i][2][k][m] += Fftempy[i][3][k][m];
Ff[i][3][k][m] += Fftempy[i][4][k][m];
}
}
}
for(i=0; i<10; i++)
requests[i]=MPI_REQUEST_NULL;
MPI_Isend(&Ff[0][0][0][0],1,passzu,comm->procneigh[2][0],10,world,&requests[0]);
MPI_Isend(&Ff[0][0][subNbz+2][0],1,passzu,comm->procneigh[2][0],20,world,&requests[1]);
MPI_Isend(&Ff[0][0][subNbz-1][0],1,passzu,comm->procneigh[2][1],30,world,&requests[2]);
MPI_Isend(&Ff[0][0][subNbz][0],1,passzu,comm->procneigh[2][1],40,world,&requests[3]);
MPI_Isend(&Ff[0][0][subNbz+1][0],1,passzu,comm->procneigh[2][1],50,world,&requests[4]);
MPI_Irecv(&Fftempz[0][0][0][0],1,passztemp,comm->procneigh[2][1],10,world,&requests[5]);
MPI_Irecv(&Fftempz[0][0][1][0],1,passztemp,comm->procneigh[2][1],20,world,&requests[6]);
MPI_Irecv(&Fftempz[0][0][2][0],1,passztemp,comm->procneigh[2][0],30,world,&requests[7]);
MPI_Irecv(&Fftempz[0][0][3][0],1,passztemp,comm->procneigh[2][0],40,world,&requests[8]);
MPI_Irecv(&Fftempz[0][0][4][0],1,passztemp,comm->procneigh[2][0],50,world,&requests[9]);
MPI_Waitall(10,requests,MPI_STATUS_IGNORE);
for(i=0; i<subNbx+3; i++){
for(j=0; j<subNby+3; j++){
for(m=0; m<3; m++){
Ff[i][j][subNbz-2][m] += Fftempz[i][j][0][m];
Ff[i][j][subNbz-3][m] += Fftempz[i][j][1][m];
Ff[i][j][1][m] += Fftempz[i][j][2][m];
Ff[i][j][2][m] += Fftempz[i][j][3][m];
Ff[i][j][3][m] += Fftempz[i][j][4][m];
}
}
}
if(force_diagnostic > 0 && update->ntimestep > 0 && (update->ntimestep % force_diagnostic == 0)){
force[0] = force[1] = force[2] = 0.0;
torque[0] = torque[1] = torque[2] =0.0;
MPI_Allreduce(&forceloc[0],&force[0],3,MPI_DOUBLE,MPI_SUM,world);
MPI_Allreduce(&torqueloc[0],&torque[0],3,MPI_DOUBLE,MPI_SUM,world);
if(me==0){
printf("%E %E %E %E %E %E\n",force[0],force[1],force[2],
torque[0],torque[1],torque[2]);
}
}
}
//==========================================================================
// uses the Peskin stencil to perform the velocity, density and
// force interpolations.
//==========================================================================
void FixLbFluid::peskin_interpolation(int i)
{
double **x = atom->x;
double **v = atom->v;
double **f = atom->f;
int *type = atom->type;
double *rmass = atom->rmass;
double *mass = atom->mass;