Skip to content

Commit 263bc70

Browse files
authored
Merge pull request #192 from Fusion4Energy/fix-gvr-cyl-bug
Fix gvr cyl bug
2 parents 1b921a2 + da823f8 commit 263bc70

5 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/f4enix/input/ww_gvr/cli.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from copy import deepcopy
77
from enum import Enum
88
from pathlib import Path
9-
from typing import Dict, Optional, Tuple
109

1110
from f4enix.input.ww_gvr import WW
1211

@@ -62,7 +61,7 @@ def __init__(self) -> None:
6261
6362
Running Menu() will start the CLI loop directly.
6463
"""
65-
self.weight_windows: Dict[str, WW] = {}
64+
self.weight_windows: dict[str, WW] = {}
6665
self.extra_text: str = ""
6766
self.command_map = {
6867
Command.OPEN: self._handle_open,
@@ -171,7 +170,7 @@ def _handle_gvr(self):
171170
except FileNotFoundError:
172171
self.extra_text = " File not found..."
173172

174-
def _select_ww_key(self) -> Optional[str]:
173+
def _select_ww_key(self) -> str | None:
175174
if len(self.weight_windows) == 0:
176175
self.extra_text = " No weight windows loaded..."
177176
return None
@@ -189,7 +188,7 @@ def _select_ww_key(self) -> Optional[str]:
189188
return list(self.weight_windows.keys())[ww_key - 1]
190189
print(" Invalid weight window number...")
191190

192-
def _ask_gvr_parameters(self) -> Tuple[float, float]:
191+
def _ask_gvr_parameters(self) -> tuple[float, float]:
193192
try:
194193
maximum_splitting = float(
195194
input(" Enter maximum splitting ratio (default 5): ")

src/f4enix/input/ww_gvr/models.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from dataclasses import dataclass
88
from enum import Enum
99
from pathlib import Path
10-
from typing import Dict, List, Union
1110

1211
import numpy as np
1312
from numpy.typing import NDArray
@@ -47,8 +46,8 @@ def __eq__(self, other):
4746
return False
4847

4948

50-
Pathlike = Union[str, Path]
51-
NestedList = List[List[float]]
52-
ValuesByEnergy = Dict[float, np.ndarray]
53-
ValuesByParticle = Dict[ParticleType, ValuesByEnergy]
54-
EnergiesByParticle = Dict[ParticleType, List[float]]
49+
Pathlike = str | Path
50+
NestedList = list[list[float]]
51+
ValuesByEnergy = dict[float, np.ndarray]
52+
ValuesByParticle = dict[ParticleType, ValuesByEnergy]
53+
EnergiesByParticle = dict[ParticleType, list[float]]

src/f4enix/input/ww_gvr/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
Some utilities for the ww_gvr package.
33
"""
44

5-
from typing import Tuple
6-
75
import numpy as np
86

97
from f4enix.input.ww_gvr.models import Vectors
108

119

12-
def decompose_b2_vectors(b2_vectors: Vectors) -> Tuple[Vectors, Vectors]:
10+
def decompose_b2_vectors(b2_vectors: Vectors) -> tuple[Vectors, Vectors]:
1311
"""
1412
Takes a Vectors object with b2 format and returns two Vectors objects, one with the
1513
coarse vectors and the other with the fine vectors.
@@ -32,7 +30,7 @@ def decompose_b2_vectors(b2_vectors: Vectors) -> Tuple[Vectors, Vectors]:
3230
3331
Returns
3432
-------
35-
Tuple[Vectors, Vectors]
33+
tuple[Vectors, Vectors]
3634
Two Vectors objects, one with the coarse vectors and the other with the
3735
fine vectors.
3836
"""

src/f4enix/input/ww_gvr/ww_parser.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from dataclasses import dataclass
66
from pathlib import Path
7-
from typing import Any, Dict, List, TextIO
7+
from typing import Any, TextIO
88

99
import numpy as np
1010
from numpy import float64
@@ -25,20 +25,20 @@ class WWHeader:
2525
ni: int # Number of particle types
2626
nr: int # 10/16/16 = cartesian/cylindrical/spherical coord
2727
probid: str # Date and time of run
28-
ne: List[int] # Number of energy windows of each particle
28+
ne: list[int] # Number of energy windows of each particle
2929
nfx: int # Number of fine meshses in i
3030
nfy: int # Number of fine meshses in j
3131
nfz: int # Number of fine meshses in k
32-
origin: List[float] # Bottom left corner for cart, bottom center for cyl
32+
origin: list[float] # Bottom left corner for cart, bottom center for cyl
3333
ncx: int # Number of coarse meshses in i
3434
ncy: int # Number of coarse meshses in j
3535
ncz: int # Number of coarse meshses in k
3636

3737

