Flow123d  intersections_paper-476-gbe68821
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  /// Create and return shared pointer to FieldElementwise object
78  template <int spacedim, class Value>
79  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
80  {
81  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
83  return field_ptr;
84  }
85 
86  /// Destructor.
88  {
89  if (data_ptr_) chkerr(VecDestroy(&data_petsc_));
90  }
91 
92  /**
93  * Access to the vector element on index @p idx.
94  */
95  inline double &operator[](unsigned int idx)
96  {
97  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
98  return (*data_ptr_)[idx];
99  }
100 
101 private:
102  /// shared pointer to vector of data
103  VectorSeq data_ptr_;
104  /// stored vector of data in PETSC format
106 };
107 
108 /**
109  * Like VectorSeqDouble but for MPI PETSC vectors. Have acces to local part.
110  */
111 class VectorMPI {
112 public:
113  typedef typename std::vector<double> VectorData;
114  typedef typename std::shared_ptr< VectorData > VectorDataPtr;
115 
117  }
118 
119  /// Create shared pointer and PETSC vector with given size. COLLECTIVE.
120  VectorMPI(unsigned int local_size) {
121  resize(local_size);
122  }
123 
124  /**
125  * Resize the vector to given local size. Operation is allowed only if this object is
126  * a unique vector object pointing to the actual data.
127  */
128  void resize(unsigned int local_size) {
129  if (data_ptr_.use_count() ==0) {
130  data_ptr_ = std::make_shared< std::vector<double> >(local_size);
131  } else {
132  ASSERT_DBG( data_ptr_.use_count() == 1 ) ( data_ptr_.use_count() ).error("Object referenced by other pointer. Can not resize.");
133  chkerr(VecDestroy(&data_petsc_));
134  data_ptr_->resize(local_size);
135  }
136  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, local_size, PETSC_DECIDE, &((*data_ptr_)[0]), &data_petsc_));
137  chkerr(VecZeroEntries( data_petsc_ ));
138  }
139 
140  /// Return new vector with same parallel structure.
141  void duplicate(VectorMPI other) {
142  this->resize(other.data().size());
143  }
144 
145  /// Getter for shared pointer of output data.
146  VectorDataPtr data_ptr()
147  {
148  return data_ptr_;
149  }
150 
151  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
152  Vec &petsc_vec()
153  {
154  return data_petsc_;
155  }
156 
157  void zero_entries() {
158  chkerr(VecZeroEntries( data_petsc_ ));
159  }
160 
161  VectorData &data()
162  {
164  return *data_ptr_;
165  }
166 
167  /*
168  /// Create and return shared pointer to FieldElementwise object
169  template <int spacedim, class Value>
170  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
171  {
172  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
173  new FieldElementwise<spacedim, Value>( data_ptr_, n_comp ));
174  return field_ptr;
175  }*/
176 
177  void swap(VectorMPI &other) {
178  OLD_ASSERT_EQUAL(this->data_ptr_->size(), other.data_ptr_->size());
179  uint size = this->data_ptr_->size();
180  std::swap(this->data_ptr_, other.data_ptr_);
181  chkerr(VecDestroy(&data_petsc_));
182  chkerr(VecDestroy(&other.data_petsc_));
183  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, size, PETSC_DECIDE, &((*data_ptr_)[0]), &data_petsc_));
184  chkerr(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, size, PETSC_DECIDE, &((*other.data_ptr_)[0]), &other.data_petsc_));
185  }
186 
187 
188  void copy(VectorMPI &other) {
189  OLD_ASSERT_EQUAL(this->data_ptr_->size(), other.data_ptr_->size());
190  chkerr(VecCopy(other.data_petsc_, data_petsc_));
191  }
192 
193  /// Destructor.
195  {
196  if (data_ptr_.use_count() == 1) chkerr(VecDestroy(&data_petsc_));
197  }
198 
199  /**
200  * Access to the vector element on index @p idx.
201  */
202  inline double &operator[](unsigned int idx)
203  {
205  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
206  return (*data_ptr_)[idx];
207  }
208 
209 private:
210 
211  /// shared pointer to vector of data
212  VectorDataPtr data_ptr_;
213  /// stored vector of data in PETSC format
215 };
216 
217 
218 
219 #endif /* VECTOR_SEQ_DOUBLE_HH_ */
Vec data_petsc_
stored vector of data in PETSC format
void copy(VectorMPI &other)
unsigned int uint
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:142
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.
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:336
void swap(VectorMPI &other)
#define ASSERT_DBG(expr)
Definition: asserts.hh:350
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).