spat_filt.c 4.43 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.
Tilman Steinweg's avatar
Tilman Steinweg committed
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 9
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
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 14 15
 * 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
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>.
Tilman Steinweg's avatar
Tilman Steinweg committed
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
-----------------------------------------------------------------------------------------*/

/*
 * taper gradient with a gaussian frame to damp inversion artefacts near the sources and receivers
 sws == 1 vertical taper (for tomography geometry)
 sws == 2 horizontal taper (for reflection geometry)
 sws == 3 local radial taper at the source and receiver positions
 sws == 4  
 */

#include "fd.h"

void spat_filt(float ** waveconv, int iter, int sws)
{

	/* extern variables */

        extern float DH;
	extern int FREE_SURF, NX, NY, NXG, NYG;
	extern int NPROCX, NPROCY, MYID, POS[3];
	extern char JACOBIAN[STRING_SIZE];
	extern FILE *FP;
	
	/* local variables */
	int i, j, h, ifw, ii, jj, n, xb, yb, xe, ye, taperlength,taperlength2;
	int ijc, iy, ix, iii, jjj, xx, yy, srctaper_gridpt, i1, j1, filtsize;

	float **waveconvtmp, **waveconvtmps, grad, normgauss;
	char jac[STRING_SIZE];
	FILE *fp_grad;
	
	extern int SPAT_FILT_SIZE, SPAT_FILT_1, SPAT_FILT_ITER;
	/*SPAT_FILT_SIZE=23;
	SPAT_FILT_1=45;*/
	
	if (SPAT_FILT_ITER){   /* filter length is reduced in each iteration */
		SPAT_FILT_SIZE=SPAT_FILT_SIZE-iter+1;}
	
	/*if(SPAT_FILT_SIZE>=iter){*/ /* application condition for the spatial filter */
	if(SPAT_FILT_SIZE>=1){
	
        waveconvtmp = matrix(1,NYG,1,NXG);
	waveconvtmps = matrix(1,NYG,1,NXG);
	
	if(sws==1){
	sprintf(jac,"%s_g.old",JACOBIAN);}   /*sprintf(jac,"%s_g.it%i",JACOBIAN,iter);}*/

	if(sws==2){
	sprintf(jac,"%s_g_u.old",JACOBIAN);}    /*sprintf(jac,"%s_g_u.it%i",JACOBIAN,iter);}*/
	
	if(sws==3){
	sprintf(jac,"%s_g_rho.old",JACOBIAN);}    /*sprintf(jac,"%s_g_rho.it%i",JACOBIAN,iter);}*/
	
	MPI_Barrier(MPI_COMM_WORLD);
	
	if(MYID==0){
	fp_grad=fopen(jac,"rb");
74
	if (fp_grad==NULL) declare_error(" Could not open gradient file ! ");
Tilman Steinweg's avatar
Tilman Steinweg committed
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
	
	/* load merged gradient */
	for (i=1;i<=NXG;i++){
	   for (j=1;j<=NYG;j++){
                        fread(&grad, sizeof(float), 1, fp_grad);
			waveconvtmp[j][i]=grad;
			waveconvtmps[j][i]=grad;
		}
	}
	
	fclose(fp_grad);
	
	/* apply spatial Gaussian filter on gradient */
	for (i=1+SPAT_FILT_SIZE;i<=NXG-SPAT_FILT_SIZE;i++){
	   for (j=SPAT_FILT_1;j<=NYG-SPAT_FILT_SIZE;j++){

                     waveconvtmps[j][i]=0.0;   
		     normgauss=0.0;
		     
		     for (i1=i-SPAT_FILT_SIZE;i1<=i+SPAT_FILT_SIZE;i1++){
	                 /*for (j1=j+SPAT_FILT_SIZE;j1<=j+SPAT_FILT_SIZE;j1++){*/
			 
                         waveconvtmps[j][i] += waveconvtmp[j][i1] * (1/(sqrt(2*PI)*SPAT_FILT_SIZE)) * exp((-1/2)*((sqrt(((i-i1)*(i-i1))+((i-i1)*(i-i1))))*(sqrt(((i-i1)*(i-i1))+((i-i1)*(i-i1)))))/(SPAT_FILT_SIZE*SPAT_FILT_SIZE)); 
			 normgauss += (1/(sqrt(2*PI)*SPAT_FILT_SIZE)) * exp((-1/2)*((sqrt(((i-i1)*(i-i1))+((i-i1)*(i-i1))))*(sqrt(((i-i1)*(i-i1))+((i-i1)*(i-i1)))))/(SPAT_FILT_SIZE*SPAT_FILT_SIZE)); 
			 
			 /*}*/
		     }
		     
		     waveconvtmps[j][i]=waveconvtmps[j][i]/normgauss;		  
			
	   }
	}
	
        fp_grad=fopen(jac,"wb");

        /* output of the preconditioned gradient */
        for (i=1;i<=NXG;i++){
           for (j=1;j<=NYG;j++){
            
           fwrite(&waveconvtmps[j][i],sizeof(float),1,fp_grad);

           }
        }

        fclose(fp_grad);
	
	}
	
	MPI_Barrier(MPI_COMM_WORLD);
	
	fp_grad=fopen(jac,"rb");
	
	/* distribute spatial filtered gradient on computational nodes */
	for (i=1;i<=NXG;i++){
	   for (j=1;j<=NYG;j++){
	   
                        fread(&grad, sizeof(float), 1, fp_grad);
			   			
			if ((POS[1]==((i-1)/NX)) && 
		   	 (POS[2]==((j-1)/NY))){
				ii=i-POS[1]*NX;
				jj=j-POS[2]*NY;

				waveconv[jj][ii]=grad;

			}
		}
	}
		
	fclose(fp_grad);

	free_matrix(waveconvtmp,1,NXG,1,NYG);
	free_matrix(waveconvtmps,1,NXG,1,NYG);

	
	} /* end of application condition for the spatial filter */


	
	
}