-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomography.c
303 lines (251 loc) · 7.43 KB
/
tomography.c
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
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <rsf.h>
#include "raytrace.h"
#include "tomography.h"
#ifndef GDB_DEBUG
#define DSLOW 0.04
#define DANGLE 1.0
#else
#define DSSLOW 0.04
#define DANGLE 1.0
#endif
/*^*/
void calculateSplineCoeficients(int n, /* Vectors (x,y) dimension */
float* x, /* x coordinates */
float* y, /* y coordinates */
float* coef /* Spline coeficients */)
/*< Function to calculate natural cubic spline coeficients
Note: It Receives n points and two vectors x and y with n dimension.
It returns a coeficients vector with 4 coeficients for each of the
n-1 natural cubic splines, coef[n-1)*4].
IMPORTANT: The number of points must be equal or major than 3 (n>3)
and x vector must be in crescent order.
>*/
{
float s2[n]; // Second derivatives matrix
int i, ip1, ip2, im1, m; // Loop counter
float hb, ha, deltaa, deltab, t; // temporary variables
float e[n-2]; // hi's vector
float dp[n-2]; // main diagonal
/* Vectors dimension must be major than 3 */
if(n<3){
fprintf(stderr,"Erro, n<3\n");
exit(-1);
}
/* x vector must be in crescent order */
for(i=1;i<n;i++){
if(x[i-1]>x[i]){
fprintf(stderr,"Erro, vetor x deve possuir ordem crescente\n");
exit(-2);
}
}
/* Simetric tridiagonal linear system build */
ha = x[1]-x[0]; deltaa = (y[1]-y[0])/ha; m=n-2;
for(i=0;i<m;i++){
ip1 = i+1; ip2 = i+2;
hb = x[ip2]-x[ip1];
deltab = (y[ip2]-y[ip1])/hb;
e[i] = hb; dp[i] = 2*(ha+hb);
s2[ip1] = 6*(deltab-deltaa);
ha=hb; deltaa=deltab;
}
/* Gauss elimination */
for(i=1;i<m;i++){
ip1=i+1; im1=i-1;
t = e[im1]/dp[im1];
dp[i] = dp[i]-t*e[im1];
s2[ip1] = s2[ip1]-t*s2[i];
}
/* Retroactive substitutive solution */
s2[m]=s2[m]/dp[m-1];
for(i=m-1;i>0;i--){
ip1=i+1; im1=i-1;
s2[i]=(s2[i]-e[im1]*s2[ip1])/dp[im1];
}
s2[0]=0; s2[n-1]=0;
/* Calculate spline coeficients */
for(i=0;i<n-1;i++){
ha = x[i+1]-x[i];
coef[0+i*4] = (s2[i+1]-s2[i])/(6*ha);
coef[1+i*4] = s2[i]/2;
coef[2+i*4] = (y[i+1]-y[i])/ha-(s2[i+1]+2*s2[i])*(ha/6);
coef[3+i*4] = y[i];
}
}
void updateSplineCubicVelModel( float* slow, /* Slowness vector */
int* n, /* n[0]=n1 n2=n[1] */
float* o, /* o[0]=o1 o[1]=o2 */
float* d, /* d[0]=d1 d[1]=d2 */
int dim, /* Dimension of (z,vz) vectors */
float* sz, /* Spline not Depth coordinates */
float* sv /* Spline not Velocity coordinates */)
/*< Funcion to update spline cubic velocity model:
Note:
Make a velocity varying with depth model using the spline cubic interpolation
for a set of points (z,vz) given. TODO
>*/
{
int i, j=0, ic;
float z=0.0;
float coef[4*(dim-1)];
float v[n[0]];
//coef = (float*) malloc(4*(dim-1)*sizeof(float));
//v = (float*) malloc(n[0]*sizeof(float));
/* Calculate spline coeficients */
calculateSplineCoeficients(dim,sz,sv,coef);
/* Calculate velocity function */
for(i=1;i<dim;i++){
ic = (i-1)*4;
while(z<=sz[i]){
z = (j*d[0]+o[0])-sz[i-1];
if(j>=n[0]) break;
v[j] = coef[0+ic]*z*z*z+coef[1+ic]*z*z+coef[2+ic]*z+coef[3+ic];
j++;
}
}
//free(coef);
/* Update slowness model */
for(i=0;i<n[0];i++){
for(j=0;j<n[1];j++){
slow[j*n[0]+i]=1./(v[i]*v[i]);
} /* Loop over distance */
} /* Loop over depth */
//free(coef);
//free(v);
}
void updatevelmodel(float* slow, /* Slowness vector */
int* n, /* n[0]=n1 n2=n[1] */
float* o, /* o[0]=o1 o[1]=o2 */
float* d, /* d[0]=d1 d[1]=d2 */
float v0, /* First velocity is the near surface velocity */
float grad /* Velocity gradient */)
/*< Funcion to update constant velocity gradient:
Note:
This is a scratch of the function to update the velocity model,
it uses a constant gradient velocity model, and update the
gradient in each iteration of the process.
The purpose is to show that NIP sources will converge to the
reflector interface with the "right" gradient used.
TODO: Modify this function to VFSA optimization of the velocity model.
>*/
{
int i, j;
float z, v;
for(i=0;i<n[0];i++){
z = i*d[0]+o[0];
v = grad*z+v0;
for(j=0;j<n[1];j++){
slow[j*n[0]+i]=1./(v*v);
} /* Loop over distance */
} /* Loop over depth */
}
float creTimeApproximation(float h,
float m,
float v0,
float t0,
float m0,
float RNIP,
float BETA,
bool cds){
/*< CRE traveltime approximation >*/
float alpha;
float d = m-m0;
float c1;
float c2;
float a1, a2, b2, b1, Fd, Fd1, Fd2;
float t;
if(!cds){
c1 = (d+h)/RNIP;
c2 = (d-h)/RNIP;
alpha = sin(BETA)/RNIP;
t = (t0-2*RNIP/v0)+(RNIP/v0)*sqrt(1-2*alpha*(d+h)+c1*c1)+(RNIP/v0)*sqrt(1-2*alpha*(d-h)+c2*c2);
}else{
a1=(2*sin(BETA))/(v0);
a2=(2*cos(BETA)*cos(BETA)*t0)/(v0*RNIP);
b2=(2*cos(BETA)*cos(BETA)*t0)/(v0*RNIP);
b1=2*b2+a1*a1-a2;
Fd=(t0+a1*d)*(t0+a1*d)+a2*d*d;
Fd2=(t0+a1*(d-h))*(t0+a1*(d-h))+a2*(d-h)*(d-h);
Fd1=(t0+a1*(d+h))*(t0+a1*(d+h))+a2*(d+h)*(d+h);
t=sqrt((Fd+b1*h*h+sqrt(Fd2*Fd1))*0.5);
}
return t;
}
float calculateTimeMissfit(float** s, /* NIP sources matrix (z,x) pairs */
float v0, /* Near surface velocity */
float* t0, /* Normal ray traveltime for each NIP source */
float* m0, /* Central CMP for each NIP source */
float* RNIP, /* RNIP parameter for each NIP source */
float* BETA, /* BETA parameter for each NIP source */
int *n, /* Velocity model dimension n1=n[0] n2=n[1] */
float *o, /* Velocity model axis origin o1=o[0] o2=o[1] */
float *d, /* Velocity model sampling d1=d[0] d2=d[1] */
float *slow, /* Slowness velociy model */
float *a, /* Normal ray angle for each NIP source */
int ns /* Number of NIP sources */)
/*< Return time missfit sum of source-NIP-receiver rays
Note: This function traces nr reflection rays pairs from each NIP source
position passed though s matrix. It also calculate the difference between
the traveltime of the traced rays with calculated traveltime using CRE
traveltime approximation to calculate the time misfit returned by the function.
>*/
{
float currentRayAngle;
int i, ir, it, is;
float p[2], t, nrdeg;
int nt=5000, nr=5; //TODO to correct nr
float dt=0.001;
raytrace rt;
float** traj; // Ray trajectory (z,x)
float m, h, tmis=0;
float xs, xr, tr, ts, *x;
x = sf_floatalloc(2);
for(is=0;is<ns;is++){
x[0]=s[is][0];
x[1]=s[is][1];
nrdeg = a[is]; // TODO verify is it in degree?
for(ir=0;ir<nr;ir++){
for(i=0; i<2; i++){
/* initialize ray tracing object */
rt = raytrace_init(2,true,nt,dt,n,o,d,slow,ORDER);
/* Ray tracing */
traj = sf_floatalloc2(2,nt+1);
/* initialize ray direction */
currentRayAngle=(i==0)?(nrdeg-(ir+1)*DANGLE)*DEG2RAD:(nrdeg+(ir+1)*DANGLE)*DEG2RAD;
p[0] = -cosf(currentRayAngle);
p[1] = sinf(currentRayAngle);
it = trace_ray (rt, x, p, traj);
if(it>0){
t = it*dt;
if(i==0){
ts=t;
xs=x[1];
}else{
tr=t;
xr=x[1];
}
}else if(it == 0){
t = abs(nt)*dt;
nt += 1000;
}else{
sf_warning("=> x=%f y=%f t=%f",s[1],s[0],t);
sf_error("Bad angle, ray get to the model side/bottom");
}
/* Raytrace close */
raytrace_close(rt);
free(traj);
x[0] = s[is][0];
x[1] = s[is][1];
} /* Loop over source-NIP-receiver rays */
m = (xr+xs)/2.;
h = (xr-xs)/2.;
t = creTimeApproximation(h,m,v0,t0[is],m0[is],RNIP[is],BETA[is],true);
tmis += fabs((ts+tr)-t);
} /* Loop over reflection rays */
} /* Loop over NIP sources */
/* TODO: Evaluate the best function to calcullate the time misfit */
tmis = (tmis*tmis)/(nr*ns);
return tmis;
}