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 "ADKernel.h" 13 : 14 : #include <set> 15 : 16 : /** 17 : * This ADKernel adds standardized methods for assembling to a primary 18 : * scalar variable associated with the primary variable of the ADKernel 19 : * object. Essentially, the entire row of the residual and Jacobian 20 : * associated with this scalar variable will also be assembled here 21 : * using the loops over volumetric elements. 22 : * This variable is "scalar_variable" in the input file and "kappa" 23 : * within the source code. 24 : */ 25 : class ADKernelScalarBase : public ADKernel 26 : { 27 : public: 28 : static InputParameters validParams(); 29 : 30 : ADKernelScalarBase(const InputParameters & parameters); 31 : 32 : /** 33 : * The scalar variable that this kernel operates on. 34 : */ 35 : const MooseVariableScalar & scalarVariable() const 36 : { 37 : mooseAssert(_kappa_var_ptr, "kappa pointer should have been set in the constructor"); 38 : return *_kappa_var_ptr; 39 : } 40 : 41 : virtual void computeResidual() override; 42 : virtual void computeJacobian() override; 43 : /** 44 : * Computes d-_var-residual / d-jvar as well as d-_kappa-residual / d-jvar 45 : */ 46 : virtual void computeOffDiagJacobian(unsigned int jvar) override; 47 : /** 48 : * Computes jacobian block with respect to a scalar variable 49 : * @param jvar, the number of the (other) scalar variable 50 : */ 51 : void computeOffDiagJacobianScalar(unsigned int /*jvar_num*/) override; 52 : 53 : /** 54 : * Computes residual and jacobian block for field and scalar variables 55 : */ 56 : void computeResidualAndJacobian() override; 57 : 58 : /// Inform MOOSE that this kernel also computes a residual for its coupled scalar variable 59 : std::set<std::string> additionalROVariables() override; 60 : 61 : protected: 62 : /** 63 : * Method for computing the scalar part of residual at quadrature points 64 : */ 65 : virtual ADReal computeScalarQpResidual(); 66 : 67 : /** 68 : * compute the \p _scalar_residuals member for filling the Jacobian. We want to calculate these 69 : * residuals up-front when doing loal derivative indexing because we can use those residuals to 70 : * fill \p _local_ke for every associated jvariable. We do not want to re-do these calculations 71 : * for every jvariable and corresponding \p _local_ke. For global indexing we will simply pass 72 : * the computed \p _residuals directly to \p Assembly::addJacobian 73 : */ 74 : virtual void computeScalarResidualsForJacobian(); 75 : 76 : /** 77 : * For coupling scalar variables 78 : * Added solely for GenericKernelScalar override; should not be used 79 : */ 80 0 : virtual Real computeQpOffDiagJacobianScalar(unsigned int /*jvar*/) { return 0; } 81 : 82 : /** 83 : * Method for computing the scalar variable part of Jacobian at quadrature points 84 : * Added solely for GenericKernelScalar override; should not be used 85 : */ 86 0 : virtual Real computeScalarQpJacobian() { return 0; } 87 : 88 : /** 89 : * Method for computing an off-diagonal jacobian component at quadrature points. 90 : * Added solely for GenericKernelScalar override; should not be used 91 : */ 92 0 : virtual Real computeScalarQpOffDiagJacobian(const unsigned int /*jvar_num*/) { return 0; } 93 : 94 : /** 95 : * Method for computing an off-diagonal jacobian component at quadrature points. 96 : * Added solely for GenericKernelScalar override; should not be used 97 : */ 98 0 : virtual Real computeScalarQpOffDiagJacobianScalar(const unsigned int /*svar_num*/) { return 0; } 99 : 100 : /** 101 : * Put necessary evaluations depending on qp but independent of test functions here 102 : */ 103 14115 : virtual void initScalarQpResidual() {} 104 : 105 : /// Whether a scalar variable is declared for this kernel 106 : const bool _use_scalar; 107 : 108 : /// Whether to compute scalar contributions for this instance 109 : const bool _compute_scalar_residuals; 110 : 111 : /// Whether to compute field contributions for this instance 112 : const bool _compute_field_residuals; 113 : 114 : /// (Pointer to) Scalar variable this kernel operates on 115 : const MooseVariableScalar * const _kappa_var_ptr; 116 : 117 : /// The unknown scalar variable ID 118 : const unsigned int _kappa_var; 119 : 120 : /// Order of the scalar variable, used in several places 121 : const unsigned int _k_order; 122 : 123 : /// Reference to the current solution at the current quadrature point 124 : const ADVariableValue & _kappa; 125 : 126 : /// Used internally to iterate over each scalar component 127 : unsigned int _h; 128 : unsigned int _l; 129 : std::vector<ADReal> _scalar_residuals; 130 : }; 131 : 132 : inline ADReal 133 3 : ADKernelScalarBase::computeScalarQpResidual() 134 : { 135 3 : mooseError( 136 : "A scalar_variable has been set and compute_scalar_residuals=true, ", 137 : "but the computeScalarQpResidual method was not overridden. Accidental call of base class?"); 138 : return 0; 139 : }