/[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 3892 - (show annotations)
Tue Apr 10 08:57:23 2012 UTC (6 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 5872 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



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

  ViewVC Help
Powered by ViewVC 1.1.26