def get_2d_slice_of_3d_function_on_Oz(mesh, u, T, dim, v_degree): # create a boundary mesh bmesh = BoundaryMesh(mesh, "exterior") cell_func = CellFunction('size_t', bmesh, 0) coordinates = bmesh.coordinates() z_indeces = postprocess.allocate_array(dim) for cell in cells(bmesh): indx = 0 for vertex in vertices(cell): z_indeces[indx] = coordinates[vertex.index()][dim - 1] indx += 1 #print "Vertex index with coordinates", vertex.index(), coordinates[vertex.index()][dim-1] if (dim == 3 and near(z_indeces[0], T) and near(z_indeces[1], T) and near(z_indeces[2], T)) or (dim == 2 and near( z_indeces[0], T) and near(z_indeces[1], T)): #print "right cell", cell.index() cell_func[cell] = 1 submesh = SubMesh(bmesh, cell_func, 1) # create a FunctionSpace on the submesh- Vs = FunctionSpace(submesh, "Lagrange", v_degree) us = interpolate(u, Vs) return us, submesh
def construct_boundary_faces(self, mesh): # Get boundary facets function facet_funcs = FacetFunction('size_t', mesh) # Dirichlet part of the boudary is marked by 0 dirichlet_marker = 0 facet_funcs.set_all(dirichlet_marker) neumann_marker = dirichlet_marker + 1 # Mark different parts of the domain for non-homogeneous Dirichlet BC if test_num == 45: # Creat parts of the boundary as subdomains bottom = AutoSubDomain(lambda x: near(x[1], 0)) # Mark part of the domain with non-zero Dirichlet condition bottom.mark(facet_funcs, dirichlet_marker + 1) elif test_num == 48: # Creat parts of the boundary as subdomains right = AutoSubDomain(lambda x: near(x[0], 1)) # Mark part of the domain with non-zero Dirichlet condition right.mark(facet_funcs, neumann_marker) ds = Measure('ds')[facet_funcs] dS = Measure('dS')[facet_funcs] return facet_funcs, ds, dS
def map(self, x, y): if near(x[0], 1.0) and near(x[1], 1.0): y[0] = x[0] - 1.0 y[1] = x[1] - 1.0 elif near(x[0], 1.0): y[0] = x[0] - 1.0 y[1] = x[1] elif near(x[1], 1.0): y[0] = x[0] y[1] = x[1] - 1.0
def geometry_3d(): """Prepares 3D geometry. Returns facet function with 1, 2 on parts of the boundary.""" mesh = Mesh('lego_beam.xml') gdim = mesh.geometry().dim() x0 = mesh.coordinates()[:, 0].min() x1 = mesh.coordinates()[:, 0].max() boundary_parts = FacetFunction('size_t', mesh) left = AutoSubDomain(lambda x: near(x[0], x0)) right = AutoSubDomain(lambda x: near(x[0], x1)) left.mark(boundary_parts, 1) right.mark(boundary_parts, 2) boundary_parts._mesh = mesh # Workaround issue #467 return boundary_parts
def geometry_2d(length): """Prepares 2D geometry. Returns facet function with 1, 2 on parts of the boundary.""" n = 4 x0 = 0.0 x1 = x0 + length y0 = 0.0 y1 = 1.0 mesh = RectangleMesh(x0, y0, x1, y1, int((x1 - x0) * n), int( (y1 - y0) * n), 'crossed') boundary_parts = FacetFunction('size_t', mesh) left = AutoSubDomain(lambda x: near(x[0], x0)) right = AutoSubDomain(lambda x: near(x[0], x1)) left.mark(boundary_parts, 1) right.mark(boundary_parts, 2) boundary_parts._mesh = mesh # Workaround issue #467 return boundary_parts
def eval(self, value, x): x_dist2 = float((x[0] - self.center[0]) * (x[0] - self.center[0])) y_dist2 = float((x[1] - self.center[1]) * (x[1] - self.center[1])) z_dist2 = float((x[2] - self.center[2]) * (x[2] - self.center[2])) rad = float(sqrt(x_dist2 + y_dist2 + z_dist2)) # do not evaluate on boundaries or outside of circle: velocity = 0 if near(rad, self.radius) or rad > self.radius else \ 2.*self.onset_factor*self.factor*self.itp(self.t)*(1.0 - rad*rad/(self.radius*self.radius)) value[0] = velocity * self.normal[0] value[1] = velocity * self.normal[1] value[2] = velocity * self.normal[2]
def eval(self, value, x): x_dist2 = float((x[0]-self.center[0])*(x[0]-self.center[0])) y_dist2 = float((x[1]-self.center[1])*(x[1]-self.center[1])) z_dist2 = float((x[2]-self.center[2])*(x[2]-self.center[2])) rad = float(sqrt(x_dist2+y_dist2+z_dist2)) # do not evaluate on boundaries or outside of circle: velocity = 0 if near(rad, self.radius) or rad > self.radius else \ 2.*self.onset_factor*self.factor*self.itp(self.t)*(1.0 - rad*rad/(self.radius*self.radius)) value[0] = velocity * self.normal[0] value[1] = velocity * self.normal[1] value[2] = velocity * self.normal[2]
def solve_elasticity(mesh: Mesh) -> Function: V = VectorFunctionSpace(mesh, 'P', 1) bc = DirichletBC(V, Constant((0, 0, 0)), lambda x, on_boundary: on_boundary and near(x[2], 0)) u = TrialFunction(V) v = TestFunction(V) f = Expression(('0', '0', '-rho*g'), rho=_c.rho, g=_c.g, degree=3) T = Constant((0, 0, 0)) a = inner(sigma(u), sym(nabla_grad(v))) * dx L = dot(f, v) * dx + dot(T, v) * ds u = Function(V) solve(a == L, u, bc) return u
time = 0.2 nu_factor = 10. length = 20.0 v_in = 10.0 nu = Constant(3.7 * nu_factor) dt = Constant(timestep) f = Constant(0.0) mesh_size = 10 # Create meshes and facet function mesh = IntervalMesh(mesh_size, 0.0, length) mesh_plot = IntervalMesh(8*mesh_size, 0.0, length) boundary_parts = FacetFunction('size_t', mesh) right = AutoSubDomain(lambda x: near(x[0], length)) left = AutoSubDomain(lambda x: near(x[0], 0.0)) right.mark(boundary_parts, 2) left.mark(boundary_parts, 1) # Create function spaces V = FunctionSpace(mesh, 'Lagrange', 2) # Vplot = FunctionSpace(mesh_plot, 'Lagrange', 1) Vplot = V Q = FunctionSpace(mesh, 'Lagrange', 1) # BC conditions, nullspace v_in_expr = Constant(v_in) plt = plot(interpolate(v_in_expr, V), range_min=0., range_max=2*v_in, window_width= width, window_height= height) plt.write_png('%s/correct' % dir) # v_in_expr = Expression('(t<1.0)?t*v:v', v=Constant(v_in), t=0.0)
def inside(self, x, on_boundary): return bool((near(x[0], 0.0) and near(x[1], 0.0)) and (not ((near(x[0], 0.0) and near(x[1], 1.0)) or (near(x[0], 1.0) and near(x[1], 0.0)))) and on_boundary)
def construct_boundary_faces(self, mesh): # Get boundary facets function facet_funcs = FacetFunction('size_t', mesh) facet_funcs.set_all(0) # Dirichlet part of the boudary is marked by 0 # dirichlet_bc_marker = 1 # By default left and right is Dirichlet BC left = AutoSubDomain(lambda x: near(x[0], 0.0)) # x_min right = AutoSubDomain(lambda x: near(x[0], 1.0)) # x_max left.mark(facet_funcs, dirichlet_bc_marker) right.mark(facet_funcs, dirichlet_bc_marker) if dim == 3: # By default left and right is Dirichlet BC top = AutoSubDomain(lambda x: near(x[1], 0.0)) # x_min bottom = AutoSubDomain(lambda x: near(x[1], 1.0)) # x_max top.mark(facet_funcs, dirichlet_bc_marker) bottom.mark(facet_funcs, dirichlet_bc_marker) #neumann_bc_marker = dirichlet_bc_marker + 1 if self.test_num == 24: left = AutoSubDomain(lambda x: near(x[0], 0.0)) # x_min right = AutoSubDomain(lambda x: near(x[0], 10.0)) # x_max left.mark(facet_funcs, neumann_bc_marker) right.mark(facet_funcs, neumann_bc_marker) if self.test_num == 37: right = AutoSubDomain(lambda x: near(x[0], 1.0)) # x_max right.mark(facet_funcs, neumann_bc_marker) if self.test_num == 38 or test_num == 39 or test_num == 40 or test_num == 41 or test_num == 42: left = AutoSubDomain(lambda x: near(x[0], 0.0)) # x_min right = AutoSubDomain(lambda x: near(x[0], 1.0)) # x_max left.mark(facet_funcs, neumann_bc_marker) right.mark(facet_funcs, neumann_bc_marker) # The boundary with IC #init_bc_marker = neumann_bc_marker + 1 # = 3 sigma_0 = AutoSubDomain(lambda x: near(x[1], 0.0)) sigma_0.mark(facet_funcs, init_bc_marker) # The top of the space-time cylinder #do_nothing_bc_marker = init_bc_marker + 1 # = 4 sigma_T = AutoSubDomain(lambda x: near(x[1], self.T)) sigma_T.mark(facet_funcs, do_nothing_bc_marker) ds = Measure('ds')[facet_funcs] dS = Measure('dS')[facet_funcs] return facet_funcs, ds, dS
def inside(self, x, on_boundary): return on_boundary and near(x[1], 0)
def inside(self, x, on_boundary): return near( geoNormals[i].dot(Point(x)-geo._points[geoBndPoints[i]]), 0.0 ) and on_boundary
def on_upper_surface(self, x, on_boundary): return on_boundary and near(x[2], self.H)
def on_bottom_surface(self, x, on_boundary): return on_boundary and near(x[2], 0)
time = 0.2 nu_factor = 10. length = 20.0 v_in = 10.0 nu = Constant(3.7 * nu_factor) dt = Constant(timestep) f = Constant(0.0) mesh_size = 10 # Create meshes and facet function mesh = IntervalMesh(mesh_size, 0.0, length) mesh_plot = IntervalMesh(8 * mesh_size, 0.0, length) boundary_parts = FacetFunction('size_t', mesh) right = AutoSubDomain(lambda x: near(x[0], length)) left = AutoSubDomain(lambda x: near(x[0], 0.0)) right.mark(boundary_parts, 2) left.mark(boundary_parts, 1) # Create function spaces V = FunctionSpace(mesh, 'Lagrange', 2) # Vplot = FunctionSpace(mesh_plot, 'Lagrange', 1) Vplot = V Q = FunctionSpace(mesh, 'Lagrange', 1) # BC conditions, nullspace v_in_expr = Constant(v_in) plt = plot(interpolate(v_in_expr, V), range_min=0., range_max=2 * v_in,