Skip to content

Commit fa87c69

Browse files
author
Malcolm White
committed
UPDATE:: Migrate demo notebook to current API
1 parent 70f3fe5 commit fa87c69

File tree

1 file changed

+94
-56
lines changed

1 file changed

+94
-56
lines changed

jupyter/demo.ipynb

+94-56
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@
4040
"metadata": {},
4141
"outputs": [],
4242
"source": [
43-
"def tt_head(xs, ys, zs, xr, yr, zr, y1, v1, v2):\n",
44-
" theta = np.arcsin(v1/v2)\n",
45-
" l1 = (y1 - ys) / np.cos(theta)\n",
46-
" l2 = np.sqrt((xr-xs)**2 + (zr-zs)**2) - np.tan(theta) * (2 * y1 - ys - yr)\n",
47-
" l3 = (y1 - yr) / np.cos(theta)\n",
48-
" return ((l1 + l3) / v1 + l2 / v2)\n",
49-
"\n",
50-
"def tt_direct(xs, ys, zs, xr, yr, zr, v1):\n",
51-
" return (np.sqrt((xr-xs)**2 + (yr-ys)**2 + (zr-zs)**2) / v1)\n",
52-
"\n",
5343
"def plot(solver, ix=None, iy=None, iz=None, attr='uu', rays=None, cbar_label='Travel-time [s]'):\n",
5444
" grid = pykonal.GridND(ndim=3)\n",
5545
" grid.min_coords = solver.pgrid.min_coords\n",
@@ -140,6 +130,36 @@
140130
" fig.tight_layout()"
141131
]
142132
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"### Define function to instantiate EikonalSolver with uniform velocity model"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"def init_solver():\n",
147+
" # Initialize the solver\n",
148+
" solver = pykonal.EikonalSolver()\n",
149+
"\n",
150+
" # Initialize the velocity grid with a uniform velocity model.\n",
151+
" # EikonalSolver.vgrid.min_coords specifies the minimum coordinates of the velocity grid\n",
152+
" solver.vgrid.min_coords = 0, 0, 0 # xmin, ymin, zmin\n",
153+
" # EikonalSolver.vgrid.node_intervals specifies the spacing between velocity grid nodes\n",
154+
" solver.vgrid.node_intervals = 1, 1, 1 # dx, dy, dz\n",
155+
" # EikonalSolver.vgrid.npts specifies the number of grid nodes along each axis\n",
156+
" solver.vgrid.npts = 11, 11, 11 # nx, ny, nz\n",
157+
" # EikonalSolver.vv holds the velocity at each grid node and should be a numpy.ndarray\n",
158+
" # with shape == EikonalSolver.vgrid.npts.\n",
159+
" solver.vv = np.ones(solver.vgrid.npts)\n",
160+
" return (solver)"
161+
]
162+
},
143163
{
144164
"cell_type": "markdown",
145165
"metadata": {},
@@ -154,29 +174,13 @@
154174
"outputs": [],
155175
"source": [
156176
"# Initialize the solver\n",
157-
"solver = pykonal.EikonalSolver()\n",
158-
"\n",
159-
"# Initialize the velocity grid with a uniform velocity model.\n",
160-
"# EikonalSolver.vgrid.min_coords specifies the minimum coordinates of the velocity grid\n",
161-
"solver.vgrid.min_coords = 0, 0, 0 # xmin, ymin, zmin\n",
162-
"# EikonalSolver.vgrid.node_intervals specifies the spacing between velocity grid nodes\n",
163-
"solver.vgrid.node_intervals = 1, 1, 1 # dx, dy, dz\n",
164-
"# EikonalSolver.vgrid.npts specifies the number of grid nodes along each axis\n",
165-
"solver.vgrid.npts = 11, 11, 11 # nx, ny, nz\n",
166-
"# EikonalSolver.vv holds the velocity at each grid node and should be a numpy.ndarray\n",
167-
"# with shape == EikonalSolver.vgrid.npts.\n",
168-
"solver.vv = np.ones(solver.vgrid.npts)\n",
169-
"\n",
170-
"# Initialize the propagation grid to coincide with velocity grid exactly.\n",
171-
"# Attributes of the propagation grid are analogous to those of the velocity grid.\n",
172-
"solver.pgrid.min_coords = solver.vgrid.min_coords\n",
173-
"solver.pgrid.node_intervals = solver.vgrid.node_intervals\n",
174-
"solver.pgrid.npts = solver.vgrid.npts\n",
177+
"solver = init_solver()\n",
175178
"\n",
176179
"# Add a source in the center of the computational domain.\n",
177-
"# Source locations need to be specified in physical coordinates.\n",
178-
"src = (5, 5, 5)\n",
179-
"solver.add_source(src)\n",
180+
"src_idx = (5, 5, 5) # The source location as an array index\n",
181+
"solver.uu[src_idx] = 0 # Set the traveltime at the source location to zero\n",
182+
"solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
183+
"solver.close.push(*src_idx) # Push the source index onto the close heap\n",
180184
"\n",
181185
"# Solve the Eikonal equation.\n",
182186
"solver.solve()\n",
@@ -199,12 +203,21 @@
199203
"metadata": {},
200204
"outputs": [],
201205
"source": [
206+
"# Initialize the solver\n",
207+
"solver = init_solver()\n",
202208
"# Decrease the node interval by a factor of 2\n",
203209
"solver.pgrid.node_intervals = solver.vgrid.node_intervals / 2\n",
204210
"# And increase the number of points by a factor of 2, making sure to not go beyond the\n",
205211
"# boundaries of the velocity grid\n",
206212
"solver.pgrid.npts = solver.vgrid.npts * 2 - 1\n",
207213
"\n",
214+
"\n",
215+
"# Add a source in the center of the computational domain.\n",
216+
"src_idx = (10, 10, 10) # The source location as an array index\n",
217+
"solver.uu[src_idx] = 0 # Set the traveltime at the source location to zero\n",
218+
"solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
219+
"solver.close.push(*src_idx) # Push the source index onto the close heap\n",
220+
"\n",
208221
"# Solve the Eikonal equation again.\n",
209222
"solver.solve()\n",
210223
"\n",
@@ -226,8 +239,16 @@
226239
"metadata": {},
227240
"outputs": [],
228241
"source": [
242+
"solver = init_solver()\n",
229243
"solver.pgrid.node_intervals = solver.vgrid.node_intervals / 10\n",
230244
"solver.pgrid.npts = solver.vgrid.npts * 10 - 9\n",
245+
"\n",
246+
"# Add a source in the center of the computational domain.\n",
247+
"src_idx = (50, 50, 50) # The source location as an array index\n",
248+
"solver.uu[src_idx] = 0 # Set the traveltime at the source location to zero\n",
249+
"solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
250+
"solver.close.push(*src_idx) # Push the source index onto the close heap\n",
251+
"\n",
231252
"solver.solve()\n",
232253
"plot(solver)"
233254
]
@@ -245,8 +266,15 @@
245266
"metadata": {},
246267
"outputs": [],
247268
"source": [
248-
"solver.clear_sources()\n",
249-
"solver.add_source((0, 5, 10))\n",
269+
"solver = init_solver()\n",
270+
"solver.pgrid.node_intervals = solver.vgrid.node_intervals / 10\n",
271+
"solver.pgrid.npts = solver.vgrid.npts * 10 - 9\n",
272+
"\n",
273+
"src_idx = (25, 50, 75) # The source location as an array index\n",
274+
"solver.uu[src_idx] = 0 # Set the traveltime at the source location to zero\n",
275+
"solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
276+
"solver.close.push(*src_idx) # Push the source index onto the close heap\n",
277+
"\n",
250278
"solver.solve()\n",
251279
"plot(solver)"
252280
]
@@ -264,7 +292,15 @@
264292
"metadata": {},
265293
"outputs": [],
266294
"source": [
267-
"solver.add_source((10, 5, 0))\n",
295+
"solver = init_solver()\n",
296+
"solver.pgrid.node_intervals = solver.vgrid.node_intervals / 10\n",
297+
"solver.pgrid.npts = solver.vgrid.npts * 10 - 9\n",
298+
"\n",
299+
"for src_idx in ((25, 50, 75), (75, 50, 25)):\n",
300+
" solver.uu[src_idx] = 0 # Set the traveltime at the source location to zero\n",
301+
" solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
302+
" solver.close.push(*src_idx) # Push the source index onto the close heap\n",
303+
"\n",
268304
"solver.solve()\n",
269305
"plot(solver)"
270306
]
@@ -282,7 +318,17 @@
282318
"metadata": {},
283319
"outputs": [],
284320
"source": [
285-
"solver.add_source((5, 5, 5), t0=2.5)\n",
321+
"solver = init_solver()\n",
322+
"solver.pgrid.node_intervals = solver.vgrid.node_intervals / 10\n",
323+
"solver.pgrid.npts = solver.vgrid.npts * 10 - 9\n",
324+
"\n",
325+
"for src_idx, t0 in (((25, 50, 75), 0), ((75, 50, 25), 2.5)):\n",
326+
" solver.uu[src_idx] = t0 # Set the traveltime at the source location to t0\n",
327+
" solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
328+
" solver.close.push(*src_idx) # Push the source index onto the close heap\n",
329+
"\n",
330+
"solver.solve()\n",
331+
"plot(solver)\n",
286332
"solver.solve()\n",
287333
"plot(solver)"
288334
]
@@ -300,32 +346,24 @@
300346
"metadata": {},
301347
"outputs": [],
302348
"source": [
349+
"solver = init_solver()\n",
350+
"\n",
303351
"vy = np.linspace(1, 5, solver.vgrid.npts[1])\n",
304352
"for iy in range(len(vy)):\n",
305353
" solver.vv[:,iy] = vy[iy]\n",
306354
" \n",
307-
"# This will plot the velocity model\n",
308-
"plot(solver, attr='vv_p', cbar_label='Velocity [km/s]')\n",
309-
"\n",
310-
"solver.clear_sources()\n",
311-
"solver.add_source((5, 5, 5))\n",
312-
"solver.solve()\n",
313-
"plot(solver)"
314-
]
315-
},
316-
{
317-
"cell_type": "code",
318-
"execution_count": null,
319-
"metadata": {},
320-
"outputs": [],
321-
"source": [
322-
"solver.vv[:, int(solver.vgrid.npts[1]//2):] = 5\n",
355+
"solver.pgrid.node_intervals = solver.vgrid.node_intervals / 10\n",
356+
"solver.pgrid.npts = solver.vgrid.npts * 10 - 9\n",
323357
"\n",
324-
"plot(solver, attr='vv_p', cbar_label='Velocity [km/s]')\n",
358+
"src_idx = (25, 50, 75)\n",
359+
"solver.uu[src_idx] = 0 # Set the traveltime at the source location to zero\n",
360+
"solver.is_far[src_idx] = False # Set the is_far flag to False for the source node\n",
361+
"solver.close.push(*src_idx) # Push the source index onto the close heap\n",
325362
"\n",
326-
"solver.clear_sources()\n",
327-
"solver.add_source((1, 3, 1))\n",
328363
"solver.solve()\n",
364+
"\n",
365+
"# This will plot the velocity model\n",
366+
"plot(solver, attr='vvp', cbar_label='Velocity [km/s]')\n",
329367
"plot(solver)"
330368
]
331369
},
@@ -343,7 +381,7 @@
343381
"outputs": [],
344382
"source": [
345383
"ray = solver.trace_ray((9.5, 0, 9.5))\n",
346-
"plot(solver, rays=[ray])"
384+
"plot(solver,rays=[ray])"
347385
]
348386
},
349387
{
@@ -391,5 +429,5 @@
391429
}
392430
},
393431
"nbformat": 4,
394-
"nbformat_minor": 2
432+
"nbformat_minor": 4
395433
}

0 commit comments

Comments
 (0)