Skip to content

Commit 9252c2d

Browse files
committed
inp: fixed SDEF option parsing
1 parent f738f45 commit 9252c2d

41 files changed

Lines changed: 891 additions & 231 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/source/pymcnp/inp/data/sdef.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@
117117
:inherited-members:
118118
```
119119

120+
### ``Cel_2`` Class
121+
122+
```{eval-rst}
123+
.. autoclass:: pymcnp.inp.data.sdef.Cel_2
124+
:members:
125+
:inherited-members:
126+
```
127+
120128
[f subpackage](sdef/f)
121129

122130
### ``Dat_0`` Class
@@ -223,6 +231,14 @@
223231
:inherited-members:
224232
```
225233

234+
### ``Ext_2`` Class
235+
236+
```{eval-rst}
237+
.. autoclass:: pymcnp.inp.data.sdef.Ext_2
238+
:members:
239+
:inherited-members:
240+
```
241+
226242
[f subpackage](sdef/f)
227243

228244
### ``Loc_0`` Class
@@ -295,6 +311,14 @@
295311
:inherited-members:
296312
```
297313

314+
### ``Pos_2`` Class
315+
316+
```{eval-rst}
317+
.. autoclass:: pymcnp.inp.data.sdef.Pos_2
318+
:members:
319+
:inherited-members:
320+
```
321+
298322
[f subpackage](sdef/f)
299323

300324
### ``Rad_0`` Class
@@ -445,6 +469,14 @@
445469
:inherited-members:
446470
```
447471

472+
### ``X_2`` Class
473+
474+
```{eval-rst}
475+
.. autoclass:: pymcnp.inp.data.sdef.X_2
476+
:members:
477+
:inherited-members:
478+
```
479+
448480
[f subpackage](sdef/f)
449481

450482
### ``Y_0`` Class
@@ -463,6 +495,13 @@
463495
:inherited-members:
464496
```
465497

498+
### ``Y_2`` Class
499+
500+
```{eval-rst}
501+
:members:
502+
:inherited-members:
503+
```
504+
466505
[f subpackage](sdef/f)
467506

468507
### ``Z_0`` Class
@@ -481,4 +520,12 @@
481520
:inherited-members:
482521
```
483522

523+
### ``Z_2`` Class
524+
525+
```{eval-rst}
526+
.. autoclass:: pymcnp.inp.data.sdef.Z_2
527+
:members:
528+
:inherited-members:
529+
```
530+
484531
[f subpackage](sdef/f)

files/inp/valid_14.inp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ m2 92238 0.98 92235 0.02
4343
m3 1001 1
4444
sdef erg fcel d1 x fcel d11 y fcel d13 z fcel d15 cel d6
4545
rad fcel d17 ext fcel d19 pos fcel d21 axs fcel d23
46-
ds1 s d2 d3 d4 d5
46+
c ds1 s d2 d3 d4 d5
4747
sp2 -2 1.2
4848
sp3 -2 1.3
4949
sp4 -2 1.4
@@ -59,25 +59,25 @@ c si9 l 3:5(1 2 0):13
5959
sp9 1
6060
c si10 l 3:5(1 2 0):15
6161
sp10 1
62-
ds11 s d12 0 0 d25
62+
c ds11 s d12 0 0 d25
6363
si12 -4 4
6464
sp12 0 1
65-
ds13 s d14 0 0 d26
65+
c ds13 s d14 0 0 d26
6666
si14 -3 3
6767
sp14 0 1
68-
ds15 s d16 0 0 d16
68+
c ds15 s d16 0 0 d16
6969
si16 -60 60
7070
sp16 0 1
71-
ds17 s 0 d18 d18 0
71+
c ds17 s 0 d18 d18 0
7272
si18 0 3
7373
sp18 -21 1
74-
ds19 s 0 d20 d20 0
74+
c ds19 s 0 d20 d20 0
7575
si20 -60 60
7676
sp20 0 1
77-
ds21 s 0 d22 d22 0
77+
c ds21 s 0 d22 d22 0
7878
si22 l 10 5 0
7979
sp22 1
80-
ds23 s 0 d24 d24 0
80+
c ds23 s 0 d24 d24 0
8181
si24 l 0 0 1
8282
sp24 1
8383
si25 16 20

files/inp/valid_29.inp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MX2:P model
2323
CUT:N 60e8
2424
CUT:P 60e8
2525
PHYS:P j 1 j -1 j -101
26-
SDEF PAR=P ERG=D1 X=FERG D2 Y=FERG D3 Z=FERG D4 TME=FERG D7
26+
SDEF PAR=P ERG=D1 X=FERG D2 Y=FERG D3 Z=FERG D4 TME=FERG D7
2727
VEC=1 0 0 DIR=FERG D8 CEL=1 WGT=1
2828
SI1 S 5 6
2929
SP1 0.9391 0.0609

