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

Fluka viewer #46

Merged
merged 6 commits into from
Jul 11, 2023
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
15 changes: 14 additions & 1 deletion docs/source/manual/viewing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
24 changes: 24 additions & 0 deletions src/pyg4ometry/fluka/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ 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)
Expand Down Expand Up @@ -232,6 +236,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)
Expand Down
24 changes: 23 additions & 1 deletion src/pyg4ometry/visualisation/ViewerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/fluka/T902_cube_from_six_PLAs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
return {
"testStatus": True,
"logicalVolume": greg.getWorldVolume(),
"vtkViewer": v,
"flukaRegistry": freg,
}


if __name__ == "__main__":
Expand Down
8 changes: 8 additions & 0 deletions tests/fluka/test_Fluka.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -760,3 +761,10 @@ 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()