-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMDE_linear.cpp
378 lines (312 loc) · 12.3 KB
/
MDE_linear.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "global.h"
#include "initial.h"
#include "fft.h"
#include "G_matrix.h"
#include "MDE_linear.h"
# define Maier_Saupe 0
/////////////////////////////////////////////////////////////////////////////////////////////
void MDE_linear_driver ( chain *Chain, chemical *Chemical )
{
int s;
const int forward=1;
const int backward=-1;
double **Pr=dmatrix(0,LOCAL_SIZE-1,0,M_Bar-1);
Propagator_init( forward, Chain );
// cout<<"WA(0)"<<Chemical->W_sp[0][0]<<endl;
// cout<<"WB(0)"<<Chemical->W_sp[1][0]<<endl;
for (s=1;s<=Chain->N_s;s++) {
Pr_stepping(s, forward, Chemical, Chain, Pr);
MDE_one_step( s,forward, local_r_grid, Chain, Chemical, Pr );
// if( myid==0) {
// cout<<"0q_f[s][0][0] s= "<<s<<" "<<Chain->q_f[s][0][0]<<endl;
// }
assert(Chain->q_f[s][0][0]<10);
}
Propagator_init( backward, Chain );
for (s=Chain->N_s-1;s>=0;s--) {
Pr_stepping(s, backward, Chemical, Chain, Pr);
MDE_one_step( s,backward, local_r_grid, Chain, Chemical, Pr );
// if(s==Chain->N_s-1 && myid==0 ) {
// cout<<"Pr(0,0) s=NS-1"<<Pr[0][0]<<endl;
// }
// if( myid==0 ) {
// cout<<"0q_b[s][0][0] s= "<<s<<" "<<Chain->q_b[s][0][0]<<endl;
// }
}
free_dmatrix(Pr,0,LOCAL_SIZE-1,0,M_Bar-1);
}
void calc_THETAij( chemical &Chemical )
{
int i,j;
int K;
for(K=0;K<LOCAL_SIZE;K++){
for(i=0;i<M_Bar;i++){
for(j=0;j<M_Bar;j++){
THETAij[K][i][j]=(Chemical.M_OP[K][0][0]-Chemical.M_OP[K][1][1])*THETAij_M11_M22[i][j]
+ Chemical.M_OP[K][2][2]*THETAij_M33[i][j]
+ Chemical.M_OP[K][0][1]*THETAij_M12[i][j]
+ Chemical.M_OP[K][0][2]*THETAij_M13[i][j]
+ Chemical.M_OP[K][1][2]*THETAij_M23[i][j];
}
}
}
THETA_nonzero_2D.clear(); // remove the existing elements in "THETA_nonzero_2D"
array_struct_int_int_double test1;
for(K=0;K<LOCAL_SIZE;K++){
vector<array_struct_int_int_double> test_THETA;
for(i=0;i<M_Bar;i++){
for(j=0;j<M_Bar;j++){
if (fabs(THETAij[K][i][j]) >= Thresh_sprase_matrix)
{
test1.i=i;
test1.j=j;
test1.x=THETAij[K][i][j];
test_THETA.push_back(test1);
}
}
}
THETA_nonzero_2D.push_back(test_THETA);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// initial condition of propagator q_f and q_b
void Propagator_init( int orient, chain *Chain )
{
int K,i;
if(orient==1) {
for(K=0;K<LOCAL_SIZE;K++){
for(i=0;i<M_Bar;i++){
Chain->q_f[0][K][i]=0.0;
if (i==0) Chain->q_f[0][K][i]=1.0;
}
}
}
else if (orient==-1) {
for(K=0;K<LOCAL_SIZE;K++){
for(i=0;i<M_Bar;i++){
Chain->q_b[Chain->N_s][K][i]=0.0;
if (i==0) Chain->q_b[Chain->N_s][K][i]=1.0;
}
}
}
}
void Pr_stepping(int s_step, int orient, chemical *Chemical, chain *Chain, double **Pr)
{
int i,j,l,n,n1,nblk,nch,K,index_nonzero;
double ds;
double q_coef1[3][3];
double q_coef2[3][3];
double temp1,temp2,Wtemp,tempx;
// set up coefficient for s_step=1,2,s(s>=3)
q_coef1[0][0]=1.0;
q_coef1[0][1]=0.0;
q_coef1[0][2]=0.0;
q_coef1[1][0]=2.0;
q_coef1[1][1]=-0.5;
q_coef1[1][2]=0.0;
q_coef1[2][0]=3.0;
q_coef1[2][1]=-1.5;
q_coef1[2][2]=1.0/3.0;
q_coef2[0][0]=1.0;
q_coef2[0][1]=0.0;
q_coef2[0][2]=0.0;
q_coef2[1][0]=2.0;
q_coef2[1][1]=-1.0;
q_coef2[1][2]=0.0;
q_coef2[2][0]=3.0;
q_coef2[2][1]=-3.0;
q_coef2[2][2]=1.0;
ds=1.0/Chain->N_s;
for(K=0;K<LOCAL_SIZE;K++){
for(i=0;i<M_Bar;i++){
Pr[K][i]=0.0;
}
}
// if(s_step==31) {
// cout<<"block num"<<Chain->n_blk<<endl;
// cout<<"block begin[0]"<<Chain->block_begin[0]<<endl;
// cout<<"block end[0]"<<Chain->block_end[0]<<endl;
// cout<<"block begin[1]"<<Chain->block_begin[1]<<endl;
// cout<<"block end[1]"<<Chain->block_end[1]<<endl;
// }
assert(orient == 1 || orient==-1); // two directions of a chain
if(orient==1) {
n = 3 < s_step ? 3 : s_step;
assert(s_step > 0 );} // s_step should not be the starting indx of a chain.
else {
assert(s_step < Chain->N_s );
n = 3 < (Chain->N_s-s_step) ? 3 : (Chain->N_s-s_step);}
//get block indx
for(n1=0;n1<Chain->n_blk;n1++){
if(s_step>Chain->block_begin[n1] && s_step<=Chain->block_end[n1]) {
nblk=n1;
break; }
}
if(s_step==0) nblk=0;
nch=Chain->block_spe[nblk];
//check if n, nch and nblk are all correct
// if(myid==0 && orient==-1 && s_step==Chain->N_s-1) {
// cout<<"s_step="<<s_step<<endl;
// cout<<"n="<<n<<endl;
// cout<<"nblk="<<nblk<<endl;
// cout<<"nch="<<nch<<endl;
// }
MPI_Barrier(MPI_COMM_WORLD);
for(K=0;K<LOCAL_SIZE;K++){
Wtemp=Chemical->W_sp[nch][K];
# if Maier_Saupe
for(index_nonzero=0;index_nonzero<THETA_nonzero_2D[K].size();index_nonzero++){
i=THETA_nonzero_2D[K][index_nonzero].i;
j=THETA_nonzero_2D[K][index_nonzero].j;
tempx=THETA_nonzero_2D[K][index_nonzero].x;
temp2=0.0;
if(orient==1)
for(l=1;l<=n;l++){
temp2=temp2+ q_coef2[n-1][l-1]*Chain->q_f[s_step-l][K][j];}
else
for(l=1;l<=n;l++){
temp2=temp2+ q_coef2[n-1][l-1]*Chain->q_b[s_step+l][K][j];}
//Pr[K][i]=Pr[K][i] + ds*tempx*q[0][K][j];
Pr[K][i]=Pr[K][i] + ds*tempx*temp2;
}
# else
# endif
for(i=0;i<M_Bar;i++){
temp1=0.0;
temp2=0.0;
if(orient==1)
for(l=1;l<=n;l++){
assert(l<=3 && n<=3 && s_step-l>=0);
temp1=temp1+ q_coef1[n-1][l-1]*Chain->q_f[s_step-l][K][i];
temp2=temp2+ q_coef2[n-1][l-1]*Chain->q_f[s_step-l][K][i];}
else
for(l=1;l<=n;l++){
temp1=temp1+ q_coef1[n-1][l-1]*Chain->q_b[s_step+l][K][i];
temp2=temp2+ q_coef2[n-1][l-1]*Chain->q_b[s_step+l][K][i];}
//Pr[K][i]=Pr[K][i] + (1.0 - ds*Wtemp)*q[0][K][i];
//if(K==0 && s_step==Chain->N_s-1 && myid==0 && i==0) cout<<"temp1,2"<<temp1<<" "<<temp2<<endl;
//if(K==0 && s_step==Chain->N_s-1 && myid==0 && i==0) cout<<"q_b[s_step+l][K][i]"<<Chain->q_b[100][K][i]<<endl;
Pr[K][i]=Pr[K][i] + temp1 - ds*Wtemp*temp2;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
void MDE_one_step(int s_step,int orient,grid &Grid,chain *Chain, chemical *Chemical, double **Pr )
{
int i,j,i1,j1,k1,n,nblk,nch,nG,s,blk_step;
int i_i,j_j,k_k,K;
int n1,n2,n3;
double **Pk_Real=dmatrix(0,LOCAL_SIZE_K-1,0,M_Bar-1);
double **Pk_Imag=dmatrix(0,LOCAL_SIZE_K-1,0,M_Bar-1);
double **q_temp_Real=dmatrix(0,LOCAL_SIZE_K-1,0,M_Bar-1);
double **q_temp_Imag=dmatrix(0,LOCAL_SIZE_K-1,0,M_Bar-1);
double **G_R_inv=dmatrix(0,M_Bar-1,0,M_Bar-1);
double **G_I_inv=dmatrix(0,M_Bar-1,0,M_Bar-1);
////////////////////////////////////////////////////////
n1=Grid.n_grid[0];
n2=Grid.n_grid[1];
n3=Grid.n_grid[2];
//get block indx
for(n=0;n<Chain->n_blk;n++){
if(s_step>Chain->block_begin[n] && s_step<=Chain->block_end[n]) {
nblk=n;
// cout<<"nsp="<<nsp<<" s_step= "<<s_step<<endl;
break; }
}
if(s_step==0) nblk=0;
nch=Chain->block_spe[nblk];
if(orient==1) { nG=n;}
else {nG=n+ Chemical->n_spe;}
if(s_step==0) nG=0;
// cout<<"nG=,nch"<<nG<<" "<<nch<<endl;
assert(nG >=0 && nG<2*Chemical->n_spe);
for(i=0;i<M_Bar;i++){
for (i1=Grid.start[0],K=0;i1<=Grid.end[0];i1++) {
for (j1=Grid.start[1];j1<=Grid.end[1];j1++) {
// why k<=local_r_grid.end[2]+2 ??, because we assume SIDEz to be even.
// Warning: it is wrong!! Make sure you understand this.
//for (k1=Grid.start[2];k1<=Grid.end[2]+2;k1++,K++) {
for (k1=Grid.start[2];k1<=Grid.end[2];k1++,K++) {
i_i=i1-Grid.start[0];
j_j=j1-Grid.start[1];
k_k=k1-Grid.start[2];
fft_pll.r_in[i_i*(n2*(n3+2))+j_j*(n3+2)+k_k]=Pr[K][i]; // the compilcated subindex could be inefficient, try to
// optimize in future.
}
}
}
// this actually ineffcient when placing the FFT as a inner loop, try to use fftw MANYPLAN in futrure
fftw_execute(fft_pll.plan_f);
for (K=0;K<LOCAL_SIZE_K;K++) {
Pk_Real[K][i]=fft_pll.c_out[K][0];
Pk_Imag[K][i]=fft_pll.c_out[K][1];
// if(K==0) cout<<"Pk_R(0,0)"<<Pk_Real[0][0]<<endl;
}
}
if(s_step<=2 && orient==1) {
//if(s_step==1) blk_step=1;
//if(s_step==2) blk_step=2;
blk_step=s_step;
for(K=0;K<LOCAL_SIZE_K;K++) {
matrix_G_inverse( blk_step, K, 0.5, matrix_Rx, matrix_Ry, matrix_Rz, \
G_R_inv,G_I_inv,global_grid, diblock, HEX ); // Does this pass by reference really work? Check out!!!
compx_matrx_mul_vector(M_Bar, G_R_inv, G_I_inv, Pk_Real[K], Pk_Imag[K], q_temp_Real[K], q_temp_Imag[K]);
// if(K==0) cout<<"q_temp_R(0,0)"<<q_temp_Real[0][0]<<endl;
}
}
else if(s_step>=Chain->N_s-2 && orient==-1) {
if(s_step==Chain->N_s-1) blk_step=-1;
if(s_step==Chain->N_s-2) blk_step=-2;
for(K=0;K<LOCAL_SIZE_K;K++){
matrix_G_inverse( blk_step, K, 0.5, matrix_Rx, matrix_Ry, matrix_Rz, \
G_R_inv,G_I_inv,global_grid, diblock, HEX ); // Does this pass by reference really work? Check out!!!
compx_matrx_mul_vector(M_Bar, G_R_inv, G_I_inv, Pk_Real[K], Pk_Imag[K], q_temp_Real[K], q_temp_Imag[K]);
}
}
else {
for(K=0;K<LOCAL_SIZE_K;K++){
// compx_matrx_mul_vector(M_Bar, GA1_R_inverse[K],GA1_I_inverse[K],Pk_Real[K],
// Pk_Imag[K],q_temp_Real[K],q_temp_Imag[K]);
compx_sparse_matrx_mul_vector(M_Bar, sa_G_R_inverse[nG][K], ija_G_R_inverse[nG][K], \
sa_G_I_inverse[nG][K], ija_G_I_inverse[nG][K], \
Pk_Real[K], Pk_Imag[K],q_temp_Real[K],q_temp_Imag[K]);
}
}
for(i=0;i<M_Bar;i++){
for(K=0;K<LOCAL_SIZE_K;K++){
fft_pll.c_out[K][0]=q_temp_Real[K][i];
fft_pll.c_out[K][1]=q_temp_Imag[K][i];
}
fftw_execute(fft_pll.plan_b);
for (i1=Grid.start[0],K=0;i1<=Grid.end[0];i1++) {
for (j1=Grid.start[1];j1<=Grid.end[1];j1++) {
for (k1=Grid.start[2];k1<=Grid.end[2];k1++,K++) {
i_i=i1-Grid.start[0];
j_j=j1-Grid.start[1];
k_k=k1-Grid.start[2];
// find a better way to deal with orient in future. How to pass Chain.q_f as a pointer??
switch(orient)
{
case 1 :
{
Chain->q_f[s_step][K][i]=fft_pll.r_in[i_i*(n2*(n3+2))+j_j*(n3+2)+k_k]/global_grid.grid_num;
break;
}
case -1 :
{
Chain->q_b[s_step][K][i]=fft_pll.r_in[i_i*(n2*(n3+2))+j_j*(n3+2)+k_k]/global_grid.grid_num;
break;
}
default: {}
}
}
}
}
} // end loop i for M_Bar
free_dmatrix(Pk_Real,0,LOCAL_SIZE_K-1,0,M_Bar-1);
free_dmatrix(Pk_Imag,0,LOCAL_SIZE_K-1,0,M_Bar-1);
free_dmatrix(q_temp_Real,0,LOCAL_SIZE_K-1,0,M_Bar-1);
free_dmatrix(q_temp_Imag,0,LOCAL_SIZE_K-1,0,M_Bar-1);
free_dmatrix(G_R_inv,0,M_Bar-1,0,M_Bar-1);
free_dmatrix(G_I_inv,0,M_Bar-1,0,M_Bar-1);
}