libMesh::PetscVector< T > Class Template Referencefinal

NumericVector interface to PETSc Vec. More...

#include <petsc_vector.h>

Inheritance diagram for libMesh::PetscVector< T >:

Public Member Functions

 PetscVector (const Parallel::Communicator &comm_in, const ParallelType type=AUTOMATIC)
 
 PetscVector (const Parallel::Communicator &comm_in, const numeric_index_type n, const ParallelType type=AUTOMATIC)
 
 PetscVector (const Parallel::Communicator &comm_in, const numeric_index_type n, const numeric_index_type n_local, const ParallelType type=AUTOMATIC)
 
 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)
 
 PetscVector (Vec v, const Parallel::Communicator &comm_in)
 
PetscVector< T > & operator= (const PetscVector< T > &v)
 
 PetscVector (PetscVector &&)=delete
 
 PetscVector (const PetscVector &)=delete
 
PetscVectoroperator= (PetscVector &&)=delete
 
virtual ~PetscVector ()
 
virtual void close () override
 
virtual void clear () override
 
virtual void zero () override
 
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
 
virtual void init (const numeric_index_type N, const bool fast=false, const ParallelType type=AUTOMATIC) override
 
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
 
virtual void init (const NumericVector< T > &other, const bool fast=false) override
 
virtual NumericVector< T > & operator= (const T s) override
 
virtual NumericVector< T > & operator= (const NumericVector< T > &v) override
 
virtual NumericVector< T > & operator= (const std::vector< T > &v) override
 
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
 
PetscScalar * get_array ()
 
const PetscScalar * get_array_read () const
 
void restore_array ()
 
virtual NumericVector< T > & operator+= (const NumericVector< T > &v) override
 
virtual NumericVector< T > & operator-= (const NumericVector< T > &v) override
 
virtual void reciprocal () override
 
virtual void conjugate () override
 
virtual void set (const numeric_index_type i, const T value) override
 
virtual void add (const numeric_index_type i, const T value) override
 
virtual void add (const T s) override
 
virtual void add (const NumericVector< T > &v) override
 
virtual void add (const T a, const NumericVector< T > &v) override
 
virtual void add_vector (const T *v, const std::vector< numeric_index_type > &dof_indices) override
 
virtual void add_vector (const NumericVector< T > &v, const SparseMatrix< T > &A) override
 
virtual void add_vector_transpose (const NumericVector< T > &v, const SparseMatrix< T > &A) override
 
void add_vector_conjugate_transpose (const NumericVector< T > &v, const SparseMatrix< T > &A)
 
virtual void insert (const T *v, const std::vector< numeric_index_type > &dof_indices) override
 
virtual void scale (const T factor) override
 
virtual NumericVector< T > & operator/= (const NumericVector< T > &v) override
 
virtual void abs () override
 
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
 
virtual void localize (NumericVector< T > &v_local) const override
 
virtual void localize (NumericVector< T > &v_local, const std::vector< numeric_index_type > &send_list) const override
 
virtual void localize (std::vector< T > &v_local, const std::vector< numeric_index_type > &indices) const override
 
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
 
virtual void localize_to_one (std::vector< T > &v_local, const processor_id_type proc_id=0) const override
 
virtual void pointwise_mult (const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
 
virtual void print_matlab (const std::string &name="") const override
 
virtual void create_subvector (NumericVector< T > &subvector, const std::vector< numeric_index_type > &rows) const override
 
virtual void swap (NumericVector< T > &v) override
 
Vec vec ()
 
template<>
void localize_to_one (std::vector< Real > &v_local, const processor_id_type pid) const
 
template<>
void localize_to_one (std::vector< Complex > &v_local, const processor_id_type pid) const
 
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
 
NumericVector< T > & operator*= (const T a)
 
NumericVector< T > & operator/= (const T a)
 
void add_vector (const std::vector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
virtual void add_vector (const NumericVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
void add_vector (const DenseVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
void add_vector (const NumericVector< T > &v, const ShellMatrix< T > &A)
 
void insert (const std::vector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
virtual void insert (const NumericVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
void insert (const DenseVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
void insert (const DenseSubVector< T > &v, const std::vector< numeric_index_type > &dof_indices)
 
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
 
template<>
void print (std::ostream &os) const
 
virtual void print_global (std::ostream &os=libMesh::out) const
 
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())
 
static std::string get_info ()
 
static void print_info (std::ostream &out=libMesh::out)
 
static unsigned int n_objects ()
 
static void enable_print_counter_info ()
 
static void disable_print_counter_info ()
 

Protected Types

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

Protected Member Functions

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

Protected Attributes

bool _is_closed
 
bool _is_initialized
 
ParallelType _type
 
const Parallel::Communicator_communicator
 

Static Protected Attributes

static Counts _counts
 
static Threads::atomic< unsigned int > _n_objects
 
static Threads::spin_mutex _mutex
 
static bool _enable_print_counter = true
 

Private Types

typedef std::unordered_map< numeric_index_type, numeric_index_typeGlobalToLocalMap
 

Private Member Functions

void _get_array (bool read_only) const
 
void _restore_array () const
 

Private Attributes

Vec _vec
 
std::atomic< bool > _array_is_present
 
bool _array_is_present
 
numeric_index_type _first
 
numeric_index_type _last
 
numeric_index_type _local_size
 
Vec _local_form
 
const PetscScalar * _read_only_values
 
PetscScalar * _values
 
std::mutex _petsc_vector_mutex
 
Threads::spin_mutex _petsc_vector_mutex
 
GlobalToLocalMap _global_to_local_map
 
bool _destroy_vec_on_exit
 
bool _values_manually_retrieved
 
bool _values_read_only
 

Detailed Description

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

NumericVector interface to PETSc Vec.

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

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 
)
inlineexplicit

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 }
numeric_index_type _last
Definition: petsc_vector.h:359
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
Definition: petsc_vector.h:427
numeric_index_type _first
Definition: petsc_vector.h:352

◆ PetscVector() [2/7]

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

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 }
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
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
Definition: petsc_vector.h:628

◆ 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 
)
inline

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 }
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
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
Definition: petsc_vector.h:628

◆ 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 
)
inline

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

Definition at line 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 }
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
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
Definition: petsc_vector.h:628

◆ PetscVector() [5/7]

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

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

Definition at line 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, ierr, 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 }
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
Definition: petsc_vector.h:427
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ 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 ( )
inlinevirtual

Definition at line 619 of file petsc_vector.h.

620 {
621  this->clear ();
622 }
virtual void clear() override
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

Definition at line 1348 of file petsc_vector.C.

References libMesh::GHOSTED, ierr, and libMesh::initialized().

1349 {
1350  libmesh_assert (this->initialized());
1351 
1352 #ifdef LIBMESH_HAVE_CXX11_THREAD
1353  std::atomic_thread_fence(std::memory_order_acquire);
1354 #else
1355  Threads::spin_mutex::scoped_lock lock(_petsc_vector_mutex);
1356 #endif
1357 
1358  // If the values have already been retrieved and we're currently
1359  // trying to get a non-read only view (ie read/write) and the
1360  // values are currently read only... then we need to restore
1361  // the array first... and then retrieve it again.
1362  if (_array_is_present && !read_only && _values_read_only)
1363  _restore_array();
1364 
1365  // If we already have a read/write array - and we're trying
1366  // to get a read only array - let's just use the read write
1367  if (_array_is_present && read_only && !_values_read_only)
1369 
1370  if (!_array_is_present)
1371  {
1372 #ifdef LIBMESH_HAVE_CXX11_THREAD
1373  std::lock_guard<std::mutex> lock(_petsc_vector_mutex);
1374 #endif
1375  if (!_array_is_present)
1376  {
1377  PetscErrorCode ierr=0;
1378  if (this->type() != GHOSTED)
1379  {
1380 #if PETSC_VERSION_LESS_THAN(3,2,0)
1381  // Vec{Get,Restore}ArrayRead were introduced in PETSc 3.2.0. If you
1382  // have an older PETSc than that, we'll do an ugly
1383  // const_cast and call VecGetArray() instead.
1384  ierr = VecGetArray(_vec, const_cast<PetscScalar **>(&_values));
1385  _values_read_only = false;
1386 #else
1387  if (read_only)
1388  {
1389  ierr = VecGetArrayRead(_vec, &_read_only_values);
1390  _values_read_only = true;
1391  }
1392  else
1393  {
1394  ierr = VecGetArray(_vec, &_values);
1395  _values_read_only = false;
1396  }
1397 #endif
1398  LIBMESH_CHKERR(ierr);
1399  _local_size = this->local_size();
1400  }
1401  else
1402  {
1403  ierr = VecGhostGetLocalForm (_vec,&_local_form);
1404  LIBMESH_CHKERR(ierr);
1405 
1406 #if PETSC_VERSION_LESS_THAN(3,2,0)
1407  // Vec{Get,Restore}ArrayRead were introduced in PETSc 3.2.0. If you
1408  // have an older PETSc than that, we'll do an ugly
1409  // const_cast and call VecGetArray() instead.
1410  ierr = VecGetArray(_local_form, const_cast<PetscScalar **>(&_values));
1411  _values_read_only = false;
1412 #else
1413  if (read_only)
1414  {
1415  ierr = VecGetArrayRead(_local_form, &_read_only_values);
1416  _values_read_only = true;
1417  }
1418  else
1419  {
1420  ierr = VecGetArray(_local_form, &_values);
1421  _values_read_only = false;
1422  }
1423 #endif
1424  LIBMESH_CHKERR(ierr);
1425 
1426  PetscInt my_local_size = 0;
1427  ierr = VecGetLocalSize(_local_form, &my_local_size);
1428  LIBMESH_CHKERR(ierr);
1429  _local_size = static_cast<numeric_index_type>(my_local_size);
1430  }
1431 
1432  { // cache ownership range
1433  PetscInt petsc_first=0, petsc_last=0;
1434  ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
1435  LIBMESH_CHKERR(ierr);
1436  _first = static_cast<numeric_index_type>(petsc_first);
1437  _last = static_cast<numeric_index_type>(petsc_last);
1438  }
1439 #ifdef LIBMESH_HAVE_CXX11_THREAD
1440  std::atomic_thread_fence(std::memory_order_release);
1441  _array_is_present.store(true, std::memory_order_relaxed);
1442 #else
1443  _array_is_present = true;
1444 #endif
1445  }
1446  }
1447 }
numeric_index_type _local_size
Definition: petsc_vector.h:364
virtual bool initialized() const
virtual numeric_index_type local_size() const override
Definition: petsc_vector.h:942
numeric_index_type _last
Definition: petsc_vector.h:359
PetscScalar * _values
Definition: petsc_vector.h:391
void _restore_array() const
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
std::mutex _petsc_vector_mutex
Definition: petsc_vector.h:399
const PetscScalar * _read_only_values
Definition: petsc_vector.h:381
dof_id_type numeric_index_type
Definition: id_types.h:92
ParallelType type() const
PetscErrorCode ierr
numeric_index_type _first
Definition: petsc_vector.h:352

◆ _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.

Definition at line 1452 of file petsc_vector.C.

References libMesh::GHOSTED, ierr, and libMesh::initialized().

Referenced by libMesh::PetscVector< T >::add(), libMesh::PetscVector< T >::create_subvector(), libMesh::PetscVector< T >::init(), and libMesh::PetscVector< T >::operator=().

1453 {
1455  libmesh_error_msg("PetscVector values were manually retrieved but are being automatically restored!");
1456 
1457 #ifdef LIBMESH_HAVE_CXX11_THREAD
1458  std::atomic_thread_fence(std::memory_order_acquire);
1459 #else
1460  Threads::spin_mutex::scoped_lock lock(_petsc_vector_mutex);
1461 #endif
1462 
1463  libmesh_assert (this->initialized());
1464  if (_array_is_present)
1465  {
1466 #ifdef LIBMESH_HAVE_CXX11_THREAD
1467  std::lock_guard<std::mutex> lock(_petsc_vector_mutex);
1468 #endif
1469 
1470  if (_array_is_present)
1471  {
1472  PetscErrorCode ierr=0;
1473  if (this->type() != GHOSTED)
1474  {
1475 #if PETSC_VERSION_LESS_THAN(3,2,0)
1476  // Vec{Get,Restore}ArrayRead were introduced in PETSc 3.2.0. If you
1477  // have an older PETSc than that, we'll do an ugly
1478  // const_cast and call VecRestoreArray() instead.
1479  ierr = VecRestoreArray (_vec, const_cast<PetscScalar **>(&_values));
1480 #else
1481  if (_values_read_only)
1482  ierr = VecRestoreArrayRead (_vec, &_read_only_values);
1483  else
1484  ierr = VecRestoreArray (_vec, &_values);
1485 #endif
1486 
1487  LIBMESH_CHKERR(ierr);
1488  _values = nullptr;
1489  }
1490  else
1491  {
1492 #if PETSC_VERSION_LESS_THAN(3,2,0)
1493  // Vec{Get,Restore}ArrayRead were introduced in PETSc 3.2.0. If you
1494  // have an older PETSc than that, we'll do an ugly
1495  // const_cast and call VecRestoreArray() instead.
1496  ierr = VecRestoreArray (_local_form, const_cast<PetscScalar **>(&_values));
1497 #else
1498  if (_values_read_only)
1499  ierr = VecRestoreArrayRead (_local_form, &_read_only_values);
1500  else
1501  ierr = VecRestoreArray (_local_form, &_values);
1502 #endif
1503  LIBMESH_CHKERR(ierr);
1504  _values = nullptr;
1505  ierr = VecGhostRestoreLocalForm (_vec,&_local_form);
1506  LIBMESH_CHKERR(ierr);
1507  _local_form = nullptr;
1508  _local_size = 0;
1509  }
1510 #ifdef LIBMESH_HAVE_CXX11_THREAD
1511  std::atomic_thread_fence(std::memory_order_release);
1512  _array_is_present.store(false, std::memory_order_relaxed);
1513 #else
1514  _array_is_present = false;
1515 #endif
1516  }
1517  }
1518 }
numeric_index_type _local_size
Definition: petsc_vector.h:364
virtual bool initialized() const
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
std::mutex _petsc_vector_mutex
Definition: petsc_vector.h:399
const PetscScalar * _read_only_values
Definition: petsc_vector.h:381
ParallelType type() const
PetscErrorCode ierr

