11 from adaptivity
import metric_pnorm, adapt
12 from pylab
import loglog, hold, show, axis, box, figure, tricontourf, triplot, colorbar, savefig, title, get_cmap, xlabel, ylabel
13 from pylab
import plot
as pyplot
14 from numpy
import array
15 from sympy
import Symbol, diff, Subs
16 from sympy
import sin
as pysin
17 from sympy
import atan2
as pyatan
18 from numpy
import log
as pylog
19 from numpy
import exp
as pyexp2
28 sx = Symbol(
'sx'); sy = Symbol(
'sy'); sT = Symbol(
'sT'); st = Symbol(
'st'); spi = Symbol(
'spi')
29 testsol = 0.1*pysin(50*sx+2*spi*st/sT)+pyatan(-0.1,2*sx - pysin(5*sy+2*spi*st/sT))
30 ddtestsol = str(diff(testsol,sx,sx)+diff(testsol,sy,sy)).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'spi',
'pi')
33 ddtestsol = ddtestsol.replace(
"(2*x[0] - sin(5*x[1] + 2*pi*st/sT))**2",
"pow(2*x[0] - sin(5*x[1] + 2*pi*st/sT),2.)")
34 ddtestsol = ddtestsol.replace(
"cos(5*x[1] + 2*pi*st/sT)**2",
"pow(cos(5*x[1] + 2*pi*st/sT),2.)")
35 ddtestsol = ddtestsol.replace(
"(pow(2*x[0] - sin(5*x[1] + 2*pi*st/sT),2.) + 0.01)**2",
"pow((pow(2*x[0] - sin(5*x[1] + 2*pi*st/sT),2.) + 0.01),2.)")
36 ddtestsol = ddtestsol.replace(
"(1 + 0.01/pow(2*x[0] - sin(5*x[1] + 2*pi*st/sT),2.))**2",
"pow(1 + 0.01/pow(2*x[0] - sin(5*x[1] + 2*pi*st/sT),2.),2.)")
37 ddtestsol = ddtestsol.replace(
"(2*x[0] - sin(5*x[1] + 2*pi*st/sT))**5",
"pow(2*x[0] - sin(5*x[1] + 2*pi*st/sT),5.)")
39 ddtestsol = ddtestsol.replace(
'sT',str(period)).replace(
'st',str(timet))
40 testsol = str(testsol).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'spi',
'pi').replace(
'sT',str(period)).replace(
'st',str(timet))
41 ddtestsol =
"-("+ddtestsol+
")"
43 error_list = []; dof_list = []
49 mesh = RectangleMesh(-1.5,-0.25,0.5,0.75,1*meshsz,1*meshsz,
"left/right")
52 return near(x[0],mesh.coordinates()[:,0].min())
or near(x[0],mesh.coordinates()[:,0].max()) \
53 or near(x[1],mesh.coordinates()[:,1].min())
or near(x[1],mesh.coordinates()[:,1].max())
55 for iii
in range(Nadapt):
57 V = FunctionSpace(mesh,
"CG" ,2); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V)
59 a = inner(grad(dis), grad(dus))*dx
60 L = Expression(ddtestsol)*dus*dx
61 bc = DirichletBC(V, Expression(testsol), boundary)
63 soltime = time()-startTime
67 metricTime = time()-startTime
70 TadaptTime = time()-startTime
71 L2error = errornorm(Expression(testsol), u, degree_rise=4, norm_type=
'L2')
72 printstr =
"%5.0f elements, %0.0e L2error, adapt took %0.0f %% of the total time, (%0.0f %% of which was the metric calculation)" \
73 % (mesh.num_cells(),L2error,TadaptTime/(TadaptTime+soltime)*100,metricTime/TadaptTime*100)
74 if len(eta_list) == 1:
77 error_list.append(L2error); dof_list.append(len(u.vector().array()))
81 dof_list = array(dof_list); error_list = array(error_list)
83 loglog(dof_list,error_list,
'.b-',linewidth=2,markersize=16); xlabel(
'Degree of freedoms'); ylabel(
'L2 error')
86 coords = mesh.coordinates().transpose()
91 testf = interpolate(Expression(testsol),FunctionSpace(mesh,
'CG',1))
92 vtx2dof = vertex_to_dof_map(FunctionSpace(mesh,
"CG" ,1))
93 zz = testf.vector().array()[vtx2dof]
94 hh=tricontourf(coords[0],coords[1],mesh.cells(),zz,100)
99 testfe = interpolate(u,FunctionSpace(mesh,
'CG',1))
100 zz = testfe.vector().array()[vtx2dof]
101 hh=tricontourf(coords[0],coords[1],mesh.cells(),zz,100)
106 zz -= testf.vector().array()[vtx2dof]; zz[zz==1] -= 1e-16
107 hh=tricontourf(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),zz,100,cmap=get_cmap(
'binary'))
110 hold(
'on'); triplot(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),color=
'r',linewidth=0.5); hold('off')
111 axis(
'equal'); box(
'off'); title(
'error')
114 if __name__==
"__main__":