%matplotlib inline
from dolfin import *
# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, "Lagrange", 1)
# Define Dirichlet boundary (x = 0 or x = 1)
def boundary(x):
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS
from dolfin.common import plotting
# Define boundary condition
u0 = Constant(0.0)
bc = DirichletBC(V, u0, boundary)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
g = Expression("sin(5*x[0])", degree=2)
a = inner(grad(u), grad(v))*dx
L = f*v*dx + g*v*ds
# Compute solution
w = Function(V)
solve(a == L, w, bc)
import matplotlib.pyplot as plt
fig = plt.figure()
plot(w)
<matplotlib.tri.tricontour.TriContourSet at 0x7f01a9649780>
_.collections
<a list of 35 mcoll.PathCollection objects>
from matplotlib import animation
def animate(i):
print("frame %i" % i)
f = Expression(f"10*exp(-(pow(x[0] - 0.05 * {i}, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
L = f*v*dx + g*v*ds
# Compute solution
u = Function(V)
solve(a == L, u, bc)
plt.title("x-%.2f" % (.05 * i))
return plot(u).collections
# construct the animation
fig = plt.gcf()
anim = animation.FuncAnimation(fig, animate,
frames=20, interval=50)
# generate the HTML animation
anim.save("anim.html", writer="html")
plt.close(fig)
# display the resulting HTML
from IPython.display import HTML, display
with open("anim.html") as f:
display(HTML(f.read()))
frame 0 frame 0 frame 1 frame 2 frame 3 frame 4 frame 5 frame 6 frame 7 frame 8 frame 9 frame 10 frame 11 frame 12 frame 13 frame 14 frame 15 frame 16 frame 17 frame 18 frame 19