3838
@dataclass()
3939
class WWHeaderCyl(WWHeader):
40-
director_1: List[float] # Vector defining the director 1
41-
director_2: List[float] # Vector defining the director 2
40+
director_1: list[float] # Vector defining the director 1
41+
director_2: list[float] # Vector defining the director 2
4242

4343

4444
@dataclass()
@@ -102,7 +102,7 @@ def _parse_header(infile: TextIO) -> WWHeader:
102102
ncy = int(float(words[1])) # Number of coarse meshes in j
103103
ncz = int(float(words[2])) # Number of coarse meshes in k
104104

105-
header_args: Dict[str, Any] = {
105+
header_args: dict[str, Any] = {
106106
"if_": if_,
107107
"iv": iv,
108108
"ni": ni,
@@ -147,7 +147,7 @@ def _read_block_2_vector(
147147
...]
148148
"""
149149
expected_length = 3 * number_of_coarse_intervals + 1
150-
vector: List[float] = []
150+
vector: list[float] = []
151151
while len(vector) < expected_length:
152152
words = infile.readline().split()
153153
vector.extend([float(word) for word in words])
@@ -162,7 +162,7 @@ def _read_block_3(infile: TextIO, header: WWHeader) -> tuple[NestedList, NestedL
162162
for particle_index in range(header.ni):
163163
# Read energy bins for this particle
164164
expected_energy_bins = header.ne[particle_index]
165-
energies_current_particle: List[float] = []
165+
energies_current_particle: list[float] = []
166166
while len(energies_current_particle) < expected_energy_bins:
167167
words = infile.readline().split()
168168
energies_current_particle.extend([float(word) for word in words])
@@ -171,7 +171,7 @@ def _read_block_3(infile: TextIO, header: WWHeader) -> tuple[NestedList, NestedL
171171
expected_value_bins = (
172172
header.nfx * header.nfy * header.nfz * expected_energy_bins
173173
)
174-
values_current_particle: List[float] = []
174+
values_current_particle: list[float] = []
175175
while len(values_current_particle) < expected_value_bins:
176176
words = infile.readline().split()
177177
values_current_particle.extend([float(word) for word in words])
@@ -224,11 +224,11 @@ def _read_header_from_meshtally_file(mesh: Fmesh) -> WWHeader:
224224
nfy = ncy = len(mesh.x2bin) - 1
225225
nfz = ncz = len(mesh.x3bin) - 1
226226
if mesh.trsf and any(mesh.trsf.origin):
227-
origin = [mesh.trsf.origin[2], mesh.trsf.origin[1], mesh.trsf.origin[0]]
227+
origin = [mesh.trsf.origin[0], mesh.trsf.origin[1], mesh.trsf.origin[2]]
228228
else:
229229
origin = [0.0, 0.0, 0.0]
230230

231-
header_args: Dict[str, Any] = {
231+
header_args: dict[str, Any] = {
232232
"if_": if_,
233233
"iv": iv,
234234
"ni": ni,
@@ -257,6 +257,12 @@ def _read_header_from_meshtally_file(mesh: Fmesh) -> WWHeader:
257257
director_2 = [0.0, 1.0, 0.0] # Cant be the same as the axis
258258
else:
259259
director_2 = mesh.trsf.vec.tolist()
260+
# The director vectors are AXS and VEC but with origin in the bottom center of
261+
# the cylinder instead of 0,0,0 as provided by MESHTAL.
262+
height = mesh.x2bin[-1] - mesh.x2bin[0]
263+
director_1 = (np.array(director_1) * height + np.array(origin)).tolist()
264+
radius = mesh.x1bin[-1] - mesh.x1bin[0]
265+
director_2 = (np.array(director_2) * radius + np.array(origin)).tolist()
260266
header_args.update(
261267
{
262268
"director_1": director_1,

tests/test_ww_gvr/test_ww_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ def test_read_meshtally_file_cyl():
189189
ncx=3,
190190
ncy=10,
191191
ncz=10,
192-
director_1=[1.0, 0.0, 0.0],
193-
director_2=[0.0, 0.0, 1.0],
192+
director_1=[0.0, -50.0, -50.0],
193+
director_2=[-50.0, -50.0, 0.0],
194194
)
195195
expected_b2_vec_i = np.array(
196196
[0.0, 1.0, 16.67, 1.0, 1.0, 33.33, 1.0, 1.0, 50.0, 1.0]
197197
)
198198
expected_energies = [[100]]
199199

200200
result = read_meshtally_file(
201-
Path("tests") / "test_ww_gvr" / "resources" / "meshtal_cyl", tally_id=4
201+
Path("tests") / "test_ww_gvr" / "resources" / "meshtal_cyl", tally_id=4
202202
)
203203

204204
assert expected_ww_header == result.header

0 commit comments

Comments
 (0)