-
Notifications
You must be signed in to change notification settings - Fork 3
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
ValueError second time .run() is called #12
Comments
Yes. This is not a bug but a design choice (which might be a bad one :-) but at least it was a conscious one). The API is more similar to a class based API than a function based API. The solver has a state defined by N ( When the user requires a
Is this choice not ergonomic in your application? |
Thanks for the explanation @hgrecco - I see that this is actually mentioned in the docs:
Regarding your question:
It's not a big deal, I just had to RTFM :) I was thinking if I could propose an improvement to the API or the error message, but now that I understand what Closing! |
I just started using nbkode and I encountered the same ValueError message which leads me to here :D By reading the upper posts(and the documents), I understand that this is a design choice, not a bug. it is clear. But my question is: Does that mean I have to compile the solver every-each time if I change the initial conditions y0 (or input params)? Say, put a scenario like this: Since I have very limited knowledge of numba, forgive me if this question is a "naive" one :) BTW, nbkode is really great! it is easy to use, flexible as scipy does, and got some 200x runtime speedup in my test, and that is literally freaking me out! It really did help a lot. Thanks to @hgrecco and @maurosilber and all the contributors. |
If you need to change the RHS function each time, yes (at least, until some of the issues linked in #5 are addressed in numba). But if it is the same function, and only the initial conditions Note that you need to
Here is a minimal example: import contextlib
import time
import numba
import nbkode
@contextlib.contextmanager
def timeit(message):
start = time.perf_counter()
yield
end = time.perf_counter()
total = end - start
print(f"{total:.2e} seconds", message)
@numba.njit # this is important
def func(t, y):
return -y
for i in range(3):
print("Loop", i)
with timeit("Initialize"):
solver = nbkode.RungeKutta23(func, 0, 1)
with timeit("First run"):
solver.run(5)
with timeit("Second run"):
solver.run(10) which prints:
|
@maurosilber Thanks for the example code! import contextlib
import time
import numba
import nbkode
@contextlib.contextmanager
def timeit(message):
start = time.perf_counter()
yield
end = time.perf_counter()
total = end - start
print(f"{total:.2e} seconds", message)
@numba.njit # this is important
def func(t, y, p):
return -y * p
for i in range(3):
print("Loop", i)
with timeit("Initialize"):
solver = nbkode.DOP853(func, 0, 1, params=2)
with timeit("First run"):
solver.run(5)
with timeit("Second run"):
solver.run(10) It prints:
|
Yes, that's what I mentioned in item 2. But, you can do that closure outside the For instance, @numba.njit
def func_with_params(t, y, p):
return -p * y
@numba.njit
def func(t, y):
return func_with_params(t, y, 2) Or, more generally: def param_closure(func, params):
@numba.njit
def rhs(t, y):
return func(t, y, params)
return rhs
rhs = param_closure(func_with_params, 2) |
Oh! Right! It works! Finally, I got what you mean! |
This code:
Has this effect:
This is Python 3.8, nbkode 0.3.
The text was updated successfully, but these errors were encountered: