# Diff of /trunk/escript/py_src/linearPDEs.py

revision 1204 by gross, Sat Jun 23 11:43:12 2007 UTC revision 1639 by gross, Mon Jul 14 08:55:25 2008 UTC
# Line 1  Line 1
1    #
2  # \$Id\$  # \$Id\$
3    #
4    #######################################################
5    #
6    #           Copyright 2003-2007 by ACceSS MNRF
7    #       Copyright 2007 by University of Queensland
8    #
9    #                http://esscc.uq.edu.au
10    #        Primary Business: Queensland, Australia
13    #
14    #######################################################
15    #
16
17  """  """
18  The module provides an interface to define and solve linear partial  The module provides an interface to define and solve linear partial
19  differential equations (PDEs) within L{escript}. L{linearPDEs} does not provide any  differential equations (PDEs) within L{escript}. L{linearPDEs} does not provide any
# Line 19  to define of solve these sepecial PDEs. Line 34  to define of solve these sepecial PDEs.
34  @var __date__: date of the version  @var __date__: date of the version
35  """  """
36
37    import math
38  import escript  import escript
39  import util  import util
40  import numarray  import numarray
# Line 112  class PDECoefficient(object): Line 128  class PDECoefficient(object):
128         @param reduced: indicates if reduced         @param reduced: indicates if reduced
129         @type reduced: C{bool}         @type reduced: C{bool}
130         """         """

