PiecewiseBilinear::PiecewiseBilinear(const InputParameters & parameters) : Function(parameters), _data_file_name(getParam<FileName>("data_file")), _axis(getParam<int>("axis")), _yaxis(getParam<int>("yaxis")), _xaxis(getParam<int>("xaxis")), _axisValid( _axis > -1 && _axis < 3 ), _yaxisValid( _yaxis > -1 && _yaxis < 3 ), _xaxisValid( _xaxis > -1 && _xaxis < 3 ), _scale_factor( getParam<Real>("scale_factor") ), _radial(getParam<bool>("radial")) { if (!_axisValid && !_yaxisValid && !_xaxisValid) mooseError("In PiecewiseBilinear " << _name << ": None of axis, yaxis, or xaxis properly defined. Allowable range is 0-2"); if (_axisValid && (_yaxisValid || _xaxisValid)) mooseError("In PiecewiseBilinear " << _name << ": Cannot define axis with either yaxis or xaxis"); if (_radial && (!_yaxisValid || !_xaxisValid)) mooseError("In PiecewiseBilinear " << _name << ": yaxis and xaxis must be defined when radial = true"); std::vector<Real> x; std::vector<Real> y; ColumnMajorMatrix z; std::vector<Real> z_vec; if (!_data_file_name.empty()) { if ( parameters.isParamValid("x") || parameters.isParamValid("y") || parameters.isParamValid("z") ) mooseError("In PiecewiseBilinear: Cannot specify 'data_file' and 'x', 'y', or 'z' together."); else parse( x, y, z ); } else if ( !(parameters.isParamValid("x") && parameters.isParamValid("y") && parameters.isParamValid("z")) ) mooseError("In PiecewiseBilinear: 'x' and 'y' and 'z' must be specified if any one is specified."); else { x = getParam<std::vector<Real> >("x"); y = getParam<std::vector<Real> >("y"); z_vec = getParam<std::vector<Real> >("z"); //check that size of z = (size of x)*(size of y) if (z_vec.size() != x.size()*y.size()) mooseError("In PiecewiseBilinear: Size of z should be the size of x times the size of y."); //reshape and populate z matrix z.reshape(y.size(),x.size()); int idx = 0; for (unsigned int i = 0; i < y.size(); i++) for (unsigned int j = 0; j < x.size(); j++) { z(i,j) = z_vec[idx]; idx += 1; } } _bilinear_interp = libmesh_make_unique<BilinearInterpolation>(x, y, z); }
void PiecewiseBilinear::parse( std::vector<Real> & x, std::vector<Real> & y, ColumnMajorMatrix & z) { std::ifstream file(_data_file_name.c_str()); if (!file.good()) mooseError("In PiecewiseBilinear " << _name << ": Error opening file '" + _data_file_name + "'."); std::string line; unsigned int linenum= 0; unsigned int itemnum = 0; unsigned int num_cols = 0; std::vector<Real> data; while (getline (file, line)) { linenum++; std::istringstream linestream(line); std::string item; itemnum = 0; while (getline (linestream, item, ',')) { itemnum++; std::istringstream i(item); Real d; i >> d; data.push_back(d); } if (linenum == 1) num_cols = itemnum; else if (num_cols+1 != itemnum) mooseError("In PiecewiseBilinear " << _name << ": Read " << itemnum << " columns of data but expected " << num_cols+1 << " columns while reading line " << linenum << " of '" << _data_file_name << "'."); } x.resize(itemnum-1); y.resize(linenum-1); z.reshape(linenum-1,itemnum-1); unsigned int offset(0); // Extract the first line's data (the x axis data) for (unsigned int j = 0; j < itemnum-1; ++j) { x[j] = data[offset]; ++offset; } for (unsigned int i = 0; i < linenum-1; ++i) { // Extract the y axis entry for this line y[i] = data[offset]; ++offset; // Extract the function values for this row in the matrix for (unsigned int j = 0; j < itemnum-1; ++j) { z(i,j) = data[offset]; ++offset; } } if (data.size() != offset) mooseError("ERROR! Inconsistency in data read from '" + _data_file_name + "' for PiecewiseBilinear function."); }