Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 "MooseTypes.h" 13 : #include "GradientLimiterType.h" 14 : 15 : #include "libmesh/utility.h" 16 : 17 : #include <memory> 18 : #include <string> 19 : #include <unordered_map> 20 : #include <unordered_set> 21 : #include <vector> 22 : 23 : class SystemBase; 24 : 25 : namespace libMesh 26 : { 27 : template <typename T> 28 : class NumericVector; 29 : } 30 : 31 : /** 32 : * Shared storage and allocation logic for linear finite-volume cell gradients for 33 : * variables in the system attribute of this class 34 : */ 35 : class LinearFVGradientInterface 36 : { 37 : public: 38 62732 : LinearFVGradientInterface(SystemBase & sys) : _sys(sys) {} 39 : 40 : /** 41 : * Access the stored raw cell-centered gradient components. 42 : * @return Raw cell-centered gradient vectors keyed by spatial direction. 43 : */ 44 : const std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> & 45 1228 : linearFVGradientContainer() const 46 : { 47 1228 : return _raw_grad_container; 48 : } 49 : 50 : /** 51 : * Request storage and assembly of limiter-specific cell gradients. 52 : * @param limiter_type The limiter whose gradient storage should be made available. 53 : * @param variable_number The libMesh variable number requesting the limited gradients. 54 : */ 55 : void requestLinearFVLimitedGradients(const Moose::FV::GradientLimiterType limiter_type, 56 : unsigned int variable_number); 57 : 58 : /** 59 : * Access the stored raw or limited cell-centered gradient components. 60 : * @param limiter_type The limiter type whose gradient container is being requested. 61 : * @return The requested raw or limited gradient vectors ordered by spatial direction. 62 : */ 63 : const std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> & 64 : linearFVLimitedGradientContainer(const Moose::FV::GradientLimiterType limiter_type) const; 65 : 66 : /** 67 : * Access the limiter types requested for this system. 68 : * @return The set of limiter types whose limited gradients should be assembled. 69 : * They are only assembled for the variable(s) for which they were requested not all of them 70 : */ 71 : const std::unordered_set<Moose::FV::GradientLimiterType> & 72 55447 : requestedLinearFVLimitedGradientTypes() const 73 : { 74 55447 : return _requested_limited_gradient_types; 75 : } 76 : 77 : protected: 78 : /** 79 : * Compute and store raw and requested limited Green-Gauss gradients for linear FV variables. 80 : */ 81 : void computeGradients(); 82 : 83 : /** 84 : * Rebuild persistent raw and temporary gradient storage after mesh/DOF changes. 85 : */ 86 : void rebuildLinearFVGradientStorage(); 87 : 88 : /** 89 : * Return temporary storage for gradients during gradient assembly. 90 : * The returned vectors are persistent scratch storage reused across calls and swapped with the 91 : * final gradient container before gradient assembly returns. 92 : */ 93 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> & 94 43577 : temporaryLinearFVGradientContainer() 95 : { 96 43577 : return _temporary_gradient; 97 : } 98 : 99 : /** 100 : * Return temporary storage for limited gradients during gradient assembly. 101 : * The returned vectors are persistent scratch storage reused across calls and swapped with the 102 : * final limited-gradient container before gradient assembly returns. 103 : * @param limiter_type The limiter type whose temporary storage is being accessed. 104 : */ 105 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> & 106 11870 : temporaryLinearFVLimitedGradientContainer(const Moose::FV::GradientLimiterType limiter_type) 107 : { 108 11870 : return _temporary_limited_gradient[limiter_type]; 109 : } 110 : 111 : /** 112 : * Access the persisted limited-gradient storage for a specific limiter. 113 : * @param limiter_type The limiter type whose persisted storage is being accessed. 114 : * @return The persisted limited-gradient vectors keyed by spatial direction. 115 : */ 116 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> & 117 11870 : rawLinearFVLimitedGradientContainer(const Moose::FV::GradientLimiterType limiter_type) 118 : { 119 11870 : return _raw_limited_grad_containers[limiter_type]; 120 : } 121 : 122 : /** 123 : * Access the variable numbers that requested limited gradients for a specific limiter. 124 : * @param limiter_type The limiter type whose request set is being accessed. 125 : * @return The set of variable numbers that requested the limiter. 126 : */ 127 : const std::unordered_set<unsigned int> & 128 11870 : requestedLinearFVLimitedGradientVariables(const Moose::FV::GradientLimiterType limiter_type) const 129 : { 130 11870 : return libmesh_map_find(_requested_limited_gradient_variables, limiter_type); 131 : } 132 : 133 : bool needsLinearFVGradientStorage() const; 134 : 135 : void initializeContainer( 136 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> & container) const; 137 : 138 : /// Reference to the system object 139 : SystemBase & _sys; 140 : 141 : /// Scratch storage for raw gradients assembled during the current compute pass. 142 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> _temporary_gradient; 143 : 144 : /// Persisted raw cell-centered gradient components keyed by spatial direction. 145 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>> _raw_grad_container; 146 : 147 : /// Set of requested limiter types for which limited gradients should be computed. 148 : std::unordered_set<Moose::FV::GradientLimiterType> _requested_limited_gradient_types; 149 : 150 : /// Variable numbers requesting limited gradients, keyed by limiter type. 151 : std::unordered_map<Moose::FV::GradientLimiterType, std::unordered_set<unsigned int>> 152 : _requested_limited_gradient_variables; 153 : 154 : /// Persisted limited gradient components keyed by limiter type. 155 : std::unordered_map<Moose::FV::GradientLimiterType, 156 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>>> 157 : _raw_limited_grad_containers; 158 : 159 : /// Scratch storage for limited gradients assembled during the current compute pass. 160 : std::unordered_map<Moose::FV::GradientLimiterType, 161 : std::vector<std::unique_ptr<libMesh::NumericVector<libMesh::Number>>>> 162 : _temporary_limited_gradient; 163 : };