/[escript]/trunk/doc/cookbook/example07.tex
ViewVC logotype

Annotation of /trunk/doc/cookbook/example07.tex

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3386 - (hide annotations)
Thu Nov 25 05:05:50 2010 UTC (8 years, 2 months ago) by caltinay
File MIME type: application/x-tex
File size: 15551 byte(s)
scons choked on the newlines within the parameter list. Fixed.

1 ahallam 3003
2     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3     %
4     % Copyright (c) 2003-2010 by University of Queensland
5     % Earth Systems Science Computational Center (ESSCC)
6     % http://www.uq.edu.au/esscc
7     %
8     % Primary Business: Queensland, Australia
9     % Licensed under the Open Software License version 3.0
10     % http://www.opensource.org/licenses/osl-3.0.php
11     %
12     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13    
14     The acoustic wave equation governs the propagation of pressure waves. Wave
15     types that obey this law tend to travel in liquids or gases where shear waves
16 ahallam 3004 or longitudinal style wave motion is not possible. An obvious example is sound
17 ahallam 3003 waves.
18    
19 ahallam 3004 The acoustic wave equation is defined as;
20 ahallam 3003 \begin{equation}
21     \nabla ^2 p - \frac{1}{c^2} \frac{\partial ^2 p}{\partial t^2} = 0
22     \label{eqn:acswave}
23     \end{equation}
24 ahallam 3370 where $p$ is the pressure, $t$ is the time and $c$ is the wave velocity. In this
25     chapter the acoustic wave equation is demonstrated. Important steps include the
26     translation of the Laplacian $\nabla^2$ to the \esc general form, the stiff
27 ahallam 3373 equation stability criterion and solving for the displacement or acceleration solution.
28 ahallam 3003
29 ahallam 3004 \section{The Laplacian in \esc}
30 ahallam 3373 The Laplacian operator which can be written as $\Delta$ or $\nabla^2$, is
31 ahallam 3370 calculated via the divergence of the gradient of the object, which in this
32     example is the scalar $p$. Thus we can write;
33 ahallam 3004 \begin{equation}
34 ahallam 3029 \nabla^2 p = \nabla \cdot \nabla p =
35 jfenwick 3308 \sum_{i}^n
36     \frac{\partial^2 p}{\partial x^2_{i}}
37 ahallam 3004 \label{eqn:laplacian}
38     \end{equation}
39 ahallam 3232 For the two dimensional case in Cartesian coordinates \autoref{eqn:laplacian}
40 ahallam 3004 becomes;
41     \begin{equation}
42     \nabla^2 p = \frac{\partial^2 p}{\partial x^2}
43     + \frac{\partial^2 p}{\partial y^2}
44     \end{equation}
45 ahallam 3003
46 ahallam 3004 In \esc the Laplacian is calculated using the divergence representation and the
47 ahallam 3370 intrinsic functions \textit{grad()} and \textit{trace()}. The function
48 ahallam 3004 \textit{grad{}} will return the spatial gradients of an object.
49     For a rank 0 solution, this is of the form;
50     \begin{equation}
51     \nabla p = \left[
52 jfenwick 3308 \frac{\partial p}{\partial x _{0}},
53     \frac{\partial p}{\partial x _{1}}
54 ahallam 3004 \right]
55     \label{eqn:grad}
56     \end{equation}
57     Larger ranked solution objects will return gradient tensors. For example, a
58 jfenwick 3308 pressure field which acts in the directions $p _{0}$ and $p
59     _{1}$ would return;
60 ahallam 3004 \begin{equation}
61     \nabla p = \begin{bmatrix}
62 jfenwick 3308 \frac{\partial p _{0}}{\partial x _{0}} &
63     \frac{\partial p _{1}}{\partial x _{0}} \\
64     \frac{\partial p _{0}}{\partial x _{1}} &
65     \frac{\partial p _{1}}{\partial x _{1}}
66 ahallam 3004 \end{bmatrix}
67     \label{eqn:gradrank1}
68     \end{equation}
69 ahallam 3003
70 ahallam 3232 \autoref{eqn:grad} corresponds to the Linear PDE general form value
71 ahallam 3373 $X$. Notice however, that the general form contains the term $X
72 jfenwick 3308 _{i,j}$\footnote{This is the first derivative in the $j^{th}$
73 ahallam 3029 direction for the $i^{th}$ component of the solution.},
74 ahallam 3370 hence for a rank 0 object there is no need to do more then calculate the
75 ahallam 3004 gradient and submit it to the solver. In the case of the rank 1 or greater
76 ahallam 3373 object, it is also necessary to calculate the trace. This is the sum of the
77 ahallam 3232 diagonal in \autoref{eqn:gradrank1}.
78 ahallam 3004
79     Thus when solving for equations containing the Laplacian one of two things must
80 ahallam 3370 be completed. If the object \verb!p! is less then rank 1 the gradient is
81 ahallam 3004 calculated via;
82 ahallam 3025 \begin{python}
83 ahallam 3063 gradient=grad(p)
84 ahallam 3025 \end{python}
85 ahallam 3370 and if the object is greater then or equal to a rank 1 tensor, the trace of
86 ahallam 3004 the gradient is calculated.
87 ahallam 3025 \begin{python}
88 ahallam 3004 gradient=trace(grad(p))
89 ahallam 3025 \end{python}
90 ahallam 3370 These values can then be submitted to the PDE solver via the general form term
91 ahallam 3004 $X$. The Laplacian is then computed in the solution process by taking the
92     divergence of $X$.
93    
94 ahallam 3025 Note, if you are unsure about the rank of your tensor, the \textit{getRank}
95     command will return the rank of the PDE object.
96     \begin{python}
97     rank = p.getRank()
98     \end{python}
99    
100    
101     \section{Numerical Solution Stability} \label{sec:nsstab}
102 ahallam 3004 Unfortunately, the wave equation belongs to a class of equations called
103     \textbf{stiff} PDEs. These types of equations can be difficult to solve
104 ahallam 3373 numerically as they tend to oscillate about the exact solution, which can
105 ahallam 3370 eventually lead to a catastrophic failure. To counter this problem, explicitly
106 ahallam 3373 stable schemes like the backwards Euler method, and correct parameterisation of
107 ahallam 3370 the problem are required.
108    
109     There are two variables which must be considered for
110     stability when numerically trying to solve the wave equation. For linear media,
111     the two variables are related via;
112 ahallam 3004 \begin{equation} \label{eqn:freqvel}
113     f=\frac{v}{\lambda}
114     \end{equation}
115 ahallam 3025 The velocity $v$ that a wave travels in a medium is an important variable. For
116 ahallam 3370 stability the analytical wave must not propagate faster then the numerical wave
117     is able to, and in general, needs to be much slower then the numerical wave.
118 ahallam 3003 For example, a line 100m long is discretised into 1m intervals or 101 nodes. If
119     a wave enters with a propagation velocity of 100m/s then the travel time for
120     the wave between each node will be 0.01 seconds. The time step, must therefore
121 ahallam 3370 be significantly less then this. Of the order $10E-4$ would be appropriate.
122 ahallam 3384 This stability criterion is known as the Courant–Friedrichs–Lewy
123     condition given by
124     \begin{equation}
125     dt=f\cdot \frac{dx}{v}
126     \end{equation}
127     where $dx$ is the mesh size and $f$ is a safety factor. To obtain a time step of
128     $10E-4$, a safety factor of $f=0.1$ was used.
129 ahallam 3003
130 ahallam 3004 The wave frequency content also plays a part in numerical stability. The
131     nyquist-sampling theorem states that a signals bandwidth content will be
132 jfenwick 3308 accurately represented when an equispaced sampling rate $f _{n}$ is
133 ahallam 3370 equal to or greater then twice the maximum frequency of the signal
134 jfenwick 3308 $f_{s}$, or;
135 ahallam 3004 \begin{equation} \label{eqn:samptheorem}
136 jfenwick 3308 f_{n} \geqslant f_{s}
137 ahallam 3004 \end{equation}
138 ahallam 3384 For example, a 50Hz signal will require a sampling rate greater then 100Hz or
139 ahallam 3004 one sample every 0.01 seconds. The wave equation relies on a spatial frequency,
140 ahallam 3373 thus the sampling theorem in this case applies to the solution mesh spacing.
141     This relationship confirms that the frequency content of the input signal
142     directly affects the time discretisation of the problem.
143 ahallam 3004
144     To accurately model the wave equation with high resolutions and velocities
145     means that very fine spatial and time discretisation is necessary for most
146 ahallam 3384 problems. This requirement makes the wave equation arduous to
147 ahallam 3003 solve numerically due to the large number of time iterations required in each
148 ahallam 3004 solution. Models with very high velocities and frequencies will be the worst
149 ahallam 3029 affected by this problem.
150 ahallam 3003
151     \section{Displacement Solution}
152     \sslist{example07a.py}
153    
154     We begin the solution to this PDE with the centred difference formula for the
155     second derivative;
156     \begin{equation}
157     f''(x) \approx \frac{f(x+h - 2f(x) + f(x-h)}{h^2}
158     \label{eqn:centdiff}
159     \end{equation}
160 ahallam 3232 substituting \autoref{eqn:centdiff} for $\frac{\partial ^2 p }{\partial t ^2}$
161     in \autoref{eqn:acswave};
162 ahallam 3003 \begin{equation}
163 jfenwick 3308 \nabla ^2 p - \frac{1}{c^2h^2} \left[p_{(t+1)} - 2p_{(t)} +
164     p_{(t-1)} \right]
165 ahallam 3003 = 0
166     \label{eqn:waveu}
167     \end{equation}
168     Rearranging for $p_{(t+1)}$;
169     \begin{equation}
170 jfenwick 3308 p_{(t+1)} = c^2 h^2 \nabla ^2 p_{(t)} +2p_{(t)} -
171     p_{(t-1)}
172 ahallam 3003 \end{equation}
173     this can be compared with the general form of the \modLPDE module and it
174 jfenwick 3308 becomes clear that $D=1$, $X_{i,j}=-c^2 h^2 \nabla ^2 p_{(t)}$ and
175 ahallam 3004 $Y=2p_{(t)} - p_{(t-1)}$.
176 ahallam 3003
177 ahallam 3025 The solution script is similar to others that we have created in previous
178 ahallam 3004 chapters. The general steps are;
179     \begin{enumerate}
180     \item The necessary libraries must be imported.
181     \item The domain needs to be defined.
182     \item The time iteration and control parameters need to be defined.
183     \item The PDE is initialised with source and boundary conditions.
184     \item The time loop is started and the PDE is solved at consecutive time steps.
185 ahallam 3370 \item All or select solutions are saved to file for visualisation later on.
186 ahallam 3004 \end{enumerate}
187    
188     Parts of the script which warrant more attention are the definition of the
189     source, visualising the source, the solution time loop and the VTK data export.
190    
191     \subsection{Pressure Sources}
192     As the pressure is a scalar, one need only define the pressure for two
193     time steps prior to the start of the solution loop. Two known solutions are
194     required because the wave equation contains a double partial derivative with
195     respect to time. This is often a good opportunity to introduce a source to the
196     solution. This model has the source located at it's centre. The source should
197     be smooth and cover a number of samples to satisfy the frequency stability
198 ahallam 3370 criterion. Small sources will generate high frequency signals. Here, when using
199     a rectangular domain, the source is defined by a cosine function.
200 ahallam 3025 \begin{python}
201 ahallam 3004 U0=0.01 # amplitude of point source
202     xc=[500,500] #location of point source
203     # define small radius around point xc
204     src_radius = 30
205     # for first two time steps
206 ahallam 3025 u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*\
207     whereNegative(length(x-xc)-src_radius)
208 ahallam 3004 u_m1=u
209 ahallam 3025 \end{python}
210 ahallam 3004
211 ahallam 3025 \subsection{Visualising the Source}
212     There are two options for visualising the source. The first is to export the
213     initial conditions of the model to VTK, which can be interpreted as a scalar
214 ahallam 3370 surface in Mayavi2. The second is to take a cross section of the model which
215     will require the \textit{Locator} function.
216 ahallam 3063 First \verb!Locator! must be imported;
217 ahallam 3025 \begin{python}
218     from esys.escript.pdetools import Locator
219     \end{python}
220     The function can then be used on the domain to locate the nearest domain node
221     to the point or points of interest.
222    
223     It is now necessary to build a list of $(x,y)$ locations that specify where are
224 ahallam 3370 model slice will go. This is easily implemented with a loop;
225 ahallam 3025 \begin{python}
226     cut_loc=[]
227     src_cut=[]
228     for i in range(ndx/2-ndx/10,ndx/2+ndx/10):
229     cut_loc.append(xstep*i)
230     src_cut.append([xstep*i,xc[1]])
231     \end{python}
232 ahallam 3063 We then submit the output to \verb!Locator! and finally return the appropriate
233     values using the \verb!getValue! function.
234 ahallam 3025 \begin{python}
235 ahallam 3373 src=Locator(mydomain,src_cut)
236 ahallam 3025 src_cut=src.getValue(u)
237     \end{python}
238 ahallam 3370 It is then a trivial task to plot and save the output using \mpl
239     (\autoref{fig:cxsource}).
240 ahallam 3025 \begin{python}
241 ahallam 3373 pl.plot(cut_loc,src_cut)
242 ahallam 3025 pl.axis([xc[0]-src_radius*3,xc[0]+src_radius*3,0.,2*U0])
243     pl.savefig(os.path.join(savepath,"source_line.png"))
244     \end{python}
245     \begin{figure}[h]
246 ahallam 3029 \centering
247 ahallam 3063 \includegraphics[width=6in]{figures/sourceline.png}
248 ahallam 3025 \caption{Cross section of the source function.}
249     \label{fig:cxsource}
250     \end{figure}
251    
252    
253     \subsection{Point Monitoring}
254     In the more general case where the solution mesh is irregular or specific
255     locations need to be monitored, it is simple enough to use the \textit{Locator}
256     function.
257     \begin{python}
258     rec=Locator(mydomain,[250.,250.])
259     \end{python}
260 ahallam 3029 When the solution \verb u is updated we can extract the value at that point
261 ahallam 3025 via;
262     \begin{python}
263     u_rec=rec.getValue(u)
264     \end{python}
265 ahallam 3063 For consecutive time steps one can record the values from \verb!u_rec! in an
266     array initialised as \verb!u_rec0=[]! with;
267 ahallam 3025 \begin{python}
268     u_rec0.append(rec.getValue(u))
269     \end{python}
270    
271     It can be useful to monitor the value at a single or multiple individual points
272     in the model during the modelling process. This is done using
273 ahallam 3063 the \verb!Locator! function.
274 ahallam 3025
275    
276 ahallam 3003 \section{Acceleration Solution}
277     \sslist{example07b.py}
278    
279 ahallam 3373 An alternative method to the displacement solution, is to solve for the
280     acceleration $\frac{\partial ^2 p}{\partial t^2}$ directly. The displacement can
281     then be derived from the acceleration after a solution has been calculated
282     The acceleration is given by a modified form of \autoref{eqn:waveu};
283 ahallam 3003 \begin{equation}
284     \nabla ^2 p - \frac{1}{c^2} a = 0
285     \label{eqn:wavea}
286     \end{equation}
287 jfenwick 3308 and can be solved directly with $Y=0$ and $X=-c^2 \nabla ^2 p_{(t)}$.
288 ahallam 3003 After each iteration the displacement is re-evaluated via;
289     \begin{equation}
290 jfenwick 3308 p_{(t+1)}=2p_{(t)} - p_{(t-1)} + h^2a
291 ahallam 3003 \end{equation}
292    
293 ahallam 3029 \subsection{Lumping}
294 ahallam 3004 For \esc, the acceleration solution is prefered as it allows the use of matrix
295     lumping. Lumping or mass lumping as it is sometimes known, is the process of
296     aggressively approximating the density elements of a mass matrix into the main
297     diagonal. The use of Lumping is motivaed by the simplicity of diagonal matrix
298     inversion. As a result, Lumping can significantly reduce the computational
299 ahallam 3029 requirements of a problem. Care should be taken however, as this
300     function can only be used when the $A$, $B$ and $C$ coefficients of the
301     general form are zero.
302 ahallam 3003
303 ahallam 3384 More information about the lumping implementation used in \esc and its accuracy
304     can be found in the user guide.
305    
306 ahallam 3004 To turn lumping on in \esc one can use the command;
307 ahallam 3025 \begin{python}
308 ahallam 3384 mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().HRZ_LUMPING)
309 ahallam 3025 \end{python}
310 ahallam 3004 It is also possible to check if lumping is set using;
311 ahallam 3025 \begin{python}
312 ahallam 3004 print mypde.isUsingLumping()
313 ahallam 3025 \end{python}
314 ahallam 3004
315     \section{Stability Investigation}
316     It is now prudent to investigate the stability limitations of this problem.
317 ahallam 3025 First, we let the frequency content of the source be very small. If we define
318 ahallam 3370 the source as a cosine input, then the wavlength of the input is equal to the
319 ahallam 3025 radius of the source. Let this value be 5 meters. Now, if the maximum velocity
320 ahallam 3373 of the model is $c=380.0ms^{-1}$, then the source
321 jfenwick 3308 frequency is $f_{r} = \frac{380.0}{5} = 76.0 Hz$. This is a worst case
322 ahallam 3025 scenario with a small source and the models maximum velocity.
323    
324 ahallam 3232 Furthermore, we know from \autoref{sec:nsstab}, that the spatial sampling
325 ahallam 3025 frequency must be at least twice this value to ensure stability. If we assume
326     the model mesh is a square equispaced grid,
327     then the sampling interval is the side length divided by the number of samples,
328     given by $\Delta x = \frac{1000.0m}{400} = 2.5m$ and the maximum sampling
329     frequency capable at this interval is
330 jfenwick 3308 $f_{s}=\frac{380.0ms^{-1}}{2.5m}=152Hz$ this is just equal to the
331 ahallam 3232 required rate satisfying \autoref{eqn:samptheorem}.
332 ahallam 3004
333 ahallam 3232 \autoref{fig:ex07sampth} depicts three examples where the grid has been
334 ahallam 3025 undersampled, sampled correctly, and over sampled. The grids used had
335     200, 400 and 800 nodes per side respectively. Obviously, the oversampled grid
336     retains the best resolution of the modelled wave.
337 ahallam 3004
338 ahallam 3025 The time step required for each of these examples is simply calculated from
339     the propagation requirement. For a maximum velocity of $380.0ms^{-1}$,
340     \begin{subequations}
341     \begin{equation}
342     \Delta t \leq \frac{1000.0m}{200} \frac{1}{380.0} = 0.013s
343     \end{equation}
344     \begin{equation}
345     \Delta t \leq \frac{1000.0m}{400} \frac{1}{380.0} = 0.0065s
346     \end{equation}
347     \begin{equation}
348     \Delta t \leq \frac{1000.0m}{800} \frac{1}{380.0} = 0.0032s
349     \end{equation}
350     \end{subequations}
351 ahallam 3373 Observe that for each doubling of the number of nodes in the mesh, we halve
352     the time step. To illustrate the impact this has, consider our model. If the
353 ahallam 3025 source is placed at the center, it is $500m$ from the nearest boundary. With a
354     velocity of $380.0ms^{-1}$ it will take $\approx1.3s$ for the wavefront to
355     reach that boundary. In each case, this equates to $100$, $200$ and $400$ time
356     steps. This is again, only a best case scenario, for true stability these time
357 ahallam 3373 values may need to be halved and possibly halved again.
358 ahallam 3004
359 ahallam 3025 \begin{figure}[ht]
360     \centering
361     \subfigure[Undersampled Example]{
362 caltinay 3386 \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07usamp.png}
363 ahallam 3025 \label{fig:ex07usamp}
364     }
365     \subfigure[Just sampled Example]{
366 caltinay 3386 \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07jsamp.png}
367 ahallam 3025 \label{fig:ex07jsamp}
368     }
369     \subfigure[Over sampled Example]{
370 caltinay 3386 \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07nsamp.png}
371 ahallam 3025 \label{fig:ex07nsamp}
372     }
373 caltinay 3386 \caption{Sampling Theorem example for stability investigation}
374 ahallam 3373 \label{fig:ex07sampth}
375 ahallam 3025 \end{figure}
376 ahallam 3004

  ViewVC Help
Powered by ViewVC 1.1.26