LCOV - code coverage report
Current view: top level - src/fvkernels - FVScalarLagrangeMultiplierConstraint.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 43 46 93.5 %
Date: 2025-07-17 01:28:37 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          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             : #include "FVScalarLagrangeMultiplierConstraint.h"
      11             : 
      12             : #include "MooseVariableScalar.h"
      13             : #include "MooseVariableFV.h"
      14             : #include "Assembly.h"
      15             : 
      16             : InputParameters
      17       57306 : FVScalarLagrangeMultiplierConstraint::validParams()
      18             : {
      19       57306 :   InputParameters params = FVElementalKernel::validParams();
      20       57306 :   params.addClassDescription(
      21             :       "Base class for imposing constraints using scalar Lagrange multipliers");
      22       57306 :   params.addParam<PostprocessorName>("phi0", "0", "The value that the constraint will enforce.");
      23       57306 :   params.addRequiredCoupledVar("lambda", "Lagrange multiplier variable");
      24       57306 :   return params;
      25           0 : }
      26             : 
      27         126 : FVScalarLagrangeMultiplierConstraint::FVScalarLagrangeMultiplierConstraint(
      28         126 :     const InputParameters & parameters)
      29             :   : FVElementalKernel(parameters),
      30         126 :     _phi0(getPostprocessorValue("phi0")),
      31         126 :     _lambda_var(*getScalarVar("lambda", 0)),
      32         252 :     _lambda(adCoupledScalarValue("lambda"))
      33             : {
      34         126 : }
      35             : 
      36             : void
      37        6363 : FVScalarLagrangeMultiplierConstraint::computeResidualAndJacobian()
      38             : {
      39        6363 :   const auto volume = _assembly.elemVolume();
      40       25452 :   addResidualsAndJacobian(_assembly,
      41        6363 :                           std::array<ADReal, 1>{{_lambda[0] * volume}},
      42        6363 :                           _var.dofIndices(),
      43        6363 :                           _var.scalingFactor());
      44       25452 :   addResidualsAndJacobian(_assembly,
      45        6363 :                           std::array<ADReal, 1>{{computeQpResidual() * volume}},
      46        6363 :                           _lambda_var.dofIndices(),
      47        6363 :                           _lambda_var.scalingFactor());
      48       19089 : }
      49             : 
      50             : void
      51        9168 : FVScalarLagrangeMultiplierConstraint::computeResidual()
      52             : {
      53             :   // Primal residual
      54        9168 :   prepareVectorTag(_assembly, _var.number());
      55             :   mooseAssert(_local_re.size() == 1, "We should only have a single dof");
      56             :   mooseAssert(_lambda.size() == 1 && _lambda_var.order() == 1,
      57             :               "The lambda variable should be first order");
      58        9168 :   _local_re(0) += MetaPhysicL::raw_value(_lambda[0]) * _assembly.elemVolume();
      59        9168 :   accumulateTaggedLocalResidual();
      60             : 
      61             :   // LM residual. We may not have any actual ScalarKernels in our simulation so we need to manually
      62             :   // make sure the scalar residuals get cached for later addition
      63        9168 :   const auto lm_r = MetaPhysicL::raw_value(computeQpResidual()) * _assembly.elemVolume();
      64             :   mooseAssert(_lambda_var.dofIndices().size() == 1, "We should only have a single dof");
      65       27504 :   addResiduals(_assembly,
      66        9168 :                std::array<Real, 1>{{lm_r}},
      67        9168 :                _lambda_var.dofIndices(),
      68        9168 :                _lambda_var.scalingFactor());
      69        9168 : }
      70             : 
      71             : void
      72           0 : FVScalarLagrangeMultiplierConstraint::computeJacobian()
      73             : {
      74           0 : }
      75             : 
      76             : void
      77        2623 : FVScalarLagrangeMultiplierConstraint::computeOffDiagJacobian()
      78             : {
      79             :   // Primal
      80             :   mooseAssert(_lambda.size() == 1 && _lambda_var.order() == 1,
      81             :               "The lambda variable should be first order");
      82        2623 :   const auto primal_r = _lambda[0] * _assembly.elemVolume();
      83             :   mooseAssert(_var.dofIndices().size() == 1, "We should only have one dof");
      84       10492 :   addJacobian(
      85        5246 :       _assembly, std::array<ADReal, 1>{{primal_r}}, _var.dofIndices(), _var.scalingFactor());
      86             : 
      87             :   // LM
      88        2623 :   const auto lm_r = computeQpResidual() * _assembly.elemVolume();
      89             :   mooseAssert(_lambda_var.dofIndices().size() == 1, "We should only have one dof");
      90       10492 :   addJacobian(_assembly,
      91        2623 :               std::array<ADReal, 1>{{lm_r}},
      92        2623 :               _lambda_var.dofIndices(),
      93        2623 :               _lambda_var.scalingFactor());
      94        7869 : }

Generated by: LCOV version 1.14