denise.c 123 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
/*-----------------------------------------------------------------------------------------
 * Copyright (C) 2013  For the list of authors, see file AUTHORS.
 *
 * This file is part of DENISE.
 * 
 * DENISE 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.
 * 
 * DENISE 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 DENISE. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
-----------------------------------------------------------------------------------------*/

/* ----------------------------------------------------------------------
* This is program DENISE.
* subwavelength DEtail resolving Nonlinear Iterative SEismic inversion
*
* If you use this code for your own research please cite at least one article
* written by the developers of the package, e.g.
* D. K�hn. Time domain 2D elastic full waveform tomography. PhD Thesis, Kiel
* University, 2011.
*
*  ----------------------------------------------------------------------*/


#include "fd.h"           /* general include file for viscoelastic FD programs */

#include "globvar.h"      /* definition of global variables  */
#include "cseife.h"

#include "stfinv/stfinv.h" /* libstfinv - inversion for source time function */

int main(int argc, char **argv){
	/* variables in main */
	int ns, nseismograms=0, nt, nd, fdo3, j, i, ii, jj, shotid, recid, k, nc, iter, h, infoout, SHOTINC, TIMEWIN, test_eps, lq, iq, jq, hin, hin1, s=0;
	int NTDTINV, nxny, nxnyi, imat, imat1, imat2, IDXI, IDYI, hi, NTST, NTSTI, partest;
	int lsnap, nsnap=0, lsamp=0, buffsize, invtime, invtimer, sws, swstestshot, snapseis, snapseis1, PML;
	int ntr=0, ntr_loc=0, ntr_glob=0, nsrc=0, nsrc_loc=0, nsrc_glob=0, ishot, irec, nshots=0, nshots1, Lcount, itest, Lcountsum, itestshot;

	float pum, ppim, ppim1, ppim2, thetaf, thetab, e33, e33b, e11, e11b, muss, lamss; 
	float memdyn, memmodel, memseismograms, membuffer, memtotal, dngn, fphi, sum, avggrad, beta, betan, betaz, betaLog, betaVp, betaVs, betarho, eps_scale, L2old;
	float fac1, fac2, wavefor, waverecipro, dump, dump1, epsilon, gradsign, mun, eps1, gradplastiter, gradglastiter, gradclastiter, betar, sig_max, sig_max1;
	float signL1, RMS, opteps_vp, opteps_vs, opteps_rho, Vs, Vp, Vp_avg, C_vp, Vs_avg, C_vs, Cd, rho_avg, C_rho, Vs_sum, Vp_sum, rho_sum, Zp, Zs;
	float freqshift, dfreqshift, memfwt, memfwt1, memfwtdata;
	char *buff_addr, ext[10], *fileinp;
	char wave_forward[225], wave_recipro[225], wave_conv[225], jac[225], jac2[225], jacsum[225], dwavelet[225], vyf[STRING_SIZE];

53
54
	double time1, time2, time3, time4, time5, time6, time7, time8, time_av_v_update=0.0, time_av_s_update=0.0, time_av_v_exchange=0.0, time_av_s_exchange=0.0, time_av_timestep=0.0;
	
Tilman Steinweg's avatar
Tilman Steinweg committed
55
56
57
58
	float L2, L2sum, L2_all_shots, L2sum_all_shots, *L2t, alphanomsum, alphanom, alphadenomsum, alphadenom, scaleamp ,sdummy, lamr; 
	int sum_killed_traces=0, sum_killed_traces_testshots=0, killed_traces=0, killed_traces_testshots=0;
	int *ptr_killed_traces=&killed_traces, *ptr_killed_traces_testshots=&killed_traces_testshots;

59
	float energy, energy_sum, energy_all_shots, energy_sum_all_shots;
Tilman Steinweg's avatar
Tilman Steinweg committed
60

61
62
63
64
65
66
67
	float  ** psxx, **  psxy, ** psyy, **psp, ** ux, ** uy, ** uxy, ** uyx, ** Vp0, ** uttx, ** utty, ** Vs0, ** Rho0;
	float  ** pvx,  **  pvy,  ** waveconv, **waveconv_lam, **waveconv_mu, **waveconv_rho, **waveconv_rho_s, **waveconv_u, **waveconvtmp, **wcpart, **wavejac;
	float  ** waveconv_shot,  ** waveconv_u_shot, **waveconv_rho_shot;
	float  ** pvxp1, ** pvyp1, ** pvxm1, ** pvym1;
	float  ** gradg, ** gradp, ** gradg_rho, ** gradp_rho, ** gradg_u, ** gradp_u;
	float  ** prho,  ** prhonp1, ** prip=NULL, **prjp=NULL, **pripnp1=NULL, **prjpnp1=NULL, **  ppi, **  pu, **  punp1, **  puipjp, **  ppinp1;
	float  ** vpmat, ***forward_prop_x, ***forward_prop_y, ***forward_prop_rho_x, ***forward_prop_u, ***forward_prop_rho_y, ***forward_prop_p;
Tilman Steinweg's avatar
Tilman Steinweg committed
68

69
70
71
72
	float  	** sectionvx=NULL, ** sectionvy=NULL, ** sectionp=NULL, ** sectionpnp1=NULL, ** sectioncurl=NULL, ** sectiondiv=NULL, ** sectionvxdata=NULL, ** sectionvxdiff=NULL, ** sectionvxdiffold=NULL,
		** sectionvydiffold=NULL, ** sectionpdata=NULL, ** sectionpdiff=NULL, ** sectionpdiffold=NULL, ** sectionvydiff=NULL, ** sectionvydata=NULL, ** sectionpn=NULL, ** sectionread=NULL,
		** sectionvy_conv=NULL, ** sectionvy_obs=NULL, ** sectionvx_conv=NULL,** sectionvx_obs=NULL, ** sectionp_conv=NULL,** sectionp_obs=NULL, * source_time_function=NULL;
		
Tilman Steinweg's avatar
Tilman Steinweg committed
73
	float  **  absorb_coeff, ** taper_coeff, * epst1, * epst2,  * epst3, * picked_times;
74
	float  ** srcpos=NULL, **srcpos_loc=NULL, ** srcpos1=NULL, **srcpos_loc_back=NULL, ** signals=NULL, ** signals_rec=NULL, *hc=NULL;
Tilman Steinweg's avatar
Tilman Steinweg committed
75
	int   ** recpos=NULL, ** recpos_loc=NULL;
76
	
Tilman Steinweg's avatar
Tilman Steinweg committed
77
78
	int * DTINV_help;
	
79
	float ** bufferlef_to_rig,  ** bufferrig_to_lef, ** buffertop_to_bot, ** bufferbot_to_top, ** p_bufferlef_to_rig,  ** p_bufferrig_to_lef, ** p_buffertop_to_bot, ** p_bufferbot_to_top; 
Tilman Steinweg's avatar
Tilman Steinweg committed
80
81

	/* PML variables */
82
83
84
	float 	* d_x, * K_x, * alpha_prime_x, * a_x, * b_x, * d_x_half, * K_x_half, * alpha_prime_x_half, * a_x_half, * b_x_half,
		* d_y, * K_y, * alpha_prime_y, * a_y, * b_y, * d_y_half, * K_y_half, * alpha_prime_y_half, * a_y_half, * b_y_half;
	float 	** psi_sxx_x, ** psi_syy_y, ** psi_sxy_y, ** psi_sxy_x, ** psi_vxx, ** psi_vyy, ** psi_vxy, ** psi_vyx, ** psi_vxxs;
Tilman Steinweg's avatar
Tilman Steinweg committed
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

	/* Variables for viscoelastic modeling */
	float **ptaus=NULL, **ptaup=NULL, *etaip=NULL, *etajm=NULL, *peta=NULL, **ptausipjp=NULL, **fipjp=NULL, ***dip=NULL, *bip=NULL, *bjm=NULL;
	float *cip=NULL, *cjm=NULL, ***d=NULL, ***e=NULL, ***pr=NULL, ***pp=NULL, ***pq=NULL, **f=NULL, **g=NULL;

	/* Variables for step length calculation */
	int step1, step2, step3=0, itests, iteste, stepmax, countstep;
	float scalefac;

	/* Variables for Pseudo-Hessian calculation */
	int RECINC, ntr1;
	float * jac_rho, * jac_u, * jac_lam_x, * jac_lam_y;
	float * temp_TS, * temp_TS1, * temp_TS2, * temp_TS3, * temp_TS4, * temp_TS5, * temp_conv, * temp_conv1, * temp_conv2;
	float temp_hess, temp_hess_lambda, temp_hess_mu, mulamratio;
	float ** hessian, ** hessian_u, ** hessian_rho, **hessian_shot, **hessian_u_shot, **hessian_rho_shot;
	int QUELLART_OLD;

	/* Variables of the L-BFGS method */
	float *** y_LBFGS_vp, *** s_LBFGS_vp, * rho_LBFGS, * alpha_LBFGS; 
	float *** y_LBFGS_vs, *** s_LBFGS_vs;
	float *** y_LBFGS_rho, *** s_LBFGS_rho;
	int NLBFGS;
	float * rho_LBFGS_vp, * rho_LBFGS_vs, * alpha_LBFGS_vp, * alpha_LBFGS_vs;

	int * recswitch=NULL;
	float ** fulldata=NULL, ** fulldata_vx=NULL, ** fulldata_vy=NULL, ** fulldata_p=NULL, ** fulldata_curl=NULL, ** fulldata_div=NULL;

	/* different modelling types */
	int mod_type=0;

	/*vector for abort criterion*/
	float * L2_hist=NULL;

	/* help variable for MIN_ITER */
	int min_iter_help=0;

	/* variable for time domain filtering */
	float FC;
	float *FC_EXT=NULL;
	int nfrq=0;
	int FREQ_NR=1;
	/* declaration of variables for trace killing */
	int ** kill_tmp;	
	FILE *ftracekill;

	FILE *fprec, *FP2, *FP3, *FP4, *FP5, *FPL2, *FP6, *FP7;
		
	MPI_Request *req_send, *req_rec;
	MPI_Status  *send_statuses, *rec_statuses;

	/* Initialize MPI environment */
	MPI_Init(&argc,&argv);
	MPI_Comm_size(MPI_COMM_WORLD,&NP);
	MPI_Comm_rank(MPI_COMM_WORLD,&MYID);

	setvbuf(stdout, NULL, _IONBF, 0);

	if (MYID == 0){
		time1=MPI_Wtime(); 
		clock();
	}


	/* print program name, version etc to stdout*/
	if (MYID == 0) info(stdout);

	/* read parameters from parameter-file (stdin) */
	fileinp=argv[1];
	FP=fopen(fileinp,"r");
	if(FP==NULL) {
		if (MYID == 0){
			printf("\n==================================================================\n");
			printf(" Cannot open Denise input file %s \n",fileinp);
			printf("\n==================================================================\n\n");
			err(" --- ");
		}
	}

	/* read json formatted input file */
	read_par_json(stdout,fileinp);

	exchange_par();

	if (MYID == 0) note(stdout);

	
	/* open log-file (each PE is using different file) */
	/*	fp=stdout; */
	sprintf(ext,".%i",MYID);  
	strcat(LOG_FILE,ext);

	if ((MYID==0) && (LOG==1)) FP=stdout;
	else FP=fopen(LOG_FILE,"w");
	fprintf(FP," This is the log-file generated by PE %d \n\n",MYID);
		
	/* domain decomposition */
	initproc();

	NT=iround(TIME/DT);  	  /* number of timesteps */
	/*ns=iround(NT/NDT);*/           /* number of samples per trace */
	ns=NT;	/* in a FWI one has to keep all samples of the forward modeled data
		at the receiver positions to calculate the adjoint sources and to do 
		the backpropagation; look at function saveseis_glob.c to see that every
		NDT sample for the forward modeled wavefield is written to su files*/
	lsnap=iround(TSNAP1/DT);      /* first snapshot at this timestep */
	lsamp=NDT;


	/* output of parameters to log-file or stdout */
	if (MYID==0) write_par(FP);

		
	/* NXG, NYG denote size of the entire (global) grid */
	NXG=NX;
	NYG=NY;

	/* In the following, NX and NY denote size of the local grid ! */
	NX = IENDX;
	NY = IENDY;


	if (SEISMO){
207
208
209
210
211
		recpos=receiver(FP, &ntr);
		recswitch = ivector(1,ntr);
		recpos_loc = splitrec(recpos,&ntr_loc, ntr, recswitch);
		ntr_glob=ntr;
		ntr=ntr_loc;
Tilman Steinweg's avatar
Tilman Steinweg committed
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
	}

	/* memory allocation for abort criterion*/
	L2_hist = vector(1,1000);

	if(INV_STF) fulldata = matrix(1,ntr_glob,1,NT);

	/* estimate memory requirement of the variables in megabytes*/
		
	switch (SEISMO){
	case 1 : /* particle velocities only */
		nseismograms=2;	
		break;	
	case 2 : /* pressure only */
		nseismograms=1;	
		break;	
	case 3 : /* curl and div only */
		nseismograms=2;		
		break;	
	case 4 : /* everything */
		nseismograms=5;		
		break;
	case 5 : /* everything except curl and div */
		nseismograms=3;		
		break;
	}	
		
	/* use only every DTINV time sample for the inversion */
	/*DTINV=15;*/
	DTINV_help=ivector(1,NT);
	NTDTINV=ceil((float)NT/(float)DTINV);		/* round towards next higher integer value */

	/* save every IDXI and IDYI spatial point during the forward modelling */
	IDXI=1;
	IDYI=1;

	/*allocate memory for dynamic, static and buffer arrays */
	fac1=(NX+FDORDER)*(NY+FDORDER);
	fac2=sizeof(float)*pow(2.0,-20.0);

252
	nd = FDORDER/2;
Tilman Steinweg's avatar
Tilman Steinweg committed
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
	fdo3 = 2*nd;

	if (L){
		memdyn=(5.0+3.0*(float)L)*fac1*fac2;
		memmodel=(12.0+3.0*(float)L)*fac1*fac2;
		
	} else {
		memdyn=5.0*fac1*fac2;
		memmodel=6.0*fac1*fac2;
	}
	memseismograms=nseismograms*ntr*ns*fac2;

	memfwt=5.0*((NX/IDXI)+FDORDER)*((NY/IDYI)+FDORDER)*NTDTINV*fac2;
	memfwt1=20.0*NX*NY*fac2;
	memfwtdata=6.0*ntr*ns*fac2;

	membuffer=2.0*fdo3*(NY+NX)*fac2;
	buffsize=2.0*2.0*fdo3*(NX +NY)*sizeof(MPI_FLOAT);
	memtotal=memdyn+memmodel+memseismograms+memfwt+memfwt1+memfwtdata+membuffer+(buffsize*pow(2.0,-20.0));


	if (MYID==0){
		fprintf(FP,"\n **Message from main (printed by PE %d):\n",MYID);
		fprintf(FP," Size of local grids: NX=%d \t NY=%d\n",NX,NY);
		fprintf(FP," Each process is now trying to allocate memory for:\n");
		fprintf(FP," Dynamic variables: \t\t %6.2f MB\n", memdyn);
		fprintf(FP," Static variables: \t\t %6.2f MB\n", memmodel);
		fprintf(FP," Seismograms: \t\t\t %6.2f MB\n", memseismograms);
		fprintf(FP," Buffer arrays for grid exchange:%6.2f MB\n", membuffer);
		fprintf(FP," Network Buffer for MPI_Bsend: \t %6.2f MB\n", buffsize*pow(2.0,-20.0));
		fprintf(FP," ------------------------------------------------ \n");
		fprintf(FP," Total memory required: \t %6.2f MB.\n\n", memtotal);
	}


	/* allocate buffer for buffering messages */
	buff_addr=malloc(buffsize);
	if (!buff_addr) err("allocation failure for buffer for MPI_Bsend !");
	MPI_Buffer_attach(buff_addr,buffsize);

	/* allocation for request and status arrays */
	req_send=(MPI_Request *)malloc(REQUEST_COUNT*sizeof(MPI_Request));
	req_rec=(MPI_Request *)malloc(REQUEST_COUNT*sizeof(MPI_Request));
	send_statuses=(MPI_Status *)malloc(REQUEST_COUNT*sizeof(MPI_Status));
	rec_statuses=(MPI_Status *)malloc(REQUEST_COUNT*sizeof(MPI_Status));


	/* memory allocation for dynamic (wavefield) arrays */
	if(!ACOUSTIC){
		psxx =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		psxy =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		psyy =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	}else{
		psp  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	}
	pvx  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	pvy  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	pvxp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	pvyp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	pvxm1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	pvym1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	if(!ACOUSTIC){
		ux   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		uy   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		uxy  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		uyx  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		uttx   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		utty   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	}
	Vp0  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	if(!ACOUSTIC)
		Vs0  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	Rho0  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		
	/* memory allocation for static (model) arrays */
	prho =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	prhonp1 =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	prip =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	prjp =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	pripnp1 =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	prjpnp1 =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	ppi  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	ppinp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	if(!ACOUSTIC){
		pu   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		punp1   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		puipjp   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
	}
	vpmat   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);

	if (L) {
		/* dynamic (wavefield) arrays for viscoelastic modeling */
		pr = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
		pp = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
		pq = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
		/* memory allocation for static arrays for viscoelastic modeling */
		dip = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
		d =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
		e =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
		ptaus =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		ptausipjp =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		ptaup =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		fipjp =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		f =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		g =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		peta =  vector(1,L);
		etaip =  vector(1,L);
		etajm =  vector(1,L);
		bip =  vector(1,L);
		bjm =  vector(1,L);
		cip =  vector(1,L);
		cjm =  vector(1,L);
	}

	/*nf=4;
	nfstart=4;*/

	NTST=20;
	NTSTI=NTST/DTINV;

	nxny=NX*NY;
	nxnyi=(NX/IDXI)*(NY/IDYI);

	/* Parameters for step length calculations */
	stepmax = STEPMAX; /* number of maximum misfit calculations/steplength 2/3*/
	scalefac = SCALEFAC; /* scale factor for the step length */

	if(INVMAT==0){
		waveconv = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		waveconv_lam = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		waveconv_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);

		waveconvtmp = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		wcpart = matrix(1,3,1,3);
		wavejac = matrix(-nd+1,NY+nd,-nd+1,NX+nd);

		if(!ACOUSTIC){
			forward_prop_x =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
			// forward_prop_x =  vector(1,nxnyi*(NTDTINV));
392
			forward_prop_y =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
Tilman Steinweg's avatar
Tilman Steinweg committed
393
394
395
396
			// forward_prop_y =  vector(1,nxnyi*(NTDTINV));
		}else{
			forward_prop_p =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
		}
