Skip to content

Commit

Permalink
Clean up optical mock test data (#1519)
Browse files Browse the repository at this point in the history
* Refactored optical mock tests to use GlobalTestBase, and to split validation checks into separate file
* Added missing includes to ValidationUtils.hh
* Changed templated unit constructors in optical tests to not explicitly require real_types
* Added some of Seth's suggested changes
* Test of using googletest formatters for grids and tables
* Cleaned up changes to GridAccessor and formatting
* Removed dangling override of IsVecEq in ValidationUtils.cc
* Fixed size_t vs size_type in native_array_from
* Changed native_array_from signature to avoid collisions
* Changed native_array_from_indexer to use integer sequence to support size_type instead of size_t
* Removed native_array_from for fixed size array argument
* Sort optical tests in cmakelists
  • Loading branch information
hhollenb authored Dec 5, 2024
1 parent cbf14de commit e88f25a
Show file tree
Hide file tree
Showing 15 changed files with 825 additions and 643 deletions.
14 changes: 14 additions & 0 deletions src/celeritas/UnitTypes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ struct EElectron
//!@{
//! \name Atomic units

//! Atom-scale energy
struct ElectronVolt
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
#if CELERITAS_UNITS == CELERITAS_UNITS_CLHEP
return units::megaelectronvolt / real_type(1e6);
#else
return constants::e_electron * units::volt;
#endif
}
static char const* label() { return "eV"; }
};

//! Nucleus-scale energy
struct Mev
{
Expand Down
15 changes: 8 additions & 7 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 @@ -306,17 +307,17 @@ celeritas_add_test(io/SeltzerBergerReader.test.cc ${_needs_geant4})

#-----------------------------------------------------------------------------#
# Optical

celeritas_add_test(optical/Absorption.test.cc)
celeritas_add_test(optical/Cherenkov.test.cc)
celeritas_add_test(optical/ImportedModelAdapter.test.cc)
celeritas_add_test(optical/MfpBuilder.test.cc)
celeritas_add_test(optical/OpticalCollector.test.cc ${_needs_geant4})
celeritas_add_test(optical/OpticalUtils.test.cc)
celeritas_add_test(optical/Rayleigh.test.cc)
celeritas_add_test(optical/RayleighMfpCalculator.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/WavelengthShift.test.cc ${_needs_double})
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
26 changes: 12 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,19 @@ TEST_F(AbsorptionModelTest, description)
// Check absorption model MFP tables match imported ones
TEST_F(AbsorptionModelTest, interaction_mfp)
{
OwningGridAccessor 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());
EXPECT_TABLE_EQ(
this->import_model_by_class(ImportModelClass::absorption).mfp_table,
storage(builder.grid_ids()));
}

//---------------------------------------------------------------------------//
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]);
EXPECT_GRID_EQ(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));
EXPECT_GRID_EQ(expected_model.mfp_table[mat_id.get()],
adapter.mfp(mat_id));
}
}
}
Expand Down
17 changes: 9 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();
OwningGridAccessor storage;

std::vector<ItemRange<OwningGridAccessor::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,7 @@ 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]);
EXPECT_TABLE_EQ(models[table_id].mfp_table, storage(tables[table_id]));
}
}

Expand Down
Loading

0 comments on commit e88f25a

Please sign in to comment.