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

Revision 3386 - (show annotations)
Thu Nov 25 05:05:50 2010 UTC (8 years, 3 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 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 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 or longitudinal style wave motion is not possible. An obvious example is sound 17 waves. 18 19 The acoustic wave equation is defined as; 20 \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 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 equation stability criterion and solving for the displacement or acceleration solution. 28 29 \section{The Laplacian in \esc} 30 The Laplacian operator which can be written as $\Delta$ or $\nabla^2$, is 31 calculated via the divergence of the gradient of the object, which in this 32 example is the scalar $p$. Thus we can write; 33 \begin{equation} 34 \nabla^2 p = \nabla \cdot \nabla p = 35 \sum_{i}^n 36 \frac{\partial^2 p}{\partial x^2_{i}} 37 \label{eqn:laplacian} 38 \end{equation} 39 For the two dimensional case in Cartesian coordinates \autoref{eqn:laplacian} 40 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 46 In \esc the Laplacian is calculated using the divergence representation and the 47 intrinsic functions \textit{grad()} and \textit{trace()}. The function 48 \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 \frac{\partial p}{\partial x _{0}}, 53 \frac{\partial p}{\partial x _{1}} 54 \right] 55 \label{eqn:grad} 56 \end{equation} 57 Larger ranked solution objects will return gradient tensors. For example, a 58 pressure field which acts in the directions $p _{0}$ and $p 59 _{1}$ would return; 60 \begin{equation} 61 \nabla p = \begin{bmatrix} 62 \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 \end{bmatrix} 67 \label{eqn:gradrank1} 68 \end{equation} 69 70 \autoref{eqn:grad} corresponds to the Linear PDE general form value 71 $X$. Notice however, that the general form contains the term $X 72 _{i,j}$\footnote{This is the first derivative in the $j^{th}$ 73 direction for the $i^{th}$ component of the solution.}, 74 hence for a rank 0 object there is no need to do more then calculate the 75 gradient and submit it to the solver. In the case of the rank 1 or greater 76 object, it is also necessary to calculate the trace. This is the sum of the 77 diagonal in \autoref{eqn:gradrank1}. 78 79 Thus when solving for equations containing the Laplacian one of two things must 80 be completed. If the object \verb!p! is less then rank 1 the gradient is 81 calculated via; 82 \begin{python} 83 gradient=grad(p) 84 \end{python} 85 and if the object is greater then or equal to a rank 1 tensor, the trace of 86 the gradient is calculated. 87 \begin{python} 88 gradient=trace(grad(p)) 89 \end{python} 90 These values can then be submitted to the PDE solver via the general form term 91 $X$. The Laplacian is then computed in the solution process by taking the 92 divergence of $X$. 93 94 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 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 numerically as they tend to oscillate about the exact solution, which can 105 eventually lead to a catastrophic failure. To counter this problem, explicitly 106 stable schemes like the backwards Euler method, and correct parameterisation of 107 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 \begin{equation} \label{eqn:freqvel} 113 f=\frac{v}{\lambda} 114 \end{equation} 115 The velocity $v$ that a wave travels in a medium is an important variable. For 116 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 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 be significantly less then this. Of the order $10E-4$ would be appropriate. 122 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 130 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 accurately represented when an equispaced sampling rate $f _{n}$ is 133 equal to or greater then twice the maximum frequency of the signal 134 $f_{s}$, or; 135 \begin{equation} \label{eqn:samptheorem} 136 f_{n} \geqslant f_{s} 137 \end{equation} 138 For example, a 50Hz signal will require a sampling rate greater then 100Hz or 139 one sample every 0.01 seconds. The wave equation relies on a spatial frequency, 140 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 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 problems. This requirement makes the wave equation arduous to 147 solve numerically due to the large number of time iterations required in each 148 solution. Models with very high velocities and frequencies will be the worst 149 affected by this problem. 150 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 substituting \autoref{eqn:centdiff} for $\frac{\partial ^2 p }{\partial t ^2}$ 161 in \autoref{eqn:acswave}; 162 \begin{equation} 163 \nabla ^2 p - \frac{1}{c^2h^2} \left[p_{(t+1)} - 2p_{(t)} + 164 p_{(t-1)} \right] 165 = 0 166 \label{eqn:waveu} 167 \end{equation} 168 Rearranging for $p_{(t+1)}$; 169 \begin{equation} 170 p_{(t+1)} = c^2 h^2 \nabla ^2 p_{(t)} +2p_{(t)} - 171 p_{(t-1)} 172 \end{equation} 173 this can be compared with the general form of the \modLPDE module and it 174 becomes clear that $D=1$, $X_{i,j}=-c^2 h^2 \nabla ^2 p_{(t)}$ and 175 $Y=2p_{(t)} - p_{(t-1)}$. 176 177 The solution script is similar to others that we have created in previous 178 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 \item All or select solutions are saved to file for visualisation later on. 186 \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 criterion. Small sources will generate high frequency signals. Here, when using 199 a rectangular domain, the source is defined by a cosine function. 200 \begin{python} 201 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 u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*\ 207 whereNegative(length(x-xc)-src_radius) 208 u_m1=u 209 \end{python} 210 211 \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 surface in Mayavi2. The second is to take a cross section of the model which 215 will require the \textit{Locator} function. 216 First \verb!Locator! must be imported; 217 \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 model slice will go. This is easily implemented with a loop; 225 \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]) 231 \end{python} 232 We then submit the output to \verb!Locator! and finally return the appropriate 233 values using the \verb!getValue! function. 234 \begin{python} 235 src=Locator(mydomain,src_cut) 236 src_cut=src.getValue(u) 237 \end{python} 238 It is then a trivial task to plot and save the output using \mpl 239 (\autoref{fig:cxsource}). 240 \begin{python} 241 pl.plot(cut_loc,src_cut) 242 pl.axis([xc-src_radius*3,xc+src_radius*3,0.,2*U0]) 243 pl.savefig(os.path.join(savepath,"source_line.png")) 244 \end{python} 245 \begin{figure}[h] 246 \centering 247 \includegraphics[width=6in]{figures/sourceline.png} 248 \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 When the solution \verb u is updated we can extract the value at that point 261 via; 262 \begin{python} 263 u_rec=rec.getValue(u) 264 \end{python} 265 For consecutive time steps one can record the values from \verb!u_rec! in an 266 array initialised as \verb!u_rec0=[]! with; 267 \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 the \verb!Locator! function. 274 275 276 \section{Acceleration Solution} 277 \sslist{example07b.py} 278 279 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 \begin{equation} 284 \nabla ^2 p - \frac{1}{c^2} a = 0 285 \label{eqn:wavea} 286 \end{equation} 287 and can be solved directly with $Y=0$ and $X=-c^2 \nabla ^2 p_{(t)}$. 288 After each iteration the displacement is re-evaluated via; 289 \begin{equation} 290 p_{(t+1)}=2p_{(t)} - p_{(t-1)} + h^2a 291 \end{equation} 292 293 \subsection{Lumping} 294 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 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 303 More information about the lumping implementation used in \esc and its accuracy 304 can be found in the user guide. 305 306 To turn lumping on in \esc one can use the command; 307 \begin{python} 308 mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().HRZ_LUMPING) 309 \end{python} 310 It is also possible to check if lumping is set using; 311 \begin{python} 312 print mypde.isUsingLumping() 313 \end{python} 314 315 \section{Stability Investigation} 316 It is now prudent to investigate the stability limitations of this problem. 317 First, we let the frequency content of the source be very small. If we define 318 the source as a cosine input, then the wavlength of the input is equal to the 319 radius of the source. Let this value be 5 meters. Now, if the maximum velocity 320 of the model is $c=380.0ms^{-1}$, then the source 321 frequency is $f_{r} = \frac{380.0}{5} = 76.0 Hz$. This is a worst case 322 scenario with a small source and the models maximum velocity. 323 324 Furthermore, we know from \autoref{sec:nsstab}, that the spatial sampling 325 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 $f_{s}=\frac{380.0ms^{-1}}{2.5m}=152Hz$ this is just equal to the 331 required rate satisfying \autoref{eqn:samptheorem}. 332 333 \autoref{fig:ex07sampth} depicts three examples where the grid has been 334 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 338 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 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 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 values may need to be halved and possibly halved again. 358 359 \begin{figure}[ht] 360 \centering 361 \subfigure[Undersampled Example]{ 362 \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07usamp.png} 363 \label{fig:ex07usamp} 364 } 365 \subfigure[Just sampled Example]{ 366 \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07jsamp.png} 367 \label{fig:ex07jsamp} 368 } 369 \subfigure[Over sampled Example]{ 370 \includegraphics[width=0.45\textwidth,trim=0cm 6cm 5cm 6cm,clip]{figures/ex07nsamp.png} 371 \label{fig:ex07nsamp} 372 } 373 \caption{Sampling Theorem example for stability investigation} 374 \label{fig:ex07sampth} 375 \end{figure} 376