-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvfsacrsnh_lib.c
More file actions
368 lines (288 loc) · 10.5 KB
/
vfsacrsnh_lib.c
File metadata and controls
368 lines (288 loc) · 10.5 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
vfsacrsnh_lib.c (c)
Purpose: 'Mvfsacrsnh.c' library.
Site: https://www.geofisicando.com
Programmer: Rodolfo A. C. Neves (Dirack) 13/08/2021
Email: rodolfo_profissional@hotmail.com
License: GPL-3.0 <https://www.gnu.org/licenses/gpl-3.0.txt>.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <rsf.h>
/*^*/
#define signal(s) ((s<0)?(-1.):(1.))
/*< Signal function >*/
/*^*/
void generatePreciseRandomSeed()
/*< Generate a more precise random seed >*/
{
struct timeval start;
gettimeofday(&start,NULL);
srand((unsigned)start.tv_usec);
}
float getRandomNumberBetween0and1(){
/*< Function to get a random number between 0 and 1 >*/
return (float)(rand()%1000)/1000;
}
float getVfsaIterationTemperature( int iteration /* Number of the current iteration */,
float dampingFactor /* C0, the VFSA damping factor */,
float initialTemperature /* T0, the VFSA Initial temperature */)
/*< Temperature function for VFSA algorithm >*/
{
return initialTemperature*expf(-dampingFactor*pow(iteration,0.25));
}
void disturbParameters( float temperature /* Temperature of the current iteration */,
float* disturbedParameter /* disturbed parameters vector */,
float* parameter /* parameters vector */,
float rn_max,
float rn_min,
float rnip_max,
float rnip_min,
float beta_max,
float beta_min)
/*< Disturb parameters from the previous VFSA iteration
Note: It receives a parameter vector and distubs it accordingly to
VFSA algorithm disturb parameters step.
>*/
{
float u;
float disturbance;
float rn_aperture=rn_max-rn_min;
float rnip_aperture=rnip_max-rnip_min;
float beta_aperture=beta_max-beta_min;
u=getRandomNumberBetween0and1();
disturbance = signal(u - 0.5) * temperature * (pow( (1+temperature),fabs(2*u-1) )-1);
/* Disturb RN */
disturbedParameter[0] = parameter[0] + disturbance * (rn_aperture);
if (disturbedParameter[0] >= rn_max || disturbedParameter[0] <= rn_min) {
disturbedParameter[0] = (rn_aperture) * getRandomNumberBetween0and1() + rn_min;
}
/* Disturb RNIP */
disturbedParameter[1] = parameter[1] + disturbance * (rnip_aperture);
if (disturbedParameter[1] >= (rnip_max) || disturbedParameter[1] <= (rnip_min)) {
disturbedParameter[1] = (rnip_aperture) * getRandomNumberBetween0and1() + rnip_min;
}
if(disturbedParameter[1]<rnip_min) sf_error("rnip < rnip_min, %f < %f",disturbedParameter[1],rnip_min);
/* Disturb BETA */
disturbedParameter[2] = parameter[2] + (disturbance/10.) * (beta_aperture);
if (disturbedParameter[2] >= beta_max || disturbedParameter[2] <= beta_min) {
disturbedParameter[2] = (beta_aperture) * getRandomNumberBetween0and1() + beta_min;
}
}
void nonHyperbolicCRSapp( float **t /* non-hyperbolic CRS traveltime surface */,
int mMAX /* CMP aperture */,
int hMAX /* Offset aperture */,
float m0 /* Central CMP of the approximation */,
float dm, /* CMP sampling */
float om /* CMP axis origin */,
float dh /* half-offset sampling */,
float oh /* half-offset axis origin */,
float t0 /* Normal ray traveltime */,
float v0 /* Near surface velocity */,
float RN /* RN, CRS parameter */,
float RNIP /* RNIP, CRS parameter */,
float BETA /* BETA, CRS parameter */,
bool half /* Half-Offset coordinates */)
/*< Returns the Non hyperbolic CRS approximation surface (FOMEL; KAZINNIK, 2013) >*/
{
float m0_index=(int)(m0/dm);
float a1, a2, b2, c1, Fd, Fd1, Fd2;
int im, ih;
float m, h, mmh, mph;
float sinB=sin(BETA),cosB=cos(BETA);
om = om+(m0_index-mMAX)*dm;
a1=(2*sinB)/(v0);
a2=(2*cosB*cosB*t0)/(v0*RN);
b2=(2*cosB*cosB*t0)/(v0*RNIP);
c1=2*b2+a1*a1-a2;
for (im=0; im < 2*mMAX+1; im++){
m=(im*dm+om)-m0;
for(ih=0;ih<hMAX;ih++){
h=ih*dh+oh;
if(!half) h/=2;
mmh=m-h;
mph=m+h;
Fd=(t0+a1*m)*(t0+a1*m)+a2*m*m;
Fd2=(t0+a1*(mmh))*(t0+a1*(mmh))+a2*(mmh)*(mmh);
Fd1=(t0+a1*(mph))*(t0+a1*(mph))+a2*(mph)*(mph);
t[im][ih]=sqrt((Fd+c1*h*h+sqrt(Fd2*Fd1))*0.5);
}
}
}
float semblance(float m0 /* Central CMP of the approximation */,
float dm /* CMP sampling */,
float om /* CMP axis origin */,
float oh /* half-offset axis origin */,
float dh /* half-offset axis sampling */,
float dt /* time sampling */,
int nt /* number of time samples */,
float t0 /* Normal ray traveltime */,
float v0 /* Near surface velocity */,
float RN /* RN, CRS parameter */,
float RNIP /* RNIP, CRS parameter */,
float BETA /* BETA, CRS parameter */,
float*** t /* reflection data cube A(m,h,t) */,
int mMAX, /* CMP aperture */
int hMAX, /* Offset aperture */
bool half /* Half-offset coordinates */)
/*< Calculate semblance between the Non Hyperbolic CRS approximation surface and reflection data >*/
{
int im, ih, numSamples=0;
float amplitude=0.;
float amplitudeSampleSum=0.;
float amplitudeSquaredSampleSum=0.;
float semblance=0;
int tetai;
float **teta;
int m0_index_init, m0_index_end;
teta = sf_floatalloc2(hMAX,2*mMAX+1);
m0_index_init = (int)(m0/dm)-mMAX;
m0_index_end = (int)(m0/dm)+mMAX;
nonHyperbolicCRSapp(teta,mMAX,hMAX,m0,dm,om,dh,oh,t0,v0,RN,RNIP,BETA,half);
for (im=m0_index_init; im < m0_index_end; im++){
for(ih=0;ih<hMAX;ih++){
tetai=roundf(teta[im-m0_index_init][ih]/dt);
if(tetai>=0 && tetai < nt){
amplitude = t[im][ih][tetai];
amplitudeSampleSum += amplitude;
amplitudeSquaredSampleSum += (amplitude*amplitude);
}
numSamples++;
}
}
if(amplitudeSampleSum==0)
return semblance=0;
else
return semblance=(amplitudeSampleSum*amplitudeSampleSum)/(numSamples*amplitudeSquaredSampleSum);
}
bool checkValueInsideBoundaries(float value, float min, float max)
/*< check if the value is inside the boundaries min < value < max >*/
{
return (min < value && value < max);
}
bool validBoundariesAndApertureForCMP(int nm0, float om0, float dm0, int mMAX, int nm, float om, float dm, char *strerr)
/*< Check if CMP coordinates for m0s are inside data cube space to avoid segmentation fault errors >*/
{
float m0min, m0max;
m0min = om0-(dm*mMAX);
m0max = om0+dm0*(nm0-1)+(dm*mMAX);
if(m0min < om || m0max > (om+dm*(nm-1))){
sprintf(strerr,"Invalid boundaries for CMP m0min=%f m0max=%f. Please check aperture!",m0min,m0max);
return false;
}
return true;
}
bool validBoundariesAndApertureForOffset(int nh, float oh, float dh, int hMAX, char *strerr)
/*< Check if Offset coordinates are inside data cube space to avoid segmentation fault errors >*/
{
if((oh+dh*hMAX) > (oh+dh*(nh-1))){
sprintf(strerr,"Invalid boundaries for Offset hmin=%f hmax=%f. Please check aperture!",oh,oh+dh*hMAX);
return false;
}
return true;
}
bool validBoundariesAndApertureForT0(int nt0, float ot0, float dt0, int nt, float ot, float dt, char *strerr)
/*< Check if t0 coordinates are inside data cube space to avoid segmentation fault errors >*/
{
float t0min = ot0;
float t0max = ot0+dt0*(nt0-1);
if(t0min < ot || t0max > ot+dt*(nt-1)){
sprintf(strerr,"Invalid boundaries for time t0 t0min=%f t0max=%f. Please check aperture!",t0min,t0max);
return false;
}
return true;
}
bool repeatOptionEqual1ForGetConvergenceGraphTrue(bool get_convergence_graph, int repeat)
/*< Repeat option should be equal one when get_convergence_graph flag is on to avoid multiple thread >*/
{
return repeat==1 && get_convergence_graph;
}
void prepareConvergenceGraphFile(sf_file outgraph, bool get_convergence_graph, int repeat, int itmax)
/*< Check parameters (repeat should be equal to 1 when generating a convergence graph) and Prepare the convergence graph file >*/
{
if(!repeatOptionEqual1ForGetConvergenceGraphTrue(get_convergence_graph,repeat))
sf_error("The repeat parameter should be equal 1 for getgraph=y!");
sf_putint(outgraph,"n1",itmax);
sf_putfloat(outgraph,"d1",1);
sf_putfloat(outgraph,"o1",0);
sf_putstring(outgraph,"title","Convergence graph");
sf_putstring(outgraph,"label1","Iteration");
sf_putstring(outgraph,"label2","Semblance");
}
bool checkParametersFileDimensionReturnStrError(sf_file parametersFile,int nt0, int nm0, char* strerr)
/*< check n1 and n2 dimension and n1 should be equal to nt0 and n2 should be equal to nm0 in parameters files >*/
{
int n1, n2;
if(!sf_histint(parametersFile,"n1",&n1)){
strcpy(strerr,"No n1= in file");
}else if(!sf_histint(parametersFile,"n2",&n2)){
strcpy(strerr,"No n2= in file");
}else if(n1!=nt0){
strcpy(strerr,"n1 should be equal to nt0 in file");
}else if(n2!=nm0){
strcpy(strerr,"n2 should be equal to nm0 in file");
}else{
return true;
}
return false;
}
bool checkAndLoadDataCubeDimensionsReturnStrError(sf_file in,
int *n1, float *o1, float *d1,
int *n2, float *o2, float *d2,
int *n3, float *o3, float *d3,
char *strerr)
/*< Check each dimension for a data cube and load it >*/
{
if(!sf_histint(in,"n1",n1)){
strcpy(strerr,"No n1= in input");
}else if (!sf_histfloat(in,"o1",o1)){
strcpy(strerr,"No o1= in input");
}else if (!sf_histfloat(in,"d1",d1)){
strcpy(strerr,"No d1= in input");
}else if (!sf_histint(in,"n2",n2)){
strcpy(strerr,"No n2= in input");
}else if (!sf_histfloat(in,"o2",o2)){
strcpy(strerr,"No o2= in input");
}else if (!sf_histfloat(in,"d2",d2)){
strcpy(strerr,"No d2= in input");
}else if (!sf_histint(in,"n3",n3)){
strcpy(strerr,"No n3= in input");
}else if (!sf_histfloat(in,"o3",o3)){
strcpy(strerr,"No o3= in input");
}else if (!sf_histfloat(in,"d3",d3)){
strcpy(strerr,"No d3= in input");
}else{
return true;
}
return false;
}
void loadParametersFilesVectors(float** parametersFilesVectors[6],char* labels[6], int nt0, int nm0)
/*< Load parameters file vector >*/
{
int q;
sf_file parametersFiles;
char strerr[50];
for(q=0;q<6;q++){
parametersFiles = sf_input(labels[q]);
if(!checkParametersFileDimensionReturnStrError(parametersFiles,nt0,nm0,strerr))
sf_error("%s %s",strerr,labels[q]);
parametersFilesVectors[q] = sf_floatalloc2(nt0,nm0);
sf_floatread(parametersFilesVectors[q][0],nt0*nm0,parametersFiles);
sf_fileclose(parametersFiles);
}
}
void prepareMTConvergenceGraphFile(sf_file outgraph, bool get_mt_convergence_graph, int repeat, int itmax)
/*< Check parameters (repeat should be equal to 1 when generating a convergence graph) and Prepare the convergence graph file >*/
{
sf_putint(outgraph,"n1",itmax);
sf_putfloat(outgraph,"d1",1);
sf_putfloat(outgraph,"o1",0);
sf_putint(outgraph,"n2",repeat);
sf_putfloat(outgraph,"o2",0.);
sf_putfloat(outgraph,"d2",1.);
sf_putstring(outgraph,"label1","Iteration");
sf_putstring(outgraph,"label2","Semblance");
}