calc_mat_change_test.c 16.4 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 4
 *
 * This file is part of DENISE.
5
 *
tilman.metz's avatar
tilman.metz committed
6 7 8
 * DENISE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
9
 *
tilman.metz's avatar
tilman.metz committed
10 11 12 13
 * DENISE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
tilman.metz's avatar
tilman.metz committed
15 16
 * You should have received a copy of the GNU General Public License
 * along with DENISE. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
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 26 27
 *   Daniel Koehn
 *   last update 9.11.2007
 *
 *  ---------------------------------------------------------------------*/

#include "fd.h"
28
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,
29
                          int epstest, int INVMAT, 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){
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    
    
    /*--------------------------------------------------------------------------*/
    FILE *FP1;
    /* extern variables */
    extern float DH, DT, VP_VS_RATIO;
    extern float EPSILON, EPSILON_u, EPSILON_rho, MUN;
    extern int NX, NY, NXG, NYG,  POS[3], MYID, INVMAT1,WAVETYPE;
    
    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 */
    
    float Rho, Vp, Vs, Vsnp1, Vpnp1, x, y, undf, r, pi0, K, mu, Zp, Zs, pro_vs, pro_vp, pro_rho;
    float dpi, pimax, rhomax, umax, gradmax, gradmax_rho, gradmax_u, epsilon1, pimaxr, gradmaxr, gradmaxr_u, umaxr, gradmaxr_rho, rhomaxr;
    int i, j, ii, jj, testuplow, pr=0;
    char modfile[STRING_SIZE];
    int w=0,l=0;
    
    extern char JACOBIAN[STRING_SIZE];
    char jac[225],jac2[225];
58
    FILE *FP_JAC,*FP_JAC2,*FP_JAC3;
59 60
    
    if(GRAD_METHOD==2&&(itest==0)){
61 62
        w=iter%N_LBFGS;
        if(w==0) w=N_LBFGS;
63
        
64 65 66 67
        if(!ACOUSTIC){
            sprintf(jac,"%s_s_LBFGS_vs_it%d_w%d.bin.%i.%i",JACOBIAN,iter+1,w,POS[1],POS[2]);
            FP_JAC=fopen(jac,"wb");
        }
68
        
69 70
        if(LBFGS_NPAR>1){
            sprintf(jac2,"%s_s_LBFGS_rho_it%d_w%d.bin.%i.%i",JACOBIAN,iter+1,w,POS[1],POS[2]);
71 72 73
            FP_JAC2=fopen(jac2,"wb");
        }
        
74 75 76 77 78
        if(LBFGS_NPAR>2){
            sprintf(jac2,"%s_s_LBFGS_vp_it%d_w%d.bin.%i.%i",JACOBIAN,iter+1,w,POS[1],POS[2]);
            FP_JAC3=fopen(jac2,"wb");
        }
        
79 80 81 82 83 84 85 86 87 88 89 90 91 92
        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;
    
93 94
    for (j=1;j<=NY;j++){
        for (i=1;i<=NX;i++){
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            
            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;
        
115 116
        for (j=1;j<=NY;j++){
            for (i=1;i<=NX;i++){
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                
                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;
    
137 138
    for (j=1;j<=NY;j++){
        for (i=1;i<=NX;i++){
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            
            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 */
            if((INVMAT1==3) || (INVMAT1==1)){
                
                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++;
318 319 320 321 322
                        
                        if(!ACOUSTIC) {
                            s_LBFGS[w][l]=(unp1[j][i]-u[j][i])/Vs_avg;
                            fwrite(&s_LBFGS[w][l],sizeof(float),1,FP_JAC);
                        }
323
                        
324 325 326
                        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);
327 328
                        }
                        
329 330 331
                        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);
332
                        }
333
                        
334 335 336 337 338 339 340 341 342 343
                    }
                    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)){
344 345 346 347 348 349 350 351 352
        if(!ACOUSTIC){
            fclose(FP_JAC);
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(jac,"%s_s_LBFGS_vs_it%d_w%d.bin",JACOBIAN,iter+1,w);
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(jac,"%s_s_LBFGS_vs_it%d_w%d.bin.%i.%i",JACOBIAN,iter+1,w,POS[1],POS[2]);
            remove(jac);
        }
353
        
354
        if(LBFGS_NPAR>1){
355 356
            fclose(FP_JAC2);
            MPI_Barrier(MPI_COMM_WORLD);
357 358 359 360 361 362 363 364 365 366 367
            sprintf(jac,"%s_s_LBFGS_rho_it%d_w%d.bin",JACOBIAN,iter+1,w);
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(jac,"%s_s_LBFGS_rho_it%d_w%d.bin.%i.%i",JACOBIAN,iter+1,w,POS[1],POS[2]);
            remove(jac);
        }
        
        if(LBFGS_NPAR>2){
            fclose(FP_JAC3);
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(jac,"%s_s_LBFGS_vp_it%d_w%d.bin",JACOBIAN,iter+1,w);
368 369
            if (MYID==0) mergemod(jac,3);
            MPI_Barrier(MPI_COMM_WORLD);
370
            sprintf(jac,"%s_s_LBFGS_vp_it%d_w%d.bin.%i.%i",JACOBIAN,iter+1,w,POS[1],POS[2]);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
            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
450
        
451 452 453 454 455
        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);
    }
tilman.metz's avatar
tilman.metz committed
456
}