Skip to content

Commit

Permalink
Merge pull request #518 from idaholab/mat_optimize
Browse files Browse the repository at this point in the history
Optimize Material Update Pointers
  • Loading branch information
MicahGale authored Aug 28, 2024
2 parents 884bba7 + 05a9bce commit 5a12a28
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ jobs:
- run: python benchmark/benchmark_big_model.py
name: Benchmark against big model


changelog-test:
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/main'
Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmark_big_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import tracemalloc

FAIL_THRESHOLD = 500
FAIL_THRESHOLD = 30

tracemalloc.start()
start = time.time()
Expand Down
7 changes: 7 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
MontePy Changelog
=================

#Next Version#
--------------

**Performance Improvement**

* Fixed method of linking ``Material`` to ``ThermalScattering`` objects, avoiding a very expensive O(N:sup:`2`) (:issue:`510`).

0.4.0
--------------

Expand Down
2 changes: 2 additions & 0 deletions montepy/data_inputs/data_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def update_pointers(self, data_inputs):
:param data_inputs: a list of the data inputs in the problem
:type data_inputs: list
:returns: True iff this input should be removed from ``problem.data_inputs``
:rtype: bool, None
"""
pass

Expand Down
13 changes: 1 addition & 12 deletions montepy/data_inputs/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,7 @@ def update_pointers(self, data_inputs):
:param data_inputs: a list of the data inputs in the problem
:type data_inputs: list
"""
for input in list(data_inputs):
if isinstance(input, thermal_scattering.ThermalScatteringLaw):
if input.old_number == self.number:
if not self._thermal_scattering:
self._thermal_scattering = input
input._parent_material = self
data_inputs.remove(input)
else:
raise MalformedInputError(
self,
f"Multiple MT inputs were specified for this material: {self.number}.",
)
pass

@staticmethod
def _class_prefix():
Expand Down
31 changes: 26 additions & 5 deletions montepy/data_inputs/thermal_scattering.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,40 @@ def update_pointers(self, data_inputs):
:param data_inputs: a list of the data inputs in the problem
:type data_inputs: list
:returns: True iff this input should be removed from ``problem.data_inputs``
:rtype: bool
"""
# use caching first
if self._problem:
try:
mat = self._problem.materials[self.old_number]
except KeyError:
raise MalformedInputError(
self._input, "MT input is detached from a parent material"
)
# brute force it
found = False
for input in data_inputs:
if isinstance(input, montepy.data_inputs.material.Material):
if input.number == self.old_number:
for data_input in data_inputs:
if isinstance(data_input, montepy.data_inputs.material.Material):
if data_input.number == self.old_number:
mat = data_input
found = True
self._parent_material = input

break
# actually update things
if not found:
raise MalformedInputError(
self._input, "MT input is detached from a parent material"
)

if mat.thermal_scattering:
raise MalformedInputError(
self,
f"Multiple MT inputs were specified for this material: {self.old_number}.",
)
mat.thermal_scattering = self
self._parent_material = mat
return True

def __str__(self):
return f"THERMAL SCATTER: {self.thermal_scattering_laws}"

Expand Down
8 changes: 6 additions & 2 deletions montepy/mcnp_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,11 @@ def handle_error(e):
ParticleTypeNotInCell,
) as e:
handle_error(e)
for input in self._data_inputs:
to_delete = []
for data_index, data_input in enumerate(self._data_inputs):
try:
input.update_pointers(self._data_inputs)
if data_input.update_pointers(self._data_inputs):
to_delete.append(data_index)
except (
BrokenObjectLinkError,
MalformedInputError,
Expand All @@ -343,6 +345,8 @@ def handle_error(e):
) as e:
handle_error(e)
continue
for delete_index in to_delete[::-1]:
del self._data_inputs[delete_index]

def remove_duplicate_surfaces(self, tolerance):
"""Finds duplicate surfaces in the problem, and remove them.
Expand Down
4 changes: 2 additions & 2 deletions prof/dump_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from pstats import SortKey

stats = pstats.Stats("prof/combined.prof")
stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME).print_stats(300, "montepy")
stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME).print_stats(100, "sly")
stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME).print_stats("montepy", 50)
stats.sort_stats(SortKey.CUMULATIVE, SortKey.TIME).print_stats("sly", 20)
4 changes: 2 additions & 2 deletions prof/profile_big_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

stats = pstats.Stats("prof/big.prof")
stats.sort_stats(pstats.SortKey.CUMULATIVE, pstats.SortKey.TIME).print_stats(
100, "montepy"
"montepy", 70
)
stats.sort_stats(pstats.SortKey.CUMULATIVE, pstats.SortKey.TIME).print_stats(100, "sly")
stats.sort_stats(pstats.SortKey.CUMULATIVE, pstats.SortKey.TIME).print_stats("sly", 20)
3 changes: 2 additions & 1 deletion tests/test_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ def test_thermal_scattering_format_mcnp(self):
in_str = "M20 1001.80c 0.5 8016.80c 0.5"
input_card = Input([in_str], BlockType.DATA)
material = Material(input_card)
material.update_pointers([card])
material.thermal_scattering = card
card._parent_material = material
material.thermal_scattering.thermal_scattering_laws = ["grph.20t"]
self.assertEqual(card.format_for_mcnp_input((6, 2, 0)), ["Mt20 grph.20t "])

Expand Down

0 comments on commit 5a12a28

Please sign in to comment.