LCOV - code coverage report
Current view: top level - src/vectorpostprocessors - NodalValueSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 39 44 88.6 %
Date: 2026-05-29 20:35:17 Functions: 6 6 100.0 %
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 "NodalValueSampler.h"
      11             : 
      12             : // MOOSE includes
      13             : #include "MooseVariableFE.h"
      14             : 
      15             : // C++ includes
      16             : #include <numeric>
      17             : 
      18             : registerMooseObject("MooseApp", NodalValueSampler);
      19             : 
      20             : InputParameters
      21        3651 : NodalValueSampler::validParams()
      22             : {
      23        3651 :   InputParameters params = NodalVariableVectorPostprocessor::validParams();
      24             : 
      25        3651 :   params.addClassDescription("Samples values of nodal variable(s).");
      26             : 
      27        3651 :   params += SamplerBase::validParams();
      28             : 
      29        3651 :   return params;
      30           0 : }
      31             : 
      32         305 : NodalValueSampler::NodalValueSampler(const InputParameters & parameters)
      33         305 :   : NodalVariableVectorPostprocessor(parameters), SamplerBase(parameters, this, _communicator)
      34             : {
      35             :   // ensure that variables are 'nodal' (they have DoFs at nodes)
      36         612 :   for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
      37             :   {
      38         316 :     if (!_coupled_moose_vars[i]->isNodal())
      39           6 :       paramError("variable", "The variable '", _coupled_moose_vars[i]->name(), "' is not nodal.");
      40         933 :     SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
      41             :   }
      42             : 
      43         296 :   std::vector<std::string> var_names(_coupled_moose_vars.size());
      44         296 :   _values.resize(_coupled_moose_vars.size());
      45         296 :   _has_values.resize(_coupled_moose_vars.size());
      46             : 
      47         603 :   for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
      48         307 :     var_names[i] = _coupled_moose_vars[i]->name();
      49             : 
      50             :   // Initialize the data structures in SamplerBase
      51         296 :   SamplerBase::setupVariables(var_names);
      52         296 : }
      53             : 
      54             : void
      55        4450 : NodalValueSampler::initialize()
      56             : {
      57        4450 :   SamplerBase::initialize();
      58        4450 : }
      59             : 
      60             : void
      61       67619 : NodalValueSampler::execute()
      62             : {
      63             :   // There may not be a nodal solution value at every node.  This
      64             :   // can happen if, for instance, you have a LINEAR, LAGRANGE
      65             :   // variable defined on a mesh with quadratic elements.
      66             :   //
      67             :   // We currently handle the following cases:
      68             :   // 1.) *none* of the coupled vars have values at the current node
      69             :   // 2.) *all* of the coupled vars have values at the current node
      70             :   //
      71             :   // If you have two different discretizations, you'll have to use two
      72             :   // separate NodalValueSampler objects to get their values.
      73      135315 :   for (unsigned int i = 0; i < _coupled_standard_moose_vars.size(); i++)
      74             :   {
      75       67696 :     const auto & dof_indices = _coupled_standard_moose_vars[i]->dofIndices();
      76             : 
      77       67696 :     if (dof_indices.size() > 0)
      78             :     {
      79       67696 :       const VariableValue & nodal_solution = _coupled_standard_moose_vars[i]->dofValues();
      80             :       mooseAssert(nodal_solution.size() == dof_indices.size(), "These must be the same length");
      81       67696 :       _values[i] = nodal_solution[_qp];
      82       67696 :       _has_values[i] = 1;
      83             :     }
      84             :     else
      85             :     {
      86           0 :       _values[i] = 0.; // arbitrary, will not be used
      87           0 :       _has_values[i] = 0;
      88             :     }
      89             :   }
      90             : 
      91             :   // Sum the number of values we had
      92             :   unsigned int num_values =
      93       67619 :       std::accumulate(_has_values.begin(), _has_values.end(), 0, std::plus<unsigned int>());
      94             : 
      95             :   // If the number of values matches the number of available values,
      96             :   // call addSample() as normal.  If there are more than zero values
      97             :   // available but less than the number requested, throw an error.
      98             :   // Otherwise, num_values==0, and we can skip adding the sample
      99             :   // entirely without error.
     100       67619 :   if (num_values == _has_values.size())
     101       67619 :     SamplerBase::addSample(*_current_node, _current_node->id(), _values);
     102             : 
     103           0 :   else if (num_values != 0 && num_values < _has_values.size())
     104           0 :     mooseError("You must use separate NodalValueSampler objects for variables with different "
     105             :                "discretizations.");
     106       67619 : }
     107             : 
     108             : void
     109        4074 : NodalValueSampler::finalize()
     110             : {
     111        4074 :   SamplerBase::finalize();
     112        4074 : }
     113             : 
     114             : void
     115         376 : NodalValueSampler::threadJoin(const UserObject & y)
     116             : {
     117         376 :   const auto & vpp = static_cast<const NodalValueSampler &>(y);
     118             : 
     119         376 :   SamplerBase::threadJoin(vpp);
     120         376 : }

Generated by: LCOV version 1.14