Set function values in parallel (dolfin-x)

Hello,

I know this has been asked - and solved - before in dolfin (i.e. https://fenicsproject.org/qa/11448/assign-values-to-function-in-parallel), however I haven’t figured out how to do this in the recent dolfin-x version as there is no HDF5File(…) read function anymore.

Consider the following MWE. I have a data text file with 3 components per DOF and want to assign these to a vector function living in a CG1 function space.

Problem: Executing this in parallel does not yield the same vector field as in serial.

#!/usr/bin/env python3
from dolfinx import BoxMesh, VectorFunctionSpace, Function
from dolfinx.io import XDMFFile
import dolfinx.cpp.mesh
from mpi4py import MPI
import numpy as np
from numpy import loadtxt
from ufl import sin, cos

comm = MPI.COMM_WORLD

mode = 'parallel' # serial, parallel

mesh = BoxMesh(comm, [np.array([0.0, 0.0, 0.0]),np.array([2.0, 1.0, 1.0])], [5, 5, 5],
               dolfinx.cpp.mesh.CellType.tetrahedron, dolfinx.cpp.mesh.GhostMode.none)

V = VectorFunctionSpace(mesh, ("CG", 1))

# generate some data
filename = 'tmp/data.txt'
fl = open(filename, 'wt')

# one vector per node
for i in range(int(V.dim/3)):
    x, y, z = sin(i), cos(i), 1.0
    fl.write(str(x) + " " + str(y) + " " + str(z) + "\n")
fl.close()

# load data - V.dim x 3 array
data = loadtxt('tmp/data.txt')

# roll out to 3*V.dim x 1 array
data_flat = data.flatten()   

# function to be filled with data
f0 = Function(V, name="fiber")

# set function
f0.vector[:] = data_flat

# write mesh and function
with XDMFFile(comm,'tmp/tmp_'+mode+'.xdmf','w') as outfile:
    #mesh.topology.create_connectivity_all() # Not sure if needed? Does not change output...
    outfile.write_mesh(mesh)
    outfile.write_function(f0)

So I guess in parallel, the DOFs are re-ordered and do not correspond to the ordering of the text file anymore. How can I know how this ordering happens so that I can map between the initial sorting and the new one?

I would appreciate if somebody has a solution to this!

Thanks,
Marc

1 Like