From 6d0ff45d6007dbfbb71421bbb97f1b1680690ea8 Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Mon, 9 Sep 2024 20:08:10 +0200 Subject: [PATCH 1/2] Restore G4 11.0 support with units `nm` and `um` are only supported starting G4 11.1.0 --- src/legendoptics/pyg4utils.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/legendoptics/pyg4utils.py b/src/legendoptics/pyg4utils.py index 6f9dd97..e9feb94 100644 --- a/src/legendoptics/pyg4utils.py +++ b/src/legendoptics/pyg4utils.py @@ -4,6 +4,7 @@ import numpy as np import pint +import pyg4ometry.gdml.Units as gdml_u import pyg4ometry.geant4 as g4 from pint import Quantity @@ -70,9 +71,19 @@ def pyg4_def_scint_by_particle_type(mat, scint_cfg: ScintConfig) -> None: ) +def _gdml_unit(u: str) -> str: + # Only as of Geant4 11.1.0, `um` and `nm` are supported. + u = u.replace("µ", "u") + if u == "nm": + u = "nanometer" + if u == "um": + u = "micrometer" + return u + + @pint.register_unit_format("gdml") def _gdml_format(unit, registry, **options): - proc = {u.replace("µ", "u"): e for u, e in unit.items()} + proc = {_gdml_unit(u): e for u, e in unit.items()} return pint.formatting.formatter( proc.items(), as_ratio=True, @@ -100,7 +111,7 @@ def _val_pint_to_gdml(v): base_unit = v.units unit = f"{base_unit:~gdml}" - assert unit == f"{base_unit:~}".replace(" ", "").replace("µ", "u") + # assert unit == f"{base_unit:~}".replace(" ", "").replace("µ", "u") assert "dimensionless" not in unit msg = f"Unit pint->gdml: {unit} - {base_unit}" @@ -110,13 +121,17 @@ def _val_pint_to_gdml(v): return unit, v # Only as of Geant4 11.1.0, `um` and `nm` are supported. - length_u = ["km", "m", "cm", "mm", "um", "nm"] + length_u = ["km", "m", "cm", "mm", "nanometer", "micrometer"] dimless_props = [ "RINDEX", "WLSCOMPONENT", "REFLECTIVITY", "REALRINDEX", "IMAGINARYRINDEX", + "SPECULARLOBECONSTANT", + "SPECULARSPIKECONSTANT", + "BACKSCATTERCONSTANT", + "EFFICIENCY", ] length_props = ["ABSLENGTH", "WLSABSLENGTH", "RAYLEIGH"] @@ -157,6 +172,12 @@ def addConstPropertyPint(self, name, value): g4.Material.addConstPropertyPint = addConstPropertyPint g4.solid.OpticalSurface.addConstPropertyPint = addConstPropertyPint + # those units are supported by G4, but not by pyg4ometry... + if "nanometer" not in gdml_u.units: + gdml_u.units["nanometer"] = gdml_u.units["nm"] + if "micrometer" not in gdml_u.units: + gdml_u.units["micrometer"] = gdml_u.units["um"] + _patch_g4_pint_unit_support() From aa86c76561738c66f1403ed26f5ad26b897e1f1b Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Mon, 9 Sep 2024 20:29:07 +0200 Subject: [PATCH 2/2] Make docs build happe --- src/legendoptics/pyg4utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/legendoptics/pyg4utils.py b/src/legendoptics/pyg4utils.py index e9feb94..6181bc6 100644 --- a/src/legendoptics/pyg4utils.py +++ b/src/legendoptics/pyg4utils.py @@ -173,10 +173,12 @@ def addConstPropertyPint(self, name, value): g4.solid.OpticalSurface.addConstPropertyPint = addConstPropertyPint # those units are supported by G4, but not by pyg4ometry... - if "nanometer" not in gdml_u.units: - gdml_u.units["nanometer"] = gdml_u.units["nm"] - if "micrometer" not in gdml_u.units: - gdml_u.units["micrometer"] = gdml_u.units["um"] + # the first check is ugly, but necessary: we have a mock import of gdml_u for the docs build. + if isinstance(gdml_u.units, dict): + if "nanometer" not in gdml_u.units: + gdml_u.units["nanometer"] = gdml_u.units["nm"] + if "micrometer" not in gdml_u.units: + gdml_u.units["micrometer"] = gdml_u.units["um"] _patch_g4_pint_unit_support()