maroba / findiff

Python package for numerical derivatives and partial differential equations in any number of dimensions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accuracy not affecting stencil generation

orebas opened this issue · comments

It seems like when I generate a Stencil, the accuracy is being ignored.
Repro:
x = np.linspace(0, 10, 11)
y = np.linspace(0, 10, 11)
X, Y = np.meshgrid(x, y, indexing='ij')
u = X+Y
d1x = FinDiff(0, x[1] - x[0],1,acc=10)
stencil1 = d1x.stencil(u.shape)
print(stencil1)

Output (this is the same no matter when I change accuracy too):
('L', 'L'): {(0, 0): -1.5, (1, 0): 2.0, (2, 0): -0.5}
('L', 'C'): {(0, 0): -1.5, (1, 0): 2.0, (2, 0): -0.5}
('L', 'H'): {(0, 0): -1.5, (1, 0): 2.0, (2, 0): -0.5}
('C', 'L'): {(-1, 0): -0.5, (0, 0): 0.0, (1, 0): 0.5}
('C', 'C'): {(-1, 0): -0.5, (0, 0): 0.0, (1, 0): 0.5}
('C', 'H'): {(-1, 0): -0.5, (0, 0): 0.0, (1, 0): 0.5}
('H', 'L'): {(-2, 0): 0.5, (-1, 0): -2.0, (0, 0): 1.5}
('H', 'C'): {(-2, 0): 0.5, (-1, 0): -2.0, (0, 0): 1.5}
('H', 'H'): {(-2, 0): 0.5, (-1, 0): -2.0, (0, 0): 1.5}

As a comment, I wanted to let you know that I am finding this package otherwise quite useful, indeed I have not found anything as user-friendly. Thank you.

Thanks for compliment!

Regarding your issue, the acc property doesn't seem to be passed down correctly to the stencil generator. However, until I fix this, you can alternatively attach the acc argument to the stencil call (instead of the FinDiff constructor) and write:

d1x = FinDiff(0, x[1] - x[0], 1)
stencil1 = d1x.stencil(u.shape, acc=4)
print(stencil1)

Output:

('L', 'L'):	{(0, 0): -2.083333333333331, (1, 0): 3.9999999999999916, (2, 0): -2.999999999999989, (3, 0): 1.3333333333333268, (4, 0): -0.24999999999999858}
('L', 'C'):	{(0, 0): -2.083333333333331, (1, 0): 3.9999999999999916, (2, 0): -2.999999999999989, (3, 0): 1.3333333333333268, (4, 0): -0.24999999999999858}
('L', 'H'):	{(0, 0): -2.083333333333331, (1, 0): 3.9999999999999916, (2, 0): -2.999999999999989, (3, 0): 1.3333333333333268, (4, 0): -0.24999999999999858}
('C', 'L'):	{(-2, 0): 0.08333333333333333, (-1, 0): -0.6666666666666666, (0, 0): 0.0, (1, 0): 0.6666666666666666, (2, 0): -0.08333333333333333}
('C', 'C'):	{(-2, 0): 0.08333333333333333, (-1, 0): -0.6666666666666666, (0, 0): 0.0, (1, 0): 0.6666666666666666, (2, 0): -0.08333333333333333}
('C', 'H'):	{(-2, 0): 0.08333333333333333, (-1, 0): -0.6666666666666666, (0, 0): 0.0, (1, 0): 0.6666666666666666, (2, 0): -0.08333333333333333}
('H', 'L'):	{(-4, 0): 0.24999999999999958, (-3, 0): -1.3333333333333313, (-2, 0): 2.9999999999999956, (-1, 0): -3.999999999999996, (0, 0): 2.0833333333333317}
('H', 'C'):	{(-4, 0): 0.24999999999999958, (-3, 0): -1.3333333333333313, (-2, 0): 2.9999999999999956, (-1, 0): -3.999999999999996, (0, 0): 2.0833333333333317}
('H', 'H'):	{(-4, 0): 0.24999999999999958, (-3, 0): -1.3333333333333313, (-2, 0): 2.9999999999999956, (-1, 0): -3.999999999999996, (0, 0): 2.0833333333333317}

Yes, you are right. Stencils for mixed partial derivatives were not yet supported.

In the meantime I have fixed the issue that you reported and also implemented the stencils for mixed partial derivatives. Both is uploaded on github and pypi as version v0.8.6.

By the way, do you explicitly need the stencils or do you just want to apply the derivatives?