11 from adaptivity
import metric_pnorm, logproject, adapt
12 from sympy
import Symbol, diff
13 from sympy
import tanh
as pytanh
14 from sympy
import cos
as pysin
15 from sympy
import sin
as pycos
23 mesh = BoxMesh(-0.5,-0.5,-0.5,0.5,0.5,0.5,meshsz,meshsz,meshsz)
27 sx = Symbol(
'sx'); sy = Symbol(
'sy'); sz = Symbol(
'sz'); width_ = Symbol(
'ww'); aa = Symbol(
'aa'); bb = Symbol(
'bb')
28 testsol = pytanh((sx*pycos(aa)*pysin(bb)+sy*pysin(aa)*pysin(bb)+sz*pycos(bb))/width_)
29 ddtestsol = str(diff(testsol,sx,sx)+diff(testsol,sy,sy)+diff(testsol,sz,sz)).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'sz',
'x[2]')
31 ddtestsol = ddtestsol.replace(
'tanh((x[0]*sin(aa)*cos(bb) + x[1]*cos(aa)*cos(bb) + x[2]*sin(bb))/ww)**2',
'pow(tanh((x[0]*sin(aa)*sin(bb) + x[1]*cos(aa)*sin(bb) + x[2]*cos(bb))/ww),2.)')
32 ddtestsol = ddtestsol.replace(
'cos(aa)**2',
'pow(cos(aa),2.)').replace(
'sin(aa)**2',
'pow(sin(aa),2.)').replace(
'ww**2',
'(ww*ww)').replace(
'cos(bb)**2',
'(cos(bb)*cos(bb))').replace(
'sin(bb)**2',
'(sin(bb)*sin(bb))')
34 ddtestsol = ddtestsol.replace(
'aa',str(angle)).replace(
'ww',str(width)).replace(
'bb',str(angle2))
35 testsol = str(testsol).replace(
'sx',
'x[0]').replace(
'sy',
'x[1]').replace(
'aa',str(angle)).replace(
'ww',str(width)).replace(
'bb',str(angle2)).replace(
'sz',
'x[2]')
36 ddtestsol =
"-("+ddtestsol+
")"
38 return x[0]-mesh.coordinates()[:,0].min() < DOLFIN_EPS
or mesh.coordinates()[:,0].max()-x[0] < DOLFIN_EPS \
39 or mesh.coordinates()[:,1].min() < DOLFIN_EPS
or mesh.coordinates()[:,1].max()-x[1] < DOLFIN_EPS \
40 or mesh.coordinates()[:,2].min() < DOLFIN_EPS
or mesh.coordinates()[:,2].max()-x[2] < DOLFIN_EPS
43 for iii
in range(Nadapt):
44 V = FunctionSpace(mesh,
"CG" ,2); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V)
45 a = inner(grad(dis), grad(dus))*dx
46 L = Expression(ddtestsol)*dus*dx
47 bc = DirichletBC(V, Expression(testsol), boundary)
51 H =
metric_pnorm(u, eta, max_edge_length=2., max_edge_ratio=50, CG1out=
True)
55 L2error = errornorm(Expression(testsol), u, degree_rise=4, norm_type=
'L2')
56 log(INFO+1,
"total (adapt+metric) time was %0.1fs, L2error=%0.0e, nodes: %0.0f" % (time()-startTime,L2error,mesh.num_vertices()))
59 plot(u,interactive=
True)
60 plot(mesh,interactive=
True)
63 if __name__==
"__main__":