jorgensd / dolfinx_mpc

Extension for dolfinx to handle multi-point constraints.
https://jorgensd.github.io/dolfinx_mpc/
MIT License
30 stars 12 forks source link

TypeError when running periodic demos with dolfinx/dolfinx_mpc v0.8.0 #131

Open bnherrerac opened 3 days ago

bnherrerac commented 3 days ago

Hello, I am unable to run the periodic demos using dolfinx 0.8.0 and dolfinx_mpc 0.8.0, with Python 3.12.6. The demos I can't run are the following:

In both cases I get a similar error, here's the error for demo_periodic_geometrical.py:

  File "/home/bnherrerac/p2/demos/demo_periodic_geometrical.py", line 81, in <module>
    mpc.create_periodic_constraint_geometrical(V, periodic_boundary, periodic_relation, bcs)
  File "/home/bnherrerac/anaconda3/envs/fenicsx-env/lib/python3.12/site-packages/dolfinx_mpc/multipointconstraint.py", line 286, in create_periodic_constraint_geometrical
    mpc_data = dolfinx_mpc.cpp.mpc.create_periodic_constraint_geometrical(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: create_periodic_constraint_geometrical(): incompatible function arguments. The following argument types are supported:
    1. create_periodic_constraint_geometrical(V: dolfinx::fem::FunctionSpace<float>, indicator: Callable[[numpy.ndarray[dtype=float32, writable=False, shape=(*, *), ]], ndarray[dtype=bool, shape=(*), order='C']], relation: Callable[[numpy.ndarray[dtype=float32, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float32, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<float, float>], scale: float, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_float
    2. create_periodic_constraint_geometrical(V: dolfinx::fem::FunctionSpace<float>, indicator: Callable[[numpy.ndarray[dtype=float32, writable=False, shape=(*, *), ]], ndarray[dtype=bool, shape=(*), order='C']], relation: Callable[[numpy.ndarray[dtype=float32, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float32, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<std::complex<float>, float>], scale: complex, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_complex_float
    3. create_periodic_constraint_geometrical(V: dolfinx::fem::FunctionSpace<double>, indicator: Callable[[numpy.ndarray[dtype=float64, writable=False, shape=(*, *), ]], ndarray[dtype=bool, shape=(*), order='C']], relation: Callable[[numpy.ndarray[dtype=float64, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float64, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<double, double>], scale: float, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_double
    4. create_periodic_constraint_geometrical(V: dolfinx::fem::FunctionSpace<double>, indicator: Callable[[numpy.ndarray[dtype=float64, writable=False, shape=(*, *), ]], ndarray[dtype=bool, shape=(*), order='C']], relation: Callable[[numpy.ndarray[dtype=float64, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float64, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<std::complex<double>, double>], scale: complex, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_complex_double

Invoked with types: dolfinx.cpp.fem.FunctionSpace_float64, function, function, list, float, bool 

And the error for demo_periodic3d_topological.py:

Traceback (most recent call last):
  File "/home/bnherrerac/p2/demos/demo_periodic3d_topological.py", line 187, in <module>
    demo_periodic3D(celltype)
  File "/home/bnherrerac/p2/demos/demo_periodic3d_topological.py", line 97, in demo_periodic3D
    mpc.create_periodic_constraint_topological(V.sub(0), mt, 2, periodic_relation, bcs, default_scalar_type(1))
  File "/home/bnherrerac/anaconda3/envs/fenicsx-env/lib/python3.12/site-packages/dolfinx_mpc/multipointconstraint.py", line 254, in create_periodic_constraint_topological
    mpc_data = dolfinx_mpc.cpp.mpc.create_periodic_constraint_topological(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: create_periodic_constraint_topological(): incompatible function arguments. The following argument types are supported:
    1. create_periodic_constraint_topological(V: dolfinx::fem::FunctionSpace<float>, meshtags: dolfinx::mesh::MeshTags<int>, dim: int, relation: Callable[[numpy.ndarray[dtype=float32, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float32, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<float, float>], scale: float, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_float
    2. create_periodic_constraint_topological(V: dolfinx::fem::FunctionSpace<float>, meshtags: dolfinx::mesh::MeshTags<int>, dim: int, relation: Callable[[numpy.ndarray[dtype=float32, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float32, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<std::complex<float>, float>], scale: complex, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_complex_float
    3. create_periodic_constraint_topological(V: dolfinx::fem::FunctionSpace<double>, meshtags: dolfinx::mesh::MeshTags<int>, dim: int, relation: Callable[[numpy.ndarray[dtype=float64, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float64, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<double, double>], scale: float, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_double
    4. create_periodic_constraint_topological(V: dolfinx::fem::FunctionSpace<double>, meshtags: dolfinx::mesh::MeshTags<int>, dim: int, relation: Callable[[numpy.ndarray[dtype=float64, writable=False, shape=(*, *), ]], numpy.ndarray[dtype=float64, shape=(*, *), ]], bcs: list[dolfinx::fem::DirichletBC<std::complex<double>, double>], scale: complex, collapse: bool) -> dolfinx_mpc.cpp.mpc.mpc_data_complex_double

Invoked with types: dolfinx.cpp.fem.FunctionSpace_float64, dolfinx.cpp.mesh.MeshTags_int32, int, function, list, float, bool

I installed dolfinx and dolfinx_mpc via conda install, and made sure that both versions are 0.8.0. I am trying to run a similar code, where I impose periodic boundary conditions in opposite faces of an unit cell, but I get stuck when calling create_periodic_constraint_geometrical, as in the demo. If you have any idea how I could get around this, it would be very helpful. Thanks in advance!

jorgensd commented 3 days ago

Could you try with 0.8.1: https://github.com/jorgensd/dolfinx_mpc/tree/v0.8.1 and also give me the conda commands you ran to create your environment ?

bnherrerac commented 2 days ago

Tried with 0.8.1 keeping dolfinx 0.8.0, the same error happened. I rerun these conda commands to create the env and install all the dependencies:

conda create -n fenicsx-env
conda activate fenicsx-env
conda install -c conda-forge fenics-dolfinx mpich pyvista
conda install conda-forge::dolfinx_mpc
conda install conda-forge::pytest
conda install scipy

which leaves me with dolfinx 0.8.0 and dolfinx-mpc 0.8.1. Here is a MWE that reproduces the error after this installation:

import numpy as np
from mpi4py import MPI
from dolfinx import fem, mesh
from dolfinx.common import Timer
from dolfinx_mpc import MultiPointConstraint

dolfinx_mesh = mesh.create_unit_cube(MPI.COMM_WORLD, 10, 10, 10)
V = fem.functionspace(dolfinx_mesh, ("Lagrange", 1, (dolfinx_mesh.geometry.dim, )))

def x_periodic_boundary(x):
    return np.isclose(x[0], 1.0, atol=1e-8)

def x_periodic_relation(x):
    out_x = np.zeros_like(x)
    out_x[0] = 1 - x[0]
    out_x[1] = x[1]
    out_x[2] = x[2]
    return out_x

with Timer("~PERIODIC: Initialize MPC"):
    mpc = MultiPointConstraint(V)
    mpc.create_periodic_constraint_geometrical(V, x_periodic_boundary, x_periodic_relation, bcs=[])
    mpc.finalize()

Thank you for your help!

jorgensd commented 16 hours ago

I can reproduce this. I think this is an issue with an incompatibility with nanobind versions. I will ask @minrk for guidance as he is a wizard at this!

minrk commented 6 hours ago

This is almost certainly an incompatibility in nanobind (likely a compiler version mismatch) that should be pinned but apparently isn't. Can you share the output of conda env export?

minrk commented 6 hours ago

Yup, it's the same nanobind abi pinning issue that I believe I've fixed in dolfinx itself, but didn't realize it extended to mpc as well. The quickest workaround is to add gxx=12 to your list of packages, and it should pick the right compatible version of everything.

When this PR is merged, the latest builds of everything should work together again.