def test_build_mesh(): """ Create a few meshes, extract the vertices and cells from them and pass them to build_mesh() to rebuild the mesh. Then check that the result is the same as the original. """ def assert_mesh_builds_correctly(mesh): coords = mesh.coordinates() cells = mesh.cells() mesh_new = build_mesh(coords, cells) assert np.allclose(coords, mesh_new.coordinates()) assert np.allclose(cells, mesh_new.cells()) mesh1 = df.RectangleMesh(df.Point(0, 0), df.Point(20, 10), 12, 8) assert_mesh_builds_correctly(mesh1) mesh2_temp = mshr.Circle(df.Point(2.0, -3.0), 10) mesh2 = mshr.generate_mesh(mesh2_temp, 10) assert_mesh_builds_correctly(mesh2) mesh3 = df.BoxMesh(df.Point(0, 0, 0), df.Point(20, 10, 5), 12, 8, 3) assert_mesh_builds_correctly(mesh3) mesh4_temp = mshr.Sphere(df.Point(2.0, 3.0, -4.0), 10) mesh4 = mshr.generate_mesh(mesh4_temp, 10) assert_mesh_builds_correctly(mesh4)
def make_spherical_mesh(size, resolution): #Defining the domain for the mesh using the Sphere function from mshr domain = mshr.Sphere(origin, size) #Meshing the sphere generated with resolution 10 (cells per dimension) initial_mesh = mshr.generate_mesh(domain, resolution) return initial_mesh
def initiate_fem(self): #define mesh #self.mesh = BoxMesh(Point(0, 0, 0), Point(self.L, self.W, self.W), 10, 3, 3) # Parameters R = self.W / 4 r = 0.08 t = self.W x = self.W / 2 + R * cos(float(t) / 180 * pi) y = self.W / 2 z = R * sin(t) # Create geometry s1 = mshr.Sphere(Point(x + self.L - 3 / 2 * self.W, y, z), r) s2 = mshr.Sphere(Point(x, y, z), r) b1 = mshr.Box(Point(0, 0, 0), Point(self.L, self.W, self.W)) b2 = mshr.Box(Point(self.L / 2 - self.w, 0, self.W / 2), Point(self.L / 2 + self.w, self.W, self.W)) geometry1 = b1 - s1 - s2 geometry2 = b1 - b2 # Create and store mesh self.mesh = mshr.generate_mesh(geometry1, 10) # use geometry1 or geometry2 #cell_markers = CellFunction("bool", mesh) #cell_markers.set_all(False) #for cell in cells(mesh): #p = cell.midpoint() #if p.x() > (self.L/2-2*self.w) and p.x() < (self.L/2-2*self.w): #cell_markers[cell] = True ## refining mesh #mesh = LocalMeshCoarsening(mesh, cell_markers) #self.mesh = mesh File('results/cracked_beam.pvd') << self.mesh #define function space self.V = VectorFunctionSpace(self.mesh, 'P', 1) #define dirichlet boundary self.bc = DirichletBC(self.V, Constant((0, 0, 0)), clamped_boundary) #define right hand side function self.f = Constant((0, 0, -self.rho * self.g)) self.T = Constant((0, 0, 0)) #define functions q = TrialFunction(self.V) # p = TrialFunction(self.V) self.d = q.geometric_dimension() v = TestFunction(self.V) aq = inner(q, v) * dx kq = -inner(sigma(q, self.lambda_, self.mu, self.d), epsilon(v)) * dx Lq = inner(self.f, v) * dx Kq, bq = assemble_system(kq, Lq, self.bc) self.Aq, dummy = assemble_system(aq, Lq, self.bc) #define the mass and stiffness matrices M = np.matrix(self.Aq.array()) print(M.shape) self.M_inv = np.linalg.inv(M) self.K = np.matrix(Kq.array()) print(self.K.shape) #define the force term c = np.matrix(bq.array()) self.cp = np.transpose(c)
def __init__(self, a, L): NonlinearProblem.__init__(self) self.L = L self.a = a def F(self, b, x): assemble(self.L, tensor=b) def J(self, A, x): assemble(self.a, tensor=A) # Define mesh and function space p1 = Point(1.0, 1.0) p0 = Point(0.0, 0.0, 0.0) sphere = mshr.Sphere(p0, 1.0) mesh = mshr.generate_mesh(sphere, 16) V_ele = FiniteElement("CG", mesh.ufl_cell(), 1) V = FunctionSpace(mesh, MixedElement([V_ele, V_ele])) # Define functions W_init = InitialConditions(degree=1) phi = TestFunction(V) dp = TrialFunction(V) W0 = Function(V) W = Function(V) # Interpolate initial conditions and split functions W0.interpolate(W_init) q, p = split(phi)
if has_value == 0: regions.array()[regions.array()>i] -= 1 values = np.unique(regions.array()) if cell_connectivity: cf = CellFunction("size_t", mesh) cf.set_all(0) cells = mesh.cells()[:,0] cf.array()[:] = regions.array()[cells] regions = cf return regions if __name__ == '__main__': import mshr from dolfin import * #set_log_level(60) #print "hei" domain = mshr.Sphere(Point(-1.0,0.0,0.0), 0.8)+mshr.Sphere(Point(1.0,0.0,0.0), 0.8) domain += mshr.Sphere(Point(0.0,1.5,0.0), 0.8)+mshr.Sphere(Point(0.0,-1.5,0.0), 0.8) #domain = mshr.Circle(Point(-1.0,0.0), 0.8)+mshr.Circle(Point(1.0,0.0), 0.8) mesh = mshr.generate_mesh(domain, 20) #from IPython import embed; embed() tic() connectivity = compute_connectivity(mesh) print(mesh.size_global(0), mesh.size_global(3), toc()) File("connectivity.xdmf") << connectivity
if d == MPI.min(mesh.mpi_comm(), d): v = regions[int(i)] else: v = 0 v = MPI.max(mesh.mpi_comm(), v) mesh = create_submesh(mesh, regions, v) return mesh if __name__ == '__main__': import mshr from dolfin import * set_log_level(100) domain = mshr.Sphere(Point(-1.0,0.0,0.0), 1.2)+mshr.Sphere(Point(1.0,0.0,0.0), 1.2) mesh = mshr.generate_mesh(domain, 30) p = np.array([1.0,1.0,0.0]) n = np.array([0,1,0]) slicemesh = create_slice(mesh, p, n, closest_region=False, crinkle_clip=True) plot(slicemesh) interactive() from dolfin import File File("basemesh.pvd") << mesh File("slice_mesh.xdmf") << slicemesh