Issues while running a script in parrallel

Hi there,

I am currently experiencing issues while trying to run the following script in parrallel :
I run it with mpirun -np 4 python3 Scriptname.py
FEniCS is installed through conda via conda-forge channel

from fenics import *
from dolfin import *
from ufl import nabla_div
import os
import time
from mpi4py import MPI

# stuff for paralell processing
comm = MPI.COMM_WORLD
mpi_rank = comm.Get_rank()
mpi_size = comm.Get_size()

# Constitutive parameters
# Stress unit is MPa
E = 62000
nu = 0.24
Sig_y = 7.0
# Convert into lame coefficients
mu = E/(2*(1+nu))
lambda_ = nu*E/((1+nu)*(1-2*nu))
# Compressive displacement
Utop = -0.1

N = 16
h = 1/N
mesh = BoxMesh(Point(0, 0, 0), Point(1, 1, 1), N, N, N)
tol=1E-8

class BoundaryBottom(SubDomain):
    def inside(self, x, on_boundary):
        return x[1] < h+tol
    
    
class BoundaryTop(SubDomain):
    def inside(self, x, on_boundary):
        return  x[1] > 1.0 - (h+tol)
    
# Define subdomains for boundary conditions
sub_domains = MeshFunction("size_t", mesh, mesh.topology().dim())
sub_domains.set_all(0)
boundaryBottom = BoundaryBottom()
boundaryTop = BoundaryTop()
boundaryBottom.mark(sub_domains, 1)
boundaryTop.mark(sub_domains, 2)

# Define Functional spaces V for variational formulation and projecting quantities
# sequential version
V = VectorFunctionSpace(mesh, "CG", 2)
W = FunctionSpace(mesh, "CG", 1)
Ws = TensorFunctionSpace(mesh, "CG", 1)
# functions and BC)
u = TrialFunction(V)
v = TestFunction(V)
bc1= DirichletBC(V, Constant((0,0,0)), boundaryBottom)
bc2= DirichletBC(V.sub(1), Utop, boundaryTop)
bc=[bc1,bc2]
d=u.geometric_dimension()

def epsilon(u):
    return sym(grad(u))

def sigma(u):
    return 2*mu*epsilon(u) + lambda_*nabla_div(u)*Identity(d)

a = inner(sigma(u), epsilon(v))*dx
f= Constant((0,0,0))
L = dot(f,v)*dx
u=Function(V)
solve(a==L, u, bc)


# Post treatment computations
s = sigma(u) - (1./3)*tr(sigma(u))*Identity(d)
von_Mises = project(sqrt (3/2.* inner (s, s) ), W)

# parallel version
# 1) write 1 vtk file per processor
output_file = File("VTK/VM_{}.pvd".format(comm.Get_rank()))
output_file << von_Mises
# 2) assemble into a single vtk

# Create an XDMFFile for saving the assembled results
xdmf_file = XDMFFile("VTK/assembled_VM.xdmf")

# Loop through the parallel VTK files and add them to the XDMF file
for process_rank in range(mpi_size):
    vtk_filename = "VTK/VM_{}.pvd".format(process_rank)
    vtk_file = File(vtk_filename)

    von_Mises_array = vtk_file.read_checkpoint(von_Mises, "dataset")

    # Create a Function with the same function space and assign the data
    von_Mises_data = Function(Ws)  
    von_Mises_data.vector().set_local(von_Mises_array)

    # Write the data to the XDMF file
    xdmf_file.write(von_Mises_data, "dataset")

# Close the XDMF file
xdmf_file.close()
  1. when I open the file with paraview I get an error message showed on the picture attached.

  2. I get the following error message related to assembling the vtkfiles

Invoked with: <dolfin.cpp.io.File object at 0x7ff25ff18670>, <dolfin.cpp.io.XDMFFile object at 0x7ff262603e70>
    vtk_file >> xdmf_file
    ~~~~~~~~~^^~~~~~~~~~~
  File "/home/quentin_digimind/anaconda3/envs/FENICS-ENV/lib/python3.12/site-packages/dolfin/io/__init__.py", line 33, in __rshift__
  File "/home/quentin_digimind/anaconda3/envs/FENICS-ENV/lib/python3.12/site-packages/dolfin/io/__init__.py", line 33, in __rshift__
    self.read(u)
TypeError: read(): incompatible function arguments. The following argument types are supported:
    1. (self: dolfin.cpp.io.File, arg0: dolfin::Parameters) -> None
    2. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.log.Table) -> None
    3. (self: dolfin.cpp.io.File, arg0: dolfin::GenericVector) -> None
    4. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.function.Function) -> None
    5. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshFunctionBool) -> None
    6. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshFunctionInt) -> None
    7. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshFunctionSizet) -> None
    8. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshFunctionDouble) -> None
    9. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshValueCollection_bool) -> None
    10. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshValueCollection_int) -> None
    11. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshValueCollection_sizet) -> None
    12. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.MeshValueCollection_double) -> None
    13. (self: dolfin.cpp.io.File, arg0: dolfin.cpp.mesh.Mesh) -> None

Any Idea about How to fix that ?

Many thanks,

Quentin SCHMID

First of all, please use 3x` encapsulation of your code, ie.

```python
# add code here

```
1 Like

just edited the post. Sorry about that

Your example fails to run (using the latest version of legacy dolfin, with ufl_legacy rather than ufl):

Traceback (most recent call last):
  File "/tests/test.py", line 90, in <module>
    von_Mises_array = vtk_file.read_checkpoint(von_Mises, "dataset")
                      ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dolfin.cpp.io.File' object has no attribute 'read_checkpoint'

This is from

    vtk_file = File(vtk_filename)

    von_Mises_array = vtk_file.read_checkpoint(von_Mises, "dataset")

As I understand it, checkpointing is available only for xdmf files (XDMFFile), not generic Files (and not vtk files).

1 Like

To add to @dparsons excellent answer, one can XDMFFile and HDF5File for checkpointing.
Only XDMFFile can be opened in Paraview.

1 Like