131         super(PDECoefficient, self).__init__()         super(PDECoefficient, self).__init__()
132         self.what=where         self.what=where
133         self.pattern=pattern         self.pattern=pattern
# Line 365  class LinearPDE(object): Line 380  class LinearPDE(object):
380
381     The PDE is symmetrical if     The PDE is symmetrical if
382
383     M{A[i,j]=A[j,i]}  and M{B[j]=C[j]} and M{A_reduced[i,j]=A_reduced[j,i]}  and M{B_reduced[j]=C_reduced[j]     M{A[i,j]=A[j,i]}  and M{B[j]=C[j]} and M{A_reduced[i,j]=A_reduced[j,i]}  and M{B_reduced[j]=C_reduced[j]}
384
385     For a system of PDEs and a solution with several components the PDE has the form     For a system of PDEs and a solution with several components the PDE has the form
386
# Line 416  class LinearPDE(object): Line 431  class LinearPDE(object):
431     of the solution at side 1 and at side 0, denotes the jump of M{u} across discontinuity along the normal calcualted by     of the solution at side 1 and at side 0, denotes the jump of M{u} across discontinuity along the normal calcualted by
432     L{jump<util.jump>}.     L{jump<util.jump>}.
433     The coefficient M{d_contact} is a rank two and M{y_contact} is a rank one both in the L{FunctionOnContactZero<escript.FunctionOnContactZero>} or L{FunctionOnContactOne<escript.FunctionOnContactOne>}.     The coefficient M{d_contact} is a rank two and M{y_contact} is a rank one both in the L{FunctionOnContactZero<escript.FunctionOnContactZero>} or L{FunctionOnContactOne<escript.FunctionOnContactOne>}.
434      The coefficient M{d_contact_reduced} is a rank two and M{y_contact_reduced} is a rank one both in the L{ReducedFunctionOnContactZero<escript.ReducedFunctionOnContactZero>} or L{ReducedFunctionOnContactOne<escript.ReducedFunctionOnContactOne>}.     The coefficient M{d_contact_reduced} is a rank two and M{y_contact_reduced} is a rank one both in the L{ReducedFunctionOnContactZero<escript.ReducedFunctionOnContactZero>} or L{ReducedFunctionOnContactOne<escript.ReducedFunctionOnContactOne>}.
435     In case of a single PDE and a single component solution the contact condition takes the form     In case of a single PDE and a single component solution the contact condition takes the form
436
437     M{n[j]*J0_{j}=n[j]*J1_{j}=(y_contact+y_contact_reduced)-(d_contact+y_contact_reduced)*jump(u)}     M{n[j]*J0_{j}=n[j]*J1_{j}=(y_contact+y_contact_reduced)-(d_contact+y_contact_reduced)*jump(u)}
# Line 444  class LinearPDE(object): Line 459  class LinearPDE(object):
459     @cvar SCSL: SGI SCSL solver library     @cvar SCSL: SGI SCSL solver library
460     @cvar MKL: Intel's MKL solver library     @cvar MKL: Intel's MKL solver library
461     @cvar UMFPACK: the UMFPACK library     @cvar UMFPACK: the UMFPACK library
462       @cvar TRILINOS: the TRILINOS parallel solver class library from Sandia Natl Labs
463     @cvar ITERATIVE: The default iterative solver     @cvar ITERATIVE: The default iterative solver
464     @cvar AMG: algebraic multi grid     @cvar AMG: algebraic multi grid
465     @cvar RILU: recursive ILU     @cvar RILU: recursive ILU
# Line 473  class LinearPDE(object): Line 489  class LinearPDE(object):
489     PASO= 21     PASO= 21
490     AMG= 22     AMG= 22
491     RILU = 23     RILU = 23
492       TRILINOS = 24
493       NONLINEAR_GMRES = 25
494
495     SMALL_TOLERANCE=1.e-13     SMALL_TOLERANCE=1.e-13
496     __PACKAGE_KEY="package"     __PACKAGE_KEY="package"
# Line 935  class LinearPDE(object): Line 953  class LinearPDE(object):
953         @param preconditioner: sets a new solver method.         @param preconditioner: sets a new solver method.
954         @type preconditioner: one of L{DEFAULT}, L{JACOBI} L{ILU0}, L{ILUT},L{SSOR}, L{RILU}         @type preconditioner: one of L{DEFAULT}, L{JACOBI} L{ILU0}, L{ILUT},L{SSOR}, L{RILU}
955         """         """
956         if solver==None: solve=self.DEFAULT         if solver==None: solver=self.__solver_method
957           if preconditioner==None: preconditioner=self.__preconditioner
958           if solver==None: solver=self.DEFAULT
959         if preconditioner==None: preconditioner=self.DEFAULT         if preconditioner==None: preconditioner=self.DEFAULT
960         if not (solver,preconditioner)==self.getSolverMethod():         if not (solver,preconditioner)==self.getSolverMethod():
961             self.__solver_method=solver             self.__solver_method=solver
# Line 979  class LinearPDE(object): Line 999  class LinearPDE(object):
999         elif p==self.MKL: package= "MKL"         elif p==self.MKL: package= "MKL"
1000         elif p==self.SCSL: package= "SCSL"         elif p==self.SCSL: package= "SCSL"
1001         elif p==self.UMFPACK: package= "UMFPACK"         elif p==self.UMFPACK: package= "UMFPACK"
1002           elif p==self.TRILINOS: package= "TRILINOS"
1003         else : method="unknown"         else : method="unknown"
1004         return "%s solver of %s package"%(method,package)         return "%s solver of %s package"%(method,package)
1005
# Line 997  class LinearPDE(object): Line 1018  class LinearPDE(object):
1018         sets a new solver package         sets a new solver package
1019
1020         @param package: sets a new solver method.         @param package: sets a new solver method.
1021         @type package: one of L{DEFAULT}, L{PASO} L{SCSL}, L{MKL}, L{UMFPACK}         @type package: one of L{DEFAULT}, L{PASO} L{SCSL}, L{MKL}, L{UMFPACK}, L{TRILINOS}
1022         """         """
1023         if package==None: package=self.DEFAULT         if package==None: package=self.DEFAULT
1024         if not package==self.getSolverPackage():         if not package==self.getSolverPackage():
# Line 2171  class LameEquation(LinearPDE): Line 2192  class LameEquation(LinearPDE):
2192       else:       else:
2193          raise IllegalCoefficient,"illegal coefficient %s requested for general PDE."%name          raise IllegalCoefficient,"illegal coefficient %s requested for general PDE."%name
2194
2195    def LinearSinglePDE(domain,debug=False):
2196       """
2197       defines a single linear PDEs
2198
2199       @param domain: domain of the PDE
2200       @type domain: L{Domain<escript.Domain>}
2201       @param debug: if True debug informations are printed.
2202       @rtype: L{LinearPDE}
2203       """
2204       return LinearPDE(domain,numEquations=1,numSolutions=1,debug=debug)
2205
2206    def LinearPDESystem(domain,debug=False):
2207       """
2208       defines a system of linear PDEs
2209
2210       @param domain: domain of the PDE
2211       @type domain: L{Domain<escript.Domain>}
2212       @param debug: if True debug informations are printed.
2213       @rtype: L{LinearPDE}
2214       """
2215       return LinearPDE(domain,numEquations=domain.getDim(),numSolutions=domain.getDim(),debug=debug)
2216
2217    class TransportPDE(object):
2218         """
2219         Warning: This is still a very experimental. The class is still changing!
2220
2221         Mu_{,t} =-(A_{ij}u_{,j})_j-(B_{j}u)_{,j} + C_{j} u_{,j} + Y_i + X_{i,i}
2222
2223         u=r where q>0
2224
2225         all coefficients are constant over time.
2226
2227         typical usage:
2228
2229             p=TransportPDE(dom)
2230             p.setValue(M=Scalar(1.,Function(dom),C=Scalar(1.,Function(dom)*[-1.,0.])
2231             p.setInitialSolution(u=exp(-length(dom.getX()-[0.1,0.1])**2)
2232             t=0
2233             dt=0.1
2234             while (t<1.):
2235                  u=p.solve(dt)
2236
2237         """
2238         def __init__(self,domain,num_equations=1,theta=0.5,useSUPG=False,trace=True):
2239            self.__domain=domain
2240            self.__num_equations=num_equations
2241            self.__useSUPG=useSUPG
2242            self.__trace=trace
2243            self.__theta=theta
2244            self.__matrix_type=0
2245            self.__reduced=True
2246            self.__reassemble=True
2247            if self.__useSUPG:
2248               self.__pde=LinearPDE(domain,numEquations=num_equations,numSolutions=num_equations,debug=trace)
2249               self.__pde.setSymmetryOn()
2250               self.__pde.setReducedOrderOn()
2251            else:
2252               self.__transport_problem=self.__getNewTransportProblem()
2253            self.setTolerance()
2254            self.__M=escript.Data()
2255            self.__A=escript.Data()
2256            self.__B=escript.Data()
2257            self.__C=escript.Data()
2258            self.__D=escript.Data()
2259            self.__X=escript.Data()
2260            self.__Y=escript.Data()
2261            self.__d=escript.Data()
2262            self.__y=escript.Data()
2263            self.__d_contact=escript.Data()
2264            self.__y_contact=escript.Data()
2265            self.__r=escript.Data()
2266            self.__q=escript.Data()
2267
2268         def trace(self,text):
2269                 if self.__trace: print text
2270         def getSafeTimeStepSize(self):
2271            if self.__useSUPG:
2272                if self.__reassemble:
2273                   h=self.__domain.getSize()
2274                   dt=None
2275                   if not self.__A.isEmpty():
2276                      dt2=util.inf(h**2*self.__M/util.length(self.__A))
2277                      if dt == None:
2278                         dt = dt2
2279                      else:
2280                         dt=1./(1./dt+1./dt2)
2281                   if not self.__B.isEmpty():
2282                      dt2=util.inf(h*self.__M/util.length(self.__B))
2283                      if dt == None:
2284                         dt = dt2
2285                      else:
2286                         dt=1./(1./dt+1./dt2)
2287                   if not  self.__C.isEmpty():
2288                      dt2=util.inf(h*self.__M/util.length(self.__C))
2289                      if dt == None:
2290                         dt = dt2
2291                      else:
2292                         dt=1./(1./dt+1./dt2)
2293                   if not self.__D.isEmpty():
2294                      dt2=util.inf(self.__M/util.length(self.__D))
2295                      if dt == None:
2296                         dt = dt2
2297                      else:
2298                         dt=1./(1./dt+1./dt2)
2299                   self.__dt = dt/2
2300                return self.__dt
2301            else:
2302                return self.__getTransportProblem().getSafeTimeStepSize()
2303         def getDomain(self):
2304            return self.__domain
2305         def getTheta(self):
2306            return self.__theta
2307         def getNumEquations(self):
2308            return self.__num_equations
2309         def setReducedOn(self):
2310              if not self.reduced():
2311                  if self.__useSUPG:
2312                     self.__pde.setReducedOrderOn()
2313                  else:
2314                     self.__transport_problem=self.__getNewTransportProblem()
2315              self.__reduced=True
2316         def setReducedOff(self):
2317              if self.reduced():
2318                  if self.__useSUPG:
2319                     self.__pde.setReducedOrderOff()
2320                  else:
2321                     self.__transport_problem=self.__getNewTransportProblem()
2322              self.__reduced=False
2323         def reduced(self):
2324             return self.__reduced
2325         def getFunctionSpace(self):
2326            if self.reduced():
2327               return escript.ReducedSolution(self.getDomain())
2328            else:
2329               return escript.Solution(self.getDomain())
2330
2331         def setTolerance(self,tol=1.e-8):
2332            self.__tolerance=tol
2333            if self.__useSUPG:
2334                  self.__pde.setTolerance(self.__tolerance)
2335
2336         def __getNewTransportProblem(self):
2337           """
2338           returns an instance of a new operator
2339           """
2340           self.trace("New Transport problem is allocated.")
2341           return self.getDomain().newTransportProblem( \
2342                                   self.getTheta(),
2343                                   self.getNumEquations(), \
2344                                   self.getFunctionSpace(), \
2345                                   self.__matrix_type)
2346
2347         def __getNewSolutionVector(self):
2348             if self.getNumEquations() ==1 :
2349                    out=escript.Data(0.0,(),self.getFunctionSpace())
2350             else:
2351                    out=escript.Data(0.0,(self.getNumEquations(),),self.getFunctionSpace())
2352             return out
2353
2354         def __getTransportProblem(self):
2355           if self.__reassemble:
2356                 self.__source=self.__getNewSolutionVector()
2357                 self.__transport_problem.reset()
2359                             self.__transport_problem,
2360                             self.__source,
2361                             self.__M,
2362                             self.__A,
2363                             self.__B,
2364                             self.__C,
2365                             self.__D,
2366                             self.__X,
2367                             self.__Y,
2368                             self.__d,
2369                             self.__y,
2370                             self.__d_contact,
2371                             self.__y_contact)
2372                 self.__transport_problem.insertConstraint(self.__source,self.__q,self.__r)
2373                 self.__reassemble=False
2374           return self.__transport_problem
2375         def setValue(self,M=None, A=None, B=None, C=None, D=None, X=None, Y=None,
2376                      d=None, y=None, d_contact=None, y_contact=None, q=None, r=None):
2377                 if not M==None:
2378                      self.__reassemble=True
2379                      self.__M=M
2380                 if not A==None:
2381                      self.__reassemble=True
2382                      self.__A=A
2383                 if not B==None:
2384                      self.__reassemble=True
2385                      self.__B=B
2386                 if not C==None:
2387                      self.__reassemble=True
2388                      self.__C=C
2389                 if not D==None:
2390                      self.__reassemble=True
2391                      self.__D=D
2392                 if not X==None:
2393                      self.__reassemble=True
2394                      self.__X=X
2395                 if not Y==None:
2396                      self.__reassemble=True
2397                      self.__Y=Y
2398                 if not d==None:
2399                      self.__reassemble=True
2400                      self.__d=d
2401                 if not y==None:
2402                      self.__reassemble=True
2403                      self.__y=y
2404                 if not d_contact==None:
2405                      self.__reassemble=True
2406                      self.__d_contact=d_contact
2407                 if not y_contact==None:
2408                      self.__reassemble=True
2409                      self.__y_contact=y_contact
2410                 if not q==None:
2411                      self.__reassemble=True
2412                      self.__q=q
2413                 if not r==None:
2414                      self.__reassemble=True
2415                      self.__r=r
2416
2417         def setInitialSolution(self,u):
2418                 if self.__useSUPG:
2419                     self.__u=util.interpolate(u,self.getFunctionSpace())
2420                 else:
2421                     self.__transport_problem.setInitialValue(util.interpolate(u,self.getFunctionSpace()))
2422
2423         def solve(self,dt,**kwarg):
2424               if self.__useSUPG:
2425                    if self.__reassemble:
2426                        self.__pde.setValue(D=self.__M,d=self.__d,d_contact=self.__d_contact,q=self.__q) # ,r=self.__r)
2427                        self.__reassemble=False
2428                    dt2=self.getSafeTimeStepSize()
2429                    nn=max(math.ceil(dt/self.getSafeTimeStepSize()),1.)
2430                    dt2=dt/nn
2431                    nnn=0
2432                    u=self.__u
2433                    self.trace("number of substeps is %d."%nn)
2434                    while nnn<nn :
2435                        self.__setSUPG(u,u,dt2/2)
2436                        u_half=self.__pde.getSolution(verbose=True)
2437                        self.__setSUPG(u,u_half,dt2)
2438                        u=self.__pde.getSolution(verbose=True)
2439                        nnn+=1
2440                    self.__u=u
2441                    return self.__u
2442               else:
2443                   kwarg["tolerance"]=self.__tolerance
2444                   tp=self.__getTransportProblem()
2445                   return tp.solve(self.__source,dt,kwarg)
2446         def __setSUPG(self,u0,u,dt):
2448                X=0
2449                Y=self.__M*u0
2450                X=0
2451                self.__pde.setValue(r=u0)
2452                if not self.__A.isEmpty():
2453                   X=X+dt*util.matrixmult(self.__A,g)
2454                if not self.__B.isEmpty():
2455                   X=X+dt*self.__B*u
2456                if not  self.__C.isEmpty():
2457                   Y=Y+dt*util.inner(self.__C,g)
2458                if not self.__D.isEmpty():
2459                   Y=Y+dt*self.__D*u
2460                if not self.__X.isEmpty():
2461                   X=X+dt*self.__X
2462                if not self.__Y.isEmpty():
2463                   Y=Y+dt*self.__Y
2464                self.__pde.setValue(X=X,Y=Y)
2465                if not self.__y.isEmpty():
2466                   self.__pde.setValue(y=dt*self.__y)
2467                if not self.__y_contact.isEmpty():
2468                   self.__pde.setValue(y=dt*self.__y_contact)
2469                self.__pde.setValue(r=u0)

Legend:
 Removed from v.1204 changed lines Added in v.1639