|
126 | 126 | "%matplotlib inline\n", |
127 | 127 | "import numpy as np\n", |
128 | 128 | "\n", |
129 | | - "from devito import Operator,Eq,solve,Grid,SparseFunction,norm\n", |
130 | | - "from devito import TimeFunction,Function\n", |
131 | | - "from devito import gaussian_smooth\n", |
132 | | - "from devito import mmax\n", |
| 129 | + "from devito import (Operator, Eq, solve, Grid, norm, Function,\n", |
| 130 | + " gaussian_smooth, mmax)\n", |
133 | 131 | "\n", |
134 | | - "from devito.logger import info\n", |
135 | | - "\n", |
136 | | - "from examples.seismic import Model\n", |
137 | | - "from examples.seismic import plot_velocity,plot_shotrecord\n", |
138 | | - "from examples.seismic import Receiver\n", |
139 | | - "from examples.seismic import PointSource\n", |
140 | | - "from examples.seismic import plot_image,AcquisitionGeometry\n", |
141 | | - "from examples.seismic import TimeAxis\n", |
| 132 | + "from examples.seismic import (Model, plot_velocity, Receiver, plot_image,\n", |
| 133 | + " AcquisitionGeometry, TimeAxis)\n", |
142 | 134 | "\n", |
143 | 135 | "from examples.seismic.self_adjoint import (setup_w_over_q)\n", |
144 | 136 | "from examples.seismic.acoustic import AcousticWaveSolver\n", |
145 | 137 | "\n", |
146 | 138 | "import matplotlib.pyplot as plt\n", |
147 | 139 | "from mpl_toolkits.axes_grid1 import ImageGrid\n", |
148 | 140 | "from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable\n", |
149 | | - "import matplotlib.ticker as plticker\n", |
150 | 141 | "\n", |
151 | 142 | "from devito import configuration\n", |
152 | 143 | "configuration['log-level'] = 'WARNING'" |
|
183 | 174 | "omega = 2.0 * np.pi * fpeak\n", |
184 | 175 | "qmin = 0.1\n", |
185 | 176 | "qmax = 100000\n", |
186 | | - "npad=50\n", |
| 177 | + "npad = 50\n", |
187 | 178 | "dtype = np.float32\n", |
188 | 179 | "\n", |
189 | 180 | "nshots = 21\n", |
|
309 | 300 | "outputs": [], |
310 | 301 | "source": [ |
311 | 302 | "def lsrtm_gradient(dm):\n", |
312 | | - " \n", |
| 303 | + "\n", |
313 | 304 | " residual = Receiver(name='residual', grid=model.grid, time_range=geometry.time_axis,\n", |
314 | 305 | " coordinates=geometry.rec_positions)\n", |
315 | | - " \n", |
| 306 | + "\n", |
316 | 307 | " d_obs = Receiver(name='d_obs', grid=model.grid,time_range=geometry.time_axis,\n", |
317 | 308 | " coordinates=geometry.rec_positions)\n", |
318 | 309 | "\n", |
319 | 310 | " d_syn = Receiver(name='d_syn', grid=model.grid,time_range=geometry.time_axis,\n", |
320 | 311 | " coordinates=geometry.rec_positions)\n", |
321 | | - " \n", |
| 312 | + "\n", |
322 | 313 | " grad_full = Function(name='grad_full', grid=model.grid)\n", |
323 | | - " \n", |
| 314 | + "\n", |
324 | 315 | " grad_illum = Function(name='grad_illum', grid=model.grid)\n", |
325 | | - " \n", |
| 316 | + "\n", |
326 | 317 | " src_illum = Function (name =\"src_illum\", grid = model.grid)\n", |
327 | 318 | "\n", |
328 | 319 | " # Using devito's reference of virtual source\n", |
329 | 320 | " dm_true = (solver.model.vp.data**(-2) - model0.vp.data**(-2))\n", |
330 | | - " \n", |
| 321 | + "\n", |
331 | 322 | " objective = 0.\n", |
332 | 323 | " u0 = None\n", |
333 | 324 | " for i in range(nshots):\n", |
334 | | - " \n", |
| 325 | + "\n", |
335 | 326 | " #Observed Data using Born's operator\n", |
336 | 327 | " geometry.src_positions[0, :] = source_locations[i, :]\n", |
337 | 328 | "\n", |
338 | 329 | " _, u0, _ = solver.forward(vp=model0.vp, save=True, u=u0)\n", |
339 | 330 | " \n", |
340 | 331 | " _, _, _,_ = solver.jacobian(dm_true, vp=model0.vp, rec = d_obs)\n", |
341 | | - " \n", |
| 332 | + "\n", |
342 | 333 | " #Calculated Data using Born's operator\n", |
343 | 334 | " solver.jacobian(dm, vp=model0.vp, rec = d_syn)\n", |
344 | | - " \n", |
| 335 | + "\n", |
345 | 336 | " residual.data[:] = d_syn.data[:]- d_obs.data[:]\n", |
346 | | - " \n", |
| 337 | + "\n", |
347 | 338 | " grad_shot,_ = solver.gradient(rec=residual, u=u0, vp=model0.vp)\n", |
348 | | - " \n", |
| 339 | + "\n", |
349 | 340 | " src_illum_upd = Eq(src_illum, src_illum + u0**2)\n", |
350 | 341 | " op_src = Operator([src_illum_upd])\n", |
351 | 342 | " op_src.apply()\n", |
352 | | - " \n", |
| 343 | + "\n", |
353 | 344 | " grad_sum = Eq(grad_full, grad_full + grad_shot)\n", |
354 | 345 | " op_grad = Operator([grad_sum])\n", |
355 | 346 | " op_grad.apply()\n", |
356 | | - " \n", |
| 347 | + "\n", |
357 | 348 | " objective += .5*norm(residual)**2\n", |
358 | | - " \n", |
| 349 | + "\n", |
359 | 350 | " grad_f = Eq(grad_illum, grad_full/(src_illum+10**-9))\n", |
360 | 351 | " op_gradf = Operator([grad_f])\n", |
361 | 352 | " op_gradf.apply()\n", |
362 | | - " \n", |
| 353 | + "\n", |
363 | 354 | " return objective,grad_illum,d_obs,d_syn" |
364 | 355 | ] |
365 | 356 | }, |
|
394 | 385 | "outputs": [], |
395 | 386 | "source": [ |
396 | 387 | "def get_alfa(grad_iter,image_iter,niter_lsrtm):\n", |
397 | | - " \n", |
398 | | - " \n", |
| 388 | + "\n", |
| 389 | + "\n", |
399 | 390 | " term1 = np.dot(image_iter.reshape(-1), image_iter.reshape(-1))\n", |
400 | | - " \n", |
| 391 | + "\n", |
401 | 392 | " term2 = np.dot(image_iter.reshape(-1), grad_iter.reshape(-1))\n", |
402 | | - " \n", |
| 393 | + "\n", |
403 | 394 | " term3 = np.dot(grad_iter.reshape(-1), grad_iter.reshape(-1))\n", |
404 | | - " \n", |
| 395 | + "\n", |
405 | 396 | " if niter_lsrtm == 0:\n", |
406 | | - " \n", |
| 397 | + "\n", |
407 | 398 | " alfa = .05 / mmax(grad_full)\n", |
408 | | - " \n", |
| 399 | + "\n", |
409 | 400 | " else:\n", |
410 | 401 | " abb1 = term1 / term2\n", |
411 | | - " \n", |
| 402 | + "\n", |
412 | 403 | " abb2 = term2 / term3\n", |
413 | | - " \n", |
| 404 | + "\n", |
414 | 405 | " abb3 = abb2 / abb1\n", |
415 | | - " \n", |
| 406 | + "\n", |
416 | 407 | " if abb3 > 0 and abb3 < 1:\n", |
417 | 408 | " alfa = abb2\n", |
418 | 409 | " else:\n", |
419 | 410 | " alfa = abb1\n", |
420 | | - " \n", |
421 | | - " return alfa " |
| 411 | + "\n", |
| 412 | + " return alfa" |
422 | 413 | ] |
423 | 414 | }, |
424 | 415 | { |
|
468 | 459 | "\n", |
469 | 460 | "image = np.zeros((model0.vp.shape[0], model0.vp.shape[1]))\n", |
470 | 461 | "\n", |
471 | | - "nrec=101\n", |
472 | | - "niter=20 # Number of iterations of the LSRTM\n", |
| 462 | + "nrec = 101\n", |
| 463 | + "niter = 20 # Number of iterations of the LSRTM\n", |
473 | 464 | "history = np.zeros((niter, 1)) # Objective function\n", |
474 | 465 | "\n", |
475 | 466 | "image_prev = np.zeros((model0.vp.shape[0],model0.vp.shape[1]))\n", |
|
494 | 485 | "\n", |
495 | 486 | " sk = image_up_dev - image_prev\n", |
496 | 487 | "\n", |
497 | | - " alfa = get_alfa(yk,sk,k)\n", |
| 488 | + " alfa = get_alfa(yk, sk, k)\n", |
498 | 489 | "\n", |
499 | 490 | " grad_prev = grad_full.data\n", |
500 | 491 | "\n", |
|
632 | 623 | ], |
633 | 624 | "source": [ |
634 | 625 | "#NBVAL_SKIP\n", |
635 | | - "slices=tuple(slice(model.nbl,-model.nbl) for _ in range(2))\n", |
| 626 | + "slices = tuple(slice(model.nbl, -model.nbl) for _ in range(2))\n", |
636 | 627 | "lsrtm = image_up_dev[slices]\n", |
637 | 628 | "plot_image(np.diff(lsrtm, axis=1))" |
638 | 629 | ] |
|
689 | 680 | ], |
690 | 681 | "source": [ |
691 | 682 | "#NBVAL_SKIP\n", |
692 | | - "plt.figure(figsize=(8,9))\n", |
693 | | - "x = np.linspace(0,1,101)\n", |
694 | | - "plt.plot(rtm[50,:],x,color=plt.gray(),linewidth=2)\n", |
695 | | - "plt.plot(lsrtm[50,:],x,'r',linewidth=2)\n", |
| 683 | + "plt.figure(figsize=(8, 9))\n", |
| 684 | + "x = np.linspace(0, 1, 101)\n", |
| 685 | + "plt.plot(rtm[50,:], x, color=plt.gray(), linewidth=2)\n", |
| 686 | + "plt.plot(lsrtm[50,:],x, 'r',linewidth=2)\n", |
696 | 687 | "plt.plot(dm_true[50,:],x, 'k--',linewidth=2)\n", |
697 | 688 | "\n", |
698 | | - "plt.legend(['Initial reflectivity', 'Reflectivity via LSRTM','True Reflectivity'],fontsize=15)\n", |
| 689 | + "plt.legend(['Initial reflectivity', 'Reflectivity via LSRTM','True Reflectivity'], fontsize=15)\n", |
699 | 690 | "plt.ylabel('Depth (Km)')\n", |
700 | 691 | "plt.xlabel('Amplitude')\n", |
701 | 692 | "plt.gca().invert_yaxis()\n", |
|
723 | 714 | "source": [ |
724 | 715 | "#NBVAL_SKIP\n", |
725 | 716 | "time = np.linspace(t0, tn, nt)\n", |
726 | | - "plt.figure(figsize=(8,9))\n", |
| 717 | + "plt.figure(figsize=(8, 9))\n", |
727 | 718 | "plt.ylabel('Time (ms)')\n", |
728 | 719 | "plt.xlabel('Amplitude')\n", |
729 | | - "plt.plot(d_syn.data[:, 20],time, 'y', label='Calculated data (Last Iteration)')\n", |
730 | | - "plt.plot(d_obs.data[:, 20],time, 'm', label='Observed data')\n", |
731 | | - "plt.legend(loc=\"upper left\",fontsize=12)\n", |
| 720 | + "plt.plot(d_syn.data[:, 20], time, 'y', label='Calculated data (Last Iteration)')\n", |
| 721 | + "plt.plot(d_obs.data[:, 20], time, 'm', label='Observed data')\n", |
| 722 | + "plt.legend(loc=\"upper left\", fontsize=12)\n", |
732 | 723 | "ax = plt.gca()\n", |
733 | 724 | "ax.invert_yaxis()\n", |
734 | 725 | "plt.show()" |
|
0 commit comments