Flow123d  release_3.0.0-506-g34af125
vec_seq_double.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file vec_seq_double.hh
15  * @brief
16  */
17 
18 #ifndef VECTOR_SEQ_DOUBLE_HH_
19 #define VECTOR_SEQ_DOUBLE_HH_
20 
21 #include <vector>
22 #include <memory>
23 #include "system/global_defs.h"
24 
25 #include <petscvec.h>
26 
27 #include "fem/dofhandler.hh"
28 #include "fem/finite_element.hh"
29 
30 template <int spacedim, class Value> class FieldFE;
31 
32 
33 /**
34  * Auxiliary class for output elementwise concentration vectors
35  * in convection transport, sorptions, dual porosity etc.
36  *
37  * Stores data in two formats:
38  * - shared pointer to std::vector of double
39  * - pointer to PETSC vector that use same data
40  *
41  * Allows the following functionalities:
42  * - return shared pointer to std::vector of double
43  * - return pointer to PETSC vector
44  * - create shared pointer to FieldFE object corresponding with std::vector of double
45  */
47 public:
48  typedef typename std::shared_ptr< std::vector<double> > VectorSeq;
49 
50  /// Constructor.
52  : dh_(nullptr) {}
53 
54  /// Create shared pointer and PETSC vector with given size.
55  void resize(unsigned int size)
56  {
57  data_ptr_ = std::make_shared< std::vector<double> >(size);
58  chkerr(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, size, &((*data_ptr_)[0]), &data_petsc_));
59  chkerr(VecZeroEntries( data_petsc_ ));
60  }
61 
62  /// Getter for shared pointer of output data.
63  VectorSeq get_data_ptr()
64  {
65  return data_ptr_;
66  }
67 
68 
69 
70  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
72  {
73  return data_petsc_;
74  }
75 
76  /// Getter for shared pointer of output data.
77  unsigned int size()
78  {
79  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
80  return data_ptr_->size();
81  }
82 
83 
84  /// Fill all values of data vector with given value.
85  void fill(double value)
86  {
87  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
88  std::fill(data_ptr_->begin(), data_ptr_->end(), value);
89  }
90 
91 
92  /// Create and return shared pointer to FieldFE object
93  template <int spacedim, class Value>
94  std::shared_ptr<FieldFE<spacedim, Value> > create_field(Mesh & mesh, unsigned int n_comp);
95 
96  /**
97  * Fill output data of field_ptr.
98  *
99  * Set data to data vector of field in correct order according to values of DOF handler indices.
100  */
101  template <int spacedim, class Value>
102  void fill_output_data(std::shared_ptr<FieldFE<spacedim, Value> > field_ptr);
103 
104  /// Destructor.
106  {
107  if (data_ptr_) chkerr(VecDestroy(&data_petsc_));
108  }
109 
110  /**
111  * Access to the vector element on index @p idx.
112  */
113  inline double &operator[](unsigned int idx)
114  {
115  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
116  return (*data_ptr_)[idx];
117  }
118 
119 private:
120  /// shared pointer to vector of data
121  VectorSeq data_ptr_;
122  /// stored vector of data in PETSC format
124  /// Finite element objects (allow to create DOF handler)
129  // DOF handler object allow create FieldFE
130  std::shared_ptr<DOFHandlerMultiDim> dh_;
131 };
132 
133 /**
134  * Like VectorSeqDouble but for MPI PETSC vectors. Have acces to local part.
135  */
136 class VectorMPI {
137 public:
138  typedef typename std::vector<double> VectorData;
139  typedef typename std::shared_ptr< VectorData > VectorDataPtr;
140 
142  }
143 
144  /// Create shared pointer and PETSC vector with given size. COLLECTIVE.
145  VectorMPI(unsigned int local_size) {
146  resize(local_size);
147  }
148 
149  /**
150  * Resize the vector to given local size. Operation is allowed only if this object is
151  * a unique vector object pointing to the actual data.
152  */
153  void resize(unsigned int local_size) {
154  if (data_ptr_.use_count() ==0) {
155  data_ptr_ = std::make_shared< std::vector<double> >(local_size);
156  } else {
157  ASSERT_DBG( data_ptr_.use_count() == 1 ) ( data_ptr_.use_count() ).error("Object referenced by other pointer. Can not resize.");
158  chkerr(VecDestroy(&data_petsc_));
159  data_ptr_->resize(local_size);
160  }
161  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, local_size, PETSC_DECIDE, &((*data_ptr_)[0]), &data_petsc_));
162  chkerr(VecZeroEntries( data_petsc_ ));
163  }
164 
165  /// Return new vector with same parallel structure.
166  void duplicate(VectorMPI other) {
167  this->resize(other.data().size());
168  }
169 
170  /// Getter for shared pointer of output data.
171  VectorDataPtr data_ptr()
172  {
173  return data_ptr_;
174  }
175 
176  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
177  Vec &petsc_vec()
178  {
179  return data_petsc_;
180  }
181 
182  void zero_entries() {
183  chkerr(VecZeroEntries( data_petsc_ ));
184  }
185 
186  VectorData &data()
187  {
189  return *data_ptr_;
190  }
191 
192  /*
193  /// Create and return shared pointer to FieldElementwise object
194  template <int spacedim, class Value>
195  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
196  {
197  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
198  new FieldElementwise<spacedim, Value>( data_ptr_, n_comp ));
199  return field_ptr;
200  }*/
201 
202  void swap(VectorMPI &other) {
203  OLD_ASSERT_EQUAL(this->data_ptr_->size(), other.data_ptr_->size());
204  uint size = this->data_ptr_->size();
205  std::swap(this->data_ptr_, other.data_ptr_);
206  chkerr(VecDestroy(&data_petsc_));
207  chkerr(VecDestroy(&other.data_petsc_));
208  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, size, PETSC_DECIDE, &((*data_ptr_)[0]), &data_petsc_));
209  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, size, PETSC_DECIDE, &((*other.data_ptr_)[0]), &other.data_petsc_));
210  }
211 
212 
213  void copy(VectorMPI &other) {
214  OLD_ASSERT_EQUAL(this->data_ptr_->size(), other.data_ptr_->size());
215  chkerr(VecCopy(other.data_petsc_, data_petsc_));
216  }
217 
218  /// Destructor.
220  {
221  if (data_ptr_.use_count() == 1) chkerr(VecDestroy(&data_petsc_));
222  }
223 
224  /**
225  * Access to the vector element on index @p idx.
226  */
227  inline double &operator[](unsigned int idx)
228  {
230  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
231  return (*data_ptr_)[idx];
232  }
233 
234 private:
235 
236  /// shared pointer to vector of data
237  VectorDataPtr data_ptr_;
238  /// stored vector of data in PETSC format
240 };
241 
242 
243 
244 #endif /* VECTOR_SEQ_DOUBLE_HH_ */
Declaration of class which handles the ordering of degrees of freedom (dof) and mappings between loca...
FiniteElement< 1 > * fe1_
Vec data_petsc_
stored vector of data in PETSC format
void copy(VectorMPI &other)
unsigned int uint
void fill(double value)
Fill all values of data vector with given value.
void zero_entries()
~VectorSeqDouble()
Destructor.
std::shared_ptr< std::vector< double > > VectorSeq
Definition: mesh.h:80
void duplicate(VectorMPI other)
Return new vector with same parallel structure.
FiniteElement< 0 > * fe0_
Finite element objects (allow to create DOF handler)
std::vector< double > VectorData
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:147
VectorDataPtr data_ptr()
Getter for shared pointer of output data.
VectorSeqDouble()
Constructor.
std::shared_ptr< FieldFE< spacedim, Value > > create_field(Mesh &mesh, unsigned int n_comp)
Create and return shared pointer to FieldFE object.
void fill_output_data(std::shared_ptr< FieldFE< spacedim, Value > > field_ptr)
double & operator[](unsigned int idx)
~VectorMPI()
Destructor.
double & operator[](unsigned int idx)
unsigned int size()
Getter for shared pointer of output data.
static constexpr bool value
Definition: json.hpp:87
VectorSeq data_ptr_
shared pointer to vector of data
Global macros to enhance readability and debugging, general constants.
FiniteElement< 3 > * fe3_
VectorDataPtr data_ptr_
shared pointer to vector of data
Vec data_petsc_
stored vector of data in PETSC format
void resize(unsigned int local_size)
void resize(unsigned int size)
Create shared pointer and PETSC vector with given size.
Vec & petsc_vec()
Getter for PETSC vector of output data (e.g. can be used by scatters).
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value andis_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:8688
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:335
void swap(VectorMPI &other)
#define ASSERT_DBG(expr)
Definition: asserts.hh:349
VectorMPI(unsigned int local_size)
Create shared pointer and PETSC vector with given size. COLLECTIVE.
#define OLD_ASSERT_EQUAL(a, b)
Definition: global_defs.h:133
VectorSeq get_data_ptr()
Getter for shared pointer of output data.
Abstract class for description of finite elements.
FiniteElement< 2 > * fe2_
std::shared_ptr< VectorData > VectorDataPtr
VectorData & data()
std::shared_ptr< DOFHandlerMultiDim > dh_
Definition: field.hh:56
Vec & get_data_petsc()
Getter for PETSC vector of output data (e.g. can be used by scatters).