PRAgMaTIc  master
minimal_example_minell.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 two forcing terms that give orthogonal a step function solutions
5 ### 2. solve a two poisson equations with the forcing terms using 2D anisotropic adaptivity
6 ### (on the same mesh) using the inner ellipse method for combining the metrics
7 ### 3. plot the resulting mesh and one of solutions.
8 ### the width of the step, the number of solition<->adaptation iterations as well as the
9 ### error level (eta) are optional input parameters
10 
11 from dolfin import *
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
16 
17 def check_metric_ellipse(width=2e-2, eta = 0.02, Nadapt=6):
18  set_log_level(WARNING)
19  parameters["allow_extrapolation"] = True
20 
21  ### CONSTANTS
22  meshsz = 40
23  hd = Constant(width)
24  ### SETUP MESH
25  mesh = RectangleMesh(-0.5,-0.5,0.5,0.5,1*meshsz,1*meshsz,"left/right")
26  ### SETUP SOLUTION
27  angle = pi/8#rand()*pi/2
28  #testsol = 'tanh(x[0]/' + str(float(hd)) + ')' #tanh(x[0]/hd)
29  testsol = 'tanh((' + str(cos(angle)) + '*x[0]+'+ str(sin(angle)) + '*x[1])/' + str(float(hd)) + ')' #tanh(x[0]/hd)
30  ddtestsol = str(cos(angle)+sin(angle))+'*2*'+testsol+'*(1-pow('+testsol+',2))/'+str(float(hd)**2)
31  #testsol2 = 'tanh(x[1]/' + str(float(hd)) + ')' #tanh(x[0]/hd)
32  testsol2 = 'tanh((' + str(cos(angle)) + '*x[1]-'+ str(sin(angle)) + '*x[0])/' + str(float(hd)) + ')' #tanh(x[0]/hd)
33  ddtestsol2 = str(cos(angle)-sin(angle))+'*2*'+testsol2+'*(1-pow('+testsol2+',2))/'+str(float(hd)**2)
34  def boundary(x):
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
37  # PERFORM ONE ADAPTATION ITERATION
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
45  L = R*dus*dx
46  L2 = R2*dus*dx
47  solve(a == L, u, bc)
48  solve(a == L2, u2, bc2)
49 
50  H = metric_pnorm(u , eta, max_edge_length=1., max_edge_ratio=50); #Mp = project(H, TensorFunctionSpace(mesh, "CG", 1))
51  H2 = metric_pnorm(u2, eta, max_edge_length=1., max_edge_ratio=50); #Mp2 = project(H2, TensorFunctionSpace(mesh, "CG", 1))
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()))
54  startTime = time()
55  if iii != 6:
56  # mesh2 = Mesh(adapt(Mp2))
57  mesh = Mesh(adapt(Mp3))
58  # mesh3 = adapt(Mp)
59 
60  print("total time was %0.1fs" % (time()-startTime))
61 
62  # PLOT MESH
63  figure(1); triplot(mesh.coordinates()[:,0],mesh.coordinates()[:,1],mesh.cells()); axis('equal'); axis('off'); box('off')
64  #figure(2); triplot(mesh2.coordinates()[:,0],mesh2.coordinates()[:,1],mesh2.cells()) #mesh = mesh2
65  #figure(3); triplot(mesh3.coordinates()[:,0],mesh3.coordinates()[:,1],mesh3.cells()) #mesh = mesh3
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)
70  show()
71 
72 if __name__=="__main__":
def metric_ellipse
Definition: adaptivity.py:654
Manages mesh data.
Definition: Mesh.h:70
def metric_pnorm
Definition: adaptivity.py:535