Skip to content

Commit

Permalink
redone the cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mkstoyanov committed Feb 11, 2023
1 parent 799fd2c commit ba02639
Show file tree
Hide file tree
Showing 6 changed files with 703 additions and 1,120 deletions.
4 changes: 2 additions & 2 deletions InterfaceMATLAB/tsgCoreTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -766,15 +766,15 @@ function tsgCoreTests()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% tsgIntegrate() %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[lGrid, p] = tsgMakeGlobal('_tsgcoretests_int', 1, 1, 'gauss-hermite', 'level', 2, [], [0.0, 0.0]);
[lGrid, p] = tsgMakeGlobal('_tsgcoretests_int', 1, 1, 'gauss-hermite', 'level', 2, [], [0.0,]);
v = [p.^2];
tsgLoadValues(lGrid, v)
[I] = tsgIntegrate(lGrid);
if (abs(I - pi^0.5 / 2.0) > 1.E-11)
error('Mismatch in tsgIntegrate(): case 1');
end

[lGrid, p] = tsgMakeGlobal('_tsgcoretests_int', 1, 1, 'gauss-hermite', 'level', 2, [], [2.0, 0.0]);
[lGrid, p] = tsgMakeGlobal('_tsgcoretests_int', 1, 1, 'gauss-hermite', 'level', 2, [], [2.0,]);
v = [sqrt(2.0) * ones(size(v,1), 1)];
tsgLoadValues(lGrid, v)
[I] = tsgIntegrate(lGrid);
Expand Down
61 changes: 26 additions & 35 deletions SparseGrids/TasmanianSparseGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,7 @@ void TasmanianSparseGrid::updateGlobalGrid(int depth, TypeDepth type, const int
Utils::copyArray(level_limits, getNumDimensions()));
}
void TasmanianSparseGrid::updateGlobalGrid(int depth, TypeDepth type, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits){
if (isGlobal()){
int dims = base->getNumDimensions();
if (depth < 0) throw std::invalid_argument("ERROR: updateGlobalGrid() requires non-negative depth");
size_t expected_aw_size = (OneDimensionalMeta::isTypeCurved(type)) ? 2*dims : dims;
if ((!anisotropic_weights.empty()) && (anisotropic_weights.size() != expected_aw_size)) throw std::invalid_argument("ERROR: updateGlobalGrid() requires anisotropic_weights with either 0 or dimensions entries");
if ((!level_limits.empty()) && (level_limits.size() != (size_t) dims)) throw std::invalid_argument("ERROR: updateGlobalGrid() requires level_limits with either 0 or dimensions entries");

if (!level_limits.empty()) llimits = level_limits; // if level_limits is empty, use the existing llimits (if any)

get<GridGlobal>()->updateGrid(depth, type, anisotropic_weights, llimits);
}else{
throw std::runtime_error("ERROR: updateGlobalGrid() called, but the grid is not global");
}
updateGrid(depth, type, anisotropic_weights, level_limits);
}

void TasmanianSparseGrid::updateSequenceGrid(int depth, TypeDepth type, const int *anisotropic_weights, const int *level_limits){
Expand All @@ -307,36 +295,39 @@ void TasmanianSparseGrid::updateSequenceGrid(int depth, TypeDepth type, const in
Utils::copyArray(level_limits, getNumDimensions()));
}
void TasmanianSparseGrid::updateSequenceGrid(int depth, TypeDepth type, const std::vector<int> &anisotropic_weights, const std::vector<int> &level_limits){
if (isSequence()){
int dims = base->getNumDimensions();
if (depth < 0) throw std::invalid_argument("ERROR: updateSequenceGrid() requires non-negative depth");
size_t expected_aw_size = (OneDimensionalMeta::isTypeCurved(type)) ? 2*dims : dims;
if ((!anisotropic_weights.empty()) && (anisotropic_weights.size() != expected_aw_size)) throw std::invalid_argument("ERROR: updateSequenceGrid() requires anisotropic_weights with either 0 or dimenions entries");
if ((!level_limits.empty()) && (level_limits.size() != (size_t) dims)) throw std::invalid_argument("ERROR: updateSequenceGrid() requires level_limits with either 0 or dimensions entries");

if (!level_limits.empty()) llimits = level_limits; // if level_limits is empty, use the existing llimits (if any)
get<GridSequence>()->updateGrid(depth, type, anisotropic_weights, llimits);
}else{
throw std::runtime_error("ERROR: updateSequenceGrid called, but the grid is not sequence");
}
updateGrid(depth, type, anisotropic_weights, level_limits);
}

