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

Simple "Difference" Operator #128

Open
jejjohnson opened this issue Jun 15, 2023 · 1 comment
Open

Simple "Difference" Operator #128

jejjohnson opened this issue Jun 15, 2023 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@jejjohnson
Copy link

jejjohnson commented Jun 15, 2023

Is there a native way to do a finite difference operator on a multidimensional field, i.e. $\partial_x \vec{\boldsymbol{u}}, \partial_y \vec{\boldsymbol{u}}, \partial_z \vec{\boldsymbol{u}}$, ....

Using the current API, I don't see a way to specify the scheme. It could a combination of the stagger option but I could not find a way to do it. There is a gradient operation but many times we just need a simple difference operator where we can choose which axis.

Note: This might be a problem due to the fact that we use convolutional operators for the FD scheme whereas normally I think of slicing.


Demo

I have a demo colab notebook to showcase what I mean. The equation of motion is a 1D problem but if it were 2D then this API would not work.


Proposed Solution

No specific solution but it might be helpful to have a simple API for this as in many models like the Shallow water and Quasi-geostrophic models, we need this because we have a lot of advection terms.

# current API
u_grad = gradient(u)
du_dx = u_grad.replace_params(u_grad.on_grid()[0])

# preferred API
du_dx = difference(u, axis=0)

Another solution is just to write a custom operator for the difference scheme. The colab notebook that I linked before has an example of this. This is also related to issue #127 and #125.

@jejjohnson jejjohnson added enhancement New feature or request question Further information is requested labels Jun 15, 2023
@astanziola
Copy link
Member

I might have misunderstood, but basically what you are looking for is a jacobian operator?

That's in the following sense (I'll write in in 2d for simplicity).
Given a field $u(r) = (u_x(r), u_y(r))$ with $r \in \mathbb R^2$, you are looking for

$$ \nabla u = \begin{pmatrix} \frac{\partial u_x}{\partial x} & \frac{\partial u_x}{\partial y} \\ \frac{\partial u_y}{\partial x} & \frac{\partial u_y}{\partial y} \end{pmatrix} $$

If that's the case, we could add a jacobian operator, which is probably useful in general. Here's how it looks for finite differences: https://colab.research.google.com/notebooks/intro.ipynb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants