Skip to content

Commit f654674

Browse files
committed
inp: added nps & seed properties
1 parent b92184a commit f654674

5 files changed

Lines changed: 152 additions & 3 deletions

File tree

examples/cli_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Example converting INp files using ``Convert``.
2+
Example converting INP files using ``Convert``.
33
"""
44

55
import pathlib

examples/cli_run.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def posthook_batch(self, path):
3030
inp1 = pymcnp.Inp.from_file(path1)
3131
inp2 = pymcnp.Inp.from_file(path2)
3232

33+
inp0.nps = 1e4
34+
inp1.nps = 1e5
35+
inp2.nps = 1e6
36+
inp0.seed = 123534727
37+
inp1.seed = 123534727
38+
inp2.seed = 123534727
39+
3340
# Running.
3441
runner = MyRun([inp0, inp1, inp2], command='echo')
3542
runner.run('.')

src/pymcnp/Inp.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,100 @@ def other(self, other: str | types.String) -> None:
408408
other = types.String.from_mcnp(other)
409409

410410
self._other: types.Integer = other
411+
412+
@property
413+
def nps(self) -> types.Integer:
414+
"""
415+
File nps.
416+
417+
Raises:
418+
InpError: SEMANTICS_OPTION.
419+
TypeError:
420+
"""
421+
422+
try:
423+
card_nps = next(filter(lambda card: isinstance(card, inp.Data) and isinstance(card.option, inp.data.Nps), self.data))
424+
return card_nps.option.npp
425+
except StopIteration:
426+
return None
427+
428+
@nps.setter
429+
def nps(self, nps: str | int | types.Integer) -> None:
430+
"""
431+
Sets ``nps``.
432+
433+
Parameters:
434+
nps: File nps.
435+
436+
Raises:
437+
InpError: SEMANTICS_OPTION.
438+
TypeError:
439+
"""
440+
441+
if nps is not None:
442+
if isinstance(nps, types.Integer):
443+
nps = nps
444+
elif isinstance(nps, int):
445+
nps = types.Integer(nps)
446+
elif isinstance(nps, str):
447+
nps = types.Integer.from_mcnp(nps)
448+
449+
try:
450+
card_nps = next(filter(lambda card: isinstance(card, inp.Data) and isinstance(card.option, inp.data.Nps), self.data))
451+
card_nps.option.npp = nps
452+
except StopIteration:
453+
card_nps = inp.data.Nps(nps)
454+
self.data = [*self.data, inp.Data(card_nps)]
455+
456+
@property
457+
def seed(self) -> types.Integer:
458+
"""
459+
File seed.
460+
461+
Raises:
462+
InpError: SEMANTICS_OPTION.
463+
TypeError:
464+
"""
465+
466+
try:
467+
card_rand = next(filter(lambda card: isinstance(card, inp.Data) and isinstance(card.option, inp.data.Rand), self.data))
468+
option_seed = next(filter(lambda option: isinstance(option, inp.data.rand.Seed), card_rand.option.options or []))
469+
return option_seed.seed
470+
except StopIteration:
471+
return None
472+
473+
@seed.setter
474+
def seed(self, seed: str | int | types.Integer) -> None:
475+
"""
476+
Sets ``seed``.
477+
478+
Parameters:
479+
seed: File seed.
480+
481+
Raises:
482+
InpError: SEMANTICS_OPTION.
483+
TypeError:
484+
"""
485+
486+
if seed is not None:
487+
if isinstance(seed, types.Integer):
488+
seed = seed
489+
elif isinstance(seed, int):
490+
seed = types.Integer(seed)
491+
elif isinstance(seed, str):
492+
seed = types.Integer.from_mcnp(seed)
493+
494+
try:
495+
card_rand = next(filter(lambda card: isinstance(card, inp.Data) and isinstance(card.option, inp.data.Rand), self.data))
496+
497+
try:
498+
option_seed = next(filter(lambda option: isinstance(option, inp.data.rand.Seed), card_rand.option.options or []))
499+
option_seed.seed = seed
500+
except StopIteration:
501+
option_seed = inp.data.rand.Seed(seed)
502+
card_rand.option.options = [*(card_rand.option.options or []), option_seed]
503+
504+
except StopIteration:
505+
option_seed = inp.data.rand.Seed(seed)
506+
card_rand = inp.Data(inp.data.Rand([option_seed]))
507+
self.data = [*self.data, card_rand]

src/pymcnp/cli/run.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ def run(self, path: str | pathlib.Path):
109109
path_ptrac = subdirectory / f'run-{i}.ptrac'
110110

111111
subdirectory.mkdir()
112-
with path_input.open('w') as file:
113-
file.write(inp.to_mcnp())
112+
inp.to_file(path_input)
114113

115114
self.prehook_file(subdirectory, i)
116115
process = subprocess.Popen([f'{self.command}', f'inp={path_input} outp={path_output} ptrac={path_ptrac}'])

tests/test_pymcnp/test_Inp.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,52 @@ class Test_Init(classes.Test_Init):
7777
},
7878
]
7979

80+
class Test_Properties:
81+
EXAMPLES = [
82+
{
83+
'title': consts.string.type.STRING,
84+
'cells': [consts.string.inp.CELL, consts.string.inp.COMMENT, consts.string.inp.LIKE],
85+
'surfaces': [consts.string.inp.SURFACE, consts.string.inp.COMMENT],
86+
'data': [consts.string.inp.DATA, consts.string.inp.COMMENT],
87+
'message': consts.string.type.STRING,
88+
'other': consts.string.type.STRING,
89+
},
90+
]
91+
92+
def test_nps(self):
93+
for example in self.EXAMPLES:
94+
print(example)
95+
96+
inp = pymcnp.Inp(**example)
97+
inp.nps
98+
99+
inp.nps = '1e3'
100+
inp.nps = 10
101+
inp.nps = pymcnp.utils.types.Integer(1234938)
102+
inp.nps
103+
104+
def test_seed(self):
105+
for example in self.EXAMPLES:
106+
print(example)
107+
108+
inp = pymcnp.Inp(**example)
109+
inp.seed
110+
111+
save = inp.data
112+
113+
inp.seed = '11'
114+
inp.seed = 11
115+
inp.seed = pymcnp.utils.types.Integer(11)
116+
inp.seed
117+
118+
inp.data = save
119+
inp.data = [*inp.data, pymcnp.inp.Data(pymcnp.inp.data.Rand())]
120+
121+
inp.seed = '11'
122+
inp.seed = 11
123+
inp.seed = pymcnp.utils.types.Integer(11)
124+
inp.seed
125+
80126
class Test_Mcnp(classes.Test_Mcnp):
81127
element = pymcnp.Inp
82128
EXAMPLES_VALID = []

0 commit comments

Comments
 (0)