receiver.c 6.34 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
-----------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 *  compute receiver positions or read them from external file        
 *  last update: 11.02.02
 *
 *  T. Bohlen
 *  See COPYING file for copying and redistribution conditions.
 *  ----------------------------------------------------------------------*/

#include "fd.h"

int **receiver(FILE *fp, int *ntr){

	/* declaration of extern variables */
	extern  char REC_FILE[STRING_SIZE];
	extern float DH, REFREC[4], REC_ARRAY_DEPTH, REC_ARRAY_DIST, FW;
	extern int READREC, NGEOPH, DRX, REC_ARRAY, NX;
	extern int MYID;

	int **recpos1, **recpos, nxrec=0, nyrec=0, nzrec=0;
	int   itr=1, itr1=0, itr2=0, recflag=0, c, ifw, n, i, j;
	int nxrec1, nxrec2, nyrec1, nyrec2, nzrec1=1, nzrec2=1;
	extern float XREC1, YREC1, XREC2, YREC2;
	float xrec, yrec;
	FILE *fpr;

	if (MYID==0)
	{
     	if (READREC){ /* 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");
tilman.metz's avatar
tilman.metz committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
     		if (fpr==NULL) err(" Receiver file could not be opened !");
     		*ntr=0;
     		while ((c=fgetc(fpr)) != EOF)
     			if (c=='\n') ++(*ntr);
     		rewind(fpr);
     
     		recpos1=imatrix(1,3,1,*ntr);
     		for (itr=1;itr<=*ntr;itr++){
     			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);
     		fprintf(fp," Message from function receiver (written by PE %d):\n",MYID);/***/
     		fprintf(fp," Number of receiver positions found: %i\n",*ntr);
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 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
     		/* check if more than one receiver is located
     				         at the same gridpoint */
     		for (itr=1;itr<=(*ntr-1);itr++)
     			for (itr1=itr+1;itr1<=*ntr;itr1++)
     				if ((recpos1[1][itr]==recpos1[1][itr1])
     				    && (recpos1[2][itr]==recpos1[2][itr1])
     				    && (recpos1[3][itr]==recpos1[3][itr1]))
     					recpos1[1][itr1]=-(++recflag);
     
     		recpos=imatrix(1,3,1,*ntr-recflag);
     		for (itr=1;itr<=*ntr;itr++)
     			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){
     			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");
     		}
     
     		free_imatrix(recpos1,1,3,1,*ntr);
  
     	}
     
     	else if (REC_ARRAY>0){
     		ifw=iround(FW/DH);  /* frame width in gridpoints */
     		*ntr=(1+(NX-2*ifw)/DRX)*REC_ARRAY;
     		recpos=imatrix(1,3,1,*ntr);
     		itr=0;
     		for (n=0;n<=REC_ARRAY-1;n++){
     			j=iround((REC_ARRAY_DEPTH+REC_ARRAY_DIST*(float)n)/DH);
     			for (i=ifw;i<=NX-ifw;i+=DRX){
     					itr++;
     					recpos[1][itr]=i;
     					recpos[2][itr]=j;
     			}
     		}
     	}
     	
     
     
     
     
     
     	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]);*/
     
    

	}


	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;
}