-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvolume_geometry_services.py
262 lines (213 loc) · 7.33 KB
/
volume_geometry_services.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
__author__ = "Maria K Zurek <[email protected]>"
from dataclasses import dataclass, field, asdict
import logging
import collections
import re
from typing import List, Iterable, Dict, Tuple, Union
from gemc_api_geometry import GVolume
_logger = logging.getLogger("volume_geometry_services")
def _ensure_single_unit(units: Iterable[str]) -> str:
if len(set(units)) == 1:
return units[0]
else:
raise ValueError(f"The units are different! {units}")
def _extract_numbers_units(tokens: Iterable[str], unit_separator: str = "*") -> Tuple[List[float], List[str]]:
numbers, units = [], []
for tok in tokens:
number_and_units = tok.split(unit_separator)
if len(number_and_units) == 2:
num, unit = number_and_units
else:
num, unit = tok, None
numbers.append(float(num))
units.append(unit)
return numbers, units
@dataclass
class PositionParams:
"Parses position parameters"
original: str
tokens: List[str] = None
without_units: str = None
numbers: List[float] = None
units: List[str] = None
def __post_init__(self):
self.tokens = self.original.split()
self.numbers, self.units = _extract_numbers_units(self.tokens)
@dataclass
class RotationParams:
"Parses and orders rotation parameters"
original: str
tokens: List[str] = None
order: str = None
numbers: List[str] = None
units: List[str] = None
single_unit: str = None
def __post_init__(self):
s = self.original
self.tokens = s.split()
numbers_with_units = []
if len(self.tokens) == 3:
numbers_with_units = self.tokens
elif len(self.tokens) == 5:
self.order = " ".join(self.tokens[1:2])
numbers_with_units = self.tokens[2:5]
self.numbers, self.units = _extract_numbers_units(numbers_with_units)
self.single_unit = _ensure_single_unit(self.units)
@dataclass
class SolidParams:
original: str
solid_type: str
dimensions: List[str] = None
numbers: List[float] = None
units: List[str] = None
def __post_init__(self):
self.dimensions = self.original.split()
self.numbers, self.units = _extract_numbers_units(self.dimensions)
def process_volume(self, gvolume):
map_type_to_method = {
"Box": self.process_box,
"Tube": self.process_tube,
"Sphere": self.process_sphere,
"Polycone": self.process_polycone,
"Trd": self.process_trd,
"G4Trap": self.process_trap,
}
return map_type_to_method[self.solid_type](gvolume)
def process_box(self, gvolume):
gvolume.make_box(
*self.numbers,
lunit=_ensure_single_unit(self.units),
)
def process_tube(self, gvolume):
length_units = self.units[:3]
angle_units = self.units[3:]
gvolume.make_tube(
*self.numbers,
lunit1=_ensure_single_unit(length_units),
lunit2=_ensure_single_unit(angle_units)
)
def process_sphere(self, gvolume):
length_units = self.units[:2]
angle_units = self.units[2:]
gvolume.make_sphere(
*self.numbers,
lunit1=_ensure_single_unit(length_units),
lunit2=_ensure_single_unit(angle_units)
)
def process_polycone(self, gvolume):
def get_chunk(lst, idx, size):
return lst[size * idx:size * (idx + 1)]
def get_chunks(numbers, size):
return {
idx: get_chunk(numbers[3:], idx, size)
for idx in [0, 1, 2]
}
n_planes = int(self.numbers[2])
chunks = get_chunks(self.numbers, n_planes)
args = [
self.numbers[0],
self.numbers[1],
n_planes,
*chunks[2],
*chunks[0],
*chunks[1]
]
angle_units = self.units[:2]
length_units = self.units[3:]
gvolume.make_polycone(
*args,
lunit1=_ensure_single_unit(length_units),
lunit2=_ensure_single_unit(angle_units)
)
def process_trd(self, gvolume):
gvolume.make_trapezoid(
*self.numbers,
lunit=_ensure_single_unit(self.units)
)
def process_trap(self, gvolume):
length_units = self.units[0:1] + self.units[3:6] + self.units[7:10]
angle_units = self.units[1:3] + [self.units[6], self.units[10]]
gvolume.make_trap(
self.numbers,
lunit1=_ensure_single_unit(length_units),
lunit2=_ensure_single_unit(angle_units)
)
@dataclass
class VolumeParams:
"Parses and stores G4 volume parameters"
_original: str = field(repr=False)
_tokens: List[str] = field(default_factory=list, repr=None)
name: str = None
mother: str = None
_solid: SolidParams = None
material: str = None
_position: PositionParams = None
_rotation: RotationParams = None
mfield: str = None
visible: float = None
style: float = None
color: str = None
digitization: str = None
_identifier_volume: str = None
_identifier_template: str = field(default="", repr=False)
_identifier_numbers: List[str] = None
identifier: str = None
copyOf: str = None
replicaOf: str = None
solidsOpr: str = None
mirror: str = None
exist: str = None
description: str = None
def __post_init__(self):
s = self._original
self.tokens = [
tok.strip()
for tok in s.split("|")
]
self.read_volume(self.tokens)
if self._identifier_template:
self.set_identifier(self._identifier_template)
def read_volume(self, tokens: List[str]):
self.name = tokens[0]
self.mother = tokens[1]
self._solid = SolidParams(tokens[5], tokens[4])
self._position = PositionParams(tokens[2])
self._rotation = RotationParams(tokens[3])
self._identifier_volume = tokens[6]
self._identifier_numbers = self._identifier_volume.split()
def build_gvolume(self):
gvolume = GVolume(self.name)
gvolume.set_position(
*self._position.numbers,
lunit=_ensure_single_unit(self._position.units),
)
rotation_kwargs = {}
if self._rotation.single_unit is not None:
rotation_kwargs["lunit"] = self._rotation.single_unit
if self._rotation.order is not None:
rotation_kwargs["order"] = self._rotation.order
gvolume.set_rotation(
*self._rotation.numbers,
**rotation_kwargs,
)
self._solid.process_volume(gvolume)
self.set_attributes(gvolume)
return gvolume
def set_attributes(self, gvolume):
for attr_name, attr_value in asdict(self).items():
if attr_name.startswith("_"): continue
if attr_value is not None:
setattr(gvolume, attr_name, attr_value)
def read_file(
input_file_name,
) -> Iterable[VolumeParams]:
with open(input_file_name) as f:
return [
VolumeParams(line)
for line in f.readlines()
]
def parse(text: str, pattern: Union[str, re.Pattern]) -> dict:
pat = re.compile(pattern)
m = re.match(pat ,text)
return m.groupdict()