26#ifndef VULPES_LA_VECTOR_HPP_
27#define VULPES_LA_VECTOR_HPP_
55 static_assert(std::is_same_v<double, PetscScalar>);
59 template <
typename DofHandler>
60 explicit Vector(
const DofHandler& dof_handler, PetscInt block_size = 1) {
61 static_assert(std::is_same_v<typename DofHandler::index_t, PetscInt>);
62 init(dof_handler, block_size);
66 explicit Vector(
const std::pair<PetscInt, PetscInt>& sizes) {
85 template <
typename DofHandler>
92 template <
typename DofHandler>
103 template <
typename DofHandler>
104 void init(
const DofHandler& dof_handler, PetscInt block_size) {
106 if (block_size > 1) {
109 const PetscInt n_glob = dof_handler.ndofsGlobal();
110 PetscInt dof_begin = 0;
111 PetscInt dof_end = n_glob;
112 if constexpr (DofHandler::parallel) {
113 dof_begin = dof_handler.dofOffset()[
mpi::rank()];
114 dof_end = dof_handler.dofOffset()[
mpi::rank() + 1];
116 const PetscInt n_loc = dof_end - dof_begin;
123 template <
typename DofHandler>
125 static_assert(std::is_same_v<typename DofHandler::index_t, PetscInt>);
126 const auto& ghost_dofs = dof_handler.getGhostDofs();
127 const PetscInt n_glob = dof_handler.ndofsGlobal();
128 const PetscInt n_owned = dof_handler.nOwnedDofs();
129 const PetscInt n_ghost =
static_cast<PetscInt
>(ghost_dofs.size());
131 PETSC_COMM_WORLD, n_owned, n_glob, n_ghost, ghost_dofs.data(),
145 const std::string& filename,
146 const std::string& vecname =
"",
147 PetscViewerFormat format = PETSC_VIEWER_ASCII_MATLAB)
const;
150 const std::string& filename,
const std::string& vecname =
"")
const;
161 const PetscScalar* values,
162 const PetscInt* indices,
164 InsertMode mode = ADD_VALUES);
176 const PetscScalar* values,
177 const PetscInt* indices,
179 InsertMode mode = ADD_VALUES);
189 const Eigen::Ref<const Eigen::MatrixXd>& loc_vec,
191 InsertMode mode = ADD_VALUES);
202 const Eigen::Ref<const Eigen::MatrixXd>& loc_vec,
204 InsertMode mode = ADD_VALUES);
208 PetscInt idx, PetscScalar value, InsertMode mode = INSERT_VALUES);
210 PetscScalar
getValue(PetscInt idx)
const;
220 Eigen::Map<const Eigen::VectorXd>
toEigenMap()
const;
275 void set(
double value);
277 void scale(
double value);
279 PetscScalar
norm(NormType norm_type = NORM_2)
const;
283 void add(PetscScalar alpha,
const Vector& x);
298 void pow(PetscScalar power);
306 PetscScalar
sum()
const;
308 PetscScalar
norm2()
const;
312 const Vector& vec1,
const Vector& vec2,
double tolerance = 1e-12);
Definition crs_map.hpp:82
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
void addTriplet(PetscScalar alpha, PetscScalar beta, PetscScalar gamma, const Vector &x, const Vector &y)
Add a vector in place (this = alpha x + beta y + gamma this).
Definition vector.cpp:318
void insertValues(const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
Insert values into the vector at specified indices.
Definition vector.cpp:108
void copy(const Vector &other)
Copy the contents of another vector.
Definition vector.cpp:364
void save(const std::string &filename, const std::string &vecname="", PetscViewerFormat format=PETSC_VIEWER_ASCII_MATLAB) const
Save the vector to a file.
Definition vector.cpp:58
Eigen::Map< Eigen::VectorXd > getLocalGhostedView()
Writable view of the local form of a ghost-aware Vector (owned dofs, then ghost dofs,...
Definition vector.cpp:211
void setVectorSizes(const PetscInt n_loc, const PetscInt n_glob)
Definition vector.cpp:41
void set(double value)
Set all values in the vector to a constant.
Definition vector.cpp:280
PetscScalar getValue(PetscInt idx) const
Get a single value from the vector.
Definition vector.cpp:150
PetscScalar * m_local_array
Definition vector.hpp:325
void addCouple(PetscScalar alpha, const Vector &x, const Vector &y)
Add a vector in place (this = alpha x + y).
Definition vector.cpp:313
void initGhosted(const DofHandler &dof_handler)
Initialize a ghost-aware vector from a DofHandler (see the GhostTag constructor).
Definition vector.hpp:124
PetscInt getSize() const
Get the size of the vector.
Definition vector.cpp:337
std::pair< PetscInt, PetscInt > getLocAndGlobSize() const
Get local and global sizes of the vector.
Definition vector.cpp:256
Vec & getPetscVector()
Get the underlying PETSc vector (non-const version).
Definition vector.hpp:270
PetscScalar norm(NormType norm_type=NORM_2) const
Compute the norm of the vector.
Definition vector.cpp:288
void print() const
Print the vector to standard output.
Definition vector.cpp:54
void setValue(PetscInt idx, PetscScalar value, InsertMode mode=INSERT_VALUES)
Set a single value in the vector.
Definition vector.cpp:146
void add(const Vector &x)
Add a vector in place (this += x).
Definition vector.hpp:281
static bool compareVectors(const Vector &vec1, const Vector &vec2, double tolerance=1e-12)
Static method to compare two vectors with tolerance.
Definition vector.cpp:378
Vector(const std::pair< PetscInt, PetscInt > &sizes)
Construct a Vector given local and global sizes.
Definition vector.hpp:66
void accumulateGhostsEnd()
Definition vector.cpp:205
void abs()
Replaces each component of a vector by its absolute value.
Definition vector.cpp:333
void zero()
Zero out the vector.
Definition vector.cpp:276
void accumulateGhostsBegin()
Add the ghost entries of the local form (see getLocalGhostedView()) into the entries of their owning ...
Definition vector.cpp:199
void setupVector()
Definition vector.cpp:45
void insertValuesBlocked(const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
Insert blocked values into the vector at specified indices.
Definition vector.cpp:117
void updateGhostsBegin()
updateGhosts() split in two, so that the exchanges of several vectors overlap: begin all of them,...
Definition vector.cpp:187
Vector(const DofHandler &dof_handler, PetscInt block_size=1)
Construct a Vector from a DofHandler.
Definition vector.hpp:60
PetscScalar sum() const
Compute the sum of all elements in the vector.
Definition vector.cpp:349
void createVector()
Definition vector.cpp:36
PetscInt getBlockSize() const
Get the block size of the vector.
Definition vector.cpp:270
void init(const DofHandler &dof_handler, PetscInt block_size)
Initialize the vector based on a DofHandler.
Definition vector.hpp:104
void compress()
Compress the vector (do intracommunication).
Definition vector.cpp:49
void pow(PetscScalar power)
Replaces each component of a vector by its p-th power.
Definition vector.cpp:329
std::array< PetscInt, 2 > getOwnershipRange() const
Get the ownership range of the vector.
Definition vector.cpp:263
void addGeneric(PetscScalar alpha, PetscScalar beta, const Vector &x)
Add a vector in place (this = alpha x + beta this).
Definition vector.cpp:308
Vec m_local_form
Definition vector.hpp:324
void saveBinary(const std::string &filename, const std::string &vecname="") const
Save the vector to a binary file.
Definition vector.cpp:86
Eigen::VectorXd toLocalGhostedEigenVector() const
Local-form snapshot of a ghost-aware Vector (see createGhosted()): this rank's owned dofs first ([0,...
Definition vector.cpp:229
PetscInt getLocalSize() const
Get the local size of the vector.
Definition vector.cpp:343
Eigen::Map< const Eigen::VectorXd > toLocalEigenMap() const
Convert the locally-owned vector entries to an Eigen map.
Definition vector.cpp:173
~Vector()
Definition vector.cpp:360
void updateGhosts()
Refresh the ghost padding of a ghost-aware Vector (see createGhosted()) from each ghost dof's owning ...
Definition vector.cpp:182
Vec m_petsc_vector
Definition vector.hpp:321
void getLocalValues(std::vector< PetscScalar > &values) const
Get all local values of the vector.
Definition vector.cpp:156
Vector(GhostTag, const DofHandler &dof_handler)
Construct a ghost-aware Vector from a DofHandler: the local form additionally carries a padding segme...
Definition vector.hpp:86
Eigen::Map< const Eigen::VectorXd > toEigenMap() const
Convert the locally-owned vector entries to an Eigen map (same as toLocalEigenMap()).
Definition vector.cpp:169
static Vector createGhosted(const DofHandler &dof_handler)
Construct a ghost-aware Vector from a DofHandler. See the GhostTag constructor above for what this en...
Definition vector.hpp:93
void scale(double value)
Scale the vector by a constant.
Definition vector.cpp:284
void restoreLocalGhostedView()
Definition vector.cpp:221
const Vec & getPetscVector() const
Get the underlying PETSc vector (const version).
Definition vector.hpp:268
void addScaled(PetscScalar alpha, const Vector &x)
Add a vector in place (this = x + alpha this).
Definition vector.cpp:304
bool m_is_ghosted
Definition vector.hpp:322
Vector & operator=(const Vector &other)=delete
PetscScalar norm2() const
Compute the Euclidean norm of the vector.
Definition vector.cpp:374
void updateGhostsEnd()
Definition vector.cpp:193
Definition boomer_amg.hpp:27
double dot(const Vector &a, const Vector &b)
Definition vector.cpp:368
static int rank()
Return the MPI rank in MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:116
Definition cell_manager.hpp:38
#define PetscCallAbortWorld(...)
Definition petsc_utils.hpp:32
Tag selecting the ghost-aware constructor overload (see createGhosted()).
Definition vector.hpp:74