Skip to content

Commit

Permalink
Merge pull request #36 from stewartboogert/tests
Browse files Browse the repository at this point in the history
Move old tests
  • Loading branch information
stewartboogert authored Jul 4, 2023
2 parents e3f8e29 + 79dd648 commit 53585c4
Show file tree
Hide file tree
Showing 322 changed files with 25,672 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ repos:
- id: end-of-file-fixer
- id: forbid-new-submodules
- id: mixed-line-ending
- id: name-tests-test
args: ["--pytest-test-first"]
- id: requirements-txt-fixer
- id: trailing-whitespace

Expand Down
4 changes: 2 additions & 2 deletions src/pyg4ometry/fluka/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def __repr__(self):
)

def _withLengthSafety(self, safety, reg):
lower = self.lower - [safety, safety, safety]
upper = [*self.upper, safety, safety, safety]
lower = self.lower - Three(safety, safety, safety)
upper = self.upper - Three(safety, safety, safety)
return RPP(
self.name,
lower.x,
Expand Down
2 changes: 1 addition & 1 deletion src/pyg4ometry/fluka/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def visit_UnaryOp(self, node):
operand = self.visit(node.operand)
return op(operand)

def visit_Num(self, node):
def visit_Constant(self, node):
return node.n

def visit_Name(self, node):
Expand Down
31 changes: 28 additions & 3 deletions src/pyg4ometry/geant4/Registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def solidName(var):
return var


def removeprefix(string: str, prefix: str, /) -> str:
if string.startswith(prefix):
return string[len(prefix) :]
else:
return string[:]


class Registry:
"""
Object to store geometry for input and output.
Expand Down Expand Up @@ -713,7 +720,7 @@ def addAndCollapseAssemblyVolumeRecursive(
# find the transformations for this assembly in the reference frame of the mother
# start with identity transformations and then aggregate the placement
# info of the assemblies
mtra = _np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
mtra = _np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
tra = _np.array([0, 0, 0])
for pos, rot, sca in zip(positions, rotations, scales):
assembly_mrot = _np.linalg.inv(_transformation.tbxyz2matrix(rot.eval()))
Expand Down Expand Up @@ -786,17 +793,35 @@ def addAndCollapseAssemblyVolumeRecursive(
new_pos = new_tra.tolist()

# update the position and rotation information
try:
pos_name = dv_copy.name + dv_copy.position.name.removeprefix(
dv.name
)
except AttributeError:
pos_name = dv_copy.name + removeprefix(
dv_copy.position.name, dv.name
)
dv_copy.position = _Defines.Position(
dv_copy.name + dv_copy.position.name.removeprefix(dv.name),
pos_name,
new_pos[0],
new_pos[1],
new_pos[2],
"mm",
self,
True,
)

try:
rot_name = dv_copy.name + dv_copy.rotation.name.removeprefix(
dv.name
)
except AttributeError:
rot_name = dv_copy.name + removeprefix(
dv_copy.rotation.name, dv.name
)

dv_copy.rotation = _Defines.Rotation(
dv_copy.name + dv_copy.rotation.name.removeprefix(dv.name),
rot_name,
new_rot[0],
new_rot[1],
new_rot[2],
Expand Down
5 changes: 3 additions & 2 deletions src/pyg4ometry/visualisation/VisualisationOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ def loadPredefined():
Lods from package resource files.
"""
import pkg_resources as _pkg_resources
import importlib_resources

config = _configparser.ConfigParser(
allow_no_value=True, interpolation=_configparser.ExtendedInterpolation()
)
config.optionxform = str

ini = _pkg_resources.resource_filename(__name__, "colours.ini")
ini = importlib_resources.files("pyg4ometry") / "visualisation/colours.ini"

with open(ini) as f:
config.read_file(f)

Expand Down
121 changes: 121 additions & 0 deletions tests/compare/ComparisonAssemblyVolume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import pyg4ometry
import pyg4ometry.geant4 as _g4


def Test(printOut=False):
r = _g4.Registry()

# in all of these we force testsAlreadyDone=[] as an argument to reset
# the one 'definition' of it in python. We have to be careful with this
# trick for pass by reference-like arguments

# we use successive new registries for a mish-mash of bits (not caring about writing out)
# so we can have degenerate names - the comparison doesn't care - it just looks at parameters

tests = pyg4ometry.compare.Tests()

galactic1 = _g4.MaterialPredefined("G4_Galactic", r)
galactic2 = _g4.MaterialPredefined("G4_Galactic", r)

# predefined materials
comp1 = pyg4ometry.compare.materials(galactic1, galactic2, tests)
if printOut:
comp1.print()
assert len(comp1) == 0

# some geometry
a_a_solid = _g4.solid.Box("a_a_solid", 50, 40, 30, r)
a_b_solid = _g4.solid.Tubs("a_b_solid", 0, 12, 30, 0, "2*pi", r)
iron = _g4.MaterialPredefined("G4_Fe")
copper = _g4.MaterialPredefined("G4_Cu")
a_a_lv = _g4.LogicalVolume(a_a_solid, copper, "a_a_lv", r)
a_b_lv = _g4.LogicalVolume(a_b_solid, copper, "a_b_lv", r)
a_ass = _g4.AssemblyVolume("a_assembly", r)
a_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 100], a_a_lv, "a_a_pv1", a_ass, r)
a_b_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 50], a_b_lv, "a_b_pv1", a_ass, r)

