PRAgMaTIc  master
mesh_metric2_example.py
Go to the documentation of this file.
1 ### this a testcase for use with DOLFIN/FEniCS and PRAgMaTIc
2 ### by Kristian Ejlebjerg Jensen, January 2014, Imperial College London
3 ### the purpose of the test case is to illustrate the mesh_metric2 function
4 ### first a non structured mesh is generated (inspired from the minimal example),
5 ### then the mesh_metric2 function is called and an element (i) is plotted together
6 ### with the corresponding mesh_metric2 (divided by sqrt(3)) illustrated as the steiner ellipse,
7 ### i.e. the smallest ellipse intersecting the vertices, while still having the same center as the
8 ### triangle. A unit triangle (side lengths equal to one) will give a identity tensor for the
9 ### mesh_metric2 (that is the reason for the sqrt(3) factor)
10 
11 from dolfin import *
12 from pylab import hold, show, axis
13 from pylab import plot as pyplot
14 from numpy import array, linspace, linalg, diag, cross
15 from numpy import cos as pycos
16 from numpy import sin as pysin
17 from numpy import sqrt as pysqrt
18 from numpy import abs as pyabs
19 from adaptivity import metric_pnorm, mesh_metric, adapt, mesh_metric2
20 set_log_level(WARNING)
21 
23  mesh = RectangleMesh(0,0,1,1,20,20)
24  mesh = adapt(interpolate(Constant(((10.,0.),(0.,10.))),TensorFunctionSpace(mesh,'CG',1)))
25  #extract mesh metric
26  MpH = mesh_metric2(mesh)
27  # Plot element i
28  i = 20; t = linspace(0,2*pi,101)
29  ind = MpH.function_space().dofmap().cell_dofs(i)
30  thecell = mesh.cells()[i]
31  centerxy = mesh.coordinates()[thecell,:].mean(0).repeat(3).reshape([2,3]).T
32  cxy = mesh.coordinates()[thecell,:]-centerxy
33  pyplot(cxy[:,0],cxy[:,1],'-b')
34  H = MpH.vector().gather(ind).reshape(2,2);# H = array([[H[1],H[0]],[H[0],H[2]]])
35  #H = MpH.vector().gather(ind); H = array([[H[1],H[0]],[H[0],H[2]]])
36  #H = MpH.vector().array()[ind]; H = array([[H[1],H[0]],[H[0],H[2]]])
37  [v,w] = linalg.eig(H); v /= pysqrt(3) #v = 1/pysqrt(v)/pysqrt(3)
38  elxy = array([pycos(t),pysin(t)]).T.dot(w).dot(diag(v)).dot(w.T)
39  hold('on'); pyplot(elxy[:,0],elxy[:,1],'-r'); hold('off'); axis('equal')
40  print('triangle area: %0.6f, ellipse axis product(*3*sqrt(3)/4): %0.6f' % (pyabs(linalg.det(array([cxy[1,:]-cxy[0,:],cxy[2,:]-cxy[0,:]])))/2,v[0]*v[1]*3*sqrt(3)/4))
41  show()
42 
44  mesh = BoxMesh(0,0,0,1,1,1,20,20,20)
45  mesh = adapt(interpolate(Constant(((100.,0.,0.),(0.,100.,0.),(0.,0.,100.))),TensorFunctionSpace(mesh,'CG',1)))
46 
47  #extract mesh metric
48  MpH = mesh_metric2(mesh)
49  for i in range(0,40):
50  thecell = mesh.cells()[i]
51  ind = MpH.function_space().dofmap().cell_dofs(i)
52  coords = mesh.coordinates()[thecell,:]
53  r1 = coords[1,:]-coords[0,:]
54  r2 = coords[2,:]-coords[0,:]
55  r3 = coords[0:3,:].mean(0)-coords[3,:]
56  r12c = cross(r2,r1)/6.
57  det1234 = pyabs((r12c*r3).sum())
58 
59  H = MpH.vector().gather(ind).reshape(3,3)# H = array([[H[1],H[0]],[H[0],H[2]]])
60  [v,w] = linalg.eig(H)
61  print('tetrahedron volume: %0.6f, ellipsoid axis product(*sqrt(2)/12): %0.6f' % (det1234,v.prod()*sqrt(2)/12))
62  print v.prod()*sqrt(2)/12/(det1234)
63 
64 if __name__=="__main__":
65 # test_mesh_metric()
def mesh_metric2
Definition: adaptivity.py:1054