src/pymcnp/inp/_card.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import re
2-
31
from . import _symbol
42
from .. import errors
53

@@ -65,21 +63,7 @@ def _preprocess(source: str) -> tuple[str, tuple[str]]:
6563
lines.append(line)
6664

6765
source = '\n'.join(lines)
68-
source = re.sub(r'\n +|& *\n *', ' ', source)
69-
source = re.sub(r' +', ' ', source)
70-
source = re.sub(r'[(] ', '(', source)
71-
source = re.sub(r' [)]', ')', source)
72-
source = re.sub(r'\n \n', '\n\n', source)
73-
source = re.sub(r' = | =|= |=', ' ', source)
74-
source = re.sub(r'\t', ' ', source)
75-
source = source.strip()
76-
77-
source = re.sub(r'((?: [jJ]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}j' + match[2], source)
78-
source = re.sub(r'((?: [rR]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}r' + match[2], source)
79-
source = re.sub(r'((?: [iI]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}i' + match[2], source)
80-
source = re.sub(r' (\d+)[jJ](\s|\Z|\n)', lambda match: int(match[1]) * ' j' + match[2], source)
81-
source = re.sub(r' (\d+)[rR](\s|\Z|\n)', lambda match: int(match[1]) * ' r' + match[2], source)
82-
source = re.sub(r' (\d+)[iI](\s|\Z|\n)', lambda match: int(match[1]) * ' I' + match[2], source)
66+
source = _symbol.InpNonterminal._preprocess(source)
8367

8468
return source, comments
8569

src/pymcnp/inp/_symbol.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ def _preprocess(source: str) -> str:
3232
source = re.sub(r'((?: [jJ]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}j' + match[2], source)
3333
source = re.sub(r'((?: [rR]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}r' + match[2], source)
3434
source = re.sub(r'((?: [iI]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}i' + match[2], source)
35+
source = re.sub(r'((?: [lL][oO][gG]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}log' + match[2], source)
36+
source = re.sub(r'((?: [iI][lL][oO][gG]){2,})(\s|\Z|\n)', lambda match: f' {len(match[1].strip().split())}ilog' + match[2], source)
3537
source = re.sub(r' (\d+)[jJ](\s|\Z|\n)', lambda match: int(match[1]) * ' j' + match[2], source)
3638
source = re.sub(r' (\d+)[rR](\s|\Z|\n)', lambda match: int(match[1]) * ' r' + match[2], source)
37-
source = re.sub(r' (\d+)[iI](\s|\Z|\n)', lambda match: int(match[1]) * ' I' + match[2], source)
39+
source = re.sub(r' (\d+)[iI](\s|\Z|\n)', lambda match: int(match[1]) * ' i' + match[2], source)
40+
source = re.sub(r' (\d+)[jJ](\s|\Z|\n)', lambda match: int(match[1]) * ' j' + match[2], source)
41+
source = re.sub(r' (\d+)[lL][oO][gG](\s|\Z|\n)', lambda match: int(match[1]) * ' log' + match[2], source)
42+
source = re.sub(r' (\d+)[iI][lL][oO][gG](\s|\Z|\n)', lambda match: int(match[1]) * ' ilog' + match[2], source)
3843

3944
return source

src/pymcnp/inp/data/sdef/Cel_1.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22

3-
from . import f
43
from . import _option
4+
from .... import types
55
from .... import errors
66

77

@@ -13,56 +13,58 @@ class Cel_1(_option.SdefOption):
1313
_KEYWORD = 'cel'
1414

1515
_ATTRS = {
16-
'option': f.FOption,
16+
'number': types.Distribution,
1717
}
1818

19-
_REGEX = re.compile(rf'\Acel( (?:{f.FOption._REGEX.pattern[2:-2]}))\Z', re.IGNORECASE)
19+
_REGEX = re.compile(rf'\Acel( {types.Distribution._REGEX.pattern[2:-2]})\Z', re.IGNORECASE)
2020

21-
def __init__(self, option: str | f.FOption):
21+
def __init__(self, number: str | int | types.Distribution):
2222
"""
2323
Initializes ``Cel_1``.
2424
2525
Parameters:
26-
option: Dependent distribution option.
26+
number: Cell number.
2727
2828
Raises:
2929
InpError: SEMANTICS_OPTION.
3030
"""
3131

32-
self.option: f.FOption = option
32+
self.number: types.Distribution = number
3333

