PetscVector< T > Class Template Reference

#include <petsc_vector.h>

Inheritance diagram for PetscVector< T >:

List of all members.

Public Member Functions

 PetscVector (const ParallelType type=AUTOMATIC)
 PetscVector (const unsigned int n, const ParallelType type=AUTOMATIC)
 PetscVector (const unsigned int n, const unsigned int n_local, const ParallelType type=AUTOMATIC)
 PetscVector (const unsigned int N, const unsigned int n_local, const std::vector< unsigned int > &ghost, const ParallelType type=AUTOMATIC)
 PetscVector (Vec v)
 ~PetscVector ()
void close ()
void clear ()
void zero ()
virtual AutoPtr< NumericVector
< T > > 
zero_clone () const
AutoPtr< NumericVector< T > > clone () const
void init (const unsigned int N, const unsigned int n_local, const bool fast=false, const ParallelType type=AUTOMATIC)
void init (const unsigned int N, const bool fast=false, const ParallelType type=AUTOMATIC)
virtual void init (const unsigned int, const unsigned int, const std::vector< unsigned int > &, const bool=false, const ParallelType=AUTOMATIC)
virtual void init (const NumericVector< T > &other, const bool fast=false)
NumericVector< T > & operator= (const T s)
NumericVector< T > & operator= (const NumericVector< T > &V)
PetscVector< T > & operator= (const PetscVector< T > &V)
NumericVector< T > & operator= (const std::vector< T > &v)
Real min () const
Real max () const
sum () const
Real l1_norm () const
Real l2_norm () const
Real linfty_norm () const
unsigned int size () const
unsigned int local_size () const
unsigned int first_local_index () const
unsigned int last_local_index () const
unsigned int map_global_to_local_index (const unsigned int i) const
operator() (const unsigned int i) const
virtual void get (const std::vector< unsigned int > &index, std::vector< T > &values) const
NumericVector< T > & operator+= (const NumericVector< T > &V)
NumericVector< T > & operator-= (const NumericVector< T > &V)
void set (const unsigned int i, const T value)
void add (const unsigned int i, const T value)
void add (const T s)
void add (const NumericVector< T > &V)
void add (const T a, const NumericVector< T > &v)
void add_vector (const std::vector< T > &v, const std::vector< unsigned int > &dof_indices)
void add_vector (const NumericVector< T > &V, const std::vector< unsigned int > &dof_indices)
void add_vector (const NumericVector< T > &V, const SparseMatrix< T > &A)
void add_vector (const DenseVector< T > &V, const std::vector< unsigned int > &dof_indices)
virtual void insert (const std::vector< T > &v, const std::vector< unsigned int > &dof_indices)
virtual void insert (const NumericVector< T > &V, const std::vector< unsigned int > &dof_indices)
virtual void insert (const DenseVector< T > &V, const std::vector< unsigned int > &dof_indices)
virtual void insert (const DenseSubVector< T > &V, const std::vector< unsigned int > &dof_indices)
void scale (const T factor)
virtual void abs ()
virtual T dot (const NumericVector< T > &V) const
void localize (std::vector< T > &v_local) const
void localize (NumericVector< T > &v_local) const
void localize (NumericVector< T > &v_local, const std::vector< unsigned int > &send_list) const
void localize (const unsigned int first_local_idx, const unsigned int last_local_idx, const std::vector< unsigned int > &send_list)
void localize_to_one (std::vector< T > &v_local, const unsigned int proc_id=0) const
virtual void pointwise_mult (const NumericVector< T > &vec1, const NumericVector< T > &vec2)
void print_matlab (const std::string name="NULL") const
virtual void create_subvector (NumericVector< T > &subvector, const std::vector< unsigned int > &rows) const
virtual void swap (NumericVector< T > &v)
Vec vec ()
template<>
void localize_to_one (std::vector< Real > &v_local, const unsigned int pid) const
template<>
void localize_to_one (std::vector< Complex > &v_local, const unsigned int pid) const
virtual bool initialized () const
ParallelType type () const
ParallelType & type ()
virtual bool closed () const
virtual Real subset_l1_norm (const std::set< unsigned int > &indices)
virtual Real subset_l2_norm (const std::set< unsigned int > &indices)
virtual Real subset_linfty_norm (const std::set< unsigned int > &indices)
virtual T el (const unsigned int i) const
NumericVector< T > & operator*= (const T a)
NumericVector< T > & operator/= (const T a)
void add_vector (const NumericVector< T > &v, const ShellMatrix< T > &a)
virtual int compare (const NumericVector< T > &other_vector, const Real threshold=TOLERANCE) const
template<>
int compare (const NumericVector< float > &other_vector, const Real threshold) const
template<>
int compare (const NumericVector< double > &other_vector, const Real threshold) const
template<>
int compare (const NumericVector< long double > &other_vector, const Real threshold) const
template<>
int compare (const NumericVector< Complex > &other_vector, const Real threshold) const
virtual void print (std::ostream &os=std::cout) const
template<>
void print (std::ostream &os) const
virtual void print_global (std::ostream &os=std::cout) const
template<>
void print_global (std::ostream &os) const

Static Public Member Functions

static AutoPtr< NumericVector
< T > > 
build (const SolverPackage solver_package=libMesh::default_solver_package())
static std::string get_info ()
static void print_info ()
static unsigned int n_objects ()

Protected Types

typedef std::map< std::string,
std::pair< unsigned int,
unsigned int > > 
Counts

Protected Member Functions

void increment_constructor_count (const std::string &name)
void increment_destructor_count (const std::string &name)

Protected Attributes

bool _is_closed
bool _is_initialized
ParallelType _type

Static Protected Attributes

static Counts _counts
static Threads::atomic
< unsigned int > 
_n_objects
static Threads::spin_mutex _mutex

Private Types

typedef std::map< unsigned int,
unsigned int > 
GlobalToLocalMap

Private Member Functions

void _get_array (void) const
void _restore_array (void) const

Private Attributes

Vec _vec
bool _array_is_present
unsigned int _local_size
Vec _local_form
PetscScalar * _values
GlobalToLocalMap _global_to_local_map
bool _destroy_vec_on_exit

Friends

std::ostream & operator<< (std::ostream &os, const NumericVector< T > &v)


Detailed Description

template<typename T>
class PetscVector< T >

Petsc vector. Provides a nice interface to the Petsc C-based data structures for parallel vectors.

Author:
Benjamin S. Kirk, 2002

Definition at line 59 of file petsc_vector.h.


Member Typedef Documentation

typedef std::map<std::string, std::pair<unsigned int, unsigned int> > ReferenceCounter::Counts [protected, inherited]

Data structure to log the information. The log is identified by the class name.

Definition at line 105 of file reference_counter.h.

template<typename T >
typedef std::map<unsigned int,unsigned int> PetscVector< T >::GlobalToLocalMap [private]

Type for map that maps global to local ghost cells.

Definition at line 549 of file petsc_vector.h.


Constructor & Destructor Documentation

template<typename T >
PetscVector< T >::PetscVector ( const ParallelType  type = AUTOMATIC  )  [inline, explicit]

Dummy-Constructor. Dimension=0

Definition at line 571 of file petsc_vector.h.

References NumericVector< T >::_type.

00572   : _array_is_present(false),
00573     _local_form(NULL),
00574     _values(NULL),
00575     _global_to_local_map(),
00576     _destroy_vec_on_exit(true)
00577 {
00578   this->_type = type;
00579 }

template<typename T >
PetscVector< T >::PetscVector ( const unsigned int  n,
const ParallelType  type = AUTOMATIC 
) [inline, explicit]

Constructor. Set dimension to n and initialize all elements with zero.

Definition at line 585 of file petsc_vector.h.

References PetscVector< T >::init().

00587   : _array_is_present(false),
00588     _local_form(NULL),
00589     _values(NULL),
00590     _global_to_local_map(),
00591     _destroy_vec_on_exit(true)
00592 {
00593   this->init(n, n, false, type);
00594 }

template<typename T >
PetscVector< T >::PetscVector ( const unsigned int  n,
const unsigned int  n_local,
const ParallelType  type = AUTOMATIC 
) [inline]

Constructor. Set local dimension to n_local, the global dimension to n, and initialize all elements with zero.

Definition at line 600 of file petsc_vector.h.

References PetscVector< T >::init().

00603   : _array_is_present(false),
00604     _local_form(NULL),
00605     _values(NULL),
00606     _global_to_local_map(),
00607     _destroy_vec_on_exit(true)
00608 {
00609   this->init(n, n_local, false, type);
00610 }

template<typename T >
PetscVector< T >::PetscVector ( const unsigned int  N,
const unsigned int  n_local,
const std::vector< unsigned int > &  ghost,
const ParallelType  type = AUTOMATIC 
) [inline]

Constructor. Set local dimension to n_local, the global dimension to n, but additionally reserve memory for the indices specified by the ghost argument.

Definition at line 616 of file petsc_vector.h.

References PetscVector< T >::init().

00620   : _array_is_present(false),
00621     _local_form(NULL),
00622     _values(NULL),
00623     _global_to_local_map(),
00624     _destroy_vec_on_exit(true)
00625 {
00626   this->init(n, n_local, ghost, false, type);
00627 }

template<typename T >
PetscVector< T >::PetscVector ( Vec  v  )  [inline]

Constructor. Creates a PetscVector assuming you already have a valid PETSc Vec object. In this case, v is NOT destroyed by the PetscVector constructor when this object goes out of scope. This allows ownership of v to remain with the original creator, and to simply provide additional functionality with the PetscVector.

Definition at line 635 of file petsc_vector.h.

References PetscVector< T >::_global_to_local_map, NumericVector< T >::_is_closed, NumericVector< T >::_is_initialized, NumericVector< T >::_type, PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, PetscVector< T >::local_size(), libMeshEnums::PARALLEL, and libMeshEnums::SERIAL.

00636   : _array_is_present(false),
00637     _local_form(NULL),
00638     _values(NULL),
00639     _global_to_local_map(),
00640     _destroy_vec_on_exit(false)
00641 {
00642   this->_vec = v;
00643   this->_is_closed = true;
00644   this->_is_initialized = true;
00645 
00646   /* We need to ask PETSc about the (local to global) ghost value
00647      mapping and create the inverse mapping out of it.  */
00648   int ierr=0, petsc_size=0, petsc_local_size=0;
00649   ierr = VecGetSize(_vec, &petsc_size);
00650   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00651   ierr = VecGetLocalSize(_vec, &petsc_local_size);
00652   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00653 
00654   /* \p petsc_local_size is the number of non-ghost values.  If it
00655      equals the global size, then we are a serial vector, and there
00656      are no ghost values.  */
00657   if(petsc_size!=petsc_local_size)
00658     {
00659       ISLocalToGlobalMapping mapping = _vec->mapping;
00660 
00661       // If is a sparsely stored vector, set up our new mapping
00662       if (mapping)
00663         {
00664           const unsigned int local_size = static_cast<unsigned int>(petsc_local_size);
00665           const unsigned int ghost_begin = static_cast<unsigned int>(petsc_local_size);
00666           const unsigned int ghost_end = static_cast<unsigned int>(mapping->n);
00667           for(unsigned int i=ghost_begin; i<ghost_end; i++)
00668             _global_to_local_map[mapping->indices[i]] = i-local_size;
00669           this->_type = GHOSTED;
00670         }
00671       else
00672         this->_type = PARALLEL;
00673     }
00674   else
00675     this->_type = SERIAL;
00676 
00677 }

template<typename T >
PetscVector< T >::~PetscVector (  )  [inline]

Destructor, deallocates memory. Made virtual to allow for derived classes to behave properly.

Definition at line 684 of file petsc_vector.h.

References PetscVector< T >::clear().

00685 {
00686   this->clear ();
00687 }


Member Function Documentation

template<typename T >
void PetscVector< T >::_get_array ( void   )  const [inline, private]

Queries the array (and the local form if the vector is ghosted) from Petsc.

Definition at line 1166 of file petsc_vector.h.

References PetscVector< T >::_array_is_present, PetscVector< T >::_local_form, PetscVector< T >::_local_size, PetscVector< T >::_values, PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, NumericVector< T >::initialized(), PetscVector< T >::local_size(), and NumericVector< T >::type().

Referenced by PetscVector< T >::get(), and PetscVector< T >::operator()().

01167 {
01168   libmesh_assert (this->initialized());
01169   if(!_array_is_present)
01170     {
01171       int ierr=0;
01172       if(this->type() != GHOSTED)
01173         {
01174           ierr = VecGetArray(_vec, &_values);
01175           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01176         }
01177       else
01178         {
01179           ierr = VecGhostGetLocalForm (_vec,&_local_form);
01180           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01181           ierr = VecGetArray(_local_form, &_values);
01182           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01183 #ifndef NDEBUG
01184           int local_size = 0;
01185           ierr = VecGetLocalSize(_local_form, &local_size);
01186           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01187           _local_size = static_cast<unsigned int>(local_size);
01188 #endif
01189         }
01190       _array_is_present = true;
01191     }
01192 }