397
		
Tilman Steinweg's avatar
Tilman Steinweg committed
398
399
400
401
402
		gradg = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		gradp = matrix(-nd+1,NY+nd,-nd+1,NX+nd);

		forward_prop_rho_x =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
		// forward_prop_rho_x =  vector(1,nxnyi*(NTDTINV));
403
		forward_prop_rho_y =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
Tilman Steinweg's avatar
Tilman Steinweg committed
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
		// forward_prop_rho_y =  vector(1,nxnyi*(NTDTINV));

		gradg_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		gradp_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		waveconv_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		waveconv_rho_s = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		waveconv_rho_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);

		if(!ACOUSTIC){
			forward_prop_u =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
			// forward_prop_u =  vector(1,nxnyi*(NTDTINV));

			gradg_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			gradp_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			waveconv_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			waveconv_mu = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			waveconv_u_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		}
		/* Variables for the L-BFGS method */
		/*if(GRAD_METHOD==2){
		NLBFGS = 200;
		y_LBFGS_vp  =  vector(1,nxnyi*NLBFGS);
		s_LBFGS_vp  =  vector(1,nxnyi*NLBFGS);
		rho_LBFGS = vector(1,NLBFGS);
		alpha_LBFGS = vector(1,NLBFGS);

		y_LBFGS_vs  =  vector(1,nxnyi*NLBFGS);
		s_LBFGS_vs  =  vector(1,nxnyi*NLBFGS);

		y_LBFGS_rho  =  vector(1,nxnyi*NLBFGS);
		s_LBFGS_rho  =  vector(1,nxnyi*NLBFGS);
		}*/

		if(GRAD_METHOD==3){
			NLBFGS = 200;
			y_LBFGS_vp  =  f3tensor(1,NY,1,NX,1,NLBFGS);
			s_LBFGS_vp  =  f3tensor(1,NY,1,NX,1,NLBFGS);

			y_LBFGS_vs  =  f3tensor(1,NY,1,NX,1,NLBFGS);
			s_LBFGS_vs  =  f3tensor(1,NY,1,NX,1,NLBFGS);

			y_LBFGS_rho  =  f3tensor(1,NY,1,NX,1,NLBFGS);
			s_LBFGS_rho  =  f3tensor(1,NY,1,NX,1,NLBFGS);

			rho_LBFGS_vp = vector(1,NLBFGS);
			rho_LBFGS_vs = vector(1,NLBFGS);
			alpha_LBFGS_vp = vector(1,NLBFGS);
			alpha_LBFGS_vs = vector(1,NLBFGS);
		}
		if(HESSIAN){
			jac_rho =  vector(1,nxnyi*(NTDTINV));
			jac_u =  vector(1,nxnyi*(NTDTINV));
			jac_lam_x =  vector(1,nxnyi*(NTDTINV));
			jac_lam_y =  vector(1,nxnyi*(NTDTINV));
			temp_TS =  vector(1,NTDTINV);
			temp_TS1 =  vector(1,NTDTINV);
			temp_TS2 =  vector(1,NTDTINV);
			temp_TS3 =  vector(1,NTDTINV);
			temp_TS4 =  vector(1,NTDTINV);
			temp_TS5 =  vector(1,NTDTINV);
			temp_conv =  vector(1,NTDTINV);
			temp_conv1 =  vector(1,NTDTINV);
			temp_conv2 =  vector(1,NTDTINV);
			hessian = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			hessian_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			hessian_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			hessian_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			hessian_u_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
			hessian_rho_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
		}
	}


	if(FW>0){
		d_x = vector(1,2*FW);
		K_x = vector(1,2*FW);
		alpha_prime_x = vector(1,2*FW);
		a_x = vector(1,2*FW);
		b_x = vector(1,2*FW);
		
		d_x_half = vector(1,2*FW);
		K_x_half = vector(1,2*FW);
		alpha_prime_x_half = vector(1,2*FW);
		a_x_half = vector(1,2*FW);
		b_x_half = vector(1,2*FW);

		d_y = vector(1,2*FW);
		K_y = vector(1,2*FW);
		alpha_prime_y = vector(1,2*FW);
		a_y = vector(1,2*FW);
		b_y = vector(1,2*FW);
		
		d_y_half = vector(1,2*FW);
		K_y_half = vector(1,2*FW);
		alpha_prime_y_half = vector(1,2*FW);
		a_y_half = vector(1,2*FW);
		b_y_half = vector(1,2*FW);

		psi_sxx_x =  matrix(1,NY,1,2*FW); 
		psi_syy_y =  matrix(1,2*FW,1,NX);
		psi_sxy_y =  matrix(1,2*FW,1,NX);
		psi_sxy_x =  matrix(1,NY,1,2*FW);
		psi_vxx   =  matrix(1,NY,1,2*FW);
		psi_vxxs  =  matrix(1,NY,1,2*FW); 
		psi_vyy   =  matrix(1,2*FW,1,NX);
		psi_vxy   =  matrix(1,2*FW,1,NX);
		psi_vyx   =  matrix(1,NY,1,2*FW);
	}

	taper_coeff=  matrix(1,NY,1,NX);


	/* memory allocation for buffer arrays in which the wavefield
		information which is exchanged between neighbouring PEs is stored */
	bufferlef_to_rig = matrix(1,NY,1,fdo3);
	bufferrig_to_lef = matrix(1,NY,1,fdo3);
	buffertop_to_bot = matrix(1,NX,1,fdo3);
	bufferbot_to_top = matrix(1,NX,1,fdo3);
