compvector.h

Go to the documentation of this file.
00001 #ifndef  __compvector_h
00002 #define  __compvector_h
00003 
00004 #include  "nvector.h"
00005 #include  "nmatrix.h"
00006 #include  <cassert>
00007 
00008 /*------------------------------------------------------*/
00009 
00010 namespace Gascoigne
00011 {
00012 template<class T>
00013 class CompVector : public nvector<T>
00014 {
00015  protected:
00016 
00017   int N;
00018 
00019  public:
00020 
00021   typedef typename nvector<T>::const_iterator   const_iterator;
00022   typedef typename nvector<T>::iterator         iterator;
00023 
00024   int ncomp() const { return N; }
00025   int& ncomp() { return N; }
00026 
00027   ~CompVector() {}
00028   CompVector()                             : nvector<T>()      , N(0)  {}
00029   CompVector(int NN)                       : nvector<T>()      , N(NN) {}
00030   CompVector(int NN, size_t n)             : nvector<T>(NN*n)  , N(NN) {}
00031   CompVector(int NN, size_t n, const T& d) : nvector<T>(NN*n,d), N(NN) {}
00032 
00033   CompVector(const std::vector<T>& u)
00034     {
00035       N = 1;
00036       nvector<T>::reservesize(u.size());
00037       copy(u.begin(),u.end(),nvector<T>::begin());
00038     }
00039 
00040   CompVector(const CompVector& u)
00041     {
00042     ReInit(u);
00043     }
00044 
00045    CompVector& operator=(double d)
00046      {
00047        nvector<T>::operator=(d);
00048        return *this;
00049      }
00050 
00051   size_t  n() const { return nvector<T>::size()/N; } 
00052 
00053   const_iterator  start(int i) const { return std::vector<T>::begin() + i*N; }
00054   iterator        start(int i)       { return std::vector<T>::begin() + i*N; }
00055   const_iterator  stop (int i) const { return std::vector<T>::begin() + (i+1)*N; }
00056   iterator        stop (int i)       { return std::vector<T>::begin() + (i+1)*N; }
00057 
00058   const T& operator()(int i, int c) const {return *(start(i)+c);}
00059   T&       operator()(int i, int c)       {return *(start(i)+c);}
00060 
00061   void ReInit(size_t ncomp, size_t n) {assert(ncomp); N=ncomp; reservesize(n);}
00062   
00063   void ReInit(const CompVector& u) {
00064     N = u.ncomp();
00065     nvector<T>::reservesize(u.size());
00066     copy(u.begin(),u.end(),nvector<T>::begin());
00067   }
00068   
00069   void reservesize(size_t n, const T& s=0) { nvector<T>::reservesize(n*N,s); }
00070   void reservesize(const CompVector& u) 
00071     {
00072       ncomp() = u.ncomp();
00073       nvector<T>::reservesize(u.size()); 
00074     }
00075   void resize(size_t n, const T& s=0.) { nvector<T>::resize(n*N,s); }
00076   void total_reservesize(size_t n)    { nvector<T>::reservesize(n); }
00077 
00078   void equ_node(int i, double d0)
00079     {
00080       iterator       p = start(i);
00081       const_iterator q = start(i)+N;
00082       while(p!=q) *(p++) = d0;
00083    }
00084   void scale_comp(int c,double d0)                                                                                                                                    
00085     { 
00086       iterator       p = nvector<T>::begin()+c; 
00087       while(p<nvector<T>::end()) {*p *= d0; p+=N;} 
00088     } 
00089   void scale_node(int i, double d0)
00090     {
00091       iterator       p = start(i);
00092       const_iterator q = start(i)+N;
00093       while(p!=q) *(p++) *= d0;
00094    }
00095 
00096   void add_node(int i, double d0, const nvector<T>& u0)
00097     {
00098             iterator       p = start(i);
00099       const_iterator pp= p+N;
00100       const_iterator q = u0.begin();
00101       while(p!=pp) *(p++) += d0 * *(q++);
00102     }
00103 
00104   void equ_node(int i, int j, const CompVector<T>& u0)
00105     {
00106             iterator       p = start(i);
00107       const_iterator pp= p+N;
00108       const_iterator q = u0.start(j);
00109       while(p!=pp) *(p++) = *(q++);
00110     }
00111 
00112   void equ_node(int i, double d0, int i0, const CompVector<T>& u0)
00113     {
00114             iterator       p = start(i);
00115       const_iterator pp= p+N;
00116       const_iterator q = u0.start(i0);
00117       while(p!=pp) *(p++) = d0 * *(q++);
00118     }
00119   void equ_node(int i, double d0, int i0, double d1, int i1)
00120     {
00121             iterator       p = start(i);
00122       const_iterator pp= p+N;
00123       const_iterator q0 = start(i0);
00124       const_iterator q1 = start(i1);
00125       while(p!=pp) *(p++) = d0* *q0++ + d1* *q1++;
00126     }
00127   void equ_node(int i, double d0, int i0, double d1, int i1, double d2, int i2)
00128     {
00129             iterator       p = start(i);
00130       const_iterator pp= p+N;
00131       const_iterator q0 = start(i0);
00132       const_iterator q1 = start(i1);
00133       const_iterator q2 = start(i2);
00134       while(p!=pp) *(p++) = d0* *q0++ + d1* *q1++ + d2* *q2++;
00135     }
00136   void add_node(int i, double d0, int i0, double d1, int i1, double d2, int i2)
00137     {
00138             iterator       p = start(i);
00139       const_iterator pp= p+N;
00140       const_iterator q0 = start(i0);
00141       const_iterator q1 = start(i1);
00142       const_iterator q2 = start(i2);
00143       while(p!=pp) *(p++) += d0* *q0++ + d1* *q1++ + d2* *q2++;
00144     }
00145   void equ_node(int i, double d0, int i0, double d1, int i1, 
00146                        double d2, int i2, double d3, int i3)
00147     {
00148             iterator       p = start(i);
00149       const_iterator pp= p+N;
00150       const_iterator q0 = start(i0);
00151       const_iterator q1 = start(i1);
00152       const_iterator q2 = start(i2);
00153       const_iterator q3 = start(i3);
00154       while(p!=pp) *(p++) = d0* *q0++ + d1* *q1++ + d2* *q2++ + d3* *q3++;
00155     }
00156    void equ_node(int i, double d0, int i0, const CompVector<T>& u0,
00157                 double d1, int i1, const CompVector<T>& u1)
00158     {
00159             iterator       p  = start(i);
00160       const_iterator q0 = u0.start(i0);
00161       const_iterator q1 = u1.start(i1);
00162       for(int c=0;c<N;c++)
00163         {
00164           *(p++) = d0* *(q0++) + d1* *(q1++) ;
00165         }
00166     }
00167 
00168   void zero_comp(int c)
00169     {
00170       iterator       p = std::vector<T>::begin()+c;
00171       while(p<nvector<T>::end()) {*p = 0.; p+=N;}
00172     }
00173 
00174   void zero_node(int i)
00175     {
00176             iterator       p = start(i);
00177       const_iterator q = p+N;;
00178       while(p<q) *(p++) = 0.;
00179     }
00180 
00181   void add_node(int i, double d0, int i0)
00182     {
00183             iterator       p = start(i);
00184       const_iterator q = start(i0);
00185       for(int c=0;c<N;c++) *(p++) += d0 * *(q++);
00186     }
00187 
00188   void add_node(int i, double d0, int i0, double d1, int i1)
00189     {
00190             iterator       p = start(i);
00191       const_iterator q0 = start(i0);
00192       const_iterator q1 = start(i1);
00193       for(int c=0;c<N;c++) *(p++) += d0 * *(q0++) + d1* *(q1++);
00194     }
00195 
00196   void add_node(int i, double d0, int i0, const CompVector<T>& u0)
00197     {
00198             iterator       p = start(i);
00199       const_iterator q = u0.start(i0);
00200       for(int c=0;c<N;c++) *(p++) += d0 * *(q++);
00201     }
00202   double CompScp(int c, const CompVector<T>& v) const
00203     {
00204       double d = 0.;
00205       const_iterator first  = std::vector<T>::begin()+c;
00206       const_iterator last   = std::vector<T>::end();
00207       const_iterator first2  = v.begin()+c;
00208       
00209       while( first < last)
00210         {
00211           d += *first * *first2;
00212           first += N;
00213           first2 += N;
00214         }
00215       return d;
00216     }
00217   double CompNormL8(int c) const
00218     {
00219       double d = 0;
00220       const_iterator first  = std::vector<T>::begin()+c;
00221       const_iterator last   = std::vector<T>::end();
00222       
00223       while( first < last)
00224         {
00225           d = Gascoigne::max( d, fabs(*first));
00226           first += N;
00227         }
00228       return d;
00229     }
00230   double CompMin(int c) const
00231     {
00232       double d = 1.e40;
00233       const_iterator first  = std::vector<T>::begin()+c;
00234       const_iterator last   = std::vector<T>::end();
00235       
00236       while( first < last)
00237         {
00238           d = Gascoigne::min( d, *first);
00239           first += N;
00240         }
00241       return d;
00242     }
00243   double CompMax(int c) const
00244     {
00245       double d = -1e14;
00246       const_iterator first  = std::vector<T>::begin()+c;
00247       const_iterator last   = std::vector<T>::end();
00248       
00249       while( first < last)
00250         {
00251           d = Gascoigne::max( d, *first);
00252           first += N;
00253         }
00254       return d;
00255     }
00257   void SetMax(int c, double val)
00258     {
00259       iterator first  = std::vector<T>::begin()+c;
00260       const_iterator last   = std::vector<T>::end();
00261       
00262       while( first < last)
00263         {
00264           (*first) = Gascoigne::max(val,(*first));
00265           first += N;
00266         }
00267     }
00269   void FillLocal(int i, nvector<double>& uloc) const
00270     {
00271       assert(uloc.size()==N);
00272       const_iterator  first  = start(i);
00273       for(int ii=0;ii<N;++ii) uloc[ii] = *first++;
00274     }
00275   void node_zero(int i)
00276     {
00277       iterator  first  = start(i);
00278       const_iterator last   = stop(i);
00279       
00280       while( first != last)      {*first++ = 0.;}
00281     }
00282   void CompAdd(int c, double d)
00283     {
00284       iterator       first  = std::vector<T>::begin()+c;
00285       const_iterator last   = std::vector<T>::end();
00286       
00287       while( first < last)
00288         {
00289           *first += d;
00290           first += N;
00291         }
00292     }
00293   void CompAdd(int c, double d,  const CompVector& y)
00294     {
00295       iterator       first  = std::vector<T>::begin()+c;
00296       const_iterator first2 = y.begin()+c;
00297       const_iterator last   = std::vector<T>::end();
00298       
00299       while( first < last)
00300         {
00301           *first += d * *first2;
00302           first  += N;
00303           first2 += N;
00304         }
00305     }
00306   void CompEq(int c, double d)
00307     {
00308       iterator       first  = std::vector<T>::begin()+c;
00309       const_iterator last   = std::vector<T>::end();
00310       
00311       while( first < last)
00312         {
00313           *first = d;
00314           first += N;
00315         }
00316     }
00317   void CompEq(int c, double d,  const CompVector& y)
00318     {
00319       iterator       first  = std::vector<T>::begin()+c;
00320       const_iterator first2 = y.begin()+c;
00321       const_iterator last   = std::vector<T>::end();
00322       
00323       while( first < last)
00324         {
00325           *first = d * *first2;
00326           first  += N;
00327           first2 += N;
00328         }
00329     }
00330   double CompSum(int c) const
00331     {
00332       double d = 0.;
00333       const_iterator first  = std::vector<T>::begin()+c;
00334       const_iterator last   = std::vector<T>::end();
00335       
00336       while( first < last)
00337         {
00338           d += *first;
00339           first += N;
00340         }
00341       return d;
00342     }
00343   double CompNorm(int c) const
00344     {
00345       double d = 0.;
00346       const_iterator first  = std::vector<T>::begin()+c;
00347       const_iterator last   = std::vector<T>::end();
00348       
00349       while( first < last)
00350         {
00351           d += *first * *first;
00352           first += N;
00353         }
00354       return sqrt(d);
00355     }
00356   nvector<double> CompNorm() const
00357     {      
00358       nvector<double> d(N,0.);
00359 
00360       for(int c=0;c<N;c++)
00361         {
00362           const_iterator       first   = std::vector<T>::begin()  +c;
00363           const_iterator last   = std::vector<T>::end();
00364       
00365           while( first < last)
00366             {
00367               d[c] += *first * *first;
00368               first += N;
00369             }
00370           d[c] = sqrt(d[c]);
00371         }
00372       return d;
00373     }
00374   void Add(const nvector<double>& scp, const CompVector<T>& y)
00375     {
00376       for(int c=0;c<N;c++)
00377         {
00378           iterator       first   = std::vector<T>::begin()  +c;
00379           const_iterator first2  = y.begin()+c;
00380           const_iterator last    = std::vector<T>::end();
00381           while( first < last) 
00382             {
00383               *first += scp[c] * *first2;
00384               first += N;   first2 += N;
00385             }
00386         }
00387     }
00388   void ScalarProductComp(nvector<double>& scp, const CompVector<T>& y) const
00389     {
00390       scp.resize(N);
00391       scp.zero();
00392 
00393       for(int c=0;c<N;c++)
00394         {
00395           const_iterator first   = std::vector<T>::begin()  +c;
00396           const_iterator first2  = y.begin()+c;
00397           const_iterator last    = std::vector<T>::end();
00398           while( first < last) 
00399             {
00400               scp[c] += *first * *first2;
00401               first += N;   first2 += N;
00402             }
00403         }
00404     }
00405   void ScalarProductCompMatrix(nmatrix<double>& scp, const CompVector<T>& y) const
00406     {
00407       scp.memory(N,N);
00408       scp.zero();
00409 
00410       for(int c=0;c<N;c++)
00411         {
00412           for(int d=0;d<N;d++)
00413             {
00414               if(d>c) continue;
00415               const_iterator first   = std::vector<T>::begin()  +c;
00416               const_iterator first2  = y.begin()+d;
00417               const_iterator last    = std::vector<T>::end();
00418               while( first < last) 
00419           {
00420             scp(c,d) += *first * *first2;
00421             first += N;   first2 += N;
00422           }
00423             }
00424         }
00425       for(int c=0;c<N;c++)
00426         {
00427           for(int d=0;d<N;d++)
00428             {
00429               if(d>c) scp(c,d) = scp(d,c);
00430             }
00431         }
00432     }
00433   void Read(std::istream& s, int c)
00434     {
00435       iterator first   = std::vector<T>::begin()+c;
00436       const_iterator last    = std::vector<T>::end();
00437       while( first < last) 
00438         {
00439           s >> *first;
00440           first += N;
00441         }
00442     }
00443   
00444   void BinWrite(std::ostream& out) const
00445     {
00446       out << ncomp() << " " << n() << std::endl << "[";
00447       
00448       int sizeT = sizeof(T);
00449       for(int i=0; i<nvector<T>::size(); i++)
00450         {
00451           out.write(reinterpret_cast<const char*>(&(nvector<T>::operator[](i))),sizeT);
00452         }
00453       out << "]"; 
00454     }
00455 
00456   void BinRead(std::istream& in)
00457     {
00458       char cc;
00459       int  c,n;
00460       in >> c >> n >> cc;
00461       ncomp() = c;
00462       resize(n);
00463       
00464       int sizeT = sizeof(T);
00465       for(int i=0; i<nvector<T>::size(); i++)
00466         {
00467           in.read(reinterpret_cast<char*>(&(nvector<T>::operator[](i))),sizeT);
00468         }
00469       in >> cc;
00470     }
00471 };
00472 }
00473 
00474 #endif

Generated on Thu Sep 14 10:34:34 2006 for Gascoigne by  doxygen 1.4.7