forked from SPARC-X/SPARC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcyclix_stress.c
More file actions
1232 lines (1043 loc) · 50 KB
/
Copy pathcyclix_stress.c
File metadata and controls
1232 lines (1043 loc) · 50 KB
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
/**
* @file stress.c
* @brief This file contains the functions for calculating stress in cyclix symmetry.
*
* @author abhiraj sharma <asharma424@gatech.edu>
Phanish Suryanarayana <phanish.suryanarayana@ce.gatech.edu>
*
* Copyright (c) 2017 Material Physics & Mechanics Group at Georgia Tech.
*/
#include <complex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include <assert.h>
/* BLAS and LAPACK routines */
#ifdef USE_MKL
#include <mkl.h>
#else
#include <cblas.h>
#include <lapacke.h>
#endif
#include "cyclix_stress.h"
#include "cyclix_tools.h"
#include "gradVecRoutines.h"
#include "gradVecRoutinesKpt.h"
#include "lapVecRoutines.h"
#include "tools.h"
#include "isddft.h"
#include "initialization.h"
#include "electrostatics.h"
#include "stress.h"
#include "cyclix_forces.h"
#include "forces.h"
#include "exchangeCorrelation.h"
#define TEMP_TOL 1e-12
/*
@ brief: function to calculate ionic stress/pressure and add to the electronic stress/pressure
*/
void Calculate_ionic_stress_cyclix(SPARC_OBJ *pSPARC){
double stress_i = 0.0, avgvel = 0.0, mass_tot = 0.0;
int ityp, atm, count;
count = 0;
for(ityp = 0; ityp < pSPARC->Ntypes; ityp++){
for(atm = 0; atm < pSPARC->nAtomv[ityp]; atm++){
avgvel += pSPARC->Mass[ityp] * pSPARC->ion_vel[count * 3 + 2];
mass_tot += pSPARC->Mass[ityp];
count ++;
}
}
avgvel /= mass_tot;
count = 0;
for(ityp = 0; ityp < pSPARC->Ntypes; ityp++){
for(atm = 0; atm < pSPARC->nAtomv[ityp]; atm++){
stress_i -= pSPARC->Mass[ityp] * pow((pSPARC->ion_vel[count * 3 + 2] - avgvel), 2.0);
count++;
}
}
// Determine ionic stress and pressure
double cell_measure = pSPARC->range_z;
pSPARC->stress_i[5] = (2*M_PI/pSPARC->range_y) * stress_i/cell_measure;
pSPARC->stress[5] += pSPARC->stress_i[5];
}
/*
@ brief: function to calculate electronic stress
*/
void Calculate_electronic_stress_cyclix(SPARC_OBJ *pSPARC) {
int rank;
double t1, t2;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// find exchange-correlation components of stress
t1 = MPI_Wtime();
Calculate_XC_stress_cyclix(pSPARC);
t2 = MPI_Wtime();
#ifdef DEBUG
if(!rank) printf("Time for calculating exchange-correlation stress components: %.3f ms\n", (t2 - t1)*1e3);
#endif
// find local stress components
t1 = MPI_Wtime();
Calculate_local_stress_cyclix(pSPARC);
t2 = MPI_Wtime();
#ifdef DEBUG
if(!rank) printf("Time for calculating local stress components: %.3f ms\n", (t2 - t1)*1e3);
#endif
// find nonlocal + kinetic stress components
t1 = MPI_Wtime();
if(pSPARC->isGammaPoint) {
Calculate_nonlocal_kinetic_stress_cyclix(pSPARC);
} else {
Calculate_nonlocal_kinetic_stress_kpt_cyclix(pSPARC);
}
t2 = MPI_Wtime();
#ifdef DEBUG
if(!rank) printf("Time for calculating nonlocal+kinetic stress components: %.3f ms\n", (t2 - t1)*1e3);
#endif
// find stress
if(!rank){
pSPARC->stress[5] = pSPARC->stress_k[5] + pSPARC->stress_xc[5] + pSPARC->stress_nl[5] + pSPARC->stress_el[5];
}
#ifdef DEBUG
if (!rank) {
printf("\nElectronic contribution to stress");
PrintStress(pSPARC, pSPARC->stress, NULL);
}
#endif
}
/*
* @brief: find stress contributions from exchange-correlation
* due to non-linear core correction (NLCC).
*/
void Calculate_XC_stress_nlcc_cyclix(SPARC_OBJ *pSPARC, double *stress_xc_nlcc) {
if (pSPARC->dmcomm_phi == MPI_COMM_NULL) return;
int rank;
MPI_Comm_rank(pSPARC->dmcomm_phi, &rank);
int rank_comm_world;
MPI_Comm_rank(MPI_COMM_WORLD, &rank_comm_world);
#ifdef DEBUG
if (!rank_comm_world)
printf("Start calculating NLCC exchange-correlation components of stress ...\n");
#endif
int ityp, iat, i, j, k, p, ip, jp, kp, di, dj, dk, i_DM, j_DM, k_DM, FDn, count, count_interp,
nx, ny, nz, nxp, nyp, nzp, nd_ex, nx2p, ny2p, nz2p, nd_2ex,
icor, jcor, kcor, *pshifty, *pshiftz, *pshifty_ex, *pshiftz_ex, *ind_interp;
double x0_i, y0_i, z0_i, x, y, z, *R, *R_interp;
double rchrg;
double x3_R3;
double t1, t2, t_sort = 0.0;
FDn = pSPARC->order / 2;
int DMnx = pSPARC->Nx_d;
int DMny = pSPARC->Ny_d;
// Create indices for laplacian
pshifty = (int *)malloc( (FDn+1) * sizeof(int));
pshiftz = (int *)malloc( (FDn+1) * sizeof(int));
pshifty_ex = (int *)malloc( (FDn+1) * sizeof(int));
pshiftz_ex = (int *)malloc( (FDn+1) * sizeof(int));
if (pshifty == NULL || pshiftz == NULL ||
pshifty_ex == NULL || pshiftz_ex == NULL) {
printf("\nMemory allocation failed in local forces!\n");
exit(EXIT_FAILURE);
}
for (ityp = 0; ityp < pSPARC->Ntypes; ityp++) {
rchrg = pSPARC->psd[ityp].RadialGrid[pSPARC->psd[ityp].size-1];
for (iat = 0; iat < pSPARC->Atom_Influence_local[ityp].n_atom; iat++) {
// coordinates of the image atom
x0_i = pSPARC->Atom_Influence_local[ityp].coords[iat * 3];
y0_i = pSPARC->Atom_Influence_local[ityp].coords[iat * 3 + 1];
z0_i = pSPARC->Atom_Influence_local[ityp].coords[iat * 3 + 2];
// original atom index this image atom corresponds to
//int atom_index = pSPARC->Atom_Influence_local[ityp].atom_index[iat];
// number of finite-difference nodes in each direction of overlap rb region
nx = pSPARC->Atom_Influence_local[ityp].xe[iat] - pSPARC->Atom_Influence_local[ityp].xs[iat] + 1;
ny = pSPARC->Atom_Influence_local[ityp].ye[iat] - pSPARC->Atom_Influence_local[ityp].ys[iat] + 1;
nz = pSPARC->Atom_Influence_local[ityp].ze[iat] - pSPARC->Atom_Influence_local[ityp].zs[iat] + 1;
// number of finite-difference nodes in each direction of extended_rb (rb + order/2) region
nxp = nx + pSPARC->order;
nyp = ny + pSPARC->order;
nzp = nz + pSPARC->order;
nd_ex = nxp * nyp * nzp; // total number of nodes
// number of finite-difference nodes in each direction of extended_extended_rb (rb + order) region
nx2p = nxp + pSPARC->order;
ny2p = nyp + pSPARC->order;
nz2p = nzp + pSPARC->order;
nd_2ex = nx2p * ny2p * nz2p; // total number of nodes
// radii^2 of the finite difference grids of the extended_extended_rb region
R = (double *)malloc(sizeof(double) * nd_2ex);
assert(R != NULL);
// left corner of the 2FDn-extended-rb-region
icor = pSPARC->Atom_Influence_local[ityp].xs[iat] - pSPARC->order;
jcor = pSPARC->Atom_Influence_local[ityp].ys[iat] - pSPARC->order;
kcor = pSPARC->Atom_Influence_local[ityp].zs[iat] - pSPARC->order;
// find distance between atom and finite-difference grids
count = 0; count_interp = 0;
for (k = kcor; k < kcor+nz2p; k++) {
z = k * pSPARC->delta_z;
for (j = jcor; j < jcor+ny2p; j++) {
y = j * pSPARC->delta_y;
for (i = icor; i < icor+nx2p; i++) {
x = pSPARC->xin + i * pSPARC->delta_x;
CalculateDistance(pSPARC, x, y, z, x0_i, y0_i, z0_i, &R[count]);
if (R[count] <= rchrg) count_interp++;
count++;
}
}
}
// VJ = (double *)malloc( nd_2ex * sizeof(double) );
double *rhocJ = (double *)calloc( nd_2ex,sizeof(double) );
assert(rhocJ != NULL);
// avoid interpolating positions larger than rchrg
R_interp = (double *)malloc( count_interp * sizeof(double) );
ind_interp = (int *)malloc( count_interp * sizeof(int) );
double *rhocJ_interp = (double *)calloc(count_interp, sizeof(double));
count = 0;
for (i = 0; i < nd_2ex; i++) {
if (R[i] <= rchrg) {
ind_interp[count] = i; // store index
R_interp[count] = R[i]; // store radius value
count++;
}
}
t1 = MPI_Wtime();
SplineInterpMain(pSPARC->psd[ityp].RadialGrid,pSPARC->psd[ityp].rho_c_table, pSPARC->psd[ityp].size,
R_interp, rhocJ_interp, count_interp, pSPARC->psd[ityp].SplineRhocD,pSPARC->psd[ityp].is_r_uniform);
t2 = MPI_Wtime();
t_sort += t2 - t1;
for (i = 0; i < count_interp; i++) {
rhocJ[ind_interp[i]] = rhocJ_interp[i];
}
free(rhocJ_interp); rhocJ_interp = NULL;
free(R_interp); R_interp = NULL;
free(ind_interp); ind_interp = NULL;
free(R); R = NULL;
// shift vectors initialized
pshifty[0] = pshiftz[0] = pshifty_ex[0] = pshiftz_ex[0] = 0;
for (p = 1; p <= FDn; p++) {
pshifty[p] = p * nxp;
pshiftz[p] = pshifty[p] * nyp;
pshifty_ex[p] = p * nx2p;
pshiftz_ex[p] = pshifty_ex[p] *ny2p;
}
//double xin = pSPARC->xin + pSPARC->Atom_Influence_local[ityp].xs[iat] * pSPARC->delta_x;
// calculate gradient of bJ, bJ_ref, VJ, VJ_ref in the rb-domain
dk = pSPARC->Atom_Influence_local[ityp].zs[iat] - pSPARC->DMVertices[4];
dj = pSPARC->Atom_Influence_local[ityp].ys[iat] - pSPARC->DMVertices[2];
di = pSPARC->Atom_Influence_local[ityp].xs[iat] - pSPARC->DMVertices[0];
// calculate drhocJ, 3 components
double *drhocJ_z = malloc(nd_ex * sizeof(double));
assert(drhocJ_z != NULL);
for (int k2p = FDn, kp = 0; k2p < nz2p-FDn; k2p++,kp++) {
int kshift_2p = k2p * nx2p * ny2p;
int kshift_p = kp * nxp * nyp;
for (int j2p = FDn, jp = 0; j2p < ny2p-FDn; j2p++,jp++) {
int jshift_2p = kshift_2p + j2p * nx2p;
int jshift_p = kshift_p + jp * nxp;
for (int i2p = FDn, ip = 0; i2p < nx2p-FDn; i2p++,ip++) {
int ishift_2p = jshift_2p + i2p;
int ishift_p = jshift_p + ip;
double drhocJ_z_val = 0.0;
Dpseudopot_cyclix_z(pSPARC, rhocJ, FDn, ishift_2p, pshifty_ex, pshiftz_ex, &drhocJ_z_val);
drhocJ_z[ishift_p] = drhocJ_z_val;
}
}
}
// // find int Vxc(x) * drhocJ(x) dx
double *Vxc = pSPARC->XCPotential;
for (k = 0, kp = FDn, k_DM = dk; k < nz; k++, kp++, k_DM++) {
int kshift_DM = k_DM * DMnx * DMny;
int kshift_p = kp * nxp * nyp;
//int kshift = k * nx * ny;
for (j = 0, jp = FDn, j_DM = dj; j < ny; j++, jp++, j_DM++) {
int jshift_DM = kshift_DM + j_DM * DMnx;
int jshift_p = kshift_p + jp * nxp;
//int jshift = kshift + j * nx;
for (i = 0, ip = FDn, i_DM = di; i < nx; i++, ip++, i_DM++) {
int ishift_DM = jshift_DM + i_DM;
int ishift_p = jshift_p + ip;
//int ishift = jshift + i;
x3_R3 = (k_DM + pSPARC->DMVertices[4]) * pSPARC->delta_z - z0_i;
double drhocJ_z_val = drhocJ_z[ishift_p];
double Vxc_val;
if (pSPARC->spin_typ == 0)
Vxc_val = Vxc[ishift_DM];
else
Vxc_val = 0.5 * (Vxc[ishift_DM] + Vxc[pSPARC->Nd_d+ishift_DM]);
*stress_xc_nlcc += drhocJ_z_val * x3_R3 * Vxc_val * pSPARC->Intgwt_phi[ishift_DM];
}
}
}
free(rhocJ);
free(drhocJ_z);
}
}
t1 = MPI_Wtime();
// sum over all domains
MPI_Allreduce(MPI_IN_PLACE, stress_xc_nlcc, 1, MPI_DOUBLE, MPI_SUM, pSPARC->dmcomm_phi);
t2 = MPI_Wtime();
if (!rank) {
// Define measure of unit cell
double cell_measure = pSPARC->range_z;
*stress_xc_nlcc /= cell_measure;
*stress_xc_nlcc *= 2.0 * M_PI/pSPARC->range_y;
}
#ifdef DEBUG
if (!rank_comm_world) {
printf("time for sorting and interpolate pseudopotential: %.3f ms, time for Allreduce/Reduce: %.3f ms \n", t_sort*1e3, (t2-t1)*1e3);
printf("NLCC XC contribution to stress: %f", *stress_xc_nlcc);
}
#endif
free(pshifty);
free(pshiftz);
free(pshifty_ex);
free(pshiftz_ex);
}
/*
* @brief: find stress contributions from exchange-correlation
*/
void Calculate_XC_stress_cyclix(SPARC_OBJ *pSPARC) {
if (pSPARC->dmcomm_phi == MPI_COMM_NULL) return;
int rank;
MPI_Comm_rank(pSPARC->dmcomm_phi, &rank);
#ifdef DEBUG
if (!rank) printf("Start calculating exchange-correlation components of stress ...\n");
#endif
if(strcmp(pSPARC->XC,"LDA_PW") == 0 || strcmp(pSPARC->XC,"LDA_PZ") == 0){
pSPARC->stress_xc[5] = pSPARC->Exc - pSPARC->Exc_corr;
} else if(strcmp(pSPARC->XC,"GGA_PBE") == 0 || strcmp(pSPARC->XC,"GGA_RPBE") == 0 || strcmp(pSPARC->XC,"GGA_PBEsol") == 0){
pSPARC->stress_xc[5] = pSPARC->Exc - pSPARC->Exc_corr;
int len_tot, i, count, DMnd;
DMnd = pSPARC->Nd_d;
len_tot = pSPARC->Nspdentd * DMnd;
double *Drho_z;
double stress_xc = 0.0;
Drho_z = (double *)malloc( len_tot * sizeof(double));
double *rho = (double *)malloc(len_tot * sizeof(double) );
add_rho_core(pSPARC, pSPARC->electronDens, rho, pSPARC->Nspdentd);
Gradient_vectors_dir(pSPARC, DMnd, pSPARC->DMVertices, pSPARC->Nspdentd, 0.0, rho, DMnd, Drho_z, DMnd, 2, pSPARC->dmcomm_phi);
free(rho);
count = 0;
for (int n = 0; n < pSPARC->Nspdentd; n++) {
for(i = 0; i < DMnd; i++){
stress_xc += Drho_z[count] * Drho_z[count] * pSPARC->Dxcdgrho[count] * pSPARC->Intgwt_phi[i];
count++;
}
}
// do Allreduce/Reduce to find total integral // TODO: check if there's only 1 process, then skip this
MPI_Allreduce(MPI_IN_PLACE, &stress_xc, 1, MPI_DOUBLE, MPI_SUM, pSPARC->dmcomm_phi);
pSPARC->stress_xc[5] -= stress_xc;
// deallocate
free(Drho_z);
}
if (!rank) {
// Define measure of unit cell
double cell_measure = pSPARC->range_z;
pSPARC->stress_xc[5] /= cell_measure;
pSPARC->stress_xc[5] *= 2.0 * M_PI/pSPARC->range_y;
}
if (pSPARC->NLCC_flag) {
double stress_xc_nlcc = 0;
Calculate_XC_stress_nlcc_cyclix(pSPARC, &stress_xc_nlcc);
pSPARC->stress_xc[5] += stress_xc_nlcc;
}
#ifdef DEBUG
if (!rank) {
printf("\nXC contribution to stress");
PrintStress(pSPARC, pSPARC->stress_xc, NULL);
}
#endif
}
/*
@ brief: function to calculate the local stress components
*/
void Calculate_local_stress_cyclix(SPARC_OBJ *pSPARC) {
if (pSPARC->dmcomm_phi == MPI_COMM_NULL) return;
int ityp, iat, i, j, k, p, ip, jp, kp, ip2, jp2, kp2, di, dj, dk, i_DM, j_DM, k_DM, FDn, count, count_interp,
DMnx, DMny, DMnd, nx, ny, nz, nxp, nyp, nzp, nd_ex, nx2p, ny2p, nz2p, nd_2ex,
icor, jcor, kcor, *pshifty, *pshifty_ex, *pshiftz, *pshiftz_ex, *ind_interp;
double x0_i, y0_i, z0_i, x, y, z, *R, *VJ, *VJ_ref, *bJ, *bJ_ref, DbJ_z_val, DbJ_ref_z_val, DVJ_z_val, DVJ_ref_z_val,
*R_interp, *VJ_interp;
double inv_4PI = 0.25 / M_PI, w2_diag, rchrg;
double temp1, temp2, temp3, temp_z;
double x3_R3;
double stress_el = 0.0, stress_corr = 0.0;
int rank;
MPI_Comm_rank(pSPARC->dmcomm_phi, &rank);
#ifdef DEBUG
if (!rank) printf("Start calculating local components of stress ...\n");
#endif
////////////////////////////
double t1, t2, t_sort = 0.0;
////////////////////////////
double *Lap_wt, *Lap_stencil;
FDn = pSPARC->order / 2;
w2_diag = (pSPARC->D2_stencil_coeffs_x[0] + pSPARC->D2_stencil_coeffs_z[0]) * -inv_4PI;
Lap_wt = (double *)malloc((5*(FDn+1))*sizeof(double));
Lap_stencil = Lap_wt+5;
Lap_stencil_coef_compact(pSPARC, FDn, Lap_stencil, -inv_4PI);
// Nx = pSPARC->Nx; Ny = pSPARC->Ny; Nz = pSPARC->Nz;
DMnx = pSPARC->Nx_d; DMny = pSPARC->Ny_d; // DMnz = pSPARC->Nz_d;
DMnd = pSPARC->Nd_d;
// shift vectors initialized
pshifty = (int *)malloc( (FDn+1) * sizeof(int));
pshifty_ex = (int *)malloc( (FDn+1) * sizeof(int));
pshiftz = (int *)malloc( (FDn+1) * sizeof(int));
pshiftz_ex = (int *)malloc( (FDn+1) * sizeof(int));
// find gradient of phi
double *Dphi_z;
Dphi_z = (double *)malloc( DMnd * sizeof(double));
Gradient_vectors_dir(pSPARC, DMnd, pSPARC->DMVertices, 1, 0.0, pSPARC->elecstPotential, DMnd, Dphi_z, DMnd, 2, pSPARC->dmcomm_phi);
for(i = 0; i < DMnd; i++){
temp1 = 0.5 * (pSPARC->psdChrgDens[i] - pSPARC->electronDens[i]) * pSPARC->elecstPotential[i];
stress_el += (inv_4PI * Dphi_z[i] * Dphi_z[i] + temp1) * pSPARC->Intgwt_phi[i];
}
for (ityp = 0; ityp < pSPARC->Ntypes; ityp++) {
rchrg = pSPARC->psd[ityp].RadialGrid[pSPARC->psd[ityp].size-1];
for (iat = 0; iat < pSPARC->Atom_Influence_local[ityp].n_atom; iat++) {
// coordinates of the image atom
x0_i = pSPARC->Atom_Influence_local[ityp].coords[iat * 3];
y0_i = pSPARC->Atom_Influence_local[ityp].coords[iat * 3 + 1];
z0_i = pSPARC->Atom_Influence_local[ityp].coords[iat * 3 + 2];
// original atom index this image atom corresponds to
// atom_index = pSPARC->Atom_Influence_local[ityp].atom_index[iat];
// number of finite-difference nodes in each direction of overlap rb region
nx = pSPARC->Atom_Influence_local[ityp].xe[iat] - pSPARC->Atom_Influence_local[ityp].xs[iat] + 1;
ny = pSPARC->Atom_Influence_local[ityp].ye[iat] - pSPARC->Atom_Influence_local[ityp].ys[iat] + 1;
nz = pSPARC->Atom_Influence_local[ityp].ze[iat] - pSPARC->Atom_Influence_local[ityp].zs[iat] + 1;
// nd = nx * ny * nz;
// number of finite-difference nodes in each direction of extended_rb (rb + order/2) region
nxp = nx + pSPARC->order;
nyp = ny + pSPARC->order;
nzp = nz + pSPARC->order;
nd_ex = nxp * nyp * nzp; // total number of nodes
// number of finite-difference nodes in each direction of extended_extended_rb (rb + order) region
nx2p = nxp + pSPARC->order;
ny2p = nyp + pSPARC->order;
nz2p = nzp + pSPARC->order;
nd_2ex = nx2p * ny2p * nz2p; // total number of nodes
// radii^2 of the finite difference grids of the extended_extended_rb region
R = (double *)malloc(sizeof(double) * nd_2ex);
if (R == NULL) {
printf("\nMemory allocation failed!\n");
exit(EXIT_FAILURE);
}
// left corner of the 2FDn-extended-rb-region
icor = pSPARC->Atom_Influence_local[ityp].xs[iat] - pSPARC->order;
jcor = pSPARC->Atom_Influence_local[ityp].ys[iat] - pSPARC->order;
kcor = pSPARC->Atom_Influence_local[ityp].zs[iat] - pSPARC->order;
// relative coordinate of image atoms
//x0_i_shift = x0_i - pSPARC->delta_x * icor;
//y0_i_shift = y0_i - pSPARC->delta_y * jcor;
//z0_i_shift = z0_i - pSPARC->delta_z * kcor;
// find distance between atom and finite-difference grids
count = 0; count_interp = 0;
for (k = kcor; k < kcor+nz2p; k++) {
z = k * pSPARC->delta_z;
for (j = jcor; j < jcor+ny2p; j++) {
y = j * pSPARC->delta_y;
for (i = icor; i < icor+nx2p; i++) {
x = pSPARC->xin + i * pSPARC->delta_x;
CalculateDistance(pSPARC, x, y, z, x0_i, y0_i, z0_i, &R[count]);
if (R[count] <= rchrg) count_interp++;
count++;
}
}
}
VJ_ref = (double *)malloc( nd_2ex * sizeof(double) );
if (VJ_ref == NULL) {
printf("\nMemory allocation failed!\n");
exit(EXIT_FAILURE);
}
// Calculate pseudopotential reference
Calculate_Pseudopot_Ref(R, nd_2ex, pSPARC->REFERENCE_CUTOFF, -pSPARC->Znucl[ityp], VJ_ref);
VJ = (double *)malloc( nd_2ex * sizeof(double) );
if (VJ == NULL) {
printf("\nMemory allocation failed!\n");
exit(EXIT_FAILURE);
}
// avoid sorting positions larger than rchrg
VJ_interp = (double *)malloc( count_interp * sizeof(double) );
R_interp = (double *)malloc( count_interp * sizeof(double) );
ind_interp = (int *)malloc( count_interp * sizeof(int) );
count = 0;
for (i = 0; i < nd_2ex; i++) {
if (R[i] <= rchrg) {
ind_interp[count] = i; // store index
R_interp[count] = R[i]; // store radius value
count++;
} else {
VJ[i] = -pSPARC->Znucl[ityp] / R[i];
}
}
t1 = MPI_Wtime();
// sort R_interp and then apply cubic spline interpolation to find VJ
// notice here we extract out positions within radius rchrg
//printf("rank = %d, R[%d] = %.13e\n", rank, len_interp-1, R[len_interp-1]); // R is not sorted!
SortSplineInterp(pSPARC->psd[ityp].RadialGrid,pSPARC->psd[ityp].rVloc, pSPARC->psd[ityp].size,
R_interp, VJ_interp, count_interp, pSPARC->psd[ityp].SplinerVlocD);
t2 = MPI_Wtime();
t_sort += t2 - t1;
for (i = 0; i < count_interp; i++) {
if (R_interp[i] < TEMP_TOL) {
VJ[ind_interp[i]] = pSPARC->psd[ityp].Vloc_0;
} else {
VJ[ind_interp[i]] = VJ_interp[i]/R_interp[i];
}
}
free(VJ_interp); VJ_interp = NULL;
free(R_interp); R_interp = NULL;
free(ind_interp); ind_interp = NULL;
free(R); R = NULL;
// shift vectors initialized
pshifty[0] = pshiftz[0] = pshifty_ex[0] = pshiftz_ex[0] = 0;
for (p = 1; p <= FDn; p++) {
pshifty[p] = p * nxp;
pshiftz[p] = pshifty[p] * nyp;
pshifty_ex[p] = p * nx2p;
pshiftz_ex[p] = pshifty_ex[p] *ny2p;
}
// calculate pseudocharge density bJ and bJ_ref in the FDn+rb-domain
bJ = (double *)malloc( nd_ex * sizeof(double) );
bJ_ref = (double *)malloc( nd_ex * sizeof(double) );
if (bJ == NULL || bJ_ref == NULL) {
printf("\nMemory allocation failed!\n");
exit(EXIT_FAILURE);
}
double xin = pSPARC->xin + (pSPARC->Atom_Influence_local[ityp].xs[iat] - FDn) * pSPARC->delta_x;
Calc_lapV(pSPARC, VJ, FDn, nx2p, ny2p, nz2p, nxp, nyp, nzp, Lap_wt, w2_diag, xin, -inv_4PI, bJ);
Calc_lapV(pSPARC, VJ_ref, FDn, nx2p, ny2p, nz2p, nxp, nyp, nzp, Lap_wt, w2_diag, xin, -inv_4PI, bJ_ref);
// calculate gradient of bJ, bJ_ref, VJ, VJ_ref in the rb-domain
dk = pSPARC->Atom_Influence_local[ityp].zs[iat] - pSPARC->DMVertices[4];
dj = pSPARC->Atom_Influence_local[ityp].ys[iat] - pSPARC->DMVertices[2];
di = pSPARC->Atom_Influence_local[ityp].xs[iat] - pSPARC->DMVertices[0];
for(kp = FDn, kp2 = pSPARC->order, k_DM = dk; kp2 < nzp; kp++, kp2++, k_DM++) {
int kshift_DM = k_DM * DMnx * DMny;
int kshift_2p = kp2 * nx2p * ny2p;
int kshift_p = kp * nxp * nyp;
for(jp = FDn, jp2 = pSPARC->order, j_DM = dj; jp2 < nyp; jp++, jp2++, j_DM++) {
int jshift_DM = kshift_DM + j_DM * DMnx;
int jshift_2p = kshift_2p + jp2 * nx2p;
int jshift_p = kshift_p + jp * nxp;
//#pragma simd
for(ip = FDn, ip2 = pSPARC->order, i_DM = di; ip2 < nxp ; ip++, ip2++, i_DM++) {
int ishift_DM = jshift_DM + i_DM;
int ishift_2p = jshift_2p + ip2;
int ishift_p = jshift_p + ip;
DbJ_z_val = 0.0;
DbJ_ref_z_val = 0.0;
DVJ_z_val = 0.0;
DVJ_ref_z_val = 0.0;
Dpseudopot_cyclix_z(pSPARC, bJ, FDn, ishift_p, pshifty, pshiftz, &DbJ_z_val);
Dpseudopot_cyclix_z(pSPARC, bJ_ref, FDn, ishift_p, pshifty, pshiftz, &DbJ_ref_z_val);
Dpseudopot_cyclix_z(pSPARC, VJ, FDn, ishift_2p, pshifty_ex, pshiftz_ex, &DVJ_z_val);
Dpseudopot_cyclix_z(pSPARC, VJ_ref, FDn, ishift_2p, pshifty_ex, pshiftz_ex, &DVJ_ref_z_val);
// find integrals in the stress expression
x3_R3 = (k_DM + pSPARC->DMVertices[4]) * pSPARC->delta_z - z0_i;
stress_el += DbJ_z_val * pSPARC->elecstPotential[ishift_DM] * x3_R3 * pSPARC->Intgwt_phi[ishift_DM];
temp1 = pSPARC->Vc[ishift_DM] - VJ_ref[ishift_2p];
temp2 = pSPARC->Vc[ishift_DM];
temp3 = pSPARC->psdChrgDens[ishift_DM] + pSPARC->psdChrgDens_ref[ishift_DM];
temp_z = DbJ_ref_z_val*temp1 + DbJ_z_val*temp2 + (DVJ_ref_z_val-DVJ_z_val)*temp3 - DVJ_ref_z_val*bJ_ref[ishift_p];
stress_corr += temp_z * x3_R3 * pSPARC->Intgwt_phi[ishift_DM];
}
}
}
free(VJ); VJ = NULL;
free(VJ_ref); VJ_ref = NULL;
free(bJ); bJ = NULL;
free(bJ_ref); bJ_ref = NULL;
}
}
stress_el += 0.5 * stress_corr;
t1 = MPI_Wtime();
// do Allreduce/Reduce to find total integral // TODO: check if there's only 1 process, then skip this
MPI_Allreduce(MPI_IN_PLACE, &stress_el, 1, MPI_DOUBLE, MPI_SUM, pSPARC->dmcomm_phi);
t2 = MPI_Wtime();
pSPARC->stress_el[5] = stress_el + pSPARC->Esc;
if (!rank) {
// Define measure of unit cell
double cell_measure = pSPARC->range_z;
pSPARC->stress_el[5] /= cell_measure;
pSPARC->stress_el[5] *= 2.0 * M_PI/pSPARC->range_y;
}
#ifdef DEBUG
if (!rank){
printf("time for sorting and interpolate pseudopotential: %.3f ms, time for Allreduce/Reduce: %.3f ms \n", t_sort*1e3, (t2-t1)*1e3);
printf("\nElectrostatics contribution to stress");
PrintStress(pSPARC, pSPARC->stress_el, NULL);
}
#endif
//deallocate
free(Lap_wt);
free(Dphi_z);
free(pshifty);
free(pshiftz);
free(pshifty_ex);
free(pshiftz_ex);
}
void Dpseudopot_cyclix_z(SPARC_OBJ *pSPARC, double *VJ, int FDn, int ishift_p, int *pshifty, int *pshiftz, double *DVJ_z_val) {
double c31 = -pSPARC->twist;
double DY, DZ;
for (int p = 1; p <= FDn; p++) {
DY = (VJ[ishift_p+pshifty[p]] - VJ[ishift_p-pshifty[p]]) * pSPARC->D1_stencil_coeffs_y[p];
DZ = (VJ[ishift_p+pshiftz[p]] - VJ[ishift_p-pshiftz[p]]) * pSPARC->D1_stencil_coeffs_z[p];
*DVJ_z_val += c31 * DY + DZ;
}
}
/**
* @brief Calculate nonlocal + kinetic components of stress.
*/
void Calculate_nonlocal_kinetic_stress_cyclix(SPARC_OBJ *pSPARC)
{
if (pSPARC->spincomm_index < 0 || pSPARC->kptcomm_index < 0 || pSPARC->bandcomm_index < 0 || pSPARC->dmcomm == MPI_COMM_NULL) return;
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int ncol, DMnd, DMndsp, Nspinor;
ncol = pSPARC->Nband_bandcomm; // number of bands assigned
DMnd = pSPARC->Nd_d_dmcomm;
Nspinor = pSPARC->Nspinor_spincomm;
DMndsp = DMnd * Nspinor;
double *alpha, *beta, *beta3;
double energy_nl = 0.0, stress_k = 0.0, stress_nl = 0.0;
alpha = (double *)calloc( pSPARC->IP_displ[pSPARC->n_atom] * ncol * 2 * Nspinor, sizeof(double));
#ifdef DEBUG
if (!rank) printf("Start calculating stress contributions from kinetic and nonlocal psp. \n");
#endif
beta = alpha;
Compute_Integral_psi_Chi(pSPARC, beta, pSPARC->Xorb);
/* find inner product <Chi_Jlm, dPsi_3.(z-R_J3)> */
// find dPsi in z direction
for (int spinor = 0; spinor < Nspinor; spinor++) {
Gradient_vectors_dir(pSPARC, DMnd, pSPARC->DMVertices_dmcomm, ncol, 0.0, pSPARC->Xorb+spinor*DMnd, DMndsp,
pSPARC->Yorb+spinor*DMnd, DMndsp, 2, pSPARC->dmcomm);
}
beta3 = alpha + pSPARC->IP_displ[pSPARC->n_atom] * ncol * Nspinor;
Compute_Integral_Chi_XmRjp_beta_Dpsi_cyclix(pSPARC, pSPARC->Yorb, beta3);
// Kinetic stress
Compute_stress_tensor_kinetic_cyclix(pSPARC, pSPARC->Yorb, &stress_k);
if (pSPARC->npNd > 1) {
MPI_Allreduce(MPI_IN_PLACE, alpha, pSPARC->IP_displ[pSPARC->n_atom] * ncol * 2 * Nspinor, MPI_DOUBLE, MPI_SUM, pSPARC->dmcomm);
MPI_Allreduce(MPI_IN_PLACE, &stress_k, 1, MPI_DOUBLE, MPI_SUM, pSPARC->dmcomm);
}
/* calculate nonlocal stress */
Compute_stress_tensor_nloc_by_integrals_cyclix(pSPARC, &stress_nl, alpha);
energy_nl = Compute_Nonlocal_Energy_by_integrals(pSPARC, alpha);
free(alpha);
stress_nl *= pSPARC->occfac * 2.0;
energy_nl *= pSPARC->occfac;
stress_nl -= energy_nl;
// sum over all spin
if (pSPARC->npspin > 1) {
MPI_Allreduce(MPI_IN_PLACE, &stress_nl, 1, MPI_DOUBLE, MPI_SUM, pSPARC->spin_bridge_comm);
MPI_Allreduce(MPI_IN_PLACE, &stress_k, 1, MPI_DOUBLE, MPI_SUM, pSPARC->spin_bridge_comm);
}
// sum over all bands
if (pSPARC->npband > 1) {
MPI_Allreduce(MPI_IN_PLACE, &stress_nl, 1, MPI_DOUBLE, MPI_SUM, pSPARC->blacscomm);
MPI_Allreduce(MPI_IN_PLACE, &stress_k, 1, MPI_DOUBLE, MPI_SUM, pSPARC->blacscomm);
}
pSPARC->stress_nl[5] = stress_nl;
pSPARC->stress_k[5] = stress_k;
if (!rank) {
// Define measure of unit cell
double cell_measure = pSPARC->range_z;
pSPARC->stress_nl[5] /= cell_measure;
pSPARC->stress_nl[5] *= 2.0 * M_PI/pSPARC->range_y;
pSPARC->stress_k[5] /= cell_measure;
pSPARC->stress_k[5] *= 2.0 * M_PI/pSPARC->range_y;
}
#ifdef DEBUG
if (!rank){
printf("\nNon-local contribution to stress");
PrintStress(pSPARC, pSPARC->stress_nl, NULL);
printf("\nKinetic contribution to stress");
PrintStress(pSPARC, pSPARC->stress_k, NULL);
}
#endif
}
/**
* @brief Calculate <ChiSC_Jlm, ST(x-RJ')_beta, DPsi_n> for spinor stress
*/
void Compute_Integral_Chi_XmRjp_beta_Dpsi_cyclix(SPARC_OBJ *pSPARC, double *dpsi, double *beta)
{
int i, n, ndc, ityp, iat, ncol, DMnd, atom_index;
int spinor, Nspinor, DMndsp, spinorshift;
int indx, k_DM, DMnx, DMny;
ncol = pSPARC->Nband_bandcomm; // number of bands assigned
DMnd = pSPARC->Nd_d_dmcomm;
Nspinor = pSPARC->Nspinor_spincomm;
DMndsp = DMnd * Nspinor;
DMnx = pSPARC->Nx_d_dmcomm;
DMny = pSPARC->Ny_d_dmcomm;
double *dpsi_x3_rc, *dpsi_ptr, *dpsi_x3_rc_ptr;
double R3, x3_R3;
for (ityp = 0; ityp < pSPARC->Ntypes; ityp++) {
if (! pSPARC->nlocProj[ityp].nproj) continue; // this is typical for hydrogen
for (iat = 0; iat < pSPARC->Atom_Influence_nloc[ityp].n_atom; iat++) {
R3 = pSPARC->Atom_Influence_nloc[ityp].coords[iat*3+2];
ndc = pSPARC->Atom_Influence_nloc[ityp].ndc[iat];
dpsi_x3_rc = (double *)malloc( ndc * ncol * sizeof(double));
atom_index = pSPARC->Atom_Influence_nloc[ityp].atom_index[iat];
for (spinor = 0; spinor < Nspinor; spinor++) {
for (n = 0; n < ncol; n++) {
dpsi_ptr = dpsi + n * DMndsp + spinor * DMnd;
dpsi_x3_rc_ptr = dpsi_x3_rc + n * ndc;
for (i = 0; i < ndc; i++) {
indx = pSPARC->Atom_Influence_nloc[ityp].grid_pos[iat][i];
k_DM = indx / (DMnx * DMny);
x3_R3 = (k_DM + pSPARC->DMVertices_dmcomm[4]) * pSPARC->delta_z - R3;
*(dpsi_x3_rc_ptr + i) = *(dpsi_ptr + indx) * x3_R3;
}
}
spinorshift = pSPARC->IP_displ[pSPARC->n_atom] * ncol * spinor;
cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, pSPARC->nlocProj[ityp].nproj, ncol, ndc, 1.0, pSPARC->nlocProj[ityp].Chi_cyclix[iat], ndc,
dpsi_x3_rc, ndc, 1.0, beta+spinorshift+pSPARC->IP_displ[atom_index]*ncol, pSPARC->nlocProj[ityp].nproj);
}
free(dpsi_x3_rc);
}
}
}
/**
* @brief Calculate kinetic stress tensor
*/
void Compute_stress_tensor_kinetic_cyclix(SPARC_OBJ *pSPARC, double *dpsi, double *stress_k)
{
int ncol, DMnd, Nspinor, DMndsp, Ns;
ncol = pSPARC->Nband_bandcomm; // number of bands assigned
DMnd = pSPARC->Nd_d_dmcomm;
Nspinor = pSPARC->Nspinor_spincomm;
DMndsp = DMnd * Nspinor;
Ns = pSPARC->Nstates;
int n, spinor, i;
double temp_k = 0.0;
for(n = 0; n < ncol; n++){
for (spinor = 0; spinor < Nspinor; spinor++) {
double dpsi3_dpsi3 = 0.0;
double *dpsi_ptr = dpsi + n * DMndsp + spinor * DMnd; // dpsi_1
for(i = 0; i < DMnd; i++){
dpsi3_dpsi3 += *(dpsi_ptr + i) * *(dpsi_ptr + i) * pSPARC->Intgwt_psi[i];
}
double *occ = pSPARC->occ;
if (pSPARC->spin_typ == 1) occ += spinor * Ns;
double g_nk = occ[n + pSPARC->band_start_indx];
temp_k += dpsi3_dpsi3 * g_nk;
}
}
*stress_k = - pSPARC->occfac * temp_k;
}
void Compute_stress_tensor_nloc_by_integrals_cyclix(SPARC_OBJ *pSPARC, double *stress_nl, double *alpha)
{
int n, np, ldispl, ityp, iat, ncol, Ns;
int count, l, m, lmax, spinor, Nspinor;
int ppl, *IP_displ;
double g_nk, SJ, temp2_s, gamma_Jl = 0;
ncol = pSPARC->Nband_bandcomm; // number of bands assigned
Ns = pSPARC->Nstates;
Nspinor = pSPARC->Nspinor_spincomm;
IP_displ = pSPARC->IP_displ;
double *beta3_x3 = alpha + IP_displ[pSPARC->n_atom]*ncol*Nspinor;
double stress_nl_ = 0.0;
count = 0;
for (spinor = 0; spinor < Nspinor; spinor++) {
for (ityp = 0; ityp < pSPARC->Ntypes; ityp++) {
lmax = pSPARC->psd[ityp].lmax;
for (iat = 0; iat < pSPARC->nAtomv[ityp]; iat++) {
SJ = 0.0;
for (n = pSPARC->band_start_indx; n <= pSPARC->band_end_indx; n++) {
double *occ = pSPARC->occ;
if (pSPARC->spin_typ == 1) occ += spinor * Ns;
g_nk = occ[n];
temp2_s = 0;
ldispl = 0;
for (l = 0; l <= lmax; l++) {
ppl = pSPARC->psd[ityp].ppl[l];
// skip the local l
if (l == pSPARC->localPsd[ityp]) {
ldispl += ppl;
continue;
}
for (np = 0; np < ppl; np++) {
for (m = -l; m <= l; m++) {
gamma_Jl = pSPARC->psd[ityp].Gamma[ldispl+np];
temp2_s += gamma_Jl * alpha[count] * beta3_x3[count];
count++;
}
}
ldispl += ppl;
}
SJ += temp2_s * g_nk;
}
stress_nl_ -= SJ;
}
}
}
*stress_nl = stress_nl_;
}
/**
* @brief Calculate nonlocal + kinetic components of stress.
*/
void Calculate_nonlocal_kinetic_stress_kpt_cyclix(SPARC_OBJ *pSPARC)
{
if (pSPARC->spincomm_index < 0 || pSPARC->kptcomm_index < 0 || pSPARC->bandcomm_index < 0 || pSPARC->dmcomm == MPI_COMM_NULL) return;
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int ncol, DMnd, count, kpt, Nk, size_k;
int DMndsp, Nspinor;
ncol = pSPARC->Nband_bandcomm; // number of bands assigned
DMnd = pSPARC->Nd_d_dmcomm;
Nspinor = pSPARC->Nspinor_spincomm;
DMndsp = DMnd * Nspinor;
Nk = pSPARC->Nkpts_kptcomm;
size_k = DMndsp * ncol;
double _Complex *alpha, *alpha_so1, *alpha_so2, *beta, *beta3;
double energy_nl = 0.0, stress_k = 0.0, stress_nl = 0.0;
alpha = alpha_so1 = alpha_so2 = NULL;
alpha = (double _Complex *)calloc( pSPARC->IP_displ[pSPARC->n_atom] * ncol * Nk * 2 * Nspinor, sizeof(double _Complex));
if (pSPARC->SOC_Flag) {
alpha_so1 = (double _Complex *)calloc( pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nk * 2 * Nspinor, sizeof(double _Complex));
alpha_so2 = (double _Complex *)calloc( pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nk * 2 * Nspinor, sizeof(double _Complex));
}
double k1, k2, k3, kpt_vec[3];
#ifdef DEBUG
if (!rank) printf("Start calculating stress contributions from kinetic and nonlocal psp. \n");
#endif
for(kpt = 0; kpt < Nk; kpt++){
beta = alpha + pSPARC->IP_displ[pSPARC->n_atom] * ncol * Nspinor * kpt;
Compute_Integral_psi_Chi_kpt(pSPARC, beta, pSPARC->Xorb_kpt+kpt*size_k, kpt, "SC");
if (pSPARC->SOC_Flag == 0) continue;
beta = alpha_so1 + pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nspinor * kpt;
Compute_Integral_psi_Chi_kpt(pSPARC, beta, pSPARC->Xorb_kpt+kpt*size_k, kpt, "SO1");
beta = alpha_so2 + pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nspinor * kpt;
Compute_Integral_psi_Chi_kpt(pSPARC, beta, pSPARC->Xorb_kpt+kpt*size_k, kpt, "SO2");
count++;
}
/* find inner product <Chi_Jlm, dPsi_3.(z-R_J3)> */
for(kpt = 0; kpt < pSPARC->Nkpts_kptcomm; kpt++) {
kpt_vec[0] = k1 = pSPARC->k1_loc[kpt];
kpt_vec[1] = k2 = pSPARC->k2_loc[kpt];
kpt_vec[2] = k3 = pSPARC->k3_loc[kpt];
// find dPsi in direction dim
for (int spinor = 0; spinor < Nspinor; spinor++) {
Gradient_vectors_dir_kpt(pSPARC, DMnd, pSPARC->DMVertices_dmcomm, ncol, 0.0, pSPARC->Xorb_kpt+kpt*size_k+spinor*DMnd, DMndsp,
pSPARC->Yorb_kpt+spinor*DMnd, DMndsp, 2, kpt_vec, pSPARC->dmcomm);
}
beta3 = alpha + pSPARC->IP_displ[pSPARC->n_atom] * ncol * Nspinor * (Nk + kpt);
Compute_Integral_Chi_XmRjp_beta_Dpsi_kpt_cyclix(pSPARC, pSPARC->Yorb_kpt, beta3, kpt, "SC");
if (pSPARC->SOC_Flag == 1) {
beta3 = alpha_so1 + pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nspinor * (Nk + kpt);
Compute_Integral_Chi_XmRjp_beta_Dpsi_kpt_cyclix(pSPARC, pSPARC->Yorb_kpt, beta3, kpt, "SO1");
beta3 = alpha_so2 + pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nspinor * (Nk + kpt);
Compute_Integral_Chi_XmRjp_beta_Dpsi_kpt_cyclix(pSPARC, pSPARC->Yorb_kpt, beta3, kpt, "SO2");
}
Compute_stress_tensor_kinetic_kpt_cyclix(pSPARC, pSPARC->Yorb_kpt, &stress_k, kpt);
}
if (pSPARC->npNd > 1) {
MPI_Allreduce(MPI_IN_PLACE, alpha, pSPARC->IP_displ[pSPARC->n_atom] * ncol * Nk * 2 * Nspinor, MPI_DOUBLE_COMPLEX, MPI_SUM, pSPARC->dmcomm);
MPI_Allreduce(MPI_IN_PLACE, &stress_k, 1, MPI_DOUBLE, MPI_SUM, pSPARC->dmcomm);
if (pSPARC->SOC_Flag == 1) {
MPI_Allreduce(MPI_IN_PLACE, alpha_so1, pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nk * 2 * Nspinor, MPI_DOUBLE_COMPLEX, MPI_SUM, pSPARC->dmcomm);
MPI_Allreduce(MPI_IN_PLACE, alpha_so2, pSPARC->IP_displ_SOC[pSPARC->n_atom] * ncol * Nk * 2 * Nspinor, MPI_DOUBLE_COMPLEX, MPI_SUM, pSPARC->dmcomm);
}
}
/* calculate nonlocal stress */