PRAgMaTIc  master
minimal_example3D.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 3D 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 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
16 set_log_level(INFO+1)
17 #parameters["allow_extrapolation"] = True
18 
19 def minimal_example3D(width=2e-2, Nadapt=10, eta = 0.04):
20  ### CONSTANTS
21  meshsz = 10
22  ### SETUP MESH
23  mesh = BoxMesh(-0.5,-0.5,-0.5,0.5,0.5,0.5,meshsz,meshsz,meshsz)
24  ### DERIVE FORCING TERM
25  angle = pi/8 #rand*pi/2
26  angle2 = pi/8 #rand*pi/2
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]')
30  #replace ** with pow
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))')
33  #insert vaulues
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+")"
37  def boundary(x):
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
41  # PERFORM TEN ADAPTATION ITERATIONS
42  fid = File("out.pvd")
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)
48  solve(a == L, u, bc)
49  fid << u
50  startTime = time()
51  H = metric_pnorm(u, eta, max_edge_length=2., max_edge_ratio=50, CG1out=True)
52  #H = logproject(H)
53  if iii != Nadapt-1:
54  mesh = adapt(H)
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()))
57 
58 
59  plot(u,interactive=True)
60  plot(mesh,interactive=True)
61 
62 
63 if __name__=="__main__":
def metric_pnorm
Definition: adaptivity.py:535