35#define BOOST_TEST_MODULE TestHOutflowBndCond
39#include <boost/test/included/unit_test.hpp>
66 int argc = boost::unit_test::framework::master_test_suite().argc;
67 char **argv = boost::unit_test::framework::master_test_suite().argv;
68 MPI_Init(&argc, &argv);
84template <
unsigned int NDIM>
85void CheckPointFormula(
const std::array<double, NDIM> &u,
86 const std::array<double, NDIM * NDIM> &gradient,
87 const std::array<double, NDIM> &normal,
88 const std::array<double, NDIM> &fb,
const double kinvis,
89 const double theta,
const double alpha1,
90 const double alpha2,
const double U0,
const double delta)
92 double pressure = 0.0, divergence = 0.0;
93 std::array<double, NDIM> energyMinusFb{}, velocityFlux{};
95 HOutflowPressureKernel<NektarSpaces::Serial, NDIM>(
96 1, 1, u.data(), gradient.data(), normal.data(), fb.data(), kinvis, 1.0,
97 theta, alpha1, alpha2, U0, delta, &pressure, energyMinusFb.data(),
100 double un = 0.0, u2 = 0.0, nGradUn = 0.0, expectedDiv = 0.0;
101 for (
unsigned int d = 0;
d < NDIM; ++
d)
103 un += normal[
d] * u[
d];
105 expectedDiv += gradient[
d * NDIM +
d];
106 for (
unsigned int j = 0; j < NDIM; ++j)
108 nGradUn += normal[
d] * gradient[
d * NDIM + j] * normal[j];
111 const double S0 = 0.5 * (1.0 - std::tanh(un / (U0 * delta)));
112 const double k1 = 0.5 * (theta + alpha2);
113 const double k2 = 0.5 * (1.0 - theta + alpha1);
114 double energyNormal = 0.0;
115 for (
unsigned int d = 0;
d < NDIM; ++
d)
117 const double expectedEnergy =
118 (k1 * u2 * normal[
d] + k2 * un * u[
d]) * S0 - fb[d];
119 BOOST_TEST(energyMinusFb[d] == expectedEnergy,
120 boost::test_tools::tolerance(1.0e-13));
121 energyNormal += normal[
d] * expectedEnergy;
123 const double expectedPressure = kinvis * nGradUn - energyNormal;
124 BOOST_TEST(divergence == expectedDiv,
125 boost::test_tools::tolerance(1.0e-13));
126 BOOST_TEST(pressure == expectedPressure,
127 boost::test_tools::tolerance(1.0e-13));
129 HOutflowVelocityKernel<NektarSpaces::Serial, NDIM>(
130 1, 1, &pressure, normal.data(), energyMinusFb.data(), &divergence,
131 kinvis, velocityFlux.data(), 0);
132 for (
unsigned int d = 0;
d < NDIM; ++
d)
134 const double expected =
135 ((
pressure - kinvis * expectedDiv) * normal[d] + energyMinusFb[d]) /
137 BOOST_TEST(velocityFlux[d] == expected,
138 boost::test_tools::tolerance(1.0e-13));
152 const auto &suite = boost::unit_test::framework::master_test_suite();
154 const std::string execStr(suite.argv[1]);
155 const std::string implStr(suite.argv[2]);
156 const std::string meshName(suite.argv[3]);
159 char **argv =
new char *[argc + 1];
160 argv[0] = strdup(
"TestHOutflowBndCond");
161 argv[1] = strdup(meshName.c_str());
162 argv[2] = strdup((
"--opExecSpace=" + execStr).c_str());
163 argv[3] = strdup((
"--opImpl=" + implStr).c_str());
164 argv[argc] =
nullptr;
169 for (
int i = 0; i < argc; ++i)
181std::vector<double> FillAtTwoWidths(
184 const unsigned int interleaveWidth)
191 std::vector<double> initialValues(reference.
ToVector<
double>().size());
192 for (std::size_t i = 0; i < initialValues.size(); ++i)
194 initialValues[i] = 1.0 + 1.0e-3 * i;
198 return initialValues;
205void CheckAtTwoWidths(
208 const unsigned int interleaveWidth,
209 const std::vector<double> &initialValues,
const double tolerance)
211 for (
auto &block : interleaved.GetBlocks())
213 BOOST_TEST(block.GetInterleaveWidth() == interleaveWidth);
216 const auto referenceValues = reference.
ToVector<
double>();
217 const auto interleavedValues = interleaved.
ToVector<
double>();
218 BOOST_REQUIRE_EQUAL(interleavedValues.size(), referenceValues.size());
219 double maxChange = 0.0;
220 for (std::size_t i = 0; i < referenceValues.size(); ++i)
222 maxChange = std::max(maxChange,
223 std::abs(referenceValues[i] - initialValues[i]));
224 BOOST_TEST(std::abs(interleavedValues[i] - referenceValues[i]) <=
227 BOOST_TEST(maxChange > 1.0e-2);
231BOOST_AUTO_TEST_SUITE(TestHOutflowBndCond)
235 CheckPointFormula<2>({1.25, -0.2}, {0.3, -0.4, 0.1, -0.15}, {1.0, 0.0},
236 {0.17, -0.08}, 0.025, 1.0, 0.0, 0.0, 1.0, 1.0 / 20.0);
241 CheckPointFormula<2>({-0.6, 0.7}, {-0.2, 0.35, -0.45, 0.25}, {1.0, 0.0},
242 {-0.11, 0.09}, 0.04, 0.35, 0.4, 0.2, 1.7, 0.12);
247 CheckPointFormula<3>({-0.35, 0.4, -0.15},
248 {0.2, -0.1, 0.3, -0.25, 0.15, 0.05, 0.4, -0.2, -0.3},
249 {0.0, 1.0, 0.0}, {0.03, -0.07, 0.12}, 0.015, 0.8, 0.1,
262 const auto &suite = boost::unit_test::framework::master_test_suite();
269 auto session = CreateSession();
271 auto velocityExpList =
274 auto pressureExpList =
277 velocityExpList->SetDataWarehouse();
278 pressureExpList->SetDataWarehouse();
280 const std::vector<std::string> velocityComponents = {
"u",
"v"};
281 const std::vector<std::string> pressureComponents = {
"p"};
282 const auto velocityPhysBlocks =
285 MultiRegions::UnitInterleaveWidths(velocityExpList));
290 "manufactured velocity", velocityPhysBlocks, velocityComponents, 1);
292 for (
unsigned int blk = 0; blk < velocity.
GetBlocks().size(); ++blk)
294 auto &velocityBlock = velocity.
GetBlocks()[blk];
296 velocityBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
298 const auto &expansions = collection.GetExpVector();
299 for (
unsigned int e = 0; e < expansions.size(); ++e)
304 expansions[e]->GetCoords(x, y, z);
305 for (
unsigned int p = 0; p < velocityBlock.GetNumData(); ++p)
307 const auto id = e * velocityBlock.GetNumData() + p;
308 velocityPtr[
id] = 2.0 * x[p] + 3.0 * y[p] + 1.0;
309 velocityPtr[velocityBlock.CompSize() +
id] =
310 -x[p] + 4.0 * y[p] - 2.0;
315 constexpr double kinvis = 0.3;
316 constexpr double scale = 1.0;
317 constexpr double fb0 = 0.1, fb1 = -0.2;
321 op->SetVelocityContext(velocityExpList, velocityComponents);
322 op->SetKinvis(kinvis);
327 const auto &bndExpansions = pressureExpList->GetBndCondExpansions();
328 const auto &bndConditions = pressureExpList->GetBndConditions();
329 const auto &localToGlobal = pressureExpList->GetLocalToGlobalMap();
330 const auto &coeffMap = localToGlobal->GetBndCondCoeffsToLocalCoeffsMap();
331 const auto &coeffSign = localToGlobal->GetBndCondCoeffsToLocalCoeffsSign();
332 const bool signChange = localToGlobal->GetSignChange();
335 unsigned int bndCoeffOffset = 0;
336 bool foundRegion =
false;
337 for (
unsigned int n = 0; n < bndExpansions.size(); ++n)
339 auto bndExp = bndExpansions[n];
340 if (bndConditions[n]->GetBoundaryConditionType() ==
342 IsHOutflow(bndConditions[n]))
345 const auto npts = bndExp->GetTotPoints();
347 bndExp->GetCoords(x, y, z);
349 pressureExpList->GetBoundaryNormals(n, normals);
352 for (
unsigned int p = 0; p < npts; ++p)
354 const double ux = 2.0 * x[p] + 3.0 * y[p] + 1.0;
355 const double uy = -x[p] + 4.0 * y[p] - 2.0;
356 const double u[2] = {ux, uy};
357 const double grad[2][2] = {{2.0, 3.0}, {-1.0, 4.0}};
358 const double nrm[2] = {normals[0][p], normals[1][p]};
359 const double fb[2] = {fb0, fb1};
361 double un = 0.0, u2 = 0.0, nGradUn = 0.0;
362 for (
unsigned int d = 0; d < 2; ++d)
366 for (
unsigned int j = 0; j < 2; ++j)
368 nGradUn += nrm[d] *
grad[d][j] * nrm[j];
371 const double S0 = 0.5 * (1.0 - std::tanh(un / (1.0 * 0.05)));
372 const double k1 = 0.5 * (1.0 + 0.0);
373 const double k2 = 0.5 * (1.0 - 1.0 + 0.0);
374 double energyNormal = 0.0;
375 for (
unsigned int d = 0; d < 2; ++d)
378 (k1 * u2 * nrm[d] + k2 * un * u[d]) * S0 - fb[d];
379 energyNormal += nrm[d] * value;
381 expectedPhys[p] = kinvis * nGradUn - energyNormal;
385 bndExp->FwdTrans(expectedPhys, bndCoeffsOut);
386 for (
unsigned int c = 0; c < bndExp->GetNcoeffs(); ++c)
388 const auto bndCoeff = bndCoeffOffset + c;
389 const auto localCoeff = coeffMap[bndCoeff];
390 expectedCoeffs[localCoeff] =
391 (signChange ? coeffSign[bndCoeff] : 1.0) * bndCoeffsOut[c];
394 bndCoeffOffset += bndExp->GetNcoeffs();
396 BOOST_REQUIRE_MESSAGE(foundRegion,
397 "No HOutflow pressure Dirichlet region found in "
398 "the extraction test fixture.");
401 "expected pressure coeffs",
404 MultiRegions::UnitInterleaveWidths(pressureExpList)),
405 pressureComponents, 1);
407 for (
unsigned int blk = 0; blk < expected.
GetBlocks().size(); ++blk)
409 auto &expectedBlock = expected.
GetBlocks()[blk];
411 expectedBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
413 const auto &expansions = collection.GetExpVector();
414 for (
unsigned int e = 0; e < expansions.size(); ++e)
417 pressureExpList->GetCoeff_Offset(expansions[e]->GetElmtId());
418 std::copy(expectedCoeffs.begin() + offset,
419 expectedCoeffs.begin() + offset +
420 expectedBlock.GetNumData(),
421 expectedPtr + e * expectedBlock.GetNumData());
425 const auto expectedValues = expected.
ToVector<
double>();
426 const auto maxExpected =
427 *std::max_element(expectedValues.begin(), expectedValues.end(),
428 [](
const double lhs,
const double rhs) {
429 return std::abs(lhs) < std::abs(rhs);
431 BOOST_TEST(std::abs(maxExpected) > 0.1);
436 const auto execSpace =
438 const unsigned int interleaveWidth =
439 NektarSpaces::GetVectorWidth<double>(execSpace);
444 MultiRegions::UnitInterleaveWidths(pressureExpList)),
445 pressureComponents, 1);
446 for (
const unsigned int velocityWidth : {1u, interleaveWidth, 1u})
449 op->UpdatePressure(velocity);
451 op->GetPressureDirichletOp()->Apply(pressureCoeffs);
453 const auto actualValues = pressureCoeffs.
ToVector<
double>();
454 BOOST_REQUIRE_EQUAL(actualValues.size(), expectedValues.size());
455 for (std::size_t i = 0; i < actualValues.size(); ++i)
457 BOOST_CHECK_MESSAGE(std::abs(actualValues[i] - expectedValues[i]) <
459 "velocity at width " << velocityWidth);
471 const auto &suite = boost::unit_test::framework::master_test_suite();
478 auto session = CreateSession();
480 auto velocityExpList =
483 auto pressureExpList =
486 velocityExpList->SetDataWarehouse();
487 pressureExpList->SetDataWarehouse();
489 const std::vector<std::string> velocityComponents = {
"u",
"v"};
490 const std::vector<std::string> pressureComponents = {
"p"};
498 MultiRegions::UnitInterleaveWidths(velocityExpList)),
499 velocityComponents, 1);
501 for (
auto &velocityBlock : velocity.
GetBlocks())
504 velocityBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
505 const size_t nPoints =
506 velocityBlock.GetNumElements() * velocityBlock.GetNumData();
507 for (
unsigned int d = 0; d < velocityComponents.size(); ++d)
509 std::fill(velocityPtr + d * velocityBlock.CompSize(),
510 velocityPtr + d * velocityBlock.CompSize() + nPoints,
518 MultiRegions::UnitInterleaveWidths(pressureExpList)),
519 pressureComponents, 1);
521 for (
unsigned int blk = 0; blk < pressure.GetBlocks().size(); ++blk)
523 auto &pressureBlock = pressure.GetBlocks()[blk];
525 pressureBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
527 const auto &expansions = collection.GetExpVector();
528 for (
unsigned int e = 0; e < expansions.size(); ++e)
533 expansions[e]->GetCoords(x, y, z);
534 for (
unsigned int p = 0; p < pressureBlock.GetNumData(); ++p)
536 pressurePtr[e * pressureBlock.GetNumData() + p] =
537 1.0 + x[p] + 2.0 * y[p];
542 constexpr double kinvis = 0.3;
545 op->SetVelocityContext(velocityExpList, velocityComponents);
546 op->SetKinvis(kinvis);
547 op->UpdatePressure(velocity);
549 const unsigned int interleaveWidth = NektarSpaces::GetVectorWidth<double>(
555 const auto pressureCoeffBlocks =
558 MultiRegions::UnitInterleaveWidths(pressureExpList));
560 "pressure coeffs at width 1", pressureCoeffBlocks, pressureComponents,
563 "interleaved pressure coeffs", pressureCoeffBlocks, pressureComponents,
565 const auto pressureInitialValues = FillAtTwoWidths(
566 pressureReference, pressureInterleaved, interleaveWidth);
567 auto pressureOp = op->GetPressureDirichletOp();
568 for (
unsigned int pass = 0; pass < 2; ++pass)
570 pressureOp->Apply(pressureReference);
571 pressureOp->Apply(pressureInterleaved);
573 CheckAtTwoWidths(pressureReference, pressureInterleaved, interleaveWidth,
574 pressureInitialValues, 1.0e-12);
578 op->SetPressure(pressure);
579 const auto velocityCoeffBlocks =
582 MultiRegions::UnitInterleaveWidths(velocityExpList));
584 "velocity coeffs at width 1", velocityCoeffBlocks, velocityComponents,
587 "interleaved velocity coeffs", velocityCoeffBlocks, velocityComponents,
589 const auto velocityInitialValues = FillAtTwoWidths(
590 velocityReference, velocityInterleaved, interleaveWidth);
591 auto velocityOp = op->GetVelocityNeumannOp();
592 for (
unsigned int pass = 0; pass < 2; ++pass)
594 velocityOp->Apply(velocityReference);
595 velocityOp->Apply(velocityInterleaved);
597 CheckAtTwoWidths(velocityReference, velocityInterleaved, interleaveWidth,
598 velocityInitialValues, 0.0);
604 auto addToPressure = [&](
const double shift) {
605 for (
auto &pressureBlock : pressure.GetBlocks())
609 .template GetPtr<NektarSpaces::HostSpace, ReadWrite>();
610 const size_t nPoints =
611 pressureBlock.GetNumElements() * pressureBlock.GetNumData();
612 for (
size_t p = 0; p < nPoints; ++p)
614 pressurePtr[p] += shift;
618 auto applyVelocity = [&]() {
620 "velocity forcing", velocityCoeffBlocks, velocityComponents, 1);
622 velocityOp->Apply(forcing);
625 const auto forcing = applyVelocity();
627 const auto shiftedForcing = applyVelocity();
629 const auto restoredForcing = applyVelocity();
630 BOOST_REQUIRE_EQUAL(shiftedForcing.size(), forcing.size());
631 BOOST_REQUIRE_EQUAL(restoredForcing.size(), forcing.size());
632 double maxShiftChange = 0.0, maxRestoredChange = 0.0;
633 for (std::size_t i = 0; i < forcing.size(); ++i)
636 std::max(maxShiftChange, std::abs(shiftedForcing[i] - forcing[i]));
637 maxRestoredChange = std::max(maxRestoredChange,
638 std::abs(restoredForcing[i] - forcing[i]));
640 BOOST_TEST(maxShiftChange > 1.0e-2);
641 BOOST_TEST(maxRestoredChange <= 1.0e-12);
644BOOST_AUTO_TEST_SUITE_END()
LibUtilities::SessionReaderSharedPtr session
SpatialDomains::MeshGraphSharedPtr graph
BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration)
std::vector< NekDouble > grad
static std::shared_ptr< HOutflowBndCondOp< TData > > Create(const MultiRegions::ExpListSharedPtr &pressureExpansionList, const std::vector< std::string > &pressureComponents, const std::string &execStr="")
A Field represents expansion data to be operated on.
void CopyVector(const std::vector< TDataIn, Alloc > &array)
Templated copy method. This method copies data from a std::vector laid out as ToVector() returns it,...
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
std::vector< TDataOut, Alloc > ToVector()
Copy the data to a std::vector, component by component and element by element, without padding,...
void Initialize(const TData val, const unsigned int streamID=0)
Templated initialize method: set every entry to val, but the padding, which is zero.
void SetInterleaveWidth(const unsigned int &interleaveWidth)
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.
Common base class of the operator family interfaces: holds the expansion list, component names and da...
static std::string GetOpExecSpace(std::shared_ptr< LibUtilities::SessionReader > session, const bool warn=true)
Return the execution space name ("opExecSpace") for an operator.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
std::shared_ptr< SessionReader > SessionReaderSharedPtr
LibUtilities::SessionReaderSharedPtr CreateSession()
template std::vector< BlockAttributes< FieldState::Coeff > > GetBlockAttributes< double, FieldState::Coeff >(const MultiRegions::ExpListSharedPtr explist, const std::vector< unsigned int > &interleave_widths)
template std::vector< BlockAttributes< FieldState::Phys > > GetBlockAttributes< double, FieldState::Phys >(const MultiRegions::ExpListSharedPtr explist, const std::vector< unsigned int > &interleave_widths)
Collections::Collection GetCollection(MultiRegions::ExpListSharedPtr expansionList, unsigned int block_idx)
std::shared_ptr< BoundaryConditionBase > BoundaryConditionShPtr
bool IEquals(std::string_view a, std::string_view b)
std::vector< double > d(NPUPPER *NPUPPER)
BOOST_AUTO_TEST_CASE(TestCanGetRawPtr)