Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://www.mooseframework.org 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "KokkosDispatcher.h" 13 : #include "KokkosDatum.h" 14 : #include "KokkosMesh.h" 15 : #include "KokkosSystem.h" 16 : #include "KokkosVariable.h" 17 : 18 : #include "BoundaryRestrictableRequired.h" 19 : #include "FunctionInterface.h" 20 : #include "MeshChangedInterface.h" 21 : #include "MooseObject.h" 22 : #include "PostprocessorInterface.h" 23 : #include "Restartable.h" 24 : #include "SetupInterface.h" 25 : #include "TransientInterface.h" 26 : #include "UserObjectInterface.h" 27 : #include "VectorPostprocessorInterface.h" 28 : 29 : class FEProblemBase; 30 : class InputParameters; 31 : 32 : namespace Moose::Kokkos 33 : { 34 : 35 : /** 36 : * Base class for Kokkos linear finite volume boundary conditions. Boundary conditions provide 37 : * boundary data relations for flux kernels; they do not assemble directly into tagged vectors or 38 : * matrices. 39 : */ 40 : class LinearFVBoundaryCondition : public MooseObject, 41 : public SetupInterface, 42 : public FunctionInterface, 43 : public UserObjectInterface, 44 : public TransientInterface, 45 : public PostprocessorInterface, 46 : public VectorPostprocessorInterface, 47 : public Restartable, 48 : public MeshChangedInterface, 49 : public BoundaryRestrictableRequired, 50 : public MeshHolder, 51 : public SystemHolder 52 : { 53 : public: 54 : static InputParameters validParams(); 55 : 56 : LinearFVBoundaryCondition(const InputParameters & parameters); 57 : LinearFVBoundaryCondition(const LinearFVBoundaryCondition & object); 58 : 59 : /// Tag dispatch type for boundary value relation computation 60 : struct BoundaryValueLoop 61 : { 62 : }; 63 : /// Tag dispatch type for boundary normal gradient relation computation 64 : struct BoundaryNormalGradientLoop 65 : { 66 : }; 67 : 68 : /** 69 : * Affine boundary relation used by Kokkos linear FV kernels: 70 : * boundary_quantity = coefficient * cell_unknown + source 71 : */ 72 : struct BoundaryRelation 73 : { 74 : Real coefficient = 0; 75 : Real source = 0; 76 : }; 77 : 78 : /// Boundary relation coefficient/source arrays indexed by BC-local boundary face 79 : struct BoundaryRelationData 80 : { 81 : Array<Real> coefficient; 82 : Array<Real> source; 83 : }; 84 : 85 : /// Boundary relation data indexed by relation type and BC-local boundary face 86 : struct BoundaryData 87 : { 88 : BoundaryRelationData value; 89 : BoundaryRelationData normal_gradient; 90 : }; 91 : 92 : virtual void initialSetup() override; 93 : 94 : /** 95 : * Get the Kokkos variable this boundary condition supplies data for 96 : * @returns The Kokkos variable 97 : */ 98 1377 : Variable variable() const { return _var; } 99 : 100 : /** 101 : * Get the boundary data relation arrays owned by this boundary condition 102 : */ 103 395 : const BoundaryData & boundaryData() const { return _boundary_data; } 104 : 105 : /// Number of faces in this boundary condition's existing worklist 106 : MOOSE_KOKKOS_INDEX_TYPE numBoundaryFaces() const; 107 : 108 : /// Face at a BC-local worklist index 109 : Pair<ContiguousElementID, unsigned int> 110 : boundaryFaceID(const MOOSE_KOKKOS_INDEX_TYPE bc_face_index) const; 111 : 112 : /// Whether this boundary condition overrides the boundary value relation hook 113 : bool hasBoundaryValue() const; 114 : /// Whether this boundary condition overrides the boundary normal gradient relation hook 115 : bool hasBoundaryNormalGradient() const; 116 : 117 : /// Dispatch boundary value relation computation 118 : void computeBoundaryValueData(); 119 : /// Dispatch boundary normal gradient relation computation 120 : void computeBoundaryNormalGradientData(); 121 : 122 : /** 123 : * Default boundary value relation hook. Derived boundary conditions should override this when 124 : * they can supply boundary values. 125 : */ 126 : template <typename Derived> 127 0 : KOKKOS_FUNCTION BoundaryRelation computeBoundaryValue(const FVDatum &) const 128 : { 129 0 : ::Kokkos::abort("Default computeBoundaryValue() should never be called. Make sure you " 130 : "properly redefined this method in your class without typos."); 131 : 132 : return {}; 133 : } 134 : 135 : /** 136 : * Default boundary normal gradient relation hook. Derived boundary conditions should override 137 : * this when they can supply boundary normal gradients. 138 : */ 139 : template <typename Derived> 140 0 : KOKKOS_FUNCTION BoundaryRelation computeBoundaryNormalGradient(const FVDatum &) const 141 : { 142 0 : ::Kokkos::abort("Default computeBoundaryNormalGradient() should never be called. Make sure " 143 : "you properly redefined this method in your class without typos."); 144 : 145 : return {}; 146 : } 147 : 148 : /** 149 : * Functions used to check whether derived boundary conditions override the boundary data hooks. 150 : */ 151 : ///@{ 152 : template <typename Derived> 153 82208 : static auto defaultBoundaryValue() 154 : { 155 82208 : return &LinearFVBoundaryCondition::computeBoundaryValue<Derived>; 156 : } 157 : 158 : template <typename Derived> 159 82208 : static auto defaultBoundaryNormalGradient() 160 : { 161 82208 : return &LinearFVBoundaryCondition::computeBoundaryNormalGradient<Derived>; 162 : } 163 : ///@} 164 : 165 : /** 166 : * Boundary value dispatch loop body; writes the per-face boundary value relation 167 : * @param tid The thread ID of the current boundary side 168 : * @param bc The concrete boundary condition object 169 : */ 170 : template <typename Derived> 171 : KOKKOS_FUNCTION void operator()(BoundaryValueLoop, const ThreadID tid, const Derived & bc) const; 172 : 173 : /** 174 : * Boundary normal gradient dispatch loop body; writes the per-face boundary normal gradient 175 : * relation 176 : * @param tid The thread ID of the current boundary side 177 : * @param bc The concrete boundary condition object 178 : */ 179 : template <typename Derived> 180 : KOKKOS_FUNCTION void 181 : operator()(BoundaryNormalGradientLoop, const ThreadID tid, const Derived & bc) const; 182 : 183 : protected: 184 : /// Reference to the finite element problem 185 : FEProblemBase & _fe_problem; 186 : 187 : /// Kokkos variable 188 : Variable _var; 189 : 190 : /// Boundary relation arrays owned by this boundary condition 191 : BoundaryData _boundary_data; 192 : 193 : /// Dispatcher for boundary value computation 194 : std::unique_ptr<DispatcherBase> _boundary_value_dispatcher; 195 : /// Dispatcher for boundary normal gradient computation 196 : std::unique_ptr<DispatcherBase> _boundary_normal_gradient_dispatcher; 197 : 198 : /** 199 : * TODO: Move to TransientInterface 200 : */ 201 : ///@{ 202 : /// Current time 203 : Scalar<Real> _t; 204 : /// Old (previous time step) time 205 : Scalar<const Real> _t_old; 206 : /// Current time step number 207 : Scalar<int> _t_step; 208 : /// Current time step size 209 : Scalar<Real> _dt; 210 : /// Previous time step size 211 : Scalar<Real> _dt_old; 212 : ///@} 213 : }; 214 : 215 : template <typename Derived> 216 : KOKKOS_FUNCTION void 217 28 : LinearFVBoundaryCondition::operator()(BoundaryValueLoop, 218 : const ThreadID tid, 219 : const Derived & bc) const 220 : { 221 28 : const auto [elem, side] = kokkosBoundaryElementSideID(tid); 222 28 : FVDatum datum(elem, side, kokkosMesh()); 223 28 : const auto relation = bc.template computeBoundaryValue<Derived>(datum); 224 28 : _boundary_data.value.coefficient[tid] = relation.coefficient; 225 28 : _boundary_data.value.source[tid] = relation.source; 226 28 : } 227 : 228 : template <typename Derived> 229 : KOKKOS_FUNCTION void 230 1570 : LinearFVBoundaryCondition::operator()(BoundaryNormalGradientLoop, 231 : const ThreadID tid, 232 : const Derived & bc) const 233 : { 234 1570 : const auto [elem, side] = kokkosBoundaryElementSideID(tid); 235 1570 : FVDatum datum(elem, side, kokkosMesh()); 236 1570 : const auto relation = bc.template computeBoundaryNormalGradient<Derived>(datum); 237 1570 : _boundary_data.normal_gradient.coefficient[tid] = relation.coefficient; 238 1570 : _boundary_data.normal_gradient.source[tid] = relation.source; 239 1570 : } 240 : 241 : } // namespace Moose::Kokkos