/
maggs.c
2517 lines (2159 loc) · 70.6 KB
/
maggs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the ESPResSo distribution (http://www.espresso.mpg.de).
// It is therefore subject to the ESPResSo license agreement which you accepted upon receiving the distribution
// and by which you are legally bound while utilizing this file in any form or way.
// There is NO WARRANTY, not even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// You should have received a copy of that license along with this program;
// if not, refer to http://www.espresso.mpg.de/license.html where its current version can be found, or
// write to Max-Planck-Institute for Polymer Research, Theory Group, PO Box 3148, 55021 Mainz, Germany.
// Copyright (c) 2002-2006; all rights reserved unless otherwise stated.
/** \file maggs.c
* Local Maggs algorithm for long range coulomb interaction.
*/
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "global.h"
#include "grid.h"
#include "integrate.h"
#include "initialize.h"
#include "interaction_data.h"
#include "particle_data.h"
#include "communication.h"
#include "maggs.h"
#include "thermostat.h"
#include "cells.h"
#include "domain_decomposition.h"
/* MPI tags for the maggs communications: */
/** Tag for communication in Maggs_init() -> calc_glue_patch(). */
#define REQ_MAGGS_SPREAD 300
#define REQ_MAGGS_EQUIL 301
#ifdef ELECTROSTATICS // later to remove!!!!
/************************************************
* data types
************************************************/
/** Structure of local lattice parameters. */
typedef struct {
/* local mesh characterization. */
t_dvector ld_pos; /** spacial position of left down grid point */
t_dvector ur_pos; /** spacial positon of upper right grid point */
t_dvector low_bound; /** low bound for the coord of particles of ghost region */
t_dvector up_bound; /** upper bound for the coord of particles of ghost region */
int globin_ld[3]; /** inner left down global grid point */
int in_ld[3]; /** inner left down grid point */
int in_ur[3]; /** inner up right grid point + (1,1,1) */
int halo_ld[3]; /** halo-region left down grid point */
int halo_ur[3]; /** halo-region up right global grid point */
int margin[SPACE_DIM*2]; /** number of margin mesh points (even index - left, odd - right). */
int r_margin[6]; /** number of margin mesh points from neighbour nodes */
t_ivector dim; /** grid dimension (size + glue_patch region) of local mesh. */
t_ivector size; /** dimension of mesh inside node domain. */
int volume;
int inner_vol;
} lattice_param;
/** surface_patch structure */
typedef struct {
int offset; /** source offset for the site index */
int doffset; /** desitnation offset for the site index */
int stride; /** minimal contiguous block */
int skip; /** gap between two strides (from the first element of one stride
** to the first elem. of next stride
*/
int nblocks; /** number of strides */
int coord[2]; /** coordinates of the vector fields which has to be exchanged */
int volume;
} t_surf_patch;
typedef struct {
double charge;
double psi_v; /* velocity of the scalar Yukawa field */
double psi_f; /* force of the scalar Yukawa field */
short r[SPACE_DIM];
} t_site;
/************************************************
* variables
************************************************/
int IND_ORDER[SPACE_DIM] = {2,1,0}; /* loop order for dimensions (for ESPRESSO z is shortest) */
static double maggs_pref1;
static double maggs_pref2;
MAGGS_struct maggs = { 0.,0.,0.,0.,0, 0.,0., 0., 0.,0., 0, 0.};
/** coefficents for the Coulomb self-energy */
static double alpha[8][8];
/** coefficents for the Yukawa self-energy */
static double beta[8][8];
/** local mesh. */
static lattice_param lparam;
/** local lattice */
static t_site* lattice;
/** local E field */
static double* Efield;
/** local B field */
static double* Bfield;
/** local yukawa scalar field */
static double* psi;
/** site neighbors */
static t_dirs* neighbor;
/** Array to store glue_patch data to send. */
double *send_databuf = NULL;
/** Array to store glue_patch data to recv */
double *recv_databuf = NULL;
/** \name Privat Functions */
/************************************************************/
/*@{*/
/** Calculates charge interpolation for nearest neighbor grid points.
\param first an integer coordinates of the paticle.
\param rel a relative position of the particle inside the grid cell.
\param q charge of the particle.
*/
void interpolate_charge(int *first, double *rel, double q);
void accumulate_charge_from_ghosts();
void interpolate_part_charge(double q, double *rel, double *rho);
void interpolate_charges_from_grad(int index, double q, double *rel, double *grad);
void interpolate_part_charge_from_grad(double rel_x, double *grad, double *rho);
void calc_self_energy_coeffs();
void calc_charge_gradients(double *rel, double q, double *grad);
void calc_charge_currents(double *grad, double *current);
void calc_e_force_on_particle(Particle *p, int index, double *flux);
short check_intersect_1D(double delta, double r_new, int dir, int first, double *t_step, int identity);
void calc_e_field_on_link_1D(int index, double *flux, double v, int dir);
void perform_rot_move(int ix, int iy, int iz);
void print_e_field();
void set_neighbors();
void maggs_friction_thermo();
void check_yukawa_eq();
/*@}*/
MDINLINE int dtoi(double flt)
{
int intgr;
// __asm__ __volatile__(
// "fistpl %0" : "=m" (intgr) : "t" (flt) : "st"
// "fld flt fistp intgr"
// );
return intgr;
}
MDINLINE double LAPLACIAN(int index)
{
int i;
double temp;
double inva2 = SQR(maggs.inva);
temp = -6.* psi[index];
FOR3D(i) temp += psi[neighbor[index][i]] + psi[neighbor[index][OPP_DIR(i)]];
return inva2 * temp;
}
MDINLINE int maggs_get_linear_index(int a, int b, int c, int adim[3])
{
return (c + adim[2]*(b + adim[1]*a));
}
/*
* The first index iz z!!!!
*/
MDINLINE int get_offset(int index_shift, int index_base, int axes, int adim[3])
{
int dif;
dif = index_shift - index_base;
if(axes <= 1) dif *= adim[2];
if(axes == 0) dif *= adim[1];
return (dif);
}
MDINLINE double interpol1D(double x)
{
/***********************************/
/* Interpolation function in one */
/* dimension. The explicit form of */
/* it depends on the preprocessor */
/* directive */
/***********************************/
#ifdef LINEAR_INTERPOLATION
return x;
#endif
#ifdef COS_INTERPOLATION
return sqr(sin(M_PI_2*x));
#endif
}
MDINLINE void calc_directions(int j, int* dir1, int*dir2)
{
*dir1 = *dir2 = -1;
switch(j) {
case 0 :
*dir1 = 2;
*dir2 = 1;
break;
case 1 :
*dir1 = 2;
*dir2 = 0;
break;
case 2 :
*dir1 = 1;
*dir2 = 0;
break;
}
}
MDINLINE double calc_dual_curl(int mue, int nue, int* neighbor, int index)
{
/*****************************************/
/* calculation of the plaquette lying */
/* in mue-nue plane. */
/*****************************************/
double res;
res = Efield[index+mue] + Efield[3*neighbor[mue]+nue] -
Efield[3*neighbor[nue]+mue] - Efield[index+nue];
return res;
}
MDINLINE double calc_curl(int mue, int nue, int* neighbor, int index)
/* calculation of the plaquette lying in mue-nue plane of dual space */
{
double result;
result = Bfield[index+mue] + Bfield[3*neighbor[OPP_DIR(mue)]+nue] -
Bfield[3*neighbor[OPP_DIR(nue)]+mue] - Bfield[index+nue];
return result;
}
void update_plaquette(int mue, int nue, int* neighb, int index, double delta)
{
int i = 3*index;
Efield[i+mue] += delta;
Efield[3*neighb[mue]+nue] += delta;
Efield[3*neighb[nue]+mue] -= delta;
Efield[i+nue] -= delta;
}
double check_curl_E()
{
int i, ix, iy, iz;
double curl, maxcurl, gmaxcurl;
int* anchor_neighb;
maxcurl = 0.;
FORALL_INNER_SITES(ix, iy, iz) {
i = maggs_get_linear_index(ix, iy, iz, lparam.dim);
anchor_neighb = neighbor[i];
curl = Efield[3*i] + Efield[3*anchor_neighb[0]+1]
- Efield[3*anchor_neighb[1]] - Efield[3*i+1];
curl *= maggs.inva;
if(fabs(curl)>maxcurl) maxcurl = fabs(curl);
curl = Efield[3*i+2] + Efield[3*anchor_neighb[2]]
- Efield[3*anchor_neighb[0]+2] - Efield[3*i];
curl *= maggs.inva;
if(fabs(curl)>maxcurl) maxcurl = fabs(curl);
curl = Efield[3*i+1] + Efield[3*anchor_neighb[1]+2]
- Efield[3*anchor_neighb[2]+1] - Efield[3*i+2];
curl *= maggs.inva;
if(fabs(curl)>maxcurl) maxcurl = fabs(curl);
}
MPI_Allreduce(&maxcurl,&gmaxcurl,1,MPI_DOUBLE,MPI_MAX,MPI_COMM_WORLD);
return gmaxcurl;
}
void perform_rot_move_inplane(int i, int n)
{
/* coord n is normal to the plaquette */
int mue, nue;
int * anchor_neighb;
double delta;
double ROUND_ERR = 0.01*ROUND_ERROR_PREC;
mue = 0; nue = 0;
switch(n) {
case 0 :
mue = 1;
nue = 2;
break;
case 1 :
mue = 2;
nue = 0;
break;
case 2 :
mue = 0;
nue = 1;
break;
}
anchor_neighb = &neighbor[i][0];
delta = Efield[3*i+mue] + Efield[3*anchor_neighb[mue]+nue]
- Efield[3*anchor_neighb[nue]+mue] - Efield[3*i+nue];
if(fabs(delta)>=ROUND_ERR) {
delta = -delta/4.;
update_plaquette(mue, nue, anchor_neighb, i, delta);
}
}
void calc_self_energy_coeffs()
{
double factor, prefac;
int px = 0;
int py = 0;
int pz = 0;
int i, j, k, l, m, index;
double sx = 0.;
double sy = 0.;
double sz = 0.;
double sxy = 0.;
double sxyz = 0.;
double nomx, nomy, nomz;
double inva = maggs.inva;
double invasq = inva*inva;
double n[PNN][SPACE_DIM];// = {{0.,0.,0.}, {1.,0.,0.}, {0.,1.,0.}, {1.,1.,0.},
// {0.,0.,1.}, {1.,0.,1.}, {0.,1.,1.}, {1.,1.,1.}};
index = 0;
LOOP_CUBE_VERTICES(k,l,m) {
n[index][IND_ORDER[0]] = m;
n[index][IND_ORDER[1]] = l;
n[index][IND_ORDER[2]] = k;
index++;
}
factor = M_PI / maggs.mesh;
prefac = 1. / maggs.mesh;
prefac = 0.5 * prefac * prefac * prefac * SQR(maggs.prefactor);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
alpha[i][j] = 0.;
if(maggs.yukawa == 1) beta[i][j] = 0.;
for(px = 0; px < maggs.mesh; ++px)
{
sx = sin( factor * px );
sx = sx * sx;
nomx = 2.*factor*px*(n[i][0] - n[j][0]);
for(py = 0; py < maggs.mesh; ++py)
{
sy = sin( factor * py );
sy = sy * sy;
nomy = 2.*factor*py*(n[i][1] - n[j][1]);
sxy = sx + sy;
for(pz = 0; pz < maggs.mesh; ++pz)
{
sz = sin( factor * pz );
sz = sz * sz;
nomz = 2.*factor*pz*(n[i][2] - n[j][2]);
sxyz = sxy + sz;
sxyz *= 4.;
if(sxyz > 0)
{
alpha[i][j] += cos(nomx + nomy + nomz) / sxyz;
}
if(maggs.yukawa == 1)
beta[i][j] += cos(nomx + nomy + nomz) / (sxyz + SQR(maggs.kappa*maggs.a));
}
}
}
/* invasq is needed for the calculation of forces */
alpha[i][j] = invasq * prefac * alpha[i][j];
if(maggs.yukawa == 1)
beta[i][j] = invasq * prefac * beta[i][j];
}
}
// fprintf(stderr,"alpha(0,0)=%f\n",alpha[0][0]);
MAGGS_TRACE(
if(!this_node) {
int flag_sym = 1;
for(i=0;i<8;i++) {
for(j=0;j<8;j++) {
if(alpha[i][j]!=alpha[j][i]) flag_sym = 0;
}
}
if(flag_sym==1) fprintf(stderr, "alpha matrix is symmetric\n");
}
);
}
void calc_part_yukawa_force(double *grad, double *force, int index)
{
int i, j, k, l, m;
int help_index[3];
int temp;
double local_f[SPACE_DIM];
t_site* anchor_site;
anchor_site = &lattice[index];
FOR3D(i) {
temp = neighbor[index][i];
if(temp == NOWHERE) help_index[i] = lparam.volume; /* force huge index */
else /* incr. for x-neighbor */
help_index[i] = get_offset(lattice[neighbor[index][i]].r[i], anchor_site->r[i], i, lparam.dim);
}
i = 0;
FOR3D(k) local_f[k] = 0.;
for(k=0;k<2;k++){ /* jumps from x- to x+ */
for(l=0;l<2;l++){ /* jumps from y- to y+ */
for(m=0;m<2;m++){ /* jumps from z- to z+ */
FOR3D(j)
local_f[j] += grad[i+j]*psi[index];
i+=SPACE_DIM;
index+=help_index[2];
help_index[2]=-help_index[2];
}
index+=help_index[1];
help_index[1]=-help_index[1];
}
index+=help_index[0];
help_index[0]=-help_index[0];
}
FOR3D(j) {
local_f[j] *= +maggs.prefactor * maggs.inva; // sign is important for BD_Yukawa !!!
force[j] += local_f[j];
}
}
void calc_part_self_force(double *grad, double *rho, double *force)
{
int i, j, k;
double self, temp;
FOR3D(k) {
self = 0.;
for(i=0;i<8;i++) {
temp = rho[i]*grad[i*SPACE_DIM + k];
self += alpha[i][i] * temp;
if(maggs.yukawa == 1)
self += beta[i][i] * temp;
for(j=i+1;j<8;j++) {
temp = rho[i]*grad[j*SPACE_DIM + k] + rho[j]*grad[i*SPACE_DIM + k];
self += alpha[i][j] * temp;
if(maggs.yukawa == 1)
self += beta[i][j] * temp;
}
}
force[k] += 2. * self;
}
}
void calc_part_point_forces(Particle *p, double *grad, double *rho, int lat_index)
{
static int init = 1;
/** self-energy coefficients */
// static double alpha[8][8];
static int help_index[SPACE_DIM];
int dir1, dir2, d, grad_ind;
int l, m, index, temp_ind;
double grad2[24];
if(init) {
// calc_self_energy_coeffs(alpha);
calc_self_energy_coeffs();
help_index[0] = 12;
help_index[1] = 6;
help_index[2] = 3;
init = 0;
}
/* calculate self-forces */
/* the first 4 elements are x-components */
/* looping is in the order z, y, x */
/* the shifts are multiplied by SPACE_DIM */
index = 0;
grad_ind = 0;
FOR3D(d) {
calc_directions(d, &dir1, &dir2);
for(l=0;l<2;l++){ /* jumps from dir2- to dir2+ */
for(m=0;m<2;m++){ /* jumps from dir1- to dir1+ */
temp_ind = index + d;
grad2[temp_ind] = grad[grad_ind];
grad2[temp_ind + help_index[d]] = -grad[grad_ind];
grad_ind++;
index+=help_index[dir1];
help_index[dir1]=-help_index[dir1];
}
index+=help_index[dir2];
help_index[dir2]=-help_index[dir2];
}
}
calc_part_self_force(grad2, rho, &p->f.f[0]);
if(maggs.yukawa == 1)
calc_part_yukawa_force(grad2, &p->f.f[0], lat_index);
}
void calc_local_lattice() {
int i;
int ix = 0;
int iy = 0;
int iz = 0;
int kount = 0;
int xyzcube;
xyzcube = 1;
FOR3D(i) {
/* inner left down grid point (global index) */
lparam.in_ld[i] = (int)ceil(my_left[i]*maggs.inva);
/* inner up right grid point (global index) */
lparam.in_ur[i] = (int)floor(my_right[i]*maggs.inva);
/* correct roundof errors at boundary */
if(my_right[i]*maggs.inva-lparam.in_ur[i]<ROUND_ERROR_PREC) lparam.in_ur[i]--;
if(1.0+my_left[i]*maggs.inva-lparam.in_ld[i]<ROUND_ERROR_PREC) lparam.in_ld[i]--;
lparam.globin_ld[i] = lparam.in_ld[i];
/* inner grid dimensions */
lparam.size[i] = lparam.in_ur[i] - lparam.in_ld[i] + 1;
/* spacial position of left down grid point */
lparam.ld_pos[i] = my_left[i] - maggs.a;
/* spacial position of upper right grid point */
lparam.ur_pos[i] = my_right[i] + maggs.a;
/* left down margin */
lparam.margin[i*2] = 1;
/* up right margin */
lparam.margin[(i*2)+1] = 1;
lparam.dim[i] = lparam.size[i] + lparam.margin[i*2] + lparam.margin[i*2+1];
xyzcube *= lparam.dim[i];
/* reduce inner grid indices from global to local */
lparam.in_ld[i] = lparam.margin[i*2];
lparam.in_ur[i] = lparam.margin[i*2]+lparam.size[i];
lparam.halo_ld[i] = 0;
lparam.halo_ur[i] = lparam.in_ur[i];
}
lparam.volume = xyzcube;
lparam.inner_vol = lparam.size[0]*lparam.size[1]*lparam.size[2];
/* allocate memory for sites and neighbors */
lattice = (t_site*) malloc(xyzcube*sizeof(t_site));
neighbor = (t_dirs*) malloc(xyzcube*sizeof(t_dirs));
/** allocate memory for field variables */
Bfield = (double*) malloc(3*xyzcube*sizeof(double));
Efield = (double*) malloc(3*xyzcube*sizeof(double));
if(maggs.yukawa == 1)
psi = (double*) malloc(xyzcube*sizeof(double));
/* set up lattice sites */
FORALL_SITES(ix, iy, iz) {
kount = maggs_get_linear_index(ix, iy, iz, lparam.dim);
lattice[kount].r[0] = ix;
lattice[kount].r[1] = iy;
lattice[kount].r[2] = iz;
FOR3D(i) {
Bfield[3*kount+i] = 0.;
Efield[3*kount+i] = 0.;
}
lattice[kount].charge = 0.;
if(maggs.yukawa) {
psi[kount] = 0.;
lattice[kount].psi_v = 0.;
lattice[kount].psi_f = 0.;
}
}
set_neighbors();
}
void set_neighbors() {
/* set up nearest neighbors for each site */
int ix = 0;
int iy = 0;
int iz = 0;
int xsize = lparam.dim[0];
int ysize = lparam.dim[1];
int zsize = lparam.dim[2];
int ixplus = 0;
int ixminus = 0;
int iyplus = 0;
int iyminus = 0;
int izplus = 0;
int izminus = 0;
int kount = 0;
int kountxplus = 0;
int kountxminus = 0;
int kountyplus = 0;
int kountyminus = 0;
int kountzplus = 0;
int kountzminus = 0;
for (ix = 0; ix < xsize; ix++)
{
ixplus = ix + 1;
ixminus = ix - 1;
for(iy = 0; iy < ysize; iy ++)
{
iyplus = iy + 1;
iyminus = iy - 1;
for(iz = 0; iz < zsize; iz ++)
{
izplus = iz + 1;
izminus = iz - 1;
kount = maggs_get_linear_index(ix, iy, iz, lparam.dim);
kountzplus = maggs_get_linear_index(ix, iy, izplus, lparam.dim);
kountzminus = maggs_get_linear_index(ix, iy, izminus, lparam.dim);
kountyplus = maggs_get_linear_index(ix, iyplus, iz, lparam.dim);
kountyminus = maggs_get_linear_index(ix, iyminus, iz, lparam.dim);
kountxplus = maggs_get_linear_index(ixplus, iy, iz, lparam.dim);
kountxminus = maggs_get_linear_index(ixminus, iy, iz, lparam.dim);
if(ixminus < 0) neighbor[kount][XMINUS] = -1;
else neighbor[kount][XMINUS] = kountxminus;
if(ixplus >= xsize) neighbor[kount][XPLUS] = -1;
else neighbor[kount][XPLUS] = kountxplus;
if(iyminus < 0) neighbor[kount][YMINUS] = -1;
else neighbor[kount][YMINUS] = kountyminus;
if(iyplus >= ysize) neighbor[kount][YPLUS] = -1;
else neighbor[kount][YPLUS] = kountyplus;
if(izminus < 0) neighbor[kount][ZMINUS] = -1;
else neighbor[kount][ZMINUS] = kountzminus;
if(izplus >= zsize) neighbor[kount][ZPLUS] = -1;
else neighbor[kount][ZPLUS] = kountzplus;
}
}
}
return;
}
void calc_surface_patches(t_surf_patch* surface_patch)
{
// int i;
// int maxvol = 1;
/* x=lparam.size[0] plane */
surface_patch[0].offset = lparam.dim[2]*lparam.dim[1]*lparam.size[0]; /*(size[0],0,0) point */
surface_patch[0].doffset = 0; /*(0,0,0) point */
surface_patch[0].stride = lparam.dim[2]*lparam.dim[1];
surface_patch[0].skip = 0;
surface_patch[0].nblocks = 1;
surface_patch[0].coord[0] = 2;
surface_patch[0].coord[1] = 1;
surface_patch[0].volume = lparam.dim[2]*lparam.dim[1];
/* x=1 plane */
surface_patch[1].offset = lparam.dim[2]*lparam.dim[1]; /*(1,0,0) point */
surface_patch[1].doffset = lparam.dim[2]*lparam.dim[1]*lparam.in_ur[0]; /*(halo[0],0,0) point */
surface_patch[1].stride = lparam.dim[2]*lparam.dim[1];
surface_patch[1].skip = 0;
surface_patch[1].nblocks = 1;
surface_patch[1].coord[0] = 2;
surface_patch[1].coord[1] = 1;
surface_patch[1].volume = lparam.dim[2]*lparam.dim[1];
/* y=lparam.size[1] plane */
surface_patch[2].offset = lparam.dim[2]*lparam.size[1]; /*(0,size[1],0) point */
surface_patch[2].doffset = 0; /*(0,0,0) point */
surface_patch[2].stride = lparam.dim[2];
surface_patch[2].skip = lparam.dim[2]*lparam.dim[1];
surface_patch[2].nblocks = lparam.dim[0];
surface_patch[2].coord[0] = 2;
surface_patch[2].coord[1] = 0;
surface_patch[2].volume = lparam.dim[2]*lparam.dim[0];
/* y=1 plane */
surface_patch[3].offset = lparam.dim[2]; /*(0,1,0) point */
surface_patch[3].doffset = lparam.dim[2]*lparam.in_ur[1]; /*(0,in_ur[1],0) point */
surface_patch[3].stride = lparam.dim[2];
surface_patch[3].skip = lparam.dim[2]*lparam.dim[1];
surface_patch[3].nblocks = lparam.dim[0];
surface_patch[3].coord[0] = 2;
surface_patch[3].coord[1] = 0;
surface_patch[3].volume = lparam.dim[2]*lparam.dim[0];
/* z=lparam.size[2] plane */
surface_patch[4].offset = lparam.size[2]; /*(0,0,size[2]) point */
surface_patch[4].doffset = 0; /*(0,0,0) point */
surface_patch[4].stride = 1;
surface_patch[4].skip = lparam.dim[2];
surface_patch[4].nblocks = lparam.dim[0]*lparam.dim[1];
surface_patch[4].coord[0] = 1;
surface_patch[4].coord[1] = 0;
surface_patch[4].volume = lparam.dim[0]*lparam.dim[1];
/* z=1 plane for z it must be higher*/
surface_patch[5].offset = 1; /*(0,0,1) point */
surface_patch[5].doffset = lparam.in_ur[2]; /*(0,0,in_ur[2]) point */
surface_patch[5].stride = 1;
surface_patch[5].skip = lparam.dim[2];
surface_patch[5].nblocks = lparam.dim[0]*lparam.dim[1];
surface_patch[5].coord[0] = 1;
surface_patch[5].coord[1] = 0;
surface_patch[5].volume = lparam.dim[0]*lparam.dim[1];
// for(i=0;i<6;i++) {
// if(maxvol < surface_patch[i].volume) maxvol = surface_patch[i].volume;
// }
// send_databuf = (double *) malloc(maxvol*sizeof(double));
// recv_databuf = (double *) malloc(maxvol*sizeof(double));
}
/***********
int pack_surface_patch(int offset, int skip, int stride, int *coord, int nblocks, int bufsize, double* field)
{
int l;
int position = 0;
for(l=0;l<nblocks;l++) {
MPI_Pack (&field[offset], stride, MPI_DOUBLE, send_databuf, bufsize, &position, MPI_COMM_WORLD);
offset += skip;
}
return position;
}
int unpack_surface_patch(int doffset, int skip, int stride, int *coord, int nblocks, int bufsize, double *field)
{
int l;
int position = 0;
for(l=0;l<nblocks;l++) {
MPI_Unpack (recv_databuf, bufsize, &position, &field[doffset], stride, MPI_DOUBLE, MPI_COMM_WORLD);
doffset += skip;
}
return position;
}
*******************/
void prepare_surface_planes(int dim, MPI_Datatype *xy, MPI_Datatype *xz, MPI_Datatype *yz,
t_surf_patch *surface_patch)
{
MPI_Type_contiguous(dim*surface_patch[0].stride*sizeof(double),MPI_BYTE,yz);
MPI_Type_commit(yz);
MPI_Type_vector(surface_patch[4].nblocks, dim*surface_patch[4].stride,
dim*surface_patch[4].skip, MPI_DOUBLE,xy);
MPI_Type_commit(xy);
MPI_Type_vector(surface_patch[2].nblocks, dim*surface_patch[2].stride,
dim*surface_patch[2].skip, MPI_DOUBLE,xz);
MPI_Type_commit(xz);
}
void exchange_surface_patch(double *field, int dim, int e_equil)
{
static int init = 1;
static int init_yuk = 1;
static int flag_free = 1;
static MPI_Datatype xyPlane,xzPlane,yzPlane;
static MPI_Datatype xzPlane2D, xyPlane2D, yzPlane2D;
// int coord[2];
int l, s_dir, r_dir;
// int pos=0;
MPI_Status status[2];
MPI_Request request[]={MPI_REQUEST_NULL, MPI_REQUEST_NULL};
int offset, doffset, skip, stride, nblocks;
/** surface_patch */
static t_surf_patch surface_patch[6];
if(init) {
MPI_Datatype xz_plaq, oneslice;
calc_surface_patches(surface_patch);
prepare_surface_planes(dim, &xyPlane, &xzPlane, &yzPlane, surface_patch);
MPI_Type_vector(surface_patch[0].stride, 2, 3, MPI_DOUBLE,&yzPlane2D);
MPI_Type_commit(&yzPlane2D);
/* create data type for xz plaquette */
MPI_Type_hvector(2,1*sizeof(double),2*sizeof(double), MPI_BYTE, &xz_plaq);
/* create data type for a 1D section */
MPI_Type_contiguous(surface_patch[2].stride, xz_plaq, &oneslice);
/* create data type for a 2D xz plane */
MPI_Type_hvector(surface_patch[2].nblocks, 1, dim*surface_patch[2].skip*sizeof(double), oneslice, &xzPlane2D);
MPI_Type_commit(&xzPlane2D);
/* create data type for a 2D xy plane */
MPI_Type_vector(surface_patch[4].nblocks, 2, dim*surface_patch[4].skip, MPI_DOUBLE, &xyPlane2D);
MPI_Type_commit(&xyPlane2D);
init = 0;
}
#ifndef MAGGS_DEBUG
if(!e_equil && flag_free) {
MPI_Type_free(&yzPlane);
MPI_Type_free(&xzPlane);
MPI_Type_free(&xyPlane);
flag_free = 0;
}
#endif
if(dim == 1 && init_yuk) {
prepare_surface_planes(dim, &xyPlane, &xzPlane, &yzPlane, surface_patch);
init_yuk = 0;
}
/* direction loop */
for(s_dir=0; s_dir < 6; s_dir++) {
offset = dim * surface_patch[s_dir].offset;
doffset= dim * surface_patch[s_dir].doffset;
// skip = dim * surface_patch[s_dir].skip;
// stride = dim * surface_patch[s_dir].stride;
// nblocks = surface_patch[s_dir].nblocks;
// coord[0] = surface_patch[s_dir].coord[0];
// coord[1] = surface_patch[s_dir].coord[1];
// bufsize = surface_patch[s_dir].volume * sizeof(double);
if(s_dir%2==0) r_dir = s_dir+1;
else r_dir = s_dir-1;
/* pack send halo-plane data */
if(node_neighbors[s_dir] != this_node) {
/* communication */
switch(s_dir) {
case 0 :
case 1 :
if(e_equil || dim == 1) {
MPI_Irecv (&field[doffset],1,yzPlane,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Isend(&field[offset],1,yzPlane,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
}
else {
MPI_Irecv (&field[doffset+1],1,yzPlane2D,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Isend(&field[offset+1],1,yzPlane2D,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
}
MPI_Waitall(2,request,status);
break;
case 2 :
case 3 :
if(e_equil || dim == 1) {
MPI_Irecv (&field[doffset],1,xzPlane,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Isend(&field[offset],1,xzPlane,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
}
else {
MPI_Irecv (&field[doffset],1,xzPlane2D,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Isend(&field[offset],1,xzPlane2D,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
}
MPI_Waitall(2,request,status);
break;
case 4 :
case 5 :
if(e_equil || dim == 1) {
MPI_Irecv (&field[doffset],1,xyPlane,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Isend(&field[offset],1,xyPlane,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
}
else {
MPI_Irecv (&field[doffset],1,xyPlane2D,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Isend(&field[offset],1,xyPlane2D,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
}
MPI_Waitall(2,request,status);
break;
}
/*************
pos = pack_surface_patch(offset, skip, stride, coord, nblocks, bufsize, field);
MPI_Irecv (recv_databuf,pos,MPI_PACKED,node_neighbors[s_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[0]);
MPI_Issend(send_databuf,pos,MPI_PACKED,node_neighbors[r_dir],REQ_MAGGS_SPREAD,MPI_COMM_WORLD,&request[1]);
MPI_Waitall(2,request,status);
unpack_surface_patch(doffset, skip, stride, coord, nblocks, bufsize, field);
*************/
}
else {
/** copy locally */
skip = dim * surface_patch[s_dir].skip;
stride = dim * surface_patch[s_dir].stride * sizeof(double);
nblocks = surface_patch[s_dir].nblocks;
for(l=0; l<nblocks; l++){
memcpy(&(field[doffset]), &(field[offset]), stride);
offset += skip;
doffset += skip;
}
}
}
}
void accumulate_charge_density() {
/******************************************
For each particle finds apropriate cube
AND calculates charge distribution
at each lattice site of that cube.
******************************************/
Cell *cell;
Particle* p;
int i, c, d;
int np;
int first[SPACE_DIM];
double q;
double pos[SPACE_DIM], rel[SPACE_DIM];
#ifdef MAGGS_DEBUG
int ix, iy, iz;
double latticeQ, glatticeQ;
#endif
for(i=0;i<lparam.volume;i++) lattice[i].charge = 0.;
/* === charge assignment === */
/* loop over inner cells */
for (c = 0; c < local_cells.n; c++) {
cell = local_cells.cell[c];
p = cell->part;
np = cell->n;
for(i = 0; i < np; i++) {
if( (q=p[i].p.q) != 0.0 ) {
FOR3D(d) {
pos[d] = (p[i].r.p[d] - lparam.ld_pos[d])* maggs.inva;
first[d] = (int) pos[d];
rel[d] = pos[d] - first[d];
}
interpolate_charge(first, rel, q);
}
}
}
accumulate_charge_from_ghosts();
#ifdef MAGGS_DEBUG
latticeQ = 0.;
FORALL_INNER_SITES(ix, iy, iz) {
i=maggs_get_linear_index(ix, iy, iz, lparam.dim);
latticeQ += lattice[i].charge;
}
MPI_Reduce(&latticeQ,&glatticeQ,1,MPI_DOUBLE,MPI_SUM,0, MPI_COMM_WORLD);
if(!this_node) {
if(fabs(glatticeQ)>ROUND_ERROR_PREC)
fprintf(stderr, "Severe problem: the system is not neutral: Q=%e\n", glatticeQ);
}
#endif
}
void accumulate_charge_from_rho(int *first, double *rho, int index)
{
int i, k, l, m;
int help_index[3];
int temp;
FOR3D(i) {
temp = neighbor[index][i];
if(temp == NOWHERE) help_index[i] = lparam.volume; /* force huge index */
else /* incr. for x-neighbor */
help_index[i] = get_offset(lattice[neighbor[index][i]].r[i], first[i], i, lparam.dim);
}
i = 0;
for(k=0;k<2;k++){ /* jumps from x- to x+ */
for(l=0;l<2;l++){ /* jumps from y- to y+ */
for(m=0;m<2;m++){ /* jumps from z- to z+ */
if(index < lparam.volume) {
lattice[index].charge += rho[i];
}
i++;
index+=help_index[2];
help_index[2]=-help_index[2];
}
index+=help_index[1];
help_index[1]=-help_index[1];
}
index+=help_index[0];
help_index[0]=-help_index[0];
}
}