IFOS2D.c 241 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
Tilman Steinweg's avatar
Tilman Steinweg committed
3
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
4
 * This file is part of IFOS.
5
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
6
 * IFOS is free software: you can redistribute it and/or modify
Tilman Steinweg's avatar
Tilman Steinweg committed
7 8
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
9
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
10
 * IFOS is distributed in the hope that it will be useful,
Tilman Steinweg's avatar
Tilman Steinweg committed
11 12 13
 * 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.
14
 *
Tilman Steinweg's avatar
Tilman Steinweg committed
15
 * You should have received a copy of the GNU General Public License
Florian Wittkamp's avatar
Florian Wittkamp committed
16
 * along with IFOS. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
17
 -----------------------------------------------------------------------------------------*/
Tilman Steinweg's avatar
Tilman Steinweg committed
18 19

/* ----------------------------------------------------------------------
Florian Wittkamp's avatar
Florian Wittkamp committed
20
 * This is program IFOS.
21 22 23 24 25 26 27 28
 * subwavelength DEtail resolving Nonlinear Iterative SEismic inversion
 *
 * If you use this code for your own research please cite at least one article
 * written by the developers of the package, e.g.
 * D. K�hn. Time domain 2D elastic full waveform tomography. PhD Thesis, Kiel
 * University, 2011.
 *
 *  ----------------------------------------------------------------------*/
Tilman Steinweg's avatar
Tilman Steinweg committed
29 30 31 32 33 34 35 36 37 38


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

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

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

int main(int argc, char **argv){
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    /* variables in main */
    int ns, nseismograms=0, nt, nd, fdo3, j, i, ii, jj, shotid, recid, k, nc, iter, h, infoout, SHOTINC, TIMEWIN, test_eps, lq, iq, jq, hin, hin1, s=0;
    int NTDTINV, nxny, nxnyi, imat, imat1, imat2, IDXI, IDYI, hi, NTST, NTSTI, partest;
    int lsnap, nsnap=0, lsamp=0, buffsize, invtime, invtimer, sws, swstestshot, snapseis, snapseis1, PML;
    int ntr=0, ntr_loc=0, ntr_glob=0, nsrc=0, nsrc_loc=0, nsrc_glob=0, ishot, irec, nshots=0, nshots1, Lcount, itest, Lcountsum, itestshot;
    
    float pum, ppim, ppim1, ppim2, thetaf, thetab, e33, e33b, e11, e11b, muss, lamss;
    float memdyn, memmodel, memseismograms, membuffer, memtotal, dngn, fphi, sum, avggrad, beta, betan, betaz, betaLog, betaVp, betaVs, betarho, eps_scale, L2old;
    float fac1, fac2, wavefor, waverecipro, dump, dump1, epsilon, gradsign, mun, eps1, gradplastiter, gradglastiter, gradclastiter, betar, sig_max, sig_max1;
    float signL1, RMS, opteps_vp, opteps_vs, opteps_rho, Vs, Vp, Vp_avg, C_vp, Vs_avg, C_vs, Cd, rho_avg, C_rho, Vs_sum, Vp_sum, rho_sum, Zp, Zs;
    float freqshift, dfreqshift, memfwt, memfwt1, memfwtdata;
    char *buff_addr, ext[10], *fileinp;
    char wave_forward[225], wave_recipro[225], wave_conv[225], jac[225], jac2[225], jacsum[225], dwavelet[225], vyf[STRING_SIZE];
    
    double time1, time2, time3, time4, time5, time6, time7, time8,
    time_av_v_update=0.0, time_av_s_update=0.0, time_av_v_exchange=0.0,
    time_av_s_exchange=0.0, time_av_timestep=0.0;
    
    float L2, L2sum, L2_all_shots, L2sum_all_shots, *L2t, alphanomsum, alphanom, alphadenomsum, alphadenom, scaleamp ,sdummy, lamr;
    int sum_killed_traces=0, sum_killed_traces_testshots=0, killed_traces=0, killed_traces_testshots=0;
    int *ptr_killed_traces=&killed_traces, *ptr_killed_traces_testshots=&killed_traces_testshots;
    
    float energy, energy_sum, energy_all_shots, energy_sum_all_shots;
62 63
    float energy_SH, energy_sum_SH, energy_all_shots_SH, energy_sum_all_shots_SH;
    float L2_SH, L2sum_SH, L2_all_shots_SH, L2sum_all_shots_SH;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    
    // Pointer for dynamic wavefields:
    float  **  psxx, **  psxy, **  psyy, **  psxz, **  psyz, **psp, ** ux, ** uy, ** uz, ** uxy, ** uyx, ** Vp0, ** uttx, ** utty, ** Vs0, ** Rho0;
    float  **  pvx, **  pvy, **  pvz, **waveconv, **waveconv_lam, **waveconv_mu, **waveconv_rho, **waveconv_rho_s, **waveconv_u, **waveconvtmp, **wcpart, **wavejac,**waveconv_rho_s_z,**waveconv_u_z,**waveconv_rho_z;
    float **waveconv_shot, **waveconv_u_shot, **waveconv_rho_shot, **waveconv_u_shot_z, **waveconv_rho_shot_z;
    float  **  pvxp1, **  pvyp1, **  pvzp1, **  pvxm1, **  pvym1, **  pvzm1;
    float ** gradg, ** gradp,** gradg_rho, ** gradp_rho, ** gradg_u, ** gradp_u, ** gradp_u_z,** gradp_rho_z;
    float  **  prho,**  prhonp1, **prip=NULL, **prjp=NULL, **pripnp1=NULL, **prjpnp1=NULL, **  ppi, **  pu, **  punp1, **  puipjp, **  ppinp1;
    float  **  vpmat, ***forward_prop_x, ***forward_prop_y, ***forward_prop_rho_x, ***forward_prop_u, ***forward_prop_rho_y, ***forward_prop_p;
    
    float ***forward_prop_z_xz,***forward_prop_z_yz,***forward_prop_rho_z,**waveconv_mu_z;
    float ** uxz, ** uyz;
    
    float  ** sectionvx=NULL, ** sectionvy=NULL, ** sectionvz=NULL, ** sectionp=NULL, ** sectionpnp1=NULL,
    ** sectioncurl=NULL, ** sectiondiv=NULL, ** sectionvxdata=NULL, ** sectionvydata=NULL, ** sectionvzdata=NULL, ** sectionvxdiff=NULL, ** sectionvzdiff=NULL, ** sectionvxdiffold=NULL, ** sectionvydiffold=NULL, ** sectionvzdiffold=NULL,** sectionpdata=NULL, ** sectionpdiff=NULL, ** sectionpdiffold=NULL,
    ** sectionvydiff=NULL, ** sectionpn=NULL, ** sectionread=NULL, ** sectionvy_conv=NULL, ** sectionvy_obs=NULL, ** sectionvx_conv=NULL,** sectionvx_obs=NULL, ** sectionvz_conv=NULL,** sectionvz_obs=NULL,
    ** sectionp_conv=NULL,** sectionp_obs=NULL, * source_time_function=NULL;
    float  **  absorb_coeff, ** taper_coeff, * epst1, * epst2,  * epst3, * picked_times;
82
    float  ** srcpos=NULL, **srcpos_loc=NULL, ** srcpos1=NULL, **srcpos_loc_back=NULL, ** signals=NULL,** signals_SH=NULL, ** signals_rec=NULL, *hc=NULL;
83 84 85 86 87 88 89 90 91 92 93 94 95 96
    int   ** recpos=NULL, ** recpos_loc=NULL;
    /*int   ** tracekill=NULL, TRKILL, DTRKILL;*/
    int * DTINV_help;
    
    float ** bufferlef_to_rig,  ** bufferrig_to_lef, ** buffertop_to_bot, ** bufferbot_to_top;
    
    /* PML variables */
    float * d_x, * K_x, * alpha_prime_x, * a_x, * b_x, * d_x_half, * K_x_half, * alpha_prime_x_half, * a_x_half, * b_x_half, * d_y, * K_y, * alpha_prime_y, * a_y, * b_y, * d_y_half, * K_y_half, * alpha_prime_y_half, * a_y_half, * b_y_half;
    float ** psi_sxx_x, ** psi_syy_y, ** psi_sxy_y, ** psi_sxy_x, ** psi_vxx, ** psi_vyy, ** psi_vxy, ** psi_vyx, ** psi_vxxs;
    float ** psi_sxz_x, ** psi_syz_y, ** psi_vzx, ** psi_vzy;
    
    /* Variables for viscoelastic modeling */
    float **ptaus=NULL, **ptaup=NULL, *etaip=NULL, *etajm=NULL, *peta=NULL, **ptausipjp=NULL, **fipjp=NULL, ***dip=NULL, *bip=NULL, *bjm=NULL;
    float *cip=NULL, *cjm=NULL, ***d=NULL, ***e=NULL, ***pr=NULL, ***pp=NULL, ***pq=NULL, **f=NULL, **g=NULL;
97
    float ***pt=NULL, ***po=NULL; // SH Simulation
98 99 100 101 102 103 104 105 106 107 108
    
    /* Variables for step length calculation */
    int step1, step2, step3=0, itests, iteste, stepmax, countstep;
    float scalefac;
    
    /* Variables for Pseudo-Hessian calculation */
    int RECINC, ntr1;
    float * jac_rho, * jac_u, * jac_lam_x, * jac_lam_y;
    float * temp_TS, * temp_TS1, * temp_TS2, * temp_TS3, * temp_TS4, * temp_TS5, * temp_conv, * temp_conv1, * temp_conv2;
    float temp_hess, temp_hess_lambda, temp_hess_mu, mulamratio;
    float ** hessian, ** hessian_u, ** hessian_rho, **hessian_shot, **hessian_u_shot, **hessian_rho_shot;
109
    int SOURCE_SHAPE_OLD;
110 111
    
    /* Variables for L-BFGS */
112
    int LBFGS=0,LBFGS_NPAR=3;
113 114
    int LBFGS_iter_start=1;
    float LBFGS_L2_temp;
115
    float **s_LBFGS,**y_LBFGS, *rho_LBFGS;
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
    int l=0;
    int w=0;
    int m=0;
    
    /* Check wolfe */
    int steplength_search=0;
    int FWI_run=1;
    int gradient_optimization=1;
    float alpha_SL_min=0, alpha_SL_max=0, alpha_SL=1.0;
    float alpha_SL_old;
    float ** waveconv_old,** waveconv_u_old,** waveconv_rho_old;
    float ** waveconv_up,** waveconv_u_up,** waveconv_rho_up;
    float L2_SL_old=0, L2_SL_new=0;
    float c1_SL=1e-4, c2_SL=0.9;
    int wolfe_status;
    int wolfe_sum_FWI=0;
    int wolfe_found_lower_L2=0;
    float alpha_SL_FS;
    float L2_SL_FS;
    int use_wolfe_failsafe=0;
    int wolfe_SLS_failed=0;
    
    /* Variables for energy weighted gradient */
    float ** Ws, **Wr, **We;
    float ** Ws_SH, **Wr_SH, **We_SH;
    float ** We_sum,** We_sum_SH;
    float We_sum_max1;
    float We_max_SH,We_max;
    
    int * recswitch=NULL;
    float ** fulldata=NULL, ** fulldata_vx=NULL, ** fulldata_vy=NULL, ** fulldata_vz=NULL, ** fulldata_p=NULL, ** fulldata_curl=NULL, ** fulldata_div=NULL;
    
    /* different modelling types */
    int mod_type=0;
    
    /*vector for abort criterion*/
    float * L2_hist=NULL;
    
    /* help variable for MIN_ITER */
    int min_iter_help=0;
    
    float ** workflow=NULL;
    int workflow_lines;
159
    char workflow_header[STRING_SIZE];
160 161
    int change_wavetype_iter=-10; /* Have to be inialized negative */
    int wavetype_start; /* We need this due to MPI Comm */
162 163
    int buf1=0, buf2=0;
    WORKFLOW_STAGE=1;
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
    
    /* variable for time domain filtering */
    float FC;
    float *FC_EXT=NULL;
    int nfrq=0;
    int FREQ_NR=1;
    /* declaration of variables for trace killing */
    int ** kill_tmp;
    FILE *ftracekill;
    
    FILE *fprec, *FP2, *FP3, *FP4, *FP5, *FPL2, *FP6, *FP7;
    
    /* General parameters */
    int nt_out;
    
    MPI_Request *req_send, *req_rec;
    MPI_Status  *send_statuses, *rec_statuses;
    
    /* Initialize MPI environment */
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&NP);
    MPI_Comm_rank(MPI_COMM_WORLD,&MYID);
    
    setvbuf(stdout, NULL, _IONBF, 0);
    
    if (MYID == 0){
        time1=MPI_Wtime();
        clock();
    }
    
    /* print program name, version etc to stdout*/
    if (MYID == 0) info(stdout);
    
    /* read parameters from parameter-file (stdin) */
    fileinp=argv[1];
    FP=fopen(fileinp,"r");
    if(FP==NULL) {
        if (MYID == 0){
            printf("\n==================================================================\n");
Florian Wittkamp's avatar
Florian Wittkamp committed
203
            printf(" Cannot open IFOS input file %s \n",fileinp);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            printf("\n==================================================================\n\n");
            err(" --- ");
        }
    }
    
    /* read json formatted input file */
    read_par_json(stdout,fileinp);
    
    exchange_par();
    
    wavetype_start=WAVETYPE;
    if (MYID == 0) note(stdout);
    
    
    /* open log-file (each PE is using different file) */
    /*	fp=stdout; */
    sprintf(ext,".%i",MYID);
    strcat(LOG_FILE,ext);
    
    /* If Verbose==0, no PE will write a log file */
    if(!VERBOSE) sprintf(LOG_FILE,"/dev/null");
    
Florian Wittkamp's avatar
Florian Wittkamp committed
226
    if ((MYID==0)) FP=stdout;
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
    else {
        FP=fopen(LOG_FILE,"w");
    }
    fprintf(FP," This is the log-file generated by PE %d \n\n",MYID);
    
    /* domain decomposition */
    initproc();
    
    NT=iround(TIME/DT);  	  /* number of timesteps */
    /*ns=iround(NT/NDT);*/           /* number of samples per trace */
    ns=NT;	/* in a FWI one has to keep all samples of the forward modeled data
             at the receiver positions to calculate the adjoint sources and to do
             the backpropagation; look at function saveseis_glob.c to see that every
             NDT sample for the forward modeled wavefield is written to su files*/
    lsnap=iround(TSNAP1/DT);      /* first snapshot at this timestep */
    lsamp=NDT;
    
    
    /* output of parameters to log-file or stdout */
    if (MYID==0) write_par(FP);
    
    
    /* NXG, NYG denote size of the entire (global) grid */
    NXG=NX;
    NYG=NY;
    
    /* In the following, NX and NY denote size of the local grid ! */
    NX = IENDX;
    NY = IENDY;
    
    
    if (SEISMO){
        recpos=receiver(FP, &ntr);
        recswitch = ivector(1,ntr);
        recpos_loc = splitrec(recpos,&ntr_loc, ntr, recswitch);
        ntr_glob=ntr;
        ntr=ntr_loc;
    }
    
    /* memory allocation for abort criterion*/
    L2_hist = vector(1,1000);
    
    if(INV_STF) fulldata = matrix(1,ntr_glob,1,NT);
    
    /* estimate memory requirement of the variables in megabytes*/
    
    switch (SEISMO){
        case 1 : /* particle velocities only */
            nseismograms=2;
            break;
        case 2 : /* pressure only */
            nseismograms=1;
            break;
        case 3 : /* curl and div only */
            nseismograms=2;
            break;
        case 4 : /* everything */
            nseismograms=5;
            break;
        case 5 : /* everything except curl and div */
            nseismograms=3;
            break;
    }
    
    /* use only every DTINV time sample for the inversion */
    /*DTINV=15;*/
    DTINV_help=ivector(1,NT);
    NTDTINV=ceil((float)NT/(float)DTINV);		/* round towards next higher integer value */
    
    /* save every IDXI and IDYI spatial point during the forward modelling */
    IDXI=1;
    IDYI=1;
    
    /*allocate memory for dynamic, static and buffer arrays */
    fac1=(NX+FDORDER)*(NY+FDORDER);
    fac2=sizeof(float)*pow(2.0,-20.0);
    
    nd = FDORDER/2 + 1;
    
    // decide how much space for exchange is needed
    switch (WAVETYPE) {
        case 1:
            fdo3 = 2*nd;
            break;
        case 2:
            fdo3 = 1*nd;
            break;
        case 3:
            fdo3 = 3*nd;
            break;
        default:
            fdo3 = 2*nd;
            break;
    }
    
    
    if (L){
        memdyn=(5.0+3.0*(float)L)*fac1*fac2;
        memmodel=(12.0+3.0*(float)L)*fac1*fac2;
        
    } else {
        memdyn=5.0*fac1*fac2;
        memmodel=6.0*fac1*fac2;
    }
    memseismograms=nseismograms*ntr*ns*fac2;
    
    memfwt=5.0*((NX/IDXI)+FDORDER)*((NY/IDYI)+FDORDER)*NTDTINV*fac2;
    memfwt1=20.0*NX*NY*fac2;
    memfwtdata=6.0*ntr*ns*fac2;
    
    membuffer=2.0*fdo3*(NY+NX)*fac2;
    buffsize=2.0*2.0*fdo3*(NX+NY)*sizeof(MPI_FLOAT);
    memtotal=memdyn+memmodel+memseismograms+memfwt+memfwt1+memfwtdata+membuffer+(buffsize*pow(2.0,-20.0));
    
    
    if (MYID==0 && WAVETYPE == 1){
        fprintf(FP,"\n **Message from main (printed by PE %d):\n",MYID);
        fprintf(FP," Size of local grids: NX=%d \t NY=%d\n",NX,NY);
        fprintf(FP," Each process is now trying to allocate memory for:\n");
        fprintf(FP," Dynamic variables: \t\t %6.2f MB\n", memdyn);
        fprintf(FP," Static variables: \t\t %6.2f MB\n", memmodel);
        fprintf(FP," Seismograms: \t\t\t %6.2f MB\n", memseismograms);
        fprintf(FP," Buffer arrays for grid exchange:%6.2f MB\n", membuffer);
        fprintf(FP," Network Buffer for MPI_Bsend: \t %6.2f MB\n", buffsize*pow(2.0,-20.0));
        fprintf(FP," ------------------------------------------------ \n");
        fprintf(FP," Total memory required: \t %6.2f MB.\n\n", memtotal);
    }
    
    
    /* allocate buffer for buffering messages */
    buff_addr=malloc(buffsize);
    if (!buff_addr) err("allocation failure for buffer for MPI_Bsend !");
    MPI_Buffer_attach(buff_addr,buffsize);
    
    /* allocation for request and status arrays */
    req_send=(MPI_Request *)malloc(REQUEST_COUNT*sizeof(MPI_Request));
    req_rec=(MPI_Request *)malloc(REQUEST_COUNT*sizeof(MPI_Request));
    send_statuses=(MPI_Status *)malloc(REQUEST_COUNT*sizeof(MPI_Status));
    rec_statuses=(MPI_Status *)malloc(REQUEST_COUNT*sizeof(MPI_Status));
    
    
    /* memory allocation for dynamic (wavefield) arrays */
    if(!ACOUSTIC){
        switch (WAVETYPE) {
            case 1: // P and SV Waves
                psxx =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psxy =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psyy =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                break;
                
            case 2: // SH Waves
                psxz =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psyz =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                break;
                
            case 3: // P, SH and SV Waves
                psxx =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psxy =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psyy =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psxz =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                psyz =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
                break;
        }
    }else{
        psp  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    }
    
    if(GRAD_METHOD==2) {
395
        /* Allocate memory for L-BFGS */
396
        
397
        if(WAVETYPE==2) LBFGS_NPAR=2;
398
        
399
        s_LBFGS=fmatrix(1,N_LBFGS,1,LBFGS_NPAR*NX*NY);
400
        
401
        y_LBFGS=fmatrix(1,N_LBFGS,1,LBFGS_NPAR*NX*NY);
402
        
403
        rho_LBFGS=vector(1,N_LBFGS);
404
        
405 406 407 408
        for(l=1;l<=N_LBFGS;l++){
            for(m=1;m<=LBFGS_NPAR*NX*NY;m++){
                s_LBFGS[l][m]=0.0;
                y_LBFGS[l][m]=0.0;
409
            }
410
            rho_LBFGS[l]=0.0;
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
        }
    }
    
    if(!ACOUSTIC){
        if(WAVETYPE==1||WAVETYPE==3){
            ux   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            uy   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            uxy  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            uyx  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            uttx   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            utty   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        }
        if(WAVETYPE==2||WAVETYPE==3){
            uxz   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            uyz   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        }
    }
    
    switch (WAVETYPE) {
        case 1: // P and SV Waves
            pvx  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvy  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvxp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvyp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvxm1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvym1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            break;
            
        case 2: // SH Waves
            pvz  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvzp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvzm1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            break;
            
        case 3: // P and SV Waves
            pvx  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvy  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvxp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvyp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvxm1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvym1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvz  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvzp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            pvzm1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            break;
    }
    
    Vp0  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    if(!ACOUSTIC)
        Vs0  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    Rho0  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    
    /* memory allocation for static (model) arrays */
    prho =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    prhonp1 =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    prip =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    prjp =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    pripnp1 =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    prjpnp1 =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    ppi  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    ppinp1  =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    if(!ACOUSTIC){
        pu   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        punp1   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        puipjp   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    }
    vpmat   =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
    
    
    if((EPRECOND==1)||(EPRECOND==3)){
        if(WAVETYPE==1 || WAVETYPE==3) {
            We_sum = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            Ws = matrix(-nd+1,NY+nd,-nd+1,NX+nd); /* total energy of the source wavefield */
            Wr = matrix(-nd+1,NY+nd,-nd+1,NX+nd); /* total energy of the receiver wavefield */
            We = matrix(-nd+1,NY+nd,-nd+1,NX+nd); /* total energy of source and receiver wavefield */
        }
        if(WAVETYPE==2 || WAVETYPE==3) {
            We_sum_SH = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            Ws_SH = matrix(-nd+1,NY+nd,-nd+1,NX+nd); /* total energy of the source wavefield */
            Wr_SH = matrix(-nd+1,NY+nd,-nd+1,NX+nd); /* total energy of the receiver wavefield */
            We_SH = matrix(-nd+1,NY+nd,-nd+1,NX+nd); /* total energy of source and receiver wavefield */
        }
    }
    
    if (L) {
        /* dynamic (wavefield) arrays for viscoelastic modeling */
        pr = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        pp = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        pq = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        /* memory allocation for static arrays for viscoelastic modeling */
        dip = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        d =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        e =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        ptaus =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        ptausipjp =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        if(WAVETYPE==2 || WAVETYPE==3) {
            pt = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
            po = f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,L);
        }
        ptaup =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        fipjp =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        f =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        g =  matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        peta =  vector(1,L);
        etaip =  vector(1,L);
        etajm =  vector(1,L);
        bip =  vector(1,L);
        bjm =  vector(1,L);
        cip =  vector(1,L);
        cjm =  vector(1,L);
    }
    
    /*nf=4;
     nfstart=4;*/
    
    NTST=20;
    NTSTI=NTST/DTINV;
    
    nxny=NX*NY;
    nxnyi=(NX/IDXI)*(NY/IDYI);
    
    /* Parameters for step length calculations */
    stepmax = STEPMAX; /* number of maximum misfit calculations/steplength 2/3*/
    scalefac = SCALEFAC; /* scale factor for the step length */
    
    if(INVMAT==0){
        waveconv = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        waveconv_lam = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        waveconv_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        
        waveconvtmp = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        wcpart = matrix(1,3,1,3);
        wavejac = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        
        if(!ACOUSTIC){
            forward_prop_x =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
            forward_prop_y =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
        }else{
            forward_prop_p =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
        }
        gradg = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        gradp = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        
        if(WAVETYPE==1 || WAVETYPE==3){
            forward_prop_rho_x =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
            forward_prop_rho_y =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
        }
        if(WAVETYPE==2 || WAVETYPE==3){
            forward_prop_rho_z =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
            forward_prop_z_xz =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
            forward_prop_z_yz =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
            waveconv_rho_shot_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_u_shot_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_mu_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_rho_s_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_u_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_rho_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            gradp_u_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            gradp_rho_z = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        }
        
        gradg_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        gradp_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        waveconv_rho = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        waveconv_rho_s = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        waveconv_rho_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        
        if(WOLFE_CONDITION){
579 580 581 582
            
            c1_SL=WOLFE_C1_SL;
            c2_SL=WOLFE_C2_SL;
            
583
            waveconv_old= matrix(-nd+1,NY+nd,-nd+1,NX+nd);
584
            if(!ACOUSTIC) waveconv_u_old= matrix(-nd+1,NY+nd,-nd+1,NX+nd);
585 586 587
            waveconv_rho_old= matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            
            waveconv_up= matrix(-nd+1,NY+nd,-nd+1,NX+nd);
588
            if(!ACOUSTIC) waveconv_u_up= matrix(-nd+1,NY+nd,-nd+1,NX+nd);
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 701 702 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
            waveconv_rho_up= matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        }
        
        if(!ACOUSTIC){
            forward_prop_u =  f3tensor(-nd+1,NY+nd,-nd+1,NX+nd,1,NT/DTINV);
            gradg_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            gradp_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_u = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_mu = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
            waveconv_u_shot = matrix(-nd+1,NY+nd,-nd+1,NX+nd);
        }
        
    }
    
    /* Allocate memory for boundary */
    if(FW>0){
        d_x = vector(1,2*FW);
        K_x = vector(1,2*FW);
        alpha_prime_x = vector(1,2*FW);
        a_x = vector(1,2*FW);
        b_x = vector(1,2*FW);
        
        d_x_half = vector(1,2*FW);
        K_x_half = vector(1,2*FW);
        alpha_prime_x_half = vector(1,2*FW);
        a_x_half = vector(1,2*FW);
        b_x_half = vector(1,2*FW);
        
        d_y = vector(1,2*FW);
        K_y = vector(1,2*FW);
        alpha_prime_y = vector(1,2*FW);
        a_y = vector(1,2*FW);
        b_y = vector(1,2*FW);
        
        d_y_half = vector(1,2*FW);
        K_y_half = vector(1,2*FW);
        alpha_prime_y_half = vector(1,2*FW);
        a_y_half = vector(1,2*FW);
        b_y_half = vector(1,2*FW);
        
        if (WAVETYPE==1||WAVETYPE==3){
            psi_sxx_x =  matrix(1,NY,1,2*FW);
            psi_syy_y =  matrix(1,2*FW,1,NX);
            psi_sxy_y =  matrix(1,2*FW,1,NX);
            psi_sxy_x =  matrix(1,NY,1,2*FW);
            psi_vxx   =  matrix(1,NY,1,2*FW);
            psi_vxxs  =  matrix(1,NY,1,2*FW);
            psi_vyy   =  matrix(1,2*FW,1,NX);
            psi_vxy   =  matrix(1,2*FW,1,NX);
            psi_vyx   =  matrix(1,NY,1,2*FW);
        }
        if(WAVETYPE==2||WAVETYPE == 3 ){
            psi_sxz_x =  matrix(1,NY,1,2*FW);
            psi_syz_y =  matrix(1,2*FW,1,NX);
            psi_vzx   =  matrix(1,NY,1,2*FW);
            psi_vzy   =  matrix(1,2*FW,1,NX);
        }
    }
    
    taper_coeff=  matrix(1,NY,1,NX);
    
    
    /* memory allocation for buffer arrays in which the wavefield
     information which is exchanged between neighbouring PEs is stored */
    bufferlef_to_rig = matrix(1,NY,1,fdo3);
    bufferrig_to_lef = matrix(1,NY,1,fdo3);
    buffertop_to_bot = matrix(1,NX,1,fdo3);
    bufferbot_to_top = matrix(1,NX,1,fdo3);
    
    /* Allocate memory to save full seismograms */
    switch (SEISMO){
        case 1 : /* particle velocities only */
            switch (WAVETYPE) {
                case 1:
                    fulldata_vx = matrix(1,ntr_glob,1,NT);
                    fulldata_vy = matrix(1,ntr_glob,1,NT);
                    break;
                    
                case 2:
                    fulldata_vz = matrix(1,ntr_glob,1,NT);
                    break;
                    
                case 3:
                    fulldata_vx = matrix(1,ntr_glob,1,NT);
                    fulldata_vy = matrix(1,ntr_glob,1,NT);
                    fulldata_vz = matrix(1,ntr_glob,1,NT);
                    break;
            }
            break;
        case 2 : /* pressure only */
            fulldata_p = matrix(1,ntr_glob,1,NT);
            break;
        case 3 : /* curl and div only */
            fulldata_div = matrix(1,ntr_glob,1,NT);
            fulldata_curl = matrix(1,ntr_glob,1,NT);
            break;
        case 4 : /* everything */
            switch (WAVETYPE) {
                case 1:
                    fulldata_vx = matrix(1,ntr_glob,1,NT);
                    fulldata_vy = matrix(1,ntr_glob,1,NT);
                    break;
                    
                case 2:
                    fulldata_vz = matrix(1,ntr_glob,1,NT);
                    break;
                    
                case 3:
                    fulldata_vx = matrix(1,ntr_glob,1,NT);
                    fulldata_vy = matrix(1,ntr_glob,1,NT);
                    fulldata_vz = matrix(1,ntr_glob,1,NT);
                    break;
            }
            fulldata_p = matrix(1,ntr_glob,1,NT);
            fulldata_div = matrix(1,ntr_glob,1,NT);
            fulldata_curl = matrix(1,ntr_glob,1,NT);
            break;
        case 5 : /* everything except curl and div*/
            switch (WAVETYPE) {
                case 1:
                    fulldata_vx = matrix(1,ntr_glob,1,NT);
                    fulldata_vy = matrix(1,ntr_glob,1,NT);
                    break;
                    
                case 2:
                    fulldata_vz = matrix(1,ntr_glob,1,NT);
                    break;
                    
                case 3:
                    fulldata_vx = matrix(1,ntr_glob,1,NT);
                    fulldata_vy = matrix(1,ntr_glob,1,NT);
                    fulldata_vz = matrix(1,ntr_glob,1,NT);
                    break;
            }
            fulldata_p = matrix(1,ntr_glob,1,NT);
            break;
            
    }
    if (ntr>0){
        switch (SEISMO){
            case 1 : /* particle velocities only */
                switch (WAVETYPE) {
                    case 1:
                        sectionvx=matrix(1,ntr,1,ns);
                        sectionvy=matrix(1,ntr,1,ns);
                        break;
                    case 2:
                        sectionvz=matrix(1,ntr,1,ns);
                        break;
                    case 3:
                        sectionvx=matrix(1,ntr,1,ns);
                        sectionvy=matrix(1,ntr,1,ns);
                        sectionvz=matrix(1,ntr,1,ns);
                        break;
                }
                break;
            case 2 : /* pressure only */
                sectionp=matrix(1,ntr,1,ns);
                sectionpnp1=matrix(1,ntr,1,ns);
                sectionpn=matrix(1,ntr,1,ns);
                break;
            case 3 : /* curl and div only */
                sectioncurl=matrix(1,ntr,1,ns);
                sectiondiv=matrix(1,ntr,1,ns);
                break;
            case 4 : /* everything */
                switch (WAVETYPE) {
                    case 1:
                        sectionvx=matrix(1,ntr,1,ns);
                        sectionvy=matrix(1,ntr,1,ns);
                        break;
                    case 2:
                        sectionvz=matrix(1,ntr,1,ns);
                        break;
                    case 3:
                        sectionvx=matrix(1,ntr,1,ns);
                        sectionvy=matrix(1,ntr,1,ns);
                        sectionvz=matrix(1,ntr,1,ns);
                        break;
                }
                sectioncurl=matrix(1,ntr,1,ns);
                sectiondiv=matrix(1,ntr,1,ns);
                sectionp=matrix(1,ntr,1,ns);
                break;
            case 5 : /* everything except curl and div*/
                switch (WAVETYPE) {
                    case 1:
                        sectionvx=matrix(1,ntr,1,ns);
                        sectionvy=matrix(1,ntr,1,ns);
                        break;
                    case 2:
                        sectionvz=matrix(1,ntr,1,ns);
                        break;
                    case 3:
                        sectionvx=matrix(1,ntr,1,ns);
                        sectionvy=matrix(1,ntr,1,ns);
                        sectionvz=matrix(1,ntr,1,ns);
                        break;
                }
                sectionp=matrix(1,ntr,1,ns);
                break;
        }
    }
    
    /* Memory for seismic data */
    sectionread=matrix(1,ntr_glob,1,ns);
    sectionpdata=matrix(1,ntr,1,ns);
    sectionpdiff=matrix(1,ntr,1,ns);
    sectionpdiffold=matrix(1,ntr,1,ns);
    switch (WAVETYPE) {
        case 1:
            sectionvxdata=matrix(1,ntr,1,ns);
            sectionvxdiff=matrix(1,ntr,1,ns);
            sectionvxdiffold=matrix(1,ntr,1,ns);
            sectionvydata=matrix(1,ntr,1,ns);
            sectionvydiff=matrix(1,ntr,1,ns);
            sectionvydiffold=matrix(1,ntr,1,ns);
            break;
            
        case 2:
            sectionvzdata=matrix(1,ntr,1,ns);
            sectionvzdiff=matrix(1,ntr,1,ns);
            sectionvzdiffold=matrix(1,ntr,1,ns);
            break;
            
        case 3:
            sectionvxdata=matrix(1,ntr,1,ns);
            sectionvxdiff=matrix(1,ntr,1,ns);
            sectionvxdiffold=matrix(1,ntr,1,ns);
            sectionvydata=matrix(1,ntr,1,ns);
            sectionvydiff=matrix(1,ntr,1,ns);
            sectionvydiffold=matrix(1,ntr,1,ns);
            sectionvzdata=matrix(1,ntr,1,ns);
            sectionvzdiff=matrix(1,ntr,1,ns);
            sectionvzdiffold=matrix(1,ntr,1,ns);
            break;
    }
    
    /* Memory for inversion for source time function */
    if((INV_STF==1)||(TIME_FILT==1) || (TIME_FILT==2)){
        sectionp_conv=matrix(1,ntr_glob,1,NT);
        sectionp_obs=matrix(1,ntr_glob,1,NT);
        source_time_function = vector(1,NT);
        switch (WAVETYPE) {
            case 1:
                sectionvy_conv=matrix(1,ntr_glob,1,NT);
                sectionvy_obs=matrix(1,ntr_glob,1,NT);
                sectionvx_conv=matrix(1,ntr_glob,1,NT);
                sectionvx_obs=matrix(1,ntr_glob,1,NT);
                break;
                
            case 2:
                sectionvz_conv=matrix(1,ntr_glob,1,NT);
                sectionvz_obs=matrix(1,ntr_glob,1,NT);
                break;
                
            case 3:
                sectionvy_conv=matrix(1,ntr_glob,1,NT);
                sectionvy_obs=matrix(1,ntr_glob,1,NT);
                sectionvx_conv=matrix(1,ntr_glob,1,NT);
                sectionvx_obs=matrix(1,ntr_glob,1,NT);
                sectionvz_conv=matrix(1,ntr_glob,1,NT);
                sectionvz_obs=matrix(1,ntr_glob,1,NT);
                break;
        }
    }
    
    /* memory for source position definition */
    srcpos1=fmatrix(1,8,1,1);
    
    /* memory of L2 norm */
    L2t = vector(1,4);
    epst1 = vector(1,3);
    epst2 = vector(1,3);
    epst3 = vector(1,3);
    picked_times = vector(1,ntr);
    
    fprintf(FP," ... memory allocation for PE %d was successfull.\n\n", MYID);
    
    
    /* Holberg coefficients for FD operators*/
    hc = holbergcoeff();
    
    MPI_Barrier(MPI_COMM_WORLD);
    
    /* Reading source positions from SOURCE_FILE */
    srcpos=sources(&nsrc);
    nsrc_glob=nsrc;
    
    if(INVMAT==0&&USE_WORKFLOW){
879
        read_workflow(FILE_WORKFLOW,&workflow, &workflow_lines,workflow_header);
880 881 882
    }
    
    /* create model grids */
