Skip to content

Commit

Permalink
Refactored optical mock tests to use GlobalTestBase, and to split val…
Browse files Browse the repository at this point in the history
…idation checks into separate file
  • Loading branch information
Hollenbeck-Hayden committed Nov 25, 2024
1 parent 6b053cd commit d2864bb
Show file tree
Hide file tree
Showing 13 changed files with 750 additions and 640 deletions.
8 changes: 4 additions & 4 deletions test/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ celeritas_add_test_library(testcel_celeritas
grid/CalculatorTestBase.cc
io/EventIOTestBase.cc
neutron/NeutronTestBase.cc
optical/MockImportedData.cc
optical/OpticalMockTestBase.cc
optical/OpticalTestBase.cc
optical/InteractorHostTestBase.cc
optical/ValidationUtils.cc
phys/InteractionIO.cc
phys/InteractorHostTestBase.cc
phys/MockModel.cc
Expand Down Expand Up @@ -308,12 +309,11 @@ celeritas_add_test(optical/Cerenkov.test.cc)
celeritas_add_test(optical/OpticalCollector.test.cc ${_needs_geant4})
celeritas_add_test(optical/OpticalUtils.test.cc)
celeritas_add_test(optical/Scintillation.test.cc)
celeritas_add_test(optical/Rayleigh.test.cc ${_needs_double})
celeritas_add_test(optical/Absorption.test.cc ${_needs_double})
celeritas_add_test(optical/Rayleigh.test.cc)
celeritas_add_test(optical/Absorption.test.cc)
celeritas_add_test(optical/ImportedModelAdapter.test.cc)
celeritas_add_test(optical/MfpBuilder.test.cc)
celeritas_add_test(optical/RayleighMfpCalculator.test.cc)
celeritas_add_test(optical/MockValidation.test.cc)

#-----------------------------------------------------------------------------#
# Mat
Expand Down
27 changes: 13 additions & 14 deletions test/celeritas/optical/Absorption.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "celeritas/optical/interactor/AbsorptionInteractor.hh"
#include "celeritas/optical/model/AbsorptionModel.hh"

#include "MockImportedData.hh"
#include "OpticalMockTestBase.hh"
#include "ValidationUtils.hh"
#include "celeritas_test.hh"

namespace celeritas
Expand All @@ -28,23 +29,18 @@ class AbsorptionInteractorTest : public ::celeritas::test::Test
void SetUp() override {}
};

class AbsorptionModelTest : public MockImportedData
class AbsorptionModelTest : public OpticalMockTestBase
{
protected:
void SetUp() override {}

//! Construct absorption model from mock data
std::shared_ptr<AbsorptionModel const> create_model()
{
auto models = MockImportedData::create_imported_models();

import_model_id_
= models->builtin_model_id(ImportModelClass::absorption);

auto models = std::make_shared<ImportedModels const>(
this->imported_data().optical_models);
return std::make_shared<AbsorptionModel const>(ActionId{0}, models);
}

ImportedModels::ImportedModelId import_model_id_;
};

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -80,17 +76,20 @@ TEST_F(AbsorptionModelTest, description)
// Check absorption model MFP tables match imported ones
TEST_F(AbsorptionModelTest, interaction_mfp)
{
GridStorage storage;

auto model = create_model();
auto builder = this->create_mfp_builder();
auto builder = storage.create_mfp_builder();

for (auto mat : range(OpticalMaterialId(import_materials().size())))
for (auto mat : range(OpticalMaterialId(this->num_optical_materials())))
{
model->build_mfps(mat, builder);
}

this->check_built_table_exact(
this->import_models()[import_model_id_.get()].mfp_table,
builder.grid_ids());
storage.check_built_table(
this->import_model_by_class(ImportModelClass::absorption).mfp_table,
builder.grid_ids(),
GridValidator::Exact);
}

//---------------------------------------------------------------------------//
Expand Down
36 changes: 24 additions & 12 deletions test/celeritas/optical/ImportedModelAdapter.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include "celeritas/ext/ScopedRootErrorHandler.hh"
#include "celeritas/io/ImportData.hh"

#include "MockImportedData.hh"
#include "OpticalMockTestBase.hh"
#include "ValidationUtils.hh"
#include "celeritas_test.hh"

namespace celeritas
Expand All @@ -27,10 +28,10 @@ using namespace ::celeritas::test;
// TEST HARNESS
//---------------------------------------------------------------------------//

class ImportedModelAdapterTest : public MockImportedData
class ImportedModelAdapterTest : public OpticalMockTestBase
{
protected:
void SetUp() override {}
using ImportedModelId = typename ImportedModels::ImportedModelId;

void check_model(ImportOpticalModel const& expected_model,
ImportOpticalModel const& imported_model) const
Expand All @@ -40,10 +41,21 @@ class ImportedModelAdapterTest : public MockImportedData
imported_model.mfp_table.size());
for (auto mat_id : range(imported_model.mfp_table.size()))
{
this->check_mfp(expected_model.mfp_table[mat_id],
imported_model.mfp_table[mat_id]);
check_physics_vector(expected_model.mfp_table[mat_id],
imported_model.mfp_table[mat_id]);
}
}

std::shared_ptr<ImportedModels const> const& imported_models() const
{
static std::shared_ptr<ImportedModels const> models;
if (!models)
{
models = std::make_shared<ImportedModels const>(
this->imported_data().optical_models);
}
return models;
}
};

//---------------------------------------------------------------------------//
Expand All @@ -52,8 +64,8 @@ class ImportedModelAdapterTest : public MockImportedData
// Create ImportedModels from mock data
TEST_F(ImportedModelAdapterTest, build_mock)
{
auto const& expected_models = this->import_models();
auto imported_models = this->create_imported_models();
auto const& expected_models = this->imported_data().optical_models;
auto imported_models = this->imported_models();

ASSERT_EQ(expected_models.size(), imported_models->num_models());
for (auto model_id : range(ImportedModelId{imported_models->num_models()}))
Expand All @@ -71,7 +83,7 @@ TEST_F(ImportedModelAdapterTest, builtin_map)
std::array<IMC, 3> expected_builtin_imcs{
IMC::absorption, IMC::rayleigh, IMC::wls};

auto imported_models = this->create_imported_models();
auto imported_models = this->imported_models();

// Check built-in models match expected ones
EXPECT_EQ(expected_builtin_imcs.size(), static_cast<size_type>(IMC::size_));
Expand All @@ -89,8 +101,8 @@ TEST_F(ImportedModelAdapterTest, builtin_map)
// Check adapters correctly match MFPs
TEST_F(ImportedModelAdapterTest, adapter_mfps)
{
auto const& expected_models = this->import_models();
auto imported_models = this->create_imported_models();
auto const& expected_models = this->imported_data().optical_models;
auto imported_models = this->imported_models();

ASSERT_EQ(expected_models.size(), imported_models->num_models());
for (auto model_id : range(ImportedModelId{imported_models->num_models()}))
Expand All @@ -101,8 +113,8 @@ TEST_F(ImportedModelAdapterTest, adapter_mfps)
ASSERT_EQ(expected_model.mfp_table.size(), adapter.num_materials());
for (auto mat_id : range(OpticalMaterialId{adapter.num_materials()}))
{
this->check_mfp(expected_model.mfp_table[mat_id.get()],
adapter.mfp(mat_id));
check_physics_vector(expected_model.mfp_table[mat_id.get()],
adapter.mfp(mat_id));
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions test/celeritas/optical/MfpBuilder.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//---------------------------------------------------------------------------//
#include "celeritas/optical/MfpBuilder.hh"

#include "MockImportedData.hh"
#include "OpticalMockTestBase.hh"
#include "ValidationUtils.hh"
#include "celeritas_test.hh"

namespace celeritas
Expand All @@ -21,10 +22,8 @@ using namespace ::celeritas::test;
// TEST HARNESS
//---------------------------------------------------------------------------//

class MfpBuilderTest : public MockImportedData
class MfpBuilderTest : public OpticalMockTestBase
{
protected:
void SetUp() override {}
};

//---------------------------------------------------------------------------//
Expand All @@ -33,13 +32,15 @@ class MfpBuilderTest : public MockImportedData
// Check MFP tables are built with correct structure from imported data
TEST_F(MfpBuilderTest, construct_tables)
{
std::vector<ItemRange<Grid>> tables;
auto const& models = this->import_models();
GridStorage storage;

std::vector<ItemRange<GridStorage::Grid>> tables;
auto const& models = this->imported_data().optical_models;

// Build MFP tables from imported data
for (auto const& model : models)
{
auto build = this->create_mfp_builder();
auto build = storage.create_mfp_builder();

for (auto const& mfp : model.mfp_table)
{
Expand All @@ -54,7 +55,8 @@ TEST_F(MfpBuilderTest, construct_tables)
// Check each MFP table has been built correctly
for (auto table_id : range(tables.size()))
{
this->check_built_table_exact(models[table_id].mfp_table, tables[table_id]);
storage.check_built_table(
models[table_id].mfp_table, tables[table_id], GridValidator::Exact);
}
}

Expand Down
Loading

0 comments on commit d2864bb

Please sign in to comment.