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

Diff of /trunk/escript/py_src/pdetools.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2415 by gross, Wed May 13 02:48:39 2009 UTC revision 2793 by gross, Tue Dec 1 06:10:10 2009 UTC
# Line 1  Line 1 
1    
2  ########################################################  ########################################################
3  #  #
4  # Copyright (c) 2003-2008 by University of Queensland  # Copyright (c) 2003-2009 by University of Queensland
5  # Earth Systems Science Computational Center (ESSCC)  # Earth Systems Science Computational Center (ESSCC)
6  # http://www.uq.edu.au/esscc  # http://www.uq.edu.au/esscc
7  #  #
# Line 11  Line 11 
11  #  #
12  ########################################################  ########################################################
13    
14  __copyright__="""Copyright (c) 2003-2008 by University of Queensland  __copyright__="""Copyright (c) 2003-2009 by University of Queensland
15  Earth Systems Science Computational Center (ESSCC)  Earth Systems Science Computational Center (ESSCC)
16  http://www.uq.edu.au/esscc  http://www.uq.edu.au/esscc
17  Primary Business: Queensland, Australia"""  Primary Business: Queensland, Australia"""
# Line 28  Currently includes: Line 28  Currently includes:
28      - TimeIntegrationManager - to handle extrapolation in time      - TimeIntegrationManager - to handle extrapolation in time
29      - SaddlePointProblem - solver for Saddle point problems using the inexact uszawa scheme      - SaddlePointProblem - solver for Saddle point problems using the inexact uszawa scheme
30    
31  @var __author__: name of author  :var __author__: name of author
32  @var __copyright__: copyrights  :var __copyright__: copyrights
33  @var __license__: licence agreement  :var __license__: licence agreement
34  @var __url__: url entry point on documentation  :var __url__: url entry point on documentation
35  @var __version__: version  :var __version__: version
36  @var __date__: date of the version  :var __date__: date of the version
37  """  """
38    
39  __author__="Lutz Gross, l.gross@uq.edu.au"  __author__="Lutz Gross, l.gross@uq.edu.au"
# Line 41  __author__="Lutz Gross, l.gross@uq.edu.a Line 41  __author__="Lutz Gross, l.gross@uq.edu.a
41    
42  import escript  import escript
43  import linearPDEs  import linearPDEs
44  import numarray  import numpy
45  import util  import util
46  import math  import math
47    
 ##### Added by Artak  
 # from Numeric import zeros,Int,Float64  
 ###################################  
   
   
48  class TimeIntegrationManager:  class TimeIntegrationManager:
49    """    """
50    A simple mechanism to manage time dependend values.    A simple mechanism to manage time dependend values.
# Line 64  class TimeIntegrationManager: Line 59  class TimeIntegrationManager:
59           tm.checkin(dt,v)           tm.checkin(dt,v)
60           t+=dt           t+=dt
61    
62    @note: currently only p=1 is supported.    :note: currently only p=1 is supported.
63    """    """
64    def __init__(self,*inital_values,**kwargs):    def __init__(self,*inital_values,**kwargs):
65       """       """
66       Sets up the value manager where C{inital_values} are the initial values       Sets up the value manager where ``inital_values`` are the initial values
67       and p is the order used for extrapolation.       and p is the order used for extrapolation.
68       """       """
69       if kwargs.has_key("p"):       if kwargs.has_key("p"):
# Line 113  class TimeIntegrationManager: Line 108  class TimeIntegrationManager:
108    
109    def extrapolate(self,dt):    def extrapolate(self,dt):
110        """        """
111        Extrapolates to C{dt} forward in time.        Extrapolates to ``dt`` forward in time.
112        """        """
113        if self.__order==0:        if self.__order==0:
114           out=self.__v_mem[0]           out=self.__v_mem[0]
# Line 139  class Projector: Line 134  class Projector:
134      """      """
135      Creates a continuous function space projector for a domain.      Creates a continuous function space projector for a domain.
136    
137      @param domain: Domain of the projection.      :param domain: Domain of the projection.
138      @param reduce: Flag to reduce projection order      :param reduce: Flag to reduce projection order
139      @param fast: Flag to use a fast method based on matrix lumping      :param fast: Flag to use a fast method based on matrix lumping
140      """      """
141      self.__pde = linearPDEs.LinearPDE(domain)      self.__pde = linearPDEs.LinearPDE(domain)
142      if fast:      if fast:
143          self.__pde.setSolverMethod(linearPDEs.LinearPDE.LUMPING)          self.__pde.getSolverOptions().setSolverMethod(linearPDEs.SolverOptions.LUMPING)
144      self.__pde.setSymmetryOn()      self.__pde.setSymmetryOn()
145      self.__pde.setReducedOrderTo(reduce)      self.__pde.setReducedOrderTo(reduce)
146      self.__pde.setValue(D = 1.)      self.__pde.setValue(D = 1.)
147      return      return
148      def getSolverOptions(self):
149        """
150        Returns the solver options of the PDE solver.
151        
152        :rtype: `linearPDEs.SolverOptions`
153        """
154        return self.__pde.getSolverOptions()
155    
156    def __call__(self, input_data):    def __call__(self, input_data):
157      """      """
158      Projects C{input_data} onto a continuous function.      Projects ``input_data`` onto a continuous function.
159    
160      @param input_data: the data to be projected      :param input_data: the data to be projected
161      """      """
162      out=escript.Data(0.,input_data.getShape(),self.__pde.getFunctionSpaceForSolution())      out=escript.Data(0.,input_data.getShape(),self.__pde.getFunctionSpaceForSolution())
163      self.__pde.setValue(Y = escript.Data(), Y_reduced = escript.Data())      self.__pde.setValue(Y = escript.Data(), Y_reduced = escript.Data())
# Line 190  class NoPDE: Line 192  class NoPDE:
192       """       """
193       Solves the following problem for u:       Solves the following problem for u:
194    
195       M{kronecker[i,j]*D[j]*u[j]=Y[i]}       *kronecker[i,j]*D[j]*u[j]=Y[i]*
196    
197       with constraint       with constraint
198    
199       M{u[j]=r[j]}  where M{q[j]>0}       *u[j]=r[j]*  where *q[j]>0*
200    
201       where M{D}, M{Y}, M{r} and M{q} are given functions of rank 1.       where *D*, *Y*, *r* and *q* are given functions of rank 1.
202    
203       In the case of scalars this takes the form       In the case of scalars this takes the form
204    
205       M{D*u=Y}       *D*u=Y*
206    
207       with constraint       with constraint
208    
209       M{u=r} where M{q>0}       *u=r* where *q>0*
210    
211       where M{D}, M{Y}, M{r} and M{q} are given scalar functions.       where *D*, *Y*, *r* and *q* are given scalar functions.
212    
213       The constraint overwrites any other condition.       The constraint overwrites any other condition.
214    
215       @note: This class is similar to the L{linearPDEs.LinearPDE} class with       :note: This class is similar to the `linearPDEs.LinearPDE` class with
216              A=B=C=X=0 but has the intention that all input parameters are given              A=B=C=X=0 but has the intention that all input parameters are given
217              in L{Solution} or L{ReducedSolution}.              in `Solution` or `ReducedSolution`.
218       """       """
219       # The whole thing is a bit strange and I blame Rob Woodcock (CSIRO) for       # The whole thing is a bit strange and I blame Rob Woodcock (CSIRO) for
220       # this.       # this.
# Line 220  class NoPDE: Line 222  class NoPDE:
222           """           """
223           Initializes the problem.           Initializes the problem.
224    
225           @param domain: domain of the PDE           :param domain: domain of the PDE
226           @type domain: L{Domain}           :type domain: `Domain`
227           @param D: coefficient of the solution           :param D: coefficient of the solution
228           @type D: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type D: ``float``, ``int``, ``numpy.ndarray``, `Data`
229           @param Y: right hand side           :param Y: right hand side
230           @type Y: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type Y: ``float``, ``int``, ``numpy.ndarray``, `Data`
231           @param q: location of constraints           :param q: location of constraints
232           @type q: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type q: ``float``, ``int``, ``numpy.ndarray``, `Data`
233           @param r: value of solution at locations of constraints           :param r: value of solution at locations of constraints
234           @type r: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type r: ``float``, ``int``, ``numpy.ndarray``, `Data`
235           """           """
236           self.__domain=domain           self.__domain=domain
237           self.__D=D           self.__D=D
# Line 241  class NoPDE: Line 243  class NoPDE:
243    
244       def setReducedOn(self):       def setReducedOn(self):
245           """           """
246           Sets the L{FunctionSpace} of the solution to L{ReducedSolution}.           Sets the `FunctionSpace` of the solution to `ReducedSolution`.
247           """           """
248           self.__function_space=escript.ReducedSolution(self.__domain)           self.__function_space=escript.ReducedSolution(self.__domain)
249           self.__u=None           self.__u=None
250    
251       def setReducedOff(self):       def setReducedOff(self):
252           """           """
253           Sets the L{FunctionSpace} of the solution to L{Solution}.           Sets the `FunctionSpace` of the solution to `Solution`.
254           """           """
255           self.__function_space=escript.Solution(self.__domain)           self.__function_space=escript.Solution(self.__domain)
256           self.__u=None           self.__u=None
# Line 257  class NoPDE: Line 259  class NoPDE:
259           """           """
260           Assigns values to the parameters.           Assigns values to the parameters.
261    
262           @param D: coefficient of the solution           :param D: coefficient of the solution
263           @type D: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type D: ``float``, ``int``, ``numpy.ndarray``, `Data`
264           @param Y: right hand side           :param Y: right hand side
265           @type Y: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type Y: ``float``, ``int``, ``numpy.ndarray``, `Data`
266           @param q: location of constraints           :param q: location of constraints
267           @type q: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type q: ``float``, ``int``, ``numpy.ndarray``, `Data`
268           @param r: value of solution at locations of constraints           :param r: value of solution at locations of constraints
269           @type r: C{float}, C{int}, C{numarray.NumArray}, L{Data}           :type r: ``float``, ``int``, ``numpy.ndarray``, `Data`
270           """           """
271           if not D==None:           if not D==None:
272              self.__D=D              self.__D=D
# Line 283  class NoPDE: Line 285  class NoPDE:
285           """           """
286           Returns the solution.           Returns the solution.
287    
288           @return: the solution of the problem           :return: the solution of the problem
289           @rtype: L{Data} object in the L{FunctionSpace} L{Solution} or           :rtype: `Data` object in the `FunctionSpace` `Solution` or
290                   L{ReducedSolution}                   `ReducedSolution`
291           """           """
292           if self.__u==None:           if self.__u==None:
293              if self.__D==None:              if self.__D==None:
# Line 312  class Locator: Line 314  class Locator:
314       given function space or domain which is closest to the given point x.       given function space or domain which is closest to the given point x.
315       """       """
316    
317       def __init__(self,where,x=numarray.zeros((3,))):       def __init__(self,where,x=numpy.zeros((3,))):
318         """         """
319         Initializes a Locator to access values in Data objects on the Doamin         Initializes a Locator to access values in Data objects on the Doamin
320         or FunctionSpace for the sample point which is closest to the given         or FunctionSpace for the sample point which is closest to the given
321         point x.         point x.
322    
323         @param where: function space         :param where: function space
324         @type where: L{escript.FunctionSpace}         :type where: `escript.FunctionSpace`
325         @param x: coefficient of the solution         :param x: location(s) of the Locator
326         @type x: C{numarray.NumArray} or C{list} of C{numarray.NumArray}         :type x: ``numpy.ndarray`` or ``list`` of ``numpy.ndarray``
327         """         """
328         if isinstance(where,escript.FunctionSpace):         if isinstance(where,escript.FunctionSpace):
329            self.__function_space=where            self.__function_space=where
330         else:         else:
331            self.__function_space=escript.ContinuousFunction(where)            self.__function_space=escript.ContinuousFunction(where)
332           iterative=False
333         if isinstance(x, list):         if isinstance(x, list):
334               if len(x)==0:
335                  raise "ValueError", "At least one point must be given."
336               try:
337                 iter(x[0])
338                 iterative=True
339               except TypeError:
340                 iterative=False
341           if iterative:
342             self.__id=[]             self.__id=[]
343             for p in x:             for p in x:
344                self.__id.append(util.length(self.__function_space.getX()-p[:self.__function_space.getDim()]).minGlobalDataPoint())                self.__id.append(util.length(self.__function_space.getX()-p[:self.__function_space.getDim()]).minGlobalDataPoint())
# Line 339  class Locator: Line 350  class Locator:
350         Returns the coordinates of the Locator as a string.         Returns the coordinates of the Locator as a string.
351         """         """
352         x=self.getX()         x=self.getX()
353         if instance(x,list):         if isinstance(x,list):
354            out="["            out="["
355            first=True            first=True
356            for xx in x:            for xx in x:
# Line 386  class Locator: Line 397  class Locator:
397    
398       def getValue(self,data):       def getValue(self,data):
399          """          """
400          Returns the value of C{data} at the Locator if C{data} is a L{Data}          Returns the value of ``data`` at the Locator if ``data`` is a `Data`
401          object otherwise the object is returned.          object otherwise the object is returned.
402          """          """
403          if isinstance(data,escript.Data):          if isinstance(data,escript.Data):
404             if data.getFunctionSpace()==self.getFunctionSpace():             dat=util.interpolate(data,self.getFunctionSpace())
              dat=data  
            else:  
              dat=data.interpolate(self.getFunctionSpace())  
