/[escript]/trunk/escript/py_src/flows.py
ViewVC logotype

Contents of /trunk/escript/py_src/flows.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1659 - (show annotations)
Fri Jul 18 02:28:13 2008 UTC (11 years, 4 months ago) by gross
File MIME type: text/x-python
File size: 9166 byte(s)
some first version of a robust level set
1 # $Id:$
2 #
3 #######################################################
4 #
5 # Copyright 2008 by University of Queensland
6 #
7 # http://esscc.uq.edu.au
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
15 """
16 Some models for flow
17
18 @var __author__: name of author
19 @var __copyright__: copyrights
20 @var __license__: licence agreement
21 @var __url__: url entry point on documentation
22 @var __version__: version
23 @var __date__: date of the version
24 """
25
26 __author__="Lutz Gross, l.gross@uq.edu.au"
27 __copyright__=""" Copyright (c) 2008 by ACcESS MNRF
28 http://www.access.edu.au
29 Primary Business: Queensland, Australia"""
30 __license__="""Licensed under the Open Software License version 3.0
31 http://www.opensource.org/licenses/osl-3.0.php"""
32 __url__="http://www.iservo.edu.au/esys"
33 __version__="$Revision:$"
34 __date__="$Date:$"
35
36 from escript import *
37 import util
38 from linearPDEs import LinearPDE
39 from pdetools import HomogeneousSaddlePointProblem,Projector
40
41 class StokesProblemCartesian_DC(HomogeneousSaddlePointProblem):
42 """
43 solves
44
45 -(eta*(u_{i,j}+u_{j,i}))_j - p_i = f_i
46 u_{i,i}=0
47
48 u=0 where fixed_u_mask>0
49 eta*(u_{i,j}+u_{j,i})*n_j=surface_stress
50
51 if surface_stress is not give 0 is assumed.
52
53 typical usage:
54
55 sp=StokesProblemCartesian(domain)
56 sp.setTolerance()
57 sp.initialize(...)
58 v,p=sp.solve(v0,p0)
59 """
60 def __init__(self,domain,**kwargs):
61 HomogeneousSaddlePointProblem.__init__(self,**kwargs)
62 self.domain=domain
63 self.vol=util.integrate(1.,Function(self.domain))
64 self.__pde_u=LinearPDE(domain,numEquations=self.domain.getDim(),numSolutions=self.domain.getDim())
65 self.__pde_u.setSymmetryOn()
66 self.__pde_u.setSolverMethod(preconditioner=LinearPDE.ILU0)
67
68 self.__pde_proj=LinearPDE(domain,numEquations=1,numSolutions=1)
69 self.__pde_proj.setReducedOrderOn()
70 self.__pde_proj.setSymmetryOn()
71 # self.__pde_proj.setSolverMethod(LinearPDE.LUMPING)
72
73 def initialize(self,f=Data(),fixed_u_mask=Data(),eta=1,surface_stress=Data()):
74 self.eta=eta
75 A =self.__pde_u.createCoefficientOfGeneralPDE("A")
76 self.__pde_u.setValue(A=Data())
77 for i in range(self.domain.getDim()):
78 for j in range(self.domain.getDim()):
79 A[i,j,j,i] += 1.
80 A[i,j,i,j] += 1.
81 # self.__inv_eta=util.interpolate(self.eta,ReducedFunction(self.domain))
82 self.__pde_u.setValue(A=A*self.eta,q=fixed_u_mask,Y=f,y=surface_stress)
83
84 self.__pde_proj.setValue(D=1/eta)
85 self.__pde_proj.setValue(Y=1.)
86 self.__inv_eta=util.interpolate(self.__pde_proj.getSolution(),ReducedFunction(self.domain))
87
88 def B(self,arg):
89 a=util.div(arg, ReducedFunction(self.domain))
90 return a-util.integrate(a)/self.vol
91
92 def inner(self,p0,p1):
93 return util.integrate(p0*p1)
94
95 def getStress(self,u):
96 mg=util.grad(u)
97 return 2.*self.eta*util.symmetric(mg)
98 def getEtaEffective(self):
99 return self.eta
100
101 def solve_A(self,u,p):
102 """
103 solves Av=f-Au-B^*p (v=0 on fixed_u_mask)
104 """
105 self.__pde_u.setTolerance(self.getSubProblemTolerance())
106 self.__pde_u.setValue(X=-self.getStress(u),X_reduced=-p*util.kronecker(self.domain))
107 return self.__pde_u.getSolution(verbose=self.show_details)
108
109
110 def solve_prec(self,p):
111 a=self.__inv_eta*p
112 return a-util.integrate(a)/self.vol
113
114 def stoppingcriterium(self,Bv,v,p):
115 n_r=util.sqrt(self.inner(Bv,Bv))
116 n_v=util.sqrt(util.integrate(util.length(util.grad(v))**2))
117 if self.verbose: print "PCG step %s: L2(div(v)) = %s, L2(grad(v))=%s"%(self.iter,n_r,n_v) , util.Lsup(v)
118 if self.iter == 0: self.__n_v=n_v;
119 self.__n_v, n_v_old =n_v, self.__n_v
120 self.iter+=1
121 if self.iter>1 and n_r <= n_v*self.getTolerance() and abs(n_v_old-self.__n_v) <= n_v * self.getTolerance():
122 if self.verbose: print "PCG terminated after %s steps."%self.iter
123 return True
124 else:
125 return False
126
127
128 class StokesProblemCartesian(HomogeneousSaddlePointProblem):
129 """
130 solves
131
132 -(eta*(u_{i,j}+u_{j,i}))_j - p_i = f_i
133 u_{i,i}=0
134
135 u=0 where fixed_u_mask>0
136 eta*(u_{i,j}+u_{j,i})*n_j=surface_stress
137
138 if surface_stress is not give 0 is assumed.
139
140 typical usage:
141
142 sp=StokesProblemCartesian(domain)
143 sp.setTolerance()
144 sp.initialize(...)
145 v,p=sp.solve(v0,p0)
146 """
147 def __init__(self,domain,**kwargs):
148 HomogeneousSaddlePointProblem.__init__(self,**kwargs)
149 self.domain=domain
150 self.vol=util.integrate(1.,Function(self.domain))
151 self.__pde_u=LinearPDE(domain,numEquations=self.domain.getDim(),numSolutions=self.domain.getDim())
152 self.__pde_u.setSymmetryOn()
153 # self.__pde_u.setSolverMethod(preconditioner=LinearPDE.ILU0)
154
155 self.__pde_prec=LinearPDE(domain)
156 self.__pde_prec.setReducedOrderOn()
157 self.__pde_prec.setSymmetryOn()
158
159 self.__pde_proj=LinearPDE(domain)
160 self.__pde_proj.setReducedOrderOn()
161 self.__pde_proj.setSymmetryOn()
162 self.__pde_proj.setValue(D=1.)
163
164 def initialize(self,f=Data(),fixed_u_mask=Data(),eta=1,surface_stress=Data()):
165 self.eta=eta
166 A =self.__pde_u.createCoefficientOfGeneralPDE("A")
167 self.__pde_u.setValue(A=Data())
168 for i in range(self.domain.getDim()):
169 for j in range(self.domain.getDim()):
170 A[i,j,j,i] += 1.
171 A[i,j,i,j] += 1.
172 self.__pde_prec.setValue(D=1/self.eta)
173 self.__pde_u.setValue(A=A*self.eta,q=fixed_u_mask,Y=f,y=surface_stress)
174
175 def B(self,arg):
176 d=util.div(arg)
177 self.__pde_proj.setValue(Y=d)
178 self.__pde_proj.setTolerance(self.getSubProblemTolerance())
179 return self.__pde_proj.getSolution(verbose=self.show_details)
180
181 def inner(self,p0,p1):
182 s0=util.interpolate(p0,Function(self.domain))
183 s1=util.interpolate(p1,Function(self.domain))
184 return util.integrate(s0*s1)
185
186 def inner_a(self,a0,a1):
187 p0=util.interpolate(a0[1],Function(self.domain))
188 p1=util.interpolate(a1[1],Function(self.domain))
189 alfa=(1/self.vol)*util.integrate(p0)
190 beta=(1/self.vol)*util.integrate(p1)
191 v0=util.grad(a0[0])
192 v1=util.grad(a1[0])
193 return util.integrate((p0-alfa)*(p1-beta)+((1/self.eta)**2)*util.inner(v0,v1))
194
195
196 def getStress(self,u):
197 mg=util.grad(u)
198 return 2.*self.eta*util.symmetric(mg)
199 def getEtaEffective(self):
200 return self.eta
201
202 def solve_A(self,u,p):
203 """
204 solves Av=f-Au-B^*p (v=0 on fixed_u_mask)
205 """
206 self.__pde_u.setTolerance(self.getSubProblemTolerance())
207 self.__pde_u.setValue(X=-self.getStress(u)-p*util.kronecker(self.domain))
208 return self.__pde_u.getSolution(verbose=self.show_details)
209
210
211 def solve_prec(self,p):
212 #proj=Projector(domain=self.domain, reduce = True, fast=False)
213 self.__pde_prec.setTolerance(self.getSubProblemTolerance())
214 self.__pde_prec.setValue(Y=p)
215 q=self.__pde_prec.getSolution(verbose=self.show_details)
216 return q
217
218 def solve_prec1(self,p):
219 #proj=Projector(domain=self.domain, reduce = True, fast=False)
220 self.__pde_prec.setTolerance(self.getSubProblemTolerance())
221 self.__pde_prec.setValue(Y=p)
222 q=self.__pde_prec.getSolution(verbose=self.show_details)
223 q0=util.interpolate(q,Function(self.domain))
224 print util.inf(q*q0),util.sup(q*q0)
225 q-=(1/self.vol)*util.integrate(q0)
226 print util.inf(q*q0),util.sup(q*q0)
227 return q
228
229 def stoppingcriterium(self,Bv,v,p):
230 n_r=util.sqrt(self.inner(Bv,Bv))
231 n_v=util.sqrt(util.integrate(util.length(util.grad(v))**2))
232 if self.verbose: print "PCG step %s: L2(div(v)) = %s, L2(grad(v))=%s"%(self.iter,n_r,n_v)
233 if self.iter == 0: self.__n_v=n_v;
234 self.__n_v, n_v_old =n_v, self.__n_v
235 self.iter+=1
236 if self.iter>1 and n_r <= n_v*self.getTolerance() and abs(n_v_old-self.__n_v) <= n_v * self.getTolerance():
237 if self.verbose: print "PCG terminated after %s steps."%self.iter
238 return True
239 else:
240 return False
241 def stoppingcriterium2(self,norm_r,norm_b,solver='GMRES',TOL=None):
242 if TOL==None:
243 TOL=self.getTolerance()
244 if self.verbose: print "%s step %s: L2(r) = %s, L2(b)*TOL=%s"%(solver,self.iter,norm_r,norm_b*TOL)
245 self.iter+=1
246
247 if norm_r <= norm_b*TOL:
248 if self.verbose: print "%s terminated after %s steps."%(solver,self.iter)
249 return True
250 else:
251 return False
252
253

  ViewVC Help
Powered by ViewVC 1.1.26