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