Nektar++
Loading...
Searching...
No Matches
TestDiffusionIP.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestDiffusionIP.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: Fixture for the interior penalty diffusion operator, comparing
32// it against the legacy SolverUtils InteriorPenalty diffusion.
33//
34///////////////////////////////////////////////////////////////////////////////
35
37#include <SolverCore/Diffusion/DiffusionIP/DiffusionIPOp.hpp>
38#include <SolverCore/TraceFlux/TraceFluxOp.hpp>
39
42
44
45using namespace Nektar;
46using namespace Nektar::LibUtilities;
47using namespace Nektar::MultiRegions;
48using namespace Nektar::SolverCore;
49
50/**
51 * @brief Interior penalty diffusion, redesign against legacy.
52 *
53 * Mirrors the AdvectionWeakDG fixture. The value of having it is that
54 * diffusion previously had no operator-level test at all: its only cover was
55 * the solver regression cases, which report an L2 error against an exact
56 * solution and so say only that something is wrong, not where. This compares
57 * the operator's output pointwise against the legacy operator applied to the
58 * same field, which is what makes a discrepancy locatable.
59 */
60template <typename TData>
61class TestDiffusionIP : public TestOp<TData, FieldState::Phys, FieldState::Phys,
62 MultiRegions::DisContField>
63{
64public:
65 TestDiffusionIP() = default;
66
68 {
69 // Set initial conditions from the quadrature point coordinates, which
70 // do not move when the mesh is partitioned, so serial and parallel
71 // runs start from the same field. Quadratic, so the second derivative
72 // the operator takes is not identically zero.
73 for (unsigned blk = 0, eid = 0; blk < this->fixt_in->GetBlocks().size();
74 ++blk)
75 {
76 auto &block = this->fixt_in->GetBlocks()[blk];
77 auto inptr =
78 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
79
80 const auto ndim = this->fixt_explist->GetCoordim(0);
81
82 for (unsigned n = 0; n < this->fixt_in->GetNumComponents() *
83 this->fixt_in->GetNumHomoModes();
84 ++n)
85 {
86 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
87 {
88 auto coords =
89 this->fixt_explist->GetExp(eid + el)->GetCoords();
90
91 for (unsigned phys = 0; phys < block.GetNumData();
92 ++phys, ++cnt)
93 {
94 TData v = 1.0 + TData(n);
95 for (unsigned d = 0; d < ndim; ++d)
96 {
97 const TData x = coords[d][phys];
98 v += TData(d + 1) * x + 0.5 * x * x;
99 }
100 inptr[cnt] = v;
101 }
102 }
103 inptr += block.CompSize();
104 }
105
106 eid += block.GetNumElements();
107 }
108
109 // Isotropic unit diffusivity, which is what the legacy flux vector
110 // below applies too.
111 const unsigned nDim = this->fixt_explist->GetCoordim(0);
112 m_diffCoeff.assign(nDim * (nDim + 1) / 2, TData(0.0));
113 for (unsigned d = 0; d < nDim; ++d)
114 {
115 m_diffCoeff[d * (d + 3) / 2] = TData(1.0);
116 }
117
119 }
120
122 {
123 // Legacy DiffusionIP loads IPPenaltyCoeff and never applies it - its
124 // production penalty is p^2 times the reciprocal length and nothing
125 // else - where the redesign multiplies the coefficient in. The
126 // redesign's default is 1 precisely so the two coincide, but this
127 // test should not depend on a default staying put: pin it, so the
128 // comparison means the same operator whatever the defaults become.
129 TData one = 1.0;
130 this->session->SetParameter("IPPenaltyCoeff", one);
131
132 this->fixt_explist->SetDataWarehouse();
133 this->fixt_explist->GetTrace()->SetDataWarehouse();
134
135 auto DiffusionOp = DiffusionIPOp<TData>::Create(
136 this->fixt_explist, this->session->GetVariables());
137
139 this->fixt_explist, this->session->GetVariables());
140
142 this->fixt_explist, this->session->GetVariables(),
143 "DiffusionScalarIPTraceFlux");
144
145 volFluxOp->SetDiffCoeff(m_diffCoeff);
146 traceFluxOp->SetDiffCoeff(m_diffCoeff);
147
148 DiffusionOp->SetVolumeFluxOp(volFluxOp);
149 DiffusionOp->SetTraceFluxOp(traceFluxOp);
150
151 DiffusionOp->Apply(*this->fixt_in, *this->fixt_out);
152 }
153
155 {
156 unsigned numComp = this->fixt_in->GetNumComponents();
157 size_t nphys = this->fixt_explist->GetTotPoints();
159 "InteriorPenalty", "InteriorPenalty");
160 // DiffusionIP does not use m_fluxVector at all. It calls three
161 // separate functors, and leaving any of them unset is a
162 // bad_function_call at apply time rather than at setup. The third is
163 // the symmetric flux, which is where the symmetrising terms hook in.
164 diffObject->SetDiffusionFluxCons(&TestDiffusionIP::GetFluxCons, this);
165 diffObject->SetDiffusionFluxConsTrace(
167 diffObject->SetDiffusionSymmFluxCons(&TestDiffusionIP::GetSymmFluxCons,
168 this);
169 diffObject->SetSpecialBndTreat(&TestDiffusionIP::SpecialBndTreat, this);
170
171 // Scalars, not conservative variables; identical to what
172 // UnsteadyDiffusion now does, so this fixture and the legacy solver
173 // agree on what the reference semantics are.
174 std::static_pointer_cast<SolverUtils::DiffusionIP>(diffObject)
175 ->SetScalarAveraging(true);
176
178 numComp);
179
180 std::vector<std::string> variables = this->session->GetVariables();
182 ASSERTL0(variables.size() >= numComp,
183 "numComp is larger than the number of variables");
184
185 for (unsigned i = 0; i < numComp; ++i)
186 {
187 // in case of orthogonal expansions reset expansion info for legacy
188 // code to work
189 if (!this->fixt_explist->GetExp(0)->IsBoundaryInteriorExpansion())
190 {
191 graph->ResetExpansionInfoToModified(variables[i]);
192 }
193 exp[i] =
195 this->session, graph, variables[i], true, true,
197 }
198
199 diffObject->InitObject(this->session, exp);
200
201 Array<OneD, TData> in = this->fixt_in->ToArray();
202 Array<OneD, TData> out(nphys * numComp, 0.0);
203
204 Array<OneD, Array<OneD, TData>> inarray(numComp);
205 Array<OneD, Array<OneD, TData>> outarray(numComp);
206
207 for (unsigned i = 0; i < numComp; ++i)
208 {
209 inarray[i] = Array<OneD, TData>(nphys, 0.0);
210 for (size_t j = 0; j < nphys; j++)
211 {
212 inarray[i][j] = in[i * nphys + j];
213 }
214 outarray[i] = Array<OneD, TData>(nphys, 0.0);
215 }
216
217 diffObject->Diffuse(numComp, exp, inarray, outarray);
218
219 for (unsigned i = 0; i < numComp; ++i)
220 {
221 for (size_t j = 0; j < nphys; ++j)
222 {
223 out[i * nphys + j] = outarray[i][j];
224 }
225 }
226
227 this->fixt_expected->template CopyArray<NektarSpaces::HostSpace>(out);
228 }
229
230 /// Mark every variable as carrying a non-zero flux.
231 static void SetAllNonZero(const unsigned n, Array<OneD, int> &nonZeroIndex)
232 {
233 nonZeroIndex = Array<OneD, int>(n);
234 for (unsigned i = 0; i < n; ++i)
235 {
236 nonZeroIndex[i] = i;
237 }
238 }
239
240 /**
241 * @brief Isotropic unit viscous tensor, matching #m_diffCoeff.
242 *
243 * Serves both the volume and the trace hook: with a constant unit
244 * diffusivity the flux is the gradient itself either side of a trace, so
245 * there is nothing to distinguish them.
246 */
248 const int nDim,
249 [[maybe_unused]] const Array<OneD, Array<OneD, TData>> &inarray,
250 const TensorOfArray3D<TData> &qfield,
251 TensorOfArray3D<TData> &viscousTensor, Array<OneD, int> &nonZeroIndex,
252 [[maybe_unused]] const Array<OneD, Array<OneD, TData>> &normal)
253 {
254 const unsigned nConvectiveFields = qfield[0].size();
255 const unsigned nPts = qfield[0][0].size();
256
257 // Which variables have a non-zero viscous flux. For a scalar it is
258 // all of them; the compressible solver leaves density out, which is
259 // why the hook exists. Leaving it empty is not neutral - DiffusionIP
260 // loops over it to decide what to write, so an unset one produces a
261 // silent zero rather than an error.
262 SetAllNonZero(nConvectiveFields, nonZeroIndex);
263
264 for (int j = 0; j < nDim; ++j)
265 {
266 for (unsigned i = 0; i < nConvectiveFields; ++i)
267 {
268 Vmath::Vcopy(nPts, qfield[j][i], 1, viscousTensor[j][i], 1);
269 }
270 }
271 }
272
273 /**
274 * @brief The same flux on a trace, where it is already contracted with the
275 * normal.
276 *
277 * So the output has one direction rather than nDim - which is the whole
278 * difference between this and GetFluxCons(), and why the reference
279 * implementation in NavierStokesCFE binds a different instantiation of
280 * its flux function here rather than the same one. Binding the volume
281 * form in this slot indexes the output past its first direction, and the
282 * failure surfaces well inside SolverUtils rather than at the binding.
283 */
285 const int nDim,
286 [[maybe_unused]] const Array<OneD, Array<OneD, TData>> &inarray,
287 const TensorOfArray3D<TData> &qfield, TensorOfArray3D<TData> &outarray,
288 Array<OneD, int> &nonZeroIndex,
289 const Array<OneD, Array<OneD, TData>> &normal)
290 {
291 const unsigned nConvectiveFields = qfield[0].size();
292 const unsigned nPts = qfield[0][0].size();
293
294 SetAllNonZero(nConvectiveFields, nonZeroIndex);
295
296 for (unsigned i = 0; i < nConvectiveFields; ++i)
297 {
298 Vmath::Zero(nPts, outarray[0][i], 1);
299 for (int j = 0; j < nDim; ++j)
300 {
301 Vmath::Vvtvp(nPts, qfield[j][i], 1, normal[j], 1,
302 outarray[0][i], 1, outarray[0][i], 1);
303 }
304 }
305 }
306
307 /**
308 * @brief Symmetric flux for the interior penalty form.
309 *
310 * With unit isotropic diffusivity this is the jump carried into each
311 * coordinate direction weighted by the trace normal. Kept separate from
312 * GetFluxCons() because it is the term the symmetrising work varies, and
313 * having it as its own hook is what lets that be exercised here.
314 */
316 const int nDim,
317 [[maybe_unused]] const Array<OneD, Array<OneD, TData>> &inaverg,
318 const Array<OneD, Array<OneD, TData>> &inarray,
319 TensorOfArray3D<TData> &outarray, Array<OneD, int> &nonZeroIndex,
320 const Array<OneD, Array<OneD, TData>> &normal)
321 {
322 const unsigned nConvectiveFields = inarray.size();
323 const unsigned nPts = inarray[0].size();
324
325 SetAllNonZero(nConvectiveFields, nonZeroIndex);
326
327 for (int j = 0; j < nDim; ++j)
328 {
329 for (unsigned i = 0; i < nConvectiveFields; ++i)
330 {
331 Vmath::Vmul(nPts, inarray[i], 1, normal[j], 1, outarray[j][i],
332 1);
333 }
334 }
335 }
336
337 /// Boundary fix-up hook. A scalar diffusion has nothing to correct - it
338 /// exists for the compressible solver, which resets the energy at a wall -
339 /// but DiffusionIP calls it unconditionally, so it has to be bound.
341 [[maybe_unused]] Array<OneD, Array<OneD, TData>> &consvar)
342 {
343 }
344
345public:
346 std::vector<TData> m_diffCoeff;
347};
348
349// clang-format off
350#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
351#define TESTFLOAT(type, filename) \
352 class type##float : public TestDiffusionIP<float> \
353 { \
354 public: \
355 type##float() \
356 { \
357 std::string dir = "./"; \
358 meshName = dir + filename; \
359 } \
360 };
361#else
362#define TESTFLOAT(type, filename)
363#endif
364#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
365#define TESTDOUBLE(type, filename) \
366 class type : public TestDiffusionIP<double> \
367 { \
368 public: \
369 type() \
370 { \
371 std::string dir = "./"; \
372 meshName = dir + filename; \
373 } \
374 };
375#else
376#define TESTDOUBLE(type, filename)
377#endif
378#define TEST(type, filename) \
379 TESTFLOAT(type, filename) \
380 TESTDOUBLE(type, filename)
381// clang-format on
382
383TEST(Quad, "run/Quad_Dir.xml")
384
385TEST(Tri, "run/Tri_Dir.xml")
386
387TEST(TriQuad_VarP, "run/TriQuad_varP_Dir.xml")
388
389TEST(Hex, "run/Hex_Dir.xml")
390
391TEST(Prism, "run/Prism_Dir.xml")
392
393TEST(Tet, "run/Tet_Dir.xml")
394
395TEST(CubeAllElements, "run/cube_all_elements_dir.xml")
#define ASSERTL0(condition, msg)
SpatialDomains::MeshGraphSharedPtr graph
#define TEST(type, filename)
SpatialDomains::Geometry * el
static std::shared_ptr< DiffusionScalarVolFluxOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy the data to a Nektar::Array, component by component and element by element, without padding,...
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Interior penalty diffusion, redesign against legacy.
std::vector< TData > m_diffCoeff
void GetFluxConsTrace(const int nDim, const Array< OneD, Array< OneD, TData > > &inarray, const TensorOfArray3D< TData > &qfield, TensorOfArray3D< TData > &outarray, Array< OneD, int > &nonZeroIndex, const Array< OneD, Array< OneD, TData > > &normal)
The same flux on a trace, where it is already contracted with the normal.
void GetSymmFluxCons(const int nDim, const Array< OneD, Array< OneD, TData > > &inaverg, const Array< OneD, Array< OneD, TData > > &inarray, TensorOfArray3D< TData > &outarray, Array< OneD, int > &nonZeroIndex, const Array< OneD, Array< OneD, TData > > &normal)
Symmetric flux for the interior penalty form.
TestDiffusionIP()=default
void GetFluxCons(const int nDim, const Array< OneD, Array< OneD, TData > > &inarray, const TensorOfArray3D< TData > &qfield, TensorOfArray3D< TData > &viscousTensor, Array< OneD, int > &nonZeroIndex, const Array< OneD, Array< OneD, TData > > &normal)
Isotropic unit viscous tensor, matching m_diffCoeff.
static void SetAllNonZero(const unsigned n, Array< OneD, int > &nonZeroIndex)
Mark every variable as carrying a non-zero flux.
void SpecialBndTreat(Array< OneD, Array< OneD, TData > > &consvar)
Boundary fix-up hook. A scalar diffusion has nothing to correct - it exists for the compressible solv...
DiffusionFactory & GetDiffusionFactory()
Definition Diffusion.cpp:39
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition Vmath.hpp:74
void Vvtvp(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvp (vector times vector plus vector): z = w*x + y
Definition Vmath.hpp:368
void Zero(int n, T *x, const int incx)
Zero vector.
Definition Vmath.hpp:275
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:828