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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2904 - (show annotations)
Tue Feb 2 04:21:52 2010 UTC (9 years, 7 months ago) by gross
File MIME type: text/x-python
File size: 4533 byte(s)
more revision on cookbook: example 1 and 2 have been swapped.
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
26 # To solve the problem it is necessary to import the modules we require.
27 from esys.escript import * # This imports everything from the escript library
28 from esys.escript.unitsSI import *
29 from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE
30 from esys.finley import Rectangle # This imports the rectangle domain function from finley
31 #For interactive use, you can comment out the next two lines
32 import matplotlib
33 matplotlib.use('agg') #It's just here for automated testing
34 import pylab as pl #Plotting package.
35 import numpy as np #Array package.
36 import os, sys #This package is necessary to handle saving our data.
37
38 # .. MPI WORLD CHECK
39 if getMPISizeWorld() > 1:
40 import sys
41 print "This example will not run in an MPI world."
42 sys.exit(0)
43
44 ##ESTABLISHING VARIABLES
45 #Domain related.
46 mx = 1*m #meters - model length
47 my = .1*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 #PDE related
51 rho = 7874. *kg/m**3 #kg/m^{3} density of iron
52 cp = 449.*J/(kg*K) # J/Kg.K thermal capacity
53 rhocp = rho*cp
54 kappa = 80.*W/m/K # watts/m.Kthermal conductivity
55 qH=0 * J/(sec*m**3) # J/(sec.m^{3}) no heat source
56 Tref = 20 * Celsius # base temperature of the rod
57 T0 = 100 * Celsius # temperature at heating element
58
59 t=0 * day # our start time, usually zero
60 tend= 0.5 *day # - time to end simulation
61 outputs = 200 # number of time steps required.
62 h=(tend-t)/outputs #size of time step
63 #user warning statement
64 print "Expected Number of time outputs is: ", (tend-t)/h
65 i=0 #loop counter
66 #the folder to put our outputs in, leave blank "" for script path
67 save_path= os.path.join("data","onedheatdiff002")
68 #ensure the dir exists
69 mkDir(save_path, os.path.join(save_path,"tempT"))
70
71 #... generate domain ...
72 rod = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
73 x=Solution(rod).getX()
74 #... open PDE and set coefficients ...
75 mypde=LinearPDE(rod)
76 A=zeros((2,2))
77 A[0,0]=kappa
78 q=whereZero(x[0])
79 mypde.setValue(A=A, D=rhocp/h, q=q, r=T0)
80 # ... set initial temperature ....
81 T= T0*whereZero(x[0])+Tref*(1-whereZero(x[0]))
82
83 # ... open a collector for the time marks and corresponding total energy
84 t_list=[]
85 E_list=[]
86 # ... convert solution points for plotting
87 plx = x.toListOfTuples()
88 plx = np.array(plx) #convert to tuple to numpy array
89 plx = plx[:,0] #extract x locations
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 minutes completed. total energy = %e."%(i,t/minute,totE)
98 t_list.append(t)
99 E_list.append(totE)
100
101 #establish figure 1 for temperature vs x plots
102 tempT = T.toListOfTuples()
103 pl.figure(1) #current figure
104 pl.plot(plx,tempT) #plot solution
105 # add title
106 pl.axis([0,mx,Tref*.9,T0*1.1])
107 pl.title("Temperature across rod at time %e hours"%(t/hour))
108 #save figure to file
109 pl.savefig(os.path.join(save_path,"tempT", "rodpyplot%03d.png"%i))
110 pl.clf() #clear figure
111 # plot the total energy over time:
112 pl.figure(2)
113 pl.plot(t_list,E_list)
114 pl.title("Total Energy")
115 # pl.axis([0,max(t_list),0,max(E_list)*1.1])
116 pl.savefig(os.path.join(save_path,"totE.png"))
117 pl.clf()
118
119 # compile the *.png files to create a*.avi video that show T change
120 # with time. This opperation uses linux mencoder. For other operating
121 # systems it may be possible to use your favourite video compiler to
122 # convert image files to videos. To enable this step uncomment the
123 # following lines.
124
125 #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
126 #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
127 #onedheatdiff001tempT.avi")

  ViewVC Help
Powered by ViewVC 1.1.26