/[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 1312 by ksteube, Mon Sep 24 06:18:44 2007 UTC revision 1467 by gross, Wed Apr 2 08:10:37 2008 UTC
# Line 48  import numarray Line 48  import numarray
48  import util  import util
49  import math  import math
50    
51    ##### Added by Artak
52    # from Numeric import zeros,Int,Float64
53    ###################################
54    
55    
56  class TimeIntegrationManager:  class TimeIntegrationManager:
57    """    """
58    a simple mechanism to manage time dependend values.    a simple mechanism to manage time dependend values.
# Line 435  class NegativeNorm(SolverSchemeException Line 440  class NegativeNorm(SolverSchemeException
440     """     """
441     pass     pass
442    
443  def PCG(b,x,Aprod,Msolve,bilinearform, norm, verbose=True, iter_max=100, tolerance=math.sqrt(util.EPSILON)):  class IterationHistory(object):
444     """     """
445     Solver for     The IterationHistory class is used to define a stopping criterium. It keeps track of the
446       residual norms. The stoppingcriterium indicates termination if the residual norm has been reduced by
447       a given tolerance.
448       """
449       def __init__(self,tolerance=math.sqrt(util.EPSILON),verbose=False):
450          """
451          Initialization
452    
453     M{Ax=b}        @param tolerance: tolerance
454          @type tolerance: positive C{float}
455          @param verbose: switches on the printing out some information
456          @type verbose: C{bool}
457          """
458          if not tolerance>0.:
459              raise ValueError,"tolerance needs to be positive."
460          self.tolerance=tolerance
461          self.verbose=verbose
462          self.history=[]
463       def stoppingcriterium(self,norm_r,r,x):
464           """
465           returns True if the C{norm_r} is C{tolerance}*C{norm_r[0]} where C{norm_r[0]}  is the residual norm at the first call.
466    
467     with a symmetric and positive definite operator A (more details required!).        
468     It uses the conjugate gradient method with preconditioner M providing an approximation of A.         @param norm_r: current residual norm
469           @type norm_r: non-negative C{float}
470           @param r: current residual (not used)
471           @param x: current solution approximation (not used)
472           @return: C{True} is the stopping criterium is fullfilled. Otherwise C{False} is returned.
473           @rtype: C{bool}
474    
475     The iteration is terminated if         """
476           self.history.append(norm_r)
477           if self.verbose: print "iter: %s:  inner(rhat,r) = %e"%(len(self.history)-1, self.history[-1])
478           return self.history[-1]<=self.tolerance * self.history[0]
479    
480     M{norm(r) <= tolerance * norm(b)}  def PCG(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100):
481       """
482       Solver for
483    
484     where C{norm()} defines a norm and     M{Ax=b}
485    
486     M{r = b-Ax}     with a symmetric and positive definite operator A (more details required!).
487       It uses the conjugate gradient method with preconditioner M providing an approximation of A.
488    
489     the residual.     The iteration is terminated if the C{stoppingcriterium} function return C{True}.
490    
491     For details on the preconditioned conjugate gradient method see the book:     For details on the preconditioned conjugate gradient method see the book:
492    
# Line 461  def PCG(b,x,Aprod,Msolve,bilinearform, n Line 495  def PCG(b,x,Aprod,Msolve,bilinearform, n
495     C. Romine, and H. van der Vorst.     C. Romine, and H. van der Vorst.
496    
497     @param b: the right hand side of the liner system. C{b} is altered.     @param b: the right hand side of the liner system. C{b} is altered.
498     @type b: any object type R supporting inplace add (x+=y) and scaling (x=scalar*y)     @type b: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
    @param x: an initial guess for the solution  
    @type x: any object type S supporting inplace add (x+=y), scaling (x=scalar*y)  
499     @param Aprod: returns the value Ax     @param Aprod: returns the value Ax
500     @type Aprod: function C{Aprod(x)} where C{x} is of object type S. The returned object needs to be of type R.     @type Aprod: function C{Aprod(x)} where C{x} is of the same object like argument C{x}. The returned object needs to be of the same type like argument C{b}.
501     @param Msolve: solves Mx=r     @param Msolve: solves Mx=r
502     @type Msolve: function C{Msolve(r)} where C{r} is of object type R. The returned object needs to be of tupe S.     @type Msolve: function C{Msolve(r)} where C{r} is of the same type like argument C{b}. The returned object needs to be of the same
503    type like argument C{x}.
504     @param bilinearform: inner product C{<x,r>}     @param bilinearform: inner product C{<x,r>}
505     @type bilinearform: function C{bilinearform(x,r)} where C{x} is of object type S and C{r} is of object type R. The returned value is a C{float}.     @type bilinearform: function C{bilinearform(x,r)} where C{x} is of the same type like argument C{x} and C{r} is . The returned value is a C{float}.
506     @param norm: norm calculation for the residual C{r=b-Ax}.     @param stoppingcriterium: function which returns True if a stopping criterium is meet. C{stoppingcriterium} has the arguments C{norm_r}, C{r} and C{x} giving the current norm of the residual (=C{sqrt(bilinearform(Msolve(r),r)}), the current residual and the current solution approximation. C{stoppingcriterium} is called in each iteration step.
507     @type norm: function C{norm(r)} where C{r} is of object type R. The returned value is a C{float}.     @type stoppingcriterium: function that returns C{True} or C{False}
508     @param verbose: switches on the printing out some information     @param x: an initial guess for the solution. If no C{x} is given 0*b is used.
509     @type verbose: C{bool}     @type x: any object supporting inplace add (x+=y) and scaling (x=scalar*y)
510     @param iter_max: maximum number of iteration steps.     @param iter_max: maximum number of iteration steps.
511     @type iter_max: C{int}     @type iter_max: C{int}
512     @param tolerance: tolerance     @return: the solution approximation and the corresponding residual
513     @type tolerance: positive C{float}     @rtype: C{tuple}
514     @return: the solution apprximation and the corresponding residual     @warning: C{b} and C{x} are altered.
    @rtype: C{tuple} of an S type and and an R type object.A  
    @warning: C{b} ans C{x} are altered.  
515     """     """
    if verbose:  
         print "Enter PCG for solving Ax=b\n\t iter_max =%s\t tolerance   =%e"%(iter_max,tolerance)  
516     iter=0     iter=0
517     normb = norm(b)     if x==None:
518     if normb<0: raise NegativeNorm        x=0*b
519       else:
520     b += (-1)*Aprod(x)        b += (-1)*Aprod(x)
521     r=b     r=b
522     rhat=Msolve(r)     rhat=Msolve(r)
523     d = rhat;     d = rhat
524     rhat_dot_r = bilinearform(rhat, r)     rhat_dot_r = bilinearform(rhat, r)
525       if rhat_dot_r<0: raise NegativeNorm,"negative norm."
526    
527     while True:     while not stoppingcriterium(math.sqrt(rhat_dot_r),r,x):
528         normr=norm(r)         iter+=1
        if normr<0: raise NegativeNorm  
        if verbose: print "iter: %s: norm(r) = %e, tolerance*norm(b) = %e"%(iter, normr,tolerance * normb)  
        if normr <= tolerance * normb: return x,r  
   
        iter+=1 # k = iter = 1 first time through  
529         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
530    
531         q=Aprod(d)         q=Aprod(d)
# Line 515  def PCG(b,x,Aprod,Msolve,bilinearform, n Line 540  def PCG(b,x,Aprod,Msolve,bilinearform, n
540         d=rhat         d=rhat
541    
542         rhat_dot_r = rhat_dot_r_new         rhat_dot_r = rhat_dot_r_new
543           if rhat_dot_r<0: raise NegativeNorm,"negative norm."
544    
545       return x,r
546    
547    
548    ############################
549    # Added by Artak
550    #################################3
551    
552    #Apply a sequence of k Givens rotations, used within gmres codes
553    # vrot=givapp(c, s, vin, k)
554    def givapp(c,s,vin):
555        vrot=vin # warning: vin is altered!!!!
556        if isinstance(c,float):
557            vrot=[c*vrot[0]-s*vrot[1],s*vrot[0]+c*vrot[1]]
558        else:
559            for i in range(len(c)):
560                w1=c[i]*vrot[i]-s[i]*vrot[i+1]
561            w2=s[i]*vrot[i]+c[i]*vrot[i+1]
562                vrot[i:i+2]=w1,w2
563        return vrot
564    
565    def GMRES(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100):
566       iter=0
567       r=Msolve(b)
568       r_dot_r = bilinearform(r, r)
569       if r_dot_r<0: raise NegativeNorm,"negative norm."
570       norm_b=math.sqrt(r_dot_r)
571    
572       if x==None:
573          x=0*b
574       else:
575          r=Msolve(b-Aprod(x))
576          r_dot_r = bilinearform(r, r)
577          if r_dot_r<0: raise NegativeNorm,"negative norm."
578      
579       h=numarray.zeros((iter_max,iter_max),numarray.Float64)
580       c=numarray.zeros(iter_max,numarray.Float64)
581       s=numarray.zeros(iter_max,numarray.Float64)
582       g=numarray.zeros(iter_max,numarray.Float64)
583       v=[]
584    
585       rho=math.sqrt(r_dot_r)
586       v.append(r/rho)
587       g[0]=rho
588    
589       while not stoppingcriterium(rho,norm_b):
590    
591        if iter  >= iter_max: raise MaxIterReached,"maximum number of %s steps reached."%iter_max
592    
593        
594        p=Msolve(Aprod(v[iter]))
595    
596        v.append(p)
597    
598        v_norm1=math.sqrt(bilinearform(v[iter+1], v[iter+1]))  
599    
600    # Modified Gram-Schmidt
601        for j in range(iter+1):
602          h[j][iter]=bilinearform(v[j],v[iter+1])  
603          v[iter+1]+=(-1.)*h[j][iter]*v[j]
604          
605        h[iter+1][iter]=math.sqrt(bilinearform(v[iter+1],v[iter+1]))
606        v_norm2=h[iter+1][iter]
607    
608    
609    # Reorthogonalize if needed
610        if v_norm1 + 0.001*v_norm2 == v_norm1:   #Brown/Hindmarsh condition (default)
611         for j in range(iter+1):
612            hr=bilinearform(v[j],v[iter+1])
613                h[j][iter]=h[j][iter]+hr #vhat
614                v[iter+1] +=(-1.)*hr*v[j]
615    
616         v_norm2=math.sqrt(bilinearform(v[iter+1], v[iter+1]))  
617         h[iter+1][iter]=v_norm2
618    
619    #   watch out for happy breakdown
620            if v_norm2 != 0:
621             v[iter+1]=v[iter+1]/h[iter+1][iter]
622    
623    #   Form and store the information for the new Givens rotation
624        if iter > 0 :
625            hhat=[]
626            for i in range(iter+1) : hhat.append(h[i][iter])
627            hhat=givapp(c[0:iter],s[0:iter],hhat);
628                for i in range(iter+1) : h[i][iter]=hhat[i]
629    
630        mu=math.sqrt(h[iter][iter]*h[iter][iter]+h[iter+1][iter]*h[iter+1][iter])
631        if mu!=0 :
632            c[iter]=h[iter][iter]/mu
633            s[iter]=-h[iter+1][iter]/mu
634            h[iter][iter]=c[iter]*h[iter][iter]-s[iter]*h[iter+1][iter]
635            h[iter+1][iter]=0.0
636            g[iter:iter+2]=givapp(c[iter],s[iter],g[iter:iter+2])
637    
638    # Update the residual norm
639            rho=abs(g[iter+1])
640        iter+=1
641    
642    # At this point either iter > iter_max or rho < tol.
643    # It's time to compute x and leave.        
644    
645       if iter > 0 :
646         y=numarray.zeros(iter,numarray.Float64)    
647         y[iter-1] = g[iter-1] / h[iter-1][iter-1]
648         if iter > 1 :  
649            i=iter-2  
650            while i>=0 :
651              y[i] = ( g[i] - numarray.dot(h[i][i+1:iter], y[i+1:iter])) / h[i][i]
652              i=i-1
653         xhat=v[iter-1]*y[iter-1]
654         for i in range(iter-1):
655        xhat += v[i]*y[i]
656       else : xhat=v[0]
657        
658       x += xhat
659    
660       return x
661        
662    #############################################
663    
664    class ArithmeticTuple(object):
665       """
666       tuple supporting inplace update x+=y and scaling x=a*y where x,y is an ArithmeticTuple and a is a float.
667    
668       example of usage:
669    
670       from esys.escript import Data
671       from numarray import array
672       a=Data(...)
673       b=array([1.,4.])
674       x=ArithmeticTuple(a,b)
675       y=5.*x
676    
677       """
678       def __init__(self,*args):
679           """
680           initialize object with elements args.
681    
682           @param args: tuple of object that support implace add (x+=y) and scaling (x=a*y)
683           """
684           self.__items=list(args)
685    
686       def __len__(self):
687           """
688           number of items
689    
690           @return: number of items
691           @rtype: C{int}
692           """
693           return len(self.__items)
694    
695       def __getitem__(self,index):
696           """
697           get an item
698    
699           @param index: item to be returned
700           @type index: C{int}
701           @return: item with index C{index}
702           """
703           return self.__items.__getitem__(index)
704    
705       def __mul__(self,other):
706           """
707           scaling from the right
708    
709           @param other: scaling factor
710           @type other: C{float}
711           @return: itemwise self*other
712           @rtype: L{ArithmeticTuple}
713           """
714           out=[]
715           for i in range(len(self)):
716               out.append(self[i]*other)
717           return ArithmeticTuple(*tuple(out))
718    
719       def __rmul__(self,other):
720           """
721           scaling from the left
722    
723           @param other: scaling factor
724           @type other: C{float}
725           @return: itemwise other*self
726           @rtype: L{ArithmeticTuple}
727           """
728           out=[]
729           for i in range(len(self)):
730               out.append(other*self[i])
731           return ArithmeticTuple(*tuple(out))
732    
733    #########################
734    # Added by Artak
735    #########################
736       def __div__(self,other):
737           """
738           dividing from the right
739    
740           @param other: scaling factor
741           @type other: C{float}
742           @return: itemwise self/other
743           @rtype: L{ArithmeticTuple}
744           """
745           out=[]
746           for i in range(len(self)):
747               out.append(self[i]/other)
748           return ArithmeticTuple(*tuple(out))
749    
750       def __rdiv__(self,other):
751           """
752           dividing from the left
753    
754           @param other: scaling factor
755           @type other: C{float}
756           @return: itemwise other/self
757           @rtype: L{ArithmeticTuple}
758           """
759           out=[]
760           for i in range(len(self)):
761               out.append(other/self[i])
762           return ArithmeticTuple(*tuple(out))
763      
764    ##########################################33
765    
766       def __iadd__(self,other):
767           """
768           in-place add of other to self
769    
770           @param other: increment
771           @type other: C{ArithmeticTuple}
772           """
773           if len(self) != len(other):
774               raise ValueError,"tuple length must match."
775           for i in range(len(self)):
776               self.__items[i]+=other[i]
777           return self
778    
779    class HomogeneousSaddlePointProblem(object):
780          """
781          This provides a framwork for solving homogeneous saddle point problem of the form
782    
783                 Av+B^*p=f
784                 Bv    =0
785    
786          for the unknowns v and p and given operators A and B and given right hand side f.
787          B^* is the adjoint operator of B is the given inner product.
788    
789          """
790          def __init__(self,**kwargs):
791            self.setTolerance()
792            self.setToleranceReductionFactor()
793    
794          def initialize(self):
795            """
796            initialize the problem (overwrite)
797            """
798            pass
799          def B(self,v):
800             """
801             returns Bv (overwrite)
802             @rtype: equal to the type of p
803    
804             @note: boundary conditions on p should be zero!
805             """
806             pass
807    
808          def inner(self,p0,p1):
809             """
810             returns inner product of two element p0 and p1  (overwrite)
811            
812             @type p0: equal to the type of p
813             @type p1: equal to the type of p
814             @rtype: C{float}
815    
816             @rtype: equal to the type of p
817             """
818             pass
819    
820          def solve_A(self,u,p):
821             """
822             solves Av=f-Au-B^*p with accuracy self.getReducedTolerance() (overwrite)
823    
824             @rtype: equal to the type of v
825             @note: boundary conditions on v should be zero!
826             """
827             pass
828    
829          def solve_prec(self,p):
830             """
831             provides a preconditioner for BA^{-1}B^* with accuracy self.getReducedTolerance() (overwrite)
832    
833             @rtype: equal to the type of p
834             """
835             pass
836    
837          def stoppingcriterium(self,Bv,v,p):
838             """
839             returns a True if iteration is terminated. (overwrite)
840    
841             @rtype: C{bool}
842             """
843             pass
844                
845          def __inner(self,p,r):
846             return self.inner(p,r[1])
847    
848          def __inner_p(self,p1,p2):
849             return self.inner(p1,p2)
850    
851          def __stoppingcriterium(self,norm_r,r,p):
852              return self.stoppingcriterium(r[1],r[0],p)
853    
854          def __stoppingcriterium_GMRES(self,norm_r,norm_b):
855              return self.stoppingcriterium_GMRES(norm_r,norm_b)
856    
857          def setTolerance(self,tolerance=1.e-8):
858                  self.__tol=tolerance
859          def getTolerance(self):
860                  return self.__tol
861          def setToleranceReductionFactor(self,reduction=0.01):
862                  self.__reduction=reduction
863          def getSubProblemTolerance(self):
864                  return self.__reduction*self.getTolerance()
865    
866          def solve(self,v,p,max_iter=20, verbose=False, show_details=False, solver='GMRES'):
867                  """
868                  solves the saddle point problem using initial guesses v and p.
869    
870                  @param max_iter: maximum number of iteration steps.
871                  """
872                  self.verbose=verbose
873                  self.show_details=show_details and self.verbose
874    
875              # Az=f is solved as A(z-v)=f-Av (z-v = 0 on fixed_u_mask)      
876    
877              
878              self.__z=v+self.solve_A(v,p*0)
879    
880                  Bz=self.B(self.__z)
881                  #
882              #   solve BA^-1B^*p = Bz
883                  #
884                  #   note that the residual r=Bz-BA^-1B^*p = B(z-A^-1B^*p) = Bv
885                  #
886                  #   with                    Av=Az-B^*p = f - B^*p (v=z on fixed_u_mask)
887                  #                           A(v-z)=Az-B^*p-Az = f -Az - B^*p (v-z=0 on fixed_u_mask)
888                  #
889                  self.iter=0
890              if solver=='GMRES':      
891                    if self.verbose: print "enter GMRES method (iter_max=%s)"%max_iter
892                    p=GMRES(Bz,self.__Aprod_GMRES,self.__Msolve_GMRES,self.__inner_p,self.__stoppingcriterium_GMRES,iter_max=max_iter, x=p*1.)
893                    # solve Au=f-B^*p
894                    #       A(u-v)=f-B^*p-Av
895                    #       u=v+(u-v)
896            u=v+self.solve_A(v,p)
897        
898                  else:
899                    if self.verbose: print "enter PCG method (iter_max=%s)"%max_iter
900                    p,r=PCG(ArithmeticTuple(self.__z*1.,Bz),self.__Aprod,self.__Msolve,self.__inner,self.__stoppingcriterium,iter_max=max_iter, x=p)
901                u=r[0]  
902    
903              return u,p
904    
905          def __Msolve(self,r):
906              return self.solve_prec(r[1])
907    
908          def __Msolve_GMRES(self,r):
909              return self.solve_prec(r)
910    
911    
912          def __Aprod(self,p):
913              # return BA^-1B*p
914              #solve Av =-B^*p as Av =f-Az-B^*p
915              v=self.solve_A(self.__z,p)
916              return ArithmeticTuple(v, -self.B(v))
917    
918          def __Aprod_GMRES(self,p):
919              # return BA^-1B*p
920              #solve Av =-B^*p as Av =f-Az-B^*p
921          v=self.solve_A(self.__z,p)
922              return -self.B(v)
923    
924  class SaddlePointProblem(object):  class SaddlePointProblem(object):
925     """     """
# Line 771  def MaskFromBoundaryTag(function_space,* Line 1175  def MaskFromBoundaryTag(function_space,*
1175     else:     else:
1176        return util.whereNonZero(util.interpolate(out,function_space))        return util.whereNonZero(util.interpolate(out,function_space))
1177    
1178    
1179    

Legend:
Removed from v.1312  
changed lines
  Added in v.1467

  ViewVC Help
Powered by ViewVC 1.1.26