calc_mat_change_test.c 17.6 KB
Newer Older
tilman.metz's avatar
tilman.metz committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
tilman.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz committed
18 19 20

/*------------------------------------------------------------------------
 *   calculate test step length for material parameter update
21
 *
tilman.metz's avatar
tilman.metz committed
22 23 24 25
 *
 *  ---------------------------------------------------------------------*/

#include "fd.h"
26
void calc_mat_change_test(float  **  waveconv, float  **  waveconv_rho, float  **  waveconv_u, float  **  rho, float  **  rhonp1, float **  pi, float **  pinp1, float **  u, float **  unp1, int iter,
27
                          int epstest, int FORWARD_ONLY, float eps_scale, int itest, int nfstart, float ** u_start, float ** pi_start, float ** rho_start,int wavetype_start,float **s_LBFGS,int N_LBFGS,int LBFGS_NPAR,float Vs_avg,float Vp_avg,float rho_avg,int LBFGS_iter_start){
28 29 30 31 32 33
    
    
    /*--------------------------------------------------------------------------*/
    /* extern variables */
    extern float DH, DT, VP_VS_RATIO;
    extern float EPSILON, EPSILON_u, EPSILON_rho, MUN;
34
    extern int NX, NY, NXG, NYG,  POS[3], MYID, PARAMETERIZATION,WAVETYPE;
35 36 37 38 39 40 41 42 43 44 45 46
    
    extern int INV_RHO_ITER, INV_VP_ITER, INV_VS_ITER;
    extern int VERBOSE;
    extern char INV_MODELFILE[STRING_SIZE];
    extern int GRAD_METHOD;
    extern float VPUPPERLIM, VPLOWERLIM, VSUPPERLIM, VSLOWERLIM, RHOUPPERLIM, RHOLOWERLIM;
    extern int LBFGS_STEP_LENGTH;
    extern int S, ACOUSTIC;
    extern float S_VS, S_VP, S_RHO;
    extern int GRAD_METHOD;
    /* local variables */
    
47 48 49
    float  Zp, Zs, pro_vs, pro_vp, pro_rho;
    float  pimax, rhomax, umax, gradmax, gradmax_rho, gradmax_u, epsilon1, pimaxr, gradmaxr, gradmaxr_u = 0.0, umaxr = 0.0, gradmaxr_rho, rhomaxr;
    int i, j, testuplow, pr=0;
50 51 52 53 54
    char modfile[STRING_SIZE];
    int w=0,l=0;
    
    extern char JACOBIAN[STRING_SIZE];
    char jac[225],jac2[225];
55
    FILE *FP_JAC = NULL,*FP_JAC2 = NULL,*FP_JAC3 = NULL;
56 57
    
    if(GRAD_METHOD==2&&(itest==0)){
58 59
        w=iter%N_LBFGS;
        if(w==0) w=N_LBFGS;
60
        
61
        if(!ACOUSTIC){
62
            sprintf(jac,"%s_s_LBFGS_vs_it%d.bin.%i.%i",JACOBIAN,iter+1,POS[1],POS[2]);
63 64
            FP_JAC=fopen(jac,"wb");
        }
65
        
66
        if(LBFGS_NPAR>1){
67
            sprintf(jac2,"%s_s_LBFGS_rho_it%d.bin.%i.%i",JACOBIAN,iter+1,POS[1],POS[2]);
68 69 70
            FP_JAC2=fopen(jac2,"wb");
        }
        
71
        if(LBFGS_NPAR>2){
72
            sprintf(jac2,"%s_s_LBFGS_vp_it%d.bin.%i.%i",JACOBIAN,iter+1,POS[1],POS[2]);
73 74 75
            FP_JAC3=fopen(jac2,"wb");
        }
        
76 77 78 79 80 81 82 83 84 85 86 87 88 89
        if(MYID==0) printf("\n\n ------------ L-BFGS ---------------");
        if(MYID==0) printf("\n Saving model difference for L-BFGS");
        if(MYID==0) printf("\n At Iteration %i in L-BFGS vector %i\n",iter,w);
        l=0;
        
        
    }
    /* invert for Zp and Zs */
    /* ------------------------------------------------------------------------------------ */
    
    /* find maximum of Zp and gradient waveconv */
    pimax = 0.0;
    gradmax = 0.0;
    
90 91
    for (j=1;j<=NY;j++){
        for (i=1;i<=NX;i++){
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            
            Zp = pi[j][i];
            
            if(Zp>pimax)
                pimax=Zp;
            
            if((i*j == 1) || (gradmax == 0.0)) {
                gradmax = fabs(waveconv[j][i]);
            } else {
                if(fabs(waveconv[j][i]) > gradmax)
                    gradmax = fabs(waveconv[j][i]);
            }
        }
    }
    
    /* find maximum of Zs and gradient waveconv_u */
    if(!ACOUSTIC){
        umax = 0.0;
        gradmax_u = 0.0;
        
112 113
        for (j=1;j<=NY;j++){
            for (i=1;i<=NX;i++){
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                
                Zs = u[j][i];
                
                if(Zs>umax)
                    umax=Zs;
                
                if((i*j == 1) || (gradmax_u == 0.0)) {
                    gradmax_u = fabs(waveconv_u[j][i]);
                } else {
                    if(fabs(waveconv_u[j][i]) > gradmax_u)
                        gradmax_u = fabs(waveconv_u[j][i]);
                }
            }
        }
    }
    
    /* find maximum of rho and gradient waveconv_rho */
    rhomax = 0.0;
    gradmax_rho = 0.0;
    
134 135
    for (j=1;j<=NY;j++){
        for (i=1;i<=NX;i++){
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
            
            if(rho[j][i]>rhomax){rhomax=rho[j][i];}
            
            if((i*j == 1) || (gradmax_rho == 0.0)) {
                gradmax_rho = fabs(waveconv_rho[j][i]);
            } else {
                if(fabs(waveconv_rho[j][i]) > gradmax_rho)
                    gradmax_rho = fabs(waveconv_rho[j][i]);
            }
            
        }
    }
    
    
    /* calculate scaling factor for the gradients */
    /* --------------------------------------------- */
    
    /* parameter 1 */
    MPI_Allreduce(&pimax,  &pimaxr,  1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
    MPI_Allreduce(&gradmax,&gradmaxr,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
    
    EPSILON = eps_scale * (pimaxr/gradmaxr);
    if (iter<INV_VP_ITER){EPSILON = 0.0;}
    epsilon1=EPSILON;
    
    /* L-BFGS - No normalisation */
    if(GRAD_METHOD==2&&(iter>LBFGS_iter_start)&&LBFGS_STEP_LENGTH) {
        EPSILON=eps_scale;
        if (iter<INV_VP_ITER){EPSILON = 0.0;}
    }
    
    MPI_Allreduce(&EPSILON,&epsilon1,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
    
    if (MYID==0)  EPSILON=epsilon1;
    
    exchange_par();
    
    if(!ACOUSTIC){
        /* parameter 2 */
        MPI_Allreduce(&umax,&umaxr,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
        MPI_Allreduce(&gradmax_u,&gradmaxr_u,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
        
        EPSILON_u = eps_scale * (umaxr/gradmaxr_u);
        if (iter<INV_VS_ITER){EPSILON_u = 0.0;}
        epsilon1=EPSILON_u;
        
        /* L-BFGS - No normalisation */
        if(GRAD_METHOD==2&&(iter>LBFGS_iter_start)&&LBFGS_STEP_LENGTH) {
            EPSILON_u=eps_scale;
            if (iter<INV_VS_ITER){EPSILON_u = 0.0;}
        }
        
        MPI_Allreduce(&EPSILON_u,&epsilon1,1,MPI_FLOAT,MPI_MIN,MPI_COMM_WORLD);
        
        if (MYID==0)  EPSILON_u=epsilon1;
        
        exchange_par();
    }
    
    /* parameter 3 */
    MPI_Allreduce(&rhomax,&rhomaxr,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
    MPI_Allreduce(&gradmax_rho,&gradmaxr_rho,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
    
    EPSILON_rho = eps_scale * (rhomaxr/gradmaxr_rho);
    if (iter<INV_RHO_ITER){EPSILON_rho = 0.0;}
    epsilon1=EPSILON_rho;
    
    /* L-BFGS - No normalisation */
    if(GRAD_METHOD==2&&(iter>LBFGS_iter_start)&&LBFGS_STEP_LENGTH) {
        EPSILON_rho=eps_scale;
        if (iter<INV_RHO_ITER){EPSILON_rho = 0.0;}
    }
    
    MPI_Allreduce(&EPSILON_rho,&epsilon1,1,MPI_FLOAT,MPI_MIN,MPI_COMM_WORLD);
    
    if (MYID==0)  EPSILON_rho=epsilon1;
    
    exchange_par();
    
    if(MYID==0&&VERBOSE){
        printf("MYID = %d \t pimaxr = %e \t gradmaxr = %e \n",MYID,pimaxr,gradmaxr);
        printf("MYID = %d \t EPSILON = %e \n",MYID,EPSILON);
        if(!ACOUSTIC)
            printf("MYID = %d \t umaxr = %e \t gradmaxr_u = %e \n",MYID,umaxr,gradmaxr_u);
        printf("MYID = %d \t rhomaxr = %e \t gradmaxr_rho = %e \n",MYID,rhomaxr,gradmaxr_rho);
    }
    
    /* loop over local grid */
    for (i=1;i<=NX;i++){
        for (j=1;j<=NY;j++){
            
            /* update lambda, mu, rho */
228
            if((PARAMETERIZATION==3) || (PARAMETERIZATION==1)){
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
                
                testuplow=0;
                
                pinp1[j][i] = pi[j][i] - EPSILON*waveconv[j][i];
                if(!ACOUSTIC)
                    unp1[j][i] = u[j][i] - EPSILON_u*waveconv_u[j][i];
                rhonp1[j][i] = rho[j][i] - EPSILON_rho*waveconv_rho[j][i];
                
                
                
                
                if(pinp1[j][i]<VPLOWERLIM){
                    pinp1[j][i] = VPLOWERLIM;
                }
                
                if(pinp1[j][i]>VPUPPERLIM){
                    pinp1[j][i] = VPUPPERLIM;
                }
                
                
                if(!ACOUSTIC){
                    if((unp1[j][i]<VSLOWERLIM)&&(unp1[j][i]>1e-6)){
                        unp1[j][i] = VSLOWERLIM;
                    }
                    if(unp1[j][i]>VSUPPERLIM){
                        unp1[j][i] = u[j][i];
                    }
                    
                    /* checking poisson ratio */
                    if(VP_VS_RATIO>1){
                        if((pinp1[j][i]/unp1[j][i])<VP_VS_RATIO){
                            pinp1[j][i]=unp1[j][i]*VP_VS_RATIO;
                            pr=1;
                        }
                    }/* if(VP_VS_RATIO>1) */
                }
                
                if(S==1){
                    if(!ACOUSTIC){
                        /* limited update for Vs */
                        pro_vs=((u_start[j][i]-unp1[j][i])/u_start[j][i])*100.0;
                        if(((fabs(pro_vs))>S_VS)&&(pro_vs>0.0))
                            unp1[j][i] = u_start[j][i] - u_start[j][i] * S_VS / 100.0;
                        if(((fabs(pro_vs))>S_VS)&&(pro_vs<0.0))
                            unp1[j][i] = u_start[j][i] + u_start[j][i] * S_VS / 100.0;
                    }
                    
                    /* limited update for Vp */
                    pro_vp=((pi_start[j][i]-pinp1[j][i])/pi_start[j][i])*100.0;
                    if(((fabs(pro_vp))>S_VP)&&(pro_vp>0.0))
                        pinp1[j][i] = pi_start[j][i] - pi_start[j][i] * S_VP / 100.0;
                    if(((fabs(pro_vp))>S_VP)&&(pro_vp<0.0))
                        pinp1[j][i] = pi_start[j][i] + pi_start[j][i] * S_VP / 100.0;
                    
                    /* limited update for Rho */
                    pro_rho=((rho_start[j][i]-rhonp1[j][i])/rho_start[j][i])*100.0;
                    if(((fabs(pro_rho))>S_RHO)&&(pro_rho>0.0))
                        rhonp1[j][i] = rho_start[j][i] - rho_start[j][i] * S_RHO / 100.0;
                    if(((fabs(pro_rho))>S_RHO)&&(pro_rho<0.0))
                        rhonp1[j][i] = rho_start[j][i] + rho_start[j][i] * S_RHO / 100.0;
                }
                
                
                if((rhonp1[j][i]<RHOLOWERLIM) && (INV_RHO_ITER < iter)){
                    rhonp1[j][i] = rho[j][i];
                }
                if((rhonp1[j][i]>RHOUPPERLIM) && (INV_RHO_ITER < iter)){
                    rhonp1[j][i] = rho[j][i];
                }
                
                
                /* None of these parameters should be smaller than zero */
                if(pinp1[j][i]<0.0){
                    pinp1[j][i] = pi[j][i];
                }
                if(!ACOUSTIC)
                    if(unp1[j][i]<0.0){
                        unp1[j][i] = u[j][i];
                    }
                if(rhonp1[j][i]<0.0){
                    rhonp1[j][i] = rho[j][i];
                }
                
                if(itest==0){
                    if(GRAD_METHOD==2){
                        l++;
315 316 317 318 319
                        
                        if(!ACOUSTIC) {
                            s_LBFGS[w][l]=(unp1[j][i]-u[j][i])/Vs_avg;
                            fwrite(&s_LBFGS[w][l],sizeof(float),1,FP_JAC);
                        }
320
                        
321 322 323
                        if(LBFGS_NPAR>1) {
                            s_LBFGS[w][l+NX*NY]=(rhonp1[j][i]-rho[j][i])/rho_avg;
                            fwrite(&s_LBFGS[w][l+NY*NX],sizeof(float),1,FP_JAC2);
324 325
                        }
                        
326 327 328
                        if(LBFGS_NPAR>2){
                            s_LBFGS[w][l+2*NX*NY]=(pinp1[j][i]-pi[j][i])/Vp_avg;
                            fwrite(&s_LBFGS[w][l+2*NY*NX],sizeof(float),1,FP_JAC3);
329
                        }
330
                        
331 332 333 334 335 336 337 338 339 340
                    }
                    if(!ACOUSTIC) u[j][i] = unp1[j][i];
                    rho[j][i] = rhonp1[j][i];
                    pi[j][i] = pinp1[j][i];
                }
            }
        }
    }
    
    if(GRAD_METHOD==2&&(itest==0)){
341 342 343
        if(!ACOUSTIC){
            fclose(FP_JAC);
            MPI_Barrier(MPI_COMM_WORLD);
344
            sprintf(jac,"%s_s_LBFGS_vs_it%d.bin",JACOBIAN,iter+1);
345 346
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
347
            sprintf(jac,"%s_s_LBFGS_vs_it%d.bin.%i.%i",JACOBIAN,iter+1,POS[1],POS[2]);
348 349
            remove(jac);
        }
350
        
351
        if(LBFGS_NPAR>1){
352 353
            fclose(FP_JAC2);
            MPI_Barrier(MPI_COMM_WORLD);
354
            sprintf(jac,"%s_s_LBFGS_rho_it%d.bin",JACOBIAN,iter+1);
355 356
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
357
            sprintf(jac,"%s_s_LBFGS_rho_it%d.bin.%i.%i",JACOBIAN,iter+1,POS[1],POS[2]);
358 359 360 361 362 363
            remove(jac);
        }
        
        if(LBFGS_NPAR>2){
            fclose(FP_JAC3);
            MPI_Barrier(MPI_COMM_WORLD);
364
            sprintf(jac,"%s_s_LBFGS_vp_it%d.bin",JACOBIAN,iter+1);
365 366
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
367
            sprintf(jac,"%s_s_LBFGS_vp_it%d.bin.%i.%i",JACOBIAN,iter+1,POS[1],POS[2]);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
            remove(jac);
        }
    }
    if(!ACOUSTIC)
        if((MYID==0)&&(pr==1))printf("\nThe Vp/Vs-ratio of %4.2f is violated. P-wave velocity will be increased that the Vp/Vs-ratio is at least %4.2f \n",VP_VS_RATIO,VP_VS_RATIO);
    /*if((MYID==0)&&(f==1))printf("\n RHO_VS == %d: Rho is coupled to Vp about Gardner Relationship \n",RHO_VS); */
    
    
    if(itest==0){
        if((wavetype_start==1||wavetype_start==3)){
            
            sprintf(modfile,"%s_vp.bin",INV_MODELFILE);
            writemod(modfile,pinp1,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            
            if (MYID==0) mergemod(modfile,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s_vp.bin.%i.%i",INV_MODELFILE,POS[1],POS[2]);
            remove(modfile);
            
        }
        if(!ACOUSTIC){
            
            sprintf(modfile,"%s_vs.bin",INV_MODELFILE);
            
            writemod(modfile,unp1,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            
            if (MYID==0) mergemod(modfile,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s_vs.bin.%i.%i",INV_MODELFILE,POS[1],POS[2]);
            remove(modfile);
            
        }
        
        
        sprintf(modfile,"%s_rho.bin",INV_MODELFILE);
        writemod(modfile,rho,3);
        
        MPI_Barrier(MPI_COMM_WORLD);
        
        if (MYID==0) mergemod(modfile,3);
        
        MPI_Barrier(MPI_COMM_WORLD);
        sprintf(modfile,"%s_rho.bin.%i.%i",INV_MODELFILE,POS[1],POS[2]);
        remove(modfile);
        
    }
    
    if((itest==0)&&(iter==nfstart)){
        if((wavetype_start==1||wavetype_start==3)){
            sprintf(modfile,"%s_vp_it%d.bin",INV_MODELFILE,iter);
            writemod(modfile,pi,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            
            if (MYID==0) mergemod(modfile,3);
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s_vp_it%d.bin.%i.%i",INV_MODELFILE,iter,POS[1],POS[2]);
            remove(modfile);
        }
        if(!ACOUSTIC){
            sprintf(modfile,"%s_vs_it%d.bin",INV_MODELFILE,iter);
            writemod(modfile,u,3);
            MPI_Barrier(MPI_COMM_WORLD);
            
            if (MYID==0) mergemod(modfile,3);
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s_vs_it%d.bin.%i.%i",INV_MODELFILE,iter,POS[1],POS[2]);
            remove(modfile);
        }
        
        sprintf(modfile,"%s_rho_it%d.bin",INV_MODELFILE,iter);
        writemod(modfile,rho,3);
        MPI_Barrier(MPI_COMM_WORLD);
tilman.metz's avatar
tilman.metz committed
447
        
448 449 450 451 452
        if (MYID==0) mergemod(modfile,3);
        MPI_Barrier(MPI_COMM_WORLD);
        sprintf(modfile,"%s_rho_it%d.bin.%i.%i",INV_MODELFILE,iter,POS[1],POS[2]);
        remove(modfile);
    }
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
    
    if(VERBOSE&&(itest==1)){
        if((wavetype_start==1||wavetype_start==3)){
            
            sprintf(modfile,"%s_vp.step.bin",INV_MODELFILE);
            writemod(modfile,pinp1,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            
            if (MYID==0) mergemod(modfile,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s_vp.step.bin.%i.%i",INV_MODELFILE,POS[1],POS[2]);
            remove(modfile);
            
        }
        if(!ACOUSTIC){
            
            sprintf(modfile,"%s_vs.step.bin",INV_MODELFILE);
            
            writemod(modfile,unp1,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            
            if (MYID==0) mergemod(modfile,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s_vs.step.bin.%i.%i",INV_MODELFILE,POS[1],POS[2]);
            remove(modfile);
            
        }
        
        
        sprintf(modfile,"%s_rho.step.bin",INV_MODELFILE);
        writemod(modfile,rho,3);
        
        MPI_Barrier(MPI_COMM_WORLD);
        
        if (MYID==0) mergemod(modfile,3);
        
        MPI_Barrier(MPI_COMM_WORLD);
        sprintf(modfile,"%s_rho.bin.step.%i.%i",INV_MODELFILE,POS[1],POS[2]);
        remove(modfile);
        
    }
tilman.metz's avatar
tilman.metz committed
498
}