Skip to content

Commit

Permalink
propagate registry object to material creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu committed Oct 24, 2024
1 parent 51308dc commit 7000cfc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
7 changes: 5 additions & 2 deletions src/legendhpges/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pint import Quantity
from pyg4ometry import geant4

from .materials import natural_germanium
from .materials import make_natural_germanium
from .registry import default_g4_registry
from .registry import default_units_registry as u

Expand All @@ -36,7 +36,7 @@ def __init__(
metadata: str | dict | AttrsDict,
name: str | None = None,
registry: geant4.Registry = default_g4_registry,
material: geant4.MaterialCompound = natural_germanium,
material: geant4.MaterialCompound = None,
) -> None:
if registry is None:
msg = "registry cannot be None"
Expand All @@ -46,6 +46,9 @@ def __init__(
msg = "metadata cannot be None"
raise ValueError(msg)

if material is None:
material = make_natural_germanium(registry)

# build crystal, declare as detector
if not isinstance(metadata, (dict, AttrsDict)):
with Path(metadata).open() as jfile:
Expand Down
2 changes: 1 addition & 1 deletion src/legendhpges/make_hpge.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def make_hpge(
enrichment = gedet_meta.production.enrichment
else:
enrichment = gedet_meta.production.enrichment.val
kwargs["material"] = make_enriched_germanium(enrichment)
kwargs["material"] = make_enriched_germanium(enrichment, registry)

if name is None:
if gedet_meta.name is None:
Expand Down
33 changes: 15 additions & 18 deletions src/legendhpges/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,22 @@ def _number_density_meas() -> Quantity:
return n_avogadro * natge_density_meas / a_eff


def _make_natural_germanium() -> g4.MaterialCompound:
def make_natural_germanium(
registry: g4.Registry = default_g4_registry,
) -> g4.MaterialCompound:
"""Natural germanium material builder."""
enrge_name = "NaturalGermanium"

if enrge_name not in default_g4_registry.materialDict:
if enrge_name not in registry.materialDict:
enrge = g4.ElementIsotopeMixture(enrge_name, "NatGe", len(natge_isotopes))

for iso, frac in natge_isotopes.items():
enrge.add_isotope(iso, frac)

matenrge = g4.MaterialCompound(
enrge_name, natge_density_meas.m, 1, default_g4_registry
)
matenrge = g4.MaterialCompound(enrge_name, natge_density_meas.m, 1, registry)
matenrge.add_element_massfraction(enrge, 1)
else:
matenrge = default_g4_registry.materialDict[enrge_name]
return matenrge

return registry.materialDict[enrge_name]


def enriched_germanium_density(ge76_fraction: float = 0.92) -> Quantity:
Expand All @@ -90,7 +89,10 @@ def enriched_germanium_density(ge76_fraction: float = 0.92) -> Quantity:
return (_number_density_meas() * m_eff / n_avogadro).to("g/cm^3")


def make_enriched_germanium(ge76_fraction: float = 0.92) -> g4.Material:
def make_enriched_germanium(
ge76_fraction: float = 0.92,
registry: g4.Registry = default_g4_registry,
) -> g4.Material:
"""Enriched germanium material builder.
Note
Expand All @@ -104,22 +106,17 @@ def make_enriched_germanium(ge76_fraction: float = 0.92) -> g4.Material:
"""
enrge_name = f"EnrichedGermanium{ge76_fraction:.3f}"

if enrge_name not in default_g4_registry.materialDict:
enrge = g4.ElementIsotopeMixture(enrge_name, "EnrGe", 2)
if enrge_name not in registry.materialDict:
enrge = g4.ElementIsotopeMixture(f"Element{enrge_name}", "EnrGe", 2, registry)
enrge.add_isotope(ge74, 1 - ge76_fraction)
enrge.add_isotope(ge76, ge76_fraction)

matenrge = g4.MaterialCompound(
enrge_name,
enriched_germanium_density(ge76_fraction).to("g/cm^3").m,
1,
default_g4_registry,
registry,
)
matenrge.add_element_massfraction(enrge, 1)
else:
matenrge = default_g4_registry.materialDict[enrge_name]
return matenrge


natural_germanium: g4.MaterialCompound = _make_natural_germanium()
enriched_germanium: g4.MaterialCompound = make_enriched_germanium()
return registry.materialDict[enrge_name]
3 changes: 2 additions & 1 deletion tests/test_det_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
SemiCoax,
make_hpge,
)
from legendhpges.materials import natural_germanium
from legendhpges.materials import make_natural_germanium

reg = geant4.Registry()
natural_germanium = make_natural_germanium(reg)
configs = TextDB(pathlib.Path(__file__).parent.resolve() / "configs")


Expand Down
2 changes: 1 addition & 1 deletion tests/test_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def test_g4_materials():
== materials.enriched_germanium_density(0.92).to("g/cm^3").m
)
assert (
materials.natural_germanium.density
materials.make_natural_germanium().density
== materials.natge_density_meas.to("g/cm^3").m
)

0 comments on commit 7000cfc

Please sign in to comment.