Skip to content

Commit f021243

Browse files
authored
Simplify Geant4 user application interface (#1729)
* Remove build/buildformaster in simple integration * Clean includes * Add CeleritasG4 header and Celeritas::G4 target * Update doc * Use an alias instead of an interface library
1 parent 6735a9f commit f021243

26 files changed

+158
-252
lines changed

Diff for: .github/workflows/build-spack.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ jobs:
307307
&& matrix.special != 'clang-tidy'
308308
}}
309309
env:
310-
CELER_DISABLE_ACCEL_EXAMPLES: 1 # Only run minimal example
310+
CELER_DISABLE_G4_EXAMPLES: 1 # Only run minimal example
311311
run: |
312312
. ${SPACK_VIEW}/rc
313313
CELER_INSTALL_DIR=${PWD}/build ./scripts/ci/test-examples.sh
@@ -337,7 +337,7 @@ jobs:
337337
./bin/celer-sim --config
338338
- name: Build examples
339339
env:
340-
CELER_DISABLE_ACCEL_EXAMPLES: >-
340+
CELER_DISABLE_G4_EXAMPLES: >-
341341
${{(
342342
matrix.special == 'float'
343343
|| !matrix.geant

Diff for: README.md

+15-25
Original file line numberDiff line numberDiff line change
@@ -87,43 +87,30 @@ library calls to support VecGeom's use of CUDA RDC:
8787
${Geant4_LIBRARIES}
8888
```
8989

90-
A few includes expose Celeritas classes to the user code:
90+
One catch-all include exposes the Celeritas high-level offload classes to user
91+
code:
9192
```diff
92-
--- example/accel/trackingmanager-offload.cc
93-
+++ example/accel/trackingmanager-offload.cc
94-
@@ -36,2 +36,10 @@
93+
--- example/geant4/trackingmanager-offload.cc
94+
+++ example/geant4/trackingmanager-offload.cc
95+
@@ -31,2 +31,4 @@
9596

9697
+// Celeritas
97-
+#include <accel/AlongStepFactory.hh>
98-
+#include <accel/SetupOptions.hh>
99-
+#include <accel/TrackingManagerConstructor.hh>
100-
+#include <accel/TrackingManagerIntegration.hh>
101-
+
102-
+using TMI = celeritas::TrackingManagerIntegration;
103-
+
104-
namespace
98+
+#include <CeleritasG4.hh>
99+
105100
```
106101

107-
Celeritas uses the build/run actions to set up and tear down cleanly:
102+
Celeritas uses the run action to set up and tear down cleanly:
108103
```diff
109104
--- example/accel/trackingmanager-offload.cc
110105
+++ example/accel/trackingmanager-offload.cc
111-
@@ -134,2 +142,3 @@ class RunAction final : public G4UserRunAction
112-
void BeginOfRunAction(G4Run const* run) final {
106+
@@ -133,2 +138,3 @@ class RunAction final : public G4UserRunAction
107+
{
113108
+ TMI::Instance().BeginOfRunAction(run);
114109
}
115-
@@ -137,2 +146,3 @@ class RunAction final : public G4UserRunAction
116-
void EndOfRunAction(G4Run const* run) final {
110+
@@ -136,2 +142,3 @@ class RunAction final : public G4UserRunAction
111+
{
117112
+ TMI::Instance().EndOfRunAction(run);
118113
}
119-
@@ -179,2 +189,3 @@ class ActionInitialization final : public G4VUserActionInitialization
120-
void BuildForMaster() const final {
121-
+ TMI::Instance().BuildForMaster();
122-
}
123-
@@ -185,2 +197,3 @@ class ActionInitialization final : public G4VUserActionInitialization
124-
void Build() const final{
125-
+ TMI::Instance().Build();
126-
}
127114
```
128115

129116
And integrates into the tracking loop primarily using the `G4TrackingManager`
@@ -141,6 +128,9 @@ interface:
141128
+ new celeritas::TrackingManagerConstructor(&tmi));
142129
```
143130

131+
More flexible alternatives to this high level interface, compatible with other
132+
run manager implementations and older versions of Geant4, are described in the
133+
manual.
144134

145135
[integration]: https://celeritas-project.github.io/celeritas/user/usage/integration.html
146136

Diff for: cmake/CeleritasConfig.cmake.in

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ if(NOT TARGET celeritas)
178178

179179
# Load targets created by `install(EXPORT celeritas-targets`
180180
include("${CELERITAS_CMAKE_DIR}/CeleritasTargets.cmake")
181+
182+
# Add simple alias for user apps
183+
if(CELERITAS_USE_Geant4)
184+
add_library(Celeritas::G4 ALIAS Celeritas::accel)
185+
else()
186+
add_library(Celeritas::G4 ALIAS Celeritas::BuildFlags)
187+
endif()
181188
endif()
182189

183190
#-----------------------------------------------------------------------------#

Diff for: doc/example/geant4.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The key components are global :cpp:struct:`celeritas::SetupOptions` and
3232
CMake infrastructure
3333
--------------------
3434

35-
.. literalinclude:: ../../example/accel/CMakeLists.txt
35+
.. literalinclude:: ../../example/geant4/CMakeLists.txt
3636
:language: cmake
3737
:start-at: project(
3838
:end-before: END EXAMPLE CODE
@@ -51,17 +51,17 @@ Offload using a concrete G4VTrackingManager
5151
The tracking manager is the preferred way of offloading tracks to Celeritas. It
5252
requires Geant4 11.0 or higher.
5353

54-
.. literalinclude:: ../../example/accel/trackingmanager-offload.cc
54+
.. literalinclude:: ../../example/geant4/trackingmanager-offload.cc
5555
:start-at: #include
5656

5757
Offload using a concrete G4UserTrackingAction
5858
---------------------------------------------
5959

60-
.. literalinclude:: ../../example/accel/simple-offload.cc
60+
.. literalinclude:: ../../example/geant4/simple-offload.cc
6161
:start-at: #include
6262

6363
Offload using a concrete G4VFastSimulationModel
6464
-----------------------------------------------
6565

66-
.. literalinclude:: ../../example/accel/fastsim-offload.cc
66+
.. literalinclude:: ../../example/geant4/fastsim-offload.cc
6767
:start-at: #include

Diff for: doc/usage/integration.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ recommended for all applications that support Geant4 11.0 or higher.
2121
1. Find and link in your CMake project:
2222

2323
- Find the Celeritas package.
24-
- Link ``Celeritas::accel`` .
24+
- Link ``Celeritas::G4`` .
2525
- Use ``celeritas_target_link_libraries`` instead of
2626
``target_link_libraries`` when both VecGeom and CUDA are enabled.
2727

2828
2. Set up physics offloading in your "main" function.
2929

30+
- Include ``<CeleritasG4.hh>``
3031
- Register the ``TrackingManagerConstructor``, which tells Geant4 to send EM
3132
tracks to Celeritas rather than the main tracking loop.
3233
- Tweak the ``SetupOptions`` based on problem requirements, or use the
3334
:ref:`g4_ui_macros`.
3435

3536
3. Add hooks to safely set up and tear down Celeritas and its GPU code.
3637

37-
- Initialize logging in ``UserActionInitialization``, using
38-
``BuildForMaster`` (MT mode) or ``Build`` (serial).
39-
- In ``UserRunAction``, ``BeginOfRunAction`` initializes problem data
40-
(master or serial) and local state (worker or serial), and
41-
``EndOfRunAction`` safely deallocates everything.
38+
- Call ``BeginOfRunAction`` at the beginning of the run to initialize
39+
problem data (master or serial) and local state (worker or serial).
40+
- Call ``EndOfRunAction`` at the end of the run to safely deallocate
41+
everything.
4242

4343
The changes for a simple application look like:
4444

45-
.. literalinclude:: ../../example/accel/add-celer.diff
45+
.. literalinclude:: ../../example/geant4/add-celer.diff
4646
:language: diff
4747

4848
CMake integration

Diff for: example/accel/add-celer.diff

-82
This file was deleted.
File renamed without changes.

Diff for: example/accel/CMakeLists.txt renamed to example/geant4/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ endif()
1717

1818
add_executable(simple-offload simple-offload.cc)
1919
celeritas_target_link_libraries(simple-offload
20-
Celeritas::accel
20+
Celeritas::G4
2121
${Geant4_LIBRARIES}
2222
)
2323

@@ -27,7 +27,7 @@ endif()
2727

2828
add_executable(fastsim-offload fastsim-offload.cc)
2929
celeritas_target_link_libraries(fastsim-offload
30-
Celeritas::accel
30+
Celeritas::G4
3131
${Geant4_LIBRARIES}
3232
)
3333

@@ -36,7 +36,7 @@ if(Geant4_VERSION VERSION_LESS 11.0)
3636
else()
3737
add_executable(trackingmanager-offload trackingmanager-offload.cc)
3838
celeritas_target_link_libraries(trackingmanager-offload
39-
Celeritas::accel
39+
Celeritas::G4
4040
${Geant4_LIBRARIES}
4141
)
4242
endif()

Diff for: example/geant4/add-celer.diff

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
diff --git example/geant4/trackingmanager-offload.cc example/geant4/trackingmanager-offload.cc
2+
index f11c900f0..8fda9cd09 100644
3+
--- example/geant4/trackingmanager-offload.cc
4+
+++ example/geant4/trackingmanager-offload.cc
5+
@@ -31,2 +31,5 @@
6+
7+
+// Celeritas
8+
+#include <CeleritasG4.hh>
9+
+
10+
// Celeritas convenience utils
11+
@@ -35,2 +38,4 @@
12+
13+
+using TMI = celeritas::TrackingManagerIntegration;
14+
+
15+
namespace
16+
@@ -133,2 +138,3 @@ class RunAction final : public G4UserRunAction
17+
{
18+
+ TMI::Instance().BeginOfRunAction(run);
19+
}
20+
@@ -136,2 +142,3 @@ class RunAction final : public G4UserRunAction
21+
{
22+
+ TMI::Instance().EndOfRunAction(run);
23+
}
24+
@@ -213,4 +220,8 @@ int main()
25+
26+
+ auto& tmi = TMI::Instance();
27+
+
28+
// Use FTFP_BERT, but use Celeritas tracking for e-/e+/g
29+
auto* physics_list = new FTFP_BERT{/* verbosity = */ 0};
30+
+ physics_list->RegisterPhysics(
31+
+ new celeritas::TrackingManagerConstructor(&tmi));
32+
run_manager->SetUserInitialization(physics_list);
33+
@@ -218,2 +229,4 @@ int main()
34+
35+
+ tmi.SetOptions(MakeOptions());
36+
+
37+
run_manager->Initialize();

Diff for: example/accel/fastsim-offload.cc renamed to example/geant4/fastsim-offload.cc

+8-18
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
33
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
44
//---------------------------------------------------------------------------//
5-
//! \file accel/fastsim-offload.cc
5+
//! \file geant4/fastsim-offload.cc
66
//---------------------------------------------------------------------------//
77

88
#include <algorithm>
99
#include <iterator>
1010
#include <type_traits>
11+
12+
// Geant4
1113
#include <FTFP_BERT.hh>
1214
#include <G4Box.hh>
1315
#include <G4Electron.hh>
@@ -41,11 +43,10 @@
4143
# include <G4MTRunManager.hh>
4244
#endif
4345

44-
#include <accel/AlongStepFactory.hh>
45-
#include <accel/FastSimulationIntegration.hh>
46-
#include <accel/FastSimulationModel.hh>
47-
#include <accel/SetupOptions.hh>
48-
#include <corecel/Macros.hh>
46+
// Celeritas
47+
#include <CeleritasG4.hh>
48+
49+
// Utility includes
4950
#include <corecel/io/Logger.hh>
5051

5152
using celeritas::FastSimulationIntegration;
@@ -130,20 +131,9 @@ class RunAction final : public G4UserRunAction
130131
class ActionInitialization final : public G4VUserActionInitialization
131132
{
132133
public:
133-
void BuildForMaster() const final
134-
{
135-
FastSimulationIntegration::Instance().BuildForMaster();
136-
137-
CELER_LOG_LOCAL(status) << "Constructing user actions";
138-
139-
this->SetUserAction(new RunAction{});
140-
}
134+
void BuildForMaster() const final { this->SetUserAction(new RunAction{}); }
141135
void Build() const final
142136
{
143-
FastSimulationIntegration::Instance().Build();
144-
145-
CELER_LOG_LOCAL(status) << "Constructing user actions";
146-
147137
this->SetUserAction(new PrimaryGeneratorAction{});
148138
this->SetUserAction(new RunAction{});
149139
}

0 commit comments

Comments
 (0)