Skip to content

Commit 738342a

Browse files
authored
Add problems() method to Model class (#91)
The `Model` class gets a new API method `problems()`. All it does is call the `problems()` method of its root node. This gives users an easy way to check if there are any problems reported throughout the model, by testing `if model.problems():` in application code, to then act accordingly.
1 parent afa251d commit 738342a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

mph/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ def modules(self):
246246
"""Returns the names of modules/products required to be licensed."""
247247
return [modules.get(key, key) for key in self.java.getUsedProducts()]
248248

249+
def problems(self):
250+
"""
251+
Returns problems reported by nodes in the model.
252+
253+
See {meth}`Node.problems` on how problems (error/warning
254+
messages and their origin) are returned. First and foremost,
255+
this method lets users check if any problems are reported
256+
throughout the model by testing `if model.problems():` in
257+
application code, to then act accordingly.
258+
"""
259+
return (self/None).problems()
260+
249261
####################################
250262
# Solving #
251263
####################################

tests/test_model.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,21 @@ def test_save():
709709
model.save('model.mph', format='invalid')
710710

711711

712+
def test_problems():
713+
assert not model.problems()
714+
anode = model/'physics'/'electrostatic'/'anode'
715+
anode.property('V0', '+Ua/2')
716+
study = model/'studies'/'static'
717+
try:
718+
study.run()
719+
except Exception:
720+
pass
721+
assert model.problems()
722+
anode.property('V0', '+U/2')
723+
study.run()
724+
assert not model.problems()
725+
726+
712727
def test_features():
713728
with warnings_disabled():
714729
assert 'Laplace equation' in model.features('electrostatic')
@@ -790,6 +805,7 @@ def test_load():
790805
test_datasets()
791806
test_plots()
792807
test_exports()
808+
test_modules()
793809

794810
test_build()
795811
test_mesh()
@@ -815,6 +831,8 @@ def test_load():
815831
test_reset()
816832
test_save()
817833

834+
test_problems()
835+
818836
test_features()
819837
test_toggle()
820838
test_load()

0 commit comments

Comments
 (0)