FEniCS vs FEniCSx

Hello everyone.
I have just starting to learn how to solve problems with FEniCS and I am wondering if the code written for FEniCS (whitout x) will be useful when the new version FEniCSx will be ready. If I’m not wrong, a code “written in FEniCS” don’t work with the develoment version of the software. It is neccesary to change the code. For example:
FEniCS:
mesh = IntervalMesh(50, 0, 1)
FEniCSx:
mesh = dolfinx.generation.IntervalMesh(MPI.COMM_WORLD, 50, [0, 1])

At this moment, as is normal, the documentation for FEniCS is much more rich than for FEniCSx. It is dificult for me to learn how things should be done in FEniCSx using the FEniCS examples.
It puzzles me that at fenicsproject.org it is annuce FEniCSx 0.3.0 but all in the web is about FEniCS. It seems that I’m missing something.

Should I try to write my code in FEniCSx to avoid it be obsolet in a close future?
Thanks in advance for your feedback.

If you want to learn about how to write code for FEniCSx, you can check out my tutorial at: The FEniCSx tutorial — FEniCSx tutorial

The old FEniCS was built up over 15 years, and naturally has more documentation. Some of the reasons for the new version is listed at: https://fenicsproject.org/roadmap/

Thank you very much for the tutorial, it’s really useful.

Thank you for providing these excellent tutorials.
There is still need in a tutorial where multiple variable problem would be solved monolithically (e.g. Navier-Stockes or thermo-elasticity, etc). This kind of tutorials is there for FEniCS but they were never redone for FEniCSx. For example see 9. Mixed formulation for Poisson equation — FEniCS Project
Thank you in advance.

It is quite trivial to port these demos to DOLFINx, as you have demos for mixed elements: dolfinx/demo_stokes.py at main · FEniCS/dolfinx · GitHub

import dolfinx
from mpi4py import MPI
import ufl
import numpy as np

# Create mesh
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 25, 25)

# Define function spaces and mixed (product) space
BDM = ufl.FiniteElement("BDM", mesh.ufl_cell(), 1)
DG = ufl.FiniteElement("DG", mesh.ufl_cell(), 0)
el  = ufl.MixedElement([BDM ,DG])
W = dolfinx.fem.FunctionSpace(mesh, el)

# Define trial and test functions
(sigma, u) = ufl.TrialFunctions(W)
(tau, v) = ufl.TestFunctions(W)
# Define source function
x = ufl.SpatialCoordinate(mesh)
f = 10*ufl.exp(-((x[0] - 0.5)**2 + (x[1] - 0.5)**2) / 0.02)

# Define variational form
a = (ufl.dot(sigma, tau) + ufl.div(tau)*u + ufl.div(sigma)*v)*ufl.dx
L = - f*v*ufl.dx

# Define function G such that G \cdot n = g
def bottom_boundary(x):
    return np.isclose(x[1], 0)

def top_boundary(x): 
    return np.isclose(x[1], 1)

def G(x):
    return np.sin(5*x[0])

def g(x, n):
    return (np.sin(5*x[0])*n[0], np.sin(5*x[0])*n[1])
W0, _ = W.sub(0).collapse()
w_bc = dolfinx.fem.Function(W0)
top_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim-1, top_boundary)
top_cells = dolfinx.mesh.compute_incident_entities(mesh, top_facets, mesh.topology.dim-1, mesh.topology.dim)
w_bc.interpolate(lambda x: g(x, (0,1)), top_cells)
bottom_facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim-1, bottom_boundary)
bottom_cells = dolfinx.mesh.compute_incident_entities(mesh, bottom_facets, mesh.topology.dim-1, mesh.topology.dim)
w_bc.interpolate(lambda x: g(x, (0, -1)), bottom_cells)
w_bc.x.scatter_forward()


bc_dofs = dolfinx.fem.locate_dofs_topological((W.sub(0), W0), mesh.topology.dim-1, np.hstack([top_facets, bottom_facets]))
bc = dolfinx.fem.dirichletbc(w_bc, bc_dofs, W.sub(0))

# Compute solution
w = dolfinx.fem.Function(W)
solver = dolfinx.fem.petsc.LinearProblem(a, L, bcs=[bc], u=w)
solver.solve()

(sigma, u) = w.split()

BDM_out = dolfinx.fem.VectorFunctionSpace(mesh, ("DG", 1))
sigma_out = dolfinx.fem.Function(BDM_out)
sigma_out.interpolate(sigma)
with dolfinx.io.VTXWriter(mesh.comm, "sigma.bp", [sigma_out]) as vtx:
    vtx.write(0)

with dolfinx.io.XDMFFile(mesh.comm, "u.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_function(u)

yielding the solutions

2 Likes

That’s perfect. Thank you.

Hello, regarding this example on mixed Poisson formulation, how can one generate similar plots? I’m stuck since the vtk mesh can only be created for Lagrange spaces

In legacy dolfin, everything is interpolated into ad CG 1 space when it is plotted.

What you should do in DOLFINx is to interpolate the solution into an appropriate DG space, and visualize that solution.