restart_workflow.c 7.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/*-----------------------------------------------------------------------------------------
 * Copyright (C) 2015  For the list of authors, see file AUTHORS.
 *
 * This file is part of DENISE.
 *
 * 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.
 *
 * 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.
 *
 * 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>.
 -----------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 *  Restart workflow                                Oct 2015
 *
 *  written by Wittkamp, parts copied out of readmod.c
 *
 *  If you want to adjust the workflow, I think it is not necessary to
 *  modify this file. Have a look at apply_workflow.c and adjust
 *  WORKFLOW_MAX_VAR in fd.h.
 *  ----------------------------------------------------------------------*/

#include "fd.h"

void restart_workflow(int iter,float  **  rho, float **  pi, float **  u,float ** taus, float **taup){
    
    extern int NX, NY, NXG, NYG,  POS[3], MYID, INVMAT1,L,WAVETYPE,ACOUSTIC;
    extern char  MFILE[STRING_SIZE],INV_MODELFILE[STRING_SIZE];
    extern FILE *FP;
    
    /* local variables */
    float rhov, muv, piv, vp, vs;
    float taupp, tauss;
    int i, j, ii, jj;
    FILE *fp_vs, *fp_vp, *fp_rho;
    FILE *fp_taus, *fp_taup;
    char modfile[STRING_SIZE];
    
    
    fprintf(FP,"\n Restart inversion from workflow \n");
    
    /* Check dependencies */
    if(INVMAT1!=1) err(" Restart of inversion only if INVMAT1=1! ");
    
    /* Read in old model files */
    if(INVMAT1==1){
        if(WAVETYPE==1||WAVETYPE==3){
            if(!ACOUSTIC){
                sprintf(modfile,"%s_vp_it%d.bin",INV_MODELFILE,iter);
                fprintf(FP,"\n trying to open %s",modfile);
                fp_vp=fopen(modfile,"r");
                if (fp_vp==NULL) {
                    fprintf(FP,"\n ****** FAILSAFE-MODE VP ******");
                    fprintf(FP,"\n NOT able to open %s",modfile);
                    sprintf(modfile,"%s_vp.bin",INV_MODELFILE);
                    fprintf(FP,"\n trying to open %s\n",modfile);
                    fp_vp=fopen(modfile,"r");
                    if (fp_vp==NULL) {
                        err(" Could not open model file for Vp to restart inversion! ");
                    }
                }
            }
        }
        sprintf(modfile,"%s_vs_it%d.bin",INV_MODELFILE,iter);
        fprintf(FP,"\n trying to open %s",modfile);
        fp_vs=fopen(modfile,"r");
        if (fp_vs==NULL) {
            fprintf(FP,"\n ****** FAILSAFE-MODE VS ******");
            fprintf(FP,"\n NOT able to open %s",modfile);
            sprintf(modfile,"%s_vs.bin",INV_MODELFILE);
            fprintf(FP,"\n trying to open %s\n",modfile);
            fp_vs=fopen(modfile,"r");
            if (fp_vs==NULL) {
                err(" Could not open model file for Vs to restart inversion! ");
            }
        }
        
        sprintf(modfile,"%s_rho_it%d.bin",INV_MODELFILE,iter);
        fprintf(FP,"\n trying to open %s",modfile);
        fp_rho=fopen(modfile,"r");
        if (fp_rho==NULL) {
            fprintf(FP,"\n ****** FAILSAFE-MODE RHO ******");
            fprintf(FP,"\n NOT able to open %s",modfile);
            sprintf(modfile,"%s_rho.bin",INV_MODELFILE);
            fprintf(FP,"\n trying to open %s\n",modfile);
            fp_rho=fopen(modfile,"r");
            if (fp_rho==NULL) {
                err(" Could not open model file for Rho to restart inversion! ");
            }
        }
        
        if(L) {
            
            /*******/
            iter=0; // Up to now no tau inversion, therefore we open at *it0.bin
            /*******/
            
            sprintf(modfile,"%s_taus_it%d.bin",INV_MODELFILE,iter);
            fprintf(FP,"\n trying to open %s",modfile);
            fp_taus=fopen(modfile,"r");
            if (fp_taus==NULL) err(" Could not open model file for Taus to restart inversion ! ");
            
            sprintf(modfile,"%s_taup_it%d.bin",INV_MODELFILE,iter);
            fprintf(FP,"\n trying to open %s",modfile);
            fp_taup=fopen(modfile,"r");
            if (fp_taup==NULL) err(" Could not open model file for Taup to restart inversion ! ");
        }
        
    }
    
    /* loop over global grid */
    for (i=1;i<=NXG;i++){
        for (j=1;j<=NYG;j++){
            if(WAVETYPE==1||WAVETYPE==3&&!ACOUSTIC) fread(&vp, sizeof(float), 1, fp_vp);
            fread(&vs, sizeof(float), 1, fp_vs);
            fread(&rhov, sizeof(float), 1, fp_rho);
            if(L) {
                fread(&tauss, sizeof(float), 1, fp_taus);
                fread(&taupp, sizeof(float), 1, fp_taup);
            }
            
            /* only the PE which belongs to the current global gridpoint
             is saving model parameters in his local arrays */
            if ((POS[1]==((i-1)/NX)) &&
                (POS[2]==((j-1)/NY))){
                ii=i-POS[1]*NX;
                jj=j-POS[2]*NY;
                
                u[jj][ii]=vs;
                rho[jj][ii]=rhov;
                if(WAVETYPE==1||WAVETYPE==3&&!ACOUSTIC) pi[jj][ii]=vp;
                
                if(L) {
                    taus[jj][ii]=tauss;
                    taup[jj][ii]=taupp;
                }
                
            }
        }
    }
    
    /* each PE writes his model to disk */
    if(WAVETYPE==1||WAVETYPE==3){
        if(!ACOUSTIC){
            sprintf(modfile,"%s.restart.vp",INV_MODELFILE);
            writemod(modfile,pi,3);
            MPI_Barrier(MPI_COMM_WORLD);
            if (MYID==0) mergemod(modfile,3);
            
            MPI_Barrier(MPI_COMM_WORLD);
            sprintf(modfile,"%s.restart.vp.%i.%i",INV_MODELFILE,POS[1],POS[2]);
            remove(modfile);
        }
    }
    /* each PE writes his model to disk */
    sprintf(modfile,"%s.restart.vs",INV_MODELFILE);
    writemod(modfile,u,3);
    MPI_Barrier(MPI_COMM_WORLD);
    if (MYID==0) mergemod(modfile,3);
    
    MPI_Barrier(MPI_COMM_WORLD);
    sprintf(modfile,"%s.restart.vs.%i.%i",INV_MODELFILE,POS[1],POS[2]);
    remove(modfile);
    
    /* each PE writes his model to disk */
    sprintf(modfile,"%s.restart.rho",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.restart.rho.%i.%i",INV_MODELFILE,POS[1],POS[2]);
    remove(modfile);
    
    if(L) {
        /* each PE writes his model to disk */
        sprintf(modfile,"%s.restart.taup",INV_MODELFILE);
        writemod(modfile,taup,3);
        MPI_Barrier(MPI_COMM_WORLD);
        if (MYID==0) mergemod(modfile,3);
        
        MPI_Barrier(MPI_COMM_WORLD);
        sprintf(modfile,"%s.restart.taup.%i.%i",INV_MODELFILE,POS[1],POS[2]);
        remove(modfile);
        
        /* each PE writes his model to disk */
        sprintf(modfile,"%s.restart.taus",INV_MODELFILE);
        writemod(modfile,taus,3);
        MPI_Barrier(MPI_COMM_WORLD);
        if (MYID==0) mergemod(modfile,3);
        
        MPI_Barrier(MPI_COMM_WORLD);
        sprintf(modfile,"%s.restart.taus.%i.%i",INV_MODELFILE,POS[1],POS[2]);
        remove(modfile);
    }
}