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

Feature celeritas integration #1370

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ math(EXPR DD4HEP_HIGH_MEM_POOL_DEPTH "${HOST_RAM_MB} / 2000" OUTPUT_FORMAT DECIM

option(DD4HEP_USE_XERCESC "Enable 'Detector Builders' based on XercesC" OFF)
option(DD4HEP_USE_GEANT4 "Enable the simulation part based on Geant4" OFF)
option(DD4HEP_USE_CELERITAS "Enable offloading tracks to Celeritas" OFF)
rahmans1 marked this conversation as resolved.
Show resolved Hide resolved
option(DD4HEP_IGNORE_GEANT4_TLS "Ignore the tls flag Geant4 was compiled with" OFF)
option(DD4HEP_USE_GEAR "Build gear wrapper for backward compatibility" OFF)
option(DD4HEP_USE_LCIO "Build lcio extensions" OFF)
Expand Down Expand Up @@ -154,6 +155,10 @@ if(DD4HEP_USE_GEANT4)
SET_DIRECTORY_PROPERTIES(PROPERTIES INCLUDE_DIRECTORIES "")
endif()

if(DD4HEP_USE_CELERITAS)
find_package(Celeritas REQUIRED)
endif()

if(DD4HEP_USE_LCIO)
find_package(LCIO REQUIRED CONFIG)
DD4HEP_SETUP_LCIO_TARGETS()
Expand Down
31 changes: 31 additions & 0 deletions DDG4/include/DDG4/Celeritas.h
andresailer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef Celeritas_h
#define Celeritas_h 1

#include <accel/SimpleOffload.hh>
#include <G4EmStandardPhysics.hh>

namespace celeritas
{
class LocalTransporter;
struct SetupOptions;
class SharedParams;

class EMPhysicsConstructor final : public G4EmStandardPhysics
{
public:
using G4EmStandardPhysics::G4EmStandardPhysics;

void ConstructProcess() override;
};
}

// Global shared setup options
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does DD4HEP have a namespace? Since these are all local to DD4HEP they should probably live inside it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the definitions under dd4hep::sim namespace

celeritas::SetupOptions& CelerSetupOptions();
// Shared data and GPU setup
celeritas::SharedParams& CelerSharedParams();
// Thread-local transporter
celeritas::LocalTransporter& CelerLocalTransporter();
// Thread-local offload
celeritas::SimpleOffload& CelerSimpleOffload();

#endif
2 changes: 2 additions & 0 deletions DDG4/include/DDG4/Geant4PhysicsList.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ namespace dd4hep {
virtual void enable(G4VUserPhysicsList* physics);
/// Extend physics list from factory:
G4VUserPhysicsList* extensionList();
/// Activate Celeritas tracking offload within EM physics constructor
G4VUserPhysicsList* activateCeleritas();
};

} // End namespace sim
Expand Down
87 changes: 87 additions & 0 deletions DDG4/src/Celeritas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <DDG4/Celeritas.h>

#include <G4Threading.hh>
#include <accel/AlongStepFactory.hh>
#include <celeritas/field/UniformFieldData.hh>
#include <celeritas/io/ImportData.hh>
#include <accel/LocalTransporter.hh>
#include <accel/SetupOptions.hh>
#include <accel/SharedParams.hh>
#include <accel/TrackingManagerOffload.hh>
#include <G4Electron.hh>
#include <G4Positron.hh>
#include <G4Gamma.hh>

#include <memory>

using namespace celeritas;

// Global shared setup options
SetupOptions& CelerSetupOptions()
{
static SetupOptions options = [] {
// Construct setup options the first time CelerSetupOptions is invoked
SetupOptions so;

// Set along-step factory
so.make_along_step = celeritas::UniformAlongStepFactory();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indicates a "zero" magnetic field: is there a way to assert that DD4HEP doesn't have a mag field definition so that it's consistent?

// NOTE: these numbers are appropriate for CPU execution
so.max_num_tracks = 1024;
// This will eventually go
so.max_num_events = 100000;
rahmans1 marked this conversation as resolved.
Show resolved Hide resolved
so.initializer_capacity = 1024 * 128;
// Celeritas does not support EmStandard MSC physics above 100 MeV
so.ignore_processes = {"CoulombScat"};

// Use Celeritas "hit processor" to call back to Geant4 SDs.
so.sd.enabled = false;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely shouldn't have this. Where were these lines copied from? We should probably use something closer to https://gitlab.cern.ch/bmorgan/athena/-/blob/6072d57711365aa3a58c38ff1134e3f0c569c78b/Simulation/G4Utilities/G4UserActions/src/Celeritas.cxx

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// Only call back for nonzero energy depositions: this is currently a
// global option for all detectors, so if any SDs extract data from tracks
// with no local energy deposition over the step, it must be set to false.
so.sd.ignore_zero_deposition = false;

// Using the pre-step point, reconstruct the G4 touchable handle.
so.sd.locate_touchable = true;

// Pre-step time is used
so.sd.pre.global_time = true;
return so;
}();
return options;
}

// Shared data and GPU setup
SharedParams& CelerSharedParams()
{
static SharedParams sp;
return sp;
}

// Thread-local transporter
LocalTransporter& CelerLocalTransporter()
{
static G4ThreadLocal LocalTransporter lt;
return lt;
}

// Thread-local offload interface
SimpleOffload& CelerSimpleOffload()
{
static G4ThreadLocal SimpleOffload so;
return so;
}

void EMPhysicsConstructor::ConstructProcess()
{
CELER_LOG_LOCAL(status) << "Setting up tracking manager offload";
G4EmStandardPhysics::ConstructProcess();

// Add Celeritas tracking manager to electron, positron, gamma.
auto* celer_tracking = new TrackingManagerOffload(
&CelerSharedParams(), &CelerLocalTransporter());

G4Electron::Definition()->SetTrackingManager(celer_tracking);
G4Positron::Definition()->SetTrackingManager(celer_tracking);
G4Gamma::Definition()->SetTrackingManager(celer_tracking);
}
12 changes: 12 additions & 0 deletions DDG4/src/Geant4PhysicsList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <DD4hep/InstanceCount.h>
#include <DD4hep/Printout.h>
#include <DD4hep/Plugins.h>
#include <DDG4/Celeritas.h>

// Geant4 include files
#include <G4VPhysicsConstructor.hh>
Expand Down Expand Up @@ -374,6 +375,17 @@ G4VUserPhysicsList* Geant4PhysicsListActionSequence::extensionList() {
return physics;
}

G4VUserPhysicsList* Geant4PhysicsListActionSequence::activateCeleritas() {
andresailer marked this conversation as resolved.
Show resolved Hide resolved
G4VModularPhysicsList* physics = ( m_extends.empty() )
? new EmptyPhysics()
: G4PhysListFactory().GetReferencePhysList(m_extends);

physics->ReplacePhysics(new celeritas::EMPhysicsConstructor);

return physics;
}


/// Install command control messenger if wanted
void Geant4PhysicsListActionSequence::installCommandMessenger() {
control()->addCall("dump", "Dump content of " + name(), Callback(this).make(&Geant4PhysicsListActionSequence::dump));
Expand Down