Florian Wittkamp's avatar
Florian Wittkamp committed
883
    if(L){
884
        if(!ACOUSTIC){
Florian Wittkamp's avatar
Florian Wittkamp committed
885 886 887 888
            if (READMOD){
                readmod(prho,ppi,pu,ptaus,ptaup,peta);
            }else{
                model(prho,ppi,pu,ptaus,ptaup,peta);
889 890
            }
        }else{
Florian Wittkamp's avatar
Florian Wittkamp committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
            if (READMOD){
                readmod_viscac(prho,ppi,ptaup,peta);
            }else{
                model_viscac(prho,ppi,ptaup,peta);
            }
        }
    }else{
        if(!ACOUSTIC){
            if (READMOD){
                readmod_elastic(prho,ppi,pu);
            }else{
                model_elastic(prho,ppi,pu);
            }
        }else{
            if (READMOD){
                readmod_acoustic(prho,ppi);
            }else{
                model_acoustic(prho,ppi);
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
            }
        }
    }
    
    /* check if the FD run will be stable and free of numerical dispersion */
    checkfd(FP, prho, ppi, pu, ptaus, ptaup, peta, hc, srcpos, nsrc, recpos, ntr_glob);
    
    /* calculate damping coefficients for CPMLs*/
    if(FW>0)
        PML_pro(d_x, K_x, alpha_prime_x, a_x, b_x, d_x_half, K_x_half, alpha_prime_x_half, a_x_half, b_x_half, d_y, K_y, alpha_prime_y, a_y, b_y, d_y_half, K_y_half, alpha_prime_y_half, a_y_half, b_y_half);
    
    MPI_Barrier(MPI_COMM_WORLD);
    
    /* comunication initialisation for persistent communication */
    /*comm_ini(bufferlef_to_rig, bufferrig_to_lef, buffertop_to_bot, bufferbot_to_top, req_send, req_rec);*/
    
    snapseis=1;
    snapseis1=1;
    SHOTINC=1;
    RECINC=1;
    
    switch(TIME_FILT){
        case 1: FC=FC_START; break;
            /*read frequencies from file*/
        case 2: FC_EXT=filter_frequencies(&nfrq); FC=FC_EXT[FREQ_NR]; break;
    }
    
