Skip to content

Commit

Permalink
Track down where Material.density is called more exactly
Browse files Browse the repository at this point in the history
Using traceback, we can find the thing that called `Material.density`
when the material is attached to a `Component`. This information,
specifically the file name and line number, is used in the warning
message. The file name and line number is used as the label in the
warning so multiple locations that invoke this behavior will show up
as one logged message. But their number of invocations will also be
counted.
  • Loading branch information
drewj-tp committed Sep 19, 2024
1 parent 9a0a490 commit 8d6f624
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions armi/materials/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Most temperatures may be specified in either K or C and the functions will convert for you.
"""
import functools
import traceback
import warnings

from scipy.optimize import fsolve
Expand Down Expand Up @@ -45,11 +46,15 @@ def parentAwareDensityRedirect(f):
@functools.wraps(f)
def inner(self: "Material", *args, **kwargs) -> float:
if self.parent is not None:
stack = traceback.extract_stack()
# last entry is here, second to last is what called this
caller = stack[-2]
label = f"Found call to Material.density in {caller.filename} at line {caller.lineno}"
runLog.warning(
"Calling Material.density when attached to a Component is "
"undesirable and can introduce subtle differences. Prefer "
"directly calling the parent component's density method. "
f"Found on {self=} attached to {self.parent=}",
f"{label}. Calls to Material.density when attached to a component have the potential to induce "
"subtle differences as Component.density and Material.density can diverge.",
single=True,
label=label,
)
return f(self, *args, **kwargs)

Expand Down

0 comments on commit 8d6f624

Please sign in to comment.