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

Add actions and executors for generating optical distribution data #1184

Merged
merged 4 commits into from
Apr 16, 2024

Conversation

amandalund
Copy link
Contributor

This adds a gather action to cache the pre-step data needed for the scintillation and Cerenkov pre-generators and a post-step pre-generator action to create the optical distribution data. The distributions are stored in two large buffers indexed by the current size plus the track slot ID which is compacted at the end of each step by removing all the invalid distributions (this will ensure reproducibility). These actions will be created by the optical collector in #1173.

- Add pre-step gather action to cache state data needed for pre-generators
- Add post-step pre-generator action to create and buffer optical distribution data
@amandalund amandalund added enhancement New feature or request physics Particles, processes, and stepping algorithms labels Apr 12, 2024
Copy link
Contributor

@whokion whokion left a comment

Choose a reason for hiding this comment

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

Good work! A couple pf general comments: 1) It is okay for now, but should actions for cerenkov and scintillation be separated as they may be independently added? 2) Do we need to add any unit test (okay if it is not necessary or difficult to add at this point though), especially for reproducibility, which I could not catch the requirement clearly from the code (so, please comment the place for the reproducibility requirement)? Thanks for the nice work!

src/celeritas/optical/detail/OpticalGenStorage.hh Outdated Show resolved Hide resolved
src/celeritas/optical/detail/PreGenAction.cc Outdated Show resolved Hide resolved
src/celeritas/optical/detail/PreGenAction.hh Show resolved Hide resolved
Copy link
Member

@sethrj sethrj left a comment

Choose a reason for hiding this comment

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

Looking good!

src/celeritas/optical/detail/OpticalGenStorage.hh Outdated Show resolved Hide resolved
src/celeritas/optical/detail/PreGenAction.hh Outdated Show resolved Hide resolved
auto sim = track.make_sim_view();
auto optmat_id
= track.make_material_view().make_material_view().optical_material_id();
if (sim.status() == TrackStatus::inactive || !optmat_id
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the track status will ever be inactive if the Executor is called: the TrackExecutor should filter out inactive tracks. If a track is inactive, then you might have just accessed invalid memory by getting the optical material ID...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't that only true if it's a conditional executor, i.e. constructed with make_active_track_executor()? The memory should still be valid even if the track is inactive. I used a regular TrackExecutor here because we need to clear any old distribution data in the buffers before compacting with remove_if.

Copy link
Member

Choose a reason for hiding this comment

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

Oh ok, you're right about the conditional executor. But it still could access uninitialized memory right? If a track has never been initialized, then:

track.make_material_view().make_material_view().optical_material_id()

could obtainan uninitialized/invalid material ID, and then crash when trying to read the optical material from the corresponding material view.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh ok I see what you mean: we'd be trying to construct a MaterialView with an invalid material ID. You're right, let me fix that...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I ran into a similar thing once with the cross section calculation.

src/celeritas/optical/detail/PreGenAction.hh Show resolved Hide resolved
@amandalund
Copy link
Contributor Author

Thanks @whokion: yes, we could separate the Cerenkov and scintillation actions, but this should also work as is either if both of the processes are enabled or if only one or the other is. I've added a test in #1173 (which I'll open once this is merged) which runs the stepping loop with these actions and checks the buffered data. For reproducibility the order of the distribution data in the buffers should always be the same, which is done by making sure the distributions created at each step are ordered by thread ID (originally I had used a stack allocator to buffer the data, but this would have required some additional post-processing to sort the distributions since the atomic add leads to a non-deterministic order: see #1173 (comment)).

Copy link
Member

@sethrj sethrj left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@sethrj sethrj enabled auto-merge (squash) April 16, 2024 20:45
@sethrj sethrj merged commit 3144f44 into celeritas-project:develop Apr 16, 2024
25 of 26 checks passed
@amandalund amandalund deleted the pre-gen-actions branch April 16, 2024 22:11
@amandalund amandalund mentioned this pull request Apr 20, 2024
30 tasks
Comment on lines +127 to +129
detail::PreGenExecutor{properties_->host_ref(),
cerenkov_->host_ref(),
scintillation_->host_ref(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually this definitely won't work as is if only one of the processes is enabled... do you think it would be cleaner to just split this into two actions as @whokion suggested or try to fix it to work as a single action (@sethrj)?

Copy link
Member

Choose a reason for hiding this comment

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

Two actions would probably be cleaner, but if you think it's less effort to fix the single action let's go for that.

Comment on lines +62 to +65
auto optmat_id = inactive ? OpticalMaterialId{}
: track.make_material_view()
.make_material_view()
.optical_material_id();
Copy link
Member

Choose a reason for hiding this comment

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

@amandalund I know this has been moved around (moved to Utils.hh in #1217) but wanted to revisit this on the original implementation... Since the PreGenExecutor is a "post-post-step" action, the material here is the post-boundary-crossing material if a track is stopped by a boundary. I don't think that's right... perhaps we should be collecting and storing the beginning-of-step optical material in OpticalPreStepData?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good catch, yeah we can store it with the rest of the pre-step data.

Copy link
Member

Choose a reason for hiding this comment

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

@amandalund to be clear (and avoid unintentionally duplicating fixes as we've managed to do far too often 😅) would you like to address this or shall I?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to, if you haven't already done it 😉

Copy link
Member

Choose a reason for hiding this comment

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

I haven't! I'd be grateful if you'd take handle it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request physics Particles, processes, and stepping algorithms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants