27 #ifndef __GENERIC_VECTOR_H 28 #define __GENERIC_VECTOR_H 34 #include <dolfin/common/ArrayView.h> 35 #include <dolfin/common/types.h> 36 #include <dolfin/log/log.h> 38 #include "TensorLayout.h" 39 #include "GenericTensor.h" 43 template<
typename T>
class Array;
64 "Vector cannot be initialised more than once");
67 std::vector<dolfin::la_index> ghosts;
68 std::vector<std::size_t> local_to_global(tensor_layout.
index_map(0)->size(IndexMap::MapSize::ALL));
71 for (std::size_t i = 0; i != local_to_global.size(); ++i)
72 local_to_global[i] = tensor_layout.
index_map(0)->local_to_global(i);
75 if (tensor_layout.
is_ghosted() == TensorLayout::Ghosts::GHOSTED)
77 const std::size_t nowned
78 = tensor_layout.
index_map(0)->size(IndexMap::MapSize::OWNED);
79 const std::size_t nghosts
80 = tensor_layout.
index_map(0)->size(IndexMap::MapSize::UNOWNED);
81 ghosts.resize(nghosts);
82 for (std::size_t i = 0; i != nghosts; ++i)
83 ghosts[i] = local_to_global[i + nowned];
91 virtual std::size_t
rank()
const 95 virtual std::size_t
size(std::size_t dim)
const 96 { dolfin_assert(dim == 0);
return size(); }
99 virtual std::pair<std::int64_t, std::int64_t>
106 {
get(block, num_rows[0], rows[0]); }
111 {
get_local(block, num_rows[0], rows[0]); }
116 {
set(block, num_rows[0], rows[0]); }
122 {
set_local(block, num_rows[0], rows[0]); }
127 {
add(block, num_rows[0], rows[0]); }
133 {
add_local(block, num_rows[0], rows[0]); }
139 {
add(block, rows[0].
size(), rows[0].data()); }
148 virtual void zero() = 0;
151 virtual void apply(std::string mode) = 0;
154 virtual std::string
str(
bool verbose)
const = 0;
159 virtual std::shared_ptr<GenericVector>
copy()
const = 0;
162 virtual void init(std::size_t N) = 0;
165 virtual void init(std::pair<std::size_t, std::size_t> range) = 0;
170 virtual void init(std::pair<std::size_t, std::size_t> range,
171 const std::vector<std::size_t>& local_to_global_map,
172 const std::vector<la_index>& ghost_indices) = 0;
175 virtual std::size_t
size()
const = 0;
181 virtual std::pair<std::int64_t, std::int64_t>
local_range()
const = 0;
184 virtual bool owns_index(std::size_t i)
const = 0;
188 virtual void get(
double* block, std::size_t m,
193 virtual void get_local(
double* block, std::size_t m,
197 virtual void set(
const double* block, std::size_t m,
201 virtual void set_local(
const double* block, std::size_t m,
205 virtual void add(
const double* block, std::size_t m,
209 virtual void add_local(
const double* block, std::size_t m,
213 virtual void get_local(std::vector<double>& values)
const = 0;
216 virtual void set_local(
const std::vector<double>& values) = 0;
224 const std::vector<dolfin::la_index>& indices)
const = 0;
227 virtual void gather(std::vector<double>& x,
228 const std::vector<dolfin::la_index>& indices)
const = 0;
237 virtual void abs() = 0;
243 virtual double norm(std::string norm_type)
const = 0;
246 virtual double min()
const = 0;
249 virtual double max()
const = 0;
252 virtual double sum()
const = 0;
261 auto y = this->
copy();
270 auto y = this->
copy();
279 auto y = this->
copy();
316 {
double value(0);
get_local(&value, 1, &i);
return value; }
320 {
double value(0);
get_local(&value, 1, &i);
return value; }
325 {
set(&value, 1, &i); }
virtual void add_local(const double *block, const std::vector< ArrayView< const dolfin::la_index >> &rows)
Add block of values using local indices.
Definition: GenericVector.h:143
virtual void init(const TensorLayout &tensor_layout)
Definition: GenericVector.h:58
virtual void abs()=0
Replace all entries in the vector by their absolute values.
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition: GenericVector.h:100
virtual std::size_t local_size() const =0
Return local size of vector.
virtual double inner(const GenericVector &x) const =0
Return inner product with given vector.
virtual std::pair< std::int64_t, std::int64_t > local_range() const =0
Return local ownership range of a vector.
virtual const GenericVector & operator*=(double a)=0
Multiply vector by given number.
std::pair< std::size_t, std::size_t > local_range(std::size_t dim) const
Return local range for dimension dim.
Definition: TensorLayout.cpp:76
virtual const GenericVector & operator=(const GenericVector &x)=0
Assignment operator.
std::shared_ptr< const IndexMap > index_map(std::size_t i) const
Return IndexMap for dimension.
Definition: TensorLayout.h:96
virtual double norm(std::string norm_type) const =0
Return norm of vector.
virtual std::size_t size() const =0
Return global size of vector.
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const =0
virtual double getitem(dolfin::la_index i) const
Get value of given entry.
Definition: GenericVector.h:319
virtual double min() const =0
Return minimum value of vector.
std::shared_ptr< GenericVector > operator*(double a)
Multiply vector by a scalar (returns a new vector)
Definition: GenericVector.h:277
virtual void add_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using local indices.
Definition: GenericVector.h:130
virtual void add(const double *block, const std::vector< ArrayView< const dolfin::la_index >> &rows)
Add block of values using global indices.
Definition: GenericVector.h:137
Definition: TensorLayout.h:41
virtual double sum() const =0
Return sum of vector.
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition: GenericVector.h:95
virtual std::size_t rank() const
Return tensor rank (number of dimensions)
Definition: GenericVector.h:91
virtual double operator[](dolfin::la_index i) const
Get value of given entry.
Definition: GenericVector.h:315
virtual const GenericVector & operator+=(const GenericVector &x)=0
Add given vector.
virtual const GenericVector & operator/=(double a)=0
Divide vector by given number.
A common interface for arbitrary rank tensors.
Definition: GenericTensor.h:48
Definition: ArrayView.h:31
std::shared_ptr< GenericVector > operator+(const GenericVector &x)
Sum two vectors (returns a new vector)
Definition: GenericVector.h:259
virtual double max() const =0
Return maximum value of vector.
virtual void add(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using global indices.
Definition: GenericVector.h:125
virtual void zero()=0
Set all entries to zero and keep any sparse structure.
virtual bool empty() const =0
Return true if empty.
virtual void gather_on_zero(std::vector< double > &x) const =0
Gather all entries into x on process 0.
virtual void apply(std::string mode)=0
Finalize assembly of tensor.
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual std::shared_ptr< GenericVector > copy() const =0
Return copy of vector.
virtual void set_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using local indices.
Definition: GenericVector.h:119
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
virtual void setitem(dolfin::la_index i, double value)
Definition: GenericVector.h:324
virtual ~GenericVector()
Destructor.
Definition: GenericVector.h:52
virtual std::string str(bool verbose) const =0
Return informal string representation (pretty-print)
virtual void get_local(double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows) const
Get block of values using local indices.
Definition: GenericVector.h:109
Ghosts is_ghosted() const
Require ghosts.
Definition: TensorLayout.h:103
virtual void axpy(double a, const GenericVector &x)=0
Add multiple of given vector (AXPY operation)
This class defines a common interface for vectors.
Definition: GenericVector.h:47
virtual bool owns_index(std::size_t i) const =0
Determine whether global vector index is owned by this process.
virtual const GenericVector & operator-=(const GenericVector &x)=0
Subtract given vector.