Skip to content
Merged
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ After cloning the celerpy repository and installing poetry, run `make setup` to:
- install all project and development dependencies from `poetry.lock`, and
- install pre-commit hooks.

After running `make setup`, run `make install` to install celerpy into the Poetry environment.

## Development and testing

Activating the poetry environment will load the python version and all development dependencies. It is faster than manually invoking `poetry run` or using the included makefile.
Expand All @@ -44,3 +46,36 @@ Style and linting should be performed automatically at every `git commit` after
## Release/versioning

Not yet implemented:

## Visualizing a geometry

One of the current uses of `celerpy` is to launch the [`celer-geo`](https://celeritas-project.github.io/celeritas/user/usage/execution/utilities.html) visualization application from a Celeritas installation, send it geometry inputs, and render the resulting image in Python. The following example shows how to create an image from a GDML file:

```python
from pathlib import Path
import matplotlib.pyplot as plt

from celerpy import model, visualize
from celerpy.settings import settings

# Point to a Celeritas installation prefix containing bin/celer-geo
settings.prefix_path = Path("path/to/celeritas/install")

# Input geometry
geometry = Path("path/to/geometry.gdml")

# Set the image coordinates, direction along the rendered x-axis, and pixels
# used for ray tracing
image = model.ImageInput(
lower_left=[0, -100, -100],
upper_right=[0, 100, 100],
rightward=[0.0, 0.0, 1.0],
vertical_pixels=1024
)

# Start celer-geo, trace the geometry, and plot the result
with visualize.CelerGeo.from_filename(geometry) as celer_geo:
trace = visualize.Imager(celer_geo, image)
fig, ax = plt.subplots()
trace(ax)
plt.show()
6 changes: 6 additions & 0 deletions celerpy/model/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class TraceInput(TraceSetup):
image: ImageInput | None = None
"Reuse the existing image"

def model_dump_json(self, **kwargs):
"""Override to ensure _cmd is always set to trace."""
result = self.model_dump(**kwargs)
result["_cmd"] = "trace"
return to_json(result).decode()


# celer-geo/celer-geo.cc
class OrangeStats(_Model):
Expand Down
5 changes: 4 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ dependencies: poetry.lock
test-dependencies: poetry.lock
poetry install --without=dev --with=test --no-root

.PHONY: setup
.PHONY: pre-commit
pre-commit: dependencies
$(RUN) pre-commit install

.PHONY: setup
setup: pre-commit

.PHONY: install
install:
poetry install
Expand Down
6 changes: 3 additions & 3 deletions test/mock-prefix/bin/celer-geo
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ dump(cmd)

log("entering loop")
expect_trace(
'{"geometry":"orange","memspace":null,"volumes":true,"image":{"lower_left":[0,0,0],"upper_right":[1,1,0],"rightward":[1,0,0],"vertical_pixels":4,"horizontal_divisor":null}}',
'{"geometry":"orange","memspace":null,"volumes":true,"image":{"lower_left":[0,0,0],"upper_right":[1,1,0],"rightward":[1,0,0],"vertical_pixels":4,"horizontal_divisor":null},"_cmd":"trace"}',
'{"image":{"_units":"cgs","dims":[4,4],"down":[0.0,-1.0,0.0],"origin":[0.0,1.0,0.0],"pixel_width":0.25,"right":[1.0,0.0,0.0]},"sizeof_int":4,"trace":{"geometry":"orange","memspace":"host","volumes":true},"volumes":["[EXTERIOR]","inner","world"]}\n',
)
expect_trace(
'{"geometry": "orange", "memspace": null, "volumes": false, "image": null}',
'{"geometry":"orange","memspace":null,"volumes":false,"image":null,"_cmd":"trace"}',
'{"image":{"_units":"cgs","dims":[4,4],"down":[0.0,-1.0,0.0],"origin":[0.0,1.0,0.0],"pixel_width":0.25,"right":[1.0,0.0,0.0]},"sizeof_int":4,"trace":{"geometry":"orange","memspace":"host","volumes":false}}\n',
)
expect_trace(
'{"geometry":"geant4","memspace":null,"volumes":true,"image":null}',
'{"geometry":"geant4","memspace":null,"volumes":true,"image":null,"_cmd":"trace"}',
'{"image":{"_units":"cgs","dims":[4,4],"down":[0.0,-1.0,0.0],"origin":[0.0,1.0,0.0],"pixel_width":0.25,"right":[1.0,0.0,0.0]},"sizeof_int":4,"trace":{"geometry":"geant4","memspace":"host","volumes":true},"volumes":["inner","world"]}\n',
)

Expand Down