Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP/discuss] More strict check that equation is conditionally linear #1143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions brian2/stateupdaters/exponential_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ def get_conditionally_linear_system(eqs, variables=None):
s_expr = sp.collect(s_expr,
var, evaluate=False)

if len(s_expr) > 2 or var not in s_expr:
if (len(s_expr) > 2 or
var not in s_expr or
s_expr.get(sp.S.One, sp.S.Zero).has(var)
):
raise ValueError(('The expression "%s", defining the variable %s, '
'could not be separated into linear components') %
(expr, name))
coefficients[name] = (s_expr[var], s_expr.get(1, 0))
coefficients[name] = (s_expr[var], s_expr.get(sp.S.One, sp.S.Zero))
else:
coefficients[name] = (0, s_expr)
coefficients[name] = (sp.S.Zero, s_expr)

return coefficients

Expand Down
11 changes: 11 additions & 0 deletions brian2/tests/test_stateupdaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,17 @@ def test_priority():

check_integration(eqs, variables, can_integrate)

# Equation that both linearly and non-linearly depends on the variable,
# and is therefore not "conditionally linear". The exponential Euler updater
# should therefore decline to integrate the equations.
param = 1
eqs = Equations('''dv/dt = (-v + exp(-v) + 1.0)/tau : 1''')
updater(eqs, variables) # should not raise an error
can_integrate = {linear: False, euler: True, exponential_euler: False,
rk2: True, rk4: True, heun: True, milstein: True}

check_integration(eqs, variables, can_integrate)

# Equations resulting in complex linear solution for older versions of sympy
eqs = Equations('''dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
dge/dt = -ge/(5*ms) : volt
Expand Down