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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4087 - (show annotations)
Thu Nov 22 22:28:01 2012 UTC (6 years, 7 months ago) by caltinay
File MIME type: text/x-python
File size: 5413 byte(s)
Moved matplotlib imports in test scripts before escript since there is an
import chain which pulls it so the use() function has no effect.

1
2 ##############################################################################
3 #
4 # Copyright (c) 2009-2012 by University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Open Software License version 3.0
9 # http://www.opensource.org/licenses/osl-3.0.php
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development since 2012 by School of Earth Sciences
13 #
14 ##############################################################################
15
16 __copyright__="""Copyright (c) 2009-2012 by University of Queensland
17 http://www.uq.edu.au
18 Primary Business: Queensland, Australia"""
19 __license__="""Licensed under the Open Software License version 3.0
20 http://www.opensource.org/licenses/osl-3.0.php"""
21 __url__="https://launchpad.net/escript-finley"
22
23 """
24 Author: Antony Hallam antony.hallam@uqconnect.edu.au
25 """
26
27 ############################################################FILE HEADER
28 # example02.py
29 # Model temperature diffusion along an insulated iron rod with a
30 # heating element at the left hand side.
31
32 #######################################################EXTERNAL MODULES
33 # To solve the problem it is necessary to import the modules we require.
34 #For interactive use, you can comment out the next two lines
35 import matplotlib
36 matplotlib.use('agg') #It's just here for automated testing
37 from esys.escript import * # This imports everything from the escript library
38 from esys.escript.unitsSI import *
39 from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE
40 from esys.finley import Rectangle # This imports the rectangle domain function
41 import pylab as pl #Plotting package.
42 import numpy as np #Array package.
43 import os, sys #This package is necessary to handle saving our data.
44
45 ########################################################MPI WORLD CHECK
46 if getMPISizeWorld() > 1:
47 import sys
48 print("This example will not run in an MPI world.")
49 sys.exit(0)
50
51 #################################################ESTABLISHING VARIABLES
52 #Domain related.
53 mx = 1*m #meters - model length
54 my = .1*m #meters - model width
55 ndx = 100 # mesh steps in x direction
56 ndy = 1 # mesh steps in y direction - one dimension means one element
57 #PDE related
58 rho = 7874. *kg/m**3 #kg/m^{3} density of iron
59 cp = 449.*J/(kg*K) # J/Kg.K thermal capacity
60 rhocp = rho*cp
61 kappa = 80.*W/m/K # watts/m.Kthermal conductivity
62 qH=0 * J/(sec*m**3) # J/(sec.m^{3}) no heat source
63 Tref = 20 * Celsius # base temperature of the rod
64 T0 = 100 * Celsius # temperature at heating element
65
66 ################################################ESTABLISHING PARAMETERS
67 t=0 * day # our start time, usually zero
68 tend= 0.5 *day # - time to end simulation
69 outputs = 200 # number of time steps required.
70 h=(tend-t)/outputs #size of time step
71 #user warning statement
72 print("Expected Number of time outputs is: ", (tend-t)/h)
73 i=0 #loop counter
74 #the folder to put our outputs in, leave blank "" for script path
75 save_path= os.path.join("data","example02")
76 #ensure the dir exists
77 mkDir(save_path, os.path.join(save_path,"tempT"))
78
79 ####################################################DOMAIN CONSTRUCTION
80 rod = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
81 x=Solution(rod).getX()
82 ###############################################ESCRIPT PDE CONSTRUCTION
83 mypde=LinearPDE(rod)
84 A=zeros((2,2))
85 A[0,0]=kappa
86 q=whereZero(x[0])
87 mypde.setValue(A=A, D=rhocp/h, q=q, r=T0)
88 # ... set initial temperature ....
89 T= T0*whereZero(x[0])+Tref*(1-whereZero(x[0]))
90
91 # ... open a collector for the time marks and corresponding total energy
92 t_list=[]
93 E_list=[]
94 # ... convert solution points for plotting
95 plx = x.toListOfTuples()
96 plx = np.array(plx) #convert to tuple to numpy array
97 plx = plx[:,0] #extract x locations
98 ########################################################START ITERATION
99 while t<tend:
100 i+=1
101 t+=h
102 mypde.setValue(Y=qH+rhocp/h*T)
103 T=mypde.getSolution()
104 totE=integrate(rhocp*T)
105 print("time step %s at t=%e minutes completed. total energy = %e."%(i,t/minute,totE))
106 t_list.append(t)
107 E_list.append(totE)
108
109 #establish figure 1 for temperature vs x plots
110 tempT = T.toListOfTuples()
111 pl.figure(1) #current figure
112 pl.plot(plx,tempT) #plot solution
113 # add title
114 pl.axis([0,mx,Tref*.9,T0*1.1])
115 pl.ylabel('Temperature (K)')
116 pl.xlabel("Length (m)")
117 pl.title("Temperature across rod at time %e hours"%(t/hour))
118 #save figure to file
119 pl.savefig(os.path.join(save_path,"tempT", "rodpyplot%03d.png"%i))
120 pl.clf() #clear figure
121
122 ###############################################################PLOTTING
123 # plot the total energy over time:
124 pl.figure(2)
125 pl.plot(t_list,E_list)
126 pl.title("Total Energy")
127 pl.ylabel('Energy (W)')
128 pl.xlabel('Time (s)')
129 # pl.axis([0,max(t_list),0,max(E_list)*1.1])
130 pl.savefig(os.path.join(save_path,"totE.png"))
131 pl.clf()
132
133 #########################################################CREATE A MOVIE
134 # compile the *.png files to create a*.avi video that show T change
135 # with time. This opperation uses linux mencoder. For other operating
136 # systems it may be possible to use your favourite video compiler to
137 # convert image files to videos. To enable this step uncomment the
138 # following lines.
139
140 #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
141 #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
142 #example02tempT.avi")

  ViewVC Help
Powered by ViewVC 1.1.26