936
    SOURCE_SHAPE_OLD = SOURCE_SHAPE;
937 938 939 940 941 942 943 944 945 946 947 948
    
    nt_out=10000;
    if(!VERBOSE) nt_out=1e5;
    /*------------------------------------------------------------------------------*/
    /*----------- start fullwaveform iteration loop --------------------------------*/
    /*------------------------------------------------------------------------------*/
    
    for(iter=1;iter<=ITERMAX;iter++){  /* fullwaveform iteration loop */
        
        // At each iteration the workflow is applied
        if(USE_WORKFLOW&&(INVMAT==0)){
            
949
            apply_workflow(workflow,workflow_lines,workflow_header,&iter,&FC,wavetype_start,&change_wavetype_iter,&LBFGS_iter_start);
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
            
        }
        
        if(GRAD_METHOD==2&&(INVMAT==0)){
            
            /* detect a change in inversion process and restart L-BFGS */
            if(iter==INV_RHO_ITER||iter==INV_VP_ITER||iter==INV_VS_ITER){
                LBFGS_iter_start=iter;
                
                if(WOLFE_CONDITION) {
                    /* Restart Step Length search */
                    alpha_SL_old=1;
                }
                
                /* set values */
                FWI_run=1;
                gradient_optimization=1;
            }
            
            /* restart L-BFGS */
            if(iter==LBFGS_iter_start) {
971
                lbfgs_reset(iter,N_LBFGS,LBFGS_NPAR,s_LBFGS,y_LBFGS,rho_LBFGS);
972 973 974 975 976 977 978 979 980 981 982
                
                /* set values */
                FWI_run=1;
                gradient_optimization=1;
            }
            
        }
        
        if (MYID==0){
            time2=MPI_Wtime();
            fprintf(FP,"\n\n\n ------------------------------------------------------------------\n");
983 984 985 986 987
            if(INVMAT==0) {
                fprintf(FP,"\n\n\n                   TDFWI ITERATION %d \t of %d \n",iter,ITERMAX);
            } else {
                fprintf(FP,"\n\n\n                        FD-SIMULATION \n");
            }
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
            fprintf(FP,"\n\n\n ------------------------------------------------------------------\n");
        }
        
        countstep=0;
        
        if(GRAD_METHOD==1) {FWI_run=1; steplength_search=0; gradient_optimization=1;}
        
        /*-----------------------------------------------------*/
        /*  While loop for Wolfe step length search            */
        /*-----------------------------------------------------*/
        while(FWI_run || steplength_search || gradient_optimization) {
            
            /*-----------------------------------------------------*/
            /*              Calculate Misfit and gradient          */
            /*-----------------------------------------------------*/
            if(FWI_run){
                /* For the calculation of the material parameters between gridpoints
                 they have to be averaged. For this, values lying at 0 and NX+1,
                 for example, are required on the local grid. These are now copied from the
                 neighbouring grids */
Florian Wittkamp's avatar
Florian Wittkamp committed
1008 1009 1010 1011 1012 1013
                if (L){
                    if(!ACOUSTIC){
                        matcopy(prho,ppi,pu,ptaus,ptaup);
                    } else {
                        matcopy_viscac(prho,ppi,ptaup);
                    }
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
                }else{
                    if(!ACOUSTIC){
                        matcopy_elastic(prho, ppi, pu);
                    }else{
                        matcopy_acoustic(prho, ppi);
                    }
                }
                
                MPI_Barrier(MPI_COMM_WORLD);
                
                /* MPI split for processors with ntr>0 */
                int myid_ntr, group_id=0, groupsize;
                MPI_Comm	MPI_COMM_NTR;
                
                if (ntr) group_id = 1;
                else group_id = 0;
                MPI_Comm_split(MPI_COMM_WORLD, group_id, MYID, &MPI_COMM_NTR);
                MPI_Comm_rank(MPI_COMM_NTR, &myid_ntr);
                /* end of MPI split for processors with ntr>0 */
                
                
1035
                if(!ACOUSTIC) av_mue(pu,puipjp,prho);
1036
                av_rho(prho,prip,prjp);
1037
                if (!ACOUSTIC && L) av_tau(ptaus,ptausipjp);
1038 1039 1040
                
                
                /* Preparing memory variables for update_s (viscoelastic) */
Florian Wittkamp's avatar
Florian Wittkamp committed
1041 1042 1043 1044 1045 1046 1047
                if (L) {
                    if(!ACOUSTIC){
                        prepare_update_s(etajm,etaip,peta,fipjp,pu,puipjp,ppi,prho,ptaus,ptaup,ptausipjp,f,g,bip,bjm,cip,cjm,dip,d,e);
                    } else {
                        prepare_update_p(etajm,peta,ppi,prho,ptaup,g,bjm,cjm,e);
                    }
                }
1048
                
1049
                /* Do some initia calculations */
1050 1051 1052
                if(iter==1){
                    
                    /* Calculationg material parameters according to INVMAT1 */
1053 1054
                    for (j=1;j<=NY;j=j+IDY){
                        for (i=1;i<=NX;i=i+IDX){
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
                            
                            if(INVMAT1==1){
                                
                                Vp0[j][i] = ppi[j][i];
                                if(!ACOUSTIC) Vs0[j][i] = pu[j][i];
                                Rho0[j][i] = prho[j][i];}
                            
                            
                            
                            if(INVMAT1==2){
                                
                                Vp0[j][i] = sqrt((ppi[j][i]+2.0*pu[j][i])*prho[j][i]);
                                Vs0[j][i] = sqrt((pu[j][i])*prho[j][i]);
                                Rho0[j][i] = prho[j][i];
                                
                            }
                            
                            if(INVMAT1==3){
                                
                                Vp0[j][i] = ppi[j][i];
                                Vs0[j][i] = pu[j][i];
                                Rho0[j][i] = prho[j][i];
                                
                            }
                            
                        }
                    }
                    
1083 1084 1085 1086 1087
                    /* Get average values from material parameters */
                    Vp_avg=average_matrix(ppi);
                    rho_avg=average_matrix(prho);
                    if(!ACOUSTIC) Vs_avg=average_matrix(pu);
        
1088 1089
                    if(!ACOUSTIC) if(VERBOSE) printf("MYID = %d \t Vp_avg = %e \t Vs_avg = %e \t rho_avg = %e \n ",MYID,Vp_avg,Vs_avg,rho_avg);
                    else if(VERBOSE) printf("MYID = %d \t Vp_avg = %e \t rho_avg = %e \n ",MYID,Vp_avg,rho_avg);
1090

1091 1092 1093 1094 1095 1096 1097 1098
                    C_vp = Vp_avg*Vp_avg;
                    if(!ACOUSTIC) C_vs = Vs_avg*Vs_avg;
                    C_rho = rho_avg*rho_avg;
                }
                
                /* Open Log File for L2 norm */
                if(INVMAT!=10){
                    if(MYID==0){
Florian Wittkamp's avatar
Florian Wittkamp committed
1099 1100
                        if(iter==1){
                            FPL2=fopen(MISFIT_LOG_FILE,"w");
1101
                            /* Write header for misfit log file */
1102
                            if(GRAD_METHOD==1&&VERBOSE) {
1103 1104 1105 1106 1107 1108
                                if (TIME_FILT==0){
                                    fprintf(FPL2,"opteps_vp \t epst1[1] \t epst1[2] \t epst1[3] \t L2t[1] \t L2t[2] \t L2t[3] \t L2t[4] \n");}
                                else{
                                    fprintf(FPL2,"opteps_vp \t epst1[1] \t epst1[2] \t epst1[3] \t L2t[1] \t L2t[2] \t L2t[3] \t L2t[4] \t FC \n");
                                }
                            }
Florian Wittkamp's avatar
Florian Wittkamp committed
1109
                        }
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
                        if(iter>1){FPL2=fopen(MISFIT_LOG_FILE,"a");}
                    }
                }
                
                /* initialization of L2 calculation */
                L2=0.0;
                Lcount=0;
                energy=0.0;
                L2_all_shots=0.0;
                energy_all_shots=0.0;
                killed_traces=0;
                killed_traces_testshots=0;
                
1123 1124 1125 1126 1127 1128
                if(WAVETYPE==2||WAVETYPE==3){
                    L2_SH=0.0;
                    energy_SH=0.0;
                    L2_all_shots_SH=0.0;
                    energy_all_shots_SH=0.0;
                }
1129 1130 1131 1132 1133 1134 1135
                
                EPSILON=0.0;  /* test step length */
                exchange_par();
                
                /* initialize waveconv matrix*/
                if(WAVETYPE==1||WAVETYPE==3){
                    if(INVMAT==0){
1136
                        for (j=1;j<=NY;j=j+IDY){
1137
                            for (i=1;i<=NX;i=i+IDX){
1138
                                waveconv[j][i]=0.0;
1139
                                waveconv_rho[j][i]=0.0;
1140
                                if(!ACOUSTIC) waveconv_u[j][i]=0.0;
1141 1142 1143 1144 1145 1146 1147
                            }
                        }
                    }
                }
                /* initialize waveconv matrix*/
                if(WAVETYPE==2||WAVETYPE==3){
                    if(INVMAT==0){
1148 1149
                        for (j=1;j<=NY;j=j+IDY){
                            for (i=1;i<=NX;i=i+IDX){
1150 1151 1152 1153 1154 1155 1156 1157 1158
                                waveconv_rho_z[j][i]=0.0;
                                waveconv_u_z[j][i]=0.0;
                                
                            }
                        }
                    }
                }
                
                if((EPRECOND>0)&&(EPRECOND_ITER==iter||(EPRECOND_ITER==0))){
1159 1160
                    for (j=1;j<=NY;j=j+IDY){
                        for (i=1;i<=NX;i=i+IDX){
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
                            if(WAVETYPE==1||WAVETYPE==3) We_sum[j][i]=0.0;
                            if(WAVETYPE==2||WAVETYPE==3) We_sum_SH[j][i]=0.0;
                        }
                    }
                }
                
                
                
                itestshot=TESTSHOT_START;
                swstestshot=0;
                
                if(INVTYPE==2){
                    if (RUN_MULTIPLE_SHOTS) nshots=nsrc; else nshots=1;
                    
                    /*------------------------------------------------------------------------------*/
                    /*----------- Start of loop over shots -----------------------------------------*/
                    /*------------------------------------------------------------------------------*/
                    
                    for (ishot=1;ishot<=nshots;ishot+=SHOTINC){
                        /*for (ishot=1;ishot<=1;ishot+=1){*/
1181
                        SOURCE_SHAPE = SOURCE_SHAPE_OLD;
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
                        
                        if (INV_STF==1 && WAVETYPE !=1) {
                            fprintf(FP,"\n==================================================================================\n");
                            fprintf(FP,"\n====== Source time function inversion and WAVETYPE !=1 not supported    ==========\n");
                            fprintf(FP,"\n==================================================================================\n");
                            err(" NO STFI and WAVETYPE !=1 ");
                        }
                        
                        /*------------------------------------------------------------------------------*/
                        /*----------- Start of inversion of source time function -----------------------*/
                        /*------------------------------------------------------------------------------*/
                        
                        if((INV_STF==1)&&((iter==1)||(s==1))){
                            fprintf(FP,"\n==================================================================================\n");
                            fprintf(FP,"\n MYID=%d *****  Forward simulation for inversion of source time function ******** \n",MYID);
                            fprintf(FP,"\n MYID=%d * Starting simulation (forward model) for shot %d of %d. Iteration %d ** \n",MYID,ishot,nshots,iter);
                            fprintf(FP,"\n==================================================================================\n\n");
                            
                            for (nt=1;nt<=8;nt++) srcpos1[nt][1]=srcpos[nt][ishot];
                            
                            if (RUN_MULTIPLE_SHOTS){
                                /* find this single source positions on subdomains */
                                if (nsrc_loc>0) free_matrix(srcpos_loc,1,8,1,1);
                                srcpos_loc=splitsrc(srcpos1,&nsrc_loc, 1);
                            }else{
                                /* Distribute multiple source positions on subdomains */
                                srcpos_loc = splitsrc(srcpos,&nsrc_loc, nsrc);
                            }
                            
1211
                            if((SOURCE_SHAPE==7)||(SOURCE_SHAPE==3))err("SOURCE_SHAPE==7 or SOURCE_SHAPE==3 isn't possible with INV_STF==1");
1212 1213 1214 1215
                            MPI_Barrier(MPI_COMM_WORLD);
                            /* calculate wavelet for each source point */
                            signals=NULL;
                            signals=wavelet(srcpos_loc,nsrc_loc,ishot,0);
Florian Wittkamp's avatar
Florian Wittkamp committed
1216
                            
1217 1218 1219
                            
                            /* initialize wavefield with zero */
                            if (L){
Florian Wittkamp's avatar
Florian Wittkamp committed
1220 1221 1222 1223 1224
                                if(!ACOUSTIC) {
                                    zero_fdveps_visc(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,pvz,psxx,psyy,psxy,psxz,psyz,ux,uy,uxy,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_sxz_x,psi_vxx,psi_vyx,psi_vzx,psi_syy_y,psi_sxy_y,psi_syz_y,psi_vyy,psi_vxy,psi_vzy,psi_vxxs,pr,pp,pq,pt,po);
                                } else {
                                    zero_fdveps_viscac(-nd+1, NY+nd, -nd+1, NX+nd, pvx, pvy, psp, pvxp1, pvyp1, psi_sxx_x, psi_sxy_x, psi_vxx, psi_vyx, psi_syy_y, psi_sxy_y, psi_vyy, psi_vxy, psi_vxxs, pp);
                                }
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
                            }else{
                                if(!ACOUSTIC)
                                    zero_fdveps(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,pvz,psxx,psyy,psxy,psxz,psyz,ux,uy,uxy,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_sxz_x,psi_vxx,psi_vyx,psi_vzx,psi_syy_y,psi_sxy_y,psi_syz_y,psi_vyy,psi_vxy,psi_vzy,psi_vxxs);
                                else
                                    zero_fdveps_ac(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,psp,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_vxx,psi_vyx,psi_syy_y,psi_sxy_y,psi_vyy,psi_vxy,psi_vxxs);
                            }
                            
                            
                            /*----------------------  loop over timesteps (forward model) ------------------*/
                            
                            lsnap=iround(TSNAP1/DT);
                            lsamp=NDT;
                            nsnap=0;
                            
                            hin=1;
                            hin1=1;
                            
                            imat=1;
                            imat1=1;
                            imat2=1;
                            hi=1;
                            
                            for (nt=1;nt<=NT;nt++){
                                
                                infoout = !(nt%nt_out);
                                /* Check if simulation is still stable */
                                if (isnan(pvy[NY/2][NX/2])) {
                                    fprintf(FP,"\n Time step: %d; pvy: %f \n",nt,pvy[NY/2][NX/2]);
                                    err(" Simulation is unstable !");}
                                
                                
                                if (MYID==0){
                                    if (infoout)  fprintf(FP,"\n Computing timestep %d of %d \n",nt,NT);
                                    time3=MPI_Wtime();
                                }
                                
                                if(!ACOUSTIC)
                                    update_v_PML(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, uttx, utty, psxx, psyy, psxy, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,0, K_x, a_x,b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_sxx_x, psi_syy_y, psi_sxy_y, psi_sxy_x);
                                else
                                    update_v_acoustic_PML(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, psp, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,0, K_x_half, a_x_half, b_x_half, K_y_half, a_y_half, b_y_half, psi_sxx_x, psi_syy_y);
                                
                                if (MYID==0){
                                    time4=MPI_Wtime();
                                    time_av_v_update+=(time4-time3);
                                    if (infoout)  fprintf(FP," particle velocity exchange between PEs ...");
                                }
                                
                                /* exchange of particle velocities between PEs */
                                exchange_v(pvx,pvy,pvz, bufferlef_to_rig, bufferrig_to_lef, buffertop_to_bot, bufferbot_to_top, req_send, req_rec,wavetype_start);
                                
                                if (MYID==0){
                                    time5=MPI_Wtime();
                                    time_av_v_exchange+=(time5-time4);
                                    if (infoout)  fprintf(FP," finished (real time: %4.2f s).\n",time5-time4);
                                }
                                
                                if (L) {   /* viscoelastic */
Florian Wittkamp's avatar
Florian Wittkamp committed
1282 1283 1284 1285 1286
                                    if(!ACOUSTIC) {
                                        update_s_visc_PML(1, NX, 1, NY, pvx, pvy, ux, uy, uxy, uyx, psxx, psyy, psxy, ppi, pu, puipjp, prho, hc, infoout,pr, pp, pq, fipjp, f, g, bip, bjm, cip, cjm, d, e, dip,K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                    }else{
                                        update_p_visc_PML(1, NX, 1, NY, pvx, pvy, psp, ppi, prho, hc, infoout, pp, g, bjm, cjm, e, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                    }
1287 1288 1289 1290 1291 1292 1293 1294
                                }else{
                                    if(!ACOUSTIC)
                                        update_s_elastic_PML(1, NX, 1, NY, pvx, pvy, ux, uy, uxy, uyx, psxx, psyy, psxy, ppi, pu, puipjp, absorb_coeff, prho, hc, infoout,K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                    else
                                        update_p_PML(1, NX, 1, NY, pvx, pvy, psp, ppi, absorb_coeff, prho, hc, infoout, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                }
                                
                                /* explosive source */
1295
                                if ((SOURCE_TYPE==1))
1296 1297
                                    psource(nt,psxx,psyy,psp,srcpos_loc,signals,nsrc_loc,0);
                                
1298
                                /* Applying free surface condition */
Florian Wittkamp's avatar
Florian Wittkamp committed
1299 1300
                                if ((FREE_SURF) && (POS[2]==0)){
                                    if (!ACOUSTIC){
1301 1302
                                        if (L){
                                            /* viscoelastic */
1303
                                            surface_PML(1, pvx, pvy, psxx, psyy, psxy,psyz, pp, pq, ppi, pu, prho, ptaup, ptaus, etajm, peta, hc, K_x, a_x, b_x, psi_vxxs, ux, uy,uxy,uyz,psxz,uxz);
1304 1305
                                        }else{
                                            /* elastic */
1306
                                            surface_elastic_PML(1, pvx, pvy, psxx, psyy, psxy,psyz, ppi, pu, prho, hc, K_x, a_x, b_x, psi_vxxs, ux, uy, uxy,uyz,psxz,uxz);
Florian Wittkamp's avatar
Florian Wittkamp committed
1307
                                        }
1308 1309
                                    } else {
                                        /* viscoelastic and elastic ACOUSTIC */
Florian Wittkamp's avatar
Florian Wittkamp committed
1310
                                        surface_acoustic_PML(1, psp);
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
                                    }
                                }
                                
                                
                                if (MYID==0){
                                    time6=MPI_Wtime();
                                    time_av_s_update+=(time6-time5);
                                    if (infoout)  fprintf(FP," stress exchange between PEs ...");
                                }
                                
                                
                                /* stress exchange between PEs */
                                if(!ACOUSTIC)
                                    exchange_s(psxx,psyy,psxy,psxz,psyz,bufferlef_to_rig, bufferrig_to_lef,buffertop_to_bot, bufferbot_to_top,req_send, req_rec,wavetype_start);
                                else
                                    exchange_p(psp,bufferlef_to_rig, bufferrig_to_lef,buffertop_to_bot, bufferbot_to_top,req_send, req_rec);
                                
                                if (MYID==0){
                                    time7=MPI_Wtime();
                                    time_av_s_exchange+=(time7-time6);
                                    if (infoout)  fprintf(FP," finished (real time: %4.2f s).\n",time7-time6);
                                }
                                
                                /* store amplitudes at receivers in section-arrays */
                                if (SEISMO){
                                    seismo_ssg(nt, ntr, recpos_loc, sectionvx, sectionvy,sectionvz,sectionp, sectioncurl, sectiondiv,pvx, pvy,pvz, psxx, psyy, psp, ppi, pu, hc);
                                    /*lsamp+=NDT;*/
                                }
                                
                                
                                if(nt==hin1){
                                    
                                    if(INVMAT==0){
1344 1345
                                        for (j=1;j<=NY;j=j+IDYI){
                                            for (i=1;i<=NX;i=i+IDXI){
1346 1347 1348 1349 1350 1351 1352
                                                forward_prop_rho_x[j][i][hin]=pvxp1[j][i];
                                                forward_prop_rho_y[j][i][hin]=pvyp1[j][i];
                                            }
                                        }
                                        
                                        /* save snapshots from forward model */
                                        if(!ACOUSTIC){
1353 1354
                                            for (j=1;j<=NY;j=j+IDYI){
                                                for (i=1;i<=NX;i=i+IDXI){
1355 1356 1357 1358 1359 1360
                                                    if(VELOCITY==0){
                                                        forward_prop_x[j][i][hin]=psxx[j][i];
                                                        forward_prop_y[j][i][hin]=psyy[j][i];
                                                    } else {
                                                        forward_prop_x[j][i][hin]=ux[j][i];
                                                        forward_prop_y[j][i][hin]=uy[j][i];}
Florian Wittkamp's avatar
Florian Wittkamp committed
1361 1362 1363
                                                }
                                            }
                                        }else{
1364 1365
                                            for (j=1;j<=NY;j=j+IDYI){
                                                for (i=1;i<=NX;i=i+IDXI){
Florian Wittkamp's avatar
Florian Wittkamp committed
1366
                                                    forward_prop_p[j][i][hin]=psp[j][i];
1367 1368
                                                }
                                            }
Florian Wittkamp's avatar
Florian Wittkamp committed
1369 1370
                                        }
                                        if(!ACOUSTIC){
1371 1372
                                            for (j=1;j<=NY;j=j+IDYI){
                                                for (i=1;i<=NX;i=i+IDXI){
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 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
                                                    if(VELOCITY==0){
                                                        forward_prop_u[j][i][hin]=psxy[j][i];}
                                                    else{
                                                        forward_prop_u[j][i][hin]=uxy[j][i];}
                                                }
                                            }
                                        }
                                        
                                        hin++;
                                        hin1=hin1+DTINV;
                                        
                                    }
                                    DTINV_help[nt]=1;
                                    
                                }
                                
                                /* WRITE SNAPSHOTS TO DISK */
                                if ((SNAP) && (nt==lsnap) && (nt<=TSNAP2/DT)){
                                    snap(FP,nt,++nsnap,pvx,pvy,psxx,psyy,psp,pu,ppi,hc,ishot);
                                    lsnap=lsnap+iround(TSNAPINC/DT);
                                }
                                
                                
                                if (MYID==0){
                                    time8=MPI_Wtime();
                                    time_av_timestep+=(time8-time3);
                                    if (infoout)  fprintf(FP," total real time for timestep %d : %4.2f s.\n",nt,time8-time3);
                                }
                                
                                
                            }/*--------------------  End  of loop over timesteps (forward model) ----------*/
                            
                            switch (SEISMO){
                                    
                                case 1 : 	/* particle velocities only */
                                    catseis(sectionvx, fulldata_vx, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    catseis(sectionvy, fulldata_vy, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    if(WAVETYPE==2 || WAVETYPE==3) {
                                        catseis(sectionvz, fulldata_vz, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    }
                                    if (MYID==0){
1414
                                        saveseis_glob(FP,fulldata_vx,fulldata_vy,fulldata_vz,fulldata_p,fulldata_curl,fulldata_div,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,iter,1);}
1415 1416 1417 1418
                                    break;
                                    
                                case 2 :	/* pressure only */
                                    catseis(sectionp, fulldata_p, recswitch, ntr_glob, MPI_COMM_WORLD);
1419
                                    if (MYID==0) saveseis_glob(FP,fulldata_vx,fulldata_vy,fulldata_vz,fulldata_p,fulldata_curl,fulldata_div,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,iter,1);
1420 1421 1422 1423 1424
                                    break;
                                    
                                case 3 : 	/* curl and div only */
                                    catseis(sectiondiv, fulldata_div, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    catseis(sectioncurl, fulldata_curl, recswitch, ntr_glob, MPI_COMM_WORLD);
1425
                                    if (MYID==0) saveseis_glob(FP,fulldata_vx,fulldata_vy,fulldata_vz,fulldata_p,fulldata_curl,fulldata_div,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,iter,1);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
                                    break;
                                    
                                case 4 :	/* everything */
                                    catseis(sectionvx, fulldata_vx, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    catseis(sectionvy, fulldata_vy, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    catseis(sectionp, fulldata_p, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    if(WAVETYPE==2 || WAVETYPE==3) {
                                        catseis(sectionvz, fulldata_vz, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    }
                                    catseis(sectiondiv, fulldata_div, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    catseis(sectioncurl, fulldata_curl, recswitch, ntr_glob, MPI_COMM_WORLD);
1437
                                    if (MYID==0) saveseis_glob(FP,fulldata_vx,fulldata_vy,fulldata_vz,fulldata_p,fulldata_curl,fulldata_div,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,iter,1);
1438 1439 1440 1441 1442 1443 1444 1445 1446
                                    break;
                                    
                                case 5 :	/* everything except curl and div*/
                                    catseis(sectionvx, fulldata_vx, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    catseis(sectionvy, fulldata_vy, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    if(WAVETYPE==2 || WAVETYPE==3) {
                                        catseis(sectionvz, fulldata_vz, recswitch, ntr_glob, MPI_COMM_WORLD);
                                    }
                                    catseis(sectionp, fulldata_p, recswitch, ntr_glob, MPI_COMM_WORLD);
1447
                                    if (MYID==0) saveseis_glob(FP,fulldata_vx,fulldata_vy,fulldata_vz,fulldata_p,fulldata_curl,fulldata_div,recpos,recpos_loc,ntr_glob,srcpos,ishot,ns,iter,1);
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
                                    break;
                                    
                            } /* end of switch (SEISMO) */
                            
                            /* end of Forward simulation for inversion of source time function */
                            
                            /*----------- Start of inversion of source time function -------------*/
                            if((TIME_FILT==1) ||(TIME_FILT==2)){
                                
                                if (INVMAT==0){
                                    if((INV_STF==1)&&((iter==1)||(s==1))){
                                        
                                        if (nsrc_loc>0){
                                            
                                            /*time domain filtering of the observed data sectionvy_obs */
1463
                                            if ((ADJOINT_TYPE==1)|| (ADJOINT_TYPE==2)){
1464 1465 1466
                                                inseis(fprec,ishot,sectionvy_obs,ntr_glob,ns,2,iter);
                                                timedomain_filt(sectionvy_obs,FC,ORDER,ntr_glob,ns,1);
                                            }
1467
                                            if (ADJOINT_TYPE==4){
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
                                                inseis(fprec,ishot,sectionp_obs,ntr_glob,ns,9,iter);
                                                timedomain_filt(sectionp_obs,FC,ORDER,ntr_glob,ns,1);
                                            }
                                            
                                            printf("\n ====================================================================================================== \n");
                                            printf("\n Time Domain Filter is used for the inversion: lowpass filter, corner frequency of %.2f Hz, order %d\n",FC,ORDER);
                                            printf("\n ====================================================================================================== \n");
                                            
                                            if(iter==1){
                                                printf("\n ====================================================== \n");
                                                printf("\n MYID = %d: STF inversion at first iteration \n",MYID);
                                            }
                                            else{
                                                printf("\n ================================================================================================ \n");
                                                printf("\n MYID = %d: STF inversion because of frequency step at the end of the last iteration \n",MYID);
                                            }
                                            
1485
                                            if ((ADJOINT_TYPE==1)|| (ADJOINT_TYPE==2)){
1486 1487
                                                stf(FP,fulldata_vy,sectionvy_obs,sectionvy_conv,source_time_function,recpos,recpos_loc,ntr_glob,ntr,srcpos,ishot,ns,iter,nsrc_glob,FC);
                                            }
1488
                                            if (ADJOINT_TYPE==4){
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
                                                stf(FP,fulldata_p,sectionp_obs,sectionp_conv,source_time_function,recpos,recpos_loc,ntr_glob,ntr,srcpos,ishot,ns,iter,nsrc_glob,FC);
                                            }
                                        }
                                    }
                                }
                            }
                            
                            else{
                                if (INVMAT==0){
                                    if((INV_STF==1)&&(iter==N_STF_START)){
                                        
                                        if(ishot==nshots){
                                            N_STF_START=N_STF_START+N_STF;
                                        }
                                        
                                        if (nsrc_loc>0){
                                            printf("\n ====================================================== \n");
                                            printf("\n MYID = %d: STF inversion due to the increment N_STF \n",MYID);
                                            
1508
                                            if ((ADJOINT_TYPE==1)|| (ADJOINT_TYPE==2)){
1509 1510
                                                inseis(fprec,ishot,sectionvy_obs,ntr_glob,ns,2,iter);
                                            }
1511
                                            if (ADJOINT_TYPE==4){
1512 1513 1514
                                                inseis(fprec,ishot,sectionp_obs,ntr_glob,ns,9,iter);
                                            }
                                            
1515
                                            if ((ADJOINT_TYPE==1)|| (ADJOINT_TYPE==2)){
1516 1517
                                                stf(FP,fulldata_vy,sectionvy_obs,sectionvy_conv,source_time_function,recpos,recpos_loc,ntr_glob,ntr,srcpos,ishot,ns,iter,nsrc_glob,FC);
                                            }
1518
                                            if (ADJOINT_TYPE==4){
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
                                                stf(FP,fulldata_p,sectionp_obs,sectionp_conv,source_time_function,recpos,recpos_loc,ntr_glob,ntr,srcpos,ishot,ns,iter,nsrc_glob,FC);
                                            }
                                        }
                                    }
                                }
                            }
                            
                            
                            MPI_Barrier(MPI_COMM_WORLD);
                            
                            
                        }
                        
                        /*------------------------------------------------------------------------------*/
                        /*----------- End of inversion of source time function -------------------------*/
                        /*------------------------------------------------------------------------------*/
                        
                        fprintf(FP,"\n==================================================================================\n");
                        fprintf(FP,"\n MYID=%d * Starting simulation (forward model) for shot %d of %d. Iteration %d ** \n",MYID,ishot,nshots,iter);
1538
                        fprintf(FP,"\n==================================================================================\n");
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
                        
                        for (nt=1;nt<=8;nt++) srcpos1[nt][1]=srcpos[nt][ishot];
                        
                        /*-----------------------------------*/
                        /* determine source position on grid */
                        /*-----------------------------------*/
                        if (RUN_MULTIPLE_SHOTS){
                            /* find this single source positions on subdomains */
                            if (nsrc_loc>0) free_matrix(srcpos_loc,1,8,1,1);
                            srcpos_loc=splitsrc(srcpos1,&nsrc_loc, 1);
                        }else{
                            /* Distribute multiple source positions on subdomains */
                            srcpos_loc = splitsrc(srcpos,&nsrc_loc, nsrc);
                        }
                        
Florian Wittkamp's avatar
Florian Wittkamp committed
1554
                        if(INV_STF){
1555 1556
                            SOURCE_SHAPE=7;
                            fprintf(FP,"\n MYID=%d *****  Due to inversion of source time function SOURCE_SHAPE is switched to 7  ********** \n",MYID);
Florian Wittkamp's avatar
Florian Wittkamp committed
1557 1558 1559
                            fprintf(FP,"\n MYID=%d *****  Using optimized source time function located in %s.shot%d  ********** \n\n\n",MYID,SIGNAL_FILE,ishot);
                        }
                        
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
                        MPI_Barrier(MPI_COMM_WORLD);
                        
                        /*-------------------*/
                        /* calculate wavelet */
                        /*-------------------*/
                        /* calculate wavelet for each source point P SV */
                        if(WAVETYPE==1||WAVETYPE==3){
                            signals=NULL;
                            signals=wavelet(srcpos_loc,nsrc_loc,ishot,0);
                        }
                        /* calculate wavelet for each source point SH */
                        if(WAVETYPE==2||WAVETYPE==3){
                            signals_SH=NULL;
                            signals_SH=wavelet(srcpos_loc,nsrc_loc,ishot,1);
                        }
                        
                        /*------------------------------------------------------------------------------*/
                        /*----------- Start of Time Domain Filtering -----------------------------------*/
                        /*------------------------------------------------------------------------------*/
                        
1580
                        if (((TIME_FILT==1) || (TIME_FILT==2)) && (SOURCE_SHAPE!=6) && (INV_STF==0)){
1581
                            fprintf(FP,"\n Time Domain Filter applied: Lowpass with corner frequency of %.2f Hz, order %d\n",FC,ORDER);
1582 1583 1584 1585 1586 1587
                            
                            /*time domain filtering of the source signal */
                            if(WAVETYPE==1||WAVETYPE==3) timedomain_filt(signals,FC,ORDER,nsrc_loc,ns,1);
                            if(WAVETYPE==2||WAVETYPE==3) timedomain_filt(signals_SH,FC,ORDER,nsrc_loc,ns,1);
                            
                            if(WAVETYPE==1||WAVETYPE==3){
1588
                                if ((ADJOINT_TYPE==1)|| (ADJOINT_TYPE==2)){
1589 1590 1591 1592 1593
                                    /*time domain filtering of the observed data sectionvy_obs */
                                    inseis(fprec,ishot,sectionvy_obs,ntr_glob,ns,2,iter);
                                    timedomain_filt(sectionvy_obs,FC,ORDER,ntr_glob,ns,1);
                                }
                                
1594
                                if ((ADJOINT_TYPE==1)|| (ADJOINT_TYPE==3)){
1595 1596 1597 1598 1599
                                    /*time domain filtering of the observed data sectionvx_obs */
                                    inseis(fprec,ishot,sectionvx_obs,ntr_glob,ns,1,iter);
                                    timedomain_filt(sectionvx_obs,FC,ORDER,ntr_glob,ns,1);
                                }
                                
1600
                                if (ADJOINT_TYPE==4){
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
                                    /*time domain filtering of the observed data sectionp_obs */
                                    inseis(fprec,ishot,sectionp_obs,ntr_glob,ns,9,iter);
                                    timedomain_filt(sectionp_obs,FC,ORDER,ntr_glob,ns,1);
                                }
                            }
                            
                            if(WAVETYPE==2||WAVETYPE==3){
                                /*time domain filtering of the observed data sectionvx_obs */
                                inseis(fprec,ishot,sectionvz_obs,ntr_glob,ns,10,iter);
                                timedomain_filt(sectionvz_obs,FC,ORDER,ntr_glob,ns,1);
                            }
                            
                        }
                        /*------------------------------------------------------------------------------*/
                        /*----------- End of Time Domain Filtering -------------------------------------*/
                        /*------------------------------------------------------------------------------*/
                        
                        MPI_Barrier(MPI_COMM_WORLD);
                        
Florian Wittkamp's avatar
Florian Wittkamp committed
1620 1621
                        
                        
1622
                        /* initialize wavefield with zero */
Florian Wittkamp's avatar
Florian Wittkamp committed
1623 1624 1625 1626 1627
                        if (L){
                            if(!ACOUSTIC) {
                                zero_fdveps_visc(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,pvz,psxx,psyy,psxy,psxz,psyz,ux,uy,uxy,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_sxz_x,psi_vxx,psi_vyx,psi_vzx,psi_syy_y,psi_sxy_y,psi_syz_y,psi_vyy,psi_vxy,psi_vzy,psi_vxxs,pr,pp,pq,pt,po);
                            } else {
                                zero_fdveps_viscac(-nd+1, NY+nd, -nd+1, NX+nd, pvx, pvy, psp, pvxp1, pvyp1, psi_sxx_x, psi_sxy_x, psi_vxx, psi_vyx, psi_syy_y, psi_sxy_y, psi_vyy, psi_vxy, psi_vxxs, pp); }
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
                        }else{
                            if(!ACOUSTIC)
                                zero_fdveps(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,pvz,psxx,psyy,psxy,psxz,psyz,ux,uy,uxy,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_sxz_x,psi_vxx,psi_vyx,psi_vzx,psi_syy_y,psi_sxy_y,psi_syz_y,psi_vyy,psi_vxy,psi_vzy,psi_vxxs);
                            else
                                zero_fdveps_ac(-nd+1,NY+nd,-nd+1,NX+nd,pvx,pvy,psp,pvxp1,pvyp1,psi_sxx_x,psi_sxy_x,psi_vxx,psi_vyx,psi_syy_y,psi_sxy_y,psi_vyy,psi_vxy,psi_vxxs);
                        }
                        
                        /*initialize gradient matrices for each shot with zeros PSV*/
                        if(WAVETYPE==1 || WAVETYPE==3) {
                            if(INVMAT==0){
1638 1639
                                for(j=1;j<=NY;j=j+IDY){
                                    for(i=1;i<=NX;i=i+IDX){
1640 1641 1642 1643 1644
                                        waveconv_shot[j][i]=0.0;
                                        waveconv_rho_shot[j][i]=0.0;
                                    }
                                }
                                if(!ACOUSTIC){
1645 1646
                                    for(j=1;j<=NY;j=j+IDY){
                                        for(i=1;i<=NX;i=i+IDX){
1647 1648 1649 1650 1651 1652 1653 1654 1655
                                            waveconv_u_shot[j][i]=0.0;
                                        }
                                    }
                                }
                            }
                        }
                        /*initialize gradient matrices for each shot with zeros SH*/
                        if(WAVETYPE==2 || WAVETYPE==3){
                            if(INVMAT==0){
1656 1657
                                for(j=1;j<=NY;j=j+IDY){
                                    for(i=1;i<=NX;i=i+IDX){
1658 1659 1660 1661 1662 1663 1664 1665 1666
                                        waveconv_rho_shot_z[j][i]=0.0;
                                        waveconv_u_shot_z[j][i]=0.0;
                                    }
                                }
                                
                            }
                        }
                        
                        if((EPRECOND==1)||(EPRECOND==3)&&(EPRECOND_ITER==iter||(EPRECOND_ITER==0))){
1667 1668
                            for(j=1;j<=NY;j=j+IDY){
                                for(i=1;i<=NX;i=i+IDX){
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
                                    if(WAVETYPE==1 || WAVETYPE==3){
                                        Ws[j][i]=0.0;
                                        Wr[j][i]=0.0;
                                        We[j][i]=0.0;
                                    }
                                    if(WAVETYPE==2 || WAVETYPE==3){
                                        Ws_SH[j][i]=0.0;
                                        Wr_SH[j][i]=0.0;
                                        We_SH[j][i]=0.0;
                                    }
                                }
                            }
                        }
                        
                        
                        lsnap=iround(TSNAP1/DT); lsamp=NDT; nsnap=0;
                        hin=1; hin1=1;
                        imat=1; imat1=1; imat2=1; hi=1;
                        
                        if((!VERBOSE)&&(MYID==0)) fprintf(FP,"\n ****************************************\n ");
                        
1690
                        
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
                        /*------------------------------------------------------------------------------*/
                        /*----------------------  start loop over timesteps (forward model) ------------*/
                        /*------------------------------------------------------------------------------*/
                        for (nt=1;nt<=NT;nt++){
                            
                            // Ratio to give output to stout
                            infoout = !(nt%nt_out);
                            
                            if((!VERBOSE)&&(MYID==0)) if(!(nt%(NT/40))) fprintf(FP,"*");
                            
                            /* Check if simulation is still stable P and SV */
                            if (WAVETYPE==1 || WAVETYPE==3) {
                                if (isnan(pvy[NY/2][NX/2])) {
                                    fprintf(FP,"\n Time step: %d; pvy: %f \n",nt,pvy[NY/2][NX/2]);
                                    err(" Simulation is unstable !");
                                }
                            }
                            
                            /* Check if simulation is still stable SH */
                            if (WAVETYPE==2 || WAVETYPE==3) {
                                if (isnan(pvz[NY/2][NX/2])) {
                                    fprintf(FP,"\n Time step: %d; pvy: %f \n",nt,pvy[NY/2][NX/2]);
                                    err(" Simulation is unstable !");
                                }
                            }
                            
                            
                            
                            if (MYID==0){
                                if (infoout)  fprintf(FP,"\n Computing timestep %d of %d \n",nt,NT);
                                time3=MPI_Wtime();
                            }
                            
                            
                            /* update of particle velocities */
                            if(!ACOUSTIC) {
                                if (WAVETYPE==1 || WAVETYPE==3) {
                                    update_v_PML(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, uttx, utty, psxx, psyy, psxy, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,0, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_sxx_x, psi_syy_y, psi_sxy_y, psi_sxy_x);
                                }
                                
                                if (WAVETYPE==2 || WAVETYPE==3) {
                                    update_v_PML_SH(1, NX, 1, NY, nt, pvz, pvzp1, pvzm1, psxz, psyz,prjp, srcpos_loc, signals, signals_SH, nsrc_loc, absorb_coeff,hc,infoout,0, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_sxz_x, psi_syz_y);
                                }
Florian Wittkamp's avatar
Florian Wittkamp committed
1734
                            } else {
1735
                                update_v_acoustic_PML(1, NX, 1, NY, nt, pvx, pvxp1, pvxm1, pvy, pvyp1, pvym1, psp, prip, prjp, srcpos_loc,signals,signals,nsrc_loc,absorb_coeff,hc,infoout,0, K_x_half, a_x_half, b_x_half, K_y_half, a_y_half, b_y_half, psi_sxx_x, psi_syy_y);
Florian Wittkamp's avatar
Florian Wittkamp committed
1736
                            }
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
                            
                            if (MYID==0){
                                time4=MPI_Wtime();
                                time_av_v_update+=(time4-time3);
                                if (infoout)  fprintf(FP," particle velocity exchange between PEs ...");
                            }
                            
                            /* exchange of particle velocities between PEs */
                            exchange_v(pvx,pvy,pvz, bufferlef_to_rig, bufferrig_to_lef, buffertop_to_bot, bufferbot_to_top, req_send, req_rec,wavetype_start);
                            
                            if (MYID==0){
                                time5=MPI_Wtime();
                                time_av_v_exchange+=(time5-time4);
                                if (infoout)  fprintf(FP," finished (real time: %4.2f s).\n",time5-time4);
                            }
                            
                            if (L) {   /* viscoelastic */
Florian Wittkamp's avatar
Florian Wittkamp committed
1754 1755 1756 1757 1758 1759
                                if (WAVETYPE==1 || WAVETYPE==3) {
                                    if(!ACOUSTIC) {
                                        update_s_visc_PML(1, NX, 1, NY, pvx, pvy, ux, uy, uxy, uyx, psxx, psyy, psxy, ppi, pu, puipjp, prho, hc, infoout, pr, pp, pq, fipjp, f, g, bip, bjm, cip, cjm, d, e, dip, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                    }else{
                                        update_p_visc_PML(1, NX, 1, NY, pvx, pvy, psp, ppi, prho, hc, infoout, pp, g, bjm, cjm, e, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                    }
1760 1761
                                }
                                if (WAVETYPE==2 || WAVETYPE==3) {
1762
                                    update_s_visc_PML_SH(1, NX, 1, NY, pvz, psxz, psyz, pt, po, bip, bjm, cip, cjm, d, dip,fipjp, f, hc,infoout, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half,psi_vzx, psi_vzy);
1763 1764
                                }
                            } else {   /* elastic */
Florian Wittkamp's avatar
Florian Wittkamp committed
1765 1766 1767 1768 1769
                                if (WAVETYPE==1 || WAVETYPE==3) {
                                    if(!ACOUSTIC) {
                                        update_s_elastic_PML(1, NX, 1, NY, pvx, pvy, ux, uy, uxy, uyx, psxx, psyy, psxy, ppi, pu, puipjp, absorb_coeff, prho, hc, infoout, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
                                    } else {
                                        update_p_PML(1, NX, 1, NY, pvx, pvy, psp, ppi, absorb_coeff, prho, hc, infoout, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half, psi_vxx, psi_vyy, psi_vxy, psi_vyx);
1770 1771
                                    }
                                }
Florian Wittkamp's avatar
Florian Wittkamp committed
1772 1773 1774
                                if (WAVETYPE==2 || WAVETYPE==3) {
                                    update_s_elastic_PML_SH(1, NX, 1, NY, pvz,psxz,psyz,uxz,uyz,hc,infoout, K_x, a_x, b_x, K_x_half, a_x_half, b_x_half, K_y, a_y, b_y, K_y_half, a_y_half, b_y_half,psi_vzx, psi_vzy,puipjp,pu,prho);
                                }
1775 1776
                            }
                            
Florian Wittkamp's avatar
Florian Wittkamp committed
1777 1778
                            
                            
1779
                            /* explosive source */
1780
                            if ((SOURCE_TYPE==1)&&(WAVETYPE==1||WAVETYPE==3))
1781 1782
                                psource(nt,psxx,psyy,psp,srcpos_loc,signals,nsrc_loc,0);
                            
1783
                            /* Applying free surface condition */
1784
                            if ((FREE_SURF) && (POS[2]==0)){
1785 1786 1787
                                if (!ACOUSTIC){
                                    if (L){
                                        /* viscoelastic */
1788
                                        surface_PML(1, pvx, pvy, psxx, psyy, psxy,psyz, pp, pq, ppi, pu, prho, ptaup, ptaus, etajm, peta, hc, K_x, a_x, b_x, psi_vxxs, ux, uy,uxy,uyz,psxz,uxz);
Florian Wittkamp's avatar
Florian Wittkamp committed
1789
                                    }else{
1790 1791
                                        /* elastic */
                                        surface_elastic_PML(1, pvx, pvy, psxx, psyy, psxy,psyz, ppi, pu, prho, hc, K_x, a_x, b_x, psi_vxxs, ux, uy, uxy,uyz,psxz,uxz);
Florian Wittkamp's avatar
Florian Wittkamp committed
1792
                                    }
1793