SLEPc solver with dolfinx

Hello everyone,
I’m currently working on the image dolfinx/dolfinx:latest

I’m trying to solve the classical Helmholtz equation on a simple domain as a 2D square (or 3D cube), but the tutoriel on FEniCSx GitHub about the Helmholtz equation is actually not solving the eigenproblem. Is there somewhere a simple example of the Helmholtz equation solved using SLEPc ? (I’m not talking about complex geometry nor parallelisation). I’ve tried this following code :

V = FunctionSpace(mesh, ("Lagrange", 1))
        
        u = TrialFunction(V)
        v = TestFunction(V)
        a = inner(grad(u), grad(v)) * dx
        b = inner(u, v) * dx

        A = assemble_matrix(a, [])

But I get the following issue :
AttributeError: ‘Form’ object has no attribute ‘mesh’

Also, if you have any code or part of code to share, where you succeed to compute eigenvalues from a given problem (not necessarily the Helmholtz equation) , I would be grateful. I’m also interested if you have part of code where you define simple Dirichlet Boundary condition on such an eigenproblem.
It would be the icing on the cake if you have a code which solves an eigenproblem, with Dirichlet BC somewhere AND which works in parallel :slight_smile:

You need to wrap a with dolfinx.fem.Form.

An example of an eigenvalue problem can be found at: https://github.com/jorgensd/dolfinx_mpc/blob/master/python/demos/demo_periodic_gep.py

1 Like

Thanks for the answer. Actually, this code requires the dolfinx_mpc library which is not on the docker image of dolfinx. So I’ve tried to clone the repository of dolfinx_mpc into the dolfinx image and try to install it with :

cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -B build-dir cpp/
ninja -j3 install -C build-dir
pip3 install python/. --upgrade

But I got this error message (this is just a part of the error message):

root@e0e35fdee648:~/dolfinx_mpc# ninja -j3 install -C build-dir
ninja: Entering directory `build-dir'
[1/12] Building CXX object CMakeFiles/dolfinx_mpc.dir/PeriodicConstraint.cpp.o
FAILED: CMakeFiles/dolfinx_mpc.dir/PeriodicConstraint.cpp.o 
/usr/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_CHRONO_DYN_LINK -DBOOST_TIMER_DYN_LINK -DDOLFINX_MPC_VERSION=\"0.4.1.0\" -DDOLFINX_VERSION=\"0.4.2.0\" -DHAS_ADIOS2 -DHAS_PTSCOTCH -DHAS_SLEPC -Ddolfinx_mpc_EXPORTS -I/root/dolfinx_mpc/cpp -I/root/dolfinx_mpc/cpp/dolfinx_mpc -isystem /usr/local/lib/python3.10/dist-packages/ffcx/codegeneration -isystem /usr/local/petsc/linux-gnu-real-32/include -isystem /usr/local/petsc/include -isystem /usr/local/dolfinx-complex/include -isystem /usr/local/slepc/linux-gnu-real-32/include -isystem /usr/local/slepc/include -O3 -DNDEBUG -fPIC -std=c++20 -MD -MT CMakeFiles/dolfinx_mpc.dir/PeriodicConstraint.cpp.o -MF CMakeFiles/dolfinx_mpc.dir/PeriodicConstraint.cpp.o.d -o CMakeFiles/dolfinx_mpc.dir/PeriodicConstraint.cpp.o -c /root/dolfinx_mpc/cpp/PeriodicConstraint.cpp
/root/dolfinx_mpc/cpp/PeriodicConstraint.cpp: In function 'dolfinx_mpc::mpc_data {anonymous}::_create_periodic_condition(const dolfinx::fem::FunctionSpace&, std::span<int>, const std::function<xt::xarray_container<xt::uvector<double, std::allocator<double> >, xt::layout_type::row_major, xt::svector<long unsigned int, 4, std::allocator<long unsigned int>, true>, xt::xtensor_expression_tag>(const xt::xtensor_container<xt::uvector<double, std::allocator<double> >, 2, xt::layout_type::row_major, xt::xtensor_expression_tag>&)>&, double, const std::function<const int(const int&)>&, const dolfinx::fem::FunctionSpace&)':
/root/dolfinx_mpc/cpp/PeriodicConstraint.cpp:163:17: error: 'view' is not a member of 'xt'; did you mean 'xview'?
  163 |           = xt::view(tabulated_basis_values, i, xt::all(), xt::all());
      |                 ^~~~
      |                 xview
/root/dolfinx_mpc/cpp/PeriodicConstraint.cpp:297:13: error: 'row' is not a member of 'xt'; did you mean 'pow'?
  297 |         xt::row(coords_out, insert_location) = xt::row(mapped_T, i);
      |             ^~~
      |             pow
/root/dolfinx_mpc/cpp/PeriodicConstraint.cpp:297:52: error: 'row' is not a member of 'xt'; did you mean 'pow'?
  297 |         xt::row(coords_out, insert_location) = xt::row(mapped_T, i);
      |                                                    ^~~
      |                                                    pow
/root/dolfinx_mpc/cpp/PeriodicConstraint.cpp:379:19: error: 'view' is not a member of 'xt'; did you mean 'xview'?
  379 |             = xt::view(remote_basis_values, j, xt::all(), xt::all());
      |                   ^~~~
      |                   xview
[2/12] Building CXX object CMakeFiles/dolfinx_mpc.dir/SlipConstraint.cpp.o
FAILED: CMakeFiles/dolfinx_mpc.dir/SlipConstraint.cpp.o

This is my dockerfile for creating the dolfinx container :

FROM dolfinx/dolfinx:latest

WORKDIR /usr/src/app

COPY requirements.txt .
RUN pip install -r requirements.txt
RUN apt-get update
RUN apt-get install -y vim

And this is the file I use for running the image :

docker run -ti\
  --name run_test \
  --rm \
  -v /Users/cances/Desktop/stage_cerfacs/STORM/storm_code/src/storm:/root/storm \
  -v $(pwd):/usr/src/app \
  storm_image \
  /bin/bash

dolfinx_mpc is currently broken, as I’ve not had time to add the API changes in: Remove xtensor code from element code (#2278) · FEniCS/dolfinx@290e18c · GitHub
If you use DOLFINx from one commit back: Remove xtensor from VTK helper interface (#2272) · FEniCS/dolfinx@1f3c527 · GitHub
or simply comment out the dolfinx_mpc functions in the code I referred you to, you will be able to solve an eigenvalue problem.

Ok i see, but there is this function assemble_matrix which is used for assembling matrices of the problem. This function comes from the dolfinx_mpc library

You can replace then with dolfinx.fem.petsc.assemble_matrix/vector and remove the mpc as input.