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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3892 - (show annotations)
Tue Apr 10 08:57:23 2012 UTC (6 years, 9 months ago) by jfenwick
File MIME type: text/x-python
File size: 5422 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 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 ############################################################FILE HEADER
27 # example01c.py
28 # Model temperature diffusion between two granite blocks of unequal
29 # initial temperature. Solve for the spatial distribution of temperature.
30
31 # To solve the problem it is necessary to import the modules we require.
32 from esys.escript import * # This imports everything from the escript library
33 from esys.escript.unitsSI import *
34 from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE
35 from esys.finley import Rectangle # This imports the rectangle domain function
36 #For interactive use, you can comment out the next two lines
37 import matplotlib
38 matplotlib.use('agg') #It's just here for automated testing
39 import pylab as pl #Plotting package.
40 import numpy as np #Array package.
41 import os, sys #This package is necessary to handle saving our data.
42
43 ########################################################MPI WORLD CHECK
44 if getMPISizeWorld() > 1:
45 import sys
46 print("This example will not run in an MPI world.")
47 sys.exit(0)
48
49 #################################################ESTABLISHING VARIABLES
50 #Domain related.
51 mx = 500*m #meters - model length
52 my = 100*m #meters - model width
53 ndx = 100 # mesh steps in x direction
54 ndy = 1 # mesh steps in y direction - one dimension means one element
55 boundloc = mx/2 # location of boundary between the two blocks
56 #PDE related
57 rho = 2750. *kg/m**3 #kg/m{3} density of iron
58 cp = 790.*J/(kg*K) # J/Kg.K thermal capacity
59 rhocp = rho*cp
60 kappa = 2.2*W/m/K # watts/m.Kthermal conductivity
61 qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source
62 T1=20 * Celsius # initial temperature at Block 1
63 T2=2273. * Celsius # base temperature at Block 2
64
65 ################################################ESTABLISHING PARAMETERS
66 t=0 * day # our start time, usually zero
67 tend=50 * yr # - time to end simulation
68 outputs = 200 # number of time steps required.
69 h=(tend-t)/outputs #size of time step
70 #user warning statement
71 print("Expected Number of time outputs is: ", (tend-t)/h)
72 i=0 #loop counter
73 #the folder to put our outputs in, leave blank "" for script path
74 save_path= os.path.join("data","example01")
75 #ensure the dir exists
76 mkDir(save_path, os.path.join(save_path,"tempT"))
77
78 ####################################################DOMAIN CONSTRUCTION
79 blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
80
81 ###############################################ESCRIPT PDE CONSTRUCTION
82 #... open PDE and set coefficients ...
83 mypde=LinearPDE(blocks)
84 mypde.setSymmetryOn()
85 A=zeros((2,2))
86 A[0,0]=kappa
87 mypde.setValue(A=A,D=rhocp/h)
88 # ... set initial temperature ....
89 x=Solution(blocks).getX()
90 T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))
91
92 # ... open a collector for the time marks and corresponding total energy
93 t_list=[]
94 E_list=[]
95 # ... convert solution points for plotting
96 plx = x.toListOfTuples()
97 plx = np.array(plx) #convert to tuple to numpy array
98 plx = plx[:,0] #extract x locations
99 ########################################################START ITERATION
100 while t<tend:
101 i+=1
102 t+=h
103 mypde.setValue(Y=qH+rhocp/h*T)
104 T=mypde.getSolution()
105 totE=integrate(rhocp*T)
106 print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))
107 t_list.append(t)
108 E_list.append(totE)
109
110 #establish figure 1 for temperature vs x plots
111 tempT = T.toListOfTuples()
112 pl.figure(1) #current figure
113 pl.plot(plx,tempT) #plot solution
114 # add title
115 pl.axis([0,mx,T1*.9,T2*1.1])
116 pl.title("Temperature across blocks at time %d days"%(t/day))
117 pl.ylabel('Temperature (K)')
118 pl.xlabel("Length (m)")
119 #save figure to file
120 pl.savefig(os.path.join(save_path,"tempT", "blockspyplot%03d.png"%i))
121 pl.clf() #clear figure
122
123 ###############################################################PLOTTING
124 # plot the total energy over time:
125 pl.figure(2)
126 pl.plot(t_list,E_list)
127 pl.title("Total Energy")
128 pl.axis([0,max(t_list),0,max(E_list)*1.1])
129 pl.ylabel('Energy (W)')
130 pl.xlabel('Time (s)')
131 pl.savefig(os.path.join(save_path,"totE_ex01c.png"))
132 pl.clf()
133
134 ###########################################################MAKE A MOVIE
135 # compile the *.png files to create a*.avi video that show T change
136 # with time. This opperation uses linux mencoder. For other operating
137 # systems it may be possible to use your favourite video compiler to
138 # convert image files to videos. To enable this step uncomment the
139 # following lines.
140
141 #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
142 #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
143 #example01tempT.avi")

  ViewVC Help
Powered by ViewVC 1.1.26