receiver.c 8.15 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.
tilman.metz's avatar
tilman.metz committed
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 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.metz's avatar
tilman.metz 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.metz's avatar
tilman.metz committed
17 18 19 20 21 22 23 24 25 26
-----------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 *  compute receiver positions or read them from external file        
 *
 *  See COPYING file for copying and redistribution conditions.
 *  ----------------------------------------------------------------------*/

#include "fd.h"

27
int **receiver(int *ntr, float **srcpos, int shotno){
tilman.metz's avatar
tilman.metz committed
28 29 30 31

	/* declaration of extern variables */
	extern  char REC_FILE[STRING_SIZE];
	extern float DH, REFREC[4], REC_ARRAY_DEPTH, REC_ARRAY_DIST, FW;
32 33 34
	extern int READREC, NGEOPH, DRX, REC_ARRAY, NXG, NYG;
	extern int MYID, VERBOSE,TRKILL;
	extern FILE *FP;
tilman.metz's avatar
tilman.metz committed
35

36
	int **recpos1, **recpos = NULL, nxrec=0, nyrec=0, nzrec=0, recdist;
37 38
	int   itr=1, itr1=0, itr2=0, recflag=0, c, ifw, n, i, j, ntr1=0;
	int nxrec1, nxrec2, nxrec3, nyrec1, nyrec2, nzrec1=1, nzrec2=1, offset, shotdist;
tilman.metz's avatar
tilman.metz committed
39 40
	extern float XREC1, YREC1, XREC2, YREC2;
	float xrec, yrec;
41 42
	FILE *fpr,*f;
	char filename[STRING_SIZE];
tilman.metz's avatar
tilman.metz committed
43 44 45

	if (MYID==0)
	{
46 47
     	if (READREC==1){ /* read receiver positions from file */
     		fprintf(FP,"\n Reading receiver positions from file: \n\t%s\n",REC_FILE);
48
		    fpr=fopen(REC_FILE,"r");
49
     		if (fpr==NULL) declare_error(" Receiver file could not be opened !");
tilman.metz's avatar
tilman.metz committed
50 51
     		*ntr=0;
     		while ((c=fgetc(fpr)) != EOF)
52
     			if (c=='\n') ++(ntr1);
tilman.metz's avatar
tilman.metz committed
53 54
     		rewind(fpr);
     
55 56
     		recpos1=imatrix(1,3,1,ntr1);
     		for (itr=1;itr<=ntr1;itr++){
tilman.metz's avatar
tilman.metz committed
57 58 59 60 61 62
     			fscanf(fpr,"%f%f\n",&xrec, &yrec);
     			recpos1[1][itr]=iround((xrec+REFREC[1])/DH);
     			recpos1[2][itr]=iround((yrec+REFREC[2])/DH);
     			recpos1[3][itr]=iround((0.0+REFREC[3])/DH);
     		}
     		fclose(fpr);
63 64
     		fprintf(FP," Message from function receiver (written by PE %d):\n",MYID);/***/
     		fprintf(FP," Number of receiver positions found: %i\n",ntr1);
65 66 67 68 69 70
            
            /* recpos1[1][itr] X in m
             * recpos1[2][itr] Y in m
             *
             */
            
tilman.metz's avatar
tilman.metz committed
71 72
     		/* check if more than one receiver is located
     				         at the same gridpoint */
73 74
     		for (itr=1;itr<=(ntr1-1);itr++)
     			for (itr1=itr+1;itr1<=ntr1;itr1++)
tilman.metz's avatar
tilman.metz committed
75 76 77 78 79
     				if ((recpos1[1][itr]==recpos1[1][itr1])
     				    && (recpos1[2][itr]==recpos1[2][itr1])
     				    && (recpos1[3][itr]==recpos1[3][itr1]))
     					recpos1[1][itr1]=-(++recflag);
     
80 81
     		recpos=imatrix(1,3,1,ntr1-recflag);
     		for (itr=1;itr<=ntr1;itr++)
tilman.metz's avatar
tilman.metz committed
82 83 84 85 86 87 88 89
     			if (recpos1[1][itr]>0){
     				recpos[1][++itr2]=recpos1[1][itr];
     				recpos[2][itr2]=recpos1[2][itr];
     				recpos[3][itr2]=recpos1[3][itr];
     			}
     
     		*ntr=itr2;
     		if (recflag>0){
90 91 92 93 94
     			fprintf(FP,"\n\n");
     			fprintf(FP," Warning:\n");
     			fprintf(FP," Several receivers located at the same gridpoint !\n");
     			fprintf(FP," Number of receivers reduced to %i\n", *ntr);
     			fprintf(FP,"\n\n");
tilman.metz's avatar
tilman.metz committed
95 96
     		}
     
97
     		free_imatrix(recpos1,1,3,1,ntr1);
tilman.metz's avatar
tilman.metz committed
98 99 100
  
     	}
     
101 102
     	else if (READREC==2){
		fprintf(FP,"\n Moving streamer acquisition geometry choosen. \n");
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		/* decide if XREC1 or XREC2 is the nearest receiver to shot 1 */
		if (abs(XREC1-srcpos[1][1])<abs(XREC2-srcpos[1][1])){
			nxrec1=iround(XREC1/DH);   /* (nxrec1,nyrec1) and (nxrec2,nyrec2) are */
			nyrec1=iround(YREC1/DH);   /* the positions of the first and last receiver*/
			nxrec2=iround(XREC2/DH);   /* in gridpoints */
			nyrec2=iround(YREC2/DH);
			offset=iround((XREC1-srcpos[1][1])/DH);
		}
		else {
			nxrec1=iround(XREC2/DH);   /* (nxrec1,nyrec1) and (nxrec2,nyrec2) are */
			nyrec1=iround(YREC2/DH);   /* the positions of the first and last receiver*/
			nxrec2=iround(XREC1/DH);   /* in gridpoints */
			nyrec2=iround(YREC1/DH);
			offset=iround((XREC2-srcpos[1][1])/DH);
		}
118 119 120 121 122 123 124
		if (nyrec1 != nyrec2){
			fprintf(FP,"\n\n");
			fprintf(FP," Warning:\n");
			fprintf(FP," Receiver positions are not in the same depth !\n");
			fprintf(FP," Depth of all receivers is seth to %i m\n",YREC1);
			fprintf(FP,"\n\n");
		}
125 126 127
		if (offset<0) recdist=-NGEOPH;
		else recdist=NGEOPH;
		*ntr=iround((nxrec2-nxrec1)/recdist)+1;
128
		if (shotno>0) nxrec1=nxrec1+((srcpos[1][shotno]-srcpos[1][1])/DH);
129 130 131
		recpos=imatrix(1,3,1,*ntr);
		n=0;
		
132 133 134 135 136 137
		for (itr=1;itr<=*ntr;itr++){
			nxrec=nxrec1+(itr-1)*recdist;
			if (nxrec>FW && nxrec<(NXG-FW) && nyrec1>0 && nyrec1<(NYG-FW)){
				recpos[1][itr]=nxrec;
				recpos[2][itr]=nyrec1;
			} else declare_error(" Receiver positions of current shot are out of model boundaries !");
138
		}
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		if (shotno>0){
			/* write receiver positions to file */
			sprintf(filename,"%s.shot%i",REC_FILE,shotno);
			f=fopen(filename,"wb");
			for (i=1;i<=*ntr;i++) {
				fprintf(f,"%f \t %f \n",recpos[1][i]*DH,recpos[2][i]*DH);
			}
			fclose(f);
		}
			
		if (VERBOSE==1) {
		  fprintf(FP," Message from function receiver (written by PE %d):\n",MYID);
		  fprintf(FP," Number of receiver positions found: %i\n",*ntr);
		  fprintf(FP," First receiver position for shot %i: %f (x), %f (y)\n",shotno,recpos[1][1]*DH,recpos[2][1]*DH);
		  fprintf(FP," Last receiver position for shot %i: %f (x), %f (y)\n",shotno,recpos[1][*ntr]*DH,recpos[2][*ntr]*DH);  
		}
tilman.metz's avatar
tilman.metz committed
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
     	}
     
     	else{         /* straight horizontal or vertical
     		                     line of receivers */
			nxrec1=iround(XREC1/DH);   /* (nxrec1,nyrec1) and (nxrec2,nyrec2) are */
			nyrec1=iround(YREC1/DH);   /* the positions of the first and last receiver*/
			nxrec2=iround(XREC2/DH);	 /* in gridpoints */
			nyrec2=iround(YREC2/DH);

     		if ((abs(nyrec2-nyrec1)<=abs(nxrec2-nxrec1))||
     		    (abs(nyrec2-nyrec1)<=abs(nzrec2-nzrec1))){
     			if (abs(nzrec2-nzrec1)<=abs(nxrec2-nxrec1)){
     				/* geophone-array horizontal x-dirextion */
     				*ntr=iround((nxrec2-nxrec1)/NGEOPH)+1;
     				recpos=imatrix(1,3,1,*ntr);
     				for (nxrec=nxrec1;nxrec<=nxrec2;nxrec+=NGEOPH){
     					nyrec=nyrec1+((nyrec2-nyrec1)/(nxrec2-nxrec1)*(nxrec-nxrec1));
     					nzrec=nzrec1+((nzrec2-nzrec1)/(nxrec2-nxrec1)*(nxrec-nxrec1));
     					itr=iround((nxrec-nxrec1)/NGEOPH)+1;
     					recpos[1][itr]=nxrec;
     					recpos[2][itr]=nyrec;
     					recpos[3][itr]=nzrec;
     				}
     			}

     		}
     		else{         /* receiver-line vertical */
     			*ntr=iround((nyrec2-nyrec1)/NGEOPH)+1;
     			recpos=imatrix(1,3,1,*ntr);
     			for (nyrec=nyrec1;nyrec<=nyrec2;nyrec+=NGEOPH){
     				nxrec=nxrec1+((nxrec2-nxrec1)/(nyrec2-nyrec1)*(nyrec-nyrec1));
     				/**wird nzrec noch gebraucht?**/
     				nzrec=nzrec1+((nzrec2-nzrec1)/(nyrec2-nyrec1)*(nyrec-nyrec1));
     				itr=iround((nyrec-nyrec1)/NGEOPH)+1;
     				recpos[1][itr]=nxrec;
     				recpos[2][itr]=nyrec;
     				recpos[3][itr]=nzrec;
     			}
     		}
     
     	}
     	/*   fprintf(fp,"Gridpoints of receiver positions (x,y,z):\n");
     		for (itr=1;itr<=*ntr;itr++)
     				fprintf(fp,"%i\t%i\t%i\n",recpos[1][itr],recpos[2][itr],recpos[3][itr]);*/
     
    

	}

205
      
tilman.metz's avatar
tilman.metz committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	MPI_Barrier(MPI_COMM_WORLD);
	MPI_Bcast(ntr,1,MPI_INT,0,MPI_COMM_WORLD);
	if (MYID!=0) recpos=imatrix(1,3,1,*ntr);
	MPI_Bcast(&recpos[1][1],(*ntr)*3,MPI_INT,0,MPI_COMM_WORLD);

/*	if (MYID==0)
	{
		fprintf(fp,"\n **Message from function receiver (written by PE %d):\n",MYID);
		fprintf(fp," Number of receiver positions found: %i\n",*ntr);
		fprintf(fp," Receiver positions (in gridpoints) in the global model-system:\n");
		fprintf(fp," x  \ty \n");
		fprintf(fp," -  \t- \n");
		for (l=1;l<=*ntr;l++)
			fprintf(fp," %i\t%i\n",recpos[1][l],recpos[2][l]);
		fprintf(fp,"\n\n");
	}
*/

	return recpos;
}