-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelc.c
1796 lines (1505 loc) · 53.4 KB
/
elc.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-2004; all rights reserved unless otherwise stated.
/** \file elc.c
*
* For more information about ELC, see \ref elc.h "elc.h".
*/
#include <math.h>
#include <mpi.h>
#include "utils.h"
#include "communication.h"
#include "particle_data.h"
#include "interaction_data.h"
#include "cells.h"
#include "elc.h"
#include "mmm-common.h"
#include "pressure.h"
#include "p3m.h"
#include "errorhandling.h"
#if defined(ELP3M) && defined(ELECTROSTATICS)
// #define CHECKPOINTS
// #define LOG_FORCES
/****************************************
* LOCAL DEFINES
****************************************/
/** Largest reasonable cutoff for far formula */
#define MAXIMAL_FAR_CUT 50
/****************************************
* LOCAL VARIABLES
****************************************/
/** \name Inverse box dimensions and derived constants */
/*@{*/
static double ux, ux2, uy, uy2, uz;
/*@}*/
ELC_struct elc_params = { 1e100, 10, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0 };
/****************************************
* LOCAL ARRAYS
****************************************/
/** \name Product decomposition data organization
For the cell blocks
it is assumed that the lower blocks part is in the lower half.
This has to have positive sign, so that has to be first. */
/*@{*/
#define POQESP 0
#define POQECP 1
#define POQESM 2
#define POQECM 3
#define PQESSP 0
#define PQESCP 1
#define PQECSP 2
#define PQECCP 3
#define PQESSM 4
#define PQESCM 5
#define PQECSM 6
#define PQECCM 7
/*@}*/
/** number of local particles, equals the size of \ref elc::partblk. */
static int n_localpart = 0;
/** temporary buffers for product decomposition */
static double *partblk = NULL;
/** collected data from the other cells */
static double gblcblk[8];
/** structure for storing of sin and cos values */
typedef struct {
double s, c;
} SCCache;
/** \name sin/cos caching */
/*@{*/
static SCCache *scxcache = NULL;
static int n_scxcache;
static SCCache *scycache = NULL;
static int n_scycache;
/*@}*/
/****************************************
* LOCAL FUNCTIONS
****************************************/
/** \name sin/cos storage */
/*@{*/
static void prepare_scx_cache();
static void prepare_scy_cache();
/*@}*/
/** \name common code */
/*@{*/
static void distribute(int size);
/*@}*/
/** \name p=0 per frequency code */
/*@{*/
static void setup_P(int p, double omega);
static void add_P_force();
static double P_energy(double omega);
/*@}*/
/** \name q=0 per frequency code */
/*@{*/
static void setup_Q(int q, double omega);
static void add_Q_force();
static double Q_energy(double omega);
/*@}*/
/** \name p,q <> 0 per frequency code */
/*@{*/
static void setup_PQ(int p, int q, double omega);
static void add_PQ_force(int p, int q, double omega);
static double PQ_energy(double omega);
static void add_dipole_force();
static double dipole_energy();
static double z_energy();
static void add_z_force();
/*@}*/
/* COMMON */
/**********/
void ELC_setup_constants()
{
ux = 1/box_l[0];
ux2 = ux*ux;
uy = 1/box_l[1];
uy2 = uy*uy;
uz = 1/box_l[2];
}
/* SC Cache */
/************/
static void prepare_scx_cache()
{
int np, c, i, ic, freq, o;
double pref, arg;
Particle *part;
for (freq = 1; freq <= n_scxcache; freq++) {
pref = C_2PI*ux*freq;
o = (freq-1)*n_localpart;
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
arg = pref*part[i].r.p[0];
scxcache[o + ic].s = sin(arg);
scxcache[o + ic].c = cos(arg);
ic++;
}
}
}
}
static void prepare_scy_cache()
{
int np, c, i, ic, freq, o;
double pref, arg;
Particle *part;
for (freq = 1; freq <= n_scycache; freq++) {
pref = C_2PI*uy*freq;
o = (freq-1)*n_localpart;
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
arg = pref*part[i].r.p[1];
scycache[o + ic].s = sin(arg);
scycache[o + ic].c = cos(arg);
ic++;
}
}
}
}
/*****************************************************************/
/* data distribution */
/*****************************************************************/
MDINLINE void clear_vec(double *pdc, int size)
{
int i;
for (i = 0; i < size; i++)
pdc[i] = 0;
}
MDINLINE void copy_vec(double *pdc_d, double *pdc_s, int size)
{
int i;
for (i = 0; i < size; i++)
pdc_d[i] = pdc_s[i];
}
MDINLINE void add_vec(double *pdc_d, double *pdc_s1, double *pdc_s2, int size)
{
int i;
for (i = 0; i < size; i++)
pdc_d[i] = pdc_s1[i] + pdc_s2[i];
}
MDINLINE void addscale_vec(double *pdc_d, double scale, double *pdc_s1, double *pdc_s2, int size)
{
int i;
for (i = 0; i < size; i++)
pdc_d[i] = scale*pdc_s1[i] + pdc_s2[i];
}
MDINLINE void scale_vec(double scale, double *pdc, int size)
{
int i;
for (i = 0; i < size; i++)
pdc[i] *= scale;
}
MDINLINE double *block(double *p, int index, int size)
{
return &p[index*size];
}
void distribute(int size)
{
double send_buf[8];
copy_vec(send_buf, gblcblk, size);
MPI_Allreduce(send_buf, gblcblk, size, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
}
#ifdef CHECKPOINTS
static void checkpoint(char *text, int p, int q, int e_size)
{
int c, i;
fprintf(stderr, "%d: %s %d %d\n", this_node, text, p, q);
fprintf(stderr, "partblk\n");
for (c = 0; c < n_localpart; c++) {
fprintf(stderr, "%d", c);
for (i = 0; i < e_size; i++)
fprintf(stderr, " %10.3g", block(partblk, c, 2*e_size)[i]);
fprintf(stderr, " m");
for (i = 0; i < e_size; i++)
fprintf(stderr, " %10.3g", block(partblk, c, 2*e_size)[i + e_size]);
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
fprintf(stderr, "gblcblk\n");
for (i = 0; i < e_size; i++)
fprintf(stderr, " %10.3g", gblcblk[i]);
fprintf(stderr, " m");
for (i = 0; i < e_size; i++)
fprintf(stderr, " %10.3g", gblcblk[i + e_size]);
fprintf(stderr, "\n");
}
#else
#define checkpoint(text,p,q,size)
#endif
#ifdef LOG_FORCES
static void clear_log_forces(char *where)
{
int np, c, i, j;
Particle *part;
fprintf(stderr, "%s\n", where);
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
fprintf(stderr, "%d %g %g %g\n", part[i].p.identity,
part[i].f.f[0], part[i].f.f[1], part[i].f.f[2]);
for (j = 0; j < 3; j++)
part[i].f.f[j] = 0;
}
}
}
#else
#define clear_log_forces(w)
#endif
/*****************************************************************/
/* dipole terms */
/*****************************************************************/
static void add_dipole_force()
{
int np, c, i;
Particle *part;
double pref = coulomb.prefactor*4*M_PI*ux*uy*uz;
/* for nonneutral systems, this shift gives the background contribution
(rsp. for this shift, the DM of the background is zero) */
double shift = 0.5*box_l[2];
gblcblk[0] = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q*(part[i].r.p[2] - shift);
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer)
gblcblk[0] +=elc_params.di_mid_bot*part[i].p.q*(-part[i].r.p[2] - shift);
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer))
gblcblk[0] +=elc_params.di_mid_top*part[i].p.q*(2*elc_params.h-part[i].r.p[2] - shift);
}
}
}
gblcblk[0] *= pref;
distribute(1);
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++)
part[i].f.f[2] -= gblcblk[0]*part[i].p.q;
}
if (!elc_params.neutralize) {
/* SUBTRACT the forces of the neutralizing background
looks very close to the code above, but is still different.
*/
gblcblk[0] = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q;
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer)
gblcblk[0] +=elc_params.di_mid_bot*part[i].p.q;
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer))
gblcblk[0] +=elc_params.di_mid_top*part[i].p.q;
}
}
}
gblcblk[0] *= pref;
distribute(1);
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++)
part[i].f.f[2] += gblcblk[0]*part[i].p.q*(part[i].r.p[2] - shift);
}
}
}
static double dipole_energy()
{
int np, c, i;
Particle *part;
double pref = coulomb.prefactor*2*M_PI*ux*uy*uz;
double eng, eng1, eng2, eng3;
/* for nonneutral systems, this shift gives the background contribution
(rsp. for this shift, the DM of the background is zero) */
double shift = 0.5*box_l[2];
double fac_delta_mid_bot = 1, fac_delta_mid_top = 1, fac_delta = 1;
if(elc_params.dielectric_contrast_on) {
fac_delta_mid_bot=elc_params.di_mid_bot/(1-elc_params.di_mid_top*elc_params.di_mid_bot);
fac_delta_mid_top=elc_params.di_mid_top/(1-elc_params.di_mid_top*elc_params.di_mid_bot);
fac_delta=fac_delta_mid_bot*elc_params.di_mid_top;
}
gblcblk[0] = 0;gblcblk[1]=0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q*(part[i].r.p[2] - shift);
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer)
gblcblk[1] +=elc_params.di_mid_bot*part[i].p.q*(-part[i].r.p[2] - shift);
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer))
gblcblk[1] +=elc_params.di_mid_top*part[i].p.q*(2*elc_params.h-part[i].r.p[2] - shift);
}
}
}
distribute(2);
eng = (this_node == 0) ? pref*(SQR(gblcblk[0])+gblcblk[0]*gblcblk[1]) : 0;
if (!elc_params.neutralize) {
/* SUBTRACT the energy of the neutralizing background */
gblcblk[0] = 0; gblcblk[1] = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q;
gblcblk[1] += part[i].p.q*(SQR(part[i].r.p[2]- shift));
}
}
distribute(2);
eng1 = (this_node == 0) ? pref*(-gblcblk[1]*gblcblk[0] - (.25 - .5/3.)*SQR(gblcblk[0]*box_l[2])) : 0;
if(!elc_params.dielectric_contrast_on)
eng += eng1;
else {
gblcblk[0] = 0; gblcblk[1] = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q;
gblcblk[1] += part[i].p.q*(SQR(part[i].r.p[2]-shift));
if(part[i].r.p[2]<elc_params.space_layer) {
gblcblk[0] +=elc_params.di_mid_bot*part[i].p.q;
gblcblk[1] +=elc_params.di_mid_bot*part[i].p.q*(SQR(-part[i].r.p[2]-shift));
}
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) {
gblcblk[0] +=elc_params.di_mid_top*part[i].p.q;
gblcblk[1] +=elc_params.di_mid_top*part[i].p.q*(SQR(2*elc_params.h-part[i].r.p[2]-shift));
}
}
}
distribute(2);
eng2 = (this_node == 0) ? pref*(-gblcblk[1]*gblcblk[0] - (.25 - .5/3.)*SQR(gblcblk[0]*box_l[2])) : 0;
gblcblk[0] = 0; gblcblk[1] = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
if(part[i].r.p[2]<elc_params.space_layer) {
gblcblk[0] +=elc_params.di_mid_bot*part[i].p.q;
gblcblk[1] +=elc_params.di_mid_bot*part[i].p.q*(SQR(-part[i].r.p[2]-shift));
}
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) {
gblcblk[0] +=elc_params.di_mid_top*part[i].p.q;
gblcblk[1] +=elc_params.di_mid_top*part[i].p.q*(SQR(2*elc_params.h-part[i].r.p[2]-shift));
}
}
}
distribute(2);
eng3 = (this_node == 0) ? pref*(-gblcblk[1]*gblcblk[0] - (.25 - .5/3.)*SQR(gblcblk[0]*box_l[2])) : 0;
if (this_node == 0) eng+=(eng1+eng2-eng3)/2.0;
}
}
return eng;
}
/*****************************************************************/
MDINLINE double image_sum_b(double q, double z)
{
double shift = 0.5*box_l[2];
double fac=elc_params.di_mid_top*elc_params.di_mid_bot;
double image_sum=(q/(1.0-fac)*(z-2.0*fac*box_l[2]/(1.0-fac)))-q*shift/(1-fac);
// double image_sum=q*(z-shift);
return image_sum;
}
MDINLINE double image_sum_t(double q, double z)
{
double shift = 0.5*box_l[2];
double fac=elc_params.di_mid_top*elc_params.di_mid_bot;
double image_sum=(q/(1.0-fac)*(z+2.0*fac*box_l[2]/(1.0-fac)))-q*shift/(1-fac);
return image_sum;
}
/*****************************************************************/
static double z_energy()
{
int np, c, i;
Particle *part;
double pref = coulomb.prefactor*2*M_PI*ux*uy;
double eng=0;
/* for nonneutral systems, this shift gives the background contribution
(rsp. for this shift, the DM of the background is zero) */
double shift = 0.5*box_l[2];
double q_m_t=0.0,q_m_b=0.0,q=0.0;
double fac_delta_mid_bot=1,fac_delta_mid_top=1,fac_delta=1;
if(elc_params.dielectric_contrast_on) {
fac_delta_mid_bot=elc_params.di_mid_bot/(1-elc_params.di_mid_top*elc_params.di_mid_bot);
fac_delta_mid_top=elc_params.di_mid_top/(1-elc_params.di_mid_top*elc_params.di_mid_bot);
fac_delta=fac_delta_mid_bot*elc_params.di_mid_top;
q_m_t=elc_params.di_mid_top;
q_m_b=elc_params.di_mid_bot;
q=q_m_t*q_m_b;
}
clear_vec(gblcblk, 4);
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q; //q_i
gblcblk[1] += part[i].p.q*(part[i].r.p[2] - shift); //q_i z_
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer) {
gblcblk[2] += fac_delta*(1+elc_params.di_mid_bot)*part[i].p.q;
gblcblk[3] += part[i].p.q*(image_sum_b(q_m_b*q,-(2*elc_params.h+part[i].r.p[2]))
+image_sum_b(q,-(2*elc_params.h-part[i].r.p[2])));
} else {
gblcblk[2] += fac_delta_mid_bot*(1+elc_params.di_mid_top)*part[i].p.q;
gblcblk[3] += part[i].p.q*(image_sum_b(q_m_b,-part[i].r.p[2])
+image_sum_b(q,-(2*elc_params.h-part[i].r.p[2])));
}
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) {
//note the minus sign here which is required due to |z_i-z_j|
gblcblk[2] -= fac_delta*(1+elc_params.di_mid_top)*part[i].p.q;
gblcblk[3] -= part[i].p.q*(image_sum_t(q_m_t*q,4*elc_params.h-part[i].r.p[2])
+image_sum_t(q,2*elc_params.h+part[i].r.p[2]));
} else {
//note the minus sign here which is required due to |z_i-z_j|
gblcblk[2] -= fac_delta_mid_top*(1+elc_params.di_mid_bot)*part[i].p.q;
gblcblk[3] -= part[i].p.q*(image_sum_t(q_m_t,2*elc_params.h-part[i].r.p[2])
+image_sum_t(q,2*elc_params.h+part[i].r.p[2]));
}
}
}
}
distribute(4);
if (this_node == 0)
eng -= pref*(gblcblk[1]*gblcblk[2]-gblcblk[0]*gblcblk[3]);
return eng;
}
/*****************************************************************/
static void add_z_force()
{
int np, c, i, ic;
Particle *part;
double pref = coulomb.prefactor*2*M_PI*ux*uy;
int size = 2;
double fac_delta_mid_bot=1,fac_delta_mid_top=1,fac_delta=1;
if(elc_params.dielectric_contrast_on) {
double fac_elc=1.0/(1-elc_params.di_mid_top*elc_params.di_mid_bot);
fac_delta_mid_bot=elc_params.di_mid_bot*fac_elc;
fac_delta_mid_top=elc_params.di_mid_top*fac_elc;
fac_delta=fac_delta_mid_bot*elc_params.di_mid_top;
clear_vec(gblcblk, size);
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
gblcblk[0] += part[i].p.q;
if(part[i].r.p[2]<elc_params.space_layer)
gblcblk[1] += fac_delta*(1+elc_params.di_mid_bot)*part[i].p.q;
else
gblcblk[1] += fac_delta_mid_bot*(1+elc_params.di_mid_top)*part[i].p.q;
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) {
//note the minus sign here which is required due to |z_i-z_j|
gblcblk[1] -= fac_delta*(1+elc_params.di_mid_top)*part[i].p.q;
} else {
//note the minus sign here which is required due to |z_i-z_j|
gblcblk[1] -= fac_delta_mid_top*(1+elc_params.di_mid_bot)*part[i].p.q;
}
}
}
gblcblk[0] *= pref;
gblcblk[1] *= pref;
distribute(2);
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
part[i].f.f[2] += gblcblk[1]*part[i].p.q;
}
}
}
}
/*****************************************************************/
/* PoQ exp sum */
/*****************************************************************/
static void setup_P(int p, double omega)
{
int np, c, i, ic, o = (p-1)*n_localpart;
Particle *part;
double pref = -coulomb.prefactor*4*M_PI*ux*uy/(exp(omega*box_l[2]) - 1);
double pref_di = coulomb.prefactor*4*M_PI*ux*uy;
double e;
int size = 4;
double lclimgebot[4],lclimgetop[4],lclimge[4];
double fac_delta_mid_bot=1,fac_delta_mid_top=1,fac_delta=1;
double scale=1;
if(elc_params.dielectric_contrast_on) {
double fac_elc=1.0/(1-elc_params.di_mid_top*elc_params.di_mid_bot*exp(-omega*2*elc_params.h));
fac_delta_mid_bot=elc_params.di_mid_bot*fac_elc;
fac_delta_mid_top=elc_params.di_mid_top*fac_elc;
fac_delta=fac_delta_mid_bot*elc_params.di_mid_top;
}
clear_vec(lclimge, size);
clear_vec(gblcblk, size);
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
e = exp(omega*part[i].r.p[2]);
partblk[size*ic + POQESM] = part[i].p.q*scxcache[o + ic].s/e;
partblk[size*ic + POQESP] = part[i].p.q*scxcache[o + ic].s*e;
partblk[size*ic + POQECM] = part[i].p.q*scxcache[o + ic].c/e;
partblk[size*ic + POQECP] = part[i].p.q*scxcache[o + ic].c*e;
add_vec(gblcblk, gblcblk, block(partblk, ic, size), size);
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer) { //handle the lower case first
//negative sign is okay here as the image is located at -part[i].r.p[2]
e= exp(-omega*part[i].r.p[2]);
scale = part[i].p.q*elc_params.di_mid_bot;
lclimgebot[POQESM]=scxcache[o + ic].s/e;
lclimgebot[POQESP]=scxcache[o + ic].s*e;
lclimgebot[POQECM]=scxcache[o + ic].c/e;
lclimgebot[POQECP]=scxcache[o + ic].c*e;
addscale_vec(gblcblk, scale, lclimgebot, gblcblk, size);
e = ( exp(omega*(-part[i].r.p[2] - 2*elc_params.h ))*elc_params.di_mid_bot +
exp(omega*( part[i].r.p[2] - 2*elc_params.h )) )*fac_delta;
} else {
e =( exp(omega*(-part[i].r.p[2] )) +
exp(omega*( part[i].r.p[2] - 2*elc_params.h ))*elc_params.di_mid_top )*fac_delta_mid_bot;
}
lclimge[POQESP]+= part[i].p.q*scxcache[o + ic].s*e;
lclimge[POQECP]+= part[i].p.q*scxcache[o + ic].c*e;
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) { //handle the upper case now
e=exp(omega*(2*elc_params.h-part[i].r.p[2]));
scale =part[i].p.q*elc_params.di_mid_top;
lclimgetop[POQESM]=scxcache[o + ic].s/e;
lclimgetop[POQESP]=scxcache[o + ic].s*e;
lclimgetop[POQECM]=scxcache[o + ic].c/e;
lclimgetop[POQECP]=scxcache[o + ic].c*e;
addscale_vec(gblcblk, scale, lclimgetop, gblcblk, size);
e = ( exp(omega*( part[i].r.p[2] -4*elc_params.h ))*elc_params.di_mid_top +
exp(omega*(-part[i].r.p[2] -2*elc_params.h )) )*fac_delta;
} else {
e = ( exp(omega*( +part[i].r.p[2] -2*elc_params.h )) +
exp(omega*( -part[i].r.p[2] -2*elc_params.h ))*elc_params.di_mid_bot )*fac_delta_mid_top;
}
lclimge[POQESM]+= part[i].p.q*scxcache[o + ic].s*e;
lclimge[POQECM]+= part[i].p.q*scxcache[o + ic].c*e;
}
ic++;
}
}
scale_vec(pref, gblcblk, size);
if(elc_params.dielectric_contrast_on) {
scale_vec(pref_di, lclimge, size);
add_vec(gblcblk, gblcblk, lclimge, size);
}
}
static void setup_Q(int q, double omega)
{
int np, c, i, ic, o = (q-1)*n_localpart;
Particle *part;
double pref = -coulomb.prefactor*4*M_PI*ux*uy/(exp(omega*box_l[2]) - 1);
double pref_di = coulomb.prefactor*4*M_PI*ux*uy;
double e;
int size = 4;
double lclimgebot[4],lclimgetop[4],lclimge[4];
double fac_delta_mid_bot=1,fac_delta_mid_top=1,fac_delta=1;
double scale=1;
if(elc_params.dielectric_contrast_on) {
double fac_elc=1.0/(1-elc_params.di_mid_top*elc_params.di_mid_bot*exp(-omega*2*elc_params.h));
fac_delta_mid_bot=elc_params.di_mid_bot*fac_elc;
fac_delta_mid_top=elc_params.di_mid_top*fac_elc;
fac_delta=fac_delta_mid_bot*elc_params.di_mid_top;
}
clear_vec(lclimge, size);
clear_vec(gblcblk, size);
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
e = exp(omega*part[i].r.p[2]);
partblk[size*ic + POQESM] = part[i].p.q*scycache[o + ic].s/e;
partblk[size*ic + POQESP] = part[i].p.q*scycache[o + ic].s*e;
partblk[size*ic + POQECM] = part[i].p.q*scycache[o + ic].c/e;
partblk[size*ic + POQECP] = part[i].p.q*scycache[o + ic].c*e;
add_vec(gblcblk, gblcblk, block(partblk, ic, size), size);
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer) { //handle the lower case first
//negative sign before omega is okay here as the image is located at -part[i].r.p[2]
e= exp(-omega*part[i].r.p[2]);
scale = part[i].p.q*elc_params.di_mid_bot;
lclimgebot[POQESM]=scycache[o + ic].s/e;
lclimgebot[POQESP]=scycache[o + ic].s*e;
lclimgebot[POQECM]=scycache[o + ic].c/e;
lclimgebot[POQECP]=scycache[o + ic].c*e;
addscale_vec(gblcblk, scale, lclimgebot, gblcblk, size);
e = ( exp(omega*(-part[i].r.p[2] - 2*elc_params.h ))*elc_params.di_mid_bot +
exp(omega*( part[i].r.p[2] - 2*elc_params.h )) )*fac_delta;
} else {
e= ( exp(omega*(-part[i].r.p[2] )) +
exp(omega*( part[i].r.p[2] - 2*elc_params.h ))*elc_params.di_mid_top )*fac_delta_mid_bot;
}
lclimge[POQESP]+= part[i].p.q*scycache[o + ic].s*e;
lclimge[POQECP]+= part[i].p.q*scycache[o + ic].c*e;
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) { //handle the upper case now
e=exp(omega*(2*elc_params.h-part[i].r.p[2]));
scale = part[i].p.q*elc_params.di_mid_top;
lclimgetop[POQESM]=scycache[o + ic].s/e;
lclimgetop[POQESP]=scycache[o + ic].s*e;
lclimgetop[POQECM]=scycache[o + ic].c/e;
lclimgetop[POQECP]=scycache[o + ic].c*e;
addscale_vec(gblcblk, scale, lclimgetop, gblcblk, size);
e = ( exp(omega*( part[i].r.p[2] -4*elc_params.h ))*elc_params.di_mid_top +
exp(omega*(-part[i].r.p[2] -2*elc_params.h )) )*fac_delta;
} else {
e = ( exp(omega*( part[i].r.p[2] - 2*elc_params.h )) +
exp(omega*(-part[i].r.p[2] -2*elc_params.h ))*elc_params.di_mid_bot )*fac_delta_mid_top;
}
lclimge[POQESM]+= part[i].p.q*scycache[o + ic].s*e;
lclimge[POQECM]+= part[i].p.q*scycache[o + ic].c*e;
}
ic++;
}
}
scale_vec(pref, gblcblk, size);
if(elc_params.dielectric_contrast_on) {
scale_vec(pref_di, lclimge, size);
add_vec(gblcblk, gblcblk, lclimge, size);
}
}
static void add_P_force()
{
int np, c, i, ic;
Particle *part;
int size = 4;
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
part[i].f.f[0] +=
partblk[size*ic + POQESM]*gblcblk[POQECP] - partblk[size*ic + POQECM]*gblcblk[POQESP] +
partblk[size*ic + POQESP]*gblcblk[POQECM] - partblk[size*ic + POQECP]*gblcblk[POQESM];
part[i].f.f[2] +=
partblk[size*ic + POQECM]*gblcblk[POQECP] + partblk[size*ic + POQESM]*gblcblk[POQESP] -
partblk[size*ic + POQECP]*gblcblk[POQECM] - partblk[size*ic + POQESP]*gblcblk[POQESM];
ic++;
}
}
}
static double P_energy(double omega)
{
int np, c, i, ic;
Particle *part;
int size = 4;
double eng = 0;
double pref = 1/omega;
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
eng += pref*(partblk[size*ic + POQECM]*gblcblk[POQECP] + partblk[size*ic + POQESM]*gblcblk[POQESP] +
partblk[size*ic + POQECP]*gblcblk[POQECM] + partblk[size*ic + POQESP]*gblcblk[POQESM]);
ic++;
}
}
return eng;
}
static void add_Q_force()
{
int np, c, i, ic;
Particle *part;
int size = 4;
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
part[i].f.f[1] +=
partblk[size*ic + POQESM]*gblcblk[POQECP] - partblk[size*ic + POQECM]*gblcblk[POQESP] +
partblk[size*ic + POQESP]*gblcblk[POQECM] - partblk[size*ic + POQECP]*gblcblk[POQESM];
part[i].f.f[2] +=
partblk[size*ic + POQECM]*gblcblk[POQECP] + partblk[size*ic + POQESM]*gblcblk[POQESP] -
partblk[size*ic + POQECP]*gblcblk[POQECM] - partblk[size*ic + POQESP]*gblcblk[POQESM];
ic++;
}
}
}
static double Q_energy(double omega)
{
int np, c, i, ic;
Particle *part;
int size = 4;
double eng = 0;
double pref = 1/omega;
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
eng += pref*(partblk[size*ic + POQECM]*gblcblk[POQECP] + partblk[size*ic + POQESM]*gblcblk[POQESP] +
partblk[size*ic + POQECP]*gblcblk[POQECM] + partblk[size*ic + POQESP]*gblcblk[POQESM]);
ic++;
}
}
return eng;
}
/*****************************************************************/
/* PQ particle blocks */
/*****************************************************************/
static void setup_PQ(int p, int q, double omega)
{
int np, c, i, ic, ox = (p - 1)*n_localpart, oy = (q - 1)*n_localpart;
Particle *part;
double pref = -coulomb.prefactor*8*M_PI*ux*uy/(exp(omega*box_l[2]) - 1);
double pref_di = coulomb.prefactor*8*M_PI*ux*uy;
double e;
int size = 8;
double lclimgebot[8],lclimgetop[8],lclimge[8];
double fac_delta_mid_bot=1,fac_delta_mid_top=1,fac_delta=1;
double scale=1;
if(elc_params.dielectric_contrast_on) {
double fac_elc=1.0/(1-elc_params.di_mid_top*elc_params.di_mid_bot*exp(-omega*2*elc_params.h));
fac_delta_mid_bot=elc_params.di_mid_bot*fac_elc;
fac_delta_mid_top=elc_params.di_mid_top*fac_elc;
fac_delta=fac_delta_mid_bot*elc_params.di_mid_top;
}
clear_vec(lclimge, size);
clear_vec(gblcblk, size);
ic = 0;
for (c = 0; c < local_cells.n; c++) {
np = local_cells.cell[c]->n;
part = local_cells.cell[c]->part;
for (i = 0; i < np; i++) {
e = exp(omega*part[i].r.p[2]);
partblk[size*ic + PQESSM] = scxcache[ox + ic].s*scycache[oy + ic].s*part[i].p.q/e;
partblk[size*ic + PQESCM] = scxcache[ox + ic].s*scycache[oy + ic].c*part[i].p.q/e;
partblk[size*ic + PQECSM] = scxcache[ox + ic].c*scycache[oy + ic].s*part[i].p.q/e;
partblk[size*ic + PQECCM] = scxcache[ox + ic].c*scycache[oy + ic].c*part[i].p.q/e;
partblk[size*ic + PQESSP] = scxcache[ox + ic].s*scycache[oy + ic].s*part[i].p.q*e;
partblk[size*ic + PQESCP] = scxcache[ox + ic].s*scycache[oy + ic].c*part[i].p.q*e;
partblk[size*ic + PQECSP] = scxcache[ox + ic].c*scycache[oy + ic].s*part[i].p.q*e;
partblk[size*ic + PQECCP] = scxcache[ox + ic].c*scycache[oy + ic].c*part[i].p.q*e;
add_vec(gblcblk, gblcblk, block(partblk, ic, size), size);
if(elc_params.dielectric_contrast_on) {
if(part[i].r.p[2]<elc_params.space_layer) { //handle the lower case first
//change e to take into account the z position of the images
e= exp(-omega*part[i].r.p[2]);
scale = part[i].p.q*elc_params.di_mid_bot;
lclimgebot[PQESSM] = scxcache[ox + ic].s*scycache[oy + ic].s/e;
lclimgebot[PQESCM] = scxcache[ox + ic].s*scycache[oy + ic].c/e;
lclimgebot[PQECSM] = scxcache[ox + ic].c*scycache[oy + ic].s/e;
lclimgebot[PQECCM] = scxcache[ox + ic].c*scycache[oy + ic].c/e;
lclimgebot[PQESSP] = scxcache[ox + ic].s*scycache[oy + ic].s*e;
lclimgebot[PQESCP] = scxcache[ox + ic].s*scycache[oy + ic].c*e;
lclimgebot[PQECSP] = scxcache[ox + ic].c*scycache[oy + ic].s*e;
lclimgebot[PQECCP] = scxcache[ox + ic].c*scycache[oy + ic].c*e;
addscale_vec(gblcblk, scale, lclimgebot, gblcblk, size);
e = ( exp(omega*(-part[i].r.p[2] - 2*elc_params.h ))*elc_params.di_mid_bot +
exp(omega*( part[i].r.p[2] - 2*elc_params.h )) )*fac_delta*part[i].p.q;
} else {
e = ( exp(omega*(-part[i].r.p[2] )) +
exp(omega*( part[i].r.p[2] - 2*elc_params.h ))*elc_params.di_mid_top )*fac_delta_mid_bot*part[i].p.q;
}
lclimge[PQESSP]+= scxcache[ox + ic].s*scycache[oy + ic].s*e;
lclimge[PQESCP]+= scxcache[ox + ic].s*scycache[oy + ic].c*e;
lclimge[PQECSP]+= scxcache[ox + ic].c*scycache[oy + ic].s*e;
lclimge[PQECCP]+= scxcache[ox + ic].c*scycache[oy + ic].c*e;
if(part[i].r.p[2]>(elc_params.h-elc_params.space_layer)) { //handle the upper case now
e=exp(omega*(2*elc_params.h-part[i].r.p[2]));
scale = part[i].p.q*elc_params.di_mid_top;
lclimgetop[PQESSM] = scxcache[ox + ic].s*scycache[oy + ic].s/e;
lclimgetop[PQESCM] = scxcache[ox + ic].s*scycache[oy + ic].c/e;
lclimgetop[PQECSM] = scxcache[ox + ic].c*scycache[oy + ic].s/e;
lclimgetop[PQECCM] = scxcache[ox + ic].c*scycache[oy + ic].c/e;
lclimgetop[PQESSP] = scxcache[ox + ic].s*scycache[oy + ic].s*e;
lclimgetop[PQESCP] = scxcache[ox + ic].s*scycache[oy + ic].c*e;
lclimgetop[PQECSP] = scxcache[ox + ic].c*scycache[oy + ic].s*e;
lclimgetop[PQECCP] = scxcache[ox + ic].c*scycache[oy + ic].c*e;
addscale_vec(gblcblk,scale, lclimgetop, gblcblk, size);
e = ( exp(omega*( part[i].r.p[2] -4*elc_params.h ))*elc_params.di_mid_top +
exp(omega*(-part[i].r.p[2] -2*elc_params.h )) )*fac_delta*part[i].p.q;
} else {
e = ( exp(omega*( part[i].r.p[2] -2*elc_params.h )) +
exp(omega*(-part[i].r.p[2] -2*elc_params.h ))*elc_params.di_mid_bot )*fac_delta_mid_top*part[i].p.q;
}