 # Contents of /release/4.0/doc/cookbook/example07.tex

Revision 4286 - (show annotations)
Thu Mar 7 04:28:11 2013 UTC (7 years, 3 months ago) by caltinay
Original Path: trunk/doc/cookbook/example07.tex
File MIME type: application/x-tex
File size: 15688 byte(s)
Assorted spelling fixes.


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