/[escript]/trunk/finley/test/python/convection.py
ViewVC logotype

Contents of /trunk/finley/test/python/convection.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1841 - (show annotations)
Fri Oct 3 03:57:52 2008 UTC (11 years, 9 months ago) by gross
File MIME type: text/x-python
File size: 8808 byte(s)
modification on LinearPDE class and a first version of Transport class
1
2 ########################################################
3 #
4 # Copyright (c) 2003-2008 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) 2003-2008 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__="http://www.uq.edu.au/esscc/escript-finley"
21
22 from esys.escript import *
23 from esys.escript.models import TemperatureCartesian, PlateMantelModel
24 from esys.finley import Rectangle, Brick, LoadMesh
25 from optparse import OptionParser
26 from math import pi, ceil
27
28
29 def removeRestartDirectory(dir_name):
30 if os.path.isdir(dir_name):
31 for root, dirs, files in os.walk(dir_name, topdown=False):
32 for name in files: os.remove(os.path.join(root,name))
33 for name in dirs: os.remove(os.path.join(root,name))
34 os.rmdir(dir_name)
35 print "Restart files %s have been removed."%dir_name
36
37
38 import sys
39 import time
40 t1 = time.time()
41
42 extratol=1
43
44 # read options:
45 parser = OptionParser(usage="%prog [-r [DIR]] [-e [NE]] [-s [solver]]")
46 parser.add_option("-s", "--solver", dest="solver", help="solver to be used for saddle point problem. The possible options are PCG, GMRES, NewtonGMRES, MINRES and TFQMR.", metavar="solver", default="PCG")
47 parser.add_option("-e", "--elements", dest="NE", help="number of elements in one direction.",metavar="NE", default=16)
48
49 parser.add_option("-r", "--restart", dest="restart", help="restart from latest directory. It will be deleted after a new directory has been created.", default=False, action="store_true")
50 parser.add_option("-d", "--dir", dest="restart_dir", help="restart from directory DIR. The directory will not be deleted but new restart directories are created.",metavar="DIR", default=None)
51 (options, args) = parser.parse_args()
52 restart=options.restart or (options.restart_dir !=None)
53
54 solver=options.solver
55 NE=int(options.NE)
56
57 DIM=2
58 H=1.
59 L=4*H
60 THETA=0.5 # time stepping THETA=0.5 cranck nicolson
61 TOL=1.e-4
62 PERTURBATION=0.1
63 DT=1.e-4
64 DT_MIN=1.e-5
65 T_END=0.1
66 DT_OUT=T_END/500
67 Dn_OUT=2
68 VERBOSE=True
69 create_restartfiles_every_step=10
70 if True:
71 # this is a simple linear Stokes model:
72 RA=1.e6 # Rayleigh number
73 A=22 # Arenious number
74 DI = 0. # dissipation number
75 MU=None
76 ETA0=1.
77 TAU0=None
78 N=None
79 NPL=None
80 ETAP0=ETA0
81 TAUY=None
82 useJAUMANNSTRESS=False
83 # this is a simple linear Stokes model:
84 RA=1.e5 # Rayleigh number
85 A=0 # Arenious number
86 DI = 0. # dissipation number
87 MU=None
88 ETA0=1.
89 TAU0=250.
90 N=None
91 NPL=None
92 ETAP0=ETA0
93 TAUY=TAU0
94 useJAUMANNSTRESS=False
95 else:
96 RA=1.e4 # Rayleigh number
97 A=22 # Arenious number
98 DI = 0. # dissipation number
99 MU=1.e4
100 ETA0=1.
101 TAU0=0.866*10**2.5
102 N=3
103 NPL=14
104 TAUY=TAU0
105 ETAP0=ETA0
106 useJAUMANNSTRESS=True
107
108 print "total number of elements = ",NE**DIM*int(L/H)**(DIM-1)
109
110 #
111 # set up domain:
112 #
113 if restart:
114 if options.restart_dir ==None:
115 restart_files=[]
116 for f in os.listdir("."):
117 if f.startswith("restart"): restart_files.append(f)
118 if len(restart_files)==0:
119 raise IOError,"no restart files"
120 restart_files.sort()
121 f=restart_files[-1]
122 else:
123 f=options.restart_dir
124 print "restart from directory ",f
125 try:
126 dom=LoadMesh("mesh.nc")
127 except:
128 pass
129 FF=open(os.path.join(f,"stamp.%d"%dom.getMPIRank()),"r").read().split(";")
130 t=float(FF[0])
131 t_out=float(FF[1])
132 n_out=int(FF[2])
133 n=int(FF[3])
134 out_count=int(FF[4])
135 dt=float(FF[5])
136 stress=load(os.path.join(f,"stress.nc"),dom)
137 v=load(os.path.join(f,"v.nc"),dom)
138 p=load(os.path.join(f,"p.nc"),dom)
139 T=load(os.path.join(f,"T.nc"),dom)
140 if n>1:
141 dt_a=float(FF[6])
142 a=load(os.path.join(f,"a.nc"),dom)
143 else:
144 dt_a=None
145 a=None
146 if dom.getMPIRank()==0: nusselt_file=open("nusselt.csv","a")
147 else:
148 if DIM==2:
149 dom=Rectangle(int(ceil(L*NE/H)),NE,l0=L,l1=H,order=2, useFullElementOrder=True,optimize=True)
150 else:
151 dom=Brick(int(ceil(L*NE/H)),int(ceil(L*NE/H)),NE,l0=L,l1=L,l2=H,order=2, useFullElementOrder=True,optimize=True)
152 try:
153 dom.dump("mesh.nc")
154 except:
155 pass
156 x=dom.getX()
157 T=Scalar(1,ReducedSolution(dom))
158 for d in range(DIM):
159 if d == DIM-1:
160 T*=sin(x[d]/H*pi)
161 else:
162 T*=cos(x[d]/L*pi)
163
164 T=1.-x[DIM-1]+PERTURBATION*T
165 v=Vector(0,Solution(dom))
166 stress=Tensor(0,Function(dom))
167 x2=ReducedSolution(dom).getX()
168 p=-RA*(x2[DIM-1]-0.5*x2[DIM-1]**2)
169 if dom.getMPIRank() ==0: nusselt_file=open("nusselt.csv","w")
170 t=0
171 t_out=0
172 n_out=0
173 n=0
174 out_count=0
175 dt=DT
176 a=None
177 dt_a=None
178
179 vol=integrate(1.,Function(dom))
180 p-=integrate(p)/vol
181 x=dom.getX()
182 #
183 # set up heat problem:
184 #
185 heat=TemperatureCartesian(dom,theta=THETA)
186 heat.setTolerance(TOL*extratol)
187
188 fixed_T_at=whereZero(x[DIM-1])+whereZero(H-x[DIM-1])
189 print "initial Temperature range ",inf(T),sup(T)
190 heat.setInitialTemperature(clip(T,0))
191 heat.setValue(rhocp=Scalar(1.,Function(dom)),k=Scalar(1.,Function(dom)),given_T_mask=fixed_T_at)
192 #
193 # velocity constraints:
194 #
195 x2=ReducedSolution(dom).getX()
196 fixed_v_mask=Vector(0,Solution(dom))
197 for d in range(DIM):
198 if d == DIM-1:
199 ll = H
200 else:
201 ll = L
202 fixed_v_mask+=(whereZero(x[d])+whereZero(x[d]-ll))*unitVector(d,DIM)
203 #
204 # set up velovity problem
205 #
206 sp=PlateMantelModel(dom,stress,v,p,t,useJaumannStress=useJAUMANNSTRESS)
207 sp.initialize(mu=MU, tau_0=TAU0, n=N, eta_Y=ETAP0, tau_Y=TAU0, n_Y=NPL, q=fixed_v_mask)
208 sp.setTolerance(TOL*extratol)
209 sp.setToleranceReductionFactor(TOL)
210 #
211 # let the show begin:
212 #
213 while t<T_END:
214 v_last=v*1
215 print "============== solve for v ========================"
216 FF=exp(A*(1./(1+T.interpolate(Function(dom)))-1./2.))
217 print "viscosity range :", inf(FF)*ETA0, sup(FF)*ETA0
218 sp.initialize(eta_N=ETA0*FF, eta_0=ETA0*FF, F=T*(RA*unitVector(DIM-1,DIM)))
219 sp.update(dt,max_inner_iter=20, verbose=VERBOSE, show_details=False, tol=10., solver=solver)
220 v=sp.getVelocity()
221
222 for d in range(DIM):
223 print "range %d-velocity"%d,inf(v[d]),sup(v[d])
224 if t>=t_out or n>n_out:
225 saveVTK("state.%d.vtu"%out_count,T=T,v=v)
226 print "visualization file %d for time step %e generated."%(out_count,t)
227 out_count+=1
228 t_out+=DT_OUT
229 n_out+=Dn_OUT
230 # calculation of nusselt number:
231 se=sp.getMechanicalPower()
232 print "Xse:",inf(se),sup(se)
233 Nu=1.+integrate(se)/(RA*vol)
234 if dom.getMPIRank() ==0: nusselt_file.write("%e %e\n"%(t,Nu))
235 heat.setValue(v=interpolate(v,ReducedSolution(dom)),Q=DI/RA*se)
236 print "Xnusselt number = ",Nu, "dt =",dt
237 if n>0:
238 a,a_alt = (v_last-v)/dt, a
239 dt_a,dt_a_alt = dt, dt_a
240 if n>1:
241 z=(a-a_alt)/((dt_a+dt_a_alt)/2)
242 f=Lsup(z)/Lsup(v)
243 print "estimated error ",f*dt**2
244 # dt_new=min(2*dt,max(dt/2,sqrt(0.05/f)))
245 dt_new=sqrt(0.05/f)
246 dt=min(dt_new,heat.getSafeTimeStepSize())
247 else:
248 dt=heat.getSafeTimeStepSize()
249 dt=max(DT_MIN,dt)
250 print n,". time step t=",t," step size ",dt
251 print "============== solve for T ========================"
252 T=heat.getSolution(dt, verbose=VERBOSE)
253 print "Temperature range ",inf(T),sup(T)
254 n+=1
255 t+=dt
256 # =========================
257 #
258 # create restart files:
259 #
260 if create_restartfiles_every_step>0:
261 if (n-1)%create_restartfiles_every_step == 0:
262 c=(n-1)/create_restartfiles_every_step
263 old_restart_dir="restart_%s_"%(c-1)
264 new_restart_dir="restart_%s_"%c
265
266 print "Write restart files to ",new_restart_dir
267 if not os.path.isdir(new_restart_dir): os.mkdir(new_restart_dir)
268 sp.getStress().dump(os.path.join(new_restart_dir,"stress.nc"))
269 sp.getVelocity().dump(os.path.join(new_restart_dir,"v.nc"))
270 sp.getPressure().dump(os.path.join(new_restart_dir,"p.nc"))
271 T.dump(os.path.join(new_restart_dir,"T.nc"))
272 if n>1:
273 file(os.path.join(new_restart_dir,"stamp.%d"%dom.getMPIRank()),"w").write("%e; %e; %s; %s; %s; %e; %e;\n"%(t, t_out, n_out, n, out_count, dt, dt_a))
274 a.dump(os.path.join(new_restart_dir,"a.nc"))
275 else:
276 file(os.path.join(new_restart_dir,"stamp.%d"%dom.getMPIRank()),"w").write("%e; %e; %s; %s; %s; %e;\n"%(t, t_out, n_out, n, out_count, dt))
277 removeRestartDirectory(old_restart_dir)
278 elapsed = time.time() - t1
279 print "plot","\t",NE,"\t",elapsed
280

  ViewVC Help
Powered by ViewVC 1.1.26