Skip to content

Commit

Permalink
Accomodate for late loading of JSON data from UO
Browse files Browse the repository at this point in the history
  • Loading branch information
GiudGiud committed Sep 24, 2023
1 parent 1191381 commit 40a8685
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions framework/include/functions/PiecewiseLinear.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ class PiecewiseLinear : public PiecewiseLinearBase
static InputParameters validParams();

PiecewiseLinear(const InputParameters & parameters);

/// Needed to process data from user objects that are not available at construction
void initialSetup() override;

private:
/// Whether the interpolation has been created
bool _interpolation_created;
};
3 changes: 3 additions & 0 deletions framework/include/functions/PiecewiseTabularBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class PiecewiseTabularBase : public PiecewiseBase
const bool _has_axis;
///@}

/// Boolean to keep track of whether the data has been loaded
bool _raw_data_loaded;

private:
/// Reads data from supplied CSV file.
void buildFromFile();
Expand Down
19 changes: 17 additions & 2 deletions framework/src/functions/PiecewiseLinear.C
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ PiecewiseLinear::validParams()
}

PiecewiseLinear::PiecewiseLinear(const InputParameters & parameters)
: PiecewiseLinearBase(parameters)
: PiecewiseLinearBase(parameters), _interpolation_created(false)
{
this->buildInterpolation(this->template getParam<bool>("extrap"));
if (_raw_data_loaded)
{
this->buildInterpolation(this->template getParam<bool>("extrap"));
_interpolation_created = true;
}
}

void
PiecewiseLinear::initialSetup()
{
PiecewiseTabularBase::initialSetup();
if (_raw_data_loaded && !_interpolation_created)
this->buildInterpolation(this->template getParam<bool>("extrap"));
else
mooseError("Data has still not been loaded at setup time. Something has gone wrong during "
"Function initialization, contact a developer");
}
15 changes: 14 additions & 1 deletion framework/src/functions/PiecewiseTabularBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,33 @@ PiecewiseTabularBase::buildFromFile()
// Size mismatch error
if (_raw_x.size() != _raw_y.size())
mooseError("In ", _name, ": Lengths of x and y data do not match.");

_raw_data_loaded = true;
}

void
PiecewiseTabularBase::buildFromJSON()
{
auto & json_uo = getUserObject<JSONFileReader>("json_uo");
if (!isParamValid("x_keys"))
mooseError("Missing 'x_keys' parameters for loading data from JSON");
if (!isParamValid("y_keys"))
mooseError("Missing 'y_keys' parameters for loading data from JSON");
json_uo.getVector(getParam<std::vector<std::string>>("x_keys"), _raw_x);
json_uo.getVector(getParam<std::vector<std::string>>("y_keys"), _raw_x);
json_uo.getVector(getParam<std::vector<std::string>>("y_keys"), _raw_y);
_raw_data_loaded = true;

// Size mismatch error
if (_raw_x.size() != _raw_y.size())
mooseError("Lengths of x (", _raw_x.size(), ") and y (", _raw_y.size(), ") data do not match.");
}

void
PiecewiseTabularBase::buildFromXandY()
{
_raw_x = this->template getParam<std::vector<Real>>("x");
_raw_y = this->template getParam<std::vector<Real>>("y");
_raw_data_loaded = true;
}

void
Expand All @@ -230,4 +242,5 @@ PiecewiseTabularBase::buildFromXY()
_raw_x[i] = xy[2 * i];
_raw_y[i] = xy[2 * i + 1];
}
_raw_data_loaded = true;
}

0 comments on commit 40a8685

Please sign in to comment.