Nektar++
Loading...
Searching...
No Matches
TestFusedElmt.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestFusedElmt.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: Test fixture of the fused element-local operators against the
32// separate operators they replace.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#pragma once
37
39
43#include <SolverCore/AdvDiffusion/AdvDiffFusedElmt/AdvDiffFusedElmtOp.hpp>
44#include <SolverCore/Advection/AdvectionFusedElmt/AdvectionFusedElmtOp.hpp>
46
47#include <cmath>
48#include <limits>
49#include <string>
50#include <type_traits>
51#include <vector>
52
53using namespace Nektar;
54using namespace Nektar::LibUtilities;
55using namespace Nektar::MultiRegions;
56
57/**
58 * @brief A fused element-local operator against the separate operators it
59 * replaces, for the flux policy @p Policy, on the mesh of the derived fixture
60 * with its expansions replaced: every element of the given expansion type,
61 * number of modes and number of points per direction.
62 *
63 * The operator is the advection one (SolverCore::AdvectionFusedElmtOp) or,
64 * for @p VISCOUS, the advection-diffusion one (SolverCore::AdvDiffFusedElmtOp).
65 * The separate chain is PhysTraceExtract for the state trace; for the
66 * advection-diffusion operator PhysDeriv for the gradient; the policy
67 * evaluated point by point for the fluxes; IProductWRTDerivBase with scale -1
68 * for the derivative inner products; and for the advection-diffusion
69 * operator PhysTraceExtract::ApplyNormalComponent() of the viscous flux for
70 * the normal viscous flux on the traces, and PhysTraceExtract of the gradient
71 * for the gradient trace. Each case is run with the fields at width 1 and,
72 * where the execution space interleaves, at its vector width, and for the
73 * advection-diffusion operator with a gradient trace on every other element
74 * and on every element.
75 *
76 * @tparam Policy The flux policy (TestFusedElmtPolicies.hpp).
77 * @tparam VISCOUS Test the advection-diffusion operator rather than the
78 * advection one.
79 */
80template <typename Policy, bool VISCOUS>
81class TestFusedElmt : public TestOp<double, FieldState::Phys, FieldState::Phys,
82 MultiRegions::DisContField>
83{
84public:
85 using TData = double;
87
88 static constexpr unsigned int NDIM = Policy::NDIM;
89 static constexpr unsigned int NVAR = Policy::NVAR;
90 static constexpr unsigned int NAUX = Policy::NAUX;
91
93 std::conditional_t<VISCOUS, SolverCore::AdvDiffFusedElmtOp<TData>,
94 SolverCore::AdvectionFusedElmtOp<TData>>;
95
96 TestFusedElmt() = default;
97
98 /**
99 * @brief Run one expansion: @p type with @p nmodes modes and, when
100 * positive, @p npts points per direction (the type's own otherwise).
101 *
102 * @return Whether every variant matched the chain within @p tol,
103 * relative to the largest entry of each reference.
104 */
106 const int nmodes, const int npts, const TData tol)
107 {
108 if (!this->session)
109 {
110 this->SetSession();
111 }
112 SetExpansions(type, nmodes, npts);
113 this->fixt_explist->SetDataWarehouse();
114 Reference();
115
116 const std::string exec = GlobalConfiguration::ExecStr();
117 auto op =
118 FusedElmtOp::Create(this->fixt_explist, Components(), Policy::name,
119 exec, GlobalConfiguration::ImplStr());
120 if (!op)
121 {
122 // Refused rightly only when an element has more points than a
123 // fused thread block may have threads.
124 std::cout << " no fused operator for this mesh" << std::endl;
125 return !FitsDeviceBlock(exec);
126 }
127
128 const bool interleaves = NektarSpaces::GetVectorWidth<TData>(exec) > 1;
129 bool match = RunVariant(*op, false, GradTraceList::None, tol);
130 if (interleaves)
131 {
132 match = RunVariant(*op, true, GradTraceList::None, tol) && match;
133 }
134 if constexpr (VISCOUS)
135 {
136 match =
137 RunVariant(*op, interleaves, GradTraceList::EveryOther, tol) &&
138 match;
139 match = RunVariant(*op, false, GradTraceList::All, tol) && match;
140 // Without a gradient trace again, after one was written.
141 match = RunVariant(*op, false, GradTraceList::None, tol) && match;
142 }
143 return match;
144 }
145
146protected:
147 /// Which elements of each block a variant writes the gradient trace of.
148 enum class GradTraceList
149 {
150 None,
152 All
153 };
154
155 /// The inputs, laid out as Field::ToVector() returns them.
156 std::vector<TData> m_stateValues, m_auxValues;
157 /// The chain's outputs, laid out likewise.
159
160 static std::vector<std::string> Components()
161 {
162 std::vector<std::string> comps;
163 for (unsigned int n = 0; n < NVAR; ++n)
164 {
165 comps.push_back("q" + std::to_string(n));
166 }
167 return comps;
168 }
169
170 /**
171 * @brief Whether the mesh has a deformed quadrilateral element.
172 *
173 * Order 0 skips such meshes: with one Gauss point per direction the
174 * trace normals of a deformed quadrilateral are wrong in
175 * LocalRegions::QuadExp::v_ComputeTraceNormal, which reads past its edge
176 * array.
177 */
179 {
180 if (!this->session)
181 {
182 this->SetSession();
183 }
186 for (auto [id, quad] : graph->GetGeomMap<SpatialDomains::QuadGeom>())
187 {
188 if (graph->GetMeshDimension() == 2 &&
189 quad->CalcGeomType() == SpatialDomains::eDeformed)
190 {
191 return true;
192 }
193 }
194 return false;
195 }
196
197 /**
198 * @brief Whether the operator, created on a session that names no
199 * implementation, takes the default of each block
200 * (MultiRegions::ElmtOp::GetOpImpl()): it exists exactly where the
201 * default of every block, read from the default implementation table,
202 * has a block operator of this family, which is SumFac on Serial and
203 * AVX and SumFacTOP on Device.
204 *
205 * @param type The expansion type of every element.
206 * @param nmodes The number of modes of every element.
207 */
209 const int nmodes)
210 {
211 const std::string exec = GlobalConfiguration::ExecStr();
212
213 // The fixture's session names the implementation on the command
214 // line, which overrides the default: build one that does not.
215 int argc = 3;
216 char **argv = new char *[argc];
217 argv[0] = strdup("exe_name");
218 argv[1] = strdup(this->meshName.data());
219 argv[2] = strdup(("--opExecSpace=" + exec).c_str());
221 for (int i = 0; i < argc; ++i)
222 {
223 free(argv[i]);
224 }
225 delete[] argv;
226
227 SetExpansions(type, nmodes, -1);
228 this->fixt_explist->SetDataWarehouse();
229
230 const std::string familyImpl =
231 exec == "Device" ? "SumFacTOP" : "SumFac";
232 bool expected = FitsDeviceBlock(exec);
233 for (auto &coll : MultiRegions::GetCollections(this->fixt_explist))
234 {
235 expected =
237 coll.GetExpVector()[0], exec) == familyImpl;
238 }
239 const bool created = static_cast<bool>(FusedElmtOp::Create(
240 this->fixt_explist, Components(), Policy::name, exec));
241 if (created != expected)
242 {
243 std::cout << " modes " << nmodes << ": operator "
244 << (created ? "created" : "not created")
245 << " where the default implementation "
246 << (expected ? "has" : "has no") << " block operator"
247 << std::endl;
248 }
249 return created == expected;
250 }
251
252 /// Whether every element has no more points than a fused thread block
253 /// may have threads on @p exec (SolverCore::fusedElmtMaxBlockSize).
254 bool FitsDeviceBlock(const std::string &exec) const
255 {
256 bool fits = true;
257 if (exec == "Device")
258 {
259 for (size_t e = 0; e < this->fixt_explist->GetExpSize(); ++e)
260 {
261 fits = fits &&
262 static_cast<unsigned int>(
263 this->fixt_explist->GetExp(e)->GetTotPoints()) <=
264 SolverCore::fusedElmtMaxBlockSize;
265 }
266 }
267 return fits;
268 }
269
270 /// Read the mesh and give every element @p type with @p nmodes modes
271 /// and @p npts points, then build the expansion list.
273 const int nmodes, const int npts)
274 {
276 for (const auto &var : this->session->GetVariables())
277 {
278 for (const auto &expInfo : graph->GetExpansionInfo(var))
279 {
280 expInfo.second->m_basisKeyVector =
282 expInfo.second->m_geomPtr, type, nmodes, npts);
283 }
284 }
285 const auto &vars = this->session->GetVariables();
286 this->fixt_explist =
288 this->session, graph, vars[0], true, true,
290 }
291
292 /**
293 * @brief The state and the auxiliary fields, every entry different: a
294 * positive density and pressure, a velocity of order one and an
295 * auxiliary field near one, so that the fluxes are those of a physical
296 * state. @p npts entries per component.
297 */
298 void StateValues(const size_t npts)
299 {
300 const TData gamma = 1.4;
301 m_stateValues.assign(NVAR * npts, 0.0);
302 m_auxValues.assign(NAUX * npts, 0.0);
303 for (size_t q = 0; q < npts; ++q)
304 {
305 const TData x = TData(0.37) * q;
306 const TData rho = 1.0 + 0.2 * std::sin(x + 0.3);
307 const TData pressure = 1.0 + 0.1 * std::sin(0.7 * x + 1.1);
308 TData kinetic = 0.0;
309 m_stateValues[q] = rho;
310 for (unsigned int d = 0; d < NDIM; ++d)
311 {
312 const TData velocity = 0.3 * std::cos(1.3 * x + d);
313 m_stateValues[(1 + d) * npts + q] = rho * velocity;
314 kinetic += 0.5 * rho * velocity * velocity;
315 }
316 m_stateValues[(NDIM + 1) * npts + q] =
317 pressure / (gamma - 1.0) + kinetic;
318 for (unsigned int a = 0; a < NAUX; ++a)
319 {
320 m_auxValues[a * npts + q] = 1.0 + 0.3 * std::sin(0.9 * x + a);
321 }
322 }
323 }
324
325 /// The separate chain, in the execution space under test, from the
326 /// inputs StateValues() makes.
328 {
329 const auto &explist = this->fixt_explist;
330 const auto comps = Components();
331 const auto physAttr =
332 GetBlockAttributes<TData, FieldState::Phys>(explist);
333 const auto traceAttr =
334 GetLocTraceBlockAttributes<TData, FieldState::Phys>(explist);
335 const std::string exec = GlobalConfiguration::ExecStr();
336
337 PhysField state("state", physAttr, NVAR, 1);
338 PhysField flux("flux", physAttr, NVAR * NDIM, 1);
339 PhysField trace("trace", traceAttr, NVAR, 1);
340 PhysField out("out", physAttr, NVAR, 1);
341 size_t npts = 0;
342 for (const auto &attr : physAttr)
343 {
344 npts += attr.GetNumElements() * attr.GetNumData();
345 }
346 state.template Initialize<NektarSpaces::HostSpace>(0.0);
347 flux.template Initialize<NektarSpaces::HostSpace>(0.0);
348 trace.template Initialize<NektarSpaces::HostSpace>(0.0);
349 out.template Initialize<NektarSpaces::HostSpace>(0.0);
350 StateValues(npts);
351 state.template CopyVector<NektarSpaces::HostSpace>(m_stateValues);
352
353 auto traceOp =
354 PhysTraceExtractOp<TData>::Create(explist, comps, exec, "SumFac");
355 traceOp->Apply(state, trace);
356 m_traceRef = trace.template ToVector<TData>();
357
358 // The gradient, direction-major per variable as PhysDeriv lays it
359 // out, component n * NDIM + d.
360 std::vector<TData> gradValues(NVAR * NDIM * npts, 0.0);
361 if constexpr (VISCOUS)
362 {
363 PhysField grad("grad", physAttr, NVAR * NDIM, 1);
364 grad.template Initialize<NektarSpaces::HostSpace>(0.0);
365 PhysDerivOp<TData>::Create(explist, comps, exec, "SumFac")
366 ->Apply(state, grad);
367 gradValues = grad.template ToVector<TData>();
368
369 std::vector<std::string> gradComps(NVAR * NDIM, "g");
370 PhysField gradTrace("gradTrace", traceAttr, NVAR * NDIM, 1);
371 gradTrace.template Initialize<NektarSpaces::HostSpace>(0.0);
372 PhysTraceExtractOp<TData>::Create(explist, gradComps, exec,
373 "SumFac")
374 ->Apply(grad, gradTrace);
375 m_gradTraceRef = gradTrace.template ToVector<TData>();
376 }
377
378 // The policy point by point.
379 Policy policy(this->session);
380 std::vector<TData> fluxValues(NVAR * NDIM * npts, 0.0);
381 std::vector<TData> viscValues(NVAR * NDIM * npts, 0.0);
382 for (size_t q = 0; q < npts; ++q)
383 {
384 // The advection operator has no viscous flux, and reads no
385 // gradient.
386 TData statePt[NVAR], fluxPt[NDIM][NVAR],
387 viscFluxPt[NDIM][NVAR] = {}, auxPt[NAUX > 0 ? NAUX : 1];
388 [[maybe_unused]] TData gradPt[NDIM][NVAR];
389 for (unsigned int n = 0; n < NVAR; ++n)
390 {
391 statePt[n] = m_stateValues[n * npts + q];
392 for (unsigned int d = 0; d < NDIM; ++d)
393 {
394 gradPt[d][n] = gradValues[(n * NDIM + d) * npts + q];
395 }
396 }
397 for (unsigned int a = 0; a < NAUX; ++a)
398 {
399 auxPt[a] = m_auxValues[a * npts + q];
400 }
401 // The policy's fluxes, combined here rather than through the
402 // operator's own wrappers: flux = viscFlux - advFlux.
403 const TData *auxArg = NAUX > 0 ? auxPt : nullptr;
404 policy.AdvFlux(statePt, auxArg, fluxPt);
405 if constexpr (VISCOUS)
406 {
407 policy.ViscFlux(statePt, gradPt, auxArg, viscFluxPt);
408 }
409 for (unsigned int d = 0; d < NDIM; ++d)
410 {
411 for (unsigned int n = 0; n < NVAR; ++n)
412 {
413 fluxPt[d][n] =
414 (VISCOUS ? viscFluxPt[d][n] : TData(0)) - fluxPt[d][n];
415 }
416 }
417 for (unsigned int n = 0; n < NVAR; ++n)
418 {
419 for (unsigned int d = 0; d < NDIM; ++d)
420 {
421 fluxValues[(n * NDIM + d) * npts + q] = fluxPt[d][n];
422 viscValues[(n * NDIM + d) * npts + q] = viscFluxPt[d][n];
423 }
424 }
425 }
426
427 flux.template CopyVector<NektarSpaces::HostSpace>(fluxValues);
429 explist, comps, exec, "SumFac");
430 iprodOp->SetScale(-1.0);
431 iprodOp->Apply(flux, out);
432 m_outRef = out.template ToVector<TData>();
433
434 if constexpr (VISCOUS)
435 {
436 PhysField visc("visc", physAttr, NVAR * NDIM, 1);
437 PhysField normVisc("normVisc", traceAttr, NVAR, 1);
438 visc.template Initialize<NektarSpaces::HostSpace>(0.0);
439 normVisc.template Initialize<NektarSpaces::HostSpace>(0.0);
440 visc.template CopyVector<NektarSpaces::HostSpace>(viscValues);
441 traceOp->ApplyNormalComponent(visc, normVisc);
442 m_normViscRef = normVisc.template ToVector<TData>();
443 }
444 }
445
446 /// The listed elements of each block for @p list.
447 std::vector<std::vector<unsigned int>> GradTraceElements(
448 const GradTraceList list) const
449 {
450 const auto physAttr =
451 GetBlockAttributes<TData, FieldState::Phys>(this->fixt_explist);
452 std::vector<std::vector<unsigned int>> elements(physAttr.size());
453 for (size_t blk = 0; blk < physAttr.size(); ++blk)
454 {
455 for (unsigned int e = 0; e < physAttr[blk].GetNumElements(); ++e)
456 {
457 if (list == GradTraceList::All || e % 2 == 0)
458 {
459 elements[blk].push_back(e);
460 }
461 }
462 }
463 return elements;
464 }
465
466 /// One variant: the fused operator from the inputs the chain took.
467 bool RunVariant(FusedElmtOp &op, const bool interleaved,
468 const GradTraceList list, const TData tol)
469 {
470 const auto &explist = this->fixt_explist;
471 const auto physAttr =
472 GetBlockAttributes<TData, FieldState::Phys>(explist);
473 const auto traceAttr =
474 GetLocTraceBlockAttributes<TData, FieldState::Phys>(explist);
475 const std::string exec = GlobalConfiguration::ExecStr();
476
477 PhysField state("state", physAttr, NVAR, 1);
478 PhysField trace("trace", traceAttr, NVAR, 1);
479 PhysField out("out", physAttr, NVAR, 1);
480 state.template Initialize<NektarSpaces::HostSpace>(0.0);
481 trace.template Initialize<NektarSpaces::HostSpace>(0.0);
482 out.template Initialize<NektarSpaces::HostSpace>(0.0);
483 state.template CopyVector<NektarSpaces::HostSpace>(m_stateValues);
484 PhysField aux;
485 if constexpr (NAUX > 0)
486 {
487 aux = PhysField("aux", physAttr, NAUX, 1);
488 aux.template Initialize<NektarSpaces::HostSpace>(0.0);
489 aux.template CopyVector<NektarSpaces::HostSpace>(m_auxValues);
490 }
491 PhysField normVisc;
492 if constexpr (VISCOUS)
493 {
494 normVisc = PhysField("normVisc", traceAttr, NVAR, 1);
495 normVisc.template Initialize<NektarSpaces::HostSpace>(0.0);
496 }
497
498 // The gradient trace on the listed elements only, in list order.
499 const auto elements = GradTraceElements(list);
500 PhysField gradTrace;
501 if constexpr (VISCOUS)
502 {
503 if (list != GradTraceList::None)
504 {
505 const auto vectorWidth =
507 std::vector<BlockAttributes<FieldState::Phys>> gradTraceAttr;
508 for (size_t blk = 0; blk < traceAttr.size(); ++blk)
509 {
510 const size_t nelmt = elements[blk].size();
511 gradTraceAttr.push_back(
512 {nelmt,
513 ((nelmt + vectorWidth - 1) / vectorWidth) *
514 vectorWidth,
515 traceAttr[blk].GetNumData(), 1});
516 }
517 gradTrace =
518 PhysField("gradTrace", gradTraceAttr, NVAR * NDIM, 1);
519 gradTrace.template Initialize<NektarSpaces::HostSpace>(0.0);
520 op.SetGradientTrace(&gradTrace, &elements);
521 }
522 else
523 {
524 op.SetGradientTrace(nullptr, nullptr);
525 }
526 }
527
528 if (interleaved)
529 {
530 // Interleaved at the execution space's vector width: the SIMD
531 // width, the host operators' own, or the warp size.
532 const unsigned int interleaveWidth =
533 NektarSpaces::GetVectorWidth<TData>(exec);
534 state.ReshapeStorage(interleaveWidth, exec);
535 if constexpr (NAUX > 0)
536 {
537 aux.ReshapeStorage(interleaveWidth, exec);
538 }
539 }
540 if constexpr (VISCOUS && NAUX > 0)
541 {
542 op.Apply(state, aux, trace, normVisc, out);
543 }
544 else if constexpr (VISCOUS)
545 {
546 op.Apply(state, trace, normVisc, out);
547 }
548 else if constexpr (NAUX > 0)
549 {
550 op.Apply(state, aux, trace, out);
551 }
552 else
553 {
554 op.Apply(state, trace, out);
555 }
556
557 bool match =
558 Matches("out", out.template ToVector<TData>(), m_outRef, tol);
559 match = Matches("trace", trace.template ToVector<TData>(), m_traceRef,
560 tol) &&
561 match;
562 if constexpr (VISCOUS)
563 {
564 match =
565 Matches("traceNormVisc", normVisc.template ToVector<TData>(),
566 m_normViscRef, tol) &&
567 match;
568 }
569 if (list != GradTraceList::None)
570 {
571 match = Matches("gradTrace", gradTrace.template ToVector<TData>(),
572 GradTraceExpected(elements, traceAttr, gradTrace),
573 tol) &&
574 match;
575 }
576 match = Matches("state", state.template ToVector<TData>(),
577 m_stateValues, TData(0)) &&
578 match;
579 if (!match)
580 {
581 std::cout << " mismatch: interleaved " << interleaved
582 << ", gradient trace list " << static_cast<int>(list)
583 << std::endl;
584 }
585 return match;
586 }
587
588 /// The chain's gradient trace of the listed elements, laid out as the
589 /// compact gradient trace field's ToVector() returns it.
590 std::vector<TData> GradTraceExpected(
591 const std::vector<std::vector<unsigned int>> &elements,
592 const std::vector<BlockAttributes<FieldState::Phys>> &traceAttr,
593 PhysField &gradTrace) const
594 {
595 size_t fullCompSize = 0;
596 for (const auto &attr : traceAttr)
597 {
598 fullCompSize += attr.GetNumElements() * attr.GetNumData();
599 }
600 const size_t compactCompSize =
601 gradTrace.template ToVector<TData>().size() / (NVAR * NDIM);
602
603 std::vector<TData> expected(NVAR * NDIM * compactCompSize, 0.0);
604 for (unsigned int c = 0; c < NVAR * NDIM; ++c)
605 {
606 size_t fullOffset = c * fullCompSize;
607 size_t compactOffset = c * compactCompSize;
608 for (size_t blk = 0; blk < traceAttr.size(); ++blk)
609 {
610 const unsigned int ndata = traceAttr[blk].GetNumData();
611 for (size_t k = 0; k < elements[blk].size(); ++k)
612 {
613 for (unsigned int i = 0; i < ndata; ++i)
614 {
615 expected[compactOffset + k * ndata + i] =
616 m_gradTraceRef[fullOffset +
617 elements[blk][k] * ndata + i];
618 }
619 }
620 fullOffset += traceAttr[blk].GetNumElements() * ndata;
621 compactOffset += elements[blk].size() * ndata;
622 }
623 }
624 return expected;
625 }
626
627 /// Whether @p out matches @p expected within @p tol times the largest
628 /// entry of @p expected, or times machine epsilon when that is smaller:
629 /// the inputs are of order one, so an output below epsilon is zero up
630 /// to round-off (the gradient of a one-mode expansion on a collapsed
631 /// shape).
632 static bool Matches(const std::string &what, const std::vector<TData> &out,
633 const std::vector<TData> &expected, const TData tol)
634 {
635 if (out.size() != expected.size())
636 {
637 std::cout << " " << what << ": " << out.size()
638 << " entries against " << expected.size() << std::endl;
639 return false;
640 }
641 TData norm = std::numeric_limits<TData>::epsilon(), err = 0;
642 for (size_t i = 0; i < out.size(); ++i)
643 {
644 norm = std::max(norm, std::abs(expected[i]));
645 err = std::max(err, std::isfinite(out[i])
646 ? std::abs(out[i] - expected[i])
647 : std::numeric_limits<TData>::max());
648 }
649 if (err > tol * norm)
650 {
651 std::cout << " " << what << ": max error " << err
652 << " against max entry " << norm << std::endl;
653 return false;
654 }
655 return true;
656 }
657};
658
659// clang-format off
660/// A fixture for @p policy on the mesh @p filename, testing the
661/// advection-diffusion operator for @p viscous and the advection one
662/// otherwise.
663#define TEST_FUSEDELMT_FIXTURE(type, policy, viscous, filename) \
664 class type \
665 : public TestFusedElmt<Nektar::UnitTests::policy, viscous> \
666 { \
667 public: \
668 type() \
669 { \
670 meshName = filename; \
671 } \
672 };
673
674// Orders 1 to 8, each with the expansion type's own points and with one
675// point more per direction, on the modified and the orthogonal bases, and
676// order 0 on the orthogonal basis (a one-mode modified basis is not a
677// constant) unless the mesh has a deformed quadrilateral (see
678// HasDeformedQuad()): the compile-time and the runtime sizes of the size
679// switch, more than the eight points per direction the line-blocked kernels
680// hold in registers, and trace rules that are and are not the volume rules.
681#define TEST_FUSEDELMT_CASE(test_name, test, tol) \
682 BOOST_FIXTURE_TEST_CASE(test_name, test) \
683 { \
684 std::cout << std::string("Run: ") + std::string(#test_name) \
685 << std::endl; \
686 const bool orderZero = !HasDeformedQuad(); \
687 for (const auto type : \
688 {SpatialDomains::eModified, SpatialDomains::eOrthogonal}) \
689 { \
690 const int minModes = \
691 type == SpatialDomains::eOrthogonal && orderZero ? 1 : 2; \
692 for (int nmodes = minModes; nmodes <= 9; ++nmodes) \
693 { \
694 for (const int npts : {-1, nmodes + 1}) \
695 { \
696 const bool match = RunExpansion(type, nmodes, npts, tol); \
697 if (!match) \
698 { \
699 std::cout << "Failed: type " << type << ", modes " \
700 << nmodes << ", points " << npts \
701 << std::endl; \
702 } \
703 BOOST_TEST(match); \
704 } \
705 } \
706 } \
707 }
708
709// Orders 1 to 8 on a session that names no implementation: the operator
710// takes the default of each block (DefaultImplementation()).
711#define TEST_FUSEDELMT_DEFAULT_CASE(test_name, test) \
712 BOOST_FIXTURE_TEST_CASE(test_name, test) \
713 { \
714 std::cout << std::string("Run: ") + std::string(#test_name) \
715 << std::endl; \
716 for (int nmodes = 2; nmodes <= 9; ++nmodes) \
717 { \
718 BOOST_TEST( \
719 DefaultImplementation(SpatialDomains::eModified, nmodes)); \
720 } \
721 }
722// clang-format on
Whole-field interface of the IProductWRTDerivBase element operator, which takes a vector-valued physi...
Public interface of the physical-space derivative (PhysDeriv) element operator, which maps a field at...
Public interface of the PhysTraceExtract element operator, which interpolates a volume physical-space...
SpatialDomains::MeshGraphSharedPtr graph
std::vector< NekDouble > grad
A block means a group of elements of identical shape, basis and order. BlockAttributes stores the mos...
Definition Block.hpp:50
size_t size() const
Gets the size of the field.
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static std::shared_ptr< IProductWRTDerivBaseOp< TFieldOut, TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Build a complete IProductWRTDerivBase operator through the operator and block-operator factories.
static std::string GetDefaultOpImpl(const StdRegions::StdExpansionSharedPtr &exp, const std::string &execSpace)
Return the implementation a block of elements takes when neither the command line nor the session nam...
Definition Operator.hpp:255
static std::shared_ptr< PhysDerivOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Create a physical-derivative operator, its per-block operators and, for a multi-plane 3DH1 expansion,...
static std::shared_ptr< PhysTraceExtractOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Create a trace extraction operator through the operator and block-operator factories.
static LibUtilities::BasisKeyVector DefineBasisKeyFromExpansionType(Geometry *in, ExpansionType type, const int order, const int numpoints=-1)
The basis keys of expansion type type at order on in, with numpoints quadrature points in every direc...
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
A fused element-local operator against the separate operators it replaces, for the flux policy Policy...
GradTraceList
Which elements of each block a variant writes the gradient trace of.
std::vector< TData > m_stateValues
The inputs, laid out as Field::ToVector() returns them.
void StateValues(const size_t npts)
The state and the auxiliary fields, every entry different: a positive density and pressure,...
static std::vector< std::string > Components()
bool DefaultImplementation(const SpatialDomains::ExpansionType type, const int nmodes)
Whether the operator, created on a session that names no implementation, takes the default of each bl...
void Reference()
The separate chain, in the execution space under test, from the inputs StateValues() makes.
std::vector< TData > m_outRef
TestFusedElmt()=default
static constexpr unsigned int NAUX
std::vector< TData > GradTraceExpected(const std::vector< std::vector< unsigned int > > &elements, const std::vector< BlockAttributes< FieldState::Phys > > &traceAttr, PhysField &gradTrace) const
The chain's gradient trace of the listed elements, laid out as the compact gradient trace field's ToV...
Field< TData, FieldState::Phys > PhysField
std::vector< TData > m_traceRef
The chain's outputs, laid out likewise.
std::vector< TData > m_gradTraceRef
std::conditional_t< VISCOUS, SolverCore::AdvDiffFusedElmtOp< TData >, SolverCore::AdvectionFusedElmtOp< TData > > FusedElmtOp
std::vector< TData > m_auxValues
bool RunVariant(FusedElmtOp &op, const bool interleaved, const GradTraceList list, const TData tol)
One variant: the fused operator from the inputs the chain took.
void SetExpansions(const SpatialDomains::ExpansionType type, const int nmodes, const int npts)
Read the mesh and give every element type with nmodes modes and npts points, then build the expansion...
static constexpr unsigned int NDIM
static constexpr unsigned int NVAR
static bool Matches(const std::string &what, const std::vector< TData > &out, const std::vector< TData > &expected, const TData tol)
Whether out matches expected within tol times the largest entry of expected, or times machine epsilon...
bool HasDeformedQuad()
Whether the mesh has a deformed quadrilateral element.
std::vector< TData > m_normViscRef
bool RunExpansion(const SpatialDomains::ExpansionType type, const int nmodes, const int npts, const TData tol)
Run one expansion: type with nmodes modes and, when positive, npts points per direction (the type's o...
std::vector< std::vector< unsigned int > > GradTraceElements(const GradTraceList list) const
The listed elements of each block for list.
bool FitsDeviceBlock(const std::string &exec) const
Whether every element has no more points than a fused thread block may have threads on exec (SolverCo...
Collections::CollectionVector GetCollections(MultiRegions::ExpListSharedPtr expansionList)
@ eDeformed
Geometry is curved or has non-constant factors.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition MeshGraph.h:287