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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5288 - (hide annotations)
Tue Dec 2 23:18:40 2014 UTC (4 years, 10 months ago) by sshaw
File MIME type: text/x-python
File size: 6113 byte(s)
fixing tests for cases where required domains not built
1 sshaw 5288 from __future__ import division, print_function
2 jfenwick 3981 ##############################################################################
3 gross 2904 #
4 jfenwick 4657 # Copyright (c) 2009-2014 by University of Queensland
5 jfenwick 3981 # http://www.uq.edu.au
6 gross 2904 #
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 jfenwick 3981 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 jfenwick 4657 # Development 2012-2013 by School of Earth Sciences
13     # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 jfenwick 3981 #
15     ##############################################################################
16 gross 2904
17 jfenwick 4657 __copyright__="""Copyright (c) 2009-2014 by University of Queensland
18 jfenwick 3981 http://www.uq.edu.au
19 gross 2904 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 ahallam 2977 ############################################################FILE HEADER
29     # example01c.py
30     # Model temperature diffusion between two granite blocks of unequal
31     # initial temperature. Solve for the spatial distribution of temperature.
32    
33 gross 2904 # To solve the problem it is necessary to import the modules we require.
34 caltinay 4087 #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 gross 2904 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     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 sshaw 5288 try:
44     # This imports the rectangle domain function
45     from esys.finley import MakeDomain#Converter for escript
46     HAVE_FINLEY = True
47     except ImportError:
48     print("Finley module not available")
49     HAVE_FINLEY = False
50 gross 2904
51 ahallam 2977 ########################################################MPI WORLD CHECK
52 gross 2904 if getMPISizeWorld() > 1:
53 sshaw 5288 import sys
54     print("This example will not run in an MPI world.")
55     sys.exit(0)
56 gross 2904
57 sshaw 5288 if HAVE_FINLEY:
58     #################################################ESTABLISHING VARIABLES
59     #Domain related.
60     mx = 500*m #meters - model length
61     my = 100*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     boundloc = mx/2 # location of boundary between the two blocks
65     #PDE related
66     rho = 2750. *kg/m**3 #kg/m{3} density of iron
67     cp = 790.*J/(kg*K) # J/Kg.K thermal capacity
68     rhocp = rho*cp
69     kappa = 2.2*W/m/K # watts/m.Kthermal conductivity
70     qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source
71     T1=20 * Celsius # initial temperature at Block 1
72     T2=2273. * Celsius # base temperature at Block 2
73 gross 2904
74 sshaw 5288 ################################################ESTABLISHING PARAMETERS
75     t=0 * day # our start time, usually zero
76     tend=50 * yr # - time to end simulation
77     outputs = 200 # number of time steps required.
78     h=(tend-t)/outputs #size of time step
79     #user warning statement
80     print("Expected Number of time outputs is: ", (tend-t)/h)
81     i=0 #loop counter
82     #the folder to put our outputs in, leave blank "" for script path
83     save_path= os.path.join("data","example01")
84     #ensure the dir exists
85     mkDir(save_path, os.path.join(save_path,"tempT"))
86 gross 2904
87 sshaw 5288 ####################################################DOMAIN CONSTRUCTION
88     blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
89 ahallam 2977
90 sshaw 5288 ###############################################ESCRIPT PDE CONSTRUCTION
91     #... open PDE and set coefficients ...
92     mypde=LinearPDE(blocks)
93     mypde.setSymmetryOn()
94     A=zeros((2,2))
95     A[0,0]=kappa
96     mypde.setValue(A=A,D=rhocp/h)
97     # ... set initial temperature ....
98     x=Solution(blocks).getX()
99     T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))
100 gross 2904
101 sshaw 5288 # ... open a collector for the time marks and corresponding total energy
102     t_list=[]
103     E_list=[]
104     # ... convert solution points for plotting
105     plx = x.toListOfTuples()
106     plx = np.array(plx) #convert to tuple to numpy array
107     plx = plx[:,0] #extract x locations
108     ########################################################START ITERATION
109     while t<tend:
110     i+=1
111     t+=h
112     mypde.setValue(Y=qH+rhocp/h*T)
113     T=mypde.getSolution()
114     totE=integrate(rhocp*T)
115     print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))
116     t_list.append(t)
117     E_list.append(totE)
118 gross 2904
119 sshaw 5288 #establish figure 1 for temperature vs x plots
120     tempT = T.toListOfTuples()
121     pl.figure(1) #current figure
122     pl.plot(plx,tempT) #plot solution
123     # add title
124     pl.axis([0,mx,T1*.9,T2*1.1])
125     pl.title("Temperature across blocks at time %d days"%(t/day))
126     pl.ylabel('Temperature (K)')
127     pl.xlabel("Length (m)")
128     #save figure to file
129     pl.savefig(os.path.join(save_path,"tempT", "blockspyplot%03d.png"%i))
130     pl.clf() #clear figure
131    
132     ###############################################################PLOTTING
133     # plot the total energy over time:
134     pl.figure(2)
135     pl.plot(t_list,E_list)
136     pl.title("Total Energy")
137     pl.axis([0,max(t_list),0,max(E_list)*1.1])
138     pl.ylabel('Energy (W)')
139     pl.xlabel('Time (s)')
140     pl.savefig(os.path.join(save_path,"totE_ex01c.png"))
141     pl.clf()
142 gross 2904
143 sshaw 5288 ###########################################################MAKE A MOVIE
144     # compile the *.png files to create a*.avi video that show T change
145     # with time. This opperation uses linux mencoder. For other operating
146     # systems it may be possible to use your favourite video compiler to
147     # convert image files to videos. To enable this step uncomment the
148     # following lines.
149 gross 2904
150 sshaw 5288 #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
151     #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
152     #example01tempT.avi")

  ViewVC Help
Powered by ViewVC 1.1.26