Skip to content

Commit

Permalink
throwing error on mismatched size (#610)
Browse files Browse the repository at this point in the history
* throwing error on mismatched size

* using a copy of the paramteres so that a builder can be repeatedly used

* adding const

* correcting number of tolerances for robertson
  • Loading branch information
K20shores committed Aug 5, 2024
1 parent 5878c6a commit 29d9051
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/micm/solver/solver_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ namespace micm

/// @brief Creates an instance of Solver with a properly configured ODE solver
/// @return An instance of Solver
auto Build();
auto Build() const;

protected:
/// @brief Checks for unused species
/// @throws std::system_error if an unused species is found
void UnusedSpeciesCheck();
void UnusedSpeciesCheck() const;

/// @brief Gets a map of species to their index
/// @return The species map
Expand Down
26 changes: 20 additions & 6 deletions include/micm/solver/solver_builder.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ enum class MicmSolverBuilderErrc
UnusedSpecies = 1, // Unused species present in the chemical system
MissingChemicalSystem = 2, // Missing chemical system
MissingReactions = 3, // Missing processes
MissingChemicalSpecies = 4 // Missing chemical species
MissingChemicalSpecies = 4, // Missing chemical species
InvalidToleranceSize = 5 // Invalid tolerance size
};

namespace std
Expand Down Expand Up @@ -37,7 +38,10 @@ namespace
return "Missing chemical system. Use the SetSystem function to set the chemical system.";
case MicmSolverBuilderErrc::MissingReactions:
return "Missing reactions. Use the SetReactions function to set the processes.";
case MicmSolverBuilderErrc::MissingChemicalSpecies: return "Provided chemical system contains no species.";
case MicmSolverBuilderErrc::MissingChemicalSpecies:
return "Provided chemical system contains no species.";
case MicmSolverBuilderErrc::InvalidToleranceSize:
return "Provided tolerances do not match the number of species in the chemical system. Either provide none and allow defaults to be set or pass in a number equal to the number of chemical species.";
default: return "Unknown error";
}
}
Expand Down Expand Up @@ -198,7 +202,7 @@ namespace micm
SparseMatrixPolicy,
RatesPolicy,
LinearSolverPolicy,
StatePolicy>::UnusedSpeciesCheck()
StatePolicy>::UnusedSpeciesCheck() const
{
if (ignore_unused_species_)
{
Expand Down Expand Up @@ -285,6 +289,12 @@ namespace micm
StatePolicy>::
SetAbsoluteTolerances(std::vector<double>& tolerances, const std::map<std::string, std::size_t>& species_map) const
{
if (tolerances.size() > 0 && tolerances.size() != species_map.size())
{
throw std::system_error(
make_error_code(MicmSolverBuilderErrc::InvalidToleranceSize),
"Invalid absolute tolerance vector size");
}
// if the tolerances aren't already set, initialize them and then set based off of information in the system
if (tolerances.size() != species_map.size())
{
Expand Down Expand Up @@ -345,8 +355,12 @@ namespace micm
SparseMatrixPolicy,
RatesPolicy,
LinearSolverPolicy,
StatePolicy>::Build()
StatePolicy>::Build() const
{
// make a copy of the options so that the builder can be used repeatedly
// this matters because the absolute tolerances must be set to match the system size, and that may change
auto options = this->options_;

if (!valid_system_)
{
throw std::system_error(make_error_code(MicmSolverBuilderErrc::MissingChemicalSystem), "Missing chemical system.");
Expand All @@ -366,7 +380,7 @@ namespace micm
}

this->UnusedSpeciesCheck();
this->SetAbsoluteTolerances(this->options_.absolute_tolerance_, species_map);
this->SetAbsoluteTolerances(options.absolute_tolerance_, species_map);

RatesPolicy rates(this->reactions_, species_map);
auto nonzero_elements = rates.NonZeroJacobianElements();
Expand All @@ -387,7 +401,7 @@ namespace micm
.nonzero_jacobian_elements_ = nonzero_elements };

return Solver<SolverPolicy, StatePolicy>(
SolverPolicy(this->options_, std::move(linear_solver), std::move(rates), jacobian),
SolverPolicy(options, std::move(linear_solver), std::move(rates), jacobian),
state_parameters,
this->number_of_grid_cells_,
number_of_species,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/cuda/test_cuda_analytical_rosenbrock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ TEST(AnalyticalExamplesCudaRosenbrock, Robertson)
auto rosenbrock_solver = [](auto params)
{
params.relative_tolerance_ = 1e-10;
params.absolute_tolerance_ = std::vector<double>(5, params.relative_tolerance_ * 1e-2);
params.absolute_tolerance_ = std::vector<double>(3, params.relative_tolerance_ * 1e-2);
return builderType1Cell(params);
};

Expand Down
2 changes: 1 addition & 1 deletion test/integration/jit/test_jit_analytical_rosenbrock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ TEST(AnalyticalExamplesJitRosenbrock, Robertson)
auto rosenbrock_solver = [](auto params)
{
params.relative_tolerance_ = 1e-10;
params.absolute_tolerance_ = std::vector<double>(5, params.relative_tolerance_ * 1e-2);
params.absolute_tolerance_ = std::vector<double>(3, params.relative_tolerance_ * 1e-2);
return BuilderType<1>(params);
};

Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_analytical_rosenbrock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ TEST(AnalyticalExamples, Robertson)
auto rosenbrock_solver = [](auto params)
{
params.relative_tolerance_ = 1e-10;
params.absolute_tolerance_ = std::vector<double>(5, params.relative_tolerance_ * 1e-2);
params.absolute_tolerance_ = std::vector<double>(3, params.relative_tolerance_ * 1e-2);
return BuilderType(params);
};

Expand Down
28 changes: 28 additions & 0 deletions test/unit/cuda/solver/test_cuda_solver_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,32 @@ TEST(SolverBuilder, CanBuildCudaSolvers)
.SetReactions(reactions)
.SetNumberOfGridCells(L)
.Build();
}

TEST(SolverBuilder, MismatchedToleranceSizeIsCaught)
{
auto params = micm::RosenbrockSolverParameters::ThreeStageRosenbrockParameters();
// too many
params.absolute_tolerance_ = { 1e-6, 1e-6, 1e-6, 1e-6, 1e-6 };

constexpr std::size_t L = 4;
using cuda_builder = micm::CudaSolverBuilder<micm::CudaRosenbrockSolverParameters, L>;

EXPECT_ANY_THROW(
cuda_builder(params)
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(L)
.Build();
);

// too few
params.absolute_tolerance_ = { 1e-6, 1e-6 };
EXPECT_ANY_THROW(
cuda_builder(params)
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(L)
.Build();
);
}
28 changes: 28 additions & 0 deletions test/unit/jit/solver/test_jit_solver_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,32 @@ TEST(SolverBuilder, CanBuildJitRosenbrock)
.SetReactions(reactions)
.SetNumberOfGridCells(L)
.Build();
}

TEST(SolverBuilder, MismatchedToleranceSizeIsCaught)
{
auto params = micm::RosenbrockSolverParameters::ThreeStageRosenbrockParameters();
// too many
params.absolute_tolerance_ = { 1e-6, 1e-6, 1e-6, 1e-6, 1e-6 };

constexpr std::size_t L = 4;
using jit_builder = micm::JitSolverBuilder<micm::JitRosenbrockSolverParameters, L>;

EXPECT_ANY_THROW(
jit_builder(params)
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(L)
.Build();
);

// too few
params.absolute_tolerance_ = { 1e-6, 1e-6 };
EXPECT_ANY_THROW(
jit_builder(params)
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(L)
.Build();
);
}
32 changes: 32 additions & 0 deletions test/unit/solver/test_solver_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,36 @@ TEST(SolverBuilder, CanBuildRosenbrock)
.SetReactions(reactions)
.SetNumberOfGridCells(1)
.Build();
}

TEST(SolverBuilder, MismatchedToleranceSizeIsCaught)
{
auto params = micm::RosenbrockSolverParameters::ThreeStageRosenbrockParameters();
// too many
params.absolute_tolerance_ = { 1e-6, 1e-6, 1e-6, 1e-6, 1e-6 };
EXPECT_ANY_THROW(
micm::CpuSolverBuilder<micm::RosenbrockSolverParameters>(params)
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(1)
.Build();
);

constexpr std::size_t L = 4;
// too few
params.absolute_tolerance_ = { 1e-6, 1e-6 };

auto builder = micm::CpuSolverBuilder<
micm::RosenbrockSolverParameters,
micm::VectorMatrix<double, L>,
micm::SparseMatrix<double, micm::SparseMatrixVectorOrdering<L>>
>(params);

EXPECT_ANY_THROW(
builder
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(1)
.Build();
);
}

0 comments on commit 29d9051

Please sign in to comment.