"""
FEniCS tutorial demo program:
Poisson equation with Dirichlet conditions.
Simplest example of computation and visualization.

-Laplace(u) = f on a dolphin-shaped domain.
u = u0 on the boundary.
u0 = 0, f = 1.
"""

from dolfin import *

# Create mesh and define function space
mesh = Mesh("dolfin-inside-1.xml.gz")
mesh.refine()
mesh.refine()
print(mesh)
V = FunctionSpace(mesh, 'CG', 1)

# Define boundary conditions
u0 = Constant(mesh, 0.0)

class Boundary(SubDomain):  # define the Dirichlet boundary
    def inside(self, x, on_boundary):
        return on_boundary

u0_boundary = Boundary()
bc = DirichletBC(V, u0, u0_boundary)

# Define variational problem
v = TestFunction(V)
uh = TrialFunction(V)
f = Constant(mesh, 1.0)
a = dot(grad(uh), grad(v))*dx
L = f*v*dx

# Compute solution
problem = VariationalProblem(a, L, bc)
uh = problem.solve()

# Plot solution and mesh
plot(uh)
plot(mesh)

# Dump solution to file in VTK format
file = File('poisson.pvd')
file << uh

# Hold plot
interactive()
