Skip to content

Commit c0c3980

Browse files
committed
Intra-variable Jacobian (#32212)
1 parent 4545b09 commit c0c3980

File tree

8 files changed

+51
-47
lines changed

8 files changed

+51
-47
lines changed

framework/include/nodalkernels/ADArrayNodalKernel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ class ADArrayNodalKernel : public NodalKernelBase, public MooseVariableInterface
6666

6767
/**
6868
* Dummy method so we can make derived generic classes that template on <bool is_ad> */
69-
virtual RealEigenVector computeQpJacobian()
69+
virtual void computeQpJacobian()
7070
{
7171
mooseError("I'm an AD object, so computeQpJacobian should never be called");
7272
}
7373

74+
void setJacobian(unsigned int, unsigned int, Real)
75+
{
76+
mooseError("I'm an AD object, so setJacobian should never be called");
77+
}
78+
7479
/// variable this works on
7580
MooseVariableFE<RealEigenVector> & _var;
7681

framework/include/nodalkernels/ArrayNodalKernel.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,21 @@ class ArrayNodalKernel : public NodalKernelBase, public MooseVariableInterface<R
6565
virtual void computeQpResidual(RealEigenVector & residual) = 0;
6666

6767
/**
68-
* The user can override this function to compute the "on-diagonal"
69-
* Jacobian contribution. If not overriden,
70-
* returns an array of 1s.
71-
* @return On-diagonal Jacobian contribution
68+
* The user can override this function to compute the intra-variable
69+
* off-diagonal Jacobian contribution (the coupling between array
70+
* components of the variable itself). Use setJacobian in this function.
7271
*/
73-
virtual RealEigenVector computeQpJacobian();
72+
virtual void computeQpJacobian() {}
7473

7574
/**
76-
* This is the virtual that derived classes should override for
77-
* computing an off-diagonal jacobian component.
78-
* @param Coupled variable for the off-diagonal Jacobian contribution
79-
* @return Off-diagonal Jacobian contribution
75+
* The user can override this function to compute the inter-variable
76+
* off-diagonal Jacobian contribution (the coupling between array
77+
* components of of the kernel variable and the coupled variable jvar).
78+
* Use setJacobian in this function.
8079
*/
81-
virtual RealEigenMatrix computeQpOffDiagJacobian(const MooseVariableFieldBase & jvar);
80+
virtual void computeQpOffDiagJacobian(unsigned int /*jvar*/) {}
81+
82+
void setJacobian(unsigned int i, unsigned int j, Real value);
8283

8384
/// variable this works on
8485
MooseVariableFE<RealEigenVector> & _var;
@@ -92,4 +93,11 @@ class ArrayNodalKernel : public NodalKernelBase, public MooseVariableInterface<R
9293
private:
9394
/// Work vector for residual
9495
RealEigenVector _work_vector;
96+
97+
/// DOF indices
98+
const std::vector<dof_id_type> * _ivar_indices;
99+
const std::vector<dof_id_type> * _jvar_indices;
100+
101+
/// scaling factors
102+
const std::vector<Real> & _scaling;
95103
};

framework/include/nodalkernels/ArrayReactionNodalKernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ArrayReactionNodalKernelTempl : public GenericArrayNodalKernel<is_ad>
2424

2525
protected:
2626
virtual void computeQpResidual(GenericRealEigenVector<is_ad> & residual) override;
27-
virtual RealEigenVector computeQpJacobian() override;
27+
virtual void computeQpJacobian() override;
2828

2929
/// rate coefficient
3030
const RealEigenVector & _coeff;

framework/include/nodalkernels/ArrayTimeDerivativeNodalKernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ArrayTimeDerivativeNodalKernel : public ArrayNodalKernel
2323

2424
protected:
2525
virtual void computeQpResidual(RealEigenVector & residual) override;
26-
virtual RealEigenVector computeQpJacobian() override;
26+
virtual void computeQpJacobian() override;
2727

2828
/// Time derivative of u
2929
const ArrayVariableValue & _u_dot;

framework/include/nodalkernels/GenericArrayNodalKernel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ class GenericArrayNodalKernel<true> : public ADArrayNodalKernel
3535
using GenericArrayNodalKernel<is_ad>::_count; \
3636
using GenericArrayNodalKernel<is_ad>::_u; \
3737
using GenericArrayNodalKernel<is_ad>::_qp; \
38+
using GenericArrayNodalKernel<is_ad>::setJacobian; \
3839
using GenericArrayNodalKernel<is_ad>::paramError

