update_s_ssg_CPML_elastic.c 19 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
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
/*------------------------------------------------------------------------
 * Copyright (C) 2011 For the list of authors, see file AUTHORS.
 *
 * This file is part of SOFI3D.
 * 
 * SOFI3D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
 * SOFI3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SOFI3D. See file COPYING and/or 
  * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
 * Implementation acoording to Komatitsch, D. and Martin, R.(2007): "An unsplit convolutional perfectly matched
 * layer improved at grazing incidence for the seismic wave equation", geophysics, Vol.72, No.5
 * similar to fdveps (2D) 
 *  ----------------------------------------------------------------------*/

#include "fd.h"

double update_s_CPML_elastic(int nx1, int nx2, int ny1, int ny2, int nz1, int nz2, int nt,
		float *** vx, float *** vy, float *** vz,
		float *** sxx, float *** syy, float *** szz, float *** sxy, float *** syz, float *** sxz, float ***  pi, float ***  u,
		float ***  uipjp, float ***  ujpkp, float ***  uipkp,
		float * K_x, float * a_x, float * b_x, float * K_x_half, float * a_x_half, float * b_x_half,
		float * K_y, float * a_y, float * b_y, float * K_y_half, float * a_y_half, float * b_y_half,
		float * K_z, float * a_z, float * b_z, float * K_z_half, float * a_z_half, float * b_z_half,
		float *** psi_vxx, float *** psi_vyx, float *** psi_vzx, float *** psi_vxy, float *** psi_vyy, float *** psi_vzy, float *** psi_vxz, float *** psi_vyz, float *** psi_vzz){


	extern float DT, DX, DY, DZ;
	extern int MYID, LOG, FDCOEFF, FDORDER;
	extern FILE *FP;
	extern int FREE_SURF;
	extern int NPROCX, NPROCY, NPROCZ, POS[4];
	extern int FW, NY, NZ;
	extern int OUTNTIMESTEPINFO;

	int i, j, k, h1;
	double time=0.0, time1=0.0, time2=0.0;
	float vxx=0.0,vxy=0.0,vxz=0.0,vyx=0.0,vyy=0.0,vyz=0.0,vzx=0.0,vzy=0.0,vzz=0.0;
	float vxyyx,vyzzy,vxzzx,vxxyyzz,vyyzz,vxxzz,vxxyy;
	float g,f,fipjp,fjpkp,fipkp;
	float b1=1.0, b2=0.0;

	//float dthalbe;
	//dthalbe = DT/2.0;

	if (LOG)
		if ((MYID==0) && ((nt+(OUTNTIMESTEPINFO-1))%OUTNTIMESTEPINFO)==0) time1=MPI_Wtime();


	if(FDCOEFF==1){ /* Taylor coefficients*/

		switch (FDORDER){
		case 2 :
			b1=1.0; b2=0.0;
			break;
		case 4 :
			b1=9.0/8.0; b2=-1.0/24.0;
			break;
		default:
			b1=9.0/8.0; b2=-1.0/24.0;
			//warning(" Warning in function update_s_ssg_CPML_elastic : CPML Update of FDORDER > 4 not implemented yet, coefficients of FDORDER==4 are used instead!");
			//warning is issued in checkfd_ssg, section "ABSORBING BOUNDARY"
			break;
		}
	}

	if(FDCOEFF==2){ /* Holberg coefficients E=0.1 %*/

		switch (FDORDER){
		case 2 :
			b1=1.0; b2=0.0;
			break;
		case 4 :
			b1=1.1382; b2=-0.046414;
			break;
		default:
			b1=1.1382; b2=-0.046414;
			//warning(" Warning in function update_s_ssg_CPML_elastic : CPML Update of FDORDER > 4 not implemented yet, coefficients of FDORDER==4 are used instead!");
			//warning is issued in checkfd_ssg, section "ABSORBING BOUNDARY"
			break;
		}
	}



	if (POS[1]==0){
		for (j=1;j<=NY;j++){
			for (i=1;i<=FW;i++){
				for (k=1;k<=NZ;k++){

					vxx = (b1*(vx[j][i][k]-vx[j][i-1][k])+b2*(vx[j][i+1][k]-vx[j][i-2][k]))/DX;
					vxy = (b1*(vx[j+1][i][k]-vx[j][i][k])+b2*(vx[j+2][i][k]-vx[j-1][i][k]))/DY;
					vxz = (b1*(vx[j][i][k+1]-vx[j][i][k])+b2*(vx[j][i][k+2]-vx[j][i][k-1]))/DZ;
					vyx = (b1*(vy[j][i+1][k]-vy[j][i][k])+b2*(vy[j][i+2][k]-vy[j][i-1][k]))/DX;
					vyy = (b1*(vy[j][i][k]-vy[j-1][i][k])+b2*(vy[j+1][i][k]-vy[j-2][i][k]))/DY;
					vyz = (b1*(vy[j][i][k+1]-vy[j][i][k])+b2*(vy[j][i][k+2]-vy[j][i][k-1]))/DZ;
					vzx = (b1*(vz[j][i+1][k]-vz[j][i][k])+b2*(vz[j][i+2][k]-vz[j][i-1][k]))/DX;
					vzy = (b1*(vz[j+1][i][k]-vz[j][i][k])+b2*(vz[j+2][i][k]-vz[j-1][i][k]))/DY;
					vzz = (b1*(vz[j][i][k]-vz[j][i][k-1])+b2*(vz[j][i][k+1]-vz[j][i][k-2]))/DZ;

					psi_vxx[j][i][k] = b_x[i] * psi_vxx[j][i][k] + a_x[i] * vxx;
					vxx = vxx / K_x[i] + psi_vxx[j][i][k];
					psi_vyx[j][i][k] = b_x_half[i] * psi_vyx[j][i][k] + a_x_half[i] * vyx;
					vyx = vyx / K_x_half[i] + psi_vyx[j][i][k];
					psi_vzx[j][i][k] = b_x_half[i] * psi_vzx[j][i][k] + a_x_half[i] * vzx;
					vzx = vzx / K_x_half[i] + psi_vzx[j][i][k];


					if((POS[2]==0 && FREE_SURF==0) && (j<=FW)){

						psi_vxy[j][i][k] = b_y_half[j] * psi_vxy[j][i][k] + a_y_half[j] * vxy;
						vxy = vxy / K_y_half[j] + psi_vxy[j][i][k];
						psi_vyy[j][i][k] = b_y[j] * psi_vyy[j][i][k] + a_y[j] * vyy;
						vyy = vyy / K_y[j] + psi_vyy[j][i][k];
						psi_vzy[j][i][k] = b_y_half[j] * psi_vzy[j][i][k] + a_y_half[j] * vzy;
						vzy = vzy / K_y_half[j] + psi_vzy[j][i][k]; }

					if((POS[2]==NPROCY-1) && (j>=ny2+1)){
						h1 = (j-ny2+FW);

						psi_vxy[h1][i][k] = b_y_half[h1] * psi_vxy[h1][i][k] + a_y_half[h1] * vxy;
						vxy = vxy / K_y_half[h1] + psi_vxy[h1][i][k];
						psi_vyy[h1][i][k] = b_y[h1] * psi_vyy[h1][i][k] + a_y[h1] * vyy;
						vyy = vyy / K_y[h1] + psi_vyy[h1][i][k];
						psi_vzy[h1][i][k] = b_y_half[h1] * psi_vzy[h1][i][k] + a_y_half[h1] * vzy;
						vzy = vzy / K_y_half[h1] + psi_vzy[h1][i][k]; }

					if((POS[3]==0) && (k<=FW)){

						psi_vxz[j][i][k] = b_z_half[k] * psi_vxz[j][i][k] + a_z_half[k] * vxz;
Tilman Steinweg's avatar
Tilman Steinweg committed
140
						vxz = vxz / K_z_half[k] + psi_vxz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
141
						psi_vyz[j][i][k] = b_z_half[k] * psi_vyz[j][i][k] + a_z_half[k] * vyz;
Tilman Steinweg's avatar
Tilman Steinweg committed
142
						vyz = vyz / K_z_half[k] + psi_vyz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
143
						psi_vzz[j][i][k] = b_z[k] * psi_vzz[j][i][k] + a_z[k] * vzz;
Tilman Steinweg's avatar
Tilman Steinweg committed
144
						vzz = vzz / K_z[k] + psi_vzz[j][i][k];}
Tilman Steinweg's avatar
Tilman Steinweg committed
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

					if((POS[3]==NPROCZ-1) && (k>=nz2+1)){

						h1 = (k-nz2+FW);

						psi_vxz[j][i][h1] = b_z_half[h1] * psi_vxz[j][i][h1] + a_z_half[h1] * vxz;
						vxz = vxz / K_z_half[h1] + psi_vxz[j][i][h1];
						psi_vyz[j][i][h1] = b_z_half[h1] * psi_vyz[j][i][h1] + a_z_half[h1] * vyz;
						vyz = vyz / K_z_half[h1] + psi_vyz[j][i][h1];
						psi_vzz[j][i][h1] = b_z[h1] * psi_vzz[j][i][h1] + a_z[h1] * vzz;
						vzz = vzz / K_z[h1] + psi_vzz[j][i][h1];}




					fipjp=uipjp[j][i][k]*DT;
					fjpkp=ujpkp[j][i][k]*DT;
					fipkp=uipkp[j][i][k]*DT;
					g=pi[j][i][k];
					f=2.0*u[j][i][k];

					vxyyx=vxy+vyx;
					vyzzy=vyz+vzy;
					vxzzx=vxz+vzx;
					vxxyyzz=vxx+vyy+vzz;
					vyyzz=vyy+vzz;
					vxxzz=vxx+vzz;
					vxxyy=vxx+vyy;

					sxy[j][i][k]+=(fipjp*vxyyx);
					syz[j][i][k]+=(fjpkp*vyzzy);
					sxz[j][i][k]+=(fipkp*vxzzx);
					sxx[j][i][k]+=DT*((g*vxxyyzz)-(f*vyyzz));
					syy[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxzz));
					szz[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxyy));

				}
			}
		}
	}

	if(POS[1]==NPROCX-1){
		for (j=1;j<=NY;j++){
			for (i=nx2+1;i<=nx2+FW;i++){
				for (k=1;k<=NZ;k++){

					vxx = (b1*(vx[j][i][k]-vx[j][i-1][k])+b2*(vx[j][i+1][k]-vx[j][i-2][k]))/DX;
					vxy = (b1*(vx[j+1][i][k]-vx[j][i][k])+b2*(vx[j+2][i][k]-vx[j-1][i][k]))/DY;
					vxz = (b1*(vx[j][i][k+1]-vx[j][i][k])+b2*(vx[j][i][k+2]-vx[j][i][k-1]))/DZ;
					vyx = (b1*(vy[j][i+1][k]-vy[j][i][k])+b2*(vy[j][i+2][k]-vy[j][i-1][k]))/DX;
					vyy = (b1*(vy[j][i][k]-vy[j-1][i][k])+b2*(vy[j+1][i][k]-vy[j-2][i][k]))/DY;
					vyz = (b1*(vy[j][i][k+1]-vy[j][i][k])+b2*(vy[j][i][k+2]-vy[j][i][k-1]))/DZ;
					vzx = (b1*(vz[j][i+1][k]-vz[j][i][k])+b2*(vz[j][i+2][k]-vz[j][i-1][k]))/DX;
					vzy = (b1*(vz[j+1][i][k]-vz[j][i][k])+b2*(vz[j+2][i][k]-vz[j-1][i][k]))/DY;
					vzz = (b1*(vz[j][i][k]-vz[j][i][k-1])+b2*(vz[j][i][k+1]-vz[j][i][k-2]))/DZ;

					h1 = i-nx2+FW;

					psi_vxx[j][h1][k] = b_x[h1] * psi_vxx[j][h1][k] + a_x[h1] * vxx;
					vxx = vxx /K_x[h1] + psi_vxx[j][h1][k];
					psi_vyx[j][h1][k] = b_x_half[h1] * psi_vyx[j][h1][k] + a_x_half[h1] * vyx;
					vyx = vyx  /K_x_half[h1] + psi_vyx[j][h1][k];
					psi_vzx[j][h1][k] = b_x_half[h1] * psi_vzx[j][h1][k] + a_x_half[h1] * vzx;
					vzx = vzx / K_x_half[h1]  +psi_vzx[j][h1][k];

					if((POS[2]==0 && FREE_SURF==0) && (j<=FW)){

						psi_vxy[j][i][k] = b_y_half[j] * psi_vxy[j][i][k] + a_y_half[j] * vxy;
						vxy = vxy / K_y_half[j] + psi_vxy[j][i][k];
						psi_vyy[j][i][k] = b_y[j] * psi_vyy[j][i][k] + a_y[j] * vyy;
						vyy = vyy / K_y[j] + psi_vyy[j][i][k];
						psi_vzy[j][i][k] = b_y_half[j] * psi_vzy[j][i][k] + a_y_half[j] * vzy;
						vzy = vzy / K_y_half[j] + psi_vzy[j][i][k]; }


					if((POS[2]==NPROCY-1) && (j>=ny2+1)){
						h1 = (j-ny2+FW);

						psi_vxy[h1][i][k] = b_y_half[h1] * psi_vxy[h1][i][k] + a_y_half[h1] * vxy;
						vxy = vxy / K_y_half[h1] + psi_vxy[h1][i][k];
						psi_vyy[h1][i][k] = b_y[h1] * psi_vyy[h1][i][k] + a_y[h1] * vyy;
						vyy = vyy / K_y[h1] + psi_vyy[h1][i][k];
						psi_vzy[h1][i][k] = b_y_half[h1] * psi_vzy[h1][i][k] + a_y_half[h1] * vzy;
						vzy = vzy / K_y_half[h1] + psi_vzy[h1][i][k]; }


					if((POS[3]==0) && (k<=FW)){

						psi_vxz[j][i][k] = b_z_half[k] * psi_vxz[j][i][k] + a_z_half[k] * vxz;
Tilman Steinweg's avatar
Tilman Steinweg committed
234
						vxz = vxz / K_z_half[k] + psi_vxz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
235
						psi_vyz[j][i][k] = b_z_half[k] * psi_vyz[j][i][k] + a_z_half[k] * vyz;
Tilman Steinweg's avatar
Tilman Steinweg committed
236
						vyz = vyz / K_z_half[k] + psi_vyz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
237
						psi_vzz[j][i][k] = b_z[k] * psi_vzz[j][i][k] + a_z[k] * vzz;
Tilman Steinweg's avatar
Tilman Steinweg committed
238
						vzz = vzz / K_z[k] + psi_vzz[j][i][k];}
Tilman Steinweg's avatar
Tilman Steinweg committed
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


					if((POS[3]==NPROCZ-1) && (k>=nz2+1)){

						h1 = (k-nz2+FW);

						psi_vxz[j][i][h1] = b_z_half[h1] * psi_vxz[j][i][h1] + a_z_half[h1] * vxz;
						vxz = vxz / K_z_half[h1] + psi_vxz[j][i][h1];
						psi_vyz[j][i][h1] = b_z_half[h1] * psi_vyz[j][i][h1] + a_z_half[h1] * vyz;
						vyz = vyz / K_z_half[h1] + psi_vyz[j][i][h1];
						psi_vzz[j][i][h1] = b_z[h1] * psi_vzz[j][i][h1] + a_z[h1] * vzz;
						vzz = vzz / K_z[h1] + psi_vzz[j][i][h1];}




					fipjp=uipjp[j][i][k]*DT;
					fjpkp=ujpkp[j][i][k]*DT;
					fipkp=uipkp[j][i][k]*DT;
					g=pi[j][i][k];
					f=2.0*u[j][i][k];

					vxyyx=vxy+vyx;
					vyzzy=vyz+vzy;
					vxzzx=vxz+vzx;
					vxxyyzz=vxx+vyy+vzz;
					vyyzz=vyy+vzz;
					vxxzz=vxx+vzz;
					vxxyy=vxx+vyy;

					sxy[j][i][k]+=(fipjp*vxyyx);
					syz[j][i][k]+=(fjpkp*vyzzy);
					sxz[j][i][k]+=(fipkp*vxzzx);
					sxx[j][i][k]+=DT*((g*vxxyyzz)-(f*vyyzz));
					syy[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxzz));
					szz[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxyy));


				}
			}
		}
	}

	if((POS[2]==0 && FREE_SURF==0)){
		for (j=1;j<=FW;j++){
			for (i=nx1;i<=nx2;i++){
				for (k=1;k<=NZ;k++){

					vxx = (b1*(vx[j][i][k]-vx[j][i-1][k])+b2*(vx[j][i+1][k]-vx[j][i-2][k]))/DX;
					vxy = (b1*(vx[j+1][i][k]-vx[j][i][k])+b2*(vx[j+2][i][k]-vx[j-1][i][k]))/DY;
					vxz = (b1*(vx[j][i][k+1]-vx[j][i][k])+b2*(vx[j][i][k+2]-vx[j][i][k-1]))/DZ;
					vyx = (b1*(vy[j][i+1][k]-vy[j][i][k])+b2*(vy[j][i+2][k]-vy[j][i-1][k]))/DX;
					vyy = (b1*(vy[j][i][k]-vy[j-1][i][k])+b2*(vy[j+1][i][k]-vy[j-2][i][k]))/DY;
					vyz = (b1*(vy[j][i][k+1]-vy[j][i][k])+b2*(vy[j][i][k+2]-vy[j][i][k-1]))/DZ;
					vzx = (b1*(vz[j][i+1][k]-vz[j][i][k])+b2*(vz[j][i+2][k]-vz[j][i-1][k]))/DX;
					vzy = (b1*(vz[j+1][i][k]-vz[j][i][k])+b2*(vz[j+2][i][k]-vz[j-1][i][k]))/DY;
					vzz = (b1*(vz[j][i][k]-vz[j][i][k-1])+b2*(vz[j][i][k+1]-vz[j][i][k-2]))/DZ;

					psi_vxy[j][i][k] = b_y_half[j] * psi_vxy[j][i][k] + a_y_half[j] * vxy;
					vxy = vxy / K_y_half[j] + psi_vxy[j][i][k];
					psi_vyy[j][i][k] = b_y[j] * psi_vyy[j][i][k] + a_y[j] * vyy;
					vyy = vyy / K_y[j] + psi_vyy[j][i][k];
					psi_vzy[j][i][k] = b_y_half[j] * psi_vzy[j][i][k] + a_y_half[j] * vzy;
					vzy = vzy / K_y_half[j] + psi_vzy[j][i][k];

					if((POS[3]==0) && (k<=FW)){

						psi_vxz[j][i][k] = b_z_half[k] * psi_vxz[j][i][k] + a_z_half[k] * vxz;
Tilman Steinweg's avatar
Tilman Steinweg committed
307
						vxz = vxz / K_z_half[k] + psi_vxz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
308
						psi_vyz[j][i][k] = b_z_half[k] * psi_vyz[j][i][k] + a_z_half[k] * vyz;
Tilman Steinweg's avatar
Tilman Steinweg committed
309
						vyz = vyz / K_z_half[k] + psi_vyz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
310
						psi_vzz[j][i][k] = b_z[k] * psi_vzz[j][i][k] + a_z[k] * vzz;
Tilman Steinweg's avatar
Tilman Steinweg committed
311
						vzz = vzz / K_z[k] + psi_vzz[j][i][k];}
Tilman Steinweg's avatar
Tilman Steinweg committed
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


					if((POS[3]==NPROCZ-1) && (k>=nz2+1)){

						h1 = (k-nz2+FW);

						psi_vxz[j][i][h1] = b_z_half[h1] * psi_vxz[j][i][h1] + a_z_half[h1] * vxz;
						vxz = vxz / K_z_half[h1] + psi_vxz[j][i][h1];
						psi_vyz[j][i][h1] = b_z_half[h1] * psi_vyz[j][i][h1] + a_z_half[h1] * vyz;
						vyz = vyz / K_z_half[h1] + psi_vyz[j][i][h1];
						psi_vzz[j][i][h1] = b_z[h1] * psi_vzz[j][i][h1] + a_z[h1] * vzz;
						vzz = vzz / K_z[h1] + psi_vzz[j][i][h1];}


					fipjp=uipjp[j][i][k]*DT;
					fjpkp=ujpkp[j][i][k]*DT;
					fipkp=uipkp[j][i][k]*DT;
					g=pi[j][i][k];
					f=2.0*u[j][i][k];

					vxyyx=vxy+vyx;
					vyzzy=vyz+vzy;
					vxzzx=vxz+vzx;
					vxxyyzz=vxx+vyy+vzz;
					vyyzz=vyy+vzz;
					vxxzz=vxx+vzz;
					vxxyy=vxx+vyy;

					sxy[j][i][k]+=(fipjp*vxyyx);
					syz[j][i][k]+=(fjpkp*vyzzy);
					sxz[j][i][k]+=(fipkp*vxzzx);
					sxx[j][i][k]+=DT*((g*vxxyyzz)-(f*vyyzz));
					syy[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxzz));
					szz[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxyy));


				}
			}
		}
	}

	if(POS[2]==NPROCY-1){
		for (j=ny2+1;j<=ny2+FW;j++){
			for (i=nx1;i<=nx2;i++){
				for (k=1;k<=NZ;k++){

					vxx = (b1*(vx[j][i][k]-vx[j][i-1][k])+b2*(vx[j][i+1][k]-vx[j][i-2][k]))/DX;
					vxy = (b1*(vx[j+1][i][k]-vx[j][i][k])+b2*(vx[j+2][i][k]-vx[j-1][i][k]))/DY;
					vxz = (b1*(vx[j][i][k+1]-vx[j][i][k])+b2*(vx[j][i][k+2]-vx[j][i][k-1]))/DZ;
					vyx = (b1*(vy[j][i+1][k]-vy[j][i][k])+b2*(vy[j][i+2][k]-vy[j][i-1][k]))/DX;
					vyy = (b1*(vy[j][i][k]-vy[j-1][i][k])+b2*(vy[j+1][i][k]-vy[j-2][i][k]))/DY;
					vyz = (b1*(vy[j][i][k+1]-vy[j][i][k])+b2*(vy[j][i][k+2]-vy[j][i][k-1]))/DZ;
					vzx = (b1*(vz[j][i+1][k]-vz[j][i][k])+b2*(vz[j][i+2][k]-vz[j][i-1][k]))/DX;
					vzy = (b1*(vz[j+1][i][k]-vz[j][i][k])+b2*(vz[j+2][i][k]-vz[j-1][i][k]))/DY;
					vzz = (b1*(vz[j][i][k]-vz[j][i][k-1])+b2*(vz[j][i][k+1]-vz[j][i][k-2]))/DZ;

					h1 = (j-ny2+FW);

					psi_vxy[h1][i][k] = b_y_half[h1] * psi_vxy[h1][i][k] + a_y_half[h1] * vxy;
					vxy = vxy / K_y_half[h1] + psi_vxy[h1][i][k];
					psi_vyy[h1][i][k] = b_y[h1] * psi_vyy[h1][i][k] + a_y[h1] * vyy;
					vyy = vyy / K_y[h1] + psi_vyy[h1][i][k];
					psi_vzy[h1][i][k] = b_y_half[h1] * psi_vzy[h1][i][k] + a_y_half[h1] * vzy;
					vzy = vzy / K_y_half[h1] + psi_vzy[h1][i][k];

					if((POS[3]==0) && (k<=FW)){

						psi_vxz[j][i][k] = b_z_half[k] * psi_vxz[j][i][k] + a_z_half[k] * vxz;
Tilman Steinweg's avatar
Tilman Steinweg committed
380
						vxz = vxz / K_z_half[k] + psi_vxz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
381
						psi_vyz[j][i][k] = b_z_half[k] * psi_vyz[j][i][k] + a_z_half[k] * vyz;
Tilman Steinweg's avatar
Tilman Steinweg committed
382
						vyz = vyz / K_z_half[k] + psi_vyz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
383
						psi_vzz[j][i][k] = b_z[k] * psi_vzz[j][i][k] + a_z[k] * vzz;
Tilman Steinweg's avatar
Tilman Steinweg committed
384
						vzz = vzz / K_z[k] + psi_vzz[j][i][k];}
Tilman Steinweg's avatar
Tilman Steinweg committed
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


					if((POS[3]==NPROCZ-1) && (k>=nz2+1)){

						h1 = (k-nz2+FW);

						psi_vxz[j][i][h1] = b_z_half[h1] * psi_vxz[j][i][h1] + a_z_half[h1] * vxz;
						vxz = vxz / K_z_half[h1] + psi_vxz[j][i][h1];
						psi_vyz[j][i][h1] = b_z_half[h1] * psi_vyz[j][i][h1] + a_z_half[h1] * vyz;
						vyz = vyz / K_z_half[h1] + psi_vyz[j][i][h1];
						psi_vzz[j][i][h1] = b_z[h1] * psi_vzz[j][i][h1] + a_z[h1] * vzz;
						vzz = vzz / K_z[h1] + psi_vzz[j][i][h1];}


					fipjp=uipjp[j][i][k]*DT;
					fjpkp=ujpkp[j][i][k]*DT;
					fipkp=uipkp[j][i][k]*DT;
					g=pi[j][i][k];
					f=2.0*u[j][i][k];

					vxyyx=vxy+vyx;
					vyzzy=vyz+vzy;
					vxzzx=vxz+vzx;
					vxxyyzz=vxx+vyy+vzz;
					vyyzz=vyy+vzz;
					vxxzz=vxx+vzz;
					vxxyy=vxx+vyy;

					sxy[j][i][k]+=(fipjp*vxyyx);
					syz[j][i][k]+=(fjpkp*vyzzy);
					sxz[j][i][k]+=(fipkp*vxzzx);
					sxx[j][i][k]+=DT*((g*vxxyyzz)-(f*vyyzz));
					syy[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxzz));
					szz[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxyy));


				}
			}
		}
	}


	if(POS[3]==0){
		for (j=ny1;j<=ny2;j++){
			for (i=nx1;i<=nx2;i++){
				for (k=1;k<=FW;k++){

					vxx = (b1*(vx[j][i][k]-vx[j][i-1][k])+b2*(vx[j][i+1][k]-vx[j][i-2][k]))/DX;
					vxy = (b1*(vx[j+1][i][k]-vx[j][i][k])+b2*(vx[j+2][i][k]-vx[j-1][i][k]))/DY;
					vxz = (b1*(vx[j][i][k+1]-vx[j][i][k])+b2*(vx[j][i][k+2]-vx[j][i][k-1]))/DZ;
					vyx = (b1*(vy[j][i+1][k]-vy[j][i][k])+b2*(vy[j][i+2][k]-vy[j][i-1][k]))/DX;
					vyy = (b1*(vy[j][i][k]-vy[j-1][i][k])+b2*(vy[j+1][i][k]-vy[j-2][i][k]))/DY;
					vyz = (b1*(vy[j][i][k+1]-vy[j][i][k])+b2*(vy[j][i][k+2]-vy[j][i][k-1]))/DZ;
					vzx = (b1*(vz[j][i+1][k]-vz[j][i][k])+b2*(vz[j][i+2][k]-vz[j][i-1][k]))/DX;
					vzy = (b1*(vz[j+1][i][k]-vz[j][i][k])+b2*(vz[j+2][i][k]-vz[j-1][i][k]))/DY;
					vzz = (b1*(vz[j][i][k]-vz[j][i][k-1])+b2*(vz[j][i][k+1]-vz[j][i][k-2]))/DZ;

					psi_vxz[j][i][k] = b_z_half[k] * psi_vxz[j][i][k] + a_z_half[k] * vxz;
Tilman Steinweg's avatar
Tilman Steinweg committed
443
					vxz = vxz / K_z_half[k] + psi_vxz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
444
					psi_vyz[j][i][k] = b_z_half[k] * psi_vyz[j][i][k] + a_z_half[k] * vyz;
Tilman Steinweg's avatar
Tilman Steinweg committed
445
					vyz = vyz / K_z_half[k] + psi_vyz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
446
					psi_vzz[j][i][k] = b_z[k] * psi_vzz[j][i][k] + a_z[k] * vzz;
Tilman Steinweg's avatar
Tilman Steinweg committed
447
					vzz = vzz / K_z[k] + psi_vzz[j][i][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
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



					fipjp=uipjp[j][i][k]*DT;
					fjpkp=ujpkp[j][i][k]*DT;
					fipkp=uipkp[j][i][k]*DT;
					g=pi[j][i][k];
					f=2.0*u[j][i][k];

					vxyyx=vxy+vyx;
					vyzzy=vyz+vzy;
					vxzzx=vxz+vzx;
					vxxyyzz=vxx+vyy+vzz;
					vyyzz=vyy+vzz;
					vxxzz=vxx+vzz;
					vxxyy=vxx+vyy;

					sxy[j][i][k]+=(fipjp*vxyyx);
					syz[j][i][k]+=(fjpkp*vyzzy);
					sxz[j][i][k]+=(fipkp*vxzzx);
					sxx[j][i][k]+=DT*((g*vxxyyzz)-(f*vyyzz));
					syy[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxzz));
					szz[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxyy));

				}
			}
		}
	}


	if(POS[3]==NPROCZ-1){		
		for (j=ny1;j<=ny2;j++){
			for (i=nx1;i<=nx2;i++){
				for (k=nz2+1;k<=nz2+FW;k++){

					vxx = (b1*(vx[j][i][k]-vx[j][i-1][k])+b2*(vx[j][i+1][k]-vx[j][i-2][k]))/DX;
					vxy = (b1*(vx[j+1][i][k]-vx[j][i][k])+b2*(vx[j+2][i][k]-vx[j-1][i][k]))/DY;
					vxz = (b1*(vx[j][i][k+1]-vx[j][i][k])+b2*(vx[j][i][k+2]-vx[j][i][k-1]))/DZ;
					vyx = (b1*(vy[j][i+1][k]-vy[j][i][k])+b2*(vy[j][i+2][k]-vy[j][i-1][k]))/DX;
					vyy = (b1*(vy[j][i][k]-vy[j-1][i][k])+b2*(vy[j+1][i][k]-vy[j-2][i][k]))/DY;
					vyz = (b1*(vy[j][i][k+1]-vy[j][i][k])+b2*(vy[j][i][k+2]-vy[j][i][k-1]))/DZ;
					vzx = (b1*(vz[j][i+1][k]-vz[j][i][k])+b2*(vz[j][i+2][k]-vz[j][i-1][k]))/DX;
					vzy = (b1*(vz[j+1][i][k]-vz[j][i][k])+b2*(vz[j+2][i][k]-vz[j-1][i][k]))/DY;
					vzz = (b1*(vz[j][i][k]-vz[j][i][k-1])+b2*(vz[j][i][k+1]-vz[j][i][k-2]))/DZ;

					h1 = (k-nz2+FW);

					psi_vxz[j][i][h1] = b_z_half[h1] * psi_vxz[j][i][h1] + a_z_half[h1] * vxz;
					vxz = vxz / K_z_half[h1] + psi_vxz[j][i][h1];
					psi_vyz[j][i][h1] = b_z_half[h1] * psi_vyz[j][i][h1] + a_z_half[h1] * vyz;
					vyz = vyz / K_z_half[h1] + psi_vyz[j][i][h1];
					psi_vzz[j][i][h1] = b_z[h1] * psi_vzz[j][i][h1] + a_z[h1] * vzz;
					vzz = vzz / K_z[h1] + psi_vzz[j][i][h1];





					fipjp=uipjp[j][i][k]*DT;
					fjpkp=ujpkp[j][i][k]*DT;
					fipkp=uipkp[j][i][k]*DT;
					g=pi[j][i][k];
					f=2.0*u[j][i][k];

					vxyyx=vxy+vyx;
					vyzzy=vyz+vzy;
					vxzzx=vxz+vzx;
					vxxyyzz=vxx+vyy+vzz;
					vyyzz=vyy+vzz;
					vxxzz=vxx+vzz;
					vxxyy=vxx+vyy;

					sxy[j][i][k]+=(fipjp*vxyyx);
					syz[j][i][k]+=(fjpkp*vyzzy);
					sxz[j][i][k]+=(fipkp*vxzzx);
					sxx[j][i][k]+=DT*((g*vxxyyzz)-(f*vyyzz));
					syy[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxzz));
					szz[j][i][k]+=DT*((g*vxxyyzz)-(f*vxxyy));

				}
			}
		}
	}


	if (LOG)
		if ((MYID==0) && ((nt+(OUTNTIMESTEPINFO-1))%OUTNTIMESTEPINFO)==0){
			time2=MPI_Wtime();
			time=time2-time1;
			fprintf(FP," Real time for CPML stress tensor update: \t %4.2f s.\n",time);
		}
	return time;

}