# with itself
comp2 = pyg4ometry.compare.assemblyVolumes(a_ass, a_ass, tests, testsAlreadyDone=[])
if printOut:
comp2.print()
assert len(comp2) == 0

# missing daughter
r2 = _g4.Registry()
b_ass = _g4.AssemblyVolume("a_assembly", r2)
b_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 100], a_a_lv, "a_a_pv1", b_ass, r2)
comp3 = pyg4ometry.compare.assemblyVolumes(a_ass, b_ass, tests, testsAlreadyDone=[])
if printOut:
comp3.print()
assert len(comp3) == 2

# extra daughter
r3 = _g4.Registry()
c_ass = _g4.AssemblyVolume("a_assembly", r3)
c_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 100], a_a_lv, "a_a_pv1", c_ass, r3)
c_b1_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 50], a_b_lv, "a_b_pv1", c_ass, r3)
c_b2_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 0], a_b_lv, "a_b_pv2", c_ass, r3)
comp4 = pyg4ometry.compare.assemblyVolumes(a_ass, c_ass, tests, testsAlreadyDone=[])
if printOut:
comp4.print()
assert len(comp4) == 2

# different daughter by name
r4 = _g4.Registry()
d_ass = _g4.AssemblyVolume("a_assembly", r4)
d_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 100], a_a_lv, "a_aaaa_pv1", d_ass, r4)
d_b_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 50], a_b_lv, "a_b_pv1", d_ass, r4)
comp5 = pyg4ometry.compare.assemblyVolumes(a_ass, d_ass, tests, testsAlreadyDone=[])
if printOut:
comp5.print()
assert len(comp5) == 2 # both missing and extra

# different values of pvs
r5 = _g4.Registry()
e_ass = _g4.AssemblyVolume("a_assembly", r5)
e_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, -100], a_a_lv, "a_a_pv1", e_ass, r5)
e_b_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 50], a_b_lv, "a_b_pv1", e_ass, r5)
comp6 = pyg4ometry.compare.assemblyVolumes(a_ass, e_ass, tests, testsAlreadyDone=[])
if printOut:
comp6.print()
assert (
len(comp6) == 3
) # 1 pv pos fail, 1x bounding box min fail, 1x bounding box max fail

# different values of lv material inside pvs inside avs
r6 = _g4.Registry()
f_ass = _g4.AssemblyVolume("a_assembly", r6)
a_b_lv = _g4.LogicalVolume(a_b_solid, iron, "a_b_lv", r6)
f_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 100], a_a_lv, "a_a_pv1", f_ass, r6)
f_b_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 50], a_b_lv, "a_b_pv1", f_ass, r6)
comp7 = pyg4ometry.compare.assemblyVolumes(a_ass, f_ass, tests, testsAlreadyDone=[])
if printOut:
comp7.print()
assert len(comp7) == 3 # materialName, materialNameIgnorePointer materialNameNIST

# mesh volume / area testing
r7 = _g4.Registry()
# should be equivalent to a_a_solid = _g4.solid.Box("a_a_solid", 50, 40, 30, r)
c_a_solidA = _g4.solid.Box("c_a_solidA", 80, 40, 30, r7)
c_a_solidB = _g4.solid.Box("c_a_solidB", 50, 90, 30, r7)
c_a_solid = _g4.solid.Intersection(
"c_a_solid", c_a_solidA, c_a_solidB, [[0, 0, 0], [0, 0, 0]], r7
)
c_a_lv = _g4.LogicalVolume(c_a_solid, copper, "c_a_lv", r7)
g_ass = _g4.AssemblyVolume("a_assembly", r7)
a_a_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 100], c_a_lv, "a_a_pv1", g_ass, r7)
a_b_pv = _g4.PhysicalVolume([0, 0, 0], [0, 0, 50], a_b_lv, "a_b_pv1", g_ass, r7)
testVolumeAreaOnly = pyg4ometry.compare.Tests("shapeVolume", "shapeArea")
assert len(testVolumeAreaOnly) == 2
comp8 = pyg4ometry.compare.assemblyVolumes(
a_ass, g_ass, testVolumeAreaOnly, testsAlreadyDone=[]
)
if printOut:
comp8.print()
assert len(comp8) == 0

# return {"teststatus": True}


if __name__ == "__main__":
Test()
25 changes: 25 additions & 0 deletions tests/compare/ComparisonDivisionVolume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pyg4ometry
import pyg4ometry.geant4 as _g4


def test(printOut=False):
r = _g4.Registry()

tests = pyg4ometry.compare.Tests()

galactic1 = _g4.MaterialPredefined("G4_Galactic", r)
galactic2 = _g4.MaterialPredefined("G4_Galactic", r)

# predefined materials
comp1 = pyg4ometry.compare.materials(galactic1, galactic2, tests)
if printOut:
comp1.print()
assert len(comp1) == 0

# TBC

# return {"teststatus": True}


if __name__ == "__main__":
test()
Loading

0 comments on commit 53585c4

Please sign in to comment.