Flow123d  release_1.8.2-1603-g0109a2b
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 
24 #include <petscvec.h>
25 
27 
28 
29 /**
30  * Auxiliary class for output elementwise concentration vectors
31  * in convection transport, sorptions, dual porosity etc.
32  *
33  * Stores data in two formats:
34  * - shared pointer to std::vector of double
35  * - pointer to PETSC vector that use same data
36  *
37  * Allows the following functionalities:
38  * - return shared pointer to std::vector of double
39  * - return pointer to PETSC vector
40  * - create shared pointer to FieldElementwise object corresponding with std::vector of double
41  */
43 public:
44  typedef typename std::shared_ptr< std::vector<double> > VectorSeq;
45 
46  /// Create shared pointer and PETSC vector with given size.
47  void resize(unsigned int size)
48  {
49  data_ptr_ = std::make_shared< std::vector<double> >(size);
50  VecCreateSeqWithArray(PETSC_COMM_SELF, 1, size, &((*data_ptr_)[0]), &data_petsc_);
51  VecZeroEntries( data_petsc_ );
52  }
53 
54  /// Getter for shared pointer of output data.
55  VectorSeq get_data_ptr()
56  {
57  return data_ptr_;
58  }
59 
60  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
62  {
63  return data_petsc_;
64  }
65 
66  /// Create and return shared pointer to FieldElementwise object
67  template <int spacedim, class Value>
68  std::shared_ptr<FieldElementwise<spacedim, Value> > create_field(unsigned int n_comp)
69  {
70  std::shared_ptr<FieldElementwise<spacedim, Value> > field_ptr(
72  return field_ptr;
73  }
74 
75  /// Destructor.
77  {
78  if (data_ptr_) VecDestroy(&data_petsc_);
79  }
80 
81  /**
82  * Access to the vector element on index @p idx.
83  */
84  inline double &operator[](unsigned int idx)
85  {
86  OLD_ASSERT(idx < data_ptr_->size(), "Index is out of range.\n");
87  return (*data_ptr_)[idx];
88  }
89 
90 private:
91  /// shared pointer to vector of data
92  VectorSeq data_ptr_;
93  /// stored vector of data in PETSC format
95 };
96 
97 
98 #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 OLD_ASSERT(...)
Definition: global_defs.h:128
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).