3434
@property
35-
def option(self) -> f.FOption:
35+
def number(self) -> types.Distribution:
3636
"""
37-
Dependent distribution option
37+
Cell number
3838
3939
Raises:
4040
InpError: SEMANTICS_OPTION.
4141
TypeError:
4242
"""
4343

44-
return self._option
44+
return self._number
4545

46-
@option.setter
47-
def option(self, option: str | f.FOption) -> None:
46+
@number.setter
47+
def number(self, number: str | int | types.Distribution) -> None:
4848
"""
49-
Sets ``option``.
49+
Sets ``number``.
5050
5151
Parameters:
52-
option: Dependent distribution option.
52+
number: Cell number.
5353
5454
Raises:
5555
InpError: SEMANTICS_OPTION.
5656
TypeError:
5757
"""
5858

59-
if option is not None:
60-
if isinstance(option, f.FOption):
61-
option = option
62-
elif isinstance(option, str):
63-
option = f.FOption.from_mcnp(option)
59+
if number is not None:
60+
if isinstance(number, types.Distribution):
61+
number = number
62+
elif isinstance(number, int):
63+
number = types.Distribution(number)
64+
elif isinstance(number, str):
65+
number = types.Distribution.from_mcnp(number)
6466

65-
if option is None:
66-
raise errors.InpError(errors.InpCode.SEMANTICS_OPTION, option)
67+
if number is None:
68+
raise errors.InpError(errors.InpCode.SEMANTICS_OPTION, number)
6769

68-
self._option: f.FOption = option
70+
self._number: types.Distribution = number

src/pymcnp/inp/data/sdef/Cel_2.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import re
2+
3+
from . import f
4+
from . import _option
5+
from .... import errors
6+
7+
8+
class Cel_2(_option.SdefOption):
9+
"""
10+
Represents INP cel variation #2 elements.
11+
"""
12+
13+
_KEYWORD = 'cel'
14+
15+
_ATTRS = {
16+
'option': f.FOption,
17+
}
18+
19+
_REGEX = re.compile(rf'\Acel( (?:{f.FOption._REGEX.pattern[2:-2]}))\Z', re.IGNORECASE)
20+
21+
def __init__(self, option: str | f.FOption):
22+
"""
23+
Initializes ``Cel_2``.
24+
25+
Parameters:
26+
option: Dependent distribution option.
27+
28+
Raises:
29+
InpError: SEMANTICS_OPTION.
30+
"""
31+
32+
self.option: f.FOption = option
33+
34+
@property
35+
def option(self) -> f.FOption:
36+
"""
37+
Dependent distribution option
38+
39+
Raises:
40+
InpError: SEMANTICS_OPTION.
41+
TypeError:
42+
"""
43+
44+
return self._option
45+
46+
@option.setter
47+
def option(self, option: str | f.FOption) -> None:
48+
"""
49+
Sets ``option``.
50+
51+
Parameters:
52+
option: Dependent distribution option.
53+
54+
Raises:
55+
InpError: SEMANTICS_OPTION.
56+
TypeError:
57+
"""
58+
59+
if option is not None:
60+
if isinstance(option, f.FOption):
61+
option = option
62+
elif isinstance(option, str):
63+
option = f.FOption.from_mcnp(option)
64+
65+
if option is None:
66+
raise errors.InpError(errors.InpCode.SEMANTICS_OPTION, option)
67+
68+
self._option: f.FOption = option

src/pymcnp/inp/data/sdef/Dir_1.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from . import _option
44
from .... import types
5+
from .... import errors
56

67

78
class Dir_1(_option.SdefOption):
@@ -15,9 +16,9 @@ class Dir_1(_option.SdefOption):
1516
'cosine': types.Distribution,
1617
}
1718

18-
_REGEX = re.compile(rf'\Adir( {types.Distribution._REGEX.pattern[2:-2]})?\Z', re.IGNORECASE)
19+
_REGEX = re.compile(rf'\Adir( {types.Distribution._REGEX.pattern[2:-2]})\Z', re.IGNORECASE)
1920

20-
def __init__(self, cosine: str | types.Distribution = None):
21+
def __init__(self, cosine: str | types.Distribution):
2122
"""
2223
Initializes ``Dir_1``.
2324
@@ -61,4 +62,7 @@ def cosine(self, cosine: str | types.Distribution) -> None:
6162
elif isinstance(cosine, str):
6263
cosine = types.Distribution.from_mcnp(cosine)
6364

65+
if cosine is None:
66+
raise errors.InpError(errors.InpCode.SEMANTICS_OPTION, cosine)
67+
6468
self._cosine: types.Distribution = cosine

0 commit comments

Comments
 (0)