PRAgMaTIc  master
minimal_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
4 ### 1. derive a forcing term that gives a step function solution
5 ### 2. solve a poisson equation with the forcing term using 2D anisotropic adaptivity
6 ### 3. calculate and plot the L2error of the resulting solution.
7 ### the width of the step, the number of solition<->adaptation iterations as well as the
8 ### error level (eta) are optional input parameters
9 
10 from dolfin import *
11 from adaptivity import metric_pnorm, logproject, adapt
12 from pylab import hold, show, triplot, tricontourf, colorbar, axis, box, rand, get_cmap, title, figure, savefig
13 from pylab import plot as pyplot
14 from numpy import array, ones
15 import numpy
16 from sympy import Symbol, diff
17 from sympy import tanh as pytanh
18 from sympy import cos as pysin
19 from sympy import sin as pycos
20 set_log_level(INFO+1)
21 #parameters["allow_extrapolation"] = True
22 
23 def minimal_example(width=2e-2, Nadapt=10, eta = 0.01):
24  ### CONSTANTS
25  meshsz = 40
26  hd = Constant(width)
27  ### SETUP MESH
28  mesh = RectangleMesh(-0.5,-0.5,0.5,0.5,1*meshsz,1*meshsz,"left/right")
29  ### DERIVE FORCING TERM
30  angle = pi/8 #rand*pi/2
31  sx = Symbol('sx'); sy = Symbol('sy'); width_ = Symbol('ww'); aa = Symbol('aa')
32  testsol = pytanh((sx*pycos(aa)+sy*pysin(aa))/width_)
33  ddtestsol = str(diff(testsol,sx,sx)+diff(testsol,sy,sy)).replace('sx','x[0]').replace('sy','x[1]')
34  #replace ** with pow
35  ddtestsol = ddtestsol.replace('tanh((x[0]*sin(aa) + x[1]*cos(aa))/ww)**2','pow(tanh((x[0]*sin(aa) + x[1]*cos(aa))/ww),2.)')
36  ddtestsol = ddtestsol.replace('cos(aa)**2','pow(cos(aa),2.)').replace('sin(aa)**2','pow(sin(aa),2.)').replace('ww**2','(ww*ww)')
37  #insert vaulues
38  ddtestsol = ddtestsol.replace('aa',str(angle)).replace('ww',str(width))
39  testsol = str(testsol).replace('sx','x[0]').replace('sy','x[1]').replace('aa',str(angle)).replace('ww',str(width))
40  ddtestsol = "-("+ddtestsol+")"
41  def boundary(x):
42  return x[0]-mesh.coordinates()[:,0].min() < DOLFIN_EPS or mesh.coordinates()[:,0].max()-x[0] < DOLFIN_EPS \
43  or mesh.coordinates()[:,1].min()+0.5 < DOLFIN_EPS or mesh.coordinates()[:,1].max()-x[1] < DOLFIN_EPS
44  # PERFORM TEN ADAPTATION ITERATIONS
45  for iii in range(Nadapt):
46  V = FunctionSpace(mesh, "CG" ,2); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V)
47  a = inner(grad(dis), grad(dus))*dx
48  L = Expression(ddtestsol)*dus*dx
49  bc = DirichletBC(V, Expression(testsol), boundary)
50  solve(a == L, u, bc)
51  startTime = time()
52  H = metric_pnorm(u, eta, max_edge_length=1., max_edge_ratio=50)
53  H = logproject(H)
54  if iii != Nadapt-1:
55  mesh = adapt(H)
56  L2error = errornorm(Expression(testsol), u, degree_rise=4, norm_type='L2')
57  log(INFO+1,"total (adapt+metric) time was %0.1fs, L2error=%0.0e, nodes: %0.0f" % (time()-startTime,L2error,mesh.num_vertices()))
58 
59  # # PLOT MESH
60 # figure()
61  coords = mesh.coordinates().transpose()
62 # triplot(coords[0],coords[1],mesh.cells(),linewidth=0.1)
63 # #savefig('mesh.png',dpi=300) #savefig('mesh.eps');
64 
65  figure() #solution
66  testf = interpolate(Expression(testsol),FunctionSpace(mesh,'CG',1))
67  vtx2dof = vertex_to_dof_map(FunctionSpace(mesh, "CG" ,1))
68  zz = testf.vector().array()[vtx2dof]
69  hh=tricontourf(coords[0],coords[1],mesh.cells(),zz,100)
70  colorbar(hh)
71 # savefig('solution.png',dpi=300) #savefig('solution.eps');
72 
73  figure() #analytical solution
74  testfe = interpolate(u,FunctionSpace(mesh,'CG',1))
75  zz = testfe.vector().array()[vtx2dof]
76  hh=tricontourf(coords[0],coords[1],mesh.cells(),zz,100)
77  colorbar(hh)
78  #savefig('analyt.png',dpi=300) #savefig('analyt.eps');
79 
80  figure() #error
81  zz -= testf.vector().array()[vtx2dof]; zz[zz==1] -= 1e-16
82  hh=tricontourf(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),zz,100,cmap=get_cmap('binary'))
83  colorbar(hh)
84 
85  hold('on'); triplot(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells(),color='r',linewidth=0.5); hold('off')
86  axis('equal'); box('off'); title('error')
87  show()
88 
89 if __name__=="__main__":
91 
def metric_pnorm
Definition: adaptivity.py:535
def logproject
Definition: adaptivity.py:966