Skip to content

Update ft03 #56

Description

@OsbertWang

I use FEniCS installed in WSL of Windows 10.
The version is 2019.01.
Then I update demo files in the tutorial.
I change the plot code because WSL cannot use GUI.

"""FEniCS tutorial demo program: Heat equation with Dirichlet conditions.Test problem is chosen to give an exact solution at all nodes of the mesh. u'= Laplace(u) + f in the unit square u = u_D on the boundary u = u_0 at t = 0 u = 1 + x^2 + alpha*y^2 + \beta*t f = beta - 2 - 2*alpha"""from __future__ importprint_functionfromdolfinimport*importnumpyasnpimportmatplotlib.pyplotaspltT=2.0# final timenum_steps=10# number of time stepsdt=T/num_steps# time step sizealpha=3# parameter alphabeta=1.2# parameter beta# Create mesh and define function spacenx=ny=8mesh=UnitSquareMesh(nx, ny)
V=FunctionSpace(mesh, 'P', 1)
# Define boundary conditionu_D=Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t',
degree=2, alpha=alpha, beta=beta, t=0)
defboundary(x, on_boundary):
returnon_boundarybc=DirichletBC(V, u_D, boundary)
# Define initial valueu_n=interpolate(u_D, V)
#u_n = project(u_D, V)# Define variational problemu=TrialFunction(V)
v=TestFunction(V)
f=Constant(beta-2-2*alpha)
F=u*v*dx+dt*dot(grad(u), grad(v))*dx- (u_n+dt*f)*v*dxa, L=lhs(F), rhs(F)
# Time-steppingu=Function(V)
t=0forninrange(num_steps):
# Update current timet+=dtu_D.t=t# Compute solutionsolve(a==L, u, bc)
# Plot solutionplot(u)
plt.savefig('u'+str(n)+'.png')
plt.cla# Compute error at verticesu_e=interpolate(u_D, V)
error=np.abs(u_e.vector().get_local() -u.vector().get_local()).max()
print('t = %.2f: error = %.3g'% (t, error))
# Update previous solutionu_n.assign(u)
# Hold plot# interactive()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions