https://mooseframework.inl.gov
PiecewiseConstantFromCSV.C
Go to the documentation of this file.
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 
11 
13 
16 {
18  params.addRequiredParam<UserObjectName>("read_prop_user_object",
19  "The PropertyReadFile "
20  "GeneralUserObject to read element "
21  "specific property values from file");
22  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  params.addRequiredParam<MooseEnum>("read_type",
28  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  params.set<unsigned short>("ghost_layers") = 1;
37 
38  params.addClassDescription("Uses data read from CSV to assign values");
39  return params;
40 }
41 
43  : Function(parameters),
44  _read_prop_user_object(nullptr),
45  _column_number(getParam<unsigned int>("column_number")),
46  _read_type(getParam<MooseEnum>("read_type").getEnum<PropertyReadFile::ReadTypeEnum>())
47 {
51  "The column requested in the function is likely to just be containing point coordinates");
52 }
53 
54 void
56 {
57  // Initialize this here instead of the constructor because of the potential for late deletion of
58  // remote elements
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  _read_prop_user_object = &getUserObject<PropertyReadFile>("read_prop_user_object");
64 
66  paramError("read_type", "The PropertyReadFile UO should have the same read_type parameter.");
68  paramError("column_number",
69  "Column number " + std::to_string(_column_number) +
70  " greater than total number of properties " +
71  std::to_string(_read_prop_user_object->getNumProperties()));
72 }
73 
74 Real
75 PiecewiseConstantFromCSV::value(Real, const Point & p) const
76 {
79  {
80  // This is somewhat inefficient, but it allows us to retrieve the data in the
81  // CSV by element or by block.
82  std::set<const Elem *> candidate_elements;
83  (*_point_locator)(p, candidate_elements);
84 
85  // Find the element with the lowest ID
86  const Elem * min_id_elem = nullptr;
87  for (const auto & elem : candidate_elements)
88  if (!min_id_elem || elem->id() < min_id_elem->id())
89  min_id_elem = elem;
90  if (!min_id_elem)
91  mooseError("No element located at ", p, " to search in element or block sorted CSV values");
92  if (candidate_elements.size() > 1)
93  mooseWarning("Multiple elements have been found for Point ",
94  p,
95  ". Lowest ID element will be used for reading CSV data.");
96 
97  return _read_prop_user_object->getData(min_id_elem, _column_number);
98  }
100  {
101  // Get the node id
102  const auto node = _point_locator->locate_node(p);
103 
104  if (!node)
105  mooseError("No node was found at", p, " for retrieving nodal data from CSV.");
106 
108  }
110  // No need to search for the element if we're just looking at nearest neighbors
112  else
113  mooseError("This should not be reachable. Implementation error somewhere");
114 }
Real getVoronoiData(const Point &point, const unsigned int prop_num) const
This function retrieves properties for elements from a file with nearest neighbor / grain based prope...
Base class for function objects.
Definition: Function.h:36
FEProblemBase & _ti_feproblem
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:439
const unsigned int _column_number
The column number of interest in the CSV file.
registerMooseObject("MooseApp", PiecewiseConstantFromCSV)
virtual Real value(Real t, const Point &pt) const override
Get the value of the function based on the data in the CSV file.
Read properties from file - grain, element, node or block Input file syntax: prop1 prop2 etc...
ReadTypeEnum getReadType() const
Returns the ordering of data expected in the CSV file.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Real getNodeData(const Node *const node, const unsigned int prop_num) const
This function retrieves properties for nodes, from a file that has node-based data.
Real getData(const Elem *const elem, const unsigned int prop_num) const
This function retrieves property data for elements.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
void mooseWarning(Args &&... args) const
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
Definition: MooseMesh.C:2968
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
const PropertyReadFile::ReadTypeEnum _read_type
Type of read - element, grain, or block.
std::unique_ptr< libMesh::PointLocatorBase > _point_locator
The point locator is used when values are sorted by elements or blocks in the CSV.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual MooseMesh & mesh() override
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:271
Function which provides a piecewise constant, in space, field from a CSV file.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
virtual std::unique_ptr< libMesh::PointLocatorBase > getPointLocator() const
Proxy function to get a (sub)PointLocator from either the underlying libMesh mesh (default)...
Definition: MooseMesh.C:3754
const PropertyReadFile * _read_prop_user_object
A user object that takes care of reading the CSV file.
PiecewiseConstantFromCSV(const InputParameters &parameters)
static InputParameters validParams()
Class constructor.
Definition: Function.C:16
static InputParameters validParams()
unsigned int getNumProperties() const
Returns the number of properties (columns) read in the file.
void ErrorVector unsigned int