Skip to content

Commit

Permalink
Fix unit tests when using SI (#1100)
Browse files Browse the repository at this point in the history
* Fix units in vacuum permittivity test
* Fix track sort test
* Loosen tolerance in along-step test
* Suppress broken test
* Always build demo rasterizer
* Read rasterizer input as cm
  • Loading branch information
sethrj committed Feb 5, 2024
1 parent 0f77652 commit 3a84988
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 27 deletions.
4 changes: 1 addition & 3 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ add_subdirectory(celer-g4)
if(CELERITAS_BUILD_DEMOS)
add_subdirectory(demo-interactor)
add_subdirectory(demo-geo-check)
if(CELERITAS_USE_CUDA)
add_subdirectory(demo-rasterizer)
endif()
add_subdirectory(demo-rasterizer)
endif()

#-----------------------------------------------------------------------------#
Expand Down
32 changes: 20 additions & 12 deletions app/demo-rasterizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
#-----------------------------------------------------------------------------#

add_executable(demo-rasterizer
set(SOURCES
demo-rasterizer.cc
RDemoRunner.cc
RDemoKernel.cu
ImageIO.cc
ImageStore.cc
)
if(CELERITAS_USE_HIP)
set_source_files_properties(
if(CELERITAS_USE_CUDA OR CELERITAS_USE_HIP)
list(APPEND SOURCES
RDemoKernel.cu
PROPERTIES LANGUAGE HIP
)
if(CELERITAS_USE_HIP)
set_source_files_properties(
RDemoKernel.cu
PROPERTIES LANGUAGE HIP
)
endif()
endif()

set(_rasterizer_libs
Expand All @@ -25,6 +29,8 @@ set(_rasterizer_libs
if(CELERITAS_CORE_GEO STREQUAL "VecGeom")
list(APPEND _rasterizer_libs VecGeom::vecgeom)
endif()

add_executable(demo-rasterizer ${SOURCES})
celeritas_target_link_libraries(demo-rasterizer ${_rasterizer_libs})

#-----------------------------------------------------------------------------#
Expand All @@ -40,6 +46,14 @@ set(_gdml_inp "${CELER_APP_DATA_DIR}/two-boxes.gdml")
add_test(NAME "app/demo-rasterizer"
COMMAND "$<TARGET_FILE:Python::Interpreter>" "${_driver}" "${_gdml_inp}"
)

if((CELERITAS_CORE_GEO STREQUAL "ORANGE") OR CELER_DISABLE_DEVICE)
# two-boxes.org.json isn't set up in the test directory
# or device is unavailable
set(_maybe_disable DISABLED true)
else()
set(_maybe_disable)
endif()
set(_env
"CELERITAS_DEMO_EXE=$<TARGET_FILE:demo-rasterizer>"
"CELER_DISABLE_PARALLEL=1"
Expand All @@ -49,13 +63,7 @@ set_tests_properties("app/demo-rasterizer" PROPERTIES
RESOURCE_LOCK gpu
REQUIRED_FILES "${_driver};${_gdml_inp}"
LABELS "app;nomemcheck;gpu"
${_maybe_disable}
)
if(CELERITAS_CORE_GEO STREQUAL "ORANGE" OR CELER_DISABLE_DEVICE)
# two-boxes.org.json isn't set up in the test directory
# or device is unavailable
set_tests_properties("app/demo-rasterizer" PROPERTIES
DISABLED true
)
endif()

#-----------------------------------------------------------------------------#
26 changes: 22 additions & 4 deletions app/demo-rasterizer/ImageIO.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@
//---------------------------------------------------------------------------//
#include "ImageIO.hh"

#include "celeritas_cmake_strings.h"
#include "corecel/cont/ArrayIO.json.hh"
#include "corecel/math/ArrayOperators.hh"
#include "celeritas/Quantities.hh"
#include "celeritas/Types.hh"

#include "ImageStore.hh"

namespace celeritas
{
namespace app
{
namespace
{
//---------------------------------------------------------------------------//
Real3 from_cm(Real3 const& r)
{
using CmPoint = Quantity<units::Centimeter, Real3>;
return native_value_from(CmPoint{r});
}

//---------------------------------------------------------------------------//
} // namespace

//---------------------------------------------------------------------------//
//!@{
//! I/O routines for JSON
Expand All @@ -23,13 +39,14 @@ void to_json(nlohmann::json& j, ImageRunArgs const& v)
j = nlohmann::json{{"lower_left", v.lower_left},
{"upper_right", v.upper_right},
{"rightward_ax", v.rightward_ax},
{"vertical_pixels", v.vertical_pixels}};
{"vertical_pixels", v.vertical_pixels},
{"_units", celeritas_units}};
}

void from_json(nlohmann::json const& j, ImageRunArgs& v)
{
j.at("lower_left").get_to(v.lower_left);
j.at("upper_right").get_to(v.upper_right);
v.lower_left = from_cm(j.at("lower_left").get<Real3>());
v.upper_right = from_cm(j.at("upper_right").get<Real3>());
j.at("rightward_ax").get_to(v.rightward_ax);
j.at("vertical_pixels").get_to(v.vertical_pixels);
}
Expand All @@ -41,7 +58,8 @@ void to_json(nlohmann::json& j, ImageStore const& v)
{"right_ax", v.right_ax()},
{"pixel_width", v.pixel_width()},
{"dims", v.dims()},
{"int_size", sizeof(int)}};
{"int_size", sizeof(int)},
{"_units", celeritas_units}};
}

//!@}
Expand Down
10 changes: 10 additions & 0 deletions app/demo-rasterizer/RDemoKernel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "celeritas/geo/GeoData.hh"

#include "ImageData.hh"
Expand All @@ -24,6 +26,14 @@ void trace(GeoParamsCRefDevice const& geo_params,
GeoStateRefDevice const& geo_state,
ImageData const& image);

#if !CELER_USE_DEVICE
inline void
trace(GeoParamsCRefDevice const&, GeoStateRefDevice const&, ImageData const&)
{
CELER_NOT_CONFIGURED("CUDA or HIP");
}
#endif

//---------------------------------------------------------------------------//
} // namespace app
} // namespace celeritas
1 change: 1 addition & 0 deletions app/demo-rasterizer/simple-driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

inp = {
'image': {
# TODO: input is cm for now; add 'units' argument?
'lower_left': [-10, -10, 0],
'upper_right': [10, 10, 0],
'rightward_ax': [1, 0, 0],
Expand Down
10 changes: 10 additions & 0 deletions scripts/cmake-presets/goldfinger.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
"CELERITAS_USE_VecGeom": {"type": "BOOL", "value": "ON"}
}
},
{
"name": "si",
"displayName": "With SI units",
"inherits": [".base", ".debug", "default"],
"cacheVariables": {
"CELERITAS_UNITS": "SI",
"CELERITAS_CORE_GEO": "VecGeom",
"CELERITAS_USE_VecGeom": {"type": "BOOL", "value": "ON"}
}
},
{
"name": "vecgeom",
"displayName": "With vecgeom",
Expand Down
4 changes: 2 additions & 2 deletions src/celeritas/ext/GeantGeoTrackView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class GeantGeoTrackView

// Temporary data
G4ThreeVector g4pos_;
G4ThreeVector g4dir_;
real_type g4safety_;
G4ThreeVector g4dir_; // [mm]
real_type g4safety_; // [mm]

//// HELPER FUNCTIONS ////

Expand Down
8 changes: 6 additions & 2 deletions test/celeritas/Constants.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ TEST(ConstantsTest, clhep)
a0_bohr / units::centimeter, CLHEP::Bohr_radius / CLHEP::cm, 1e-7);
EXPECT_SOFT_NEAR(alpha_fine_structure, CLHEP::fine_structure_const, 1e-9);
EXPECT_SOFT_NEAR(atomic_mass / units::gram, CLHEP::amu / CLHEP::gram, 1e-7);
EXPECT_SOFT_NEAR(eps_electric / (units::gram * units::centimeter),
CLHEP::epsilon0 / (CLHEP::gram * CLHEP::cm),
EXPECT_SOFT_NEAR(eps_electric
/ (units::coulomb * units::coulomb
/ (units::newton * units::meter * units::meter)),
CLHEP::epsilon0
/ (CLHEP::coulomb * CLHEP::coulomb
/ (CLHEP::newton * CLHEP::meter * CLHEP::meter)),
1e-7);
EXPECT_SOFT_NEAR(h_planck, CLHEP::h_Planck, 1e-7);
EXPECT_SOFT_NEAR(k_boltzmann, CLHEP::k_Boltzmann, 1e-7);
Expand Down
6 changes: 4 additions & 2 deletions test/celeritas/GenericGeoTestBase.t.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace celeritas
{
namespace test
{

//---------------------------------------------------------------------------//
//
template<class HP>
Expand Down Expand Up @@ -201,7 +200,10 @@ auto GenericGeoTestBase<HP>::track(Real3 const& pos_cm,
}
auto new_next = geo.find_next_step();
EXPECT_TRUE(new_next.boundary);
EXPECT_SOFT_NEAR(new_next.distance, next.distance / 2, 1e-10);
EXPECT_SOFT_NEAR(new_next.distance, next.distance / 2, 1e-10)
<< "reinitialized distance mismatch at index "
<< result.volumes.size() - 1 << ": " << init.pos
<< " along " << init.dir;
}
}
geo.move_to_boundary();
Expand Down
15 changes: 15 additions & 0 deletions test/celeritas/ext/GeantGeo.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,22 @@ TEST_F(SolidsTest, output)

TEST_F(SolidsTest, trace)
{
if (CELERITAS_UNITS != CELERITAS_UNITS_SI)
{
/*
* Note: with SI, this trace skips over the sphere! (During
* reinitialization halfway between the polycone and expected sphere
* boundary.) Perhaps it's some really really weird floating point edge
* case?
*
* Value of: next.distance / 2
* Actual: 0.57499999999999996
* Expected: new_next.distance
* Which is: 1.5750000000000004
* reinitialized distance mismatch at index 5: {1.8250000000000004,0,0}
* Actual values: {"ellipsoid1", "World", "polycone1", "World",
* "polycone1", "World", "box500", ...}
*/
SCOPED_TRACE("Center -x");
auto result = this->track({375, 0, 0}, {-1, 0, 0});

Expand Down
2 changes: 1 addition & 1 deletion test/celeritas/global/AlongStep.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ TEST_F(SimpleCmsRZFieldAlongStepTest, msc_rzfield)

auto result = this->run(inp, num_tracks);
EXPECT_SOFT_EQ(4.1632772063250023, result.displacement);
EXPECT_SOFT_NEAR(-0.59445532857679839, result.angle, 2e-12);
EXPECT_SOFT_NEAR(-0.59445532857679839, result.angle, 1e-11);
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/celeritas/track/TrackSort.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "corecel/data/Collection.hh"
#include "corecel/io/LogContextException.hh"
#include "celeritas/Types.hh"
#include "celeritas/UnitUtils.hh"
#include "celeritas/ext/GeantPhysicsOptions.hh"
#include "celeritas/global/ActionRegistry.hh"
#include "celeritas/global/CoreParams.hh"
Expand Down Expand Up @@ -52,7 +53,7 @@ class TestEm3NoMsc : public TestEm3Base
CELER_ASSERT(p.particle_id);
p.energy = units::MevEnergy{10000};
p.track_id = TrackId{0};
p.position = {-22, 0, 0};
p.position = from_cm({-22, 0, 0});
p.direction = {1, 0, 0};
p.time = 0;

Expand Down

0 comments on commit 3a84988

Please sign in to comment.