www.mooseframework.org
Piecewise.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "Piecewise.h"
11 #include "DelimitedFileReader.h"
12 
13 template <>
16 {
18  params.addParam<std::vector<Real>>("xy_data",
19  "All function data, supplied in abscissa, ordinate pairs");
20  params.addParam<std::vector<Real>>("x", "The abscissa values");
21  params.addParam<std::vector<Real>>("y", "The ordinate values");
22  params.addParam<FileName>("data_file", "File holding csv data for use with Piecewise");
23  params.addParam<unsigned int>("x_index_in_file", 0, "The abscissa index in the data file");
24  params.addParam<unsigned int>("y_index_in_file", 1, "The ordinate index in the data file");
25  params.addParam<bool>(
26  "xy_in_file_only", true, "If the data file only contains abscissa and ordinate data");
27 
28  MooseEnum format("columns=0 rows=1", "rows");
29  params.addParam<MooseEnum>(
30  "format", format, "Format of csv data file that is in either in columns or rows");
31  params.addParam<Real>("scale_factor", 1.0, "Scale factor to be applied to the ordinate values");
32 
33  return params;
34 }
35 
37  : PiecewiseBase(parameters), _scale_factor(getParam<Real>("scale_factor"))
38 {
39  std::pair<std::vector<Real>, std::vector<Real>> xy;
40 
41  if (isParamValid("data_file"))
42  xy = buildFromFile();
43 
44  else if (isParamValid("x") || isParamValid("y"))
45  xy = buildFromXandY();
46 
47  else if (isParamValid("xy_data"))
48  xy = buildFromXY();
49 
50  else
51  mooseError("In Piecewise ",
52  _name,
53  ": Either 'data_file', 'x' and 'y', or 'xy_data' must be specified.");
54 
55  setData(xy.first, xy.second);
56 }
57 
58 std::pair<std::vector<Real>, std::vector<Real>>
60 {
61  // Input parameters
62  const FileName & data_file_name = getParam<FileName>("data_file");
63  const MooseEnum & format = getParam<MooseEnum>("format");
64  unsigned int x_index = getParam<unsigned int>("x_index_in_file");
65  unsigned int y_index = getParam<unsigned int>("y_index_in_file");
66  bool xy_only = getParam<bool>("xy_in_file_only");
67 
68  // Check that other forms of input are not set.
69  if (isParamValid("x") || isParamValid("y") || isParamValid("xy_data"))
70  mooseError("In Piecewise ",
71  _name,
72  ": Cannot specify 'data_file' and 'x', 'y', or 'xy_data' together.");
73 
74  if (x_index == y_index)
75  mooseError("In Piecewise ",
76  _name,
77  ": 'x_index_in_file' and 'y_index_in_file' are set to the same value.");
78 
79  // Read the data from CSV file
80  MooseUtils::DelimitedFileReader reader(data_file_name, &_communicator);
82  reader.setComment("#");
83  reader.read();
84  const std::vector<std::vector<double>> & data = reader.getData();
85 
86  // Check the data format
87  if (x_index >= data.size())
88  mooseError("In Piecewise ",
89  _name,
90  ": The 'x_index_in_file' is out-of-range of the available data in '",
91  data_file_name,
92  "', which contains ",
93  data.size(),
94  " ",
95  format,
96  " of data.");
97 
98  if (y_index >= data.size())
99  mooseError("In Piecewise ",
100  _name,
101  ": The 'y_index_in_file' is out-of-range of the available data in '",
102  data_file_name,
103  "', which contains ",
104  data.size(),
105  " ",
106  format,
107  " of data.");
108 
109  if (data.size() > 2 && xy_only)
110  mooseError("In Piecewise ",
111  _name,
112  ": Read more than two ",
113  format,
114  " of data from file '",
115  data_file_name,
116  "'. Did you mean to use \"format = ",
117  format == "columns" ? "rows" : "columns",
118  "\" or set \"xy_in_file_only\" to false?");
119 
120  // Update the input vectors to contained the desired data
121  return std::make_pair(reader.getData(x_index), reader.getData(y_index));
122 }
123 
124 std::pair<std::vector<Real>, std::vector<Real>>
126 {
127  if (!isParamValid("x") || !isParamValid("y"))
128  mooseError(
129  "In Piecewise ", _name, ": Both 'x' and 'y' must be specified if either one is specified.");
130 
131  if (isParamValid("xy_data"))
132  mooseError("In Piecewise ", _name, ": Cannot specify 'x', 'y', and 'xy_data' together.");
133 
134  return std::make_pair(getParam<std::vector<Real>>("x"), getParam<std::vector<Real>>("y"));
135 }
136 
137 std::pair<std::vector<Real>, std::vector<Real>>
139 {
140  std::vector<Real> xy = getParam<std::vector<Real>>("xy_data");
141  unsigned int xy_size = xy.size();
142  if (xy_size % 2 != 0)
143  mooseError(
144  "In Piecewise ", _name, ": Length of data provided in 'xy_data' must be a multiple of 2.");
145 
146  unsigned int data_size = xy_size / 2;
147  std::vector<Real> x;
148  std::vector<Real> y;
149  x.reserve(data_size);
150  y.reserve(data_size);
151  for (unsigned int i = 0; i < xy_size; i += 2)
152  {
153  x.push_back(xy[i]);
154  y.push_back(xy[i + 1]);
155  }
156  return std::make_pair(x, y);
157 }
void read()
Perform the actual data reading.
Function base which provides a piecewise approximation to a provided (x,y) point data set...
Definition: PiecewiseBase.h:26
const std::vector< std::vector< double > > & getData() const
Return the rows/columns of data.
Utility class for reading delimited data (e.g., CSV data).
void setComment(const std::string &value)
InputParameters validParams< PiecewiseBase >()
Definition: PiecewiseBase.C:14
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void setData(const std::vector< Real > &x, const std::vector< Real > &y)
Provides a means for explicitly setting the x and y data.
Definition: PiecewiseBase.C:46
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
static PetscErrorCode Vec x
std::pair< std::vector< Real >, std::vector< Real > > buildFromXY()
Builds data from &#39;xy_data&#39; parameter.
Definition: Piecewise.C:138
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
std::pair< std::vector< Real >, std::vector< Real > > buildFromXandY()
Builds data from &#39;x&#39; and &#39;y&#39; parameters.
Definition: Piecewise.C:125
InputParameters validParams< Piecewise >()
Definition: Piecewise.C:15
void setFormatFlag(FormatFlag value)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
const std::string & _name
The name of this object, reference to value stored in InputParameters.
Definition: MooseObject.h:183
std::pair< std::vector< Real >, std::vector< Real > > buildFromFile()
Reads data from supplied CSV file.
Definition: Piecewise.C:59
Piecewise(const InputParameters &parameters)
Definition: Piecewise.C:36
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89