void TasmanianSparseGrid::updateFourierGrid(int depth, TypeDepth type, const int *anisotropic_weights, const int *level_limits){
if (empty()) throw std::runtime_error("ERROR: updateFourierGrid() called, but the grid is empty");
updateFourierGrid(depth, type, Utils::copyArray(anisotropic_weights, (OneDimensionalMeta::isTypeCurved(type)) ? 2*getNumDimensions() : getNumDimensions()),
Utils::copyArray(level_limits, getNumDimensions()));
updateGrid(depth, type, anisotropic_weights, level_limits);
}
void TasmanianSparseGrid::updateFourierGrid(int depth, TypeDepth type, std::vector<int> const &anisotropic_weights,
std::vector<int> const &level_limits){
if (!isFourier()) throw std::runtime_error("ERROR: updateFourierGrid() called, but the grid is not Fourier");
updateGrid(depth, type, anisotropic_weights, level_limits);
}
void TasmanianSparseGrid::updateGrid(int depth, TypeDepth type, const int *anisotropic_weights, const int *level_limits){
if (empty()) throw std::runtime_error("ERROR: updateGrid() called, but the grid is empty");
updateGrid(depth, type,
Utils::copyArray(anisotropic_weights, (OneDimensionalMeta::isTypeCurved(type)) ? 2*getNumDimensions() : getNumDimensions()),
Utils::copyArray(level_limits, getNumDimensions()));
}
void TasmanianSparseGrid::updateGrid(int depth, TypeDepth type, std::vector<int> const &anisotropic_weights, std::vector<int> const &level_limits){
if (empty()) throw std::runtime_error("ERROR: updateGrid() called, but the grid is empty");
int dims = base->getNumDimensions();
if (depth < 0) throw std::invalid_argument("ERROR: updateSequenceGrid() requires non-negative depth");
if (depth < 0) throw std::invalid_argument("ERROR: cannot update with a negative depth");
size_t expected_aw_size = (OneDimensionalMeta::isTypeCurved(type)) ? 2*dims : dims;
if ((!anisotropic_weights.empty()) && (anisotropic_weights.size() != expected_aw_size)) throw std::invalid_argument("ERROR: updateSequenceGrid() requires anisotropic_weights with either 0 or dimenions entries");
if ((!level_limits.empty()) && (level_limits.size() != (size_t) dims)) throw std::invalid_argument("ERROR: updateSequenceGrid() requires level_limits with either 0 or dimensions entries");

if (!level_limits.empty()) llimits = level_limits; // if level_limits is empty, use the existing llimits (if any)
get<GridFourier>()->updateGrid(depth, type, anisotropic_weights, llimits);
if (not anisotropic_weights.empty() and anisotropic_weights.size() != expected_aw_size) throw std::invalid_argument("ERROR: in updateGrid() anisotropic_weights must be either empty or has size equal to dimenions or twice dimenions based on the type of the update.");
if (not level_limits.empty() and level_limits.size() != (size_t) dims) throw std::invalid_argument("ERROR: in updateGrid() level_limits must be either empty or must have size equal to the number of dimensions");
if (not level_limits.empty()) llimits = level_limits; // if level_limits is empty, use the existing llimits (if any)
if (isGlobal()){
get<GridGlobal>()->updateGrid(depth, type, anisotropic_weights, llimits);
}else if (isSequence()){
get<GridSequence>()->updateGrid(depth, type, anisotropic_weights, llimits);
}else if (isFourier()){
get<GridFourier>()->updateGrid(depth, type, anisotropic_weights, llimits);
}else{
throw std::runtime_error("ERROR: an update operation can be performed only on Global, Sequence and Fourier grids.");
}
}

