Skip to content

Commit

Permalink
Fixed off-by-one error of inner argument to evaluate().
Browse files Browse the repository at this point in the history
When selecting inner solutions to be returned by `Model.evaluate()`,
the indices were off by one. This is because Comsol uses 1-based
array indexing whereas Python's is 0-based, and the `inner` indices
were used on the Python side after having Comsol return all solutions
at once (in cases where the evaluation feature does not accept the
indices as an argument).
  • Loading branch information
john-hen committed May 2, 2021
1 parent 8c36e35 commit 204be1b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 6 additions & 2 deletions mph/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,9 @@ def evaluate(self, expression, unit=None, dataset=None,
elif inner == 'last':
results = results[:, -1, :]
else:
results = results[:, inner, :]
if isinstance(inner, list):
inner = array(inner)
results = results[:, inner-1, :]
return results.squeeze()
# Move on if this fails. Seems to not be a global expression then.
except Exception:
Expand Down Expand Up @@ -528,7 +530,9 @@ def evaluate(self, expression, unit=None, dataset=None,
elif inner == 'last':
results = results[:, -1, :]
else:
results = results[:, inner, :]
if isinstance(inner, list):
inner = array(inner)
results = results[:, inner-1, :]
logger.info('Finished retrieving data.')

# Remove the temporary evaluation node we added to the model.
Expand Down
18 changes: 10 additions & 8 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,21 @@ def test_evaluate():
C = model.evaluate(expression, unit, dataset)
assert C[0] == Cf
assert C[-1] == Cl
# To do: This test fails because inner indices are 1-based, not 0-based.
# C = model.evaluate(expression, unit, dataset, inner=[1, 101])
# assert C[0] == Cf
# assert C[1] == Cl
C = model.evaluate(expression, unit, dataset, inner=[1, 101])
assert C[0] == Cf
assert C[1] == Cl
# Test field evaluation of time-dependent solution.
(dataset, expression, unit) = ('time-dependent', 'ec.normD', 'nC/m^2')
Df = model.evaluate(expression, unit, dataset, 'first')
assert abs(Df.max() - 7.2) < 0.1
Dl = model.evaluate(expression, unit, dataset, 'last')
assert abs(Dl.max() - 10.8) < 0.1
D = model.evaluate(expression, unit, dataset)
assert D[0].max() == Df.max()
assert D[-1].max() == Dl.max()
assert (D[0] == Df).all()
assert (D[-1] == Dl).all()
D = model.evaluate(expression, unit, dataset, inner=[1, 101])
assert (D[0] == Df).all()
assert (D[1] == Dl).all()
# Test global evaluation of parameter sweep.
(dataset, expression, unit) = ('parametric sweep', '2*ec.intWe/U^2', 'pF')
(indices, values) = model.outer(dataset)
Expand All @@ -331,8 +333,8 @@ def test_evaluate():
Dl = model.evaluate(expression, unit, dataset, 'last', 2)
assert abs(Dl.max() - 10.8) < 0.1
D = model.evaluate(expression, unit, dataset, outer=2)
assert D[0].max() == Df.max()
assert D[-1].max() == Dl.max()
assert (D[0] == Df).all()
assert (D[-1] == Dl).all()
# Test argument "dataset".
with logging_disabled():
assert model.evaluate('U')
Expand Down

0 comments on commit 204be1b

Please sign in to comment.