framework/src/nodalkernels/ArrayNodalKernel.C

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ ArrayNodalKernel::ArrayNodalKernel(const InputParameters & parameters)
2929
_var(*mooseVariable()),
3030
_u(_var.dofValues()),
3131
_count(_var.count()),
32-
_work_vector(_count)
32+
_work_vector(_count),
33+
_scaling(_var.arrayScalingFactor())
3334
{
3435
addMooseVariableDependency(mooseVariable());
3536
}
@@ -41,7 +42,7 @@ ArrayNodalKernel::computeResidual()
4142
return;
4243
_qp = 0;
4344
computeQpResidual(_work_vector);
44-
addResiduals(_assembly, _work_vector, _var.dofIndices(), _var.arrayScalingFactor());
45+
addResiduals(_assembly, _work_vector, _var.dofIndices(), _scaling);
4546
}
4647

4748
void
@@ -50,12 +51,12 @@ ArrayNodalKernel::computeJacobian()
5051
if (!_var.isNodalDefined())
5152
return;
5253
_qp = 0;
53-
const auto jacobian = computeQpJacobian();
54-
const auto & dof_indices = _var.dofIndices();
55-
mooseAssert(dof_indices.size() == _count, "The number of dofs should be equal to count");
56-
for (const auto i : make_range(_count))
57-
addJacobianElement(
58-
_assembly, jacobian(i), dof_indices[i], dof_indices[i], _var.arrayScalingFactor()[i]);
54+
55+
_ivar_indices = &_var.dofIndices();
56+
_jvar_indices = &_var.dofIndices();
57+
mooseAssert(_ivar_indices->size() == _count, "The number of dofs should be equal to count");
58+
59+
computeQpJacobian();
5960
}
6061

6162
void
@@ -69,31 +70,18 @@ ArrayNodalKernel::computeOffDiagJacobian(const unsigned int jvar_num)
6970
else
7071
{
7172
const auto & jvar = getVariable(jvar_num);
72-
const auto jacobian = computeQpOffDiagJacobian(jvar);
73-
const auto & ivar_indices = _var.dofIndices();
74-
const auto & jvar_indices = jvar.dofIndices();
73+
_ivar_indices = &_var.dofIndices();
74+
_jvar_indices = &jvar.dofIndices();
75+
mooseAssert(_ivar_indices->size() == _count, "The number of dofs should be equal to count");
76+
mooseAssert(_jvar_indices->size() == jvar.count(),
77+
"The number of dofs should be equal to count");
7578

76-
mooseAssert(ivar_indices.size() == _count, "The number of dofs should be equal to count");
77-
mooseAssert(jvar_indices.size() == jvar.count(), "The number of dofs should be equal to count");
78-
79-
for (const auto i : make_range(_var.count()))
80-
for (const auto j : make_range(jvar.count()))
81-
addJacobianElement(_assembly,
82-
jacobian(i, j),
83-
ivar_indices[i],
84-
jvar_indices[j],
85-
_var.arrayScalingFactor()[i]);
79+
computeQpOffDiagJacobian(jvar_num);
8680
}
8781
}
8882

89-
RealEigenVector
90-
ArrayNodalKernel::computeQpJacobian()
91-
{
92-
return RealEigenVector::Zero(_count);
93-
}
94-
95-
RealEigenMatrix
96-
ArrayNodalKernel::computeQpOffDiagJacobian(const MooseVariableFieldBase & jvar)
83+
void
84+
ArrayNodalKernel::setJacobian(unsigned int i, unsigned int j, Real value)
9785
{
98-
return RealEigenMatrix::Zero(_count, jvar.count());
86+
addJacobianElement(_assembly, value, (*_ivar_indices)[i], (*_jvar_indices)[j], _scaling[i]);
9987
}

framework/src/nodalkernels/ArrayReactionNodalKernel.C

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ ArrayReactionNodalKernelTempl<is_ad>::computeQpResidual(GenericRealEigenVector<i
4343
}
4444

4545
template <bool is_ad>
46-
RealEigenVector
46+
void
4747
ArrayReactionNodalKernelTempl<is_ad>::computeQpJacobian()
4848
{
49-
return _coeff;
49+
for (const auto i : index_range(_coeff))
50+
setJacobian(i, i, _coeff(i));
5051
}
5152

5253
template class ArrayReactionNodalKernelTempl<false>;

framework/src/nodalkernels/ArrayTimeDerivativeNodalKernel.C

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ ArrayTimeDerivativeNodalKernel::computeQpResidual(RealEigenVector & residual)
3434
residual = _u_dot[_qp];
3535
}
3636

37-
RealEigenVector
37+
void
3838
ArrayTimeDerivativeNodalKernel::computeQpJacobian()
3939
{
40-
return RealEigenVector::Constant(_count, _du_dot_du[_qp]);
40+
for (const auto i : make_range(_count))
41+
setJacobian(i, i, _du_dot_du[_qp]);
4142
}

0 commit comments

Comments
 (0)