1 |
|
2 |
######################################################## |
3 |
# |
4 |
# Copyright (c) 2009 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 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 |
############################################################FILE HEADER |
26 |
# example01b.py |
27 |
# Model temperature diffusion between two granite blocks of unequal |
28 |
# initial temperature. Solve for total energy in the system. Use |
29 |
# matplotlib to visualise the answer. |
30 |
|
31 |
#######################################################EXTERNAL MODULES |
32 |
# To solve the problem it is necessary to import the modules we require. |
33 |
from esys.escript import * # This imports everything from the escript library |
34 |
from esys.escript.unitsSI import * |
35 |
from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE |
36 |
from esys.finley import Rectangle # This imports the rectangle domain function |
37 |
#For interactive use, you can comment out the next two lines |
38 |
import matplotlib |
39 |
matplotlib.use('agg') #It's just here for automated testing |
40 |
import pylab as pl #Plotting package. |
41 |
import numpy as np #Array package. |
42 |
import os, sys #This package is necessary to handle saving our data. |
43 |
|
44 |
#################################################ESTABLISHING VARIABLES |
45 |
#Domain related. |
46 |
mx = 500*m #meters - model length |
47 |
my = 100*m #meters - model width |
48 |
ndx = 100 # mesh steps in x direction |
49 |
ndy = 1 # mesh steps in y direction - one dimension means one element |
50 |
boundloc = mx/2 # location of boundary between the two blocks |
51 |
#PDE related |
52 |
rho = 2750. *kg/m**3 #kg/m{3} density of iron |
53 |
cp = 790.*J/(kg*K) # J/Kg.K thermal capacity |
54 |
rhocp = rho*cp |
55 |
kappa = 2.2*W/m/K # watts/m.Kthermal conductivity |
56 |
qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source |
57 |
T1=20 * Celsius # initial temperature at Block 1 |
58 |
T2=2273. * Celsius # base temperature at Block 2 |
59 |
|
60 |
################################################ESTABLISHING PARAMETERS |
61 |
t=0 * day # our start time, usually zero |
62 |
tend=50 * yr # - time to end simulation |
63 |
outputs = 200 # number of time steps required. |
64 |
h=(tend-t)/outputs #size of time step |
65 |
#user warning statement |
66 |
print "Expected Number of time outputs is: ", (tend-t)/h |
67 |
i=0 #loop counter |
68 |
#the folder to put our outputs in, leave blank "" for script path |
69 |
save_path= os.path.join("data","example01") |
70 |
#ensure the dir exists |
71 |
mkDir(save_path, os.path.join(save_path,"tempT")) |
72 |
|
73 |
####################################################DOMAIN CONSTRUCTION |
74 |
blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy) |
75 |
|
76 |
###############################################ESCRIPT PDE CONSTRUCTION |
77 |
#... open PDE and set coefficients ... |
78 |
mypde=LinearPDE(blocks) |
79 |
mypde.setSymmetryOn() |
80 |
A=zeros((2,2)) |
81 |
A[0,0]=kappa |
82 |
mypde.setValue(A=A,D=rhocp/h) |
83 |
# ... set initial temperature .... |
84 |
x=Solution(blocks).getX() |
85 |
T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc)) |
86 |
|
87 |
# ... open a collector for the time marks and corresponding total energy |
88 |
t_list=[] |
89 |
E_list=[] |
90 |
########################################################START ITERATION |
91 |
while t<tend: |
92 |
i+=1 |
93 |
t+=h |
94 |
mypde.setValue(Y=qH+rhocp/h*T) |
95 |
T=mypde.getSolution() |
96 |
totE=integrate(rhocp*T) |
97 |
print "time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE) |
98 |
t_list.append(t) |
99 |
E_list.append(totE) |
100 |
|
101 |
###############################################################PLOTTING |
102 |
# plot the total energy over time: |
103 |
if getMPIRankWorld() == 0: |
104 |
pl.plot(t_list,E_list) |
105 |
pl.title("Total Energy") |
106 |
pl.axis([0,max(t_list),0,max(E_list)*1.1]) |
107 |
pl.ylabel('Temperature (K)') |
108 |
pl.xlabel("Length (m)") |
109 |
pl.savefig(os.path.join(save_path,"totE_ex01b.png")) |