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

Revision 3373 - (show annotations)
Tue Nov 23 00:29:07 2010 UTC (10 years, 2 months ago) by ahallam
File MIME type: application/x-tex
File size: 15173 byte(s)
New figures.

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