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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3793 - (show annotations)
Wed Feb 1 07:39:43 2012 UTC (7 years, 8 months ago) by gross
File MIME type: text/x-python
File size: 8607 byte(s)
new implementation of FCT solver with some modifications to the python interface
1 # -*- coding: utf-8 -*-
2 ########################################################
3 #
4 # Copyright (c) 2003-2010 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-2010 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 # Flux corrected transport solver benchmark
24 # we are moving a Gaussian hill around
25 #
26 # we solve a* U_{,t} - b *u_{,ii} + c_i u_{,i} + (d_i* u)_{,i}}=0
27 #
28 # U(0)= U0 * exp ( - |x-x_0(t)|^2/(4*s**2) )
29 #
30 # with a>0, b>=0, s>0
31 #
32 # we set E=b/a v=c/a w=d/a
33 #
34 # the solution is given as u(x,t)=U0*s^dim/(s**2+E*t)^{dim/2} * exp ( - |x-x_0(t)|^2/(4*(s**2+E*t)) )
35 #
36 # with x_0(t) = X0 + (v+w)*t
37 #
38 #
39 # the region |x-x_0(t)|^2/(4*(s**2+E*t)) < - log(TAU) is within the domain for all time
40 #
41 #
42 # this holds if
43 #
44 # |x_i-X0_i-(v_i+w_i)*tend | < sqrt(- log(TAU) * 4*(s**2+E*tend))=b0 and
45 # |x_i-X0_i | < sqrt(- log(TAU)) * 2*s = b1 implies 0<=x_i<=l_i
46 #
47 from math import pi, ceil
48 from time import time as clock
49 from esys.finley import Rectangle, Brick
50 from esys.escript import *
51 from esys.escript.linearPDEs import LinearSinglePDE, TransportPDE
52 from esys.weipa import saveVTK
53
54 #
55 DIM=2
56 NE_MAX=300000
57 VERBOSITY=True
58 TOL=1.e-8
59 TAU=1e-10
60 VTK_DIR="output"
61
62 #==================
63 S_MIN=0
64 TABS = [ 'dx', 'dt', 'peclet', 'error', 'sup', 'integral', 'center', 'width', 'time' ]
65
66 a=1.
67 #==================
68
69
70 mkDir(VTK_DIR)
71
72 def uRef(dom,t,E,s,v,x0, onElements=False):
73 if onElements:
74 x=Function(dom).getX()
75 else:
76 x=dom.getX()
77 X=x0[:dom.getDim()]+v[:dom.getDim()]*t
78 u=(s**2/(s**2+E*t))**(dom.getDim()/2.) * exp(-length(x-X)**2/(4*(s**2+E*t)))
79 return u
80
81
82 def getDirection(dim, d="x"):
83 k=kronecker(dim)
84 if d=="x":
85 return k[0]
86 elif d=="y":
87 return k[1]
88 elif d=="z" and dim>2:
89 return k[2]
90 elif d=="xy":
91 return (k[0]+k[1])/sqrt(2.)
92 elif d=="yz":
93 return (k[1]+k[2])/sqrt(2.)
94 elif d=="zx" and dim>2:
95 return (k[2]+k[0])/sqrt(2.)
96 elif d=="xyz" and dim>2:
97 return (k[1]+k[2]+k[0])/sqrt(3.)
98 else:
99 raise ValueError("Cannot identify direction %s"%d)
100
101 def QUALITY(u_h,u_ref):
102 u_h_e=interpolate(u_h,u_ref.getFunctionSpace())
103 x=u_ref.getFunctionSpace().getX()
104 out = {}
105 out["error"]=sqrt(integrate((u_ref-u_h_e)**2))/sqrt(integrate(u_ref**2))
106 out["sup"]=abs(sup(u_h)-sup(u_ref))/abs(sup(u_ref))
107 m0_h=integrate(u_h_e)
108 m1_h=integrate(x*u_h_e)/m0_h
109 m2_h=integrate(length(x-m1_h)**2*u_h_e)
110
111 m0=integrate(u_ref)
112 m1=integrate(x*u_ref)/m0_h
113 m2=integrate(length(x-m1)**2*u_ref)
114
115 out["m0"]=abs(m0_h-m0)/abs(m0)
116 out["m1"]=length(m1_h-m1)/length(m1)
117 out["m2"]=abs(m2_h-m2)/abs(m2)
118
119 return out
120
121 #================
122
123 def XXX(dim,tend,dt, s, h,b,c,d,c_dir="x", d_dir="x", a=1., CN=True):
124 """
125 dim - sparial dimension
126 s - width of initial profile
127 h - mesh size
128 """
129 v_c=c/a*getDirection(dim,c_dir)
130 v_d=d/a*getDirection(dim,d_dir)
131 v = (v_c+v_d)
132 E=b/a
133 if VERBOSITY:
134 print("="*100)
135 print("XX Start test dim = %d , h=%e, b=%e, c=%e, d=%e, c_dir=%s, d_dir=%s, a=%e, s=%e"%(dim, h,b,c,d,c_dir, d_dir, a, s))
136 print("="*100)
137 print("initial width s = ",s)
138 print("diffusion = ",E)
139 print("total velocity = ",v)
140 print("tend = ", tend)
141 print("tolerance = ",TOL)
142 print("number of elements over s =",s/h)
143 b0=sqrt(- log(TAU) * 4*(s**2+E*tend))
144 b1=sqrt(- log(TAU)) * 2*s
145 X0_0=max(b1,-v[0]*tend + b0)
146 X0_1=max(b1,-v[1]*tend + b0)
147 l_0=X0_0+max(v[0]*tend + b0 , b1)
148 l_1=X0_1+max(v[1]*tend + b0 , b1)
149 NE_0=max(int(l_0/h+0.5),1)
150 NE_1=max(int(l_1/h+0.5),1)
151 if dim == 2:
152 if VERBOSITY: print("%d x %d grid over %e x %e with element size %e."%(NE_0,NE_1,l_0,l_1,h))
153 if NE_0*NE_1 > NE_MAX:
154 raise ValueError("too many elements %s."%(NE_0*NE_1,))
155 dom=Rectangle(n0=NE_0,n1=NE_1,l0=l_0,l1=l_1)
156 x0=[X0_0, X0_1]
157 else:
158 X0_2=max(b1,-v[2]*tend + b0)
159 l_2=X0_2+max(v[2]*tend + b0 , b1)
160 NE_2=max(int(l_2/h+0.5),1)
161 if VERBOSITY: print("%d x %d x %d grid over %e x %e x %e with element size %e."%(NE_0,NE_1,NE_2,l_0,l_1,l_2,h))
162 if NE_0*NE_1*NE_2 > NE_MAX:
163 raise ValueError("too many elements %s."%(NE_0*NE_1*NE_2,))
164 dom=Brick(n0=NE_0,n1=NE_1, ne2=NE_2, l0=l_0,l1=l_1, l2=l_2)
165 x0=[X0_0, X0_1, X0_2]
166 if VERBOSITY:
167 print("initial location = ",x0)
168 print("XX", interpolate(uRef(dom,0.,E,s,v,x0), FunctionOnBoundary(dom)))
169
170 fc_BE=TransportPDE(dom,numEquations=1)
171 fc_BE.setValue(M=a, A=-b*kronecker(dom), B=-v_d*a, C=-v_c*a)
172 fc_BE.getSolverOptions().setVerbosity(VERBOSITY)
173 fc_BE.getSolverOptions().setTolerance(TOL)
174 #
175 fc_BE.getSolverOptions().setPreconditioner(fc_BE.getSolverOptions().GAUSS_SEIDEL)
176 fc_BE.getSolverOptions().setNumSweeps(5)
177 if VERBOSITY: print("Backward Euler Transport created")
178
179 fc_CN=TransportPDE(dom,numEquations=1)
180 fc_CN.setValue(M=a, A=-b*kronecker(dom), B=-v_d*a, C=-v_c*a)
181 fc_CN.getSolverOptions().setVerbosity(VERBOSITY)
182 fc_CN.getSolverOptions().setTolerance(TOL)
183
184 #fc_CN.getSolverOptions().setPreconditioner(fc_CN.getSolverOptions().GAUSS_SEIDEL)
185 fc_CN.getSolverOptions().setNumSweeps(2)
186 if VERBOSITY: print("Crank Nicolson Transport created")
187 dt_CN=fc_CN.getSafeTimeStepSize()
188 if VERBOSITY: print("time step size by Crank Nicolson=",dt_CN)
189
190 U0=uRef(dom,0,E,s,v,x0)
191 U0_e=uRef(dom,0,E,s,v,x0,True)
192 fc_CN.setInitialSolution(U0)
193 fc_BE.setInitialSolution(U0)
194 initial_error_L2=sqrt(integrate((U0-U0_e)**2))
195 if VERBOSITY:
196 print("initial Lsup = ",Lsup(U0), Lsup(U0_e))
197 print("initial integral = ",integrate(U0_e))
198 print("initial error = ",initial_error_L2)
199 print("used time step size =",dt)
200
201 if not CN:
202 n=int(ceil(tend/dt))
203 if VERBOSITY:
204 print("Solve Backward Euler:")
205 print("substeps : ",n)
206 t0=clock()
207 for i in range(n): u=fc_BE.getSolution(dt)
208 t0=clock()-t0
209 else:
210 if VERBOSITY: print("Solve Crank Nicolson:")
211 dt=dt_CN
212 t0=clock()
213 u=fc_CN.getSolution(tend)
214 t0=clock()-t0
215 out=QUALITY(u,uRef(dom,tend,E,s,v,x0,True))
216 print("XX", interpolate(uRef(dom,tend,E,s,v,x0), FunctionOnBoundary(dom)))
217 out['time']=t0
218 out['tend']=tend
219 out['dt']=dt
220 out['dx']=h
221 if abs(b)>0:
222 out["peclet"]=length(v)*s/b
223 else:
224 out["peclet"]=9999999.
225 # saveVTK("bb.vtu",u0=U0,u_CN=u_CN, uRef=uRef(dom,dt2,E,s,v,X0) )
226 return out
227
228 # (s, peclet, b, h0) -> error < 0.01
229 test_set = ( (0.05, 1., 1., 0.024), )
230 test_set = ( (0.05, 100000., 1., 0.024), )
231
232 if False:
233 S_MAX=0.5/sqrt(-log(TAU))/2
234 s=0.05
235 peclet = 1000.
236 b=1.
237 c=peclet*b/s
238 h=0.1/4*1.2/1.25
239 dt=6.250000e-10
240
241 print(XXX(DIM,dt,dt,s=s,h=h,b=a*b,c=a*c,d=0,c_dir="x", d_dir="x", CN=True))
242 1/0
243
244 for tst in test_set:
245 s=tst[0]
246 peclet=tst[1]
247 b=tst[2]
248 h0=tst[3]
249 c=peclet*b/s
250
251 # find appropraiate tend:
252 result=XXX(DIM,1e-99,1.,s=s,h=h0,b=a*b,c=a*c,d=0,c_dir="x", d_dir="x", CN=True)
253 tend=result['dt']
254
255 f_test=[ 1 , 2, 4 ]
256 f_test=[ 1, 2, 4, 8 ]
257 out=""
258 tab_name="dt"
259 tab_name="tend"
260 tab_name="error"
261 dt_s=[]
262 for f_h in f_test:
263 out+="h0/%s "%f_h
264 h=h0/f_h
265 result=XXX(DIM,tend,tend,s=s,h=h,b=a*b,c=a*c,d=0,c_dir="x", d_dir="x", CN=True)
266 out+=", %e"%result[tab_name]
267 print("XX",result)
268 dt_s.insert(0,result['dt'])
269 for i in range(len(f_test)-len(dt_s)): out+=", "
270 for dt in dt_s:
271 result=XXX(DIM,tend,dt,s=s,h=h,b=a*b,c=a*c,d=0,c_dir="x", d_dir="x", CN=False)
272 print("XX",result)
273 out+=", %e"%result[tab_name]
274 out+="\n"
275 header="h\dt , "
276 for dt in dt_s: header+=", %e"%dt
277 out=header+"\n"+out
278 print(out)

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26