libMesh
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
libMesh::PetscVector< T > Class Template Referencefinal

This class provides a nice interface to PETSc's Vec object. More...

#include <petsc_vector.h>

Inheritance diagram for libMesh::PetscVector< T >:
[legend]

Public Member Functions

 PetscVector (const Parallel::Communicator &comm_in, const ParallelType type=AUTOMATIC)
 Dummy-Constructor. More...
 
 PetscVector (const Parallel::Communicator &comm_in, const numeric_index_type n, const ParallelType type=AUTOMATIC)
 Constructor. More...
 
 PetscVector (const Parallel::Communicator &comm_in, const numeric_index_type n, const numeric_index_type n_local, const ParallelType type=AUTOMATIC)
 Constructor. More...
 
 PetscVector (const Parallel::Communicator &comm_in, const numeric_index_type N, const numeric_index_type n_local, const std::vector< numeric_index_type > &ghost, const ParallelType type=AUTOMATIC)
 Constructor. More...
 
 PetscVector (Vec v, const Parallel::Communicator &comm_in)
 Constructor. More...
 
PetscVector< T > & operator= (const PetscVector< T > &v)
 Copy assignment operator. More...
 
 PetscVector (PetscVector &&)=delete
 This class manages a C-style struct (Vec) manually, so we don't want to allow any automatic copy/move functions to be generated, and we can't default the destructor. More...
 
 PetscVector (const PetscVector &)=delete
 
PetscVectoroperator= (PetscVector &&)=delete
 
virtual ~PetscVector ()
 
virtual void close () override
 Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across processors. More...
 
virtual void clear () override
 Restores the NumericVector<T> to a pristine state. More...
 
virtual void zero () override
 Set all entries to zero. More...
 
virtual std::unique_ptr< NumericVector< T > > zero_clone () const override
 
virtual std::unique_ptr< NumericVector< T > > clone () const override
 
virtual void init (const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType type=AUTOMATIC) override
 Change the dimension of the vector to N. More...
 
virtual void init (const numeric_index_type N, const bool fast=false, const ParallelType type=AUTOMATIC) override
 Call init() with n_local = N. More...
 
virtual void init (const numeric_index_type N, const numeric_index_type n_local, const std::vector< numeric_index_type > &ghost, const bool fast=false, const ParallelType=AUTOMATIC) override
 Create a vector that holds tha local indices plus those specified in the ghost argument. More...
 
virtual void init (const NumericVector< T > &other, const bool fast=false) override
 Creates a vector that has the same dimension and storage type as other, including ghost dofs. More...
 
virtual NumericVector< T > & operator= (const T s) override
 Sets all entries of the vector to the value s. More...
 
virtual NumericVector< T > & operator= (const NumericVector< T > &v) override
 This looks like a copy assignment operator, but note that, unlike normal copy assignment operators, it is pure virtual. More...
 
virtual NumericVector< T > & operator= (const std::vector< T > &v) override
 Sets (*this)(i) = v(i) for each entry of the vector. More...
 
virtual Real min () const override
 
virtual Real max () const override
 
virtual T sum () const override
 
virtual Real l1_norm () const override
 
virtual Real l2_norm () const override
 
virtual Real linfty_norm () const override
 
virtual numeric_index_type size () const override
 
virtual numeric_index_type local_size () const override
 
virtual numeric_index_type first_local_index () const override
 
virtual numeric_index_type last_local_index () const override
 
numeric_index_type map_global_to_local_index (const numeric_index_type i) const
 
virtual T operator() (const numeric_index_type i) const override
 
virtual void get (const std::vector< numeric_index_type > &index, T *values) const override
 Access multiple components at once. More...
 
PetscScalar * get_array ()
 Get read/write access to the raw PETSc Vector data array. More...
 
const PetscScalar * get_array_read () const
 Get read only access to the raw PETSc Vector data array. More...
 
void restore_array ()
 Restore the data array. More...
 
virtual NumericVector< T > & operator+= (const NumericVector< T > &v) override
 Adds v to *this, $ \vec{u} \leftarrow \vec{u} + \vec{v} $. More...
 
virtual NumericVector< T > & operator-= (const NumericVector< T > &v) override
 Subtracts v from *this, $ \vec{u} \leftarrow \vec{u} - \vec{v} $. More...
 
virtual void reciprocal () override
 Computes the pointwise reciprocal, $ u_i \leftarrow \frac{1}{u_i} \, \forall i$. More...
 
virtual void conjugate () override
 Negates the imaginary component of each entry in the vector. More...
 
virtual void set (const numeric_index_type i, const T value) override
 Sets v(i) = value. More...
 
virtual void add (const numeric_index_type i, const T value) override
 Adds value to each entry of the vector. More...
 
virtual void add (const T s) override
 Adds s to each entry of the vector, $ u_i \leftarrow u_i + s $. More...
 
virtual void add (const NumericVector< T > &v) override
 Adds v to this, $ \vec{u} \leftarrow \vec{u} + \vec{v} $. More...
 
virtual void add (const T a, const NumericVector< T > &v) override
 Vector addition with a scalar multiple, $ \vec{u} \leftarrow \vec{u} + a\vec{v} $. More...
 
virtual void add_vector (const T *v, const std::vector< numeric_index_type > &dof_indices) override
 Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a pointer and each dof_indices[i] specifies where to add value v[i]. More...
 
virtual void add_vector (const NumericVector< T > &v, const SparseMatrix< T > &A) override
 Computes $ \vec{u} \leftarrow \vec{u} + A \vec{v} $, i.e. More...
 
virtual void add_vector_transpose (const NumericVector< T > &v, const SparseMatrix< T > &A) override
 Computes $ \vec{u} \leftarrow \vec{u} + A^T \vec{v} $, i.e. More...
 
void add_vector_conjugate_transpose (const NumericVector< T > &v, const SparseMatrix< T > &A)
 $ U \leftarrow U + A^H v $. More...
 
virtual void insert (const T *v, const std::vector< numeric_index_type > &dof_indices) override
 Inserts the entries of v in *this at the locations specified by v. More...
 
virtual void scale (const T factor) override
 Scale each element of the vector by the given factor. More...
 
virtual NumericVector< T > & operator/= (const NumericVector< T > &v) override
 Computes the pointwise division of this vector's entries by another's, $ u_i \leftarrow \frac{u_i}{v_i} \, \forall i$. More...
 
virtual void abs () override
 Sets $ u_i \leftarrow |u_i| $ for each entry in the vector. More...
 
virtual T dot (const NumericVector< T > &v) const override
 
indefinite_dot (const NumericVector< T > &v) const
 
virtual void localize (std::vector< T > &v_local) const override
 Creates a copy of the global vector in the local vector v_local. More...
 
virtual void localize (NumericVector< T > &v_local) const override
 Same, but fills a NumericVector<T> instead of a std::vector. More...
 
virtual void localize (NumericVector< T > &v_local, const std::vector< numeric_index_type > &send_list) const override
 Creates a local vector v_local containing only information relevant to this processor, as defined by the send_list. More...
 
virtual void localize (std::vector< T > &v_local, const std::vector< numeric_index_type > &indices) const override
 
Fill in the local std::vector "v_local" with the global indices given in "indices". More...
 
virtual void localize (const numeric_index_type first_local_idx, const numeric_index_type last_local_idx, const std::vector< numeric_index_type > &send_list) override
 Updates a local vector with selected values from neighboring processors, as defined by send_list. More...
 
virtual void localize_to_one (std::vector< T > &v_local, const processor_id_type proc_id=0) const override
 Creates a local copy of the global vector in v_local only on processor proc_id. More...
 
virtual void pointwise_mult (const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
 Computes $ u_i \leftarrow u_i v_i $ (summation not implied) i.e. More...
 
virtual void print_matlab (const std::string &name="") const override
 Print the contents of the vector in Matlab's sparse matrix format. More...
 
virtual void create_subvector (NumericVector< T > &subvector, const std::vector< numeric_index_type > &rows) const override
 Fills in subvector from this vector using the indices in rows. More...
 
virtual void swap (NumericVector< T > &v) override
 Swaps the contents of this with v. More...
 
Vec vec ()
 
virtual bool initialized () const
 
ParallelType type () const
 
ParallelTypetype ()
 
virtual bool closed () const
 
virtual Real subset_l1_norm (const std::set< numeric_index_type > &indices) const
 
virtual Real subset_l2_norm (const std::set< numeric_index_type > &indices) const
 
virtual Real subset_linfty_norm (const std::set< numeric_index_type > &indices) const
 
virtual T el (const numeric_index_type i) const
 
void get (const std::vector< numeric_index_type > &index, std::vector< T > &values) const
 Access multiple components at once. More...
 
NumericVector< T > & operator*= (const T a)
 Scales the vector by a, $ \vec{u} \leftarrow a\vec{u} $. More...
 
NumericVector< T > & operator/= (const T a)
 Scales the vector by 1/a, $ \vec{u} \leftarrow \frac{1}{a}\vec{u} $. More...
 
void add_vector (const std::vector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a std::vector and each dof_indices[i] specifies where to add value v[i]. More...
 
virtual void add_vector (const NumericVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a NumericVector and each dof_indices[i] specifies where to add value v(i). More...
 
void add_vector (const DenseVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a DenseVector and each dof_indices[i] specifies where to add value v(i). More...
 
void add_vector (const NumericVector< T > &v, const ShellMatrix< T > &A)
 Computes $ \vec{u} \leftarrow \vec{u} + A \vec{v} $, i.e. More...
 
void insert (const std::vector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Inserts the entries of v in *this at the locations specified by v. More...
 
virtual void insert (const NumericVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Inserts the entries of v in *this at the locations specified by v. More...
 
void insert (const DenseVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Inserts the entries of v in *this at the locations specified by v. More...
 
void insert (const DenseSubVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 Inserts the entries of v in *this at the locations specified by v. More...
 
virtual int compare (const NumericVector< T > &other_vector, const Real threshold=TOLERANCE) const
 
virtual int local_relative_compare (const NumericVector< T > &other_vector, const Real threshold=TOLERANCE) const
 
virtual int global_relative_compare (const NumericVector< T > &other_vector, const Real threshold=TOLERANCE) const
 
virtual void print (std::ostream &os=libMesh::out) const
 Prints the local contents of the vector, by default to libMesh::out. More...
 
template<>
void print (std::ostream &os) const
 
virtual void print_global (std::ostream &os=libMesh::out) const
 Prints the global contents of the vector, by default to libMesh::out. More...
 
template<>
void print_global (std::ostream &os) const
 
const Parallel::Communicatorcomm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static std::unique_ptr< NumericVector< T > > build (const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
 Builds a NumericVector on the processors in communicator comm using the linear solver package specified by solver_package. More...
 
static std::string get_info ()
 Gets a string containing the reference information. More...
 
static void print_info (std::ostream &out=libMesh::out)
 Prints the reference information, by default to libMesh::out. More...
 
static unsigned int n_objects ()
 Prints the number of outstanding (created, but not yet destroyed) objects. More...
 
static void enable_print_counter_info ()
 Methods to enable/disable the reference counter output from print_info() More...
 
static void disable_print_counter_info ()
 

Protected Types

typedef std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
 Data structure to log the information. More...
 

Protected Member Functions

void increment_constructor_count (const std::string &name)
 Increments the construction counter. More...
 
void increment_destructor_count (const std::string &name)
 Increments the destruction counter. More...
 

Protected Attributes

bool _is_closed
 Flag which tracks whether the vector's values are consistent on all processors after insertion or addition of values has occurred on some or all processors. More...
 
bool _is_initialized
 true once init() has been called. More...
 
ParallelType _type
 Type of vector. More...
 
const Parallel::Communicator_communicator
 

Static Protected Attributes

static Counts _counts
 Actually holds the data. More...
 
static Threads::atomic< unsigned int > _n_objects
 The number of objects. More...
 
static Threads::spin_mutex _mutex
 Mutual exclusion object to enable thread-safe reference counting. More...
 
static bool _enable_print_counter
 Flag to control whether reference count information is printed when print_info is called. More...
 

Private Types

typedef std::unordered_map< numeric_index_type, numeric_index_typeGlobalToLocalMap
 Type for map that maps global to local ghost cells. More...
 

Private Member Functions

void _get_array (bool read_only) const
 Queries the array (and the local form if the vector is ghosted) from PETSc. More...
 
void _restore_array () const
 Restores the array (and the local form if the vector is ghosted) to PETSc. More...
 

Private Attributes

Vec _vec
 Actual PETSc vector datatype to hold vector entries. More...
 
std::atomic< bool > _array_is_present
 If true, the actual PETSc array of the values of the vector is currently accessible. More...
 
bool _array_is_present
 
numeric_index_type _first
 First local index. More...
 
numeric_index_type _last
 Last local index. More...
 
numeric_index_type _local_size
 Size of the local values from _get_array() More...
 
Vec _local_form
 PETSc vector datatype to hold the local form of a ghosted vector. More...
 
const PetscScalar * _read_only_values
 Pointer to the actual PETSc array of the values of the vector. More...
 
PetscScalar * _values
 Pointer to the actual PETSc array of the values of the vector. More...
 
std::mutex _petsc_vector_mutex
 Mutex for _get_array and _restore_array. More...
 
Threads::spin_mutex _petsc_vector_mutex
 
GlobalToLocalMap _global_to_local_map
 Map that maps global to local ghost cells (will be empty if not in ghost cell mode). More...
 
bool _destroy_vec_on_exit
 This boolean value should only be set to false for the constructor which takes a PETSc Vec object. More...
 
bool _values_manually_retrieved
 Whether or not the data array has been manually retrieved using get_array() or get_array_read() More...
 
bool _values_read_only
 Whether or not the data array is for read only access. More...
 

Detailed Description

template<typename T>
class libMesh::PetscVector< T >

This class provides a nice interface to PETSc's Vec object.

All overridden virtual functions are documented in numeric_vector.h.

Author
Benjamin S. Kirk
Date
2002 NumericVector interface to PETSc Vec.

Definition at line 64 of file petsc_vector.h.

Member Typedef Documentation

◆ Counts

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

Data structure to log the information.

The log is identified by the class name.

Definition at line 117 of file reference_counter.h.

◆ GlobalToLocalMap

template<typename T>
typedef std::unordered_map<numeric_index_type,numeric_index_type> libMesh::PetscVector< T >::GlobalToLocalMap
private

Type for map that maps global to local ghost cells.

Definition at line 421 of file petsc_vector.h.

Constructor & Destructor Documentation

◆ PetscVector() [1/7]

template<typename T >
libMesh::PetscVector< T >::PetscVector ( const Parallel::Communicator comm_in,
const ParallelType  type = AUTOMATIC 
)
explicit

Dummy-Constructor.

Dimension=0

Definition at line 454 of file petsc_vector.h.

References libMesh::NumericVector< T >::_type.

454  :
455  NumericVector<T>(comm_in, ptype),
456  _array_is_present(false),
457  _first(0),
458  _last(0),
459  _local_form(nullptr),
460  _values(nullptr),
462  _destroy_vec_on_exit(true),
464  _values_read_only(false)
465 {
466  this->_type = ptype;
467 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
numeric_index_type _last
Last local index.
Definition: petsc_vector.h:359
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
bool _values_read_only
Whether or not the data array is for read only access.
Definition: petsc_vector.h:444
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
Vec _local_form
PETSc vector datatype to hold the local form of a ghosted vector.
Definition: petsc_vector.h:371
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
ParallelType _type
Type of vector.
numeric_index_type _first
First local index.
Definition: petsc_vector.h:352
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ PetscVector() [2/7]

template<typename T >
libMesh::PetscVector< T >::PetscVector ( const Parallel::Communicator comm_in,
const numeric_index_type  n,
const ParallelType  type = AUTOMATIC 
)
explicit

Constructor.

Set dimension to n and initialize all elements with zero.

Definition at line 473 of file petsc_vector.h.

References libMesh::PetscVector< T >::init().

475  :
476  NumericVector<T>(comm_in, ptype),
477  _array_is_present(false),
478  _local_form(nullptr),
479  _values(nullptr),
481  _destroy_vec_on_exit(true),
483  _values_read_only(false)
484 {
485  this->init(n, n, false, ptype);
486 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
bool _values_read_only
Whether or not the data array is for read only access.
Definition: petsc_vector.h:444
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
Vec _local_form
PETSc vector datatype to hold the local form of a ghosted vector.
Definition: petsc_vector.h:371
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType type=AUTOMATIC) override
Change the dimension of the vector to N.
Definition: petsc_vector.h:628
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ PetscVector() [3/7]

template<typename T >
libMesh::PetscVector< T >::PetscVector ( const Parallel::Communicator comm_in,
const numeric_index_type  n,
const numeric_index_type  n_local,
const ParallelType  type = AUTOMATIC 
)

Constructor.

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

Definition at line 492 of file petsc_vector.h.

References libMesh::PetscVector< T >::init().

495  :
496  NumericVector<T>(comm_in, ptype),
497  _array_is_present(false),
498  _local_form(nullptr),
499  _values(nullptr),
501  _destroy_vec_on_exit(true),
503  _values_read_only(false)
504 {
505  this->init(n, n_local, false, ptype);
506 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
bool _values_read_only
Whether or not the data array is for read only access.
Definition: petsc_vector.h:444
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
Vec _local_form
PETSc vector datatype to hold the local form of a ghosted vector.
Definition: petsc_vector.h:371
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType type=AUTOMATIC) override
Change the dimension of the vector to N.
Definition: petsc_vector.h:628
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ PetscVector() [4/7]

template<typename T >
libMesh::PetscVector< T >::PetscVector ( const Parallel::Communicator comm_in,
const numeric_index_type  N,
const numeric_index_type  n_local,
const std::vector< numeric_index_type > &  ghost,
const ParallelType  type = AUTOMATIC 
)

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 512 of file petsc_vector.h.

References libMesh::PetscVector< T >::init().

516  :
517  NumericVector<T>(comm_in, ptype),
518  _array_is_present(false),
519  _local_form(nullptr),
520  _values(nullptr),
522  _destroy_vec_on_exit(true),
524  _values_read_only(false)
525 {
526  this->init(n, n_local, ghost, false, ptype);
527 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
bool _values_read_only
Whether or not the data array is for read only access.
Definition: petsc_vector.h:444
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
Vec _local_form
PETSc vector datatype to hold the local form of a ghosted vector.
Definition: petsc_vector.h:371
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType type=AUTOMATIC) override
Change the dimension of the vector to N.
Definition: petsc_vector.h:628
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ PetscVector() [5/7]

template<typename T >
libMesh::PetscVector< T >::PetscVector ( Vec  v,
const Parallel::Communicator comm_in 
)

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 535 of file petsc_vector.h.

References libMesh::PetscVector< T >::_global_to_local_map, libMesh::NumericVector< T >::_is_closed, libMesh::NumericVector< T >::_is_initialized, libMesh::NumericVector< T >::_type, libMesh::PetscVector< T >::_vec, libMesh::GHOSTED, libMesh::PARALLEL, and libMesh::SERIAL.

536  :
537  NumericVector<T>(comm_in, AUTOMATIC),
538  _array_is_present(false),
539  _local_form(nullptr),
540  _values(nullptr),
542  _destroy_vec_on_exit(false),
544  _values_read_only(false)
545 {
546  this->_vec = v;
547  this->_is_closed = true;
548  this->_is_initialized = true;
549 
550  /* We need to ask PETSc about the (local to global) ghost value
551  mapping and create the inverse mapping out of it. */
552  PetscErrorCode ierr=0;
553  PetscInt petsc_local_size=0;
554  ierr = VecGetLocalSize(_vec, &petsc_local_size);
555  LIBMESH_CHKERR(ierr);
556 
557  // Get the vector type from PETSc.
558  // As of PETSc 3.0.0, the VecType #define lost its const-ness, so we
559  // need to have it in the code
560 #if PETSC_VERSION_LESS_THAN(3,0,0) || !PETSC_VERSION_LESS_THAN(3,4,0)
561  // Pre-3.0 and petsc-dev (as of October 2012) use non-const versions
562  VecType ptype;
563 #else
564  const VecType ptype;
565 #endif
566  ierr = VecGetType(_vec, &ptype);
567  LIBMESH_CHKERR(ierr);
568 
569  if ((std::strcmp(ptype,VECSHARED) == 0) || (std::strcmp(ptype,VECMPI) == 0))
570  {
571 #if PETSC_RELEASE_LESS_THAN(3,1,1)
572  ISLocalToGlobalMapping mapping = _vec->mapping;
573 #else
574  ISLocalToGlobalMapping mapping;
575  ierr = VecGetLocalToGlobalMapping(_vec, &mapping);
576  LIBMESH_CHKERR(ierr);
577 #endif
578 
579  // If is a sparsely stored vector, set up our new mapping
580  if (mapping)
581  {
582  const numeric_index_type my_local_size = static_cast<numeric_index_type>(petsc_local_size);
583  const numeric_index_type ghost_begin = static_cast<numeric_index_type>(petsc_local_size);
584 #if PETSC_RELEASE_LESS_THAN(3,4,0)
585  const numeric_index_type ghost_end = static_cast<numeric_index_type>(mapping->n);
586 #else
587  PetscInt n;
588  ierr = ISLocalToGlobalMappingGetSize(mapping, &n);
589  LIBMESH_CHKERR(ierr);
590  const numeric_index_type ghost_end = static_cast<numeric_index_type>(n);
591 #endif
592 #if PETSC_RELEASE_LESS_THAN(3,1,1)
593  const PetscInt * indices = mapping->indices;
594 #else
595  const PetscInt * indices;
596  ierr = ISLocalToGlobalMappingGetIndices(mapping,&indices);
597  LIBMESH_CHKERR(ierr);
598 #endif
599  for (numeric_index_type i=ghost_begin; i<ghost_end; i++)
600  _global_to_local_map[indices[i]] = i-my_local_size;
601  this->_type = GHOSTED;
602 #if !PETSC_RELEASE_LESS_THAN(3,1,1)
603  ierr = ISLocalToGlobalMappingRestoreIndices(mapping, &indices);
604  LIBMESH_CHKERR(ierr);
605 #endif
606  }
607  else
608  this->_type = PARALLEL;
609  }
610  else
611  this->_type = SERIAL;
612 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
bool _is_initialized
true once init() has been called.
bool _values_read_only
Whether or not the data array is for read only access.
Definition: petsc_vector.h:444
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
Vec _local_form
PETSc vector datatype to hold the local form of a ghosted vector.
Definition: petsc_vector.h:371
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
dof_id_type numeric_index_type
Definition: id_types.h:92
ParallelType _type
Type of vector.
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439
bool _is_closed
Flag which tracks whether the vector&#39;s values are consistent on all processors after insertion or add...

◆ PetscVector() [6/7]

template<typename T>
libMesh::PetscVector< T >::PetscVector ( PetscVector< T > &&  )
delete

This class manages a C-style struct (Vec) manually, so we don't want to allow any automatic copy/move functions to be generated, and we can't default the destructor.

◆ PetscVector() [7/7]

template<typename T>
libMesh::PetscVector< T >::PetscVector ( const PetscVector< T > &  )
delete

◆ ~PetscVector()

template<typename T >
libMesh::PetscVector< T >::~PetscVector ( )
virtual

Definition at line 619 of file petsc_vector.h.

620 {
621  this->clear ();
622 }
virtual void clear() override
Restores the NumericVector<T> to a pristine state.
Definition: petsc_vector.h:838

Member Function Documentation

◆ _get_array()

template<typename T>
void libMesh::PetscVector< T >::_get_array ( bool  read_only) const
private

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

Parameters
read_onlyWhether or not a read only copy of the raw data

◆ _restore_array()

template<typename T>
void libMesh::PetscVector< T >::_restore_array ( ) const
private

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

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

◆ abs()

template<typename T>
virtual void libMesh::PetscVector< T >::abs ( )
overridevirtual

Sets $ u_i \leftarrow |u_i| $ for each entry in the vector.

Implements libMesh::NumericVector< T >.

◆ add() [1/4]

template<typename T>
virtual void libMesh::PetscVector< T >::add ( const numeric_index_type  i,
const T  value 
)
overridevirtual

Adds value to each entry of the vector.

Implements libMesh::NumericVector< T >.

◆ add() [2/4]

template<typename T>
virtual void libMesh::PetscVector< T >::add ( const T  s)
overridevirtual

Adds s to each entry of the vector, $ u_i \leftarrow u_i + s $.

Implements libMesh::NumericVector< T >.

◆ add() [3/4]

template<typename T>
virtual void libMesh::PetscVector< T >::add ( const NumericVector< T > &  v)
overridevirtual

Adds v to this, $ \vec{u} \leftarrow \vec{u} + \vec{v} $.

Equivalent to calling operator+=().

Implements libMesh::NumericVector< T >.

◆ add() [4/4]

template<typename T>
virtual void libMesh::PetscVector< T >::add ( const T  a,
const NumericVector< T > &  v 
)
overridevirtual

Vector addition with a scalar multiple, $ \vec{u} \leftarrow \vec{u} + a\vec{v} $.

Equivalent to calling operator+=().

Implements libMesh::NumericVector< T >.

◆ add_vector() [1/6]

template<typename T>
virtual void libMesh::PetscVector< T >::add_vector ( const T *  v,
const std::vector< numeric_index_type > &  dof_indices 
)
overridevirtual

Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a pointer and each dof_indices[i] specifies where to add value v[i].

This should be overridden in subclasses for efficiency.

Reimplemented from libMesh::NumericVector< T >.

◆ add_vector() [2/6]

template<typename T>
virtual void libMesh::PetscVector< T >::add_vector ( const NumericVector< T > &  v,
const SparseMatrix< T > &  A 
)
overridevirtual

Computes $ \vec{u} \leftarrow \vec{u} + A \vec{v} $, i.e.

adds the product of a SparseMatrix A and a NumericVector v to this.

Implements libMesh::NumericVector< T >.

◆ add_vector() [3/6]

template<typename T>
void libMesh::NumericVector< T >::add_vector ( const std::vector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
inherited

Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a std::vector and each dof_indices[i] specifies where to add value v[i].

Definition at line 829 of file numeric_vector.h.

831 {
832  libmesh_assert(v.size() == dof_indices.size());
833  if (!v.empty())
834  this->add_vector(&v[0], dof_indices);
835 }
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...

◆ add_vector() [4/6]

template<typename T>
virtual void libMesh::NumericVector< T >::add_vector ( const NumericVector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
virtualinherited

Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a NumericVector and each dof_indices[i] specifies where to add value v(i).

◆ add_vector() [5/6]

template<typename T>
void libMesh::NumericVector< T >::add_vector ( const DenseVector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
inherited

Computes $ \vec{u} \leftarrow \vec{u} + \vec{v} $, where v is a DenseVector and each dof_indices[i] specifies where to add value v(i).

Definition at line 841 of file numeric_vector.h.

843 {
844  libmesh_assert(v.size() == dof_indices.size());
845  if (!v.empty())
846  this->add_vector(&v(0), dof_indices);
847 }
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...

◆ add_vector() [6/6]

template<typename T>
void libMesh::NumericVector< T >::add_vector ( const NumericVector< T > &  v,
const ShellMatrix< T > &  A 
)
inherited

Computes $ \vec{u} \leftarrow \vec{u} + A \vec{v} $, i.e.

adds the product of a ShellMatrix A and a NumericVector v to this.

◆ add_vector_conjugate_transpose()

template<typename T>
void libMesh::PetscVector< T >::add_vector_conjugate_transpose ( const NumericVector< T > &  v,
const SparseMatrix< T > &  A 
)

$ U \leftarrow U + A^H v $.

Adds the product of the conjugate-transpose of SparseMatrix A and a NumericVector v to this.

◆ add_vector_transpose()

template<typename T>
virtual void libMesh::PetscVector< T >::add_vector_transpose ( const NumericVector< T > &  v,
const SparseMatrix< T > &  A 
)
overridevirtual

Computes $ \vec{u} \leftarrow \vec{u} + A^T \vec{v} $, i.e.

adds the product of the transpose of a SparseMatrix A and a NumericVector v to this.

Implements libMesh::NumericVector< T >.

◆ build()

template<typename T>
static std::unique_ptr<NumericVector<T> > libMesh::NumericVector< T >::build ( const Parallel::Communicator comm,
const SolverPackage  solver_package = libMesh::default_solver_package() 
)
staticinherited

Builds a NumericVector on the processors in communicator comm using the linear solver package specified by solver_package.

◆ clear()

template<typename T >
void libMesh::PetscVector< T >::clear ( )
overridevirtual

Restores the NumericVector<T> to a pristine state.

Reimplemented from libMesh::NumericVector< T >.

Definition at line 838 of file petsc_vector.h.

References libMesh::initialized().

839 {
840  parallel_object_only();
841 
842  if (this->initialized())
843  this->_restore_array();
844 
845  if ((this->initialized()) && (this->_destroy_vec_on_exit))
846  {
847  PetscErrorCode ierr=0;
848 
849  ierr = LibMeshVecDestroy(&_vec);
850  LIBMESH_CHKERR(ierr);
851  }
852 
853  this->_is_closed = this->_is_initialized = false;
854 
855  _global_to_local_map.clear();
856 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
bool _is_initialized
true once init() has been called.
void _restore_array() const
Restores the array (and the local form if the vector is ghosted) to PETSc.
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
bool _is_closed
Flag which tracks whether the vector&#39;s values are consistent on all processors after insertion or add...

◆ clone()

template<typename T >
std::unique_ptr< NumericVector< T > > libMesh::PetscVector< T >::clone ( ) const
overridevirtual
Returns
A copy of this vector wrapped in a smart pointer.
Note
This must be overridden in the derived classes.

Implements libMesh::NumericVector< T >.

Definition at line 910 of file petsc_vector.h.

References libMesh::NumericVector< T >::init().

911 {
912  NumericVector<T> * cloned_vector = new PetscVector<T>(this->comm(), this->type());
913  cloned_vector->init(*this, true);
914  *cloned_vector = *this;
915  return std::unique_ptr<NumericVector<T>>(cloned_vector);
916 }
const Parallel::Communicator & comm() const
ParallelType type() const

◆ close()

template<typename T >
void libMesh::PetscVector< T >::close ( )
overridevirtual

Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across processors.

Implements libMesh::NumericVector< T >.

Definition at line 810 of file petsc_vector.h.

References libMesh::GHOSTED.

811 {
812  parallel_object_only();
813 
814  this->_restore_array();
815 
816  PetscErrorCode ierr=0;
817 
818  ierr = VecAssemblyBegin(_vec);
819  LIBMESH_CHKERR(ierr);
820  ierr = VecAssemblyEnd(_vec);
821  LIBMESH_CHKERR(ierr);
822 
823  if (this->type() == GHOSTED)
824  {
825  ierr = VecGhostUpdateBegin(_vec,INSERT_VALUES,SCATTER_FORWARD);
826  LIBMESH_CHKERR(ierr);
827  ierr = VecGhostUpdateEnd(_vec,INSERT_VALUES,SCATTER_FORWARD);
828  LIBMESH_CHKERR(ierr);
829  }
830 
831  this->_is_closed = true;
832 }
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
void _restore_array() const
Restores the array (and the local form if the vector is ghosted) to PETSc.
ParallelType type() const
bool _is_closed
Flag which tracks whether the vector&#39;s values are consistent on all processors after insertion or add...

◆ closed()

template<typename T>
virtual bool libMesh::NumericVector< T >::closed ( ) const
virtualinherited
Returns
true if the vector is closed and ready for computation, false otherwise.

Definition at line 159 of file numeric_vector.h.

159 { return _is_closed; }
bool _is_closed
Flag which tracks whether the vector&#39;s values are consistent on all processors after insertion or add...

◆ comm()

const Parallel::Communicator& libMesh::ParallelObject::comm ( ) const
inherited

◆ compare()

template<typename T>
virtual int libMesh::NumericVector< T >::compare ( const NumericVector< T > &  other_vector,
const Real  threshold = TOLERANCE 
) const
virtualinherited
Returns
-1 when this is equivalent to other_vector (up to the given threshold), or the first index where abs(a[i]-b[i]) exceeds the threshold.

◆ conjugate()

template<typename T>
virtual void libMesh::PetscVector< T >::conjugate ( )
overridevirtual

Negates the imaginary component of each entry in the vector.

Implements libMesh::NumericVector< T >.

◆ create_subvector()

template<typename T>
virtual void libMesh::PetscVector< T >::create_subvector ( NumericVector< T > &  ,
const std::vector< numeric_index_type > &   
) const
overridevirtual

Fills in subvector from this vector using the indices in rows.

Similar to the create_submatrix() routine for the SparseMatrix class, it is currently only implemented for PetscVectors.

Reimplemented from libMesh::NumericVector< T >.

◆ disable_print_counter_info()

static void libMesh::ReferenceCounter::disable_print_counter_info ( )
staticinherited

◆ dot()

template<typename T>
virtual T libMesh::PetscVector< T >::dot ( const NumericVector< T > &  v) const
overridevirtual
Returns
$ \vec{u} \cdot \vec{v} $, the dot product of (*this) with the vector v.

Uses the complex-conjugate of v in the complex-valued case.

Implements libMesh::NumericVector< T >.

◆ el()

template<typename T>
virtual T libMesh::NumericVector< T >::el ( const numeric_index_type  i) const
virtualinherited
Returns
(*this)(i).

Definition at line 340 of file numeric_vector.h.

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

◆ enable_print_counter_info()

static void libMesh::ReferenceCounter::enable_print_counter_info ( )
staticinherited

Methods to enable/disable the reference counter output from print_info()

◆ first_local_index()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::first_local_index ( ) const
overridevirtual
Returns
The index of the first vector element actually stored on this processor.
Note
The minimum for this index is 0.

Implements libMesh::NumericVector< T >.

Definition at line 959 of file petsc_vector.h.

References libMesh::initialized().

960 {
961  libmesh_assert (this->initialized());
962 
963  numeric_index_type first = 0;
964 
965  if (_array_is_present) // Can we use cached values?
966  first = _first;
967  else
968  {
969  PetscErrorCode ierr=0;
970  PetscInt petsc_first=0, petsc_last=0;
971  ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
972  LIBMESH_CHKERR(ierr);
973  first = static_cast<numeric_index_type>(petsc_first);
974  }
975 
976  return first;
977 }
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
dof_id_type numeric_index_type
Definition: id_types.h:92
numeric_index_type _first
First local index.
Definition: petsc_vector.h:352

◆ get() [1/2]

template<typename T >
void libMesh::PetscVector< T >::get ( const std::vector< numeric_index_type > &  index,
T *  values 
) const
overridevirtual

Access multiple components at once.

values will not be reallocated; it should already have enough space. The default implementation calls operator() for each index, but some implementations may supply faster methods here.

Reimplemented from libMesh::NumericVector< T >.

Definition at line 1087 of file petsc_vector.h.

References libMesh::GHOSTED.

1089 {
1090  this->_get_array(true);
1091 
1092  const std::size_t num = index.size();
1093 
1094  for (std::size_t i=0; i<num; i++)
1095  {
1096  const numeric_index_type local_index = this->map_global_to_local_index(index[i]);
1097 #ifndef NDEBUG
1098  if (this->type() == GHOSTED)
1099  {
1100  libmesh_assert_less (local_index, _local_size);
1101  }
1102 #endif
1103  values[i] = static_cast<T>(_read_only_values[local_index]);
1104  }
1105 }
numeric_index_type _local_size
Size of the local values from _get_array()
Definition: petsc_vector.h:364
void _get_array(bool read_only) const
Queries the array (and the local form if the vector is ghosted) from PETSc.
const PetscScalar * _read_only_values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:381
dof_id_type numeric_index_type
Definition: id_types.h:92
numeric_index_type map_global_to_local_index(const numeric_index_type i) const
ParallelType type() const

◆ get() [2/2]

template<typename T>
void libMesh::NumericVector< T >::get ( const std::vector< numeric_index_type > &  index,
std::vector< T > &  values 
) const
inherited

Access multiple components at once.

values will be resized, if necessary, and filled. The default implementation calls operator() for each index, but some implementations may supply faster methods here.

Definition at line 814 of file numeric_vector.h.

816 {
817  const std::size_t num = index.size();
818  values.resize(num);
819  if (!num)
820  return;
821 
822  this->get(index, &values[0]);
823 }

◆ get_array()

template<typename T >
PetscScalar * libMesh::PetscVector< T >::get_array ( )

Get read/write access to the raw PETSc Vector data array.

Note
This is an advanced interface. In general you should avoid using it unless you know what you are doing!

Definition at line 1110 of file petsc_vector.h.

1111 {
1113  _get_array(false);
1114 
1115  return _values;
1116 }
void _get_array(bool read_only) const
Queries the array (and the local form if the vector is ghosted) from PETSc.
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ get_array_read()

template<typename T >
const PetscScalar * libMesh::PetscVector< T >::get_array_read ( ) const

Get read only access to the raw PETSc Vector data array.

Note
This is an advanced interface. In general you should avoid using it unless you know what you are doing!

Definition at line 1121 of file petsc_vector.h.

1122 {
1124  _get_array(true);
1125 
1126  return _read_only_values;
1127 }
void _get_array(bool read_only) const
Queries the array (and the local form if the vector is ghosted) from PETSc.
const PetscScalar * _read_only_values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:381
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ get_info()

static std::string libMesh::ReferenceCounter::get_info ( )
staticinherited

Gets a string containing the reference information.

◆ global_relative_compare()

template<typename T>
virtual int libMesh::NumericVector< T >::global_relative_compare ( const NumericVector< T > &  other_vector,
const Real  threshold = TOLERANCE 
) const
virtualinherited
Returns
-1 when this is equivalent to other_vector (up to the given global relative threshold), or the first index where abs(a[i]-b[i])/max_j(a[j],b[j]) exceeds the threshold.

◆ increment_constructor_count()

void libMesh::ReferenceCounter::increment_constructor_count ( const std::string &  name)
protectedinherited

Increments the construction counter.

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

Definition at line 181 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::ReferenceCountedObject().

182 {
183  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
184  std::pair<unsigned int, unsigned int> & p = _counts[name];
185 
186  p.first++;
187 }
std::string name(const ElemQuality q)
static Counts _counts
Actually holds the data.
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.

◆ increment_destructor_count()

void libMesh::ReferenceCounter::increment_destructor_count ( const std::string &  name)
protectedinherited

Increments the destruction counter.

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

Definition at line 194 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::~ReferenceCountedObject().

195 {
196  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
197  std::pair<unsigned int, unsigned int> & p = _counts[name];
198 
199  p.second++;
200 }
std::string name(const ElemQuality q)
static Counts _counts
Actually holds the data.
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.

◆ indefinite_dot()

template<typename T>
T libMesh::PetscVector< T >::indefinite_dot ( const NumericVector< T > &  v) const
Returns
The dot product of (*this) with the vector v.
Note
Does not use the complex-conjugate of v in the complex-valued case.

◆ init() [1/4]

template<typename T >
void libMesh::PetscVector< T >::init ( const numeric_index_type  ,
const numeric_index_type  ,
const bool  = false,
const ParallelType  = AUTOMATIC 
)
overridevirtual

Change the dimension of the vector to N.

The reserved memory for this vector remains unchanged if possible. If N==0, all memory is freed. Therefore, 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 behaviour is analogous to that of the STL containers.

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

Implements libMesh::NumericVector< T >.

Definition at line 628 of file petsc_vector.h.

References libMesh::AUTOMATIC, libMesh::initialized(), libMesh::PARALLEL, libMesh::SERIAL, and libMesh::zero.

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

632 {
633  parallel_object_only();
634 
635  PetscErrorCode ierr=0;
636  PetscInt petsc_n=static_cast<PetscInt>(n);
637  PetscInt petsc_n_local=static_cast<PetscInt>(n_local);
638 
639 
640  // Clear initialized vectors
641  if (this->initialized())
642  this->clear();
643 
644  if (ptype == AUTOMATIC)
645  {
646  if (n == n_local)
647  this->_type = SERIAL;
648  else
649  this->_type = PARALLEL;
650  }
651  else
652  this->_type = ptype;
653 
654  libmesh_assert ((this->_type==SERIAL && n==n_local) ||
655  this->_type==PARALLEL);
656 
657  // create a sequential vector if on only 1 processor
658  if (this->_type == SERIAL)
659  {
660  ierr = VecCreateSeq (PETSC_COMM_SELF, petsc_n, &_vec);
661  CHKERRABORT(PETSC_COMM_SELF,ierr);
662 
663  ierr = VecSetFromOptions (_vec);
664  CHKERRABORT(PETSC_COMM_SELF,ierr);
665  }
666  // otherwise create an MPI-enabled vector
667  else if (this->_type == PARALLEL)
668  {
669 #ifdef LIBMESH_HAVE_MPI
670  libmesh_assert_less_equal (n_local, n);
671  ierr = VecCreateMPI (this->comm().get(), petsc_n_local, petsc_n,
672  &_vec);
673  LIBMESH_CHKERR(ierr);
674 #else
675  libmesh_assert_equal_to (n_local, n);
676  ierr = VecCreateSeq (PETSC_COMM_SELF, petsc_n, &_vec);
677  CHKERRABORT(PETSC_COMM_SELF,ierr);
678 #endif
679 
680  ierr = VecSetFromOptions (_vec);
681  LIBMESH_CHKERR(ierr);
682  }
683  else
684  libmesh_error_msg("Unsupported type " << this->_type);
685 
686  this->_is_initialized = true;
687  this->_is_closed = true;
688 
689 
690  if (fast == false)
691  this->zero ();
692 }
virtual void zero() override
Set all entries to zero.
Definition: petsc_vector.h:862
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
const Parallel::Communicator & comm() const
bool _is_initialized
true once init() has been called.
virtual void clear() override
Restores the NumericVector<T> to a pristine state.
Definition: petsc_vector.h:838
ParallelType _type
Type of vector.
bool _is_closed
Flag which tracks whether the vector&#39;s values are consistent on all processors after insertion or add...

◆ init() [2/4]

template<typename T >
void libMesh::PetscVector< T >::init ( const numeric_index_type  ,
const bool  = false,
const ParallelType  = AUTOMATIC 
)
overridevirtual

Call init() with n_local = N.

Implements libMesh::NumericVector< T >.

Definition at line 698 of file petsc_vector.h.

References libMesh::TriangleWrapper::init().

701 {
702  this->init(n,n,fast,ptype);
703 }
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType type=AUTOMATIC) override
Change the dimension of the vector to N.
Definition: petsc_vector.h:628

◆ init() [3/4]

template<typename T>
virtual void libMesh::PetscVector< T >::init ( const numeric_index_type  ,
const numeric_index_type  ,
const std::vector< numeric_index_type > &  ,
const bool  = false,
const ParallelType  = AUTOMATIC 
)
overridevirtual

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

Implements libMesh::NumericVector< T >.

◆ init() [4/4]

template<typename T >
void libMesh::PetscVector< T >::init ( const NumericVector< T > &  other,
const bool  fast = false 
)
overridevirtual

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

Implements libMesh::NumericVector< T >.

Definition at line 771 of file petsc_vector.h.

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

773 {
774  parallel_object_only();
775 
776  // Clear initialized vectors
777  if (this->initialized())
778  this->clear();
779 
780  const PetscVector<T> & v = cast_ref<const PetscVector<T> &>(other);
781 
782  // Other vector should restore array.
783  if (v.initialized())
784  {
785  v._restore_array();
786  }
787 
788  this->_global_to_local_map = v._global_to_local_map;
789 
790  // Even if we're initializing sizes based on an uninitialized or
791  // unclosed vector, *this* vector is being initialized now and is
792  // initially closed.
793  this->_is_closed = true; // v._is_closed;
794  this->_is_initialized = true; // v._is_initialized;
795 
796  this->_type = v._type;
797 
798  // We want to have a valid Vec, even if it's initially of size zero
799  PetscErrorCode ierr = VecDuplicate (v._vec, &this->_vec);
800  LIBMESH_CHKERR(ierr);
801 
802  if (fast == false)
803  this->zero ();
804 }
virtual void zero() override
Set all entries to zero.
Definition: petsc_vector.h:862
virtual bool initialized() const
bool _is_initialized
true once init() has been called.
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
virtual void clear() override
Restores the NumericVector<T> to a pristine state.
Definition: petsc_vector.h:838
ParallelType _type
Type of vector.
bool _is_closed
Flag which tracks whether the vector&#39;s values are consistent on all processors after insertion or add...

◆ initialized()

template<typename T>
virtual bool libMesh::NumericVector< T >::initialized ( ) const
virtualinherited
Returns
true if the vector has been initialized, false otherwise.

Definition at line 143 of file numeric_vector.h.

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

143 { return _is_initialized; }
bool _is_initialized
true once init() has been called.

◆ insert() [1/5]

template<typename T>
virtual void libMesh::PetscVector< T >::insert ( const T *  v,
const std::vector< numeric_index_type > &  dof_indices 
)
overridevirtual

Inserts the entries of v in *this at the locations specified by v.

Reimplemented from libMesh::NumericVector< T >.

◆ insert() [2/5]

template<typename T>
void libMesh::NumericVector< T >::insert ( const std::vector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
inherited

Inserts the entries of v in *this at the locations specified by v.

Definition at line 853 of file numeric_vector.h.

855 {
856  libmesh_assert(v.size() == dof_indices.size());
857  if (!v.empty())
858  this->insert(&v[0], dof_indices);
859 }
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)
Inserts the entries of v in *this at the locations specified by v.

◆ insert() [3/5]

template<typename T>
virtual void libMesh::NumericVector< T >::insert ( const NumericVector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
virtualinherited

Inserts the entries of v in *this at the locations specified by v.

◆ insert() [4/5]

template<typename T>
void libMesh::NumericVector< T >::insert ( const DenseVector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
inherited

Inserts the entries of v in *this at the locations specified by v.

Definition at line 865 of file numeric_vector.h.

867 {
868  libmesh_assert(v.size() == dof_indices.size());
869  if (!v.empty())
870  this->insert(&v(0), dof_indices);
871 }
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)
Inserts the entries of v in *this at the locations specified by v.

◆ insert() [5/5]

template<typename T>
void libMesh::NumericVector< T >::insert ( const DenseSubVector< T > &  v,
const std::vector< numeric_index_type > &  dof_indices 
)
inherited

Inserts the entries of v in *this at the locations specified by v.

Definition at line 877 of file numeric_vector.h.

879 {
880  libmesh_assert(v.size() == dof_indices.size());
881  if (!v.empty())
882  this->insert(&v(0), dof_indices);
883 }
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)
Inserts the entries of v in *this at the locations specified by v.

◆ l1_norm()

template<typename T>
virtual Real libMesh::PetscVector< T >::l1_norm ( ) const
overridevirtual
Returns
The $ \ell_1 $-norm of the vector, i.e. the sum of the absolute values of the entries.

Implements libMesh::NumericVector< T >.

◆ l2_norm()

template<typename T>
virtual Real libMesh::PetscVector< T >::l2_norm ( ) const
overridevirtual
Returns
The $ \ell_2 $-norm of the vector, i.e. the square root of the sum of the squares of the entries.

Implements libMesh::NumericVector< T >.

◆ last_local_index()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::last_local_index ( ) const
overridevirtual
Returns
The index+1 of the last vector element actually stored on this processor.
Note
The maximum for this index is size().

Implements libMesh::NumericVector< T >.

Definition at line 983 of file petsc_vector.h.

References libMesh::initialized().

984 {
985  libmesh_assert (this->initialized());
986 
987  numeric_index_type last = 0;
988 
989  if (_array_is_present) // Can we use cached values?
990  last = _last;
991  else
992  {
993  PetscErrorCode ierr=0;
994  PetscInt petsc_first=0, petsc_last=0;
995  ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
996  LIBMESH_CHKERR(ierr);
997  last = static_cast<numeric_index_type>(petsc_last);
998  }
999 
1000  return last;
1001 }
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
numeric_index_type _last
Last local index.
Definition: petsc_vector.h:359
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
dof_id_type numeric_index_type
Definition: id_types.h:92

◆ linfty_norm()

template<typename T>
virtual Real libMesh::PetscVector< T >::linfty_norm ( ) const
overridevirtual
Returns
The $ \ell_{\infty} $-norm of the vector, i.e. the maximum absolute value of the entries of the vector.

Implements libMesh::NumericVector< T >.

◆ local_relative_compare()

template<typename T>
virtual int libMesh::NumericVector< T >::local_relative_compare ( const NumericVector< T > &  other_vector,
const Real  threshold = TOLERANCE 
) const
virtualinherited
Returns
-1 when this is equivalent to other_vector, (up to the given local relative threshold), or the first index where abs(a[i]-b[i])/max(a[i],b[i]) exceeds the threshold.

◆ local_size()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::local_size ( ) const
overridevirtual
Returns
The local size of the vector, i.e. index_stop - index_start.

Implements libMesh::NumericVector< T >.

Definition at line 942 of file petsc_vector.h.

References libMesh::initialized().

943 {
944  libmesh_assert (this->initialized());
945 
946  PetscErrorCode ierr=0;
947  PetscInt petsc_size=0;
948 
949  ierr = VecGetLocalSize(_vec, &petsc_size);
950  LIBMESH_CHKERR(ierr);
951 
952  return static_cast<numeric_index_type>(petsc_size);
953 }
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
dof_id_type numeric_index_type
Definition: id_types.h:92

◆ localize() [1/5]

template<typename T>
virtual void libMesh::PetscVector< T >::localize ( std::vector< T > &  v_local) const
overridevirtual

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

Implements libMesh::NumericVector< T >.

◆ localize() [2/5]

template<typename T>
virtual void libMesh::PetscVector< T >::localize ( NumericVector< T > &  v_local) const
overridevirtual

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

Implements libMesh::NumericVector< T >.

◆ localize() [3/5]

template<typename T>
virtual void libMesh::PetscVector< T >::localize ( NumericVector< T > &  v_local,
const std::vector< numeric_index_type > &  send_list 
) const
overridevirtual

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

Implements libMesh::NumericVector< T >.

◆ localize() [4/5]

template<typename T>
virtual void libMesh::PetscVector< T >::localize ( std::vector< T > &  v_local,
const std::vector< numeric_index_type > &  indices 
) const
overridevirtual


Fill in the local std::vector "v_local" with the global indices given in "indices".

Note
The indices can be different on every processor, and the same index can be localized to more than one processor. The resulting v_local can be shorter than the original, and the entries will be in the order specified by indices.

Example:

*   On 4 procs *this = {a, b, c, d, e, f, g, h, i} is a parallel vector.
*   On each proc, the indices arrays are set up as:
*   proc0, indices = {1,2,4,5}
*   proc1, indices = {2,5,6,8}
*   proc2, indices = {2,3,6,7}
*   proc3, indices = {0,1,2,3}
*
*   After calling this version of localize, the v_local vectors are:
*   proc0, v_local = {b,c,e,f}
*   proc1, v_local = {c,f,g,i}
*   proc2, v_local = {c,d,g,h}
*   proc3, v_local = {a,b,c,d}
* 

This function is useful in parallel I/O routines, when you have a parallel vector of solution values which you want to write a subset of.

Implements libMesh::NumericVector< T >.

◆ localize() [5/5]

template<typename T>
virtual void libMesh::PetscVector< T >::localize ( const numeric_index_type  first_local_idx,
const numeric_index_type  last_local_idx,
const std::vector< numeric_index_type > &  send_list 
)
overridevirtual

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

Implements libMesh::NumericVector< T >.

◆ localize_to_one()

template<typename T>
virtual void libMesh::PetscVector< T >::localize_to_one ( std::vector< T > &  v_local,
const processor_id_type  proc_id = 0 
) const
overridevirtual

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 libMesh::NumericVector< T >.

◆ map_global_to_local_index()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::map_global_to_local_index ( const numeric_index_type  i) const
Returns
The local index corresponding to global index i.

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 1007 of file petsc_vector.h.

References end, and libMesh::initialized().

1008 {
1009  libmesh_assert (this->initialized());
1010 
1011  numeric_index_type first=0;
1012  numeric_index_type last=0;
1013 
1014  if (_array_is_present) // Can we use cached values?
1015  {
1016  first = _first;
1017  last = _last;
1018  }
1019  else
1020  {
1021  PetscErrorCode ierr=0;
1022  PetscInt petsc_first=0, petsc_last=0;
1023  ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
1024  LIBMESH_CHKERR(ierr);
1025  first = static_cast<numeric_index_type>(petsc_first);
1026  last = static_cast<numeric_index_type>(petsc_last);
1027  }
1028 
1029 
1030  if ((i>=first) && (i<last))
1031  {
1032  return i-first;
1033  }
1034 
1035  GlobalToLocalMap::const_iterator it = _global_to_local_map.find(i);
1036 #ifndef NDEBUG
1037  const GlobalToLocalMap::const_iterator end = _global_to_local_map.end();
1038  if (it == end)
1039  {
1040  std::ostringstream error_message;
1041  error_message << "No index " << i << " in ghosted vector.\n"
1042  << "Vector contains [" << first << ',' << last << ")\n";
1043  GlobalToLocalMap::const_iterator b = _global_to_local_map.begin();
1044  if (b == end)
1045  {
1046  error_message << "And empty ghost array.\n";
1047  }
1048  else
1049  {
1050  error_message << "And ghost array {" << b->first;
1051  for (++b; b != end; ++b)
1052  error_message << ',' << b->first;
1053  error_message << "}\n";
1054  }
1055 
1056  libmesh_error_msg(error_message.str());
1057  }
1058  libmesh_assert (it != _global_to_local_map.end());
1059 #endif
1060  return it->second+last-first;
1061 }
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
numeric_index_type _last
Last local index.
Definition: petsc_vector.h:359
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end...
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
dof_id_type numeric_index_type
Definition: id_types.h:92
numeric_index_type _first
First local index.
Definition: petsc_vector.h:352

◆ max()

template<typename T >
Real libMesh::PetscVector< T >::max ( ) const
overridevirtual
Returns
The maximum entry in the vector, or the maximum real part in the case of complex numbers.

Implements libMesh::NumericVector< T >.

Definition at line 1162 of file petsc_vector.h.

References libMesh::Real.

1163 {
1164  parallel_object_only();
1165 
1166  this->_restore_array();
1167 
1168  PetscErrorCode ierr=0;
1169  PetscInt index=0;
1170  PetscReal returnval=0.;
1171 
1172  ierr = VecMax (_vec, &index, &returnval);
1173  LIBMESH_CHKERR(ierr);
1174 
1175  // this return value is correct: VecMax returns a PetscReal
1176  return static_cast<Real>(returnval);
1177 }
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
void _restore_array() const
Restores the array (and the local form if the vector is ghosted) to PETSc.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ min()

template<typename T >
Real libMesh::PetscVector< T >::min ( ) const
overridevirtual
Returns
The minimum entry in the vector, or the minimum real part in the case of complex numbers.

Implements libMesh::NumericVector< T >.

Definition at line 1141 of file petsc_vector.h.

References libMesh::Real.

1142 {
1143  parallel_object_only();
1144 
1145  this->_restore_array();
1146 
1147  PetscErrorCode ierr=0;
1148  PetscInt index=0;
1149  PetscReal returnval=0.;
1150 
1151  ierr = VecMin (_vec, &index, &returnval);
1152  LIBMESH_CHKERR(ierr);
1153 
1154  // this return value is correct: VecMin returns a PetscReal
1155  return static_cast<Real>(returnval);
1156 }
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
void _restore_array() const
Restores the array (and the local form if the vector is ghosted) to PETSc.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ n_objects()

static unsigned int libMesh::ReferenceCounter::n_objects ( )
staticinherited

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

Definition at line 83 of file reference_counter.h.

References libMesh::ReferenceCounter::_n_objects.

84  { return _n_objects; }
static Threads::atomic< unsigned int > _n_objects
The number of objects.

◆ n_processors()

processor_id_type libMesh::ParallelObject::n_processors ( ) const
inherited
Returns
The number of processors in the group.

Definition at line 93 of file parallel_object.h.

References libMesh::ParallelObject::_communicator, and libMesh::Parallel::Communicator::size().

Referenced by libMesh::MeshBase::partition().

94  { return cast_int<processor_id_type>(_communicator.size()); }
processor_id_type size() const
Definition: communicator.h:175
const Parallel::Communicator & _communicator

◆ operator()()

template<typename T >
T libMesh::PetscVector< T >::operator() ( const numeric_index_type  i) const
overridevirtual
Returns
A copy of the ith entry of the vector.

Implements libMesh::NumericVector< T >.

Definition at line 1067 of file petsc_vector.h.

References libMesh::GHOSTED.

1068 {
1069  this->_get_array(true);
1070 
1071  const numeric_index_type local_index = this->map_global_to_local_index(i);
1072 
1073 #ifndef NDEBUG
1074  if (this->type() == GHOSTED)
1075  {
1076  libmesh_assert_less (local_index, _local_size);
1077  }
1078 #endif
1079 
1080  return static_cast<T>(_read_only_values[local_index]);
1081 }
numeric_index_type _local_size
Size of the local values from _get_array()
Definition: petsc_vector.h:364
void _get_array(bool read_only) const
Queries the array (and the local form if the vector is ghosted) from PETSc.
const PetscScalar * _read_only_values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:381
dof_id_type numeric_index_type
Definition: id_types.h:92
numeric_index_type map_global_to_local_index(const numeric_index_type i) const
ParallelType type() const

◆ operator*=()

template<typename T>
NumericVector<T>& libMesh::NumericVector< T >::operator*= ( const T  a)
inherited

Scales the vector by a, $ \vec{u} \leftarrow a\vec{u} $.

Equivalent to u.scale(a)

Returns
A reference to *this.

Definition at line 385 of file numeric_vector.h.

385 { this->scale(a); return *this; }
virtual void scale(const T factor)=0
Scale each element of the vector by the given factor.

◆ operator+=()

template<typename T>
virtual NumericVector<T>& libMesh::PetscVector< T >::operator+= ( const NumericVector< T > &  v)
overridevirtual

Adds v to *this, $ \vec{u} \leftarrow \vec{u} + \vec{v} $.

Equivalent to u.add(1, v).

Returns
A reference to *this.

Implements libMesh::NumericVector< T >.

◆ operator-=()

template<typename T>
virtual NumericVector<T>& libMesh::PetscVector< T >::operator-= ( const NumericVector< T > &  v)
overridevirtual

Subtracts v from *this, $ \vec{u} \leftarrow \vec{u} - \vec{v} $.

Equivalent to u.add(-1, v).

Returns
A reference to *this.

Implements libMesh::NumericVector< T >.

◆ operator/=() [1/2]

template<typename T>
virtual NumericVector<T>& libMesh::PetscVector< T >::operator/= ( const NumericVector< T > &  )
overridevirtual

Computes the pointwise division of this vector's entries by another's, $ u_i \leftarrow \frac{u_i}{v_i} \, \forall i$.

Returns
A reference to *this.

Implements libMesh::NumericVector< T >.

◆ operator/=() [2/2]

template<typename T>
NumericVector<T>& libMesh::NumericVector< T >::operator/= ( const T  a)
inherited

Scales the vector by 1/a, $ \vec{u} \leftarrow \frac{1}{a}\vec{u} $.

Equivalent to u.scale(1./a)

Returns
A reference to *this.

Definition at line 394 of file numeric_vector.h.

394 { this->scale(1./a); return *this; }
virtual void scale(const T factor)=0
Scale each element of the vector by the given factor.

◆ operator=() [1/5]

template<typename T>
PetscVector<T>& libMesh::PetscVector< T >::operator= ( const PetscVector< T > &  v)

Copy assignment operator.

Calls VecCopy after performing various checks.

Returns
A reference to *this as the derived type.

◆ operator=() [2/5]

template<typename T>
PetscVector& libMesh::PetscVector< T >::operator= ( PetscVector< T > &&  )
delete

◆ operator=() [3/5]

template<typename T>
virtual NumericVector<T>& libMesh::PetscVector< T >::operator= ( const T  s)
overridevirtual

Sets all entries of the vector to the value s.

Returns
A reference to *this.

Implements libMesh::NumericVector< T >.

◆ operator=() [4/5]

template<typename T>
virtual NumericVector<T>& libMesh::PetscVector< T >::operator= ( const NumericVector< T > &  v)
overridevirtual

This looks like a copy assignment operator, but note that, unlike normal copy assignment operators, it is pure virtual.

This function should be overridden in derived classes so that they can be copied correctly via references to the base class. This design usually isn't a good idea in general, but in this context it works because we usually don't have a mix of different kinds of NumericVectors active in the library at a single time.

Returns
A reference to *this as the base type.

Implements libMesh::NumericVector< T >.

◆ operator=() [5/5]

template<typename T>
virtual NumericVector<T>& libMesh::PetscVector< T >::operator= ( const std::vector< T > &  v)
overridevirtual

Sets (*this)(i) = v(i) for each entry of the vector.

Returns
A reference to *this as the base type.

Implements libMesh::NumericVector< T >.

◆ pointwise_mult()

template<typename T>
virtual void libMesh::PetscVector< T >::pointwise_mult ( const NumericVector< T > &  vec1,
const NumericVector< T > &  vec2 
)
overridevirtual

Computes $ u_i \leftarrow u_i v_i $ (summation not implied) i.e.

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

Implements libMesh::NumericVector< T >.

◆ print() [1/2]

template<typename T >
void libMesh::NumericVector< T >::print ( std::ostream &  os = libMesh::out) const
virtualinherited

Prints the local contents of the vector, by default to libMesh::out.

Definition at line 910 of file numeric_vector.h.

911 {
912  libmesh_assert (this->initialized());
913  os << "Size\tglobal = " << this->size()
914  << "\t\tlocal = " << this->local_size() << std::endl;
915 
916  os << "#\tValue" << std::endl;
917  for (numeric_index_type i=this->first_local_index(); i<this->last_local_index(); i++)
918  os << i << "\t" << (*this)(i) << std::endl;
919 }
virtual bool initialized() const
virtual numeric_index_type size() const =0
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type first_local_index() const =0
virtual numeric_index_type local_size() const =0
virtual numeric_index_type last_local_index() const =0

◆ print() [2/2]

template<>
void libMesh::NumericVector< Complex >::print ( std::ostream &  os) const
inherited

Definition at line 892 of file numeric_vector.h.

893 {
894  libmesh_assert (this->initialized());
895  os << "Size\tglobal = " << this->size()
896  << "\t\tlocal = " << this->local_size() << std::endl;
897 
898  // std::complex<>::operator<<() is defined, but use this form
899  os << "#\tReal part\t\tImaginary part" << std::endl;
900  for (numeric_index_type i=this->first_local_index(); i<this->last_local_index(); i++)
901  os << i << "\t"
902  << (*this)(i).real() << "\t\t"
903  << (*this)(i).imag() << std::endl;
904 }
virtual bool initialized() const
virtual numeric_index_type size() const =0
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type first_local_index() const =0
virtual numeric_index_type local_size() const =0
virtual numeric_index_type last_local_index() const =0

◆ print_global() [1/2]

template<typename T >
void libMesh::NumericVector< T >::print_global ( std::ostream &  os = libMesh::out) const
virtualinherited

Prints the global contents of the vector, by default to libMesh::out.

Definition at line 947 of file numeric_vector.h.

948 {
949  libmesh_assert (this->initialized());
950 
951  std::vector<T> v(this->size());
952  this->localize(v);
953 
954  // Right now we only want one copy of the output
955  if (this->processor_id())
956  return;
957 
958  os << "Size\tglobal = " << this->size() << std::endl;
959  os << "#\tValue" << std::endl;
960  for (numeric_index_type i=0; i!=v.size(); i++)
961  os << i << "\t" << v[i] << std::endl;
962 }
virtual bool initialized() const
virtual numeric_index_type size() const =0
dof_id_type numeric_index_type
Definition: id_types.h:92
processor_id_type processor_id() const
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.

◆ print_global() [2/2]

template<>
void libMesh::NumericVector< Complex >::print_global ( std::ostream &  os) const
inherited

Definition at line 925 of file numeric_vector.h.

926 {
927  libmesh_assert (this->initialized());
928 
929  std::vector<Complex> v(this->size());
930  this->localize(v);
931 
932  // Right now we only want one copy of the output
933  if (this->processor_id())
934  return;
935 
936  os << "Size\tglobal = " << this->size() << std::endl;
937  os << "#\tReal part\t\tImaginary part" << std::endl;
938  for (numeric_index_type i=0; i!=v.size(); i++)
939  os << i << "\t"
940  << v[i].real() << "\t\t"
941  << v[i].imag() << std::endl;
942 }
virtual bool initialized() const
virtual numeric_index_type size() const =0
dof_id_type numeric_index_type
Definition: id_types.h:92
processor_id_type processor_id() const
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.

◆ print_info()

static void libMesh::ReferenceCounter::print_info ( std::ostream &  out = libMesh::out)
staticinherited

Prints the reference information, by default to libMesh::out.

◆ print_matlab()

template<typename T>
virtual void libMesh::PetscVector< T >::print_matlab ( const std::string &  = "") const
overridevirtual

Print the contents of the vector in Matlab's sparse matrix format.

Optionally prints the vector to the file named name. If name is not specified it is dumped to the screen.

Reimplemented from libMesh::NumericVector< T >.

◆ processor_id()

processor_id_type libMesh::ParallelObject::processor_id ( ) const
inherited

◆ reciprocal()

template<typename T>
virtual void libMesh::PetscVector< T >::reciprocal ( )
overridevirtual

Computes the pointwise reciprocal, $ u_i \leftarrow \frac{1}{u_i} \, \forall i$.

Implements libMesh::NumericVector< T >.

◆ restore_array()

template<typename T >
void libMesh::PetscVector< T >::restore_array ( )

Restore the data array.

Note
This MUST be called after get_array() or get_array_read() and before using any other interface functions on PetscVector.

Definition at line 1131 of file petsc_vector.h.

1132 {
1133  // \note \p _values_manually_retrieved needs to be set to \p false
1134  // \e before calling \p _restore_array()!
1136  _restore_array();
1137 }
void _restore_array() const
Restores the array (and the local form if the vector is ghosted) to PETSc.
bool _values_manually_retrieved
Whether or not the data array has been manually retrieved using get_array() or get_array_read() ...
Definition: petsc_vector.h:439

◆ scale()

template<typename T>
virtual void libMesh::PetscVector< T >::scale ( const T  factor)
overridevirtual

Scale each element of the vector by the given factor.

Implements libMesh::NumericVector< T >.

◆ set()

template<typename T>
virtual void libMesh::PetscVector< T >::set ( const numeric_index_type  i,
const T  value 
)
overridevirtual

Sets v(i) = value.

Implements libMesh::NumericVector< T >.

◆ size()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::size ( ) const
overridevirtual
Returns
The size of the vector.

Implements libMesh::NumericVector< T >.

Definition at line 922 of file petsc_vector.h.

References libMesh::initialized().

923 {
924  libmesh_assert (this->initialized());
925 
926  PetscErrorCode ierr=0;
927  PetscInt petsc_size=0;
928 
929  if (!this->initialized())
930  return 0;
931 
932  ierr = VecGetSize(_vec, &petsc_size);
933  LIBMESH_CHKERR(ierr);
934 
935  return static_cast<numeric_index_type>(petsc_size);
936 }
virtual bool initialized() const
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
dof_id_type numeric_index_type
Definition: id_types.h:92

◆ subset_l1_norm()

template<typename T>
virtual Real libMesh::NumericVector< T >::subset_l1_norm ( const std::set< numeric_index_type > &  indices) const
virtualinherited
Returns
The $ \ell_1 $-norm of the vector, i.e. the sum of the absolute values for the specified entries in the vector.
Note
The indices must necessarily live on this processor.

◆ subset_l2_norm()

template<typename T>
virtual Real libMesh::NumericVector< T >::subset_l2_norm ( const std::set< numeric_index_type > &  indices) const
virtualinherited
Returns
The $ \ell_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
The indices must necessarily live on this processor.

◆ subset_linfty_norm()

template<typename T>
virtual Real libMesh::NumericVector< T >::subset_linfty_norm ( const std::set< numeric_index_type > &  indices) const
virtualinherited
Returns
The maximum absolute value of the specified entries of this vector, which is the $ \ell_{\infty} $-norm of a vector.
Note
The indices must necessarily live on this processor.

◆ sum()

template<typename T>
virtual T libMesh::PetscVector< T >::sum ( ) const
overridevirtual
Returns
The sum of all values in the vector.

Implements libMesh::NumericVector< T >.

◆ swap()

template<typename T >
void libMesh::PetscVector< T >::swap ( NumericVector< T > &  v)
overridevirtual

Swaps the contents of this with v.

There should be enough indirection in subclasses to make this an O(1) header-swap operation.

Reimplemented from libMesh::NumericVector< T >.

Definition at line 1183 of file petsc_vector.h.

References libMesh::PetscVector< T >::_array_is_present, libMesh::PetscVector< T >::_destroy_vec_on_exit, libMesh::PetscVector< T >::_global_to_local_map, libMesh::PetscVector< T >::_local_form, libMesh::PetscVector< T >::_values, libMesh::PetscVector< T >::_vec, swap(), and libMesh::NumericVector< T >::swap().

1184 {
1185  parallel_object_only();
1186 
1187  NumericVector<T>::swap(other);
1188 
1189  PetscVector<T> & v = cast_ref<PetscVector<T> &>(other);
1190 
1191  std::swap(_vec, v._vec);
1192  std::swap(_destroy_vec_on_exit, v._destroy_vec_on_exit);
1193  std::swap(_global_to_local_map, v._global_to_local_map);
1194 
1195 #ifdef LIBMESH_HAVE_CXX11_THREAD
1196  // Only truly atomic for v... but swap() doesn't really need to be thread safe!
1197  _array_is_present = v._array_is_present.exchange(_array_is_present);
1198 #else
1199  std::swap(_array_is_present, v._array_is_present);
1200 #endif
1201 
1202  std::swap(_local_form, v._local_form);
1203  std::swap(_values, v._values);
1204 }
bool _destroy_vec_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Vec object...
Definition: petsc_vector.h:433
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
PetscScalar * _values
Pointer to the actual PETSc array of the values of the vector.
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
If true, the actual PETSc array of the values of the vector is currently accessible.
Definition: petsc_vector.h:342
Vec _local_form
PETSc vector datatype to hold the local form of a ghosted vector.
Definition: petsc_vector.h:371
GlobalToLocalMap _global_to_local_map
Map that maps global to local ghost cells (will be empty if not in ghost cell mode).
Definition: petsc_vector.h:427
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
void swap(Iterator &lhs, Iterator &rhs)
swap, used to implement op=

◆ type() [1/2]

template<typename T>
ParallelType libMesh::NumericVector< T >::type ( ) const
inherited
Returns
The type (SERIAL, PARALLEL, GHOSTED) of the vector.

Definition at line 148 of file numeric_vector.h.

Referenced by libMesh::EpetraVector< T >::EpetraVector(), libMesh::DistributedVector< T >::init(), libMesh::EigenSparseVector< T >::init(), libMesh::LaspackVector< T >::init(), and libMesh::EpetraVector< T >::init().

148 { return _type; }
ParallelType _type
Type of vector.

◆ type() [2/2]

template<typename T>
ParallelType& libMesh::NumericVector< T >::type ( )
inherited
Returns
The type (SERIAL, PARALLEL, GHOSTED) of the vector.

Definition at line 153 of file numeric_vector.h.

153 { return _type; }
ParallelType _type
Type of vector.

◆ vec()

template<typename T>
Vec libMesh::PetscVector< T >::vec ( )
Returns
The raw PETSc Vec pointer.
Note
This is generally not required in user-level code.
Don't do anything crazy like calling LibMeshVecDestroy() on it, or very bad things will likely happen!

Definition at line 325 of file petsc_vector.h.

References libMesh::PetscVector< T >::_vec.

325 { libmesh_assert (_vec); return _vec; }
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333

◆ zero()

template<typename T >
void libMesh::PetscVector< T >::zero ( )
overridevirtual

Set all entries to zero.

Equivalent to v = 0, but more obvious and faster.

Implements libMesh::NumericVector< T >.

Definition at line 862 of file petsc_vector.h.

References libMesh::closed(), and libMesh::GHOSTED.

863 {
864  parallel_object_only();
865 
866  libmesh_assert(this->closed());
867 
868  this->_restore_array();
869 
870  PetscErrorCode ierr=0;
871 
872  PetscScalar z=0.;
873 
874  if (this->type() != GHOSTED)
875  {
876  ierr = VecSet (_vec, z);
877  LIBMESH_CHKERR(ierr);
878  }
879  else
880  {
881  /* Vectors that include ghost values require a special
882  handling. */
883  Vec loc_vec;
884  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
885  LIBMESH_CHKERR(ierr);
886 
887  ierr = VecSet (loc_vec, z);
888  LIBMESH_CHKERR(ierr);
889 
890  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
891  LIBMESH_CHKERR(ierr);
892  }
893 }
Vec _vec
Actual PETSc vector datatype to hold vector entries.
Definition: petsc_vector.h:333
void _restore_array() const
Restores the array (and the local form if the vector is ghosted) to PETSc.
virtual bool closed() const
ParallelType type() const

◆ zero_clone()

template<typename T >
std::unique_ptr< NumericVector< T > > libMesh::PetscVector< T >::zero_clone ( ) const
overridevirtual
Returns
A smart pointer to a copy of this vector with the same type, size, and partitioning, but with all zero entries.
Note
This must be overridden in the derived classes.

Implements libMesh::NumericVector< T >.

Definition at line 899 of file petsc_vector.h.

References libMesh::NumericVector< T >::init().

900 {
901  NumericVector<T> * cloned_vector = new PetscVector<T>(this->comm(), this->type());
902  cloned_vector->init(*this);
903  return std::unique_ptr<NumericVector<T>>(cloned_vector);
904 }
const Parallel::Communicator & comm() const
ParallelType type() const

Member Data Documentation

◆ _array_is_present [1/2]

template<typename T>
std::atomic<bool> libMesh::PetscVector< T >::_array_is_present
mutableprivate

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 342 of file petsc_vector.h.

Referenced by libMesh::PetscVector< T >::swap().

◆ _array_is_present [2/2]

template<typename T>
bool libMesh::PetscVector< T >::_array_is_present
mutableprivate

Definition at line 344 of file petsc_vector.h.

◆ _communicator

const Parallel::Communicator& libMesh::ParallelObject::_communicator
protectedinherited

◆ _counts

Counts libMesh::ReferenceCounter::_counts
staticprotectedinherited

◆ _destroy_vec_on_exit

template<typename T>
bool libMesh::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 433 of file petsc_vector.h.

Referenced by libMesh::PetscVector< T >::swap().

◆ _enable_print_counter

bool libMesh::ReferenceCounter::_enable_print_counter
staticprotectedinherited

Flag to control whether reference count information is printed when print_info is called.

Definition at line 141 of file reference_counter.h.

◆ _first

template<typename T>
numeric_index_type libMesh::PetscVector< T >::_first
mutableprivate

First local index.

Only valid when _array_is_present

Definition at line 352 of file petsc_vector.h.

◆ _global_to_local_map

template<typename T>
GlobalToLocalMap libMesh::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 427 of file petsc_vector.h.

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

◆ _is_closed

template<typename T>
bool libMesh::NumericVector< T >::_is_closed
protectedinherited

Flag which tracks whether the vector's values are consistent on all processors after insertion or addition of values has occurred on some or all processors.

Definition at line 706 of file numeric_vector.h.

Referenced by libMesh::NumericVector< Number >::closed(), libMesh::EpetraVector< T >::EpetraVector(), and libMesh::PetscVector< T >::PetscVector().

◆ _is_initialized

template<typename T>
bool libMesh::NumericVector< T >::_is_initialized
protectedinherited

◆ _last

template<typename T>
numeric_index_type libMesh::PetscVector< T >::_last
mutableprivate

Last local index.

Only valid when _array_is_present

Definition at line 359 of file petsc_vector.h.

◆ _local_form

template<typename T>
Vec libMesh::PetscVector< T >::_local_form
mutableprivate

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 371 of file petsc_vector.h.

Referenced by libMesh::PetscVector< T >::swap().

◆ _local_size

template<typename T>
numeric_index_type libMesh::PetscVector< T >::_local_size
mutableprivate

Size of the local values from _get_array()

Definition at line 364 of file petsc_vector.h.

◆ _mutex

Threads::spin_mutex libMesh::ReferenceCounter::_mutex
staticprotectedinherited

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 135 of file reference_counter.h.

◆ _n_objects

Threads::atomic<unsigned int> libMesh::ReferenceCounter::_n_objects
staticprotectedinherited

The number of objects.

Print the reference count information when the number returns to 0.

Definition at line 130 of file reference_counter.h.

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

◆ _petsc_vector_mutex [1/2]

template<typename T>
std::mutex libMesh::PetscVector< T >::_petsc_vector_mutex
mutableprivate

Mutex for _get_array and _restore_array.

This is part of the object to keep down thread contention when reading frmo multiple PetscVectors simultaneously

Definition at line 399 of file petsc_vector.h.

◆ _petsc_vector_mutex [2/2]

template<typename T>
Threads::spin_mutex libMesh::PetscVector< T >::_petsc_vector_mutex
mutableprivate

Definition at line 401 of file petsc_vector.h.

◆ _read_only_values

template<typename T>
const PetscScalar* libMesh::PetscVector< T >::_read_only_values
mutableprivate

Pointer to the actual PETSc array of the values of the vector.

This pointer is only valid if _array_is_present is true. We're using PETSc's VecGetArrayRead() function, which requires a constant PetscScalar *, but _get_array and _restore_array are const member functions, so _values also needs to be mutable (otherwise it is a "const PetscScalar * const" in that context).

Definition at line 381 of file petsc_vector.h.

◆ _type

template<typename T>
ParallelType libMesh::NumericVector< T >::_type
protectedinherited

◆ _values

template<typename T>
PetscScalar* libMesh::PetscVector< T >::_values
mutableprivate

Pointer to the actual PETSc array of the values of the vector.

This pointer is only valid if _array_is_present is true. We're using PETSc's VecGetArrayRead() function, which requires a constant PetscScalar *, but _get_array and _restore_array are const member functions, so _values also needs to be mutable (otherwise it is a "const PetscScalar * const" in that context).

Definition at line 391 of file petsc_vector.h.

Referenced by libMesh::PetscVector< T >::swap().

◆ _values_manually_retrieved

template<typename T>
bool libMesh::PetscVector< T >::_values_manually_retrieved
mutableprivate

Whether or not the data array has been manually retrieved using get_array() or get_array_read()

Definition at line 439 of file petsc_vector.h.

◆ _values_read_only

template<typename T>
bool libMesh::PetscVector< T >::_values_read_only
mutableprivate

Whether or not the data array is for read only access.

Definition at line 444 of file petsc_vector.h.

◆ _vec

template<typename T>
Vec libMesh::PetscVector< T >::_vec
private

Actual PETSc vector datatype to hold vector entries.

Definition at line 333 of file petsc_vector.h.

Referenced by libMesh::PetscVector< T >::init(), libMesh::PetscVector< T >::PetscVector(), libMesh::PetscVector< T >::swap(), and libMesh::PetscVector< T >::vec().


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