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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1659 - (hide annotations)
Fri Jul 18 02:28:13 2008 UTC (11 years, 2 months ago) by gross
File MIME type: text/x-python
File size: 9166 byte(s)
some first version of a robust level set
1 gross 1414 # $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 artak 1550 from pdetools import HomogeneousSaddlePointProblem,Projector
40 gross 1414
41 gross 1659 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 gross 1414 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 gross 1639 # self.__pde_u.setSolverMethod(preconditioner=LinearPDE.ILU0)
154 gross 1414
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 artak 1554 self.__pde_prec.setValue(D=1/self.eta)
173 gross 1414 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 artak 1550 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 gross 1414 def getStress(self,u):
197     mg=util.grad(u)
198     return 2.*self.eta*util.symmetric(mg)
199 gross 1639 def getEtaEffective(self):
200     return self.eta
201 gross 1414
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 gross 1470 self.__pde_u.setValue(X=-self.getStress(u)-p*util.kronecker(self.domain))
208 gross 1414 return self.__pde_u.getSolution(verbose=self.show_details)
209    
210 artak 1550
211 gross 1414 def solve_prec(self,p):
212 artak 1550 #proj=Projector(domain=self.domain, reduce = True, fast=False)
213 gross 1414 self.__pde_prec.setTolerance(self.getSubProblemTolerance())
214     self.__pde_prec.setValue(Y=p)
215     q=self.__pde_prec.getSolution(verbose=self.show_details)
216 artak 1554 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 artak 1550 q0=util.interpolate(q,Function(self.domain))
224 gross 1562 print util.inf(q*q0),util.sup(q*q0)
225 artak 1550 q-=(1/self.vol)*util.integrate(q0)
226 gross 1562 print util.inf(q*q0),util.sup(q*q0)
227 gross 1414 return q
228 artak 1550
229 gross 1414 def stoppingcriterium(self,Bv,v,p):
230     n_r=util.sqrt(self.inner(Bv,Bv))
231 gross 1552 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 gross 1414 self.iter+=1
236 gross 1552 if self.iter>1 and n_r <= n_v*self.getTolerance() and abs(n_v_old-self.__n_v) <= n_v * self.getTolerance():
237 gross 1414 if self.verbose: print "PCG terminated after %s steps."%self.iter
238     return True
239     else:
240     return False
241 artak 1519 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 artak 1465 self.iter+=1
246 artak 1519
247     if norm_r <= norm_b*TOL:
248 artak 1517 if self.verbose: print "%s terminated after %s steps."%(solver,self.iter)
249 artak 1465 return True
250     else:
251     return False
252 artak 1481
253 artak 1517

  ViewVC Help
Powered by ViewVC 1.1.26