◆ abs()

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

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

Implements libMesh::NumericVector< T >.

Definition at line 440 of file petsc_vector.C.

References libMesh::GHOSTED, and ierr.

441 {
442  this->_restore_array();
443 
444  PetscErrorCode ierr = 0;
445 
446  if (this->type() != GHOSTED)
447  {
448  ierr = VecAbs(_vec);
449  LIBMESH_CHKERR(ierr);
450  }
451  else
452  {
453  Vec loc_vec;
454  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
455  LIBMESH_CHKERR(ierr);
456 
457  ierr = VecAbs(loc_vec);
458  LIBMESH_CHKERR(ierr);
459 
460  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
461  LIBMESH_CHKERR(ierr);
462  }
463 }
void _restore_array() const
ParallelType type() const
PetscErrorCode ierr

◆ add() [1/4]

template<typename T >
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 >.

Definition at line 180 of file petsc_vector.C.

References ierr, and value.

181 {
182  this->_restore_array();
183  libmesh_assert_less (i, size());
184 
185  PetscErrorCode ierr=0;
186  PetscInt i_val = static_cast<PetscInt>(i);
187  PetscScalar petsc_value = static_cast<PetscScalar>(value);
188 
189  ierr = VecSetValues (_vec, 1, &i_val, &petsc_value, ADD_VALUES);
190  LIBMESH_CHKERR(ierr);
191 
192  this->_is_closed = false;
193 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
void _restore_array() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ add() [2/4]

template<typename T >
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 >.

Definition at line 321 of file petsc_vector.C.

322 {
323  this->_get_array(false);
324 
325  for (numeric_index_type i=0; i<_local_size; i++)
326  _values[i] += v_in;
327 }
numeric_index_type _local_size
Definition: petsc_vector.h:364
void _get_array(bool read_only) const
PetscScalar * _values
Definition: petsc_vector.h:391
dof_id_type numeric_index_type
Definition: id_types.h:92

◆ add() [3/4]

template<typename T >
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 >.

Definition at line 332 of file petsc_vector.C.

333 {
334  this->add (1., v);
335 }
virtual void add(const numeric_index_type i, const T value) override
Definition: petsc_vector.C:180

◆ add() [4/4]

template<typename T >
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 >.

Definition at line 340 of file petsc_vector.C.

References libMesh::PetscVector< T >::_restore_array(), libMesh::PetscVector< T >::_vec, libMesh::GHOSTED, ierr, and libMesh::PetscVector< T >::size().

341 {
342  this->_restore_array();
343 
344  PetscErrorCode ierr = 0;
345  PetscScalar a = static_cast<PetscScalar>(a_in);
346 
347  // Make sure the NumericVector passed in is really a PetscVector
348  const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
349  v->_restore_array();
350 
351  libmesh_assert_equal_to (this->size(), v->size());
352 
353  if (this->type() != GHOSTED)
354  {
355  ierr = VecAXPY(_vec, a, v->_vec);
356  LIBMESH_CHKERR(ierr);
357  }
358  else
359  {
360  Vec loc_vec;
361  Vec v_loc_vec;
362  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
363  LIBMESH_CHKERR(ierr);
364  ierr = VecGhostGetLocalForm (v->_vec,&v_loc_vec);
365  LIBMESH_CHKERR(ierr);
366 
367  ierr = VecAXPY(loc_vec, a, v_loc_vec);
368  LIBMESH_CHKERR(ierr);
369 
370  ierr = VecGhostRestoreLocalForm (v->_vec,&v_loc_vec);
371  LIBMESH_CHKERR(ierr);
372  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
373  LIBMESH_CHKERR(ierr);
374  }
375 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
void _restore_array() const
ParallelType type() const
PetscErrorCode ierr

◆ add_vector() [1/6]

template<typename T >
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 >.

Definition at line 198 of file petsc_vector.C.

References ierr.

200 {
201  // If we aren't adding anything just return
202  if (dof_indices.empty())
203  return;
204 
205  this->_restore_array();
206 
207  PetscErrorCode ierr=0;
208  const PetscInt * i_val = reinterpret_cast<const PetscInt *>(dof_indices.data());
209  const PetscScalar * petsc_value = static_cast<const PetscScalar *>(v);
210 
211  ierr = VecSetValues (_vec, cast_int<PetscInt>(dof_indices.size()),
212  i_val, petsc_value, ADD_VALUES);
213  LIBMESH_CHKERR(ierr);
214 
215  this->_is_closed = false;
216 }
void _restore_array() const
PetscErrorCode ierr

◆ add_vector() [2/6]

template<typename T >
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 >.

Definition at line 221 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, A, and ierr.

223 {
224  this->_restore_array();
225  // Make sure the data passed in are really of Petsc types
226  const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
227  const PetscMatrix<T> * A = cast_ptr<const PetscMatrix<T> *>(&A_in);
228 
229  PetscErrorCode ierr=0;
230 
231  // We shouldn't close() the matrix for you, as that would potentially modify the state of a const object.
232  if (!A->closed())
233  {
234  libmesh_warning("Matrix A must be assembled before calling PetscVector::add_vector(v, A).\n"
235  "Please update your code, as this warning will become an error in a future release.");
236  libmesh_deprecated();
237  const_cast<PetscMatrix<T> *>(A)->close();
238  }
239 
240  // The const_cast<> is not elegant, but it is required since PETSc
241  // expects a non-const Mat.
242  ierr = MatMultAdd(const_cast<PetscMatrix<T> *>(A)->mat(), v->_vec, _vec, _vec);
243  LIBMESH_CHKERR(ierr);
244 }
void _restore_array() const
PetscErrorCode ierr
static PetscErrorCode Mat * A
virtual void close() override
Definition: petsc_vector.h:810

◆ 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 
)
inlineinherited

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 835 of file numeric_vector.h.

837 {
838  libmesh_assert(v.size() == dof_indices.size());
839  if (!v.empty())
840  this->add_vector(v.data(), dof_indices);
841 }
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)

◆ add_vector() [4/6]

template<typename T>
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).

Definition at line 372 of file numeric_vector.C.

374 {
375  const std::size_t n = dof_indices.size();
376  libmesh_assert_equal_to(v.size(), n);
377  for (numeric_index_type i=0; i != n; i++)
378  this->add (dof_indices[i], v(i));
379 }
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual void add(const numeric_index_type i, const T value)=0

◆ 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 
)
inlineinherited

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 847 of file numeric_vector.h.

849 {
850  libmesh_assert(v.size() == dof_indices.size());
851  if (!v.empty())
852  this->add_vector(&v(0), dof_indices);
853 }
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)

◆ 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.

Definition at line 384 of file numeric_vector.C.

386 {
387  a.vector_mult_add(*this,v);
388 }

◆ 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.

Definition at line 277 of file petsc_vector.C.

279 {
280  libmesh_error_msg("MatMultHermitianTranspose was introduced in PETSc 3.1.0," \
281  << "No one has made it backwards compatible with older " \
282  << "versions of PETSc so far.");
283 }

◆ add_vector_transpose()

template<typename T >
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 >.

Definition at line 249 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, A, and ierr.

251 {
252  this->_restore_array();
253  // Make sure the data passed in are really of Petsc types
254  const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
255  const PetscMatrix<T> * A = cast_ptr<const PetscMatrix<T> *>(&A_in);
256 
257  PetscErrorCode ierr=0;
258 
259  // We shouldn't close() the matrix for you, as that would potentially modify the state of a const object.
260  if (!A->closed())
261  {
262  libmesh_warning("Matrix A must be assembled before calling PetscVector::add_vector_transpose(v, A).\n"
263  "Please update your code, as this warning will become an error in a future release.");
264  libmesh_deprecated();
265  const_cast<PetscMatrix<T> *>(A)->close();
266  }
267 
268  // The const_cast<> is not elegant, but it is required since PETSc
269  // expects a non-const Mat.
270  ierr = MatMultTransposeAdd(const_cast<PetscMatrix<T> *>(A)->mat(), v->_vec, _vec, _vec);
271  LIBMESH_CHKERR(ierr);
272 }
void _restore_array() const
PetscErrorCode ierr
static PetscErrorCode Mat * A
virtual void close() override
Definition: petsc_vector.h:810

◆ build()

template<typename T >
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

Definition at line 48 of file numeric_vector.C.

Referenced by libMesh::ExactSolution::_compute_error(), libMesh::UniformRefinementEstimator::_estimate_error(), libMesh::System::add_vector(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::EquationSystems::build_parallel_solution_vector(), libMesh::System::calculate_norm(), libMesh::AdjointRefinementEstimator::estimate_error(), libMesh::ExactErrorEstimator::estimate_error(), and libMesh::CondensedEigenSystem::get_eigenpair().

49 {
50  // Build the appropriate vector
51  switch (solver_package)
52  {
53 
54 #ifdef LIBMESH_HAVE_LASPACK
55  case LASPACK_SOLVERS:
56  return libmesh_make_unique<LaspackVector<T>>(comm, AUTOMATIC);
57 #endif
58 
59 #ifdef LIBMESH_HAVE_PETSC
60  case PETSC_SOLVERS:
61  return libmesh_make_unique<PetscVector<T>>(comm, AUTOMATIC);
62 #endif
63 
64 #ifdef LIBMESH_TRILINOS_HAVE_EPETRA
65  case TRILINOS_SOLVERS:
66  return libmesh_make_unique<EpetraVector<T>>(comm, AUTOMATIC);
67 #endif
68 
69 #ifdef LIBMESH_HAVE_EIGEN
70  case EIGEN_SOLVERS:
71  return libmesh_make_unique<EigenSparseVector<T>>(comm, AUTOMATIC);
72 #endif
73 
74  default:
75  return libmesh_make_unique<DistributedVector<T>>(comm, AUTOMATIC);
76  }
77 }
EIGEN_SOLVERS
Definition: libmesh.C:246
TRILINOS_SOLVERS
Definition: libmesh.C:244
const Parallel::Communicator & comm() const
LASPACK_SOLVERS
Definition: libmesh.C:248

◆ clear()

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

Restores the NumericVector<T> to a pristine state.

Reimplemented from libMesh::NumericVector< T >.

Definition at line 838 of file petsc_vector.h.

References libMesh::libMeshPrivateData::_is_initialized, ierr, and 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 }
virtual bool initialized() const
void _restore_array() const
GlobalToLocalMap _global_to_local_map
Definition: petsc_vector.h:427
PetscErrorCode ierr

◆ clone()

template<typename T >
std::unique_ptr< NumericVector< T > > libMesh::PetscVector< T >::clone ( ) const
inlineoverridevirtual
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 ( )
inlineoverridevirtual

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, and ierr.

Referenced by libMesh::__libmesh_petsc_diff_solver_monitor(), libMesh::SlepcEigenSolver< T >::get_eigenpair(), libMesh::PetscVector< T >::localize(), and libMesh::PetscLinearSolver< T >::solve().

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 }
void _restore_array() const
ParallelType type() const
PetscErrorCode ierr

◆ closed()

template<typename T>
virtual bool libMesh::NumericVector< T >::closed ( ) const
inlinevirtualinherited

◆ comm()

const Parallel::Communicator& libMesh::ParallelObject::comm ( ) const
inlineinherited
Returns
A reference to the Parallel::Communicator object used by this mesh.

Definition at line 89 of file parallel_object.h.

References libMesh::ParallelObject::_communicator.

Referenced by libMesh::__libmesh_petsc_diff_solver_jacobian(), libMesh::__libmesh_petsc_diff_solver_monitor(), libMesh::__libmesh_petsc_diff_solver_residual(), libMesh::__libmesh_tao_equality_constraints(), libMesh::__libmesh_tao_equality_constraints_jacobian(), libMesh::__libmesh_tao_gradient(), libMesh::__libmesh_tao_hessian(), libMesh::__libmesh_tao_inequality_constraints(), libMesh::__libmesh_tao_inequality_constraints_jacobian(), libMesh::__libmesh_tao_objective(), libMesh::MeshRefinement::_coarsen_elements(), libMesh::ExactSolution::_compute_error(), libMesh::UniformRefinementEstimator::_estimate_error(), libMesh::BoundaryInfo::_find_id_maps(), libMesh::SlepcEigenSolver< T >::_petsc_shell_matrix_get_diagonal(), libMesh::PetscLinearSolver< T >::_petsc_shell_matrix_get_diagonal(), libMesh::SlepcEigenSolver< T >::_petsc_shell_matrix_mult(), libMesh::PetscLinearSolver< T >::_petsc_shell_matrix_mult(), libMesh::PetscLinearSolver< T >::_petsc_shell_matrix_mult_add(), libMesh::EquationSystems::_read_impl(), libMesh::MeshRefinement::_refine_elements(), libMesh::MeshRefinement::_smooth_flags(), libMesh::PetscDMWrapper::add_dofs_helper(), libMesh::PetscDMWrapper::add_dofs_to_section(), libMesh::ImplicitSystem::add_matrix(), libMesh::System::add_vector(), libMesh::UnstructuredMesh::all_second_order(), libMesh::MeshTools::Modification::all_tri(), libMesh::LaplaceMeshSmoother::allgather_graph(), libMesh::FEMSystem::assemble_qoi(), libMesh::MeshCommunication::assign_global_indices(), libMesh::DofMap::attach_matrix(), libMesh::MeshTools::Generation::build_extrusion(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::EquationSystems::build_parallel_solution_vector(), libMesh::PetscDMWrapper::build_section(), libMesh::PetscDMWrapper::build_sf(), libMesh::MeshBase::cache_elem_dims(), libMesh::System::calculate_norm(), libMesh::DofMap::check_dirichlet_bcid_consistency(), libMesh::PetscDMWrapper::check_section_n_dofs(), libMesh::Nemesis_IO_Helper::compute_num_global_elem_blocks(), libMesh::Nemesis_IO_Helper::compute_num_global_nodesets(), libMesh::Nemesis_IO_Helper::compute_num_global_sidesets(), libMesh::Problem_Interface::computeF(), libMesh::Problem_Interface::computeJacobian(), libMesh::Problem_Interface::computePreconditioner(), libMesh::ExodusII_IO::copy_elemental_solution(), libMesh::MeshTools::correct_node_proc_ids(), libMesh::MeshTools::create_bounding_box(), libMesh::MeshTools::create_nodal_bounding_box(), libMesh::MeshRefinement::create_parent_error_vector(), libMesh::MeshTools::create_processor_bounding_box(), libMesh::MeshTools::create_subdomain_bounding_box(), libMesh::MeshCommunication::delete_remote_elements(), libMesh::DofMap::distribute_dofs(), DMlibMeshFunction(), DMlibMeshJacobian(), DMlibMeshSetSystem_libMesh(), DMVariableBounds_libMesh(), libMesh::MeshRefinement::eliminate_unrefined_patches(), libMesh::EpetraVector< T >::EpetraVector(), libMesh::WeightedPatchRecoveryErrorEstimator::estimate_error(), libMesh::PatchRecoveryErrorEstimator::estimate_error(), libMesh::JumpErrorEstimator::estimate_error(), libMesh::AdjointRefinementEstimator::estimate_error(), libMesh::ExactErrorEstimator::estimate_error(), libMesh::MeshRefinement::flag_elements_by_elem_fraction(), libMesh::MeshRefinement::flag_elements_by_error_fraction(), libMesh::MeshRefinement::flag_elements_by_nelem_target(), libMesh::CondensedEigenSystem::get_eigenpair(), libMesh::DofMap::get_info(), libMesh::ImplicitSystem::get_linear_solver(), libMesh::LocationMap< T >::init(), libMesh::TimeSolver::init(), libMesh::SystemSubsetBySubdomain::init(), libMesh::PetscDMWrapper::init_and_attach_petscdm(), libMesh::EigenSystem::init_data(), libMesh::EigenSystem::init_matrices(), libMesh::OptimizationSystem::initialize_equality_constraints_storage(), libMesh::OptimizationSystem::initialize_inequality_constraints_storage(), libMesh::MeshTools::libmesh_assert_consistent_distributed(), libMesh::MeshTools::libmesh_assert_consistent_distributed_nodes(), libMesh::MeshTools::libmesh_assert_contiguous_dof_ids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_new_node_procids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Elem >(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_topology_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_valid_boundary_ids(), libMesh::MeshTools::libmesh_assert_valid_dof_ids(), libMesh::MeshTools::libmesh_assert_valid_neighbors(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_flags(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_object_ids(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_p_levels(), libMesh::MeshTools::libmesh_assert_valid_refinement_flags(), libMesh::MeshTools::libmesh_assert_valid_unique_ids(), libMesh::libmesh_petsc_snes_fd_residual(), libMesh::libmesh_petsc_snes_jacobian(), libMesh::libmesh_petsc_snes_mffd_residual(), libMesh::libmesh_petsc_snes_postcheck(), libMesh::libmesh_petsc_snes_residual(), libMesh::libmesh_petsc_snes_residual_helper(), libMesh::MeshRefinement::limit_level_mismatch_at_edge(), libMesh::MeshRefinement::limit_level_mismatch_at_node(), libMesh::MeshRefinement::limit_overrefined_boundary(), libMesh::MeshRefinement::limit_underrefined_boundary(), libMesh::MeshRefinement::make_coarsening_compatible(), libMesh::MeshCommunication::make_elems_parallel_consistent(), libMesh::MeshRefinement::make_flags_parallel_consistent(), libMesh::MeshCommunication::make_new_node_proc_ids_parallel_consistent(), libMesh::MeshCommunication::make_new_nodes_parallel_consistent(), libMesh::MeshCommunication::make_node_ids_parallel_consistent(), libMesh::MeshCommunication::make_node_proc_ids_parallel_consistent(), libMesh::MeshCommunication::make_node_unique_ids_parallel_consistent(), libMesh::MeshCommunication::make_nodes_parallel_consistent(), libMesh::MeshCommunication::make_p_levels_parallel_consistent(), libMesh::MeshRefinement::make_refinement_compatible(), libMesh::FEMSystem::mesh_position_set(), libMesh::DistributedMesh::n_active_elem(), libMesh::MeshTools::n_active_levels(), libMesh::BoundaryInfo::n_boundary_conds(), libMesh::BoundaryInfo::n_edge_conds(), libMesh::CondensedEigenSystem::n_global_non_condensed_dofs(), libMesh::MeshTools::n_levels(), libMesh::BoundaryInfo::n_nodeset_conds(), libMesh::MeshTools::n_p_levels(), libMesh::BoundaryInfo::n_shellface_conds(), libMesh::DistributedMesh::parallel_max_elem_id(), libMesh::DistributedMesh::parallel_max_node_id(), libMesh::ReplicatedMesh::parallel_max_unique_id(), libMesh::DistributedMesh::parallel_max_unique_id(), libMesh::DistributedMesh::parallel_n_elem(), libMesh::DistributedMesh::parallel_n_nodes(), libMesh::SparsityPattern::Build::parallel_sync(), libMesh::MeshTools::paranoid_n_levels(), libMesh::petsc_auto_fieldsplit(), libMesh::System::point_gradient(), libMesh::System::point_hessian(), libMesh::System::point_value(), libMesh::MeshBase::prepare_for_use(), libMesh::Nemesis_IO::read(), libMesh::XdrIO::read(), libMesh::CheckpointIO::read_header(), libMesh::XdrIO::read_header(), libMesh::System::read_header(), libMesh::System::read_legacy_data(), libMesh::System::read_SCALAR_dofs(), libMesh::XdrIO::read_serialized_bc_names(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::System::read_serialized_blocked_dof_objects(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::XdrIO::read_serialized_subdomain_names(), libMesh::System::read_serialized_vector(), libMesh::MeshBase::recalculate_n_partitions(), libMesh::MeshRefinement::refine_and_coarsen_elements(), libMesh::DistributedMesh::renumber_dof_objects(), libMesh::CheckpointIO::select_split_config(), libMesh::DofMap::set_nonlocal_dof_objects(), libMesh::PetscDMWrapper::set_point_range_in_section(), libMesh::PetscDiffSolver::setup_petsc_data(), libMesh::LaplaceMeshSmoother::smooth(), libMesh::split_mesh(), libMesh::MeshBase::subdomain_ids(), libMesh::BoundaryInfo::sync(), libMesh::MeshRefinement::test_level_one(), libMesh::MeshRefinement::test_unflagged(), libMesh::MeshTools::total_weight(), libMesh::MeshRefinement::uniformly_coarsen(), libMesh::NameBasedIO::write(), libMesh::XdrIO::write(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::System::write_serialized_blocked_dof_objects(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), and libMesh::XdrIO::write_serialized_nodesets().

90  { return _communicator; }
const Parallel::Communicator & _communicator

◆ compare()

template<typename T>
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.

Definition at line 104 of file numeric_vector.C.

106 {
107  libmesh_assert (this->initialized());
108  libmesh_assert (other_vector.initialized());
109  libmesh_assert_equal_to (this->first_local_index(), other_vector.first_local_index());
110  libmesh_assert_equal_to (this->last_local_index(), other_vector.last_local_index());
111 
112  int first_different_i = std::numeric_limits<int>::max();
114 
115  do
116  {
117  if (std::abs((*this)(i) - other_vector(i)) > threshold)
118  first_different_i = i;
119  else
120  i++;
121  }
122  while (first_different_i==std::numeric_limits<int>::max()
123  && i<last_local_index());
124 
125  // Find the correct first differing index in parallel
126  this->comm().min(first_different_i);
127 
128  if (first_different_i == std::numeric_limits<int>::max())
129  return -1;
130 
131  return first_different_i;
132 }
double abs(double a)
virtual bool initialized() const
const Parallel::Communicator & comm() const
long double max(long double a, double b)
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type first_local_index() const =0
virtual numeric_index_type last_local_index() const =0

◆ conjugate()

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

Negates the imaginary component of each entry in the vector.

Implements libMesh::NumericVector< T >.

Definition at line 168 of file petsc_vector.C.

References ierr.

169 {
170  PetscErrorCode ierr = 0;
171 
172  // We just call the PETSc VecConjugate
173  ierr = VecConjugate(_vec);
174  LIBMESH_CHKERR(ierr);
175 }
PetscErrorCode ierr

◆ create_subvector()

template<typename T >
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 >.

Definition at line 1259 of file petsc_vector.C.

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

1261 {
1262  this->_restore_array();
1263 
1264  // PETSc data structures
1265  IS parent_is, subvector_is;
1266  VecScatter scatter;
1267  PetscErrorCode ierr = 0;
1268 
1269  // Make sure the passed in subvector is really a PetscVector
1270  PetscVector<T> * petsc_subvector = cast_ptr<PetscVector<T> *>(&subvector);
1271 
1272  // If the petsc_subvector is already initialized, we assume that the
1273  // user has already allocated the *correct* amount of space for it.
1274  // If not, we use the appropriate PETSc routines to initialize it.
1275  if (!petsc_subvector->initialized())
1276  {
1277  // Initialize the petsc_subvector to have enough space to hold
1278  // the entries which will be scattered into it. Note: such an
1279  // init() function (where we let PETSc decide the number of local
1280  // entries) is not currently offered by the PetscVector
1281  // class. Should we differentiate here between sequential and
1282  // parallel vector creation based on this->n_processors() ?
1283  ierr = VecCreateMPI(this->comm().get(),
1284  PETSC_DECIDE, // n_local
1285  cast_int<PetscInt>(rows.size()), // n_global
1286  &(petsc_subvector->_vec));
1287  LIBMESH_CHKERR(ierr);
1288 
1289  ierr = VecSetFromOptions (petsc_subvector->_vec);
1290  LIBMESH_CHKERR(ierr);
1291 
1292  // Mark the subvector as initialized
1293  petsc_subvector->_is_initialized = true;
1294  }
1295  else
1296  {
1297  petsc_subvector->_restore_array();
1298  }
1299 
1300  // Use iota to fill an array with entries [0,1,2,3,4,...rows.size()]
1301  std::vector<PetscInt> idx(rows.size());
1302  std::iota (idx.begin(), idx.end(), 0);
1303 
1304  // Construct index sets
1305  ierr = ISCreateLibMesh(this->comm().get(),
1306  cast_int<PetscInt>(rows.size()),
1307  numeric_petsc_cast(rows.data()),
1309  &parent_is);
1310  LIBMESH_CHKERR(ierr);
1311 
1312  ierr = ISCreateLibMesh(this->comm().get(),
1313  cast_int<PetscInt>(rows.size()),
1314  idx.data(),
1316  &subvector_is);
1317  LIBMESH_CHKERR(ierr);
1318 
1319  // Construct the scatter object
1320  ierr = LibMeshVecScatterCreate(this->_vec,
1321  parent_is,
1322  petsc_subvector->_vec,
1323  subvector_is,
1324  &scatter); LIBMESH_CHKERR(ierr);
1325 
1326  // Actually perform the scatter
1327  ierr = VecScatterBegin(scatter,
1328  this->_vec,
1329  petsc_subvector->_vec,
1330  INSERT_VALUES,
1331  SCATTER_FORWARD); LIBMESH_CHKERR(ierr);
1332 
1333  ierr = VecScatterEnd(scatter,
1334  this->_vec,
1335  petsc_subvector->_vec,
1336  INSERT_VALUES,
1337  SCATTER_FORWARD); LIBMESH_CHKERR(ierr);
1338 
1339  // Clean up
1340  ierr = LibMeshISDestroy(&parent_is); LIBMESH_CHKERR(ierr);
1341  ierr = LibMeshISDestroy(&subvector_is); LIBMESH_CHKERR(ierr);
1342  ierr = LibMeshVecScatterDestroy(&scatter); LIBMESH_CHKERR(ierr);
1343 }
const Parallel::Communicator & comm() const
void iota(ForwardIter first, ForwardIter last, T value)
Definition: utility.h:57
void _restore_array() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
PetscErrorCode ierr
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ disable_print_counter_info()

void libMesh::ReferenceCounter::disable_print_counter_info ( )
staticinherited

Definition at line 106 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

Referenced by libMesh::LibMeshInit::LibMeshInit().

107 {
108  _enable_print_counter = false;
109  return;
110 }

◆ dot()

template<typename T >
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 >.

Definition at line 466 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, ierr, and value.

467 {
468  this->_restore_array();
469 
470  // Error flag
471  PetscErrorCode ierr = 0;
472 
473  // Return value
474  PetscScalar value=0.;
475 
476  // Make sure the NumericVector passed in is really a PetscVector
477  const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
478 
479  // 2.3.x (at least) style. Untested for previous versions.
480  ierr = VecDot(this->_vec, v->_vec, &value);
481  LIBMESH_CHKERR(ierr);
482 
483  return static_cast<T>(value);
484 }
void _restore_array() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ el()

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

Definition at line 346 of file numeric_vector.h.

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

◆ enable_print_counter_info()

void libMesh::ReferenceCounter::enable_print_counter_info ( )
staticinherited

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

Definition at line 100 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

101 {
102  _enable_print_counter = true;
103  return;
104 }

◆ first_local_index()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::first_local_index ( ) const
inlineoverridevirtual
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 ierr, and 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
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr
numeric_index_type _first
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
inlineoverridevirtual

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
Definition: petsc_vector.h:364
void _get_array(bool read_only) const
const PetscScalar * _read_only_values
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
inlineinherited

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 820 of file numeric_vector.h.

822 {
823  const std::size_t num = index.size();
824  values.resize(num);
825  if (!num)
826  return;
827 
828  this->get(index, values.data());
829 }

◆ get_array()

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

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
PetscScalar * _values
Definition: petsc_vector.h:391

◆ get_array_read()

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

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
const PetscScalar * _read_only_values
Definition: petsc_vector.h:381

◆ get_info()

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

Gets a string containing the reference information.

Definition at line 47 of file reference_counter.C.

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

Referenced by libMesh::ReferenceCounter::print_info().

48 {
49 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
50 
51  std::ostringstream oss;
52 
53  oss << '\n'
54  << " ---------------------------------------------------------------------------- \n"
55  << "| Reference count information |\n"
56  << " ---------------------------------------------------------------------------- \n";
57 
58  for (const auto & pr : _counts)
59  {
60  const std::string name(pr.first);
61  const unsigned int creations = pr.second.first;
62  const unsigned int destructions = pr.second.second;
63 
64  oss << "| " << name << " reference count information:\n"
65  << "| Creations: " << creations << '\n'
66  << "| Destructions: " << destructions << '\n';
67  }
68 
69  oss << " ---------------------------------------------------------------------------- \n";
70 
71  return oss.str();
72 
73 #else
74 
75  return "";
76 
77 #endif
78 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42

◆ global_relative_compare()

template<typename T>
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.

Definition at line 169 of file numeric_vector.C.

171 {
172  libmesh_assert (this->initialized());
173  libmesh_assert (other_vector.initialized());
174  libmesh_assert_equal_to (this->first_local_index(), other_vector.first_local_index());
175  libmesh_assert_equal_to (this->last_local_index(), other_vector.last_local_index());
176 
177  int first_different_i = std::numeric_limits<int>::max();
179 
180  const Real my_norm = this->linfty_norm();
181  const Real other_norm = other_vector.linfty_norm();
182  const Real abs_threshold = std::max(my_norm, other_norm) * threshold;
183 
184  do
185  {
186  if (std::abs((*this)(i) - other_vector(i) ) > abs_threshold)
187  first_different_i = i;
188  else
189  i++;
190  }
191  while (first_different_i==std::numeric_limits<int>::max()
192  && i<last_local_index());
193 
194  // Find the correct first differing index in parallel
195  this->comm().min(first_different_i);
196 
197  if (first_different_i == std::numeric_limits<int>::max())
198  return -1;
199 
200  return first_different_i;
201 }
double abs(double a)
virtual bool initialized() const
const Parallel::Communicator & comm() const
long double max(long double a, double b)
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type first_local_index() const =0
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual numeric_index_type last_local_index() const =0
virtual Real linfty_norm() const =0

◆ increment_constructor_count()

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

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)
Definition: elem_quality.C:42
spin_mutex spin_mtx
Definition: threads.C:29

◆ increment_destructor_count()

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

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)
Definition: elem_quality.C:42
spin_mutex spin_mtx
Definition: threads.C:29

◆ 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.

Definition at line 487 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, ierr, and value.

488 {
489  this->_restore_array();
490 
491  // Error flag
492  PetscErrorCode ierr = 0;
493 
494  // Return value
495  PetscScalar value=0.;
496 
497  // Make sure the NumericVector passed in is really a PetscVector
498  const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
499 
500  // 2.3.x (at least) style. Untested for previous versions.
501  ierr = VecTDot(this->_vec, v->_vec, &value);
502  LIBMESH_CHKERR(ierr);
503 
504  return static_cast<T>(value);
505 }
void _restore_array() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ init() [1/4]

template<typename T >
void libMesh::PetscVector< T >::init ( const numeric_index_type  n,
const numeric_index_type  n_local,
const bool  fast = false,
const ParallelType  ptype = AUTOMATIC 
)
inlineoverridevirtual

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::libMeshPrivateData::_is_initialized, libMesh::AUTOMATIC, ierr, libMesh::initialized(), libMesh::PARALLEL, libMesh::SERIAL, and libMesh::zero.

Referenced by libMesh::PetscVector< T >::localize(), and 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
Definition: petsc_vector.h:862
virtual bool initialized() const
const Parallel::Communicator & comm() const
virtual void clear() override
Definition: petsc_vector.h:838
PetscErrorCode ierr

◆ init() [2/4]

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

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
Definition: petsc_vector.h:628

◆ init() [3/4]

template<typename T>
virtual void libMesh::PetscVector< T >::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  ptype = 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 
)
inlineoverridevirtual

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::libMeshPrivateData::_is_initialized, libMesh::PetscVector< T >::_restore_array(), libMesh::NumericVector< T >::_type, libMesh::PetscVector< T >::_vec, ierr, libMesh::NumericVector< T >::initialized(), libMesh::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
Definition: petsc_vector.h:862
virtual bool initialized() const
GlobalToLocalMap _global_to_local_map
Definition: petsc_vector.h:427
virtual void clear() override
Definition: petsc_vector.h:838
PetscErrorCode ierr

◆ initialized()

template<typename T>
virtual bool libMesh::NumericVector< T >::initialized ( ) const
inlinevirtualinherited

◆ insert() [1/5]

template<typename T >
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 >.

Definition at line 380 of file petsc_vector.C.

References ierr, and libMesh::numeric_petsc_cast().

382 {
383  if (dof_indices.empty())
384  return;
385 
386  this->_restore_array();
387 
388  PetscErrorCode ierr=0;
389  PetscInt * idx_values = numeric_petsc_cast(dof_indices.data());
390  ierr = VecSetValues (_vec, cast_int<PetscInt>(dof_indices.size()),
391  idx_values, v, INSERT_VALUES);
392  LIBMESH_CHKERR(ierr);
393 
394  this->_is_closed = false;
395 }
void _restore_array() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
PetscErrorCode ierr

◆ insert() [2/5]

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

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

Definition at line 859 of file numeric_vector.h.

861 {
862  libmesh_assert(v.size() == dof_indices.size());
863  if (!v.empty())
864  this->insert(v.data(), dof_indices);
865 }
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)

◆ insert() [3/5]

template<typename T>
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.

Definition at line 92 of file numeric_vector.C.

94 {
95  libmesh_assert_equal_to (V.size(), dof_indices.size());
96 
97  for (numeric_index_type i=0; i<dof_indices.size(); i++)
98  this->set (dof_indices[i], V(i));
99 }
dof_id_type numeric_index_type
Definition: id_types.h:92

◆ insert() [4/5]

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

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

Definition at line 871 of file numeric_vector.h.

873 {
874  libmesh_assert(v.size() == dof_indices.size());
875  if (!v.empty())
876  this->insert(&v(0), dof_indices);
877 }
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)

◆ insert() [5/5]

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

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

Definition at line 883 of file numeric_vector.h.

885 {
886  libmesh_assert(v.size() == dof_indices.size());
887  if (!v.empty())
888  this->insert(&v(0), dof_indices);
889 }
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)

◆ l1_norm()

template<typename T >
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 >.

Definition at line 57 of file petsc_vector.C.

References libMesh::closed(), ierr, libMesh::Real, and value.

58 {
59  this->_restore_array();
60  libmesh_assert(this->closed());
61 
62  PetscErrorCode ierr=0;
63  PetscReal value=0.;
64 
65  ierr = VecNorm (_vec, NORM_1, &value);
66  LIBMESH_CHKERR(ierr);
67 
68  return static_cast<Real>(value);
69 }
void _restore_array() const
virtual bool closed() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ l2_norm()

template<typename T >
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 >.

Definition at line 74 of file petsc_vector.C.

References libMesh::closed(), ierr, libMesh::Real, and value.

75 {
76  this->_restore_array();
77  libmesh_assert(this->closed());
78 
79  PetscErrorCode ierr=0;
80  PetscReal value=0.;
81 
82  ierr = VecNorm (_vec, NORM_2, &value);
83  LIBMESH_CHKERR(ierr);
84 
85  return static_cast<Real>(value);
86 }
void _restore_array() const
virtual bool closed() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ last_local_index()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::last_local_index ( ) const
inlineoverridevirtual
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 ierr, and 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
numeric_index_type _last
Definition: petsc_vector.h:359
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ linfty_norm()

template<typename T >
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 >.

Definition at line 92 of file petsc_vector.C.

References libMesh::closed(), ierr, libMesh::Real, and value.

93 {
94  this->_restore_array();
95  libmesh_assert(this->closed());
96 
97  PetscErrorCode ierr=0;
98  PetscReal value=0.;
99 
100  ierr = VecNorm (_vec, NORM_INFINITY, &value);
101  LIBMESH_CHKERR(ierr);
102 
103  return static_cast<Real>(value);
104 }
void _restore_array() const
virtual bool closed() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ local_relative_compare()

template<typename T>
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.

Definition at line 136 of file numeric_vector.C.

138 {
139  libmesh_assert (this->initialized());
140  libmesh_assert (other_vector.initialized());
141  libmesh_assert_equal_to (this->first_local_index(), other_vector.first_local_index());
142  libmesh_assert_equal_to (this->last_local_index(), other_vector.last_local_index());
143 
144  int first_different_i = std::numeric_limits<int>::max();
146 
147  do
148  {
149  if (std::abs((*this)(i) - other_vector(i)) > threshold *
150  std::max(std::abs((*this)(i)), std::abs(other_vector(i))))
151  first_different_i = i;
152  else
153  i++;
154  }
155  while (first_different_i==std::numeric_limits<int>::max()
156  && i<last_local_index());
157 
158  // Find the correct first differing index in parallel
159  this->comm().min(first_different_i);
160 
161  if (first_different_i == std::numeric_limits<int>::max())
162  return -1;
163 
164  return first_different_i;
165 }
double abs(double a)
virtual bool initialized() const
const Parallel::Communicator & comm() const
long double max(long double a, double b)
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type first_local_index() const =0
virtual numeric_index_type last_local_index() const =0

◆ local_size()

template<typename T >
numeric_index_type libMesh::PetscVector< T >::local_size ( ) const
inlineoverridevirtual
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 ierr, and libMesh::initialized().

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

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
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ localize() [1/5]

template<typename T >
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 >.

Definition at line 926 of file petsc_vector.C.

References ierr.

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

927 {
928  this->_restore_array();
929 
930  // This function must be run on all processors at once
931  parallel_object_only();
932 
933  PetscErrorCode ierr=0;
934  const PetscInt n = this->size();
935  const PetscInt nl = this->local_size();
936  PetscScalar * values;
937 
938  v_local.clear();
939  v_local.resize(n, 0.);
940 
941  ierr = VecGetArray (_vec, &values);
942  LIBMESH_CHKERR(ierr);
943 
945 
946  for (PetscInt i=0; i<nl; i++)
947  v_local[i+ioff] = static_cast<T>(values[i]);
948 
949  ierr = VecRestoreArray (_vec, &values);
950  LIBMESH_CHKERR(ierr);
951 
952  this->comm().sum(v_local);
953 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
virtual numeric_index_type first_local_index() const override
Definition: petsc_vector.h:959
virtual numeric_index_type local_size() const override
Definition: petsc_vector.h:942
const Parallel::Communicator & comm() const
void _restore_array() const
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ localize() [2/5]

template<typename T >
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 >.

Definition at line 660 of file petsc_vector.C.

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

661 {
662  this->_restore_array();
663 
664  // Make sure the NumericVector passed in is really a PetscVector
665  PetscVector<T> * v_local = cast_ptr<PetscVector<T> *>(&v_local_in);
666 
667  libmesh_assert(v_local);
668  libmesh_assert_equal_to (v_local->size(), this->size());
669 
670  PetscErrorCode ierr = 0;
671  const PetscInt n = this->size();
672 
673  IS is;
674  VecScatter scatter;
675 
676  // Create idx, idx[i] = i;
677  std::vector<PetscInt> idx(n);
678  std::iota (idx.begin(), idx.end(), 0);
679 
680  // Create the index set & scatter object
681  ierr = ISCreateLibMesh(this->comm().get(), n, idx.data(), PETSC_USE_POINTER, &is);
682  LIBMESH_CHKERR(ierr);
683 
684  ierr = LibMeshVecScatterCreate(_vec, is,
685  v_local->_vec, is,
686  &scatter);
687  LIBMESH_CHKERR(ierr);
688 
689  // Perform the scatter
690  ierr = VecScatterBegin(scatter, _vec, v_local->_vec,
691  INSERT_VALUES, SCATTER_FORWARD);
692  LIBMESH_CHKERR(ierr);
693 
694  ierr = VecScatterEnd (scatter, _vec, v_local->_vec,
695  INSERT_VALUES, SCATTER_FORWARD);
696  LIBMESH_CHKERR(ierr);
697 
698  // Clean up
699  ierr = LibMeshISDestroy (&is);
700  LIBMESH_CHKERR(ierr);
701 
702  ierr = LibMeshVecScatterDestroy(&scatter);
703  LIBMESH_CHKERR(ierr);
704 
705  // Make sure ghost dofs are up to date
706  if (v_local->type() == GHOSTED)
707  v_local->close();
708 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
const Parallel::Communicator & comm() const
void iota(ForwardIter first, ForwardIter last, T value)
Definition: utility.h:57
void _restore_array() const
PetscErrorCode ierr
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ localize() [3/5]

template<typename T >
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 >.

Definition at line 713 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, libMesh::PetscVector< T >::close(), libMesh::GHOSTED, libMesh::MeshTools::Generation::Private::idx(), ierr, libMesh::PARALLEL, PETSC_USE_POINTER, libMesh::PetscVector< T >::size(), and libMesh::NumericVector< T >::type().

715 {
716  // FIXME: Workaround for a strange bug at large-scale.
717  // If we have ghosting, PETSc lets us just copy the solution, and
718  // doing so avoids a segfault?
719  if (v_local_in.type() == GHOSTED &&
720  this->type() == PARALLEL)
721  {
722  v_local_in = *this;
723  return;
724  }
725 
726  // Normal code path begins here
727 
728  this->_restore_array();
729 
730  // Make sure the NumericVector passed in is really a PetscVector
731  PetscVector<T> * v_local = cast_ptr<PetscVector<T> *>(&v_local_in);
732 
733  libmesh_assert(v_local);
734  libmesh_assert_equal_to (v_local->size(), this->size());
735  libmesh_assert_less_equal (send_list.size(), v_local->size());
736 
737  PetscErrorCode ierr=0;
738  const numeric_index_type n_sl =
739  cast_int<numeric_index_type>(send_list.size());
740 
741  IS is;
742  VecScatter scatter;
743 
744  std::vector<PetscInt> idx(n_sl + this->local_size());
745 
746  for (numeric_index_type i=0; i<n_sl; i++)
747  idx[i] = static_cast<PetscInt>(send_list[i]);
748  for (numeric_index_type i = 0; i != this->local_size(); ++i)
749  idx[n_sl+i] = i + this->first_local_index();
750 
751  // Create the index set & scatter object
752  PetscInt * idxptr = idx.empty() ? nullptr : idx.data();
753  ierr = ISCreateLibMesh(this->comm().get(), n_sl+this->local_size(),
754  idxptr, PETSC_USE_POINTER, &is);
755  LIBMESH_CHKERR(ierr);
756 
757  ierr = LibMeshVecScatterCreate(_vec, is,
758  v_local->_vec, is,
759  &scatter);
760  LIBMESH_CHKERR(ierr);
761 
762 
763  // Perform the scatter
764  ierr = VecScatterBegin(scatter, _vec, v_local->_vec,
765  INSERT_VALUES, SCATTER_FORWARD);
766  LIBMESH_CHKERR(ierr);
767 
768  ierr = VecScatterEnd (scatter, _vec, v_local->_vec,
769  INSERT_VALUES, SCATTER_FORWARD);
770  LIBMESH_CHKERR(ierr);
771 
772  // Clean up
773  ierr = LibMeshISDestroy (&is);
774  LIBMESH_CHKERR(ierr);
775 
776  ierr = LibMeshVecScatterDestroy(&scatter);
777  LIBMESH_CHKERR(ierr);
778 
779  // Make sure ghost dofs are up to date
780  if (v_local->type() == GHOSTED)
781  v_local->close();
782 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
virtual numeric_index_type first_local_index() const override
Definition: petsc_vector.h:959
virtual numeric_index_type local_size() const override
Definition: petsc_vector.h:942
const Parallel::Communicator & comm() const
void _restore_array() const
dof_id_type numeric_index_type
Definition: id_types.h:92
ParallelType type() const
PetscErrorCode ierr
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ localize() [4/5]

template<typename T >
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 >.

Definition at line 787 of file petsc_vector.C.

References ierr, libMesh::numeric_petsc_cast(), and PETSC_USE_POINTER.

789 {
790  // Error code used to check the status of all PETSc function calls.
791  PetscErrorCode ierr = 0;
792 
793  // Create a sequential destination Vec with the right number of entries on each proc.
794  Vec dest;
795  ierr = VecCreateSeq(PETSC_COMM_SELF, cast_int<PetscInt>(indices.size()), &dest);
796  LIBMESH_CHKERR(ierr);
797 
798  // Create an IS using the libmesh routine. PETSc does not own the
799  // IS memory in this case, it is automatically cleaned up by the
800  // std::vector destructor.
801  PetscInt * idxptr =
802  indices.empty() ? nullptr : numeric_petsc_cast(indices.data());
803  IS is;
804  ierr = ISCreateLibMesh(this->comm().get(), cast_int<PetscInt>(indices.size()), idxptr,
805  PETSC_USE_POINTER, &is);
806  LIBMESH_CHKERR(ierr);
807 
808  // Create the VecScatter object. "PETSC_NULL" means "use the identity IS".
809  VecScatter scatter;
810  ierr = LibMeshVecScatterCreate(_vec,
811  /*src is=*/is,
812  /*dest vec=*/dest,
813  /*dest is=*/PETSC_NULL,
814  &scatter);
815  LIBMESH_CHKERR(ierr);
816 
817  // Do the scatter
818  ierr = VecScatterBegin(scatter, _vec, dest, INSERT_VALUES, SCATTER_FORWARD);
819  LIBMESH_CHKERR(ierr);
820 
821  ierr = VecScatterEnd(scatter, _vec, dest, INSERT_VALUES, SCATTER_FORWARD);
822  LIBMESH_CHKERR(ierr);
823 
824  // Get access to the values stored in dest.
825  PetscScalar * values;
826  ierr = VecGetArray (dest, &values);
827  LIBMESH_CHKERR(ierr);
828 
829  // Store values into the provided v_local. Make sure there is enough
830  // space reserved and then clear out any existing entries before
831  // inserting.
832  v_local.reserve(indices.size());
833  v_local.clear();
834  v_local.insert(v_local.begin(), values, values+indices.size());
835 
836  // We are done using it, so restore the array.
837  ierr = VecRestoreArray (dest, &values);
838  LIBMESH_CHKERR(ierr);
839 
840  // Clean up PETSc data structures.
841  ierr = LibMeshVecScatterDestroy(&scatter);
842  LIBMESH_CHKERR(ierr);
843 
844  ierr = LibMeshISDestroy (&is);
845  LIBMESH_CHKERR(ierr);
846 
847  ierr = LibMeshVecDestroy(&dest);
848  LIBMESH_CHKERR(ierr);
849 }
const Parallel::Communicator & comm() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
PetscErrorCode ierr

◆ localize() [5/5]

template<typename T >
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 >.

Definition at line 854 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, libMesh::PetscVector< T >::close(), libMesh::MeshTools::Generation::Private::idx(), ierr, libMesh::PetscVector< T >::init(), libMesh::Utility::iota(), libMesh::PetscVector< T >::localize(), libMesh::PARALLEL, and PETSC_USE_POINTER.

857 {
858  this->_restore_array();
859 
860  libmesh_assert_less_equal (send_list.size(), this->size());
861  libmesh_assert_less_equal (last_local_idx+1, this->size());
862 
863  const numeric_index_type my_size = this->size();
864  const numeric_index_type my_local_size = (last_local_idx - first_local_idx + 1);
865  PetscErrorCode ierr=0;
866 
867  // Don't bother for serial cases
868  // if ((first_local_idx == 0) &&
869  // (my_local_size == my_size))
870  // But we do need to stay in sync for degenerate cases
871  if (this->n_processors() == 1)
872  return;
873 
874 
875  // Build a parallel vector, initialize it with the local
876  // parts of (*this)
877  PetscVector<T> parallel_vec(this->comm(), PARALLEL);
878 
879  parallel_vec.init (my_size, my_local_size, true, PARALLEL);
880 
881 
882  // Copy part of *this into the parallel_vec
883  {
884  IS is;
885  VecScatter scatter;
886 
887  // Create idx, idx[i] = i+first_local_idx;
888  std::vector<PetscInt> idx(my_local_size);
889  std::iota (idx.begin(), idx.end(), first_local_idx);
890 
891  // Create the index set & scatter object
892  ierr = ISCreateLibMesh(this->comm().get(), my_local_size,
893  my_local_size ? idx.data() : nullptr, PETSC_USE_POINTER, &is);
894  LIBMESH_CHKERR(ierr);
895 
896  ierr = LibMeshVecScatterCreate(_vec, is,
897  parallel_vec._vec, is,
898  &scatter);
899  LIBMESH_CHKERR(ierr);
900 
901  ierr = VecScatterBegin(scatter, _vec, parallel_vec._vec,
902  INSERT_VALUES, SCATTER_FORWARD);
903  LIBMESH_CHKERR(ierr);
904 
905  ierr = VecScatterEnd (scatter, _vec, parallel_vec._vec,
906  INSERT_VALUES, SCATTER_FORWARD);
907  LIBMESH_CHKERR(ierr);
908 
909  // Clean up
910  ierr = LibMeshISDestroy (&is);
911  LIBMESH_CHKERR(ierr);
912 
913  ierr = LibMeshVecScatterDestroy(&scatter);
914  LIBMESH_CHKERR(ierr);
915  }
916 
917  // localize like normal
918  parallel_vec.close();
919  parallel_vec.localize (*this, send_list);
920  this->close();
921 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
const Parallel::Communicator & comm() const
void iota(ForwardIter first, ForwardIter last, T value)
Definition: utility.h:57
void _restore_array() const
processor_id_type n_processors() const
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr
virtual void close() override
Definition: petsc_vector.h:810
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ localize_to_one() [1/3]

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 >.

◆ localize_to_one() [2/3]

template<>
void libMesh::PetscVector< Real >::localize_to_one ( std::vector< Real > &  v_local,
const processor_id_type  pid 
) const

Definition at line 961 of file petsc_vector.C.

References ierr.

963 {
964  this->_restore_array();
965 
966  PetscErrorCode ierr=0;
967  const PetscInt n = size();
968  const PetscInt nl = local_size();
969  PetscScalar * values;
970 
971 
972  // only one processor
973  if (n_processors() == 1)
974  {
975  v_local.resize(n);
976 
977  ierr = VecGetArray (_vec, &values);
978  LIBMESH_CHKERR(ierr);
979 
980  for (PetscInt i=0; i<n; i++)
981  v_local[i] = static_cast<Real>(values[i]);
982 
983  ierr = VecRestoreArray (_vec, &values);
984  LIBMESH_CHKERR(ierr);
985  }
986 
987  // otherwise multiple processors
988  else
989  {
990  if (pid == 0) // optimized version for localizing to 0
991  {
992  Vec vout;
993  VecScatter ctx;
994 
995  ierr = VecScatterCreateToZero(_vec, &ctx, &vout);
996  LIBMESH_CHKERR(ierr);
997 
998  ierr = VecScatterBegin(ctx, _vec, vout, INSERT_VALUES, SCATTER_FORWARD);
999  LIBMESH_CHKERR(ierr);
1000  ierr = VecScatterEnd(ctx, _vec, vout, INSERT_VALUES, SCATTER_FORWARD);
1001  LIBMESH_CHKERR(ierr);
1002 
1003  if (processor_id() == 0)
1004  {
1005  v_local.resize(n);
1006 
1007  ierr = VecGetArray (vout, &values);
1008  LIBMESH_CHKERR(ierr);
1009 
1010  for (PetscInt i=0; i<n; i++)
1011  v_local[i] = static_cast<Real>(values[i]);
1012 
1013  ierr = VecRestoreArray (vout, &values);
1014  LIBMESH_CHKERR(ierr);
1015  }
1016 
1017  ierr = LibMeshVecScatterDestroy(&ctx);
1018  LIBMESH_CHKERR(ierr);
1019  ierr = LibMeshVecDestroy(&vout);
1020  LIBMESH_CHKERR(ierr);
1021 
1022  }
1023  else
1024  {
1025  v_local.resize(n);
1026 
1027  numeric_index_type ioff = this->first_local_index();
1028  std::vector<Real> local_values (n, 0.);
1029 
1030  {
1031  ierr = VecGetArray (_vec, &values);
1032  LIBMESH_CHKERR(ierr);
1033 
1034  for (PetscInt i=0; i<nl; i++)
1035  local_values[i+ioff] = static_cast<Real>(values[i]);
1036 
1037  ierr = VecRestoreArray (_vec, &values);
1038  LIBMESH_CHKERR(ierr);
1039  }
1040 
1041 
1042  MPI_Reduce (local_values.data(), v_local.data(), n, MPI_REAL, MPI_SUM,
1043  pid, this->comm().get());
1044  }
1045  }
1046 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
virtual numeric_index_type first_local_index() const override
Definition: petsc_vector.h:959
virtual numeric_index_type local_size() const override
Definition: petsc_vector.h:942
const Parallel::Communicator & comm() const
void _restore_array() const
processor_id_type n_processors() const
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr
processor_id_type processor_id() const

◆ localize_to_one() [3/3]

template<>
void libMesh::PetscVector< Complex >::localize_to_one ( std::vector< Complex > &  v_local,
const processor_id_type  pid 
) const

Definition at line 1055 of file petsc_vector.C.

References ierr.

1057 {
1058  this->_restore_array();
1059 
1060  PetscErrorCode ierr=0;
1061  const PetscInt n = size();
1062  const PetscInt nl = local_size();
1063  PetscScalar * values;
1064 
1065 
1066  v_local.resize(n);
1067 
1068 
1069  for (PetscInt i=0; i<n; i++)
1070  v_local[i] = 0.;
1071 
1072  // only one processor
1073  if (n_processors() == 1)
1074  {
1075  ierr = VecGetArray (_vec, &values);
1076  LIBMESH_CHKERR(ierr);
1077 
1078  for (PetscInt i=0; i<n; i++)
1079  v_local[i] = static_cast<Complex>(values[i]);
1080 
1081  ierr = VecRestoreArray (_vec, &values);
1082  LIBMESH_CHKERR(ierr);
1083  }
1084 
1085  // otherwise multiple processors
1086  else
1087  {
1088  numeric_index_type ioff = this->first_local_index();
1089 
1090  /* in here the local values are stored, acting as send buffer for MPI
1091  * initialize to zero, since we collect using MPI_SUM
1092  */
1093  std::vector<Real> real_local_values(n, 0.);
1094  std::vector<Real> imag_local_values(n, 0.);
1095 
1096  {
1097  ierr = VecGetArray (_vec, &values);
1098  LIBMESH_CHKERR(ierr);
1099 
1100  // provide my local share to the real and imag buffers
1101  for (PetscInt i=0; i<nl; i++)
1102  {
1103  real_local_values[i+ioff] = static_cast<Complex>(values[i]).real();
1104  imag_local_values[i+ioff] = static_cast<Complex>(values[i]).imag();
1105  }
1106 
1107  ierr = VecRestoreArray (_vec, &values);
1108  LIBMESH_CHKERR(ierr);
1109  }
1110 
1111  /* have buffers of the real and imaginary part of v_local.
1112  * Once MPI_Reduce() collected all the real and imaginary
1113  * parts in these std::vector<Real>, the values can be
1114  * copied to v_local
1115  */
1116  std::vector<Real> real_v_local(n);
1117  std::vector<Real> imag_v_local(n);
1118 
1119  // collect entries from other proc's in real_v_local, imag_v_local
1120  MPI_Reduce (real_local_values.data(),
1121  real_v_local.data(), n,
1122  MPI_REAL, MPI_SUM,
1123  pid, this->comm().get());
1124 
1125  MPI_Reduce (imag_local_values.data(),
1126  imag_v_local.data(), n,
1127  MPI_REAL, MPI_SUM,
1128  pid, this->comm().get());
1129 
1130  // copy real_v_local and imag_v_local to v_local
1131  for (PetscInt i=0; i<n; i++)
1132  v_local[i] = Complex(real_v_local[i], imag_v_local[i]);
1133  }
1134 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
virtual numeric_index_type first_local_index() const override
Definition: petsc_vector.h:959
virtual numeric_index_type local_size() const override
Definition: petsc_vector.h:942
const Parallel::Communicator & comm() const
void _restore_array() const
processor_id_type n_processors() const
dof_id_type numeric_index_type
Definition: id_types.h:92
std::complex< Real > Complex
PetscErrorCode ierr

◆ 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
inline
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, ierr, 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
numeric_index_type _last
Definition: petsc_vector.h:359
IterBase * end
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
Definition: petsc_vector.h:427
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr
numeric_index_type _first
Definition: petsc_vector.h:352

◆ max()

template<typename T >
Real libMesh::PetscVector< T >::max ( ) const
inlineoverridevirtual
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 ierr, and 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 }
void _restore_array() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscErrorCode ierr

◆ min()

template<typename T >
Real libMesh::PetscVector< T >::min ( ) const
inlineoverridevirtual
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 ierr, and 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 }
void _restore_array() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PetscErrorCode ierr

◆ n_objects()

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

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

◆ n_processors()

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

Definition at line 95 of file parallel_object.h.

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

Referenced by libMesh::BoundaryInfo::_find_id_maps(), libMesh::PetscDMWrapper::add_dofs_to_section(), libMesh::DistributedMesh::add_elem(), libMesh::DistributedMesh::add_node(), libMesh::LaplaceMeshSmoother::allgather_graph(), libMesh::FEMSystem::assembly(), libMesh::AztecLinearSolver< T >::AztecLinearSolver(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::DistributedMesh::clear(), libMesh::Nemesis_IO_Helper::compute_border_node_ids(), libMesh::Nemesis_IO_Helper::construct_nemesis_filename(), libMesh::UnstructuredMesh::create_pid_mesh(), libMesh::MeshTools::create_processor_bounding_box(), libMesh::DofMap::distribute_dofs(), libMesh::DofMap::distribute_local_dofs_node_major(), libMesh::DofMap::distribute_local_dofs_var_major(), libMesh::EnsightIO::EnsightIO(), libMesh::MeshBase::get_info(), libMesh::SystemSubsetBySubdomain::init(), libMesh::PetscDMWrapper::init_and_attach_petscdm(), libMesh::Nemesis_IO_Helper::initialize(), libMesh::DistributedMesh::insert_elem(), libMesh::MeshTools::libmesh_assert_contiguous_dof_ids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_new_node_procids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Elem >(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_topology_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_valid_boundary_ids(), libMesh::MeshTools::libmesh_assert_valid_dof_ids(), libMesh::MeshTools::libmesh_assert_valid_neighbors(), libMesh::MeshTools::libmesh_assert_valid_refinement_flags(), libMesh::DofMap::local_variable_indices(), libMesh::MeshRefinement::make_coarsening_compatible(), libMesh::MeshBase::n_active_elem_on_proc(), libMesh::MeshBase::n_elem_on_proc(), libMesh::MeshBase::n_nodes_on_proc(), libMesh::MeshBase::partition(), libMesh::PetscLinearSolver< T >::PetscLinearSolver(), libMesh::System::point_gradient(), libMesh::System::point_hessian(), libMesh::System::point_value(), libMesh::NameBasedIO::read(), libMesh::Nemesis_IO::read(), libMesh::CheckpointIO::read(), libMesh::CheckpointIO::read_connectivity(), libMesh::XdrIO::read_header(), libMesh::CheckpointIO::read_nodes(), libMesh::System::read_parallel_data(), libMesh::System::read_SCALAR_dofs(), libMesh::System::read_serialized_blocked_dof_objects(), libMesh::System::read_serialized_vector(), libMesh::DistributedMesh::renumber_dof_objects(), libMesh::DofMap::set_nonlocal_dof_objects(), libMesh::PetscDMWrapper::set_point_range_in_section(), libMesh::MeshRefinement::uniformly_coarsen(), libMesh::DistributedMesh::update_parallel_id_counts(), libMesh::GMVIO::write_binary(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::System::write_parallel_data(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::System::write_serialized_blocked_dof_objects(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), and libMesh::XdrIO::write_serialized_nodesets().

96  { 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
inlineoverridevirtual
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
Definition: petsc_vector.h:364
void _get_array(bool read_only) const
const PetscScalar * _read_only_values
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)
inlineinherited

Scales the vector by a, $ \vec{u} \leftarrow a\vec{u} $. Equivalent to u.scale(a)

Returns
A reference to *this.

Definition at line 391 of file numeric_vector.h.

391 { this->scale(a); return *this; }
virtual void scale(const T factor)=0

◆ operator+=()

template<typename T >
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 >.

Definition at line 111 of file petsc_vector.C.

References libMesh::closed().

112 {
113  this->_restore_array();
114  libmesh_assert(this->closed());
115 
116  this->add(1., v);
117 
118  return *this;
119 }
virtual void add(const numeric_index_type i, const T value) override
Definition: petsc_vector.C:180
void _restore_array() const
virtual bool closed() const

◆ operator-=()

template<typename T >
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 >.

Definition at line 125 of file petsc_vector.C.

References libMesh::closed().

126 {
127  this->_restore_array();
128  libmesh_assert(this->closed());
129 
130  this->add(-1., v);
131 
132  return *this;
133 }
virtual void add(const numeric_index_type i, const T value) override
Definition: petsc_vector.C:180
void _restore_array() const
virtual bool closed() const

◆ operator/=() [1/2]

template<typename T >
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 >.

Definition at line 427 of file petsc_vector.C.

References libMesh::PetscVector< T >::_vec, and ierr.

428 {
429  PetscErrorCode ierr = 0;
430 
431  const PetscVector<T> * v_vec = cast_ptr<const PetscVector<T> *>(&v);
432 
433  ierr = VecPointwiseDivide(_vec, _vec, v_vec->_vec);
434  LIBMESH_CHKERR(ierr);
435 
436  return *this;
437 }
PetscErrorCode ierr

◆ operator/=() [2/2]

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

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 400 of file numeric_vector.h.

400 { this->scale(1./a); return *this; }
virtual void scale(const T factor)=0

◆ 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.

Definition at line 560 of file petsc_vector.C.

References libMesh::PetscVector< T >::_restore_array(), libMesh::NumericVector< T >::_type, libMesh::PetscVector< T >::_vec, libMesh::NumericVector< T >::closed(), libMesh::GHOSTED, ierr, libMesh::PetscVector< T >::local_size(), libMesh::PARALLEL, libMesh::SERIAL, libMesh::PetscVector< T >::size(), and libMesh::NumericVector< T >::type().

561 {
562  this->_restore_array();
563  v._restore_array();
564 
565  libmesh_assert_equal_to (this->size(), v.size());
566  libmesh_assert_equal_to (this->local_size(), v.local_size());
567  libmesh_assert (v.closed());
568 
569  PetscErrorCode ierr = 0;
570 
571  if (((this->type()==PARALLEL) && (v.type()==GHOSTED)) ||
572  ((this->type()==GHOSTED) && (v.type()==PARALLEL)) ||
573  ((this->type()==GHOSTED) && (v.type()==SERIAL)) ||
574  ((this->type()==SERIAL) && (v.type()==GHOSTED)))
575  {
576  /* Allow assignment of a ghosted to a parallel vector since this
577  causes no difficulty. See discussion in libmesh-devel of
578  June 24, 2010. */
579  ierr = VecCopy (v._vec, this->_vec);
580  LIBMESH_CHKERR(ierr);
581  }
582  else
583  {
584  /* In all other cases, we assert that both vectors are of equal
585  type. */
586  libmesh_assert_equal_to (this->_type, v._type);
587 
588  if (v.size() != 0)
589  {
590  if (this->type() != GHOSTED)
591  {
592  ierr = VecCopy (v._vec, this->_vec);
593  LIBMESH_CHKERR(ierr);
594  }
595  else
596  {
597  Vec loc_vec;
598  Vec v_loc_vec;
599  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
600  LIBMESH_CHKERR(ierr);
601  ierr = VecGhostGetLocalForm (v._vec,&v_loc_vec);
602  LIBMESH_CHKERR(ierr);
603 
604  ierr = VecCopy (v_loc_vec, loc_vec);
605  LIBMESH_CHKERR(ierr);
606 
607  ierr = VecGhostRestoreLocalForm (v._vec,&v_loc_vec);
608  LIBMESH_CHKERR(ierr);
609  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
610  LIBMESH_CHKERR(ierr);
611  }
612  }
613  }
614 
615  close();
616 
617  return *this;
618 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
virtual numeric_index_type local_size() const override
Definition: petsc_vector.h:942
void _restore_array() const
ParallelType type() const
PetscErrorCode ierr
virtual void close() override
Definition: petsc_vector.h:810

◆ operator=() [2/5]

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

◆ operator=() [3/5]

template<typename T >
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 >.

Definition at line 510 of file petsc_vector.C.

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

511 {
512  this->_restore_array();
513  libmesh_assert(this->closed());
514 
515  PetscErrorCode ierr = 0;
516  PetscScalar s = static_cast<PetscScalar>(s_in);
517 
518  if (this->size() != 0)
519  {
520  if (this->type() != GHOSTED)
521  {
522  ierr = VecSet(_vec, s);
523  LIBMESH_CHKERR(ierr);
524  }
525  else
526  {
527  Vec loc_vec;
528  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
529  LIBMESH_CHKERR(ierr);
530 
531  ierr = VecSet(loc_vec, s);
532  LIBMESH_CHKERR(ierr);
533 
534  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
535  LIBMESH_CHKERR(ierr);
536  }
537  }
538 
539  return *this;
540 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
void _restore_array() const
virtual bool closed() const
ParallelType type() const
PetscErrorCode ierr

◆ operator=() [4/5]

template<typename T >
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 >.

Definition at line 546 of file petsc_vector.C.

547 {
548  // Make sure the NumericVector passed in is really a PetscVector
549  const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
550 
551  *this = *v;
552 
553  return *this;
554 }

◆ operator=() [5/5]

template<typename T >
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.

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

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

Implements libMesh::NumericVector< T >.

Definition at line 624 of file petsc_vector.C.

References libMesh::GHOSTED.

625 {
626  this->_get_array(false);
627 
632  if (this->size() == v.size())
633  {
636  for (numeric_index_type i=0; i<last-first; i++)
637  _values[i] = static_cast<PetscScalar>(v[first + i]);
638  }
639 
644  else
645  {
646  for (numeric_index_type i=0; i<_local_size; i++)
647  _values[i] = static_cast<PetscScalar>(v[i]);
648  }
649 
650  // Make sure ghost dofs are up to date
651  if (this->type() == GHOSTED)
652  this->close();
653 
654  return *this;
655 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
numeric_index_type _local_size
Definition: petsc_vector.h:364
virtual numeric_index_type first_local_index() const override
Definition: petsc_vector.h:959
void _get_array(bool read_only) const
PetscScalar * _values
Definition: petsc_vector.h:391
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type last_local_index() const override
Definition: petsc_vector.h:983
ParallelType type() const
virtual void close() override
Definition: petsc_vector.h:810

◆ pointwise_mult()

template<typename T >
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 >.

Definition at line 1141 of file petsc_vector.C.

References libMesh::GHOSTED, and ierr.

1143 {
1144  this->_restore_array();
1145 
1146  PetscErrorCode ierr = 0;
1147 
1148  // Convert arguments to PetscVector*.
1149  const PetscVector<T> * vec1_petsc = cast_ptr<const PetscVector<T> *>(&vec1);
1150  const PetscVector<T> * vec2_petsc = cast_ptr<const PetscVector<T> *>(&vec2);
1151 
1152  // Call PETSc function.
1153 
1154  if (this->type() != GHOSTED)
1155  {
1156  ierr = VecPointwiseMult(this->vec(),
1157  const_cast<PetscVector<T> *>(vec1_petsc)->vec(),
1158  const_cast<PetscVector<T> *>(vec2_petsc)->vec());
1159  LIBMESH_CHKERR(ierr);
1160  }
1161  else
1162  {
1163  Vec loc_vec;
1164  Vec v1_loc_vec;
1165  Vec v2_loc_vec;
1166  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
1167  LIBMESH_CHKERR(ierr);
1168  ierr = VecGhostGetLocalForm (const_cast<PetscVector<T> *>(vec1_petsc)->vec(),&v1_loc_vec);
1169  LIBMESH_CHKERR(ierr);
1170  ierr = VecGhostGetLocalForm (const_cast<PetscVector<T> *>(vec2_petsc)->vec(),&v2_loc_vec);
1171  LIBMESH_CHKERR(ierr);
1172 
1173  ierr = VecPointwiseMult(loc_vec,v1_loc_vec,v2_loc_vec);
1174  LIBMESH_CHKERR(ierr);
1175 
1176  ierr = VecGhostRestoreLocalForm (const_cast<PetscVector<T> *>(vec1_petsc)->vec(),&v1_loc_vec);
1177  LIBMESH_CHKERR(ierr);
1178  ierr = VecGhostRestoreLocalForm (const_cast<PetscVector<T> *>(vec2_petsc)->vec(),&v2_loc_vec);
1179  LIBMESH_CHKERR(ierr);
1180  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
1181  LIBMESH_CHKERR(ierr);
1182  }
1183 }
void _restore_array() const
ParallelType type() const
PetscErrorCode ierr

◆ print() [1/2]

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

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

Definition at line 916 of file numeric_vector.h.

917 {
918  libmesh_assert (this->initialized());
919  os << "Size\tglobal = " << this->size()
920  << "\t\tlocal = " << this->local_size() << std::endl;
921 
922  os << "#\tValue" << std::endl;
923  for (numeric_index_type i=this->first_local_index(); i<this->last_local_index(); i++)
924  os << i << "\t" << (*this)(i) << std::endl;
925 }
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
inlineinherited

Definition at line 898 of file numeric_vector.h.

899 {
900  libmesh_assert (this->initialized());
901  os << "Size\tglobal = " << this->size()
902  << "\t\tlocal = " << this->local_size() << std::endl;
903 
904  // std::complex<>::operator<<() is defined, but use this form
905  os << "#\tReal part\t\tImaginary part" << std::endl;
906  for (numeric_index_type i=this->first_local_index(); i<this->last_local_index(); i++)
907  os << i << "\t"
908  << (*this)(i).real() << "\t\t"
909  << (*this)(i).imag() << std::endl;
910 }
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
inlinevirtualinherited

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

Definition at line 953 of file numeric_vector.h.

954 {
955  libmesh_assert (this->initialized());
956 
957  std::vector<T> v(this->size());
958  this->localize(v);
959 
960  // Right now we only want one copy of the output
961  if (this->processor_id())
962  return;
963 
964  os << "Size\tglobal = " << this->size() << std::endl;
965  os << "#\tValue" << std::endl;
966  for (numeric_index_type i=0; i!=v.size(); i++)
967  os << i << "\t" << v[i] << std::endl;
968 }
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

◆ print_global() [2/2]

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

Definition at line 931 of file numeric_vector.h.

932 {
933  libmesh_assert (this->initialized());
934 
935  std::vector<Complex> v(this->size());
936  this->localize(v);
937 
938  // Right now we only want one copy of the output
939  if (this->processor_id())
940  return;
941 
942  os << "Size\tglobal = " << this->size() << std::endl;
943  os << "#\tReal part\t\tImaginary part" << std::endl;
944  for (numeric_index_type i=0; i!=v.size(); i++)
945  os << i << "\t"
946  << v[i].real() << "\t\t"
947  << v[i].imag() << std::endl;
948 }
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

◆ print_info()

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

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

Definition at line 87 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter, and libMesh::ReferenceCounter::get_info().

88 {
90  out_stream << ReferenceCounter::get_info();
91 }
static std::string get_info()

◆ print_matlab()

template<typename T >
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.

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

Otherwise the matrix will be dumped to the screen.

Destroy the viewer.

Reimplemented from libMesh::NumericVector< T >.

Definition at line 1188 of file petsc_vector.C.

References libMesh::closed(), ierr, and libMesh::Quality::name().

1189 {
1190  this->_restore_array();
1191  libmesh_assert (this->closed());
1192 
1193  PetscErrorCode ierr=0;
1194  PetscViewer petsc_viewer;
1195 
1196 
1197  ierr = PetscViewerCreate (this->comm().get(),
1198  &petsc_viewer);
1199  LIBMESH_CHKERR(ierr);
1200 
1205  if (name != "")
1206  {
1207  ierr = PetscViewerASCIIOpen( this->comm().get(),
1208  name.c_str(),
1209  &petsc_viewer);
1210  LIBMESH_CHKERR(ierr);
1211 
1212 #if PETSC_VERSION_LESS_THAN(3,7,0)
1213  ierr = PetscViewerSetFormat (petsc_viewer,
1214  PETSC_VIEWER_ASCII_MATLAB);
1215 #else
1216  ierr = PetscViewerPushFormat (petsc_viewer,
1217  PETSC_VIEWER_ASCII_MATLAB);
1218 #endif
1219 
1220  LIBMESH_CHKERR(ierr);
1221 
1222  ierr = VecView (_vec, petsc_viewer);
1223  LIBMESH_CHKERR(ierr);
1224  }
1225 
1229  else
1230  {
1231 
1232 #if PETSC_VERSION_LESS_THAN(3,7,0)
1233  ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
1234  PETSC_VIEWER_ASCII_MATLAB);
1235 #else
1236  ierr = PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
1237  PETSC_VIEWER_ASCII_MATLAB);
1238 #endif
1239 
1240  LIBMESH_CHKERR(ierr);
1241 
1242  ierr = VecView (_vec, PETSC_VIEWER_STDOUT_WORLD);
1243  LIBMESH_CHKERR(ierr);
1244  }
1245 
1246 
1250  ierr = LibMeshPetscViewerDestroy (&petsc_viewer);
1251  LIBMESH_CHKERR(ierr);
1252 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
const Parallel::Communicator & comm() const
void _restore_array() const
virtual bool closed() const
PetscErrorCode ierr

◆ processor_id()

processor_id_type libMesh::ParallelObject::processor_id ( ) const
inlineinherited
Returns
The rank of this processor in the group.

Definition at line 101 of file parallel_object.h.

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

Referenced by libMesh::BoundaryInfo::_find_id_maps(), libMesh::EquationSystems::_read_impl(), libMesh::PetscDMWrapper::add_dofs_to_section(), libMesh::DistributedMesh::add_elem(), libMesh::BoundaryInfo::add_elements(), libMesh::DofMap::add_neighbors_to_send_list(), libMesh::DistributedMesh::add_node(), libMesh::UnstructuredMesh::all_second_order(), libMesh::MeshTools::Modification::all_tri(), libMesh::FEMSystem::assembly(), libMesh::EquationSystems::build_discontinuous_solution_vector(), libMesh::Nemesis_IO_Helper::build_element_and_node_maps(), libMesh::InfElemBuilder::build_inf_elem(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::DistributedMesh::clear(), libMesh::ExodusII_IO_Helper::close(), libMesh::Nemesis_IO_Helper::compute_border_node_ids(), libMesh::Nemesis_IO_Helper::compute_communication_map_parameters(), libMesh::Nemesis_IO_Helper::compute_internal_and_border_elems_and_internal_nodes(), libMesh::Nemesis_IO_Helper::compute_node_communication_maps(), libMesh::Nemesis_IO_Helper::compute_num_global_elem_blocks(), libMesh::Nemesis_IO_Helper::compute_num_global_nodesets(), libMesh::Nemesis_IO_Helper::compute_num_global_sidesets(), libMesh::Nemesis_IO_Helper::construct_nemesis_filename(), libMesh::MeshTools::correct_node_proc_ids(), libMesh::ExodusII_IO_Helper::create(), libMesh::DistributedMesh::delete_elem(), libMesh::DistributedMesh::delete_node(), libMesh::MeshCommunication::delete_remote_elements(), libMesh::DofMap::distribute_dofs(), libMesh::DofMap::distribute_local_dofs_node_major(), libMesh::DofMap::distribute_local_dofs_var_major(), libMesh::DistributedMesh::DistributedMesh(), libMesh::DofMap::end_dof(), libMesh::DofMap::end_old_dof(), libMesh::EnsightIO::EnsightIO(), libMesh::MeshFunction::find_element(), libMesh::MeshFunction::find_elements(), libMesh::UnstructuredMesh::find_neighbors(), libMesh::DofMap::first_dof(), libMesh::DofMap::first_old_dof(), libMesh::Nemesis_IO_Helper::get_cmap_params(), libMesh::Nemesis_IO_Helper::get_eb_info_global(), libMesh::Nemesis_IO_Helper::get_elem_cmap(), libMesh::Nemesis_IO_Helper::get_elem_map(), libMesh::MeshBase::get_info(), libMesh::DofMap::get_info(), libMesh::Nemesis_IO_Helper::get_init_global(), libMesh::Nemesis_IO_Helper::get_init_info(), libMesh::Nemesis_IO_Helper::get_loadbal_param(), libMesh::Nemesis_IO_Helper::get_node_cmap(), libMesh::Nemesis_IO_Helper::get_node_map(), libMesh::Nemesis_IO_Helper::get_ns_param_global(), libMesh::Nemesis_IO_Helper::get_ss_param_global(), libMesh::SparsityPattern::Build::handle_vi_vj(), libMesh::SystemSubsetBySubdomain::init(), libMesh::ExodusII_IO_Helper::initialize(), libMesh::ExodusII_IO_Helper::initialize_element_variables(), libMesh::ExodusII_IO_Helper::initialize_global_variables(), libMesh::ExodusII_IO_Helper::initialize_nodal_variables(), libMesh::DistributedMesh::insert_elem(), libMesh::DofMap::is_evaluable(), libMesh::SparsityPattern::Build::join(), libMesh::DofMap::last_dof(), libMesh::MeshTools::libmesh_assert_consistent_distributed(), libMesh::MeshTools::libmesh_assert_consistent_distributed_nodes(), libMesh::MeshTools::libmesh_assert_contiguous_dof_ids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Elem >(), libMesh::MeshTools::libmesh_assert_valid_neighbors(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_object_ids(), libMesh::DofMap::local_variable_indices(), libMesh::MeshRefinement::make_coarsening_compatible(), libMesh::MeshBase::n_active_local_elem(), libMesh::BoundaryInfo::n_boundary_conds(), libMesh::BoundaryInfo::n_edge_conds(), libMesh::DofMap::n_local_dofs(), libMesh::System::n_local_dofs(), libMesh::MeshBase::n_local_elem(), libMesh::MeshBase::n_local_nodes(), libMesh::BoundaryInfo::n_nodeset_conds(), libMesh::BoundaryInfo::n_shellface_conds(), libMesh::SparsityPattern::Build::operator()(), libMesh::DistributedMesh::own_node(), libMesh::System::point_gradient(), libMesh::System::point_hessian(), libMesh::System::point_value(), libMesh::Nemesis_IO_Helper::put_cmap_params(), libMesh::Nemesis_IO_Helper::put_elem_cmap(), libMesh::Nemesis_IO_Helper::put_elem_map(), libMesh::Nemesis_IO_Helper::put_loadbal_param(), libMesh::Nemesis_IO_Helper::put_node_cmap(), libMesh::Nemesis_IO_Helper::put_node_map(), libMesh::NameBasedIO::read(), libMesh::Nemesis_IO::read(), libMesh::XdrIO::read(), libMesh::CheckpointIO::read(), libMesh::ExodusII_IO_Helper::read_elem_num_map(), libMesh::ExodusII_IO_Helper::read_global_values(), libMesh::CheckpointIO::read_header(), libMesh::XdrIO::read_header(), libMesh::System::read_header(), libMesh::System::read_legacy_data(), libMesh::ExodusII_IO_Helper::read_node_num_map(), libMesh::System::read_parallel_data(), libMesh::System::read_SCALAR_dofs(), libMesh::XdrIO::read_serialized_bc_names(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::System::read_serialized_blocked_dof_objects(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::System::read_serialized_data(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::XdrIO::read_serialized_subdomain_names(), libMesh::System::read_serialized_vector(), libMesh::System::read_serialized_vectors(), libMesh::DistributedMesh::renumber_dof_objects(), libMesh::CheckpointIO::select_split_config(), libMesh::DofMap::set_nonlocal_dof_objects(), libMesh::PetscDMWrapper::set_point_range_in_section(), libMesh::LaplaceMeshSmoother::smooth(), libMesh::MeshTools::total_weight(), libMesh::MeshRefinement::uniformly_coarsen(), libMesh::Parallel::Packing< T >::unpack(), libMesh::DistributedMesh::update_parallel_id_counts(), libMesh::NameBasedIO::write(), libMesh::XdrIO::write(), libMesh::CheckpointIO::write(), libMesh::EquationSystems::write(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::ExodusII_IO::write_element_data(), libMesh::ExodusII_IO_Helper::write_element_values(), libMesh::ExodusII_IO_Helper::write_elements(), libMesh::ExodusII_IO::write_global_data(), libMesh::ExodusII_IO_Helper::write_global_values(), libMesh::System::write_header(), libMesh::ExodusII_IO::write_information_records(), libMesh::ExodusII_IO_Helper::write_information_records(), libMesh::ExodusII_IO_Helper::write_nodal_coordinates(), libMesh::UCDIO::write_nodal_data(), libMesh::ExodusII_IO::write_nodal_data(), libMesh::ExodusII_IO::write_nodal_data_discontinuous(), libMesh::ExodusII_IO_Helper::write_nodal_values(), libMesh::Nemesis_IO_Helper::write_nodesets(), libMesh::ExodusII_IO_Helper::write_nodesets(), libMesh::System::write_parallel_data(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bc_names(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::System::write_serialized_blocked_dof_objects(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::System::write_serialized_data(), libMesh::XdrIO::write_serialized_nodes(), libMesh::XdrIO::write_serialized_nodesets(), libMesh::XdrIO::write_serialized_subdomain_names(), libMesh::System::write_serialized_vector(), libMesh::System::write_serialized_vectors(), libMesh::Nemesis_IO_Helper::write_sidesets(), libMesh::ExodusII_IO_Helper::write_sidesets(), libMesh::ExodusII_IO::write_timestep(), libMesh::ExodusII_IO_Helper::write_timestep(), and libMesh::ExodusII_IO::write_timestep_discontinuous().

102  { return cast_int<processor_id_type>(_communicator.rank()); }
const Parallel::Communicator & _communicator
processor_id_type rank() const
Definition: communicator.h:173

◆ reciprocal()

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

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

Implements libMesh::NumericVector< T >.

Definition at line 156 of file petsc_vector.C.

References ierr.

157 {
158  PetscErrorCode ierr = 0;
159 
160  // VecReciprocal has been in PETSc since at least 2.3.3 days
161  ierr = VecReciprocal(_vec);
162  LIBMESH_CHKERR(ierr);
163 }
PetscErrorCode ierr

◆ restore_array()

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

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

◆ scale()

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

Scale each element of the vector by the given factor.

Implements libMesh::NumericVector< T >.

Definition at line 400 of file petsc_vector.C.

References libMesh::GHOSTED, and ierr.

401 {
402  this->_restore_array();
403 
404  PetscErrorCode ierr = 0;
405  PetscScalar factor = static_cast<PetscScalar>(factor_in);
406 
407  if (this->type() != GHOSTED)
408  {
409  ierr = VecScale(_vec, factor);
410  LIBMESH_CHKERR(ierr);
411  }
412  else
413  {
414  Vec loc_vec;
415  ierr = VecGhostGetLocalForm (_vec,&loc_vec);
416  LIBMESH_CHKERR(ierr);
417 
418  ierr = VecScale(loc_vec, factor);
419  LIBMESH_CHKERR(ierr);
420 
421  ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
422  LIBMESH_CHKERR(ierr);
423  }
424 }
void _restore_array() const
ParallelType type() const
PetscErrorCode ierr

◆ set()

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

Sets v(i) = value.

Implements libMesh::NumericVector< T >.

Definition at line 138 of file petsc_vector.C.

References ierr, and value.

139 {
140  this->_restore_array();
141  libmesh_assert_less (i, size());
142 
143  PetscErrorCode ierr=0;
144  PetscInt i_val = static_cast<PetscInt>(i);
145  PetscScalar petsc_value = static_cast<PetscScalar>(value);
146 
147  ierr = VecSetValues (_vec, 1, &i_val, &petsc_value, INSERT_VALUES);
148  LIBMESH_CHKERR(ierr);
149 
150  this->_is_closed = false;
151 }
virtual numeric_index_type size() const override
Definition: petsc_vector.h:922
void _restore_array() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ size()

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

Implements libMesh::NumericVector< T >.

Definition at line 922 of file petsc_vector.h.

References ierr, and libMesh::initialized().

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

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
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ subset_l1_norm()

template<class T >
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.

Definition at line 311 of file numeric_vector.C.

Referenced by libMesh::System::discrete_var_norm().

312 {
313  const NumericVector<T> & v = *this;
314 
315  Real norm = 0;
316 
317  for (const auto & index : indices)
318  norm += std::abs(v(index));
319 
320  this->comm().sum(norm);
321 
322  return norm;
323 }
double abs(double a)
const Parallel::Communicator & comm() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ subset_l2_norm()

template<class T >
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.

Definition at line 326 of file numeric_vector.C.

Referenced by libMesh::System::discrete_var_norm().

327 {
328  const NumericVector<T> & v = *this;
329 
330  Real norm = 0;
331 
332  for (const auto & index : indices)
333  norm += TensorTools::norm_sq(v(index));
334 
335  this->comm().sum(norm);
336 
337  return std::sqrt(norm);
338 }
const Parallel::Communicator & comm() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ subset_linfty_norm()

template<class T >
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.

Definition at line 341 of file numeric_vector.C.

Referenced by libMesh::System::discrete_var_norm().

342 {
343  const NumericVector<T> & v = *this;
344 
345  Real norm = 0;
346 
347  for (const auto & index : indices)
348  {
349  Real value = std::abs(v(index));
350  if (value > norm)
351  norm = value;
352  }
353 
354  this->comm().max(norm);
355 
356  return norm;
357 }
double abs(double a)
const Parallel::Communicator & comm() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const bool value
Definition: xdr_io.C:109

◆ sum()

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

Implements libMesh::NumericVector< T >.

Definition at line 41 of file petsc_vector.C.

References libMesh::closed(), ierr, and value.

42 {
43  this->_restore_array();
44  libmesh_assert(this->closed());
45 
46  PetscErrorCode ierr=0;
47  PetscScalar value=0.;
48 
49  ierr = VecSum (_vec, &value);
50  LIBMESH_CHKERR(ierr);
51 
52  return static_cast<T>(value);
53 }
void _restore_array() const
virtual bool closed() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ swap()

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

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().

Referenced by libMesh::__libmesh_petsc_diff_solver_residual(), libMesh::__libmesh_tao_equality_constraints(), libMesh::__libmesh_tao_equality_constraints_jacobian(), libMesh::__libmesh_tao_gradient(), libMesh::__libmesh_tao_hessian(), libMesh::__libmesh_tao_inequality_constraints(), libMesh::__libmesh_tao_inequality_constraints_jacobian(), libMesh::__libmesh_tao_objective(), DMlibMeshFunction(), DMlibMeshJacobian(), libMesh::libmesh_petsc_snes_jacobian(), libMesh::libmesh_petsc_snes_postcheck(), and libMesh::libmesh_petsc_snes_residual_helper().

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 }
PetscScalar * _values
Definition: petsc_vector.h:391
std::atomic< bool > _array_is_present
Definition: petsc_vector.h:342
GlobalToLocalMap _global_to_local_map
Definition: petsc_vector.h:427
virtual void swap(NumericVector< T > &v)
void swap(Iterator &lhs, Iterator &rhs)

◆ type() [1/2]

◆ type() [2/2]

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

Definition at line 159 of file numeric_vector.h.

159 { return _type; }

◆ vec()

◆ zero()

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

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(), libMesh::GHOSTED, and ierr.

Referenced by libMesh::__libmesh_tao_equality_constraints(), libMesh::__libmesh_tao_gradient(), libMesh::__libmesh_tao_inequality_constraints(), libMesh::libmesh_petsc_snes_fd_residual(), libMesh::libmesh_petsc_snes_mffd_residual(), libMesh::libmesh_petsc_snes_residual(), and libMesh::TaoOptimizationSolver< T >::solve().

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 }
void _restore_array() const
virtual bool closed() const
ParallelType type() const
PetscErrorCode ierr

◆ zero_clone()

template<typename T >
std::unique_ptr< NumericVector< T > > libMesh::PetscVector< T >::zero_clone ( ) const
inlineoverridevirtual
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

◆ _counts

ReferenceCounter::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 = true
staticprotectedinherited

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

Definition at line 141 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::disable_print_counter_info(), libMesh::ReferenceCounter::enable_print_counter_info(), and libMesh::ReferenceCounter::print_info().

◆ _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 712 of file numeric_vector.h.

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

◆ _is_initialized

◆ _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

◆ _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


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