Nektar++
Loading...
Searching...
No Matches
TestAdvectionWeakDG.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestAdvectionWeakDG.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
37#include <SolverCore/Advection/AdvectionWeakDG/AdvectionWeakDGOp.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
50template <typename TData>
52 : public TestOp<TData, FieldState::Phys, FieldState::Phys,
53 MultiRegions::DisContField>
54{
55public:
57
59 {
60
61 // Set initial conditions from the quadrature point coordinates. The
62 // coordinates do not move when the mesh is partitioned, so serial and
63 // parallel runs start from the same field and must finish with the
64 // same one; a value taken from the storage index instead would differ
65 // between rank counts, and its magnitude would grow with the element
66 // count, which is what forced the loose tolerances this test used to
67 // carry.
68 for (unsigned blk = 0, eid = 0; blk < this->fixt_in->GetBlocks().size();
69 ++blk)
70 {
71 auto &block = this->fixt_in->GetBlocks()[blk];
72 auto inptr =
73 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
74
75 const auto cdim = this->fixt_explist->GetCoordim(0);
76
77 for (unsigned n = 0; n < this->fixt_in->GetNumComponents() *
78 this->fixt_in->GetNumHomoModes();
79 ++n)
80 {
81 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
82 {
83 auto coords =
84 this->fixt_explist->GetExp(eid + el)->GetCoords();
85
86 for (unsigned phys = 0; phys < block.GetNumData();
87 ++phys, ++cnt)
88 {
89 TData v = 1.0 + TData(n);
90 for (unsigned d = 0; d < cdim; ++d)
91 {
92 v += TData(d + 1) * coords[d][phys];
93 }
94 inptr[cnt] = v;
95 }
96 }
97 inptr += block.CompSize();
98 }
99
100 eid += block.GetNumElements();
101 }
102
103 size_t ndim = this->fixt_explist->GetCoordim(0);
104 size_t nphys = this->fixt_explist->GetTotPoints();
105
106 auto blocks_in = GetBlockAttributes<TData, FieldState::Phys>(
107 this->fixt_explist,
108 MultiRegions::UnitInterleaveWidths(this->fixt_explist));
110 "f_advectVel", blocks_in, ndim, 1);
112 std::move(f_advectVel));
113
114 for (unsigned blk = 0; blk < fixt_advectVel->GetBlocks().size(); ++blk)
115 {
116
117 auto &block = fixt_advectVel->GetBlocks()[blk];
118 auto inptr =
119 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
120 for (unsigned nc = 0; nc < block.GetNumComponents(); ++nc)
121 {
122 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
123 {
124 for (unsigned phys = 0; phys < block.GetNumData();
125 ++phys, ++cnt)
126 {
127 // inptr[cnt] = phys;
128 inptr[cnt] = 1 + nc;
129 }
130 }
131 inptr += block.CompSize();
132 }
133 }
134
135 // offset advection velocities
138 for (int i = 0; i < ndim; ++i)
139 {
140 m_advectVel[i] = tmp + i * nphys;
141 }
142
143 auto blocks_trace = GetBlockAttributes<TData, FieldState::Phys>(
144 this->fixt_explist->GetTrace(),
145 MultiRegions::UnitInterleaveWidths(this->fixt_explist->GetTrace()));
146
148 "f_traceAdvVel", blocks_trace, ndim, 1);
150 std::move(f_traceAdvVel));
151
152 for (unsigned blk = 0; blk < fixt_traceAdvVel->GetBlocks().size();
153 ++blk)
154 {
155
156 auto &block = fixt_traceAdvVel->GetBlocks()[blk];
157 auto inptr =
158 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
159 for (unsigned nc = 0; nc < block.GetNumComponents(); ++nc)
160 {
161 for (size_t el = 0, cnt = 0; el < block.GetNumElements(); ++el)
162 {
163 for (unsigned phys = 0; phys < block.GetNumData();
164 ++phys, ++cnt)
165 {
166 // inptr[cnt] = phys;
167 inptr[cnt] = 1 + nc;
168 }
169 }
170 inptr += block.CompSize();
171 }
172 }
173
175
176 // Compute expected solution.
178 }
179
181 {
182 this->fixt_explist->SetDataWarehouse();
183 this->fixt_explist->GetTrace()->SetDataWarehouse();
184
185 std::string riemName;
186 this->session->LoadSolverInfo("UpwindType", riemName, "Upwind");
187
188 auto AdvectOp = AdvectionWeakDGOp<TData>::Create(
189 this->fixt_explist, this->session->GetVariables());
190 // add prefix to name to match operator definition
191 riemName = "ScalarTraceFlux" + riemName;
193 this->fixt_explist, this->session->GetVariables(), riemName);
194
195 // Set up trace advection velocity on trace for upwind
196 // solver
197 size_t ncoordim = this->fixt_explist->GetCoordim(0);
199 "traceAdvVel",
201 this->fixt_explist),
202 ncoordim, 1);
203
204 // extract advection trace
205 auto physTraceExtractOp = PhysTraceExtractOp<double>::Create(
207 physTraceExtractOp->Apply(*this->fixt_advectVel, traceAdvVel);
208 // set up average value along trace
209 TraceFlux->SetTraceAdvVel(traceAdvVel);
210
211 // finally append TraceFlux to AdvectOp
212 AdvectOp->SetTraceFlux(TraceFlux);
213
214 auto VolumeFluxOp = LinearAdvVolumeFluxOp<TData>::Create(
215 this->fixt_explist, this->session->GetVariables());
216 VolumeFluxOp->SetAdvVel(*this->fixt_advectVel);
217 AdvectOp->SetVolumeFluxOp(VolumeFluxOp);
218
219 AdvectOp->Apply(*this->fixt_in, *this->fixt_out);
220 }
221
223 {
224 unsigned numComp = this->fixt_in->GetNumComponents();
225 size_t nphys = this->fixt_explist->GetTotPoints();
226
227 std::string advName;
228 std::string riemName;
229 this->session->LoadSolverInfo("AdvectionType", advName, "WeakDG");
230 auto advObject =
232 advObject->SetFluxVector(&TestAdvectionWeakDG::GetFluxVector, this);
233
234 this->session->LoadSolverInfo("UpwindType", riemName, "Upwind");
235 auto riemannSolver =
237 riemName, this->session);
238 riemannSolver->SetScalar("Vn", &TestAdvectionWeakDG::GetNormalVel,
239 this);
240
242 numComp);
243
244 std::vector<std::string> variables = this->session->GetVariables();
246 ASSERTL0(variables.size() >= numComp,
247 "numComp is larger than the number of variales");
248
249 for (unsigned i = 0; i < numComp; ++i)
250 {
251 // in case of orthogonal expansions reset expansion info for legacy
252 // code to work
253 if (!this->fixt_explist->GetExp(0)->IsBoundaryInteriorExpansion())
254 {
255 graph->ResetExpansionInfoToModified(variables[i]);
256 }
257 exp[i] =
259 this->session, graph, variables[i], true, true,
261 }
262
263 advObject->SetRiemannSolver(riemannSolver);
264 advObject->InitObject(this->session, exp);
265
266 Array<OneD, TData> in = this->fixt_in->ToArray();
267 Array<OneD, TData> out(nphys * numComp, 0.0);
268
269 Array<OneD, Array<OneD, TData>> inarray(numComp);
270 Array<OneD, Array<OneD, TData>> outarray(numComp);
271
272 for (unsigned i = 0; i < numComp; ++i)
273 {
274 inarray[i] = Array<OneD, TData>(nphys, 0.0);
275 for (size_t j = 0; j < nphys; j++)
276 {
277 inarray[i][j] = in[i * nphys + j];
278 }
279 outarray[i] = Array<OneD, TData>(nphys, 0.0);
280 }
281
282 advObject->Advect(numComp, exp, m_advectVel, inarray, outarray, 0);
283
284 for (unsigned i = 0; i < numComp; ++i)
285 {
286 for (size_t j = 0; j < nphys; ++j)
287 {
288 out[i * nphys + j] = outarray[i][j];
289 }
290 }
291
292 this->fixt_expected->template CopyArray<NektarSpaces::HostSpace>(out);
293 }
294
296 {
297 const unsigned coordDim = this->fixt_explist->GetCoordim(0);
298 const size_t nTracePts = this->fixt_explist->GetTrace()->GetTotPoints();
299
300 if (m_normalVel.size() == 0)
301 {
302 m_normalVel = Array<OneD, TData>(nTracePts, 0.0);
303 // Gather normals
304 Array<OneD, Array<OneD, TData>> traceNormals(coordDim);
305 for (unsigned i = 0; i < coordDim; ++i)
306 {
307 traceNormals[i] = Array<OneD, TData>(nTracePts, 0.0);
308 }
309
310 this->fixt_explist->GetTrace()->GetNormals(traceNormals);
311
312 // v = (1,1,...), so Vn = n · v = sum_k n_k
313 for (unsigned i = 0; i < coordDim; ++i)
314 {
315 for (size_t j = 0; j < nTracePts; ++j)
316 {
317 m_normalVel[j] +=
318 traceNormals[i][j] * m_traceAdvVel[i * nTracePts + j];
319 }
320 }
321 }
322
323 return m_normalVel;
324 }
325
328 {
329 ASSERTL1(flux[0].size() == m_advectVel.size(),
330 "Dimension of flux array and velocity array do not match");
331
332 const int nq = this->fixt_explist->GetNpoints();
333
334 for (int i = 0; i < flux.size(); ++i)
335 {
336 for (int j = 0; j < flux[0].size(); ++j)
337 {
338 for (int k = 0; k < nq; ++k)
339 {
340 flux[i][j][k] = physfield[i][k] * m_advectVel[j][k];
341 }
342 }
343 }
344 }
345
346public:
352};
353
354// clang-format off
355#if defined(NEKTAR_ENABLE_SINGLE_PRECISION)
356#define TESTFLOAT(type, filename) \
357 class type##float : public TestAdvectionWeakDG<float> \
358 { \
359 public: \
360 type##float() \
361 { \
362 meshName = filename; \
363 } \
364 };
365#else
366#define TESTFLOAT(type, filename)
367#endif
368#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
369#define TESTDOUBLE(type, filename) \
370 class type : public TestAdvectionWeakDG<double> \
371 { \
372 public: \
373 type() \
374 { \
375 meshName = filename; \
376 } \
377 };
378#else
379#define TESTDOUBLE(type, filename)
380#endif
381#define TEST(type, filename) \
382 TESTFLOAT(type, filename) \
383 TESTDOUBLE(type, filename)
384// clang-format on
385
386TEST(Seg, "run/Seg_Dir.xml")
387
388TEST(SegOrtho, "run/Seg_Ortho_Dir.xml")
389
390TEST(Tri, "run/Tri_Dir.xml")
391
392TEST(TriOrtho, "run/Tri_Ortho_Dir.xml")
393
394TEST(Quad, "run/Quad_Dir.xml")
395
396TEST(Quad_Per, "run/Quad_Per.xml")
397
398TEST(QuadOrtho_Per, "run/Quad_Ortho_Per.xml")
399
400TEST(TriQuad_VarP, "run/TriQuad_varP_Dir.xml")
401
402TEST(Prism_VarP, "run/Prism_varP_Dir.xml")
403
404TEST(Hex, "run/Hex_Dir.xml")
405
406TEST(HexOrtho, "run/Hex_Ortho_Dir.xml")
407
408TEST(Hex_Per, "run/Hex_Per.xml")
409
410TEST(Prism, "run/Prism_Dir.xml")
411
412TEST(PrismOrtho, "run/Prism_Ortho_Dir.xml")
413
414TEST(Pyr, "run/Pyr_Dir.xml")
415
416TEST(PyrOrtho, "run/Pyr_Ortho_Dir.xml")
417
418TEST(Tet, "run/Tet_Dir.xml")
419
420TEST(TetOrtho, "run/Tet_Ortho_Dir.xml")
421
422TEST(CubeAllElements, "run/cube_all_elements_dir.xml")
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Public interface of the PhysTraceExtract element operator, which interpolates a volume physical-space...
#define TEST(type, filename)
SpatialDomains::MeshGraphSharedPtr graph
SpatialDomains::Geometry * el
A Field represents expansion data to be operated on.
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,...
std::vector< std::string > GetComponentNames() const
Gets a copy of the components names for a vector field.
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< LinearAdvVolumeFluxOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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 MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
void GetFluxVector(const Array< OneD, Array< OneD, TData > > &physfield, Array< OneD, Array< OneD, Array< OneD, TData > > > &flux)
TestAdvectionWeakDG()=default
LibUtilities::Field< TData, FieldState::Phys > * fixt_advectVel
Array< OneD, TData > m_traceAdvVel
Array< OneD, Array< OneD, TData > > m_advectVel
Array< OneD, TData > m_normalVel
LibUtilities::Field< TData, FieldState::Phys > * fixt_traceAdvVel
const Array< OneD, TData > & GetNormalVel()
template std::vector< BlockAttributes< FieldState::Phys > > GetLocTraceBlockAttributes< double, FieldState::Phys >(const MultiRegions::ExpListSharedPtr explist, const unsigned interleave_width, const bool useGLLTracePoints)
AdvectionFactory & GetAdvectionFactory()
Gets the factory for initialising advection objects.
Definition Advection.cpp:43
RiemannSolverFactory & GetRiemannSolverFactory()