LCOV - code coverage report
Current view: top level - src/functions - PiecewiseConstantFromCSV.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 48 52 92.3 %
Date: 2025-07-17 01:28:37 Functions: 4 4 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 "PiecewiseConstantFromCSV.h"
      11             : 
      12             : registerMooseObject("MooseApp", PiecewiseConstantFromCSV);
      13             : 
      14             : InputParameters
      15       15079 : PiecewiseConstantFromCSV::validParams()
      16             : {
      17       15079 :   InputParameters params = Function::validParams();
      18       15079 :   params.addRequiredParam<UserObjectName>("read_prop_user_object",
      19             :                                           "The PropertyReadFile "
      20             :                                           "GeneralUserObject to read element "
      21             :                                           "specific property values from file");
      22       15079 :   params.addRequiredParam<unsigned int>(
      23             :       "column_number", "The column number (0-indexing) for the desired data in the CSV");
      24             : 
      25             :   // This parameter is added for optimization when doing nearest neighbor interpolation
      26             :   // but it's also safer to have that parameter be close to the use case, and not only in the UO
      27       45237 :   params.addRequiredParam<MooseEnum>("read_type",
      28       30158 :                                      MooseEnum("element voronoi block node"),
      29             :                                      "Organization of data in the CSV file: "
      30             :                                      "element:by element "
      31             :                                      "node: by node "
      32             :                                      "voronoi:nearest neighbor / voronoi tesselation structure "
      33             :                                      "block:by mesh block");
      34             :   // We need one ghost layer in case we need to retrieve the id or subdomain id from an
      35             :   // element that is on the other side of a domain boundary
      36       15079 :   params.set<unsigned short>("ghost_layers") = 1;
      37             : 
      38       15079 :   params.addClassDescription("Uses data read from CSV to assign values");
      39       15079 :   return params;
      40           0 : }
      41             : 
      42         434 : PiecewiseConstantFromCSV::PiecewiseConstantFromCSV(const InputParameters & parameters)
      43             :   : Function(parameters),
      44         434 :     _read_prop_user_object(nullptr),
      45         434 :     _column_number(getParam<unsigned int>("column_number")),
      46         868 :     _read_type(getParam<MooseEnum>("read_type").getEnum<PropertyReadFile::ReadTypeEnum>())
      47             : {
      48         438 :   if (_column_number < _ti_feproblem.mesh().dimension() &&
      49           4 :       _read_type == PropertyReadFile::ReadTypeEnum::VORONOI)
      50           4 :     mooseWarning(
      51             :         "The column requested in the function is likely to just be containing point coordinates");
      52         430 : }
      53             : 
      54             : void
      55         302 : PiecewiseConstantFromCSV::initialSetup()
      56             : {
      57             :   // Initialize this here instead of the constructor because of the potential for late deletion of
      58             :   // remote elements
      59         302 :   _point_locator = _ti_feproblem.mesh().getPointLocator();
      60             : 
      61             :   // Get a pointer to the PropertyReadFile. A pointer is used because the UserObject is not
      62             :   // available during the construction of the function
      63         302 :   _read_prop_user_object = &getUserObject<PropertyReadFile>("read_prop_user_object");
      64             : 
      65         302 :   if (_read_type != _read_prop_user_object->getReadType())
      66           4 :     paramError("read_type", "The PropertyReadFile UO should have the same read_type parameter.");
      67         298 :   if (_column_number > _read_prop_user_object->getNumProperties())
      68           4 :     paramError("column_number",
      69           4 :                "Column number " + std::to_string(_column_number) +
      70           4 :                    " greater than total number of properties " +
      71           4 :                    std::to_string(_read_prop_user_object->getNumProperties()));
      72         294 : }
      73             : 
      74             : Real
      75        3456 : PiecewiseConstantFromCSV::value(Real, const Point & p) const
      76             : {
      77        3456 :   if (_read_type == PropertyReadFile::ReadTypeEnum::ELEMENT ||
      78        2240 :       _read_type == PropertyReadFile::ReadTypeEnum::BLOCK)
      79             :   {
      80             :     // This is somewhat inefficient, but it allows us to retrieve the data in the
      81             :     // CSV by element or by block.
      82        1664 :     std::set<const Elem *> candidate_elements;
      83        1664 :     (*_point_locator)(p, candidate_elements);
      84             : 
      85             :     // Find the element with the lowest ID
      86        1664 :     const Elem * min_id_elem = nullptr;
      87        5176 :     for (const auto & elem : candidate_elements)
      88        3512 :       if (!min_id_elem || elem->id() < min_id_elem->id())
      89        2327 :         min_id_elem = elem;
      90        1664 :     if (!min_id_elem)
      91           0 :       mooseError("No element located at ", p, " to search in element or block sorted CSV values");
      92        1664 :     if (candidate_elements.size() > 1)
      93         840 :       mooseWarning("Multiple elements have been found for Point ",
      94             :                    p,
      95             :                    ". Lowest ID element will be used for reading CSV data.");
      96             : 
      97        1664 :     return _read_prop_user_object->getData(min_id_elem, _column_number);
      98        1664 :   }
      99        1792 :   else if (_read_type == PropertyReadFile::ReadTypeEnum::NODE)
     100             :   {
     101             :     // Get the node id
     102         448 :     const auto node = _point_locator->locate_node(p);
     103             : 
     104         448 :     if (!node)
     105           0 :       mooseError("No node was found at", p, " for retrieving nodal data from CSV.");
     106             : 
     107         448 :     return _read_prop_user_object->getNodeData(node, _column_number);
     108             :   }
     109        1344 :   else if (_read_type == PropertyReadFile::ReadTypeEnum::VORONOI)
     110             :     // No need to search for the element if we're just looking at nearest neighbors
     111        1344 :     return _read_prop_user_object->getVoronoiData(p, _column_number);
     112             :   else
     113           0 :     mooseError("This should not be reachable. Implementation error somewhere");
     114             : }

Generated by: LCOV version 1.14