From cc250894584bdc07a32988c1bd79d174652c92b7 Mon Sep 17 00:00:00 2001 From: Stewart Boogert Date: Thu, 6 Jul 2023 17:47:17 +0100 Subject: [PATCH 1/6] Handy to have a registry to test --- tests/fluka/T902_cube_from_six_PLAs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fluka/T902_cube_from_six_PLAs.py b/tests/fluka/T902_cube_from_six_PLAs.py index 95961b076..8ca71e01a 100644 --- a/tests/fluka/T902_cube_from_six_PLAs.py +++ b/tests/fluka/T902_cube_from_six_PLAs.py @@ -43,7 +43,7 @@ def Test(vis=False, interactive=False): v.addLogicalVolume(wlv) v.view(interactive=interactive) - return {"testStatus": True, "logicalVolume": greg.getWorldVolume(), "vtkViewer": v} + return {"testStatus": True, "logicalVolume": greg.getWorldVolume(), "vtkViewer": v, "flukaRegistry":freg} if __name__ == "__main__": From 644d2659a07bbf11540f9b2fa61e7841324b75dd Mon Sep 17 00:00:00 2001 From: Stewart Boogert Date: Thu, 6 Jul 2023 17:48:30 +0100 Subject: [PATCH 2/6] Properly formed fluka should probably always have an intersection. Although the conversion G4->FLUKA can have -pla which of course is +(~pla) Avoid the problem but Need to have a different set of logic to fix long term --- src/pyg4ometry/fluka/region.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/pyg4ometry/fluka/region.py b/src/pyg4ometry/fluka/region.py index 01e6bc02d..08e50a413 100644 --- a/src/pyg4ometry/fluka/region.py +++ b/src/pyg4ometry/fluka/region.py @@ -131,6 +131,11 @@ def _getSolidFromBoolean(boolean, g4reg, aabb): return boolean.body.geant4Solid(g4reg, aabb=aabb) def mesh(self, aabb=None): + + if len(self.intersections) == 0: + print(self.dumpsDebug()) + return None + result = self.intersections[0].body.mesh(aabb=aabb) for boolean in self.intersections[1:] + self.subtractions: mesh = boolean.body.mesh(aabb=aabb) @@ -232,6 +237,26 @@ def dumps(self): return fs + def dumpsDebug(self): + """Returns a string of this Zone instance in the equivalent + FLUKA syntax with extra debug information""" + fs = "" + + booleans = self.intersections + self.subtractions + for s in booleans: + if isinstance(s, Intersection): + if isinstance(s.body, Zone): + fs += f" +({s.body.dumps()})" + else: + fs += f" +{s.body.name} ({type(s.body)})" + elif isinstance(s, Subtraction): + if isinstance(s.body, Zone): + fs += f" -({s.body.dumps()})" + else: + fs += f" -{s.body.name} ({type(s.body)})" + + return fs + def withLengthSafety(self, bigger_flukareg, smaller_flukareg, shrink_intersections): zone_out = Zone(name=self.name) logger.debug("zone.name = %s", self.name) From b3f2dba2f7f14f6cd834fbfa22afe6bf0b016f29 Mon Sep 17 00:00:00 2001 From: Stewart Boogert Date: Thu, 6 Jul 2023 17:48:47 +0100 Subject: [PATCH 3/6] Add visualisation of FLUKA to pyg4ometry --- src/pyg4ometry/visualisation/ViewerBase.py | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/pyg4ometry/visualisation/ViewerBase.py b/src/pyg4ometry/visualisation/ViewerBase.py index 345b9cd52..9441fb7a6 100644 --- a/src/pyg4ometry/visualisation/ViewerBase.py +++ b/src/pyg4ometry/visualisation/ViewerBase.py @@ -191,6 +191,28 @@ def addLogicalVolume( self.addInstance(pv_name, new_mtra, new_tra, pv_name) self.addVisOptions(pv_name, pv.visOptions) + + def addFlukaRegions(self, fluka_registry, max_region=1000000, debugIO = False): + icount = 0 + for k in fluka_registry.regionDict: + if debugIO : + print("ViewerBase.addFlukaRegions>", k) + m = fluka_registry.regionDict[k].mesh() + + if m is not None: + self.addMesh(k,m) + self.addInstance(k, + _np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), + _np.array([0, 0, 0]), + k+"_instance") + self.addVisOptions(k, _VisOptions()) + + icount += 1 + + if icount > max_region : + break + + def addMesh(self, name, mesh): """ Add a single mesh @@ -213,7 +235,7 @@ def addInstance(self, name, transformation, translation, instanceName=""): :param name: name of mesh to add instance :type name: str :param transformation: Transformation matrix for instance - :type transformation: matrix(3,3) + :type transformation: array(3,3) :param translation: Translation for instance :type translation: array(3) :param instanceName: Name of the instance e.g PV From 076ad4a595b496bf463748ae9ccfcaae4dcf7df1 Mon Sep 17 00:00:00 2001 From: Stewart Boogert Date: Thu, 6 Jul 2023 18:24:38 +0100 Subject: [PATCH 4/6] Added test of fluka vis --- tests/fluka/test_Fluka.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/fluka/test_Fluka.py b/tests/fluka/test_Fluka.py index f2354f4d2..58701ce96 100644 --- a/tests/fluka/test_Fluka.py +++ b/tests/fluka/test_Fluka.py @@ -2,6 +2,7 @@ import numpy as np import pytest +import pyg4ometry.visualisation.VtkViewerNew as _VtkViewerNew from pyg4ometry.fluka.fluka_registry import RotoTranslationStore, FlukaRegistry from pyg4ometry.fluka.directive import rotoTranslationFromTra2 @@ -760,3 +761,9 @@ def test_addRotoTranslation(): # TODO check # with pytest.raises(KeyError): # store.addRotoTranslation(rtrans5) + +def test_fluka_vis(): + r = T902_cube_from_six_PLAs.Test(False,False)["flukaRegistry"] + v = _VtkViewerNew() + v.addFlukaRegions(r) + v.buildPipelinesAppend() From 1fbf402649e4c8811fa44d996f569132dc2871b8 Mon Sep 17 00:00:00 2001 From: Stewart Boogert Date: Thu, 6 Jul 2023 19:27:15 +0100 Subject: [PATCH 5/6] Short documentation fragment to explain how to view FLUKA geometry --- docs/source/manual/viewing.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/source/manual/viewing.rst b/docs/source/manual/viewing.rst index 310a31968..853839858 100644 --- a/docs/source/manual/viewing.rst +++ b/docs/source/manual/viewing.rst @@ -232,7 +232,7 @@ which will result in every volume being visualised with a random colour to be di .. code-block:: - v = pyg4ometry.visualisation.VtkViewerColourd(defaultColour="random") + v = pyg4ometry.visualisation.VtkViewerColoured(defaultColour="random") Overlaying Two Geometries @@ -248,3 +248,16 @@ Logical Volume Difference The function :code:`pyg4ometry.visualisation.viewLogicalVolumeDifference` is provided that will view two :code:`pyg4ometry.geant4.LogicalVolume` instances. It will also calculate the difference mesh between the two and visualise that also on top of the two with a different colour to highlight it. + +Viewing FLUKA geometry +---------------------- + +The viewer can be used to view FLUKA geometry. + +.. code-block:: + + r = pyg4ometry.fluka.Reader("./FLUKA_FILE.inp") + v = pyg4ometry.visualisation.VtkViewerNew() + v.addFlukaRegions(r.getRegistry()) + v.buildPipelinesAppend() + v.view() \ No newline at end of file From 235bf027c61375b2ec22007b963ce540ed39aab0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Jul 2023 18:32:42 +0000 Subject: [PATCH 6/6] style: pre-commit fixes --- docs/source/manual/viewing.rst | 2 +- src/pyg4ometry/fluka/region.py | 1 - src/pyg4ometry/visualisation/ViewerBase.py | 20 ++++++++++---------- tests/fluka/T902_cube_from_six_PLAs.py | 7 ++++++- tests/fluka/test_Fluka.py | 3 ++- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/docs/source/manual/viewing.rst b/docs/source/manual/viewing.rst index 853839858..ab2c4098f 100644 --- a/docs/source/manual/viewing.rst +++ b/docs/source/manual/viewing.rst @@ -260,4 +260,4 @@ The viewer can be used to view FLUKA geometry. v = pyg4ometry.visualisation.VtkViewerNew() v.addFlukaRegions(r.getRegistry()) v.buildPipelinesAppend() - v.view() \ No newline at end of file + v.view() diff --git a/src/pyg4ometry/fluka/region.py b/src/pyg4ometry/fluka/region.py index 08e50a413..a678ddf01 100644 --- a/src/pyg4ometry/fluka/region.py +++ b/src/pyg4ometry/fluka/region.py @@ -131,7 +131,6 @@ def _getSolidFromBoolean(boolean, g4reg, aabb): return boolean.body.geant4Solid(g4reg, aabb=aabb) def mesh(self, aabb=None): - if len(self.intersections) == 0: print(self.dumpsDebug()) return None diff --git a/src/pyg4ometry/visualisation/ViewerBase.py b/src/pyg4ometry/visualisation/ViewerBase.py index 9441fb7a6..524fc37b2 100644 --- a/src/pyg4ometry/visualisation/ViewerBase.py +++ b/src/pyg4ometry/visualisation/ViewerBase.py @@ -191,28 +191,28 @@ def addLogicalVolume( self.addInstance(pv_name, new_mtra, new_tra, pv_name) self.addVisOptions(pv_name, pv.visOptions) - - def addFlukaRegions(self, fluka_registry, max_region=1000000, debugIO = False): + def addFlukaRegions(self, fluka_registry, max_region=1000000, debugIO=False): icount = 0 for k in fluka_registry.regionDict: - if debugIO : + if debugIO: print("ViewerBase.addFlukaRegions>", k) m = fluka_registry.regionDict[k].mesh() if m is not None: - self.addMesh(k,m) - self.addInstance(k, - _np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), - _np.array([0, 0, 0]), - k+"_instance") + self.addMesh(k, m) + self.addInstance( + k, + _np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), + _np.array([0, 0, 0]), + k + "_instance", + ) self.addVisOptions(k, _VisOptions()) icount += 1 - if icount > max_region : + if icount > max_region: break - def addMesh(self, name, mesh): """ Add a single mesh diff --git a/tests/fluka/T902_cube_from_six_PLAs.py b/tests/fluka/T902_cube_from_six_PLAs.py index 8ca71e01a..955ed5220 100644 --- a/tests/fluka/T902_cube_from_six_PLAs.py +++ b/tests/fluka/T902_cube_from_six_PLAs.py @@ -43,7 +43,12 @@ def Test(vis=False, interactive=False): v.addLogicalVolume(wlv) v.view(interactive=interactive) - return {"testStatus": True, "logicalVolume": greg.getWorldVolume(), "vtkViewer": v, "flukaRegistry":freg} + return { + "testStatus": True, + "logicalVolume": greg.getWorldVolume(), + "vtkViewer": v, + "flukaRegistry": freg, + } if __name__ == "__main__": diff --git a/tests/fluka/test_Fluka.py b/tests/fluka/test_Fluka.py index 58701ce96..f6b2822e8 100644 --- a/tests/fluka/test_Fluka.py +++ b/tests/fluka/test_Fluka.py @@ -762,8 +762,9 @@ def test_addRotoTranslation(): # with pytest.raises(KeyError): # store.addRotoTranslation(rtrans5) + def test_fluka_vis(): - r = T902_cube_from_six_PLAs.Test(False,False)["flukaRegistry"] + r = T902_cube_from_six_PLAs.Test(False, False)["flukaRegistry"] v = _VtkViewerNew() v.addFlukaRegions(r) v.buildPipelinesAppend()