Dolfinx_mpc type error

Hello,

I am trying to implement periodic boundary conditions for 3D TGV using dolfinx_mpc. But, I am having some type errors.

My dolfinx and dolfinx_mpc versions are as following:

The code I am using is as following:

from mpi4py import MPI
import numpy as np
import dolfinx
from dolfinx.mesh import create_box
from dolfinx.fem import functionspace
from dolfinx_mpc import MultiPointConstraint

# 1. Create a simple mesh and function space
mesh = create_box(MPI.COMM_WORLD, [[0,0,0], [1,1,1]], [8,8,8])
V = functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))

# 2. Define the domain boundaries
L_min = [0.0, 0.0, 0.0]
L_max = [1.0, 1.0, 1.0]
tol = 1e-6

# 3. Define the correct periodic functions for all 3 dimensions
def periodic_boundary(x):
    return np.isclose(x[0], L_min[0], atol=tol) | np.isclose(x[1], L_min[1], atol=tol) | np.isclose(x[2], L_min[2], atol=tol)

def periodic_relation(x):
    out_x = x.copy()
    mask_x = np.isclose(x[0], L_min[0], atol=tol)
    mask_y = np.isclose(x[1], L_min[1], atol=tol)
    mask_z = np.isclose(x[2], L_min[2], atol=tol)
    out_x[0, mask_x] = L_max[0]
    out_x[1, mask_y] = L_max[1]
    out_x[2, mask_z] = L_max[2]
    return out_x

# 4. Attempt to create the MultiPointConstraint
try:
    print("Attempting to create MultiPointConstraint...")
    mpc = MultiPointConstraint(V)
    mpc.create_periodic_constraint_geometrical(
        V, periodic_boundary, periodic_relation, bcs=[], scale=1.0
    )
    mpc.finalize()
    print("SUCCESS: MultiPointConstraint created and finalized without error.")

except TypeError as e:
    print("\nERROR: The minimal test script failed with the same TypeError.")
    print("This strongly suggests an issue with the software environment, not the code logic.")
    print("\nFull Error Message:")
    # We print the error message itself to confirm it's the same one
    print(e)

except Exception as e:
    print(f"\nAn unexpected error occurred: {e}")

The below is the error message:

Does anyone have similar issues?

Thanks,

I can run your code without any problems using dolfinx_mpc 0.9.1. Maybe consider upgrading?

Thank your for your help!

However, even though I updated the version of dolfinx_mpc to v0.9.2, the same error message occurs.

How did you install DOLFINx_MPC? The above issue usually means that nanobind has been updated between install dolfinx and dolfinx_mpc.

For instance using docker (with apt as installation method):

FROM ubuntu:24.04

RUN apt-get update && apt-get install -y software-properties-common
RUN add-apt-repository ppa:fenics-packages/fenics

RUN apt update && apt install -y python3-pip  python3-dolfinx  python3-dolfinx-mpc  git
WORKDIR /src

RUN git clone --single-branch --branch=v0.9.1 https://github.com/jorgensd/dolfinx_mpc.git
RUN python3 -m pip install pytest --break-system-packages
RUN python3 -m pytest -xvs dolfinx_mpc/python/tests

I get

root@e8b3e2c57ad2:~/shared# python3 test_code.py 
Attempting to create MultiPointConstraint...
SUCCESS: MultiPointConstraint created and finalized without error.

Similarly, using the ghcr.io/jorgensd/dolfinx_mpc:v0.9.3 docker image I also get a working code.

Finally, using

name: fea
channels:
  - conda-forge
  - defaults
dependencies:
  - fenics-dolfinx
  - python=3.13
  - dolfinx_mpc

and creating a conda environment with

conda env create -f environment.yml

also results in a working code.

I installed dolfinx using spack, and dolfinx_mpc using the following command:

git clone https://github.com/jorgensd/dolfinx_mpc.git
cd dolfinx_mpc
git checkout v0.9.2
cmake -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=./install-dir \
  -B build-dir cpp/
ninja -j3 install -C build-dir
python3 -m pip -v install --config-settings=cmake.build-type="Release" --config-settings=cmake.args="-DCMAKE_PREFIX_PATH=/lus/grand/projects/NeuralDE/hjkim/dolfinx_mpc/install-dir" --no-build-isolation ./python -U

I checked that dolfinx and dolfinx_mpc were built using the same compilers (gcc & g++).

But while I’m installing dolfinx_mpc, I installed nanobind and scikit-build-core using pip because they were not installed in my spack environment.

I think nanobind and scikit-build-core couldn’t be seen by pip while installing even though both libraries were installed in my spack environment.

py-nanobind@2.5.0

py-scikit-build-core@0.10.7

That is probably the issue. This is because they are marked as build-dependencies by fenics-dolfinx and not run-time dependencies.

One would have to add the and then concretize with them.

The good news is:

I am currently working on a dolfinx_mpc spack package, which can be tested with:

git clone --branch=py-dolfinx-mpc --single-branch https://github.com/jorgensd/spack-packages.git
spack repo set --destination /path/to/the/above/spack-package-repo builtin

and then installed with

spack add py-dolfinx-mpc@0.9.2 ^fenics-dolfinx+petsc
spack concretize
spack install

and add any additional dependencies

Thanks you @dokken !

After setting the same nanobind and scikit-build-core for dolfinx_mpc, my code is working.

hjkim@x3005c0s37b1n0:/grand/NeuralDE/hjkim/FeniCSx/NS/TGV/test> python TGV_mpc.py -N 4
Attempting to create MultiPointConstraint…
SUCCESS: MultiPointConstraint created and finalized without error.

I will also try to install dolfinx_mpc using spack.

Thanks!

@dokken I haven’t tried yet, but does dolfinx_mpc work with MPI?

Yes, it does work with mpi