template<typename T >
void PetscVector< T >::_restore_array ( void   )  const [inline, private]

Restores the array (and the local form if the vector is ghosted) to Petsc.

Definition at line 1198 of file petsc_vector.h.

References PetscVector< T >::_array_is_present, PetscVector< T >::_local_form, PetscVector< T >::_local_size, PetscVector< T >::_values, PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, NumericVector< T >::initialized(), and NumericVector< T >::type().

Referenced by PetscVector< T >::abs(), PetscVector< T >::add(), PetscVector< T >::add_vector(), PetscVector< T >::clear(), PetscVector< T >::close(), PetscVector< T >::create_subvector(), PetscVector< T >::dot(), PetscVector< T >::init(), PetscVector< T >::l1_norm(), PetscVector< T >::l2_norm(), PetscVector< T >::linfty_norm(), PetscVector< T >::localize(), PetscVector< T >::localize_to_one(), PetscVector< T >::max(), PetscVector< T >::min(), PetscVector< T >::operator+=(), PetscVector< T >::operator-=(), PetscVector< T >::operator=(), PetscVector< T >::pointwise_mult(), PetscVector< T >::print_matlab(), PetscVector< T >::scale(), PetscVector< T >::set(), PetscVector< T >::sum(), and PetscVector< T >::zero().

01199 {
01200   libmesh_assert (this->initialized());
01201   if(_array_is_present)
01202     {
01203       int ierr=0;
01204       if(this->type() != GHOSTED)
01205         {
01206           ierr = VecRestoreArray (_vec, &_values);
01207           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01208           _values = NULL;
01209         }
01210       else
01211         {
01212           ierr = VecRestoreArray (_local_form, &_values);
01213           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01214           _values = NULL;
01215           ierr = VecGhostRestoreLocalForm (_vec,&_local_form);
01216           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01217           _local_form = NULL;
01218 #ifndef NDEBUG
01219           _local_size = 0;
01220 #endif
01221         }
01222       _array_is_present = false;
01223     }
01224 }

template<typename T >
void PetscVector< T >::abs (  )  [inline, virtual]

v = abs(v)... that is, each entry in v is replaced by its absolute value.

Implements NumericVector< T >.

Definition at line 458 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, and NumericVector< T >::type().

00459 {
00460   this->_restore_array();
00461 
00462   int ierr = 0;
00463 
00464   if(this->type() != GHOSTED)
00465     {
00466       ierr = VecAbs(_vec);
00467       CHKERRABORT(libMesh::COMM_WORLD,ierr);  
00468     }
00469   else
00470     {
00471       Vec loc_vec;
00472       ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00473       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00474 
00475       ierr = VecAbs(loc_vec);
00476       CHKERRABORT(libMesh::COMM_WORLD,ierr);  
00477 
00478       ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00479       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00480     }
00481 }

template<typename T >
void PetscVector< T >::add ( const T  a,
const NumericVector< T > &  v 
) [inline, virtual]

$ U+=a*V $ . Simple vector addition, equal to the operator +=.

Implements NumericVector< T >.

Definition at line 315 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, PetscVector< T >::size(), and NumericVector< T >::type().

00316 {
00317   this->_restore_array();
00318 
00319   int ierr = 0;
00320   PetscScalar a = static_cast<PetscScalar>(a_in);
00321 
00322   // Make sure the NumericVector passed in is really a PetscVector
00323   const PetscVector<T>* v = libmesh_cast_ptr<const PetscVector<T>*>(&v_in);
00324   v->_restore_array();
00325 
00326   libmesh_assert(this->size() == v->size());
00327   
00328   if(this->type() != GHOSTED)
00329     {
00330 #if PETSC_VERSION_LESS_THAN(2,3,0)
00331       // 2.2.x & earlier style
00332       ierr = VecAXPY(&a, v->_vec, _vec);
00333       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00334 #else
00335       // 2.3.x & later style
00336       ierr = VecAXPY(_vec, a, v->_vec);
00337       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00338 #endif
00339     }
00340   else
00341     {
00342       Vec loc_vec;
00343       Vec v_loc_vec;
00344       ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00345       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00346       ierr = VecGhostGetLocalForm (v->_vec,&v_loc_vec);
00347       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00348 
00349 #if PETSC_VERSION_LESS_THAN(2,3,0)
00350       // 2.2.x & earlier style
00351       ierr = VecAXPY(&a, v_loc_vec, loc_vec);
00352       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00353 #else
00354       // 2.3.x & later style
00355       ierr = VecAXPY(loc_vec, a, v_loc_vec);
00356       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00357 #endif
00358 
00359       ierr = VecGhostRestoreLocalForm (v->_vec,&v_loc_vec);
00360       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00361       ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00362       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00363     }
00364 }

template<typename T >
void PetscVector< T >::add ( const NumericVector< T > &  V  )  [inline, virtual]

$ U+=V $ . Simple vector addition, equal to the operator +=.

Implements NumericVector< T >.

Definition at line 307 of file petsc_vector.C.

00308 {
00309   this->add (1., v);
00310 }

template<typename T >
void PetscVector< T >::add ( const T  s  )  [inline, virtual]

$U(0-LIBMESH_DIM)+=s$. Addition of s to all components. Note that s is a scalar and not a vector.

Implements NumericVector< T >.

Definition at line 242 of file petsc_vector.C.

References NumericVector< T >::_is_closed, PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, PetscVector< T >::first_local_index(), libMeshEnums::GHOSTED, PetscVector< T >::local_size(), and NumericVector< T >::type().

00243 {
00244   this->_restore_array();
00245 
00246   int ierr=0;
00247   PetscScalar* values;
00248   const PetscScalar v = static_cast<PetscScalar>(v_in);  
00249 
00250   if(this->type() != GHOSTED)
00251     {
00252       const int n   = static_cast<int>(this->local_size());
00253       const int fli = static_cast<int>(this->first_local_index());
00254       
00255       for (int i=0; i<n; i++)
00256         {
00257           ierr = VecGetArray (_vec, &values);
00258           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00259           
00260           int ig = fli + i;      
00261           
00262           PetscScalar value = (values[i] + v);
00263           
00264           ierr = VecRestoreArray (_vec, &values);
00265           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00266           
00267           ierr = VecSetValues (_vec, 1, &ig, &value, INSERT_VALUES);
00268           CHKERRABORT(libMesh::COMM_WORLD,ierr); 
00269         }
00270     }
00271   else
00272     {
00273       /* Vectors that include ghost values require a special
00274          handling.  */
00275       Vec loc_vec;
00276       ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00277       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00278 
00279       int n=0;
00280       ierr = VecGetSize(loc_vec, &n);
00281       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00282       
00283       for (int i=0; i<n; i++)
00284         {
00285           ierr = VecGetArray (loc_vec, &values);
00286           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00287           
00288           PetscScalar value = (values[i] + v);
00289           
00290           ierr = VecRestoreArray (loc_vec, &values);
00291           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00292           
00293           ierr = VecSetValues (loc_vec, 1, &i, &value, INSERT_VALUES);
00294           CHKERRABORT(libMesh::COMM_WORLD,ierr); 
00295         }
00296 
00297       ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00298       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00299     }
00300 
00301   this->_is_closed = false;
00302 }

template<typename T >
void PetscVector< T >::add ( const unsigned int  i,
const T  value 
) [inline, virtual]

v(i) += value

Implements NumericVector< T >.

Definition at line 166 of file petsc_vector.C.

References NumericVector< T >::_is_closed, PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, and PetscVector< T >::size().

00167 {
00168   this->_restore_array();
00169   libmesh_assert(i<size());
00170   
00171   int ierr=0;
00172   int i_val = static_cast<int>(i);
00173   PetscScalar petsc_value = static_cast<PetscScalar>(value);
00174 
00175   ierr = VecSetValues (_vec, 1, &i_val, &petsc_value, ADD_VALUES);
00176          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00177 
00178   this->_is_closed = false;
00179 }

template<typename T >
void NumericVector< T >::add_vector ( const NumericVector< T > &  v,
const ShellMatrix< T > &  a 
) [inline, inherited]

$U+=A*V$, add the product of a ShellMatrix A and a NumericVector V to this, where this=U.

Definition at line 253 of file numeric_vector.C.

References ShellMatrix< T >::vector_mult_add().

00255 {
00256   a.vector_mult_add(*this,v);
00257 }

