-
Notifications
You must be signed in to change notification settings - Fork 148
Use derivimplicit if cnexp solution contains an unimplemented function
#3590
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fc978e
Use `derivimplicit` if `cnexp` provides an unimplemented function
0be6d6c
Add test
956ace0
Add STATE so sympy doesn't raise an error
b18656a
Add usecase test
c54c010
Add explanations for possible ambiguities
4ff6f36
Better comments
9d15879
Merge branch 'master' into jelic/fix_lambertw
cattabiani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
test/nmodl/transpiler/usecases/solve/cnexp_to_derivimplicit.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| NEURON { | ||
| SUFFIX cnexp_to_derivimplicit | ||
| } | ||
|
|
||
| STATE { | ||
| x | ||
| } | ||
|
|
||
| INITIAL { | ||
| x = 42 | ||
| } | ||
|
|
||
| BREAKPOINT { | ||
| SOLVE dX METHOD cnexp | ||
| } | ||
|
|
||
| DERIVATIVE dX { | ||
| x' = -x/(1 + x) | ||
| } |
50 changes: 50 additions & 0 deletions
50
test/nmodl/transpiler/usecases/solve/test_cnexp_to_derivimplicit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from typing import Optional | ||
|
|
||
| from scipy.special import lambertw | ||
| import numpy as np | ||
| from neuron import h, gui | ||
| from neuron.units import ms | ||
|
|
||
|
|
||
| def test_cnexp_to_derivimplicit( | ||
| mech: str, | ||
| rtol: float, | ||
| dt: Optional[float] = None, | ||
| ): | ||
| """ | ||
| Test that NMODL changes the solver from cnexp to derivimplicit if it | ||
| detects the Lambert W function in the solution | ||
| """ | ||
| nseg = 1 | ||
|
|
||
| s = h.Section() | ||
| s.insert(mech) | ||
| s.nseg = nseg | ||
|
|
||
| x_hoc = h.Vector().record(getattr(s(0.5), f"_ref_x_{mech}")) | ||
| t_hoc = h.Vector().record(h._ref_t) | ||
|
|
||
| h.stdinit() | ||
| if dt is not None: | ||
| h.dt = dt * ms | ||
| h.tstop = 5.0 * ms | ||
| h.run() | ||
|
|
||
| x = np.array(x_hoc.as_numpy()) | ||
| t = np.array(t_hoc.as_numpy()) | ||
|
|
||
| # solution to: | ||
| # x'(t) = - x / (x + 1) | ||
| # with x(t=0) = C1 is: | ||
| # x(t) = lambertw(C1 * exp(-t) * exp(C1)) | ||
| x_exact = lambertw(42 * np.exp(-t) * np.exp(42)) | ||
| np.testing.assert_allclose(x, x_exact, rtol=rtol) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_cnexp_to_derivimplicit( | ||
| "cnexp_to_derivimplicit", | ||
| # by trial and error, the derivimplicit solver seems to be accurate | ||
| # down to almost 1e-6, but not quite, hence the seemingly magic number | ||
| rtol=1.1e-6, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.