To date, most of the codes that I have designed using FEniCS were based on flat style. Those were all contained in a single file. FEniCS documentation includes an excellent post on how to improve the poison’s solver. I would recommend anyone reading this post first to go and understand that post.

from fenics import *

# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)

# Define boundary condition
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

def boundary(x, on_boundary):
return on_boundary

bc = DirichletBC(V, u_D, boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)

A typical FEniCS code starts with definition of a FunctionSpace and all the other entities that are defined on this space, contains the original definition of the FunctionSpace