Python Presets and Scaling Up
Learning objectives
- Run four of the book's engines as Python programs: a convolutional synthetic, a 2D finite-difference run, a SEG-Y writer and Hudson's crack weaknesses
- Check a program against the kernel the book uses: its printed numbers, its curves and, for the writer, the bytes of its file
- Read the stability and the accuracy of a finite-difference run from its program: the Courant number, the points per wavelength, the numerical speed
- Say what Devito, SPECFEM3D and Madagascar are for, and what stays the same when a model leaves the browser
The Same Physics, in Real Code
The Modeling Lab runs the book's engines in the page, where every slider answers at once. A page cannot do everything a research run needs, and a reader who wants to change a line, loop over a hundred models or keep the numbers needs the engines as code. This section writes four of them out as Python programs of 58 to 146 lines: the convolutional synthetic of Sections 2.1 to 2.3, a 2D finite-difference run of Part 4, a SEG-Y writer of Part 9 and the crack weaknesses of Part 8. Each uses numpy for its numbers and matplotlib for the figure it closes with, and each prints a short report that you can check by hand.
To run one, copy it, or press Open in the Playground to send it to the Python Playground under Tools. The Playground runs Pyodide, "a Python distribution for the browser and Node.js based on WebAssembly", which carries numpy and matplotlib among its packages; NumPy itself is the array library of Harris and others (2020). The programs need nothing else, and they run unchanged in any Python with those two packages.
The programs take their constants from the book's kernel, typed in to five decimals the way a reader would carry them: the standard shale and sand, the 30 Hz Ricker, the 160 by 120 grid of 5 m cells, the engine's own sponge. The page cannot run Python, so it computes what each program prints with the kernel itself, and the book's audit runs the real programs under Python and holds three things to the page: every printed line, every curve the program plots and, for the writer, every byte of the file. Plate (d) of the figure shows the first of these: each key number as the program prints it, the kernel's value to three more digits, and the difference in units of the last printed digit.
Program 1: A Convolutional Synthetic
The program is Sections 2.1 to 2.3 in about ninety lines. The shale's density comes from the rule of Gardner, Gardner and Gregory (1974), with in km/s, which gives 2.2521 g/cc at 2800 m/s; the impedances give the coefficient at the top of each sand, +0.0164 for brine, −0.0640 for oil and −0.1384 for gas, with an increase of impedance downward positive. The wavelet is the zero-phase Ricker of Ricker (1953), 81 taps of 1 ms for 30 Hz, with zero crossings at ±7.50 ms and a peak to trough time of 12.99 ms. The trace is np.convolve(r, w, mode="same"), the reflectivity convolved with the wavelet and trimmed to the length of the reflectivity.
A gas sand bed 20 ms thick, starting at 200 ms, has a top coefficient of −0.1384 and a base of +0.1384. The trace at the top reads −0.1626, 1.175 times the coefficient, because the base's opposite coefficient meets the trough of the Ricker and adds to it. The program then scans the thickness from 1 to 60 ms for the brightest top: 1.446 times the coefficient, at 13 ms, the peak to trough time. At 60 ms the two reflections are resolved and the top reads its own coefficient, 1.000; at 2 ms they nearly cancel and it reads 0.103 (Section 2.3).
Program 2: A Finite-Difference Run
The second program is the book's wave engine in numpy. It advances the 2D acoustic equation with the fourth-order stencil of Section 4.2 and a second-order leapfrog in time, with a Cerjan sponge on all four sides (Section 4.5), for the standard shale over a half-space of the standard gas sand. The Courant number is the book's 0.25, four time steps to a cell, against the stability limit of the fourth-order stencil, the limit of Lines, Slawinski and Bording (1998); Section 4.3 derives both. The source is the 30 Hz Ricker on one cell at (400 m, 100 m) and the receiver is at (600 m, 100 m).
The report is about accuracy as much as about the record. The Ricker's upper half-power frequency is 43.25 Hz, and Alford, Kelly and Boore (1974) ask for five points per wavelength there at fourth order. The wavelength to resolve is the shortest in the model, the gas sand's, 2342 m/s over 43.25 Hz: on 10 m cells it has 5.42 points, the numerical phase speed is −0.657 percent along a grid axis and +0.008 percent on a diagonal, and the largest cell the rule allows is 10.83 m. On 5 m cells there are 10.83 points; on 20 m cells, 2.71 points and a speed 9.87 percent low along an axis (Section 4.4). Both runs are stable all the same: stability and accuracy are separate limits. The program runs the cell you choose and the 5 m cell, and prints how far the first record is from the second: 16.36 percent of the rms on 10 m cells, 61.61 percent on 20 m cells.
On 10 m cells the direct wave peaks at +53.81 (in units of ) at 107.1 ms and the reflection at −4.55 at 192.0 ms, each late by the 33.0 ms at which the source's Ricker peaks. Their ratio is −0.085 here and −0.097 on 5 m cells; it is neither the velocity contrast's −0.089 nor a number to read as a coefficient, since the two waves have travelled 200 and 447 m from a source that is a line in 2D (Section 4.6). The engine has no density: it reflects by the velocity contrast alone.
The page and the program use the same arithmetic in different orders, and the audit holds the program's record to the kernel's SSMWave.makeSim to one part in 100,000 of its peak. The cost of a run is cells times steps times the nine points of the stencil: 86 million multiply-adds on 10 m cells and 368 million on 5 m cells, 851 steps of a Python loop that calls numpy on whole arrays. That is fine for a model like this one. For millions of cells and thousands of steps the loop, and not the arithmetic, is what a compiled code removes.
Program 3: A SEG-Y Writer
The third program writes the standard flat bed, the gas sand between 300 and 330 m in the standard shale, on the 160 by 120 grid of 5 m cells, as SEG-Y revision 1 (Norris and Faichney, 2002) using only the standard library and numpy, and reads it back from its bytes. The file is a 3200-byte textual header of forty lines of eighty characters, written in EBCDIC with .encode("cp037"), a 400-byte binary header, and for each of 160 traces a 240-byte header and 120 four-byte samples: 3200 + 400 + 160 × (240 + 120 × 4) = 118,800 bytes. The program puts each value at the standard's own byte number, counted from 1, big-endian: the sample interval at 3217, the samples per trace at 3221, the format code at 3225, the revision at 3501.
The control chooses the float format. IBM floats, code 1, have a 7-bit exponent of 16 and a 24-bit fraction, so the shale's 2800 m/s is stored as 43 AF 00 00; IEEE floats, code 5, store it as 45 2F 00 00. The program prints the first sample both ways: the IBM bytes read as an IEEE float are 350.0 m/s, and the IEEE bytes read as an IBM float are 192512.0. The format code is the only thing that says which the bytes are. The CRC-32 of the file is 37f7d160 as IBM and 5bd7b2d1 as IEEE; the audit writes the same model with SSM.segy, the writer of Section 9.5, and finds the same bytes. As there, the sample interval field holds the 5 m cell, which the standard defines in microseconds: this file's own convention, which its text header says in words.
Program 4: Crack Weaknesses to Weak HTI Parameters
The fourth program is a short piece of Part 8. One set of aligned dry cracks of density sits in a host with . Hudson's first-order theory (Hudson, 1981) gives the excess compliances and , which Equations 17 and 18 of Bakulin, Grechka and Tsvankin (2000) print as the weaknesses of Hudson's theory; their Equations 7 and 8 turn compliances into the weaknesses and , which stay between 0 and 1. The weak forms of Equations 31 to 33 then give the parameters of the HTI medium, to first order in the weaknesses:
The host here is the program's own choice, the standard gas sand with = 0.6757, so = 0.4566; it is not the host of Part 8. At a crack density of 0.030 the program prints = 0.1612 and = 0.0767, weaknesses = 0.1388 and = 0.0712, and = −0.0689, = −0.0760 and = −0.0356: all three negative, the crack-normal direction the slow one. Doubling the density does not double the weaknesses, since saturates: from 0.050 to 0.100 the normal weakness goes from 0.212 to 0.350, 1.65 times. The weak forms are first-order in the weaknesses, and the last row of the table the program prints, crack density 0.100, has parameters of −0.17 and −0.21: no longer weak, and beyond what first order should be asked to carry.
Beyond the Laptop: Devito, SPECFEM3D and Madagascar
The programs here run on a laptop, and the next order of size does not. Three packages are where a reader usually goes, and each does something different from the others. Devito is "an embedded domain-specific language for finite differences and geophysical exploration" (Louboutin and others, 2019): the user writes the partial differential equation symbolically, in Python, with the help of SymPy, and Devito generates, compiles and runs optimized C code for the stencil just in time. The methods it serves include full-waveform inversion and reverse-time migration; the finite-difference program above is the kind of stencil loop that a Devito operator writes for you. SPECFEM3D Cartesian is a different method: it is "based upon the spectral-element method" and simulates seismic wave propagation at the local or regional scale (the SPECFEM3D user manual; Komatitsch and Tromp, 1999). It is written in Fortran 2003 and uses MPI for parallel machines. Madagascar is, in the words of its title, an "open-source software project for multidimensional data analysis and reproducible computational experiments" (Fomel and others, 2013): a package designed for the analysis of large multidimensional data, in which a result is a script that anyone can run again. It keeps its data in its own RSF format, and how it reads a given SEG-Y file is a question for its own documentation, not for this book.
What stays the same on the way up is the model and the checks. A model leaves the page as a file, and Section 9.5 is what that file has to say; a run is only as good as the grid it is on, and the rule of five points per wavelength and the Courant number do not change with the code. The numbers the programs print here are the ones to reproduce first in any new package, before a larger model is run in it. Section 12.4, From the Lab to Research, looks further.
References
- Alford, R. M., Kelly, K. R., & Boore, D. M. (1974). Accuracy of finite-difference modeling of the acoustic wave equation. Geophysics, 39(6), 834–842.
- Bakulin, A., Grechka, V., & Tsvankin, I. (2000). Estimation of fracture parameters from reflection seismic data, Part I: HTI model due to a single fracture set. Geophysics, 65, 1788–1802.
- Cerjan, C., Kosloff, D., Kosloff, R., & Reshef, M. (1985). A nonreflecting boundary condition for discrete acoustic and elastic wave equations. Geophysics, 50(4), 705–708.
- Fomel, S., Sava, P., Vlad, I., Liu, Y., & Bashkardin, V. (2013). Madagascar: open-source software project for multidimensional data analysis and reproducible computational experiments. Journal of Open Research Software, 1(1), e8.
- Gardner, G. H. F., Gardner, L. W., & Gregory, A. R. (1974). Formation velocity and density: The diagnostic basics for stratigraphic traps. Geophysics, 39, 770–780.
- Harris, C. R., Millman, K. J., van der Walt, S. J., and others (2020). Array programming with NumPy. Nature, 585(7825), 357–362.
- Hudson, J. A. (1981). Wave speeds and attenuation of elastic waves in material containing cracks. Geophysical Journal of the Royal Astronomical Society, 64, 133–150.
- Komatitsch, D., & Tromp, J. (1999). Introduction to the spectral element method for three-dimensional seismic wave propagation. Geophysical Journal International, 139(3), 806–822.
- Lines, L. R., Slawinski, R., & Bording, R. P. (1998). A recipe for stability analysis of finite-difference wave equation computations. CREWES Research Report, 10.
- Louboutin, M., Lange, M., Luporini, F., Kukreja, N., Witte, P. A., Herrmann, F. J., Velesko, P., & Gorman, G. J. (2019). Devito (v3.1.0): an embedded domain-specific language for finite differences and geophysical exploration. Geoscientific Model Development, 12(3), 1165–1187.
- Norris, M. W., & Faichney, A. K. (Eds.) (2002). SEG Y rev 1 data exchange format, Release 1.0, May 2002. SEG Technical Standards Committee, Society of Exploration Geophysicists.
- Pyodide contributors. Pyodide documentation. https://pyodide.org
- Ricker, N. (1953). The form and laws of propagation of seismic wavelets. Geophysics, 18, 10–40.
- SPECFEM3D Cartesian user manual. https://specfem3d.readthedocs.io