Nektar++
Loading...
Searching...
No Matches
Classes | Macros | Functions
TestHOutflowBndCond.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <LibUtilities/BasicUtils/StringUtils.hpp>
#include <LibUtilities/BasicUtils/SessionReader.h>
#include <MultiRegions/ContField.h>
#include <SpatialDomains/MeshGraphIO.h>
#include <IncNavierStokesSolverRedesign/BoundaryConditions/HOutflowBndCond/HOutflowBndCondKernels.hpp>
#include <IncNavierStokesSolverRedesign/BoundaryConditions/HOutflowBndCond/HOutflowBndCondOp.hpp>
#include <LibUtilities/Backends/Backends.hpp>
#include <LibUtilities/BasicUtils/Field/Field.hpp>

Go to the source code of this file.

Classes

struct  GlobalConfiguration
 

Macros

#define BOOST_TEST_MODULE   TestHOutflowBndCond
 
#define NOMINMAX
 

Functions

 BOOST_TEST_GLOBAL_CONFIGURATION (GlobalConfiguration)
 
 BOOST_AUTO_TEST_CASE (outgoing_2d_default_parameters_nonzero_fb)
 
 BOOST_AUTO_TEST_CASE (backflow_2d_nondefault_energy_parameters)
 
 BOOST_AUTO_TEST_CASE (backflow_3d_divergence_term)
 
 BOOST_AUTO_TEST_CASE (build_boundary_element_data_manufactured_multi_element)
 
 BOOST_AUTO_TEST_CASE (apply_interleaved_matches_width_one)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   TestHOutflowBndCond

Definition at line 35 of file TestHOutflowBndCond.cpp.

◆ NOMINMAX

#define NOMINMAX

Definition at line 38 of file TestHOutflowBndCond.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/5]

BOOST_AUTO_TEST_CASE ( apply_interleaved_matches_width_one  )

Definition at line 469 of file TestHOutflowBndCond.cpp.

