Nektar++
Loading...
Searching...
No Matches
HelmholtzOp.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: HelmholtzOp.hpp
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
35/**
36 * @file HelmholtzOp.hpp
37 * @brief Public interface of the weak Helmholtz (Helmholtz) element
38 * operator, which maps a coefficient-space field to coefficient space.
39 *
40 * @details
41 * For every element the operator forms the coefficient-space vector
42 * \f[ H_{pq} = \int_{\Omega^e} \nabla\phi_{pq} \cdot
43 * \left( \mathbf{D}\,\nabla u \right) d\mathbf{x}
44 * + \lambda \int_{\Omega^e} \phi_{pq}\, u \, d\mathbf{x},
45 * \qquad u = \sum_{rs} \hat{u}_{rs} \phi_{rs}, \f]
46 * that is, the weak Laplacian of the Laplacian family plus a
47 * \f$\lambda\f$-weighted mass term, applied matrix-free to the
48 * element's coefficients. \f$\mathbf{D}\f$ is the constant symmetric
49 * diffusion tensor set through SetDiffCoeff() and \f$\lambda\f$ the
50 * scalar set through SetLambda(); both must be set before the first
51 * Apply().
52 *
53 * On a 3D-homogeneous-1D (3DH1) field with more than one homogeneous
54 * plane the block operators apply this plane by plane, lambda term
55 * included, and detail::HelmholtzOpImpl adds the weak z-Laplacian in
56 * v_ApplyFFT(): the xy mass matrix applied to minus the second
57 * z-derivative. The diffusion tensor covers the plane's coordinate
58 * dimension only, so that term has a unit coefficient and no xz/yz
59 * coupling.
60 *
61 * ### Geometric factors and the metric contraction
62 * Both integrals are evaluated on the reference element, so the
63 * geometry enters through the Jacobian \f$J\f$ and the derivative
64 * factors \f$M_{kd} = \partial\xi_d/\partial x_k\f$ of
65 * SpatialDomains::GeomFactors, which convert reference derivatives into
66 * physical ones,
67 * \f$\partial u/\partial x_k = \sum_d M_{kd}\,\partial u/\partial\xi_d\f$.
68 * Substituting that for both \f$u\f$ and \f$\phi_{pq}\f$ in the first
69 * integral turns its integrand into a contraction with the metric
70 * \f[ G_{de} = \sum_{k,l} M_{kd}\, D_{kl}\, M_{le},
71 * \qquad H_{pq} = \sum_{d,e} \int
72 * \frac{\partial\phi_{pq}}{\partial\xi_d}\, G_{de}\,
73 * \frac{\partial u}{\partial\xi_e}\, J \, d\boldsymbol{\xi}
74 * + \lambda \int \phi_{pq}\, u\, J \, d\boldsymbol{\xi}, \f]
75 * the integrals being replaced by quadrature sums. On a block of
76 * regular elements \f$M\f$ and \f$J\f$ are constant per element; on a
77 * deformed block they vary from point to point. Every implementation in
78 * this family is instantiated with a DEFORMED template parameter that
79 * selects between the two.
80 *
81 * ### Where lambda enters and how the two terms are combined
82 * No implementation assembles a Helmholtz matrix: each evaluates the
83 * derivatives of \f$u\f$ at the quadrature points and combines the two
84 * contributions on the way back to coefficient space. The paths differ
85 * both in where those derivatives come from and in where the sum is
86 * taken:
87 * - the StdMat paths obtain the reference derivatives directly from the
88 * coefficients, through one standard-region matrix per direction
89 * (eDerivStdMat), and evaluate \f$u\f$ itself by backward transform
90 * only when \f$\lambda \neq 0\f$. The two terms then stay apart to the
91 * very end: the mass term is the inner product of \f$\lambda J u\f$
92 * with the basis and is written to the output first, the derivative
93 * terms being accumulated onto it by running their inner products
94 * with a GEMM beta of one. When \f$\lambda = 0\f$ the backward
95 * transform and that first inner product are skipped altogether and
96 * the first derivative inner product overwrites the output instead.
97 * - the SumFac paths evaluate \f$u\f$ once by backward transform and
98 * differentiate those physical values. \f$\lambda\f$ enters at the
99 * metric stage: the physical values are scaled in place by
100 * \f$\lambda\, W J\f$ while the derivatives are scaled by
101 * \f$W J G\f$ (\f$W\f$ the quadrature weights), the transposed
102 * derivative operators then sum the derivative terms *onto* that same
103 * quadrature-point array, and one inner product with the basis --
104 * without weights, which have already been applied -- produces the
105 * result. \f$\lambda = 0\f$ takes no special path on any of the
106 * SumFac paths; it simply zeroes the mass contribution.
107 * The mass term is thus always the element-operator equivalent of the
108 * Mass family, and the derivative terms of the Laplacian family.
109 *
110 * ### Relation to the Laplacian family
111 * Setting \f$\lambda = 0\f$ makes this operator the weak Laplacian.
112 * Rather than calling into that family, Helmholtz carries its own
113 * copies of its implementation and kernel headers with the mass term
114 * added.
115 *
116 * The family follows the standard element-operator layout (see
117 * ElmtOp.hpp):
118 * - HelmholtzOp (this file): whole-field interface setting the Apply()
119 * field states, holding lambda and the diffusion tensor, and running
120 * the per-block operators, then the v_ApplyFFT() hook;
121 * - detail::HelmholtzOpImpl (HelmholtzOpImpl.hpp): the
122 * factory-registered whole-field implementation, one entry per
123 * execution space, which builds the weak z-Laplacian and runs it in
124 * v_ApplyFFT();
125 * - HelmholtzBlockOp (HelmholtzBlockOp.hpp): per-block interface;
126 * - detail::HelmholtzBlockOpImpl: the per-block implementations, one
127 * header per implementation family --
128 * HelmholtzBlockOpSerialAVXStdMat.hpp and HelmholtzBlockOpSerialAVXSumFac.hpp
129 * each serve both the Serial and the AVX space,
130 * HelmholtzBlockOpDeviceStdMat.hpp serves Device/StdMat, and
131 * HelmholtzBlockOpDeviceSumFac.hpp serves both device sum-factorisation
132 * strategies (SumFac and SumFacTOP);
133 * - the kernels in HelmholtzBlockOpSerialAVXStdMatKernels.hpp,
134 * HelmholtzBlockOpSerialAVXSumFacKernels.hpp,
135 * HelmholtzBlockOpDeviceStdMatKernels.hpp,
136 * HelmholtzBlockOpDeviceSumFacKernels.hpp and
137 * HelmholtzBlockOpDeviceSumFacTOPKernels.hpp;
138 * - detail::DerivZOpImpl (DerivZOpImpl.hpp) and MassOp: the second
139 * z-derivative and the xy mass matrix forming the weak z-Laplacian of
140 * multi-plane 3DH1 fields.
141 */
142
143#pragma once
144
146
148
149namespace Nektar::MultiRegions
150{
151
152/**
153 * @brief Whole-field interface of the weak Helmholtz operator: maps
154 * coefficient-space fields to coefficient space.
155 *
156 * @details
157 * As with the other element-operator families, this class sets the
158 * Apply() field states at compile time -- input and output in
159 * FieldState::Coeff -- and drives one HelmholtzBlockOp per element block,
160 * followed by v_ApplyFFT(), which detail::HelmholtzOpImpl implements to
161 * add the weak z-Laplacian of a multi-plane 3DH1 field. See ElmtOp for
162 * the block decomposition and factory scheme.
163 *
164 * Apply() always overwrites the output field.
165 *
166 * Two settings specify what the block operators compute: the mass weight
167 * lambda and the packed symmetric diffusion tensor, forwarded to every
168 * block operator by SetLambda() and SetDiffCoeff().
169 *
170 * @tparam TData Floating-point type of the field data.
171 */
172template <typename TData>
173class HelmholtzOp : public ElmtOp<FieldState::Coeff, FieldState::Coeff, TData>
174{
175 friend class ElmtOp<FieldState::Coeff, FieldState::Coeff, TData>;
176
177public:
178 /**
179 * @brief Create a weak-Helmholtz operator through the operator and
180 * block-operator factories.
181 *
182 * Builds the interface object under the factory key
183 * `"Helmholtz" + execStr` and one block operator per element block
184 * under `"BlockHelmholtz" + execStr + implStr` (see ElmtOp::Create
185 * for the two-stage construction and for the session defaults used
186 * when @p execStr / @p implStr are empty).
187 *
188 * @param expansionList Expansion list the operator acts on.
189 * @param components Names of the field components the
190 * operator is set up for.
191 * @param execStr Execution space ("Serial", "AVX" or
192 * "Device"); session default if empty.
193 * @param implStr Block implementation ("StdMat",
194 * "SumFac", "SumFacTOP"); session default
195 * if empty.
196 *
197 * @return The assembled operator. SetLambda() and SetDiffCoeff()
198 * must still be called before Apply().
199 */
200 static std::shared_ptr<HelmholtzOp<TData>> Create(
201 const MultiRegions::ExpListSharedPtr &expansionList,
202 const std::vector<std::string> &components,
203 const std::string &execStr = "", const std::string &implStr = "")
204 {
206 template Create<HelmholtzOp, HelmholtzBlockOp>(
207 expansionList, components, execStr, implStr);
208 }
209
210 /// Operator base name; Create() appends the execution space to it
211 /// to form the factory key.
212 static inline const std::string name = "Helmholtz";
213
214 /**
215 * @brief Set the scalar weight \f$\lambda\f$ of the mass term used
216 * by subsequent Apply() calls.
217 *
218 * The value is copied down to every block operator; calling this
219 * again replaces it. A lambda of zero leaves the plain weak
220 * Laplacian -- the StdMat implementations then skip the mass term's
221 * two matrix multiplications entirely, while the sum-factorised ones
222 * take no special path: they carry the zero through as a scaling of
223 * the physical values.
224 *
225 * @param lambda Weight of the \f$(\phi_{pq}, u)\f$ term.
226 */
227 void SetLambda(TData lambda)
228 {
229 // Loop over the blocks.
230 for (unsigned int blk = 0; blk < this->m_blockOp.size(); ++blk)
231 {
232 this->m_blockOp[blk]->SetLambda(lambda);
233 }
234 m_isSetLambda = true;
235 }
236
237 /**
238 * @brief Set the constant symmetric diffusion tensor \f$\mathbf{D}\f$ used
239 * by subsequent Apply() calls.
240 *
241 * The tensor is passed in packed upper-triangular form, one entry
242 * per independent component, so @p diffCoeff holds
243 * `ncoord * (ncoord + 1) / 2` values for a coordinate dimension of
244 * `ncoord`: `{D00}` in 1D, `{D00, D01, D11}` in 2D and
245 * `{D00, D01, D11, D02, D12, D22}` in 3D -- the order the kernels
246 * index through LibUtilities::GetDiffCoeffMap() and the one the
247 * factors map onto. The values are copied down to every block
248 * operator; calling this again replaces them.
249 *
250 * @param diffCoeff Packed tensor entries, in the order above.
251 */
252 void SetDiffCoeff(std::vector<TData> &diffCoeff)
253 {
254 // Loop over the blocks.
255 for (unsigned int blk = 0; blk < this->m_blockOp.size(); ++blk)
256 {
257 this->m_blockOp[blk]->SetDiffCoeff(diffCoeff);
258 }
259 m_isSetDiffCoeff = true;
260 m_isSetVarDiffCoeff = false;
261 }
262
263 /**
264 * @brief Set a spatially varying symmetric diffusion tensor.
265 *
266 * Physical-space components use the same packed upper-triangular order
267 * as the constant overload: D00, D01, D11, D02, D12, D22. The field is
268 * borrowed and must outlive Apply(); changes to its values are visible
269 * on the next application. Implementations may change its interleaving.
270 * For homogeneous expansions these are the plane tensor components;
271 * the existing unit-coefficient homogeneous diffusion is retained.
272 */
275 {
276 ASSERTL0(diffCoeff.GetBlocks().size() == this->m_blockOp.size(),
277 "Diffusion coefficient blocks must match the expansion");
278 for (unsigned int blk = 0; blk < this->m_blockOp.size(); ++blk)
279 {
280 this->m_blockOp[blk]->SetVarDiffCoeff(diffCoeff.GetBlocks()[blk]);
281 }
282
283 m_isSetDiffCoeff = false;
284 m_isSetVarDiffCoeff = true;
285 }
286
287protected:
288 /// Set by SetLambda(); checked by v_Apply() in debug builds.
289 bool m_isSetLambda = false;
290 /// Set by SetDiffCoeff(); checked by v_Apply() in debug builds.
291 bool m_isSetDiffCoeff = false;
292 /// Whether a spatially varying tensor is selected.
294
295 /// One block operator per element block of the expansion list;
296 /// populated by ElmtOp::Create().
297 std::vector<std::shared_ptr<HelmholtzBlockOp<TData>>> m_blockOp;
298
299 /**
300 * @brief Construct the interface part of a concrete implementation;
301 * called by the factory-registered creator functions.
302 *
303 * @param expansionList Expansion list the operator acts on.
304 * @param components Component names the operator is set up
305 * for.
306 */
308 const std::vector<std::string> &components)
309 : ElmtOp<FieldState::Coeff, FieldState::Coeff, TData>(expansionList,
310 components)
311 {
312 }
313
314 ~HelmholtzOp() override = default;
315
316 /**
317 * @brief Apply the weak Helmholtz operator block by block.
318 *
319 * Checks that @p in and @p out conform (equal component and
320 * homogeneous-mode counts) and that lambda and the diffusion tensor
321 * have been set, then hands each pair of blocks to the
322 * corresponding entry of #m_blockOp. It then calls v_ApplyFFT(),
323 * which for a field with more than one homogeneous mode adds the
324 * weak z-Laplacian to @p out.
325 *
326 * @param in Coefficient-space input field.
327 * @param out Coefficient-space output field; overwritten.
328 */
331 {
332 ASSERTL1(in.GetNumComponents() == out.GetNumComponents(),
333 "Number of input and output components differ");
334
335 ASSERTL1(in.GetNumHomoModes() == out.GetNumHomoModes(),
336 "Number of input and output homogeneous modes differ");
337
339 "lambda has not been set."
340 "Set the value with SetLambda() before calling Apply().");
341
343 "diffusion coefficient has not been set. "
344 "Set the value with SetDiffCoeff() or SetVarDiffCoeff() "
345 "before calling Apply().");
346
348 "can't set both DiffCoeff and VarDiffCoeff. "
349 "Set the value with SetDiffCoeff() or SetVarDiffCoeff() "
350 "before calling Apply().");
351
352 // Loop over the blocks.
353 for (unsigned int blk = 0; blk < this->m_blockOp.size(); ++blk)
354 {
355 // Block dependent.
356 auto &inblock = in.GetBlocks()[blk];
357 auto &outblock = out.GetBlocks()[blk];
358
359 this->m_blockOp[blk]->Apply(inblock, outblock);
360 }
361
362 // Apply FFT.
363 v_ApplyFFT(in, out);
364 }
365
366 /// Weak z-Laplacian, called by v_Apply() after the block loop; adds
367 /// it to every output block of a multi-plane 3DH1 field and leaves
368 /// any other field untouched.
369 virtual void v_ApplyFFT(
372};
373
374} // namespace Nektar::MultiRegions
Whole-field base class and factory machinery shared by every element-operator family in the MultiRegi...
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Per-block interface class of the weak Helmholtz operator.
A Field represents expansion data to be operated on.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
Common base class of the element-operator interfaces: an operator applied independently to every elem...
Definition ElmtOp.hpp:111
Whole-field interface of the weak Helmholtz operator: maps coefficient-space fields to coefficient sp...
bool m_isSetVarDiffCoeff
Whether a spatially varying tensor is selected.
bool m_isSetLambda
Set by SetLambda(); checked by v_Apply() in debug builds.
void SetLambda(TData lambda)
Set the scalar weight of the mass term used by subsequent Apply() calls.
void SetDiffCoeff(std::vector< TData > &diffCoeff)
Set the constant symmetric diffusion tensor used by subsequent Apply() calls.
static std::shared_ptr< HelmholtzOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Create a weak-Helmholtz operator through the operator and block-operator factories.
HelmholtzOp(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components)
Construct the interface part of a concrete implementation; called by the factory-registered creator f...
static const std::string name
Operator base name; Create() appends the execution space to it to form the factory key.
void SetVarDiffCoeff(LibUtilities::Field< TData, FieldState::Phys > &diffCoeff)
Set a spatially varying symmetric diffusion tensor.
virtual void v_ApplyFFT(LibUtilities::Field< TData, FieldState::Coeff > &in, LibUtilities::Field< TData, FieldState::Coeff > &out)=0
Weak z-Laplacian, called by v_Apply() after the block loop; adds it to every output block of a multi-...
void v_Apply(LibUtilities::Field< TData, FieldState::Coeff > &in, LibUtilities::Field< TData, FieldState::Coeff > &out) override
Apply the weak Helmholtz operator block by block.
bool m_isSetDiffCoeff
Set by SetDiffCoeff(); checked by v_Apply() in debug builds.
std::vector< std::shared_ptr< HelmholtzBlockOp< TData > > > m_blockOp
One block operator per element block of the expansion list; populated by ElmtOp::Create().
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
FieldState
Possible states for Field data.