-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfix_ordern.cpp
1158 lines (1099 loc) · 38.8 KB
/
fix_ordern.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
/* ----------------------------------------------------------------------
fix ordern is a child class of "fix", developed based on two classes
of "fix ave/time" and "fix ave/correlate/long", provided by LAMMPS.
This command is distributed under the GNU General Public License.
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
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.
------------------------------------------------------------------------- */
#include "fix_ordern.h"
#include "update.h"
#include "force.h"
#include "modify.h"
#include "compute.h"
#include "memory.h"
#include "error.h"
#include "group.h"
#include "domain.h"
#include "atom.h"
#include "citeme.h"
#include <unistd.h>
using namespace LAMMPS_NS;
using namespace FixConst;
enum{SCALAR,VECTOR};
enum{VISCOSITY,THERMCOND,DIFFUSIVITY};
#define INVOKED_VECTOR 2
static const char cite_fix_ordern[] =
"fix ave/ordern command:\n\n"
"@Article{Jamali2019,\n"
" author = {Jamali, Seyed Hossein and Wolf, Ludger and Becker, Tim M. and de Groen, Mariëtte and Ramdin, Mahinder and Hartkamp, Remco and Bardow, André and Vlugt, Thijs J. H. and Moultos, Othonas A.},\n"
" title = {OCTP: A Tool for On-the-Fly Calculation of Transport Properties of Fluids with the Order-n Algorithm in LAMMPS},\n"
" doi = {10.1021/acs.jcim.8b00939},\n"
" journal = {J. Chem. Inf. Model.},\n"
" year = {2019},\n"
" volume = {59},\n"
" pages = {1290-1294}\n "
"}\n\n";
/* ---------------------------------------------------------------------- */
FixOrderN::FixOrderN(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg)
{
if (lmp->citeme) lmp->citeme->add(cite_fix_ordern);
// At least 7 arguments are needed: [0-2], MODE, nevery, nwrite, value
if (narg < 7) error->all(FLERR,"Illegal fix ordern command");
MPI_Comm_rank(world,&me);
restart_global = 1;
// Initial values
restart_continue = 0;
startstep = 0;
flag_Dxyz = 0;
flag_TCconv = 0;
tnb = 10;
tnbe = 10;
fp1 = NULL;
fp2 = NULL;
title = NULL;
format_user = NULL;
format = (char *) " %g";
dynamic_group_allow = 0; // the groups should not be modified.
// SPECIFYING THE MAIN ARGUMENTS
// Define the type of transport property calculation
if (strcmp(arg[3],"diffusivity") == 0) {
mode = DIFFUSIVITY;
char filen1[] = "selfdiffusivity.dat";
filename1 = new char[strlen(filen1)+1];
strcpy(filename1,filen1);
char filen2[] = "onsagercoefficient.dat";
filename2 = new char[strlen(filen2)+1];
strcpy(filename2,filen2);
} else if (strcmp(arg[3],"viscosity") == 0) {
mode = VISCOSITY;
char filen1[] = "viscosity.dat";
filename1 = new char[strlen(filen1)+1];
strcpy(filename1,filen1);
filename2 = NULL;
} else if (strcmp(arg[3],"thermalconductivity") == 0) {
mode = THERMCOND;
char filen1[] = "thermconductivity.dat";
filename1 = new char[strlen(filen1)+1];
strcpy(filename1,filen1);
filename2 = NULL;
} else
error->all(FLERR,"Illegal fix ordern command");
// rate of sampling (end_of_step())
nevery = utils::inumeric(FLERR,arg[4],false,lmp);
// rate of writing files
nfreq = utils::inumeric(FLERR,arg[5],false,lmp);
global_freq = nevery;
// OBTAINING THE ID OF COMPUTE FOR THIS FIX
// number of input values (it must be only one compute)
nvalues = 0;
int iarg = 6;
while (iarg < narg) {
if ((strncmp(arg[iarg],"c_",2) == 0)) {
nvalues++;
iarg++;
} else break;
}
if (nvalues == 0) error->all(FLERR,"Incorrect number of inputs for fix ordern command");
if (nvalues > 1) error->all(FLERR,"Incorrect number of inputs for fix ordern command");
char *suffix = new char[strlen(arg[6])];
strcpy(suffix,&arg[6][2]);
char *ptr = strchr(suffix,'[');
if (ptr) error->all(FLERR,"All components of the vector are required for fix ordern command");
idcompute = new char[strlen(suffix) + 1];
strcpy(idcompute,suffix);
delete [] suffix;
icompute = modify->find_compute(idcompute); // id of the compute (int)
nrows = modify->compute[icompute]->size_vector; // get the number of rows (int)
Compute *compute = modify->compute[icompute]; // the whole compute class
// PARSING OPTIONAL ARGUMENTS
iarg = 7;
while (iarg < narg) {
// add more file options for mode == visocisty/diffusion/thermcond
if (strcmp(arg[iarg],"file") == 0) {
if (mode == DIFFUSIVITY) {
if (iarg+3 > narg) error->all(FLERR,"Illegal fix ordern command");
delete [] filename1;
filename1 = new char[strlen(arg[iarg+1])+1];
strcpy(filename1,arg[iarg+1]);
delete [] filename2;
filename2 = new char[strlen(arg[iarg+2])+1];
strcpy(filename2,arg[iarg+2]);
iarg += 3;
} else if (mode == THERMCOND || mode == VISCOSITY) {
if (iarg+2 > narg) error->all(FLERR,"Illegal fix ordern command");
delete [] filename1;
filename1 = new char[strlen(arg[iarg+1])+1];
strcpy(filename1,arg[iarg+1]);
iarg += 2;
} else error->all(FLERR,"Illegal fix ordern command");
} else if (strcmp(arg[iarg],"start") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal fix ordern command");
startstep = utils::inumeric(FLERR,arg[iarg+1],false,lmp);
iarg += 2;
} else if (strcmp(arg[iarg],"nb") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal fix ordern command");
tnb = utils::inumeric(FLERR,arg[iarg+1],false,lmp);
iarg += 2;
} else if (strcmp(arg[iarg],"nbe") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal fix ordern command");
tnbe = utils::inumeric(FLERR,arg[iarg+1],false,lmp);
iarg += 2;
} else if (strcmp(arg[iarg],"format") == 0) {
if (iarg+2 > narg) error->all(FLERR,"Illegal fix ordern command");
delete [] format_user;
int n = strlen(arg[iarg+1]) + 2;
format_user = new char[n];
sprintf(format_user," %s",arg[iarg+1]);
format = format_user;
iarg += 2;
} else if (strcmp(arg[iarg],"title") == 0) {
if (iarg+2 > narg) error->all(FLERR,"Illegal fix ordern command");
delete [] title;
int n = strlen(arg[iarg+1]) + 1;
title = new char[n];
strcpy(title,arg[iarg+1]);
iarg += 2;
} else if (strcmp(arg[iarg],"Dxyz") == 0) {
if (iarg+2 > narg) error->all(FLERR,"Illegal fix ordern command");
if (strcmp(arg[iarg+1],"no") == 0) flag_Dxyz = 0;
else if (strcmp(arg[iarg+1],"yes") == 0) flag_Dxyz = 1;
else error->all(FLERR,"Illegal fix ordern command");
iarg += 2;
} else if (strcmp(arg[iarg],"TCconvective") == 0) {
if (iarg+2 > narg) error->all(FLERR,"Illegal fix ordern command");
if (strcmp(arg[iarg+1],"no") == 0) flag_TCconv = 0;
else if (strcmp(arg[iarg+1],"yes") == 0) flag_TCconv = 1;
else error->all(FLERR,"Illegal fix ordern command");
iarg += 2;
} else error->all(FLERR,"Illegal fix ordern command");
}
// SETUP & ERROR CHECK
// for fix inputs, check that fix frequency is acceptable
// set variable_length if any compute is variable length
if (nevery <= 0 || nfreq <= 0 || startstep < 0)
error->all(FLERR,"Illegal fix ordern command: illegal numbers for fix ordern command");
if ( (nfreq % (2*nevery)) && ( (mode == VISCOSITY) || (mode == THERMCOND) ) )
error->all(FLERR,"Illegal fix ordern command: nevery is not a factor of nfreq");
if (startstep % (nevery) )
error->all(FLERR,"Illegal fix ordern command: nevery is not a factor of start");
if (icompute < 0)
error->all(FLERR,"No compute ID for fix ordern command");
if (modify->compute[icompute]->vector_flag == 0)
error->all(FLERR,"No global compute vector is computed for fix ordern command");
if (modify->compute[icompute]->size_vector_variable)
error->all(FLERR,"Input vector for fix ordern command has variable size");
// DEFINING THE PARAMETERS AND VARILABLES
count = -1; // the number of samp
cnb = 1;
boltz = force->boltz;
nktv2p = force->nktv2p;
// Specific variables for each mode
if (mode == DIFFUSIVITY) {
deltat = (double) (nevery)*(update->dt);
tngroup = group->ngroup ; // the size of the arrays (group "all" included)
ngroup = 0; // WILL BE OVERWRITTEN at count = 0 (# of groups for diffusion)
tnatom = atom->natoms; // Total # of atoms in the system (nrows = 5*tnatom)
vecsize = 0; // WILL BE OVERWRITTEN at count = 0 (# of atoms in groups)
} else if (mode == VISCOSITY) {
deltat = (double) (2.0*nevery)*(update->dt);
vecsize = 7;
sampsize = 8;
sumP = 0;
numP = 0;
} else if (mode == THERMCOND) {
deltat = (double) (2.0*nevery)*(update->dt);
vecsize = 6;
sampsize = 6;
}
// Order-n algorithm-specific parameters
if ( (mode == VISCOSITY) || (mode == THERMCOND) )
{
memory->create(data,vecsize,"fix/ordern:data");
memory->create(simpf0,vecsize,"fix/ordern:simpf0");
memory->create(simpf1,vecsize,"fix/ordern:simpf1");
memory->create(samp,tnb,tnbe,sampsize,"fix/ordern:samp");
memory->create(oldint,tnb,tnbe,vecsize,"fix/ordern:oldint");
memory->create(rint,vecsize,"fix/ordern:rint");
} else if ( mode == DIFFUSIVITY)
{
memory->create(PosC_ii,tnb,tnbe,tngroup,"fix/ordern:PosC_ii");
memory->create(PosC_iix,tnb,tnbe,tngroup,"fix/ordern:PosC_iix");
memory->create(PosC_iiy,tnb,tnbe,tngroup,"fix/ordern:PosC_iiy");
memory->create(PosC_iiz,tnb,tnbe,tngroup,"fix/ordern:PosC_iiz");
memory->create(PosC_ij,tnb,tnbe,tngroup,tngroup,"fix/ordern:PosC_ij");
memory->create(PosC_ijx,tnb,tnbe,tngroup,tngroup,"fix/ordern:PosC_ijx");
memory->create(PosC_ijy,tnb,tnbe,tngroup,tngroup,"fix/ordern:PosC_ijy");
memory->create(PosC_ijz,tnb,tnbe,tngroup,tngroup,"fix/ordern:PosC_ijz");
memory->create(PosCorrSum,tnb,tnbe,tngroup,3,"fix/ordern:PosCorrSum");
memory->create(atomingroup,tnatom,2,"fix/ordern:atomingroup");
memory->create(groupinfo,tngroup,2,"fix/ordern:groupinfo");
// NOTE: "oldint" and "rint" will be made at count = 0 in "invoke_scalar"
}
memory->create(recdata,nrows,"fix/ordern:recdata"); // data passed from compute
memory->create(nsamp,tnb,tnbe,"fix/ordern:nsamp");
memory->create(nbe,tnb,"fix/ordern:nbe");
for (int i = 0; i < tnb; i++) nbe[i]=1;
// this fix produces a global scalar
// intensive/extensive flags set by compute that produces value
// This fix produces only a SCALAR value (The timestep)
scalar_flag = 1;
vector_flag = 0;
extscalar = 0;
// nvalid = next step on which end_of_step does something
// add nvalid to all computes that store invocation times
// since don't know a priori which are invoked by this fix
// once in end_of_step() can set timestep for ones actually invoked
nvalid_last = -1;
nvalid = nextvalid();
modify->addstep_compute_all(nvalid);
// Opening new data files to output data
if (me == 0) {
if (mode == DIFFUSIVITY) {
fp1 = fopen(filename1,"w");
fp2 = fopen(filename2,"w");
if (fp1 == NULL || fp2 == NULL) {
error->all(FLERR,"Cannot open fix ordern files");
}
} else if (mode == THERMCOND || mode == VISCOSITY) {
fp1 = fopen(filename1,"w");
if (fp1 == NULL) {
error->all(FLERR,"Cannot open fix ordern files");
}
}
}
// Writing the header lines to files
if (fp1 && me == 0) {
clearerr(fp1);
if (title) fprintf(fp1,"%s\n",title);
if (mode == DIFFUSIVITY) {
fprintf(fp1,"#NOTE: MSDs should be divided by ");
fprintf(fp1,"the number of molecules of species i (N_i).\n");
fprintf(fp1,"#NOTE: MSDs have been divided by ");
fprintf(fp1,"the factor 6 (or 2, i.e., MSD_x, MSD_y, and MSD_z).\n");
} else if (mode == VISCOSITY) {
fprintf(fp1,"#NOTE: MSDs should be divided by the temperature.\n");
fprintf(fp1,"#NOTE: MSDs have been divided by ");
fprintf(fp1,"2 or 10 (i.e., the viscosity computed from all components).\n");
fprintf(fp1,"#Time\tMSD_xx\tMSD_yy\tMSD_zz\tMSD_xy\tMSD_xz\tMSD_yz\t");
fprintf(fp1,"MSD_off\tMSD_all\tMSD_bulkvisc\n");
} else if (mode == THERMCOND) {
fprintf(fp1,"#NOTE: MSDs should be divided by (temperature^2).\n");
fprintf(fp1,"#NOTE: MSDs have been divided by 2.\n");
fprintf(fp1,"#Time\tMSD_x\tMSD_y\tMSD_z\tMSD_all");
if (flag_TCconv)
fprintf(fp1,"\tMSDconvect_x\tMSDconvect_y\tMSDconvect_z\tMSDconvect_all");
fprintf(fp1,"\n");
}
if (ferror(fp1)) error->one(FLERR,"Error in writing file header for fix ordern command");
filepos1 = ftell(fp1);
}
if (fp2 && me == 0) {
clearerr(fp2);
if (title) fprintf(fp2,"%s\n",title);
if (mode == DIFFUSIVITY) {
fprintf(fp2,"#NOTE: MSDs should be divided by ");
fprintf(fp2,"the total number of molecules (N).\n");
fprintf(fp1,"#NOTE: MSDs have been divided by ");
fprintf(fp1,"the factor 6 (or 2, i.e., MSD_x, MSD_y, and MSD_z).\n");
}
if (ferror(fp2)) error->one(FLERR,"Error in writing file header for fix ordern command");
filepos2 = ftell(fp2);
}
delete [] title;
}
/* ---------------------------------------------------------------------- */
FixOrderN::~FixOrderN()
{
delete [] format_user;
if (fp1 && me == 0)
{
fclose(fp1);
delete [] filename1;
}
if (fp2 && me == 0)
{
fclose(fp2);
delete [] filename2;
}
if ( (mode == VISCOSITY) || (mode == THERMCOND) )
{
memory->destroy(data);
memory->destroy(simpf0);
memory->destroy(simpf1);
memory->destroy(samp);
} else if ( mode == DIFFUSIVITY)
{
memory->destroy(PosC_ii);
memory->destroy(PosC_iix);
memory->destroy(PosC_iiy);
memory->destroy(PosC_iiz);
memory->destroy(PosC_ij);
memory->destroy(PosC_ijx);
memory->destroy(PosC_ijy);
memory->destroy(PosC_ijz);
memory->destroy(PosCorrSum);
memory->destroy(atomingroup);
memory->destroy(groupinfo);
}
memory->destroy(recdata);
memory->destroy(nsamp);
memory->destroy(oldint);
memory->destroy(rint);
memory->destroy(nbe);
}
/* ----------------------------------------------------------------------
defines when fix can be called (at the end of step)
------------------------------------------------------------------------- */
int FixOrderN::setmask()
{
int mask = 0;
mask |= END_OF_STEP;
return mask;
}
/* ----------------------------------------------------------------------
Initializing the whole fix in Modify::init()
------------------------------------------------------------------------- */
void FixOrderN::init()
{
// set current indices for all computes,fixes,variables
int icompute_new = modify->find_compute(idcompute);
if (icompute < 0 || icompute_new != icompute)
error->all(FLERR,"No compute ID for fix ordern command");
// need to reset nvalid if nvalid < ntimestep b/c minimize was performed
if (nvalid < update->ntimestep) {
nvalid = nextvalid();
modify->addstep_compute_all(nvalid);
}
}
/* ----------------------------------------------------------------------
only does something if nvalid = current timestep
------------------------------------------------------------------------- */
void FixOrderN::setup(int /*vflag*/)
{
end_of_step();
}
/* ---------------------------------------------------------------------- */
void FixOrderN::end_of_step()
{
// skip if not step which requires doing something
// error check if timestep was reset in an invalid manner
bigint ntimestep = update->ntimestep;
if (ntimestep < nvalid_last || ntimestep > nvalid)
error->all(FLERR,"Invalid timestep reset for fix order");
if (ntimestep != nvalid) return;
nvalid_last = nvalid;
invoke_scalar(ntimestep);
}
void FixOrderN::invoke_scalar(bigint ntimestep)
{
int i,j,k,l;
double scalar = ntimestep;
// update the next timestep to call end_of_step()
modify->addstep_compute(ntimestep+nevery);
// invoke compute vector if not previously invoked
// get the data from compute_vector and store it in recdata
// the size of recdata is nrows
modify->clearstep_compute();
Compute *compute = modify->compute[icompute]; // the whole compute class
if (!(compute->invoked_flag & INVOKED_VECTOR)) {
compute->compute_vector();
compute->invoked_flag |= INVOKED_VECTOR;
}
double *cvector = compute->vector;
for (i = 0; i < nrows; i++)
recdata[i] = cvector[i];
// update the next timestep to call end_of_step()
nvalid += nevery;
modify->addstep_compute(nvalid);
// Check if this timestep has not been sampled during restarting
if (restart_continue)
{
restart_continue = 0;
return;
}
if (count < 0)
{
count = 0;
icount = 0;
} else {
icount++;
if ( (mode == DIFFUSIVITY) || (icount%2 == 0) )
count++;
}
// From now, everything is computed only on the main core
if (me != 0) return;
// Preliminary calculations for each transport property
// Fill in the vector "data" accordingly
if (mode == DIFFUSIVITY) // DIFFUSION
{
if (count == 0) // only run during the first time step
{
natom = 0;
// Finding corresponding groups to each atom at the first time
for (sortID = 0; sortID < tnatom; sortID++)
{
ID = (int) (recdata[5*sortID+3]+0.1);
atommask = (int) recdata[5*sortID+4]+0.1;
int groupfound = 0;
if (ngroup > 0 ) // First try to match with available groups
{
for (j = 1; j <= ngroup; j++)
{
if ( atommask & groupinfo[j][1] )
{
atomingroup[ID][0] = natom; // ID starts from 0
atomingroup[ID][1] = j; // groupID starts from 1
natom++;
groupfound = 1;
break;
}
}
}
if (groupfound == 0) // If no match, try to find a new group
{
for (k = 1 ; k < tngroup; k++)
{
if ( atommask & group->bitmask[k] )
{
ngroup++;
groupinfo[ngroup][0] = k;
groupinfo[ngroup][1] = group->bitmask[k];
atomingroup[ID][0] = natom; // ID starts from 0
atomingroup[ID][1] = ngroup; // groupID starts from 1
natom++;
groupfound = 1;
break;
}
}
}
// if this atom doesn't belong to any group
if (groupfound == 0)
atomingroup[ID][0] = atomingroup[ID][1] = -1;
}
vecsize = 3*natom;
// NOTE: "oldint" and "rint" are constructed hear at count = 0
memory->create(oldint,tnb,tnbe,vecsize,"fix/ordern:oldint");
memory->create(rint,vecsize,"fix/ordern:rint");
// initializing the arrays
for(sortID = 0; sortID < tnatom ; sortID++)
{
ID = (int) (recdata[5*sortID+3]+0.1);
if (atomingroup[ID][0] < 0)
continue;
atomID = atomingroup[ID][0];
for( i = 0; i < tnb; i++)
{
oldint[i][tnbe-1][3*atomID] = recdata[5*sortID];
oldint[i][tnbe-1][3*atomID+1] = recdata[5*sortID+1];
oldint[i][tnbe-1][3*atomID+2] = recdata[5*sortID+2];
for ( j = 0; j < tnbe; j++)
{
nsamp[i][j] = 0.0;
for ( k = 0; k < tngroup; k++)
{
PosC_ii[i][j][k] = 0.0;
PosC_iix[i][j][k] = 0.0;
PosC_iiy[i][j][k] = 0.0;
PosC_iiz[i][j][k] = 0.0;
for ( l = 0; l < tngroup ; l++)
{
PosC_ij[i][j][k][l] = 0.0;
PosC_ijx[i][j][k][l] = 0.0;
PosC_ijy[i][j][k][l] = 0.0;
PosC_ijz[i][j][k][l] = 0.0;
}
}
}
}
}
}
if (count == 0) return; // nothing to do at this timestep
for(sortID = 0; sortID < tnatom ; sortID++)
{
ID = (int) (recdata[5*sortID+3]+0.1);
if (atomingroup[ID][0] < 0)
continue;
atomID = atomingroup[ID][0];
rint[3*atomID] = recdata[5*sortID];
rint[3*atomID+1] = recdata[5*sortID+1];
rint[3*atomID+2] = recdata[5*sortID+2];
}
} else if (mode == VISCOSITY) // VISCOSITY
{
if (count == 0) {
for ( i = 0; i<tnb; i++ )
for ( j = 0; j < tnbe; j++ ) {
nsamp[i][j] = 0.0;
for (int k = 0; k < vecsize; k++) {
oldint[i][j][k] = 0.0;
samp[i][j][k] = 0.0;
}
samp[i][j][vecsize] = 0.0;
}
}
data[6] = (recdata[0]+recdata[1]+recdata[2])/3.0;
for (i = 0; i < 3; i++) data[i] = (recdata[i] - data[6]);
for (i = 3; i < 6; i++) data[i] = recdata[i];
sumP += data[6];
numP += 1.0;
} else if (mode == THERMCOND) // THERMAL CONDUCTIVITY
{
if (count == 0) {
for ( i = 0; i<tnb; i++ )
for ( j = 0; j < tnbe; j++ ) {
nsamp[i][j] = 0.0;
for (int k = 0; k < vecsize; k++) {
oldint[i][j][k] = 0.0;
samp[i][j][k] = 0.0;
}
}
}
for (i = 0; i < vecsize; i++) data[i] = recdata[i];
}
// INTEGRATION according to Simpson's rule
if (mode == VISCOSITY || mode == THERMCOND)
{
integrate();
if ((icount % 2) == 1) return; // return for odd timesteps
}
// ORDER-N ALGORITHM
// Assign the number of active blocks
i = count/(pow(tnbe,cnb));
while (i != 0)
{
cnb++;
i /= tnbe;
}
for(i = 0; i < cnb; i++) // loop over all active blocks
{
if ((count)%((int)pow(tnbe,i))==0)
{
cnbe = MIN(nbe[i],tnbe);
for (j=tnbe-1; j>=tnbe-cnbe; j--) // loop over all active block elements
{
nsamp[i][j] += 1.0;
if ( (mode == VISCOSITY) || (mode == THERMCOND) )
{
for ( k = 0; k < vecsize; k++)
{
dist = rint[k]-oldint[i][j][k];
// Add the coefficient later
samp[i][j][k] += (dist*dist);
if ( (mode == VISCOSITY) && (k == vecsize-1) )
{
samp[i][j][vecsize] += (dist);
}
}
} else if (mode == DIFFUSIVITY)
{
for( k = 1 ; k <= ngroup ; k++ )
{
PosCorrSum[i][j][k][0] = 0;
PosCorrSum[i][j][k][1] = 0;
PosCorrSum[i][j][k][2] = 0;
}
double distx, disty, distz;
double distx2, disty2, distz2;
for (ID = 0; ID < tnatom ; ID++)
{
if (atomingroup[ID][0] < 0)
continue;
atomID = atomingroup[ID][0];
atomgroup = atomingroup[ID][1];
distx = oldint[i][j][3*atomID] - rint[3*atomID];
disty = oldint[i][j][3*atomID+1] - rint[3*atomID+1];
distz = oldint[i][j][3*atomID+2] - rint[3*atomID+2];
distx2 = distx*distx;
disty2 = disty*disty;
distz2 = distz*distz;
// Add coefficients (2.0 and 6.0) later
PosC_iix[i][j][atomgroup] += distx2;
PosC_iiy[i][j][atomgroup] += disty2;
PosC_iiz[i][j][atomgroup] += distz2;
PosC_ii[i][j][atomgroup] += (distx2 + disty2 + distz2);
PosCorrSum[i][j][atomgroup][0] += distx;
PosCorrSum[i][j][atomgroup][1] += disty;
PosCorrSum[i][j][atomgroup][2] += distz;
}
for ( k = 1 ; k <= ngroup ; k++)
{
for ( l = 1 ; l <= ngroup ; l++)
{
distx2 = PosCorrSum[i][j][k][0] * PosCorrSum[i][j][l][0];
disty2 = PosCorrSum[i][j][k][1] * PosCorrSum[i][j][l][1];
distz2 = PosCorrSum[i][j][k][2] * PosCorrSum[i][j][l][2];
// Add coefficients (2.0 and 6.0) later
PosC_ijx[i][j][k][l] += distx2;
PosC_ijy[i][j][k][l] += disty2;
PosC_ijz[i][j][k][l] += distz2;
PosC_ij[i][j][k][l] += ( distx2 + disty2 + distz2 );
}
}
}
}
//increase the current blocklength
nbe[i]++;
//shift to the left, set last index to the correlation value
if ( (mode == VISCOSITY) || (mode == THERMCOND) )
{
for (int k=0; k < vecsize; k++)
{
for (int j=1; j < tnbe; j++)
oldint[i][j-1][k] = oldint[i][j][k] ;
oldint[i][tnbe-1][k] = rint[k];
}
} else if (mode == DIFFUSIVITY)
{
for (ID = 0; ID < tnatom ; ID++)
{
if (atomingroup[ID][0] < 0)
continue;
atomID = atomingroup[ID][0];
for(j = 1 ; j < tnbe ; j++)
{
oldint[i][j-1][3*atomID] = oldint[i][j][3*atomID];
oldint[i][j-1][3*atomID+1] = oldint[i][j][3*atomID+1];
oldint[i][j-1][3*atomID+2] = oldint[i][j][3*atomID+2];
}
oldint[i][tnbe-1][3*atomID] = rint[3*atomID];
oldint[i][tnbe-1][3*atomID+1] = rint[3*atomID+1];
oldint[i][tnbe-1][3*atomID+2] = rint[3*atomID+2];
}
}
}
}
// Output results to files (fp1 and fp2) if time == nfreq
if (ntimestep % nfreq) return;
if (mode == DIFFUSIVITY)
write_diffusivity();
else if (mode == VISCOSITY)
write_viscosity();
else if (mode == THERMCOND)
write_thermcond();
}
/* ----------------------------------------------------------------------
calculate nvalid = next step on which end_of_step does something
lower bound is the smallest multiple of nevery larger than startstep
used only in the initialization
------------------------------------------------------------------------- */
bigint FixOrderN::nextvalid()
{
bigint nvalid = update->ntimestep;
if (startstep > nvalid) nvalid = startstep;
if (nvalid % nevery) nvalid = (nvalid/nevery)*nevery+nevery;
return nvalid;
}
/*-------------------------------------------------------------------------
Integrating Dynamical Variables According to the Simpson's Rule
------------------------------------------------------------------------- */
void FixOrderN::integrate()
{
if (icount == 0) {
for (int i = 0; i < vecsize; i++)
{
rint[i] = 0.0;
simpf0[i] = data[i];
}
} else if ((icount % 2) == 1) {
for (int i = 0; i < vecsize; i++)
simpf1[i] = data[i];
} else {
for (int i = 0; i < vecsize; i++)
{
// delta = dt*(f0+4f1+f2)/6
rint[i] += deltat*(simpf0[i]+4*simpf1[i]+data[i])/6.0;
simpf0[i] = data[i];
simpf1[i] = 0.0;
}
}
return;
}
/*-------------------------------------------------------------------------
Writing Order-n Results for Diffusivity into a File
------------------------------------------------------------------------- */
void FixOrderN::write_diffusivity()
{
fseek(fp1,filepos1,SEEK_SET);
fseek(fp2,filepos2,SEEK_SET);
int i, j, k, l, xyz;
// Writing the header
fprintf(fp1,"#Time\t");
fprintf(fp2,"#Time\t");
for ( k = 1; k <= ngroup; k++)
{
if (flag_Dxyz)
{
fprintf(fp1,"MSD__%s_x\t",group->names[groupinfo[k][0]]);
fprintf(fp1,"MSD__%s_y\t",group->names[groupinfo[k][0]]);
fprintf(fp1,"MSD__%s_z\t",group->names[groupinfo[k][0]]);
}
fprintf(fp1,"MSD__%s\t",group->names[groupinfo[k][0]]);
for ( l = k; l <= ngroup; l++)
{
if (flag_Dxyz)
{
fprintf(fp2,"MSD__%s_%s_x\t",
group->names[groupinfo[k][0]],group->names[groupinfo[l][0]]);
fprintf(fp2,"MSD__%s_%s_y\t",
group->names[groupinfo[k][0]],group->names[groupinfo[l][0]]);
fprintf(fp2,"MSD__%s_%s_z\t",
group->names[groupinfo[k][0]],group->names[groupinfo[l][0]]);
}
fprintf(fp2,"MSD__%s_%s\t",
group->names[groupinfo[k][0]],group->names[groupinfo[l][0]]);
}
}
fprintf(fp1,"\n");
fprintf(fp2,"\n");
for( i = 0;i < MIN(tnb,cnb); i++ )
{
cnbe = MIN(nbe[i],tnbe);
if (i == MIN(tnb,cnb)-1)
cnbe = cnbe-1;
for( j = 1; j <= cnbe; j++ )
{
time = (double) ((1.0*j)*(deltat)*pow(tnbe,i));
fprintf(fp1,format,time);
fprintf(fp2,format,time);
for( k = 1; k <= ngroup; k++ )
{
if (flag_Dxyz)
{
fprintf(fp1,format,PosC_iix[i][tnbe-j][k]/nsamp[i][tnbe-j]/2.0);
fprintf(fp1,format,PosC_iiy[i][tnbe-j][k]/nsamp[i][tnbe-j]/2.0);
fprintf(fp1,format,PosC_iiz[i][tnbe-j][k]/nsamp[i][tnbe-j]/2.0);
}
fprintf(fp1,format,PosC_ii[i][tnbe-j][k]/nsamp[i][tnbe-j]/6.0);
for( l = k; l <= ngroup; l++ )
{
if (flag_Dxyz)
{
fprintf(fp2,format,PosC_ijx[i][tnbe-j][k][l]/nsamp[i][tnbe-j]/2.0);
fprintf(fp2,format,PosC_ijy[i][tnbe-j][k][l]/nsamp[i][tnbe-j]/2.0);
fprintf(fp2,format,PosC_ijz[i][tnbe-j][k][l]/nsamp[i][tnbe-j]/2.0);
}
fprintf(fp2,format,PosC_ij[i][tnbe-j][k][l]/nsamp[i][tnbe-j]/6.0);
}
}
fprintf(fp1,"\n");
fprintf(fp2,"\n");
}
}
fflush(fp1);
fflush(fp2);
// delete all unnecessary text from the output file
long fileend1 = ftell(fp1);
if (fileend1 > 0) ftruncate(fileno(fp1),fileend1);
long fileend2 = ftell(fp2);
if (fileend2 > 0) ftruncate(fileno(fp2),fileend2);
}
/*-------------------------------------------------------------------------
Writing Order-n Results for Viscosity into a File
------------------------------------------------------------------------- */
void FixOrderN::write_viscosity()
{
fseek(fp1,filepos1,SEEK_SET);
int i, j, k;
double totalall;
double totaloff;
double stresscomp;
double volume = (domain->xprd * domain->yprd * domain->zprd);
double coef = (volume/2.0/boltz)*(1.0/nktv2p);
avgP = sumP / numP;
for (i=0; i<MIN(tnb,cnb); i++)
{
cnbe = MIN(nbe[i],tnbe);
if (i == MIN(tnb,cnb)-1)
cnbe = cnbe-1;
for (j = 1; j <= cnbe; j++) // Just neglect the first data on the right (k=2)
{
time = (double) ((1.0*j)*(deltat)*pow(tnbe,i));
fprintf(fp1,format,time);
// SHEAR VISCOSITY
totalall = 0.0;
totaloff = 0.0;
for (k = 0; k < 6; k++)
{
stresscomp = coef*samp[i][tnbe-j][k]/nsamp[i][tnbe-j];
if (k<3)
{
// implicit 4/3 contribution from diagonal
fprintf(fp1,format,stresscomp*3.0/4.0);
totalall += 1.0*1.0*stresscomp/10.0;
}
else
{
fprintf(fp1,format,stresscomp);
totalall += 2.0*1.0*stresscomp/10.0;
totaloff += stresscomp/3.0;
}
}
fprintf(fp1,format,totaloff);
fprintf(fp1,format,totalall);
// BULK VISCOSITY
double intp2 = samp[i][tnbe-j][vecsize-1]/nsamp[i][tnbe-j];
double intp = samp[i][tnbe-j][vecsize]/nsamp[i][tnbe-j];
double bulkvis = coef*(intp2 - 2.0*intp*(avgP*time) + (avgP*time)*(avgP*time));
fprintf(fp1,format,bulkvis);
fprintf(fp1,"\n");
}
}
fflush(fp1);
// delete all unnecessary text from the output file
long fileend1 = ftell(fp1);
if (fileend1 > 0) ftruncate(fileno(fp1),fileend1);
}
/*-------------------------------------------------------------------------
Writing Order-n Results for Thermal Conductivity into a File
------------------------------------------------------------------------- */
void FixOrderN::write_thermcond()
{
fseek(fp1,filepos1,SEEK_SET);
int i, j, k;
double totalcond;
double totalconvcond;
double fluxcomp;
double volume = (domain->xprd * domain->yprd * domain->zprd);
double coef = (1.0/volume/2.0/boltz);
for (i=0; i<MIN(tnb,cnb); i++)
{
cnbe = MIN(nbe[i],tnbe);
if (i == MIN(tnb,cnb)-1)
cnbe = cnbe-1;
for (j = 1; j <= cnbe; j++) // Just neglect the first data on the right (k=2)
{
time = (double) ((1.0*j)*(deltat)*pow(tnbe,i));
fprintf(fp1,format,time);
totalcond = 0.0;
totalconvcond = 0.0;
for (k = 0; k < 3; k++)
{
fluxcomp = coef*samp[i][tnbe-j][k]/nsamp[i][tnbe-j];
fprintf(fp1,format,fluxcomp);
totalcond += fluxcomp/3.0;
}
fprintf(fp1,format,totalcond);
if (flag_TCconv) // convective terms of the heat flux
{
for (k = 3; k < 6; k++)
{
fluxcomp = coef*samp[i][tnbe-j][k]/nsamp[i][tnbe-j];
fprintf(fp1,format,fluxcomp);
totalconvcond += fluxcomp/3.0;
}
fprintf(fp1,format,totalconvcond);
}
fprintf(fp1,"\n");
}
}
fflush(fp1);
// delete all unnecessary text from the output file
long fileend1 = ftell(fp1);
if (fileend1 > 0) ftruncate(fileno(fp1),fileend1);
}
/* ----------------------------------------------------------------------
Write Restart data to restart file
this is based on the command fix ave/correlate/long
------------------------------------------------------------------------- */
void FixOrderN::write_restart(FILE *fp)
{
if (me == 0)
{
int i, j, k, l;
int nsize = 1 + 6 + 4; // initial + general + specific variables
nsize += (tnb + tnb*tnbe + tnb*tnbe*vecsize + vecsize); // nbe,nsamp,oldint,rint
if ( (mode == VISCOSITY) || (mode == THERMCOND) ) {
nsize += (tnb*tnbe*sampsize + vecsize*3); // samp, simpf0, simpf1, data
} else if (mode == DIFFUSIVITY) {
nsize += (tnb*tnbe*tngroup*(4 + 5*tngroup)); // 4*PosC_ii,4*PosC_ij,PosCorrSum
nsize += (2*tngroup + 2*tnatom); // groupinfo, atomingroup
}
int n = 0; // the counter over all components of the array
double *list; // the array written to the restart file
memory->create(list,nsize,"fix/ordern:list");
// Parameters of the simulation //
list[n++] = mode;
list[n++] = tnb;
list[n++] = tnbe;
list[n++] = vecsize;
list[n++] = count;
list[n++] = cnb;
if ( (mode == VISCOSITY) || (mode == THERMCOND) ) {
list[n++] = sampsize;
list[n++] = icount;
list[n++] = sumP;
list[n++] = numP;
} else if (mode == DIFFUSIVITY) {
list[n++] = ngroup;
list[n++] = natom;
list[n++] = tngroup;
list[n++] = tnatom;
}