13 #include "libmesh/int_range.h" 23 "axis", axis,
"The axis used (x, y, or z) if this is to be a function of position");
24 params.
addParam<
Real>(
"scale_factor", 1.0,
"Scale factor to be applied to the output values");
28 params.
addParam<std::vector<Real>>(
"xy_data",
29 "All function data, supplied in abscissa, ordinate pairs");
30 params.
addParam<std::vector<Real>>(
"x",
"The abscissa values");
31 params.
addParam<std::vector<Real>>(
"y",
"The ordinate values");
34 params.
addParam<FileName>(
"data_file",
"File holding CSV data");
35 params.
addParam<
unsigned int>(
"x_index_in_file", 0,
"The abscissa index in the data file");
36 params.
addParam<
unsigned int>(
"y_index_in_file", 1,
"The ordinate index in the data file");
38 "x_title",
"The title of the column/row containing the x data in the data file");
40 "y_title",
"The title of the column/row containing the y data in the data file");
42 "xy_in_file_only",
true,
"If the data file only contains abscissa and ordinate data");
43 MooseEnum format(
"columns=0 rows=1",
"rows");
45 "format", format,
"Format of csv data file that is in either in columns or rows");
48 params.
addParam<UserObjectName>(
"json_uo",
"JSONFileReader holding the data");
49 params.
addParam<std::vector<std::string>>(
50 "x_keys",
"Ordered vector of keys in the JSON tree to obtain the abscissa");
51 params.
addParam<std::vector<std::string>>(
52 "y_keys",
"Ordered vector of keys in the JSON tree to obtain the ordinate");
56 "data_file x_index_in_file y_index_in_file x_title y_title xy_in_file_only format",
57 "Data from CSV file");
64 _scale_factor(this->template getParam<
Real>(
"scale_factor")),
65 _has_axis(isParamValid(
"axis")),
66 _raw_data_loaded(false)
70 _axis = this->
template getParam<MooseEnum>(
"axis");
76 mooseError(
"Either 'data_file' or 'json_uo' or 'x' and 'y' or 'xy_data' must be specified " 80 "Both 'x' and 'y' parameters or neither (for another input method) must be specified");
86 "A parameter was passed for an option using data from a CSV file but the " 87 "'data_file' parameter has not been set. This is not allowed. Please check the parameter " 88 "groups in the documentation for the list of parameters for each data input method.");
90 mooseError(
"A parameter was passed for a JSON input option but the 'json_uo' parameter has not " 91 "been set. This is not allowed. Please check the parameter groups in the " 92 "documentation for the list of parameters for each data input method.");
102 mooseError(
"Unknown X-Y data source. Are you missing a parameter? Did you misspell one?");
120 const auto & data_file_name = this->
template getParam<FileName>(
"data_file");
121 const MooseEnum format = this->
template getParam<MooseEnum>(
"format");
126 bool xy_only = this->
template getParam<bool>(
"xy_in_file_only");
132 "When accessing data through 'x_title' or 'y_title' this parameter should not be used");
143 const auto & columns = reader.
getNames();
144 const auto & data = reader.
getData();
146 if (data.size() > 2 && xy_only)
149 ": Read more than two ",
151 " of data from file '",
153 "'. Did you mean to use \"format = ",
154 format ==
"columns" ?
"rows" :
"columns",
155 "\" or set \"xy_in_file_only\" to false?");
159 const auto setIndex = [&](
unsigned int & index,
const std::string & prefix)
163 const auto name = this->
template getParam<std::string>(prefix +
"_title");
165 if (columns[i] ==
name)
172 " in the data file has the title '",
177 index = this->
template getParam<unsigned int>(prefix +
"_index_in_file");
179 if (index >= data.size())
181 "Index out-of-range of the available data in '",
183 "', which contains ",
190 setIndex(x_index,
"x");
191 setIndex(y_index,
"y");
193 if (x_index == y_index)
195 "In ",
_name,
": 'x_index_in_file' and 'y_index_in_file' are set to the same value.");
211 auto & json_uo = getUserObject<JSONFileReader>(
"json_uo");
213 mooseError(
"Missing 'x_keys' parameters for loading data from JSON");
215 mooseError(
"Missing 'y_keys' parameters for loading data from JSON");
216 json_uo.getVector<
Real>(getParam<std::vector<std::string>>(
"x_keys"),
_raw_x);
217 json_uo.getVector<
Real>(getParam<std::vector<std::string>>(
"y_keys"),
_raw_y);
228 _raw_x = this->
template getParam<std::vector<Real>>(
"x");
229 _raw_y = this->
template getParam<std::vector<Real>>(
"y");
236 const auto & xy = this->
template getParam<std::vector<Real>>(
"xy_data");
237 const auto xy_size = xy.size();
238 if (xy_size % 2 != 0)
239 mooseError(
"In ",
_name,
": Length of data provided in 'xy_data' must be a multiple of 2.");
241 const auto data_size = xy_size / 2;
247 _raw_y[i] = xy[2 * i + 1];
Function base which provides a piecewise approximation to a specified (x,y) point data set...
const InputParameters & _pars
The object's parameters.
PiecewiseTabularBase(const InputParameters ¶meters)
const unsigned int invalid_uint
const std::string & _name
The name of this class.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
void setComment(const std::string &value)
static InputParameters validParams()
const Parallel::Communicator & _communicator
void buildFromFile()
Reads data from supplied CSV file.
static InputParameters validParams()
const std::string & name() const
Get the name of the class.
std::vector< Real > _raw_x
raw function data as read
void buildFromXandY()
Builds data from 'x' and 'y' parameters.
void setFormatFlag(FormatFlag value)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
void initialSetup() override
Needed to load data from user objects that are not available at construction.
void read()
Perform the actual data reading.
const std::vector< std::vector< T > > & getData() const
Return the rows/columns of data.
int _axis
if _has_axis is true point component to use as function argument, otherwise use t ...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Utility class for reading delimited data (e.g., CSV data).
IntRange< T > make_range(T beg, T end)
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
std::vector< Real > _raw_y
void buildFromJSON()
Reads data from supplied JSON reader.
const std::vector< std::string > & getNames() const
Return the column/row names.
bool _raw_data_loaded
Boolean to keep track of whether the data has been loaded.
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
void buildFromXY()
Builds data from 'xy_data' parameter.
auto index_range(const T &sizable)