522
523
524
525
526
527
528
529
530
	
	/* In exchange_p for acousic simulation only one variable (sp) will be exchanged, 
	therfore not fdo3=2*nd but 1*nd is enough buffer*/
	if (ACOUSTIC) {
	p_bufferlef_to_rig = matrix(1,NY,1,nd);
        p_bufferrig_to_lef = matrix(1,NY,1,nd);
        p_buffertop_to_bot = matrix(1,NX,1,nd);
        p_bufferbot_to_top = matrix(1,NX,1,nd);
	}
Tilman Steinweg's avatar
Tilman Steinweg committed
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

	switch (SEISMO){
		case 1 : /* particle velocities only */
			fulldata_vx = matrix(1,ntr_glob,1,NT);
			fulldata_vy = matrix(1,ntr_glob,1,NT);	
			break;	
		case 2 : /* pressure only */
			fulldata_p = matrix(1,ntr_glob,1,NT);
			break;	
		case 3 : /* curl and div only */
			fulldata_div = matrix(1,ntr_glob,1,NT);
			fulldata_curl = matrix(1,ntr_glob,1,NT);
			break;	
		case 4 : /* everything */
			fulldata_vx = matrix(1,ntr_glob,1,NT);
			fulldata_vy = matrix(1,ntr_glob,1,NT);
			fulldata_p = matrix(1,ntr_glob,1,NT);
			fulldata_div = matrix(1,ntr_glob,1,NT);
			fulldata_curl = matrix(1,ntr_glob,1,NT);
			break;	
		case 5 : /* everything except curl and div*/
			fulldata_vx = matrix(1,ntr_glob,1,NT);
			fulldata_vy = matrix(1,ntr_glob,1,NT);
			fulldata_p = matrix(1,ntr_glob,1,NT);
			break;	
		
	}

	if (ntr>0){
		switch (SEISMO){
		case 1 : /* particle velocities only */
			sectionvx=matrix(1,ntr,1,ns);
			sectionvy=matrix(1,ntr,1,ns);
			break;	
		case 2 : /* pressure only */
			sectionp=matrix(1,ntr,1,ns);
			sectionpnp1=matrix(1,ntr,1,ns);
			sectionpn=matrix(1,ntr,1,ns);
			break;	
		case 3 : /* curl and div only */
			sectioncurl=matrix(1,ntr,1,ns);
			sectiondiv=matrix(1,ntr,1,ns);	
			break;	
		case 4 : /* everything */
			sectionvx=matrix(1,ntr,1,ns);
			sectionvy=matrix(1,ntr,1,ns);	
			sectioncurl=matrix(1,ntr,1,ns);
			sectiondiv=matrix(1,ntr,1,ns);		
			sectionp=matrix(1,ntr,1,ns);
			break;	
		case 5 : /* everything except curl and div*/
			sectionvx=matrix(1,ntr,1,ns);
			sectionvy=matrix(1,ntr,1,ns);
			sectionp=matrix(1,ntr,1,ns);
			break;	
		}
	}	

	/* Memory for seismic data */
	sectionread=matrix(1,ntr_glob,1,ns);
