PRAgMaTIc  master
inlet_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 ### illustrate the need for explicit definition corner nodes
5 
6 from dolfin import *
7 from adaptivity import metric_pnorm, logproject, adapt, polyhedron_surfmesh
8 import numpy
9 
10 set_log_level(INFO+1)
11 
12 def minimal_example3D(meshsz=20, Nadapt=10, dL=0.05, eta = 0.01, returnmesh=False, hax=False):
13 
14  class inlet(SubDomain):
15  def inside(self, x, on_boundary):
16  return 0.5-DOLFIN_EPS < x[0] \
17  and -dL-DOLFIN_EPS < x[1] and x[1] < dL+DOLFIN_EPS \
18  and -dL-DOLFIN_EPS < x[2] and x[2] < dL+DOLFIN_EPS
19  def boundary(x, on_boundary): #not(boundary(x))
20  return on_boundary and ((0.5-DOLFIN_EPS >= x[0] \
21  or -dL-DOLFIN_EPS >= x[1] or x[1] >= dL+DOLFIN_EPS \
22  or -dL-DOLFIN_EPS >= x[2] or x[2] >= dL+DOLFIN_EPS))
23 
24  def get_bnd_mesh(mesh):
25  coords = mesh.coordinates()
26  [bfaces,bfaces_IDs] = polyhedron_surfmesh(mesh)
27  bcoords = (coords[bfaces[:,0],:]+coords[bfaces[:,1],:]+coords[bfaces[:,2],:])/3.
28  I = (bcoords[:,0] > 0.5-DOLFIN_EPS) & (bcoords[:,1] < dL) & (bcoords[:,1] > -dL) \
29  & (bcoords[:,2] < dL) & (bcoords[:,2] > -dL)
30  I2 = (bcoords[:,0] > 0.5-DOLFIN_EPS) & (bcoords[:,1] < dL) & (bcoords[:,1] > -dL) \
31  & (bcoords[:,2] > dL)
32  I3 = (bcoords[:,0] > 0.5-DOLFIN_EPS) & (bcoords[:,1] < dL) & (bcoords[:,1] > -dL) \
33  & (bcoords[:,2] < -dL)
34  bfaces_IDs[I] = 97
35  if hax:
36  bfaces_IDs[I2] = 98
37  bfaces_IDs[I3] = 99 #problems
38 
39  c1 = numpy.array([0.5, -dL, -dL]).repeat(coords.shape[0]).reshape([3,coords.shape[0]]).T
40  c2 = numpy.array([0.5, -dL, dL]).repeat(coords.shape[0]).reshape([3,coords.shape[0]]).T
41  c3 = numpy.array([0.5, dL, -dL]).repeat(coords.shape[0]).reshape([3,coords.shape[0]]).T
42  c4 = numpy.array([0.5, dL, dL]).repeat(coords.shape[0]).reshape([3,coords.shape[0]]).T
43  crnds = numpy.where((numpy.sqrt(((coords - c1)**2).sum(1)) < 1e3*DOLFIN_EPS) | \
44  (numpy.sqrt(((coords - c2)**2).sum(1)) < 1e3*DOLFIN_EPS) | \
45  (numpy.sqrt(((coords - c3)**2).sum(1)) < 1e3*DOLFIN_EPS) | \
46  (numpy.sqrt(((coords - c4)**2).sum(1)) < 1e3*DOLFIN_EPS))[0]
47 # return [bfaces,bfaces_IDs,crnds]
48  return [bfaces,bfaces_IDs]
49 
50  ### SETUP MESH
51  mesh = BoxMesh(-0.5,-0.5,-0.5,0.5,0.5,0.5,meshsz,meshsz,meshsz)
52  fid = File("out.pvd")
53  # PERFORM TEN ADAPTATION ITERATIONS
54  for iii in range(Nadapt):
55  V = FunctionSpace(mesh, "CG" ,2); dis = TrialFunction(V); dus = TestFunction(V); u = Function(V)
56  a = inner(grad(dis), grad(dus))*dx
57  boundaries = FacetFunction("size_t",mesh)
58  Inlet = inlet()
59  boundaries.set_all(0)
60  Inlet.mark(boundaries, 1)
61  ds = Measure("ds")[boundaries]
62 
63  L = dus*ds(1) #+dus*dx
64  bc = DirichletBC(V, Constant(0.), boundary)
65  solve(a == L, u, bc)
66  fid << u
67  startTime = time()
68  H = metric_pnorm(u, eta, max_edge_length=2., max_edge_ratio=50, CG1out=True)
69  #H = logproject(H)
70  if iii != Nadapt-1:
71  [bfaces,bfaces_IDs] = get_bnd_mesh(mesh)
72  mesh = adapt(H, bfaces=bfaces, bfaces_IDs=bfaces_IDs)
73  log(INFO+1,"total (adapt+metric) time was %0.1fs, nodes: %0.0f" % (time()-startTime, mesh.num_vertices()))
74 
75  plot(u,interactive=True)
76  plot(mesh,interactive=True)
77 
78 
79 if __name__=="__main__":
80  minimal_example3D(meshsz=10, dL=0.1, hax=True)
81 # minimal_example3D(meshsz=10, dL=0.1, hax=False)
82 
def polyhedron_surfmesh
Definition: adaptivity.py:117
def metric_pnorm
Definition: adaptivity.py:535