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
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
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_neigh_history.h"
#include <mpi.h>
#include <cstring>
#include "my_page.h"
#include "atom.h"
#include "comm.h"
#include "neighbor.h"
#include "neigh_list.h"
#include "force.h"
#include "pair.h"
#include "memory.h"
#include "error.h"
using namespace LAMMPS_NS;
using namespace FixConst;
enum{DEFAULT,NPARTNER,PERPARTNER}; // also set in fix neigh/history/omp
/* ---------------------------------------------------------------------- */
FixNeighHistory::FixNeighHistory(LAMMPS *lmp, int narg, char **arg) :
Fix(lmp, narg, arg),
pair(NULL), npartner(NULL), partner(NULL), valuepartner(NULL),
ipage_atom(NULL), dpage_atom(NULL), ipage_neigh(NULL), dpage_neigh(NULL)
{
if (narg != 4) error->all(FLERR,"Illegal fix NEIGH_HISTORY command");
restart_peratom = 1;
create_attribute = 1;
maxexchange_dynamic = 1;
newton_pair = force->newton_pair;
dnum = force->inumeric(FLERR,arg[3]);
dnumbytes = dnum * sizeof(double);
zeroes = new double[dnum];
for (int i = 0; i < dnum; i++) zeroes[i] = 0.0;
onesided = 0;
if (strcmp(id,"LINE_NEIGH_HISTORY") == 0) onesided = 1;
if (strcmp(id,"TRI_NEIGH_HISTORY") == 0) onesided = 1;
if (newton_pair) comm_reverse = 1; // just for single npartner value
// variable-size history communicated via
// reverse_comm_fix_variable()
// perform initial allocation of atom-based arrays
// register with atom class
grow_arrays(atom->nmax);
atom->add_callback(0);
atom->add_callback(1);
pgsize = oneatom = 0;
// other per-atom vectors
firstflag = NULL;
firstvalue = NULL;
maxatom = 0;
// per-atom and per-neighbor data structs
ipage_atom = NULL;
dpage_atom = NULL;
ipage_neigh = NULL;
dpage_neigh = NULL;
// initialize npartner to 0 so neighbor list creation is OK the 1st time
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++) npartner[i] = 0;
maxpartner = 0;
nlocal_neigh = nall_neigh = 0;
commflag = DEFAULT;
}
/* ---------------------------------------------------------------------- */
FixNeighHistory::~FixNeighHistory()
{
if (copymode) return;
// unregister this fix so atom class doesn't invoke it any more
atom->delete_callback(id,0);
atom->delete_callback(id,1);
// delete locally stored arrays
delete [] zeroes;
memory->sfree(firstflag);
memory->sfree(firstvalue);
memory->destroy(npartner);
memory->sfree(partner);
memory->sfree(valuepartner);
delete [] ipage_atom;
delete [] dpage_atom;
delete [] ipage_neigh;
delete [] dpage_neigh;
// to better detect use-after-delete errors
firstflag = NULL;
firstvalue = NULL;
pair = NULL;
npartner = NULL;
partner = NULL;
valuepartner = NULL;
}
/* ---------------------------------------------------------------------- */
int FixNeighHistory::setmask()
{
int mask = 0;
mask |= PRE_EXCHANGE;
mask |= MIN_PRE_EXCHANGE;
mask |= POST_NEIGHBOR;
mask |= MIN_POST_NEIGHBOR;
return mask;
}
/* ---------------------------------------------------------------------- */
void FixNeighHistory::init()
{
if (atom->tag_enable == 0)
error->all(FLERR,"Neighbor history requires atoms have IDs");
allocate_pages();
}
/* ----------------------------------------------------------------------
create pages if first time or if neighbor pgsize/oneatom has changed
note that latter could cause neighbor history info to be discarded
------------------------------------------------------------------------- */
void FixNeighHistory::allocate_pages()
{
int create = 0;
if (ipage_atom == NULL) create = 1;
if (pgsize != neighbor->pgsize) create = 1;
if (oneatom != neighbor->oneatom) create = 1;
if (create) {
delete [] ipage_atom;
delete [] dpage_atom;
delete [] ipage_neigh;
delete [] dpage_neigh;
pgsize = neighbor->pgsize;
oneatom = neighbor->oneatom;
int nmypage = comm->nthreads;
ipage_atom = new MyPage<tagint>[nmypage];
dpage_atom = new MyPage<double>[nmypage];
ipage_neigh = new MyPage<int>[nmypage];
dpage_neigh = new MyPage<double>[nmypage];
for (int i = 0; i < nmypage; i++) {
ipage_atom[i].init(oneatom,pgsize);
dpage_atom[i].init(dnum*oneatom,dnum*pgsize);
ipage_neigh[i].init(oneatom,pgsize);
dpage_neigh[i].init(dnum*oneatom,dnum*pgsize);
}
}
}
/* ---------------------------------------------------------------------- */
void FixNeighHistory::setup_post_neighbor()
{
post_neighbor();
}
/* ----------------------------------------------------------------------
copy partner info from neighbor data structs (NDS) to atom arrays
should be called whenever NDS store current history info
and need to transfer the info to owned atoms
e.g. when atoms migrate to new procs, new neigh list built, or between runs
when atoms may be added or deleted (NDS becomes out-of-date)
the next post_neighbor() will put this info back into new NDS
called during run before atom exchanges, including for restart files
called at end of run via post_run()
do not call during setup of run (setup_pre_exchange)
b/c there is no guarantee of a current NDS (even on continued run)
if run command does a 2nd run with pre = no, then no neigh list
will be built, but old neigh list will still have the info
onesided and newton on and newton off versions
------------------------------------------------------------------------- */
void FixNeighHistory::pre_exchange()
{
if (onesided) pre_exchange_onesided();
else if (newton_pair) pre_exchange_newton();
else pre_exchange_no_newton();
}
/* ----------------------------------------------------------------------
onesided version for sphere contact with line/tri particles
neighbor list has I = sphere, J = line/tri
only store history info with spheres
------------------------------------------------------------------------- */
void FixNeighHistory::pre_exchange_onesided()
{
int i,j,ii,jj,m,n,inum,jnum;
int *ilist,*jlist,*numneigh,**firstneigh;
int *allflags;
double *allvalues,*onevalues;
// NOTE: all operations until very end are with nlocal_neigh <= current nlocal
// b/c previous neigh list was built with nlocal_neigh
// nlocal can be larger if other fixes added atoms at this pre_exchange()
// clear two paged data structures
ipage_atom->reset();
dpage_atom->reset();
// 1st loop over neighbor list, I = sphere, J = tri
// only calculate npartner for owned spheres
for (i = 0; i < nlocal_neigh; i++) npartner[i] = 0;
tagint *tag = atom->tag;
NeighList *list = pair->list;
inum = list->inum;
ilist = list->ilist;
numneigh = list->numneigh;
firstneigh = list->firstneigh;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
allflags = firstflag[i];
for (jj = 0; jj < jnum; jj++)
if (allflags[jj]) npartner[i]++;
}
// get page chunks to store partner IDs and values for owned atoms
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
n = npartner[i];
partner[i] = ipage_atom->get(n);
valuepartner[i] = dpage_atom->get(dnum*n);
if (partner[i] == NULL || valuepartner[i] == NULL)
error->one(FLERR,"Neighbor history overflow, boost neigh_modify one");
}
// 2nd loop over neighbor list, I = sphere, J = tri
// store partner IDs and values for owned+ghost atoms
// re-zero npartner to use as counter
for (i = 0; i < nlocal_neigh; i++) npartner[i] = 0;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
allflags = firstflag[i];
allvalues = firstvalue[i];
for (jj = 0; jj < jnum; jj++) {
if (allflags[jj]) {
onevalues = &allvalues[dnum*jj];
j = jlist[jj];
j &= NEIGHMASK;
m = npartner[i]++;
partner[i][m] = tag[j];
memcpy(&valuepartner[i][dnum*m],onevalues,dnumbytes);
}
}
}
// set maxpartner = max # of partners of any owned atom
// maxexchange = max # of values for any Comm::exchange() atom
maxpartner = 0;
for (i = 0; i < nlocal_neigh; i++) maxpartner = MAX(maxpartner,npartner[i]);
maxexchange = (dnum+1)*maxpartner + 1;
// zero npartner values from previous nlocal_neigh to current nlocal
int nlocal = atom->nlocal;
for (i = nlocal_neigh; i < nlocal; i++) npartner[i] = 0;
}
/* ----------------------------------------------------------------------
newton ON version
performs reverse comm to acquire partner values from ghost atoms
------------------------------------------------------------------------- */
void FixNeighHistory::pre_exchange_newton()
{
int i,j,ii,jj,m,n,inum,jnum;
int *ilist,*jlist,*numneigh,**firstneigh;
int *allflags;
double *allvalues,*onevalues,*jvalues;
// NOTE: all operations until very end are with
// nlocal_neigh <= current nlocal and nall_neigh
// b/c previous neigh list was built with nlocal_neigh & nghost_neigh
// nlocal can be larger if other fixes added atoms at this pre_exchange()
// clear two paged data structures
ipage_atom->reset();
dpage_atom->reset();
// 1st loop over neighbor list
// calculate npartner for owned+ghost atoms
for (i = 0; i < nall_neigh; i++) npartner[i] = 0;
tagint *tag = atom->tag;
NeighList *list = pair->list;
inum = list->inum;
ilist = list->ilist;
numneigh = list->numneigh;
firstneigh = list->firstneigh;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
allflags = firstflag[i];
for (jj = 0; jj < jnum; jj++) {
if (allflags[jj]) {
npartner[i]++;
j = jlist[jj];
j &= NEIGHMASK;
npartner[j]++;
}
}
}
// perform reverse comm to augment owned npartner counts with ghost counts
commflag = NPARTNER;
comm->reverse_comm_fix(this,0);
// get page chunks to store partner IDs and values for owned+ghost atoms
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
n = npartner[i];
partner[i] = ipage_atom->get(n);
valuepartner[i] = dpage_atom->get(dnum*n);
if (partner[i] == NULL || valuepartner[i] == NULL) {
error->one(FLERR,"Neighbor history overflow, boost neigh_modify one");
}
}
for (i = nlocal_neigh; i < nall_neigh; i++) {
n = npartner[i];
partner[i] = ipage_atom->get(n);
valuepartner[i] = dpage_atom->get(dnum*n);
if (partner[i] == NULL || valuepartner[i] == NULL) {
error->one(FLERR,"Neighbor history overflow, boost neigh_modify one");
}
}
// 2nd loop over neighbor list
// store partner IDs and values for owned+ghost atoms
// re-zero npartner to use as counter
for (i = 0; i < nall_neigh; i++) npartner[i] = 0;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
allflags = firstflag[i];
allvalues = firstvalue[i];
for (jj = 0; jj < jnum; jj++) {
if (allflags[jj]) {
onevalues = &allvalues[dnum*jj];
j = jlist[jj];
j &= NEIGHMASK;
m = npartner[i]++;
partner[i][m] = tag[j];
memcpy(&valuepartner[i][dnum*m],onevalues,dnumbytes);
m = npartner[j]++;
partner[j][m] = tag[i];
jvalues = &valuepartner[j][dnum*m];
if (pair->nondefault_history_transfer)
pair->transfer_history(onevalues,jvalues);
else for (n = 0; n < dnum; n++) jvalues[n] = -onevalues[n];
}
}
}
// perform reverse comm to augment
// owned atom partner/valuepartner with ghost info
// use variable variant b/c size of packed data can be arbitrarily large
// if many touching neighbors for large particle
commflag = PERPARTNER;
comm->reverse_comm_fix_variable(this);
// set maxpartner = max # of partners of any owned atom
// maxexchange = max # of values for any Comm::exchange() atom
maxpartner = 0;
for (i = 0; i < nlocal_neigh; i++) maxpartner = MAX(maxpartner,npartner[i]);
maxexchange = (dnum+1)*maxpartner + 1;
// zero npartner values from previous nlocal_neigh to current nlocal
int nlocal = atom->nlocal;
for (i = nlocal_neigh; i < nlocal; i++) npartner[i] = 0;
}
/* ----------------------------------------------------------------------
newton OFF version
do not need partner values from ghost atoms
assume J values are negative of I values
------------------------------------------------------------------------- */
void FixNeighHistory::pre_exchange_no_newton()
{
int i,j,ii,jj,m,n,inum,jnum;
int *ilist,*jlist,*numneigh,**firstneigh;
int *allflags;
double *allvalues,*onevalues,*jvalues;
// NOTE: all operations until very end are with nlocal_neigh <= current nlocal
// b/c previous neigh list was built with nlocal_neigh
// nlocal can be larger if other fixes added atoms at this pre_exchange()
// clear two paged data structures
ipage_atom->reset();
dpage_atom->reset();
// 1st loop over neighbor list
// calculate npartner for owned atoms
for (i = 0; i < nlocal_neigh; i++) npartner[i] = 0;
tagint *tag = atom->tag;
NeighList *list = pair->list;
inum = list->inum;
ilist = list->ilist;
numneigh = list->numneigh;
firstneigh = list->firstneigh;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
allflags = firstflag[i];
for (jj = 0; jj < jnum; jj++) {
if (allflags[jj]) {
npartner[i]++;
j = jlist[jj];
j &= NEIGHMASK;
if (j < nlocal_neigh) npartner[j]++;
}
}
}
// get page chunks to store partner IDs and values for owned atoms
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
n = npartner[i];
partner[i] = ipage_atom->get(n);
valuepartner[i] = dpage_atom->get(dnum*n);
if (partner[i] == NULL || valuepartner[i] == NULL)
error->one(FLERR,"Neighbor history overflow, boost neigh_modify one");
}
// 2nd loop over neighbor list
// store partner IDs and values for owned+ghost atoms
// re-zero npartner to use as counter
for (i = 0; i < nlocal_neigh; i++) npartner[i] = 0;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
allflags = firstflag[i];
allvalues = firstvalue[i];
for (jj = 0; jj < jnum; jj++) {
if (allflags[jj]) {
onevalues = &allvalues[dnum*jj];
j = jlist[jj];
j &= NEIGHMASK;
m = npartner[i]++;
partner[i][m] = tag[j];
memcpy(&valuepartner[i][dnum*m],onevalues,dnumbytes);
if (j < nlocal_neigh) {
m = npartner[j]++;
partner[j][m] = tag[i];
jvalues = &valuepartner[j][dnum*m];
if (pair->nondefault_history_transfer)
pair->transfer_history(onevalues, jvalues);
else for (n = 0; n < dnum; n++) jvalues[n] = -onevalues[n];
}
}
}
}
// set maxpartner = max # of partners of any owned atom
// maxexchange = max # of values for any Comm::exchange() atom
maxpartner = 0;
for (i = 0; i < nlocal_neigh; i++) maxpartner = MAX(maxpartner,npartner[i]);
maxexchange = (dnum+1)*maxpartner + 1;
// zero npartner values from previous nlocal_neigh to current nlocal
int nlocal = atom->nlocal;
for (i = nlocal_neigh; i < nlocal; i++) npartner[i] = 0;
}
/* ---------------------------------------------------------------------- */
void FixNeighHistory::min_pre_exchange()
{
pre_exchange();
}
/* ----------------------------------------------------------------------
called after neighbor list is build
recover history info stored temporarily in per-atom partner lists
and store afresh in per-neighbor firstflag and firstvalue lists
------------------------------------------------------------------------- */
void FixNeighHistory::post_neighbor()
{
int i,j,m,ii,jj,nn,np,inum,jnum,rflag;
tagint jtag;
int *ilist,*jlist,*numneigh,**firstneigh;
int *allflags;
double *allvalues;
// store atom counts used for new neighbor list which was just built
int nlocal = atom->nlocal;
int nall = nlocal + atom->nghost;
nlocal_neigh = nlocal;
nall_neigh = nall;
// realloc firstflag and firstvalue if needed
if (maxatom < nlocal) {
memory->sfree(firstflag);
memory->sfree(firstvalue);
maxatom = nall;
firstflag = (int **)
memory->smalloc(maxatom*sizeof(int *),"neighbor_history:firstflag");
firstvalue = (double **)
memory->smalloc(maxatom*sizeof(double *),"neighbor_history:firstvalue");
}
// loop over newly built neighbor list
// repopulate entire per-neighbor data structs
// whether with old-neigh partner info or zeroes
ipage_neigh->reset();
dpage_neigh->reset();
tagint *tag = atom->tag;
NeighList *list = pair->list;
inum = list->inum;
ilist = list->ilist;
numneigh = list->numneigh;
firstneigh = list->firstneigh;
for (ii = 0; ii < inum; ii++) {
i = ilist[ii];
jlist = firstneigh[i];
jnum = numneigh[i];
firstflag[i] = allflags = ipage_neigh->get(jnum);
firstvalue[i] = allvalues = dpage_neigh->get(jnum*dnum);
np = npartner[i];
nn = 0;
for (jj = 0; jj < jnum; jj++) {
j = jlist[jj];
rflag = sbmask(j) | pair->beyond_contact;
j &= NEIGHMASK;
jlist[jj] = j;
// rflag = 1 if r < radsum in npair_size() method
// preserve neigh history info if tag[j] is in old-neigh partner list
// this test could be more geometrically precise for two sphere/line/tri
if (rflag) {
jtag = tag[j];
for (m = 0; m < np; m++)
if (partner[i][m] == jtag) break;
if (m < np) {
allflags[jj] = 1;
memcpy(&allvalues[nn],&valuepartner[i][dnum*m],dnumbytes);
} else {
allflags[jj] = 0;
memcpy(&allvalues[nn],zeroes,dnumbytes);
}
} else {
allflags[jj] = 0;
memcpy(&allvalues[nn],zeroes,dnumbytes);
}
nn += dnum;
}
}
}
/* ---------------------------------------------------------------------- */
void FixNeighHistory::min_post_neighbor()
{
post_neighbor();
}
/* ---------------------------------------------------------------------- */
void FixNeighHistory::post_run()
{
pre_exchange();
}
/* ----------------------------------------------------------------------
memory usage of local atom-based arrays
------------------------------------------------------------------------- */
double FixNeighHistory::memory_usage()
{
int nmax = atom->nmax;
double bytes = nmax * sizeof(int); // npartner
bytes += nmax * sizeof(tagint *); // partner
bytes += nmax * sizeof(double *); // valuepartner
bytes += maxatom * sizeof(int *); // firstflag
bytes += maxatom * sizeof(double *); // firstvalue
int nmypage = comm->nthreads;
for (int i = 0; i < nmypage; i++) {
bytes += ipage_atom[i].size();
bytes += dpage_atom[i].size();
bytes += ipage_neigh[i].size();
bytes += dpage_neigh[i].size();
}
return bytes;
}
/* ----------------------------------------------------------------------
allocate local atom-based arrays
------------------------------------------------------------------------- */
void FixNeighHistory::grow_arrays(int nmax)
{
memory->grow(npartner,nmax,"neighbor_history:npartner");
partner = (tagint **) memory->srealloc(partner,nmax*sizeof(tagint *),
"neighbor_history:partner");
valuepartner = (double **) memory->srealloc(valuepartner,
nmax*sizeof(double *),
"neighbor_history:valuepartner");
}
/* ----------------------------------------------------------------------
copy values within local atom-based arrays
------------------------------------------------------------------------- */
void FixNeighHistory::copy_arrays(int i, int j, int /*delflag*/)
{
// just copy pointers for partner and valuepartner
// b/c can't overwrite chunk allocation inside ipage_atom,dpage_atom
// incoming atoms in unpack_exchange just grab new chunks
// so are orphaning chunks for migrating atoms
// OK, b/c will reset ipage_atom,dpage_atom on next reneighboring
npartner[j] = npartner[i];
partner[j] = partner[i];
valuepartner[j] = valuepartner[i];
}
/* ----------------------------------------------------------------------
initialize one atom's array values, called when atom is created
------------------------------------------------------------------------- */
void FixNeighHistory::set_arrays(int i)
{
npartner[i] = 0;
}
/* ----------------------------------------------------------------------
only called by Comm::reverse_comm_fix_variable for PERPARTNER mode
------------------------------------------------------------------------- */
int FixNeighHistory::pack_reverse_comm_size(int n, int first)
{
int i,last;
int dnump1 = dnum + 1;
int m = 0;
last = first + n;
for (i = first; i < last; i++)
m += 1 + dnump1*npartner[i];
return m;
}
/* ----------------------------------------------------------------------
two modes: NPARTNER and PERPARTNER
------------------------------------------------------------------------- */
int FixNeighHistory::pack_reverse_comm(int n, int first, double *buf)
{
int i,k,last;
int m = 0;
last = first + n;
if (commflag == NPARTNER) {
for (i = first; i < last; i++) {
buf[m++] = npartner[i];
}
} else if (commflag == PERPARTNER) {
for (i = first; i < last; i++) {
buf[m++] = npartner[i];
for (k = 0; k < npartner[i]; k++) {
buf[m++] = partner[i][k];
memcpy(&buf[m],&valuepartner[i][dnum*k],dnumbytes);
m += dnum;
}
}
} else error->all(FLERR,"Unsupported comm mode in neighbor history");
return m;
}
/* ----------------------------------------------------------------------
two modes: NPARTNER and PERPARTNER
------------------------------------------------------------------------- */
void FixNeighHistory::unpack_reverse_comm(int n, int *list, double *buf)
{
int i,j,k,kk,ncount;
int m = 0;
if (commflag == NPARTNER) {
for (i = 0; i < n; i++) {
j = list[i];
npartner[j] += static_cast<int> (buf[m++]);
}
} else if (commflag == PERPARTNER) {
for (i = 0; i < n; i++) {
j = list[i];
ncount = static_cast<int> (buf[m++]);
for (k = 0; k < ncount; k++) {
kk = npartner[j]++;
partner[j][kk] = static_cast<tagint> (buf[m++]);
memcpy(&valuepartner[j][dnum*kk],&buf[m],dnumbytes);
m += dnum;
}
}
} else error->all(FLERR,"Unsupported comm mode in neighbor history");
}
/* ----------------------------------------------------------------------
pack values in local atom-based arrays for exchange with another proc
------------------------------------------------------------------------- */
int FixNeighHistory::pack_exchange(int i, double *buf)
{
int m = 0;
buf[m++] = npartner[i];
for (int n = 0; n < npartner[i]; n++) {
buf[m++] = partner[i][n];
memcpy(&buf[m],&valuepartner[i][dnum*n],dnumbytes);
m += dnum;
}
return m;
}
/* ----------------------------------------------------------------------
unpack values in local atom-based arrays from exchange with another proc
------------------------------------------------------------------------- */
int FixNeighHistory::unpack_exchange(int nlocal, double *buf)
{
// allocate new chunks from ipage_atom,dpage_atom for incoming values
int m = 0;
npartner[nlocal] = static_cast<int> (buf[m++]);
maxpartner = MAX(maxpartner,npartner[nlocal]);
partner[nlocal] = ipage_atom->get(npartner[nlocal]);
valuepartner[nlocal] = dpage_atom->get(dnum*npartner[nlocal]);
for (int n = 0; n < npartner[nlocal]; n++) {
partner[nlocal][n] = static_cast<tagint> (buf[m++]);
memcpy(&valuepartner[nlocal][dnum*n],&buf[m],dnumbytes);
m += dnum;
}
return m;
}
/* ----------------------------------------------------------------------
pack values in local atom-based arrays for restart file
------------------------------------------------------------------------- */
int FixNeighHistory::pack_restart(int i, double *buf)
{
int m = 1;
buf[m++] = npartner[i];
for (int n = 0; n < npartner[i]; n++) {
buf[m++] = partner[i][n];
memcpy(&buf[m],&valuepartner[i][dnum*n],dnumbytes);
m += dnum;
}
buf[0] = m;
return m;
}
/* ----------------------------------------------------------------------
unpack values from atom->extra array to restart the fix
------------------------------------------------------------------------- */
void FixNeighHistory::unpack_restart(int nlocal, int nth)
{
// ipage_atom = NULL if being called from granular pair style init()
if (ipage_atom == NULL) allocate_pages();
// skip to Nth set of extra values
double **extra = atom->extra;
int m = 0;
for (int i = 0; i < nth; i++) m += static_cast<int> (extra[nlocal][m]);
m++;
// allocate new chunks from ipage_atom,dpage_atom for incoming values
npartner[nlocal] = static_cast<int> (extra[nlocal][m++]);
maxpartner = MAX(maxpartner,npartner[nlocal]);
partner[nlocal] = ipage_atom->get(npartner[nlocal]);
valuepartner[nlocal] = dpage_atom->get(dnum*npartner[nlocal]);
for (int n = 0; n < npartner[nlocal]; n++) {
partner[nlocal][n] = static_cast<tagint> (extra[nlocal][m++]);
memcpy(&valuepartner[nlocal][dnum*n],&extra[nlocal][m],dnumbytes);
m += dnum;
}
}
/* ----------------------------------------------------------------------
maxsize of any atom's restart data
------------------------------------------------------------------------- */
int FixNeighHistory::maxsize_restart()
{
// maxpartner_all = max # of touching partners across all procs
int maxpartner_all;
MPI_Allreduce(&maxpartner,&maxpartner_all,1,MPI_INT,MPI_MAX,world);
return (dnum+1)*maxpartner_all + 2;
}
/* ----------------------------------------------------------------------
size of atom nlocal's restart data
------------------------------------------------------------------------- */
int FixNeighHistory::size_restart(int nlocal)
{
return (dnum+1)*npartner[nlocal] + 2;
}