Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented clear for material #668

Draft
wants to merge 7 commits into
base: alpha-test-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions montepy/data_inputs/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __setitem__(self, key, value):
try:
node = self._libraries[key]
except KeyError:
node = self._generate_default_node(key)
node = self._generate_default_node(key, str(value))
self._parent()._append_param_lib(node)
self._libraries[key] = node
node["data"].value = str(value)
Expand Down Expand Up @@ -98,13 +98,13 @@ def _validate_key(key):
return key

@staticmethod
def _generate_default_node(key: LibraryType):
def _generate_default_node(key: LibraryType, val: str):
classifier = syntax_node.ClassifierNode()
classifier.prefix = key.value
ret = {
"classifier": classifier,
"seperator": syntax_node.PaddingNode(" = "),
"data": syntax_node.ValueNode("", str),
"data": syntax_node.ValueNode(val, str),
}
return syntax_node.SyntaxNode("mat library", ret)

Expand Down Expand Up @@ -641,6 +641,7 @@ def append(self, nuclide_frac_pair: tuple[Nuclide, float]):
self._check_valid_comp(nuclide_frac_pair)
self._elements.add(nuclide_frac_pair[0].element)
self._nuclei.add(nuclide_frac_pair[0].nucleus)
# node for fraction
node = self._generate_default_node(
float, str(nuclide_frac_pair[1]), "\n" + " " * DEFAULT_INDENT
)
Expand Down Expand Up @@ -806,6 +807,13 @@ def contains(
)
)

def clear(self):
"""
Clears all nuclide components from this material.
"""
for _ in range(len(self)):
del self[0]

def normalize(self):
"""
Normalizes the components fractions so that they sum to 1.0.
Expand Down
34 changes: 34 additions & 0 deletions tests/inputs/pin_cell.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Made up PWR pin Cell Problem
c uranium rod
1 1 10.0 -1 101 -102 imp:n=1
c gas gap
2 0 1 -2 101 -102 imp:n=1
c cladding
3 10 5.0 2 -3 101 -102 imp:n=1
c water cell
5 2 1.0 2 101 -102
103 -104
105 -106 imp:n=1

1 CZ 0.39
2 CZ 0.40
3 CZ 0.46
101 PZ -0.63
102 PZ 0.63
103 PX -0.63
104 PX 0.63
105 PY -0.63
106 PY 0.63

M1 92235.80c 0.04
92238.80c 0.96
M2 1001.80c 0.66
8016.80c 0.33
M10 40090.80c 0.515
40091.80c 0.112
40092.80c 0.171
40094.80c 0.174
40096.80c 0.028
mode n
nps 1e3

32 changes: 30 additions & 2 deletions tests/test_material.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from hypothesis import given, strategies as st, settings, HealthCheck
from hypothesis import assume, given, note, strategies as st, settings, HealthCheck
import pathlib
import pytest
from hypothesis import assume, given, note, strategies as st

import montepy

Expand Down Expand Up @@ -89,6 +89,19 @@ def test_mat_get_nuclide_library(
assert iter_key == item_key
assert big_mat_lib.default_libraries[iter_key] == item_val

def test_mat_clear(_, big_mat_lib):
old_mat = big_mat_lib.clone()
old_len = len(big_mat_lib)
assert old_len > 0
big_mat_lib.clear()
assert 0 == len(big_mat_lib)
for part in old_mat.default_libraries:
assert (
old_mat.default_libraries[part] == big_mat_lib.default_libraries[part]
)
big_mat_lib.add_nuclide("O-16.80c", 1.0)
_.verify_export(big_mat_lib)

def test_mat_get_nuclide_library_bad(_, big_mat_lib):
with pytest.raises(TypeError):
big_mat_lib.get_nuclide_library(5, "nlib")
Expand Down Expand Up @@ -404,6 +417,21 @@ def test_mat_long_str(_, big_material):
str(big_material)
repr(big_material)

@pytest.mark.parametrize("file", ["test.imcnp", "pin_cell.imcnp"])
def test_read_add_write(_, file):
problem = montepy.read_input(pathlib.Path("tests") / "inputs" / file)
mat = problem.materials[2]
mat.add_nuclide("O-16.80c", 0.3)
try:
out_file = f"mat_read_write_21ij43werr{file}"
problem.write_problem(out_file)
with open(out_file, "r") as fh:
for line in fh:
print(line.rstrip())
new_problem = montepy.read_input(out_file)
finally:
pathlib.Path(out_file).unlink(True)

@pytest.mark.parametrize(
"line, mat_number, is_atom, fractions",
[
Expand Down
Loading