template<typename T >
void PetscVector< T >::add_vector ( const DenseVector< T > &  V,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$U+=V $ where U and V are type DenseVector<T> and you want to specify WHERE to add the DenseVector<T> V

Implements NumericVector< T >.

Definition at line 230 of file petsc_vector.C.

References DenseVector< T >::size().

00232 {
00233   libmesh_assert (V.size() == dof_indices.size());
00234 
00235   for (unsigned int i=0; i<V.size(); i++)
00236     this->add (dof_indices[i], V(i));
00237 }

template<typename T >
void PetscVector< T >::add_vector ( const NumericVector< T > &  V,
const SparseMatrix< T > &  A 
) [inline, virtual]

$U+=A*V$, add the product of a SparseMatrix A and a NumericVector V to this, where this=U.

Implements NumericVector< T >.

Definition at line 209 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, and libMesh::COMM_WORLD.

00211 {
00212   this->_restore_array();
00213   // Make sure the data passed in are really of Petsc types
00214   const PetscVector<T>* V = libmesh_cast_ptr<const PetscVector<T>*>(&V_in);
00215   const PetscMatrix<T>* A = libmesh_cast_ptr<const PetscMatrix<T>*>(&A_in);
00216 
00217   int ierr=0;
00218 
00219   A->close();
00220 
00221   // The const_cast<> is not elegant, but it is required since PETSc
00222   // is not const-correct.  
00223   ierr = MatMultAdd(const_cast<PetscMatrix<T>*>(A)->mat(), V->_vec, _vec, _vec);
00224          CHKERRABORT(libMesh::COMM_WORLD,ierr); 
00225 }

template<typename T >
void PetscVector< T >::add_vector ( const NumericVector< T > &  V,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$ U+=V $ where U and V are type NumericVector<T> and you want to specify WHERE to add the NumericVector<T> V

Implements NumericVector< T >.

Definition at line 197 of file petsc_vector.C.

References NumericVector< T >::size().

00199 {
00200   libmesh_assert (V.size() == dof_indices.size());
00201 
00202   for (unsigned int i=0; i<V.size(); i++)
00203     this->add (dof_indices[i], V(i));
00204 }

template<typename T >
void PetscVector< T >::add_vector ( const std::vector< T > &  v,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$ U+=v $ where v is a std::vector<T> and you want to specify WHERE to add it

Implements NumericVector< T >.

Definition at line 184 of file petsc_vector.C.

References PetscVector< T >::_restore_array().

00186 {
00187   this->_restore_array();
00188   libmesh_assert (v.size() == dof_indices.size());
00189 
00190   for (unsigned int i=0; i<v.size(); i++)
00191     this->add (dof_indices[i], v[i]);
00192 }

template<typename T >
AutoPtr< NumericVector< T > > NumericVector< T >::build ( const SolverPackage  solver_package = libMesh::default_solver_package()  )  [inline, static, inherited]

Builds a NumericVector using the linear solver package specified by solver_package

Definition at line 41 of file numeric_vector.C.

References LASPACK_SOLVERS, libMeshEnums::PETSC_SOLVERS, and TRILINOS_SOLVERS.

Referenced by ExactErrorEstimator::estimate_error().

00042 {
00043   // Build the appropriate vector
00044   switch (solver_package)
00045     {
00046 
00047 
00048 #ifdef LIBMESH_HAVE_LASPACK
00049     case LASPACK_SOLVERS:
00050       {
00051         AutoPtr<NumericVector<T> > ap(new LaspackVector<T>);
00052         return ap;
00053       }
00054 #endif
00055 
00056 
00057 #ifdef LIBMESH_HAVE_PETSC
00058     case PETSC_SOLVERS:
00059       {
00060         AutoPtr<NumericVector<T> > ap(new PetscVector<T>);
00061         return ap;
00062       }
00063 #endif
00064 
00065 
00066 #ifdef LIBMESH_HAVE_TRILINOS
00067     case TRILINOS_SOLVERS:
00068       {
00069         AutoPtr<NumericVector<T> > ap(new EpetraVector<T>);
00070         return ap;
00071       }
00072 #endif
00073 
00074 
00075     default:
00076       AutoPtr<NumericVector<T> > ap(new DistributedVector<T>);
00077       return ap;
00078     }
00079     
00080   AutoPtr<NumericVector<T> > ap(NULL);
00081   return ap;    
00082 }

template<typename T >
void PetscVector< T >::clear (  )  [inline, virtual]

Returns:
the PetscVector<T> to a pristine state.

Reimplemented from NumericVector< T >.

Definition at line 882 of file petsc_vector.h.

References PetscVector< T >::_destroy_vec_on_exit, PetscVector< T >::_global_to_local_map, NumericVector< T >::_is_closed, NumericVector< T >::_is_initialized, PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, and NumericVector< T >::initialized().

Referenced by PetscVector< T >::init(), and PetscVector< T >::~PetscVector().

00883 {
00884   if (this->initialized())
00885     this->_restore_array();
00886 
00887   if ((this->initialized()) && (this->_destroy_vec_on_exit))
00888     {
00889       int ierr=0;
00890 
00891       ierr = VecDestroy(_vec);
00892              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00893     }
00894 
00895   this->_is_closed = this->_is_initialized = false;
00896 
00897   _global_to_local_map.clear();
00898 }

template<typename T >
AutoPtr< NumericVector< T > > PetscVector< T >::clone (  )  const [inline, virtual]

Creates a copy of this vector and returns it in an AutoPtr.

Implements NumericVector< T >.

Definition at line 962 of file petsc_vector.h.

00963 {
00964   AutoPtr<NumericVector<T> > cloned_vector (new PetscVector<T>);
00965 
00966   cloned_vector->init(*this, true);
00967 
00968   *cloned_vector = *this;
00969 
00970   return cloned_vector;
00971 }

template<typename T >
void PetscVector< T >::close (  )  [inline, virtual]

Call the assemble functions

Implements NumericVector< T >.

Definition at line 856 of file petsc_vector.h.

References NumericVector< T >::_is_closed, PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, and NumericVector< T >::type().

Referenced by SlepcEigenSolver< T >::get_eigenpair(), PetscVector< T >::localize(), PetscVector< T >::operator=(), PetscLinearSolver< T >::solve(), and PetscDiffSolver::solve().

00857 {
00858   this->_restore_array();
00859   
00860   int ierr=0;
00861   
00862   ierr = VecAssemblyBegin(_vec);
00863   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00864   ierr = VecAssemblyEnd(_vec);
00865   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00866 
00867   if(this->type() == GHOSTED)
00868     {
00869       ierr = VecGhostUpdateBegin(_vec,INSERT_VALUES,SCATTER_FORWARD);
00870       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00871       ierr = VecGhostUpdateEnd(_vec,INSERT_VALUES,SCATTER_FORWARD);
00872       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00873     }
00874 
00875   this->_is_closed = true;
00876 }

template<>
int NumericVector< Complex >::compare ( const NumericVector< Complex > &  other_vector,
const Real  threshold 
) const [inline, inherited]

Definition at line 167 of file numeric_vector.C.

References NumericVector< T >::first_local_index(), NumericVector< T >::initialized(), and NumericVector< T >::last_local_index().

00169 {
00170   libmesh_assert (this->initialized());
00171   libmesh_assert (other_vector.initialized());
00172   libmesh_assert (this->first_local_index() == other_vector.first_local_index());
00173   libmesh_assert (this->last_local_index()  == other_vector.last_local_index());
00174 
00175   int rvalue     = -1;
00176   unsigned int i = first_local_index();
00177 
00178   do
00179     {
00180       if (( std::abs( (*this)(i).real() - other_vector(i).real() ) > threshold ) ||
00181           ( std::abs( (*this)(i).imag() - other_vector(i).imag() ) > threshold ))
00182         rvalue = i;
00183       else
00184         i++;
00185     }
00186   while (rvalue==-1 && i<this->last_local_index());
00187 
00188   return rvalue;
00189 }

template<>
int NumericVector< long double >::compare ( const NumericVector< long double > &  other_vector,
const Real  threshold 
) const [inline, inherited]

Definition at line 140 of file numeric_vector.C.

References NumericVector< T >::first_local_index(), NumericVector< T >::initialized(), and NumericVector< T >::last_local_index().

00142 {
00143   libmesh_assert (this->initialized());
00144   libmesh_assert (other_vector.initialized());
00145   libmesh_assert (this->first_local_index() == other_vector.first_local_index());
00146   libmesh_assert (this->last_local_index()  == other_vector.last_local_index());
00147 
00148   int rvalue     = -1;
00149   unsigned int i = first_local_index();
00150 
00151   do
00152     {
00153       if ( std::abs( (*this)(i) - other_vector(i) ) > threshold )
00154         rvalue = i;
00155       else
00156         i++;
00157     }
00158   while (rvalue==-1 && i<last_local_index());
00159 
00160   return rvalue;
00161 }

template<>
int NumericVector< double >::compare ( const NumericVector< double > &  other_vector,
const Real  threshold 
) const [inline, inherited]

Definition at line 114 of file numeric_vector.C.

References NumericVector< T >::first_local_index(), NumericVector< T >::initialized(), and NumericVector< T >::last_local_index().

00116 {
00117   libmesh_assert (this->initialized());
00118   libmesh_assert (other_vector.initialized());
00119   libmesh_assert (this->first_local_index() == other_vector.first_local_index());
00120   libmesh_assert (this->last_local_index()  == other_vector.last_local_index());
00121 
00122   int rvalue     = -1;
00123   unsigned int i = first_local_index();
00124 
00125   do
00126     {
00127       if ( std::abs( (*this)(i) - other_vector(i) ) > threshold )
00128         rvalue = i;
00129       else
00130         i++;
00131     }
00132   while (rvalue==-1 && i<last_local_index());
00133 
00134   return rvalue;
00135 }

template<>
int NumericVector< float >::compare ( const NumericVector< float > &  other_vector,
const Real  threshold 
) const [inline, inherited]

Definition at line 89 of file numeric_vector.C.

References NumericVector< T >::first_local_index(), NumericVector< T >::initialized(), and NumericVector< T >::last_local_index().

00091 {
00092   libmesh_assert (this->initialized());
00093   libmesh_assert (other_vector.initialized());
00094   libmesh_assert (this->first_local_index() == other_vector.first_local_index());
00095   libmesh_assert (this->last_local_index()  == other_vector.last_local_index());
00096 
00097   int rvalue     = -1;
00098   unsigned int i = first_local_index();
00099 
00100   do
00101     {
00102       if ( std::abs( (*this)(i) - other_vector(i) ) > threshold )
00103         rvalue = i;
00104       else
00105         i++;
00106     }
00107   while (rvalue==-1 && i<last_local_index());
00108 
00109   return rvalue;
00110 }

template<typename T >
virtual int NumericVector< T >::compare ( const NumericVector< T > &  other_vector,
const Real  threshold = TOLERANCE 
) const [virtual, inherited]

Returns:
-1 when this is equivalent to other_vector, up to the given threshold. When differences occur, the return value contains the first index where the difference exceeded the threshold. When no threshold is given, the libMesh TOLERANCE is used.

template<typename T >
void PetscVector< T >::create_subvector ( NumericVector< T > &  subvector,
const std::vector< unsigned int > &  rows 
) const [inline, virtual]

Creates a "subvector" from this vector using the rows indices of the "rows" array.

Reimplemented from NumericVector< T >.

Definition at line 1195 of file petsc_vector.C.

References NumericVector< T >::_is_initialized, PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, MeshTools::Generation::Private::idx(), NumericVector< T >::initialized(), and Utility::iota().

01197 {
01198   this->_restore_array();
01199 
01200   // PETSc data structures
01201   IS parent_is, subvector_is;
01202   VecScatter scatter;
01203   int ierr = 0;
01204   
01205   // Make sure the passed in subvector is really a PetscVector
01206   PetscVector<T>* petsc_subvector = libmesh_cast_ptr<PetscVector<T>*>(&subvector);
01207   
01208   // If the petsc_subvector is already initialized, we assume that the
01209   // user has already allocated the *correct* amount of space for it.
01210   // If not, we use the appropriate PETSc routines to initialize it.
01211   if (!petsc_subvector->initialized())
01212     {
01213       // Initialize the petsc_subvector to have enough space to hold
01214       // the entries which will be scattered into it.  Note: such an
01215       // init() function (where we let PETSc decide the number of local
01216       // entries) is not currently offered by the PetscVector
01217       // class.  Should we differentiate here between sequential and
01218       // parallel vector creation based on libMesh::n_processors() ?
01219       ierr = VecCreateMPI(libMesh::COMM_WORLD,
01220                           PETSC_DECIDE,          // n_local
01221                           rows.size(),           // n_global
01222                           &(petsc_subvector->_vec)); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01223 
01224       ierr = VecSetFromOptions (petsc_subvector->_vec); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01225 
01226       // Mark the subvector as initialized
01227       petsc_subvector->_is_initialized = true;
01228     }
01229   else
01230     {
01231       petsc_subvector->_restore_array();
01232     }
01233   
01234   // Use iota to fill an array with entries [0,1,2,3,4,...rows.size()]
01235   std::vector<int> idx(rows.size());
01236   Utility::iota (idx.begin(), idx.end(), 0);
01237 
01238   // Construct index sets
01239   ierr = ISCreateGeneral(libMesh::COMM_WORLD,
01240                          rows.size(),
01241                          (int*) &rows[0],
01242                          &parent_is); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01243 
01244   ierr = ISCreateGeneral(libMesh::COMM_WORLD,
01245                          rows.size(),
01246                          (int*) &idx[0],
01247                          &subvector_is); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01248 
01249   // Construct the scatter object
01250   ierr = VecScatterCreate(this->_vec,
01251                           parent_is,
01252                           petsc_subvector->_vec,
01253                           subvector_is,
01254                           &scatter); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01255 
01256   // Actually perform the scatter
01257 #if PETSC_VERSION_LESS_THAN(2,3,3)
01258   ierr = VecScatterBegin(this->_vec,
01259                          petsc_subvector->_vec,
01260                          INSERT_VALUES,
01261                          SCATTER_FORWARD,
01262                          scatter); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01263 
01264   ierr = VecScatterEnd(this->_vec,
01265                        petsc_subvector->_vec,
01266                        INSERT_VALUES,
01267                        SCATTER_FORWARD,
01268                        scatter); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01269 #else
01270   // API argument order change in PETSc 2.3.3
01271   ierr = VecScatterBegin(scatter,
01272                          this->_vec,
01273                          petsc_subvector->_vec,
01274                          INSERT_VALUES,
01275                          SCATTER_FORWARD); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01276 
01277   ierr = VecScatterEnd(scatter,
01278                        this->_vec,
01279                        petsc_subvector->_vec,
01280                        INSERT_VALUES,
01281                        SCATTER_FORWARD); CHKERRABORT(libMesh::COMM_WORLD,ierr);
01282 #endif
01283   
01284   // Clean up 
01285   ierr = ISDestroy(parent_is);       CHKERRABORT(libMesh::COMM_WORLD,ierr);
01286   ierr = ISDestroy(subvector_is);    CHKERRABORT(libMesh::COMM_WORLD,ierr);
01287   ierr = VecScatterDestroy(scatter); CHKERRABORT(libMesh::COMM_WORLD,ierr); 
01288 
01289 }

template<typename T >
T PetscVector< T >::dot ( const NumericVector< T > &  V  )  const [inline, virtual]

Computes the dot product, p = U.V

Implements NumericVector< T >.

Definition at line 484 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, and libMesh::COMM_WORLD.

00485 {
00486   this->_restore_array();
00487 
00488   // Error flag
00489   int ierr = 0;
00490   
00491   // Return value
00492   PetscScalar value=0.;
00493   
00494   // Make sure the NumericVector passed in is really a PetscVector
00495   const PetscVector<T>* v = libmesh_cast_ptr<const PetscVector<T>*>(&V);
00496 
00497   // 2.3.x (at least) style.  Untested for previous versions.
00498   ierr = VecDot(this->_vec, v->_vec, &value);
00499          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00500 
00501   return static_cast<T>(value);
00502 }

template<typename T >
virtual T NumericVector< T >::el ( const unsigned int  i  )  const [inline, virtual, inherited]

Returns:
the element U(i)

Definition at line 322 of file numeric_vector.h.

00322 { return (*this)(i); }

template<typename T >
unsigned int PetscVector< T >::first_local_index (  )  const [inline, virtual]

Returns:
the index of the first vector element actually stored on this processor

Implements NumericVector< T >.

Definition at line 1012 of file petsc_vector.h.

References PetscVector< T >::_vec, libMesh::COMM_WORLD, and NumericVector< T >::initialized().

Referenced by PetscVector< T >::add(), PetscVector< T >::localize(), PetscVector< T >::localize_to_one(), and PetscVector< T >::operator=().

01013 {
01014   libmesh_assert (this->initialized());
01015   
01016   int ierr=0, petsc_first=0, petsc_last=0;
01017   
01018   ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
01019          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01020   
01021   return static_cast<unsigned int>(petsc_first);
01022 }

template<typename T >
void PetscVector< T >::get ( const std::vector< unsigned int > &  index,
std::vector< T > &  values 
) const [inline, virtual]

Access multiple components at once. Overloaded method that should be faster (probably much faster) than calling operator() individually for each index.

Reimplemented from NumericVector< T >.

Definition at line 1088 of file petsc_vector.h.

References PetscVector< T >::_get_array(), PetscVector< T >::_local_size, PetscVector< T >::_values, libMeshEnums::GHOSTED, PetscVector< T >::map_global_to_local_index(), and NumericVector< T >::type().

01089 {
01090   this->_get_array();
01091 
01092   const unsigned int num = index.size();
01093   values.resize(num);
01094 
01095   for(unsigned int i=0; i<num; i++)
01096     {
01097       const unsigned int local_index = this->map_global_to_local_index(index[i]);
01098 #ifndef NDEBUG
01099       if(this->type() == GHOSTED)
01100         {
01101           libmesh_assert(local_index<_local_size);
01102         }
01103 #endif
01104       values[i] = static_cast<T>(_values[local_index]);
01105     }
01106 }

std::string ReferenceCounter::get_info (  )  [static, inherited]

Gets a string containing the reference information.

Definition at line 45 of file reference_counter.C.

References ReferenceCounter::_counts, and QuadratureRules::name().

Referenced by ReferenceCounter::print_info().

00046 {
00047 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00048 
00049   std::ostringstream out;
00050   
00051   out << '\n'
00052       << " ---------------------------------------------------------------------------- \n"
00053       << "| Reference count information                                                |\n"
00054       << " ---------------------------------------------------------------------------- \n";
00055   
00056   for (Counts::iterator it = _counts.begin();
00057        it != _counts.end(); ++it)
00058     {
00059       const std::string name(it->first);
00060       const unsigned int creations    = it->second.first;
00061       const unsigned int destructions = it->second.second;
00062 
00063       out << "| " << name << " reference count information:\n"
00064           << "|  Creations:    " << creations    << '\n'
00065           << "|  Destructions: " << destructions << '\n';
00066     }
00067   
00068   out << " ---------------------------------------------------------------------------- \n";
00069 
00070   return out.str();
00071 
00072 #else
00073 
00074   return "";
00075   
00076 #endif
00077 }

void ReferenceCounter::increment_constructor_count ( const std::string &  name  )  [inline, protected, inherited]

Increments the construction counter. Should be called in the constructor of any derived class that will be reference counted.

Definition at line 149 of file reference_counter.h.

References ReferenceCounter::_counts, and Threads::spin_mtx.

Referenced by ReferenceCountedObject< SparseMatrix< T > >::ReferenceCountedObject().

00150 {
00151   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00152   std::pair<unsigned int, unsigned int>& p = _counts[name];
00153 
00154   p.first++;
00155 }

void ReferenceCounter::increment_destructor_count ( const std::string &  name  )  [inline, protected, inherited]

Increments the destruction counter. Should be called in the destructor of any derived class that will be reference counted.

Definition at line 167 of file reference_counter.h.

References ReferenceCounter::_counts, and Threads::spin_mtx.

Referenced by ReferenceCountedObject< SparseMatrix< T > >::~ReferenceCountedObject().

00168 {
00169   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00170   std::pair<unsigned int, unsigned int>& p = _counts[name];
00171 
00172   p.second++;
00173 }

template<typename T >
void PetscVector< T >::init ( const NumericVector< T > &  other,
const bool  fast = false 
) [inline, virtual]

Creates a vector that has the same dimension and storage type as other, including ghost dofs.

Implements NumericVector< T >.

Definition at line 820 of file petsc_vector.h.

References PetscVector< T >::_global_to_local_map, NumericVector< T >::_is_closed, NumericVector< T >::_is_initialized, PetscVector< T >::_restore_array(), NumericVector< T >::_type, PetscVector< T >::_vec, PetscVector< T >::clear(), libMesh::COMM_WORLD, NumericVector< T >::initialized(), PetscVector< T >::size(), and PetscVector< T >::zero().

00822 {
00823   // Clear initialized vectors 
00824   if (this->initialized())
00825     this->clear();
00826 
00827   const PetscVector<T>& v = libmesh_cast_ref<const PetscVector<T>&>(other);
00828 
00829   // Other vector should restore array.
00830   if(v.initialized())
00831     {
00832       v._restore_array();
00833     }
00834 
00835   this->_global_to_local_map = v._global_to_local_map;
00836   this->_is_closed      = v._is_closed;
00837   this->_is_initialized = v._is_initialized;
00838   this->_type = v._type;
00839 
00840   if (v.size() != 0)
00841     {
00842       int ierr = 0;
00843 
00844       ierr = VecDuplicate (v._vec, &this->_vec);
00845       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00846     }
00847   
00848   if (fast == false)
00849     this->zero ();
00850 }

template<typename T >
void PetscVector< T >::init ( const unsigned int  n,
const unsigned int  n_local,
const std::vector< unsigned int > &  ghost,
const bool  fast = false,
const ParallelType  type = AUTOMATIC 
) [inline, virtual]

Create a vector that holds tha local indices plus those specified in the ghost argument.

Implements NumericVector< T >.

Definition at line 767 of file petsc_vector.h.

References PetscVector< T >::_global_to_local_map, NumericVector< T >::_is_initialized, NumericVector< T >::_type, PetscVector< T >::_vec, libMeshEnums::AUTOMATIC, PetscVector< T >::clear(), libMesh::COMM_WORLD, libMeshEnums::GHOSTED, NumericVector< T >::initialized(), and PetscVector< T >::zero().

00772 {
00773   int ierr=0;
00774   int petsc_n=static_cast<int>(n);
00775   int petsc_n_local=static_cast<int>(n_local);
00776   int petsc_n_ghost=static_cast<int>(ghost.size());
00777 
00778   // If the mesh is disjoint, the following assertion will fail.
00779   // If the mesh is not disjoint, every processor will either have
00780   // all the dofs, none of the dofs, or some non-zero dofs at the
00781   // boundary between processors.
00782   libmesh_assert(n_local == 0 || n_local == n || !ghost.empty());
00783 
00784   int* petsc_ghost = ghost.empty() ? PETSC_NULL :
00785     const_cast<int*>(reinterpret_cast<const int*>(&ghost[0]));
00786 
00787   // Clear initialized vectors 
00788   if (this->initialized())
00789     this->clear();
00790 
00791   libmesh_assert(type == AUTOMATIC || type == GHOSTED);
00792   this->_type = GHOSTED;
00793 
00794   /* Make the global-to-local ghost cell map.  */
00795   for(unsigned int i=0; i<ghost.size(); i++)
00796     {
00797       _global_to_local_map[ghost[i]] = i;
00798     }
00799 
00800   /* Create vector.  */
00801   ierr = VecCreateGhost (libMesh::COMM_WORLD, petsc_n_local, petsc_n,
00802                          petsc_n_ghost, petsc_ghost,
00803                          &_vec);
00804   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00805   
00806   ierr = VecSetFromOptions (_vec);
00807   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00808   
00809   this->_is_initialized = true;
00810 //  this->_is_closed = true;
00811   
00812   if (fast == false)
00813     this->zero ();
00814 }

template<typename T >
void PetscVector< T >::init ( const unsigned int  N,
const bool  fast = false,
const ParallelType  type = AUTOMATIC 
) [inline, virtual]

call init with n_local = N,

Implements NumericVector< T >.

Definition at line 756 of file petsc_vector.h.

References PetscVector< T >::init().

00759 {
00760   this->init(n,n,fast,type);
00761 }

template<typename T >
void PetscVector< T >::init ( const unsigned int  N,
const unsigned int  n_local,
const bool  fast = false,
const ParallelType  type = AUTOMATIC 
) [inline, virtual]

Change the dimension of the vector to N. The reserved memory for this vector remains unchanged if possible, to make things faster, but this may waste some memory, so take this in the back of your head. However, if N==0 all memory is freed, i.e. if you want to resize the vector and release the memory not needed, you have to first call init(0) and then init(N). This cited behaviour is analogous to that of the STL containers.

On fast==false, the vector is filled by zeros.

Implements NumericVector< T >.

Definition at line 693 of file petsc_vector.h.

References NumericVector< T >::_is_initialized, NumericVector< T >::_type, PetscVector< T >::_vec, libMeshEnums::AUTOMATIC, PetscVector< T >::clear(), libMesh::COMM_WORLD, NumericVector< T >::initialized(), libMeshEnums::PARALLEL, libMeshEnums::SERIAL, and PetscVector< T >::zero().

Referenced by PetscVector< T >::init(), PetscVector< T >::localize(), and PetscVector< T >::PetscVector().

00697 {
00698   int ierr=0;
00699   int petsc_n=static_cast<int>(n);
00700   int petsc_n_local=static_cast<int>(n_local);
00701 
00702 
00703   // Clear initialized vectors 
00704   if (this->initialized())
00705     this->clear();
00706 
00707   if (type == AUTOMATIC)
00708     {
00709       if (n == n_local)
00710         this->_type = SERIAL;
00711       else
00712         this->_type = PARALLEL;
00713     }
00714   else
00715     this->_type = type;
00716 
00717   libmesh_assert ((this->_type==SERIAL && n==n_local) ||
00718                   this->_type==PARALLEL);
00719   
00720   // create a sequential vector if on only 1 processor 
00721   if (this->_type == SERIAL)
00722     {
00723       ierr = VecCreateSeq (PETSC_COMM_SELF, petsc_n, &_vec);
00724              CHKERRABORT(PETSC_COMM_SELF,ierr);
00725       
00726       ierr = VecSetFromOptions (_vec);
00727              CHKERRABORT(PETSC_COMM_SELF,ierr);
00728     }
00729   // otherwise create an MPI-enabled vector
00730   else if (this->_type == PARALLEL)
00731     {
00732       libmesh_assert (n_local <= n);
00733       
00734       ierr = VecCreateMPI (libMesh::COMM_WORLD, petsc_n_local, petsc_n,
00735                            &_vec);
00736              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00737       
00738       ierr = VecSetFromOptions (_vec);
00739              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00740     }  
00741   else
00742     libmesh_error();
00743   
00744   this->_is_initialized = true;
00745 //  this->_is_closed = true;
00746   
00747   
00748   if (fast == false)
00749     this->zero ();
00750 }

template<typename T >
virtual bool NumericVector< T >::initialized (  )  const [inline, virtual, inherited]

Returns:
true if the vector has been initialized, false otherwise.

Definition at line 109 of file numeric_vector.h.

Referenced by PetscVector< T >::_get_array(), PetscVector< T >::_restore_array(), LaspackVector< T >::abs(), DistributedVector< T >::abs(), LaspackVector< T >::add(), DistributedVector< T >::add(), DistributedVector< T >::add_vector(), EpetraVector< T >::clear(), PetscVector< T >::clear(), LaspackVector< T >::clear(), EpetraVector< T >::close(), LaspackVector< T >::close(), DistributedVector< T >::close(), NumericVector< T >::compare(), PetscVector< T >::create_subvector(), LaspackVector< T >::dot(), EpetraVector< T >::first_local_index(), PetscVector< T >::first_local_index(), LaspackVector< T >::first_local_index(), DistributedVector< T >::first_local_index(), PetscVector< T >::init(), LaspackVector< T >::init(), DistributedVector< T >::init(), DistributedVector< T >::insert(), DistributedVector< T >::l1_norm(), DistributedVector< T >::l2_norm(), EpetraVector< T >::last_local_index(), PetscVector< T >::last_local_index(), LaspackVector< T >::last_local_index(), DistributedVector< T >::last_local_index(), DistributedVector< T >::linfty_norm(), EpetraVector< T >::local_size(), PetscVector< T >::local_size(), LaspackVector< T >::local_size(), DistributedVector< T >::local_size(), DistributedVector< T >::localize(), DistributedVector< T >::localize_to_one(), PetscVector< T >::map_global_to_local_index(), EpetraVector< T >::max(), LaspackVector< T >::max(), DistributedVector< T >::max(), EpetraVector< T >::min(), LaspackVector< T >::min(), DistributedVector< T >::min(), EpetraVector< T >::operator()(), LaspackVector< T >::operator()(), DistributedVector< T >::operator()(), DistributedVector< T >::operator+=(), DistributedVector< T >::operator-=(), LaspackVector< T >::operator=(), DistributedVector< T >::operator=(), NumericVector< T >::print(), NumericVector< T >::print_global(), LaspackVector< T >::scale(), DistributedVector< T >::scale(), LaspackVector< T >::set(), DistributedVector< T >::set(), EpetraVector< T >::size(), PetscVector< T >::size(), LaspackVector< T >::size(), DistributedVector< T >::size(), DistributedVector< T >::sum(), EpetraVector< T >::zero(), LaspackVector< T >::zero(), and DistributedVector< T >::zero().

00109 { return _is_initialized; }

template<typename T >
void PetscVector< T >::insert ( const DenseSubVector< T > &  V,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$ U=V $ where V is type DenseSubVector<T> and you want to specify WHERE to insert it

Implements NumericVector< T >.

Definition at line 405 of file petsc_vector.C.

References DenseVectorBase< T >::size().

00407 {
00408   libmesh_assert (V.size() == dof_indices.size());
00409 
00410   for (unsigned int i=0; i<V.size(); i++)
00411     this->set (dof_indices[i], V(i));
00412 }

template<typename T >
void PetscVector< T >::insert ( const DenseVector< T > &  V,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$ U=V $ where V is type DenseVector<T> and you want to specify WHERE to insert it

Implements NumericVector< T >.

Definition at line 393 of file petsc_vector.C.

References DenseVector< T >::size().

00395 {
00396   libmesh_assert (V.size() == dof_indices.size());
00397 
00398   for (unsigned int i=0; i<V.size(); i++)
00399     this->set (dof_indices[i], V(i));
00400 }

template<typename T >
void PetscVector< T >::insert ( const NumericVector< T > &  V,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$U=V$, where U and V are type NumericVector<T> and you want to specify WHERE to insert the NumericVector<T> V

Implements NumericVector< T >.

Definition at line 381 of file petsc_vector.C.

References NumericVector< T >::size().

00383 {
00384   libmesh_assert (V.size() == dof_indices.size());
00385 
00386   for (unsigned int i=0; i<V.size(); i++)
00387     this->set (dof_indices[i], V(i));
00388 }

template<typename T >
void PetscVector< T >::insert ( const std::vector< T > &  v,
const std::vector< unsigned int > &  dof_indices 
) [inline, virtual]

$ U=v $ where v is a std::vector<T> and you want to specify WHERE to insert it

Implements NumericVector< T >.

Definition at line 369 of file petsc_vector.C.

00371 {
00372   libmesh_assert (v.size() == dof_indices.size());
00373 
00374   for (unsigned int i=0; i<v.size(); i++)
00375     this->set (dof_indices[i], v[i]);
00376 }

template<typename T >
Real PetscVector< T >::l1_norm (  )  const [inline, virtual]

Returns:
the $l_1$-norm of the vector, i.e. the sum of the absolute values.

Implements NumericVector< T >.

Definition at line 67 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, NumericVector< T >::closed(), and libMesh::COMM_WORLD.

00068 {
00069   this->_restore_array();
00070   libmesh_assert(this->closed());
00071   
00072   int ierr=0;
00073   PetscReal value=0.;
00074   
00075   ierr = VecNorm (_vec, NORM_1, &value);
00076          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00077   
00078   return static_cast<Real>(value);
00079 }

template<typename T >
Real PetscVector< T >::l2_norm (  )  const [inline, virtual]

Returns:
the $l_2$-norm of the vector, i.e. the square root of the sum of the squares of the elements.

Implements NumericVector< T >.

Definition at line 84 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, NumericVector< T >::closed(), and libMesh::COMM_WORLD.

00085 {
00086   this->_restore_array();
00087   libmesh_assert(this->closed());
00088   
00089   int ierr=0;
00090   PetscReal value=0.;
00091   
00092   ierr = VecNorm (_vec, NORM_2, &value);
00093          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00094   
00095   return static_cast<Real>(value);
00096 }

template<typename T >
unsigned int PetscVector< T >::last_local_index (  )  const [inline, virtual]

Returns:
the index of the last vector element actually stored on this processor

Implements NumericVector< T >.

Definition at line 1028 of file petsc_vector.h.

References PetscVector< T >::_vec, libMesh::COMM_WORLD, and NumericVector< T >::initialized().

01029 {
01030   libmesh_assert (this->initialized());
01031   
01032   int ierr=0, petsc_first=0, petsc_last=0;
01033   
01034   ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
01035          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01036   
01037   return static_cast<unsigned int>(petsc_last);
01038 }

template<typename T >
Real PetscVector< T >::linfty_norm (  )  const [inline, virtual]

Returns:
the maximum absolute value of the elements of this vector, which is the $l_\infty$-norm of a vector.

Implements NumericVector< T >.

Definition at line 102 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, NumericVector< T >::closed(), and libMesh::COMM_WORLD.

00103 {
00104   this->_restore_array();
00105   libmesh_assert(this->closed());
00106   
00107   int ierr=0;
00108   PetscReal value=0.;
00109   
00110   ierr = VecNorm (_vec, NORM_INFINITY, &value);
00111          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00112   
00113   return static_cast<Real>(value);
00114 }

template<typename T >
unsigned int PetscVector< T >::local_size (  )  const [inline, virtual]

Returns:
the local size of the vector (index_stop-index_start)

Implements NumericVector< T >.

Definition at line 996 of file petsc_vector.h.

References PetscVector< T >::_vec, libMesh::COMM_WORLD, and NumericVector< T >::initialized().

Referenced by PetscVector< T >::_get_array(), PetscVector< T >::add(), PetscVector< T >::localize(), PetscVector< T >::localize_to_one(), PetscVector< T >::operator=(), and PetscVector< T >::PetscVector().

00997 {
00998   libmesh_assert (this->initialized());
00999   
01000   int ierr=0, petsc_size=0;
01001   
01002   ierr = VecGetLocalSize(_vec, &petsc_size);
01003          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01004   
01005   return static_cast<unsigned int>(petsc_size);
01006 }

template<typename T >
void PetscVector< T >::localize ( const unsigned int  first_local_idx,
const unsigned int  last_local_idx,
const std::vector< unsigned int > &  send_list 
) [inline, virtual]

Updates a local vector with selected values from neighboring processors, as defined by send_list.

Implements NumericVector< T >.

Definition at line 813 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, PetscVector< T >::close(), libMesh::COMM_WORLD, MeshTools::Generation::Private::idx(), PetscVector< T >::init(), Utility::iota(), PetscVector< T >::local_size(), PetscVector< T >::localize(), libMeshEnums::PARALLEL, and PetscVector< T >::size().

00816 {
00817   this->_restore_array();
00818 
00819   // Only good for serial vectors.
00820   // libmesh_assert (this->size() == this->local_size());
00821   libmesh_assert (last_local_idx > first_local_idx);
00822   libmesh_assert (send_list.size() <= this->size());
00823   libmesh_assert (last_local_idx < this->size());
00824   
00825   const unsigned int size       = this->size();
00826   const unsigned int local_size = (last_local_idx - first_local_idx + 1);
00827   int ierr=0;  
00828   
00829   // Don't bother for serial cases
00830   if ((first_local_idx == 0) &&
00831       (local_size == size))
00832     return;
00833   
00834   
00835   // Build a parallel vector, initialize it with the local
00836   // parts of (*this)
00837   PetscVector<T> parallel_vec;
00838 
00839   parallel_vec.init (size, local_size, true, PARALLEL);
00840 
00841 
00842   // Copy part of *this into the parallel_vec
00843   {
00844     IS is;
00845     VecScatter scatter;
00846 
00847     // Create idx, idx[i] = i+first_local_idx;
00848     std::vector<int> idx(local_size);
00849     Utility::iota (idx.begin(), idx.end(), first_local_idx);
00850 
00851     // Create the index set & scatter object
00852     ierr = ISCreateGeneral(libMesh::COMM_WORLD, local_size, &idx[0], &is); 
00853            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00854 
00855     ierr = VecScatterCreate(_vec,              is,
00856                             parallel_vec._vec, is,
00857                             &scatter);
00858            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00859 
00860     // Perform the scatter
00861 #if PETSC_VERSION_LESS_THAN(2,3,3)
00862 
00863     ierr = VecScatterBegin(_vec, parallel_vec._vec, INSERT_VALUES,
00864                            SCATTER_FORWARD, scatter);
00865            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00866   
00867     ierr = VecScatterEnd  (_vec, parallel_vec._vec, INSERT_VALUES,
00868                            SCATTER_FORWARD, scatter);
00869            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00870 
00871 #else
00872            
00873       // API argument order change in PETSc 2.3.3
00874     ierr = VecScatterBegin(scatter, _vec, parallel_vec._vec,
00875                            INSERT_VALUES, SCATTER_FORWARD);
00876            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00877   
00878     ierr = VecScatterEnd  (scatter, _vec, parallel_vec._vec,
00879                            INSERT_VALUES, SCATTER_FORWARD);
00880            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00881            
00882 #endif
00883 
00884     // Clean up
00885     ierr = ISDestroy (is);
00886            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00887   
00888     ierr = VecScatterDestroy(scatter);
00889            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00890   }
00891 
00892   // localize like normal
00893   parallel_vec.close();
00894   parallel_vec.localize (*this, send_list);
00895   this->close();
00896 }

template<typename T >
void PetscVector< T >::localize ( NumericVector< T > &  v_local,
const std::vector< unsigned int > &  send_list 
) const [inline, virtual]

Creates a local vector v_local containing only information relevant to this processor, as defined by the send_list.

Implements NumericVector< T >.

Definition at line 734 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, PetscVector< T >::close(), libMesh::COMM_WORLD, PetscVector< T >::first_local_index(), libMeshEnums::GHOSTED, MeshTools::Generation::Private::idx(), PetscVector< T >::local_size(), PetscVector< T >::size(), and NumericVector< T >::type().

00736 {
00737   this->_restore_array();
00738 
00739   // Make sure the NumericVector passed in is really a PetscVector
00740   PetscVector<T>* v_local = libmesh_cast_ptr<PetscVector<T>*>(&v_local_in);
00741 
00742   libmesh_assert (v_local != NULL);
00743   libmesh_assert (v_local->size() == this->size());
00744   libmesh_assert (send_list.size()     <= v_local->size());
00745   
00746   int ierr=0;
00747   const unsigned int n_sl = send_list.size();
00748 
00749   IS is;
00750   VecScatter scatter;
00751 
00752   std::vector<int> idx(n_sl + this->local_size());
00753   
00754   for (unsigned int i=0; i<n_sl; i++)
00755     idx[i] = static_cast<int>(send_list[i]);
00756   for (unsigned int i = 0; i != this->local_size(); ++i)
00757     idx[n_sl+i] = i + this->first_local_index();
00758   
00759   // Create the index set & scatter object
00760   if (idx.empty())
00761     ierr = ISCreateGeneral(libMesh::COMM_WORLD,
00762                            n_sl+this->local_size(), PETSC_NULL, &is);
00763   else
00764     ierr = ISCreateGeneral(libMesh::COMM_WORLD,
00765                            n_sl+this->local_size(), &idx[0], &is);
00766            CHKERRABORT(libMesh::COMM_WORLD,ierr);
00767 
00768   ierr = VecScatterCreate(_vec,          is,
00769                           v_local->_vec, is,
00770                           &scatter);
00771          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00772 
00773   
00774   // Perform the scatter
00775 #if PETSC_VERSION_LESS_THAN(2,3,3)
00776          
00777   ierr = VecScatterBegin(_vec, v_local->_vec, INSERT_VALUES,
00778                          SCATTER_FORWARD, scatter);
00779          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00780   
00781   ierr = VecScatterEnd  (_vec, v_local->_vec, INSERT_VALUES,
00782                          SCATTER_FORWARD, scatter);
00783          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00784 
00785 #else
00786          
00787   // API argument order change in PETSc 2.3.3
00788   ierr = VecScatterBegin(scatter, _vec, v_local->_vec,
00789                          INSERT_VALUES, SCATTER_FORWARD);
00790          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00791   
00792   ierr = VecScatterEnd  (scatter, _vec, v_local->_vec,
00793                          INSERT_VALUES, SCATTER_FORWARD);
00794          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00795 
00796 #endif
00797          
00798 
00799   // Clean up
00800   ierr = ISDestroy (is);
00801          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00802   
00803   ierr = VecScatterDestroy(scatter);
00804          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00805 
00806   // Make sure ghost dofs are up to date
00807   if (v_local->type() == GHOSTED)
00808     v_local->close();
00809 }

template<typename T >
void PetscVector< T >::localize ( NumericVector< T > &  v_local  )  const [inline, virtual]

Same, but fills a NumericVector<T> instead of a std::vector.

Implements NumericVector< T >.

Definition at line 670 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, PetscVector< T >::close(), libMesh::COMM_WORLD, libMeshEnums::GHOSTED, MeshTools::Generation::Private::idx(), Utility::iota(), PetscVector< T >::size(), and NumericVector< T >::type().

00671 {
00672   this->_restore_array();
00673 
00674   // Make sure the NumericVector passed in is really a PetscVector
00675   PetscVector<T>* v_local = libmesh_cast_ptr<PetscVector<T>*>(&v_local_in);
00676 
00677   libmesh_assert (v_local != NULL);
00678   libmesh_assert (v_local->size() == this->size());
00679 
00680   int ierr = 0;
00681   const int n = this->size();
00682 
00683   IS is;
00684   VecScatter scatter;
00685 
00686   // Create idx, idx[i] = i;
00687   std::vector<int> idx(n); Utility::iota (idx.begin(), idx.end(), 0);
00688 
00689   // Create the index set & scatter object
00690   ierr = ISCreateGeneral(libMesh::COMM_WORLD, n, &idx[0], &is);
00691          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00692 
00693   ierr = VecScatterCreate(_vec,          is,
00694                           v_local->_vec, is,
00695                           &scatter);
00696          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00697 
00698   // Perform the scatter
00699 #if PETSC_VERSION_LESS_THAN(2,3,3)
00700          
00701   ierr = VecScatterBegin(_vec, v_local->_vec, INSERT_VALUES,
00702                          SCATTER_FORWARD, scatter);
00703          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00704   
00705   ierr = VecScatterEnd  (_vec, v_local->_vec, INSERT_VALUES,
00706                          SCATTER_FORWARD, scatter);
00707          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00708 #else
00709   // API argument order change in PETSc 2.3.3
00710   ierr = VecScatterBegin(scatter, _vec, v_local->_vec,
00711                          INSERT_VALUES, SCATTER_FORWARD);
00712          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00713   
00714   ierr = VecScatterEnd  (scatter, _vec, v_local->_vec,
00715                          INSERT_VALUES, SCATTER_FORWARD);
00716          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00717 #endif
00718 
00719   // Clean up
00720   ierr = ISDestroy (is);
00721          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00722   
00723   ierr = VecScatterDestroy(scatter);
00724          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00725 
00726   // Make sure ghost dofs are up to date
00727   if (v_local->type() == GHOSTED)
00728     v_local->close();
00729 }

template<typename T >
void PetscVector< T >::localize ( std::vector< T > &  v_local  )  const [inline, virtual]

Creates a copy of the global vector in the local vector v_local.

Implements NumericVector< T >.

Definition at line 901 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, PetscVector< T >::first_local_index(), PetscVector< T >::local_size(), PetscVector< T >::size(), and PetscVector< T >::sum().

Referenced by PetscVector< T >::localize().

00902 {
00903   this->_restore_array();
00904 
00905   // This function must be run on all processors at once
00906   parallel_only();
00907 
00908   int ierr=0;
00909   const int n = this->size();
00910   const int nl = this->local_size();
00911   PetscScalar *values;
00912 
00913   v_local.clear();
00914   v_local.resize(n, 0.);
00915 
00916   ierr = VecGetArray (_vec, &values);
00917          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00918 
00919   unsigned int ioff = first_local_index();
00920 
00921   for (int i=0; i<nl; i++)
00922     v_local[i+ioff] = static_cast<T>(values[i]);
00923 
00924   ierr = VecRestoreArray (_vec, &values);
00925          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00926 
00927   Parallel::sum(v_local);
00928 }

template<>
void PetscVector< Complex >::localize_to_one ( std::vector< Complex > &  v_local,
const unsigned int  pid 
) const [inline]

Definition at line 993 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, PetscVector< T >::first_local_index(), PetscVector< T >::local_size(), and PetscVector< T >::size().

00995 {
00996   this->_restore_array();
00997 
00998   int ierr=0;
00999   const int n  = size();
01000   const int nl = local_size();
01001   PetscScalar *values;
01002 
01003   
01004   v_local.resize(n);
01005 
01006   
01007   for (int i=0; i<n; i++)
01008     v_local[i] = 0.;
01009   
01010   // only one processor
01011   if (n == nl)
01012     {      
01013       ierr = VecGetArray (_vec, &values);
01014              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01015 
01016       for (int i=0; i<n; i++)
01017         v_local[i] = static_cast<Complex>(values[i]);
01018 
01019       ierr = VecRestoreArray (_vec, &values);
01020              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01021     }
01022 
01023   // otherwise multiple processors
01024   else
01025     {
01026       unsigned int ioff = this->first_local_index();
01027 
01028       /* in here the local values are stored, acting as send buffer for MPI
01029        * initialize to zero, since we collect using MPI_SUM
01030        */
01031       std::vector<Real> real_local_values(n, 0.);
01032       std::vector<Real> imag_local_values(n, 0.);
01033 
01034       {
01035         ierr = VecGetArray (_vec, &values);
01036                CHKERRABORT(libMesh::COMM_WORLD,ierr);
01037         
01038         // provide my local share to the real and imag buffers
01039         for (int i=0; i<nl; i++)
01040           {
01041             real_local_values[i+ioff] = static_cast<Complex>(values[i]).real();
01042             imag_local_values[i+ioff] = static_cast<Complex>(values[i]).imag();
01043           }
01044 
01045         ierr = VecRestoreArray (_vec, &values);
01046                CHKERRABORT(libMesh::COMM_WORLD,ierr);
01047       }
01048    
01049       /* have buffers of the real and imaginary part of v_local.
01050        * Once MPI_Reduce() collected all the real and imaginary
01051        * parts in these std::vector<double>, the values can be 
01052        * copied to v_local
01053        */
01054       std::vector<Real> real_v_local(n);
01055       std::vector<Real> imag_v_local(n);
01056 
01057       // collect entries from other proc's in real_v_local, imag_v_local
01058       MPI_Reduce (&real_local_values[0], &real_v_local[0], n, 
01059                   MPI_DOUBLE, MPI_SUM,
01060                   pid, libMesh::COMM_WORLD);    
01061 
01062       MPI_Reduce (&imag_local_values[0], &imag_v_local[0], n, 
01063                   MPI_DOUBLE, MPI_SUM,
01064                   pid, libMesh::COMM_WORLD);    
01065 
01066       // copy real_v_local and imag_v_local to v_local
01067       for (int i=0; i<n; i++)
01068         v_local[i] = Complex(real_v_local[i], imag_v_local[i]);
01069     }  
01070 }

template<>
void PetscVector< Real >::localize_to_one ( std::vector< Real > &  v_local,
const unsigned int  pid 
) const [inline]

Definition at line 936 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, PetscVector< T >::first_local_index(), PetscVector< T >::local_size(), and PetscVector< T >::size().

00938 {
00939   this->_restore_array();
00940 
00941   int ierr=0;
00942   const int n  = size();
00943   const int nl = local_size();
00944   PetscScalar *values;
00945 
00946   
00947   v_local.resize(n);
00948 
00949   
00950   // only one processor
00951   if (n == nl)
00952     {      
00953       ierr = VecGetArray (_vec, &values);
00954              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00955 
00956       for (int i=0; i<n; i++)
00957         v_local[i] = static_cast<Real>(values[i]);
00958 
00959       ierr = VecRestoreArray (_vec, &values);
00960              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00961     }
00962 
00963   // otherwise multiple processors
00964   else
00965     {
00966       unsigned int ioff = this->first_local_index();
00967       std::vector<Real> local_values (n, 0.);
00968       
00969       {
00970         ierr = VecGetArray (_vec, &values);
00971                CHKERRABORT(libMesh::COMM_WORLD,ierr);
00972         
00973         for (int i=0; i<nl; i++)
00974           local_values[i+ioff] = static_cast<Real>(values[i]);
00975         
00976         ierr = VecRestoreArray (_vec, &values);
00977                CHKERRABORT(libMesh::COMM_WORLD,ierr);
00978       }
00979       
00980 
00981       MPI_Reduce (&local_values[0], &v_local[0], n, MPI_REAL, MPI_SUM,
00982                   pid, libMesh::COMM_WORLD);
00983     }
00984 }

template<typename T >
void PetscVector< T >::localize_to_one ( std::vector< T > &  v_local,
const unsigned int  proc_id = 0 
) const [virtual]

Creates a local copy of the global vector in v_local only on processor proc_id. By default the data is sent to processor 0. This method is useful for outputting data from one processor.

Implements NumericVector< T >.

template<typename T >
unsigned int PetscVector< T >::map_global_to_local_index ( const unsigned int  i  )  const [inline]

Maps the global index i to the corresponding global index. If the index is not a ghost cell, this is done by subtraction the number of the first local index. If it is a ghost cell, it has to be looked up in the map.

Definition at line 1044 of file petsc_vector.h.

References PetscVector< T >::_global_to_local_map, PetscVector< T >::_vec, libMesh::COMM_WORLD, and NumericVector< T >::initialized().

Referenced by PetscVector< T >::get(), and PetscVector< T >::operator()().

01045 {
01046   libmesh_assert (this->initialized());
01047 
01048   int ierr=0, petsc_first=0, petsc_last=0;
01049   ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
01050   CHKERRABORT(libMesh::COMM_WORLD,ierr);
01051   const unsigned int first = static_cast<unsigned int>(petsc_first);
01052   const unsigned int last = static_cast<unsigned int>(petsc_last);
01053 
01054   if((i>=first) && (i<last))
01055     {
01056       return i-first;
01057     }
01058 
01059   GlobalToLocalMap::const_iterator it = _global_to_local_map.find(i);
01060   libmesh_assert (it!=_global_to_local_map.end());
01061   return it->second+last-first;
01062 }

template<typename T >
Real PetscVector< T >::max (  )  const [inline, virtual]

Returns:
the maximum element in the vector. In case of complex numbers, this returns the maximum Real part.

Implements NumericVector< T >.

Definition at line 1130 of file petsc_vector.h.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, and libMesh::COMM_WORLD.

01131 {
01132   this->_restore_array();
01133 
01134   int index=0, ierr=0;
01135   PetscReal max=0.;
01136 
01137   ierr = VecMax (_vec, &index, &max);
01138          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01139 
01140   // this return value is correct: VecMax returns a PetscReal
01141   return static_cast<Real>(max);
01142 }

template<typename T >
Real PetscVector< T >::min (  )  const [inline, virtual]

Returns:
the minimum element in the vector. In case of complex numbers, this returns the minimum Real part.

Implements NumericVector< T >.

Definition at line 1112 of file petsc_vector.h.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, and libMesh::COMM_WORLD.

01113 {
01114   this->_restore_array();
01115 
01116   int index=0, ierr=0;
01117   PetscReal min=0.;
01118 
01119   ierr = VecMin (_vec, &index, &min);
01120          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01121 
01122   // this return value is correct: VecMin returns a PetscReal
01123   return static_cast<Real>(min);
01124 }

static unsigned int ReferenceCounter::n_objects (  )  [inline, static, inherited]

Prints the number of outstanding (created, but not yet destroyed) objects.

Definition at line 76 of file reference_counter.h.

References ReferenceCounter::_n_objects.

00077   { return _n_objects; }

template<typename T >
T PetscVector< T >::operator() ( const unsigned int  i  )  const [inline, virtual]

Access components, returns U(i).

Implements NumericVector< T >.

Definition at line 1068 of file petsc_vector.h.

References PetscVector< T >::_get_array(), PetscVector< T >::_local_size, PetscVector< T >::_values, libMeshEnums::GHOSTED, PetscVector< T >::map_global_to_local_index(), and NumericVector< T >::type().

01069 {
01070   this->_get_array();
01071 
01072   const unsigned int local_index = this->map_global_to_local_index(i);
01073 
01074 #ifndef NDEBUG
01075   if(this->type() == GHOSTED)
01076     {
01077       libmesh_assert(local_index<_local_size);
01078     }
01079 #endif
01080   
01081   return static_cast<T>(_values[local_index]);
01082 }

template<typename T >
NumericVector<T>& NumericVector< T >::operator*= ( const T  a  )  [inline, inherited]

Multiplication operator. Equivalent to U.scale(a)

Definition at line 348 of file numeric_vector.h.

00348 { this->scale(a); return *this; }

template<typename T >
NumericVector< T > & PetscVector< T >::operator+= ( const NumericVector< T > &  V  )  [inline, virtual]

Addition operator. Fast equivalent to U.add(1, V).

Implements NumericVector< T >.

Definition at line 121 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), and NumericVector< T >::closed().

00122 {
00123   this->_restore_array();
00124   libmesh_assert(this->closed());
00125   
00126   this->add(1., v);
00127   
00128   return *this;
00129 }

template<typename T >
NumericVector< T > & PetscVector< T >::operator-= ( const NumericVector< T > &  V  )  [inline, virtual]

Subtraction operator. Fast equivalent to U.add(-1, V).

Implements NumericVector< T >.

Definition at line 135 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), and NumericVector< T >::closed().

00136 {
00137   this->_restore_array();
00138   libmesh_assert(this->closed());
00139   
00140   this->add(-1., v);
00141   
00142   return *this;
00143 }

template<typename T >
NumericVector<T>& NumericVector< T >::operator/= ( const T  a  )  [inline, inherited]

Division operator. Equivalent to U.scale(1./a)

Definition at line 354 of file numeric_vector.h.

00354 { this->scale(1./a); return *this; }

template<typename T >
NumericVector< T > & PetscVector< T >::operator= ( const std::vector< T > &  v  )  [inline, virtual]

$U = V$: copy all components.

Case 1: The vector is the same size of The global vector. Only add the local components.

Case 2: The vector is the same size as our local piece. Insert directly to the local piece.

Implements NumericVector< T >.

Definition at line 617 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, PetscVector< T >::close(), libMesh::COMM_WORLD, PetscVector< T >::first_local_index(), libMeshEnums::GHOSTED, PetscVector< T >::local_size(), PetscVector< T >::size(), and NumericVector< T >::type().

00618 {
00619   this->_restore_array();
00620 
00621   const unsigned int nl   = this->local_size();
00622   const unsigned int ioff = this->first_local_index();
00623   int ierr=0;
00624   PetscScalar* values;
00625       
00630   if (this->size() == v.size())
00631     {
00632       ierr = VecGetArray (_vec, &values);
00633              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00634 
00635       for (unsigned int i=0; i<nl; i++)
00636         values[i] =  static_cast<PetscScalar>(v[i+ioff]);
00637       
00638       ierr = VecRestoreArray (_vec, &values);
00639              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00640     }
00641 
00646   else
00647     {
00648       libmesh_assert (this->local_size() == v.size());
00649 
00650       ierr = VecGetArray (_vec, &values);
00651              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00652 
00653       for (unsigned int i=0; i<nl; i++)
00654         values[i] = static_cast<PetscScalar>(v[i]);
00655       
00656       ierr = VecRestoreArray (_vec, &values);
00657              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00658     }
00659 
00660   // Make sure ghost dofs are up to date
00661   if (this->type() == GHOSTED)
00662     this->close();
00663 
00664   return *this;
00665 }

template<typename T >
PetscVector< T > & PetscVector< T >::operator= ( const PetscVector< T > &  V  )  [inline]

$U = V$: copy all components.

Definition at line 573 of file petsc_vector.C.

References PetscVector< T >::_global_to_local_map, PetscVector< T >::_restore_array(), NumericVector< T >::_type, PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, PetscVector< T >::local_size(), PetscVector< T >::size(), and NumericVector< T >::type().

00574 {
00575   this->_restore_array();
00576   v._restore_array();
00577   libmesh_assert (this->_type == v._type);
00578   libmesh_assert (this->size() == v.size());
00579   libmesh_assert (this->local_size() == v.local_size());
00580   libmesh_assert (this->_global_to_local_map ==
00581                   v._global_to_local_map);
00582 
00583   if (v.size() != 0)
00584     {
00585       int ierr = 0;
00586       if(this->type() != GHOSTED)
00587         {
00588           ierr = VecCopy (v._vec, this->_vec);
00589           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00590         }
00591       else
00592         {
00593           Vec loc_vec;
00594           Vec v_loc_vec;
00595           ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00596           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00597           ierr = VecGhostGetLocalForm (v._vec,&v_loc_vec);
00598           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00599 
00600           ierr = VecCopy (v_loc_vec, loc_vec);
00601           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00602 
00603           ierr = VecGhostRestoreLocalForm (v._vec,&v_loc_vec);
00604           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00605           ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00606           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00607         }
00608     }
00609   
00610   return *this;
00611 }

template<typename T >
NumericVector< T > & PetscVector< T >::operator= ( const NumericVector< T > &  V  )  [inline, virtual]

$U = V$: copy all components.

Implements NumericVector< T >.

Definition at line 559 of file petsc_vector.C.

00560 {
00561   // Make sure the NumericVector passed in is really a PetscVector
00562   const PetscVector<T>* v = libmesh_cast_ptr<const PetscVector<T>*>(&v_in);
00563 
00564   *this = *v;
00565   
00566   return *this;
00567 }

template<typename T >
NumericVector< T > & PetscVector< T >::operator= ( const T  s  )  [inline, virtual]

Change the dimension to that of the vector V. The same applies as for the other init function.

The elements of V are not copied, i.e. this function is the same as calling init(V.size(),fast). $U(0-N) = s$: fill all components.

Implements NumericVector< T >.

Definition at line 509 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, NumericVector< T >::closed(), libMesh::COMM_WORLD, libMeshEnums::GHOSTED, PetscVector< T >::size(), and NumericVector< T >::type().

00510 {
00511   this->_restore_array();
00512   libmesh_assert(this->closed());
00513 
00514   int ierr = 0;
00515   PetscScalar s = static_cast<PetscScalar>(s_in);
00516 
00517   if (this->size() != 0)
00518     {
00519       if(this->type() != GHOSTED)
00520         {
00521 #if PETSC_VERSION_LESS_THAN(2,3,0)
00522           // 2.2.x & earlier style
00523           ierr = VecSet(&s, _vec);
00524           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00525 #else
00526           // 2.3.x & later style         
00527           ierr = VecSet(_vec, s);
00528           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00529 #endif
00530         }
00531       else
00532         {
00533           Vec loc_vec;
00534           ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00535           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00536 
00537 #if PETSC_VERSION_LESS_THAN(2,3,0)
00538           // 2.2.x & earlier style
00539           ierr = VecSet(&s, loc_vec);
00540           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00541 #else
00542           // 2.3.x & later style         
00543           ierr = VecSet(loc_vec, s);
00544           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00545 #endif
00546 
00547           ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00548           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00549         }
00550     }
00551   
00552   return *this;
00553 }

template<typename T >
void PetscVector< T >::pointwise_mult ( const NumericVector< T > &  vec1,
const NumericVector< T > &  vec2 
) [inline, virtual]

Computes the pointwise (i.e. component-wise) product of vec1 and vec2 and stores the result in *this.

Implements NumericVector< T >.

Definition at line 1077 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, NumericVector< T >::type(), and PetscVector< T >::vec().

01079 {
01080   this->_restore_array();
01081 
01082   int ierr = 0;
01083 
01084   // Convert arguments to PetscVector*.
01085   const PetscVector<T>* vec1_petsc = libmesh_cast_ptr<const PetscVector<T>*>(&vec1);
01086   const PetscVector<T>* vec2_petsc = libmesh_cast_ptr<const PetscVector<T>*>(&vec2);
01087 
01088   // Call PETSc function.
01089 
01090 #if PETSC_VERSION_LESS_THAN(2,3,1)
01091 
01092   std::cout << "This method has been developed with PETSc 2.3.1.  "
01093             << "No one has made it backwards compatible with older "
01094             << "versions of PETSc so far; however, it might work "
01095             << "without any change with some older version." << std::endl;
01096   libmesh_error();
01097 
01098 #else
01099   
01100   if(this->type() != GHOSTED)
01101     {
01102       ierr = VecPointwiseMult(this->vec(),
01103                               const_cast<PetscVector<T>*>(vec1_petsc)->vec(),
01104                               const_cast<PetscVector<T>*>(vec2_petsc)->vec());
01105       CHKERRABORT(libMesh::COMM_WORLD,ierr);
01106     }
01107   else
01108     {
01109           Vec loc_vec;
01110           Vec v1_loc_vec;
01111           Vec v2_loc_vec;
01112           ierr = VecGhostGetLocalForm (_vec,&loc_vec);
01113           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01114           ierr = VecGhostGetLocalForm (const_cast<PetscVector<T>*>(vec1_petsc)->vec(),&v1_loc_vec);
01115           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01116           ierr = VecGhostGetLocalForm (const_cast<PetscVector<T>*>(vec2_petsc)->vec(),&v2_loc_vec);
01117           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01118 
01119           ierr = VecPointwiseMult(loc_vec,v1_loc_vec,v2_loc_vec);
01120           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01121 
01122           ierr = VecGhostRestoreLocalForm (const_cast<PetscVector<T>*>(vec1_petsc)->vec(),&v1_loc_vec);
01123           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01124           ierr = VecGhostRestoreLocalForm (const_cast<PetscVector<T>*>(vec2_petsc)->vec(),&v2_loc_vec);
01125           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01126           ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
01127           CHKERRABORT(libMesh::COMM_WORLD,ierr);
01128     }
01129       
01130 #endif
01131 
01132 }

template<>
void NumericVector< Complex >::print ( std::ostream &  os  )  const [inline, inherited]

Definition at line 739 of file numeric_vector.h.

References NumericVector< T >::first_local_index(), NumericVector< T >::initialized(), NumericVector< T >::last_local_index(), NumericVector< T >::local_size(), and NumericVector< T >::size().

00740 {
00741   libmesh_assert (this->initialized());
00742   os << "Size\tglobal =  " << this->size()
00743      << "\t\tlocal =  " << this->local_size() << std::endl;
00744   
00745   // std::complex<>::operator<<() is defined, but use this form
00746   os << "#\tReal part\t\tImaginary part" << std::endl;
00747   for (unsigned int i=this->first_local_index(); i<this->last_local_index(); i++)
00748     os << i << "\t" 
00749        << (*this)(i).real() << "\t\t" 
00750        << (*this)(i).imag() << std::endl;
00751 }

template<typename T >
void NumericVector< T >::print ( std::ostream &  os = std::cout  )  const [inline, virtual, inherited]

Prints the local contents of the vector to the screen.

Definition at line 757 of file numeric_vector.h.

References NumericVector< T >::first_local_index(), NumericVector< T >::initialized(), NumericVector< T >::last_local_index(), NumericVector< T >::local_size(), and NumericVector< T >::size().

00758 {
00759   libmesh_assert (this->initialized());
00760   os << "Size\tglobal =  " << this->size()
00761      << "\t\tlocal =  " << this->local_size() << std::endl;
00762 
00763   os << "#\tValue" << std::endl;
00764   for (unsigned int i=this->first_local_index(); i<this->last_local_index(); i++)
00765     os << i << "\t" << (*this)(i) << std::endl;
00766 }

template<>
void NumericVector< Complex >::print_global ( std::ostream &  os  )  const [inline, inherited]

Definition at line 772 of file numeric_vector.h.

References NumericVector< T >::initialized(), NumericVector< T >::localize(), libMesh::processor_id(), and NumericVector< T >::size().

00773 {
00774   libmesh_assert (this->initialized());
00775 
00776   std::vector<Complex> v(this->size());
00777   this->localize(v);
00778 
00779   // Right now we only want one copy of the output
00780   if (libMesh::processor_id())
00781     return;
00782   
00783   os << "Size\tglobal =  " << this->size() << std::endl;
00784   os << "#\tReal part\t\tImaginary part" << std::endl;
00785   for (unsigned int i=0; i!=v.size(); i++)
00786     os << i << "\t" 
00787        << v[i].real() << "\t\t" 
00788        << v[i].imag() << std::endl;
00789 }

template<typename T >
void NumericVector< T >::print_global ( std::ostream &  os = std::cout  )  const [inline, virtual, inherited]

Prints the global contents of the vector to the screen.

Definition at line 794 of file numeric_vector.h.

References NumericVector< T >::initialized(), NumericVector< T >::localize(), libMesh::processor_id(), and NumericVector< T >::size().

00795 {
00796   libmesh_assert (this->initialized());
00797 
00798   std::vector<T> v(this->size());
00799   this->localize(v);
00800 
00801   // Right now we only want one copy of the output
00802   if (libMesh::processor_id())
00803     return;
00804 
00805   os << "Size\tglobal =  " << this->size() << std::endl;
00806   os << "#\tValue" << std::endl;
00807   for (unsigned int i=0; i!=v.size(); i++)
00808     os << i << "\t" << v[i] << std::endl;
00809 }

void ReferenceCounter::print_info (  )  [static, inherited]

Prints the reference information to std::cout.

Definition at line 83 of file reference_counter.C.

References ReferenceCounter::get_info().

00084 {
00085 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00086   
00087   std::cout << ReferenceCounter::get_info();
00088   
00089 #endif
00090 }

template<typename T >
void PetscVector< T >::print_matlab ( const std::string  name = "NULL"  )  const [inline, virtual]

Print the contents of the vector in Matlab format. Optionally prints the matrix to the file named name. If name is not specified it is dumped to the screen.

Create an ASCII file containing the matrix if a filename was provided.

Otherwise the matrix will be dumped to the screen.

Destroy the viewer.

Reimplemented from NumericVector< T >.

Definition at line 1137 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, NumericVector< T >::closed(), and libMesh::COMM_WORLD.

01138 {
01139   this->_restore_array();
01140   libmesh_assert (this->closed());
01141   
01142   int ierr=0; 
01143   PetscViewer petsc_viewer;
01144 
01145 
01146   ierr = PetscViewerCreate (libMesh::COMM_WORLD,
01147                             &petsc_viewer);
01148          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01149 
01154   if (name != "NULL")
01155     {
01156       ierr = PetscViewerASCIIOpen( libMesh::COMM_WORLD,
01157                                    name.c_str(),
01158                                    &petsc_viewer);
01159              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01160       
01161       ierr = PetscViewerSetFormat (petsc_viewer,
01162                                    PETSC_VIEWER_ASCII_MATLAB);
01163              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01164   
01165       ierr = VecView (_vec, petsc_viewer);
01166              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01167     }
01168 
01172   else
01173     {
01174       ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
01175                                    PETSC_VIEWER_ASCII_MATLAB);
01176              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01177   
01178       ierr = VecView (_vec, PETSC_VIEWER_STDOUT_WORLD);
01179              CHKERRABORT(libMesh::COMM_WORLD,ierr);
01180     }
01181 
01182 
01186   ierr = PetscViewerDestroy (petsc_viewer);
01187          CHKERRABORT(libMesh::COMM_WORLD,ierr);
01188 }

template<typename T >
void PetscVector< T >::scale ( const T  factor  )  [inline, virtual]

Scale each element of the vector by the given factor.

Implements NumericVector< T >.

Definition at line 417 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, and NumericVector< T >::type().

00418 {
00419   this->_restore_array();
00420 
00421   int ierr = 0;
00422   PetscScalar factor = static_cast<PetscScalar>(factor_in);
00423   
00424   if(this->type() != GHOSTED)
00425     {
00426 #if PETSC_VERSION_LESS_THAN(2,3,0)
00427       // 2.2.x & earlier style
00428       ierr = VecScale(&factor, _vec);
00429       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00430 #else
00431       // 2.3.x & later style     
00432       ierr = VecScale(_vec, factor);
00433       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00434 #endif
00435     }
00436   else
00437     {
00438       Vec loc_vec;
00439       ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00440       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00441 
00442 #if PETSC_VERSION_LESS_THAN(2,3,0)
00443       // 2.2.x & earlier style
00444       ierr = VecScale(&factor, loc_vec);
00445       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00446 #else
00447       // 2.3.x & later style     
00448       ierr = VecScale(loc_vec, factor);
00449       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00450 #endif
00451 
00452       ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00453       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00454     }
00455 }

template<typename T >
void PetscVector< T >::set ( const unsigned int  i,
const T  value 
) [inline, virtual]

v(i) = value

Implements NumericVector< T >.

Definition at line 148 of file petsc_vector.C.

References NumericVector< T >::_is_closed, PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, and PetscVector< T >::size().

00149 {
00150   this->_restore_array();
00151   libmesh_assert(i<size());
00152   
00153   int ierr=0;
00154   int i_val = static_cast<int>(i);
00155   PetscScalar petsc_value = static_cast<PetscScalar>(value);
00156 
00157   ierr = VecSetValues (_vec, 1, &i_val, &petsc_value, INSERT_VALUES);
00158          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00159 
00160   this->_is_closed = false;
00161 }

template<typename T >
unsigned int PetscVector< T >::size (  )  const [inline, virtual]

Returns:
dimension of the vector. This function was formerly called n(), but was renamed to get the PetscVector<T> class closer to the C++ standard library's std::vector container.

Implements NumericVector< T >.

Definition at line 977 of file petsc_vector.h.

References PetscVector< T >::_vec, libMesh::COMM_WORLD, and NumericVector< T >::initialized().

Referenced by PetscVector< T >::add(), PetscVector< T >::init(), PetscVector< T >::localize(), PetscVector< T >::localize_to_one(), PetscVector< T >::operator=(), and PetscVector< T >::set().

00978 {
00979   libmesh_assert (this->initialized());
00980   
00981   int ierr=0, petsc_size=0;
00982   
00983   if (!this->initialized())
00984     return 0;
00985   
00986   ierr = VecGetSize(_vec, &petsc_size);
00987          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00988 
00989   return static_cast<unsigned int>(petsc_size);
00990 }

template<class T >
Real NumericVector< T >::subset_l1_norm ( const std::set< unsigned int > &  indices  )  [inline, virtual, inherited]

Returns:
the $l_1$-norm of the vector, i.e. the sum of the absolute values for the specified entries in the vector.
Note that the indices must necessary live on this processor.

Definition at line 193 of file numeric_vector.C.

References NumericVector< T >::sum().

Referenced by System::discrete_var_norm().

00194 {
00195   NumericVector<T> & v = *this;
00196   
00197   std::set<unsigned int>::iterator it = indices.begin();
00198   const std::set<unsigned int>::iterator it_end = indices.end();
00199 
00200   Real norm = 0;
00201   
00202   for(; it!=it_end; ++it)
00203     norm += std::abs(v(*it));
00204 
00205   Parallel::sum(norm);
00206 
00207   return norm;
00208 }

template<class T >
Real NumericVector< T >::subset_l2_norm ( const std::set< unsigned int > &  indices  )  [inline, virtual, inherited]

Returns:
the $l_2$-norm of the vector, i.e. the square root of the sum of the squares of the elements for the specified entries in the vector.
Note that the indices must necessary live on this processor.

Definition at line 211 of file numeric_vector.C.

References libmesh_norm(), and NumericVector< T >::sum().

Referenced by System::discrete_var_norm().

00212 {
00213   NumericVector<T> & v = *this;
00214 
00215   std::set<unsigned int>::iterator it = indices.begin();
00216   const std::set<unsigned int>::iterator it_end = indices.end();
00217 
00218   Real norm = 0;
00219   
00220   for(; it!=it_end; ++it)
00221     norm += libmesh_norm(v(*it));
00222 
00223   Parallel::sum(norm);
00224 
00225   return std::sqrt(norm);
00226 }

template<class T >
Real NumericVector< T >::subset_linfty_norm ( const std::set< unsigned int > &  indices  )  [inline, virtual, inherited]

Returns:
the maximum absolute value of the specified entries of this vector, which is the $l_\infty$-norm of a vector.
Note that the indices must necessary live on this processor.

Definition at line 229 of file numeric_vector.C.

References NumericVector< T >::abs(), and NumericVector< T >::max().

Referenced by System::discrete_var_norm().

00230 {
00231   NumericVector<T> & v = *this;
00232 
00233   std::set<unsigned int>::iterator it = indices.begin();
00234   const std::set<unsigned int>::iterator it_end = indices.end();
00235 
00236   Real norm = 0;
00237   
00238   for(; it!=it_end; ++it)
00239     {
00240       Real value = std::abs(v(*it));
00241       if(value > norm)
00242         norm = value;
00243     }
00244 
00245   Parallel::max(norm);
00246 
00247   return norm;
00248 }

template<typename T >
T PetscVector< T >::sum (  )  const [inline, virtual]

Returns:
the sum of values in a vector

Implements NumericVector< T >.

Definition at line 51 of file petsc_vector.C.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, NumericVector< T >::closed(), and libMesh::COMM_WORLD.

Referenced by PetscVector< T >::localize().

00052 {
00053   this->_restore_array();
00054   libmesh_assert(this->closed());
00055   
00056   int ierr=0;
00057   PetscScalar value=0.;
00058   
00059   ierr = VecSum (_vec, &value);
00060          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00061   
00062   return static_cast<T>(value);
00063 }

template<typename T >
void PetscVector< T >::swap ( NumericVector< T > &  v  )  [inline, virtual]

template<typename T >
ParallelType& NumericVector< T >::type (  )  [inline, inherited]

Returns:
the type (SERIAL, PARALLEL, GHOSTED) of the vector.

Definition at line 119 of file numeric_vector.h.

00119 { return _type; }

template<typename T >
Vec PetscVector< T >::vec (  )  [inline]

Returns the raw PETSc vector context pointer. Note this is generally not required in user-level code. Just don't do anything crazy like calling VecDestroy()!

Definition at line 493 of file petsc_vector.h.

References PetscVector< T >::_vec.

Referenced by PetscPreconditioner< T >::apply(), PetscMatrix< T >::get_diagonal(), SlepcEigenSolver< T >::get_eigenpair(), PetscVector< T >::pointwise_mult(), PetscLinearSolver< T >::solve(), and PetscDiffSolver::solve().

00493 { libmesh_assert (_vec != NULL); return _vec; }

template<typename T >
void PetscVector< T >::zero (  )  [inline, virtual]

Set all entries to zero. Equivalent to v = 0, but more obvious and faster.

Implements NumericVector< T >.

Definition at line 904 of file petsc_vector.h.

References PetscVector< T >::_restore_array(), PetscVector< T >::_vec, libMesh::COMM_WORLD, libMeshEnums::GHOSTED, and NumericVector< T >::type().

Referenced by PetscVector< T >::init().

00905 {
00906   this->_restore_array();
00907   
00908   int ierr=0;
00909 
00910   PetscScalar z=0.;
00911 
00912   if(this->type() != GHOSTED)
00913     {
00914 #if PETSC_VERSION_LESS_THAN(2,3,0)  
00915       // 2.2.x & earlier style
00916       ierr = VecSet (&z, _vec);
00917       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00918 #else
00919       // 2.3.x & newer
00920       ierr = VecSet (_vec, z);
00921       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00922 #endif
00923     }
00924   else
00925     {
00926       /* Vectors that include ghost values require a special
00927          handling.  */
00928       Vec loc_vec;
00929       ierr = VecGhostGetLocalForm (_vec,&loc_vec);
00930       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00931 #if PETSC_VERSION_LESS_THAN(2,3,0)  
00932       // 2.2.x & earlier style
00933       ierr = VecSet (&z, loc_vec);
00934       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00935 #else
00936       // 2.3.x & newer
00937       ierr = VecSet (loc_vec, z);
00938       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00939 #endif
00940       ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
00941       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00942     }
00943 }

template<typename T >
AutoPtr< NumericVector< T > > PetscVector< T >::zero_clone (  )  const [inline, virtual]

Creates a vector which has the same type, size and partitioning as this vector, but whose data is all zero. Returns it in an AutoPtr.

Implements NumericVector< T >.

Definition at line 949 of file petsc_vector.h.

00950 {
00951   AutoPtr<NumericVector<T> > cloned_vector (new PetscVector<T>);
00952 
00953   cloned_vector->init(*this);
00954 
00955   return cloned_vector;
00956 }


Friends And Related Function Documentation

template<typename T >
std::ostream& operator<< ( std::ostream &  os,
const NumericVector< T > &  v 
) [friend, inherited]

Same as above but allows you to use stream syntax.

Definition at line 545 of file numeric_vector.h.

00546   {
00547     v.print_global(os);
00548     return os;
00549   }


Member Data Documentation

template<typename T >
bool PetscVector< T >::_array_is_present [mutable, private]

If true, the actual Petsc array of the values of the vector is currently accessible. That means that the members _local_form and _values are valid.

Definition at line 510 of file petsc_vector.h.

Referenced by PetscVector< T >::_get_array(), PetscVector< T >::_restore_array(), and PetscVector< T >::swap().

template<typename T >
bool PetscVector< T >::_destroy_vec_on_exit [private]

This boolean value should only be set to false for the constructor which takes a PETSc Vec object.

Definition at line 561 of file petsc_vector.h.

Referenced by PetscVector< T >::clear(), and PetscVector< T >::swap().

template<typename T >
GlobalToLocalMap PetscVector< T >::_global_to_local_map [private]

Map that maps global to local ghost cells (will be empty if not in ghost cell mode).

Definition at line 555 of file petsc_vector.h.

Referenced by PetscVector< T >::clear(), PetscVector< T >::init(), PetscVector< T >::map_global_to_local_index(), PetscVector< T >::operator=(), PetscVector< T >::PetscVector(), and PetscVector< T >::swap().

template<typename T >
Vec PetscVector< T >::_local_form [mutable, private]

Petsc vector datatype to hold the local form of a ghosted vector. The contents of this field are only valid if the vector is ghosted and _array_is_present is true.

Definition at line 526 of file petsc_vector.h.

Referenced by PetscVector< T >::_get_array(), PetscVector< T >::_restore_array(), and PetscVector< T >::swap().

template<typename T >
unsigned int PetscVector< T >::_local_size [mutable, private]

Size of the local form, for being used in assertations. The contents of this field are only valid if the vector is ghosted and _array_is_present is true.

Definition at line 518 of file petsc_vector.h.

Referenced by PetscVector< T >::_get_array(), PetscVector< T >::_restore_array(), PetscVector< T >::get(), and PetscVector< T >::operator()().

Threads::spin_mutex ReferenceCounter::_mutex [static, protected, inherited]

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 123 of file reference_counter.h.

Threads::atomic< unsigned int > ReferenceCounter::_n_objects [static, protected, inherited]

The number of objects. Print the reference count information when the number returns to 0.

Definition at line 118 of file reference_counter.h.

Referenced by ReferenceCounter::n_objects(), ReferenceCounter::ReferenceCounter(), and ReferenceCounter::~ReferenceCounter().

template<typename T >
PetscScalar* PetscVector< T >::_values [mutable, private]

Pointer to the actual Petsc array of the values of the vector. This pointer is only valid if _array_is_present is true.

Definition at line 532 of file petsc_vector.h.

Referenced by PetscVector< T >::_get_array(), PetscVector< T >::_restore_array(), PetscVector< T >::get(), PetscVector< T >::operator()(), and PetscVector< T >::swap().


The documentation for this class was generated from the following files:

Site Created By: libMesh Developers
Last modified: November 25 2009 03:44:45.

Hosted By:
SourceForge.net Logo