15 from adaptivity
import metric_pnorm, adapt, logproject
16 from pylab
import polyfit, hold, show, triplot, tricontourf, colorbar, axis, box, get_cmap, figure, legend, savefig, xlabel, ylabel, title, xlim, ylim
17 from pylab
import loglog
as pyloglog
18 from numpy
import array
20 from sympy
import Symbol, diff
21 from sympy
import sqrt
as pysqrt
22 from numpy
import abs
as pyabs
23 from numpy
import log
as pylog
24 from numpy
import exp
as pyexp2
29 def ellipse_convergence(asp=2,width=1e-2, Nadapt=10, eta_list=0.04*pyexp2(-array(range(15))*pylog(2)/2), \
30 use_adapt=
True, problem=2, outname=
'', CGorderL = [2, 3], noplot=
False, octaveimpl=
False):
32 sx = Symbol(
'sx'); sy = Symbol(
'sy'); width_ = Symbol(
'ww'); asp_ = Symbol(
'a')
33 rrpy = pysqrt(sx*sx/asp_+sy*sy*asp_)
35 stepfunc = 0.5+165./104./width_*(rrpy-0.25)-20./13./width_**3*(rrpy-0.25)**3-102./13./width_**5*(rrpy-0.25)**5+240./13./width_**7*(rrpy-0.25)**7
37 stepfunc = 0.5+15./8./width_*(rrpy-0.25)-5./width_**3*(rrpy-0.25)**3+6./width_**5*(rrpy-0.25)**5
39 stepfunc = 0.5+1.5/width_*(rrpy-0.25)-2/width_**3*(rrpy-0.25)**3
41 ddstepfunc = str(diff(stepfunc,sx,sx)+diff(stepfunc,sy,sy)).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'x[0]**2',
'(x[0]*x[0])').replace(
'x[1]**2',
'(x[1]*x[1])')
42 stepfunc = str(stepfunc).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'x[0]**2',
'(x[0]*x[0])').replace(
'x[1]**2',
'(x[1]*x[1])')
44 stepfunc = stepfunc.replace(
'(a*(x[1]*x[1]) + (x[0]*x[0])/a)**(1/2)',
'sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a)')
45 ddstepfunc = ddstepfunc.replace(
'(a*(x[1]*x[1]) + (x[0]*x[0])/a)**(1/2)',
'sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a)')
46 ddstepfunc = ddstepfunc.replace(
'(a*(x[1]*x[1]) + (x[0]*x[0])/a)**(3/2)',
'pow(a*x[1]*x[1]+x[0]*x[0]/a,1.5)')
47 ddstepfunc = ddstepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**2',
'pow(sqrt(a*x[1]*x[1]+x[0]*x[0]/a) - 0.25,2.)')
48 ddstepfunc = ddstepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**3',
'pow(sqrt(a*x[1]*x[1]+x[0]*x[0]/a) - 0.25,3.)')
49 ddstepfunc = ddstepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**4',
'pow(sqrt(a*x[1]*x[1]+x[0]*x[0]/a) - 0.25,4.)')
50 ddstepfunc = ddstepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**5',
'pow(sqrt(a*x[1]*x[1]+x[0]*x[0]/a) - 0.25,5.)')
51 ddstepfunc = ddstepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**6',
'pow(sqrt(a*x[1]*x[1]+x[0]*x[0]/a) - 0.25,6.)')
52 stepfunc = stepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**3',
'pow(sqrt(a*x[1]*x[1] + x[0]*x[0]/a) - 0.25,3.)')
53 stepfunc = stepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**5',
'pow(sqrt(a*x[1]*x[1] + x[0]*x[0]/a) - 0.25,5.)')
54 stepfunc = stepfunc.replace(
'(sqrt(a*(x[1]*x[1]) + (x[0]*x[0])/a) - 0.25)**7',
'pow(sqrt(a*x[1]*x[1] + x[0]*x[0]/a) - 0.25,7.)')
55 testsol =
'(0.25-ww/2<sqrt(x[0]*x[0]/a+x[1]*x[1]*a) && sqrt(x[0]*x[0]/a+x[1]*x[1]*a) < 0.25+ww/2 ? (' + stepfunc +
') : 0) + (0.25+ww/2<sqrt(x[0]*x[0]/a+x[1]*x[1]*a) ? 1 : 0)'
57 ddtestsol =
'0.25-ww/2<sqrt(x[0]*x[0]/a+x[1]*x[1]*a) && sqrt(x[0]*x[0]/a+x[1]*x[1]*a) < 0.25+ww/2 ? (' + ddstepfunc +
') : 0'
89 ddtestsol = ddtestsol.replace(
'a**2',
'(a*a)').replace(
'ww**2',
'(ww*ww)').replace(
'ww**3',
'pow(ww,3.)').replace(
'ww**4',
'pow(ww,4.)').replace(
'ww**5',
'pow(ww,5.)').replace(
'ww**6',
'pow(ww,6.)').replace(
'ww**7',
'pow(ww,7.)')
90 testsol = testsol.replace(
'ww**2',
'(ww*ww)').replace(
'ww**3',
'pow(ww,3.)').replace(
'ww**5',
'pow(ww,5.)').replace(
'ww**7',
'pow(ww,7.)')
91 ddtestsol = ddtestsol.replace(
'ww',str(width)).replace(
'a',str(asp))
92 testsol = testsol.replace(
'ww',str(width)).replace(
'a',str(asp))
93 ddtestsol =
'-('+ddtestsol+
')'
96 return x[0]+0.5 < DOLFIN_EPS
or 0.5-x[0] < DOLFIN_EPS
or x[1]+0.5 < DOLFIN_EPS
or 0.5-x[1] < DOLFIN_EPS
98 for CGorder
in CGorderL:
104 meshsz = int(round(80*0.005/(eta*(bool(use_adapt)==
False)+0.05*(bool(use_adapt)==
True))))
105 if (
not bool(use_adapt))
and meshsz > 80:
108 mesh = RectangleMesh(-0.0,-0.0,0.5*sqrt(asp),0.5/sqrt(asp),meshsz,meshsz,
"left/right")
110 for iii
in range(Nadapt):
111 V = FunctionSpace(mesh,
"CG", CGorder); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V)
113 R = Expression(ddtestsol)
114 a = inner(grad(dis), grad(dus))*dx
116 bc = DirichletBC(V, Expression(testsol), boundary)
118 if not bool(use_adapt):
123 mesh =
adapt(H, octaveimpl=octaveimpl, debugon=
False)
125 L2error = errornorm(Expression(testsol), u, degree_rise=CGorder+2, norm_type=
'L2')
126 dofs.append(len(u.vector().array()))
127 L2errors.append(L2error)
128 log(INFO+1,
"%1dX ADAPT<->SOLVE complete: DOF=%5d, error=%0.0e" % (Nadapt, dofs[len(dofs)-1], L2error))
132 testf = interpolate(u ,FunctionSpace(mesh,
'CG',1))
133 testfe = interpolate(Expression(testsol),FunctionSpace(mesh,
'CG',1))
134 vtx2dof = vertex_to_dof_map(FunctionSpace(mesh,
"CG" ,1))
135 zz = testf.vector().array()[vtx2dof]; zz[zz==1] -= 1e-16
136 hh=tricontourf(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),zz,100,cmap=get_cmap(
'binary'))
138 axis(
'equal'); axis(
'off'); box(
'off'); xlim([0,0.5*sqrt(asp)]); ylim([0, 0.5/sqrt(asp)]); savefig(
'solution.png',dpi=300)
140 hold(
'on'); triplot(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),color=
'r',linewidth=0.5); hold('off')
141 axis(
'equal'); box(
'off')
142 axis(
'off'); xlim([0,0.5*sqrt(asp)]); ylim([0, 0.5/sqrt(asp)])
143 savefig(outname+
'final_mesh_CG2.png',dpi=300)
146 zz = pyabs(testf.vector().array()-testfe.vector().array())[vtx2dof]; zz[zz==1] -= 1e-16
147 hh=tricontourf(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),zz,100,cmap=get_cmap(
'binary'))
148 colorbar(hh); axis(
'equal'); box(
'off'); title(
'error')
151 pyloglog(dofs,L2errors,
'-b.',linewidth=2,markersize=16); xlabel(
'Degree of freedoms'); ylabel(
'L2 error')
153 dofs = array(dofs); L2errors = array(L2errors)
154 fid = open(
"DOFS_L2errors_CG"+str(CGorder)+outname+
".mpy",
'w')
155 pickle.dump([dofs,L2errors],fid)
159 fid = open(
"DOFS_L2errors_CG2"+outname+
".mpy",
'r')
160 [dofs,L2errors] = pickle.load(fid)
165 I = array(range(len(dofs)-NfitP,len(dofs)))
166 slope,ints = polyfit(pylog(dofs[I]), pylog(L2errors[I]), 1)
168 fid = open(
"DOFS_L2errors_CG2_fit"+outname+
".mpy",
'w')
169 pickle.dump([dofs,L2errors,slope,ints],fid)
173 os.system(
'rm '+outname+
'.lock')
177 fid = open(
"DOFS_L2errors_CG3.mpy",
'r')
178 [dofs_old,L2errors_old] = pickle.load(fid)
180 slope2,ints2 = polyfit(pylog(dofs_old[I]), pylog(L2errors_old[I]), 1)
182 pyloglog(dofs,L2errors,'-b.',dofs_old,L2errors_old,
'--b.',linewidth=2,markersize=16)
183 hold(
'on'); pyloglog(dofs,pyexp2(ints)*dofs**slope,
'-r',dofs_old,pyexp2(ints2)*dofs_old**slope2,
'--r',linewidth=1); hold(
'off')
184 xlabel(
'Degree of freedoms'); ylabel(
'L2 error')
185 legend([
'CG2',
'CG3',
"%0.2f*log(DOFs)" % slope,
"%0.2f*log(DOFs)" % slope2])
190 if __name__==
"__main__":