OGS
PropertyVector.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <cassert>
13#include <iterator>
14#include <ostream>
15#include <string>
16#include <utility>
17#include <vector>
18
19#include "BaseLib/Algorithm.h"
20#include "Location.h"
21
22namespace MeshLib
23{
25{
26public:
28 std::vector<std::size_t> const& exclude_positions) const = 0;
29 virtual ~PropertyVectorBase() = default;
30
32 std::string const& getPropertyName() const { return _property_name; }
34
35protected:
36 PropertyVectorBase(std::string property_name,
37 MeshItemType mesh_item_type,
38 std::size_t n_components)
39 : _n_components(n_components),
40 _mesh_item_type(mesh_item_type),
41 _property_name(std::move(property_name))
42 {
43 }
44
45 int const _n_components;
47 std::string const _property_name;
48};
49
54template <typename PROP_VAL_TYPE>
55class PropertyVector : public std::vector<PROP_VAL_TYPE>,
57{
58 friend class Properties;
59
60public:
61 std::size_t getNumberOfTuples() const
62 {
63 return std::vector<PROP_VAL_TYPE>::size() / _n_components;
64 }
65
67 PROP_VAL_TYPE& getComponent(std::size_t tuple_index, int component)
68 {
69 assert(component < _n_components);
70 assert(tuple_index < getNumberOfTuples());
71 return this->operator[](tuple_index* getNumberOfGlobalComponents() +
72 component);
73 }
74
76 PROP_VAL_TYPE const& getComponent(std::size_t tuple_index,
77 int component) const
78 {
79 assert(component < _n_components);
80 assert(tuple_index < getNumberOfTuples());
81 return this->operator[](tuple_index* getNumberOfGlobalComponents() +
82 component);
83 }
84
86 std::vector<std::size_t> const& exclude_positions) const override
87 {
90 BaseLib::excludeObjectCopy(*this, exclude_positions, *t);
91 return t;
92 }
93
96 std::size_t size() const { return std::vector<PROP_VAL_TYPE>::size(); }
97
98protected:
104 explicit PropertyVector(std::string const& property_name,
105 MeshItemType mesh_item_type,
106 std::size_t n_components)
107 : std::vector<PROP_VAL_TYPE>(),
108 PropertyVectorBase(property_name, mesh_item_type, n_components)
109 {
110 }
111
119 PropertyVector(std::size_t n_property_values,
120 std::string const& property_name,
121 MeshItemType mesh_item_type,
122 std::size_t n_components)
123 : std::vector<PROP_VAL_TYPE>(n_property_values * n_components),
124 PropertyVectorBase(property_name, mesh_item_type, n_components)
125 {
126 }
127};
128
138template <typename T>
139class PropertyVector<T*> : public std::vector<std::size_t>,
140 public PropertyVectorBase
141{
142 friend class Properties;
143
144public:
147 {
148 for (auto v : _values)
149 {
150 delete[] v;
151 }
152 }
153
156 T* const& operator[](std::size_t id) const
157 {
158 return _values[std::vector<std::size_t>::operator[](id)];
159 }
160
161 T*& operator[](std::size_t id)
162 {
163 return _values[std::vector<std::size_t>::operator[](id)];
164 }
165
166 void initPropertyValue(std::size_t group_id, T const& value)
167 {
168 if (_n_components != 1)
169 {
170 OGS_FATAL(
171 "Single-component version of initPropertyValue() is called "
172 "for a multi-components PropertyVector<T*>");
173 }
174 auto* p = new T[1];
175 p[0] = value;
176 _values[group_id] = p;
177 }
178
179 void initPropertyValue(std::size_t group_id, std::vector<T> const& values)
180 {
181 if (_n_components != static_cast<int>(values.size()))
182 {
183 OGS_FATAL(
184 "The size of provided values in initPropertyValue() is "
185 "not same as the number of components in PropertyVector<T*>");
186 }
187
188 auto* p = new T[values.size()];
189 for (unsigned i = 0; i < values.size(); i++)
190 {
191 p[i] = values[i];
192 }
193 _values[group_id] = p;
194 }
195
196 std::size_t getNumberOfTuples() const
197 {
198 return std::vector<std::size_t>::size();
199 }
200
203 std::size_t size() const
204 {
205 return _n_components * std::vector<std::size_t>::size();
206 }
207
209 std::vector<std::size_t> const& exclude_positions) const override
210 {
211 // create new PropertyVector with modified mapping
213 _values.size() / _n_components,
214 BaseLib::excludeObjectCopy(*this, exclude_positions),
215 _property_name, _mesh_item_type, _n_components));
216 // copy pointers to property values
217 for (std::size_t j(0); j < _values.size(); j++)
218 {
219 std::vector<T> values(_values[j], _values[j] + _n_components);
220 t->initPropertyValue(j, values);
221 }
222 return t;
223 }
224
226 T const& getComponent(std::size_t tuple_index, int component) const
227 {
228 assert(component < _n_components);
229 assert(tuple_index < getNumberOfTuples());
230 const double* p = this->operator[](tuple_index);
231 if (p == nullptr)
232 {
233 OGS_FATAL(
234 "No data found in the property vector {:s} "
235 "for the tuple index {:d} and component {:d}",
236 getPropertyName(), tuple_index, component);
237 }
238 return p[component];
239 }
240
241#ifndef NDEBUG
242 std::ostream& print(std::ostream& os) const
243 {
244 os << "\nPropertyVector<T*> at address: " << this << ":\n";
245 os << "\tmapping (" << size() << "):\n";
246 std::copy(this->cbegin(), this->cend(),
247 std::ostream_iterator<std::size_t>(os, " "));
248 os << "\n\tvalues (" << _values.size() << "):\n";
249 for (std::size_t k(0); k < _values.size(); k++)
250 {
251 os << "val: " << *(_values[k]) << ", address: " << _values[k]
252 << "\n";
253 }
254 return os;
255 }
256#endif
257
258protected:
270 PropertyVector(std::size_t n_prop_groups,
271 std::vector<std::size_t>
272 item2group_mapping,
273 std::string const& property_name,
274 MeshItemType mesh_item_type,
275 std::size_t n_components)
276 : std::vector<std::size_t>(std::move(item2group_mapping)),
277 PropertyVectorBase(property_name, mesh_item_type, n_components),
278 _values(n_prop_groups * n_components)
279 {
280 }
281
282private:
283 std::vector<T*> _values;
284 // hide method
285 T* at(std::size_t);
286};
287
288} // end namespace MeshLib
#define OGS_FATAL(...)
Definition: Error.h:26
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition: Properties.h:36
virtual PropertyVectorBase * clone(std::vector< std::size_t > const &exclude_positions) const =0
MeshItemType getMeshItemType() const
MeshItemType const _mesh_item_type
PropertyVectorBase(std::string property_name, MeshItemType mesh_item_type, std::size_t n_components)
virtual ~PropertyVectorBase()=default
std::string const _property_name
int getNumberOfGlobalComponents() const
std::string const & getPropertyName() const
T const & getComponent(std::size_t tuple_index, int component) const
Returns the value for the given component stored in the given tuple.
PropertyVectorBase * clone(std::vector< std::size_t > const &exclude_positions) const override
T *& operator[](std::size_t id)
void initPropertyValue(std::size_t group_id, std::vector< T > const &values)
PropertyVector(std::size_t n_prop_groups, std::vector< std::size_t > item2group_mapping, std::string const &property_name, MeshItemType mesh_item_type, std::size_t n_components)
The constructor taking meta information for the data.
T *const & operator[](std::size_t id) const
~PropertyVector() override
Destructor ensures the deletion of the heap-constructed objects.
std::ostream & print(std::ostream &os) const
void initPropertyValue(std::size_t group_id, T const &value)
std::size_t getNumberOfTuples() const
PropertyVector(std::size_t n_property_values, std::string const &property_name, MeshItemType mesh_item_type, std::size_t n_components)
The constructor taking meta information for the data.
PROP_VAL_TYPE & getComponent(std::size_t tuple_index, int component)
Returns the value for the given component stored in the given tuple.
PropertyVector(std::string const &property_name, MeshItemType mesh_item_type, std::size_t n_components)
The constructor taking meta information for the data.
std::size_t getNumberOfTuples() const
PropertyVectorBase * clone(std::vector< std::size_t > const &exclude_positions) const override
std::size_t size() const
PROP_VAL_TYPE const & getComponent(std::size_t tuple_index, int component) const
Returns the value for the given component stored in the given tuple.
std::vector< T > excludeObjectCopy(std::vector< T > const &src_vec, std::vector< std::size_t > const &exclude_positions)
Definition: Algorithm.h:33
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
static const double p
static const double v
static const double t
MeshItemType
Definition: Location.h:21