/[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 5288 - (show annotations)
Tue Dec 2 23:18:40 2014 UTC (4 years, 9 months ago) by sshaw
File MIME type: text/x-python
File size: 5959 byte(s)
fixing tests for cases where required domains not built
1 from __future__ import division, print_function
2 ##############################################################################
3 #
4 # Copyright (c) 2009-2014 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 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 __copyright__="""Copyright (c) 2009-2014 by 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 # example02.py
30 # Model temperature diffusion along an insulated iron rod with a
31 # heating element at the left hand side.
32
33 #######################################################EXTERNAL MODULES
34 # To solve the problem it is necessary to import the modules we require.
35 #For interactive use, you can comment out the next two lines
36 import matplotlib
37 matplotlib.use('agg') #It's just here for automated testing
38 from esys.escript import * # This imports everything from the escript library
39 from esys.escript.unitsSI import *
40 from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE
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 try:
45 from esys.finley import Rectangle
46 HAVE_FINLEY = True
47 except ImportError:
48 print("Finley module not available")
49 HAVE_FINLEY = False
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 if HAVE_FINLEY:
58 #################################################ESTABLISHING VARIABLES
59 #Domain related.
60 mx = 1*m #meters - model length
61 my = .1*m #meters - model width
62 ndx = 100 # mesh steps in x direction
63 ndy = 1 # mesh steps in y direction - one dimension means one element
64 #PDE related
65 rho = 7874. *kg/m**3 #kg/m^{3} density of iron
66 cp = 449.*J/(kg*K) # J/Kg.K thermal capacity
67 rhocp = rho*cp
68 kappa = 80.*W/m/K # watts/m.Kthermal conductivity
69 qH=0 * J/(sec*m**3) # J/(sec.m^{3}) no heat source
70 Tref = 20 * Celsius # base temperature of the rod
71 T0 = 100 * Celsius # temperature at heating element
72
73 ################################################ESTABLISHING PARAMETERS
74 t=0 * day # our start time, usually zero
75 tend= 0.5 *day # - time to end simulation
76 outputs = 200 # number of time steps required.
77 h=(tend-t)/outputs #size of time step
78 #user warning statement
79 print("Expected Number of time outputs is: ", (tend-t)/h)
80 i=0 #loop counter
81 #the folder to put our outputs in, leave blank "" for script path
82 save_path= os.path.join("data","example02")
83 #ensure the dir exists
84 mkDir(save_path, os.path.join(save_path,"tempT"))
85
86 ####################################################DOMAIN CONSTRUCTION
87 rod = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
88 x=Solution(rod).getX()
89 ###############################################ESCRIPT PDE CONSTRUCTION
90 mypde=LinearPDE(rod)
91 A=zeros((2,2))
92 A[0,0]=kappa
93 q=whereZero(x[0])
94 mypde.setValue(A=A, D=rhocp/h, q=q, r=T0)
95 # ... set initial temperature ....
96 T= T0*whereZero(x[0])+Tref*(1-whereZero(x[0]))
97
98 # ... open a collector for the time marks and corresponding total energy
99 t_list=[]
100 E_list=[]
101 # ... convert solution points for plotting
102 plx = x.toListOfTuples()
103 plx = np.array(plx) #convert to tuple to numpy array
104 plx = plx[:,0] #extract x locations
105 ########################################################START ITERATION
106 while t<tend:
107 i+=1
108 t+=h
109 mypde.setValue(Y=qH+rhocp/h*T)
110 T=mypde.getSolution()
111 totE=integrate(rhocp*T)
112 print("time step %s at t=%e minutes completed. total energy = %e."%(i,t/minute,totE))
113 t_list.append(t)
114 E_list.append(totE)
115
116 #establish figure 1 for temperature vs x plots
117 tempT = T.toListOfTuples()
118 pl.figure(1) #current figure
119 pl.plot(plx,tempT) #plot solution
120 # add title
121 pl.axis([0,mx,Tref*.9,T0*1.1])
122 pl.ylabel('Temperature (K)')
123 pl.xlabel("Length (m)")
124 pl.title("Temperature across rod at time %e hours"%(t/hour))
125 #save figure to file
126 pl.savefig(os.path.join(save_path,"tempT", "rodpyplot%03d.png"%i))
127 pl.clf() #clear figure
128
129 ###############################################################PLOTTING
130 # plot the total energy over time:
131 pl.figure(2)
132 pl.plot(t_list,E_list)
133 pl.title("Total Energy")
134 pl.ylabel('Energy (W)')
135 pl.xlabel('Time (s)')
136 # pl.axis([0,max(t_list),0,max(E_list)*1.1])
137 pl.savefig(os.path.join(save_path,"totE.png"))
138 pl.clf()
139
140 #########################################################CREATE A MOVIE
141 # compile the *.png files to create a*.avi video that show T change
142 # with time. This opperation uses linux mencoder. For other operating
143 # systems it may be possible to use your favourite video compiler to
144 # convert image files to videos. To enable this step uncomment the
145 # following lines.
146
147 #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
148 #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
149 #example02tempT.avi")

  ViewVC Help
Powered by ViewVC 1.1.26