Skip to content

Commit

Permalink
Update scoring names to be distinct for FLUKA (#224)
Browse files Browse the repository at this point in the history
* updating scoring name ready

* fix most tests

* new approach

* update tests

* update hash generation

* clean up

* fix

* move hash method outside of quantity class

* replace characters not supported by fluka
  • Loading branch information
dudiiiiiiii authored Jan 12, 2025
1 parent b8a784d commit 3ff963f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
11 changes: 6 additions & 5 deletions converter/fluka/cards/scoring_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def handle_usrbin_scoring(detector: _DetectorType, quantity: Quantity, output_un

output_unit_in_fluka_convention = str(output_unit * -1)

# save output_unit to quantity object for later unique name generation
quantity.output_unit = output_unit

output.extend(parse_detector(detector, quantity, quantity_to_score, output_unit_in_fluka_convention))

counter.usrbin_counter += 1
Expand All @@ -70,8 +73,7 @@ def handle_usrbin_scoring(detector: _DetectorType, quantity: Quantity, output_un
def parse_detector(detector, quantity, quantity_to_score, output_unit_in_fluka_convention) -> list[Card]:
"""Creates USRBIN card"""
if isinstance(detector, CylinderDetector):
return _parse_cylinder_detector(detector, quantity, quantity_to_score,
output_unit_in_fluka_convention)
return _parse_cylinder_detector(detector, quantity, quantity_to_score, output_unit_in_fluka_convention)
return _parse_mesh_detector(detector, quantity, quantity_to_score, output_unit_in_fluka_convention)


Expand All @@ -82,7 +84,7 @@ def _parse_mesh_detector(detector: MeshDetector, quantity: Quantity, quantity_to
first_card.what = [
'10.0', quantity_to_score, output_unit_in_fluka_convention, detector.x_max, detector.y_max, detector.z_max
]
first_card.sdum = short_name(quantity.name)
first_card.sdum = short_name(quantity.name_string())

second_card = Card(codewd='USRBIN')
second_card.what = [
Expand All @@ -105,7 +107,7 @@ def _parse_cylinder_detector(detector: CylinderDetector, quantity: Quantity, qua
first_card.what = [
'11.0', quantity_to_score, output_unit_in_fluka_convention, detector.r_max, detector.y, detector.z_max
]
first_card.sdum = short_name(quantity.name)
first_card.sdum = short_name(quantity.name_string())

second_card = Card(codewd='USRBIN')
second_card.what = [
Expand Down Expand Up @@ -167,7 +169,6 @@ def __str__(self) -> str:

# temporary default for no symmetry
result: list[str] = []

default_output_unit = 21
counter = ScoringCardIndexCounter()
for scoring in self.data:
Expand Down
27 changes: 27 additions & 0 deletions converter/fluka/helper_parsers/scoring_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dataclasses import dataclass
from typing import Optional, Union
import hashlib
import base64

from converter.fluka.helper_parsers.detector_parser import MeshDetector, parse_mesh_detector, CylinderDetector, \
parse_cylinder_detector
Expand Down Expand Up @@ -30,10 +32,19 @@ class Quantity:
"""Class representing Quantity"""

name: str
output_unit: Optional[int]
scoring_filter: Optional[Union[CustomFilter, ParticleFilter]]
modifiers: list[any] # unused
keyword: str = 'DOSE'

def name_string(self) -> str:
"""Generate unique name for scoring based on its name and output_unit"""
# Create a consistent hash based on the name and output_unit
generated_hash = generate_custom_hash(f'{self.name}_{self.output_unit}', 5)

# Generate the string in the desired format
return f'{self.name[:4]}_{generated_hash}'


@dataclass
class Scoring:
Expand Down Expand Up @@ -122,6 +133,7 @@ def parse_scorings(detectors_json: dict, scorings_json: dict) -> list[Scoring]:

quantities.append(
Quantity(name=quantity['name'],
output_unit=None,
keyword=quantity['keyword'],
scoring_filter=scoring_filter,
modifiers=quantity.get('modifiers')))
Expand All @@ -139,3 +151,18 @@ def parse_detector(detector_dict: dict) -> Optional[Union[MeshDetector, Cylinder
return parse_cylinder_detector(detector_dict)

return None


def generate_custom_hash(input_string, length=10):
"""Generate custom hash with specific length consisting of only 0-9, a-z and A-Z characters"""
# Generate a SHA-256 hash
hash_object = hashlib.sha256(input_string.encode())
# Get the binary digest
binary_hash = hash_object.digest()
# Encode it in standard base64
base64_hash = base64.b64encode(binary_hash).decode('utf-8')
# Replace '+' and '/' with alphanumeric characters (e.g., 'a' and 'b')
base64_hash = base64_hash.replace('+', 'a').replace('/', 'b')
# Remove padding '=' and truncate to the desired length
custom_hash = base64_hash.replace('=', '')[:length]
return custom_hash
4 changes: 2 additions & 2 deletions tests/fluka/expected_fluka_output/fl_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
ASSIGNMA WATER world
ASSIGNMA BLCKHOLE boundary
* generated scoring cards
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Fluence
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Flue_TzG3O
USRBIN -0.05 -5.0 -6.0 1.0 100.0 120.0&
AUXSCORE USRBIN -100100.0 1.0 1.0 1.0
* random number generator settings
RANDOMIZ 137
* number of particles to simulate
START 10000.0
STOP
"""
"""
34 changes: 17 additions & 17 deletions tests/fluka/test_scoring_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def detectors_json_4(project4_fluka_json: dict) -> dict:
def expected_card() -> str:
"""Returns expected Fluka scoring card sets"""
lines = """
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Fluence
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Fluence_21
USRBIN -0.05 -5.0 -6.0 1.0 100.0 120.0&
AUXSCORE USRBIN -100100.0 1.0 1.0 1.0
"""
Expand All @@ -59,7 +59,7 @@ def expected_card() -> str:
def expected_scores() -> str:
"""Returns expected Fluka scoring card sets"""
lines = """
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Fluence
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Flue_TzG3O
USRBIN -0.05 -5.0 -6.0 1.0 100.0 120.0&
AUXSCORE USRBIN -100100.0 1.0 1.0 1.0
"""
Expand All @@ -71,10 +71,10 @@ def expected_scores() -> str:
def expected_scores_2() -> str:
"""Returns expected Fluka scoring card sets"""
lines = """
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Fluence
USRBIN 10.0 ALL-PART -21.0 0.05 5.0 6.0Flue_TzG3O
USRBIN -0.05 -5.0 -6.0 1.0 100.0 120.0&
AUXSCORE USRBIN -100100.0 1.0 1.0 1.0
USRBIN 10.0 DOSE -22.0 12.5 25.0 37.5MyDose
USRBIN 10.0 DOSE -22.0 12.5 25.0 37.5MyDo_dX4W2
USRBIN 7.5 15.0 22.5 10.0 100.0 150.0&
AUXSCORE USRBIN NEUTRON 2.0 2.0 1.0
"""
Expand All @@ -86,7 +86,7 @@ def expected_scores_2() -> str:
def expected_scores_cylinder() -> str:
"""Returns expected Fluka scoring card sets"""
lines = """
USRBIN 11.0 DOSE -21.0 3.0 0.0 21.0Dose1
USRBIN 11.0 DOSE -21.0 3.0 0.0 21.0Dose_Tt536
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
"""

Expand All @@ -97,36 +97,36 @@ def expected_scores_cylinder() -> str:
def expected_scores_4() -> str:
"""Returns expected Fluka scoring card sets"""
lines = """
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_Total
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_To_al6m6
USRBIN -2.5 -2.5 3.0 1.0 1.0 100.0&
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_Proton
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_Pr_EWZdl
USRBIN -2.5 -2.5 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN PROTON 2.0 2.0 1.0
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_He3
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_He_q3AVx
USRBIN -2.5 -2.5 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN 3-HELIUM 3.0 3.0 1.0
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_C_Cus
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_C__8Y1Bp
USRBIN -2.5 -2.5 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN-1200600.0 4.0 4.0 1.0
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_He_Cus
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_He_oeZGk
USRBIN -2.5 -2.5 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN -400200.0 5.0 5.0 1.0
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_B_Cus
USRBIN 10.0 DOSE -21.0 2.5 2.5 21.0D_B__HBQJv
USRBIN -2.5 -2.5 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN -900400.0 6.0 6.0 1.0
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_Total
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_To_SNVdq
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
USRBIN 11.0 PROTON -22.0 2.5 0.0 21.0F_Proton
USRBIN 11.0 PROTON -22.0 2.5 0.0 21.0F_Pr_7CgH1
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
USRBIN 11.0 3-HELIUM -22.0 2.5 0.0 21.0F_He3
USRBIN 11.0 3-HELIUM -22.0 2.5 0.0 21.0F_He_KCn1N
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_C_Cus
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_C__w6biy
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN-1200600.0 10.0 10.0 1.0
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_He_Cus
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_He_WQwNa
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN -400200.0 11.0 11.0 1.0
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_B_Cus
USRBIN 11.0 ALL-PART -22.0 2.5 0.0 21.0F_B__WYZoZ
USRBIN 0.0 0.0 3.0 1.0 1.0 100.0&
AUXSCORE USRBIN -900400.0 12.0 12.0 1.0
"""
Expand Down

0 comments on commit 3ff963f

Please sign in to comment.