/[escript]/trunk/doc/examples/cookbook/example03a.py
ViewVC logotype

Contents of /trunk/doc/examples/cookbook/example03a.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5707 - (show annotations)
Mon Jun 29 03:59:06 2015 UTC (3 years, 8 months ago) by sshaw
File MIME type: text/x-python
File size: 6641 byte(s)
adding copyright headers to files without copyright info, moved header to top of file in some cases where it wasn't
1 ##############################################################################
2 #
3 # Copyright (c) 2009-2015 by The University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Open Software License version 3.0
8 # http://www.opensource.org/licenses/osl-3.0.php
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15 from __future__ import division, print_function
16
17 __copyright__="""Copyright (c) 2009-2015 by The University of Queensland
18 http://www.uq.edu.au
19 Primary Business: Queensland, Australia"""
20 __license__="""Licensed under the Open Software License version 3.0
21 http://www.opensource.org/licenses/osl-3.0.php"""
22 __url__="https://launchpad.net/escript-finley"
23
24 """
25 Author: Antony Hallam antony.hallam@uqconnect.edu.au
26 """
27
28 ############################################################FILE HEADER
29 # example03a.py
30 # Model temperature diffusion between a granite intrusion and sandstone
31 # country rock. This is a two dimensional problem with the granite as a
32 # heat source.
33
34 #######################################################EXTERNAL MODULES
35 #To solve the problem it is necessary to import the modules we require.
36 #For interactive use, you can comment out the next two lines
37 import matplotlib
38 matplotlib.use('agg') #It's just here for automated testing
39 #This imports everything from the escript library
40 from esys.escript import *
41 # This defines the LinearPDE module as LinearPDE
42 from esys.escript.linearPDEs import LinearPDE
43 # A useful unit handling package which will make sure all our units
44 # match up in the equations under SI.
45 from esys.escript.unitsSI import *
46 import pylab as pl #Plotting package.
47 import numpy as np #Array package.
48 import os #This package is necessary to handle saving our data.
49 from cblib import toXYTuple
50 try:
51 from esys.finley import Rectangle
52 HAVE_FINLEY = True
53 except ImportError:
54 print("Finley module not available")
55 HAVE_FINLEY = False
56 ########################################################MPI WORLD CHECK
57 if getMPISizeWorld() > 1:
58 import sys
59 print("This example will not run in an MPI world.")
60 sys.exit(0)
61
62 try:
63 from mpl_toolkits.natgrid import _natgrid
64 HAVE_NATGRID=True
65 except ImportError:
66 HAVE_NATGRID=False
67
68
69 if HAVE_FINLEY and HAVE_NATGRID:
70 #################################################ESTABLISHING VARIABLES
71 #PDE related
72 mx = 600*m #meters - model length
73 my = 600*m #meters - model width
74 ndx = 150 #mesh steps in x direction
75 ndy = 150 #mesh steps in y direction
76 r = 200*m #meters - radius of intrusion
77 ic = [300*m, 0] #centre of intrusion (meters)
78 qH=0.*J/(sec*m**3) #our heat source temperature is now zero
79
80 ## Intrusion Variables - Granite
81 Ti=2273.*Celsius # Kelvin -the starting temperature of our RHS Block
82 rhoi = 2750*kg/m**3 #kg/m^{3} density of granite
83 cpi = 790.*J/(kg*K) #j/Kg.K thermal capacity
84 rhocpi = rhoi*cpi #DENSITY * SPECIFIC HEAT
85 kappai=2.2*W/m/K #watts/m.K thermal conductivity
86 ## Country Rock Variables - Sandstone
87 Tc = 473*Celsius # Kelvin #the starting temperature of our country rock
88 rhoc = 2000*kg/m**3 #kg/m^{3} density
89 cpc = 920.*J/(kg*K) #j/kg.k specific heat
90 rhocpc = rhoc*cpc #DENSITY * SPECIFIC HEAT
91 kappac = 1.9*W/m/K #watts/m.K thermal conductivity
92
93 #Script/Iteration Related
94 t=0. #our start time, usually zero
95 tend=200.* yr #the time we want to end the simulation
96 outputs = 200 # number of time steps required.
97 h=(tend-t)/outputs #size of time step
98 #user warning
99 print("Expected Number of Output Files is: ", outputs)
100 print("Step size is: ", h/day, "days")
101 i=0 #loop counter
102 #the folder to put our outputs in, leave blank "" for script path
103 save_path= os.path.join("data","example03")
104 mkDir(save_path)
105 ########## note this folder path must exist to work ###################
106
107 ################################################ESTABLISHING PARAMETERS
108 #generate domain using rectangle
109 model = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
110 #extract finite points - the solution points
111 #create the PDE
112 mypde=LinearPDE(model) #assigns a domain to our PDE
113 mypde.setSymmetryOn() #set the fast solver on for symmetry
114 #establish location of boundary between two materials
115 x=Function(model).getX()
116 bound = length(x-ic)-r #where the boundary will be located
117 kappa = kappai*whereNegative(bound)+kappac*(1-whereNegative(bound))
118 rhocp = rhocpi*whereNegative(bound)+rhocpc*(1-whereNegative(bound))
119 #define our PDE coeffs
120 mypde.setValue(A=kappa*kronecker(model),D=rhocp/h)
121 #set initial temperature (make sure we use the right sample points)
122 x=Solution(model).getX()
123 bound = length(x-ic)-r #where the boundary will be located
124 T= Ti*whereNegative(bound)+Tc*(1-whereNegative(bound))
125
126 # rearrage mymesh to suit solution function space for contouring
127 coordX, coordY = toXYTuple(T.getFunctionSpace().getX())
128 # create regular grid
129 xi = np.linspace(0.0,mx,75)
130 yi = np.linspace(0.0,my, 75)
131
132 ########################################################START ITERATION
133 while t<=tend:
134 i+=1 #counter
135 t+=h #current time
136 mypde.setValue(Y=qH+T*rhocp/h)
137 T=mypde.getSolution()
138 tempT = T.toListOfTuples()
139 # grid the data.
140 zi = pl.matplotlib.mlab.griddata(coordX,coordY,tempT,xi,yi)
141 # contour the gridded data, plotting dots at the
142 # randomly spaced data points.
143 pl.matplotlib.pyplot.autumn()
144 pl.contourf(xi,yi,zi,10)
145 CS = pl.contour(xi,yi,zi,5,linewidths=0.5,colors='k')
146 pl.clabel(CS, inline=1, fontsize=8)
147 pl.axis([0,600,0,600])
148 pl.title("Heat diffusion from an intrusion.")
149 pl.xlabel("Horizontal Displacement (m)")
150 pl.ylabel("Depth (m)")
151 pl.savefig(os.path.join(save_path,"temp%03d.png"%i))
152 pl.clf()
153 print("time step %s at t=%e days completed."%(i,t/day))
154
155 #########################################################CREATE A MOVIE
156 # compile the *.png files to create an *.avi video that shows T change
157 # with time. This opperation uses linux mencoder.
158 os.system("mencoder mf://"+save_path+"/*.png -mf type=png:\
159 w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
160 example03tempT.avi")

  ViewVC Help
Powered by ViewVC 1.1.26