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

revision 1331 by gross, Tue Oct 23 00:42:15 2007 UTC revision 1486 by artak, Thu Apr 10 00:10:50 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 472  class IterationHistory(object): Line 477  class IterationHistory(object):
477         if self.verbose: print "iter: %s:  inner(rhat,r) = %e"%(len(self.history)-1, self.history[-1])         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]         return self.history[-1]<=self.tolerance * self.history[0]
479
480       def stoppingcriterium2(self,norm_r,norm_b):
481           """
482           returns True if the C{norm_r} is C{tolerance}*C{norm_b}
483
484
485           @param norm_r: current residual norm
486           @type norm_r: non-negative C{float}
487           @param norm_b: norm of right hand side
488           @type norm_b: non-negative C{float}
489           @return: C{True} is the stopping criterium is fullfilled. Otherwise C{False} is returned.
490           @rtype: C{bool}
491
492           """
493           self.history.append(norm_r)
494           if self.verbose: print "iter: %s:  norm(r) = %e"%(len(self.history)-1, self.history[-1])
495           return self.history[-1]<=self.tolerance * norm_b
496
497  def PCG(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100):  def PCG(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100):
498     """     """
499     Solver for     Solver for
# Line 539  type like argument C{x}. Line 561  type like argument C{x}.
561
562     return x,r     return x,r
563
564
565    ############################
566    # Added by Artak
567    #################################3
568
569    #Apply a sequence of k Givens rotations, used within gmres codes
570    # vrot=givapp(c, s, vin, k)
571    def givapp(c,s,vin):
572        vrot=vin # warning: vin is altered!!!!
573        if isinstance(c,float):
574            vrot=[c*vrot[0]-s*vrot[1],s*vrot[0]+c*vrot[1]]
575        else:
576            for i in range(len(c)):
577                w1=c[i]*vrot[i]-s[i]*vrot[i+1]
578            w2=s[i]*vrot[i]+c[i]*vrot[i+1]
579                vrot[i:i+2]=w1,w2
580        return vrot
581
582    def GMRES(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100, iter_restart=10):
583       m=iter_restart
584       iter=0
585       while True:
586          if iter  >= iter_max: raise MaxIterReached,"maximum number of %s steps reached."%iter_max
587          x,stopped=GMRESm(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=x, iter_max=iter_max-iter, iter_restart=m)
588          iter+=iter_restart
589          if stopped: break
590       return x
591
592    def GMRESm(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100, iter_restart=10):
593       iter=0
594       r=Msolve(b)
595       r_dot_r = bilinearform(r, r)
596       if r_dot_r<0: raise NegativeNorm,"negative norm."
597       norm_b=math.sqrt(r_dot_r)
598
599       if x==None:
600          x=0*b
601       else:
602          r=Msolve(b-Aprod(x))
603          r_dot_r = bilinearform(r, r)
604          if r_dot_r<0: raise NegativeNorm,"negative norm."
605
606       h=numarray.zeros((iter_restart,iter_restart),numarray.Float64)
607       c=numarray.zeros(iter_restart,numarray.Float64)
608       s=numarray.zeros(iter_restart,numarray.Float64)
609       g=numarray.zeros(iter_restart,numarray.Float64)
610       v=[]
611
612       rho=math.sqrt(r_dot_r)
613       v.append(r/rho)
614       g[0]=rho
615
616       while not (stoppingcriterium(rho,norm_b) or iter==iter_restart-1):
617
618        if iter  >= iter_max: raise MaxIterReached,"maximum number of %s steps reached."%iter_max
619
620
621        p=Msolve(Aprod(v[iter]))
622
623        v.append(p)
624
625        v_norm1=math.sqrt(bilinearform(v[iter+1], v[iter+1]))
626
627    # Modified Gram-Schmidt
628        for j in range(iter+1):
629          h[j][iter]=bilinearform(v[j],v[iter+1])
630          v[iter+1]+=(-1.)*h[j][iter]*v[j]
631
632        h[iter+1][iter]=math.sqrt(bilinearform(v[iter+1],v[iter+1]))
633        v_norm2=h[iter+1][iter]
634
635
636    # Reorthogonalize if needed
637        if v_norm1 + 0.001*v_norm2 == v_norm1:   #Brown/Hindmarsh condition (default)
638         for j in range(iter+1):
639            hr=bilinearform(v[j],v[iter+1])
640                h[j][iter]=h[j][iter]+hr #vhat
641                v[iter+1] +=(-1.)*hr*v[j]
642
643         v_norm2=math.sqrt(bilinearform(v[iter+1], v[iter+1]))
644         h[iter+1][iter]=v_norm2
645
646    #   watch out for happy breakdown
647            if v_norm2 != 0:
648             v[iter+1]=v[iter+1]/h[iter+1][iter]
649
650    #   Form and store the information for the new Givens rotation
651        if iter > 0 :
652            hhat=[]
653            for i in range(iter+1) : hhat.append(h[i][iter])
654            hhat=givapp(c[0:iter],s[0:iter],hhat);
655                for i in range(iter+1) : h[i][iter]=hhat[i]
656
657        mu=math.sqrt(h[iter][iter]*h[iter][iter]+h[iter+1][iter]*h[iter+1][iter])
658        if mu!=0 :
659            c[iter]=h[iter][iter]/mu
660            s[iter]=-h[iter+1][iter]/mu
661            h[iter][iter]=c[iter]*h[iter][iter]-s[iter]*h[iter+1][iter]
662            h[iter+1][iter]=0.0
663            g[iter:iter+2]=givapp(c[iter],s[iter],g[iter:iter+2])
664
665    # Update the residual norm
666            rho=abs(g[iter+1])
667        iter+=1
668
669    # At this point either iter > iter_max or rho < tol.
670    # It's time to compute x and leave.
671
672       if iter > 0 :
673         y=numarray.zeros(iter,numarray.Float64)
674         y[iter-1] = g[iter-1] / h[iter-1][iter-1]
675         if iter > 1 :
676            i=iter-2
677            while i>=0 :
678              y[i] = ( g[i] - numarray.dot(h[i][i+1:iter], y[i+1:iter])) / h[i][i]
679              i=i-1
680         xhat=v[iter-1]*y[iter-1]
681         for i in range(iter-1):
682        xhat += v[i]*y[i]
683       else : xhat=v[0]
684
685       x += xhat
686       if iter!=iter_restart-1:
687          stopped=True
688       else:
689          stopped=False
690
691       return x,stopped
692
693    def MINRES(b, Aprod, Msolve, bilinearform, stoppingcriterium, x=None, iter_max=100):
694
695        #
696        #  minres solves the system of linear equations Ax = b
697        #  where A is a symmetric matrix (possibly indefinite or singular)
698        #  and b is a given vector.
699        #
700        #  "A" may be a dense or sparse matrix (preferably sparse!)
701        #  or the name of a function such that
702        #               y = A(x)
703        #  returns the product y = Ax for any given vector x.
704        #
705        #  "M" defines a positive-definite preconditioner M = C C'.
706        #  "M" may be a dense or sparse matrix (preferably sparse!)
707        #  or the name of a function such that
708        #  solves the system My = x for any given vector x.
709        #
710        #
711
712        #------------------------------------------------------------------
713        # Set up y and v for the first Lanczos vector v1.
714        # y  =  beta1 P' v1,  where  P = C**(-1).
715        # v is really P' v1.
716        #------------------------------------------------------------------
717        if x==None:
718          x=0*b
719        else:
720          b += (-1)*Aprod(x)
721
722        r1    = b
723        y = Msolve(b)
724        beta1 = bilinearform(b,y)
725
726        if beta1< 0: raise NegativeNorm,"negative norm."
727
728        #  If b = 0 exactly, stop with x = 0.
729        if beta1==0: return x*0.
730
731        if beta1> 0:
732          beta1  = math.sqrt(beta1)
733
734        #------------------------------------------------------------------
735        # Initialize quantities.
736        # ------------------------------------------------------------------
737        iter   = 0
738        Anorm = 0
739        ynorm = 0
740        oldb   = 0
741        beta   = beta1
742        dbar   = 0
743        epsln  = 0
744        phibar = beta1
745        rhs1   = beta1
746        rhs2   = 0
747        rnorm  = phibar
748        tnorm2 = 0
749        ynorm2 = 0
750        cs     = -1
751        sn     = 0
752        w      = b*0.
753        w2     = b*0.
754        r2     = r1
755        eps    = 0.0001
756
757        #---------------------------------------------------------------------
758        # Main iteration loop.
759        # --------------------------------------------------------------------
760        while not stoppingcriterium(rnorm,Anorm*ynorm):    #  checks ||r|| < (||A|| ||x||) * TOL
761
762        if iter  >= iter_max: raise MaxIterReached,"maximum number of %s steps reached."%iter_max
763            iter    = iter  +  1
764
765            #-----------------------------------------------------------------
766            # Obtain quantities for the next Lanczos vector vk+1, k = 1, 2,...
767            # The general iteration is similar to the case k = 1 with v0 = 0:
768            #
769            #   p1      = Operator * v1  -  beta1 * v0,
770            #   alpha1  = v1'p1,
771            #   q2      = p2  -  alpha1 * v1,
772            #   beta2^2 = q2'q2,
773            #   v2      = (1/beta2) q2.
774            #
775            # Again, y = betak P vk,  where  P = C**(-1).
776            #-----------------------------------------------------------------
777            s = 1/beta                 # Normalize previous vector (in y).
778            v = s*y                    # v = vk if P = I
779
780            y      = Aprod(v)
781
782            if iter >= 2:
783              y = y - (beta/oldb)*r1
784
785            alfa   = bilinearform(v,y)              # alphak
786            y      = (- alfa/beta)*r2 + y
787            r1     = r2
788            r2     = y
789            y = Msolve(r2)
790            oldb   = beta                           # oldb = betak
791            beta   = bilinearform(r2,y)             # beta = betak+1^2
792            if beta < 0: raise NegativeNorm,"negative norm."
793
794            beta   = math.sqrt( beta )
795            tnorm2 = tnorm2 + alfa*alfa + oldb*oldb + beta*beta
796
797            if iter==1:                 # Initialize a few things.
798              gmax   = abs( alfa )      # alpha1
799              gmin   = gmax             # alpha1
800
801            # Apply previous rotation Qk-1 to get
802            #   [deltak epslnk+1] = [cs  sn][dbark    0   ]
803            #   [gbar k dbar k+1]   [sn -cs][alfak betak+1].
804
805            oldeps = epsln
806            delta  = cs * dbar  +  sn * alfa  # delta1 = 0         deltak
807            gbar   = sn * dbar  -  cs * alfa  # gbar 1 = alfa1     gbar k
808            epsln  =               sn * beta  # epsln2 = 0         epslnk+1
809            dbar   =            -  cs * beta  # dbar 2 = beta2     dbar k+1
810
811            # Compute the next plane rotation Qk
812
813            gamma  = math.sqrt(gbar*gbar+beta*beta)  # gammak
814            gamma  = max(gamma,eps)
815            cs     = gbar / gamma             # ck
816            sn     = beta / gamma             # sk
817            phi    = cs * phibar              # phik
818            phibar = sn * phibar              # phibark+1
819
820            # Update  x.
821
822            denom = 1/gamma
823            w1    = w2
824            w2    = w
825            w     = (v - oldeps*w1 - delta*w2) * denom
826            x     = x  +  phi*w
827
828            # Go round again.
829
830            gmax   = max(gmax,gamma)
831            gmin   = min(gmin,gamma)
832            z      = rhs1 / gamma
833            ynorm2 = z*z  +  ynorm2
834            rhs1   = rhs2 -  delta*z
835            rhs2   =      -  epsln*z
836
837            # Estimate various norms and test for convergence.
838
839            Anorm  = math.sqrt( tnorm2 )
840            ynorm  = math.sqrt( ynorm2 )
841
842            rnorm  = phibar
843
844        return x
845
846    #############################################
847
848  class ArithmeticTuple(object):  class ArithmeticTuple(object):
849     """     """
850     tuple supporting inplace update x+=y and scaling x=a*y where x,y is an ArithmeticTuple and a is a float.     tuple supporting inplace update x+=y and scaling x=a*y where x,y is an ArithmeticTuple and a is a float.
# Line 608  class ArithmeticTuple(object): Line 914  class ArithmeticTuple(object):
914             out.append(other*self[i])             out.append(other*self[i])
915         return ArithmeticTuple(*tuple(out))         return ArithmeticTuple(*tuple(out))
916
917    #########################
918    # Added by Artak
919    #########################
920       def __div__(self,other):
921           """
922           dividing from the right
923
924           @param other: scaling factor
925           @type other: C{float}
926           @return: itemwise self/other
927           @rtype: L{ArithmeticTuple}
928           """
929           out=[]
930           for i in range(len(self)):
931               out.append(self[i]/other)
932           return ArithmeticTuple(*tuple(out))
933
934       def __rdiv__(self,other):
935           """
936           dividing from the left
937
938           @param other: scaling factor
939           @type other: C{float}
940           @return: itemwise other/self
941           @rtype: L{ArithmeticTuple}
942           """
943           out=[]
944           for i in range(len(self)):
945               out.append(other/self[i])
946           return ArithmeticTuple(*tuple(out))
947
948    ##########################################33
949
951         """         """
952         in-place add of other to self         in-place add of other to self
# Line 621  class ArithmeticTuple(object): Line 960  class ArithmeticTuple(object):
960             self.__items[i]+=other[i]             self.__items[i]+=other[i]
961         return self         return self
962
964          """
965          This provides a framwork for solving homogeneous saddle point problem of the form
966
967                 Av+B^*p=f
968                 Bv    =0
969
970          for the unknowns v and p and given operators A and B and given right hand side f.
971          B^* is the adjoint operator of B is the given inner product.
972
973          """
974          def __init__(self,**kwargs):
975            self.setTolerance()
976            self.setToleranceReductionFactor()
977
978          def initialize(self):
979            """
980            initialize the problem (overwrite)
981            """
982            pass
983          def B(self,v):
984             """
985             returns Bv (overwrite)
986             @rtype: equal to the type of p
987
988             @note: boundary conditions on p should be zero!
989             """
990             pass
991
992          def inner(self,p0,p1):
993             """
994             returns inner product of two element p0 and p1  (overwrite)
995
996             @type p0: equal to the type of p
997             @type p1: equal to the type of p
998             @rtype: C{float}
999
1000             @rtype: equal to the type of p
1001             """
1002             pass
1003
1004          def solve_A(self,u,p):
1005             """
1006             solves Av=f-Au-B^*p with accuracy self.getReducedTolerance() (overwrite)
1007
1008             @rtype: equal to the type of v
1009             @note: boundary conditions on v should be zero!
1010             """
1011             pass
1012
1013          def solve_prec(self,p):
1014             """
1015             provides a preconditioner for BA^{-1}B^* with accuracy self.getReducedTolerance() (overwrite)
1016
1017             @rtype: equal to the type of p
1018             """
1019             pass
1020
1021          def stoppingcriterium(self,Bv,v,p):
1022             """
1023             returns a True if iteration is terminated. (overwrite)
1024
1025             @rtype: C{bool}
1026             """
1027             pass
1028
1029          def __inner(self,p,r):
1030             return self.inner(p,r[1])
1031
1032          def __inner_p(self,p1,p2):
1033             return self.inner(p1,p2)
1034
1035          def __stoppingcriterium(self,norm_r,r,p):
1036              return self.stoppingcriterium(r[1],r[0],p)
1037
1038          def __stoppingcriterium_GMRES(self,norm_r,norm_b):
1039              return self.stoppingcriterium_GMRES(norm_r,norm_b)
1040
1041          def __stoppingcriterium_MINRES(self,norm_r,norm_Ax):
1042              return self.stoppingcriterium_MINRES(norm_r,norm_Ax)
1043
1044
1045          def setTolerance(self,tolerance=1.e-8):
1046                  self.__tol=tolerance
1047          def getTolerance(self):
1048                  return self.__tol
1049          def setToleranceReductionFactor(self,reduction=0.01):
1050                  self.__reduction=reduction
1051          def getSubProblemTolerance(self):
1052                  return self.__reduction*self.getTolerance()
1053
1054          def solve(self,v,p,max_iter=20, verbose=False, show_details=False, solver='PCG'):
1055                  """
1056                  solves the saddle point problem using initial guesses v and p.
1057
1058                  @param max_iter: maximum number of iteration steps.
1059                  """
1060                  self.verbose=verbose
1061                  self.show_details=show_details and self.verbose
1062
1063                  # assume p is known: then v=A^-1(f-B^*p)
1064                  # which leads to BA^-1B^*p = BA^-1f
1065
1066              # Az=f is solved as A(z-v)=f-Av (z-v = 0 on fixed_u_mask)
1067
1068
1069              self.__z=v+self.solve_A(v,p*0)
1070
1071                  Bz=self.B(self.__z)
1072                  #
1073              #   solve BA^-1B^*p = Bz
1074                  #
1075                  #   note that the residual r=Bz-BA^-1B^*p = B(z-A^-1B^*p) = Bv
1076                  #
1077                  #   with                    Av=Az-B^*p = f - B^*p (v=z on fixed_u_mask)
1078                  #                           A(v-z)=Az-B^*p-Az = f -Az - B^*p (v-z=0 on fixed_u_mask)
1079                  #
1080                  self.iter=0
1081              if solver=='GMRES':
1082                    if self.verbose: print "enter GMRES method (iter_max=%s)"%max_iter
1083                    p=GMRES(Bz,self.__Aprod_GMRES,self.__Msolve_GMRES,self.__inner_p,self.__stoppingcriterium_GMRES,iter_max=max_iter, x=p*1.)
1084                    # solve Au=f-B^*p
1085                    #       A(u-v)=f-B^*p-Av
1086                    #       u=v+(u-v)
1087            u=v+self.solve_A(v,p)
1088
1089              if solver=='MINRES':
1090                    if self.verbose: print "enter MINRES method (iter_max=%s)"%max_iter
1091                    p=MINRES(Bz,self.__Aprod_GMRES,self.__Msolve_GMRES,self.__inner_p,self.__stoppingcriterium_MINRES,iter_max=max_iter, x=p*1.)
1092                    # solve Au=f-B^*p
1093                    #       A(u-v)=f-B^*p-Av
1094                    #       u=v+(u-v)
1095            u=v+self.solve_A(v,p)
1096                  else:
1097                    if self.verbose: print "enter PCG method (iter_max=%s)"%max_iter
1098                    p,r=PCG(ArithmeticTuple(self.__z*1.,Bz),self.__Aprod,self.__Msolve,self.__inner,self.__stoppingcriterium,iter_max=max_iter, x=p)
1099                u=r[0]
1100
1101                  print "RESULT div(u)=",util.Lsup(self.B(u)),util.Lsup(u)
1102
1103              return u,p
1104
1105          def __Msolve(self,r):
1106              return self.solve_prec(r[1])
1107
1108          def __Msolve_GMRES(self,r):
1109              return self.solve_prec(r)
1110
1111
1112          def __Aprod(self,p):
1113              # return BA^-1B*p
1114              #solve Av =-B^*p as Av =f-Az-B^*p
1115              v=self.solve_A(self.__z,-p)
1116              return ArithmeticTuple(v, self.B(v))
1117
1118          def __Aprod_GMRES(self,p):
1119              # return BA^-1B*p
1120              #solve Av =-B^*p as Av =f-Az-B^*p
1121          v=self.solve_A(self.__z,-p)
1122              return self.B(v)
1123