class BoxGrid(object): def __init__(self, x0, x1, y0, y1, z0, z1, n, unstructured=False): class Left(SubDomain): def inside(self, x, on_boundary): return near(x[0], x0) class Right(SubDomain): def inside(self, x, on_boundary): return near(x[0], x1) class Back(SubDomain): def inside(self, x, on_boundary): return near(x[1], y0) class Front(SubDomain): def inside(self, x, on_boundary): return near(x[1], y1) class Bottom(SubDomain): def inside(self, x, on_boundary): return near(x[2], z0) class Top(SubDomain): def inside(self, x, on_boundary): return near(x[2], z1) if unstructured: self.geometry = Box(Point(x0, y0, z0), Point(x1, y1, z1)) self.mesh = generate_mesh(self.geometry, n) else: nx = int(round(n**(1./3.)*(x1 - x0))) ny = int(round(n**(1./3.)*(y1 - y0))) nz = int(round(n**(1./3.)*(z1 - z0))) self.mesh = BoxMesh(Point(x0, y0, z0), Point(x1, y1, z1), nx, ny, nz) self.domains = MeshFunction("size_t", self.mesh, self.mesh.topology().dim()) self.domains.set_all(0) self.dx = Measure('dx', domain=self.mesh, subdomain_data=self.domains) self.boundaries = MeshFunction("size_t", self.mesh, self.mesh.topology().dim()-1) self.boundaries.set_all(0) self.left = Left() self.left.mark(self.boundaries, 1) self.right = Right() self.right.mark(self.boundaries, 2) self.front = Front() self.front.mark(self.boundaries, 3) self.back = Back() self.back.mark(self.boundaries, 4) self.bottom = Bottom() self.bottom.mark(self.boundaries, 5) self.top = Top() self.top.mark(self.boundaries, 6) self.ds = Measure('ds', domain=self.mesh, subdomain_data=self.boundaries) self.dS = Measure('dS', domain=self.mesh, subdomain_data=self.boundaries)
def create_mesh(): N = 20 x0, y0, z0 = -1.5, -1.5, -0.25 x1, y1, z1 = 1.5, 1.5, 0.25 # mesh = UnitCubeMesh.create(N, N, N//2, CellType.Type.hexahedron) mesh = BoxMesh(Point(x0, y0, z0), Point(x1, y1, z1), N, N, N//2) # mesh = UnitCubeMesh(N, N, N) # mesh size is smaller near x=y=0 # mesh.coordinates()[:, :2] = mesh.coordinates()[:, :2]**2 # mesh size is smaller near z=0 and mapped to a [-1;0] domain along z # mesh.coordinates()[:, 2] = -mesh.coordinates()[:, 2]**2 # left = CompiledSubDomain("near(x[0], side) && on_boundary", side = 0.0) # right = CompiledSubDomain("near(x[0], side) && on_boundary", side = 1.0) class Top(SubDomain): def inside(self, x, on_boundary): return near(x[2], z1) and on_boundary class Bottom(SubDomain): def inside(self, x, on_boundary): return near(x[2], z0) and on_boundary class Left(SubDomain): def inside(self, x, on_boundary): return near(x[0], x0) and on_boundary class Right(SubDomain): def inside(self, x, on_boundary): return near(x[0], x1) and on_boundary class Front(SubDomain): def inside(self, x, on_boundary): return near(x[1], y0) and on_boundary class Back(SubDomain): def inside(self, x, on_boundary): return near(x[1], y1) and on_boundary class Symmetry_x(SubDomain): def inside(self, x, on_boundary): return near(x[0], 0) and on_boundary class Symmetry_y(SubDomain): def inside(self, x, on_boundary): return near(x[1], 0) and on_boundary # exterior facets MeshFunction boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1) boundaries.set_all(0) Top().mark(boundaries, 1) Bottom().mark(boundaries, 2) Left().mark(boundaries, 3) Right().mark(boundaries, 4) Front().mark(boundaries, 5) Back().mark(boundaries, 6) # Symmetry_x().mark(boundaries, 2) # Symmetry_y().mark(boundaries, 3) subdomains = MeshFunction("size_t", mesh, mesh.topology().dim()) subdomains.set_all(0) File("hyperelastic_cube.xml") << mesh File("hyperelastic_cube_physical_region.xml") << subdomains File("hyperelastic_cube_facet_region.xml") << boundaries return (mesh, subdomains, boundaries)