405             id=self.getId()             id=self.getId()
406             r=data.getRank()             r=data.getRank()
407             if isinstance(id,list):             if isinstance(id,list):
408                 out=[]                 out=[]
409                 for i in id:                 for i in id:
410                    o=data.getValueOfGlobalDataPoint(*i)                    o=numpy.array(dat.getTupleForGlobalDataPoint(*i))
411                    if data.getRank()==0:                    if data.getRank()==0:
412                       out.append(o[0])                       out.append(o[0])
413                    else:                    else:
414                       out.append(o)                       out.append(o)
415                 return out                 return out
416             else:             else:
417               out=data.getValueOfGlobalDataPoint(*id)               out=numpy.array(dat.getTupleForGlobalDataPoint(*id))
418               if data.getRank()==0:               if data.getRank()==0:
419                  return out[0]                  return out[0]
420               else:               else:
# Line 414  class Locator: Line 422  class Locator:
422          else:          else:
423             return data             return data
424    
425    
426    def getInfLocator(arg):
427        """
428        Return a Locator for a point with the inf value over all arg.
429        """
430        if not isinstance(arg, escript.Data):
431        raise TypeError,"getInfLocator: Unknown argument type."
432        a_inf=util.inf(arg)
433        loc=util.length(arg-a_inf).minGlobalDataPoint() # This gives us the location but not coords
434        x=arg.getFunctionSpace().getX()
435        x_min=x.getTupleForGlobalDataPoint(*loc)
436        return Locator(arg.getFunctionSpace(),x_min)
437    
438    def getSupLocator(arg):
439        """
440        Return a Locator for a point with the sup value over all arg.
441        """
442        if not isinstance(arg, escript.Data):
443        raise TypeError,"getInfLocator: Unknown argument type."
444        a_inf=util.sup(arg)
445        loc=util.length(arg-a_inf).minGlobalDataPoint() # This gives us the location but not coords
446        x=arg.getFunctionSpace().getX()
447        x_min=x.getTupleForGlobalDataPoint(*loc)
448        return Locator(arg.getFunctionSpace(),x_min)
449        
450    
451  class SolverSchemeException(Exception):  class SolverSchemeException(Exception):
452     """     """
453     This is a generic exception thrown by solvers.     This is a generic exception thrown by solvers.
# Line 455  def PCG(r, Aprod, x, Msolve, bilinearfor Line 489  def PCG(r, Aprod, x, Msolve, bilinearfor
489     """     """
490     Solver for     Solver for
491    
492     M{Ax=b}     *Ax=b*
493    
494     with a symmetric and positive definite operator A (more details required!).     with a symmetric and positive definite operator A (more details required!).
495     It uses the conjugate gradient method with preconditioner M providing an     It uses the conjugate gradient method with preconditioner M providing an
# Line 463  def PCG(r, Aprod, x, Msolve, bilinearfor Line 497  def PCG(r, Aprod, x, Msolve, bilinearfor
497    
498     The iteration is terminated if     The iteration is terminated if
499    
500     M{|r| <= atol+rtol*|r0|}     *|r| <= atol+rtol*|r0|*
501    
502     where M{r0} is the initial residual and M{|.|} is the energy norm. In fact     where *r0* is the initial residual and *|.|* is the energy norm. In fact
503    
504     M{|r| = sqrt( bilinearform(Msolve(r),r))}     *|r| = sqrt( bilinearform(Msolve(r),r))*
505    
506     For details on the preconditioned conjugate gradient method see the book:     For details on the preconditioned conjugate gradient method see the book:
507    
# Line 475  def PCG(r, Aprod, x, Msolve, bilinearfor Line 509  def PCG(r, Aprod, x, Msolve, bilinearfor
509     T.F. Chan, J. Demmel, J. Donato, J. Dongarra, V. Eijkhout, R. Pozo,     T.F. Chan, J. Demmel, J. Donato, J. Dongarra, V. Eijkhout, R. Pozo,
510     C. Romine, and H. van der Vorst}.     C. Romine, and H. van der Vorst}.
511    
512     @param r: initial residual M{r=b-Ax}. C{r} is altered.     :param r: initial residual *r=b-Ax*. ``r`` is altered.
513     @type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)     :type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
514     @param x: an initial guess for the solution     :param x: an initial guess for the solution
515     @type x: any object supporting inplace add (x+=y) and scaling (x=scalar*y)     :type x: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
516     @param Aprod: returns the value Ax     :param Aprod: returns the value Ax
517     @type Aprod: function C{Aprod(x)} where C{x} is of the same object like     :type Aprod: function ``Aprod(x)`` where ``x`` is of the same object like
518                  argument C{x}. The returned object needs to be of the same type                  argument ``x``. The returned object needs to be of the same type
519                  like argument C{r}.                  like argument ``r``.
520     @param Msolve: solves Mx=r     :param Msolve: solves Mx=r
521     @type Msolve: function C{Msolve(r)} where C{r} is of the same type like     :type Msolve: function ``Msolve(r)`` where ``r`` is of the same type like
522                   argument C{r}. The returned object needs to be of the same                   argument ``r``. The returned object needs to be of the same
523                   type like argument C{x}.                   type like argument ``x``.
524     @param bilinearform: inner product C{<x,r>}     :param bilinearform: inner product ``<x,r>``
525     @type bilinearform: function C{bilinearform(x,r)} where C{x} is of the same     :type bilinearform: function ``bilinearform(x,r)`` where ``x`` is of the same
526                         type like argument C{x} and C{r} is. The returned value                         type like argument ``x`` and ``r`` is. The returned value
527                         is a C{float}.                         is a ``float``.
528     @param atol: absolute tolerance     :param atol: absolute tolerance
529     @type atol: non-negative C{float}     :type atol: non-negative ``float``
530     @param rtol: relative tolerance     :param rtol: relative tolerance
531     @type rtol: non-negative C{float}     :type rtol: non-negative ``float``
532     @param iter_max: maximum number of iteration steps     :param iter_max: maximum number of iteration steps
533     @type iter_max: C{int}     :type iter_max: ``int``
534     @return: the solution approximation and the corresponding residual     :return: the solution approximation and the corresponding residual
535     @rtype: C{tuple}     :rtype: ``tuple``
536     @warning: C{r} and C{x} are altered.     :warning: ``r`` and ``x`` are altered.
537     """     """
538     iter=0     iter=0
539     rhat=Msolve(r)     rhat=Msolve(r)
# Line 522  def PCG(r, Aprod, x, Msolve, bilinearfor Line 556  def PCG(r, Aprod, x, Msolve, bilinearfor
556         q=Aprod(d)         q=Aprod(d)
557         alpha = rhat_dot_r / bilinearform(d, q)         alpha = rhat_dot_r / bilinearform(d, q)
558         x += alpha * d         x += alpha * d
559         r += (-alpha) * q         if isinstance(q,ArithmeticTuple):
560           r += q * (-alpha)      # Doing it the other way calls the float64.__mul__ not AT.__rmul__
561           else:
562               r += (-alpha) * q
563         rhat=Msolve(r)         rhat=Msolve(r)
564         rhat_dot_r_new = bilinearform(rhat, r)         rhat_dot_r_new = bilinearform(rhat, r)
565         beta = rhat_dot_r_new / rhat_dot_r         beta = rhat_dot_r_new / rhat_dot_r
# Line 550  class Defect(object): Line 586  class Defect(object):
586          """          """
587          Returns the inner product of x0 and x1          Returns the inner product of x0 and x1
588    
589          @param x0: value for x0          :param x0: value for x0
590          @param x1: value for x1          :param x1: value for x1
591          @return: the inner product of x0 and x1          :return: the inner product of x0 and x1
592          @rtype: C{float}          :rtype: ``float``
593          """          """
594          return 0          return 0
595    
596      def norm(self,x):      def norm(self,x):
597          """          """
598          Returns the norm of argument C{x}.          Returns the norm of argument ``x``.
599    
600          @param x: a value          :param x: a value
601          @return: norm of argument x          :return: norm of argument x
602          @rtype: C{float}          :rtype: ``float``
603          @note: by default C{sqrt(self.bilinearform(x,x)} is returned.          :note: by default ``sqrt(self.bilinearform(x,x)`` is returned.
604          """          """
605          s=self.bilinearform(x,x)          s=self.bilinearform(x,x)
606          if s<0: raise NegativeNorm,"negative norm."          if s<0: raise NegativeNorm,"negative norm."
# Line 572  class Defect(object): Line 608  class Defect(object):
608    
609      def eval(self,x):      def eval(self,x):
610          """          """
611          Returns the value F of a given C{x}.          Returns the value F of a given ``x``.
612    
613          @param x: value for which the defect C{F} is evaluated          :param x: value for which the defect ``F`` is evaluated
614          @return: value of the defect at C{x}          :return: value of the defect at ``x``
615          """          """
616          return 0          return 0
617    
618      def __call__(self,x):      def __call__(self,x):
619          return self.eval(x)          return self.eval(x)
620    
621      def setDerivativeIncrementLength(self,inc=math.sqrt(util.EPSILON)):      def setDerivativeIncrementLength(self,inc=1000.*math.sqrt(util.EPSILON)):
622          """          """
623          Sets the relative length of the increment used to approximate the          Sets the relative length of the increment used to approximate the
624          derivative of the defect. The increment is inc*norm(x)/norm(v)*v in the          derivative of the defect. The increment is inc*norm(x)/norm(v)*v in the
625          direction of v with x as a starting point.          direction of v with x as a starting point.
626    
627          @param inc: relative increment length          :param inc: relative increment length
628          @type inc: positive C{float}          :type inc: positive ``float``
629          """          """
630          if inc<=0: raise ValueError,"positive increment required."          if inc<=0: raise ValueError,"positive increment required."
631          self.__inc=inc          self.__inc=inc
# Line 598  class Defect(object): Line 634  class Defect(object):
634          """          """
635          Returns the relative increment length used to approximate the          Returns the relative increment length used to approximate the
636          derivative of the defect.          derivative of the defect.
637          @return: value of the defect at C{x}          :return: value of the defect at ``x``
638          @rtype: positive C{float}          :rtype: positive ``float``
639          """          """
640          return self.__inc          return self.__inc
641    
642      def derivative(self, F0, x0, v, v_is_normalised=True):      def derivative(self, F0, x0, v, v_is_normalised=True):
643          """          """
644          Returns the directional derivative at C{x0} in the direction of C{v}.          Returns the directional derivative at ``x0`` in the direction of ``v``.
645    
646          @param F0: value of this defect at x0          :param F0: value of this defect at x0
647          @param x0: value at which derivative is calculated          :param x0: value at which derivative is calculated
648          @param v: direction          :param v: direction
649          @param v_is_normalised: True to indicate that C{v} is nomalized          :param v_is_normalised: True to indicate that ``v`` is nomalized
650                                  (self.norm(v)=0)                                  (self.norm(v)=0)
651          @return: derivative of this defect at x0 in the direction of C{v}          :return: derivative of this defect at x0 in the direction of ``v``
652          @note: by default numerical evaluation (self.eval(x0+eps*v)-F0)/eps is          :note: by default numerical evaluation (self.eval(x0+eps*v)-F0)/eps is
653                 used but this method maybe overwritten to use exact evaluation.                 used but this method maybe overwritten to use exact evaluation.
654          """          """
655          normx=self.norm(x0)          normx=self.norm(x0)
# Line 631  class Defect(object): Line 667  class Defect(object):
667          return (F1-F0)/epsnew          return (F1-F0)/epsnew
668    
669  ######################################  ######################################
670  def NewtonGMRES(defect, x, iter_max=100, sub_iter_max=20, atol=0,rtol=1.e-4, sub_tol_max=0.5, gamma=0.9, verbose=False):  def NewtonGMRES(defect, x, iter_max=100, sub_iter_max=20, atol=0,rtol=1.e-4, subtol_max=0.5, gamma=0.9, verbose=False):
671     """     """
672     Solves a non-linear problem M{F(x)=0} for unknown M{x} using the stopping     Solves a non-linear problem *F(x)=0* for unknown *x* using the stopping
673     criterion:     criterion:
674    
675     M{norm(F(x) <= atol + rtol * norm(F(x0)}     *norm(F(x) <= atol + rtol * norm(F(x0)*
676    
677     where M{x0} is the initial guess.     where *x0* is the initial guess.
678    
679     @param defect: object defining the function M{F}. C{defect.norm} defines the     :param defect: object defining the function *F*. ``defect.norm`` defines the
680                    M{norm} used in the stopping criterion.                    *norm* used in the stopping criterion.
681     @type defect: L{Defect}     :type defect: `Defect`
682     @param x: initial guess for the solution, C{x} is altered.     :param x: initial guess for the solution, ``x`` is altered.
683     @type x: any object type allowing basic operations such as     :type x: any object type allowing basic operations such as
684              C{numarray.NumArray}, L{Data}              ``numpy.ndarray``, `Data`
685     @param iter_max: maximum number of iteration steps     :param iter_max: maximum number of iteration steps
686     @type iter_max: positive C{int}     :type iter_max: positive ``int``
687     @param sub_iter_max: maximum number of inner iteration steps     :param sub_iter_max: maximum number of inner iteration steps
688     @type sub_iter_max: positive C{int}     :type sub_iter_max: positive ``int``
689     @param atol: absolute tolerance for the solution     :param atol: absolute tolerance for the solution
690     @type atol: positive C{float}     :type atol: positive ``float``
691     @param rtol: relative tolerance for the solution     :param rtol: relative tolerance for the solution
692     @type rtol: positive C{float}     :type rtol: positive ``float``
693     @param gamma: tolerance safety factor for inner iteration     :param gamma: tolerance safety factor for inner iteration
694     @type gamma: positive C{float}, less than 1     :type gamma: positive ``float``, less than 1
695     @param sub_tol_max: upper bound for inner tolerance     :param subtol_max: upper bound for inner tolerance
696     @type sub_tol_max: positive C{float}, less than 1     :type subtol_max: positive ``float``, less than 1
697     @return: an approximation of the solution with the desired accuracy     :return: an approximation of the solution with the desired accuracy
698     @rtype: same type as the initial guess     :rtype: same type as the initial guess
699     """     """
700     lmaxit=iter_max     lmaxit=iter_max
701     if atol<0: raise ValueError,"atol needs to be non-negative."     if atol<0: raise ValueError,"atol needs to be non-negative."
702     if rtol<0: raise ValueError,"rtol needs to be non-negative."     if rtol<0: raise ValueError,"rtol needs to be non-negative."
703     if rtol+atol<=0: raise ValueError,"rtol or atol needs to be non-negative."     if rtol+atol<=0: raise ValueError,"rtol or atol needs to be non-negative."
704     if gamma<=0 or gamma>=1: raise ValueError,"tolerance safety factor for inner iteration (gamma =%s) needs to be positive and less than 1."%gamma     if gamma<=0 or gamma>=1: raise ValueError,"tolerance safety factor for inner iteration (gamma =%s) needs to be positive and less than 1."%gamma
705     if sub_tol_max<=0 or sub_tol_max>=1: raise ValueError,"upper bound for inner tolerance for inner iteration (sub_tol_max =%s) needs to be positive and less than 1."%sub_tol_max     if subtol_max<=0 or subtol_max>=1: raise ValueError,"upper bound for inner tolerance for inner iteration (subtol_max =%s) needs to be positive and less than 1."%subtol_max
706    
707     F=defect(x)     F=defect(x)
708     fnrm=defect.norm(F)     fnrm=defect.norm(F)
709     stop_tol=atol + rtol*fnrm     stop_tol=atol + rtol*fnrm
710     sub_tol=sub_tol_max     subtol=subtol_max
711     if verbose: print "NewtonGMRES: initial residual = %e."%fnrm     if verbose: print "NewtonGMRES: initial residual = %e."%fnrm
712     if verbose: print "             tolerance = %e."%sub_tol     if verbose: print "             tolerance = %e."%subtol
713     iter=1     iter=1
714     #     #
715     # main iteration loop     # main iteration loop
# Line 681  def NewtonGMRES(defect, x, iter_max=100, Line 717  def NewtonGMRES(defect, x, iter_max=100,
717     while not fnrm<=stop_tol:     while not fnrm<=stop_tol:
718              if iter  >= iter_max: raise MaxIterReached,"maximum number of %s steps reached."%iter_max              if iter  >= iter_max: raise MaxIterReached,"maximum number of %s steps reached."%iter_max
719              #              #
720          #   adjust sub_tol_          #   adjust subtol_
721          #          #
722              if iter > 1:              if iter > 1:
723             rat=fnrm/fnrmo             rat=fnrm/fnrmo
724                 sub_tol_old=sub_tol                 subtol_old=subtol
725             sub_tol=gamma*rat**2             subtol=gamma*rat**2
726             if gamma*sub_tol_old**2 > .1: sub_tol=max(sub_tol,gamma*sub_tol_old**2)             if gamma*subtol_old**2 > .1: subtol=max(subtol,gamma*subtol_old**2)
727             sub_tol=max(min(sub_tol,sub_tol_max), .5*stop_tol/fnrm)             subtol=max(min(subtol,subtol_max), .5*stop_tol/fnrm)
728          #          #
729          # calculate newton increment xc          # calculate newton increment xc
730              #     if iter_max in __FDGMRES is reached MaxIterReached is thrown              #     if iter_max in __FDGMRES is reached MaxIterReached is thrown
# Line 696  def NewtonGMRES(defect, x, iter_max=100, Line 732  def NewtonGMRES(defect, x, iter_max=100,
732              #     if  atol is reached sub_iter returns the numer of steps performed to get there              #     if  atol is reached sub_iter returns the numer of steps performed to get there
733              #              #
734              #              #
735              if verbose: print "             subiteration (GMRES) is called with relative tolerance %e."%sub_tol              if verbose: print "             subiteration (GMRES) is called with relative tolerance %e."%subtol
736              try:              try:
737                 xc, sub_iter=__FDGMRES(F, defect, x, sub_tol*fnrm, iter_max=iter_max-iter, iter_restart=sub_iter_max)                 xc, sub_iter=__FDGMRES(F, defect, x, subtol*fnrm, iter_max=iter_max-iter, iter_restart=sub_iter_max)
738              except MaxIterReached:              except MaxIterReached:
739                 raise MaxIterReached,"maximum number of %s steps reached."%iter_max                 raise MaxIterReached,"maximum number of %s steps reached."%iter_max
740              if sub_iter<0:              if sub_iter<0:
# Line 717  def NewtonGMRES(defect, x, iter_max=100, Line 753  def NewtonGMRES(defect, x, iter_max=100,
753  def __givapp(c,s,vin):  def __givapp(c,s,vin):
754      """      """
755      Applies a sequence of Givens rotations (c,s) recursively to the vector      Applies a sequence of Givens rotations (c,s) recursively to the vector
756      C{vin}      ``vin``
757    
758      @warning: C{vin} is altered.      :warning: ``vin`` is altered.
759      """      """
760      vrot=vin      vrot=vin
761      if isinstance(c,float):      if isinstance(c,float):
# Line 733  def __givapp(c,s,vin): Line 769  def __givapp(c,s,vin):
769      return vrot      return vrot
770    
771  def __FDGMRES(F0, defect, x0, atol, iter_max=100, iter_restart=20):  def __FDGMRES(F0, defect, x0, atol, iter_max=100, iter_restart=20):
772     h=numarray.zeros((iter_restart,iter_restart),numarray.Float64)     h=numpy.zeros((iter_restart,iter_restart),numpy.float64)
773     c=numarray.zeros(iter_restart,numarray.Float64)     c=numpy.zeros(iter_restart,numpy.float64)
774     s=numarray.zeros(iter_restart,numarray.Float64)     s=numpy.zeros(iter_restart,numpy.float64)
775     g=numarray.zeros(iter_restart,numarray.Float64)     g=numpy.zeros(iter_restart,numpy.float64)
776     v=[]     v=[]
777    
778     rho=defect.norm(F0)     rho=defect.norm(F0)
# Line 777  def __FDGMRES(F0, defect, x0, atol, iter Line 813  def __FDGMRES(F0, defect, x0, atol, iter
813    
814          #   Form and store the information for the new Givens rotation          #   Form and store the information for the new Givens rotation
815          if iter > 0 :          if iter > 0 :
816              hhat=numarray.zeros(iter+1,numarray.Float64)              hhat=numpy.zeros(iter+1,numpy.float64)
817              for i in range(iter+1) : hhat[i]=h[i,iter]              for i in range(iter+1) : hhat[i]=h[i,iter]
818              hhat=__givapp(c[0:iter],s[0:iter],hhat);              hhat=__givapp(c[0:iter],s[0:iter],hhat);
819              for i in range(iter+1) : h[i,iter]=hhat[i]              for i in range(iter+1) : h[i,iter]=hhat[i]
# Line 800  def __FDGMRES(F0, defect, x0, atol, iter Line 836  def __FDGMRES(F0, defect, x0, atol, iter
836     # At this point either iter > iter_max or rho < tol.     # At this point either iter > iter_max or rho < tol.
837     # It's time to compute x and leave.     # It's time to compute x and leave.
838     if iter > 0 :     if iter > 0 :
839       y=numarray.zeros(iter,numarray.Float64)       y=numpy.zeros(iter,numpy.float64)
840       y[iter-1] = g[iter-1] / h[iter-1,iter-1]       y[iter-1] = g[iter-1] / h[iter-1,iter-1]
841       if iter > 1 :       if iter > 1 :
842          i=iter-2          i=iter-2
843          while i>=0 :          while i>=0 :
844            y[i] = ( g[i] - numarray.dot(h[i,i+1:iter], y[i+1:iter])) / h[i,i]            y[i] = ( g[i] - numpy.dot(h[i,i+1:iter], y[i+1:iter])) / h[i,i]
845            i=i-1            i=i-1
846       xhat=v[iter-1]*y[iter-1]       xhat=v[iter-1]*y[iter-1]
847       for i in range(iter-1):       for i in range(iter-1):
# Line 824  def GMRES(r, Aprod, x, bilinearform, ato Line 860  def GMRES(r, Aprod, x, bilinearform, ato
860     """     """
861     Solver for     Solver for
862    
863     M{Ax=b}     *Ax=b*
864    
865     with a general operator A (more details required!).     with a general operator A (more details required!).
866     It uses the generalized minimum residual method (GMRES).     It uses the generalized minimum residual method (GMRES).
867    
868     The iteration is terminated if     The iteration is terminated if
869    
870     M{|r| <= atol+rtol*|r0|}     *|r| <= atol+rtol*|r0|*
871    
872     where M{r0} is the initial residual and M{|.|} is the energy norm. In fact     where *r0* is the initial residual and *|.|* is the energy norm. In fact
873    
874     M{|r| = sqrt( bilinearform(r,r))}     *|r| = sqrt( bilinearform(r,r))*
875    
876     @param r: initial residual M{r=b-Ax}. C{r} is altered.     :param r: initial residual *r=b-Ax*. ``r`` is altered.
877     @type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)     :type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
878     @param x: an initial guess for the solution     :param x: an initial guess for the solution
879     @type x: same like C{r}     :type x: same like ``r``
880     @param Aprod: returns the value Ax     :param Aprod: returns the value Ax
881     @type Aprod: function C{Aprod(x)} where C{x} is of the same object like     :type Aprod: function ``Aprod(x)`` where ``x`` is of the same object like
882                  argument C{x}. The returned object needs to be of the same                  argument ``x``. The returned object needs to be of the same
883                  type like argument C{r}.                  type like argument ``r``.
884     @param bilinearform: inner product C{<x,r>}     :param bilinearform: inner product ``<x,r>``
885     @type bilinearform: function C{bilinearform(x,r)} where C{x} is of the same     :type bilinearform: function ``bilinearform(x,r)`` where ``x`` is of the same
886                         type like argument C{x} and C{r}. The returned value is                         type like argument ``x`` and ``r``. The returned value is
887                         a C{float}.                         a ``float``.
888     @param atol: absolute tolerance     :param atol: absolute tolerance
889     @type atol: non-negative C{float}     :type atol: non-negative ``float``
890     @param rtol: relative tolerance     :param rtol: relative tolerance
891     @type rtol: non-negative C{float}     :type rtol: non-negative ``float``
892     @param iter_max: maximum number of iteration steps     :param iter_max: maximum number of iteration steps
893     @type iter_max: C{int}     :type iter_max: ``int``
894     @param iter_restart: in order to save memory the orthogonalization process     :param iter_restart: in order to save memory the orthogonalization process
895                          is terminated after C{iter_restart} steps and the                          is terminated after ``iter_restart`` steps and the
896                          iteration is restarted.                          iteration is restarted.
897     @type iter_restart: C{int}     :type iter_restart: ``int``
898     @return: the solution approximation and the corresponding residual     :return: the solution approximation and the corresponding residual
899     @rtype: C{tuple}     :rtype: ``tuple``
900     @warning: C{r} and C{x} are altered.     :warning: ``r`` and ``x`` are altered.
901     """     """
902     m=iter_restart     m=iter_restart
903     restarted=False     restarted=False
# Line 895  def GMRES(r, Aprod, x, bilinearform, ato Line 931  def GMRES(r, Aprod, x, bilinearform, ato
931  def _GMRESm(r, Aprod, x, bilinearform, atol, iter_max=100, iter_restart=20, verbose=False, P_R=None):  def _GMRESm(r, Aprod, x, bilinearform, atol, iter_max=100, iter_restart=20, verbose=False, P_R=None):
932     iter=0     iter=0
933    
934     h=numarray.zeros((iter_restart+1,iter_restart),numarray.Float64)     h=numpy.zeros((iter_restart+1,iter_restart),numpy.float64)
935     c=numarray.zeros(iter_restart,numarray.Float64)     c=numpy.zeros(iter_restart,numpy.float64)
936     s=numarray.zeros(iter_restart,numarray.Float64)     s=numpy.zeros(iter_restart,numpy.float64)
937     g=numarray.zeros(iter_restart+1,numarray.Float64)     g=numpy.zeros(iter_restart+1,numpy.float64)
938     v=[]     v=[]
939    
940     r_dot_r = bilinearform(r, r)     r_dot_r = bilinearform(r, r)
# Line 966  def _GMRESm(r, Aprod, x, bilinearform, a Line 1002  def _GMRESm(r, Aprod, x, bilinearform, a
1002    
1003     if verbose: print "GMRES: iteration stopped after %s step."%iter     if verbose: print "GMRES: iteration stopped after %s step."%iter
1004     if iter > 0 :     if iter > 0 :
1005       y=numarray.zeros(iter,numarray.Float64)       y=numpy.zeros(iter,numpy.float64)
1006       y[iter-1] = g[iter-1] / h[iter-1,iter-1]       y[iter-1] = g[iter-1] / h[iter-1,iter-1]
1007       if iter > 1 :       if iter > 1 :
1008          i=iter-2          i=iter-2
1009          while i>=0 :          while i>=0 :
1010            y[i] = ( g[i] - numarray.dot(h[i,i+1:iter], y[i+1:iter])) / h[i,i]            y[i] = ( g[i] - numpy.dot(h[i,i+1:iter], y[i+1:iter])) / h[i,i]
1011            i=i-1            i=i-1
1012       xhat=v[iter-1]*y[iter-1]       xhat=v[iter-1]*y[iter-1]
1013       for i in range(iter-1):       for i in range(iter-1):
# Line 993  def MINRES(r, Aprod, x, Msolve, bilinear Line 1029  def MINRES(r, Aprod, x, Msolve, bilinear
1029      """      """
1030      Solver for      Solver for
1031    
1032      M{Ax=b}      *Ax=b*
1033    
1034      with a symmetric and positive definite operator A (more details required!).      with a symmetric and positive definite operator A (more details required!).
1035      It uses the minimum residual method (MINRES) with preconditioner M      It uses the minimum residual method (MINRES) with preconditioner M
# Line 1001  def MINRES(r, Aprod, x, Msolve, bilinear Line 1037  def MINRES(r, Aprod, x, Msolve, bilinear
1037    
1038      The iteration is terminated if      The iteration is terminated if
1039    
1040      M{|r| <= atol+rtol*|r0|}      *|r| <= atol+rtol*|r0|*
1041    
1042      where M{r0} is the initial residual and M{|.|} is the energy norm. In fact      where *r0* is the initial residual and *|.|* is the energy norm. In fact
1043    
1044      M{|r| = sqrt( bilinearform(Msolve(r),r))}      *|r| = sqrt( bilinearform(Msolve(r),r))*
1045    
1046      For details on the preconditioned conjugate gradient method see the book:      For details on the preconditioned conjugate gradient method see the book:
1047    
# Line 1013  def MINRES(r, Aprod, x, Msolve, bilinear Line 1049  def MINRES(r, Aprod, x, Msolve, bilinear
1049      T.F. Chan, J. Demmel, J. Donato, J. Dongarra, V. Eijkhout, R. Pozo,      T.F. Chan, J. Demmel, J. Donato, J. Dongarra, V. Eijkhout, R. Pozo,
1050      C. Romine, and H. van der Vorst}.      C. Romine, and H. van der Vorst}.
1051    
1052      @param r: initial residual M{r=b-Ax}. C{r} is altered.      :param r: initial residual *r=b-Ax*. ``r`` is altered.
1053      @type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)      :type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
1054      @param x: an initial guess for the solution      :param x: an initial guess for the solution
1055      @type x: any object supporting inplace add (x+=y) and scaling (x=scalar*y)      :type x: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
1056      @param Aprod: returns the value Ax      :param Aprod: returns the value Ax
1057      @type Aprod: function C{Aprod(x)} where C{x} is of the same object like      :type Aprod: function ``Aprod(x)`` where ``x`` is of the same object like
1058                   argument C{x}. The returned object needs to be of the same                   argument ``x``. The returned object needs to be of the same
1059                   type like argument C{r}.                   type like argument ``r``.
1060      @param Msolve: solves Mx=r      :param Msolve: solves Mx=r
1061      @type Msolve: function C{Msolve(r)} where C{r} is of the same type like      :type Msolve: function ``Msolve(r)`` where ``r`` is of the same type like
1062                    argument C{r}. The returned object needs to be of the same                    argument ``r``. The returned object needs to be of the same
1063                    type like argument C{x}.                    type like argument ``x``.
1064      @param bilinearform: inner product C{<x,r>}      :param bilinearform: inner product ``<x,r>``
1065      @type bilinearform: function C{bilinearform(x,r)} where C{x} is of the same      :type bilinearform: function ``bilinearform(x,r)`` where ``x`` is of the same
1066                          type like argument C{x} and C{r} is. The returned value                          type like argument ``x`` and ``r`` is. The returned value
1067                          is a C{float}.                          is a ``float``.
1068      @param atol: absolute tolerance      :param atol: absolute tolerance
1069      @type atol: non-negative C{float}      :type atol: non-negative ``float``
1070      @param rtol: relative tolerance      :param rtol: relative tolerance
1071      @type rtol: non-negative C{float}      :type rtol: non-negative ``float``
1072      @param iter_max: maximum number of iteration steps      :param iter_max: maximum number of iteration steps
1073      @type iter_max: C{int}      :type iter_max: ``int``
1074      @return: the solution approximation and the corresponding residual      :return: the solution approximation and the corresponding residual
1075      @rtype: C{tuple}      :rtype: ``tuple``
1076      @warning: C{r} and C{x} are altered.      :warning: ``r`` and ``x`` are altered.
1077      """      """
1078      #------------------------------------------------------------------      #------------------------------------------------------------------
1079      # Set up y and v for the first Lanczos vector v1.      # Set up y and v for the first Lanczos vector v1.
# Line 1171  def TFQMR(r, Aprod, x, bilinearform, ato Line 1207  def TFQMR(r, Aprod, x, bilinearform, ato
1207    """    """
1208    Solver for    Solver for
1209    
1210    M{Ax=b}    *Ax=b*
1211    
1212    with a general operator A (more details required!).    with a general operator A (more details required!).
1213    It uses the Transpose-Free Quasi-Minimal Residual method (TFQMR).    It uses the Transpose-Free Quasi-Minimal Residual method (TFQMR).
1214    
1215    The iteration is terminated if    The iteration is terminated if
1216    
1217    M{|r| <= atol+rtol*|r0|}    *|r| <= atol+rtol*|r0|*
1218    
1219    where M{r0} is the initial residual and M{|.|} is the energy norm. In fact    where *r0* is the initial residual and *|.|* is the energy norm. In fact
1220    
1221    M{|r| = sqrt( bilinearform(r,r))}    *|r| = sqrt( bilinearform(r,r))*
1222    
1223    @param r: initial residual M{r=b-Ax}. C{r} is altered.    :param r: initial residual *r=b-Ax*. ``r`` is altered.
1224    @type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)    :type r: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
1225    @param x: an initial guess for the solution    :param x: an initial guess for the solution
1226    @type x: same like C{r}    :type x: same like ``r``
1227    @param Aprod: returns the value Ax    :param Aprod: returns the value Ax
1228    @type Aprod: function C{Aprod(x)} where C{x} is of the same object like    :type Aprod: function ``Aprod(x)`` where ``x`` is of the same object like
1229                 argument C{x}. The returned object needs to be of the same type                 argument ``x``. The returned object needs to be of the same type
1230                 like argument C{r}.                 like argument ``r``.
1231    @param bilinearform: inner product C{<x,r>}    :param bilinearform: inner product ``<x,r>``
1232    @type bilinearform: function C{bilinearform(x,r)} where C{x} is of the same    :type bilinearform: function ``bilinearform(x,r)`` where ``x`` is of the same
1233                        type like argument C{x} and C{r}. The returned value is                        type like argument ``x`` and ``r``. The returned value is
1234                        a C{float}.                        a ``float``.
1235    @param atol: absolute tolerance    :param atol: absolute tolerance
1236    @type atol: non-negative C{float}    :type atol: non-negative ``float``
1237    @param rtol: relative tolerance    :param rtol: relative tolerance
1238    @type rtol: non-negative C{float}    :type rtol: non-negative ``float``
1239    @param iter_max: maximum number of iteration steps    :param iter_max: maximum number of iteration steps
1240    @type iter_max: C{int}    :type iter_max: ``int``
1241    @rtype: C{tuple}    :rtype: ``tuple``
1242    @warning: C{r} and C{x} are altered.    :warning: ``r`` and ``x`` are altered.
1243    """    """
1244    u1=0    u1=0
1245    u2=0    u2=0
# Line 1273  def TFQMR(r, Aprod, x, bilinearform, ato Line 1309  def TFQMR(r, Aprod, x, bilinearform, ato
1309    
1310  class ArithmeticTuple(object):  class ArithmeticTuple(object):
1311     """     """
1312     Tuple supporting inplace update x+=y and scaling x=a*y where C{x,y} is an     Tuple supporting inplace update x+=y and scaling x=a*y where ``x,y`` is an
1313     ArithmeticTuple and C{a} is a float.     ArithmeticTuple and ``a`` is a float.
1314    
1315     Example of usage::     Example of usage::
1316    
1317         from esys.escript import Data         from esys.escript import Data
1318         from numarray import array         from numpy import array
1319         a=Data(...)         a=Data(...)
1320         b=array([1.,4.])         b=array([1.,4.])
1321         x=ArithmeticTuple(a,b)         x=ArithmeticTuple(a,b)
# Line 1288  class ArithmeticTuple(object): Line 1324  class ArithmeticTuple(object):
1324     """     """
1325     def __init__(self,*args):     def __init__(self,*args):
1326         """         """
1327         Initializes object with elements C{args}.         Initializes object with elements ``args``.
1328    
1329         @param args: tuple of objects that support inplace add (x+=y) and         :param args: tuple of objects that support inplace add (x+=y) and
1330                      scaling (x=a*y)                      scaling (x=a*y)
1331         """         """
1332         self.__items=list(args)         self.__items=list(args)
# Line 1299  class ArithmeticTuple(object): Line 1335  class ArithmeticTuple(object):
1335         """         """
1336         Returns the number of items.         Returns the number of items.
1337    
1338         @return: number of items         :return: number of items
1339         @rtype: C{int}         :rtype: ``int``
1340         """         """
1341         return len(self.__items)         return len(self.__items)
1342    
# Line 1308  class ArithmeticTuple(object): Line 1344  class ArithmeticTuple(object):
1344         """         """
1345         Returns item at specified position.         Returns item at specified position.
1346    
1347         @param index: index of item to be returned         :param index: index of item to be returned
1348         @type index: C{int}         :type index: ``int``
1349         @return: item with index C{index}         :return: item with index ``index``
1350         """         """
1351         return self.__items.__getitem__(index)         return self.__items.__getitem__(index)
1352    
1353     def __mul__(self,other):     def __mul__(self,other):
1354         """         """
1355         Scales by C{other} from the right.         Scales by ``other`` from the right.
1356    
1357         @param other: scaling factor         :param other: scaling factor
1358         @type other: C{float}         :type other: ``float``
1359         @return: itemwise self*other         :return: itemwise self*other
1360         @rtype: L{ArithmeticTuple}         :rtype: `ArithmeticTuple`
1361         """         """
1362         out=[]         out=[]
1363         try:         try:
# Line 1335  class ArithmeticTuple(object): Line 1371  class ArithmeticTuple(object):
1371    
1372     def __rmul__(self,other):     def __rmul__(self,other):
1373         """         """
1374         Scales by C{other} from the left.         Scales by ``other`` from the left.
1375    
1376         @param other: scaling factor         :param other: scaling factor
1377         @type other: C{float}         :type other: ``float``
1378         @return: itemwise other*self         :return: itemwise other*self
1379         @rtype: L{ArithmeticTuple}         :rtype: `ArithmeticTuple`
1380         """         """
1381         out=[]         out=[]
1382         try:         try:
# Line 1354  class ArithmeticTuple(object): Line 1390  class ArithmeticTuple(object):
1390    
1391     def __div__(self,other):     def __div__(self,other):
1392         """         """
1393         Scales by (1/C{other}) from the right.         Scales by (1/``other``) from the right.
1394    
1395         @param other: scaling factor         :param other: scaling factor
1396         @type other: C{float}         :type other: ``float``
1397         @return: itemwise self/other         :return: itemwise self/other
1398         @rtype: L{ArithmeticTuple}         :rtype: `ArithmeticTuple`
1399         """         """
1400         return self*(1/other)         return self*(1/other)
1401    
1402     def __rdiv__(self,other):     def __rdiv__(self,other):
1403         """         """
1404         Scales by (1/C{other}) from the left.         Scales by (1/``other``) from the left.
1405    
1406         @param other: scaling factor         :param other: scaling factor
1407         @type other: C{float}         :type other: ``float``
1408         @return: itemwise other/self         :return: itemwise other/self
1409         @rtype: L{ArithmeticTuple}         :rtype: `ArithmeticTuple`
1410         """         """
1411         out=[]         out=[]
1412         try:         try:
# Line 1384  class ArithmeticTuple(object): Line 1420  class ArithmeticTuple(object):
1420    
1421     def __iadd__(self,other):     def __iadd__(self,other):
1422         """         """
1423         Inplace addition of C{other} to self.         Inplace addition of ``other`` to self.
1424    
1425         @param other: increment         :param other: increment
1426         @type other: C{ArithmeticTuple}         :type other: ``ArithmeticTuple``
1427         """         """
1428         if len(self) != len(other):         if len(self) != len(other):
1429             raise ValueError,"tuple lengths must match."             raise ValueError,"tuple lengths must match."
# Line 1397  class ArithmeticTuple(object): Line 1433  class ArithmeticTuple(object):
1433    
1434     def __add__(self,other):     def __add__(self,other):
1435         """         """
1436         Adds C{other} to self.         Adds ``other`` to self.
1437    
1438         @param other: increment         :param other: increment
1439         @type other: C{ArithmeticTuple}         :type other: ``ArithmeticTuple``
1440         """         """
1441         out=[]         out=[]
1442         try:         try:
# Line 1414  class ArithmeticTuple(object): Line 1450  class ArithmeticTuple(object):
1450    
1451     def __sub__(self,other):     def __sub__(self,other):
1452         """         """
1453         Subtracts C{other} from self.         Subtracts ``other`` from self.
1454    
1455         @param other: decrement         :param other: decrement
1456         @type other: C{ArithmeticTuple}         :type other: ``ArithmeticTuple``
1457         """         """
1458         out=[]         out=[]
1459         try:         try:
# Line 1431  class ArithmeticTuple(object): Line 1467  class ArithmeticTuple(object):
1467    
1468     def __isub__(self,other):     def __isub__(self,other):
1469         """         """
1470         Inplace subtraction of C{other} from self.         Inplace subtraction of ``other`` from self.
1471    
1472         @param other: decrement         :param other: decrement
1473         @type other: C{ArithmeticTuple}         :type other: ``ArithmeticTuple``
1474         """         """
1475         if len(self) != len(other):         if len(self) != len(other):
1476             raise ValueError,"tuple length must match."             raise ValueError,"tuple length must match."
# Line 1457  class HomogeneousSaddlePointProblem(obje Line 1493  class HomogeneousSaddlePointProblem(obje
1493        This class provides a framework for solving linear homogeneous saddle        This class provides a framework for solving linear homogeneous saddle
1494        point problems of the form::        point problems of the form::
1495    
1496            M{Av+B^*p=f}            *Av+B^*p=f*
1497            M{Bv     =0}            *Bv     =0*
1498    
1499        for the unknowns M{v} and M{p} and given operators M{A} and M{B} and        for the unknowns *v* and *p* and given operators *A* and *B* and
1500        given right hand side M{f}. M{B^*} is the adjoint operator of M{B}.        given right hand side *f*. *B^** is the adjoint operator of *B*.
1501          *A* may depend weakly on *v* and *p*.
1502        """        """
1503        def __init__(self,**kwargs):        def __init__(self, **kwargs):
1504        """
1505        initializes the saddle point problem
1506        """
1507            self.resetControlParameters()
1508          self.setTolerance()          self.setTolerance()
1509          self.setAbsoluteTolerance()          self.setAbsoluteTolerance()
1510          self.setSubProblemTolerance()        def resetControlParameters(self,gamma=0.85, gamma_min=1.e-2,chi_max=0.1, omega_div=0.2, omega_conv=1.1, rtol_min=1.e-7, rtol_max=0.9, chi=1., C_p=1., C_v=1., safety_factor=0.3):
1511             """
1512             sets a control parameter
1513    
1514        #=============================================================           :param gamma: ``1/(1-gamma)`` controls the perturbation of the converegence rate due to termination errors in the subproblems.
1515        def initialize(self):           :type gamma: ``float``
1516          """           :param gamma_min: minimum value for ``gamma``.
1517          Initializes the problem (overwrite).           :type gamma_min: ``float``
1518          """           :param chi_max: maximum tolerable converegence rate.
1519          pass           :type chi_max: ``float``
1520             :param omega_div: reduction fact for ``gamma`` if no convergence is detected.
1521             :type omega_div: ``float``
1522             :param omega_conv: raise fact for ``gamma`` if convergence is detected.
1523             :type omega_conv: ``float``
1524             :param rtol_min: minimum relative tolerance used to calculate presssure and velocity increment.
1525             :type rtol_min: ``float``
1526             :param rtol_max: maximuim relative tolerance used to calculate presssure and velocity increment.
1527             :type rtol_max: ``float``
1528             :param chi: initial convergence measure.
1529             :type chi: ``float``
1530             :param C_p: initial value for constant to adjust pressure tolerance
1531             :type C_p: ``float``
1532             :param C_v: initial value for constant to adjust velocity tolerance
1533             :type C_v: ``float``
1534             :param safety_factor: safety factor for addjustment of pressure and velocity tolerance from stopping criteria
1535             :type safety_factor: ``float``
1536             """
1537             self.setControlParameter(gamma, gamma_min ,chi_max , omega_div , omega_conv, rtol_min , rtol_max, chi,C_p, C_v,safety_factor)
1538    
1539          def setControlParameter(self,gamma=None, gamma_min=None ,chi_max=None, omega_div=None, omega_conv=None, rtol_min=None, rtol_max=None, chi=None, C_p=None, C_v=None, safety_factor=None):
1540             """
1541             sets a control parameter
1542    
1543             :param gamma: ``1/(1-gamma)`` controls the perturbation of the converegence rate due to termination errors in the subproblems.
1544             :type gamma: ``float``
1545             :param gamma_min: minimum value for ``gamma``.
1546             :type gamma_min: ``float``
1547             :param chi_max: maximum tolerable converegence rate.
1548             :type chi_max: ``float``
1549             :param omega_div: reduction fact for ``gamma`` if no convergence is detected.
1550             :type omega_div: ``float``
1551             :param omega_conv: raise fact for ``gamma`` if convergence is detected.
1552             :type omega_conv: ``float``
1553             :param rtol_min: minimum relative tolerance used to calculate presssure and velocity increment.
1554             :type rtol_min: ``float``
1555             :param rtol_max: maximuim relative tolerance used to calculate presssure and velocity increment.
1556             :type rtol_max: ``float``
1557             :param chi: initial convergence measure.
1558             :type chi: ``float``
1559             :param C_p: initial value for constant to adjust pressure tolerance
1560             :type C_p: ``float``
1561             :param C_v: initial value for constant to adjust velocity tolerance
1562             :type C_v: ``float``
1563             :param safety_factor: safety factor for addjustment of pressure and velocity tolerance from stopping criteria
1564             :type safety_factor: ``float``
1565             """
1566             if not gamma == None:
1567                if gamma<=0 or gamma>=1:
1568                   raise ValueError,"Initial gamma needs to be positive and less than 1."
1569             else:
1570                gamma=self.__gamma
1571    
1572             if not gamma_min == None:
1573                if gamma_min<=0 or gamma_min>=1:
1574                   raise ValueError,"gamma_min needs to be positive and less than 1."
1575             else:
1576                gamma_min = self.__gamma_min
1577    
1578             if not chi_max == None:
1579                if chi_max<=0 or chi_max>=1:
1580                   raise ValueError,"chi_max needs to be positive and less than 1."
1581             else:
1582                chi_max = self.__chi_max
1583    
1584             if not omega_div == None:
1585                if omega_div<=0 or omega_div >=1:
1586                   raise ValueError,"omega_div needs to be positive and less than 1."
1587             else:
1588                omega_div=self.__omega_div
1589    
1590             if not omega_conv == None:
1591                if omega_conv<1:
1592                   raise ValueError,"omega_conv needs to be greater or equal to 1."
1593             else:
1594                omega_conv=self.__omega_conv
1595    
1596             if not rtol_min == None:
1597                if rtol_min<=0 or rtol_min>=1:
1598                   raise ValueError,"rtol_min needs to be positive and less than 1."
1599             else:
1600                rtol_min=self.__rtol_min
1601    
1602             if not rtol_max == None:
1603                if rtol_max<=0 or rtol_max>=1:
1604                   raise ValueError,"rtol_max needs to be positive and less than 1."
1605             else:
1606                rtol_max=self.__rtol_max
1607    
1608             if not chi == None:
1609                if chi<=0 or chi>1:
1610                   raise ValueError,"chi needs to be positive and less than 1."
1611             else:
1612                chi=self.__chi
1613    
1614             if not C_p == None:
1615                if C_p<1:
1616                   raise ValueError,"C_p need to be greater or equal to 1."
1617             else:
1618                C_p=self.__C_p
1619    
1620             if not C_v == None:
1621                if C_v<1:
1622                   raise ValueError,"C_v need to be greater or equal to 1."
1623             else:
1624                C_v=self.__C_v
1625    
1626             if not safety_factor == None:
1627                if safety_factor<=0 or safety_factor>1:
1628                   raise ValueError,"safety_factor need to be between zero and one."
1629             else:
1630                safety_factor=self.__safety_factor
1631    
1632             if gamma<gamma_min:
1633                   raise ValueError,"gamma = %e needs to be greater or equal gamma_min = %e."%(gamma,gamma_min)
1634             if rtol_max<=rtol_min:
1635                   raise ValueError,"rtol_max = %e needs to be greater rtol_min = %e."%(rtol_max,rtol_min)
1636                
1637             self.__gamma = gamma
1638             self.__gamma_min = gamma_min
1639             self.__chi_max = chi_max
1640             self.__omega_div = omega_div
1641             self.__omega_conv = omega_conv
1642             self.__rtol_min = rtol_min
1643             self.__rtol_max = rtol_max
1644             self.__chi = chi
1645             self.__C_p = C_p
1646             self.__C_v = C_v
1647             self.__safety_factor = safety_factor
1648    
1649        def inner_pBv(self,p,v):        #=============================================================
1650          def inner_pBv(self,p,Bv):
1651           """           """
1652           Returns inner product of element p and Bv (overwrite).           Returns inner product of element p and Bv (overwrite).
1653    
1654           @param p: a pressure increment           :param p: a pressure increment
1655           @param v: a residual           :param Bv: a residual
1656           @return: inner product of element p and Bv           :return: inner product of element p and Bv
1657           @rtype: C{float}           :rtype: ``float``
1658           @note: used if PCG is applied.           :note: used if PCG is applied.
1659           """           """
1660           raise NotImplementedError,"no inner product for p implemented."           raise NotImplementedError,"no inner product for p and Bv implemented."
1661    
1662        def inner_p(self,p0,p1):        def inner_p(self,p0,p1):
1663           """           """
1664           Returns inner product of p0 and p1 (overwrite).           Returns inner product of p0 and p1 (overwrite).
1665    
1666           @param p0: a pressure           :param p0: a pressure
1667           @param p1: a pressure           :param p1: a pressure
1668           @return: inner product of p0 and p1           :return: inner product of p0 and p1
1669           @rtype: C{float}           :rtype: ``float``
1670           """           """
1671           raise NotImplementedError,"no inner product for p implemented."           raise NotImplementedError,"no inner product for p implemented."
1672        
# Line 1502  class HomogeneousSaddlePointProblem(obje Line 1674  class HomogeneousSaddlePointProblem(obje
1674           """           """
1675           Returns the norm of v (overwrite).           Returns the norm of v (overwrite).
1676    
1677           @param v: a velovity           :param v: a velovity
1678           @return: norm of v           :return: norm of v
1679           @rtype: non-negative C{float}           :rtype: non-negative ``float``
1680           """           """
1681           raise NotImplementedError,"no norm of v implemented."           raise NotImplementedError,"no norm of v implemented."
1682          def getDV(self, p, v, tol):
   
       def getV(self, p, v0):  
1683           """           """
1684           return the value for v for a given p (overwrite)           return a correction to the value for a given v and a given p with accuracy `tol` (overwrite)
1685    
1686           @param p: a pressure           :param p: pressure
1687           @param v0: a initial guess for the value v to return.           :param v: pressure
1688           @return: v given as M{v= A^{-1} (f-B^*p)}           :return: dv given as *dv= A^{-1} (f-A v-B^*p)*
1689             :note: Only *A* may depend on *v* and *p*
1690           """           """
1691           raise NotImplementedError,"no v calculation implemented."           raise NotImplementedError,"no dv calculation implemented."
1692    
1693                    
1694        def norm_Bv(self,v):        def Bv(self,v, tol):
1695          """          """
1696          Returns Bv (overwrite).          Returns Bv with accuracy `tol` (overwrite)
1697    
1698          @rtype: equal to the type of p          :rtype: equal to the type of p
1699          @note: boundary conditions on p should be zero!          :note: boundary conditions on p should be zero!
1700          """          """
1701          raise NotImplementedError, "no operator B implemented."          raise NotImplementedError, "no operator B implemented."
1702    
1703        def solve_AinvBt(self,p):        def norm_Bv(self,Bv):
1704            """
1705            Returns the norm of Bv (overwrite).
1706    
1707            :rtype: equal to the type of p
1708            :note: boundary conditions on p should be zero!
1709            """
1710            raise NotImplementedError, "no norm of Bv implemented."
1711    
1712          def solve_AinvBt(self,dp, tol):
1713           """           """
1714           Solves M{Av=B^*p} with accuracy L{self.getSubProblemTolerance()}           Solves *A dv=B^*dp* with accuracy `tol`
          (overwrite).  
1715    
1716           @param p: a pressure increment           :param dp: a pressure increment
1717           @return: the solution of M{Av=B^*p}           :return: the solution of *A dv=B^*dp*
1718           @note: boundary conditions on v should be zero!           :note: boundary conditions on dv should be zero! *A* is the operator used in ``getDV`` and must not be altered.
1719           """           """
1720           raise NotImplementedError,"no operator A implemented."           raise NotImplementedError,"no operator A implemented."
1721    
1722        def solve_precB(self,v):        def solve_prec(self,Bv, tol):
1723           """           """
1724           Provides a preconditioner for M{BA^{-1}B^*} with accuracy           Provides a preconditioner for *(BA^{-1}B^ * )* applied to Bv with accuracy `tol`
          L{self.getSubProblemTolerance()} (overwrite).  
1725    
1726           @rtype: equal to the type of p           :rtype: equal to the type of p
1727           @note: boundary conditions on p should be zero!           :note: boundary conditions on p should be zero!
1728           """           """
1729           raise NotImplementedError,"no preconditioner for Schur complement implemented."           raise NotImplementedError,"no preconditioner for Schur complement implemented."
1730        #=============================================================        #=============================================================
1731        def __Aprod_PCG(self,p):        def __Aprod_PCG(self,dp):
1732            return self.solve_AinvBt(p)            dv=self.solve_AinvBt(dp, self.__subtol)
1733              return ArithmeticTuple(dv,self.Bv(dv, self.__subtol))
1734    
1735        def __inner_PCG(self,p,v):        def __inner_PCG(self,p,r):
1736           return self.inner_pBv(p,v)           return self.inner_pBv(p,r[1])
1737    
1738        def __Msolve_PCG(self,v):        def __Msolve_PCG(self,r):
1739            return self.solve_precB(v)            return self.solve_prec(r[1], self.__subtol)
1740        #=============================================================        #=============================================================
1741        def __Aprod_GMRES(self,p):        def __Aprod_GMRES(self,p):
1742            return self.solve_precB(self.solve_AinvBt(p))            return self.solve_prec(self.Bv(self.solve_AinvBt(p, self.__subtol), self.__subtol), self.__subtol)
1743        def __inner_GMRES(self,p0,p1):        def __inner_GMRES(self,p0,p1):
1744           return self.inner_p(p0,p1)           return self.inner_p(p0,p1)
1745    
1746        #=============================================================        #=============================================================
1747        def norm_p(self,p):        def norm_p(self,p):
1748            """            """
1749            calculates the norm of C{p}            calculates the norm of ``p``
1750                        
1751            @param p: a pressure            :param p: a pressure
1752            @return: the norm of C{p} using the inner product for pressure            :return: the norm of ``p`` using the inner product for pressure
1753            @rtype: C{float}            :rtype: ``float``
1754            """            """
1755            f=self.inner_p(p,p)            f=self.inner_p(p,p)
1756            if f<0: raise ValueError,"negative pressure norm."            if f<0: raise ValueError,"negative pressure norm."
1757            return math.sqrt(f)            return math.sqrt(f)
1758                    
1759          def solve(self,v,p,max_iter=20, verbose=False, usePCG=True, iter_restart=20, max_correction_steps=10):
       def solve(self,v,p,max_iter=20, verbose=False, show_details=False, usePCG=True, iter_restart=20, max_correction_steps=10):  
1760           """           """
1761           Solves the saddle point problem using initial guesses v and p.           Solves the saddle point problem using initial guesses v and p.
1762    
1763           @param v: initial guess for velocity           :param v: initial guess for velocity
1764           @param p: initial guess for pressure           :param p: initial guess for pressure
1765           @type v: L{Data}           :type v: `Data`
1766           @type p: L{Data}           :type p: `Data`
1767           @param usePCG: indicates the usage of the PCG rather than GMRES scheme.           :param usePCG: indicates the usage of the PCG rather than GMRES scheme.
1768           @param max_iter: maximum number of iteration steps per correction           :param max_iter: maximum number of iteration steps per correction
1769                            attempt                            attempt
1770           @param verbose: if True, shows information on the progress of the           :param verbose: if True, shows information on the progress of the
1771                           saddlepoint problem solver.                           saddlepoint problem solver.
1772           @param show_details: if True, shows details of the sub problem solver           :param iter_restart: restart the iteration after ``iter_restart`` steps
          @param iter_restart: restart the iteration after C{iter_restart} steps  
1773                                (only used if useUzaw=False)                                (only used if useUzaw=False)
1774           @type usePCG: C{bool}           :type usePCG: ``bool``
1775           @type max_iter: C{int}           :type max_iter: ``int``
1776           @type verbose: C{bool}           :type verbose: ``bool``
1777           @type show_details: C{bool}           :type iter_restart: ``int``
1778           @type iter_restart: C{int}           :rtype: ``tuple`` of `Data` objects
1779           @rtype: C{tuple} of L{Data} objects           :note: typically this method is overwritten by a subclass. It provides a wrapper for the ``_solve`` method.
1780             """
1781             return self._solve(v=v,p=p,max_iter=max_iter,verbose=verbose, usePCG=usePCG, iter_restart=iter_restart, max_correction_steps=max_correction_steps)
1782    
1783          def _solve(self,v,p,max_iter=20, verbose=False, usePCG=True, iter_restart=20, max_correction_steps=10):
1784             """
1785             see `_solve` method.
1786           """           """
1787           self.verbose=verbose           self.verbose=verbose
          self.show_details=show_details and self.verbose  
1788           rtol=self.getTolerance()           rtol=self.getTolerance()
1789           atol=self.getAbsoluteTolerance()           atol=self.getAbsoluteTolerance()
1790           correction_step=0           correction_step=0
1791           converged=False           converged=False
1792             error=None
1793             chi=None
1794             gamma=self.__gamma
1795             C_p=self.__C_p
1796             C_v=self.__C_v
1797           while not converged:           while not converged:
1798                  if error== None or chi == None:
1799                      gamma_new=gamma/self.__omega_conv
1800                  else:
1801                     if chi < self.__chi_max:
1802                        gamma_new=min(max(gamma*self.__omega_conv,1-chi*error/(self.__safety_factor*ATOL)), 1-chi/self.__chi_max)
1803                     else:
1804                        gamma_new=gamma*self.__omega_div
1805                  gamma=max(gamma_new, self.__gamma_min)
1806                # calculate velocity for current pressure:                # calculate velocity for current pressure:
1807                v=self.getV(p,v)                rtol_v=min(max(gamma/(1.+gamma)/C_v,self.__rtol_min), self.__rtol_max)
1808                norm_v=self.norm_v(v)                rtol_p=min(max(gamma/C_p, self.__rtol_min), self.__rtol_max)
1809                norm_Bv=self.norm_Bv(v)                self.__subtol=rtol_p**2
1810                ATOL=norm_v*rtol+atol                if self.verbose: print "HomogeneousSaddlePointProblem: step %s: gamma = %e, rtol_v= %e, rtol_p=%e"%(correction_step,gamma,rtol_v,rtol_p)
1811                if self.verbose: print "HomogeneousSaddlePointProblem: norm v= %e, norm_Bv= %e, tolerance = %e."%(norm_v, norm_Bv,ATOL)                if self.verbose: print "HomogeneousSaddlePointProblem: subtolerance: %e"%self.__subtol
1812                  # calculate velocity for current pressure: A*dv= F-A*v-B*p
1813                  dv1=self.getDV(p,v,rtol_v)
1814                  v1=v+dv1
1815                  Bv1=self.Bv(v1, self.__subtol)
1816                  norm_Bv1=self.norm_Bv(Bv1)
1817                  norm_dv1=self.norm_v(dv1)
1818                  norm_v1=self.norm_v(v1)
1819                  ATOL=norm_v1*rtol+atol
1820                  if self.verbose: print "HomogeneousSaddlePointProblem: step %s: Bv = %e, dv = %e, v=%e"%(correction_step,norm_Bv1, norm_dv1, norm_v1)
1821                if not ATOL>0: raise ValueError,"overall absolute tolerance needs to be positive."                if not ATOL>0: raise ValueError,"overall absolute tolerance needs to be positive."
1822                if norm_Bv <= ATOL:                if max(norm_Bv1, norm_dv1) <= ATOL:
1823                   converged=True                    converged=True
1824                      v=v1
1825                else:                else:
1826                   correction_step+=1                    # now we solve for the pressure increment dp from B*A^{-1}B^* dp = Bv1
1827                   if correction_step>max_correction_steps:                    if usePCG:
1828                        raise CorrectionFailed,"Given up after %d correction steps."%correction_step                      dp,r,a_norm=PCG(ArithmeticTuple(v1,Bv1),self.__Aprod_PCG,0*p,self.__Msolve_PCG,self.__inner_PCG,atol=0, rtol=rtol_p,iter_max=max_iter, verbose=self.verbose)
1829                   dp=self.solve_precB(v)                      v2=r[0]
1830                   if usePCG:                      Bv2=r[1]
1831                     norm2=self.inner_pBv(dp,v)                    else:
1832                     if norm2<0: raise ValueError,"negative PCG norm."                      dp=GMRES(self.solve_prec(Bv1,self.__subtol),self.__Aprod_GMRES, 0*p, self.__inner_GMRES,atol=0, rtol=rtol_p,iter_max=max_iter, iter_restart=iter_restart, verbose=self.verbose)
1833                     norm2=math.sqrt(norm2)                      dv2=self.solve_AinvBt(dp, self.__subtol)
1834                   else:                      v2=v1-dv2
1835                     norm2=self.norm_p(dp)                      Bv2=self.Bv(v2, self.__subtol)
1836                   ATOL_ITER=ATOL/norm_Bv*norm2                    #
1837                   if self.verbose: print "HomogeneousSaddlePointProblem: tolerance for solver: %e"%ATOL_ITER                    # convergence indicators:
1838                   if usePCG:                    #
1839                         p,v0,a_norm=PCG(v,self.__Aprod_PCG,p,self.__Msolve_PCG,self.__inner_PCG,atol=ATOL_ITER, rtol=0.,iter_max=max_iter, verbose=self.verbose)                    norm_v2=self.norm_v(v2)
1840                   else:                    norm_dv2=self.norm_v(v2-v)
1841                         p=GMRES(dp,self.__Aprod_GMRES, p, self.__inner_GMRES,atol=ATOL_ITER, rtol=0.,iter_max=max_iter, iter_restart=iter_restart, verbose=self.verbose)                    error_new=max(norm_dv2, norm_Bv1)
1842           if self.verbose: print "HomogeneousSaddlePointProblem: tolerance reached."                    norm_Bv2=self.norm_Bv(Bv2)
1843                      if self.verbose: print "HomogeneousSaddlePointProblem: step %s (part 2): Bv = %e, dv = %e, v=%e"%(correction_step,norm_Bv2, norm_dv2, norm_v2)
1844                      if error !=None:
1845                          chi_new=error_new/error
1846                          if self.verbose: print "HomogeneousSaddlePointProblem: step %s: convergence rate = %e, est. error = %e"%(correction_step,chi_new, error_new)
1847                          if chi != None:
1848                              gamma0=max(gamma, 1-chi/chi_new)
1849                              C_p*=gamma0/gamma
1850                              C_v*=gamma0/gamma*(1+gamma)/(1+gamma0)
1851                          chi=chi_new
1852                      else:
1853                          if self.verbose: print "HomogeneousSaddlePointProblem: step %s: est. error = %e"%(correction_step, error_new)
1854    
1855                      error = error_new
1856                      correction_step+=1
1857                      if correction_step>max_correction_steps:
1858                            raise CorrectionFailed,"Given up after %d correction steps."%correction_step
1859                      v,p=v2,p+dp
1860             if self.verbose: print "HomogeneousSaddlePointProblem: tolerance reached after %s steps."%correction_step
1861       return v,p       return v,p
1862    
1863        #========================================================================        #========================================================================
# Line 1640  class HomogeneousSaddlePointProblem(obje Line 1865  class HomogeneousSaddlePointProblem(obje
1865           """           """
1866           Sets the relative tolerance for (v,p).           Sets the relative tolerance for (v,p).
1867    
1868           @param tolerance: tolerance to be used           :param tolerance: tolerance to be used
1869           @type tolerance: non-negative C{float}           :type tolerance: non-negative ``float``
1870           """           """
1871           if tolerance<0:           if tolerance<0:
1872               raise ValueError,"tolerance must be positive."               raise ValueError,"tolerance must be positive."
1873           self.__rtol=tolerance           self.__rtol=tolerance
          self.setSubProblemTolerance()  
1874    
1875        def getTolerance(self):        def getTolerance(self):
1876           """           """
1877           Returns the relative tolerance.           Returns the relative tolerance.
1878    
1879           @return: relative tolerance           :return: relative tolerance
1880           @rtype: C{float}           :rtype: ``float``
1881           """           """
1882           return self.__rtol           return self.__rtol
1883    
# Line 1661  class HomogeneousSaddlePointProblem(obje Line 1885  class HomogeneousSaddlePointProblem(obje
1885           """           """
1886           Sets the absolute tolerance.           Sets the absolute tolerance.
1887    
1888           @param tolerance: tolerance to be used           :param tolerance: tolerance to be used
1889           @type tolerance: non-negative C{float}           :type tolerance: non-negative ``float``
1890           """           """
1891           if tolerance<0:           if tolerance<0:
1892               raise ValueError,"tolerance must be non-negative."               raise ValueError,"tolerance must be non-negative."
# Line 1672  class HomogeneousSaddlePointProblem(obje Line 1896  class HomogeneousSaddlePointProblem(obje
1896           """           """
1897           Returns the absolute tolerance.           Returns the absolute tolerance.
1898    
1899           @return: absolute tolerance           :return: absolute tolerance
1900           @rtype: C{float}           :rtype: ``float``
1901           """           """
1902           return self.__atol           return self.__atol
1903    
       def setSubProblemTolerance(self,rtol=None):  
          """  
          Sets the relative tolerance to solve the subproblem(s).  
   
          @param rtol: relative tolerence  
          @type rtol: positive C{float}  
          """  
          if rtol == None:  
               rtol=max(200.*util.EPSILON,self.getTolerance()**2)  
          if rtol<=0:  
              raise ValueError,"tolerance must be positive."  
          self.__sub_tol=rtol  
   
       def getSubProblemTolerance(self):  
          """  
          Returns the subproblem reduction factor.  
   
          @return: subproblem reduction factor  
          @rtype: C{float}  
          """  
          return self.__sub_tol  
1904    
1905  def MaskFromBoundaryTag(domain,*tags):  def MaskFromBoundaryTag(domain,*tags):
1906     """     """
# Line 1706  def MaskFromBoundaryTag(domain,*tags): Line 1909  def MaskFromBoundaryTag(domain,*tags):
1909    
1910     Usage: m=MaskFromBoundaryTag(domain, "left", "right")     Usage: m=MaskFromBoundaryTag(domain, "left", "right")
1911    
1912     @param domain: domain to be used     :param domain: domain to be used
1913     @type domain: L{escript.Domain}     :type domain: `escript.Domain`
1914     @param tags: boundary tags     :param tags: boundary tags
1915     @type tags: C{str}     :type tags: ``str``
1916     @return: a mask which marks samples that are touching the boundary tagged     :return: a mask which marks samples that are touching the boundary tagged
1917              by any of the given tags              by any of the given tags
1918     @rtype: L{escript.Data} of rank 0     :rtype: `escript.Data` of rank 0
1919     """     """
1920     pde=linearPDEs.LinearPDE(domain,numEquations=1, numSolutions=1)     pde=linearPDEs.LinearPDE(domain,numEquations=1, numSolutions=1)
1921     d=escript.Scalar(0.,escript.FunctionOnBoundary(domain))     d=escript.Scalar(0.,escript.FunctionOnBoundary(domain))
# Line 1720  def MaskFromBoundaryTag(domain,*tags): Line 1923  def MaskFromBoundaryTag(domain,*tags):
1923     pde.setValue(y=d)     pde.setValue(y=d)
1924     return util.whereNonZero(pde.getRightHandSide())     return util.whereNonZero(pde.getRightHandSide())
1925    
1926  #==============================================================================  def MaskFromTag(domain,*tags):
 class SaddlePointProblem(object):  
1927     """     """
1928     This implements a solver for a saddle point problem     Creates a mask on the Solution(domain) function space where the value is
1929       one for samples that touch regions tagged by tags.
1930    
1931     M{f(u,p)=0}     Usage: m=MaskFromTag(domain, "ham")
1932     M{g(u)=0}  
1933       :param domain: domain to be used
1934       :type domain: `escript.Domain`
1935       :param tags: boundary tags
1936       :type tags: ``str``
1937       :return: a mask which marks samples that are touching the boundary tagged
1938                by any of the given tags
1939       :rtype: `escript.Data` of rank 0
1940       """
1941       pde=linearPDEs.LinearPDE(domain,numEquations=1, numSolutions=1)
1942       d=escript.Scalar(0.,escript.Function(domain))
1943       for t in tags: d.setTaggedValue(t,1.)
1944       pde.setValue(Y=d)
1945       return util.whereNonZero(pde.getRightHandSide())
1946    
    for u and p. The problem is solved with an inexact Uszawa scheme for p:  
   
    M{Q_f (u^{k+1}-u^{k}) = - f(u^{k},p^{k})}  
    M{Q_g (p^{k+1}-p^{k}) =   g(u^{k+1})}  
   
    where Q_f is an approximation of the Jacobian A_f of f with respect to u and  
    Q_f is an approximation of A_g A_f^{-1} A_g with A_g is the Jacobian of g  
    with respect to p. As a the construction of a 'proper' Q_g can be difficult,  
    non-linear conjugate gradient method is applied to solve for p, so Q_g plays  
    in fact the role of a preconditioner.  
    """  
    def __init__(self,verbose=False,*args):  
        """  
        Initializes the problem.  
   
        @param verbose: if True, some information is printed in the process  
        @type verbose: C{bool}  
        @note: this method may be overwritten by a particular saddle point  
               problem  
        """  
        print "SaddlePointProblem should not be used anymore!"  
        if not isinstance(verbose,bool):  
             raise TypeError("verbose needs to be of type bool.")  
        self.__verbose=verbose  
        self.relaxation=1.  
        DeprecationWarning("SaddlePointProblem should not be used anymore.")  
   
    def trace(self,text):  
        """  
        Prints C{text} if verbose has been set.  
   
        @param text: a text message  
        @type text: C{str}  
        """  
        if self.__verbose: print "%s: %s"%(str(self),text)  
   
    def solve_f(self,u,p,tol=1.e-8):  
        """  
        Solves  
   
        A_f du = f(u,p)  
   
        with tolerance C{tol} and returns du. A_f is Jacobian of f with respect  
        to u.  
   
        @param u: current approximation of u  
        @type u: L{escript.Data}  
        @param p: current approximation of p  
        @type p: L{escript.Data}  
        @param tol: tolerance expected for du  
        @type tol: C{float}  
        @return: increment du  
        @rtype: L{escript.Data}  
        @note: this method has to be overwritten by a particular saddle point  
               problem  
        """  
        pass  
   
    def solve_g(self,u,tol=1.e-8):  
        """  
        Solves  
   
        Q_g dp = g(u)  
   
        where Q_g is a preconditioner for A_g A_f^{-1} A_g with A_g is the  
        Jacobian of g with respect to p.  
   
        @param u: current approximation of u  
        @type u: L{escript.Data}  
        @param tol: tolerance expected for dp  
        @type tol: C{float}  
        @return: increment dp  
        @rtype: L{escript.Data}  
        @note: this method has to be overwritten by a particular saddle point  
               problem  
        """  
        pass  
   
    def inner(self,p0,p1):  
        """  
        Inner product of p0 and p1 approximating p. Typically this returns  
        C{integrate(p0*p1)}.  
        @return: inner product of p0 and p1  
        @rtype: C{float}  
        """  
        pass  
   
    subiter_max=3  
    def solve(self,u0,p0,tolerance=1.e-6,tolerance_u=None,iter_max=100,accepted_reduction=0.995,relaxation=None):  
         """  
         Runs the solver.  
   
         @param u0: initial guess for C{u}  
         @type u0: L{esys.escript.Data}  
         @param p0: initial guess for C{p}  
         @type p0: L{esys.escript.Data}  
         @param tolerance: tolerance for relative error in C{u} and C{p}  
         @type tolerance: positive C{float}  
         @param tolerance_u: tolerance for relative error in C{u} if different  
                             from C{tolerance}  
         @type tolerance_u: positive C{float}  
         @param iter_max: maximum number of iteration steps  
         @type iter_max: C{int}  
         @param accepted_reduction: if the norm g cannot be reduced by  
                                    C{accepted_reduction} backtracking to adapt  
                                    the relaxation factor. If  
                                    C{accepted_reduction=None} no backtracking  
                                    is used.  
         @type accepted_reduction: positive C{float} or C{None}  
         @param relaxation: initial relaxation factor. If C{relaxation==None},  
                            the last relaxation factor is used.  
         @type relaxation: C{float} or C{None}  
         """  
         tol=1.e-2  
         if tolerance_u==None: tolerance_u=tolerance  
         if not relaxation==None: self.relaxation=relaxation  
         if accepted_reduction ==None:  
               angle_limit=0.  
         elif accepted_reduction>=1.:  
               angle_limit=0.  
         else:  
               angle_limit=util.sqrt(1-accepted_reduction**2)  
         self.iter=0  
         u=u0  
         p=p0  
         #  
         #   initialize things:  
         #  
         converged=False  
         #  
         #  start loop:  
         #  
         #  initial search direction is g  
         #  
         while not converged :  
             if self.iter>iter_max:  
                 raise ArithmeticError("no convergence after %s steps."%self.iter)  
             f_new=self.solve_f(u,p,tol)  
             norm_f_new = util.Lsup(f_new)  
             u_new=u-f_new  
             g_new=self.solve_g(u_new,tol)  
             self.iter+=1  
             norm_g_new = util.sqrt(self.inner(g_new,g_new))  
             if norm_f_new==0. and norm_g_new==0.: return u, p  
             if self.iter>1 and not accepted_reduction==None:  
                #  
                #   did we manage to reduce the norm of G? I  
                #   if not we start a backtracking procedure  
                #  
                # print "new/old norm = ",norm_g_new, norm_g, norm_g_new/norm_g  
                if norm_g_new > accepted_reduction * norm_g:  
                   sub_iter=0  
                   s=self.relaxation  
                   d=g  
                   g_last=g  
                   self.trace("    start substepping: f = %s, g = %s, relaxation = %s."%(norm_f_new, norm_g_new, s))  
                   while sub_iter < self.subiter_max and  norm_g_new > accepted_reduction * norm_g:  
                      dg= g_new-g_last  
                      norm_dg=abs(util.sqrt(self.inner(dg,dg))/self.relaxation)  
                      rad=self.inner(g_new,dg)/self.relaxation  
                      # print "   ",sub_iter,": rad, norm_dg:",abs(rad), norm_dg*norm_g_new * angle_limit  
                      # print "   ",sub_iter,": rad, norm_dg:",rad, norm_dg, norm_g_new, norm_g  
                      if abs(rad) < norm_dg*norm_g_new * angle_limit:  
                          if sub_iter>0: self.trace("    no further improvements expected from backtracking.")  
                          break  
                      r=self.relaxation  
                      self.relaxation= - rad/norm_dg**2  
                      s+=self.relaxation  
                      #####  
                      # a=g_new+self.relaxation*dg/r  
                      # print "predicted new norm = ",util.sqrt(self.inner(a,a)),util.sqrt(self.inner(g_new,g_new)), self.relaxation  
                      #####  
                      g_last=g_new  
                      p+=self.relaxation*d  
                      f_new=self.solve_f(u,p,tol)  
                      u_new=u-f_new  
                      g_new=self.solve_g(u_new,tol)  
                      self.iter+=1  
                      norm_f_new = util.Lsup(f_new)  
                      norm_g_new = util.sqrt(self.inner(g_new,g_new))  
                      # print "   ",sub_iter," new g norm",norm_g_new  
                      self.trace("    %s th sub-step: f = %s, g = %s, relaxation = %s."%(sub_iter, norm_f_new, norm_g_new, s))  
                      #  
                      #   can we expect reduction of g?  
                      #  
                      # u_last=u_new  
                      sub_iter+=1  
                   self.relaxation=s  
             #  
             #  check for convergence:  
             #  
             norm_u_new = util.Lsup(u_new)  
             p_new=p+self.relaxation*g_new  
             norm_p_new = util.sqrt(self.inner(p_new,p_new))  
             self.trace("%s th step: f = %s, f/u = %s, g = %s, g/p = %s, relaxation = %s."%(self.iter, norm_f_new ,norm_f_new/norm_u_new, norm_g_new, norm_g_new/norm_p_new, self.relaxation))  
   
             if self.iter>1:  
                dg2=g_new-g  
                df2=f_new-f  
                norm_dg2=util.sqrt(self.inner(dg2,dg2))  
                norm_df2=util.Lsup(df2)  
                # print norm_g_new, norm_g, norm_dg, norm_p, tolerance  
                tol_eq_g=tolerance*norm_dg2/(norm_g*abs(self.relaxation))*norm_p_new  
                tol_eq_f=tolerance_u*norm_df2/norm_f*norm_u_new  
                if norm_g_new <= tol_eq_g and norm_f_new <= tol_eq_f:  
                    converged=True  
             f, norm_f, u, norm_u, g, norm_g, p, norm_p = f_new, norm_f_new, u_new, norm_u_new, g_new, norm_g_new, p_new, norm_p_new  
         self.trace("convergence after %s steps."%self.iter)  
         return u,p  
1947    

Legend:
Removed from v.2415  
changed lines
  Added in v.2793

  ViewVC Help
Powered by ViewVC 1.1.26