Nektar++
Loading...
Searching...
No Matches
ExpListHelper.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ExpListHelper.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
38
41
42using namespace Nektar::LibUtilities;
43
45{
46
47// Helper function
49 MultiRegions::ExpListSharedPtr expansionList, unsigned int block_idx)
50{
52 auto explistHomo1D =
53 std::dynamic_pointer_cast<MultiRegions::ExpListHomogeneous1D>(
54 expansionList);
55 auto explistHomo2D =
56 std::dynamic_pointer_cast<MultiRegions::ExpListHomogeneous2D>(
57 expansionList);
58 if (explistHomo1D)
59 {
60 tmp = explistHomo1D->GetPlane(0);
61 }
62 else if (explistHomo2D)
63 {
64 tmp = explistHomo2D->GetLine(0);
65 }
66 else
67 {
68 tmp = expansionList;
69 }
70
71 return tmp->GetCollections()[block_idx];
72}
73
76{
78 auto explistHomo1D =
79 std::dynamic_pointer_cast<MultiRegions::ExpListHomogeneous1D>(
80 expansionList);
81 auto explistHomo2D =
82 std::dynamic_pointer_cast<MultiRegions::ExpListHomogeneous2D>(
83 expansionList);
84 if (explistHomo1D)
85 {
86 return explistHomo1D->GetPlane(0)->GetCollections();
87 }
88 else if (explistHomo2D)
89 {
90 return explistHomo2D->GetLine(0)->GetCollections();
91 }
92 else
93 {
94 return expansionList->GetCollections();
95 }
96}
97
98/**
99 * @brief Get the BlockAttributes for a given field state from an ExpList.
100 * This method basically captures identical elements that are contiguously
101 * stored in the ExpList and group them into blocks. Padding elements will
102 * also be set based on given vector width.
103 *
104 * Without @p interleave_widths each block takes the default width of
105 * Operator::GetDefaultInterleaveWidths(), the width its operators work at
106 * by default, so that a Field built on it is not reshaped on every Apply.
107 * Give the widths only where a Field must be at others.
108 *
109 * @param explist Expansion list to query.
110 * @param interleave_widths Interleave width of each block, in block order;
111 * empty for the defaults.
112 * @return std::vector<BlockAttributes>
113 */
114template <typename TPadding, FieldState TState>
115std::vector<BlockAttributes<TState>> GetBlockAttributes(
116 const MultiRegions::ExpListSharedPtr explist,
117 const std::vector<unsigned int> &interleave_widths)
118{
119 // The TPadding type is use to dertermine the padding requirement and must
120 // be of floating point type.
121 static_assert(std::is_floating_point_v<TPadding>,
122 "GetBlockAttributes: Data type must be float or double.");
123
124 // Use maximum vector width for back-ends interoperability.
125 const auto vector_width = NektarSpaces::max_vector_width<TPadding>::value;
126
127 const std::vector<unsigned int> widths =
128 interleave_widths.empty()
130 : interleave_widths;
131
132 auto colls = GetCollections(explist);
133 ASSERTL0(widths.size() == colls.size(),
134 "GetBlockAttributes: " + std::to_string(widths.size()) +
135 " interleave widths given for " +
136 std::to_string(colls.size()) + " blocks.");
137
138 std::vector<BlockAttributes<TState>> blockAttr;
139 for (size_t blk = 0; blk < colls.size(); ++blk)
140 {
141 auto expPtr = colls[blk].GetExpVector()[0];
142 const size_t num_elements = colls[blk].GetExpVector().size();
143 const unsigned int ndata = TState == FieldState::Phys
144 ? expPtr->GetTotPoints()
145 : expPtr->GetNcoeffs();
146 size_t num_elements_with_padding =
147 ((num_elements + vector_width - 1) / vector_width) * vector_width;
148 blockAttr.push_back(
149 {num_elements, num_elements_with_padding, ndata, widths[blk]});
150 }
151
152 return blockAttr;
153}
154
155template std::vector<BlockAttributes<FieldState::Phys>> GetBlockAttributes<
156 double, FieldState::Phys>(
157 const MultiRegions::ExpListSharedPtr explist,
158 const std::vector<unsigned int> &interleave_widths);
159template std::vector<BlockAttributes<FieldState::Coeff>> GetBlockAttributes<
160 double, FieldState::Coeff>(
161 const MultiRegions::ExpListSharedPtr explist,
162 const std::vector<unsigned int> &interleave_widths);
163template std::vector<BlockAttributes<FieldState::Phys>> GetBlockAttributes<
164 float, FieldState::Phys>(
165 const MultiRegions::ExpListSharedPtr explist,
166 const std::vector<unsigned int> &interleave_widths);
167template std::vector<BlockAttributes<FieldState::Coeff>> GetBlockAttributes<
168 float, FieldState::Coeff>(
169 const MultiRegions::ExpListSharedPtr explist,
170 const std::vector<unsigned int> &interleave_widths);
171
172template <typename TPadding, FieldState TState>
173std::vector<BlockAttributes<TState>> GetLocTraceBlockAttributes(
174 const MultiRegions::ExpListSharedPtr explist,
175 const unsigned interleave_width, const bool useGLLTracePoints)
176{
177 // The TPadding type is use to dertermine the padding requirement and must
178 // be of floating point type.
179 static_assert(std::is_floating_point_v<TPadding>,
180 "GetBlockAttributes: Data type must be float or double.");
181
182 // Use maximum vector width for back-ends interoperability.
183 const auto vector_width = NektarSpaces::max_vector_width<TPadding>::value;
184
185 std::vector<BlockAttributes<TState>> blockAttr;
186
187 auto colls = GetCollections(explist);
188 for (auto &coll : colls)
189 {
190 auto expPtr = coll.GetExpVector()[0];
191 const size_t num_elements = coll.GetExpVector().size();
192 auto shapetype = expPtr->DetShapeType();
193 unsigned dimension = expPtr->GetNumBases();
194 unsigned ndatatot = 0;
195 for (unsigned d = 0; d < dimension; ++d)
196 {
197 unsigned ndata = 1;
198 for (unsigned d1 = 0, cnt = 0; d1 < dimension; ++d1)
199 {
200 if (d1 == d)
201 {
202 continue;
203 }
204 ndata *= expPtr
205 ->GetTraceBasisKey(dimension - 1 - d, cnt++,
206 useGLLTracePoints)
207 .GetNumPoints();
208 }
209 ndatatot +=
210 ndata * LibUtilities::ShapeTypeNumTraceInDir[shapetype][d];
211 }
212
213 size_t num_elements_with_padding =
214 ((num_elements + vector_width - 1) / vector_width) * vector_width;
215 blockAttr.push_back({num_elements, num_elements_with_padding, ndatatot,
216 interleave_width});
217 }
218
219 return blockAttr;
220}
221
222template std::vector<BlockAttributes<FieldState::Phys>> GetLocTraceBlockAttributes<
224 const unsigned interleave_width,
225 const bool useGLLTracePoints);
226template std::vector<BlockAttributes<FieldState::Phys>> GetLocTraceBlockAttributes<
228 const unsigned interleave_width,
229 const bool useGLLTracePoints);
230
231/**
232 * @brief As GetBlockAttributes(), but on the @p scale over-integrated grid.
233 *
234 * The point counts are the ones the 1D interpolation and Galerkin projection
235 * matrices are built for: direction 0 scales outright, and a direction
236 * carrying one point fewer than direction 0 keeps that offset so the
237 * collapsed coordinate of a triangle or tetrahedron lines up.
238 *
239 * @param explist Expansion list to size the blocks from.
240 * @param scale Over-integration factor, 1.5 for the 3/2 rule.
241 * @param interleave_width Vector width to use for the field.
242 * @return std::vector<BlockAttributes>
243 */
244template <typename TPadding, FieldState TState>
245std::vector<BlockAttributes<TState>> GetScaledBlockAttributes(
246 const MultiRegions::ExpListSharedPtr explist, const NekDouble scale,
247 const unsigned interleave_width)
248{
249 // The TPadding type is use to dertermine the padding requirement and must
250 // be of floating point type.
251 static_assert(std::is_floating_point_v<TPadding>,
252 "GetScaledBlockAttributes: Data type must be float or "
253 "double.");
254
255 static_assert(TState == FieldState::Phys,
256 "GetScaledBlockAttributes: only a quadrature grid is "
257 "over-integrated.");
258
259 // Use maximum vector width for back-ends interoperability.
260 const auto vector_width = NektarSpaces::max_vector_width<TPadding>::value;
261
262 std::vector<BlockAttributes<TState>> blockAttr;
263
264 auto colls = GetCollections(explist);
265 for (auto &coll : colls)
266 {
267 auto expPtr = coll.GetExpVector()[0];
268 const size_t num_elements = coll.GetExpVector().size();
269
270 const unsigned int npts0 = expPtr->GetNumPoints(0);
271 unsigned int ndata = 1;
272 for (unsigned int d = 0; d < expPtr->GetNumBases(); ++d)
273 {
274 const unsigned int npts = expPtr->GetNumPoints(d);
275 ndata *= (d != 0 && npts0 - npts == 1)
276 ? static_cast<unsigned int>(scale * npts0) - 1
277 : static_cast<unsigned int>(scale * npts);
278 }
279
280 size_t num_elements_with_padding =
281 ((num_elements + vector_width - 1) / vector_width) * vector_width;
282 blockAttr.push_back(
283 {num_elements, num_elements_with_padding, ndata, interleave_width});
284 }
285
286 return blockAttr;
287}
288
289template std::vector<BlockAttributes<FieldState::Phys>> GetScaledBlockAttributes<
291 const NekDouble scale,
292 const unsigned interleave_width);
293template std::vector<BlockAttributes<FieldState::Phys>> GetScaledBlockAttributes<
295 const NekDouble scale,
296 const unsigned interleave_width);
297
298} // namespace Nektar::MultiRegions
#define ASSERTL0(condition, msg)
Whole-field operator base class and factory machinery shared by every operator family in the MultiReg...
static std::vector< unsigned int > GetDefaultInterleaveWidths(const MultiRegions::ExpListSharedPtr &expansionList)
Return the default interleave widths, one per block of expansionList, that persistent Fields (e....
Definition Operator.hpp:288
std::vector< Collection > CollectionVector
Definition Collection.h:142
constexpr unsigned int ShapeTypeNumTraceInDir[SIZE_ShapeType][3]
Collections::CollectionVector GetCollections(MultiRegions::ExpListSharedPtr expansionList)
std::vector< BlockAttributes< TState > > GetLocTraceBlockAttributes(const MultiRegions::ExpListSharedPtr explist, const unsigned interleave_width, const bool useGLLTracePoints)
std::vector< BlockAttributes< TState > > GetScaledBlockAttributes(const MultiRegions::ExpListSharedPtr explist, const NekDouble scale, const unsigned interleave_width)
As GetBlockAttributes(), but on the scale over-integrated grid.
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
Collections::Collection GetCollection(MultiRegions::ExpListSharedPtr expansionList, unsigned int block_idx)
std::vector< BlockAttributes< TState > > GetBlockAttributes(const MultiRegions::ExpListSharedPtr explist, const std::vector< unsigned int > &interleave_widths)
Get the BlockAttributes for a given field state from an ExpList. This method basically captures ident...