Flow123d  jenkins-Flow123d-windows-release-multijob-285
vec_seq_double.hh
Go to the documentation of this file.
1 /*
2  * vec_seq_double.hh
3  *
4  * Created on: Aug 30, 2012
5  * Author: jb
6  */
7 
8 
9 #ifndef VECTOR_SEQ_DOUBLE_HH_
10 #define VECTOR_SEQ_DOUBLE_HH_
11 
12 #include <vector>
13 #include <memory>
14 
15 #include <petscvec.h>
16 
18 
19 
20 /**
21  * Auxiliary class for output elementwise concentration vectors
22  * in convection transport, sorptions, dual porosity etc.
23  *
24  * Stores data in two formats:
25  * - shared pointer to std::vector of double
26  * - pointer to PETSC vector that use same data
27  *
28  * Allows the following functionalities:
29  * - return shared pointer to std::vector of double
30  * - return pointer to PETSC vector
31  * - create shared pointer to FieldElementwise object corresponding with std::vector of double
32  */
34 public:
35  typedef typename std::shared_ptr< std::vector<double> > VectorSeq;
36 
37  /// Create shared pointer and PETSC vector with given size.
38  void resize(unsigned int size)
39  {
40  data_ptr_ = std::make_shared< std::vector<double> >(size);
41  VecCreateSeqWithArray(PETSC_COMM_SELF, 1, size, &((*data_ptr_)[0]), &data_petsc_);
42  VecZeroEntries( data_petsc_ );
43  }
44 
45  /// Getter for shared pointer of output data.
46  VectorSeq get_data_ptr()
47  {
48  return data_ptr_;
49  }
50 
51  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
53  {
54  return data_petsc_;
55  }
56 
57  /// Create and return shared pointer to FieldElementwise object
58  template <int spacedim, class Value>
59  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
60  {
61  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
63  return field_ptr;
64  }
65 
66  /// Destructor.
68  {
69  if (data_ptr_) VecDestroy(&data_petsc_);
70  }
71 
72  /**
73  * Access to the vector element on index @p idx.
74  */
75  inline double &operator[](unsigned int idx)
76  {
77  ASSERT(idx < data_ptr_->size(), "Index is out of range.\n");
78  return (*data_ptr_)[idx];
79  }
80 
81 private:
82  /// shared pointer to vector of data
83  VectorSeq data_ptr_;
84  /// stored vector of data in PETSC format
86 };
87 
88 
89 #endif /* VECTOR_SEQ_DOUBLE_HH_ */
Vec data_petsc_
stored vector of data in PETSC format
~VectorSeqDouble()
Destructor.
std::shared_ptr< std::vector< double > > VectorSeq
double & operator[](unsigned int idx)
VectorSeq data_ptr_
shared pointer to vector of data
#define ASSERT(...)
Definition: global_defs.h:121
void resize(unsigned int size)
Create shared pointer and PETSC vector with given size.
std::shared_ptr< FieldElementwise< spacedim, Value > > create_field(unsigned int n_comp)
Create and return shared pointer to FieldElementwise object.
VectorSeq get_data_ptr()
Getter for shared pointer of output data.
Vec & get_data_petsc()
Getter for PETSC vector of output data (e.g. can be used by scatters).