Giter Club home page Giter Club logo

Comments (4)

maroba avatar maroba commented on September 15, 2024

Hi,

I'm not sure if you mean stencil or matrix representation. Anyways, let's look at both.

First of all, yes it is

x, y = np.linspace(0, 1, 100), np.linspace(0, 1, 100)

or simply

x = y = np.linspace(0, 1, 100)

For the stencil, consider the simplified version with only a 5x5 grid of grid spacing 1:

laplace_2d = FinDiff(0, 1, 2) + FinDiff(1, 1, 2)
stencil = laplace_2d.stencil((5, 5))
print(stencil)  # as of version v0.9.1 (just released) it is just stencil an no longer stencil.data

which yields

{('L', 'L'): {(0, 0): 4.0, (0, 1): -5.0, (0, 2): 4.0, (0, 3): -1.0, (1, 0): -5.0, (2, 0): 4.0, (3, 0): -1.0},
 ('L', 'C'): {(0, -1): 1.0, (0, 1): 1.0, (1, 0): -5.0, (2, 0): 4.0, (3, 0): -1.0},
 ('L', 'H'): {(0, -3): -1.0, (0, -2): 4.0, (0, -1): -5.0, (0, 0): 4.0, (1, 0): -5.0,
              (2, 0): 4.0, (3, 0): -1.0},
 ('C', 'L'): {(-1, 0): 1.0, (0, 1): -5.0, (0, 2): 4.0, (0, 3): -1.0, (1, 0): 1.0},
 ('C', 'C'): {(-1, 0): 1.0, (0, -1): 1.0, (0, 0): -4.0, (0, 1): 1.0, (1, 0): 1.0},
 ('C', 'H'): {(-1, 0): 1.0, (0, -3): -1.0, (0, -2): 4.0, (0, -1): -5.0, (0, 0): 8.881784197001252e-16,
              (1, 0): 1.0},
 ('H', 'L'): {(-3, 0): -1.0, (-2, 0): 4.0, (-1, 0): -5.0, (0, 0): 4.0, (0, 1): -5.0,
              (0, 2): 4.0, (0, 3): -1.0},
 ('H', 'C'): {(-3, 0): -1.0, (-2, 0): 4.0, (-1, 0): -5.0, (0, -1): 1.0, (0, 0): 8.881784197001252e-16,
              (0, 1): 1.0},
 ('H', 'H'): {(-3, 0): -1.0, (-2, 0): 4.0, (-1, 0): -5.0, (0, -3): -1.0, (0, -2): 4.0,
              (0, -1): -5.0, (0, 0): 4.0}}

which looks ok. In the interior of the grid, the (C,C) stencil applies, near the boundary one of the others. L means low index boundary region, H means high index boundary region. The stencil in your case looks strange because it contains 1 / grid_spacing **2 terms, which is just 1 in my example.

If you mean matrix, then this is how you can get it:

laplace_2d = FinDiff(0, dx, 2) + FinDiff(1, dy, 2)
mat = laplace_2d.matrix(u.shape)   #  returns a scipy sparse matrix

# apply matrix representation to u:
mat.dot(u.reshape(-1)).reshape(u.shape)

Regarding the Helmholtz operator in 1D, the following works:

from findiff import *
dx = 1
H = Identity() - FinDiff(0, dx, 2)
print(H.matrix((10,)).toarray())

which gives

[[-1.  5. -4.  1.  0.  0.  0.  0.  0.  0.]
 [-1.  3. -1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0. -1.  3. -1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0. -1.  3. -1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0. -1.  3. -1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0. -1.  3. -1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0. -1.  3. -1.  0.  0.]
 [ 0.  0.  0.  0.  0.  0. -1.  3. -1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0. -1.  3. -1.]
 [ 0.  0.  0.  0.  0.  0.  1. -4.  5. -1.]]

which looks correct. For actual computation you need a reasonable grid spacing, of course.

The stencil method for Helmholtz example really gives an error. I will fix it in the next days
as version 0.9.2.

Edit: What part is a feature request?

from findiff.

maroba avatar maroba commented on September 15, 2024

Ok, I just released v0.9.2 with the necessary fix.

from findiff.

sotakao avatar sotakao commented on September 15, 2024

Thank you very much! I just checked that I can create the stencil for the Helmholtz operator with v0.9.2.
Have a nice day.

from findiff.

maroba avatar maroba commented on September 15, 2024

Thanks for checking. Have a nice day, too!

from findiff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.