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

revision 2800 by ahallam, Thu Sep 24 03:04:04 2009 UTC revision 2801 by ahallam, Thu Dec 3 01:45:48 2009 UTC
# Line 14  Line 14
14  \section{Two Dimensional Heat Diffusion for a basic Magmatic Intrusion}  \section{Two Dimensional Heat Diffusion for a basic Magmatic Intrusion}
15  \sslist{twodheatdiff001.py and cblib.py}  \sslist{twodheatdiff001.py and cblib.py}
16  %\label{Sec:2DHD}  %\label{Sec:2DHD}
17   Building upon our success from the 1D models it is now prudent to expand our domain by another dimension. For this example we will be using a very simple magmatic intrusion as the basis for our model. The simulation will be a single event where some molten granite has formed a hemisphericle dome at the base of some cold sandstone country rock. A hemisphere is symmetric so taking a cross-section through its centre will effectively model a 3D problem in 2D. New concepts will include non-linear boundaries and the ability to prescribe location specific variables.   Building upon our success from the 1D models, it is now prudent to expand our domain by another dimension. For this example we will be using a very simple magmatic intrusion as the basis for our model. The simulation will be a single event where some molten granite has formed a hemispherical dome at the base of some cold sandstone country rock. A hemisphere is symmetric so taking a cross-section through its centre will effectively model a 3D problem in 2D. New concepts will include non-linear boundaries and the ability to prescribe location specific variables.
18
19  \begin{figure}[h!]  \begin{figure}[h!]
20  \centerline{\includegraphics[width=4.in]{figures/twodheatdiff}}  \centerline{\includegraphics[width=4.in]{figures/twodheatdiff}}
# Line 22  Line 22
22  \label{fig:twodhdmodel}  \label{fig:twodhdmodel}
23  \end{figure}  \end{figure}
24
25  To expand upon our 1D problem, the domain must first be expanded. This will be done in our definition phase by creating a square domain in $x$ and $y$ that is 600 meters along each side \reffig{fig:twodhdmodel}. The number of discrete spatial cells will be 100. The radius of the intrusion will be 200 meters  And the location of the centre of the intrusion will be at the 300 meter mark on the x-axis. The domain variables are;  To expand upon our 1D problem, the domain must first be expanded. This will be done in our definition phase by creating a square domain in $x$ and $y$ that is 600 meters along each side \reffig{fig:twodhdmodel}. The number of discrete spatial cells will be 100 in either direction. The radius of the intrusion will be 200 meters with the centre located at the 300 meter mark on the x-axis. The domain variables are;
26  \begin{verbatim}  \begin{python}
27  mx = 600*m #meters - model length  mx = 600*m #meters - model length
28  my = 600*m #meters - model width  my = 600*m #meters - model width
29  ndx = 100 #mesh steps in x direction  ndx = 100 #mesh steps in x direction
# Line 31  ndy = 100 #mesh steps in y direction Line 31  ndy = 100 #mesh steps in y direction
31  r = 200*m #meters - radius of intrusion  r = 200*m #meters - radius of intrusion
32  ic = [300, 0] #centre of intrusion (meters)  ic = [300, 0] #centre of intrusion (meters)
33  q=0.*Celsius #our heat source temperature is now zero  q=0.*Celsius #our heat source temperature is now zero
34  \end{verbatim}  \end{python}
35  The next step is to define our variables for each material in the model in a manner similar to the previous tutorial. Note that each material has its own unique set of values. The time steps and set up for the domain remain as before. Prior to setting up the PDE the boundary between the two materials must be established. The distance $s$ between two points in car Cartesian coordinates is defined as:  The next step is to define our variables for each material in the model in a manner similar to the previous tutorial. Note that each material has its own unique set of values. The time steps and set up for the domain remain as in Section \ref{sec:key}. Prior to setting up the PDE the boundary between the two materials must be established. The distance $s$ between two points in Cartesian coordinates is defined as:
36
37   (x_{1}-x_{0})^{2}+(y_{1}-y_{0})^{2} = s^{2}   (x_{1}-x_{0})^{2}+(y_{1}-y_{0})^{2} = s^{2}
38
39  If $[x_{0},y_{0}]$ is the point $c$ the centre of the semi-circle that defines our intrusion then for all the points $[x,y]$ in our solution space we can define a distance to $c$. Hence, and points that fall within the radius $r$ of our intrusion will have a corresponding value $s < r$ and all those belonging to the country rock will have a value $s > r$. By subtracting $r$ from both of these conditions we find $s-r < 0$ for all intrusion points and $s-r > 0$ for all country rock points. Defining these conditions within the script is quite simple and is done using the following command:  If we define the point $[x_{0},y_{0}]$ as $c$ which denotes the centre of the semi-circle of our intrusion, then for all the points $[x,y]$ in our model we can calculate a distance to $c$. All the points that fall within the radius $r$ of our intrusion will have a corresponding value $s < r$ and all those belonging to the country rock will have a value $s > r$. By subtracting $r$ from both of these conditions we find $s-r < 0$ for all intrusion points and $s-r > 0$ for all country rock points. Defining these conditions within the script is quite simple and is done using the following command:
40  \begin{verbatim}  \begin{python}
41   bound = length(x-ic)-r #where the boundary will be located   bound = length(x-ic)-r #where the boundary will be located
42  \end{verbatim}  \end{python}
43  This definition of the boundary can now be used with the \verb wherePositive()  and \verb whereNegative()  commands from before to help define the material constants and temperatures in our domain. By examining the general form we solved in the earlier tutorials, it is obvious that both \verb A  and \verb D  depend on the predefined variables. To set these variables accordingly and complete our PDE we use:  This definition of the boundary can now be used with the \verb wherePositive()  and \verb whereNegative()  commands to help define the material constants and temperatures in our domain. By examining the general form we solved in the earlier tutorials, it is obvious that both \verb A  and \verb D  depend on the predefined variables. To set these variables accordingly and complete our PDE we use:
44  \begin{verbatim}  \begin{python}
45  A = (kappai)*whereNegative(bound)+(kappac)*wherePositive(bound)  A = (kappai)*whereNegative(bound)+(kappac)*wherePositive(bound)
46  D = (rhocpi/h)*whereNegative(bound)+(rhocpc/h)*wherePositive(bound)  D = (rhocpi/h)*whereNegative(bound)+(rhocpc/h)*wherePositive(bound)
47
48  mypde.setValue(A=A*kronecker(model),D=D,d=eta,y=eta*Tc)  mypde.setValue(A=A*kronecker(model),D=D,d=eta,y=eta*Tc)
49  \end{verbatim}  \end{python}
50  Our PDE has now been properly established. The initial conditions for temperature are set out in a similar matter:  Our PDE has now been properly established. The initial conditions for temperature are set out in a similar matter:
51  \begin{verbatim}  \begin{python}
52  #defining the initial temperatures.  #defining the initial temperatures.
53   T= Ti*whereNegative(bound)+Tc*wherePositive(bound)   T= Ti*whereNegative(bound)+Tc*wherePositive(bound)
54  \end{verbatim}  \end{python}
55  The iteration process now begins as before, but using our new conditions for \verb D  as defined above.  The iteration process now begins as before, but using our new conditions for \verb D  as defined above.
56
57  \subsection{Contouring escript data}  \subsection{Contouring escript data}
58  It is possible to contour our solution using \modmpl . Unfortunately the \modmpl contouring function only accepts regularly gridded data. As our solution is not regularly gridded, it is necessary to interpolate our solution onto a regular grid. First we extract the model coordinates using \verb getX  these are then transformed to a numpy array and into individual $x$ and $y$ arrays. We also need to generate our regular grid which is done using the \modnumpy function \verb linspace  .  It is possible to contour our solution using \modmpl . Unfortunately the \modmpl contouring function only accepts regularly gridded data. As our solution is not regularly gridded, it is necessary to interpolate our solution onto a regular grid. First we extract the model coordinates using \verb getX  these are then transformed to a \verb numpy  array known as a \verb tuple . Ths multi-dimensional array is then broken down into individual $x$ and $y$ arrays. This is a one step process using the function \verb toXYTuple . We also need to generate our regular grid which is done using the \modnumpy function \verb linspace  .
59  \begin{verbatim}  \begin{python}
60  # rearrage mymesh to suit solution function space for contouring        # rearrage mymesh to suit solution function space for contouring
61  oldspacecoords=model.getX()  oldspacecoords=model.getX()
62  coords=Data(oldspacecoords, T.getFunctionSpace())  coords=Data(oldspacecoords, T.getFunctionSpace())
# Line 64  coordX, coordY = toXYTuple(coords) Line 64  coordX, coordY = toXYTuple(coords)
64  # create regular grid  # create regular grid
65  xi = np.linspace(0.0,mx,100)  xi = np.linspace(0.0,mx,100)
66  yi = np.linspace(0.0,my,100)  yi = np.linspace(0.0,my,100)
67  \end{verbatim}  \end{python}
68  The remainder of our contouring commands reside within the \verb while  loop so that a new contour is generated for each time step. For each time step the solution much be regridded for \modmpl using the \verb griddata  function. This function interprets an irregular grid and solution from \verb tempT  , \verb xi   and \verb yi  this is transformed to the new coordinates defined by \verb coordX  and \verb coordY  with an output \verb zi  . It is now possible to use the \verb contourf  function which generates a colour filled contour. The colour gradient of our plots is set via the command \verb pl.matplotlib.pyplot.autumn() , other colours are listed on the \modmpl web page. Our results are then contoured, visually adjusted using the \modmpl functions and then saved to file. \verb pl.clf()  clears the figure in readiness for the next time iteration.  The remainder of our contouring commands reside within a \verb while  loop so that a new contour is generated for each time step. For each time step the solution must be regridded for \modmpl using the \verb griddata  function. This function interprets an irregular grid and solution from \verb tempT  , \verb xi   and \verb yi . This is transformed to the new coordinates defined by \verb coordX  and \verb coordY  with an output \verb zi  . It is now possible to use the \verb contourf  function which generates colour filled contours. The colour gradient of our plots is set via the command \verb pl.matplotlib.pyplot.autumn() , other colours are listed on the \modmpl web page. Our results are then contoured, visually adjusted using the \modmpl functions and then saved to file. \verb pl.clf()  clears the figure in readiness for the next time iteration.
69
70  \begin{verbatim}  \begin{python}
71  #grid the data.  #grid the data.
72  zi = pl.matplotlib.mlab.griddata(coordX,coordY,tempT,xi,yi)  zi = pl.matplotlib.mlab.griddata(coordX,coordY,tempT,xi,yi)
73  # contour the gridded data, plotting dots at the randomly spaced data points.  # contour the gridded data, plotting dots at the randomly spaced data points.
# Line 81  pl.xlabel("Horizontal Displacement (m)") Line 81  pl.xlabel("Horizontal Displacement (m)")
81  pl.ylabel("Depth (m)")  pl.ylabel("Depth (m)")
82  pl.savefig(os.path.join(save_path,"heatrefraction%03d.png") %i)  pl.savefig(os.path.join(save_path,"heatrefraction%03d.png") %i)
83  pl.clf()          pl.clf()
84  \end{verbatim}  \end{python}
85
86  \begin{figure}[h!]  \begin{figure}[h!]
87  \centerline{\includegraphics[width=4.in]{figures/heatrefraction050}}  \centerline{\includegraphics[width=4.in]{figures/heatrefraction050}}

Legend:
 Removed from v.2800 changed lines Added in v.2801