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

Revision 4285 - (hide annotations)
Thu Mar 7 01:08:43 2013 UTC (7 years, 2 months ago) by caltinay
Original Path: trunk/doc/cookbook/example07.tex
File MIME type: application/x-tex
File size: 15686 byte(s)
Corrected some spelling.


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