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*x + 2*x*x', 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)
L = f*v*dx

# Compute solution
usol = Function(V)

problem = LinearVariationalProblem(a, L, usol, bc)
solver = LinearVariationalSolver(problem)
solver.solve()


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 When we are modularizing the code we have to keep in consideration the fact that any kind of further manipulation of the variables will require the original definition of the function space. Thus, when we put the above code into some python function we have to return all the variables that we wish to use outside the function.