Loading...
Searching...
No Matches
stencil.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BTPIT_STENCIL_HPP__
26#define __BTPIT_STENCIL_HPP__
27
28#include "stencil_weight.hpp"
29
30#include "bitpit_common.hpp"
31#include "bitpit_containers.hpp"
32#include "bitpit_operators.hpp"
33
34#include <array>
35#include <ostream>
36#include <unordered_map>
37#include <vector>
38
39namespace bitpit {
40
41// Stream operators for the stencil class
42template<typename weight_t, typename value_t>
43class DiscreteStencil;
44
45template<typename weight_t, typename value_t>
46OBinaryStream & operator<<(OBinaryStream &buffer, const DiscreteStencil<weight_t, value_t> &stencil);
47
48template<typename weight_t, typename value_t>
49IBinaryStream & operator>>(IBinaryStream &buffer, DiscreteStencil<weight_t, value_t> &stencil);
50
58template<typename weight_t, typename value_t = typename DiscreteStencilWeightValueInfo<weight_t>::type>
60
61template<typename W, typename V>
62friend OBinaryStream & (operator<<) (OBinaryStream &buffer, const DiscreteStencil<W, V> &stencil);
63template<typename W, typename V>
64friend IBinaryStream & (operator>>) (IBinaryStream &buffer, DiscreteStencil<W, V> &stencil);
65
66public:
67 long NULL_ID = - std::numeric_limits<long>::max();
68
69 using weight_type = weight_t;
71
72 using value_type = value_t;
73
75
79 struct item_type {
80 long id;
81 weight_type weight;
82 };
83
84 DiscreteStencil(const weight_t &zero = weight_t());
85 DiscreteStencil(std::size_t nItems, const weight_t &zero = weight_t());
86 DiscreteStencil(std::size_t size, const long *pattern, const weight_t &zero = weight_t());
87 DiscreteStencil(std::size_t size, const long *pattern, const weight_t *weights, const weight_t &zero = weight_t());
88
89 virtual ~DiscreteStencil() = default;
90
91 void initialize(std::size_t nItems, const weight_t &zero = weight_t());
92 void initialize(std::size_t size, const long *pattern, const weight_t &zero = weight_t());
93 void initialize(std::size_t size, const long *pattern, const weight_t *weights, const weight_t &zero = weight_t());
95
96 void clear(bool release = false);
97
98 std::size_t size() const;
99
100 void resize(std::size_t nItems);
101 void reserve(std::size_t nItems);
102
103 long & getPattern(std::size_t pos);
104 const long & getPattern(std::size_t pos) const;
105 long * patternData();
106 const long * patternData() const;
107 void setPattern(std::size_t pos, long id);
108
109 weight_t & getWeight(std::size_t pos);
110 const weight_t & getWeight(std::size_t pos) const;
111 weight_t * weightData();
112 const weight_t * weightData() const;
113 void setWeight(std::size_t pos, const weight_t &weight);
114 void setWeight(std::size_t pos, weight_t &&weight);
115 void sumWeight(std::size_t pos, const weight_t &weight, double factor = 1.);
116 void zeroWeight(std::size_t pos);
117
118 void setItem(std::size_t pos, long id, const weight_t &weight);
119 void setItem(std::size_t pos, long id, weight_t &&weight);
120 void sumItem(long id, const weight_t &weight, double factor = 1.);
121 void appendItem(long id, const weight_t &weight);
122 void appendItem(long id, weight_t &&weight);
123
124 weight_t & getConstant();
125 const weight_t & getConstant() const;
126 void setConstant(const weight_t &constant);
127 void setConstant(weight_t &&constant);
128 void sumConstant(const weight_t &constant, double factor = 1.);
129 void zeroConstant();
130
131 void sum(const DiscreteStencil<weight_t, value_t> &other, double factor);
132
133 void optimize(double tolerance = 1.e-12);
134 void renumber(const std::unordered_map<long, long> &map);
135 template<typename Mapper>
136 void renumber(const Mapper &mapper);
137 void addComplementToZero(long id);
138 void zero();
139
140 void display(std::ostream &out, double factor = 1.) const;
141
142 size_t getBinarySize() const;
143
144 weight_t & at(long id);
145 const weight_t & at(long id) const;
146
147 weight_t & operator[](long id);
148
153
154protected:
156
157 weight_t m_zero;
158 std::vector<long> m_pattern;
159 std::vector<weight_t> m_weights;
160 weight_t m_constant;
161
162 virtual void appendWeight(weight_t &&weight);
163 virtual void appendWeight(const weight_t &weight);
164
165 virtual void clearWeights(bool release);
166
167private:
168 weight_t * findWeight(long id);
169 const weight_t * findWeight(long id) const;
170
171};
172
181template<typename weight_t, typename value_t = typename DiscreteStencilWeightValueInfo<weight_t>::type>
182class MPDiscreteStencil : public DiscreteStencil<weight_t, value_t>
183{
184
185public:
187
188 MPDiscreteStencil(const weight_t &zero = weight_t());
189 MPDiscreteStencil(std::size_t nItems, const weight_t &zero = weight_t());
190 MPDiscreteStencil(std::size_t size, const long *pattern, const weight_t &zero = weight_t());
191 MPDiscreteStencil(std::size_t size, const long *pattern, const weight_t *weights, const weight_t &zero = weight_t());
192
194
195protected:
196 weight_pool_type *m_weightPool;
197
198 void appendWeight(const weight_t &weight) override;
199
200 void clearWeights(bool release) override;
201
202};
203
204// Declaration of the typdefs
208
212
213}
214
215// Operators for the stencil class
216template <typename weight_t, typename value_t>
218
219template <typename weight_t, typename value_t>
221
222template <typename weight_t, typename value_t>
224
225template <typename weight_t, typename value_t>
227
228template <typename weight_t, typename value_t>
230
231// Operators for the specializations
232template <typename V>
233typename bitpit::DiscreteStencil<std::array<V, 3>> operator*(const typename bitpit::DiscreteStencil<V> &stencil, const std::array<V, 3> &vector);
234template <typename V>
235typename bitpit::DiscreteStencil<std::array<V, 3>> operator*(const std::array<V, 3> &vector, const typename bitpit::DiscreteStencil<V> &stencil);
236
237// Functions for the specializations
238template <typename V>
239typename bitpit::DiscreteStencil<V> dotProduct(const typename bitpit::DiscreteStencil<std::array<V, 3>> &stencil, const typename bitpit::DiscreteStencil<std::array<V, 3>>::weight_type &vector);
240template <typename V>
241void dotProduct(const typename bitpit::DiscreteStencil<std::array<V, 3>> &stencil, const typename bitpit::DiscreteStencil<std::array<V, 3>>::weight_type &vector, typename bitpit::DiscreteStencil<V> *stencil_dotProduct);
242
243template <typename V>
244typename bitpit::DiscreteStencil<std::array<V, 3>> project(const typename bitpit::DiscreteStencil<std::array<V, 3>> &stencil, const std::array<V, 3> &direction);
245template <typename V>
246void project(const typename bitpit::DiscreteStencil<std::array<V, 3>> &stencil, const std::array<V, 3> &direction, typename bitpit::DiscreteStencil<std::array<V, 3>> *stencil_projection);
247
248// Template implementation
249#include "stencil.tpp"
250
251// Explicit instantization
252#ifndef __BTPIT_STENCIL_SRC__
253namespace bitpit {
254
255extern template class DiscreteStencilWeightPool<double>;
256extern template class DiscreteStencilWeightPool<std::array<double, 3>>;
257extern template class DiscreteStencilWeightPool<std::vector<double>>;
258
259extern template class DiscreteStencil<double>;
260extern template class DiscreteStencil<std::array<double, 3>>;
261extern template class DiscreteStencil<std::vector<double>>;
262
263extern template class MPDiscreteStencil<double>;
264extern template class MPDiscreteStencil<std::array<double, 3>>;
265extern template class MPDiscreteStencil<std::vector<double>>;
266
267}
268#endif
269
270#endif
Metafunction for generating a discretization stencil.
Definition stencil.hpp:59
void sumWeight(std::size_t pos, const weight_t &weight, double factor=1.)
Definition stencil.tpp:428
void sumConstant(const weight_t &constant, double factor=1.)
Definition stencil.tpp:579
weight_t * weightData()
Definition stencil.tpp:380
static const weight_manager_type m_weightManager
Definition stencil.hpp:155
long & getPattern(std::size_t pos)
Definition stencil.tpp:298
void addComplementToZero(long id)
Definition stencil.tpp:684
void setWeight(std::size_t pos, const weight_t &weight)
Definition stencil.tpp:403
void reserve(std::size_t nItems)
Definition stencil.tpp:285
virtual void clearWeights(bool release)
Definition stencil.tpp:787
void resize(std::size_t nItems)
Definition stencil.tpp:270
void optimize(double tolerance=1.e-12)
Definition stencil.tpp:639
weight_t & at(long id)
Definition stencil.tpp:843
void clear(bool release=false)
Definition stencil.tpp:604
DiscreteStencil< weight_t, value_t > & operator-=(const DiscreteStencil< weight_t, value_t > &other)
Definition stencil.tpp:949
weight_t & getWeight(std::size_t pos)
Definition stencil.tpp:357
DiscreteStencil(const weight_t &zero=weight_t())
Definition stencil.tpp:108
void setItem(std::size_t pos, long id, const weight_t &weight)
Definition stencil.tpp:452
weight_t & getConstant()
Definition stencil.tpp:545
void zeroWeight(std::size_t pos)
Definition stencil.tpp:439
std::size_t size() const
Definition stencil.tpp:259
DiscreteStencil< weight_t, value_t > & operator+=(const DiscreteStencil< weight_t, value_t > &other)
Definition stencil.tpp:935
void renumber(const std::unordered_map< long, long > &map)
Definition stencil.tpp:658
void display(std::ostream &out, double factor=1.) const
Definition stencil.tpp:802
size_t getBinarySize() const
Definition stencil.tpp:824
void appendItem(long id, const weight_t &weight)
Definition stencil.tpp:506
DiscreteStencil< weight_t, value_t > & operator/=(double factor)
Definition stencil.tpp:918
virtual void appendWeight(weight_t &&weight)
Definition stencil.tpp:760
void setConstant(const weight_t &constant)
Definition stencil.tpp:556
DiscreteStencil< weight_t, value_t > & operator*=(double factor)
Definition stencil.tpp:901
void sum(const DiscreteStencil< weight_t, value_t > &other, double factor)
Definition stencil.tpp:623
void setPattern(std::size_t pos, long id)
Definition stencil.tpp:345
static const weight_manager_type & getWeightManager()
Definition stencil.tpp:42
void sumItem(long id, const weight_t &weight, double factor=1.)
Definition stencil.tpp:483
void initialize(std::size_t nItems, const weight_t &zero=weight_t())
Definition stencil.tpp:165
weight_t & operator[](long id)
Definition stencil.tpp:882
Output binary stream.
Metafunction for generating a discretization stencil with a memory pool (MP).
Definition stencil.hpp:183
void appendWeight(const weight_t &weight) override
Definition stencil.tpp:1028
void clearWeights(bool release) override
Definition stencil.tpp:1049
void setWeightPool(weight_pool_type *pool)
Definition stencil.tpp:1017
MPDiscreteStencil(const weight_t &zero=weight_t())
Definition stencil.tpp:962
Output binary stream.
T dotProduct(const std::array< T, d > &x, const std::array< T, d > &y)
std::vector< T > operator+(const std::vector< T > &, const std::vector< T > &)
std::ostream & operator<<(std::ostream &, const std::vector< T > &)
--- layout: doxygen_footer ---