/[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 2100 - (show annotations)
Wed Nov 26 08:13:00 2008 UTC (10 years, 4 months ago) by gross
File MIME type: text/x-python
File size: 8819 byte(s)
This commit cleans up the incompressible solver and adds a DarcyFlux solver in model module. 
Some documentation for both classes has been added.
The convection code is only linear at the moment.



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, StokesProblemCartesian
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 dom.onMasterProcessor() and 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 dom.MPIBarrier()
37
38
39 import sys
40 import time
41 t1 = time.time()
42
43 extratol=1
44
45 # read options:
46 parser = OptionParser(usage="%prog [-r [DIR]] [-e [NE]]")
47 parser.add_option("-e", "--elements", dest="NE", help="number of elements in one direction.",metavar="NE", default=16)
48 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")
49 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)
50 (options, args) = parser.parse_args()
51 restart=options.restart or (options.restart_dir !=None)
52
53 NE=int(options.NE)
54
55 DIM=2
56 H=1.
57 L=4*H
58 THETA=0.5 # time stepping THETA=0.5 cranck nicolson
59 TOL=1.e-4
60 PERTURBATION=0.1
61 DT=1.e-4
62 DT_MIN=1.e-5
63 T_END=0.1
64 DT_OUT=T_END/500
65 Dn_OUT=2
66 VERBOSE=True
67 create_restartfiles_every_step=10
68 if True:
69 # this is a simple linear Stokes model:
70 RA=1.e6 # Rayleigh number
71 A=22 # Arenious number
72 DI = 0. # dissipation number
73 MU=None
74 ETA0=1.
75 TAU0=None
76 N=None
77 NPL=None
78 ETAP0=ETA0
79 TAUY=None
80 useJAUMANNSTRESS=False
81 # this is a simple linear Stokes model:
82 RA=1.e5 # Rayleigh number
83 A=11 # Arenious number
84 DI = 0. # dissipation number
85 MU=None
86 ETA0=1.
87 TAU0=250.
88 N=None
89 NPL=None
90 ETAP0=ETA0
91 TAUY=TAU0
92 useJAUMANNSTRESS=False
93 else:
94 RA=1.e4 # Rayleigh number
95 A=22 # Arenious number
96 DI = 0. # dissipation number
97 MU=1.e4
98 ETA0=1.
99 TAU0=0.866*10**2.5
100 N=3
101 NPL=14
102 TAUY=TAU0
103 ETAP0=ETA0
104 useJAUMANNSTRESS=True
105
106 print "total number of elements = ",NE**DIM*int(L/H)**(DIM-1)
107
108 #
109 # set up domain:
110 #
111 if restart:
112 if options.restart_dir ==None:
113 restart_files=[]
114 for f in os.listdir("."):
115 if f.startswith("restart"): restart_files.append(f)
116 if len(restart_files)==0:
117 raise IOError,"no restart files"
118 restart_files.sort()
119 f=restart_files[-1]
120 else:
121 f=options.restart_dir
122 print "restart from directory ",f
123 try:
124 dom=LoadMesh("mesh.nc")
125 except:
126 pass
127 FF=open(os.path.join(f,"stamp.%d"%dom.getMPIRank()),"r").read().split(";")
128 t=float(FF[0])
129 t_out=float(FF[1])
130 n_out=int(FF[2])
131 n=int(FF[3])
132 out_count=int(FF[4])
133 dt=float(FF[5])
134 stress=load(os.path.join(f,"stress.nc"),dom)
135 v=load(os.path.join(f,"v.nc"),dom)
136 p=load(os.path.join(f,"p.nc"),dom)
137 T=load(os.path.join(f,"T.nc"),dom)
138 if n>1:
139 dt_a=float(FF[6])
140 a=load(os.path.join(f,"a.nc"),dom)
141 else:
142 dt_a=None
143 a=None
144 if dom.onMasterProcessor(): nusselt_file=open("nusselt.csv","a")
145 else:
146 if DIM==2:
147 dom=Rectangle(int(ceil(L*NE/H)),NE,l0=L,l1=H,order=2, useFullElementOrder=True,optimize=True)
148 else:
149 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)
150 try:
151 dom.dump("mesh.nc")
152 except:
153 pass
154 x=dom.getX()
155 T=Scalar(1,ReducedSolution(dom))
156 for d in range(DIM):
157 if d == DIM-1:
158 T*=sin(x[d]/H*pi)
159 else:
160 T*=cos(x[d]/L*pi)
161
162 T=1.-x[DIM-1]+PERTURBATION*T
163 v=Vector(0,Solution(dom))
164 stress=Tensor(0,Function(dom))
165 x2=ReducedSolution(dom).getX()
166 p=-RA*(x2[DIM-1]-0.5*x2[DIM-1]**2)
167 if dom.onMasterProcessor(): nusselt_file=open("nusselt.csv","w")
168 t=0
169 t_out=0
170 n_out=0
171 n=0
172 out_count=0
173 dt=DT
174 a=None
175 dt_a=None
176
177 vol=integrate(1.,Function(dom))
178 p-=integrate(p)/vol
179 x=dom.getX()
180 #
181 # set up heat problem:
182 #
183 heat=TemperatureCartesian(dom,theta=THETA)
184 heat.setTolerance(TOL*extratol)
185
186 fixed_T_at=whereZero(x[DIM-1])+whereZero(H-x[DIM-1])
187 print "initial Temperature range ",inf(T),sup(T)
188 heat.setInitialTemperature(clip(T,0))
189 heat.setValue(rhocp=Scalar(1.,Function(dom)),k=Scalar(1.,Function(dom)),given_T_mask=fixed_T_at)
190 #
191 # velocity constraints:
192 #
193 x2=ReducedSolution(dom).getX()
194 fixed_v_mask=Vector(0,Solution(dom))
195 for d in range(DIM):
196 if d == DIM-1:
197 ll = H
198 else:
199 ll = L
200 fixed_v_mask+=(whereZero(x[d])+whereZero(x[d]-ll))*unitVector(d,DIM)
201 #
202 # set up velovity problem
203 #
204 sp=StokesProblemCartesian(dom)
205 # ,stress,v,p,t,useJaumannStress=useJAUMANNSTRESS)
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.initialize(eta=ETA0*FF, f=T*(RA*unitVector(DIM-1,DIM)),fixed_u_mask=fixed_v_mask)
220 v,p=sp.solve(v,p,max_iter=20, verbose=VERBOSE, show_details=False)
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=ETA0*FF*length(symmetric(grad(v)))**2 # this propably wroing!
232 print "Xse:",inf(se),sup(se)
233 Nu=1.+integrate(se)/(RA*vol)
234 if dom.onMasterProcessor(): 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 dom.onMasterProcessor() and not os.path.isdir(new_restart_dir): os.mkdir(new_restart_dir)
268 dom.MPIBarrier()
269 # sp.getStress().dump(os.path.join(new_restart_dir,"stress.nc"))
270 v.dump(os.path.join(new_restart_dir,"v.nc"))
271 p.dump(os.path.join(new_restart_dir,"p.nc"))
272 T.dump(os.path.join(new_restart_dir,"T.nc"))
273 if n>1:
274 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))
275 a.dump(os.path.join(new_restart_dir,"a.nc"))
276 else:
277 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))
278 removeRestartDirectory(old_restart_dir)
279 elapsed = time.time() - t1
280 print "plot","\t",NE,"\t",elapsed
281

  ViewVC Help
Powered by ViewVC 1.1.26