Loading...
Searching...
No Matches
stencil_solver.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 __BITPIT_STENCIL_SOLVER_HPP__
26#define __BITPIT_STENCIL_SOLVER_HPP__
27
28#if BITPIT_ENABLE_MPI==1
29#include <mpi.h>
30#endif
31#include <algorithm>
32#include <vector>
33
34#include "bitpit_LA.hpp"
35
36#include "stencil.hpp"
37
38namespace bitpit {
39
40template<typename stencil_t>
42
43public:
45
46 virtual std::size_t size() const = 0;
47
48 virtual const stencil_t & at(long rowIndex) const = 0;
49 virtual const stencil_t & rawAt(std::size_t rowRawIndex) const = 0;
50
51 virtual const stencil_t & at(long blockIndex, int componentIdx) const = 0;
52 virtual const stencil_t & rawAt(std::size_t blockRawIndex, int componentIdx) const = 0;
53
54protected:
56
57};
58
59template<typename stencil_t, typename stencil_container_t>
61
62protected:
63 const stencil_container_t *m_stencils;
64 int m_stride;
65
66 DiscretizationStencilProxyBaseStorage(const stencil_container_t *stencils, int stride);
67
68};
69
70template<typename stencil_t, typename stencil_container_t>
71class DiscretizationStencilProxyStorage : public DiscretizationStencilProxyBaseStorage<stencil_t, stencil_container_t> {
72
73public:
74 DiscretizationStencilProxyStorage(const stencil_container_t *stencils);
75
76 std::size_t size() const override;
77
78 const stencil_t & at(long rowIndex) const override;
79 const stencil_t & rawAt(std::size_t rowRawIndex) const override;
80
81 const stencil_t & at(long blockIndex, int componentIdx) const override;
82 const stencil_t & rawAt(std::size_t blockRawIndex, int componentIdx) const override;
83
84};
85
86template<typename stencil_t>
87class DiscretizationStencilProxyStorage<stencil_t, PiercedStorage<stencil_t>> : public DiscretizationStencilProxyBaseStorage<stencil_t, PiercedStorage<stencil_t>> {
88
89public:
91
92 std::size_t size() const override;
93
94 const stencil_t & at(long rowIndex) const override;
95 const stencil_t & rawAt(std::size_t rowRawIndex) const override;
96
97 const stencil_t & at(long blockIndex, int componentIdx) const override;
98 const stencil_t & rawAt(std::size_t blockRawIndex, int componentIdx) const override;
99
100};
101
102template<typename stencil_t, typename solver_kernel_t = SystemSolver>
103class DiscretizationStencilSolverAssembler : public solver_kernel_t::Assembler {
104
105public:
106 using stencil_type = stencil_t;
107
108 using solver_kernel_type = solver_kernel_t;
109 using assembly_type = typename solver_kernel_t::Assembler;
110 using assembly_options_type = typename assembly_type::AssemblyOptions;
111
112 template<typename stencil_container_t, typename... AssemblerKernelArgs>
113 DiscretizationStencilSolverAssembler(const stencil_container_t *stencils, AssemblerKernelArgs&&... assemblerKernelArgs);
114#if BITPIT_ENABLE_MPI==1
115 template<typename stencil_container_t, typename... AssemblerKernelArgs>
116 DiscretizationStencilSolverAssembler(MPI_Comm communicator, bool partitioned, const stencil_container_t *stencils,
117 AssemblerKernelArgs&&... assemblerKernelArgs);
118#endif
119
120#if BITPIT_ENABLE_MPI==1
121 bool isPartitioned() const override;
122 const MPI_Comm & getCommunicator() const override;
123#endif
124
125 assembly_options_type getOptions() const override;
126
127 int getBlockSize() const override;
128
129 long getRowCount() const override;
130 long getColCount() const override;
131
132 long getRowElementCount() const override;
133 long getColElementCount() const override;
134
135#if BITPIT_ENABLE_MPI==1
136 long getRowGlobalCount() const override;
137 long getColGlobalCount() const override;
138
139 long getRowGlobalElementCount() const override;
140 long getColGlobalElementCount() const override;
141
142 long getRowGlobalOffset() const override;
143 long getColGlobalOffset() const override;
144
145 long getRowGlobalElementOffset() const override;
146 long getColGlobalElementOffset() const override;
147#endif
148
149 long getRowNZCount(long rowIndex) const override;
150 long getMaxRowNZCount() const override;
151
152 void getRowPattern(long rowIndex, ConstProxyVector<long> *pattern) const override;
153 void getRowValues(long rowIndex, ConstProxyVector<double> *values) const override;
154 void getRowData(long rowIndex, ConstProxyVector<long> *pattern, ConstProxyVector<double> *values) const override;
155
156 virtual void getRowConstant(long rowIndex, bitpit::ConstProxyVector<double> *constant) const;
157
158protected:
159 using stencil_weight_type = typename stencil_type::weight_type;
160 using stencil_value_type = typename stencil_type::value_type;
161
162 long m_nRows;
163 long m_nCols;
164
165#if BITPIT_ENABLE_MPI==1
166 long m_nGlobalRows;
167 long m_nGlobalCols;
168
169 long m_globalRowOffset;
170 long m_globalColOffset;
171#endif
172
173 std::unique_ptr<DiscretizationStencilStorageInterface<stencil_t>> m_stencils;
174
175 int m_blockSize;
176
177 long m_maxRowNZ;
178
179 template<typename... AssemblerKernelArgs>
181 AssemblerKernelArgs&&... assemblerKernelArgs);
182 template<typename... AssemblerKernelArgs>
183 DiscretizationStencilSolverAssembler(AssemblerKernelArgs&&... assemblerKernelArgs);
184#if BITPIT_ENABLE_MPI==1
185 template<typename... AssemblerKernelArgs>
186 DiscretizationStencilSolverAssembler(MPI_Comm communicator, bool partitioned,
187 std::unique_ptr<DiscretizationStencilStorageInterface<stencil_t>> &&stencils,
188 AssemblerKernelArgs&&... assemblerKernelArgs);
189 template<typename... AssemblerKernelArgs>
190 DiscretizationStencilSolverAssembler(MPI_Comm communicator, bool partitioned,
191 AssemblerKernelArgs&&... assemblerKernelArgs);
192#endif
193
194 void setStencils(std::unique_ptr<DiscretizationStencilStorageInterface<stencil_t>> &&stencils);
195
196 void setMatrixSizes();
197 void setMatrixSizes(long nRows, long nCols);
198
199 template<typename W = stencil_weight_type, typename V = stencil_value_type, typename std::enable_if<std::is_fundamental<W>::value>::type * = nullptr>
200 void setBlockSize();
201 template<typename W = stencil_weight_type, typename V = stencil_value_type, std::size_t D = std::tuple_size<W>::value, typename std::enable_if<std::is_same<std::array<V, D>, W>::value>::type * = nullptr>
202 void setBlockSize();
203 template<typename W = stencil_weight_type, typename V = stencil_value_type, typename std::enable_if<std::is_same<std::vector<V>, W>::value>::type * = nullptr>
204 void setBlockSize();
205 void setBlockSize(int blockSize);
206
207 void setMaximumRowNZ();
208 void setMaximumRowNZ(long maxRowNZ);
209
210 virtual const stencil_t & getRowStencil(long rowIndex) const;
211
212 void getPattern(const stencil_t &stencil, ConstProxyVector<long> *pattern) const;
213
214 template<typename W = stencil_weight_type, typename std::enable_if<std::is_fundamental<W>::value>::type * = nullptr>
215 void getValues(const stencil_t &stencil, ConstProxyVector<double> *values) const;
216
217 template<typename W = stencil_weight_type, typename std::enable_if<!std::is_fundamental<W>::value>::type * = nullptr>
218 void getValues(const stencil_t &stencil, ConstProxyVector<double> *values) const;
219
220 template<typename W = stencil_weight_type, typename std::enable_if<std::is_fundamental<W>::value>::type * = nullptr>
221 void getConstant(const stencil_t &stencil, bitpit::ConstProxyVector<double> *constant) const;
222
223 template<typename W = stencil_weight_type, typename std::enable_if<!std::is_fundamental<W>::value>::type * = nullptr>
224 void getConstant(const stencil_t &stencil, bitpit::ConstProxyVector<double> *constant) const;
225
226private:
227#if BITPIT_ENABLE_MPI==1
228 bool m_partitioned;
229 MPI_Comm m_communicator;
230#endif
231
232};
233
234template<typename stencil_t, typename solver_kernel_t = SystemSolver>
235class DiscretizationStencilSolver : public solver_kernel_t {
236
237public:
239
240 using solver_kernel_t::solver_kernel_t;
241
242 void clear() override;
243
244 template<typename stencil_container_t = std::vector<stencil_t>>
245 void assembly(const stencil_container_t &stencils);
246#if BITPIT_ENABLE_MPI==1
247 template<typename stencil_container_t = std::vector<stencil_t>>
248 void assembly(MPI_Comm communicator, bool partitioned, const stencil_container_t &stencils);
249#endif
250 void assembly(const Assembler &assembler);
251
252 template<typename stencil_container_t = std::vector<stencil_t>>
253 void update(const stencil_container_t &stencils);
254 template<typename stencil_container_t = std::vector<stencil_t>>
255 void update(const std::vector<long> &rows, const stencil_container_t &stencils);
256 template<typename stencil_container_t = std::vector<stencil_t>>
257 void update(std::size_t nRows, const long *rows, const stencil_container_t &stencils);
258 void update(long nRows, const long *rows, const Assembler &assembler);
259
260 void solve();
261
262 void matrixAssembly(const Assembler &assembler);
263 void matrixUpdate(long nRows, const long *rows, const Assembler &assembler);
264
265protected:
266 std::vector<double> m_constants;
267
268 using solver_kernel_t::assembly;
269 using solver_kernel_t::update;
270
271 void assembleConstants(const Assembler &assembler);
272 void updateConstants(std::size_t nRows, const long *rows, const Assembler &assembler);
273
274};
275
276}
277
278// Template implementation
279#include "stencil_solver.tpp"
280
281// Declaration of the typdefs
282namespace bitpit {
283
284typedef DiscretizationStencilSolverAssembler<StencilScalar> StencilScalarSolverAssembler;
285typedef DiscretizationStencilSolverAssembler<StencilVector> StencilVectorSolverAssembler;
286typedef DiscretizationStencilSolverAssembler<StencilBlock> StencilBlockSolverAssembler;
287
288typedef DiscretizationStencilSolver<StencilScalar> StencilScalarSolver;
289typedef DiscretizationStencilSolver<StencilVector> StencilVectorSolver;
290typedef DiscretizationStencilSolver<StencilBlock> StencilBlockSolver;
291
292}
293
294// Explicit instantization
295#ifndef __BITPIT_STENCIL_SOLVER_SRC__
296namespace bitpit {
297
298extern template class DiscretizationStencilSolverAssembler<StencilScalar>;
299extern template class DiscretizationStencilSolverAssembler<StencilVector>;
300extern template class DiscretizationStencilSolverAssembler<StencilBlock>;
301
302extern template class DiscretizationStencilSolver<StencilScalar>;
303extern template class DiscretizationStencilSolver<StencilVector>;
304extern template class DiscretizationStencilSolver<StencilBlock>;
305
306}
307#endif
308
309#endif
The DiscretizationStencilProxyBaseStorage class defines a proxy for stencil storage.
DiscretizationStencilProxyBaseStorage(const stencil_container_t *stencils, int stride)
const stencil_t & rawAt(std::size_t rowRawIndex) const override
const stencil_t & at(long rowIndex) const override
DiscretizationStencilProxyStorage(const stencil_container_t *stencils)
The DiscretizationStencilSolverAssembler class defines an assembler for building the stencil solver.
long getRowNZCount(long rowIndex) const override
virtual void getRowConstant(long rowIndex, bitpit::ConstProxyVector< double > *constant) const
void getRowValues(long rowIndex, ConstProxyVector< double > *values) const override
void getRowPattern(long rowIndex, ConstProxyVector< long > *pattern) const override
void setStencils(std::unique_ptr< DiscretizationStencilStorageInterface< stencil_t > > &&stencils)
DiscretizationStencilSolverAssembler(const stencil_container_t *stencils, AssemblerKernelArgs &&... assemblerKernelArgs)
void getValues(const stencil_t &stencil, ConstProxyVector< double > *values) const
void getRowData(long rowIndex, ConstProxyVector< long > *pattern, ConstProxyVector< double > *values) const override
const MPI_Comm & getCommunicator() const override
void getPattern(const stencil_t &stencil, ConstProxyVector< long > *pattern) const
assembly_options_type getOptions() const override
virtual const stencil_t & getRowStencil(long rowIndex) const
void getConstant(const stencil_t &stencil, bitpit::ConstProxyVector< double > *constant) const
void matrixAssembly(const Assembler &assembler)
void assembleConstants(const Assembler &assembler)
void matrixUpdate(long nRows, const long *rows, const Assembler &assembler)
void assembly(const stencil_container_t &stencils)
void update(const stencil_container_t &stencils)
void updateConstants(std::size_t nRows, const long *rows, const Assembler &assembler)
The DiscretizationStencilStorageInterface class defines the interface for stencil storage.
Metafunction for generating a pierced storage.
Metafunction for generating a list of elements that can be either stored in an external vectror or,...
--- layout: doxygen_footer ---