checkfd.c 27.4 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
/*------------------------------------------------------------------------
 * Copyright (C) 2011 For the list of authors, see file AUTHORS.
 *
 * This file is part of SOFI2D.
 *
 * SOFI2D 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.
 *
 * SOFI2D 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 SOFI2D. See file COPYING and/or
  * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/
/*-------------------------------------------------------------
 *  Check FD-Grid for stability and grid dispersion.
 *  If the stability criterion is not fulfilled the program will
 *  terminate.
 *
 *  ----------------------------------------------------------*/

#include <limits.h>

#include "fd.h"

Tilman Steinweg's avatar
Tilman Steinweg committed
30
31
void checkfd(FILE *fp, float **prho, float **ppi, float **pu,
             float **ptaus, float **ptaup, float *peta, float *hc, float **srcpos, int nsrc, int **recpos, int ntr) {
Tilman Steinweg's avatar
Tilman Steinweg committed
32
33
34
35
36


	extern float DH, DT, TS, TIME, TSNAP2;
	extern float XREC1, XREC2, YREC1, YREC2;
	extern int NX, NY, L, MYID, IDX, IDY, NT, NDT, RSG;
37
	extern int READREC, NPROCX,NPROCY, SRCREC, FREE_SURF, ABS_TYPE, FW, BOUNDARY,FDORDER_TIME;
Tilman Steinweg's avatar
Tilman Steinweg committed
38
39
40
41
42
43
44
	extern int SNAP, SEISMO, CHECKPTREAD, CHECKPTWRITE, SEIS_FORMAT[6], SNAP_FORMAT, POS[4];
	extern char SEIS_FILE[STRING_SIZE], CHECKPTFILE[STRING_SIZE], SNAP_FILE[STRING_SIZE];
	extern char SOURCE_FILE[STRING_SIZE], REC_FILE[STRING_SIZE];

	/* local variables */

	float  c, cmax_p=0.0, cmin_p=1e9, cmax_s=0.0, cmin_s=1e9,  cwater=1.0, fmax, gamma;
45
	float  cmax=0.0, cmin=1e9, sum, dtstab, dhstab, ts, cmax_r, cmin_r, temporal;
Tilman Steinweg's avatar
Tilman Steinweg committed
46
47
48
	float snapoutx=0.0, snapouty=0.0;
	float srec_minx=DH*NX*NPROCX+1, srec_miny=DH*NY*NPROCY+1;
	float srec_maxx=-1.0, srec_maxy=-1.0;
Tilman Steinweg's avatar
Tilman Steinweg committed
49
	float CFL;
Tilman Steinweg's avatar
Tilman Steinweg committed
50
51
52
53
54
55
56
57
58
59
60
	const float w=2.0*PI/TS; /*center frequency of source*/

	int i, j, k, l, ny1=1, nx, ny, myidcounter, nfw;

	char xfile[STRING_SIZE], errormessage[STRING_SIZE], xmod[4], file_ext[8];
	FILE *fpcheck;


	nx=NX;
	ny=NY;

Tilman Steinweg's avatar
Tilman Steinweg committed
61
62
63
64
65
	fprintf(fp,"\n **********************************************************");
	fprintf(fp,"\n ************ CHECKS OF INPUT FILE PARAMETERS  ************");
	fprintf(fp,"\n **********************************************************\n\n");
	fprintf(fp,"\n **Message from checkfd_ssg (printed by PE %d):\n",MYID);
	fprintf(fp,"\n\n ------------------ CHECK OUTPUT FILES --------------------------\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
66
67
68
69
70
71
72
73
74
	/* The original checks might delete files accidentally that would not be overwritten anyway.
	   and did not test accessibility from all CPUs which may be vary, especially in distributed clusters */

	/*Checking SNAP Output */
	/*-------------------- */
	/*-------------------------------------- */
	/* only MYID is performing the file checks, if to many PEs try to do it simultaneously,
	 * the code and/or FILESYSTEM and/or MPI implementation will cause segmentation faults
	 */
Tilman Steinweg's avatar
Tilman Steinweg committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	if ((SNAP>0) && (MYID==0)) {

		switch (SNAP_FORMAT) {
			case 1:
				sprintf(file_ext, ".su");
				strcpy(xmod, "ab");
				break;

			case 2:
				sprintf(file_ext, ".asc");
				strcpy(xmod, "a");
				break;

			case 3:
				sprintf(file_ext, ".bin");
				strcpy(xmod, "ab");
				break;

			default:
				declare_error(" Sorry. Snapshot format (SNAP_FORMAT) unknown. \n");
				break;
Tilman Steinweg's avatar
Tilman Steinweg committed
96
97
98
		}


Tilman Steinweg's avatar
Tilman Steinweg committed
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
		fprintf(fp," Check accessibility for snapshot files ... \n");

		switch (SNAP) {
			case 1 :
				sprintf(xfile,"%s%s.vx.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0  cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				sprintf(xfile,"%s%s.vy.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				break;

			case 2 :
				sprintf(xfile,"%s%s.p.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				break;

			case 4 :
				sprintf(xfile,"%s%s.vx.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				sprintf(xfile,"%s%s.vy.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				sprintf(xfile,"%s%s.p.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

			case 3 :
				sprintf(xfile,"%s%s.div.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				sprintf(xfile,"%s%s.curl.%i.%i",SNAP_FILE,file_ext,POS[1],POS[2]);
				fprintf(fp,"    Check accessibility for snapshot file %s... \n",xfile);

				if ((fpcheck=fopen(xfile,xmod)) ==NULL) err2(" PE0 cannot write snapshots to %s!",xfile);

				else fclose(fpcheck);

				break;
Tilman Steinweg's avatar
Tilman Steinweg committed
167
168
169
170
171
172
173
174
175
		}
	}


	/*Checking SEISMOGRAM Output Particle velocities */
	/*-------------------------------------- */
	/* only MYID is performing the file checks, if to many PEs try to do it simultaneously,
	 * the code and/or FILESYSTEM and/or MPI implementation will cause segmentation faults
	 */
Tilman Steinweg's avatar
Tilman Steinweg committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
	if ((SEISMO>0) && (MYID==0)) {

		fprintf(fp," Check accessibility for seismogram files of each PE ... \n");
		fprintf(fp," However, the list below refers only to PE0  ... \n");

		switch (SEIS_FORMAT[0]) {
			case 1:
				sprintf(file_ext,"su");
				break;

			case 2:
				sprintf(file_ext,"txt");
				break;

			case 3:
				sprintf(file_ext,"bin");
				break;
Tilman Steinweg's avatar
Tilman Steinweg committed
193
194
		}

Tilman Steinweg's avatar
Tilman Steinweg committed
195
196
197
		if (SEIS_FORMAT[0]==2) strcpy(xmod,"a");

		else strcpy(xmod,"w+b");
Tilman Steinweg's avatar
Tilman Steinweg committed
198
199
200
201

		/*MYID=0 is checking if all seismogram file written by other PEs can be written
		 * assuming that all PEs can address the files ystem equally
		 */
Tilman Steinweg's avatar
Tilman Steinweg committed
202
		for (myidcounter=0; myidcounter< (NPROCX*NPROCY); myidcounter++) {
Tilman Steinweg's avatar
Tilman Steinweg committed
203

Tilman Steinweg's avatar
Tilman Steinweg committed
204
205
206
			switch (SEISMO) {
				case 1: /* particle velocities only */
					sprintf(xfile,"%s_vx.%s.%d",SEIS_FILE,file_ext,myidcounter);
Tilman Steinweg's avatar
Tilman Steinweg committed
207

Tilman Steinweg's avatar
Tilman Steinweg committed
208
209
					/*in case of number of PE's=500, there will be 500 messages, too many to be displayed! */
					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);
Tilman Steinweg's avatar
Tilman Steinweg committed
210

Tilman Steinweg's avatar
Tilman Steinweg committed
211
					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);
Tilman Steinweg's avatar
Tilman Steinweg committed
212

Tilman Steinweg's avatar
Tilman Steinweg committed
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
					//if (access(xfile,W_OK|X_OK)==-1) declare_error(errormessage);
					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					sprintf(xfile,"%s_vy.%s.%d",SEIS_FILE,file_ext,myidcounter);

					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					break;

				case 2 : /* pressure only */
					sprintf(xfile,"%s_p.%s.%d",SEIS_FILE,file_ext,myidcounter);

					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					break;

				case 4 : /* everything */
					sprintf(xfile,"%s_vx.%s.%d",SEIS_FILE,file_ext,myidcounter);

					/*in case of number of PE's=500, there will be 500 messages, too many to be displayed! */
					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					//if (access(xfile,W_OK|X_OK)==-1) declare_error(errormessage);
					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					sprintf(xfile,"%s_vy.%s.%d",SEIS_FILE,file_ext,myidcounter);

					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					//if (access(xfile,W_OK|X_OK)==-1) declare_error(errormessage);
					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					sprintf(xfile,"%s_p.%s.%d",SEIS_FILE,file_ext,myidcounter);

					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

				case 3 : /* curl and div only */
					sprintf(xfile,"%s_div.%s.%d",SEIS_FILE,file_ext,myidcounter);

					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					sprintf(xfile,"%s_curl.%s.%d",SEIS_FILE,file_ext,myidcounter);

					if (MYID==myidcounter) fprintf(fp,"    Check accessibility for seismogram file %s... \n",xfile);

					sprintf(errormessage,"PE %i cannot write seismogram file %s!",MYID,xfile);

					if ((fpcheck=fopen(xfile,xmod)) ==NULL) declare_error(errormessage);

					else fclose(fpcheck);

					remove(xfile);

					break;
Tilman Steinweg's avatar
Tilman Steinweg committed
315
316
317
318
319
320
321

			}
		}
	}

	/*Checking CHECKPOINT Output */
	/*-------------------------- */
Tilman Steinweg's avatar
Tilman Steinweg committed
322
323
324
325
326
327
328
329
	if (CHECKPTREAD>0) {
		strcpy(xmod,"rb");
		sprintf(xfile,"%s.%d",CHECKPTFILE,MYID);
		fprintf(fp," Check readability for checkpoint files %s... \n",xfile);

		if (((fpcheck=fopen(xfile,xmod)) ==NULL) && (MYID==0)) declare_error(" PE 0 cannot read checkpoints!");

		else fclose(fpcheck);
Tilman Steinweg's avatar
Tilman Steinweg committed
330
	}
Tilman Steinweg's avatar
Tilman Steinweg committed
331
332
333
334
335
336
337
338
339

	if ((CHECKPTWRITE>0)) {
		strcpy(xmod,"ab");
		sprintf(xfile,"%s.%d",CHECKPTFILE,MYID);
		fprintf(fp," Check writability for checkpoint files %s... \n",xfile);

		if (((fpcheck=fopen(xfile,xmod)) ==NULL) && (MYID==0)) declare_error(" PE 0 cannot write checkpoints!");

		else fclose(fpcheck);    /* Is there any reason to remove it? */
Tilman Steinweg's avatar
Tilman Steinweg committed
340
341
	}

Tilman Steinweg's avatar
Tilman Steinweg committed
342
	fprintf(fp," Accessibility of output files from PE %d has been checked successfully.\n", MYID);
Tilman Steinweg's avatar
Tilman Steinweg committed
343

Tilman Steinweg's avatar
Tilman Steinweg committed
344
	fprintf(fp,"\n\n --------- DETERMININATION OF MIN AND MAX VELOCITIES -----------\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
345
346
347
348
349
350
351
352
353
354
355
356
357

	/* low Q frame not yet applied as a absorbing boundary */
	/* if (!FREE_SURF) ny1=1+FW;*/
	/*nfw=FW; check only outside the absorbing frame */
	nfw=0;
	cmax_s=0;
	cmin_s=10000;
	cmax_p=0;
	cmin_p=10000;


	/* find maximum model phase velocity of shear waves at infinite
	      frequency within the whole model */
Tilman Steinweg's avatar
Tilman Steinweg committed
358
359
360
361
362
	if (L>0) {   /*viscoelastic*/
		for (i=1+nfw; i<= (nx-nfw); i++) {
			for (j=ny1; j<= (ny-nfw); j++) {
				c=sqrt(pu[j][i]* (1.0/prho[j][i]) * (1.0+L*ptaus[j][i]));

Tilman Steinweg's avatar
Tilman Steinweg committed
363
364
				/*if c is close to zero (water, air), c will be ignored for finding
				cmax,cmin*/
Tilman Steinweg's avatar
Tilman Steinweg committed
365
366
				if ((cmax_s<c) && (c>cwater)) cmax_s=c;

Tilman Steinweg's avatar
Tilman Steinweg committed
367
368
369
				/* find minimum model phase velocity of shear waves at center
					       frequency of the source */
				sum=0.0;
Tilman Steinweg's avatar
Tilman Steinweg committed
370
371

				for (l=1; l<=L; l++) {
Tilman Steinweg's avatar
Tilman Steinweg committed
372
					ts=DT/peta[l];
Tilman Steinweg's avatar
Tilman Steinweg committed
373
					sum=sum+ ((w*w*ptaus[j][i]*ts*ts) / (1.0+w*w*ts*ts));
Tilman Steinweg's avatar
Tilman Steinweg committed
374
				}
Tilman Steinweg's avatar
Tilman Steinweg committed
375
376
377
378

				c=sqrt((pu[j][i]/prho[j][i]));

				if ((cmin_s>c) && (c>cwater)) cmin_s=c;
Tilman Steinweg's avatar
Tilman Steinweg committed
379
380
			}
		}
Tilman Steinweg's avatar
Tilman Steinweg committed
381

Tilman Steinweg's avatar
Tilman Steinweg committed
382
	} else { /* L=0, elastic */
Tilman Steinweg's avatar
Tilman Steinweg committed
383
384
385
386
		for (i=1+nfw; i<= (nx-nfw); i++) {
			for (j=ny1; j<= (ny-nfw); j++) {
				c=sqrt(pu[j][i]/prho[j][i]);

Tilman Steinweg's avatar
Tilman Steinweg committed
387
				/*if c is close to zero (water, air), c will be ignored for finding	cmax,cmin*/
Tilman Steinweg's avatar
Tilman Steinweg committed
388
389
390
				if ((c>cwater) && (cmax_s<c)) cmax_s=c;

				if ((c>cwater) && (cmin_s>c)) cmin_s=c;
Tilman Steinweg's avatar
Tilman Steinweg committed
391
392
393
394
395
396
			}
		}
	}

	/* find maximum model phase velocity of P-waves at infinite
		 frequency within the whole model */
Tilman Steinweg's avatar
Tilman Steinweg committed
397
398
399
400
401
402
403
	if (L>0) {   /*viscoelastic*/
		for (i=1+nfw; i<= (nx-nfw); i++) {
			for (j=ny1; j<= (ny-nfw); j++) {
				c=sqrt(ppi[j][i]* (1.0/prho[j][i]) * (1.0+L*ptaup[j][i]));

				if ((c>cwater) && (cmax_p<c)) cmax_p=c;

Tilman Steinweg's avatar
Tilman Steinweg committed
404
405
				/* find minimum model phase velocity of P-waves at center frequency of the source */
				sum=0.0;
Tilman Steinweg's avatar
Tilman Steinweg committed
406
407

				for (l=1; l<=L; l++) {
Tilman Steinweg's avatar
Tilman Steinweg committed
408
					ts=DT/peta[l];
Tilman Steinweg's avatar
Tilman Steinweg committed
409
					sum=sum+ ((w*w*ptaup[j][i]*ts*ts) / (1.0+w*w*ts*ts));
Tilman Steinweg's avatar
Tilman Steinweg committed
410
				}
Tilman Steinweg's avatar
Tilman Steinweg committed
411
412
413
414

				c=sqrt((ppi[j][i]/prho[j][i]));

				if ((c>cwater) && (cmin_p>c)) cmin_p=c;
Tilman Steinweg's avatar
Tilman Steinweg committed
415
416
417

			}
		}
Tilman Steinweg's avatar
Tilman Steinweg committed
418

Tilman Steinweg's avatar
Tilman Steinweg committed
419
	} else { /* L=0, elastic */
Tilman Steinweg's avatar
Tilman Steinweg committed
420
421
422
423
		for (i=1+nfw; i<= (nx-nfw); i++) {
			for (j=ny1; j<= (ny-nfw); j++) {
				c=sqrt(ppi[j][i]/prho[j][i]);

Tilman Steinweg's avatar
Tilman Steinweg committed
424
				/*if c is close to zero (water, air), c will be ignored for finding	cmax,cmin*/
Tilman Steinweg's avatar
Tilman Steinweg committed
425
426
427
				if ((c>cwater) && (cmax_p<c)) cmax_p=c;

				if ((c>cwater) && (cmin_p>c)) cmin_p=c;
Tilman Steinweg's avatar
Tilman Steinweg committed
428
429
430
431
			}
		}
	}

Tilman Steinweg's avatar
Tilman Steinweg committed
432
433
434
435
436
437
	fprintf(fp," Minimum and maximum P-wave and S-wave velocities within subvolumes: \n ");
	fprintf(fp," MYID\t Vp_min(f=fc) \t Vp_max(f=inf) \t Vs_min(f=fc) \t Vs_max(f=inf) \n");
	fprintf(fp," %d \t %5.2f \t %5.2f \t %5.2f \t %5.2f \n", MYID, cmin_p, cmax_p, cmin_s, cmax_s);

	fprintf(fp," Note : if any P- or S-wave velocity is set below 1.0 m/s to simulate water or air,\n");
	fprintf(fp," this minimum velocity will be ignored for determining stable DH and DT.\n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
438

Tilman Steinweg's avatar
Tilman Steinweg committed
439
	if (cmax_s>cmax_p) cmax=cmax_s;
Tilman Steinweg's avatar
Tilman Steinweg committed
440
441

	else cmax=cmax_p;
Tilman Steinweg's avatar
Tilman Steinweg committed
442
443
444

	if (cmin_s<cmin_p) cmin=cmin_s;

Tilman Steinweg's avatar
Tilman Steinweg committed
445
446
447
	else cmin=cmin_p;

	/* find global maximum for Vp and global minimum for Vs*/
Tilman Steinweg's avatar
Tilman Steinweg committed
448
449
	MPI_Allreduce(&cmax,&cmax_r,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
	MPI_Allreduce(&cmin,&cmin_r,1,MPI_FLOAT,MPI_MIN,MPI_COMM_WORLD);
Tilman Steinweg's avatar
Tilman Steinweg committed
450
451
	cmax=cmax_r;
	cmin=cmin_r;
Tilman Steinweg's avatar
Tilman Steinweg committed
452
453
454
455
456
457
458
459

	if (FDORDER_TIME==4) {
		temporal=3.0/2.0;

	} else {
		temporal=1.0;
	}

Tilman Steinweg's avatar
Tilman Steinweg committed
460
	fmax=2.0/TS;
Tilman Steinweg's avatar
Tilman Steinweg committed
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
	dhstab = (cmin/ (hc[0]*fmax));
	gamma = fabs(hc[1]) + fabs(hc[2]) + fabs(hc[3]) + fabs(hc[4]) + fabs(hc[5]) + fabs(hc[6]);
	dtstab = DH/ (sqrt(2) *gamma*cmax*temporal);
	CFL=cmax*DT/DH;

	if (RSG) dtstab=DH/cmax;

	if (MYID == 0) {

		fprintf(fp," Global values for entire model: \n");
		fprintf(fp," V_max= %5.2f m/s \t V_min= %5.2f m/s \n\n", cmax,cmin);
		fprintf(fp,"\n\n ------------------ CHECK FOR GRID DISPERSION --------------------\n");
		fprintf(fp," To satisfactorily limit grid dispersion the number of gridpoints \n");
		fprintf(fp," per minimum wavelength (of S-waves) should be 6 (better more).\n");
		fprintf(fp," Here the minimum wavelength is assumed to be minimum model phase velocity \n");
		fprintf(fp," (of S-waves) at maximum frequency of the source\n");
		fprintf(fp," devided by maximum frequency of the source.\n");
		fprintf(fp," Maximum frequency of the source is approximately %6.3f Hz\n",2.0/TS);
		fprintf(fp," The minimum wavelength (P- or S-wave) in the following simulation will\n");
		fprintf(fp," be %5.3f meter.\n", cmin/fmax);
		fprintf(fp," Thus, the recommended value for DH is %5.3f meter.\n", dhstab);
		fprintf(fp," You have specified DH= %5.3f meter.\n\n", DH);

		if (DH>dhstab)
			warning(" Grid dispersion will influence wave propagation, choose smaller grid spacing (DH).");


		fprintf(fp," \n\n ----------------------- CHECK FOR STABILITY ---------------------\n");
		fprintf(fp," The following simulation is stable provided that\n\n");

		if (RSG) fprintf(fp," \t p=cmax*DT/DH < 1,\n\n");

		else     fprintf(fp," \t p=cmax*DT/DH < 1/(sqrt(2)*gamma),\n\n");

		fprintf(fp," where cmax is the maximum phase velocity at infinite frequency\n");

		if (RSG==0) fprintf(fp," and gamma = sum(|FD coeff.|)\n");

		fprintf(fp," In the current simulation cmax is %8.2f m/s .\n\n",cmax);

		fprintf(fp," DT is the timestep and DH is the grid size.\n\n");
		fprintf(fp," In this simulation the Courant-Friedrichs-Lewy number is %2.4f.\n",CFL);
		fprintf(fp," In this simulation the stability limit for timestep DT is %e seconds .\n",dtstab);
		fprintf(fp," You have specified DT= %e s.\n", DT);

		if (DT>dtstab)
			declare_error(" The simulation will get unstable, choose smaller DT. ");

		else fprintf(fp," The simulation will be stable.\n");

		fprintf(fp," \n\n --------------------- CHECK FOR INPUT ERRORS ---------------------\n");

		fprintf(fp," Checking the time of wave propagation. \n");

		if (SNAP) {
			fprintf(fp," Checking the snapshot parameters. \n");

			if (TSNAP2>TIME) {
				sprintf(errormessage,"\nTSNAP2 = %e (last snapshot) > Time of wave propagation %e. TSNAP2 was changed to be equal to TIME.\n",TSNAP2, TIME);
				TSNAP2=TIME;

				if (MYID==0)
					warning(errormessage); /* if TSNAP2>simulation TIME, snapmerge will generate "additional" snapshots out of nowhere, thus, snapshot files size blow up */
			}

			snapoutx=NX/ (float) IDX;
			snapouty=NY/ (float) IDY;

			fprintf(fp," Output of snapshot gridpoints per node (NX/NPROCX/IDX) %8.2f .\n", snapoutx);
			fprintf(fp," Output of snapshot gridpoints per node (NY/NPROCY/IDY) %8.2f .\n", snapouty);

			if (snapoutx- (int) snapoutx>0)
				declare_error("\n\n Ratio NX-NPROCX-IDX must be whole-numbered \n\n");

			if (snapouty- (int) snapouty>0)
				declare_error("\n\n Ratio NY-NPROCY-IDY must be whole-numbered \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
537
538
539
540

		}


Tilman Steinweg's avatar
Tilman Steinweg committed
541
542
543
544
545
		if ((SEISMO) && (MYID==0)) {
			fprintf(fp," Checking the number of seismogram samples. \n");
			fprintf(fp,"    Number of timesteps %d.\n", NT);
			fprintf(fp,"    Seismogram sampling rate in timesteps %d.\n", NDT);
			fprintf(fp,"    Number of seismogram output samples %d.\n", NT/NDT);
Tilman Steinweg's avatar
Tilman Steinweg committed
546
547
548
549
550

			/* SU and SEG-Y allow 32767 samples, furthermore the exist programs allow for 65535 samples
			and pseudo-SEG-Y formats allow foralmost arbitrarily long traces.
			For binary and textual output the limit is arbitrary. USHRT_MAX is the limut of an unsigned short specified in limits.h */

Tilman Steinweg's avatar
Tilman Steinweg committed
551
552
553
			if ((SEIS_FORMAT[0]==1) && (NT/NDT) > (USHRT_MAX)) {
				fprintf(fp," Maximum allowed number of samples per trace in SU format: %d \n", USHRT_MAX);
				declare_error(" Sorry. Too many samples per receiver! \n");
Tilman Steinweg's avatar
Tilman Steinweg committed
554
555
556
			}
		}

Tilman Steinweg's avatar
Tilman Steinweg committed
557
		if ((SEISMO>0) && (MYID==0)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
558
559
			srec_minx=DH*NX*NPROCX+1, srec_miny=DH*NY*NPROCY+1;
			srec_maxx=-1.0, srec_maxy=-1.0;
Tilman Steinweg's avatar
Tilman Steinweg committed
560
561
562
563
564
565
			fprintf(fp," Checking for receiver position(s) specified in input file.\n");
			fprintf(fp,"    Global grid size in m: %5.2f (x) : %5.2f (y) \n",NX*DH*NPROCX,NY*DH*NPROCY);

			if (FREE_SURF==0) fprintf(fp,"    Global grid size in m(-width of abs.boundary) : \n        %5.2f-%5.2f (x in m) : %5.2f-%5.2f (y in m) \n", (float) FW*DH,NX*DH*NPROCX- (float) FW*DH, (float) FW*DH,NY*DH*NPROCY- (float) FW*DH);

			if (FREE_SURF==1) fprintf(fp,"    Global grid size in m(-width of abs.boundary) : \n        %5.2f-%5.2f (x in m) : %5.2f-%5.2f (y in m) \n", (float) FW*DH,NX*DH*NPROCX- (float) FW*DH,DH,NY*DH*NPROCY- (float) FW*DH);
Tilman Steinweg's avatar
Tilman Steinweg committed
566
567
568
569
570
571
572
573

			/* find maximum and minimum source positions coordinate ---- from input file*/
			/*for usability reasons, "z" - as commonly used - denotes the depth (vertical direction),
			      however, internally "y" is used for the vertical coordinate,
			      we simply switch the "y" and "z" coordinate as read in the input file,
			      therefore we determine the minimum/maximum position in y-direction by the ZREC1 variable and vice versa.
			this has to be considered for the receiver line coordinates specified in both the input file and separate source/receiver files*/

Tilman Steinweg's avatar
Tilman Steinweg committed
574
575
			if (READREC==0) {
				if (XREC1>XREC2) {
Tilman Steinweg's avatar
Tilman Steinweg committed
576
577
					srec_maxx=XREC1;
					srec_minx=XREC2;
Tilman Steinweg's avatar
Tilman Steinweg committed
578

Tilman Steinweg's avatar
Tilman Steinweg committed
579
580
581
582
				} else {
					srec_maxx=XREC2;
					srec_minx=XREC1;
				}
Tilman Steinweg's avatar
Tilman Steinweg committed
583
584

				if (YREC1>YREC2) {
Tilman Steinweg's avatar
Tilman Steinweg committed
585
586
					srec_maxy=YREC1;
					srec_miny=YREC2;
Tilman Steinweg's avatar
Tilman Steinweg committed
587

Tilman Steinweg's avatar
Tilman Steinweg committed
588
589
590
591
				} else {
					srec_maxy=YREC2;
					srec_miny=YREC1;
				}
Tilman Steinweg's avatar
Tilman Steinweg committed
592
593

				fprintf(fp,"    Number of receiver positions in input file : %i \n", ntr);
Tilman Steinweg's avatar
Tilman Steinweg committed
594
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
595
596

			if (READREC==1) {
Tilman Steinweg's avatar
Tilman Steinweg committed
597
				/* find maximum and minimum source positions coordinate ---- from receiver file*/
Tilman Steinweg's avatar
Tilman Steinweg committed
598
				for (k=1; k<=ntr; k++) {
Tilman Steinweg's avatar
Tilman Steinweg committed
599
					/* find maximum source positions coordinate*/
Tilman Steinweg's avatar
Tilman Steinweg committed
600
601
602
603
					if ((recpos[1][k]*DH) >srec_maxx) srec_maxx=recpos[1][k]*DH;

					if ((recpos[2][k]*DH) >srec_maxy) srec_maxy=recpos[2][k]*DH;

Tilman Steinweg's avatar
Tilman Steinweg committed
604
					/* find minimum source positions coordinate*/
Tilman Steinweg's avatar
Tilman Steinweg committed
605
606
607
					if ((recpos[1][k]*DH) <srec_minx) srec_minx=recpos[1][k]*DH;

					if ((recpos[2][k]*DH) <srec_miny) srec_miny=recpos[2][k]*DH;
Tilman Steinweg's avatar
Tilman Steinweg committed
608
				}
Tilman Steinweg's avatar
Tilman Steinweg committed
609
610

				fprintf(fp,"    Number of receiver positions in receiver file %s : %i \n", REC_FILE, ntr);
Tilman Steinweg's avatar
Tilman Steinweg committed
611
612
613
614
			}



Tilman Steinweg's avatar
Tilman Steinweg committed
615
616
617
			fprintf(fp,"    Minimum receiver position coordinates : %5.2f (x) : %5.2f (y) \n",srec_minx,srec_miny);
			fprintf(fp,"    Maximum receiver position coordinates : %5.2f (x) : %5.2f (y) \n",srec_maxx,srec_maxy);

Tilman Steinweg's avatar
Tilman Steinweg committed
618
			/* checking if receiver coordinate of first receiver in line specified in input-file is inside the global grid */
Tilman Steinweg's avatar
Tilman Steinweg committed
619
620
			if (((srec_maxx<0.0) || (srec_maxy<0.0)) || ((srec_minx<0.0) || (srec_miny<0.0))) {
				declare_error("\n\n Coordinate of at least one receiver location is outside the global grid. \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
621
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
622
623
624

			if ((srec_maxx>NX*DH*NPROCX) || (srec_maxy>NY*DH*NPROCY)) {
				declare_error("\n\n Coordinate of at least one receiver location is outside the global grid. \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
625
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
626

Tilman Steinweg's avatar
Tilman Steinweg committed
627
			/* checking if receiver coordinate of first receiver in line specified in input-file is outside the Absorbing Boundary  */
Tilman Steinweg's avatar
Tilman Steinweg committed
628
			if ((srec_maxx< ((float) FW*DH)) || (srec_minx< ((float) FW*DH))) {
Tilman Steinweg's avatar
Tilman Steinweg committed
629
				/* this warning appears, when at least a single receiver is located in AB between 0 - FW+DX/DX/DZ ("inner boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
630
				warning("\n\n Coordinate of at least one receiver location is inside the Absorbing Boundary (warning 1, left boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
631
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
632
633

			if (srec_maxx> (NX*DH*NPROCX- (float) FW*DH)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
634
				/* this warning appears, when at least a single receiver is located in AB between NX/NY/NZ-FW+DX/DX/DZ and NX/NY/NZ ("outer boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
635
				warning("\n\n Coordinate of at least one receiver location is inside the Absorbing Boundary (warning 2, right boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
636
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
637
638

			if (srec_maxy> (NY*DH*NPROCY- (float) FW*DH)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
639
				/* this warning appears, when at least a single receiver is located in AB between NX/NY/NZ-FW+DX/DX/DZ and NX/NY/NZ ("outer boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
640
				warning("\n\n Coordinate of at least one receiver location is inside the Absorbing Boundary (warning 3, lower boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
641
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
642
643

			if ((srec_miny< ((float) FW*DH)) && !(FREE_SURF)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
644
				/* this warning appears, when at least a single receiver is located in AB between NX/NY/NZ-FW+DX/DX/DZ and NX/NY/NZ ("outer boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
645
				warning("\n\n Coordinate of at least one receiver location is inside the Absorbing Boundary (warning 4, top boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
646
647
			}

Tilman Steinweg's avatar
Tilman Steinweg committed
648
			fprintf(fp," ... complete, receiver position specified in input file are located within the global grid.\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
649
650
651

		}

Tilman Steinweg's avatar
Tilman Steinweg committed
652
		if ((SRCREC==1) && (MYID==0)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
653
654
			srec_minx=DH*NX*NPROCX+1, srec_miny=DH*NY*NPROCY+1;
			srec_maxx=-1.0, srec_maxy=-1.0;
Tilman Steinweg's avatar
Tilman Steinweg committed
655
656
657
658
659
660
			fprintf(fp," Checking for source position(s) specified in source file. \n");
			fprintf(fp,"    Global grid size in m: %5.2f (x) : %5.2f (y) \n",NX*DH*NPROCX,NY*DH*NPROCY);

			if (FREE_SURF==0) fprintf(fp,"    Global grid size in m (-width of abs.boundary) : \n        %5.2f-%5.2f (x in m) : %5.2f-%5.2f (y in m) \n", (float) FW*DH,NX*DH*NPROCX- (float) FW*DH, (float) FW*DH,NY*DH*NPROCY- (float) FW*DH);

			if (FREE_SURF==1) fprintf(fp,"    Global grid size in m(-width of abs.boundary) : \n        %5.2f-%5.2f (x in m) : %5.2f-%5.2f (y in m) \n", (float) FW*DH,NX* (float) DH*NPROCX- (float) FW*DH,DH,NY*DH*NPROCY- (float) FW*DH);
Tilman Steinweg's avatar
Tilman Steinweg committed
661
662


Tilman Steinweg's avatar
Tilman Steinweg committed
663
			for (k=1; k<=nsrc; k++) {
Tilman Steinweg's avatar
Tilman Steinweg committed
664
				/* find maximum source positions coordinate*/
Tilman Steinweg's avatar
Tilman Steinweg committed
665
666
667
668
				if (srcpos[1][k]>srec_maxx) srec_maxx=srcpos[1][k];

				if (srcpos[2][k]>srec_maxy) srec_maxy=srcpos[2][k];

Tilman Steinweg's avatar
Tilman Steinweg committed
669
				/* find minimum source positions coordinate*/
Tilman Steinweg's avatar
Tilman Steinweg committed
670
671
672
				if (srcpos[1][k]<srec_minx) srec_minx=srcpos[1][k];

				if (srcpos[2][k]<srec_miny) srec_miny=srcpos[2][k];
Tilman Steinweg's avatar
Tilman Steinweg committed
673
674
			}

Tilman Steinweg's avatar
Tilman Steinweg committed
675
676
677
678
			fprintf(fp,"    Number of source positions in source file %s. : %i.\n", SOURCE_FILE, nsrc);
			fprintf(fp,"    Minimum source position coordinates : %5.2f (x) : %5.2f (y) \n",srec_minx,srec_miny);
			fprintf(fp,"    Maximum source position coordinates : %5.2f (x) : %5.2f (y) \n",srec_maxx,srec_maxy);

Tilman Steinweg's avatar
Tilman Steinweg committed
679
			/* checking if receiver coordinate of first receiver in line specified in input-file is inside the global grid */
Tilman Steinweg's avatar
Tilman Steinweg committed
680
681
			if (((srec_maxx<0.0) || (srec_maxy<0.0)) || ((srec_minx<0.0) || (srec_miny<0.0))) {
				declare_error("\n\n Coordinate of at least one source location is outside the global grid. \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
682
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
683
684
685

			if ((srec_maxx>NX*DH*NPROCX) || (srec_maxy>NY*DH*NPROCY)) {
				declare_error("\n\n Coordinate of at least one source location is outside the global grid. \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
686
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
687

Tilman Steinweg's avatar
Tilman Steinweg committed
688
			/* checking if receiver coordinate of first receiver in line specified in input-file is outside the Absorbing Boundary  */
Tilman Steinweg's avatar
Tilman Steinweg committed
689
			if ((srec_maxx< ((float) FW*DH)) || (srec_minx< ((float) FW*DH))) {
Tilman Steinweg's avatar
Tilman Steinweg committed
690
				/* this warning appears, when at least a single receiver is located in AB between 0 - FW+DX/DX/DZ ("inner boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
691
				warning("\n\n Coordinate of at least one source location is inside the Absorbing Boundary (warning 1, left boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
692
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
693
694

			if (srec_maxx> (NX*DH*NPROCX- (float) FW*DH)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
695
				/* this warning appears, when at least a single receiver is located in AB between NX/NY/NZ-FW+DX/DX/DZ and NX/NY/NZ ("outer boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
696
				warning("\n\n Coordinate of at least one source location is inside the Absorbing Boundary (warning 2, right boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
697
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
698
699

			if (srec_maxy> (NY*DH*NPROCY- (float) FW*DH)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
700
				/* this warning appears, when at least a single receiver is located in AB between NX/NY/NZ-FW+DX/DX/DZ and NX/NY/NZ ("outer boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
701
				warning("\n\n Coordinate of at least one source location is inside the Absorbing Boundary (warning 3, lower boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
702
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
703
704

			if ((srec_miny< ((float) FW*DH)) && !(FREE_SURF)) {
Tilman Steinweg's avatar
Tilman Steinweg committed
705
				/* this warning appears, when at least a single receiver is located in AB between NX/NY/NZ-FW+DX/DX/DZ and NX/NY/NZ ("outer boundary")*/
Tilman Steinweg's avatar
Tilman Steinweg committed
706
				warning("\n\n Coordinate of at least one source location is inside the Absorbing Boundary (warning 4, top boundary). \n\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
707
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
708
709

			fprintf(fp," ...complete, all source position(s) specified in source file are located within the global grid.\n");
Tilman Steinweg's avatar
Tilman Steinweg committed
710
711
712
713


		}

Tilman Steinweg's avatar
Tilman Steinweg committed
714
715
		fprintf(fp,"\n\n ----------------------- ABSORBING BOUNDARY ------------------------\n");

Tilman Steinweg's avatar
Tilman Steinweg committed
716
717


Tilman Steinweg's avatar
Tilman Steinweg committed
718
719
720
721
722
723
		if (ABS_TYPE==1) {
			fprintf(fp," You have specified a CPML boundary (ABS_TYPE=1) with a width of %i gridpoints (%5.2f m).\n",FW, (float) FW*DH);

			if (FW<10) {
				fprintf(fp," Width (FW) of absorbing frame should be at least 10 gridpoints.\n\n");
				warning("  Be aware of artificial reflections from grid boundaries ! \n");
Tilman Steinweg's avatar
Tilman Steinweg committed
724
725
726
727

			}
		}

Tilman Steinweg's avatar
Tilman Steinweg committed
728
729
730
731
732
733
		if (ABS_TYPE==2) {
			fprintf(fp," You have specified an absorbing boundary (ABS_TYPE=2) width of %i gridpoints (%5.2f m).\n",FW, (float) FW*DH);

			if (FW<30) {
				fprintf(fp," Width (FW) of absorbing frame should be at least 30 gridpoints.\n\n");
				warning(" Be aware of artificial reflections from grid boundaries ! \n");
Tilman Steinweg's avatar
Tilman Steinweg committed
734
735
736
			}
		}

Tilman Steinweg's avatar
Tilman Steinweg committed
737
738
739
740
		if (((NX) <FW) || ((NY) <FW))	{
			fprintf(fp," \n Width of absorbing boundary (FW = %i gridpoints) is larger than at least one subdomain dimension: \n",FW);
			fprintf(fp," \t NX/NPROCX = %i, NY/NPROCY = %i gridpoints.\n",NX,NY);
			declare_error(" Choose smaller width of absorbing frame (FW) or increase subdomain dimensions");
Tilman Steinweg's avatar
Tilman Steinweg committed
741
		}
Tilman Steinweg's avatar
Tilman Steinweg committed
742
743
744
745
746

		if (BOUNDARY)
			if (ABS_TYPE==1 || ABS_TYPE==2) {
				warning(" You have activated a periodic boundary and set an absorbing boundary at the same time! \n");
			}
Tilman Steinweg's avatar
Tilman Steinweg committed
747
748
	}
}
Tilman Steinweg's avatar
Tilman Steinweg committed
749