11 from adaptivity
import metric_pnorm, logproject, adapt
12 from pylab
import hold, show, triplot, tricontourf, colorbar, axis, box, rand, get_cmap, title, figure, savefig
13 from pylab
import plot
as pyplot
14 from numpy
import array, ones
16 from sympy
import Symbol, diff
17 from sympy
import tanh
as pytanh
18 from sympy
import cos
as pysin
19 from sympy
import sin
as pycos
28 mesh = RectangleMesh(-0.5,-0.5,0.5,0.5,1*meshsz,1*meshsz,
"left/right")
31 sx = Symbol(
'sx'); sy = Symbol(
'sy'); width_ = Symbol(
'ww'); aa = Symbol(
'aa')
32 testsol = pytanh((sx*pycos(aa)+sy*pysin(aa))/width_)
33 ddtestsol = str(diff(testsol,sx,sx)+diff(testsol,sy,sy)).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]')
35 ddtestsol = ddtestsol.replace(
'tanh((x[0]*sin(aa) + x[1]*cos(aa))/ww)**2',
'pow(tanh((x[0]*sin(aa) + x[1]*cos(aa))/ww),2.)')
36 ddtestsol = ddtestsol.replace(
'cos(aa)**2',
'pow(cos(aa),2.)').replace(
'sin(aa)**2',
'pow(sin(aa),2.)').replace(
'ww**2',
'(ww*ww)')
38 ddtestsol = ddtestsol.replace(
'aa',str(angle)).replace(
'ww',str(width))
39 testsol = str(testsol).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'aa',str(angle)).replace(
'ww',str(width))
40 ddtestsol =
"-("+ddtestsol+
")"
42 return x[0]-mesh.coordinates()[:,0].min() < DOLFIN_EPS
or mesh.coordinates()[:,0].max()-x[0] < DOLFIN_EPS \
43 or mesh.coordinates()[:,1].min()+0.5 < DOLFIN_EPS
or mesh.coordinates()[:,1].max()-x[1] < DOLFIN_EPS
45 for iii
in range(Nadapt):
46 V = FunctionSpace(mesh,
"CG" ,2); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V)
47 a = inner(grad(dis), grad(dus))*dx
48 L = Expression(ddtestsol)*dus*dx
49 bc = DirichletBC(V, Expression(testsol), boundary)
52 H =
metric_pnorm(u, eta, max_edge_length=1., max_edge_ratio=50)
56 L2error = errornorm(Expression(testsol), u, degree_rise=4, norm_type=
'L2')
57 log(INFO+1,
"total (adapt+metric) time was %0.1fs, L2error=%0.0e, nodes: %0.0f" % (time()-startTime,L2error,mesh.num_vertices()))
61 coords = mesh.coordinates().transpose()
66 testf = interpolate(Expression(testsol),FunctionSpace(mesh,
'CG',1))
67 vtx2dof = vertex_to_dof_map(FunctionSpace(mesh,
"CG" ,1))
68 zz = testf.vector().array()[vtx2dof]
69 hh=tricontourf(coords[0],coords[1],mesh.cells(),zz,100)
74 testfe = interpolate(u,FunctionSpace(mesh,
'CG',1))
75 zz = testfe.vector().array()[vtx2dof]
76 hh=tricontourf(coords[0],coords[1],mesh.cells(),zz,100)
81 zz -= testf.vector().array()[vtx2dof]; zz[zz==1] -= 1e-16
82 hh=tricontourf(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),zz,100,cmap=get_cmap(
'binary'))
85 hold(
'on'); triplot(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),color=
'r',linewidth=0.5); hold('off')
86 axis(
'equal'); box(
'off'); title(
'error')
89 if __name__==
"__main__":