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

Refactor Python wrapper method la.Vector.norm into function #3103

Closed
nate-sime opened this issue Mar 13, 2024 · 0 comments · Fixed by #3108
Closed

Refactor Python wrapper method la.Vector.norm into function #3103

nate-sime opened this issue Mar 13, 2024 · 0 comments · Fixed by #3108
Labels
enhancement New feature or request

Comments

@nate-sime
Copy link
Contributor

Describe new/missing feature

I would like to move the Python wrapper method la.Vector.norm

https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/wrappers/la.cpp#L51-L55

to a function defined in the declare_functions function

https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/wrappers/la.cpp#L179

To better match the C++ structure from the Python interface the norm method

https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/la.py#L261-L270

would be refactored into a standalone function of the form

def norm(x: Vector, type: _cpp.la.Norm = _cpp.la.Norm.l2) -> np.floating:
    ...

This would further permit declaring la.Vectors containing int32_t, int64_t etc, which is very useful for, e.g., mesh associated data. E.g:

from mpi4py import MPI
import numpy as np
import dolfinx

def pprint(*msg):
    print(f"[{MPI.COMM_WORLD.rank}]: {', '.join(map(str, msg))}", flush=True)

Nx, Ny = 2, 2
mesh = dolfinx.mesh.create_rectangle(
    MPI.COMM_WORLD, [[0.0, 0.0], [1.0, 1.0]], [Nx, Ny],
    cell_type=dolfinx.mesh.CellType.quadrilateral)

im_v = mesh.topology.index_map(0)
v = dolfinx.cpp.la.Vector_int32(im_v, 1)
v.array[:im_v.size_local] = np.arange(*im_v.local_range)
pprint("before:", v.array)
v.scatter_forward()
pprint("after:", v.array)

yields

[0]: before:, [0 1 2 3 0 0 0 0 0]
[1]: before:, [4 5 6 7 8 0 0 0 0]
[1]: after:, [4 5 6 7 8 2 0 1 3]
[0]: after:, [0 1 2 3 5 7 4 6 8]

Suggested user interface

No response

@nate-sime nate-sime added the enhancement New feature or request label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant