Dear Jason,
The attachment is my code, I use [`line_profiler`](
https://github.com/rkern/line_profiler) to record time consuming. I added a break into the original optimal_path code, because it doesn't need too many iterations in Rung-Kutta, and now it needs 40-70 iterations for this example.
if add @profile to function get_kernel, the output:
```
6 @profile
7 def get_kernel(data, nx, ny, loc_source, loc_receiver, velocity, num_interval):
8 """Compute kernel function for ray theory
9 get kernel when given location of source and receiver based
10 on some velocity model
11
12
13 Parameter
14 =========
15
16 data: tuple
17 travel time data (num_of_receivers)
18 nx: int
19 number of grids along x axis
20 ny: int
21 number of grids along y axis
22 loc_source: tuple
23 (int, int), e.g. (2, 3)
24 loc_receiver: tuple
25 ((int, int), (int, int), ...), e.g. ((1, 2), (3, 4))
26 velocity: array
27 shape (ny, nx)
28 num_interval: int
29 number of sampling to get grids which shortest path goes across
30
31 Return
32 ======
33
34 kernel: array
35 shape (ny, nx)"""
36
37 # default set dx = 1
38 1 18 18.0 0.0 dx = 1
39
40 1 52 52.0 0.0 coordx = np.arange(nx)
41 1 6 6.0 0.0 coordy = np.arange(ny)
42 1 494 494.0 0.0 X, Y = np.meshgrid(coordx, coordy)
43
44 1 3 3.0 0.0 xs, ys = loc_source
45 1 148 148.0 0.0 phi = -1 * np.ones_like(X)
46 1 1168 1168.0 0.0 phi[np.logical_and(np.abs(X - xs) <= dx, np.abs(Y - ys) <= dx)] = 1
47 1 31392 31392.0 0.3 travel_time = skfmm.travel_time(phi, velocity, dx)
48
49 1 61 61.0 0.0 kernel = np.zeros_like(velocity)
50 1 4183 4183.0 0.0 t_rec = get_time(travel_time, coordx, coordy, loc_receiver)
51 1 1 1.0 0.0 i = 0
52 1 39 39.0 0.0 v2 = velocity**2
53 400 655 1.6 0.0 for rec in loc_receiver:
54 399 9107368 22825.5 98.0 yl, xl = optimal_path(travel_time, rec, loc_source, dx, coordx, coordy)
55 399 117388 294.2 1.3 grid_indx = get_curve(xl, yl, num_interval)
56 399 1170 2.9 0.0 dt = t_rec[i] - data[i]
57 399 4020 10.1 0.0 ix, iy = zip(*grid_indx)
58 399 27472 68.9 0.3 kernel[ix, iy] += -dt / v2[ix, iy]
59 399 727 1.8 0.0 i += 1
60 1 1 1.0 0.0 return kernel
```
The time that optimal_path costs is 98%, which is the main cost for the entire program.
The cost of time in optimal_path is below:
```
62 @profile
63 def optimal_path(travel_time,
64 starting_point,
65 ending_point,
66 dx,
67 coordx,
68 coordy,
69 N=10000):
70 """
71 Find the optimal path from starting_point to the zero contour
72 of travel_time. dx is the grid spacing
73 Solve the equation x_t = - grad t / | grad t |
74 """
75 399 278446 697.9 2.9 grad_t_y, grad_t_x = np.gradient(travel_time, dx)
76 399 2728 6.8 0.0 if isinstance(travel_time, np.ma.MaskedArray):
77 grad_t_y[grad_t_y.mask] = 0.0
78 grad_t_y = grad_t_y.data
79 grad_t_x[grad_t_x.mask] = 0.0
80 grad_t_x = grad_t_x.data
81
82 399 1562867 3917.0 16.3 gradx_interp = RectBivariateSpline(coordy, coordx, grad_t_x)
83 399 1511659 3788.6 15.8 grady_interp = RectBivariateSpline(coordy, coordx, grad_t_y)
84
85 399 1425 3.6 0.0 def get_velocity(position):
86 """ return normalized velocity at pos """
87 x, y = position
88 vel = np.array([gradx_interp(y, x)[0][0], grady_interp(y, x)[0][0]])
89 return vel / np.linalg.norm(vel)
90
91 399 885 2.2 0.0 def euler_point_update(pos, ds):
92 return pos - get_velocity(pos) * ds
93
94 399 814 2.0 0.0 def runge_kutta(pos, ds):
95 """ Fourth order Runge Kutta point update """
96 k1 = ds * get_velocity(pos)
97 k2 = ds * get_velocity(pos - k1 / 2.0)
98 k3 = ds * get_velocity(pos - k2 / 2.0)
99 k4 = ds * get_velocity(pos - k3)
100 return pos - (k1 + 2 * k2 + 2 * k3 + k4) / 6.0
101
102 399 211439 529.9 2.2 x = runge_kutta(starting_point, dx)
103 399 1115 2.8 0.0 xl, yl = [], []
104 399 870 2.2 0.0 xe, ye = ending_point
105 21687 46534 2.1 0.5 for i in range(N):
106 21687 59000 2.7 0.6 xl.append(x[1])
107 21687 51408 2.4 0.5 yl.append(x[0])
108 21687 5695582 262.6 59.4 x = runge_kutta(x, dx)
109 21687 112979 5.2 1.2 distance = ((x[1] - xl[-1])**2 + (x[0] - yl[-1])**2)**0.5
110 21687 55296 2.5 0.6 if distance < dx * 0.9999:
111 399 918 2.3 0.0 break
112 399 823 2.1 0.0 return yl, xl
```
Thanks
> You received this message because you are subscribed to a topic in the Google Groups "scikit-fmm" group.
> To unsubscribe from this topic, visit
https://groups.google.com/d/topic/scikit-fmm/XvV_KhCUN5c/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
scikit-fmm+...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/scikit-fmm/CAOU2ZSW9Qx7oZhocJLqHHmFEbbGMC_k%3DpV1Ux%3DH4OfbPY1g7Nw%40mail.gmail.com.