Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

662 more strict tolerance for unitintegration tests #670

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 42 additions & 54 deletions test/integration/analytical_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ constexpr size_t NUM_CELLS = 3;

double relative_error(double a, double b)
{
return abs(a - b) / abs(a);
return abs(a - b) / abs(b);
}

double relative_difference(double a, double b)
Expand Down Expand Up @@ -212,8 +212,8 @@ void test_simple_system(
EXPECT_EQ(result.state_, (micm::SolverState::Converged));
for (std::size_t i = 0; i < NUM_CELLS; ++i)
{
EXPECT_NEAR(combined_error(k1[i], state.rate_constants_[i][0], 1e-15), 0, relative_tolerance);
EXPECT_NEAR(combined_error(k2[i], state.rate_constants_[i][1], 1e-15), 0, relative_tolerance);
EXPECT_NEAR(k1[i], state.rate_constants_[i][0], relative_tolerance);
EXPECT_NEAR(k2[i], state.rate_constants_[i][1], relative_tolerance);
model_concentrations[i_time][i][idx_A] = state.variables_[i][_a];
model_concentrations[i_time][i][idx_B] = state.variables_[i][_b];
model_concentrations[i_time][i][idx_C] = state.variables_[i][_c];
Expand Down Expand Up @@ -244,27 +244,18 @@ void test_simple_system(
for (std::size_t i_cell = 0; i_cell < model_concentrations[i_time].size(); ++i_cell)
{
EXPECT_NEAR(
combined_error(
model_concentrations[i_time][i_cell][idx_A],
analytical_concentrations[i_time][i_cell][idx_A],
absolute_tolerances[0]),
0,
model_concentrations[i_time][i_cell][idx_A],
analytical_concentrations[i_time][i_cell][idx_A],
relative_tolerance)
<< "Arrays differ at index (" << i_time << ", " << i_cell << ", " << 0 << ") for " << test_label;
EXPECT_NEAR(
combined_error(
model_concentrations[i_time][i_cell][idx_B],
analytical_concentrations[i_time][i_cell][idx_B],
absolute_tolerances[1]),
0,
model_concentrations[i_time][i_cell][idx_B],
analytical_concentrations[i_time][i_cell][idx_B],
relative_tolerance)
<< "Arrays differ at index (" << i_time << ", " << i_cell << ", " << 1 << ") for " << test_label;
EXPECT_NEAR(
combined_error(
model_concentrations[i_time][i_cell][idx_C],
analytical_concentrations[i_time][i_cell][idx_C],
absolute_tolerances[2]),
0,
model_concentrations[i_time][i_cell][idx_C],
analytical_concentrations[i_time][i_cell][idx_C],
relative_tolerance)
<< "Arrays differ at index (" << i_time << ", " << i_cell << ", " << 2 << ") for " << test_label;
}
Expand Down Expand Up @@ -375,27 +366,18 @@ void test_simple_stiff_system(
for (std::size_t i_cell = 0; i_cell < model_concentrations[i_time].size(); ++i_cell)
{
EXPECT_NEAR(
combined_error(
model_concentrations[i_time][i_cell][idx_A],
analytical_concentrations[i_time][i_cell][idx_A],
absolute_tolerances[0]),
0,
model_concentrations[i_time][i_cell][idx_A],
analytical_concentrations[i_time][i_cell][idx_A],
relative_tolerance)
<< "Arrays differ at index (" << i_time << ", " << i_cell << ", " << 0 << ") for " << test_label;
EXPECT_NEAR(
combined_error(
model_concentrations[i_time][i_cell][idx_B],
analytical_concentrations[i_time][i_cell][idx_B],
absolute_tolerances[1]),
0,
model_concentrations[i_time][i_cell][idx_B],
analytical_concentrations[i_time][i_cell][idx_B],
relative_tolerance)
<< "Arrays differ at index (" << i_time << ", " << i_cell << ", " << 1 << ") for " << test_label;
EXPECT_NEAR(
combined_error(
model_concentrations[i_time][i_cell][idx_C],
analytical_concentrations[i_time][i_cell][idx_C],
absolute_tolerances[2]),
0,
model_concentrations[i_time][i_cell][idx_C],
analytical_concentrations[i_time][i_cell][idx_C],
relative_tolerance)
<< "Arrays differ at index (" << i_time << ", " << i_cell << ", " << 2 << ") for " << test_label;
}
Expand All @@ -409,7 +391,7 @@ void test_simple_stiff_system(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_troe(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 1e-10,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -564,7 +546,7 @@ void test_analytical_stiff_troe(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_photolysis(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 2e-6,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -623,7 +605,7 @@ void test_analytical_photolysis(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_stiff_photolysis(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 2e-5,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -705,7 +687,7 @@ void test_analytical_stiff_photolysis(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_ternary_chemical_activation(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 1e-08,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -861,7 +843,7 @@ void test_analytical_stiff_ternary_chemical_activation(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_tunneling(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 1e-8,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -995,7 +977,7 @@ void test_analytical_stiff_tunneling(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_arrhenius(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 1e-9,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -1129,7 +1111,7 @@ void test_analytical_stiff_arrhenius(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_branched(
BuilderPolicy builder,
double tolerance = 1e-6,
double tolerance = 1e-13,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -1323,7 +1305,7 @@ void test_analytical_stiff_branched(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_robertson(
BuilderPolicy builder,
double tolerance = 1e-8,
double relative_tolerance = 1e-8,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -1447,19 +1429,19 @@ void test_analytical_robertson(
{
double rel_error = relative_error(model_concentrations[i][_a], analytical_concentrations[i][0]);
double abs_error = std::abs(model_concentrations[i][_a] - analytical_concentrations[i][0]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 0 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;

rel_error = relative_error(model_concentrations[i][_b], analytical_concentrations[i][1]);
abs_error = std::abs(model_concentrations[i][_b] - analytical_concentrations[i][1]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 1 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;

rel_error = relative_error(model_concentrations[i][_c], analytical_concentrations[i][2]);
abs_error = std::abs(model_concentrations[i][_c] - analytical_concentrations[i][2]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 2 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;
}
Expand All @@ -1468,14 +1450,16 @@ void test_analytical_robertson(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_oregonator(
BuilderPolicy builder,
double tolerance = 1e-8,
double absolute_tolerance = 1e-8,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
/*
* This problem is described in
* Hairer, E., Wanner, G., 1996. Solving Ordinary Differential Equations II: Stiff and Differential-Algebraic Problems, 2nd
* edition. ed. Springer, Berlin ; New York. Page 144. It actually comes from Field and Noyes (1974)
* A driver for a version of this is from here, but this needs a custom forcing and jacobian and so we tried to translate it
* https://www.unige.ch/~hairer/testset/testset.html
*
* Field, R.J., Noyes, R.M., 1974. Oscillations in chemical systems. IV. Limit cycle behavior in a model of a real chemical
* reaction. The Journal of Chemical Physics 60, 1877–1884. https://doi.org/10.1063/1.1681288
Expand All @@ -1494,6 +1478,9 @@ void test_analytical_oregonator(
*
* solutions are provided here
* https://www.unige.ch/~hairer/testset/testset.html
*
* I don't understand the transfomrations. Multiplying the timestep by tau, and the concnetrations by the constants
* in the paper give very similar values.
*/

auto X = micm::Species("X");
Expand Down Expand Up @@ -1540,7 +1527,8 @@ void test_analytical_oregonator(
.SetReactions(processes)
.Build();

double time_step = 30;
double tau = 0.1610;
double time_step = 30 * tau;
size_t N = 12;

std::vector<std::vector<double>> model_concentrations(N + 1, std::vector<double>(5));
Expand Down Expand Up @@ -1628,9 +1616,9 @@ void test_analytical_oregonator(

for (size_t i = 0; i < model_concentrations.size(); ++i)
{
EXPECT_NEAR(model_concentrations[i][_x], analytical_concentrations[i][0], tolerance);
EXPECT_NEAR(model_concentrations[i][_y], analytical_concentrations[i][1], tolerance);
EXPECT_NEAR(model_concentrations[i][_z], analytical_concentrations[i][2], tolerance);
EXPECT_NEAR(model_concentrations[i][_x], analytical_concentrations[i][0], absolute_tolerance);
EXPECT_NEAR(model_concentrations[i][_y], analytical_concentrations[i][1], absolute_tolerance);
EXPECT_NEAR(model_concentrations[i][_z], analytical_concentrations[i][2], absolute_tolerance);
}
}

Expand Down Expand Up @@ -1831,7 +1819,7 @@ void test_analytical_hires(
template<class BuilderPolicy, class StateType = micm::State<>>
void test_analytical_e5(
BuilderPolicy builder,
double tolerance = 1e-8,
double relative_tolerance = 1e-8,
std::function<void(StateType&)> prepare_for_solve = [](StateType& state) {},
std::function<void(StateType&)> postpare_for_solve = [](StateType& state) {})
{
Expand Down Expand Up @@ -1954,28 +1942,28 @@ void test_analytical_e5(
double absolute_tolerance = 1e-6;
double rel_error = relative_error(model_concentrations[i][0], analytical_concentrations[i][0]);
double abs_error = std::abs(model_concentrations[i][0] - analytical_concentrations[i][0]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 0 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;

absolute_tolerance = 1e-13;
rel_error = relative_error(model_concentrations[i][1], analytical_concentrations[i][1]);
abs_error = std::abs(model_concentrations[i][1] - analytical_concentrations[i][1]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 1 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;

absolute_tolerance = 1e-13;
rel_error = relative_error(model_concentrations[i][2], analytical_concentrations[i][2]);
abs_error = std::abs(model_concentrations[i][2] - analytical_concentrations[i][2]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 2 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;

absolute_tolerance = 1e-13;
rel_error = relative_error(model_concentrations[i][3], analytical_concentrations[i][3]);
abs_error = std::abs(model_concentrations[i][3] - analytical_concentrations[i][3]);
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < tolerance)
EXPECT_TRUE(abs_error < absolute_tolerance || rel_error < relative_tolerance)
<< "Arrays differ at index (" << i << ", " << 3 << ") with relative error " << rel_error << " and absolute error "
<< abs_error;
}
Expand Down
14 changes: 7 additions & 7 deletions test/integration/analytical_surface_rxn_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ void test_analytical_surface_rxn(

size_t idx_foo = 0, idx_bar = 1, idx_baz = 2;

std::cout << std::setw(3) << "i" << std::setw(7) << "time" << std::setw(11) << "true foo" << std::setw(11) << "model foo"
<< std::setw(11) << "true bar" << std::setw(11) << "model bar" << std::setw(11) << "true baz" << std::setw(11)
<< "model baz" << std::endl;
// std::cout << std::setw(3) << "i" << std::setw(7) << "time" << std::setw(11) << "true foo" << std::setw(11) << "model foo"
// << std::setw(11) << "true bar" << std::setw(11) << "model bar" << std::setw(11) << "true baz" << std::setw(11)
// << "model baz" << std::endl;

for (int i = 1; i <= nstep; ++i)
{
Expand Down Expand Up @@ -122,9 +122,9 @@ void test_analytical_surface_rxn(
EXPECT_NEAR(0, relative_error(analytic_conc[i][idx_bar], model_conc[i][idx_bar]), tolerance);
EXPECT_NEAR(0, relative_error(analytic_conc[i][idx_baz], model_conc[i][idx_baz]), tolerance);

std::cout << std::setw(3) << i << " " << std::fixed << std::setprecision(2) << std::setw(5) << time << " "
<< std::fixed << std::setprecision(7) << analytic_conc[i][idx_foo] << " " << model_conc[i][idx_foo] << " "
<< analytic_conc[i][idx_bar] << " " << model_conc[i][idx_bar] << " " << analytic_conc[i][idx_baz] << " "
<< model_conc[i][idx_baz] << " " << std::endl;
// std::cout << std::setw(3) << i << " " << std::fixed << std::setprecision(2) << std::setw(5) << time << " "
// << std::fixed << std::setprecision(7) << analytic_conc[i][idx_foo] << " " << model_conc[i][idx_foo] << " "
// << analytic_conc[i][idx_bar] << " " << model_conc[i][idx_bar] << " " << analytic_conc[i][idx_baz] << " "
// << model_conc[i][idx_baz] << " " << std::endl;
}
}
Loading
Loading