maroba / findiff

Python package for numerical derivatives and partial differential equations in any number of dimensions.
MIT License
420 stars 60 forks source link

Non-uniform grid (dim >1) #27

Closed cjoana closed 3 years ago

cjoana commented 4 years ago

Hi, great work!

I have been trying the package and I cannot find how to use 'FindDiff' for non-uniforms grids with dimension 2 or higher. I mean, I see that for FinDiff(i_cord, dcord_or_cord, acc=2), the second index is either d_cord (e.g. dx for 0th-cord, if float) or coordinates (eg. x-values, if array). Therefore in a non-uniform grid, this should be a N-dim array with the ith-coordinate of each cell. However, it only accepts a 1D array, allowing non-uniform grid only in 1 dimension.

maroba commented 4 years ago

Hi,

thanks!

The non-linear case accepts the 1D axes instead of the meshed N-dimensional grids. Here is an example, how it works in two dimensions:

import findiff
import numpy as np
import matplotlib.pyplot as plt

x, y = np.logspace(-1, 1, 50), np.logspace(-1, 1, 50)
X, Y = np.meshgrid(x, y, indexing='ij')

f = np.sin(X) * np.sin(Y)
df_dy = np.sin(X) * np.cos(Y)

d_dy = findiff.FinDiff(1, y, acc=6)
df_dy_fd = d_dy(f)
plt.plot(y, df_dy[0, :], '-')
plt.plot(y, df_dy_fd[0, :], 'o')

If you have trouble with that or have more questions, don't hesitate to ask. :-)

cjoana commented 4 years ago

Yes, sure. But here is an uniform grid. The issue is when the gridding is non-uniform, there the 1D array is not enough.

maroba commented 4 years ago

What do you mean? The example above is a non-uniform grid (logspace, not linspace). Can you give an example of your issue in code?

cjoana commented 3 years ago

Hi, sorry for the late reply. Ok, I see what you mean.

What I was meaning by a non-uniform grid is something like adaptative mesh refinement (AMR) grid. Something like this illustration: Selection_181

There you would need to insert the whole grid of coordinates to derive the individual "dx" at each cell. Either this or perform the derivatives per AMR levels (but this then is specific to AMR like non-uniform grids).

maroba commented 3 years ago

Ah, I see. I'm sorry, the non-uniform grid in findiff so far is only very basic. It is actually a cartesian product of one-dimensional non-uniform grids, which was sufficient for my needs so far. AMR is more advanced. That would something for the future to implement.