readmod.c 9.25 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

/*------------------------------------------------------------------------
20
 *   Read viscoelastic model properties (vp,vs,density,Qp,Qs) from files
Tilman Steinweg's avatar
Tilman Steinweg committed
21 22 23 24 25
 *
 *  ----------------------------------------------------------------------*/


/* This file contains function readmod, which has the purpose
26
 to read data from model-files for viscoelastic simulation */
Tilman Steinweg's avatar
Tilman Steinweg committed
27 28 29 30

#include "fd.h"

void readmod(float  **  rho, float **  pi, float **  u, float ** taus, float ** taup, float * eta){
31 32 33
    
    extern float DT, *FL, TAU;
    extern int L,WAVETYPE, VERBOSE;
34
    extern int NX, NY, NXG, NYG,  POS[3], MYID, PARAMETERIZATION;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    extern char  MFILE[STRING_SIZE];
    extern FILE *FP;
    
    
    /* local variables */
    float rhov, muv, piv, vp, vs, qp, qs, *pts;
    int i, j, ii, jj, l, sw_Qp=1, sw_Qs=1;
    FILE *fp_vs, *fp_vp, *fp_rho, *fp_qp, *fp_qs;
    char filename[STRING_SIZE];
    
    
    /* vector for maxwellbodies */
    pts=vector(1,L);
    for (l=1;l<=L;l++) {
        pts[l]=1.0/(2.0*PI*FL[l]);
        eta[l]=DT/pts[l];
    }
    
53
	   fprintf(FP,"\n...reading model information from model-files...\n");
54
    
Tilman Steinweg's avatar
Tilman Steinweg committed
55 56
	   /* read density and seismic velocities */
	   /* ----------------------------------- */
57
	   if(PARAMETERIZATION==1){
58 59 60 61 62
           
           if(WAVETYPE==1||WAVETYPE==3){
               fprintf(FP,"\t Vp:\n\t %s.vp\n\n",MFILE);
               sprintf(filename,"%s.vp",MFILE);
               fp_vp=fopen(filename,"r");
63
               if (fp_vp==NULL) declare_error(" Could not open model file for Vp ! ");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
               
               
               fprintf(FP,"\t Qp:\n\t %s.qp\n\n",MFILE);
               sprintf(filename,"%s.qp",MFILE);
               fp_qp=fopen(filename,"r");
               if (fp_qp==NULL){
                   if (MYID==0){
                       printf(" Could not open model file for Qp-values ! \n");
                       printf(" Uses input file value for tau! \n");}
                   sw_Qp=0;
               }
               
           }
           
           
           fprintf(FP,"\t Vs:\n\t %s.vs\n\n",MFILE);
           sprintf(filename,"%s.vs",MFILE);
           fp_vs=fopen(filename,"r");
82
           if (fp_vs==NULL) declare_error(" Could not open model file for Vs ! ");
83 84 85 86
           
           fprintf(FP,"\t Density:\n\t %s.rho\n\n",MFILE);
           sprintf(filename,"%s.rho",MFILE);
           fp_rho=fopen(filename,"r");
87
           if (fp_rho==NULL) declare_error(" Could not open model file for densities ! ");
88 89 90 91 92 93 94
           
           fprintf(FP,"\t Qs:\n\t %s.qs\n\n",MFILE);
           sprintf(filename,"%s.qs",MFILE);
           fp_qs=fopen(filename,"r");
           if (fp_qs==NULL){
               if (MYID==0){
                   printf(" Could not open model file for Qs-values ! ");
Tilman Steinweg's avatar
Tilman Steinweg committed
95 96 97
                   printf(" Uses input file value for tau! \n");}
               sw_Qs=0;
           }
98 99
           
       }
Tilman Steinweg's avatar
Tilman Steinweg committed
100 101 102
	   
	   /* read density and Lame parameters */
	   /* ----------------------------------- */
103
	   if(PARAMETERIZATION==3){
104 105 106
           fprintf(FP,"\t Lame parameter lambda:\n\t %s.lam\n\n",MFILE);
           sprintf(filename,"%s.lam",MFILE);
           fp_vp=fopen(filename,"r");
107
           if (fp_vp==NULL) declare_error(" Could not open model file for Lame parameter lambda ! ");
108 109 110 111 112
           
           
           fprintf(FP,"\t Lame parameter mu:\n\t %s.mu\n\n",MFILE);
           sprintf(filename,"%s.mu",MFILE);
           fp_vs=fopen(filename,"r");
113
           if (fp_vs==NULL) declare_error(" Could not open model file for Lame parameter mu ! ");
114 115 116 117
           
           fprintf(FP,"\t Density:\n\t %s.rho\n\n",MFILE);
           sprintf(filename,"%s.rho",MFILE);
           fp_rho=fopen(filename,"r");
118
           if (fp_rho==NULL) declare_error(" Could not open model file for densities ! ");
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
           
           fprintf(FP,"\t Qp:\n\t %s.qp\n\n",MFILE);
           sprintf(filename,"%s.qp",MFILE);
           fp_qp=fopen(filename,"r");
           if (fp_qp==NULL){
               if (MYID==0){
                   printf(" Could not open model file for Qp-values ! \n");
                   printf(" Uses input file value for tau! \n");}
               sw_Qp=0;
           }
           
           fprintf(FP,"\t Qs:\n\t %s.qs\n\n",MFILE);
           sprintf(filename,"%s.qs",MFILE);
           fp_qs=fopen(filename,"r");
           if (fp_qs==NULL){
               if (MYID==0){
                   printf(" Could not open model file for Qs-values ! ");
Tilman Steinweg's avatar
Tilman Steinweg committed
136 137 138
                   printf(" Uses input file value for tau! \n");}
               sw_Qs=0;
           }
139
       }
Tilman Steinweg's avatar
Tilman Steinweg committed
140
	   
141 142 143 144 145 146
    
    /* loop over global grid */
    for (i=1;i<=NXG;i++){
        for (j=1;j<=NYG;j++){
            
            if(WAVETYPE==1||WAVETYPE==3){
147 148
                
                if(feof(fp_vp)){
149
                    declare_error("Model file VP is to small. Check dimensions NX*NY of file.");
150
                }
151
                fread(&vp, sizeof(float), 1, fp_vp);
152
                
153
                if (sw_Qp){
154 155
                    
                    if(feof(fp_qp)){
156
                        declare_error("Model file QP is to small. Check dimensions NX*NY of file.");
157 158 159 160 161 162 163
                    }
                    
                    fread(&qp, sizeof(float), 1, fp_qp);
                }
            }
            
            if(feof(fp_vs)){
164
                declare_error("Model file VS is to small. Check dimensions NX*NY of file.");
165 166 167
            }
            
            fread(&vs, sizeof(float), 1, fp_vs);
168 169
            
            if(feof(fp_rho)){
170
                declare_error("Model file RHO is to small. Check dimensions NX*NY of file.");
171 172
            }
            
173
            fread(&rhov, sizeof(float), 1, fp_rho);
174
            
175
            if (sw_Qs){
176 177
                
                if(feof(fp_vs)){
178
                    declare_error("Model file QS is to small. Check dimensions NX*NY of file.");
179 180 181 182
                }
                
                fread(&qs, sizeof(float), 1, fp_qs);
            }
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
            
            /* 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 (sw_Qs){
                    taus[jj][ii]=2.0/qs;}
                else taus[jj][ii]=TAU;
                if(WAVETYPE==1||WAVETYPE==3){
                    pi[jj][ii]=vp;
                    if (sw_Qp){
                        taup[jj][ii]=2.0/qp;}
                    else taup[jj][ii]=TAU;
                }
                
                
                
            }
        }
    }
    
209

210 211
    
    if(WAVETYPE==1||WAVETYPE==3){
212 213 214
        
        fread(&vp, sizeof(float), 1, fp_vp);
        if(!feof(fp_vp)){
215
            declare_error("Model file VP is to big. Check dimensions NX*NY of file.");
216
        }
217
        fclose(fp_vp);
218 219 220 221
        
        if (sw_Qp) {
            fread(&qp, sizeof(float), 1, fp_qp);
            if(!feof(fp_qp)){
222
                declare_error("Model file QP is to big. Check dimensions NX*NY of file.");
223 224 225 226 227 228 229
            }
            fclose(fp_qp);
        }
    }
    
    fread(&vs, sizeof(float), 1, fp_vs);
    if(!feof(fp_vs)){
230
        declare_error("Model file VS is to big. Check dimensions NX*NY of file.");
231 232
    }
    fclose(fp_vs);
233 234 235
    
    fread(&rho, sizeof(float), 1, fp_rho);
    if(!feof(fp_rho)){
236
        declare_error("Model file RHO is to big. Check dimensions NX*NY of file.");
237
    }
238
    fclose(fp_rho);
239 240 241 242
    
    if (sw_Qs){
        fread(&qs, sizeof(float), 1, fp_qs);
        if(!feof(fp_qs)){
243
            declare_error("Model file QS is to big. Check dimensions NX*NY of file.");
244 245 246
        }
        fclose(fp_qs);
    }
247 248 249 250
    
    
    /* each PE writes his model to disk */
    if(WAVETYPE==1||WAVETYPE==3){
251 252
        if(PARAMETERIZATION==1) sprintf(filename,"%s.out.vp",MFILE);
        if(PARAMETERIZATION==3) sprintf(filename,"%s.out.pi",MFILE);
253 254 255 256
        write_matrix_disk(pi, filename);
        
        sprintf(filename,"%s.out.qp",MFILE);
        write_matrix_disk(taup, filename);
257 258
    }
    
259 260
    if(PARAMETERIZATION==1) sprintf(filename,"%s.out.vs",MFILE);
    if(PARAMETERIZATION==3) sprintf(filename,"%s.out.mu",MFILE);
261
    write_matrix_disk(u, filename);
262 263
    
    sprintf(filename,"%s.out.rho",MFILE);
264
    write_matrix_disk(rho, filename);
265
    
266 267
    sprintf(filename,"%s.out.qs",MFILE);
    write_matrix_disk(taus, filename);
268 269
    
    free_vector(pts,1,L);
Tilman Steinweg's avatar
Tilman Steinweg committed
270 271 272 273 274
}