Nektar++
Loading...
Searching...
No Matches
HOPressureBndCondOpImpl.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: HOPressureBndCondOpImpl.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: Higher-order pressure boundary condition implementation.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
39
41
42#include <algorithm>
43#include <map>
44#include <set>
45#include <utility>
46#include <vector>
47
50
63
64namespace Nektar::detail
65{
66
67/**
68 * @brief Implementation of HOPressureBndCondOp.
69 *
70 * @details
71 * Update() gathers the velocity and the flux on the volume elements adjacent
72 * to the regions, computes the curl-curl of the velocity there, combines it
73 * with the flux, extracts the result onto every region with
74 * PhysTraceExtractBndCond, projects it onto the normals and takes its inner
75 * product with the boundary basis. Apply() scatters that into the pressure
76 * forcing. Every block of the work runs on its own stream.
77 *
78 * UpdateWithAcceleration() subtracts the normal component of the Dirichlet
79 * velocity as the velocity solve imposes it: the session expression projected
80 * onto the boundary expansion of the velocity, at the points of the region.
81 * The predictor carries that velocity on the boundary, so the acceleration
82 * vanishes at a steady state; the expression itself would leave the
83 * projection error, divided by the implicit step size, in the flux. Its
84 * coefficients on that expansion are kept and computed once, unless a
85 * Dirichlet velocity condition of the region is time-dependent; it is
86 * evaluated from them at every update into the work storage of the flux,
87 * which no longer holds the flux by then.
88 *
89 * On a region with a velocity component that is not Dirichlet,
90 * UpdateWithAcceleration() also extracts predictor - rhs and subtracts its
91 * projection onto the normal components of those velocity components, whose
92 * flux is thus formed from rhs, without acceleration.
93 *
94 * UpdateWeak() does the same with the predictor left out of the combined
95 * flux, which then holds the viscous term alone before the acceleration and
96 * predictor - rhs are subtracted.
97 */
98template <typename ExecSpace, typename TData>
100{
101 using MemSpace = typename ExecSpace::memory_space;
102
103public:
105 const std::vector<std::string> &components)
106 : HOPressureBndCondOp<TData>(expansionList, components)
107 {
108 m_coordDim = expansionList->GetCoordim(0);
110 }
111
112 // className - for OperatorFactory
113 static std::string className;
114
115 // instantiation function for CreatorFunction in OperatorFactory
116 static std::unique_ptr<MultiRegions::Operator<TData>> Instantiate(
117 const MultiRegions::ExpListSharedPtr &expansionList,
118 const std::vector<std::string> &components)
119 {
120 return std::make_unique<HOPressureBndCondOpImpl<ExecSpace, TData>>(
121 expansionList, components);
122 }
123
124protected:
125 // The flux an update computes: that of Update(), from its rhs; that of
126 // UpdateWithAcceleration(), from the predictor less the acceleration to
127 // the boundary velocity; or that of UpdateWeak(), which leaves the
128 // predictor out as well.
129 enum class FluxForm
130 {
131 Plain,
133 Weak
134 };
135
136 // An index into a block at interleave width 1 and one into a block at
137 // m_interleaveWidth, each built from host data the first time a block at
138 // that width is seen, and kept.
146
147 // Point of the source block and point of the destination block of every
148 // point of a volume gather, for one pair of interleave widths of the two
149 // blocks.
155
156 // The boundary-adjacent elements of one block of the velocity field
157 // that land in one block of the restricted field.
159 {
160 unsigned int srcBlock = 0;
161 unsigned int dstBlock = 0;
162 size_t nData = 0;
163 // Element of the source block and element of the destination block
164 // of every element gathered, in increasing destination element.
165 std::vector<size_t> srcElmt;
166 std::vector<size_t> dstElmt;
167 // The indices for every pair of interleave widths of the source and
168 // the destination block seen, built from srcElmt and dstElmt the
169 // first time the pair is seen, and kept.
170 std::map<std::pair<unsigned int, unsigned int>, GatherIndices> indices;
171
172 // Move-only, as GatherIndices is: otherwise MSVC instantiates the
173 // copy of indices when std::vector<VolumeGather> reallocates.
174 VolumeGather() = default;
175 VolumeGather(const VolumeGather &) = delete;
179 };
180
181 // The coefficients of one domain block a boundary coefficient map
182 // writes: e * nCoeffs + c for coefficient c of element e, and the index
183 // built from it for the interleave width of the block.
185 {
186 size_t nCoeffs = 0;
187 std::vector<size_t> elmtCoeff;
189 };
190
191 // Interleave width of an interleaved field: 1 (Serial), the SIMD width
192 // (AVX) or the warp size (Device).
193 static constexpr unsigned int m_interleaveWidth =
195 // Stream m_compactBndCoeff is taken on, the first block's, through which
196 // the block streams writing and reading it wait on one another.
197 static constexpr unsigned int m_opStreamID = 1;
198
199 bool m_isUpdated = false;
201 // Whether m_bndVelocityCoeffs has been computed, by the first update
202 // with the acceleration.
204 unsigned int m_coordDim = 0;
205 // On the boundary-adjacent elements, the velocity, from which m_curlCurl
206 // is computed, then the forcing, rhs or predictor, which m_curlCurl is
207 // combined with in place into the flux, and then predictor - rhs. Both
208 // are laid out at the interleave width of the velocity
209 // (SetRestrictedInterleaveWidth()).
212 std::vector<LibUtilities::Field<TData, FieldState::Phys>> m_bndPhys;
213 std::vector<LibUtilities::Field<TData, FieldState::Coeff>> m_bndCoeffs;
214 // Per region, the flux extracted onto the points of the region, then
215 // predictor - rhs where the region has a velocity component that is not
216 // Dirichlet, and then the Dirichlet velocity as the velocity solve
217 // imposes it, zero in the velocity components that are not Dirichlet;
218 // see EvaluateBoundaryVelocity().
219 std::vector<LibUtilities::Field<TData, FieldState::Phys>> m_bndWorkPhys;
220 // Per region, the coefficients of that Dirichlet velocity on the
221 // boundary expansion of the velocity (uninstantiated for a point region).
222 std::vector<LibUtilities::Field<TData, FieldState::Coeff>>
224 // Per region, whether a Dirichlet velocity condition of the region is
225 // time-dependent, so that m_bndVelocityCoeffs is computed at every
226 // update.
228 std::vector<LibUtilities::Field<TData, FieldState::Phys>> m_bndNormals;
229 // Per region, the normals with the components of the Dirichlet velocity
230 // components zeroed, instantiated only for a region with a velocity
231 // component that is not Dirichlet.
232 std::vector<LibUtilities::Field<TData, FieldState::Phys>>
234 std::vector<MultiRegions::ExpListSharedPtr> m_bndExpLists;
235 // The sum of the surface integrals of m_bndCoeffs.
238 std::vector<unsigned int> m_bndRegionIds;
239 std::vector<std::shared_ptr<MultiRegions::IProductWRTBaseOp<TData>>>
241 // Per region, the operators evaluating the Dirichlet velocity on the
242 // boundary expansion of the velocity: the expression of the Dirichlet
243 // velocity, its projection onto the expansion and the evaluation of that
244 // at its points (nullptr for a point region).
245 std::vector<std::shared_ptr<MultiRegions::ExpressionOp<TData>>>
247 std::vector<std::shared_ptr<MultiRegions::FwdTransBCOp<TData>>>
249 std::vector<std::shared_ptr<MultiRegions::BwdTransOp<TData>>>
251 std::vector<bool> m_bndIsPoint;
253 std::shared_ptr<MultiRegions::CurlCurlOp<TData>> m_curlCurlOp;
254 std::vector<VolumeGather> m_volumeGather;
255 std::vector<unsigned int> m_bndConditionIndices;
256
257 // Per region, extracts the flux in m_curlCurl, and predictor - rhs in
258 // m_bndElmtWork, onto the points of the region.
259 std::vector<std::shared_ptr<
262
263 // Number of boundary coefficients of all the regions on this rank, the
264 // size of m_compactBndCoeff.
266 // Offset of every region in m_compactBndCoeff.
267 std::vector<size_t> m_bndCoeffOffsets;
268 // Boundary coefficients of all the regions, one after the other, which
269 // BuildBoundaryCoeffs() gathers into m_bndCoeff and m_groupValue.
271 // Streams of the region and domain blocks, which write
272 // m_compactBndCoeff and read the values gathered from it.
273 std::vector<unsigned int> m_updateStreams;
274
275 // Maps from the boundary coefficients to the domain coefficients, built
276 // by BuildCGBndCondCoeffMaps(): the targets written once (m_map, m_sign,
277 // m_bndCoeff, m_bndCoeffSrc), and the targets written by more than one
278 // boundary coefficient (m_group*), such as a vertex shared by two faces
279 // of one element. The targets are indexed at the interleave width of the
280 // block v_Apply() receives.
281 bool m_anySignChange = false;
282
283 std::vector<CoeffTargets> m_map;
284 std::vector<LibUtilities::MemoryRegion<TData>> m_sign;
285 std::vector<LibUtilities::MemoryRegion<TData>> m_bndCoeff;
286 std::vector<LibUtilities::MemoryRegion<size_t>> m_bndCoeffSrc;
287 // Number of targets written once, per block (the pressure has one
288 // component).
289 std::vector<size_t> m_uniqueCounts;
290
291 std::vector<CoeffTargets> m_groupTarget;
292 std::vector<LibUtilities::MemoryRegion<size_t>> m_groupOffset;
293 std::vector<LibUtilities::MemoryRegion<size_t>> m_groupSrc;
294 std::vector<LibUtilities::MemoryRegion<TData>> m_groupSign;
295 std::vector<LibUtilities::MemoryRegion<TData>> m_groupValue;
296 // Number of targets written more than once, per block.
297 std::vector<size_t> m_groupCounts;
298
300 const MultiRegions::ExpListSharedPtr &velocityExpansionList,
301 const std::vector<std::string> &velocityComponents) override
302 {
303 m_coordDim = velocityExpansionList->GetCoordim(0);
304 ASSERTL1(velocityComponents.size() >= m_coordDim,
305 "Insufficient velocity components for HOPressureBndCond.");
306 const std::vector<std::string> velocityComps(
307 velocityComponents.begin(),
308 velocityComponents.begin() + m_coordDim);
309
310 BuildBoundaryVelocityData(velocityExpansionList, velocityComps);
311 BuildBoundaryElementData(velocityExpansionList, velocityComps);
313 }
314
322
331
340
341 void v_UpdateBndCoeffs([[maybe_unused]] const TData &time) override
342 {
343 // The flux is set by Update().
344 }
345
346 size_t v_GetNumBndDofs() override
347 {
349 }
350
356
358 [[maybe_unused]] const std::string &userDefinedType) override
359 {
360 NEKERROR(ErrorUtil::efatal, "HOPressureBndCondOp applies only its own "
361 "regions; it has no default "
362 "regions to exclude.");
363 }
364
365 // Build the expansion list of the volume elements adjacent to the
366 // regions, the maps gathering them from the velocity field, and the
367 // operators acting on them.
369 const MultiRegions::ExpListSharedPtr &velocityExpansionList,
370 const std::vector<std::string> &velocityComponents)
371 {
372 struct VolumeLocation
373 {
374 unsigned int block = 0;
375 size_t element = 0;
377 };
378
379 const auto velocityBlocks =
380 MultiRegions::GetBlockAttributes<TData, FieldState::Phys>(
381 velocityExpansionList,
382 MultiRegions::UnitInterleaveWidths(velocityExpansionList));
383 std::map<int, VolumeLocation> fullVolumeLocation;
384 std::map<int, int> faceOwner;
385 for (unsigned int blk = 0; blk < velocityBlocks.size(); ++blk)
386 {
387 auto collection =
388 MultiRegions::GetCollection(velocityExpansionList, blk);
389 const auto &expansions = collection.GetExpVector();
390 for (unsigned int e = 0; e < expansions.size(); ++e)
391 {
392 const auto volumeGeometry =
393 expansions[e]->GetGeom()->GetGlobalID();
394 fullVolumeLocation[volumeGeometry] = {blk, e, expansions[e]};
395 for (unsigned int face = 0; face < expansions[e]->GetNtraces();
396 ++face)
397 {
398 faceOwner[expansions[e]->GetGeom()->GetTid(face)] =
399 volumeGeometry;
400 }
401 }
402 }
403
404 // The volume elements adjacent to a region, matched to its
405 // elements by geometry id.
406 std::set<int> selectedVolumeGeometry;
407 for (unsigned int n = 0; n < m_bndExpLists.size(); ++n)
408 {
409 const auto bndBlocks =
410 MultiRegions::GetBlockAttributes<TData, FieldState::Phys>(
411 m_bndExpLists[n],
412 MultiRegions::UnitInterleaveWidths(m_bndExpLists[n]));
413 for (unsigned int blk = 0; blk < bndBlocks.size(); ++blk)
414 {
415 auto collection =
417 const auto &expansions = collection.GetExpVector();
418 for (unsigned int e = 0; e < expansions.size(); ++e)
419 {
420 const auto owner =
421 faceOwner.find(expansions[e]->GetGeom()->GetGlobalID());
422 if (owner != faceOwner.end())
423 {
424 selectedVolumeGeometry.insert(owner->second);
425 }
426 }
427 }
428 }
429
430 if (selectedVolumeGeometry.empty())
431 {
432 return;
433 }
434 // The kernels are instantiated for two and three dimensions.
435 ASSERTL0(m_coordDim == 2 || m_coordDim == 3,
436 "HOPressureBndCond is implemented for two and three "
437 "dimensions.");
438
439 SpatialDomains::ExpansionInfoMap restrictedExpansionInfo;
440 const auto &velocityExpansionInfo =
441 velocityExpansionList->GetGraph()->GetExpansionInfo(
442 velocityComponents[0]);
443 for (const auto geometry : selectedVolumeGeometry)
444 {
445 const auto info = velocityExpansionInfo.find(geometry);
446 ASSERTL1(info != velocityExpansionInfo.end(),
447 "Unable to locate boundary-adjacent velocity expansion "
448 "metadata.");
449 restrictedExpansionInfo.emplace(*info);
450 }
451
454 velocityExpansionList->GetSession(), restrictedExpansionInfo,
455 false);
456 m_bndElmtExpansionList->SetDataWarehouse();
457 const auto restrictedBlocks =
458 MultiRegions::GetBlockAttributes<TData, FieldState::Phys>(
460 MultiRegions::UnitInterleaveWidths(m_bndElmtExpansionList));
461
463 "HOPressureBndCond boundary element velocity and forcing",
464 restrictedBlocks, velocityComponents, 1);
466 "HOPressureBndCond CurlCurl and flux", restrictedBlocks,
467 velocityComponents, 1);
468 m_bndElmtWork.template Initialize<MemSpace>(0.0);
469 m_curlCurl.template Initialize<MemSpace>(0.0);
470
471 std::map<int, VolumeLocation> restrictedVolumeLocation;
472 for (unsigned int blk = 0; blk < restrictedBlocks.size(); ++blk)
473 {
474 auto collection =
476 const auto &expansions = collection.GetExpVector();
477 for (unsigned int e = 0; e < expansions.size(); ++e)
478 {
479 restrictedVolumeLocation
480 [expansions[e]->GetGeom()->GetGlobalID()] = {blk, e,
481 expansions[e]};
482 }
483 }
484
485 // Per pair of source and destination block, the destination element
486 // of every element gathered, mapped to its source element, in
487 // increasing destination element.
488 std::map<std::pair<unsigned int, unsigned int>,
489 std::map<size_t, size_t>>
490 volumeMaps;
491 for (const auto geometry : selectedVolumeGeometry)
492 {
493 const auto &src = fullVolumeLocation.at(geometry);
494 const auto &dst = restrictedVolumeLocation.at(geometry);
495 ASSERTL1(src.expansion->GetTotPoints() ==
496 dst.expansion->GetTotPoints(),
497 "Boundary element quadrature differs after restriction.");
498 volumeMaps[{src.block, dst.block}][dst.element] = src.element;
499 }
500 for (auto &[blocks, maps] : volumeMaps)
501 {
502 VolumeGather gather;
503 gather.srcBlock = blocks.first;
504 gather.dstBlock = blocks.second;
505 gather.nData = restrictedBlocks[blocks.second].GetNumData();
506 for (const auto &[dstElmt, srcElmt] : maps)
507 {
508 gather.dstElmt.push_back(dstElmt);
509 gather.srcElmt.push_back(srcElmt);
510 }
511 m_volumeGather.push_back(std::move(gather));
512 }
513
515 m_bndElmtExpansionList, velocityComponents, ExecSpace::name);
516
517 // An extraction for every region, empty where no element of the
518 // region is on this rank. m_bndElmtExpansionList, built from an
519 // ExpansionInfoMap, has no mesh graph, so the extraction takes that of
520 // the velocity field.
521 for (unsigned int n = 0; n < m_bndExpLists.size(); ++n)
522 {
523 m_bndFluxExtractOps.push_back(
525 ExecSpace, TData>>(m_bndElmtExpansionList,
526 velocityComponents, m_bndExpLists[n],
527 ExecSpace::name,
528 velocityExpansionList->GetGraph()));
529 }
530 }
531
532 // Point of a block at interleave width @p interleaveWidth of every point
533 // @p gather copies of the elements @p elmts, its source or its
534 // destination elements: point q of element e lies at
535 // (e / interleaveWidth) * interleaveWidth * nData + q * interleaveWidth
536 // + e % interleaveWidth. The points are in the order of the destination
537 // points in a destination block at interleave width
538 // @p dstInterleaveWidth, the lane varying fastest, so that neighbouring
539 // threads write neighbouring points, and read neighbouring points where
540 // neighbouring destination elements come from neighbouring source
541 // elements.
542 static std::vector<size_t> PointIndex(const VolumeGather &gather,
543 const std::vector<size_t> &elmts,
544 const unsigned int interleaveWidth,
545 const unsigned int dstInterleaveWidth)
546 {
547 std::vector<size_t> index;
548 index.reserve(elmts.size() * gather.nData);
549 // The destination elements increase, so those of one destination
550 // group are consecutive.
551 for (size_t first = 0; first < elmts.size();)
552 {
553 const size_t dstGroup = gather.dstElmt[first] / dstInterleaveWidth;
554 size_t last = first + 1;
555 while (last < elmts.size() &&
556 gather.dstElmt[last] / dstInterleaveWidth == dstGroup)
557 {
558 ++last;
559 }
560 for (size_t q = 0; q < gather.nData; ++q)
561 {
562 for (size_t k = first; k < last; ++k)
563 {
564 const size_t group = elmts[k] / interleaveWidth;
565 const size_t lane = elmts[k] % interleaveWidth;
566 index.push_back(group * interleaveWidth * gather.nData +
567 q * interleaveWidth + lane);
568 }
569 }
570 first = last;
571 }
572 return index;
573 }
574
575 // Coefficient of a domain block at interleave width @p interleaveWidth of
576 // every target of @p targets: coefficient c of element e lies at
577 // (e / interleaveWidth) * interleaveWidth * nCoeffs + c * interleaveWidth
578 // + e % interleaveWidth.
579 static std::vector<size_t> TargetIndex(const CoeffTargets &targets,
580 const unsigned int interleaveWidth)
581 {
582 std::vector<size_t> index(targets.elmtCoeff.size());
583 for (size_t k = 0; k < index.size(); ++k)
584 {
585 const size_t elmt = targets.elmtCoeff[k] / targets.nCoeffs;
586 const size_t coeff = targets.elmtCoeff[k] % targets.nCoeffs;
587 const size_t group = elmt / interleaveWidth;
588 const size_t lane = elmt % interleaveWidth;
589 index[k] = group * interleaveWidth * targets.nCoeffs +
590 coeff * interleaveWidth + lane;
591 }
592 return index;
593 }
594
595 // The one of the indices of @p indices for a block at interleave width
596 // @p interleaveWidth, built from the host index @p buildIndex returns for
597 // that width, on stream @p streamID, the first time it is requested.
598 template <typename BuildIndexFn>
600 InterleaveWidthIndices &indices, const unsigned int interleaveWidth,
601 const unsigned int streamID, BuildIndexFn &&buildIndex)
602 {
603 ASSERTL0(interleaveWidth == 1u || interleaveWidth == m_interleaveWidth,
604 "HOPressureBndCond takes fields at interleave width 1 or at "
605 "the interleave width of its execution space.");
606 const bool isUnitWidth = interleaveWidth == 1u;
607 auto &index = isUnitWidth ? indices.index : indices.interleavedIndex;
608 bool &isBuilt = isUnitWidth ? indices.isBuiltIndex
609 : indices.isBuiltInterleavedIndex;
610 if (!isBuilt)
611 {
613 MemSpace, size_t>(buildIndex(interleaveWidth), streamID);
614 isBuilt = true;
615 }
616 return index;
617 }
618
619 // The indices of @p gather for a source block at interleave width
620 // @p srcInterleaveWidth and a destination block at
621 // @p dstInterleaveWidth, built on stream @p streamID on first use.
623 VolumeGather &gather, const unsigned int srcInterleaveWidth,
624 const unsigned int dstInterleaveWidth, const unsigned int streamID)
625 {
626 ASSERTL0((srcInterleaveWidth == 1u ||
627 srcInterleaveWidth == m_interleaveWidth) &&
628 (dstInterleaveWidth == 1u ||
629 dstInterleaveWidth == m_interleaveWidth),
630 "HOPressureBndCond takes fields at interleave width 1 or at "
631 "the interleave width of its execution space.");
632 auto [it, isNew] = gather.indices.try_emplace(
633 std::make_pair(srcInterleaveWidth, dstInterleaveWidth));
634 if (isNew)
635 {
636 it->second.srcIndex =
638 MemSpace, size_t>(PointIndex(gather, gather.srcElmt,
639 srcInterleaveWidth,
640 dstInterleaveWidth),
641 streamID);
642 it->second.dstIndex =
644 MemSpace, size_t>(PointIndex(gather, gather.dstElmt,
645 dstInterleaveWidth,
646 dstInterleaveWidth),
647 streamID);
648 }
649 return it->second;
650 }
651
652 // Lay @p field, on the boundary-adjacent elements, out at interleave
653 // width @p interleaveWidth, that of the velocity, so that the gathers
654 // write it and the operators on those elements read it without
655 // reshaping. A field changing width is zeroed, so that its padding
656 // stays zero: every element of it is written again before it is read.
659 const unsigned int interleaveWidth)
660 {
661 ASSERTL0(interleaveWidth == 1u || interleaveWidth == m_interleaveWidth,
662 "HOPressureBndCond takes fields at interleave width 1 or at "
663 "the interleave width of its execution space.");
664 bool isAtWidth = true;
665 for (auto &block : field.GetBlocks())
666 {
667 isAtWidth =
668 isAtWidth && block.GetInterleaveWidth() == interleaveWidth;
669 }
670 if (!isAtWidth)
671 {
672 Math::zero<ExecSpace>(field);
673 field.SetInterleaveWidth(interleaveWidth);
674 }
675 }
676
677 // The index of @p targets into a domain block at interleave width
678 // @p interleaveWidth, built on stream @p streamID on first use.
680 CoeffTargets &targets, const unsigned int interleaveWidth,
681 const unsigned int streamID)
682 {
684 targets.indices, interleaveWidth, streamID,
685 [&targets](const unsigned int blockInterleaveWidth) {
686 return TargetIndex(targets, blockInterleaveWidth);
687 });
688 }
689
690 // Copy the boundary-adjacent elements of the first m_coordDim components of
691 // src, a field on the velocity expansion list, into dst, or subtract
692 // them from it with SUBTRACT, each block at its own interleave width.
693 // Each gather runs on the stream of its destination block, after the
694 // work queued on the stream of its source block, and the source streams
695 // wait on the gathers in turn, without the host waiting on either.
696 template <bool SUBTRACT = false>
700 {
701 for (auto &gather : m_volumeGather)
702 {
703 const unsigned int srcStreamID = gather.srcBlock + 1;
704 const unsigned int streamID = gather.dstBlock + 1;
705 auto &srcBlock = src.GetBlocks()[gather.srcBlock];
706 auto &dstBlock = dst.GetBlocks()[gather.dstBlock];
707
708 RecordStreamEvent<ExecSpace>(srcStreamID);
709 SetStreamDependencies<ExecSpace>(streamID, srcStreamID);
710
711 auto &indices = GatherIndicesAtInterleaveWidths(
712 gather, srcBlock.GetInterleaveWidth(),
713 dstBlock.GetInterleaveWidth(), streamID);
714 auto srcPtr =
715 srcBlock.template GetPtr<MemSpace, ReadOnly>(streamID);
716 // The points of the block this gather does not write are
717 // padding, which stays zero, or written by another gather.
718 auto dstPtr =
719 dstBlock.template GetPtr<MemSpace, ReadWrite>(streamID);
720 auto srcIdx =
721 indices.srcIndex.template GetPtr<MemSpace, ReadOnly>(streamID);
722 auto dstIdx =
723 indices.dstIndex.template GetPtr<MemSpace, ReadOnly>(streamID);
724 const size_t nsize = gather.srcElmt.size() * gather.nData;
725 if (m_coordDim == 2)
726 {
727 HOPressureVolumeGatherKernel<ExecSpace, 2, SUBTRACT>(
728 nsize, srcBlock.CompSize(), dstBlock.CompSize(), srcIdx,
729 dstIdx, srcPtr, dstPtr, streamID);
730 }
731 else if (m_coordDim == 3)
732 {
733 HOPressureVolumeGatherKernel<ExecSpace, 3, SUBTRACT>(
734 nsize, srcBlock.CompSize(), dstBlock.CompSize(), srcIdx,
735 dstIdx, srcPtr, dstPtr, streamID);
736 }
737 RecordStreamEvent<ExecSpace>(streamID);
738 }
739
740 for (const auto &gather : m_volumeGather)
741 {
742 const unsigned int srcStreamID = gather.srcBlock + 1;
743 const unsigned int streamID = gather.dstBlock + 1;
744 SetStreamDependencies<ExecSpace>(srcStreamID, streamID);
745 }
746 }
747
748 // Compute scale * (forcing - kinvis * m_curlCurl) . n on every region,
749 // forcing being the rhs of Update() or the predictor of
750 // UpdateWithAcceleration(). The weak form of UpdateWeak() leaves forcing
751 // out: its flux is -scale * kinvis * m_curlCurl . n. With @p rhs, that
752 // of UpdateWithAcceleration() and UpdateWeak(), subtract
753 // scale * (predictor - rhs) . n over the velocity components that are
754 // not Dirichlet on a region.
756 const FluxForm form,
758 LibUtilities::Field<TData, FieldState::Phys> *rhs, const TData kinvis,
759 const TData scale)
760 {
761 // Combine the flux on the boundary-adjacent elements into
762 // m_curlCurl, before extraction, reading the forcing from
763 // m_bndElmtWork, and then form predictor - rhs in m_bndElmtWork.
764 const bool isWeak = form == FluxForm::Weak;
765 const bool isSubtractHistory = rhs && this->m_hasNonDirichletVelocity;
766 if (!isWeak)
767 {
769 }
770 for (unsigned int blk = 0; blk < m_curlCurl.GetBlocks().size(); ++blk)
771 {
772 const unsigned int streamID = blk + 1;
773 auto &forcingBlk = m_bndElmtWork.GetBlocks()[blk];
774 auto &curlCurlBlk = m_curlCurl.GetBlocks()[blk];
775 auto forcingPtr =
776 forcingBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
777 auto curlCurlPtr =
778 curlCurlBlk.template GetPtr<MemSpace, ReadWrite>(streamID);
779 // Every point of the block at its interleave width, padding
780 // included, which the combination keeps zero.
781 const size_t nPoints = curlCurlBlk.GetNumElementsWithPadding() *
782 curlCurlBlk.GetNumData();
783 if (isWeak)
784 {
785 if (m_coordDim == 2)
786 {
787 HOPressureVolumeCombineKernel<ExecSpace, 2, false>(
788 nPoints, curlCurlBlk.CompSize(), kinvis, scale,
789 forcingPtr, curlCurlPtr, streamID);
790 }
791 else if (m_coordDim == 3)
792 {
793 HOPressureVolumeCombineKernel<ExecSpace, 3, false>(
794 nPoints, curlCurlBlk.CompSize(), kinvis, scale,
795 forcingPtr, curlCurlPtr, streamID);
796 }
797 }
798 else
799 {
800 if (m_coordDim == 2)
801 {
802 HOPressureVolumeCombineKernel<ExecSpace, 2, true>(
803 nPoints, curlCurlBlk.CompSize(), kinvis, scale,
804 forcingPtr, curlCurlPtr, streamID);
805 }
806 else if (m_coordDim == 3)
807 {
808 HOPressureVolumeCombineKernel<ExecSpace, 3, true>(
809 nPoints, curlCurlBlk.CompSize(), kinvis, scale,
810 forcingPtr, curlCurlPtr, streamID);
811 }
812 }
813 }
814
815 // predictor - rhs overwrites the forcing the strong combination above
816 // has read; the weak one reads none, so the predictor is gathered.
817 if (isSubtractHistory)
818 {
819 if (isWeak)
820 {
822 }
823 GatherBoundaryElements<true>(*rhs, m_bndElmtWork);
824 }
825
826 // Each extraction orders itself after the combine above and the
827 // streams of the boundary blocks after itself.
828 for (unsigned int n = 0; n < m_bndPhys.size(); ++n)
829 {
831
832 for (unsigned int blk = 0; blk < m_bndPhys[n].GetBlocks().size();
833 ++blk)
834 {
835 const unsigned int streamID = blk + 1;
836 auto &fluxBlk = m_bndWorkPhys[n].GetBlocks()[blk];
837 auto &normalBlk = m_bndNormals[n].GetBlocks()[blk];
838 auto &outBlk = m_bndPhys[n].GetBlocks()[blk];
839 auto fluxPtr =
840 fluxBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
841 auto normalPtr =
842 normalBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
843 auto outPtr =
844 outBlk.template GetPtr<MemSpace, WriteOnly>(streamID);
845
846 const size_t nPoints =
847 outBlk.GetNumElementsWithPadding() * outBlk.GetNumData();
848 if (m_coordDim == 2)
849 {
850 HOPressureBoundaryCombineKernel<ExecSpace, 2>(
851 nPoints, fluxBlk.CompSize(), fluxPtr, normalPtr, outPtr,
852 streamID);
853 }
854 else if (m_coordDim == 3)
855 {
856 HOPressureBoundaryCombineKernel<ExecSpace, 3>(
857 nPoints, fluxBlk.CompSize(), fluxPtr, normalPtr, outPtr,
858 streamID);
859 }
860 }
861
862 if (!isSubtractHistory || !m_bndNonDirichletNormals[n])
863 {
864 continue;
865 }
866
867 // The extraction waits on the combine above, which read
868 // m_bndWorkPhys[n].
870 for (unsigned int blk = 0; blk < m_bndPhys[n].GetBlocks().size();
871 ++blk)
872 {
873 const unsigned int streamID = blk + 1;
874 auto &historyBlk = m_bndWorkPhys[n].GetBlocks()[blk];
875 auto &normalBlk = m_bndNonDirichletNormals[n].GetBlocks()[blk];
876 auto &outBlk = m_bndPhys[n].GetBlocks()[blk];
877 auto historyPtr =
878 historyBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
879 auto normalPtr =
880 normalBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
881 auto outPtr =
882 outBlk.template GetPtr<MemSpace, ReadWrite>(streamID);
883
884 const size_t nPoints =
885 outBlk.GetNumElementsWithPadding() * outBlk.GetNumData();
886 if (m_coordDim == 2)
887 {
888 HOPressureSubtractNormalVelocityKernel<ExecSpace, 2>(
889 nPoints, historyBlk.CompSize(), scale, historyPtr,
890 normalPtr, outPtr, streamID);
891 }
892 else if (m_coordDim == 3)
893 {
894 HOPressureSubtractNormalVelocityKernel<ExecSpace, 3>(
895 nPoints, historyBlk.CompSize(), scale, historyPtr,
896 normalPtr, outPtr, streamID);
897 }
898 }
899 }
900 }
901
903 const MultiRegions::ExpListSharedPtr &velocityExpansionList,
904 const std::vector<std::string> &velocityComponents)
905 {
907 this->m_expansionList->GetSession(),
908 this->m_expansionList->GetGraph());
909 const auto &bndConditions = bcs.GetBoundaryConditions();
910 const auto &velocityBndExpansions =
911 velocityExpansionList->GetBndCondExpansions();
912
913 ASSERTL1(m_bndExpLists.size() == m_bndRegionIds.size(),
914 "HOPressureBndCond boundary metadata differs.");
915 for (unsigned int n = 0; n < m_bndExpLists.size(); ++n)
916 {
917 auto bndExp = m_bndExpLists[n];
918 const auto conditionIt = bndConditions.find(m_bndRegionIds[n]);
919 ASSERTL0(conditionIt != bndConditions.end(),
920 "Unable to locate velocity boundary conditions for "
921 "HOPressureBndCond.");
922
923 // A velocity component that is not Dirichlet has no prescribed
924 // boundary velocity and takes no acceleration: its boundary
925 // velocity is zero, and BuildBoundaryPhys() forms its flux from
926 // rhs through m_bndNonDirichletNormals.
927 std::vector<LibUtilities::EquationSharedPtr> equations;
928 std::vector<bool> isDirichlet(m_coordDim);
929 bool isTimeDependent = false;
930 for (unsigned int d = 0; d < m_coordDim; ++d)
931 {
932 const auto componentIt =
933 conditionIt->second->find(velocityComponents[d]);
934 ASSERTL0(componentIt != conditionIt->second->end(),
935 "Unable to locate a velocity component boundary "
936 "condition for HOPressureBndCond.");
937 isDirichlet[d] =
938 componentIt->second->GetBoundaryConditionType() ==
940 isTimeDependent =
941 isTimeDependent ||
942 (isDirichlet[d] && componentIt->second->IsTimeDependent());
943 equations.push_back(
944 isDirichlet[d] ? componentIt->second->GetEquation()
947 this->m_expansionList->GetSession()
948 ->GetInterpreter(),
949 "0"));
950 }
951 const bool hasNonDirichletVelocity =
952 std::find(isDirichlet.begin(), isDirichlet.end(), false) !=
953 isDirichlet.end();
956 (hasNonDirichletVelocity && bndExp->GetExpSize() > 0);
957
958 const auto bndPhysBlocks =
959 MultiRegions::GetBlockAttributes<TData, FieldState::Phys>(
960 bndExp, MultiRegions::UnitInterleaveWidths(bndExp));
961 m_bndWorkPhys.emplace_back(
962 "HOPressureBndCond boundary flux and velocity", bndPhysBlocks,
963 velocityComponents, 1);
964 m_bndNormals.emplace_back("HOPressureBndCond boundary normals",
965 bndPhysBlocks, velocityComponents, 1);
966 m_bndWorkPhys.back().template Initialize<MemSpace>(0.0);
967 m_bndNormals.back().template Initialize<MemSpace>(0.0);
968 m_bndNonDirichletNormals.emplace_back();
969 if (hasNonDirichletVelocity)
970 {
973 "HOPressureBndCond boundary non-Dirichlet normals",
974 bndPhysBlocks, velocityComponents, 1);
975 m_bndNonDirichletNormals.back().template Initialize<MemSpace>(
976 0.0);
977 }
978
980 this->m_expansionList->GetBoundaryNormals(m_bndConditionIndices[n],
981 normals);
982 for (unsigned int blk = 0;
983 blk < m_bndNormals.back().GetBlocks().size(); ++blk)
984 {
985 auto &normalBlock = m_bndNormals.back().GetBlocks()[blk];
986 auto normalPtr =
987 normalBlock
988 .template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
989 TData *nonDirichletNormalPtr = nullptr;
990 if (hasNonDirichletVelocity)
991 {
992 nonDirichletNormalPtr =
994 .GetBlocks()[blk]
995 .template GetPtr<NektarSpaces::HostSpace,
996 WriteOnly>();
997 }
998 auto collection = MultiRegions::GetCollection(bndExp, blk);
999 const auto &expansions = collection.GetExpVector();
1000 for (unsigned int e = 0; e < expansions.size(); ++e)
1001 {
1002 const auto offset =
1003 bndExp->GetPhys_Offset(expansions[e]->GetElmtId());
1004 for (unsigned int d = 0; d < m_coordDim; ++d)
1005 {
1006 for (unsigned int p = 0; p < normalBlock.GetNumData();
1007 ++p)
1008 {
1009 const size_t idx = d * normalBlock.CompSize() +
1010 e * normalBlock.GetNumData() + p;
1011 normalPtr[idx] =
1012 static_cast<TData>(normals[d][offset + p]);
1013 if (nonDirichletNormalPtr)
1014 {
1015 nonDirichletNormalPtr[idx] =
1016 isDirichlet[d] ? TData(0.0)
1017 : normalPtr[idx];
1018 }
1019 }
1020 }
1021 }
1022 }
1023
1024 // The velocity solve imposes the Dirichlet velocity as its
1025 // projection onto the boundary expansion of the velocity, on
1026 // which it is therefore evaluated. The flux and the normals are
1027 // on the boundary expansion of the pressure, whose blocks must
1028 // hold the same elements at the same points.
1029 auto velocityBndExp =
1030 velocityBndExpansions[m_bndConditionIndices[n]];
1031 // The operators built on this list below need a data warehouse,
1032 // which it has not got: the equation system sets the warehouse
1033 // of a field and of its trace, not of its boundary expansion
1034 // lists, and the boundary condition operators of the velocity
1035 // solve build lists of their own. Setting it here cannot affect
1036 // them, since an operator holds the warehouse it was built with.
1037 velocityBndExp->SetDataWarehouse();
1038 ASSERTL0(velocityBndExp->SameQuadratureLayout(bndExp),
1039 "HOPressureBndCond requires the velocity and pressure "
1040 "boundary expansions to share their quadrature points.");
1041 m_bndVelocityIsTimeDependent.push_back(isTimeDependent);
1042
1044 velocityBndExp, velocityComponents, ExecSpace::name);
1045 expressionOp->SetExpressions(equations);
1046 m_bndVelocityExpressionOps.push_back(expressionOp);
1047
1048 // A point region takes the prescribed velocity itself.
1049 m_bndVelocityCoeffs.emplace_back();
1050 m_bndVelocityFwdTransOps.push_back(nullptr);
1051 m_bndVelocityBwdTransOps.push_back(nullptr);
1052 if (m_bndIsPoint[n])
1053 {
1054 continue;
1055 }
1056 m_bndVelocityCoeffs.back() =
1058 "HOPressureBndCond boundary velocity coeffs",
1059 MultiRegions::GetBlockAttributes<TData, FieldState::Coeff>(
1060 velocityBndExp,
1061 MultiRegions::UnitInterleaveWidths(velocityBndExp)),
1062 velocityComponents, 1);
1063 m_bndVelocityCoeffs.back().template Initialize<MemSpace>(0.0);
1066 velocityBndExp, velocityComponents, ExecSpace::name);
1069 velocityBndExp, velocityComponents, ExecSpace::name);
1070 }
1071 }
1072
1073 // Whether a pressure boundary condition takes the higher-order pressure
1074 // flux: a Neumann condition with USERDEFINEDTYPE="H". Any other Neumann
1075 // region keeps the value prescribed in the session.
1076 static bool IsHOPressure(
1078 {
1079 return condition->GetBoundaryConditionType() ==
1082 condition->GetUserDefined(),
1084 }
1085
1087 {
1088 ASSERTL1(this->m_components.size() == 1,
1089 "HOPressureBndCond expects one pressure component.");
1090
1091 auto contField = std::dynamic_pointer_cast<MultiRegions::ContField>(
1092 this->m_expansionList);
1093 ASSERTL1(contField,
1094 "HOPressureBndCond requires a continuous pressure field.");
1095
1096 const auto &bndExpansions =
1097 this->m_expansionList->GetBndCondExpansions();
1098 const auto &bndConditions = this->m_expansionList->GetBndConditions();
1100 this->m_expansionList->GetSession(),
1101 this->m_expansionList->GetGraph());
1102 const auto &bregions = bcs.GetBoundaryRegions();
1103 std::vector<unsigned int> bndRegionIds;
1104 for (const auto &[regionId, region] : bregions)
1105 {
1106 bndRegionIds.push_back(regionId);
1107 }
1108 ASSERTL1(bndRegionIds.size() == bndExpansions.size(),
1109 "Boundary region and expansion counts differ.");
1110
1111 // isTypeByRegion and numBcExpCoeffs have an entry for every region,
1112 // including those without the higher-order condition, for the
1113 // offsets of BuildCGBndCondCoeffMaps().
1114 std::vector<std::vector<bool>> isTypeByRegion(bndExpansions.size());
1115 std::vector<size_t> numBcExpCoeffs(bndExpansions.size());
1116 for (unsigned int n = 0; n < bndExpansions.size(); ++n)
1117 {
1118 const bool isHOP = IsHOPressure(bndConditions[n]);
1119 isTypeByRegion[n] = {isHOP};
1120 numBcExpCoeffs[n] = bndExpansions[n]->GetNcoeffs();
1121
1122 if (!isHOP)
1123 {
1124 continue;
1125 }
1126
1127 auto bndExp = bndExpansions[n];
1128 bndExp->SetDataWarehouse();
1129
1131 m_numBndCoeffCompSize += numBcExpCoeffs[n];
1132
1133 m_bndExpLists.push_back(bndExp);
1134 m_bndRegionIds.push_back(bndRegionIds[n]);
1135 m_bndConditionIndices.push_back(n);
1136 m_bndPhys.emplace_back(
1137 "HOPressureBndCond boundary phys",
1138 MultiRegions::GetBlockAttributes<TData, FieldState::Phys>(
1139 bndExp, MultiRegions::UnitInterleaveWidths(bndExp)),
1140 this->m_components, 1);
1141 m_bndCoeffs.emplace_back(
1142 "HOPressureBndCond boundary coeffs",
1143 MultiRegions::GetBlockAttributes<TData, FieldState::Coeff>(
1144 bndExp, MultiRegions::UnitInterleaveWidths(bndExp)),
1145 this->m_components, 1);
1146 m_bndPhys.back().template Initialize<MemSpace>(0.0);
1147 m_bndCoeffs.back().template Initialize<MemSpace>(0.0);
1148
1149 const bool isPoint = bndExp->GetShapeDimension() == 0;
1150 m_bndIsPoint.push_back(isPoint);
1151 m_bndIProductOps.push_back(
1152 isPoint ? nullptr
1154 bndExp, this->m_components, ExecSpace::name));
1155 }
1156
1157 // The pressure, the one component, of every region.
1161 std::vector<std::vector<bool>>(m_bndExpLists.size(),
1162 std::vector<bool>{true}));
1163
1164 // No higher-order pressure region on this rank. The check is local:
1165 // BuildCGBndCondCoeffMaps() below uses the assembly map of the
1166 // pressure field and builds no field collectively.
1167 if (m_numBndCoeffCompSize == 0)
1168 {
1169 return;
1170 }
1171
1172 auto maps =
1173 MultiRegions::detail::BuildCGBndCondCoeffMaps<ExecSpace, TData>(
1174 this->m_expansionList, this->m_components, bregions,
1175 isTypeByRegion, numBcExpCoeffs, m_numBndCoeffCompSize,
1177 [](unsigned int, const auto &, const auto &, const auto &) {});
1178
1179 m_anySignChange = maps.anySignChange;
1180 m_sign = std::move(maps.uniqueSign);
1181 m_bndCoeff = std::move(maps.uniqueBndCoeff);
1182 m_bndCoeffSrc = std::move(maps.uniqueBndCoeffSrc);
1183 m_groupOffset = std::move(maps.groupOffset);
1184 m_groupSrc = std::move(maps.groupSrc);
1185 m_groupSign = std::move(maps.groupSign);
1186 m_groupValue = std::move(maps.groupValue);
1187 m_uniqueCounts.resize(maps.compUniqueCounts.size());
1188 m_groupCounts.resize(maps.compGroupCounts.size());
1189 for (size_t blk = 0; blk < m_uniqueCounts.size(); ++blk)
1190 {
1191 m_uniqueCounts[blk] = maps.compUniqueCounts[blk][0];
1192 m_groupCounts[blk] = maps.compGroupCounts[blk][0];
1193 }
1196 // Allocated here, on the null stream, so the region block streams
1197 // that write it never race the allocation.
1198 m_compactBndCoeff.template GetPtr<MemSpace, WriteOnly>();
1199
1200 // The region blocks write the compact array and the domain blocks
1201 // read the values gathered from it.
1202 size_t nUpdateStreams = m_bndCoeff.size();
1203 for (auto &field : m_bndCoeffs)
1204 {
1205 nUpdateStreams = std::max(nUpdateStreams, field.GetBlocks().size());
1206 }
1207 for (unsigned int id = 1; id <= nUpdateStreams; ++id)
1208 {
1209 m_updateStreams.push_back(id);
1210 }
1211
1212 m_map = BuildCoeffTargets(maps.uniqueMap, this->m_expansionList);
1214 BuildCoeffTargets(maps.groupTarget, this->m_expansionList);
1215 }
1216
1217 // The targets of every domain block of @p expansionList from the index
1218 // into the block at interleave width 1 BuildCGBndCondCoeffMaps() returns,
1219 // which is read back to the host once. The indices into the blocks are
1220 // built from it on first use.
1221 std::vector<CoeffTargets> BuildCoeffTargets(
1222 std::vector<LibUtilities::MemoryRegion<size_t>> &unitWidthIndex,
1223 const MultiRegions::ExpListSharedPtr &expansionList)
1224 {
1225 const auto coeffBlocks =
1226 MultiRegions::GetBlockAttributes<TData, FieldState::Coeff>(
1227 expansionList,
1228 MultiRegions::UnitInterleaveWidths(expansionList));
1229 std::vector<CoeffTargets> targets(unitWidthIndex.size());
1230 for (unsigned int blk = 0; blk < unitWidthIndex.size(); ++blk)
1231 {
1232 targets[blk].nCoeffs = coeffBlocks[blk].GetNumData();
1233 const size_t nTargets = unitWidthIndex[blk].size();
1234 if (nTargets > 0)
1235 {
1236 auto elmtCoeff =
1237 unitWidthIndex[blk]
1238 .template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
1239 targets[blk].elmtCoeff.assign(elmtCoeff, elmtCoeff + nTargets);
1240 }
1241 }
1242 return targets;
1243 }
1244
1246 {
1247 if (m_numBndCoeffCompSize == 0)
1248 {
1249 return;
1250 }
1251
1252 if (form != FluxForm::Plain)
1253 {
1255 }
1256
1257 // Copy the boundary coefficients of every region into
1258 // m_compactBndCoeff, in the layout BuildCGBndCondCoeffMaps() assumed,
1259 // each block on the stream that computed it.
1260 auto compactPtr =
1261 m_compactBndCoeff.template GetPtr<MemSpace, WriteOnly>(
1262 m_opStreamID);
1263
1264 for (unsigned int n = 0; n < m_bndPhys.size(); ++n)
1265 {
1266 if (!m_bndIsPoint[n])
1267 {
1268 m_bndIProductOps[n]->Apply(m_bndPhys[n], m_bndCoeffs[n]);
1269 }
1270 else
1271 {
1272 // A point region has a single block.
1273 const unsigned int blk = 0;
1274 const unsigned int streamID = blk + 1;
1275 auto &physBlk = m_bndPhys[n].GetBlocks()[blk];
1276 auto &coeffBlk = m_bndCoeffs[n].GetBlocks()[blk];
1277 auto src =
1278 physBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
1279 auto dst =
1280 coeffBlk.template GetPtr<MemSpace, WriteOnly>(streamID);
1281 const auto count =
1282 coeffBlk.GetNumElements() * coeffBlk.GetNumData();
1283 Math::copyKernel<ExecSpace>(count, src, dst, streamID);
1284 }
1285
1286 const size_t bndOffset = m_bndCoeffOffsets[n];
1287 size_t regionOffset = 0;
1288 for (unsigned int blk = 0; blk < m_bndCoeffs[n].GetBlocks().size();
1289 ++blk)
1290 {
1291 const unsigned int streamID = blk + 1;
1292 auto &wspBlk = m_bndCoeffs[n].GetBlocks()[blk];
1293 auto srcPtr =
1294 wspBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
1295 const auto blockLen =
1296 wspBlk.GetNumElements() * wspBlk.GetNumData();
1297
1298 // On the stream that computed the coefficients of the block.
1299 Math::copyKernel<ExecSpace>(
1300 blockLen, srcPtr, compactPtr + bndOffset + regionOffset,
1301 streamID);
1302 regionOffset += blockLen;
1303 }
1304 }
1305
1306 // Gather m_compactBndCoeff into the domain blocks v_Apply() reads,
1307 // each on the stream of its block. Every gather reads entries written
1308 // on any region block's stream, so the block streams first wait on
1309 // one another through m_opStreamID.
1310 JoinStreams<ExecSpace>(m_opStreamID, m_updateStreams);
1311 ReleaseStreams<ExecSpace>(m_opStreamID, m_updateStreams);
1312
1313 for (unsigned int blk = 0; blk < m_bndCoeff.size(); ++blk)
1314 {
1315 const unsigned int streamID = blk + 1;
1316 auto idxPtr =
1317 m_bndCoeffSrc[blk].template GetPtr<MemSpace, ReadOnly>(
1318 streamID);
1319 auto dstPtr =
1320 m_bndCoeff[blk].template GetPtr<MemSpace, WriteOnly>(streamID);
1321 MultiRegions::detail::CGBndCondGatherKernel<ExecSpace>(
1322 m_bndCoeffSrc[blk].size(), idxPtr, compactPtr, dstPtr,
1323 streamID);
1324
1325 // Gathered even when empty, so that m_groupValue[blk] is valid
1326 // when v_Apply() reads it.
1327 auto groupIdxPtr =
1328 m_groupSrc[blk].template GetPtr<MemSpace, ReadOnly>(streamID);
1329 auto groupDstPtr =
1330 m_groupValue[blk].template GetPtr<MemSpace, WriteOnly>(
1331 streamID);
1332 MultiRegions::detail::CGBndCondGatherKernel<ExecSpace>(
1333 m_groupSrc[blk].size(), groupIdxPtr, compactPtr, groupDstPtr,
1334 streamID);
1335 }
1336
1337 // The next update overwrites m_compactBndCoeff on the region block
1338 // streams, which wait on the gathers in turn.
1339 JoinStreams<ExecSpace>(m_opStreamID, m_updateStreams);
1340 ReleaseStreams<ExecSpace>(m_opStreamID, m_updateStreams);
1341 }
1342
1343 // Compute the flux of @p form from the current fields and its boundary
1344 // coefficients; see BuildBoundaryPhys() for @p forcing and @p rhs.
1345 void UpdateFlux(const FluxForm form,
1349 {
1351 (rhs && *rhs),
1352 "HOPressureBndCond requires the explicit contribution rhs on "
1353 "a region with a velocity component that is not Dirichlet.");
1355 "SetVelocityContext must be called before Update.");
1356 ASSERTL1(this->m_isSetKinvis,
1357 "SetKinvis must be called before Update.");
1358 if (!m_curlCurlOp)
1359 {
1360 return;
1361 }
1362 // The fields on the boundary-adjacent elements take the interleave
1363 // width of the velocity, and the forcing is gathered into them at
1364 // that width too.
1365 const unsigned int interleaveWidth =
1366 velocity.GetBlocks()[m_volumeGather[0].srcBlock]
1367 .GetInterleaveWidth();
1369 SetRestrictedInterleaveWidth(m_curlCurl, interleaveWidth);
1372 BuildBoundaryPhys(form, forcing, rhs, this->m_kinvis, this->m_scale);
1373 }
1374
1375 // Evaluate on every region, into m_bndWorkPhys, the Dirichlet velocity
1376 // as the velocity solve imposes it, at the time set by SetTime(): the
1377 // session expression, projected onto the boundary expansion of the
1378 // velocity with FwdTransBCOp, as DirBndCondOp sets the Dirichlet
1379 // coefficients of the velocity, and evaluated at its points with
1380 // BwdTransOp. The coefficients of a region without a time-dependent
1381 // Dirichlet velocity condition are computed only once. A point region
1382 // takes the expression itself. m_bndWorkPhys is free once
1383 // BuildBoundaryPhys() has combined the flux, and is written on the
1384 // streams of the boundary blocks, which read it there.
1386 {
1387 for (unsigned int n = 0; n < m_bndWorkPhys.size(); ++n)
1388 {
1389 const bool isCoeffsCurrent =
1391 if (m_bndIsPoint[n] || !isCoeffsCurrent)
1392 {
1393 m_bndVelocityExpressionOps[n]->SetTime(this->m_time);
1395 m_bndWorkPhys[n]);
1396 }
1397 if (m_bndIsPoint[n])
1398 {
1399 continue;
1400 }
1401 if (!isCoeffsCurrent)
1402 {
1403 // The forward transform assumes a zero output, of which it
1404 // sets the vertex modes first.
1405 Math::zero<ExecSpace>(m_bndVelocityCoeffs[n]);
1408 }
1410 m_bndWorkPhys[n]);
1411 }
1413 }
1414
1416 {
1418 for (unsigned int n = 0; n < m_bndPhys.size(); ++n)
1419 {
1420 for (unsigned int blk = 0;
1421 blk < m_bndWorkPhys[n].GetBlocks().size(); ++blk)
1422 {
1423 const unsigned int streamID = blk + 1;
1424 auto &velocityBlk = m_bndWorkPhys[n].GetBlocks()[blk];
1425 auto &normalBlk = m_bndNormals[n].GetBlocks()[blk];
1426 auto &fluxBlk = m_bndPhys[n].GetBlocks()[blk];
1427 auto velocityPtr =
1428 velocityBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
1429 auto normalPtr =
1430 normalBlk.template GetPtr<MemSpace, ReadOnly>(streamID);
1431 auto fluxPtr =
1432 fluxBlk.template GetPtr<MemSpace, ReadWrite>(streamID);
1433 const size_t nPoints =
1434 fluxBlk.GetNumElementsWithPadding() * fluxBlk.GetNumData();
1435 if (m_coordDim == 2)
1436 {
1437 HOPressureSubtractNormalVelocityKernel<ExecSpace, 2>(
1438 nPoints, velocityBlk.CompSize(), this->m_scale,
1439 velocityPtr, normalPtr, fluxPtr, streamID);
1440 }
1441 else if (m_coordDim == 3)
1442 {
1443 HOPressureSubtractNormalVelocityKernel<ExecSpace, 3>(
1444 nPoints, velocityBlk.CompSize(), this->m_scale,
1445 velocityPtr, normalPtr, fluxPtr, streamID);
1446 }
1447 }
1448 }
1449 }
1450
1451 void FinaliseUpdate(const FluxForm form)
1452 {
1453 BuildBoundaryCoeffs(form);
1454 m_isUpdated = true;
1455 }
1456
1458 {
1460 {
1461 return;
1462 }
1463
1464 for (unsigned int blk = 0; blk < inout.GetBlocks().size(); ++blk)
1465 {
1466 const unsigned int streamID = blk + 1;
1467
1468 // The targets index the block at its own interleave width.
1469 auto &inoutBlk = inout.GetBlocks()[blk];
1470 const unsigned int interleaveWidth = inoutBlk.GetInterleaveWidth();
1471
1472 auto inoutPtr =
1473 inoutBlk.template GetPtr<MemSpace, ReadWrite>(streamID);
1474 auto bndCoeffPtr =
1475 m_bndCoeff[blk].template GetPtr<MemSpace, ReadOnly>(streamID);
1476
1477 // Targets written by one boundary coefficient.
1478 const size_t nUnique = m_uniqueCounts[blk];
1479 if (nUnique > 0)
1480 {
1481 auto mapPtr =
1482 TargetsAtInterleaveWidth(m_map[blk], interleaveWidth,
1483 streamID)
1484 .template GetPtr<MemSpace, ReadOnly>(streamID);
1485 if (m_anySignChange)
1486 {
1487 auto signPtr =
1488 m_sign[blk].template GetPtr<MemSpace, ReadOnly>(
1489 streamID);
1490 MultiRegions::detail::NeuBndCondKernel<ExecSpace>(
1491 nUnique, signPtr, mapPtr, bndCoeffPtr, inoutPtr,
1492 streamID);
1493 }
1494 else
1495 {
1496 MultiRegions::detail::NeuBndCondKernel<ExecSpace>(
1497 nUnique, mapPtr, bndCoeffPtr, inoutPtr, streamID);
1498 }
1499 }
1500
1501 // Targets written by more than one boundary coefficient, summed
1502 // before one add.
1503 const size_t nGroups = m_groupCounts[blk];
1504 if (nGroups > 0)
1505 {
1506 auto groupOffsetPtr =
1507 m_groupOffset[blk].template GetPtr<MemSpace, ReadOnly>(
1508 streamID);
1509 auto groupTargetPtr =
1511 interleaveWidth, streamID)
1512 .template GetPtr<MemSpace, ReadOnly>(streamID);
1513 auto groupValuePtr =
1514 m_groupValue[blk].template GetPtr<MemSpace, ReadOnly>(
1515 streamID);
1516
1517 if (m_anySignChange)
1518 {
1519 auto groupSignPtr =
1520 m_groupSign[blk].template GetPtr<MemSpace, ReadOnly>(
1521 streamID);
1522 MultiRegions::detail::NeuBndCondGroupKernel<ExecSpace>(
1523 nGroups, groupOffsetPtr, groupTargetPtr, groupSignPtr,
1524 groupValuePtr, inoutPtr, streamID);
1525 }
1526 else
1527 {
1528 MultiRegions::detail::NeuBndCondGroupKernel<ExecSpace>(
1529 nGroups, groupOffsetPtr, groupTargetPtr, groupValuePtr,
1530 inoutPtr, streamID);
1531 }
1532 }
1533 }
1534 }
1535};
1536
1537} // namespace Nektar::detail
Public interface of the backward-transform (BwdTrans) element operator, which maps a field from coeff...
Whole-field interface of the curl-curl element operator, which evaluates curl(curl u) at the quadratu...
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Public interface of the Expression operator, which evaluates analytical expressions at the quadrature...
Whole-field interface of the IProductWRTBase element operator, which takes a physical-space field to ...
int64_t id
Higher-order pressure boundary condition, added to the pressure system as a Neumann contribution.
static const std::string userDefinedType
USERDEFINEDTYPE of the pressure Neumann conditions the operator applies.
bool m_isSetKinvis
Set by SetKinvis(); checked by Update() in debug builds.
TData m_kinvis
Kinematic viscosity, set by SetKinvis().
TData m_time
Time of the prescribed boundary velocity, set by SetTime().
TData m_scale
Scale of the fields passed to Update(), set by SetScale().
bool m_hasNonDirichletVelocity
Set by SetVelocityContext(); read by HasNonDirichletVelocity().
A Field represents expansion data to be operated on.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
A MemoryRegion represents a memory region.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static std::shared_ptr< BwdTransOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Create a backward-transform operator through the operator and block-operator factories.
static std::shared_ptr< CurlCurlOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Build a complete curl-curl operator through the operator and block-operator factories.
static std::shared_ptr< ExpressionOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Create an Expression operator through the two-stage element-operator factory and initialise its defau...
static std::shared_ptr< FwdTransBCOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
static std::shared_ptr< IProductWRTBaseOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="", const std::string &implStr="")
Build a complete IProductWRTBase operator through the operator and block-operator factories.
std::vector< std::string > m_components
Names of the field components the operator was built for.
Definition Operator.hpp:351
MultiRegions::ExpListSharedPtr m_expansionList
Expansion list the operator acts on.
Definition Operator.hpp:349
Adds the surface integral of boundary fields, per component and homogeneous plane,...
Extracts a volume field onto the points of a boundary region of a continuous field.
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition Conditions.h:273
const BoundaryConditionCollection & GetBoundaryConditions(void) const
Definition Conditions.h:283
Implementation of HOPressureBndCondOp.
std::vector< LibUtilities::Field< TData, FieldState::Coeff > > m_bndCoeffs
std::vector< std::shared_ptr< MultiRegions::FwdTransBCOp< TData > > > m_bndVelocityFwdTransOps
static bool IsHOPressure(const SpatialDomains::BoundaryConditionShPtr &condition)
LibUtilities::MemoryRegion< TData > m_compactBndCoeff
std::vector< LibUtilities::MemoryRegion< size_t > > m_groupSrc
static std::unique_ptr< MultiRegions::Operator< TData > > Instantiate(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components)
MultiRegions::ExpListSharedPtr m_bndElmtExpansionList
MultiRegions::detail::CGBndCondSurfaceIntegral< ExecSpace, TData > m_surfaceIntegral
void v_SetVelocityContext(const MultiRegions::ExpListSharedPtr &velocityExpansionList, const std::vector< std::string > &velocityComponents) override
std::vector< MultiRegions::ExpListSharedPtr > m_bndExpLists
std::shared_ptr< MultiRegions::CurlCurlOp< TData > > m_curlCurlOp
void v_AddNeumannSurfaceIntegral(LibUtilities::MemoryRegion< TData > &data) override
static LibUtilities::MemoryRegion< size_t > & IndexAtInterleaveWidth(InterleaveWidthIndices &indices, const unsigned int interleaveWidth, const unsigned int streamID, BuildIndexFn &&buildIndex)
static GatherIndices & GatherIndicesAtInterleaveWidths(VolumeGather &gather, const unsigned int srcInterleaveWidth, const unsigned int dstInterleaveWidth, const unsigned int streamID)
static std::vector< size_t > PointIndex(const VolumeGather &gather, const std::vector< size_t > &elmts, const unsigned int interleaveWidth, const unsigned int dstInterleaveWidth)
void BuildBoundaryPhys(const FluxForm form, LibUtilities::Field< TData, FieldState::Phys > &forcing, LibUtilities::Field< TData, FieldState::Phys > *rhs, const TData kinvis, const TData scale)
LibUtilities::Field< TData, FieldState::Phys > m_bndElmtWork
HOPressureBndCondOpImpl(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components)
void UpdateFlux(const FluxForm form, LibUtilities::Field< TData, FieldState::Phys > &forcing, LibUtilities::Field< TData, FieldState::Phys > &velocity, LibUtilities::Field< TData, FieldState::Phys > *rhs)
LibUtilities::Field< TData, FieldState::Phys > m_curlCurl
void v_UpdateBndCoeffs(const TData &time) override
std::vector< LibUtilities::MemoryRegion< TData > > m_groupValue
std::vector< LibUtilities::MemoryRegion< TData > > m_groupSign
std::vector< LibUtilities::MemoryRegion< size_t > > m_bndCoeffSrc
void v_Update(LibUtilities::Field< TData, FieldState::Phys > &rhs, LibUtilities::Field< TData, FieldState::Phys > &velocity) override
std::vector< std::shared_ptr< MultiRegions::detail::PhysTraceExtractBndCond< ExecSpace, TData > > > m_bndFluxExtractOps
std::vector< std::shared_ptr< MultiRegions::ExpressionOp< TData > > > m_bndVelocityExpressionOps
static LibUtilities::MemoryRegion< size_t > & TargetsAtInterleaveWidth(CoeffTargets &targets, const unsigned int interleaveWidth, const unsigned int streamID)
static std::vector< size_t > TargetIndex(const CoeffTargets &targets, const unsigned int interleaveWidth)
std::vector< LibUtilities::Field< TData, FieldState::Phys > > m_bndNormals
std::vector< LibUtilities::MemoryRegion< size_t > > m_groupOffset
std::vector< LibUtilities::MemoryRegion< TData > > m_bndCoeff
static void SetRestrictedInterleaveWidth(LibUtilities::Field< TData, FieldState::Phys > &field, const unsigned int interleaveWidth)
void v_UpdateWithAcceleration(LibUtilities::Field< TData, FieldState::Phys > &rhs, LibUtilities::Field< TData, FieldState::Phys > &velocity, LibUtilities::Field< TData, FieldState::Phys > &predictor) override
void v_Apply(LibUtilities::Field< TData, FieldState::Coeff > &inout) override
void BuildBoundaryElementData(const MultiRegions::ExpListSharedPtr &velocityExpansionList, const std::vector< std::string > &velocityComponents)
void BuildBoundaryVelocityData(const MultiRegions::ExpListSharedPtr &velocityExpansionList, const std::vector< std::string > &velocityComponents)
std::vector< std::shared_ptr< MultiRegions::IProductWRTBaseOp< TData > > > m_bndIProductOps
std::vector< CoeffTargets > BuildCoeffTargets(std::vector< LibUtilities::MemoryRegion< size_t > > &unitWidthIndex, const MultiRegions::ExpListSharedPtr &expansionList)
std::vector< LibUtilities::MemoryRegion< TData > > m_sign
std::vector< LibUtilities::Field< TData, FieldState::Phys > > m_bndNonDirichletNormals
void GatherBoundaryElements(LibUtilities::Field< TData, FieldState::Phys > &src, LibUtilities::Field< TData, FieldState::Phys > &dst)
std::vector< LibUtilities::Field< TData, FieldState::Coeff > > m_bndVelocityCoeffs
void v_UpdateWeak(LibUtilities::Field< TData, FieldState::Phys > &rhs, LibUtilities::Field< TData, FieldState::Phys > &velocity, LibUtilities::Field< TData, FieldState::Phys > &predictor) override
std::vector< LibUtilities::Field< TData, FieldState::Phys > > m_bndWorkPhys
std::vector< std::shared_ptr< MultiRegions::BwdTransOp< TData > > > m_bndVelocityBwdTransOps
std::vector< LibUtilities::Field< TData, FieldState::Phys > > m_bndPhys
void v_ExcludeUserDefinedType(const std::string &userDefinedType) override
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition Expansion.h:66
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
Collections::Collection GetCollection(MultiRegions::ExpListSharedPtr expansionList, unsigned int block_idx)
std::shared_ptr< BoundaryConditionBase > BoundaryConditionShPtr
Definition Conditions.h:251
std::map< int, ExpansionInfoShPtr > ExpansionInfoMap
Definition MeshGraph.h:160
bool IEquals(std::string_view a, std::string_view b)
std::map< std::pair< unsigned int, unsigned int >, GatherIndices > indices
VolumeGather & operator=(const VolumeGather &)=delete
VolumeGather & operator=(VolumeGather &&)=default