LCOV - code coverage report
Current view: top level - src/vectorpostprocessors - NodalValueSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 38 43 88.4 %
Date: 2025-07-17 01:28:37 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       14903 : NodalValueSampler::validParams()
      22             : {
      23       14903 :   InputParameters params = NodalVariableVectorPostprocessor::validParams();
      24             : 
      25       14903 :   params.addClassDescription("Samples values of nodal variable(s).");
      26             : 
      27       14903 :   params += SamplerBase::validParams();
      28             : 
      29       14903 :   return params;
      30           0 : }
      31             : 
      32         328 : NodalValueSampler::NodalValueSampler(const InputParameters & parameters)
      33         328 :   : NodalVariableVectorPostprocessor(parameters), SamplerBase(parameters, this, _communicator)
      34             : {
      35             :   // ensure that variables are 'nodal' (they have DoFs at nodes)
      36         655 :   for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
      37             :   {
      38         339 :     if (!_coupled_moose_vars[i]->isNodal())
      39           4 :       paramError("variable", "The variable '", _coupled_moose_vars[i]->name(), "' is not nodal.");
      40         335 :     SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
      41             :   }
      42             : 
      43         316 :   std::vector<std::string> var_names(_coupled_moose_vars.size());
      44         316 :   _values.resize(_coupled_moose_vars.size());
      45         316 :   _has_values.resize(_coupled_moose_vars.size());
      46             : 
      47         643 :   for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
      48         327 :     var_names[i] = _coupled_moose_vars[i]->name();
      49             : 
      50             :   // Initialize the data structures in SamplerBase
      51         316 :   SamplerBase::setupVariables(var_names);
      52         316 : }
      53             : 
      54             : void
      55        4408 : NodalValueSampler::initialize()
      56             : {
      57        4408 :   SamplerBase::initialize();
      58        4408 : }
      59             : 
      60             : void
      61       70183 : 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      140443 :   for (unsigned int i = 0; i < _coupled_standard_moose_vars.size(); i++)
      74             :   {
      75       70260 :     const VariableValue & nodal_solution = _coupled_standard_moose_vars[i]->dofValues();
      76             : 
      77       70260 :     if (nodal_solution.size() > 0)
      78             :     {
      79       70260 :       _values[i] = nodal_solution[_qp];
      80       70260 :       _has_values[i] = 1;
      81             :     }
      82             :     else
      83             :     {
      84           0 :       _values[i] = 0.; // arbitrary, will not be used
      85           0 :       _has_values[i] = 0;
      86             :     }
      87             :   }
      88             : 
      89             :   // Sum the number of values we had
      90             :   unsigned int num_values =
      91       70183 :       std::accumulate(_has_values.begin(), _has_values.end(), 0, std::plus<unsigned int>());
      92             : 
      93             :   // If the number of values matches the number of available values,
      94             :   // call addSample() as normal.  If there are more than zero values
      95             :   // available but less than the number requested, throw an error.
      96             :   // Otherwise, num_values==0, and we can skip adding the sample
      97             :   // entirely without error.
      98       70183 :   if (num_values == _has_values.size())
      99       70183 :     SamplerBase::addSample(*_current_node, _current_node->id(), _values);
     100             : 
     101           0 :   else if (num_values != 0 && num_values < _has_values.size())
     102           0 :     mooseError("You must use separate NodalValueSampler objects for variables with different "
     103             :                "discretizations.");
     104       70183 : }
     105             : 
     106             : void
     107        4032 : NodalValueSampler::finalize()
     108             : {
     109        4032 :   SamplerBase::finalize();
     110        4032 : }
     111             : 
     112             : void
     113         376 : NodalValueSampler::threadJoin(const UserObject & y)
     114             : {
     115         376 :   const auto & vpp = static_cast<const NodalValueSampler &>(y);
     116             : 
     117         376 :   SamplerBase::threadJoin(vpp);
     118         376 : }

Generated by: LCOV version 1.14