findiff icon indicating copy to clipboard operation
findiff copied to clipboard

Higher accuracy derivatives in 3D

Open AbduKT opened this issue 1 year ago • 1 comments

I am trying to calculate higher accuracy derivatives in 3D, however it takes extremely long time. Is there any suggestions to solve this issue

offsets = list(product(range(-4,5), repeat=3))

stencil = Stencil(offsets, partials={(1, 0, 0): 1,}, spacings=(1, 1, 1))

print('Accuracy = ', stencil.accuracy)

print('Grad_x')

for i in stencil.values:
    if abs(stencil.values[i]) > 1e-10:
        print( round(stencil.values[i], 8), ' <--> ', i)

AbduKT avatar Sep 28 '22 02:09 AbduKT

Hi, that's the curse of dimensions. ;-) You are using 9x9x9=729 coefficients, most of which are zero in your case. And the number of Taylor terms that need to be considered grows even faster. In version 0.9.* for higher dimensional cases, it is easier to build a stencil manually from the one-dimensional somewhere along these lines:

from findiff import coefficients

coefs = coefficients(deriv=1, offsets=list(range(-4, 5)), symbolic=True)
coefs

which yields

    {'coefficients': [1/280, -4/105, 1/5, -4/5, 0, 4/5, -1/5, 4/105, -1/280],
          'offsets': [-4, -3, -2, -1, 0, 1, 2, 3, 4],
          'accuracy': 8}

and then

stencil_d_dx_3d = {(off, 0, 0): coef for off, coef in zip(coefs['offsets'], coefs['coefficients'])}

Actually, this is much more convenient as of version 1.0.0 which is supposed to be released soon.

maroba avatar Sep 29 '22 10:09 maroba

Thanks for the reply. But I think for laplacian or deriv = 2, the coefficient for (0,0,0) is not correct. I try to scale this number and use it.

AbduKT avatar Oct 12 '22 10:10 AbduKT