sofi3D.c 68 KB
Newer Older
tilman.metz's avatar
tilman.metz 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
/*------------------------------------------------------------------------
 * 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>.
--------------------------------------------------------------------------*/
/* ----------------------------------------------------------------------
 * This is program SOFI3D.
 * Parallel 3-D Viscoelastic Finite Difference Seismic Modelling
 * using the Standard Staggered Grid (SSG)
 *
 * PLEASE DO NOT DISTRIBUTE. PLEASE REFER OTHER PEOPLE TO :
 *
 * Prof. Dr. Thomas Bohlen, Karlsruhe Institute of Technology,
 * Geophysical Institute,
 * Hertzstr. 16, 76187 Karlsruhe, Germany
 * Phone/Fax: +49 (0)721 608 44416
 * mailto:thomas.bohlen@kit.edu,
 * http://www.gpi.kit.edu/
 * http://www.gpi.kit.edu/SOFI3D.php
 *
 *
 * If you want to publish synthetic data calculated with this program please
 * give a reference to the following paper:
 * Bohlen, T., 2002, Parallel 3-D viscoelastic finite-difference seismic modelling,
 * Computers @ Geopsciences, Vol. 28, No. 8, 887-889.
 *
 *  ----------------------------------------------------------------------*/

#include "fd.h"
#include "globvar.h"

int main(int argc, char **argv){
	int ns, nt, nseismograms=0, nf1, nf2;
	int lsnap, nsnap=0, lsamp=0, nlsamp=0, buffsize;
	int ntr=0, ntr_loc=0, ntr_glob=0, nsrc=0, nsrc_loc=0, ishot, nshots; /* removed variable "h", not in use*/

	double 	time1=0.0, time2=0.0, time3=0.0, time4=0.0;
	double * time_v_update, * time_s_update, * time_s_exchange,* time_v_exchange, * time_timestep;	
	int * xb, * yb, * zb, l;

	float  *** absorb_coeff=NULL;
	float  ***  sxy=NULL, ***  syz=NULL, ***  sxz=NULL;
	float  ***  sxx=NULL, ***  syy=NULL, ***  szz=NULL;
	float  ***  rxy=NULL, ***  ryz=NULL, ***  rxz=NULL;
	float  ***  rxx=NULL, ***  ryy=NULL, ***  rzz=NULL;
	float  ***  vx=NULL, ***  vy=NULL, ***  vz=NULL;
    
    /* Save old spatial derivations of velocity for Adam Bashforth */
    float *** vxyyx=NULL, *** vyzzy=NULL, *** vxzzx=NULL, *** vxxyyzz=NULL, *** vyyzz=NULL, *** vxxzz=NULL, *** vxxyy=NULL;
    float *** vxyyx_2=NULL, *** vyzzy_2=NULL, *** vxzzx_2=NULL, *** vxxyyzz_2=NULL, *** vyyzz_2=NULL, *** vxxzz_2=NULL, *** vxxyy_2=NULL;
    float *** vxyyx_3=NULL, *** vyzzy_3=NULL, *** vxzzx_3=NULL, *** vxxyyzz_3=NULL, *** vyyzz_3=NULL, *** vxxzz_3=NULL, *** vxxyy_3=NULL;
    float *** vxyyx_4=NULL, *** vyzzy_4=NULL, *** vxzzx_4=NULL, *** vxxyyzz_4=NULL, *** vyyzz_4=NULL, *** vxxzz_4=NULL, *** vxxyy_4=NULL;
    
    /* Save old derivation of the stress for Adam Bashforth */
    float *** svx=NULL,  *** svy=NULL, *** svz=NULL;
    float *** svx_2=NULL, *** svy_2=NULL, *** svz_2=NULL, *** svx_3=NULL, *** svy_3=NULL, *** svz_3=NULL;
    float *** svx_4=NULL, *** svy_4=NULL, *** svz_4=NULL;

    /* We need some pointers for the time shift for Adam Bashforth*/
    float *** shift_s1=NULL,*** shift_s2=NULL,*** shift_s3=NULL;
    float *** shift_v1=NULL,*** shift_v2=NULL,*** shift_v3=NULL,*** shift_v4=NULL,*** shift_v5=NULL,*** shift_v6=NULL,*** shift_v7=NULL;
    float *** shift_r1=NULL,*** shift_r2=NULL,*** shift_r3=NULL,*** shift_r4=NULL,*** shift_r5=NULL,*** shift_r6=NULL;
    
    /* We need some pointes for the memory variables for Adams Bashforth */
    float  ***  rxy_2=NULL, ***  ryz_2=NULL, ***  rxz_2=NULL;
    float  ***  rxx_2=NULL, ***  ryy_2=NULL, ***  rzz_2=NULL;
    float  ***  rxy_3=NULL, ***  ryz_3=NULL, ***  rxz_3=NULL;
    float  ***  rxx_3=NULL, ***  ryy_3=NULL, ***  rzz_3=NULL;
    float  ***  rxy_4=NULL, ***  ryz_4=NULL, ***  rxz_4=NULL;
    float  ***  rxx_4=NULL, ***  ryy_4=NULL, ***  rzz_4=NULL;
    
	float  ***  rho, ***  pi, ***  u;
	float  ***  taus=NULL, ***  taup=NULL, *eta=NULL;
	float  *** uipjp, *** ujpkp, *** uipkp, *** tausipjp=NULL, *** tausjpkp=NULL, *** tausipkp=NULL,*** rjp, *** rkp, *** rip;

	float  ** srcpos=NULL, **srcpos_loc=NULL, ** srcpos1=NULL, ** signals=NULL;
	int    ** recpos=NULL, ** recpos_loc=NULL;
	float  ** sectionvx=NULL, ** sectionvy=NULL, ** sectionvz=NULL, ** sectionp=NULL,
			** sectioncurl=NULL, ** sectiondiv=NULL;
	float *** bufferlef_to_rig, *** bufferrig_to_lef;
	float *** buffertop_to_bot, *** bufferbot_to_top;
	float *** bufferfro_to_bac, *** bufferbac_to_fro;

	float *** sbufferlef_to_rig, *** sbufferrig_to_lef;
	float *** sbuffertop_to_bot, *** sbufferbot_to_top;
	float *** sbufferfro_to_bac, *** sbufferbac_to_fro;

	float ** seismo_fulldata=NULL;
	int * recswitch=NULL;

	float * K_x=NULL, * alpha_prime_x=NULL, * a_x=NULL, * b_x=NULL, * K_x_half=NULL, * alpha_prime_x_half=NULL, * a_x_half=NULL, * b_x_half=NULL, * K_y=NULL, * alpha_prime_y=NULL, * a_y=NULL, * b_y=NULL, * K_y_half=NULL, * alpha_prime_y_half=NULL, * a_y_half=NULL, * b_y_half=NULL, * K_z=NULL, * alpha_prime_z=NULL, * a_z=NULL, * b_z=NULL, * K_z_half=NULL, * alpha_prime_z_half=NULL, * a_z_half=NULL, * b_z_half=NULL;
	float *** psi_sxx_x=NULL, *** psi_syy_y=NULL, *** psi_szz_z=NULL, *** psi_sxy_y=NULL, *** psi_sxy_x=NULL, *** psi_sxz_x=NULL, *** psi_sxz_z=NULL, *** psi_syz_y=NULL, *** psi_syz_z=NULL, *** psi_vxx=NULL, *** psi_vyy=NULL, *** psi_vzz=NULL, *** psi_vxy=NULL, *** psi_vxz=NULL, *** psi_vyx=NULL, *** psi_vyz=NULL, *** psi_vzx=NULL, *** psi_vzy=NULL, *** psi_vxxs=NULL;

	MPI_Request *req_send, *req_rec, *sreq_send, *sreq_rec;
	MPI_Status  *send_statuses, *rec_statuses;

	float memdyn, memmodel, memseismograms, membuffer, memcpml=0.0, memtotal;
	float amon=0.0, str=0.0, dip=0.0, rake=0.0;
	float fac1, fac2;
	char *buff_addr, ext[10];
	int * stype=NULL, *stype_loc=NULL;
	char buffer[STRING_SIZE], bufferstring[10];
	FILE * fpsrc=NULL;

	/* 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);

	/* initialize clock for estimating runtime of program */	
	if (MYID == 0){
		time1=MPI_Wtime();
		clock();
	}	

	/* print program name, version, author etc to stdout*/
	if (MYID == 0) info(stdout);
	SOFI3DVERS=33; /* 3D isotropic elastic */

	/* PE 0 is reading the parameters from the input file, default is sofi3D.json */
	if (MYID == 0){
		if (argc>1) {
			strncpy(FILEINP,argv[1],STRING_SIZE);
			fprintf(stderr," Input parameter filename read from command line : %s. \n\n",FILEINP);
			if (strlen(FILEINP)>STRING_SIZE-1) {
				fprintf(stderr,"\n SOFI3D cannot handel pathes with more than %d characters.\n",STRING_SIZE-1);
				fprintf(stderr," Error: SOFI3D could not read input parameter file name. -> Exit. \n\n");
				return -1;
			}
		}
		else {
			strcpy(FILEINP,"sofi3D.json");
			fprintf(stderr," Caution: input parameter filename set to default 'sofi3D.json'. \n\n");
		}
		FP=fopen(FILEINP,"r");

		//read json formated input file
		read_par_json(stdout, FILEINP);
		fclose(FP);
	} 
	/* PE 0 will broadcast the parameters to all others PEs */
	exchange_par(); 
    
	/* Print info on log-files to stdout */
	if (MYID == 0) note(stdout);

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

	/* nodes MYIDo writes logging info to LOG_FILE or stdout */	
	if (MYID==0) 
		switch (LOG){
		case 0 : FP=fopen("/dev/null","w"); /* no logging information will be output */
		break;
		case 1 : FP=stdout; /* logging information will be written to standard output */
		break;
		case 2 : if ((FP=fopen(LOG_FILE,"w"))==NULL) err(" Opening log-file failed.");
		/* logging information will be written to LOG_FILE */
		break;
		}

	/* all other nodes write logging info to LOG_FILE */		
	if (MYID>0) 
		if ((FP=fopen(LOG_FILE,"w"))==NULL) err(" Opening log-file failed.");

	fprintf(FP," This is the log-file generated by PE %d \n\n",MYID);

	/* domain decomposition */
	initproc();

	/* set some time counters */
	NT=(int)ceil(TIME/DT); /* number of timesteps - replaces: NT=iround(TIME/DT); */
	TIME=(NT-1)*DT; /* TIME set to true time of the last time step */

	if (NDTSHIFT>NT) {
		ns=0;
	}
	else {
		ns=(int)ceil((float)(NT-NDTSHIFT)/(float)NDT); /* number of samples per trace - replaces buggy formula: ns=iround(NT-NDTSHIFT/NDT); */
	}
	lsnap=iround(TSNAP1/DT); /* first snapshot at this timestep */
	if (((ns<0) && (MYID==0)) && (SEISMO>0)) {
		/*fprintf(FP," \nSampling interval for seismogram output (DT) : %f \n\n",DT);
		fprintf(FP," \nSampling interval shift (NDTSHIFT) : %i , TIME : %f NT : %i NDT : %i \n\n",NDTSHIFT,TIME, NT,NDT);*/
		fprintf(FP," \nSampling rate for seismogram output (NDT) is out of limit : %i \n\n",ns);
		err(" Check Sampling rate for seismogram output (NDT)!");
	}

	/* output of parameters to stdout: */
	if (MYID==0) writepar(FP,ns);

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

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

	/* compute receiver locations within each subgrid and
	   store local receiver coordinates in recpos_loc */	
	if (SEISMO){
		fprintf(FP,"\n ------------------ READING RECEIVER PARAMETERS ----------------- \n");
		recpos=receiver(FP,&ntr);
		recswitch = ivector(1,ntr);
		recpos_loc = splitrec(recpos,&ntr_loc, ntr, recswitch);
		ntr_glob=ntr;
		ntr=ntr_loc;
	}

	/* number of seismogram sections which have to be stored in core memory*/
	/* allocation of memory for seismogramm merge */
	switch (SEISMO){
	case 1 : /* particle velocities only */
		nseismograms=3;
		break;
	case 2 : /* pressure only */
		nseismograms=1;
		break;
	case 3 : /* curl and div only */
		nseismograms=2;
		break;
	case 4 : /* everything */
		nseismograms=6;
		break;
	default : nseismograms=0;
	break;
	}

	/*allocate memory for dynamic, static and buffer arrays */
	fac1=(NZ+FDORDER)*(NY+FDORDER)*(NX+FDORDER);
    fac2=sizeof(float)*pow(2.0,-20.0);
    
    if (L>0){ /*viscoelastic case*/
        if(FDORDER_TIME==2){
            memdyn=15.0*fac1*fac2;
            memmodel=16.0*fac1*fac2;
        } else {
            if(FDORDER_TIME==3) {
                memdyn=57*fac1*fac2;
                memmodel=16.0*fac1*fac2;
            } else {
                memdyn=78*fac1*fac2;
                memmodel=16.0*fac1*fac2;
            }
        }
    } else{   /* elastic case*/
        if(FDORDER_TIME==2){
            memdyn=9.0*fac1*fac2;
            memmodel=10.0*fac1*fac2;
        } else {
            if(FDORDER_TIME==3) {
                memdyn=39*fac1*fac2;
                memmodel=10.0*fac1*fac2;
            } else {
                memdyn=49*fac1*fac2;
                memmodel=10.0*fac1*fac2;
            }
        }
    }

	memseismograms=nseismograms*ntr_glob*ns*fac2 + nseismograms*ntr*ns*fac2;
	membuffer=(2.0*(3.0*FDORDER/2-1)*(NY*NZ+NX*NZ+NY*NX)+2.0*(3.0*FDORDER/2-2)*(NY*NZ+NX*NZ+NY*NX))*fac2;
	membuffer=4.0*6.0*((NX*NZ)+(NY*NZ)+(NX*NY))*fac2;
	if (ABS_TYPE==1) memcpml=2.0*FW*6.0*(NY*NZ+NX*NZ+NY*NX)*fac2+24.0*2.0*FW*fac2;
	buffsize=(FDORDER)*4.0*6.0*(max((NX*NZ),max((NY*NZ),(NX*NY))))*sizeof(MPI_FLOAT);
	memtotal=memdyn+memmodel+memseismograms+membuffer+memcpml+(buffsize*pow(2.0,-20.0));


	if (MYID==0){
		fprintf(FP,"\n ----------------------------------------------------------------");
		fprintf(FP,"\n ------------------ MEMORY ALLOCATION --------------------------- \n");
		fprintf(FP,"\n **Message from main (printed by PE %d):\n",MYID);
		fprintf(FP," Size of local grids: NX=%d \t NY=%d \t NZ=%d \n",NX,NY,NZ);
		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);
		if (ABS_TYPE==1) fprintf(FP," CPML memory variables : \t %6.2f MB\n", memcpml);
		fprintf(FP," Buffer arrays for grid exchange:%6.2f MB\n", membuffer);
		fprintf(FP," Network Buffer for MPI_Bsend: \t %6.2f MB\n\n", buffsize*pow(2.0,-20.0));
		fprintf(FP," Total memory required: \t %6.2f MB.\n", memtotal);
		fprintf(FP," Please note that the memory consumption is only a good estimate! ");
		fprintf(FP,"\n ---------------------------------------------------------------- \n\n");
	}

	/* 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));
	sreq_send=(MPI_Request *)malloc(REQUEST_COUNT*sizeof(MPI_Request));
	sreq_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));

	/* allocation for timing arrays used for performance analysis */
	time_v_update=dvector(1,NT);
	time_s_update=dvector(1,NT);
	time_s_exchange=dvector(1,NT);
	time_v_exchange=dvector(1,NT);
	time_timestep=dvector(1,NT);

	l=1;
	if(ABS_TYPE==1 && FDORDER==2){l=2;}
    
	/* memory allocation for dynamic (wavefield) arrays */
	if(POS[2]==0){
		vx  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		vy  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		vz  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        
        if(FDORDER_TIME != 2){ /* Allocate memory for Adams Bashforth */
            vxyyx  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyzzy  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxzzx  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyyzz  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyyzz  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxzz  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyy  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            vxyyx_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyzzy_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxzzx_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyyzz_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyyzz_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxzz_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyy_2  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            vxyyx_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyzzy_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxzzx_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyyzz_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyyzz_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxzz_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyy_3  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            svx=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svy=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svz=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            svx_2=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svy_2=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svz_2=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            svx_3=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svy_3=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svz_3=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            if(FDORDER_TIME==4){
                svx_4=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                svy_4=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                svz_4=f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                
                vxyyx_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vyzzy_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxzzx_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxxyyzz_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vyyzz_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxxzz_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxxyy_4  =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            }
        }
        
		sxy =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		syz =  f3tensor(0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	}

    if(POS[2]>0){
        vx  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        vy  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        vz  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        
        if(FDORDER_TIME != 2){ /* Allocate memory for Adams Bashforth */
            vxyyx  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyzzy  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxzzx  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyyzz  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyyzz  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxzz  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyy  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            vxyyx_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyzzy_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxzzx_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyyzz_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyyzz_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxzz_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyy_2  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            vxyyx_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyzzy_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxzzx_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyyzz_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vyyzz_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxzz_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            vxxyy_3  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            svx=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svy=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svz=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            svx_2=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svy_2=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svz_2=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            svx_3=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svy_3=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            svz_3=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            if(FDORDER_TIME==4){
                svx_4=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                svy_4=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                svz_4=f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                
                vxyyx_4  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vyzzy_4  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxzzx_4  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxxyyzz_4  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vyyzz_4  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxxzz_4  =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                vxxyy_4  = f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            }
        }
        
        sxy =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        syz =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
    }

	sxz =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	sxx =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	syy =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	szz =  f3tensor(1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);

	xb=ivector(0,1);
	yb=ivector(0,1);
	zb=ivector(0,1);

	if (L){ /* no allocation in case of purely elastic simulation */
		/* memory allocation for dynamic (model) arrays */
		rxy =  f3tensor(1,NY,1,NX,1,NZ);
		ryz =  f3tensor(1,NY,1,NX,1,NZ);
		rxz =  f3tensor(1,NY,1,NX,1,NZ);
		rxx =  f3tensor(1,NY,1,NX,1,NZ);
		ryy =  f3tensor(1,NY,1,NX,1,NZ);
		rzz =  f3tensor(1,NY,1,NX,1,NZ);
        
        if(FDORDER_TIME != 2){ /* Allocate memory for Adams Bashforth */
            rxy_2 =  f3tensor(1,NY,1,NX,1,NZ);
            ryz_2 =  f3tensor(1,NY,1,NX,1,NZ);
            rxz_2 =  f3tensor(1,NY,1,NX,1,NZ);
            rxx_2 =  f3tensor(1,NY,1,NX,1,NZ);
            ryy_2 =  f3tensor(1,NY,1,NX,1,NZ);
            rzz_2 =  f3tensor(1,NY,1,NX,1,NZ);
            
            rxy_3 =  f3tensor(1,NY,1,NX,1,NZ);
            ryz_3 =  f3tensor(1,NY,1,NX,1,NZ);
            rxz_3 =  f3tensor(1,NY,1,NX,1,NZ);
            rxx_3 =  f3tensor(1,NY,1,NX,1,NZ);
            ryy_3 =  f3tensor(1,NY,1,NX,1,NZ);
            rzz_3 =  f3tensor(1,NY,1,NX,1,NZ);
            if(FDORDER_TIME==4){
                rxy_4 =  f3tensor(1,NY,1,NX,1,NZ);
                ryz_4 =  f3tensor(1,NY,1,NX,1,NZ);
                rxz_4 =  f3tensor(1,NY,1,NX,1,NZ);
                rxx_4 =  f3tensor(1,NY,1,NX,1,NZ);
                ryy_4 =  f3tensor(1,NY,1,NX,1,NZ);
                rzz_4 =  f3tensor(1,NY,1,NX,1,NZ);
            }
        }
        
		/* memory allocation for static (model) arrays */
		taus=  f3tensor(0,NY+1,0,NX+1,0,NZ+1);
		taup=  f3tensor(0,NY+1,0,NX+1,0,NZ+1);
		tausipjp=f3tensor(1,NY,1,NX,1,NZ);
		tausjpkp=f3tensor(1,NY,1,NX,1,NZ);
		tausipkp=f3tensor(1,NY,1,NX,1,NZ);
		eta =  vector(1,L);
	}

	/* memory allocation for static (model) arrays */
	rho =  f3tensor(0,NY+1,0,NX+1,0,NZ+1);
	pi  =  f3tensor(0,NY+1,0,NX+1,0,NZ+1);
	u   =  f3tensor(0,NY+1,0,NX+1,0,NZ+1);

	absorb_coeff=  f3tensor(1,NY,1,NX,1,NZ);

	/* averaged material parameters */
	uipjp=f3tensor(1,NY,1,NX,1,NZ);
	ujpkp=f3tensor(1,NY,1,NX,1,NZ);
	uipkp=f3tensor(1,NY,1,NX,1,NZ);
	rjp=f3tensor(1,NY,1,NX,1,NZ);
	rkp=f3tensor(1,NY,1,NX,1,NZ);
	rip=f3tensor(1,NY,1,NX,1,NZ);


	/* memory allocation for CPML variables*/
	if(ABS_TYPE==1){

		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);
		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);

		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);
		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);

		K_z = vector(1,2*FW);
		alpha_prime_z = vector(1,2*FW);
		a_z = vector(1,2*FW);
		b_z = vector(1,2*FW);
		K_z_half = vector(1,2*FW);
		alpha_prime_z_half = vector(1,2*FW);
		a_z_half = vector(1,2*FW);
		b_z_half = vector(1,2*FW);


		psi_sxx_x =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_sxy_x =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_sxz_x =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_syy_y =  f3tensor(1,2*FW,1,NX,1,NZ);
		psi_sxy_y =  f3tensor(1,2*FW,1,NX,1,NZ);
		psi_syz_y =  f3tensor(1,2*FW,1,NX,1,NZ);
		psi_szz_z =  f3tensor(1,NY,1,NX,1,2*FW);
		psi_sxz_z =  f3tensor(1,NY,1,NX,1,2*FW);
		psi_syz_z =  f3tensor(1,NY,1,NX,1,2*FW);


		psi_vxx   =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_vxxs  =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_vyy   =  f3tensor(1,2*FW,1,NX,1,NZ);
		psi_vzz   =  f3tensor(1,NY,1,NX,1,2*FW);
		psi_vxy   =  f3tensor(1,2*FW,1,NX,1,NZ);
		psi_vxz   =  f3tensor(1,NY,1,NX,1,2*FW);
		psi_vyx   =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_vyz   =  f3tensor(1,NY,1,NX,1,2*FW);
		psi_vzx   =  f3tensor(1,NY,1,2*FW,1,NZ);
		psi_vzy   =  f3tensor(1,2*FW,1,NX,1,NZ);

	}


	/* memory allocation for buffer arrays in which the wavefield information which is exchanged between neighboring PEs is stored */

	/* number of wavefield parameters that need to be exchanged - see exchange_v.c */
	nf1=(3*FDORDER/2)-1;
	nf2=nf1-1;

	bufferlef_to_rig = f3tensor(1,NY,1,NZ,1,nf1);
	bufferrig_to_lef = f3tensor(1,NY,1,NZ,1,nf2);
	buffertop_to_bot = f3tensor(1,NX,1,NZ,1,nf1);
	bufferbot_to_top = f3tensor(1,NX,1,NZ,1,nf2);
	bufferfro_to_bac = f3tensor(1,NY,1,NX,1,nf1);
	bufferbac_to_fro = f3tensor(1,NY,1,NX,1,nf2);

	sbufferlef_to_rig = f3tensor(1,NY,1,NZ,1,nf2);
	sbufferrig_to_lef = f3tensor(1,NY,1,NZ,1,nf1);
	sbuffertop_to_bot = f3tensor(1,NX,1,NZ,1,nf2);
	sbufferbot_to_top = f3tensor(1,NX,1,NZ,1,nf1);
	sbufferfro_to_bac = f3tensor(1,NY,1,NX,1,nf2);
	sbufferbac_to_fro = f3tensor(1,NY,1,NX,1,nf1);

	/* allocate buffer for seismogram output, merged seismogram section of all PEs */
	if (SEISMO) seismo_fulldata=fmatrix(1,ntr_glob,1,ns);

	/* allocate buffer for seismogram output, seismogram section of each PE */
	if (ntr>0){
		switch (SEISMO){
		case 1 : /* particle velocities only */
			sectionvx=fmatrix(1,ntr,1,ns);
			sectionvy=fmatrix(1,ntr,1,ns);
			sectionvz=fmatrix(1,ntr,1,ns);
			break;
		case 2 : /* pressure only */
			sectionp=fmatrix(1,ntr,1,ns);
			break;
		case 3 : /* curl and div only */
			sectioncurl=fmatrix(1,ntr,1,ns);
			sectiondiv=fmatrix(1,ntr,1,ns);
			break;
		case 4 : /* everything */
			sectionvx=fmatrix(1,ntr,1,ns);
			sectionvy=fmatrix(1,ntr,1,ns);
			sectionvz=fmatrix(1,ntr,1,ns);
			sectioncurl=fmatrix(1,ntr,1,ns);
			sectiondiv=fmatrix(1,ntr,1,ns);
			sectionp=fmatrix(1,ntr,1,ns);
			break;
		}
	}	


	if (MYID==0) 
		fprintf(FP," ... memory allocation for PE %d was successfull.\n\n", MYID);


	/* memory for source position definition */
	srcpos1=fmatrix(1,6,1,1);

	/* Reading source positions from SOURCE_FILE */ 	
	fprintf(FP,"\n ------------------ READING SOURCE PARAMETERS ------------------- \n");
	switch (SRCREC) {
	case 0:
		if (MYID==0) err("SRCREC parameter is invalid (SRCREC!=1)! No source parameters specified!");
		break;
	case 1:
		if (MYID==0) {
			fprintf(FP,"\n Reading source parameters from file: %s (SOFI3D source format)\n",SOURCE_FILE);

			if ((fpsrc=fopen(SOURCE_FILE,"r"))==NULL) err(" Source file could not be opened !");
			while(fgets(buffer, STRING_SIZE, fpsrc))
			{
				sscanf(buffer,"%s",bufferstring);
				/* checks if the line contains a '%'character which indicates a comment line,
					and if the reading of a string was successful, which is not the case for an empty line*/
				if ((strchr(buffer,'#')==0) && (sscanf(buffer,"%s",bufferstring)==1)) ++(nsrc);
			}
			rewind(fpsrc);
			if ((nsrc)==0) fprintf(FP,"\n WARNING: Could not determine number of sources parameter sets in input file. Assuming %d.\n",(nsrc=0));
			else fprintf(FP," Number of source positions specified in %s : %d \n",SOURCE_FILE,nsrc);
		}

		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Bcast(&nsrc,1,MPI_INT,0,MPI_COMM_WORLD);

		stype = ivector(1,nsrc);
		srcpos=sources(fpsrc,&nsrc,stype);

		/*originally, SOURCE_TYPE=stype is defined in the source file, if not, SOURCE_TYPE is taken from the input file */
		/*if (stype==NULL) printf("PE%d: Source type(s) undefined?! \n",MYID);*/

		break;
	case 2:
		if ((PLANE_WAVE_DEPTH>0)) {
			if (MYID==0) {
				/*stype=(int *)malloc(nsrc*sizeof(int));*/ /* for unknown reasons, the pointer does not point to memory that has been allocated by a subroutine this way */

				/*determining the number of sources in the specified plane normal/tilted to the surface/upper model boundary*/
				nsrc=(NXG-2*FW+1)*(NZG-2*FW+1);
				/*fprintf(FP,"\n nsrc= %i with NGX=%i, NYG=%i and FW=%i. \n",nsrc,NXG,NYG,FW);*/
			}

			MPI_Barrier(MPI_COMM_WORLD);
			MPI_Bcast(&nsrc,1,MPI_INT,0,MPI_COMM_WORLD);

			stype = ivector(1,nsrc);
			srcpos=pwsources(&nsrc,stype);

		}
		else {
			err("SRCREC parameter specifies PLANE_WAVE excitation, but PLANE_WAVE_DEPTH<=0!");
		}
		break;
		/*more source file formats or source file options can be implemented here*/
	default: err("SRCREC parameter is invalid (SRCREC!=1 or SRCREC!=2)! No source parameters specified!");
	break;
	}

	/* create model grids */
	fprintf(FP,"\n ------------------ MODEL CREATION AND OUTPUT-------------------- \n");
	if (READMOD) readmod(rho,pi,u,taus,taup,eta);
	else {
		if (L==0) model_elastic(rho,pi,u,taus,taup,eta); /* elastic modeling, L is specified in input file*/
        else {
            model_visco(rho,pi,u,taus,taup,eta); /* viscoelastic modeling, L is specified in input file*/
        }
	}
701 702
	
	
tilman.metz's avatar
tilman.metz committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	if (RUN_MULTIPLE_SHOTS) nshots=nsrc; else nshots=1;		
	/*printf("\n ------------------ checkfd by MYID %i -------------------- \n", MYID);*/
	/* check if the FD run will be stable and free of numerical dispersion */
	checkfd(FP,rho,pi,u,taus,taup,eta,srcpos,nsrc,recpos,ntr_glob);

	/* calculate 3-D array for exponential damping of reflections
           at the edges of the numerical mesh (PML-boundary)*/
	/*if(ABS_TYPE==1){   
	  absorb_PML(absorb_coeffx, absorb_coeffy, absorb_coeffz);
        }*/

	/* calculate damping coefficients for CPML boundary*/
	if(ABS_TYPE==1){
		CPML_coeff(K_x,alpha_prime_x,a_x,b_x,K_x_half,alpha_prime_x_half,a_x_half,b_x_half,K_y,alpha_prime_y,a_y,b_y,K_y_half,alpha_prime_y_half,a_y_half,b_y_half,K_z,alpha_prime_z,a_z,b_z,K_z_half,alpha_prime_z_half,a_z_half,b_z_half);
	}

	/* calculate 3-D array for exponential damping of reflections
	   at the edges of the numerical mesh */
	if(ABS_TYPE==2){   
		absorb(absorb_coeff);
	}

	/* For the calculation of the material parameters beteween gridpoints
	   the 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 */
	matcopy(rho,pi,u,taus,taup);

	/* spatial averaging of material parameters, i.e. Tau for S-waves, shear modulus, and density */
	av_mat(rho,pi,u,taus,taup,uipjp,ujpkp,uipkp,tausipjp,tausjpkp,tausipkp,rjp,rkp,rip);
    
    
	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, -1, NZ+2, vx,vy,vz,sxx,syy,szz,sxy,syz,sxz);
		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, bufferfro_to_bac,
	    bufferbac_to_fro, req_send, req_rec);

	comm_ini_s(sbufferlef_to_rig, sbufferrig_to_lef,
	    sbuffertop_to_bot, sbufferbot_to_top, sbufferfro_to_bac,
	    sbufferbac_to_fro, sreq_send, sreq_rec);*/

	/* initialisation of PML and ABS domain */
	if(ABS_TYPE==1){    
		CPML_ini_elastic(xb,yb,zb);
	}

	if(ABS_TYPE==2){    
		xb[0]=1; xb[1]=NX;
		yb[0]=1; yb[1]=NY;
		zb[0]=1; zb[1]=NZ;
	}

	if (MYID==0){
		time2=MPI_Wtime();
		fprintf(FP,"\n\n\n **************************************************\n");
		fprintf(FP," *********** STARTING TIME STEPPING ***************\n");
		fprintf(FP," **************************************************\n");
		fprintf(FP," real time before starting time loop: %4.2f s.\n",time2-time1);
	}


	for (ishot=1;ishot<=nshots;ishot++){

		fprintf(FP,"\n MYID=%d *****  Starting simulation for shot %d of %d  ********** \n",MYID,ishot,nshots);
		for (nt=1;nt<=6;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,6,1,1);
			if (stype_loc==NULL) stype_loc = ivector(1,nsrc);;
			srcpos_loc=splitsrc(srcpos1,&nsrc_loc, 1, stype_loc, stype);
		}
		else {
			/* Distribute multiple source positions on subdomains */
			if (stype_loc==NULL) stype_loc = ivector(1,nsrc);
			srcpos_loc = splitsrc(srcpos,&nsrc_loc, nsrc, stype_loc, stype);
		}

		/* calculate wavelet for each source point */
		signals=wavelet(srcpos_loc,nsrc_loc);

		/* output of calculated wavelet for each source point */

		if ((OUTSOURCEWAVELET !=0 ) && (nsrc_loc>0)) {
			char  source_signal_file[STRING_SIZE];
			sprintf(source_signal_file,"source_signal.MYID%d.shot%d.su",MYID,ishot);
			fprintf(FP,"\n PE %d outputs source time function in SU format to %s \n ", MYID, source_signal_file);
			output_source_signal(fopen(source_signal_file,"w"), signals, NT, 1);
		}
        
        /* initialize wavefield with zero */
        if (L==1 && ABS_TYPE==2) {
            zero(1-FDORDER/2,NX+FDORDER/2,1-FDORDER/2,NY+FDORDER/2,1-FDORDER/2,NZ+FDORDER/2,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,
                 vxyyx,vyzzy,vxzzx,vxxyyzz,vyyzz,vxxzz,vxxyy,vxyyx_2,vyzzy_2,vxzzx_2,vxxyyzz_2,vyyzz_2,vxxzz_2,vxxyy_2,vxyyx_3,
                 vyzzy_3,vxzzx_3,vxxyyzz_3,vyyzz_3,vxxzz_3,vxxyy_3,vxyyx_4,vyzzy_4,vxzzx_4,
                 vxxyyzz_4,vyyzz_4,vxxzz_4,vxxyy_4,svx,svy,svz,svx_2,svy_2,svz_2,svx_3,svy_3,svz_3,svx_4,svy_4,svz_4,rxx,ryy,rzz,rxy,ryz,rxz,rxx_2,ryy_2,rzz_2,rxy_2,ryz_2,rxz_2,rxx_3,ryy_3,rzz_3,rxy_3,ryz_3,rxz_3,rxx_4,ryy_4,rzz_4,rxy_4,ryz_4,rxz_4);
        }
        
		if(L==0 && ABS_TYPE==2){
            zero_elastic(1-FDORDER/2,NX+FDORDER/2,1-FDORDER/2,NY+FDORDER/2,1-FDORDER/2,NZ+FDORDER/2,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,
                         vxyyx,vyzzy,vxzzx,vxxyyzz,vyyzz,vxxzz,vxxyy,vxyyx_2,vyzzy_2,vxzzx_2,vxxyyzz_2,vyyzz_2,vxxzz_2,vxxyy_2,vxyyx_3,
                         vyzzy_3,vxzzx_3,vxxyyzz_3,vyyzz_3,vxxzz_3,vxxyy_3,vxyyx_4,vyzzy_4,vxzzx_4,
                         vxxyyzz_4,vyyzz_4,vxxzz_4,vxxyy_4,svx,svy,svz,svx_2,svy_2,svz_2,svx_3,svy_3,svz_3,svx_4,svy_4,svz_4);
		}
		if(ABS_TYPE==1){
			zero_elastic_CPML(NX,NY,NZ,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,rxx,ryy,rzz,rxy,ryz,rxz,psi_sxx_x,psi_sxy_x,psi_sxz_x,psi_sxy_y,psi_syy_y,psi_syz_y,psi_sxz_z,psi_syz_z,psi_szz_z,psi_vxx,psi_vyx,psi_vzx,psi_vxy,psi_vyy,psi_vzy,psi_vxz,psi_vyz,psi_vzz,rxx_2,ryy_2,rzz_2,rxy_2,ryz_2,rxz_2,rxx_3,ryy_3,rzz_3,rxy_3,ryz_3,rxz_3,rxx_4,ryy_4,rzz_4,rxy_4,ryz_4,rxz_4);

		}
        
		/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
		/* start of loop over time steps */

		lsamp=NDTSHIFT+1;
		nlsamp=1;
		for (nt=1;nt<=NT;nt++){

			time_v_update[nt]=0.0;
			time_s_update[nt]=0.0;
			time_v_exchange[nt]=0.0;
			time_s_exchange[nt]	=0.0;

			/* Check if simulation is still stable */
			if (isnan(vy[NY/2][NX/2][NZ/2])) err(" Simulation is unstable !"); /* maybe just breaking the loop would be better */

			if (LOG)
				if ((MYID==0) && ((nt+(OUTNTIMESTEPINFO-1))%OUTNTIMESTEPINFO)==0) {
					fprintf(FP,"\n Computing timestep %d of %d \n",nt,NT);
					time2=MPI_Wtime();
				}

			/* update of particle velocities */
			time_v_update[nt]=update_v(xb[0],xb[1],yb[0],yb[1],zb[0],zb[1],nt,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,rho,rjp, rkp,rip,srcpos_loc,signals,nsrc_loc,absorb_coeff,stype_loc,svx,svy,svz,svx_2,svy_2,svz_2,svx_3,svy_3,svz_3,svx_4,svy_4,svz_4);

			if(ABS_TYPE==1){
				update_v_CPML(xb[0],xb[1],yb[0],yb[1],zb[0],zb[1],nt,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,rho,rjp,rkp,rip,srcpos_loc,signals,nsrc_loc,absorb_coeff,stype_loc,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,K_z,a_z,b_z,K_z_half,a_z_half,b_z_half,psi_sxx_x,psi_sxy_x,psi_sxz_x,psi_sxy_y,psi_syy_y,psi_syz_y,psi_sxz_z,psi_syz_z,psi_szz_z);}
			;
            
            /* Shift spartial derivations of the stress one time step back */
            if (FDORDER_TIME==4){
                shift_s1=svx_4;svx_4=svx_3;svx_3=svx_2;svx_2=svx;svx=shift_s1;
                shift_s2=svy_4;svy_4=svy_3;svy_3=svy_2;svy_2=svy;svy=shift_s2;
                shift_s3=svz_4;svz_4=svz_3;svz_3=svz_2;svz_2=svz;svz=shift_s3;
            }
            if (FDORDER_TIME==3){
                shift_s1=svx_3; svx_3=svx_2;svx_2=svx;svx=shift_s1;
                shift_s2=svy_3;svy_3=svy_2;svy_2=svy;svy=shift_s2;
                shift_s3=svz_3;svz_3=svz_2; svz_2=svz;svz=shift_s3;
            }

			/* exchange values of particle velocities at grid boundaries between PEs */

			time_v_exchange[nt]=exchange_v(nt,vx,vy,vz, bufferlef_to_rig, bufferrig_to_lef, buffertop_to_bot, bufferbot_to_top,
					bufferfro_to_bac, bufferbac_to_fro, req_send, req_rec);

			/* update of components of stress tensor */

            
            /* update NON PML boundaries */
            if (L>0){
                
                time_s_update[nt]=update_s(xb[0],xb[1],yb[0],yb[1],zb[0],zb[1],nt,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,rxx,ryy,rzz,rxy,ryz,rxz,
                                           pi,u,uipjp,ujpkp,uipkp,taus,tausipjp,tausjpkp,tausipkp,taup,eta,vxyyx,vyzzy,vxzzx,vxxyyzz,vyyzz,vxxzz,vxxyy,vxyyx_2,vyzzy_2,
                                           vxzzx_2,vxxyyzz_2,vyyzz_2,vxxzz_2,vxxyy_2,vxyyx_3,vyzzy_3,vxzzx_3,vxxyyzz_3,vyyzz_3,vxxzz_3,vxxyy_3,vxyyx_4,vyzzy_4,vxzzx_4,
                                           vxxyyzz_4,vyyzz_4,vxxzz_4,vxxyy_4,rxx_2,ryy_2,rzz_2,rxy_2,ryz_2,rxz_2,rxx_3,ryy_3,rzz_3,rxy_3,ryz_3,rxz_3,rxx_4,ryy_4,rzz_4,rxy_4,ryz_4,rxz_4);
                if(ABS_TYPE==1) update_s_CPML(xb[0],xb[1],yb[0],yb[1],zb[0],zb[1],nt,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,rxx,ryy,rzz,
                                              rxy,ryz,rxz,pi,u,uipjp,ujpkp,uipkp,taus,tausipjp,tausjpkp,tausipkp,taup,eta,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,K_z,a_z,b_z,K_z_half,a_z_half,b_z_half,
                                              psi_vxx,psi_vyx,psi_vzx,psi_vxy,psi_vyy,psi_vzy,psi_vxz,psi_vyz,psi_vzz);
            } else{
                time_s_update[nt]=update_s_elastic(xb[0],xb[1],yb[0],yb[1],zb[0],zb[1],nt,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,rxx,ryy,rzz,rxy,ryz,rxz,
                                                   pi,u,uipjp,ujpkp,uipkp,taus,tausipjp,tausjpkp,tausipkp,taup,eta,vxyyx,vyzzy,vxzzx,vxxyyzz,vyyzz,vxxzz,vxxyy,vxyyx_2,vyzzy_2,
                                                   vxzzx_2,vxxyyzz_2,vyyzz_2,vxxzz_2,vxxyy_2,vxyyx_3,vyzzy_3,vxzzx_3,vxxyyzz_3,vyyzz_3,vxxzz_3,vxxyy_3,vxyyx_4,vyzzy_4,vxzzx_4,
                                                   vxxyyzz_4,vyyzz_4,vxxzz_4,vxxyy_4);
                if(ABS_TYPE==1) update_s_CPML_elastic(xb[0],xb[1],yb[0],yb[1],zb[0],zb[1],nt,vx,vy,vz,sxx,syy,szz,sxy,syz,sxz,
                                                      pi,u,uipjp,ujpkp,uipkp,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,K_z,a_z,b_z,K_z_half,a_z_half,b_z_half,
                                                      psi_vxx,psi_vyx,psi_vzx,psi_vxy,psi_vyy,psi_vzy,psi_vxz,psi_vyz,psi_vzz);
            }
           
            /* Shift spartial derivations from the velocity one time step back */
            if (FDORDER_TIME==4){
                shift_v1=vxyyx_4;vxyyx_4=vxyyx_3;vxyyx_3=vxyyx_2;vxyyx_2=vxyyx;vxyyx=shift_v1;
                shift_v2=vyzzy_4;vyzzy_4=vyzzy_3;vyzzy_3=vyzzy_2;vyzzy_2=vyzzy;vyzzy=shift_v2;
                shift_v3=vxzzx_4;vxzzx_4=vxzzx_3;vxzzx_3=vxzzx_2;vxzzx_2=vxzzx;vxzzx=shift_v3;
                shift_v4=vxxyyzz_4;vxxyyzz_4=vxxyyzz_3;vxxyyzz_3=vxxyyzz_2;vxxyyzz_2=vxxyyzz;vxxyyzz=shift_v4;
                shift_v5=vyyzz_4;vyyzz_4=vyyzz_3;vyyzz_3=vyyzz_2;vyyzz_2=vyyzz;vyyzz=shift_v5;
                shift_v6=vxxzz_4;vxxzz_4=vxxzz_3;vxxzz_3=vxxzz_2; vxxzz_2=vxxzz;vxxzz=shift_v6;
                shift_v7=vxxyy_4;vxxyy_4=vxxyy_3;vxxyy_3=vxxyy_2;vxxyy_2=vxxyy;vxxyy=shift_v7;
                if (L==1) {
                    shift_r1=rxy_4;rxy_4=rxy_3; rxy_3=rxy_2;rxy_2=rxy;rxy=shift_r1;
                    shift_r2=ryz_4;ryz_4=ryz_3;ryz_3=ryz_2;ryz_2=ryz;ryz=shift_r2;
                    shift_r3=rxz_4;rxz_4=rxz_3;rxz_3=rxz_2;rxz_2=rxz;rxz=shift_r3;
                    shift_r4=rxx_4;rxx_4=rxx_3;rxx_3=rxx_2;rxx_2=rxx;rxx=shift_r4;
                    shift_r5=ryy_4;ryy_4=ryy_3;ryy_3=ryy_2;ryy_2=ryy;ryy=shift_r5;
                    shift_r6=rzz_4;rzz_4=rzz_3;rzz_3=rzz_2;rzz_2=rzz;rzz=shift_r6;
                }
                
            }
            if (FDORDER_TIME==3){
                shift_v1=vxyyx_3;vxyyx_3=vxyyx_2;vxyyx_2=vxyyx;vxyyx=shift_v1;
                shift_v2=vyzzy_3;vyzzy_3=vyzzy_2;vyzzy_2=vyzzy;vyzzy=shift_v2;
                shift_v3=vxzzx_3;vxzzx_3=vxzzx_2;vxzzx_2=vxzzx;vxzzx=shift_v3;
                shift_v4=vxxyyzz_3;vxxyyzz_3=vxxyyzz_2;vxxyyzz_2=vxxyyzz;vxxyyzz=shift_v4;
                shift_v5=vyyzz_3;vyyzz_3=vyyzz_2;vyyzz_2=vyyzz;vyyzz=shift_v5;
                shift_v6=vxxzz_3;vxxzz_3=vxxzz_2;vxxzz_2=vxxzz;vxxzz=shift_v6;
                shift_v7=vxxyy_3;vxxyy_3=vxxyy_2;vxxyy_2=vxxyy;vxxyy=shift_v7;
                if (L==1) {
                    shift_r1=rxy_3;rxy_3=rxy_2;rxy_2=rxy;rxy=shift_r1;
                    shift_r2=ryz_3;ryz_3=ryz_2;ryz_2=ryz;ryz=shift_r2;
                    shift_r3=rxz_3;rxz_3=rxz_2;rxz_2=rxz;rxz=shift_r3;
                    shift_r4=rxx_3;rxx_3=rxx_2;rxx_2=rxx;rxx=shift_r4;
                    shift_r5=ryy_3;ryy_3=ryy_2;ryy_2=ryy;ryy=shift_r5;
                    shift_r6=rzz_3;rzz_3=rzz_2;rzz_2=rzz;rzz=shift_r6;
                }
            }

			/* explosive source */
			if (!CHECKPTREAD){
				psource(nt,sxx,syy,szz,srcpos_loc,signals,nsrc_loc,stype_loc);
				/*
				 * eqsource is a implementation of moment tensor points sources. */
				 eqsource(nt,sxx,syy,szz,sxy, syz, sxz, srcpos_loc,signals,nsrc_loc,stype_loc, amon, str, dip, rake);
			}
			
			/* stress free surface ? */
			if ((FREE_SURF) && (POS[2]==0)){
946 947 948 949
				if (L) surface(1,u,pi,taus,taup,eta,sxx,syy,szz,sxy,syz,rxx,ryy,rzz,vx,vy,vz,K_x,a_x,b_x,
                                                      K_z,a_z,b_z,psi_vxx, psi_vzz);
				else surface_elastic(1,u,pi,sxx,syy,szz,sxy,syz,vx,vy,vz,K_x,a_x,b_x,
                                                      K_z,a_z,b_z,psi_vxx, psi_vzz);
tilman.metz's avatar
tilman.metz committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
			}

			/* exchange values of stress at boundaries between PEs */
			time_s_exchange[nt]=exchange_s(nt,sxx,syy,szz,sxy,syz,sxz,sbufferlef_to_rig,sbufferrig_to_lef,sbuffertop_to_bot,sbufferbot_to_top,sbufferfro_to_bac,sbufferbac_to_fro, sreq_send, sreq_rec);


			/* store amplitudes at receivers in e.g. sectionvx, sectionvz, sectiondiv, ...*/
			if ((SEISMO) && (ntr>0) && (nt==lsamp)){
				seismo(nlsamp,ntr,recpos_loc,sectionvx,sectionvy,sectionvz,
						sectiondiv,sectioncurl,sectionp,vx,vy,vz,sxx,syy,szz,pi,u);
				nlsamp++;
				lsamp+=NDT;
			}


			/* save snapshot in file */
			if ((SNAP) && (nt==lsnap) && (nt<=TSNAP2/DT)){
				snap(FP,nt,++nsnap,SNAP_FORMAT,SNAP,vx,vy,vz,sxx,syy,szz,u,pi,
						IDX,IDY,IDZ,1,1,1,NX,NY,NZ);
				lsnap=lsnap+iround(TSNAPINC/DT);
			}

			if (LOG)
				if ((MYID==0) && ((nt+(OUTNTIMESTEPINFO-1))%OUTNTIMESTEPINFO)==0) {
					time3=MPI_Wtime();
					time_timestep[nt]=(time3-time2);
					fprintf(FP," total real time for timestep %d : \t\t %4.2f s.\n",nt,time3-time2);
				}


		} /* end of loop over timesteps */
		/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

		fprintf(FP, "\n\n *********** Finish TIME STEPPING ****************\n");
		fprintf(FP, " **************************************************\n\n");

		/* write seismograms to file(s) */
		if (SEISMO){

			/* saves seismograms portion of each PE individually to file */
			//if (ntr>0) saveseis(FP,sectionvx,sectionvy,sectionvz,sectionp,sectioncurl,sectiondiv,recpos,recpos_loc,ntr,srcpos1,ishot,ns);

			/* merge of seismogram data from all PE and output data collectively */
			switch (SEISMO){
			case 1 : /* particle velocities only */
				catseis(sectionvx, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,1);
				catseis(sectionvy, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,2);
				catseis(sectionvz, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,3);

				break;
			case 2 : /* pressure only */
				catseis(sectionp, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,4);

				break;
			case 3 : /* curl and div only */
				catseis(sectiondiv, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,5);
				catseis(sectioncurl, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,6);

				break;
			case 4 : /* everything */
				/*fprintf(FP," start merging, ntr= %d : \n",ntr_glob);
				fprintf(stdout,"Message from PE %d\n",MYID);*/
				catseis(sectionvx, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,1);
				catseis(sectionvy, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,2);
				catseis(sectionvz, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,3);
				catseis(sectionp, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,4);
				catseis(sectiondiv, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,5);
				catseis(sectioncurl, seismo_fulldata, recswitch, ntr_glob,ns);
				if (MYID==0) saveseis_glob(FP,seismo_fulldata,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,6);

				break;
			default :	break;

			}
			fprintf(FP, "\n\n");

		}

		/* output timing information (real times for update and exchange) */
		if (LOG)
			if (MYID==0) timing(time_v_update,time_s_update, time_s_exchange,time_v_exchange,time_timestep, ishot);


	} /* end of loop over shots */

	if (CHECKPTWRITE){
		if (MYID==0){
			time3=MPI_Wtime();
			fprintf(FP," Saving wavefield to check-point file %s \n",CHECKPTFILE);
		}

		save_checkpoint(-1, NX+2, -1, NY+2, -1, NZ+2, vx,vy,vz,sxx,syy,szz,sxy,syz,sxz);
		MPI_Barrier(MPI_COMM_WORLD);
		if (MYID==0){
			time4=MPI_Wtime();
			fprintf(FP," finished (real time: %4.2f s).\n",time4-time3);
		}
	}

	l=1;
	if(ABS_TYPE==1 && FDORDER==2){l=2;}

	/*de-allocation of memory */
	if(POS[2]==0){
		free_f3tensor(vx,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(vy,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(vz,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(sxy,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(syz,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        
        if(FDORDER_TIME != 2){
            free_f3tensor(vxyyx,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyzzy,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxzzx,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyyzz,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyyzz,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxzz,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyy,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);

            free_f3tensor(vxyyx_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyzzy_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxzzx_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyyzz_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyyzz_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxzz_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyy_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(vxyyx_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyzzy_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxzzx_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyyzz_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyyzz_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxzz_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyy_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(svx,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svy,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svz,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(svx_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svy_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svz_2,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(svx_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svy_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svz_3,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            
            if(FDORDER_TIME==4){
                free_f3tensor(vxyyx_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vyzzy_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxzzx_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxxyyzz_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vyyzz_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxxzz_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxxyy_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                
                free_f3tensor(svx_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(svy_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(svz_4,0-FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            }

        }

	}

	if(POS[2]>0){
		free_f3tensor(vx,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(vy,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(vz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(sxy,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
		free_f3tensor(syz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
        
        if(FDORDER_TIME != 2){
            free_f3tensor(vxyyx,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyzzy,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxzzx,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyyzz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyyzz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxzz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyy,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(vxyyx_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyzzy_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxzzx_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyyzz_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyyzz_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxzz_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyy_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(vxyyx_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyzzy_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxzzx_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyyzz_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vyyzz_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxzz_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(vxxyy_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(svx,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svy,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(svx_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svy_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svz_2,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            free_f3tensor(svx_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svy_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            free_f3tensor(svz_3,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            
            
            if(FDORDER_TIME==4){
                free_f3tensor(vxyyx_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vyzzy_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxzzx_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxxyyzz_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vyyzz_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxxzz_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(vxxyy_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                
                free_f3tensor(svx_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(svy_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
                free_f3tensor(svz_4,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
            }
            
        }
        
        
	}

	free_f3tensor(sxz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	free_f3tensor(sxx,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	free_f3tensor(syy,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);
	free_f3tensor(szz,1-l*FDORDER/2,NY+l*FDORDER/2,1-l*FDORDER/2,NX+l*FDORDER/2,1-l*FDORDER/2,NZ+l*FDORDER/2);

	if(ABS_TYPE==1){

		free_vector(K_x,1,2*FW);
		free_vector(alpha_prime_x,1,2*FW);
		free_vector(a_x,1,2*FW);
		free_vector(b_x,1,2*FW);
		free_vector(K_x_half,1,2*FW);
		free_vector(alpha_prime_x_half,1,2*FW);
		free_vector(a_x_half,1,2*FW);
		free_vector(b_x_half,1,2*FW);

		free_vector(K_y,1,2*FW);
		free_vector(alpha_prime_y,1,2*FW);
		free_vector(a_y,1,2*FW);
		free_vector(b_y,1,2*FW);
		free_vector(K_y_half,1,2*FW);
		free_vector(alpha_prime_y_half,1,2*FW);
		free_vector(a_y_half,1,2*FW);
		free_vector(b_y_half,1,2*FW);

		free_vector(K_z,1,2*FW);
		free_vector(alpha_prime_z,1,2*FW);
		free_vector(a_z,1,2*FW);
		free_vector(b_z,1,2*FW);
		free_vector(K_z_half,1,2*FW);
		free_vector(alpha_prime_z_half,1,2*FW);
		free_vector(a_z_half,1,2*FW);
		free_vector(b_z_half,1,2*FW);

		free_f3tensor(psi_sxx_x,1,NY,1,2*FW,1,NZ);
		free_f3tensor(psi_syy_y,1,2*FW,1,NX,1,NZ);
		free_f3tensor(psi_szz_z,1,NY,1,NX,1,2*FW);
		free_f3tensor(psi_sxy_x,1,NY,1,2*FW,1,NZ);
		free_f3tensor(psi_sxy_y,1,2*FW,1,NX,1,NZ);
		free_f3tensor(psi_sxz_x,1,NY,1,2*FW,1,NZ);
		free_f3tensor(psi_sxz_z,1,NY,1,NX,1,2*FW);
		free_f3tensor(psi_syz_y,1,2*FW,1,NX,1,NZ);
		free_f3tensor(psi_syz_z,1,NY,1,NX,1,2*FW);

		free_f3tensor(psi_vxx,1,NY,1,2*FW,1,NZ);
		free_f3tensor(psi_vyy,1,2*FW,1,NX,1,NZ);
		free_f3tensor(psi_vzz,1,NY,1,NX,1,2*FW);
		free_f3tensor(psi_vxy,1,2*FW,1,NX,1,NZ);
		free_f3tensor(psi_vyx,1,NY,1,2*FW,1,NZ);
		free_f3tensor(psi_vxz,1,NY,1,NX,1,2*FW);
		free_f3tensor(psi_vzx,1,NY,1,2*FW,1,NZ);
		free_f3tensor(psi_vyz,1,NY,1,NX,1,2*FW);
		free_f3tensor(psi_vzy,1,2*FW,1,NX,1,NZ);
	}

	if (L) {
        free_f3tensor(rxx,1,NY,1,NX,1,NZ);
        free_f3tensor(ryy,1,NY,1,NX,1,NZ);
        free_f3tensor(rzz,1,NY,1,NX,1,NZ);
        free_f3tensor(rxy,1,NY,1,NX,1,NZ);
        free_f3tensor(ryz,1,NY,1,NX,1,NZ);
        free_f3tensor(rxz,1,NY,1,NX,1,NZ);
        if (FDORDER_TIME>2) {
            free_f3tensor(rxx_2,1,NY,1,NX,1,NZ);
            free_f3tensor(ryy_2,1,NY,1,NX,1,NZ);
            free_f3tensor(rzz_2,1,NY,1,NX,1,NZ);
            free_f3tensor(rxy_2,1,NY,1,NX,1,NZ);
            free_f3tensor(ryz_2,1,NY,1,NX,1,NZ);
            free_f3tensor(rxz_2,1,NY,1,NX,1,NZ);
            free_f3tensor(rxx_3,1,NY,1,NX,1,NZ);
            free_f3tensor(ryy_3,1,NY,1,NX,1,NZ);
            free_f3tensor(rzz_3,1,NY,1,NX,1,NZ);
            free_f3tensor(rxy_3,1,NY,1,NX,1,NZ);
            free_f3tensor(ryz_3,1,NY,1,NX,1,NZ);
            free_f3tensor(rxz_3,1,NY,1,NX,1,NZ);
            if (FDORDER_TIME==4) {
                free_f3tensor(rxx_4,1,NY,1,NX,1,NZ);
                free_f3tensor(ryy_4,1,NY,1,NX,1,NZ);
                free_f3tensor(rzz_4,1,NY,1,NX,1,NZ);
                free_f3tensor(rxy_4,1,NY,1,NX,1,NZ);
                free_f3tensor(ryz_4,1,NY,1,NX,1,NZ);
                free_f3tensor(rxz_4,1,NY,1,NX,1,NZ);
            }
        }
		free_f3tensor(taus,0,NY+1,0,NX+1,0,NZ+1);
		free_f3tensor(taup,0,NY+1,0,NX+1,0,NZ+1);
		free_vector(eta,1,L);
		free_f3tensor(tausipjp,1,NY,1,NX,1,NZ);
		free_f3tensor(tausjpkp,1,NY,1,NX,1,NZ);
		free_f3tensor(tausipkp,1,NY,1,NX,1,NZ);

	}

	free_f3tensor(rho,0,NY+1,0,NX+1,0,NZ+1);
	free_f3tensor(pi,0,NY+1,0,NX+1,0,NZ+1);
	free_f3tensor(u,0,NY+1,0,NX+1,0,NZ+1);
	free_f3tensor(absorb_coeff,1,NY,1,NX,1,NZ);

	/* averaged material parameters */
	free_f3tensor(uipjp,1,NY,1,NX,1,NZ);
	free_f3tensor(ujpkp,1,NY,1,NX,1,NZ);
	free_f3tensor(uipkp,1,NY,1,NX,1,NZ);


	free_f3tensor(rjp,1,NY,1,NX,1,NZ);
	free_f3tensor(rkp,1,NY,1,NX,1,NZ);
	free_f3tensor(rip,1,NY,1,NX,1,NZ);

	if (nsrc_loc>0){
		free_matrix(signals,1,nsrc_loc,1,NT);
		free_matrix(srcpos_loc,1,6,1,nsrc_loc);
	}


	free_f3tensor(bufferlef_to_rig,1,NY,1,NZ,1,nf1);
	free_f3tensor(bufferrig_to_lef,1,NY,1,NZ,1,nf2);
	free_f3tensor(buffertop_to_bot,1,NX,1,NZ,1,nf1);
	free_f3tensor(bufferbot_to_top,1,NX,1,NZ,1,nf2);
	free_f3tensor(bufferfro_to_bac,1,NY,1,NX,1,nf1);
	free_f3tensor(bufferbac_to_fro,1,NY,1,NX,1,nf2);

	free_f3tensor(sbufferlef_to_rig,1,NY,1,NZ,1,nf2);
	free_f3tensor(sbufferrig_to_lef,1,NY,1,NZ,1,nf1);
	free_f3tensor(sbuffertop_to_bot,1,NX,1,NZ,1,nf2);
	free_f3tensor(sbufferbot_to_top,1,NX,1,NZ,1,nf1);
	free_f3tensor(sbufferfro_to_bac,1,NY,1,NX,1,nf2);
	free_f3tensor(sbufferbac_to_fro,1,NY,1,NX,1,nf1);

	/* free memory for global receiver source positions */
	if (SEISMO>0) {
		free_imatrix(recpos,1,3,1,ntr_glob);
	}

	/* free memory for global source positions */
	free_matrix(srcpos,1,6,1,nsrc);


	if ((ntr>0) && (SEISMO>0)){

		free_imatrix(recpos_loc,1,3,1,ntr);
		free_matrix(seismo_fulldata,1,ntr_glob,1,ns);

		switch (SEISMO){
		case 1 : /* particle velocities only */
			free_matrix(sectionvx,1,ntr,1,ns);
			free_matrix(sectionvy,1,ntr,1,ns);
			free_matrix(sectionvz,1,ntr,1,ns);
			break;
		case 2 : /* pressure only */
			free_matrix(sectionp,1,ntr,1,ns);
			break;
		case 3 : /* curl and div only */
			free_matrix(sectioncurl,1,ntr,1,ns);
			free_matrix(sectiondiv,1,ntr,1,ns);
			break;
		case 4 : /* everything */
			free_matrix(sectionvx,1,ntr,1,ns);
			free_matrix(sectionvy,1,ntr,1,ns);
			free_matrix(sectionvz,1,ntr,1,ns);
			free_matrix(sectionp,1,ntr,1,ns);
			free_matrix(sectioncurl,1,ntr,1,ns);
			free_matrix(sectiondiv,1,ntr,1,ns);
			break;
		}

	}

	/* free memory for source position definition */
	free_matrix(srcpos1,1,6,1,1);
	free_ivector(stype,1,nsrc);
	free_ivector(stype_loc,1,nsrc);

	/* de-allocate buffer for messages */
	MPI_Buffer_detach(buff_addr,&buffsize);


	MPI_Barrier(MPI_COMM_WORLD);

	/* merge snapshot files created by the PEs into one file */
	/* if ((SNAP) && (MYID==0)) snapmerge(nsnap);*/

	free_ivector(xb,0,1);
	free_ivector(yb,0,1);
	free_ivector(zb,0,1);

	/* free timing arrays */
	free_dvector(time_v_update,1,NT);
	free_dvector(time_s_update,1,NT);
	free_dvector(time_s_exchange,1,NT);
	free_dvector(time_v_exchange,1,NT);
	free_dvector(time_timestep,1,NT);


	if (MYID==0){
		fprintf(FP,"\n **Info from main (written by PE %d): \n",MYID);
		time4=MPI_Wtime();
		fprintf(FP," Total real time of program: %4.2f seconds.\n\n",time4-time1);
		fprintf(FP," ******************************************************\n");
		fprintf(FP," **************** SOFI3D has finished *****************\n");
		fprintf(FP," ******************************************************\n\n");
	}


	fclose(FP);

	MPI_Finalize();

	return 0;

}