Flow123d  release_2.2.0-914-gf1a3a4f
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 
28 
29 
30 /**
31  * Auxiliary class for output elementwise concentration vectors
32  * in convection transport, sorptions, dual porosity etc.
33  *
34  * Stores data in two formats:
35  * - shared pointer to std::vector of double
36  * - pointer to PETSC vector that use same data
37  *
38  * Allows the following functionalities:
39  * - return shared pointer to std::vector of double
40  * - return pointer to PETSC vector
41  * - create shared pointer to FieldElementwise object corresponding with std::vector of double
42  */
44 public:
45  typedef typename std::shared_ptr< std::vector<double> > VectorSeq;
46 
47  /// Create shared pointer and PETSC vector with given size.
48  void resize(unsigned int size)
49  {
50  data_ptr_ = std::make_shared< std::vector<double> >(size);
51  chkerr(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, size, &((*data_ptr_)[0]), &data_petsc_));
52  chkerr(VecZeroEntries( data_petsc_ ));
53  }
54 
55  /// Getter for shared pointer of output data.
56  VectorSeq get_data_ptr()
57  {
58  return data_ptr_;
59  }
60 
61 
62 
63  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
65  {
66  return data_petsc_;
67  }
68 
69  /// Getter for shared pointer of output data.
70  unsigned int size()
71  {
72  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
73  return data_ptr_->size();
74  }
75 
76 
77  /// Fill all values of data vector with given value.
78  void fill(double value)
79  {
80  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
81  std::fill(data_ptr_->begin(), data_ptr_->end(), value);
82  }
83 
84 
85  /// Create and return shared pointer to FieldElementwise object
86  template <int spacedim, class Value>
87  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
88  {
89  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
91  return field_ptr;
92  }
93 
94  /// Destructor.
96  {
97  if (data_ptr_) chkerr(VecDestroy(&data_petsc_));
98  }
99 
100  /**
101  * Access to the vector element on index @p idx.
102  */
103  inline double &operator[](unsigned int idx)
104  {
105  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
106  return (*data_ptr_)[idx];
107  }
108 
109 private:
110  /// shared pointer to vector of data
111  VectorSeq data_ptr_;
112  /// stored vector of data in PETSC format
114 };
115 
116 /**
117  * Like VectorSeqDouble but for MPI PETSC vectors. Have acces to local part.
118  */
119 class VectorMPI {
120 public:
121  typedef typename std::vector<double> VectorData;
122  typedef typename std::shared_ptr< VectorData > VectorDataPtr;
123 
125  }
126 
127  /// Create shared pointer and PETSC vector with given size. COLLECTIVE.
128  VectorMPI(unsigned int local_size) {
129  resize(local_size);
130  }
131 
132  /**
133  * Resize the vector to given local size. Operation is allowed only if this object is
134  * a unique vector object pointing to the actual data.
135  */
136  void resize(unsigned int local_size) {
137  if (data_ptr_.use_count() ==0) {
138  data_ptr_ = std::make_shared< std::vector<double> >(local_size);
139  } else {
140  ASSERT_DBG( data_ptr_.use_count() == 1 ) ( data_ptr_.use_count() ).error("Object referenced by other pointer. Can not resize.");
141  chkerr(VecDestroy(&data_petsc_));
142  data_ptr_->resize(local_size);
143  }
144  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, local_size, PETSC_DECIDE, &((*data_ptr_)[0]), &data_petsc_));
145  chkerr(VecZeroEntries( data_petsc_ ));
146  }
147 
148  /// Return new vector with same parallel structure.
149  void duplicate(VectorMPI other) {
150  this->resize(other.data().size());
151  }
152 
153  /// Getter for shared pointer of output data.
154  VectorDataPtr data_ptr()
155  {
156  return data_ptr_;
157  }
158 
159  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
160  Vec &petsc_vec()
161  {
162  return data_petsc_;
163  }
164 
165  void zero_entries() {
166  chkerr(VecZeroEntries( data_petsc_ ));
167  }
168 
169  VectorData &data()
170  {
172  return *data_ptr_;
173  }
174 
175  /*
176  /// Create and return shared pointer to FieldElementwise object
177  template <int spacedim, class Value>
178  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
179  {
180  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
181  new FieldElementwise<spacedim, Value>( data_ptr_, n_comp ));
182  return field_ptr;
183  }*/
184 
185  void swap(VectorMPI &other) {
186  OLD_ASSERT_EQUAL(this->data_ptr_->size(), other.data_ptr_->size());
187  uint size = this->data_ptr_->size();
188  std::swap(this->data_ptr_, other.data_ptr_);
189  chkerr(VecDestroy(&data_petsc_));
190  chkerr(VecDestroy(&other.data_petsc_));
191  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, size, PETSC_DECIDE, &((*data_ptr_)[0]), &data_petsc_));
192  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, size, PETSC_DECIDE, &((*other.data_ptr_)[0]), &other.data_petsc_));
193  }
194 
195 
196  void copy(VectorMPI &other) {
197  OLD_ASSERT_EQUAL(this->data_ptr_->size(), other.data_ptr_->size());
198  chkerr(VecCopy(other.data_petsc_, data_petsc_));
199  }
200 
201  /// Destructor.
203  {
204  if (data_ptr_.use_count() == 1) chkerr(VecDestroy(&data_petsc_));
205  }
206 
207  /**
208  * Access to the vector element on index @p idx.
209  */
210  inline double &operator[](unsigned int idx)
211  {
213  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
214  return (*data_ptr_)[idx];
215  }
216 
217 private:
218 
219  /// shared pointer to vector of data
220  VectorDataPtr data_ptr_;
221  /// stored vector of data in PETSC format
223 };
224 
225 
226 
227 #endif /* VECTOR_SEQ_DOUBLE_HH_ */
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
void duplicate(VectorMPI other)
Return new vector with same parallel structure.
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.
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.
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
std::shared_ptr< FieldElementwise< spacedim, Value > > create_field(unsigned int n_comp)
Create and return shared pointer to FieldElementwise object.
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.
std::shared_ptr< VectorData > VectorDataPtr
VectorData & data()
Vec & get_data_petsc()
Getter for PETSC vector of output data (e.g. can be used by scatters).