LCOV - code coverage report
Current view: top level - src/vectorpostprocessors - NodalValueSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: fef103 Lines: 39 44 88.6 %
Date: 2025-09-03 20:01:23 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       14923 : NodalValueSampler::validParams()
      22             : {
      23       14923 :   InputParameters params = NodalVariableVectorPostprocessor::validParams();
      24             : 
      25       14923 :   params.addClassDescription("Samples values of nodal variable(s).");
      26             : 
      27       14923 :   params += SamplerBase::validParams();
      28             : 
      29       14923 :   return params;
      30           0 : }
      31             : 
      32         338 : NodalValueSampler::NodalValueSampler(const InputParameters & parameters)
      33         338 :   : NodalVariableVectorPostprocessor(parameters), SamplerBase(parameters, this, _communicator)
      34             : {
      35             :   // ensure that variables are 'nodal' (they have DoFs at nodes)
      36         676 :   for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
      37             :   {
      38         350 :     if (!_coupled_moose_vars[i]->isNodal())
      39           8 :       paramError("variable", "The variable '", _coupled_moose_vars[i]->name(), "' is not nodal.");
      40        1030 :     SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
      41             :   }
      42             : 
      43         326 :   std::vector<std::string> var_names(_coupled_moose_vars.size());
      44         326 :   _values.resize(_coupled_moose_vars.size());
      45         326 :   _has_values.resize(_coupled_moose_vars.size());
      46             : 
      47         664 :   for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
      48         338 :     var_names[i] = _coupled_moose_vars[i]->name();
      49             : 
      50             :   // Initialize the data structures in SamplerBase
      51         326 :   SamplerBase::setupVariables(var_names);
      52         326 : }
      53             : 
      54             : void
      55        4810 : NodalValueSampler::initialize()
      56             : {
      57        4810 :   SamplerBase::initialize();
      58        4810 : }
      59             : 
      60             : void
      61       76992 : 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      154072 :   for (unsigned int i = 0; i < _coupled_standard_moose_vars.size(); i++)
      74             :   {
      75       77080 :     const auto & dof_indices = _coupled_standard_moose_vars[i]->dofIndices();
      76             : 
      77       77080 :     if (dof_indices.size() > 0)
      78             :     {
      79       77080 :       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       77080 :       _values[i] = nodal_solution[_qp];
      82       77080 :       _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       76992 :       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       76992 :   if (num_values == _has_values.size())
     101       76992 :     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       76992 : }
     107             : 
     108             : void
     109        4434 : NodalValueSampler::finalize()
     110             : {
     111        4434 :   SamplerBase::finalize();
     112        4434 : }
     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