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

Make "sparse" solver check if equations are linear. #860

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ctrl-z-9000-times
Copy link
Contributor

If the system is linear, then newtons method always converges
in exactly one iteration. When using the sparse solver on
linear systems omit the newtons iteration and solve directly.

This should make the resulting code run marginally faster by
skipping the check for convergence. Currently the check for
convergence is implemented as "error = sqrt(|F|^2)".

nmodl/ode.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

Merging #860 (d868bd3) into master (cde5dbf) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #860      +/-   ##
==========================================
- Coverage   61.41%   61.40%   -0.02%     
==========================================
  Files         208      208              
  Lines       30048    30081      +33     
==========================================
+ Hits        18455    18472      +17     
- Misses      11593    11609      +16     
Impacted Files Coverage Δ
src/pybind/pyembed.hpp 88.23% <ø> (ø)
test/unit/visitor/sympy_solver.cpp 98.98% <ø> (ø)
src/pybind/wrapper.cpp 100.00% <100.00%> (ø)
src/visitors/sympy_solver_visitor.cpp 91.25% <100.00%> (+0.06%) ⬆️
src/codegen/codegen_compatibility_visitor.cpp 46.66% <0.00%> (-1.95%) ⬇️
src/codegen/codegen_compatibility_visitor.hpp 15.38% <0.00%> (-1.29%) ⬇️
src/visitors/sympy_replace_solutions_visitor.cpp 92.17% <0.00%> (-0.41%) ⬇️
src/codegen/codegen_ispc_visitor.cpp 19.26% <0.00%> (-0.17%) ⬇️
...anguage/templates/visitors/checkparent_visitor.cpp 91.64% <0.00%> (-0.11%) ⬇️
src/codegen/codegen_acc_visitor.cpp 0.52% <0.00%> (-0.01%) ⬇️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cde5dbf...d868bd3. Read the comment docs.

If the system is linear, then newtons method always converges
in exactly one iteration. When using the sparse solver on
linear systems omit the newtons iteration and solve directly.

This should make the resulting code run marginally faster by
skipping the check for convergence. Currently the check for
convergence is implemented as "error = sqrt(|F|^2)".
nmodl_eigen_f[0] = 5.0-nmodl_eigen_x[0]
nmodl_eigen_j[0] = -1.0
nmodl_eigen_f[0] = 5.0-pow(nmodl_eigen_x[0], 3)
nmodl_eigen_j[0] = -3.0 * pow(nmodl_eigen_x[0], 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will wait for comments from @cattabiani but just from code generation perspective I want to point out that use of pow is expensive and typically we want to rewrite/simplify x^2 to x*x. IIRC, we use somewhere sympy to do this simplification.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, sorry, I missread the comment. IIRC pow was used because we are not sure, in general, that the exponent is a positive integer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few points about replacing pows:

  • it brings some marginal improvements (I believe) if it remains robust and sound for all the mod files. It could be more error-prone from the user perspective. I let @alkino verify this
  • pow has always being used, since I stated working on nmodl. I did not know about this Liam adversity to the function. I do not think it is really related to this pr
  • the way to change it (that I see atm) would be to partially remove pow from the list of available functions in ode.py or a replacement afterward. Both ways look very cumbersome, hacky and maybe error-prone for not a lot of improvement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason "pow" appears in this PR is because I changed the testcase from solving a linear equation "x=5" to solving a non-linear equation "x^3=5".

I changed that test because it is a test for the nonlinear solver, and I wanted it to continue testing the same code paths.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding info from @cattabiani via Slack:

the relevant ticket and mod file that needs to be be checked with this PR is: #801

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I ran this PR on the file cdp5.mod and it works.
It successfully runs and the resulting C++ file correctly uses newtons method.

Here is the file in question:
https://senselab.med.yale.edu/ModelDB/showmodel?model=239421&file=/Purkinjecell_2017/mod_files/cdp5.mod#tabs-2

nmodl/ode.py Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants