Repository navigation
Expand file tree
/
Copy pathadjointlib.py
More file actions
814 lines (648 loc) · 28.3 KB
/
Copy pathadjointlib.py
File metadata and controls
814 lines (648 loc) · 28.3 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
from typing import Tuple
import numpy as np
# import pdb
def ray_tracer(dt: float, tmax: float, r0: np.array, xLim: Tuple[float, float],
zLim: Tuple[float, float], coeffs_v: np.array, dg: float) -> tuple:
"""Generate Ray Tracing using Runge-Kutta 2nd order method.
Args:
dt (float): Sampling period.
tmax (float): Maximum ray tracing time.
r0 (np.array): Initial conditions for the ray [x0, z0, px0, pz0].
xLim (Tuple[float, float]): Min and Max values for x.
zLim (Tuple[float, float]): Min and Max values for z.
coeffs_v (np.array): Velocity model coefficients.
dg (float): Degree of grid.
Returns:
A tuple containing:
- tray (np.array): Array of time steps.
- rt (np.array): Ray trajectory at each time step.
- dray (np.array): Derivatives of the ray trajectory.
- velocity_vector (np.array): Velocity vector at each time step.
"""
num_points = np.ceil(tmax / dt) + 1
tray = np.linspace(0, tmax, int(num_points))
# pdb.set_trace()
if tray[-1] != tmax:
tray = np.append(tray, tmax)
nt = len(tray)
# Start ray with initial conditions
rt = np.zeros((nt, 4))
rt[0] = r0
coeffs_mirror = mirrorW2d(coeffs_v)
velocity_vector = np.zeros((nt, 3))
# Determine derivatives at t=0
dray = np.zeros((nt, 4))
v0, vx0, vz0, _, _, _ = defvel(coeffs_mirror, r0[0], r0[1], dg)
velocity_vector[0] = np.array([v0, vx0, vz0])
dray[0] = np.array([(v0 ** 2) * r0[2],
(v0 ** 2) * r0[3],
- vx0 / v0,
- vz0 / v0])
xlim1, xlim2 = xLim[0], xLim[-1]
zlim1, zlim2 = zLim[0], zLim[-1]
for ii in range(1, nt):
# Determine k1
xt, zt = rt[ii - 1, 0], rt[ii - 1, 1]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, _, _, _ = defvel(coeffs_mirror, xt, zt, dg)
k1 = dt * np.asarray([(v ** 2) * rt[ii - 1, 2],
(v ** 2) * rt[ii - 1, 3],
- vx / v,
- vz / v])
# Determine k2
rk = rt[ii - 1] + 0.5 * k1
xt, zt = rk[0], rk[1]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, _, _, _ = defvel(coeffs_mirror, xt, zt, dg)
k2 = dt * np.asarray([(v ** 2) * rk[2],
(v ** 2) * rk[3],
- vx / v,
- vz / v])
# Solve ray
rt[ii] = rt[ii - 1] + k2
# Get deviration for each point of the ray
dray[ii] = np.array([(v ** 2) * rt[ii, 2], # dx/dt
(v ** 2) * rt[ii, 3], # dz/dt
- vx / v,
- vz / v])
velocity_vector[ii] = np.array([v, vx, vz])
return tray, rt, dray, velocity_vector
def ray_tracer_rk2_t(dt, tmax, r0, xLim, zLim, coeffs_v, dg):
"""Generate Ray Tracing using Runge-Kutta 2nd order method.
This function is similar to ray_tracer but returns derivatives only at the
final time.
Args:
dt (float): Sampling period.
tmax (float): Maximum ray tracing time.
r0 (np.array): Initial conditions for the ray [x0, z0, px0, pz0].
xLim (Tuple[float, float]): Min and Max values for x.
zLim (Tuple[float, float]): Min and Max values for z.
coeffs_v (np.array): Velocity model coefficients.
dg (float): Degree of grid.
Returns:
A tuple containing:
- tray (np.array): Array of time steps.
- rt (np.array): Ray trajectory at each time step.
- dray (np.array): Derivatives at the final time.
"""
num_points = np.ceil(tmax / dt) + 1
tray = np.linspace(0, tmax, int(num_points))
# pdb.set_trace()
if tray[-1] != tmax:
tray = np.append(tray, tmax)
nt = len(tray)
rt = np.zeros((nt, 4))
rt[0] = r0
coeffs_mirror = mirrorW2d(coeffs_v)
xlim1, xlim2 = xLim[0], xLim[-1]
zlim1, zlim2 = zLim[0], zLim[-1]
for ii in range(1, nt):
# k1
xt, zt = rt[ii - 1, 0], rt[ii - 1, 1]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, _, _, _ = defvel(coeffs_mirror, xt, zt, dg)
k1 = dt * np.asarray([(v ** 2) * rt[ii - 1, 2], (v ** 2)
* rt[ii - 1, 3], -(1 / v) * vx, -(1 / v) * vz])
# k2
rk = rt[ii - 1] + 0.5 * k1
xt, zt = rk[0], rk[1]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, _, _, _ = defvel(coeffs_mirror, xt, zt, dg)
k2 = dt * np.asarray([(v ** 2) * rk[2],
(v ** 2) * rk[3],
-(1 / v) * vx,
-(1 / v) * vz])
# solve ray
rt[ii] = rt[ii - 1] + k2
# determine derivatives at tf
xt, zt, px, pz = rt[-1, 0], rt[-1, 1], rt[-1, 2], rt[-1, 3]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, _, _, _ = defvel(coeffs_mirror, xt, zt, dg)
dray = np.array([(v ** 2) * px, # dx/dt
(v ** 2) * pz, # dz/dt
-(1 / v) * vx, # dpx/dt
-(1 / v) * vz]) # dpz/dt
return tray, rt, dray
def adjoint_state_solver_rk2_t(dt, rf, lambdaf, tf, xLim, zLim, coeffs_v, dg):
"""Solve the adjoint state equations using Runge-Kutta 2nd order method.
Args:
dt (float): Sampling period.
rf (np.array): Final conditions for the ray [xf, zf, pxf, pzf].
lambdaf (np.array): Final conditions for the adjoint variables.
tf (float): Final time.
xLim (Tuple[float, float]): Min and Max values for x.
zLim (Tuple[float, float]): Min and Max values for z.
coeffs_v (np.array): Velocity model coefficients.
dg (float): Degree of grid.
Returns:
A tuple containing:
- rt (np.array): Adjoint state trajectory.
- tray (np.array): Array of time steps (reversed).
- v0 (float): Velocity at the initial point.
"""
rf = np.array(rf)
lambdaf = np.array(lambdaf)
num_points = np.ceil(tf / dt) + 1
tray = np.linspace(0, tf, int(num_points))
if tray[-1] != tf:
tray = np.append(tray, tf)
tray = tray[::-1]
nt = len(tray)
rt = np.zeros((nt, 8))
rt[0] = np.concatenate([rf, lambdaf])
xlim1, xlim2 = xLim[0], xLim[-1]
zlim1, zlim2 = zLim[0], zLim[-1]
coeffs_mirror = mirrorW2d(coeffs_v)
dt = -dt
for ii in range(1, nt):
# k1
rk = rt[ii - 1]
xt, zt = rk[0], rk[1]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, vxx, vzz, vxz = defvel(coeffs_mirror, xt, zt, dg)
x = (v ** 2) * rk[2]
z = (v ** 2) * rk[3]
px = -(1 / v) * vx
pz = -(1 / v) * vz
lambda1 = -rk[4] * rk[2] * 2 * v * vx - rk[5] * rk[3] * 2 * v * vx \
+ rk[6] * ((1 / v) * vxx - (1 / (v ** 2)) * vx * vx) \
+ rk[7] * ((1 / v) * vxz - (1 / (v ** 2)) * vx * vz)
lambda2 = -rk[4] * rk[2] * 2 * v * vz - rk[5] * rk[3] * 2 * v * vz \
+ rk[6] * ((1 / v) * vxz - (1 / (v ** 2)) * vx * vz) \
+ rk[7] * ((1 / v) * vzz - (1 / (v ** 2)) * vz * vz)
lambda3 = -rk[4] * (v ** 2)
lambda4 = -rk[5] * (v ** 2)
k1 = dt * np.asarray([x, z, px, pz, lambda1,
lambda2, lambda3, lambda4])
# k2
rk = rt[ii - 1] + 0.5 * k1
xt, zt = rk[0], rk[1]
xt = max(xt, xlim1)
xt = min(xt, xlim2)
zt = max(zt, zlim1)
zt = min(zt, zlim2)
v, vx, vz, vxx, vzz, vxz = defvel(coeffs_mirror, xt, zt, dg)
x = (v ** 2) * rk[2]
z = (v ** 2) * rk[3]
px = -(1 / v) * vx
pz = -(1 / v) * vz
lambda1 = -rk[4] * rk[2] * 2 * v * vx - rk[5] * rk[3] * 2 * v * vx \
+ rk[6] * ((1 / v) * vxx - (1 / (v ** 2)) * vx * vx) \
+ rk[7] * ((1 / v) * vxz - (1 / (v ** 2)) * vx * vz)
lambda2 = -rk[4] * rk[2] * 2 * v * vz - rk[5] * rk[3] * 2 * v * vz \
+ rk[6] * ((1 / v) * vxz - (1 / (v ** 2)) * vx * vz) \
+ rk[7] * ((1 / v) * vzz - (1 / (v ** 2)) * vz * vz)
lambda3 = -rk[4] * (v ** 2)
lambda4 = -rk[5] * (v ** 2)
k2 = dt * np.asarray([x, z, px, pz, lambda1,
lambda2, lambda3, lambda4])
# solve
rt[ii] = rt[ii - 1] + k2
# determine v
v0 = v
return rt, tray, v0
def mirrorW2d(s):
"""Mirrors the borders of a 2D array to handle boundary conditions.
Args:
s (np.array): The input 2D array.
Returns:
np.array: The mirrored 2D array.
"""
N, M = s.shape
s_mirror = np.zeros((N + 3, M + 3))
s_mirror[1:-2, 1:-2] = s
# Mirror rows 1 and N-1
s_mirror[0, 1:-2] = s[1, :]
s_mirror[-2, 1:-2] = s[-2, :]
# Mirror columns 1 and N-1
s_mirror[1:-2, 0] = s[:, 1]
s_mirror[1:-2, -2] = s[:, -2]
# Edges treatment
s_mirror[0, 0] = s[1, 1]
s_mirror[0, -2] = s[1, -2]
s_mirror[-2, 0] = s[-2, 1]
s_mirror[-2, -2] = s[-2, -2]
return s_mirror
def direct_filter_1d(s):
"""Apply a 1D direct filter for B-spline coefficient calculation.
Args:
s (np.array): The input 1D array.
Returns:
np.array: The filtered 1D array.
"""
N = len(s)
z1 = -2 + np.sqrt(3)
cplus = np.zeros(N)
cminus = np.zeros(N)
sum0 = 0
for k in range(N):
sum0 = sum0 + 6 * s[k] * (z1**(k))
cplus[0] = sum0
for k in range(1, N):
cplus[k] = 6 * s[k] + z1 * cplus[k - 1]
cminus[N - 1] = (z1 / (z1**2 - 1)) * (cplus[N - 1] + z1 * cplus[N - 2])
for k in range(N - 2, -1, -1):
cminus[k] = z1 * (cminus[k + 1] - cplus[k])
return cminus
def direct_filter_2d(img):
"""Apply a 2D direct filter by applying the 1D filter along each axis.
Args:
img (np.array): The input 2D array (image).
Returns:
np.array: The filtered 2D array.
"""
N, M = img.shape
coeffs = np.zeros((N, M))
# Filtrations along y
for i in range(N):
row = img[i, :]
filt_row = direct_filter_1d(row)
coeffs[i, :] = filt_row
# Filtrations along x
for j in range(M):
col = coeffs[:, j]
filt_col = direct_filter_1d(col)
coeffs[:, j] = filt_col
return coeffs
def coeffs_bsplines_2d(s):
"""Calculate the B-spline coefficients for a 2D array.
Args:
s (np.array): The input 2D array.
Returns:
np.array: The B-spline coefficients.
"""
return direct_filter_2d(s)
def bspline(x):
"""Calculate the value of the cubic B-spline function.
Args:
x (float): The input value.
Returns:
float: The B-spline value.
"""
abs_x = abs(x)
if (abs_x >= 0) and (abs_x < 1):
betta = 2 / 3 - abs_x ** 2 + (abs_x ** 3) / 2
elif (abs_x >= 1) and (abs_x < 2):
betta = ((2 - abs_x) ** 3) / 6
elif (abs_x >= 2):
betta = 0
return betta
def d1bspline(x):
"""Calculate the value of the first derivative of the cubic B-spline function.
Args:
x (float): The input value.
Returns:
float: The first derivative value.
"""
abs_x = abs(x)
if abs_x >= 0 and abs_x < 1:
betta = 0.5 * x * (3 * abs_x - 4)
elif abs_x >= 1 and abs_x < 2:
betta = (-x * (2 - abs_x)**2) / (2 * abs_x)
else:
betta = 0
return betta
def d2bspline(x):
"""Calculate the value of the second derivative of the cubic B-spline function.
Args:
x (float): The input value.
Returns:
float: The second derivative value.
"""
abs_x = abs(x)
if abs_x == 0:
betta = -2
elif abs_x > 0 and abs_x < 1:
betta = 3 * x**2 / abs_x - 2
elif abs_x >= 1 and abs_x < 2:
betta = 2 - abs_x
else:
betta = 0
return betta
def interp2d_bsplines(s, rate1, rate2):
"""Interpolate a 2D array using B-splines.
Args:
s (np.array): The input 2D array.
rate1 (int): The interpolation rate for the first dimension.
rate2 (int): The interpolation rate for the second dimension.
Returns:
A tuple containing:
- s_interp (np.array): The interpolated 2D array.
- coeffs (np.array): The B-spline coefficients.
"""
coeffs = coeffs_bsplines_2d(s)
coeffs_mirror = mirrorW2d(coeffs)
N = rate1 * s.shape[0] - (rate1 - 1)
M = rate2 * s.shape[1] - (rate2 - 1)
s_interp = np.zeros((N, M))
for k in range(N):
for l in range(M):
s_interp[k, l] = interp2d_bsplines_core(
coeffs_mirror, k/rate1, l/rate2)
return s_interp, coeffs
def interp2d_bsplines_core(coeffs_mirror, row, col):
"""Core function for 2D B-spline interpolation at a single point.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients.
row (float): The row coordinate for interpolation.
col (float): The column coordinate for interpolation.
Returns:
float: The interpolated value.
"""
k = int(row)
l = int(col)
interp_value = (
coeffs_mirror[k + 0][l + 0] * bspline(row - k + 1) * bspline(col - l + 1) +
coeffs_mirror[k + 1][l + 0] * bspline(row - k + 0) * bspline(col - l + 1) +
coeffs_mirror[k + 2][l + 0] * bspline(row - k - 1) * bspline(col - l + 1) +
coeffs_mirror[k + 3][l + 0] * bspline(row - k - 2) * bspline(col - l + 1) +
coeffs_mirror[k + 0][l + 1] * bspline(row - k + 1) * bspline(col - l + 0) +
coeffs_mirror[k + 1][l + 1] * bspline(row - k + 0) * bspline(col - l + 0) +
coeffs_mirror[k + 2][l + 1] * bspline(row - k - 1) * bspline(col - l + 0) +
coeffs_mirror[k + 3][l + 1] * bspline(row - k - 2) * bspline(col - l + 0) +
coeffs_mirror[k + 0][l + 2] * bspline(row - k + 1) * bspline(col - l - 1) +
coeffs_mirror[k + 1][l + 2] * bspline(row - k + 0) * bspline(col - l - 1) +
coeffs_mirror[k + 2][l + 2] * bspline(row - k - 1) * bspline(col - l - 1) +
coeffs_mirror[k + 3][l + 2] * bspline(row - k - 2) * bspline(col - l - 1) +
coeffs_mirror[k + 0][l + 3] * bspline(row - k + 1) * bspline(col - l - 2) +
coeffs_mirror[k + 1][l + 3] * bspline(row - k + 0) * bspline(col - l - 2) +
coeffs_mirror[k + 2][l + 3] * bspline(row - k - 1) * bspline(col - l - 2) +
coeffs_mirror[k + 3][l + 3] *
bspline(row - k - 2) * bspline(col - l - 2)
)
return interp_value
def interp2d_dz_bsplines_core(coeffs_mirror, row, col):
"""Core function for 2D B-spline interpolation of the derivative w.r.t. z.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients.
row (float): The row coordinate for interpolation.
col (float): The column coordinate for interpolation.
Returns:
float: The interpolated derivative value.
"""
k = int(row)
l = int(col)
interp_value = (
coeffs_mirror[k + 0][l + 0] * d1bspline(row - k + 1) * bspline(col - l + 1) +
coeffs_mirror[k + 1][l + 0] * d1bspline(row - k + 0) * bspline(col - l + 1) +
coeffs_mirror[k + 2][l + 0] * d1bspline(row - k - 1) * bspline(col - l + 1) +
coeffs_mirror[k + 3][l + 0] * d1bspline(row - k - 2) * bspline(col - l + 1) +
coeffs_mirror[k + 0][l + 1] * d1bspline(row - k + 1) * bspline(col - l + 0) +
coeffs_mirror[k + 1][l + 1] * d1bspline(row - k + 0) * bspline(col - l + 0) +
coeffs_mirror[k + 2][l + 1] * d1bspline(row - k - 1) * bspline(col - l + 0) +
coeffs_mirror[k + 3][l + 1] * d1bspline(row - k - 2) * bspline(col - l + 0) +
coeffs_mirror[k + 0][l + 2] * d1bspline(row - k + 1) * bspline(col - l - 1) +
coeffs_mirror[k + 1][l + 2] * d1bspline(row - k + 0) * bspline(col - l - 1) +
coeffs_mirror[k + 2][l + 2] * d1bspline(row - k - 1) * bspline(col - l - 1) +
coeffs_mirror[k + 3][l + 2] * d1bspline(row - k - 2) * bspline(col - l - 1) +
coeffs_mirror[k + 0][l + 3] * d1bspline(row - k + 1) * bspline(col - l - 2) +
coeffs_mirror[k + 1][l + 3] * d1bspline(row - k + 0) * bspline(col - l - 2) +
coeffs_mirror[k + 2][l + 3] * d1bspline(row - k - 1) * bspline(col - l - 2) +
coeffs_mirror[k + 3][l + 3] *
d1bspline(row - k - 2) * bspline(col - l - 2)
)
return interp_value
def interp2d_dx_bsplines_core(coeffs_mirror, row, col):
"""Core function for 2D B-spline interpolation of the derivative w.r.t. x.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients.
row (float): The row coordinate for interpolation.
col (float): The column coordinate for interpolation.
Returns:
float: The interpolated derivative value.
"""
k = int(row)
l = int(col)
interp_value = (
coeffs_mirror[k + 0][l + 0] * bspline(row - k + 1) * d1bspline(col - l + 1) +
coeffs_mirror[k + 1][l + 0] * bspline(row - k + 0) * d1bspline(col - l + 1) +
coeffs_mirror[k + 2][l + 0] * bspline(row - k - 1) * d1bspline(col - l + 1) +
coeffs_mirror[k + 3][l + 0] * bspline(row - k - 2) * d1bspline(col - l + 1) +
coeffs_mirror[k + 0][l + 1] * bspline(row - k + 1) * d1bspline(col - l + 0) +
coeffs_mirror[k + 1][l + 1] * bspline(row - k + 0) * d1bspline(col - l + 0) +
coeffs_mirror[k + 2][l + 1] * bspline(row - k - 1) * d1bspline(col - l + 0) +
coeffs_mirror[k + 3][l + 1] * bspline(row - k - 2) * d1bspline(col - l + 0) +
coeffs_mirror[k + 0][l + 2] * bspline(row - k + 1) * d1bspline(col - l - 1) +
coeffs_mirror[k + 1][l + 2] * bspline(row - k + 0) * d1bspline(col - l - 1) +
coeffs_mirror[k + 2][l + 2] * bspline(row - k - 1) * d1bspline(col - l - 1) +
coeffs_mirror[k + 3][l + 2] * bspline(row - k - 2) * d1bspline(col - l - 1) +
coeffs_mirror[k + 0][l + 3] * bspline(row - k + 1) * d1bspline(col - l - 2) +
coeffs_mirror[k + 1][l + 3] * bspline(row - k + 0) * d1bspline(col - l - 2) +
coeffs_mirror[k + 2][l + 3] * bspline(row - k - 1) * d1bspline(col - l - 2) +
coeffs_mirror[k + 3][l + 3] *
bspline(row - k - 2) * d1bspline(col - l - 2)
)
return interp_value
def interp2d_dzdz_bsplines_core(coeffs_mirror, row, col):
"""Core function for 2D B-spline interpolation of the second derivative w.r.t. z.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients.
row (float): The row coordinate for interpolation.
col (float): The column coordinate for interpolation.
Returns:
float: The interpolated second derivative value.
"""
k = int(row)
l = int(col)
interp_value = (
coeffs_mirror[k + 0][l + 0] * d2bspline(row - k + 1) * bspline(col - l + 1) +
coeffs_mirror[k + 1][l + 0] * d2bspline(row - k + 0) * bspline(col - l + 1) +
coeffs_mirror[k + 2][l + 0] * d2bspline(row - k - 1) * bspline(col - l + 1) +
coeffs_mirror[k + 3][l + 0] * d2bspline(row - k - 2) * bspline(col - l + 1) +
coeffs_mirror[k + 0][l + 1] * d2bspline(row - k + 1) * bspline(col - l + 0) +
coeffs_mirror[k + 1][l + 1] * d2bspline(row - k + 0) * bspline(col - l + 0) +
coeffs_mirror[k + 2][l + 1] * d2bspline(row - k - 1) * bspline(col - l + 0) +
coeffs_mirror[k + 3][l + 1] * d2bspline(row - k - 2) * bspline(col - l + 0) +
coeffs_mirror[k + 0][l + 2] * d2bspline(row - k + 1) * bspline(col - l - 1) +
coeffs_mirror[k + 1][l + 2] * d2bspline(row - k + 0) * bspline(col - l - 1) +
coeffs_mirror[k + 2][l + 2] * d2bspline(row - k - 1) * bspline(col - l - 1) +
coeffs_mirror[k + 3][l + 2] * d2bspline(row - k - 2) * bspline(col - l - 1) +
coeffs_mirror[k + 0][l + 3] * d2bspline(row - k + 1) * bspline(col - l - 2) +
coeffs_mirror[k + 1][l + 3] * d2bspline(row - k + 0) * bspline(col - l - 2) +
coeffs_mirror[k + 2][l + 3] * d2bspline(row - k - 1) * bspline(col - l - 2) +
coeffs_mirror[k + 3][l + 3] *
d2bspline(row - k - 2) * bspline(col - l - 2)
)
return interp_value
def interp2d_dxdx_bsplines_core(coeffs_mirror, row, col):
"""Core function for 2D B-spline interpolation of the second derivative w.r.t. x.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients.
row (float): The row coordinate for interpolation.
col (float): The column coordinate for interpolation.
Returns:
float: The interpolated second derivative value.
"""
k = int(row)
l = int(col)
interp_value = (
coeffs_mirror[k + 0][l + 0] * bspline(row - k + 1) * d2bspline(col - l + 1) +
coeffs_mirror[k + 1][l + 0] * bspline(row - k + 0) * d2bspline(col - l + 1) +
coeffs_mirror[k + 2][l + 0] * bspline(row - k - 1) * d2bspline(col - l + 1) +
coeffs_mirror[k + 3][l + 0] * bspline(row - k - 2) * d2bspline(col - l + 1) +
coeffs_mirror[k + 0][l + 1] * bspline(row - k + 1) * d2bspline(col - l + 0) +
coeffs_mirror[k + 1][l + 1] * bspline(row - k + 0) * d2bspline(col - l + 0) +
coeffs_mirror[k + 2][l + 1] * bspline(row - k - 1) * d2bspline(col - l + 0) +
coeffs_mirror[k + 3][l + 1] * bspline(row - k - 2) * d2bspline(col - l + 0) +
coeffs_mirror[k + 0][l + 2] * bspline(row - k + 1) * d2bspline(col - l - 1) +
coeffs_mirror[k + 1][l + 2] * bspline(row - k + 0) * d2bspline(col - l - 1) +
coeffs_mirror[k + 2][l + 2] * bspline(row - k - 1) * d2bspline(col - l - 1) +
coeffs_mirror[k + 3][l + 2] * bspline(row - k - 2) * d2bspline(col - l - 1) +
coeffs_mirror[k + 0][l + 3] * bspline(row - k + 1) * d2bspline(col - l - 2) +
coeffs_mirror[k + 1][l + 3] * bspline(row - k + 0) * d2bspline(col - l - 2) +
coeffs_mirror[k + 2][l + 3] * bspline(row - k - 1) * d2bspline(col - l - 2) +
coeffs_mirror[k + 3][l + 3] *
bspline(row - k - 2) * d2bspline(col - l - 2)
)
return interp_value
def interp2d_dxdz_bsplines_core(coeffs_mirror, row, col):
"""Core function for 2D B-spline interpolation of the mixed second derivative.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients.
row (float): The row coordinate for interpolation.
col (float): The column coordinate for interpolation.
Returns:
float: The interpolated mixed derivative value.
"""
k = int(row)
l = int(col)
interp_value = (
coeffs_mirror[k + 0][l + 0] * d1bspline(row - k + 1) * d1bspline(col - l + 1) +
coeffs_mirror[k + 1][l + 0] * d1bspline(row - k + 0) * d1bspline(col - l + 1) +
coeffs_mirror[k + 2][l + 0] * d1bspline(row - k - 1) * d1bspline(col - l + 1) +
coeffs_mirror[k + 3][l + 0] * d1bspline(row - k - 2) * d1bspline(col - l + 1) +
coeffs_mirror[k + 0][l + 1] * d1bspline(row - k + 1) * d1bspline(col - l + 0) +
coeffs_mirror[k + 1][l + 1] * d1bspline(row - k + 0) * d1bspline(col - l + 0) +
coeffs_mirror[k + 2][l + 1] * d1bspline(row - k - 1) * d1bspline(col - l + 0) +
coeffs_mirror[k + 3][l + 1] * d1bspline(row - k - 2) * d1bspline(col - l + 0) +
coeffs_mirror[k + 0][l + 2] * d1bspline(row - k + 1) * d1bspline(col - l - 1) +
coeffs_mirror[k + 1][l + 2] * d1bspline(row - k + 0) * d1bspline(col - l - 1) +
coeffs_mirror[k + 2][l + 2] * d1bspline(row - k - 1) * d1bspline(col - l - 1) +
coeffs_mirror[k + 3][l + 2] * d1bspline(row - k - 2) * d1bspline(col - l - 1) +
coeffs_mirror[k + 0][l + 3] * d1bspline(row - k + 1) * d1bspline(col - l - 2) +
coeffs_mirror[k + 1][l + 3] * d1bspline(row - k + 0) * d1bspline(col - l - 2) +
coeffs_mirror[k + 2][l + 3] * d1bspline(row - k - 1) * d1bspline(col - l - 2) +
coeffs_mirror[k + 3][l + 3] *
d1bspline(row - k - 2) * d1bspline(col - l - 2)
)
return interp_value
def defvel(coeffs_mirror, xtau, ztau, dg):
"""Calculate velocity and its derivatives at a point using B-spline interpolation.
Args:
coeffs_mirror (np.array): The mirrored B-spline coefficients of the velocity model.
xtau (float): The x-coordinate.
ztau (float): The z-coordinate.
dg (float): The grid spacing.
Returns:
A tuple containing:
- v (float): Velocity.
- vx (float): Derivative of velocity w.r.t. x.
- vz (float): Derivative of velocity w.r.t. z.
- vxx (float): Second derivative of velocity w.r.t. x.
- vzz (float): Second derivative of velocity w.r.t. z.
- vxz (float): Mixed second derivative of velocity w.r.t. x and z.
"""
v = interp2d_bsplines_core(coeffs_mirror, ztau / dg, xtau / dg)
vx = (1 / dg) * interp2d_dx_bsplines_core(coeffs_mirror, ztau / dg, xtau / dg)
vz = (1 / dg) * interp2d_dz_bsplines_core(coeffs_mirror, ztau / dg, xtau / dg)
vxx = (1 / dg) * (1 / dg) * \
interp2d_dxdx_bsplines_core(coeffs_mirror, ztau / dg, xtau / dg)
vzz = (1 / dg) * (1 / dg) * \
interp2d_dzdz_bsplines_core(coeffs_mirror, ztau / dg, xtau / dg)
vxz = (1 / dg) * (1 / dg) * \
interp2d_dxdz_bsplines_core(coeffs_mirror, ztau / dg, xtau / dg)
return v, vx, vz, vxx, vzz, vxz
def derivative_bsplines_coeffs(rt, xi, zj, dg, mu3, mu4, x0, z0, theta0, v0, dt, coeffs_v):
"""Calculate the derivative of the cost function w.r.t. B-spline coefficients.
Args:
rt (np.array): Adjoint state trajectory.
xi (np.array): X-coordinates of the grid.
zj (np.array): Z-coordinates of the grid.
dg (float): Grid spacing.
mu3 (float): Weighting factor for the p_x component of the cost function.
mu4 (float): Weighting factor for the p_z component of the cost function.
x0 (float): Initial x-coordinate of the ray.
z0 (float): Initial z-coordinate of the ray.
theta0 (float): Initial angle of the ray.
v0 (float): Initial velocity.
dt (float): Time step.
coeffs_v (np.array): Velocity model B-spline coefficients.
Returns:
A tuple containing:
- dJdw (np.array): Derivative of the cost function w.r.t. the coefficients.
- out_test (np.array): Contribution from the initial condition term.
"""
x_t = rt[::-1, 0]
z_t = rt[::-1, 1]
px_t = rt[::-1, 2]
pz_t = rt[::-1, 3]
lambda1_t = rt[::-1, 4]
lambda2_t = rt[::-1, 5]
lambda3_t = rt[::-1, 6]
lambda4_t = rt[::-1, 7]
x_w = xi
z_w = zj
len_x = len(x_w)
len_z = len(z_w)
len_ray = len(x_t)
dJdw = np.zeros((len_z, len_x))
out_test = np.zeros((len_z, len_x))
# Term for the derivative near to the source
A = (mu3 * np.sin(theta0) + mu4 * np.cos(theta0)) * (1 / (v0 ** 2))
# determine outputs v, vx, and vz
coeffs_mirror = mirrorW2d(coeffs_v)
v_t = np.zeros(len_ray)
vx_t = np.zeros(len_ray)
vz_t = np.zeros(len_ray)
for ii in range(len_ray):
xt = x_t[ii]
zt = z_t[ii]
v_t[ii], vx_t[ii], vz_t[ii], _, _, _ = defvel(
coeffs_mirror, xt, zt, dg)
for kk in range(len_x):
xk = x_w[kk]
for ll in range(len_z):
zl = z_w[ll]
I1 = 0
I2 = 0
I3 = 0
I4 = 0
for ii in range(len_ray):
arg_k = (x_t[ii] - xk) / dg
Bk = bspline(arg_k)
d1Bk = d1bspline(arg_k) / dg
arg_l = (z_t[ii] - zl) / dg
Bl = bspline(arg_l)
d1Bl = d1bspline(arg_l) / dg
I1 += dt * 2 * (lambda1_t[ii] * px_t[ii] * v_t[ii] * Bk * Bl)
I2 += dt * 2 * (lambda2_t[ii] * pz_t[ii] * v_t[ii] * Bk * Bl)
I3 += dt * \
lambda3_t[ii] * ((1 / v_t[ii]) * d1Bk * Bl -
(1 / (v_t[ii] ** 2)) * vx_t[ii] * Bk * Bl)
I4 += dt * lambda4_t[ii] * ((1 / v_t[ii]) * Bk *
d1Bl - (1 / (v_t[ii] ** 2)) * vz_t[ii] * Bk * Bl)
Isum = -I1 - I2 + I3 + I4
B0k = bspline((x0 - xk) / dg)
B0l = bspline((z0 - zl) / dg)
I0 = A * B0k * B0l
dJdw[ll, kk] = Isum
out_test[ll, kk] = I0
return dJdw, out_test