591
	sectionvxdata=matrix(1,ntr,1,ns);
Tilman Steinweg's avatar
Tilman Steinweg committed
592
	sectionvxdiff=matrix(1,ntr,1,ns);
593
	sectionvxdiffold=matrix(1,ntr,1,ns);
Tilman Steinweg's avatar
Tilman Steinweg committed
594
595
596
	sectionvydata=matrix(1,ntr,1,ns);
	sectionvydiff=matrix(1,ntr,1,ns);
	sectionvydiffold=matrix(1,ntr,1,ns);
597
598
	sectionpdata=matrix(1,ntr,1,ns);
	sectionpdiff=matrix(1,ntr,1,ns);
Tilman Steinweg's avatar
Tilman Steinweg committed
599
600
	sectionpdiffold=matrix(1,ntr,1,ns);

601
	if((INV_STF==1)||(TIME_FILT==1)||(TIME_FILT==2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
602
603
604
		/* Memory for inversion for source time function */
		sectionvx_conv=matrix(1,ntr_glob,1,NT);
		sectionvx_obs=matrix(1,ntr_glob,1,NT);
605
606
		sectionvy_conv=matrix(1,ntr_glob,1,NT);
		sectionvy_obs=matrix(1,ntr_glob,1,NT);
Tilman Steinweg's avatar
Tilman Steinweg committed
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
		sectionp_conv=matrix(1,ntr_glob,1,NT);
		sectionp_obs=matrix(1,ntr_glob,1,NT);
		source_time_function = vector(1,NT);
	}
	/* memory for source position definition */
	srcpos1=fmatrix(1,8,1,1);

	/* memory of L2 norm */
	L2t = vector(1,4);
	epst1 = vector(1,3);
	epst2 = vector(1,3);
	epst3 = vector(1,3);
	picked_times = vector(1,ntr);
		
	fprintf(FP," ... memory allocation for PE %d was successfull.\n\n", MYID);

			
	/* Holberg coefficients for FD operators*/
	hc = holbergcoeff();

	MPI_Barrier(MPI_COMM_WORLD);

	/* Reading source positions from SOURCE_FILE */ 	
	srcpos=sources(&nsrc);
	nsrc_glob=nsrc;


	/* create model grids */

	if(L){
637
638
639
640
641
642
643
644
645
		if(!ACOUSTIC){
			if (READMOD){ readmod(prho,ppi,pu,ptaus,ptaup,peta);
			}else{ model(prho,ppi,pu,ptaus,ptaup,peta);
			}
		}else{
			if (READMOD){ readmod_viscac(prho,ppi,ptaup,peta);
			}else{ model_viscac(prho,ppi,ptaup,peta);
			}
		}
Tilman Steinweg's avatar
Tilman Steinweg committed
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
	} else{
		if(!ACOUSTIC){
			if (READMOD){ readmod_elastic(prho,ppi,pu);
			}else{ model_elastic(prho,ppi,pu);
			}
		}else{
			if (READMOD){ readmod_acoustic(prho,ppi);
			}else{ model_acoustic(prho,ppi);
			}
		}
	}

	/* check if the FD run will be stable and free of numerical dispersion */
	checkfd(FP, prho, ppi, pu, ptaus, ptaup, peta, hc, srcpos, nsrc, recpos, ntr_glob);

	/*if(L){
		checkfd_ssg_visc(FP,prho,ppi,pu,ptaus,ptaup,peta,hc);
	} else{
		checkfd_ssg_elastic(FP,prho,ppi,pu,hc);
	}
	*/

	/* calculate damping coefficients for CPMLs*/
	if(FW>0)
		PML_pro(d_x, K_x, alpha_prime_x, a_x, b_x, d_x_half, K_x_half, alpha_prime_x_half, a_x_half, b_x_half, d_y, K_y, alpha_prime_y, a_y, b_y, d_y_half, K_y_half, alpha_prime_y_half, a_y_half, b_y_half);

	MPI_Barrier(MPI_COMM_WORLD);

	if (CHECKPTREAD){
		if (MYID==0){
			time3=MPI_Wtime();
			fprintf(FP," Reading wavefield from check-point file %s \n",CHECKPTFILE);	
		}
		
		read_checkpoint(-1, NX+2, -1, NY+2, pvx, pvy, psxx, psyy, psxy);
		MPI_Barrier(MPI_COMM_WORLD);
		if (MYID==0){
			time4=MPI_Wtime();
			fprintf(FP," finished (real time: %4.2f s).\n",time4-time3);
		}
	}
	

	/* comunication initialisation for persistent communication */
	/*comm_ini(bufferlef_to_rig, bufferrig_to_lef, buffertop_to_bot, bufferbot_to_top, req_send, req_rec);*/

	snapseis=1;
	snapseis1=1;
	SHOTINC=1;
	RECINC=1;

	switch(TIME_FILT){
		case 1: FC=FC_START; break;
		/*read frequencies from file*/
		case 2: FC_EXT=filter_frequencies(&nfrq); FC=FC_EXT[FREQ_NR]; break;
	}

	QUELLART_OLD = QUELLART;

	for(iter=1;iter<=ITERMAX;iter++){  /* fullwaveform iteration loop */	

		if (MYID==0){
			time2=MPI_Wtime();
			fprintf(FP,"\n\n\n ------------------------------------------------------------------\n");
			fprintf(FP,"\n\n\n                   TDFWI ITERATION %d \t of %d \n",iter,ITERMAX);
			fprintf(FP,"\n\n\n ------------------------------------------------------------------\n");
		}


		/* For the calculation of the material parameters between gridpoints
		they have to be averaged. For this, values lying at 0 and NX+1,
		for example, are required on the local grid. These are now copied from the
		neighbouring grids */		
		if (L){
720
721
722
723
724
			if(!ACOUSTIC){
				matcopy(prho,ppi,pu,ptaus,ptaup);
			}else{
				matcopy_viscac(prho,ppi,ptaup);
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
		}else{
			if(!ACOUSTIC){
				matcopy_elastic(prho, ppi, pu);
			}else{
				matcopy_acoustic(prho, ppi);
			}
		}

		MPI_Barrier(MPI_COMM_WORLD);

		/* MPI split for processors with ntr>0 */
		int myid_ntr, group_id=0, groupsize;
		MPI_Comm	MPI_COMM_NTR;

		if (ntr) group_id = 1;
		else group_id = 0;
		MPI_Comm_split(MPI_COMM_WORLD, group_id, MYID, &MPI_COMM_NTR);
		MPI_Comm_rank(MPI_COMM_NTR, &myid_ntr);
		/* end of MPI split for processors with ntr>0 */


		if(!ACOUSTIC) av_mue(pu,puipjp,prho);
		av_rho(prho,prip,prjp);
748
		if (!ACOUSTIC && L) av_tau(ptaus,ptausipjp);
Tilman Steinweg's avatar
Tilman Steinweg committed
749
750
751


		/* Preparing memory variables for update_s (viscoelastic) */
752
753
754
755
756
757
758
		if (L){
			if(!ACOUSTIC){
				prepare_update_s(etajm,etaip,peta,fipjp,pu,puipjp,ppi,prho,ptaus,ptaup,ptausipjp,f,g,bip,bjm,cip,cjm,dip,d,e);
			}else{
				prepare_update_p(etajm,peta,ppi,prho,ptaup,g,bjm,cjm,e);
			}
		}
Tilman Steinweg's avatar
Tilman Steinweg committed
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

		if(iter==1){
			for (i=1;i<=NX;i=i+IDX){ 
			for (j=1;j<=NY;j=j+IDY){
			
				if(INVMAT1==1){
				
				Vp0[j][i] = ppi[j][i];
				if(!ACOUSTIC) Vs0[j][i] = pu[j][i];
				Rho0[j][i] = prho[j][i];}
				
					
					
				if(INVMAT1==2){
				
				Vp0[j][i] = sqrt((ppi[j][i]+2.0*pu[j][i])*prho[j][i]);
				Vs0[j][i] = sqrt((pu[j][i])*prho[j][i]);
				Rho0[j][i] = prho[j][i];
				
				}
				
				if(INVMAT1==3){
				
				Vp0[j][i] = ppi[j][i];
				Vs0[j][i] = pu[j][i];
				Rho0[j][i] = prho[j][i];
				
				}
			
			}
			}

791
792
793
			/* ----------------------------- */
			/* calculate Covariance matrices */
			/* ----------------------------- */
Tilman Steinweg's avatar
Tilman Steinweg committed
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

			Lcount = 1;
			Vp_avg = 0.0;
			if(!ACOUSTIC) Vs_avg = 0.0;
			rho_avg = 0.0;
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
			
				/* calculate average Vp, Vs */
				Vp_avg+=ppi[j][i];
				if(!ACOUSTIC)  Vs_avg+=pu[j][i];
				/* calculate average rho */
				rho_avg+=prho[j][i];
				
				Lcount++;
			}
			}
				
			/* calculate average Vp, Vs and rho of all CPUs*/
			Lcountsum = 0.0;
			MPI_Allreduce(&Lcount,&Lcountsum,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
			Lcount=Lcountsum;
			
			Vp_sum = 0.0;
			MPI_Allreduce(&Vp_avg,&Vp_sum,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			Vp_avg=Vp_sum;
			
			if(!ACOUSTIC){
				Vs_sum = 0.0;
				MPI_Allreduce(&Vs_avg,&Vs_sum,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
				Vs_avg=Vs_sum;
			}
			
			rho_sum = 0.0;
			MPI_Allreduce(&rho_avg,&rho_sum,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			rho_avg=rho_sum;
			
			Vp_avg /=Lcount; 
			if(!ACOUSTIC) Vs_avg /=Lcount; 
			rho_avg /=Lcount;
			
			if(!ACOUSTIC) printf("MYID = %d \t Vp_avg = %e \t Vs_avg = %e \t rho_avg = %e \n ",MYID,Vp_avg,Vs_avg,rho_avg);
			else printf("MYID = %d \t Vp_avg = %e \t rho_avg = %e \n ",MYID,Vp_avg,rho_avg);
			
			C_vp = Vp_avg*Vp_avg;
			if(!ACOUSTIC) C_vs = Vs_avg*Vs_avg;
			C_rho = rho_avg*rho_avg;
		}

		/* Open Log File for L2 norm */

		if(INVMAT!=10){
			if(MYID==0){
				if(iter==1){FPL2=fopen(MISFIT_LOG_FILE,"w");}
				if(iter>1){FPL2=fopen(MISFIT_LOG_FILE,"a");}
			}
		}

		/* initialization of L2 calculation */
		L2=0.0;
		Lcount=0;
		energy=0.0;
		L2_all_shots=0.0;
		energy_all_shots=0.0;
		killed_traces=0; 
		killed_traces_testshots=0;


		EPSILON=0.0;  /* test step length */
		exchange_par();

		/* initialize waveconv matrix*/
		if(INVMAT==0){
			for (i=1;i<=NX;i=i+IDX){ 
			for (j=1;j<=NY;j=j+IDY){
				waveconv[j][i]=0.0;    
			}
			}
			
			if(!ACOUSTIC){
				for (i=1;i<=NX;i=i+IDX){ 
				for (j=1;j<=NY;j=j+IDY){
					waveconv_u[j][i]=0.0;   
				}
				}
			}

			for (i=1;i<=NX;i=i+IDX){ 
			for (j=1;j<=NY;j=j+IDY){
				waveconv_rho[j][i]=0.0;
			
			}
			}
		}

		if(HESSIAN){
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				hessian[j][i]=0.0;
				hessian_u[j][i]=0.0;
				hessian_rho[j][i]=0.0;
			}
			}
		}

		if(HESSIAN && TRKILL){ /* reading trace kill information */
			kill_tmp = imatrix(1,ntr_glob,1,nsrc_glob);
			
			ftracekill=fopen(TRKILL_FILE,"r");
			if (ftracekill==NULL) err(" Trace kill file could not be opened!");
			for(i=1;i<=ntr_glob;i++){
			for(j=1;j<=nsrc_glob;j++){
				fscanf(ftracekill,"%d",&kill_tmp[i][j]);
			}
			}
			fclose(ftracekill);
		} /* end if(TRKILL)*/


		itestshot=TESTSHOT_START;
		swstestshot=0;

		if(INVTYPE==2){ 
			if (RUN_MULTIPLE_SHOTS) nshots=nsrc; else nshots=1;
			
			for (ishot=1;ishot<=nshots;ishot+=SHOTINC){
				/*for (ishot=1;ishot<=1;ishot+=1){*/
				QUELLART = QUELLART_OLD;
				if((INV_STF==1)&&((iter==1)||(s==1))){
					fprintf(FP,"\n==================================================================================\n");
					fprintf(FP,"\n MYID=%d *****  Forward simulation for inversion of source time function ******** \n",MYID);
					fprintf(FP,"\n MYID=%d *****  Starting simulation (forward model) for shot %d of %d  ********** \n",MYID,ishot,nshots);
					fprintf(FP,"\n==================================================================================\n\n");
						
					for (nt=1;nt<=8;nt++) srcpos1[nt][1]=srcpos[nt][ishot]; 
					
					if (RUN_MULTIPLE_SHOTS){
						/* find this single source positions on subdomains */
						if (nsrc_loc>0) free_matrix(srcpos_loc,1,8,1,1);
						srcpos_loc=splitsrc(srcpos1,&nsrc_loc, 1);
					}else{
						/* Distribute multiple source positions on subdomains */
						srcpos_loc = splitsrc(srcpos,&nsrc_loc, nsrc);
					}

					if((QUELLART==7)||(QUELLART==3))err("QUELLART==7 or QUELLART==3 isn't possible with INV_STF==1");
					MPI_Barrier(MPI_COMM_WORLD);
					/* calculate wavelet for each source point */
					signals=NULL;
					signals=wavelet(srcpos_loc,nsrc_loc,ishot);
					
946
					
Tilman Steinweg's avatar
Tilman Steinweg committed
947
948
					/* initialize wavefield with zero */
					if (L){
949
950
951
952
						if(!ACOUSTIC)
							zero_fdveps_visc(-nd+1, NY+nd, -nd+1, NX+nd, pvx, pvy, psxx, psyy, psxy, ux, uy, uxy, pvxp1, pvyp1, psi_sxx_x, psi_sxy_x, psi_vxx, psi_vyx, psi_syy_y, psi_sxy_y, psi_vyy, psi_vxy, psi_vxxs, pr, pp, pq);
						else
							zero_fdveps_viscac(-nd+1, NY+nd, -nd+1, NX+nd, pvx, pvy, psp, pvxp1, pvyp1, psi_sxx_x, psi_sxy_x, psi_vxx, psi_vyx, psi_syy_y, psi_sxy_y, psi_vyy, psi_vxy, psi_vxxs, pp);
Tilman Steinweg's avatar
Tilman Steinweg committed
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
					}else{
						if(!ACOUSTIC)
							zero_fdveps(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,psxx,psyy,psxy,ux,uy,uxy,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_vxx,psi_vyx,psi_syy_y,psi_sxy_y,psi_vyy,psi_vxy,psi_vxxs);
						else
							zero_fdveps_ac(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,psp,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_vxx,psi_vyx,psi_syy_y,psi_sxy_y,psi_vyy,psi_vxy,psi_vxxs);
					}
					
					
					/*----------------------  loop over timesteps (forward model) ------------------*/

					lsnap=iround(TSNAP1/DT);  
					lsamp=NDT;
					nsnap=0;

					hin=1;
					hin1=1;

					imat=1;
					imat1=1;
					imat2=1;
					hi=1;

					for (nt=1;nt<=NT;nt++){
						
						/* Check if simulation is still stable */
						if (isnan(pvy[NY/2][NX/2])) {
							fprintf(FP,"\n Time step: %d; pvy: %f \n",nt,pvy[NY/2][NX/2]);
							err(" Simulation is unstable !");}

						
						infoout = !(nt%10000);

						if (MYID==0){
							if (infoout)  fprintf(FP,"\n Computing timestep %d of %d \n",nt,NT);
							time3=MPI_Wtime();
						}

						/* update of particle velocities */
						/*update_v_hc(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, uttx, utty, psxx, psyy, psxy, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,2);*/ 
						
						if(!ACOUSTIC)
							update_v_PML(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, uttx, utty, psxx, psyy, psxy, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,0, K_x, a_x,b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_sxx_x, psi_syy_y, psi_sxy_y, psi_sxy_x);
						else
							update_v_acoustic_PML(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, psp, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,0, K_x_half, a_x_half, b_x_half, K_y_half, a_y_half, b_y_half, psi_sxx_x, psi_syy_y);

						if (MYID==0){
							time4=MPI_Wtime();
							time_av_v_update+=(time4-time3);
For faster browsing, not all history is shown. View entire blame