12 from adaptivity
import metric_pnorm, metric_ellipse, adapt
13 from pylab
import hold, show, figure, triplot, rand, tricontourf, axis, box
14 from pylab
import plot
as pyplot
15 from numpy
import array, ones
18 set_log_level(WARNING)
19 parameters[
"allow_extrapolation"] =
True
25 mesh = RectangleMesh(-0.5,-0.5,0.5,0.5,1*meshsz,1*meshsz,
"left/right")
29 testsol =
'tanh((' + str(cos(angle)) +
'*x[0]+'+ str(sin(angle)) +
'*x[1])/' + str(float(hd)) +
')'
30 ddtestsol = str(cos(angle)+sin(angle))+
'*2*'+testsol+
'*(1-pow('+testsol+
',2))/'+str(float(hd)**2)
32 testsol2 =
'tanh((' + str(cos(angle)) +
'*x[1]-'+ str(sin(angle)) +
'*x[0])/' + str(float(hd)) +
')'
33 ddtestsol2 = str(cos(angle)-sin(angle))+
'*2*'+testsol2+
'*(1-pow('+testsol2+
',2))/'+str(float(hd)**2)
35 return x[0]-mesh.coordinates()[:,0].min() < DOLFIN_EPS
or mesh.coordinates()[:,0].max()-x[0] < DOLFIN_EPS \
36 or mesh.coordinates()[:,1].min()+0.5 < DOLFIN_EPS
or mesh.coordinates()[:,1].max()-x[1] < DOLFIN_EPS
38 for iii
in range(Nadapt):
39 V = FunctionSpace(mesh,
"CG" ,2); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V); u2 = Function(V)
40 bc = DirichletBC(V, Expression(testsol), boundary)
41 bc2 = DirichletBC(V, Expression(testsol2), boundary)
42 R = interpolate(Expression(ddtestsol),V)
43 R2 = interpolate(Expression(ddtestsol2),V)
44 a = inner(grad(dis), grad(dus))*dx
48 solve(a == L2, u2, bc2)
50 H =
metric_pnorm(u , eta, max_edge_length=1., max_edge_ratio=50);
51 H2 =
metric_pnorm(u2, eta, max_edge_length=1., max_edge_ratio=50);
52 H3 =
metric_ellipse(H,H2); Mp3 = project(H3, TensorFunctionSpace(mesh,
"CG", 1))
53 print(
"H11: %0.0f, H22: %0.0f, V: %0.0f,E: %0.0f" % (assemble(abs(H3[0,0])*dx),assemble(abs(H3[1,1])*dx),mesh.num_vertices(),mesh.num_cells()))
60 print(
"total time was %0.1fs" % (time()-startTime))
63 figure(1); triplot(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells()); axis(
'equal'); axis(
'off'); box(
'off')
66 figure(4); testf = interpolate(u2,FunctionSpace(mesh,
'CG',1))
67 vtx2dof = vertex_to_dof_map(FunctionSpace(mesh,
"CG" ,1))
68 zz = testf.vector().array()[vtx2dof]; zz[zz==1] -= 1e-16
69 tricontourf(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),zz,100)
72 if __name__==
"__main__":