Conversion from FEniCS (legacy) to FEniCSx

Hello,

I am currently translating a code originally developed in FEniCS (legacy) to FEniCSx.

In the legacy version, I used the following function:

def Heaviside(H_, sigma, subspace_number, sigma_min, sigma_max):

fsub = sigma.sub(subspace_number, deepcopy=True)
tmp = fsub.vector().get_local()
tmp1 = tmp - sigma_min
tmp2 = (tmp1 + np.abs(tmp1)) * 0.5
tmp3 = tmp2 + sigma_min
tmp4 = sigma_max - tmp3
tmp5 = (tmp4 + np.abs(tmp4)) * 0.5
tmp6 = (-1.tmp5) + sigma_max
h = 0.5 - 0.5
np.cos(np.pi*(tmp6 - sigma_min)/(sigma_max - sigma_min))
H_.vector()[:] = h
return H_

In FEniCSx, I’m having trouble with the line fsub.vector().get_local(). When trying to implement it, I couldn’t find a direct equivalent of .get_local in dolfinx. From reading forum discussions, I found references to .vector.getArray() or .vector.getArray(readonly=False), but these only return empty arrays rather than containing the values of my solution sigma.

Thank you, regards.

Use sub.x.petsc_vec.array_r, to get an array where you can access data (only local nodes), similarly you can use .array_w to get a local view you can modify.

If you want to update ghost values, you can use sub.x.array as shown in:
https://scientificcomputing.github.io/mpi-tutorial/notebooks/dolfinx_MPI_tutorial.html#assembling-scalars-vectors-matrices-in-parallel

1 Like