How do I make a jit compiled backward differenced operator? #321
-
I would like to make jit compiled backward and forward gradients, using e.g. make_operator as in Numba-accelerated PDEs. I have read the documentation about make_operator and register_operator, so I may have understood these routines, but the way I currently implement the backward differentiation cannot be jit compiled because it involves ScalarField. Not sure how to work around this. This is what I got so far:
This works, but how to get this jit compiled?
but that gives a Numba problem:
which I understand, I cannot use ScalarField, of course, but how should I then enforce the boundary condition? Is the way I use make_operator and register_operator okay? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The operator returned by Here is an example that registers an operator and then uses it to apply it to a field (after setting BCs) and also to create a numba-compiled function that does the same and can be used in other numba-compiled code: import pde
from pde.grids.operators.cartesian import _make_derivative
# register the backward gradient operator with the Cartesian grid
pde.CartesianGrid.register_operator("grad_backward", lambda grid: _make_derivative(grid, method="backward"))
# create a test grid
grid = pde.UnitGrid([8])
# apply custom operator to a field
field = pde.ScalarField.from_expression(grid, "x**2")
field_grad1 = field._apply_operator("grad_backward", {"value": 0})
field_grad1.plot()
# make a numba compiled operator that sets BCs and applys operator
op = grid.make_operator("grad_backward", {"value": 0})
plt.plot(op(field.data)) |
Beta Was this translation helpful? Give feedback.
The operator returned by
_make_derivative
is already numba-compiled, so this should not be an issue.If you want to register a new operator and later use it with a grid class, you can typically do that during the initialization of your module or script since it only needs to be done once.
Here is an example that registers an operator and then uses it to apply it to a field (after setting BCs) and also to create a numba-compiled function that does the same and can be used in other numba-compiled code: