Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector.hpp
1 // $Id: numeric_vector.h,v 1.11 2005/02/22 22:17:34 jwpeterson Exp $
2 
3 // The libMesh Finite Element Library.
4 // Copyright (C) 2002-2005 Benjamin S. Kirk, John W. Peterson
5 
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3.0 of the License, or (at your option) any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #ifndef __numeric_vector_h__
21 #define __numeric_vector_h__
22 
23 #include <vector>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/numeric/ublas/vector.hpp>
26 
27 #include <feel/feelcore/traits.hpp>
28 
29 #include <feel/feelalg/datamap.hpp>
30 
31 namespace Feel
32 {
33 namespace ublas = boost::numeric::ublas;
34 
35 // forward declarations
36 template <typename T> class Vector;
37 template <typename T> class MatrixSparse;
38 template <typename T> class MatrixShell;
39 
48 template <typename T>
49 class Vector
50 {
51 public:
52 
53  typedef T value_type;
54  typedef typename type_traits<T>::real_type real_type;
55 
56  typedef Vector<T> self_type;
57  typedef boost::shared_ptr<Vector<T> > self_ptrtype;
58  typedef boost::shared_ptr<Vector<T> > clone_ptrtype;
59 
60  typedef DataMap datamap_type;
61  typedef boost::shared_ptr<datamap_type> datamap_ptrtype;
65  Vector ();
66 
67  Vector ( datamap_ptrtype const& n );
68 
72  Vector ( const size_type n, WorldComm const& _worldComm = Environment::worldComm() );
73 
78  Vector ( const size_type n,
79  const size_type n_local,
80  WorldComm const& _worldComm = Environment::worldComm() );
81 
82  Vector ( Vector const& v );
83 
88  virtual ~Vector ();
89 
90  datamap_type const& map() const
91  {
92  return *M_map;
93  }
94 
95  datamap_ptrtype const& mapPtr() const
96  {
97  return M_map;
98  }
99 
100  void setMap( datamap_ptrtype const& d )
101  {
102  M_map=d;
103  }
104 
109  virtual bool isInitialized() const
110  {
111  return M_is_initialized;
112  }
113 
118  virtual bool closed() const
119  {
120  return M_is_closed;
121  }
122 
126  virtual void close () = 0;
127 
131  virtual void clear ();
132 
133  void localize(const Vector<T>& V);
134 
139  virtual void zero () = 0;
140 
144  virtual void zero ( size_type /*start*/, size_type /*stop*/ ) = 0;
145 
149  virtual void setConstant( value_type v ) = 0;
150 
154  virtual void setZero()
155  {
156  this->zero();
157  }
158 
162  virtual void setOnes()
163  {
164  this->setConstant( 1 );
165  }
166 
171  virtual clone_ptrtype clone () const = 0;
172 
186  virtual void init ( const size_type,
187  const size_type,
188  const bool = false );
189 
193  virtual void init ( const size_type,
194  const bool = false );
195 
196 
197  // surement a virtualiser!!!
198  void init ( datamap_ptrtype const& dm )
199  {
200  M_is_closed = false;
201  M_is_initialized = false;
202  M_map = dm;
203  }
204 
205 
206 
207  // /**
208  // * Change the dimension to that of the
209  // * vector \p V. The same applies as for
210  // * the other \p init function.
211  // *
212  // * The elements of \p V are not copied, i.e.
213  // * this function is the same as calling
214  // * \p init(V.size(),fast).
215  // */
216  // virtual void init (const Vector<T>&,
217  // const bool = false) {}
218 
222  Vector<T> & operator= ( const T s );
223 
227  virtual Vector<T> & operator= ( const Vector<T> &V );
228 
232  Vector<T> & operator= ( const std::vector<T> &v );
233 
237  virtual value_type sum() const = 0;
238 
244  virtual real_type min () const = 0;
245 
249  //virtual real_type min( size_type& index ) const = 0;
250 
256  virtual real_type max () const = 0;
257 
261  //virtual real_type max( size_type& index ) const = 0;
262 
267  virtual real_type l1Norm () const = 0;
268 
274  virtual real_type l2Norm () const = 0;
275 
281  virtual real_type linftyNorm () const = 0;
282 
290  virtual size_type size () const
291  {
292  return M_map->nDof();
293  }
294 
299  virtual size_type localSize() const
300  {
301  return M_map->nLocalDofWithGhost();
302  }
303 
309  virtual size_type firstLocalIndex() const
310  {
311  return M_map->minMyGID();
312  }
313 
319  virtual size_type lastLocalIndex() const
320  {
321  return M_map->maxMyGID()+1;
322  }
323 
324  virtual bool localIndexIsGhost(size_type localDof) const
325  {
326  return M_map->dofGlobalProcessIsGhost(localDof);
327  }
328 
332  WorldComm const& comm() const
333  {
334  return M_map->comm();
335  }
336 
340  virtual T operator() ( const size_type i ) const = 0;
341 
346  virtual Vector<T> & operator += ( const Vector<value_type> &V ) = 0;
347 
352  virtual Vector<T> & operator -= ( const Vector<value_type> &V ) = 0;
353 
357  virtual void set ( const size_type i, const value_type& value ) = 0;
358 
362  virtual void add ( const size_type i, const value_type& value ) = 0;
363 
367  virtual void addVector ( int* i, int n, value_type* v ) = 0;
368 
374  virtual void add ( const value_type& s ) = 0;
375 
381  virtual void add ( const Vector<value_type>& V ) = 0;
382 
388  virtual void add ( const value_type& a, const Vector<value_type>& v ) = 0;
389 
395  void add ( const value_type& a, const boost::shared_ptr<Vector<value_type> >& v )
396  {
397  add( a, *v );
398  }
404  virtual void addVector ( const std::vector<T>& v,
405  const std::vector<size_type>& dof_indices ) = 0;
406 
413  virtual void addVector ( const Vector<T>& V,
414  const std::vector<size_type>& dof_indices ) = 0;
415 
420  virtual void addVector ( const Vector<T>& V_in,
421  const MatrixSparse<T>& A_in ) = 0;
422 
427  void addVector ( const boost::shared_ptr<Vector<T> >& V_in,
428  const boost::shared_ptr<MatrixSparse<T> >& A_in )
429  {
430  addVector( *V_in, *A_in );
431  }
432 
437  void addVector ( const Vector<T>& V_in,
438  const MatrixShell<T>& A_in );
439 
444  void addVector ( const boost::shared_ptr<Vector<T> >& V_in,
445  const boost::shared_ptr<MatrixShell<T> >& A_in );
446 
447 #if 0
448 
454  virtual void addVector ( const DenseVector<T>& V,
455  const std::vector<size_type>& dof_indices ) = 0;
456 #endif
457 
458  virtual value_type dot( Vector<T> const& v ) = 0;
459  virtual value_type dot( boost::shared_ptr<Vector<T> > const& v ) { return dot( *v ); }
460 
465  virtual void insert ( const std::vector<T>& v,
466  const std::vector<size_type>& dof_indices ) = 0;
467 
474  virtual void insert ( const Vector<T>& V,
475  const std::vector<size_type>& dof_indices ) = 0;
476 
483  virtual void insert ( const ublas::vector<T>& V,
484  const std::vector<size_type>& dof_indices ) = 0;
485 
490  virtual void scale ( const T factor ) = 0;
491 
500  virtual int compare ( const Vector<T> &other_vector,
501  const real_type threshold = 1e-10 ) const;
502 
503 
504 
508  virtual void print( std::ostream& os=std::cout ) const;
509 
513  friend std::ostream& operator << ( std::ostream& os, const Vector<T>& v )
514  {
515  v.print( os );
516  return os;
517  }
518 
525  virtual void printMatlab( const std::string name="NULL", bool renumber = false ) const
526  {
527  std::cerr << "ERROR: Not Implemented in base class yet!" << std::endl;
528  std::cerr << "ERROR writing MATLAB file " << name << std::endl;
529  FEELPP_ASSERT( 0 ).error( "invalid call" );
530  }
531 
538  virtual void createSubvector( Vector<T>& ,
539  const std::vector<size_type>& ) const
540  {
541  std::cerr << "ERROR: Not Implemented in base class yet!" << std::endl;
542  FEELPP_ASSERT( 0 ).error( "invalid call" );
543  }
544 
545 protected:
546 
552 
558 
562  datamap_ptrtype M_map;
563 };
564 
566 typedef boost::shared_ptr<vector_type> vector_ptrtype;
567 
568 /*----------------------- Inline functions ----------------------------------*/
569 
577 template <typename T>
578 typename type_traits<T>::real_type
579 inner_product( Vector<T> const& v1, Vector<T> const& v2 )
580 {
581  FEELPP_ASSERT( v1.localSize() == v2.localSize() &&
582  v1.size() == v2.size() )
583  ( v1.localSize() )( v2.localSize() )
584  ( v1.size() )( v2.size() ).error( "incompatible vector sizes" );
585 
586  typedef typename type_traits<T>::real_type real_type;
587 
588  size_type s = v1.localSize();
589  //size_type s = v1.map().nLocalDofWithoutGhost();
590  real_type res = 0;
591  size_type start = v1.firstLocalIndex();
592  real_type global_res = 0;
593 
594  if ( v1.comm().size() == 1 )
595  {
596  for ( size_type i = 0; i < s; ++i )
597  res += v1( start + i )* v2( start + i );
598 
599  global_res = res;
600  }
601  else
602  {
603  for ( size_type i = 0; i < s; ++i )
604  {
605  if ( !v1.localIndexIsGhost( start + i ) )
606  res += v1( start + i )* v2( start + i );
607  }
608 #if defined( FEELPP_HAS_MPI )
609  mpi::all_reduce( v1.comm(), res, global_res, std::plus<real_type>() );
610 #endif
611  }
612 
613  return global_res;
614 }
622 template <typename T>
623 typename type_traits<T>::real_type
624 inner_product( boost::shared_ptr<Vector<T> > const& v1,
625  boost::shared_ptr<Vector<T> > const& v2 )
626 {
627  return inner_product( *v1, *v2 );
628 }
629 
630 template <typename T>
631 typename type_traits<T>::real_type
632 dot( boost::shared_ptr<Vector<T> > const& v1,
633  boost::shared_ptr<Vector<T> > const& v2 )
634 {
635  return inner_product( *v1, *v2 );
636 }
637 template <typename T>
638 typename type_traits<T>::real_type
639 dot( Vector<T> const& v1,
640  Vector<T> const& v2 )
641 {
642  return inner_product( v1, v2 );
643 }
644 
645 
646 
647 
648 } // Feel
649 
650 #endif // #ifdef __numeric_vector_h__
virtual real_type l1Norm() const =0
virtual void init(const size_type, const size_type, const bool=false)
Definition: vector.cpp:107
virtual Vector< T > & operator+=(const Vector< value_type > &V)=0
virtual real_type l2Norm() const =0
virtual size_type localSize() const
Definition: vector.hpp:299
Definition: solverlinear.hpp:33
virtual void createSubvector(Vector< T > &, const std::vector< size_type > &) const
Definition: vector.hpp:538
virtual void addVector(int *i, int n, value_type *v)=0
virtual ~Vector()
Definition: vector.cpp:88
virtual bool isInitialized() const
Definition: vector.hpp:109
virtual void set(const size_type i, const value_type &value)=0
virtual real_type min() const =0
void add(const value_type &a, const boost::shared_ptr< Vector< value_type > > &v)
Definition: vector.hpp:395
virtual void insert(const std::vector< T > &v, const std::vector< size_type > &dof_indices)=0
virtual void print(std::ostream &os=std::cout) const
Definition: vector.cpp:316
void addVector(const boost::shared_ptr< Vector< T > > &V_in, const boost::shared_ptr< MatrixSparse< T > > &A_in)
Definition: vector.hpp:427
virtual size_type lastLocalIndex() const
Definition: vector.hpp:319
virtual void add(const size_type i, const value_type &value)=0
virtual bool closed() const
Definition: vector.hpp:118
virtual void setOnes()
Definition: vector.hpp:162
virtual void setConstant(value_type v)=0
virtual value_type sum() const =0
bool M_is_closed
Definition: vector.hpp:551
virtual real_type max() const =0
type_traits< T >::real_type inner_product(Vector< T > const &v1, Vector< T > const &v2)
Definition: vector.hpp:579
WorldComm const & comm() const
Definition: vector.hpp:332
virtual void printMatlab(const std::string name="NULL", bool renumber=false) const
Definition: vector.hpp:525
virtual void close()=0
virtual real_type linftyNorm() const =0
bool M_is_initialized
Definition: vector.hpp:557
virtual void zero()=0
virtual clone_ptrtype clone() const =0
size_t size_type
Indices (starting from 0)
Definition: feelcore/feel.hpp:319
virtual T operator()(const size_type i) const =0
Definition: matrixsparse.hpp:76
matrices that define its action against a vector
Definition: matrixshell.hpp:49
Vector< T > & operator=(const T s)
Definition: vector.cpp:97
virtual size_type size() const
Definition: vector.hpp:290
virtual int compare(const Vector< T > &other_vector, const real_type threshold=1e-10) const
virtual void setZero()
Definition: vector.hpp:154
virtual Vector< T > & operator-=(const Vector< value_type > &V)=0
Vector()
Definition: vector.cpp:36
virtual void clear()
Definition: vector.cpp:155
virtual void scale(const T factor)=0
datamap_ptrtype M_map
Definition: vector.hpp:562
virtual size_type firstLocalIndex() const
Definition: vector.hpp:309

Generated on Sun Dec 22 2013 13:11:15 for Feel++ by doxygen 1.8.5