Skip to content

Commit

Permalink
Merge branch 'develop' into 476-parsing-of-transformations-on-fill-ke…
Browse files Browse the repository at this point in the history
…ywords-appears-to-be-failing
  • Loading branch information
MicahGale authored Aug 13, 2024
2 parents ed514bc + e63392e commit f0d4cd8
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 73 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![JOSS article status](https://joss.theoj.org/papers/e5b5dc8cea19605a1507dd4d420d5199/status.svg)](https://joss.theoj.org/papers/e5b5dc8cea19605a1507dd4d420d5199)
[![Coverage Status](https://coveralls.io/repos/github/idaholab/MontePy/badge.svg?branch=develop)](https://coveralls.io/github/idaholab/MontePy?branch=develop)
[![PyPI version](https://badge.fury.io/py/montepy.svg)](https://badge.fury.io/py/montepy)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)

MontePy is a python library to read, edit, and write MCNP input files.

Expand Down
5 changes: 5 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ MontePy Changelog
* Fixed bug with appending and renumbering numbered objects from other MCNP problems (:issue:`466`).
* Fixed bug with dynamic typing and the parsers that only appear in edge cases (:issue:`461`).
* Fixed parser bug with having spaces in the start of the transform input for the fill of a cell (:pull:`479`).
* Fixed bug with trying to get trailing comments from non-existant parts of the syntax tree (:pull:`480`).

**Code Quality**

* Simpler ``Isotope`` representation (:issue:`473`).


0.3.2
Expand Down
4 changes: 4 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
"sphinx.ext.napoleon",
"sphinx.ext.intersphinx",
"sphinx.ext.extlinks",
"sphinx_sitemap",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
html_favicon = "monty.svg"
html_logo = "monty.svg"

html_baseurl = "https://www.montepy.org/"
html_extra_path = ["robots.txt"]
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
Expand Down
4 changes: 4 additions & 0 deletions doc/source/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Allow: /

Sitemap: https://www.montepy.org/sitemap.xml
13 changes: 9 additions & 4 deletions montepy/data_inputs/isotope.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def library(self, library):
raise TypeError("library must be a string")
self._library = library

def __str__(self):
return f"{self.element.symbol:>2}-{self.A:<3} ({self._library})"
def __repr__(self):
return f"{self.__class__.__name__}({repr(self.nuclide_str())})"

def mcnp_str(self):
"""
Expand All @@ -182,6 +182,10 @@ def mcnp_str(self):
"""
return f"{self.ZAID}.{self.library}" if self.library else self.ZAID

def nuclide_str(self):
suffix = f".{self._library}" if self._library else ""
return f"{self.element.symbol}-{self.A}{suffix}"

def get_base_zaid(self):
"""
Get the ZAID identifier of the base isotope this is an isomer of.
Expand All @@ -193,8 +197,9 @@ def get_base_zaid(self):
"""
return self.Z * 1000 + self.A

def __repr__(self):
return f"ZAID={self.ZAID}, Z={self.Z}, A={self.A}, element={self.element}, library={self.library}"
def __str__(self):
suffix = f" ({self._library})" if self._library else ""
return f"{self.element.symbol:>2}-{self.A:<3}{suffix}"

def __hash__(self):
return hash(self._ZAID)
Expand Down
5 changes: 3 additions & 2 deletions montepy/input_parser/syntax_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ def comments(self):
def get_trailing_comment(self):
if len(self.nodes) == 0:
return
tail = next(reversed(self.nodes.items()))
return tail[1].get_trailing_comment()
for node in reversed(self.nodes.values()):
if node is not None:
return node.get_trailing_comment()

def _delete_trailing_comment(self):
tail = next(reversed(self.nodes.items()))
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ dependencies = [
[project.optional-dependencies]
test = ["coverage[toml]>=6.3.2", "pytest"]
# This is needed for a sphinx bug. See #414.
doc = ["sphinx>=7.4.0", "sphinxcontrib-apidoc", "sphinx_rtd_theme"]
doc = [
"sphinx>=7.4.0",
"sphinxcontrib-apidoc",
"sphinx_rtd_theme",
"sphinx-sitemap"
]
format = ["black>=23.3.0"]
build = [
"build",
Expand Down
3 changes: 3 additions & 0 deletions tests/inputs/test_missing_mat_for_mt.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ c surfaces
1 so 0.5

c data
m5 8016.71c 2.6999999-02
8017.71c 9.9999998-01
plib=84p
MT1 lwtr.01t
121 changes: 55 additions & 66 deletions tests/test_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,6 @@


class testMaterialClass(TestCase):
def test_material_init(self):
# test invalid material number
input_card = Input(["Mfoo"], BlockType.DATA)
with self.assertRaises(MalformedInputError):
Material(input_card)
input_card = Input(["M-20"], BlockType.DATA)
with self.assertRaises(MalformedInputError):
Material(input_card)

in_str = "M20 1001.80c 0.5 8016.710nc 0.5"
input_card = Input([in_str], BlockType.DATA)
material = Material(input_card)
self.assertEqual(material.number, 20)
self.assertEqual(material.old_number, 20)
self.assertTrue(material.is_atom_fraction)
for component in material.material_components:
self.assertEqual(material.material_components[component].fraction, 0.5)

# test implicit library with syntax tree errors
in_str = """m1 1001 0.33
8016 0.666667"""
input_card = Input(in_str.split("\n"), BlockType.DATA)
material = Material(input_card)
# test implicit library
in_str = "M20 1001 0.5 2001 0.5 8016.710nc 0.5"
input_card = Input([in_str], BlockType.DATA)
material = Material(input_card)
self.assertEqual(material.number, 20)
self.assertEqual(material.old_number, 20)
self.assertTrue(material.is_atom_fraction)
for component in material.material_components:
self.assertEqual(material.material_components[component].fraction, 0.5)

# test weight fraction
in_str = "M20 1001.80c -0.5 8016.80c -0.5"
input_card = Input([in_str], BlockType.DATA)
material = Material(input_card)
self.assertFalse(material.is_atom_fraction)
for component in material.material_components:
self.assertEqual(material.material_components[component].fraction, 0.5)

# test bad fraction
in_str = "M20 1001.80c foo"
input_card = Input([in_str], BlockType.DATA)
with self.assertRaises(MalformedInputError):
material = Material(input_card)
# test mismatch fraction
in_str = "M20 1001.80c 0.5 8016.80c -0.5"
input_card = Input([in_str], BlockType.DATA)
with self.assertRaises(MalformedInputError):
material = Material(input_card)
# test parameters
in_str = "M20 1001.80c 0.5 8016.80c 0.5 Gas=1"
input_card = Input([in_str], BlockType.DATA)
material = Material(input_card)
self.assertEqual(material.parameters["gas"]["data"][0].value, 1.0)

def test_material_parameter_parsing(self):
for line in ["M20 1001.80c 1.0 gas=0", "M20 1001.80c 1.0 gas = 0 nlib = 00c"]:
Expand Down Expand Up @@ -99,16 +43,17 @@ def test_material_str(self):
in_str = "M20 1001.80c 0.5 8016.80c 0.4 94239.80c 0.1"
input_card = Input([in_str], BlockType.DATA)
material = Material(input_card)
answers = """MATERIAL: 20 fractions: atom
answers = """\
MATERIAL: 20 fractions: atom
H-1 (80c) 0.5
O-16 (80c) 0.4
Pu-239 (80c) 0.1
"""
output = repr(material)
print(output)
self.assertEqual(output, answers)
assert output == answers
output = str(material)
self.assertEqual(output, "MATERIAL: 20, ['hydrogen', 'oxygen', 'plutonium']")
assert output == "MATERIAL: 20, ['hydrogen', 'oxygen', 'plutonium']"

def test_material_sort(self):
in_str = "M20 1001.80c 0.5 8016.80c 0.5"
Expand Down Expand Up @@ -190,6 +135,46 @@ def test_material_update_format():
assert "8016" in material.format_for_mcnp_input((6, 2, 0))[0]


@pytest.mark.parametrize(
"line, mat_number, is_atom, fractions",
[
("M20 1001.80c 0.5 8016.710nc 0.5", 20, True, [0.5, 0.5]),
("m1 1001 0.33 8016 0.666667", 1, True, [0.33, 0.666667]),
("M20 1001 0.5 8016 0.5", 20, True, [0.5, 0.5]),
("M20 1001.80c -0.5 8016.80c -0.5", 20, False, [0.5, 0.5]),
("M20 1001.80c -0.5 8016.710nc -0.5", 20, False, [0.5, 0.5]),
("M20 1001.80c 0.5 8016.80c 0.5 Gas=1", 20, True, [0.5, 0.5]),
(
"m1 8016.71c 2.6999999-02 8017.71c 9.9999998-01 plib=84p",
1,
True,
[2.6999999e-2, 9.9999998e-01],
),
],
)
def test_material_init(line, mat_number, is_atom, fractions):

input = Input([line], BlockType.DATA)
material = Material(input)
assert material.number == mat_number
assert material.old_number == mat_number
assert material.is_atom_fraction == is_atom
for component, gold in zip(material.material_components.values(), fractions):
assert component.fraction == pytest.approx(gold)
if "gas" in line:
assert material.parameters["gas"]["data"][0].value == pytest.approx(1.0)


@pytest.mark.parametrize(
"line", ["Mfoo", "M-20", "M20 1001.80c foo", "M20 1001.80c 0.5 8016.80c -0.5"]
)
def test_bad_init(line):
# test invalid material number
input = Input([line], BlockType.DATA)
with pytest.raises(MalformedInputError):
Material(input)


class TestIsotope(TestCase):
def test_isotope_init(self):
isotope = Isotope("1001.80c")
Expand Down Expand Up @@ -245,14 +230,18 @@ def test_isotope_library_setter(self):

def test_isotope_str(self):
isotope = Isotope("1001.80c")
self.assertEqual(isotope.mcnp_str(), "1001.80c")
self.assertEqual(str(isotope), " H-1 (80c)")
self.assertEqual(
repr(isotope), "ZAID=1001, Z=1, A=1, element=hydrogen, library=80c"
)
assert isotope.mcnp_str() == "1001.80c"
assert isotope.nuclide_str() == "H-1.80c"
assert repr(isotope) == "Isotope('H-1.80c')"
assert str(isotope) == " H-1 (80c)"
isotope = Isotope("94239.80c")
self.assertEqual(isotope.mcnp_str(), "94239.80c")
self.assertEqual(str(isotope), "Pu-239 (80c)")
assert isotope.nuclide_str() == "Pu-239.80c"
assert isotope.mcnp_str() == "94239.80c"
assert repr(isotope) == "Isotope('Pu-239.80c')"
isotope = Isotope("95642")
assert isotope.nuclide_str() == "Am-242"
assert isotope.mcnp_str() == "95642"
assert repr(isotope) == "Isotope('Am-242')"


class TestThermalScattering(TestCase):
Expand Down

0 comments on commit f0d4cd8

Please sign in to comment.