43#include <SolverCore/AdvDiffusion/AdvDiffFusedElmt/AdvDiffFusedElmtOp.hpp>
44#include <SolverCore/Advection/AdvectionFusedElmt/AdvectionFusedElmtOp.hpp>
80template <
typename Policy,
bool VISCOUS>
82 MultiRegions::DisContField>
88 static constexpr unsigned int NDIM = Policy::NDIM;
89 static constexpr unsigned int NVAR = Policy::NVAR;
90 static constexpr unsigned int NAUX = Policy::NAUX;
93 std::conditional_t<VISCOUS, SolverCore::AdvDiffFusedElmtOp<TData>,
94 SolverCore::AdvectionFusedElmtOp<TData>>;
106 const int nmodes,
const int npts,
const TData tol)
116 const std::string exec = GlobalConfiguration::ExecStr();
119 exec, GlobalConfiguration::ImplStr());
124 std::cout <<
" no fused operator for this mesh" << std::endl;
128 const bool interleaves = NektarSpaces::GetVectorWidth<TData>(exec) > 1;
134 if constexpr (VISCOUS)
162 std::vector<std::string> comps;
163 for (
unsigned int n = 0; n <
NVAR; ++n)
165 comps.push_back(
"q" + std::to_string(n));
188 if (
graph->GetMeshDimension() == 2 &&
211 const std::string exec = GlobalConfiguration::ExecStr();
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)
230 const std::string familyImpl =
231 exec ==
"Device" ?
"SumFacTOP" :
"SumFac";
237 coll.GetExpVector()[0], exec) == familyImpl;
239 const bool created =
static_cast<bool>(FusedElmtOp::Create(
241 if (created != expected)
243 std::cout <<
" modes " << nmodes <<
": operator "
244 << (created ?
"created" :
"not created")
245 <<
" where the default implementation "
246 << (expected ?
"has" :
"has no") <<
" block operator"
249 return created == expected;
257 if (exec ==
"Device")
259 for (
size_t e = 0; e < this->
fixt_explist->GetExpSize(); ++e)
262 static_cast<unsigned int>(
264 SolverCore::fusedElmtMaxBlockSize;
273 const int nmodes,
const int npts)
276 for (
const auto &var : this->
session->GetVariables())
278 for (
const auto &expInfo :
graph->GetExpansionInfo(var))
280 expInfo.second->m_basisKeyVector =
282 expInfo.second->m_geomPtr, type, nmodes, npts);
285 const auto &vars = this->
session->GetVariables();
300 const TData gamma = 1.4;
303 for (
size_t q = 0; q < npts; ++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);
310 for (
unsigned int d = 0; d <
NDIM; ++d)
312 const TData velocity = 0.3 * std::cos(1.3 * x + d);
314 kinetic += 0.5 * rho * velocity * velocity;
317 pressure / (gamma - 1.0) + kinetic;
318 for (
unsigned int a = 0; a <
NAUX; ++a)
320 m_auxValues[a * npts + q] = 1.0 + 0.3 * std::sin(0.9 * x + a);
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();
342 for (
const auto &attr : physAttr)
344 npts += attr.GetNumElements() * attr.GetNumData();
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);
351 state.template CopyVector<NektarSpaces::HostSpace>(
m_stateValues);
355 traceOp->Apply(state, trace);
356 m_traceRef = trace.template ToVector<TData>();
360 std::vector<TData> gradValues(
NVAR *
NDIM * npts, 0.0);
361 if constexpr (VISCOUS)
364 grad.template Initialize<NektarSpaces::HostSpace>(0.0);
366 ->Apply(state,
grad);
367 gradValues =
grad.template ToVector<TData>();
369 std::vector<std::string> gradComps(
NVAR *
NDIM,
"g");
371 gradTrace.template Initialize<NektarSpaces::HostSpace>(0.0);
374 ->Apply(
grad, gradTrace);
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)
389 for (
unsigned int n = 0; n <
NVAR; ++n)
392 for (
unsigned int d = 0; d <
NDIM; ++d)
394 gradPt[d][n] = gradValues[(n *
NDIM + d) * npts + q];
397 for (
unsigned int a = 0; a <
NAUX; ++a)
403 const TData *auxArg =
NAUX > 0 ? auxPt :
nullptr;
404 policy.AdvFlux(statePt, auxArg, fluxPt);
405 if constexpr (VISCOUS)
407 policy.ViscFlux(statePt, gradPt, auxArg, viscFluxPt);
409 for (
unsigned int d = 0; d <
NDIM; ++d)
411 for (
unsigned int n = 0; n <
NVAR; ++n)
414 (VISCOUS ? viscFluxPt[d][n] :
TData(0)) - fluxPt[d][n];
417 for (
unsigned int n = 0; n <
NVAR; ++n)
419 for (
unsigned int d = 0; d <
NDIM; ++d)
421 fluxValues[(n *
NDIM + d) * npts + q] = fluxPt[d][n];
422 viscValues[(n *
NDIM + d) * npts + q] = viscFluxPt[d][n];
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>();
434 if constexpr (VISCOUS)
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);
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)
455 for (
unsigned int e = 0; e < physAttr[blk].GetNumElements(); ++e)
459 elements[blk].push_back(e);
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();
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);
485 if constexpr (
NAUX > 0)
488 aux.template Initialize<NektarSpaces::HostSpace>(0.0);
489 aux.template CopyVector<NektarSpaces::HostSpace>(
m_auxValues);
492 if constexpr (VISCOUS)
495 normVisc.template Initialize<NektarSpaces::HostSpace>(0.0);
501 if constexpr (VISCOUS)
505 const auto vectorWidth =
507 std::vector<BlockAttributes<FieldState::Phys>> gradTraceAttr;
508 for (
size_t blk = 0; blk < traceAttr.size(); ++blk)
510 const size_t nelmt = elements[blk].size();
511 gradTraceAttr.push_back(
513 ((nelmt + vectorWidth - 1) / vectorWidth) *
515 traceAttr[blk].GetNumData(), 1});
519 gradTrace.template Initialize<NektarSpaces::HostSpace>(0.0);
520 op.SetGradientTrace(&gradTrace, &elements);
524 op.SetGradientTrace(
nullptr,
nullptr);
532 const unsigned int interleaveWidth =
533 NektarSpaces::GetVectorWidth<TData>(exec);
535 if constexpr (
NAUX > 0)
540 if constexpr (VISCOUS &&
NAUX > 0)
542 op.Apply(state, aux, trace, normVisc, out);
544 else if constexpr (VISCOUS)
546 op.Apply(state, trace, normVisc, out);
548 else if constexpr (
NAUX > 0)
550 op.Apply(state, aux, trace, out);
554 op.Apply(state, trace, out);
562 if constexpr (VISCOUS)
565 Matches(
"traceNormVisc", normVisc.template ToVector<TData>(),
571 match =
Matches(
"gradTrace", gradTrace.template ToVector<TData>(),
576 match =
Matches(
"state", state.template ToVector<TData>(),
581 std::cout <<
" mismatch: interleaved " << interleaved
582 <<
", gradient trace list " <<
static_cast<int>(list)
591 const std::vector<std::vector<unsigned int>> &elements,
595 size_t fullCompSize = 0;
596 for (
const auto &attr : traceAttr)
598 fullCompSize += attr.GetNumElements() * attr.GetNumData();
600 const size_t compactCompSize =
601 gradTrace.template ToVector<TData>().
size() / (
NVAR *
NDIM);
603 std::vector<TData> expected(
NVAR *
NDIM * compactCompSize, 0.0);
604 for (
unsigned int c = 0; c <
NVAR *
NDIM; ++c)
606 size_t fullOffset = c * fullCompSize;
607 size_t compactOffset = c * compactCompSize;
608 for (
size_t blk = 0; blk < traceAttr.size(); ++blk)
610 const unsigned int ndata = traceAttr[blk].GetNumData();
611 for (
size_t k = 0; k < elements[blk].size(); ++k)
613 for (
unsigned int i = 0; i < ndata; ++i)
615 expected[compactOffset + k * ndata + i] =
617 elements[blk][k] * ndata + i];
620 fullOffset += traceAttr[blk].GetNumElements() * ndata;
621 compactOffset += elements[blk].size() * ndata;
632 static bool Matches(
const std::string &what,
const std::vector<TData> &out,
633 const std::vector<TData> &expected,
const TData tol)
635 if (out.size() != expected.size())
637 std::cout <<
" " << what <<
": " << out.size()
638 <<
" entries against " << expected.size() << std::endl;
641 TData norm = std::numeric_limits<TData>::epsilon(), err = 0;
642 for (
size_t i = 0; i < out.size(); ++i)
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());
649 if (err > tol * norm)
651 std::cout <<
" " << what <<
": max error " << err
652 <<
" against max entry " << norm << std::endl;
663#define TEST_FUSEDELMT_FIXTURE(type, policy, viscous, filename) \
665 : public TestFusedElmt<Nektar::UnitTests::policy, viscous> \
670 meshName = filename; \
681#define TEST_FUSEDELMT_CASE(test_name, test, tol) \
682 BOOST_FIXTURE_TEST_CASE(test_name, test) \
684 std::cout << std::string("Run: ") + std::string(#test_name) \
686 const bool orderZero = !HasDeformedQuad(); \
687 for (const auto type : \
688 {SpatialDomains::eModified, SpatialDomains::eOrthogonal}) \
690 const int minModes = \
691 type == SpatialDomains::eOrthogonal && orderZero ? 1 : 2; \
692 for (int nmodes = minModes; nmodes <= 9; ++nmodes) \
694 for (const int npts : {-1, nmodes + 1}) \
696 const bool match = RunExpansion(type, nmodes, npts, tol); \
699 std::cout << "Failed: type " << type << ", modes " \
700 << nmodes << ", points " << npts \
711#define TEST_FUSEDELMT_DEFAULT_CASE(test_name, test) \
712 BOOST_FIXTURE_TEST_CASE(test_name, test) \
714 std::cout << std::string("Run: ") + std::string(#test_name) \
716 for (int nmodes = 2; nmodes <= 9; ++nmodes) \
719 DefaultImplementation(SpatialDomains::eModified, nmodes)); \
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...
SpatialDomains::MeshGraphSharedPtr graph
std::vector< NekDouble > grad
A block means a group of elements of identical shape, basis and order. BlockAttributes stores the mos...
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...
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 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
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...
std::shared_ptr< MultiRegions::ExpList > fixt_explist
LibUtilities::SessionReaderSharedPtr session
Collections::CollectionVector GetCollections(MultiRegions::ExpListSharedPtr expansionList)
@ eDeformed
Geometry is curved or has non-constant factors.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr