# Diff of /trunk/doc/cookbook/example07.tex

revision 3054 by ahallam, Wed Jun 30 02:22:25 2010 UTC revision 3373 by ahallam, Tue Nov 23 00:29:07 2010 UTC
# Line 23  The acoustic wave equation is defined as Line 23  The acoustic wave equation is defined as
23   \nabla ^2 p - \frac{1}{c^2} \frac{\partial ^2 p}{\partial t^2} = 0   \nabla ^2 p - \frac{1}{c^2} \frac{\partial ^2 p}{\partial t^2} = 0
24  \label{eqn:acswave}  \label{eqn:acswave}
25
26  where $p$ is the pressure, $t$ is the time and $c$ is the wave velocity.  where $p$ is the pressure, $t$ is the time and $c$ is the wave velocity. In this
27    chapter the acoustic wave equation is demonstrated. Important steps include the
28    translation of the Laplacian $\nabla^2$ to the \esc general form, the stiff
29    equation stability criterion and solving for the displacement or acceleration solution.
30
31  \section{The Laplacian in \esc}  \section{The Laplacian in \esc}
32  The Laplacian opperator which can be written as $\Delta$ or $\nabla^2$  is  The Laplacian operator which can be written as $\Delta$ or $\nabla^2$,  is
33  calculated via the divergence of the gradient of the object, which is in this  calculated via the divergence of the gradient of the object, which in this
34  example $p$. Thus we can write;  example is the scalar $p$. Thus we can write;
35
36   \nabla^2 p = \nabla \cdot \nabla p =   \nabla^2 p = \nabla \cdot \nabla p =
37      \sum\hackscore{i}^n      \sum_{i}^n
38      \frac{\partial^2 p}{\partial x^2\hackscore{i}}      \frac{\partial^2 p}{\partial x^2_{i}}
39   \label{eqn:laplacian}   \label{eqn:laplacian}
40
41  For the two dimensional case in Cartesian coordinates \refEq{eqn:laplacian}  For the two dimensional case in Cartesian coordinates \autoref{eqn:laplacian}
42  becomes;  becomes;
43
44   \nabla^2 p = \frac{\partial^2 p}{\partial x^2}   \nabla^2 p = \frac{\partial^2 p}{\partial x^2}
# Line 43  becomes; Line 46  becomes;
46
47
48  In \esc the Laplacian is calculated using the divergence representation and the  In \esc the Laplacian is calculated using the divergence representation and the
49  intrinsic functions \textit{grad()} and \textit{trace()}. The fucntion  intrinsic functions \textit{grad()} and \textit{trace()}. The function
51  For a rank 0 solution, this is of the form;  For a rank 0 solution, this is of the form;
52
53   \nabla p = \left[   \nabla p = \left[
54         \frac{\partial p}{\partial x \hackscore{0}},           \frac{\partial p}{\partial x _{0}},
55         \frac{\partial p}{\partial x \hackscore{1}}         \frac{\partial p}{\partial x _{1}}
56                    \right]                    \right]
58
59  Larger ranked solution objects will return gradient tensors. For example, a  Larger ranked solution objects will return gradient tensors. For example, a
60  pressure field which acts in the directions $p \hackscore{0}$ and $p pressure field which acts in the directions$p _{0}$and$p
61  \hackscore{1}$would return; _{1}$ would return;
62
63    \nabla p = \begin{bmatrix}    \nabla p = \begin{bmatrix}
64         \frac{\partial p \hackscore{0}}{\partial x \hackscore{0}} &         \frac{\partial p _{0}}{\partial x _{0}} &
65          \frac{\partial p \hackscore{1}}{\partial x \hackscore{0}} \\          \frac{\partial p _{1}}{\partial x _{0}} \\
66        \frac{\partial p \hackscore{0}}{\partial x \hackscore{1}} &        \frac{\partial p _{0}}{\partial x _{1}} &
67          \frac{\partial p \hackscore{1}}{\partial x \hackscore{1}}          \frac{\partial p _{1}}{\partial x _{1}}
68                    \end{bmatrix}                    \end{bmatrix}
70
71
72  \refEq{eqn:grad} corresponds to the Linear PDE general form value  \autoref{eqn:grad} corresponds to the Linear PDE general form value
73  $X$. Notice however that the general form contains the term $X$X$. Notice however, that the general form contains the term$X
74  \hackscore{i,j}$\footnote{This is the first derivative in the$j^{th}$_{i,j}$\footnote{This is the first derivative in the $j^{th}$
75  direction for the $i^{th}$ component of the solution.},  direction for the $i^{th}$ component of the solution.},
76  hence for a rank 0 object there is no need to do more than calculate the  hence for a rank 0 object there is no need to do more then calculate the
77  gradient and submit it to the solver. In the case of the rank 1 or greater  gradient and submit it to the solver. In the case of the rank 1 or greater
78  object, it is nesscary to calculate the trace also. This is the sum of the  object, it is also necessary to calculate the trace. This is the sum of the
80
81  Thus when solving for equations containing the Laplacian one of two things must  Thus when solving for equations containing the Laplacian one of two things must
82  be completed. If the object \verb p   is less than rank 1 the gradient is  be completed. If the object \verb!p! is less then rank 1 the gradient is
83  calculated via;  calculated via;
84  \begin{python}  \begin{python}
86  \end{python}  \end{python}
87  and if the object is greater thank or equal to a rank 1 tensor, the trace of  and if the object is greater then or equal to a rank 1 tensor, the trace of
89  \begin{python}  \begin{python}
91  \end{python}  \end{python}
92  These valuse can then be submitted to the PDE solver via the general form term  These values can then be submitted to the PDE solver via the general form term
93  $X$. The Laplacian is then computed in the solution process by taking the  $X$. The Laplacian is then computed in the solution process by taking the
94  divergence of $X$.  divergence of $X$.
95
# Line 100  command will return the rank of the PDE Line 103  command will return the rank of the PDE
103  \section{Numerical Solution Stability} \label{sec:nsstab}  \section{Numerical Solution Stability} \label{sec:nsstab}
104  Unfortunately, the wave equation belongs to a class of equations called  Unfortunately, the wave equation belongs to a class of equations called
105  \textbf{stiff} PDEs. These types of equations can be difficult to solve  \textbf{stiff} PDEs. These types of equations can be difficult to solve
106  numerically as they tend to oscilate about the exact solution which can  numerically as they tend to oscillate about the exact solution, which can
107  eventually lead to a catastrophic failure in the solution. To counter this  eventually lead to a catastrophic failure. To counter this problem, explicitly
108  problem, explicitly stable schemes like  stable schemes like the backwards Euler method, and correct parameterisation of
109  the backwards Euler method are required. There are two variables which must be  the problem are required.
110  considered for stability when numerically trying to solve the wave equation.
111  For linear media, the two variables are related via;  There are two variables which must be considered for
112    stability when numerically trying to solve the wave equation. For linear media,
113    the two variables are related via;
114   \label{eqn:freqvel}   \label{eqn:freqvel}
115  f=\frac{v}{\lambda}  f=\frac{v}{\lambda}
116
117  The velocity $v$ that a wave travels in a medium is an important variable. For  The velocity $v$ that a wave travels in a medium is an important variable. For
118  stability the analytical wave must not propagate faster than the numerical wave  stability the analytical wave must not propagate faster then the numerical wave
119  is able to, and in general, needs to be much slower than the numerical wave.  is able to, and in general, needs to be much slower then the numerical wave.
120  For example, a line 100m long is discretised into 1m intervals or 101 nodes. If  For example, a line 100m long is discretised into 1m intervals or 101 nodes. If
121  a wave enters with a propagation velocity of 100m/s then the travel time for  a wave enters with a propagation velocity of 100m/s then the travel time for
122  the wave between each node will be 0.01 seconds. The time step, must therefore  the wave between each node will be 0.01 seconds. The time step, must therefore
123  be significantly less than this. Of the order $10E-4$ would be appropriate.  be significantly less then this. Of the order $10E-4$ would be appropriate.
124
125  The wave frequency content also plays a part in numerical stability. The  The wave frequency content also plays a part in numerical stability. The
126  nyquist-sampling theorem states that a signals bandwidth content will be  nyquist-sampling theorem states that a signals bandwidth content will be
127  accurately represented when an equispaced sampling rate $f \hackscore{n}$ is  accurately represented when an equispaced sampling rate $f _{n}$ is
128  equal to or greater than twice the maximum frequency of the signal  equal to or greater then twice the maximum frequency of the signal
129  $f\hackscore{s}$, or;  $f_{s}$, or;
130   \label{eqn:samptheorem}   \label{eqn:samptheorem}
131   f\hackscore{n} \geqslant f\hackscore{s}   f_{n} \geqslant f_{s}
132
133  For example a 50Hz signal will require a sampling rate greater than 100Hz or  For example a 50Hz signal will require a sampling rate greater then 100Hz or
134  one sample every 0.01 seconds. The wave equation relies on a spatial frequency,  one sample every 0.01 seconds. The wave equation relies on a spatial frequency,
135  thus the sampling theorem in this case applies to the solution mesh spacing. In  thus the sampling theorem in this case applies to the solution mesh spacing.
136  this way, the frequency content of the input signal directly affects the time  This relationship confirms that the frequency content of the input signal
137  discretisation of the problem.    directly affects the time discretisation of the problem.
138
139  To accurately model the wave equation with high resolutions and velocities  To accurately model the wave equation with high resolutions and velocities
140  means that very fine spatial and time discretisation is necessary for most  means that very fine spatial and time discretisation is necessary for most
# Line 148  second derivative; Line 153  second derivative;
153   f''(x) \approx \frac{f(x+h - 2f(x) + f(x-h)}{h^2}   f''(x) \approx \frac{f(x+h - 2f(x) + f(x-h)}{h^2}
154  \label{eqn:centdiff}  \label{eqn:centdiff}
155
156  substituting \refEq{eqn:centdiff} for $\frac{\partial ^2 p }{\partial t ^2}$  substituting \autoref{eqn:centdiff} for $\frac{\partial ^2 p }{\partial t ^2}$
157  in \refEq{eqn:acswave};  in \autoref{eqn:acswave};
158
159   \nabla ^2 p - \frac{1}{c^2h^2} \left[p\hackscore{(t+1)} - 2p\hackscore{(t)} +   \nabla ^2 p - \frac{1}{c^2h^2} \left[p_{(t+1)} - 2p_{(t)} +
160  p\hackscore{(t-1)} \right]  p_{(t-1)} \right]
161  = 0  = 0
162  \label{eqn:waveu}  \label{eqn:waveu}
163
164  Rearranging for $p_{(t+1)}$;  Rearranging for $p_{(t+1)}$;
165
166   p\hackscore{(t+1)} = c^2 h^2 \nabla ^2 p\hackscore{(t)} +2p\hackscore{(t)} -   p_{(t+1)} = c^2 h^2 \nabla ^2 p_{(t)} +2p_{(t)} -
167  p\hackscore{(t-1)}  p_{(t-1)}
168
169  this can be compared with the general form of the \modLPDE module and it  this can be compared with the general form of the \modLPDE module and it
170  becomes clear that $D=1$, $X\hackscore{i,j}=-c^2 h^2 \nabla ^2 p_{(t)}$ and  becomes clear that $D=1$, $X_{i,j}=-c^2 h^2 \nabla ^2 p_{(t)}$ and
171  $Y=2p_{(t)} - p_{(t-1)}$.  $Y=2p_{(t)} - p_{(t-1)}$.
172
173  The solution script is similar to others that we have created in previous  The solution script is similar to others that we have created in previous
# Line 173  chapters. The general steps are; Line 178  chapters. The general steps are;
178   \item The time iteration and control parameters need to be defined.   \item The time iteration and control parameters need to be defined.
179   \item The PDE is initialised with source and boundary conditions.   \item The PDE is initialised with source and boundary conditions.
180   \item The time loop is started and the PDE is solved at consecutive time steps.   \item The time loop is started and the PDE is solved at consecutive time steps.
181   \item All or select solutions are saved to file for visualisation lated on.   \item All or select solutions are saved to file for visualisation later on.
182  \end{enumerate}  \end{enumerate}
183
184  Parts of the script which warrant more attention are the definition of the  Parts of the script which warrant more attention are the definition of the
# Line 186  required because the wave equation conta Line 191  required because the wave equation conta
191  respect to time. This is often a good opportunity to introduce a source to the  respect to time. This is often a good opportunity to introduce a source to the
192  solution. This model has the source located at it's centre. The source should  solution. This model has the source located at it's centre. The source should
193  be smooth and cover a number of samples to satisfy the frequency stability  be smooth and cover a number of samples to satisfy the frequency stability
194  criterion. Small sources will generate high frequency signals. Here, the source  criterion. Small sources will generate high frequency signals. Here, when using
195  is defined by a cosine function.  a rectangular domain, the source is defined by a cosine function.
196  \begin{python}  \begin{python}
197  U0=0.01 # amplitude of point source  U0=0.01 # amplitude of point source
198  xc=[500,500] #location of point source  xc=[500,500] #location of point source
204  u_m1=u  u_m1=u
205  \end{python}  \end{python}
When using a rectangular domain
206
207  \subsection{Visualising the Source}  \subsection{Visualising the Source}
208  There are two options for visualising the source. The first is to export the  There are two options for visualising the source. The first is to export the
209  initial conditions of the model to VTK, which can be interpreted as a scalar  initial conditions of the model to VTK, which can be interpreted as a scalar
210  suface in mayavi. The second is to take a cross section of the model.  surface in Mayavi2. The second is to take a cross section of the model which
211    will require the \textit{Locator} function.
212  For the later, we will require the \textit{Locator} function.  First \verb!Locator! must be imported;
First \verb Locator  must be imported;
213  \begin{python}  \begin{python}
214   from esys.escript.pdetools import Locator   from esys.escript.pdetools import Locator
215  \end{python}  \end{python}
# Line 214  The function can then be used on the dom Line 217  The function can then be used on the dom
217  to the point or points of interest.  to the point or points of interest.
218
219  It is now necessary to build a list of $(x,y)$ locations that specify where are  It is now necessary to build a list of $(x,y)$ locations that specify where are
220  model slice will go. This is easily implemeted with a loop;  model slice will go. This is easily implemented with a loop;
221  \begin{python}  \begin{python}
222  cut_loc=[]  cut_loc=[]
223  src_cut=[]  src_cut=[]
# Line 222  for i in range(ndx/2-ndx/10,ndx/2+ndx/10 Line 225  for i in range(ndx/2-ndx/10,ndx/2+ndx/10
225      cut_loc.append(xstep*i)      cut_loc.append(xstep*i)
226      src_cut.append([xstep*i,xc[1]])      src_cut.append([xstep*i,xc[1]])
227  \end{python}  \end{python}
228  We then submit the output to \verb Locator  and finally return the appropriate  We then submit the output to \verb!Locator! and finally return the appropriate
229  values using the \verb getValue  function.  values using the \verb!getValue! function.
230  \begin{python}  \begin{python}
231   src=Locator(mydomain,src_cut)  src=Locator(mydomain,src_cut)
232  src_cut=src.getValue(u)  src_cut=src.getValue(u)
233  \end{python}  \end{python}
234  It is then a trivial task to plot and save the output using \mpl.  It is then a trivial task to plot and save the output using \mpl
235    (\autoref{fig:cxsource}).
236  \begin{python}  \begin{python}
237   pl.plot(cut_loc,src_cut)  pl.plot(cut_loc,src_cut)
239  pl.savefig(os.path.join(savepath,"source_line.png"))  pl.savefig(os.path.join(savepath,"source_line.png"))
240  \end{python}  \end{python}
241  \begin{figure}[h]  \begin{figure}[h]
242   \centering   \centering
% \includegraphics[width=6in]{figures/sourceline.png}
244   \caption{Cross section of the source function.}   \caption{Cross section of the source function.}
245   \label{fig:cxsource}   \label{fig:cxsource}
246  \end{figure}  \end{figure}
# Line 255  via; Line 258  via;
258  \begin{python}  \begin{python}
259   u_rec=rec.getValue(u)   u_rec=rec.getValue(u)
260  \end{python}  \end{python}
261  For consecutive time steps one can record the values from \verb u_rec  in an  For consecutive time steps one can record the values from \verb!u_rec! in an
262  array initialised as \verb u_rec0=[]  with;  array initialised as \verb!u_rec0=[]! with;
263  \begin{python}  \begin{python}
264    u_rec0.append(rec.getValue(u))    u_rec0.append(rec.getValue(u))
265  \end{python}  \end{python}
266
267  It can be useful to monitor the value at a single or multiple individual points  It can be useful to monitor the value at a single or multiple individual points
268  in the model during the modelling process. This is done using  in the model during the modelling process. This is done using
269  the \verb Locator  function.  the \verb!Locator! function.
270
271
272  \section{Acceleration Solution}  \section{Acceleration Solution}
273  \sslist{example07b.py}  \sslist{example07b.py}
274
275  An alternative method is to solve for the acceleration $\frac{\partial ^2 An alternative method to the displacement solution, is to solve for the 276 p}{\partial t^2}$ directly, and derive the displacement solution from the  acceleration $\frac{\partial ^2 p}{\partial t^2}$ directly. The displacement can
277  PDE solution. \refEq{eqn:waveu} is thus modified;  then be derived from the acceleration after a solution has been calculated
278    The acceleration is given by a modified form of \autoref{eqn:waveu};
279
280    \nabla ^2 p - \frac{1}{c^2} a = 0    \nabla ^2 p - \frac{1}{c^2} a = 0
281  \label{eqn:wavea}  \label{eqn:wavea}
282
283  and can be solved directly with $Y=0$ and $X=-c^2 \nabla ^2 p\hackscore{(t)}$.  and can be solved directly with $Y=0$ and $X=-c^2 \nabla ^2 p_{(t)}$.
284  After each iteration the displacement is re-evaluated via;  After each iteration the displacement is re-evaluated via;
285
286   p\hackscore{(t+1)}=2p\hackscore{(t)} - p\hackscore{(t-1)} + h^2a   p_{(t+1)}=2p_{(t)} - p_{(t-1)} + h^2a
287
288
289  \subsection{Lumping}  \subsection{Lumping}
# Line 304  It is also possible to check if lumping Line 308  It is also possible to check if lumping
308  \section{Stability Investigation}  \section{Stability Investigation}
309  It is now prudent to investigate the stability limitations of this problem.  It is now prudent to investigate the stability limitations of this problem.
310  First, we let the frequency content of the source be very small. If we define  First, we let the frequency content of the source be very small. If we define
311  the source as a cosine input, than the wavlength of the input is equal to the  the source as a cosine input, then the wavlength of the input is equal to the
312  radius of the source. Let this value be 5 meters. Now, if the maximum velocity  radius of the source. Let this value be 5 meters. Now, if the maximum velocity
313  of the model is $c=380.0ms^{-1}$ then the source  of the model is $c=380.0ms^{-1}$, then the source
314  frequency is $f\hackscore{r} = \frac{380.0}{5} = 76.0 Hz$. This is a worst case  frequency is $f_{r} = \frac{380.0}{5} = 76.0 Hz$. This is a worst case
315  scenario with a small source and the models maximum velocity.  scenario with a small source and the models maximum velocity.
316
317  Furthermore, we know from \refSec{sec:nsstab}, that the spatial sampling  Furthermore, we know from \autoref{sec:nsstab}, that the spatial sampling
318  frequency must be at least twice this value to ensure stability. If we assume  frequency must be at least twice this value to ensure stability. If we assume
319  the model mesh is a square equispaced grid,  the model mesh is a square equispaced grid,
320  then the sampling interval is the side length divided by the number of samples,  then the sampling interval is the side length divided by the number of samples,
321  given by $\Delta x = \frac{1000.0m}{400} = 2.5m$ and the maximum sampling  given by $\Delta x = \frac{1000.0m}{400} = 2.5m$ and the maximum sampling
322  frequency capable at this interval is  frequency capable at this interval is
323  $f\hackscore{s}=\frac{380.0ms^{-1}}{2.5m}=152Hz$ this is just equal to the  $f_{s}=\frac{380.0ms^{-1}}{2.5m}=152Hz$ this is just equal to the
324  required rate satisfying \refeq{eqn:samptheorem}.  required rate satisfying \autoref{eqn:samptheorem}.
325
326  \reffig{fig:ex07sampth} depicts three examples where the grid has been  \autoref{fig:ex07sampth} depicts three examples where the grid has been
327  undersampled, sampled correctly, and over sampled. The grids used had  undersampled, sampled correctly, and over sampled. The grids used had
328  200, 400 and 800 nodes per side respectively. Obviously, the oversampled grid  200, 400 and 800 nodes per side respectively. Obviously, the oversampled grid
329  retains the best resolution of the modelled wave.  retains the best resolution of the modelled wave.
# Line 337  the propagation requirement. For a maxim Line 341  the propagation requirement. For a maxim
341    \Delta t \leq \frac{1000.0m}{800} \frac{1}{380.0} = 0.0032s    \Delta t \leq \frac{1000.0m}{800} \frac{1}{380.0} = 0.0032s
342
343  \end{subequations}  \end{subequations}
344  We can see, that for each doubling of the number of nodes in the mesh, we halve  Observe that for each doubling of the number of nodes in the mesh, we halve
345  the timestep. To illustrate the impact this has, consider our model. If the  the time step. To illustrate the impact this has, consider our model. If the
346  source is placed at the center, it is $500m$ from the nearest boundary. With a  source is placed at the center, it is $500m$ from the nearest boundary. With a
347  velocity of $380.0ms^{-1}$ it will take $\approx1.3s$ for the wavefront to  velocity of $380.0ms^{-1}$ it will take $\approx1.3s$ for the wavefront to
348  reach that boundary. In each case, this equates to $100$,  $200$ and $400$ time  reach that boundary. In each case, this equates to $100$,  $200$ and $400$ time
349  steps. This is again, only a best case scenario, for true stability these time  steps. This is again, only a best case scenario, for true stability these time
350  values may need to be halved and possibly havled again.  values may need to be halved and possibly halved again.
351
352  \begin{figure}[ht]  \begin{figure}[ht]
353  \centering  \centering
354  \subfigure[Undersampled Example]{  \subfigure[Undersampled Example]{
355  \includegraphics[width=3in]{figures/ex07usamp.png}  \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm
356    ,clip]{figures/ex07usamp.png}
357  \label{fig:ex07usamp}  \label{fig:ex07usamp}
358  }  }
359  \subfigure[Just sampled Example]{  \subfigure[Just sampled Example]{
360  \includegraphics[width=3in]{figures/ex07jsamp.png}  \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm
361    ,clip]{figures/ex07jsamp.png}
362  \label{fig:ex07jsamp}  \label{fig:ex07jsamp}
363  }  }
364  \subfigure[Over sampled Example]{  \subfigure[Over sampled Example]{
365  \includegraphics[width=3in]{figures/ex07nsamp.png}  \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm
366    ,clip]{figures/ex07nsamp.png}
367  \label{fig:ex07nsamp}  \label{fig:ex07nsamp}
368  }  }
\label{fig:ex07sampth}
369  \caption{Sampling Theorem example for stability  \caption{Sampling Theorem example for stability
370  investigation.}  investigation.}
371    \label{fig:ex07sampth}
372  \end{figure}  \end{figure}
373
374

Legend:
 Removed from v.3054 changed lines Added in v.3373