TypeOneDRule TasmanianSparseGrid::getRule() const{ return (base) ? base->getRule() : rule_none; }
Expand Down
27 changes: 26 additions & 1 deletion SparseGrids/TasmanianSparseGrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ namespace TasGrid{
* This can be done either in batches of point or in dynamic construction setup
* where values can be given one at a time in an arbitrary order (see next paragraph).
* See also the papers referenced in TasGrid::TypeDepth and TasGrid::TypeRefinement.
* - updateGlobalGrid(), updateSequenceGrid(), updateFourierGrid()
* - updateGrid()
* - setAnisotropicRefinement(), setSurplusRefinement()
* - getAnisotropicRefinement(), getSurplusRefinement()
* - clearRefinement()
Expand Down Expand Up @@ -337,6 +337,11 @@ class TasmanianSparseGrid{
//! \brief Read the grid from the given stream \b ifs using either \b binary or ASCII format.
void read(std::istream &ifs, bool binary = mode_binary);

//! \brief Overload that works directly with std::string
void write(std::string const& fname, bool binary = mode_binary) const{ write(fname.c_str(), binary); }
//! \brief Overload that works directly with std::string
void read(std::string const& fname){ read(fname.c_str()); }

/*!
* \brief Make a Global Grid using Lagrange polynomials with support over the entire domain.
*
Expand Down Expand Up @@ -612,6 +617,18 @@ class TasmanianSparseGrid{
* Array dimensions are not checked, otherwise identical to updateFourierGrid().
*/
void updateFourierGrid(int depth, TypeDepth type, const int *anisotropic_weights = nullptr, const int *level_limits = nullptr);
/*!
* \brief Based on the grid type, calls updateGlobalGrid(), updateSequenceGrid() or updateFourierGrid().
*
* \throws std::runtime_error if the grid is not Global, Sequence or Fourier.
*/
void updateGrid(int depth, TypeDepth type, std::vector<int> const &anisotropic_weights, std::vector<int> const &level_limits = std::vector<int>());
/*!
* \brief Overload using raw-arrays.
*
* Array dimensions are not checked, otherwise identical to updateFourierGrid().
*/
void updateGrid(int depth, TypeDepth type, const int *anisotropic_weights = nullptr, const int *level_limits = nullptr);

//! \brief Return the \b alpha parameter in the call to makeGlobalGrid(), or return 0 if the grid is not Global.
double getAlpha() const{ return (isGlobal()) ? get<GridGlobal>()->getAlpha() : 0.0; }
Expand Down Expand Up @@ -1030,6 +1047,14 @@ class TasmanianSparseGrid{
* \param q must have size of at least getNumOutputs().
*/
void integrate(double q[]) const;
/*!
* \brief Overload that returns a vector.
*/
std::vector<double> integrate() const{
std::vector<double> result;
integrate(result);
return result;
}
/*!
* \brief Computes the derivative (if available) of the surrogate model at an input point.
*
Expand Down
8 changes: 0 additions & 8 deletions SparseGrids/gridtestUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,18 +574,10 @@ std::vector<std::function<void(void)>> GridUnitTester::getRuntimeErrorCalls() co
TasmanianSparseGrid grid;
grid.updateGlobalGrid(2, type_level); // grid not initialized
},
[](void)->void{
auto grid = makeSequenceGrid(2, 1, 3, type_level, rule_rleja);
grid.updateGlobalGrid(2, type_level); // grid not global
},
[](void)->void{
TasmanianSparseGrid grid;
grid.updateSequenceGrid(2, type_level); // grid not initialized
},
[](void)->void{
auto grid = makeGlobalGrid(2, 1, 3, type_level, rule_rleja);
grid.updateSequenceGrid(2, type_level); // grid not sequence
},
[](void)->void{
std::vector<double> v;
auto grid = makeGlobalGrid(2, 1, 3, type_level, rule_rleja);
Expand Down
Loading

0 comments on commit ba02639

Please sign in to comment.