https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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#include "Assembly.h"
12
14
17{
19 params.addRequiredParam<UserObjectName>("read_prop_user_object",
20 "The PropertyReadFile "
21 "GeneralUserObject to read element "
22 "specific property values from file");
23 params.addRequiredParam<unsigned int>(
24 "column_number", "The column number (0-indexing) for the desired data in the CSV");
25
26 // This parameter is added for optimization when doing nearest neighbor interpolation
27 // but it's also safer to have that parameter be close to the use case, and not only in the UO
28 params.addRequiredParam<MooseEnum>("read_type",
29 MooseEnum("element voronoi block node"),
30 "Organization of data in the CSV file: "
31 "element:by element "
32 "node: by node "
33 "voronoi:nearest neighbor / voronoi tesselation structure "
34 "block:by mesh block");
35 // We need one ghost layer in case we need to retrieve the id or subdomain id from an
36 // element that is on the other side of a domain boundary
37 params.set<unsigned short>("ghost_layers") = 1;
38
39 params.addClassDescription("Uses data read from CSV to assign values");
40 return params;
41}
42
44 : Function(parameters),
45 _read_prop_user_object(nullptr),
46 _column_number(getParam<unsigned int>("column_number")),
47 _read_type(getParam<MooseEnum>("read_type").getEnum<PropertyReadFile::ReadTypeEnum>()),
48 _tid(isParamValid("_tid") ? getParam<THREAD_ID>("_tid") : 0)
49{
53 "The column requested in the function is likely to just be containing point coordinates");
54}
55
56void
58{
59 // Initialize this here instead of the constructor because of the potential for late deletion of
60 // remote elements
62
63 // Get a pointer to the PropertyReadFile. A pointer is used because the UserObject is not
64 // available during the construction of the function
65 _read_prop_user_object = &getUserObject<PropertyReadFile>("read_prop_user_object");
66
68 paramError("read_type", "The PropertyReadFile UO should have the same read_type parameter.");
70 paramError("column_number",
71 "Column number " + std::to_string(_column_number) +
72 " greater than total number of properties " +
73 std::to_string(_read_prop_user_object->getNumProperties()));
74}
75
76Real
77PiecewiseConstantFromCSV::value(Real, const Point & p) const
78{
80 {
81 // Block-sorted data is keyed by subdomain. A constant_on = SUBDOMAIN material is computed
82 // during subdomainSetup, where the element loop has already advanced currentSubdomainID to the
83 // new subdomain (in preElement) but has NOT yet reinitialized the current element (that happens
84 // later in onElement). So assembly.elem() still points at the previously processed element and
85 // the point p handed in is that element's stale q-point -- point-locating it lands in the
86 // previous subdomain (or fails outright). Detect this by the subdomain mismatch and answer
87 // directly from the subdomain being processed. Otherwise (elem() is consistent with
88 // currentSubdomainID) p is a genuine location -- a material quadrature point, or a nodal
89 // initial condition -- so fall through to point-location, which also resolves nodes shared
90 // between blocks deterministically (lowest element id).
91 const Assembly & assembly = _ti_feproblem.assembly(_tid, /*nl_sys=*/0);
92 const SubdomainID current_subdomain = assembly.currentSubdomainID();
93 const Elem * const current_elem = assembly.elem();
94 if (!current_elem || current_elem->subdomain_id() != current_subdomain)
95 return _read_prop_user_object->getBlockData(current_subdomain, _column_number);
96 }
97
100 {
101 // Genuine per-location lookup: find the element containing the point.
102 std::set<const Elem *> candidate_elements;
103 (*_point_locator)(p, candidate_elements);
104
105 // Find the element with the lowest ID
106 const Elem * min_id_elem = nullptr;
107 for (const auto & elem : candidate_elements)
108 if (!min_id_elem || elem->id() < min_id_elem->id())
109 min_id_elem = elem;
110 if (!min_id_elem)
111 mooseError("No element located at ", p, " to search in element or block sorted CSV values");
112 if (candidate_elements.size() > 1)
113 mooseWarning("Multiple elements have been found for Point ",
114 p,
115 ". Lowest ID element will be used for reading CSV data.");
116
119 return _read_prop_user_object->getData(min_id_elem, _column_number);
120 }
122 {
123 // Get the node id
124 const auto node = _point_locator->locate_node(p);
125
126 if (!node)
127 mooseError("No node was found at", p, " for retrieving nodal data from CSV.");
128
130 }
132 // No need to search for the element if we're just looking at nearest neighbors
134 else
135 mooseError("This should not be reachable. Implementation error somewhere");
136}
subdomain_id_type SubdomainID
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
unsigned int THREAD_ID
Definition MooseTypes.h:237
registerMooseObject("MooseApp", PiecewiseConstantFromCSV)
void ErrorVector unsigned int
Keeps track of stuff related to assembling.
Definition Assembly.h:102
const Elem *const & elem() const
Return the current element.
Definition Assembly.h:410
const SubdomainID & currentSubdomainID() const
Return the current subdomain ID.
Definition Assembly.h:415
virtual MooseMesh & mesh() override
virtual Assembly & assembly(const THREAD_ID tid, const unsigned int sys_num) override
Base class for function objects.
Definition Function.h:30
static InputParameters validParams()
Class constructor.
Definition Function.C:16
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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 addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
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:457
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
Definition MooseMesh.C:2983
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:3835
Function which provides a piecewise constant, in space, field from a CSV file.
const THREAD_ID _tid
Thread id, used to query the current element/subdomain for block-sorted data.
const PropertyReadFile::ReadTypeEnum _read_type
Type of read - element, grain, or block.
const unsigned int _column_number
The column number of interest in the CSV file.
const PropertyReadFile * _read_prop_user_object
A user object that takes care of reading the CSV file.
PiecewiseConstantFromCSV(const InputParameters &parameters)
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
std::unique_ptr< libMesh::PointLocatorBase > _point_locator
The point locator is used when values are sorted by elements or blocks in the CSV.
virtual Real value(Real t, const Point &pt) const override
Get the value of the function based on the data in the CSV file.
static InputParameters validParams()
Read properties from file - grain, element, node or block Input file syntax: prop1 prop2 etc.
Real getData(const Elem *const elem, const unsigned int prop_num) const
This function retrieves property data for elements.
unsigned int getNumProperties() const
Returns the number of properties (columns) read in the file.
ReadTypeEnum getReadType() const
Returns the ordering of data expected in the CSV file.
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...
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 getBlockData(const Elem *const elem, const unsigned int prop_num) const
This function retrieves properties for elements, from a file that has block-based data.
FEProblemBase & _ti_feproblem