Skip to content

Commit

Permalink
Merge pull request #681 from pybamm-team/issue-570-processed-var-warning
Browse files Browse the repository at this point in the history
Issue 570 processed var warning
  • Loading branch information
valentinsulzer authored Oct 25, 2019
2 parents fd9073d + 2962b1d commit 9d3ea6b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

## Bug fixes

- Add warning if `ProcessedVariable` is called outisde its interpolation range ([#681](https://github.com/pybamm-team/PyBaMM/pull/681))
- Improve the way `ProcessedVariable` objects are created in higher dimensions ([#581](https://github.com/pybamm-team/PyBaMM/pull/581))

# [v0.1.0](https://github.com/pybamm-team/PyBaMM/tree/v0.1.0) - 2019-10-08
Expand Down
2 changes: 1 addition & 1 deletion pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _load_version_int():
__version_int__ = _load_version_int()
__version__ = ".".join([str(x) for x in __version_int__])
if sys.version_info[0] < 3:
del (x) # Before Python3, list comprehension iterators leaked
del x # Before Python3, list comprehension iterators leaked

#
# Expose PyBaMM version
Expand Down
9 changes: 6 additions & 3 deletions pybamm/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#
import logging


def set_logging_level(level):
logger.setLevel(level)


format = (
"%(asctime)s - [%(levelname)s] %(module)s.%(funcName)s(%(lineno)d): "
+ "%(message)s"
Expand All @@ -12,7 +17,5 @@

# Create a custom logger
logger = logging.getLogger(__name__)
set_logging_level("WARNING")


def set_logging_level(level):
logger.setLevel(level)
15 changes: 10 additions & 5 deletions pybamm/processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,24 @@ def initialise_3D_scikit_fem(self):
fill_value=np.nan,
)

def __call__(self, t=None, x=None, r=None, y=None, z=None):
def __call__(self, t=None, x=None, r=None, y=None, z=None, warn=True):
"""
Evaluate the variable at arbitrary t (and x, r, y and/or z), using interpolation
"""
if self.dimensions == 1:
return self._interpolation_function(t)
out = self._interpolation_function(t)
elif self.dimensions == 2:
if t is None:
return self._interpolation_function(y, z)
out = self._interpolation_function(y, z)
else:
return self.call_2D(t, x, r, z)
out = self.call_2D(t, x, r, z)
elif self.dimensions == 3:
return self.call_3D(t, x, r, y, z)
out = self.call_3D(t, x, r, y, z)
if warn is True and np.isnan(out).any():
pybamm.logger.warning(
"Calling variable outside interpolation range (returns 'nan')"
)
return out

def call_2D(self, t, x, r, z):
"Evaluate a 2D variable"
Expand Down
10 changes: 7 additions & 3 deletions pybamm/quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ def plot(self, t):
spatial_scale = self.spatial_scales[spatial_var_name]
self.plots[key][i][j], = ax.plot(
spatial_var_value * spatial_scale,
variable(t, **{spatial_var_name: spatial_var_value}),
variable(
t, **{spatial_var_name: spatial_var_value}, warn=False
),
lw=2,
color=colors[i],
linestyle=linestyles[j],
Expand All @@ -333,7 +335,7 @@ def plot(self, t):
full_t = self.ts[i]
self.plots[key][i][j], = ax.plot(
full_t * self.time_scale,
variable(full_t),
variable(full_t, warn=False),
lw=2,
color=colors[i],
linestyle=linestyles[j],
Expand Down Expand Up @@ -394,7 +396,9 @@ def update(self, val):
for j, variable in enumerate(variable_lists):
plot[i][j].set_ydata(
variable(
t_dimensionless, **{spatial_var_name: spatial_var_value}
t_dimensionless,
**{spatial_var_name: spatial_var_value},
warn=False
)
)
else:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
class TestLogger(unittest.TestCase):
def test_logger(self):
logger = pybamm.logger
self.assertEqual(logger.level, 0)
self.assertEqual(logger.level, 30)
pybamm.set_logging_level("INFO")
self.assertEqual(logger.level, 20)
pybamm.set_logging_level("WARNING")
self.assertEqual(logger.level, 30)
pybamm.set_logging_level("ERROR")
self.assertEqual(logger.level, 40)


if __name__ == "__main__":
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def test_processed_var_1D_interpolation(self):
np.testing.assert_array_equal(processed_eqn(t_sol), t_sol * y_sol[0])
np.testing.assert_array_almost_equal(processed_eqn(0.5), 0.5 * 2.5)

# Suppress warning for this test
pybamm.set_logging_level("ERROR")
np.testing.assert_array_equal(processed_eqn(2), np.nan)
pybamm.set_logging_level("WARNING")

def test_processed_var_2D_interpolation(self):
t = pybamm.t
var = pybamm.Variable("var", domain=["negative electrode", "separator"])
Expand Down

0 comments on commit 9d3ea6b

Please sign in to comment.