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

  ViewVC Help
Powered by ViewVC 1.1.26