Debian Codegraph @

doc/index.rst

raw
1 .. py:module:: skfmm
2
3 scikit-fmm documentation
4 ========================
5
6 :py:obj:`scikit-fmm` is a python extension module which implements the
7 fast marching method.
8
9 The fast marching method is used to model the evolution of boundaries
10 and interfaces in a variety of application areas.
11
12 More specifically, the fast marching method is a numerical technique for
13 finding approximate solutions to boundary value problems of the
14 Eikonal equation,
15
16 .. math::
17 F(x) | \nabla T(x) | = 1
18
19 Typically, such a problem describes the evolution of a closed curve as
20 a function of time :math:`T` with speed :math:`F(x)>0` in the normal
21 direction at a point x on the curve. The speed function is specified,
22 and the time at which the contour crosses a point x is obtained by
23 solving the equation. The initial location of the boundary is defined
24 by the zero contour (or zero level-set) of a scalar function.
25
26 In this document the scalar function containing the initial interface
27 location is referred to as phi. The scalar function phi can be thought to
28 exist in a dimension higher than the boundary of interest and only the
29 zero contour of the function is physically meaningful. The boundary
30 grows outward in the local normal direction at a speed given by
31 :math:`F(x)`.
32
33 :py:obj:`scikit-fmm` is a simple module which provides the functions:
34 :py:func:`distance`, :py:func:`travel_time` and
35 :py:func:`extension_velocities`. The import name of
36 :py:obj:`scikit-fmm` is :py:mod:`skfmm`.
37
38
39 Examples
40 ========
41 First, a simple example::
42
43 >>> import skfmm
44 >>> import numpy as np
45 >>> phi = np.ones((3, 3))
46 >>> phi[1, 1] = -1
47 >>> skfmm.distance(phi)
48 array([[ 1.20710678, 0.5 , 1.20710678],
49 [ 0.5 , -0.35355339, 0.5 ],
50 [ 1.20710678, 0.5 , 1.20710678]])
51
52 Here the zero contour of phi is around the (1, 1) point. The return
53 value of :py:func:`distance` gives the signed distance from zero
54 contour. No grid spacing is given, so it is taken as 1. To specify a
55 spacing use the optional dx argument::
56
57 >>> skfmm.distance(phi, dx=0.25)
58 array([[ 0.3017767 , 0.125 , 0.3017767 ],
59 [ 0.125 , -0.08838835, 0.125 ],
60 [ 0.3017767 , 0.125 , 0.3017767 ]])
61
62 A more detailed example:
63
64 .. image:: 2d_phi.png
65
66 The boundary is specified as the zero contour of a scalar function phi:
67
68 ::
69
70 >>> import numpy as np
71 >>> import pylab as pl
72 >>> X, Y = np.meshgrid(np.linspace(-1,1,200), np.linspace(-1,1,200))
73 >>> phi = -1 * np.ones_like(X)
74 >>> phi[X > -0.5] = 1
75 >>> phi[np.logical_and(np.abs(Y) < 0.25, X > -0.75)] = 1
76 >>> d = skfmm.distance(phi, dx=1e-2)
77
78 .. image:: 2d_phi_distance.png
79
80 :py:obj:`scikit-fmm` can also calculate travel times from an interface
81 given an array containing the interface propogation speed at each
82 point. Using the same initial interface position as above we set the
83 interface propagation speed to be 1.5 times greater in the upper half
84 of the domain.
85
86 ::
87
88 >>> speed = np.ones_like(X)
89 >>> speed[Y > 0] = 1.5
90 >>> t = skfmm.travel_time(phi, speed, dx=1e-2)
91
92 .. image:: 2d_phi_travel_time.png
93
94 Consider an obstacle within which the speed is vanishing. In principle this may
95 lead to singularities (division by zero) in the update algorithm. Therefore,
96 both :py:func:`travel_time` and :py:func:`distance` support masked arrays for
97 input. This allows an obstacle to be introduced. (Note that if the speed in a cell
98 is less than machine precision, a cell is masked internally to prevent division by 0.)
99
100 ::
101
102 >>> mask = np.logical_and(abs(X) < 0.1, abs(Y) < 0.5)
103 >>> phi = np.ma.MaskedArray(phi, mask)
104 >>> t = skfmm.travel_time(phi, speed, dx=1e-2)
105
106 .. image:: 2d_phi_travel_time_mask.png
107
108 The distance function, travel time or extension velocities can be
109 limited to with in a narrow band around the zero contour by specifying
110 the `narrow` keyword.
111
112 ::
113
114 >>> phi = -1 * np.ones_like(X)
115 >>> phi[X > -0.5] = 1
116 >>> phi[np.logical_and(np.abs(Y) < 0.25, X > -0.75)] = 1
117 >>> d = skfmm.distance(phi, dx=1e-2, narrow=0.3)
118
119 .. image:: 2d_phi_distance_narrow.png
120
121 The full example is in examples/2d_example.py.
122 :doc:`examples`
123
124
125
126 An example of using periodic boundary conditions.
127
128 ::
129
130 >>> X, Y = np.meshgrid(np.linspace(-1,1,501), np.linspace(-1,1,501))
131 >>> phi = (X+0.8)**2+(Y+0.8)**2 - 0.01
132 >>> speed = 1+X**2+Y**2
133 >>> skfmm.distance(phi, dx=2.0/500)
134 >>> skfmm.distance(phi, dx=2.0/500, periodic=True)
135 >>> skfmm.travel_time(phi, speed, dx=2.0/500, periodic=(1,0))
136
137 .. image:: periodic.png
138
139 The full example is in examples/boundaryconditions_example.py
140 :doc:`examples`
141
142 An example of using :py:obj:`scikit-fmm` to compute extension velocities.
143
144 ::
145
146 >>> N = 150
147 >>> X, Y = np.meshgrid(np.linspace(-1, 1, N), np.linspace(-1, 1, N))
148 >>> r = 1.75
149 >>> dx = 2.0 / (N - 1)
150 >>> phi = (X) ** 2 + (Y+1.85) ** 2 - r ** 2
151 >>> speed = X + 1.25
152 >>> d, f_ext = extension_velocities(phi, speed, dx)
153
154 .. image:: extension_velocity.png
155
156 The full example is in examples/extension_velocities_example.py.
157 :doc:`examples`
158
159 .. toctree::
160 :maxdepth: 2
161
162 examples
163 testing
164
165 Limitations:
166 ============
167 :py:obj:`scikit-fmm` only works for regular Cartesian grids, but grid cells may
168 have a different (uniform) length in each dimension.
169
170 Function Reference
171 ==================
172
173 .. autofunction:: distance
174
175 .. autofunction:: travel_time
176
177 .. autofunction:: extension_velocities
178
179 .. autoclass:: heap
180 :members:
181
182 Testing
183 =======
184
185 To run all the tests use
186
187 $ python -c "import skfmm; skfmm.test()"
188
189 See the full :doc:`testing`.