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

Torch test #77

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ cmake -S esi-g4ox -B build
cmake --build build
```

To run a test that generates a NumPy file (`out/photons.npy`) with the simulated photons, execute:

```bash
./build/src/simtox
```

Similarly, to start a Geant4-based simulation of optical photons, run:

```bash
Expand All @@ -32,6 +26,15 @@ Before runnning `simg4ox` it is recommended to set the following environment var
export TMP=/tmp/myname
export GEOM=mygeom
export OPTICKS_EVENT_MODE=DebugLite
QCurandState_SPEC=3:0:0 /usr/local/opticks/lib/QCurandStateTest
opticks_running_mode=SRM_TORCH
export storch_FillGenstep_pos=0,-270,0
export storch_FillGenstep_type=rectangle
export storch_FillGenstep_zenith=-20,20
export storch_FillGenstep_azimuth=-20,20
export OPTICKS_RUNNING_MODE=${OPTICKS_RUNNING_MODE:-$opticks_running_mode}
export OPTICKS_NUM_PHOTON=100
unset export SGenerate__GeneratePhotons_RNG_PRECOOKED=NO
```


Expand Down
153 changes: 8 additions & 145 deletions src/g4app.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
#include "G4SDManager.hh"
#include "SysRap/NP.hh"
#include "SysRap/SEvt.hh"
#include "SysRap/SGenerate.h"
#include "SysRap/STrackInfo.h"
#include "SysRap/spho.h"
#include "SysRap/sphoton.h"
#include "U4/U4Random.hh"
#include "U4/U4StepPoint.hh"
#include "U4/U4Touchable.h"
#include "U4/U4Track.h"
#include "U4/U4VPrimaryGenerator.h"

bool IsSubtractionSolid(G4VSolid *solid)
{
Expand Down Expand Up @@ -280,37 +282,12 @@ struct PrimaryGenerator : G4VUserPrimaryGeneratorAction

void GeneratePrimaries(G4Event *event) override
{
NP *photons = NP::Make<float>(0, 4, 4);

photons->load("out/photons.npy");

size_t n_photons = photons->num_items();
sphoton *sphotons = reinterpret_cast<sphoton *>(photons->bytes());

for (int i = 0; i < n_photons; i++)
{
sphoton &p = sphotons[i];

G4ThreeVector position_mm(p.pos.x, p.pos.y, p.pos.z);
G4double time_ns = p.time;
G4ThreeVector direction(p.mom.x, p.mom.y, p.mom.z);
// direction = direction.unit();
G4double wavelength_nm = p.wavelength;
G4ThreeVector polarization(p.pol.x, p.pol.y, p.pol.z);

G4PrimaryVertex *vertex = new G4PrimaryVertex(position_mm, time_ns);
G4double kineticEnergy = h_Planck * c_light / (wavelength_nm * nm);

G4PrimaryParticle *particle = new G4PrimaryParticle(G4OpticalPhoton::Definition());
particle->SetKineticEnergy(kineticEnergy);
particle->SetMomentumDirection(direction);
particle->SetPolarization(polarization);

vertex->SetPrimary(particle);
event->AddPrimaryVertex(vertex);
}

sev->SetInputPhoton(photons);
int idx_arg = event->GetEventID();
NP *gs = SEvent::MakeTorchGenstep(idx_arg);
NP *ph = SGenerate::GeneratePhotons(gs);
U4VPrimaryGenerator::GeneratePrimaries_From_Photons(event, ph);
delete ph;
SEvent::SetGENSTEP(gs);
}
};

Expand All @@ -330,13 +307,9 @@ struct EventAction : G4UserEventAction
void EndOfEventAction(const G4Event *event) override
{
int eventID = event->GetEventID();
sev->addEventConfigArray();
sev->gather();
sev->endOfEvent(eventID);

// GPU-based simulation
G4CXOpticks *gx = G4CXOpticks::Get();

gx->simulate(eventID, false);
cudaDeviceSynchronize();
unsigned int num_hits = SEvt::GetNumHit(0);
Expand All @@ -363,17 +336,6 @@ struct EventAction : G4UserEventAction
}
};

void get_label(spho &ulabel, const G4Track *track)
{
spho *label = STrackInfo<spho>::GetRef(track);
assert(label && label->isDefined() && "all photons are expected to be labelled");

std::array<int, spho::N> a_label;
label->serialize(a_label);

ulabel.load(a_label);
}

struct SteppingAction : G4UserSteppingAction
{
SEvt *sev;
Expand All @@ -387,47 +349,6 @@ struct SteppingAction : G4UserSteppingAction
// std::cout<< step->GetPreStepPoint()->GetPhysicalVolume()->GetName() << std::endl;
if (step->GetTrack()->GetDefinition() != G4OpticalPhoton::OpticalPhotonDefinition())
return;

const G4VProcess *process = step->GetPreStepPoint()->GetProcessDefinedStep();

if (process == nullptr)
return;

const G4Track *track = step->GetTrack();
G4VPhysicalVolume *pv = track->GetVolume();
const G4VTouchable *touch = track->GetTouchable();

spho ulabel = {};
get_label(ulabel, track);

const G4StepPoint *pre = step->GetPreStepPoint();
const G4StepPoint *post = step->GetPostStepPoint();

G4ThreeVector delta = step->GetDeltaPosition();
double step_mm = delta.mag() / mm;

sev->checkPhotonLineage(ulabel);

sphoton &current_photon = sev->current_ctx.p;

if (current_photon.flagmask_count() == 1)
{
U4StepPoint::Update(current_photon, pre); // populate current_photon with pos, mom, pol, time, wavelength
sev->pointPhoton(ulabel); // copying current into buffers
}

bool tir;
unsigned flag = U4StepPoint::Flag<G4OpBoundaryProcess>(post, true, tir);
bool is_detect_flag = OpticksPhoton::IsSurfaceDetectFlag(flag);

current_photon.iindex =
is_detect_flag ? U4Touchable::ImmediateReplicaNumber(touch) : U4Touchable::AncestorReplicaNumber(touch);

U4StepPoint::Update(current_photon, post);

current_photon.set_flag(flag);

sev->pointPhoton(ulabel);
}
};

Expand All @@ -442,72 +363,14 @@ struct TrackingAction : G4UserTrackingAction

void PreUserTrackingAction_Optical_FabricateLabel(const G4Track *track)
{
U4Track::SetFabricatedLabel<spho>(track);
spho *label = STrackInfo<spho>::GetRef(track);
assert(label);
}

void PreUserTrackingAction(const G4Track *track) override
{
spho *label = STrackInfo<spho>::GetRef(track);

if (label == nullptr)
{
PreUserTrackingAction_Optical_FabricateLabel(track);
label = STrackInfo<spho>::GetRef(track);
}

assert(label && label->isDefined());

std::array<int, spho::N> a_label;
label->serialize(a_label);

spho ulabel = {};
ulabel.load(a_label);

U4Random::SetSequenceIndex(ulabel.id);

bool resume_fSuspend = track == transient_fSuspend_track;

if (ulabel.gen() == 0)
{
if (resume_fSuspend == false)
sev->beginPhoton(ulabel);
else
sev->resumePhoton(ulabel);
}
else if (ulabel.gen() > 0)
{
if (resume_fSuspend == false)
sev->rjoinPhoton(ulabel);
else
sev->rjoin_resumePhoton(ulabel);
}
}

void PostUserTrackingAction(const G4Track *track) override
{
G4TrackStatus tstat = track->GetTrackStatus();

bool is_fStopAndKill = tstat == fStopAndKill;
bool is_fSuspend = tstat == fSuspend;
bool is_fStopAndKill_or_fSuspend = is_fStopAndKill || is_fSuspend;

assert(is_fStopAndKill_or_fSuspend);

spho ulabel = {};
get_label(ulabel, track);

if (is_fStopAndKill)
{
U4Random::SetSequenceIndex(-1);
sev->finalPhoton(ulabel);
transient_fSuspend_track = nullptr;
}
else if (is_fSuspend)
{
transient_fSuspend_track = track;
}
}
};

Expand Down
Loading