470{
471 const auto &suite = boost::unit_test::framework::master_test_suite();
472 if (suite.argc < 4)
473 {
474 // Run without a session, for the tests above only.
475 return;
476 }
477
478 auto session = CreateSession();
480 auto velocityExpList =
482 session, graph, "u", true, false, Collections::eNoCollection);
483 auto pressureExpList =
485 session, graph, "p", true, false, Collections::eNoCollection);
486 velocityExpList->SetDataWarehouse();
487 pressureExpList->SetDataWarehouse();
488
489 const std::vector<std::string> velocityComponents = {"u", "v"};
490 const std::vector<std::string> pressureComponents = {"p"};
491
492 // An outflow velocity, u . n = 2 on the regions, so that S0 = 0, and a
493 // solved pressure varying along them.
495 "outflow velocity",
497 velocityExpList,
498 MultiRegions::UnitInterleaveWidths(velocityExpList)),
499 velocityComponents, 1);
500 velocity.Initialize<NektarSpaces::HostSpace>(0.0);
501 for (auto &velocityBlock : velocity.GetBlocks())
502 {
503 auto velocityPtr =
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)
508 {
509 std::fill(velocityPtr + d * velocityBlock.CompSize(),
510 velocityPtr + d * velocityBlock.CompSize() + nPoints,
511 -2.0);
512 }
513 }
515 "solved pressure",
517 pressureExpList,
518 MultiRegions::UnitInterleaveWidths(pressureExpList)),
519 pressureComponents, 1);
520 pressure.Initialize<NektarSpaces::HostSpace>(0.0);
521 for (unsigned int blk = 0; blk < pressure.GetBlocks().size(); ++blk)
522 {
523 auto &pressureBlock = pressure.GetBlocks()[blk];
524 auto pressurePtr =
525 pressureBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
526 auto collection = GetCollection(pressureExpList, blk);
527 const auto &expansions = collection.GetExpVector();
528 for (unsigned int e = 0; e < expansions.size(); ++e)
529 {
530 Array<OneD, double> x(pressureBlock.GetNumData());
531 Array<OneD, double> y(pressureBlock.GetNumData());
532 Array<OneD, double> z(pressureBlock.GetNumData(), 0.0);
533 expansions[e]->GetCoords(x, y, z);
534 for (unsigned int p = 0; p < pressureBlock.GetNumData(); ++p)
535 {
536 pressurePtr[e * pressureBlock.GetNumData() + p] =
537 1.0 + x[p] + 2.0 * y[p];
538 }
539 }
540 }
541
542 constexpr double kinvis = 0.3;
543 auto op =
544 HOutflowBndCondOp<double>::Create(pressureExpList, pressureComponents);
545 op->SetVelocityContext(velocityExpList, velocityComponents);
546 op->SetKinvis(kinvis);
547 op->UpdatePressure(velocity);
548
549 const unsigned int interleaveWidth = NektarSpaces::GetVectorWidth<double>(
551
552 // The vertex shared by the two regions is set from both, on the device
553 // in either order, with values equal up to the rounding of the velocity
554 // gradient.
555 const auto pressureCoeffBlocks =
557 pressureExpList,
558 MultiRegions::UnitInterleaveWidths(pressureExpList));
560 "pressure coeffs at width 1", pressureCoeffBlocks, pressureComponents,
561 1);
563 "interleaved pressure coeffs", pressureCoeffBlocks, pressureComponents,
564 1);
565 const auto pressureInitialValues = FillAtTwoWidths(
566 pressureReference, pressureInterleaved, interleaveWidth);
567 auto pressureOp = op->GetPressureDirichletOp();
568 for (unsigned int pass = 0; pass < 2; ++pass)
569 {
570 pressureOp->Apply(pressureReference);
571 pressureOp->Apply(pressureInterleaved);
572 }
573 CheckAtTwoWidths(pressureReference, pressureInterleaved, interleaveWidth,
574 pressureInitialValues, 1.0e-12);
575
576 // The velocity forcing sums the contributions to the shared vertex
577 // before adding them, so both widths agree exactly.
578 op->SetPressure(pressure);
579 const auto velocityCoeffBlocks =
581 velocityExpList,
582 MultiRegions::UnitInterleaveWidths(velocityExpList));
584 "velocity coeffs at width 1", velocityCoeffBlocks, velocityComponents,
585 1);
587 "interleaved velocity coeffs", velocityCoeffBlocks, velocityComponents,
588 1);
589 const auto velocityInitialValues = FillAtTwoWidths(
590 velocityReference, velocityInterleaved, interleaveWidth);
591 auto velocityOp = op->GetVelocityNeumannOp();
592 for (unsigned int pass = 0; pass < 2; ++pass)
593 {
594 velocityOp->Apply(velocityReference);
595 velocityOp->Apply(velocityInterleaved);
596 }
597 CheckAtTwoWidths(velocityReference, velocityInterleaved, interleaveWidth,
598 velocityInitialValues, 0.0);
599
600 // The velocity condition computes the flux from the pressure as it
601 // stands when it is applied: refilling the pressure in place, without
602 // setting it again, changes the forcing, and restoring the pressure
603 // restores the forcing.
604 auto addToPressure = [&](const double shift) {
605 for (auto &pressureBlock : pressure.GetBlocks())
606 {
607 auto pressurePtr =
608 pressureBlock
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)
613 {
614 pressurePtr[p] += shift;
615 }
616 }
617 };
618 auto applyVelocity = [&]() {
620 "velocity forcing", velocityCoeffBlocks, velocityComponents, 1);
621 forcing.Initialize<NektarSpaces::HostSpace>(0.0);
622 velocityOp->Apply(forcing);
623 return forcing.ToVector<double>();
624 };
625 const auto forcing = applyVelocity();
626 addToPressure(0.5);
627 const auto shiftedForcing = applyVelocity();
628 addToPressure(-0.5);
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)
634 {
635 maxShiftChange =
636 std::max(maxShiftChange, std::abs(shiftedForcing[i] - forcing[i]));
637 maxRestoredChange = std::max(maxRestoredChange,
638 std::abs(restoredForcing[i] - forcing[i]));
639 }
640 BOOST_TEST(maxShiftChange > 1.0e-2);
641 BOOST_TEST(maxRestoredChange <= 1.0e-12);
642}
LibUtilities::SessionReaderSharedPtr session
SpatialDomains::MeshGraphSharedPtr graph
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.
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...
Definition Operator.hpp:100
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
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::vector< double > p(NPUPPER)
std::vector< double > d(NPUPPER *NPUPPER)

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), Nektar::HOutflowBndCondOp< TData >::Create(), Nektar::Collections::eNoCollection, Nektar::MultiRegions::GetBlockAttributes< double, FieldState::Coeff >(), Nektar::MultiRegions::GetBlockAttributes< double, FieldState::Phys >(), Nektar::LibUtilities::Field< TData, TState >::GetBlocks(), Nektar::MultiRegions::GetCollection(), graph, Nektar::LibUtilities::Field< TData, TState >::Initialize(), Nektar::SpatialDomains::MeshGraphIO::Read(), session, and Nektar::LibUtilities::Field< TData, TState >::ToVector().

◆ BOOST_AUTO_TEST_CASE() [2/5]

BOOST_AUTO_TEST_CASE ( backflow_2d_nondefault_energy_parameters  )

Definition at line 239 of file TestHOutflowBndCond.cpp.

240{
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);
243}

◆ BOOST_AUTO_TEST_CASE() [3/5]

BOOST_AUTO_TEST_CASE ( backflow_3d_divergence_term  )

Definition at line 245 of file TestHOutflowBndCond.cpp.

246{
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,
250 0.25, 0.9, 0.08);
251}

◆ BOOST_AUTO_TEST_CASE() [4/5]

BOOST_AUTO_TEST_CASE ( build_boundary_element_data_manufactured_multi_element  )

Definition at line 260 of file TestHOutflowBndCond.cpp.

261{
262 const auto &suite = boost::unit_test::framework::master_test_suite();
263 if (suite.argc < 4)
264 {
265 // Run without a session, for the tests above only.
266 return;
267 }
268
269 auto session = CreateSession();
271 auto velocityExpList =
273 session, graph, "u", true, false, Collections::eNoCollection);
274 auto pressureExpList =
276 session, graph, "p", true, false, Collections::eNoCollection);
277 velocityExpList->SetDataWarehouse();
278 pressureExpList->SetDataWarehouse();
279
280 const std::vector<std::string> velocityComponents = {"u", "v"};
281 const std::vector<std::string> pressureComponents = {"p"};
282 const auto velocityPhysBlocks =
284 velocityExpList,
285 MultiRegions::UnitInterleaveWidths(velocityExpList));
286
287 // A linear velocity field, whose gradient PhysDerivOp reproduces to
288 // rounding error.
290 "manufactured velocity", velocityPhysBlocks, velocityComponents, 1);
291 velocity.Initialize<NektarSpaces::HostSpace>(0.0);
292 for (unsigned int blk = 0; blk < velocity.GetBlocks().size(); ++blk)
293 {
294 auto &velocityBlock = velocity.GetBlocks()[blk];
295 auto velocityPtr =
296 velocityBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
297 auto collection = GetCollection(velocityExpList, blk);
298 const auto &expansions = collection.GetExpVector();
299 for (unsigned int e = 0; e < expansions.size(); ++e)
300 {
301 Array<OneD, double> x(velocityBlock.GetNumData());
302 Array<OneD, double> y(velocityBlock.GetNumData());
303 Array<OneD, double> z(velocityBlock.GetNumData(), 0.0);
304 expansions[e]->GetCoords(x, y, z);
305 for (unsigned int p = 0; p < velocityBlock.GetNumData(); ++p)
306 {
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;
311 }
312 }
313 }
314
315 constexpr double kinvis = 0.3;
316 constexpr double scale = 1.0;
317 constexpr double fb0 = 0.1, fb1 = -0.2;
318
319 auto op =
320 HOutflowBndCondOp<double>::Create(pressureExpList, pressureComponents);
321 op->SetVelocityContext(velocityExpList, velocityComponents);
322 op->SetKinvis(kinvis);
323 op->SetScale(scale);
324
325 // The Dong pressure condition from the analytic velocity at the points
326 // of the region, projected and mapped onto the pressure coefficients.
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();
333
334 Array<OneD, double> expectedCoeffs(pressureExpList->GetNcoeffs(), 0.0);
335 unsigned int bndCoeffOffset = 0;
336 bool foundRegion = false;
337 for (unsigned int n = 0; n < bndExpansions.size(); ++n)
338 {
339 auto bndExp = bndExpansions[n];
340 if (bndConditions[n]->GetBoundaryConditionType() ==
342 IsHOutflow(bndConditions[n]))
343 {
344 foundRegion = true;
345 const auto npts = bndExp->GetTotPoints();
346 Array<OneD, double> x(npts), y(npts), z(npts, 0.0);
347 bndExp->GetCoords(x, y, z);
349 pressureExpList->GetBoundaryNormals(n, normals);
350
351 Array<OneD, double> expectedPhys(npts, 0.0);
352 for (unsigned int p = 0; p < npts; ++p)
353 {
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};
360
361 double un = 0.0, u2 = 0.0, nGradUn = 0.0;
362 for (unsigned int d = 0; d < 2; ++d)
363 {
364 un += nrm[d] * u[d];
365 u2 += u[d] * u[d];
366 for (unsigned int j = 0; j < 2; ++j)
367 {
368 nGradUn += nrm[d] * grad[d][j] * nrm[j];
369 }
370 }
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)
376 {
377 const double value =
378 (k1 * u2 * nrm[d] + k2 * un * u[d]) * S0 - fb[d];
379 energyNormal += nrm[d] * value;
380 }
381 expectedPhys[p] = kinvis * nGradUn - energyNormal;
382 }
383
384 Array<OneD, double> bndCoeffsOut(bndExp->GetNcoeffs(), 0.0);
385 bndExp->FwdTrans(expectedPhys, bndCoeffsOut);
386 for (unsigned int c = 0; c < bndExp->GetNcoeffs(); ++c)
387 {
388 const auto bndCoeff = bndCoeffOffset + c;
389 const auto localCoeff = coeffMap[bndCoeff];
390 expectedCoeffs[localCoeff] =
391 (signChange ? coeffSign[bndCoeff] : 1.0) * bndCoeffsOut[c];
392 }
393 }
394 bndCoeffOffset += bndExp->GetNcoeffs();
395 }
396 BOOST_REQUIRE_MESSAGE(foundRegion,
397 "No HOutflow pressure Dirichlet region found in "
398 "the extraction test fixture.");
399
401 "expected pressure coeffs",
403 pressureExpList,
404 MultiRegions::UnitInterleaveWidths(pressureExpList)),
405 pressureComponents, 1);
406 expected.Initialize<NektarSpaces::HostSpace>(0.0);
407 for (unsigned int blk = 0; blk < expected.GetBlocks().size(); ++blk)
408 {
409 auto &expectedBlock = expected.GetBlocks()[blk];
410 auto expectedPtr =
411 expectedBlock.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
412 auto collection = GetCollection(pressureExpList, blk);
413 const auto &expansions = collection.GetExpVector();
414 for (unsigned int e = 0; e < expansions.size(); ++e)
415 {
416 const auto offset =
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());
422 }
423 }
424
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);
430 });
431 BOOST_TEST(std::abs(maxExpected) > 0.1);
432
433 // The velocity at interleave width 1 and at the interleave width of the
434 // execution space. The operator gathers it into fields at its width,
435 // whose width changes from one update to the next.
436 const auto execSpace =
438 const unsigned int interleaveWidth =
439 NektarSpaces::GetVectorWidth<double>(execSpace);
441 "pressure coeffs",
443 pressureExpList,
444 MultiRegions::UnitInterleaveWidths(pressureExpList)),
445 pressureComponents, 1);
446 for (const unsigned int velocityWidth : {1u, interleaveWidth, 1u})
447 {
448 velocity.ReshapeStorage(velocityWidth, execSpace);
449 op->UpdatePressure(velocity);
450 pressureCoeffs.Initialize<NektarSpaces::HostSpace>(0.0);
451 op->GetPressureDirichletOp()->Apply(pressureCoeffs);
452
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)
456 {
457 BOOST_CHECK_MESSAGE(std::abs(actualValues[i] - expectedValues[i]) <
458 2.5e-10,
459 "velocity at width " << velocityWidth);
460 }
461 }
462}
int64_t id
std::vector< NekDouble > grad
static std::string GetOpExecSpace(std::shared_ptr< LibUtilities::SessionReader > session, const bool warn=true)
Return the execution space name ("opExecSpace") for an operator.
Definition Operator.hpp:190

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), Nektar::HOutflowBndCondOp< TData >::Create(), Nektar::SpatialDomains::eDirichlet, Nektar::Collections::eNoCollection, Nektar::MultiRegions::GetBlockAttributes< double, FieldState::Coeff >(), Nektar::MultiRegions::GetBlockAttributes< double, FieldState::Phys >(), Nektar::LibUtilities::Field< TData, TState >::GetBlocks(), Nektar::MultiRegions::GetCollection(), Nektar::MultiRegions::Operator< TData >::GetOpExecSpace(), grad, graph, id, Nektar::LibUtilities::Field< TData, TState >::Initialize(), Nektar::SpatialDomains::MeshGraphIO::Read(), Nektar::LibUtilities::Field< TData, TState >::ReshapeStorage(), session, and Nektar::LibUtilities::Field< TData, TState >::ToVector().

◆ BOOST_AUTO_TEST_CASE() [5/5]

BOOST_AUTO_TEST_CASE ( outgoing_2d_default_parameters_nonzero_fb  )

Definition at line 233 of file TestHOutflowBndCond.cpp.

234{
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);
237}

◆ BOOST_TEST_GLOBAL_CONFIGURATION()

BOOST_TEST_GLOBAL_CONFIGURATION ( GlobalConfiguration  )