Skip to content

Commit

Permalink
Add DivField and GradField kernels (idaholab#26105)
Browse files Browse the repository at this point in the history
Co-authored-by: karthichockalingam <karthikeyan.chockalingam@stfc.ac.uk>
  • Loading branch information
nmnobre and karthichockalingam committed Nov 30, 2023
1 parent 91ee5f2 commit d810382
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
40 changes: 40 additions & 0 deletions framework/include/kernels/DivField.h
@@ -0,0 +1,40 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "Kernel.h"

/**
* Weak form contribution corresponding to k*div(u)
*/
class DivField : public Kernel
{
public:
static InputParameters validParams();

DivField(const InputParameters & parameters);

protected:
virtual Real computeQpResidual() override;
virtual Real computeQpOffDiagJacobian(unsigned jvar) override;

/// coupled vector variable
VectorMooseVariable & _u_var;
unsigned int _u_var_num;

/// div of the coupled vector variable
const VectorVariableDivergence & _div_u;

/// div of the shape function of the coupled vector variable
const VectorVariablePhiDivergence & _div_phi;

/// scalar coefficient
Real _coeff;
};
43 changes: 43 additions & 0 deletions framework/include/kernels/GradField.h
@@ -0,0 +1,43 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "VectorKernel.h"

/**
* Weak form contribution corresponding to -k*grad(p)
*/
class GradField : public VectorKernel
{
public:
static InputParameters validParams();

GradField(const InputParameters & parameters);

protected:
virtual Real computeQpResidual() override;
virtual Real computeQpOffDiagJacobian(unsigned jvar) override;

/// coupled scalar variable
MooseVariable & _p_var;
unsigned int _p_var_num;

/// value of the coupled scalar variable
const VariableValue & _p;

/// shape function of the coupled scalar variable
const VariablePhiValue & _p_phi;

/// div of the test function
const VectorVariableTestDivergence & _div_test;

/// scalar coefficient
Real _coeff;
};
49 changes: 49 additions & 0 deletions framework/src/kernels/DivField.C
@@ -0,0 +1,49 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "DivField.h"
#include "Assembly.h"

registerMooseObject("MooseApp", DivField);

InputParameters
DivField::validParams()
{
InputParameters params = Kernel::validParams();
params.addClassDescription("Takes the divergence of a vector field, optionally"
"scaled by a constant scalar coefficient.");
params.addRequiredCoupledVar("coupled_vector_variable", "The vector field");
params.addParam<Real>("coeff", 1.0, "The constant coefficient");
return params;
}

DivField::DivField(const InputParameters & parameters)
: Kernel(parameters),
_u_var(*getVectorVar("coupled_vector_variable", 0)),
_u_var_num(coupled("coupled_vector_variable")),
_div_u(_u_var.divSln()),
_div_phi(_assembly.divPhi(_u_var)),
_coeff(getParam<Real>("coeff"))
{
}

Real
DivField::computeQpResidual()
{
return _coeff * _div_u[_qp] * _test[_i][_qp];
}

Real
DivField::computeQpOffDiagJacobian(unsigned int jvar)
{
if (_u_var_num == jvar)
return _coeff * _div_phi[_j][_qp] * _test[_i][_qp];

return 0.0;
}
50 changes: 50 additions & 0 deletions framework/src/kernels/GradField.C
@@ -0,0 +1,50 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "GradField.h"
#include "Assembly.h"

registerMooseObject("MooseApp", GradField);

InputParameters
GradField::validParams()
{
InputParameters params = VectorKernel::validParams();
params.addClassDescription("Takes the gradient of a scalar field, optionally"
"scaled by a constant scalar coefficient.");
params.addRequiredCoupledVar("coupled_scalar_variable", "The scalar field");
params.addParam<Real>("coeff", 1.0, "The constant coefficient");
return params;
}

GradField::GradField(const InputParameters & parameters)
: VectorKernel(parameters),
_p_var(*getVar("coupled_scalar_variable", 0)),
_p_var_num(coupled("coupled_scalar_variable")),
_p(coupledValue("coupled_scalar_variable")),
_p_phi(_assembly.phi(_p_var)),
_div_test(_var.divPhi()),
_coeff(getParam<Real>("coeff"))
{
}

Real
GradField::computeQpResidual()
{
return _coeff * _p[_qp] * _div_test[_i][_qp];
}

Real
GradField::computeQpOffDiagJacobian(unsigned int jvar)
{
if (_p_var_num == jvar)
return _coeff * _p_phi[_j][_qp] * _div_test[_i][_qp];

return 0.0;
}

0 comments on commit d810382

Please sign in to comment.