#ifndef MatriceCreuse_h_ #define MatriceCreuse_h_ #define VERSION_MATRICE_CREUSE 4 // add Sep 2007 for generic Space solver #define DCL_ARG_SPARSE_SOLVER(T,A) Stack stack, HashMatrix *A, Data_Sparse_Solver & ds #define ARG_SPARSE_SOLVER(A) stack,A, ds #ifdef HAVE_LIBUMFPACK_XXXXXXXXXXXXX extern "C" { #ifdef HAVE_UMFPACK_H #include #else #ifdef HAVE_UMFPACK_UMFPACK_H #include #else #ifdef HAVE_BIG_UMFPACK_UMFPACK_H #include #else #ifdef HAVE_UFSPARSE_UMFPACK_H #include #else #ifdef HAVE_SUITESPARSE_UMFPACK_H #include #else // Defaults to a local version of the UMFPACK headers #include "../../3rdparty/include/umfpack.h" #endif // HAVE_SUITESPARSE_UMFPACK_H #endif // HAVE_UFSPARSE_UMFPACK_H #endif // HAVE_BIG_UMFPACK_UMFPACK_H #endif // HAVE_UMFPACK_UMFPACK_H #endif // HAVE_UMFPACK_H } #endif #include "RNM.hpp" #include "fem.hpp" #include "FESpace.hpp" #include "DOperator.hpp" #include "QuadratureFormular.hpp" #include "HashMatrix.hpp" extern double ff_tgv; using Fem2D::Mesh; using Fem2D::FESpace; using Fem2D::FElement; using Fem2D::baseFElement; using Fem2D::FMortar; using Fem2D::TypeOfMortar; using Fem2D::QuadratureFormular; using Fem2D::QuadratureFormular1d; using Fem2D::QuadratureFormular_T_5; using Fem2D::QF_GaussLegendre3; const double EPSILON=1e-20; using Fem2D::onWhatIsEdge; //#define APROGRAMMER(a) {cerr << "A PROGRAMMER " #a << endl; exit (1) ;} #define ERREUR(a,b) do {cerr << "ERROR " #a<< b < class VirtualMatrix; template class HashMatrix ; template using MatriceCreuse=VirtualMatrix ; template using MatriceMorse=HashMatrix; //template class MatriceCreuse; template class MatriceElementaire; template class MatriceElementaireSymetrique; template class MatriceElementairePleine; //template class MatriceMorseOld; template class MatriceProdTensoriel; //template R Square(R x){ return x*x;} template T* docpyornot(bool nocpy,T* p,int n) { T * r=p; if( !nocpy) { // do copy r= new T[n]; ffassert(r); if(p) for(int i=0;i T* docpy(TT* p,int n) { T * r=0; if(p && n) { // do copy r= new T[n]; ffassert(r); for(int i=0;i class VirtualMatrix; template class HashMatrix; template class StopGC { public: virtual bool Stop(int, R *, R * ){cout << " Stop !!!!!\n"; return false;} }; template// S=StopGC int ConjuguedGradient(const M & A,const P & C,const KN_ &b,KN_ &x,const int nbitermax, double &eps,long kprint=1000000000,S *Stop=0) { // ConjuguedGradient lineare A*x est appele avec des conditions au limites // non-homogene puis homogene pour calculer le gradient if (verbosity>50) kprint=2; if (verbosity>99) cout << A << endl; // throwassert(&x && &b && &A && &C); typedef KN Rn; int n=b.N(); throwassert(n==x.N()); Rn g(n), h(n), Ah(n), & Cg(Ah); // on utilise Ah pour stocke Cg g = A*x; double xx= std::real((x,conj(x))); double epsold=eps; g -= b;// g = Ax-b Cg = C*g; // gradient preconditionne h =-Cg; double g2 = std::real((Cg,conj(g))); if (g2 < 1e-30) { if(verbosity>1 || (kprint<100000)) cout << "GC g^2 =" << g2 << " < 1.e-30 Nothing to do " << endl; return 2; } double reps2 =eps >0 ? eps*eps*g2 : -eps; // epsilon relatif eps = reps2; for (int iter=0;iter<=nbitermax;iter++) { Ah = A*h; double hAh =std::real((h,conj(Ah))); // if (Abs(hAh)<1e-30) ExecError("CG2: Matrix non defined, sorry "); R ro = - std::real((g,conj(h)))/ hAh; // ro optimal (produit scalaire usuel) x += ro *h; g += ro *Ah; // plus besoin de Ah, on utilise avec Cg optimisation Cg = C*g; double g2p=g2; g2 = std::real((Cg,conj(g))); bool stop = Stop && Stop->Stop(iter,x,g); if ( !(iter%kprint) && iter && (verbosity>3) ) cout << "CG:" <0 ) { // change fo eps converge to fast due to the // penalization of boundary condition. eps = epsold*epsold*g2; if (verbosity>3 || (kprint<3)) cout << "CG converge to fast (pb of BC) restart: " << iter << " ro = " << ro << " ||g||^2 = " << g2 << " <= " << reps2 << " new eps2 =" << eps <1 || (kprint<100000) ) cout << "CG converge: " << iter << " ro = " << ro << " ||g||^2 = " << g2 << endl; return 1;// ok } } double gamma = g2/g2p; h *= gamma; h -= Cg; // h = -Cg * gamma* h } if(verbosity) cout << " GC: method doesn't converge in " << nbitermax << " iteration , xx= " << xx<< endl; return 0; } template// S=StopGC int ConjuguedGradient2(const M & A,const P & C,KN_ &x,const KN_ &b,const int nbitermax, double &eps,long kprint=1000000000,S *Stop=0) { // ConjuguedGradient2 affine A*x = 0 est toujours appele avec les condition aux limites // ------------- // throwassert(&x && &A && &C); typedef KN Rn; int n=x.N(); // if (verbosity>99) kprint=1; R ro=1; Rn g(n),h(n),Ah(n), & Cg(Ah); // on utilise Ah pour stocke Cg g = A*x; g -= b; Cg = C*g; // gradient preconditionne h =-Cg; R g2 = (Cg,g); if (g2 < 1e-30) { if(verbosity>1 || kprint< 1000000) cout << "GC g^2 =" << g2 << " < 1.e-30 Nothing to do " << endl; return 2; } if (verbosity>5 || (kprint<2)) cout << " 0 GC g^2 =" << g2 << endl; R reps2 =eps >0 ? eps*eps*g2 : -eps; // epsilon relatif eps = reps2; for (int iter=0;iter<=nbitermax;iter++) { R rop = ro; x += rop*h; // x+ rop*h , g=Ax (x old) // ((Ah = A*x - b) - g); // Ah -= b; // Ax + rop*Ah = rop*Ah + g = // Ah -= g; // Ah*rop Ah = A*x; Ah -= b; // Ax + rop*Ah = rop*Ah + g = Ah -= g; // Ah*rop R hAh =(h,Ah); if (std::norm(hAh)<1e-60) ExecError("CG2: Matrix is not defined (/0), sorry "); ro = - (g,h)*rop/hAh ; // ro optimal (produit scalaire usuel) if ( ro != ro) ExecError("CG2: Bug : ro is NaN ??? "); x += (ro-rop) *h; g += (ro/rop) *Ah; // plus besoin de Ah, on utilise avec Cg optimisation Cg = C*g; R g2p=g2; g2 = (Cg,g); bool stop = Stop && Stop->Stop(iter,x,g); if ( ( (iter%kprint) == kprint-1) /*&& verbosity >1*/ ) cout << "CG:" <