def mesh_randomizer_2d(mesh, percentage, preserve_boundary=True): """ Randomly perturb a given mesh. Args: mesh: Input mesh. percentage: Maximum perturbation in percentage of mesh.hmin(). preserve_boundary: Whether to move the vertices on the boundary. Returns: rmesh: The perturbed mesh. """ # Generate a deep copy of the mesh rmesh = Mesh(mesh) meshsize = rmesh.hmin() # Randomly perturbed the mesh radius = np.random.rand(rmesh.num_vertices()) * percentage * meshsize theta = np.random.rand(rmesh.num_vertices()) * 2.0 * np.pi deltax = np.zeros([rmesh.num_vertices(), 2]) deltax[:, 0] = (radius * np.sin(theta)).transpose() deltax[:, 1] = (radius * np.cos(theta)).transpose() # What to do with the boundary vertices if preserve_boundary: # Exterior means global boundary boundary_mesh = BoundaryMesh(rmesh, "exterior") # entity_map contains the indices of vertices on the boundary boundary_vertices = boundary_mesh.entity_map(0).array() deltax[boundary_vertices] = 0.0 rmesh.coordinates()[:] = rmesh.coordinates() + deltax return rmesh
def evenodd_functions_old( omesh, degree, func, width=None, evenodd=None ): """Break a function into even and odd components Required parameters: omesh: the mesh on which the function is defined degree: the degree of the FunctionSpace func: the Function. This has to be something that fe.interpolate can interpolate onto a FunctionSpace or that fe.project can project onto a FunctionSpace. width: the width of the domain on which func is defined. (If not provided, this will be determined from omesh. evenodd: the symmetries of the functions to be constructed evenodd_symmetries(dim) is used if this is not provided """ SS = FunctionSpace(omesh, 'CG', degree) dim = omesh.geometry().dim() if width is None: stats = mesh_stats(omesh) width = stats['xmax'] if evenodd is None: evenodd = evenodd_symmetries(dim) try: f0 = fe.interpolate(func, SS) except TypeError: f0 = fe.project(func, SS) ffuncs = [] flips = evenodd_symmetries(dim) for flip in (flips): fmesh = Mesh(omesh) SSf = FunctionSpace(fmesh, 'CG', degree) ffunc = fe.interpolate(f0, SSf) fmesh.coordinates()[:, :] = (width*flip + (1 - 2*flip)*fmesh.coordinates()) fmesh.bounding_box_tree().build(fmesh) ffuncs.append(ffunc) E = evenodd_matrix(evenodd) components = matmul(2**(-dim)*E, ffuncs) cs = [] for c in components: try: cs.append(fe.interpolate(c, SS)) except TypeError: cs.append(fe.project(c, SS, solver_type='lu')) return(cs)
def gaussian_mesh_randomizer(mesh, percentage, preserve_boundary=True): """ Randomly perturb a given mesh. Args: mesh: Input mesh. percentage: Maximum perturbation in percentage of mesh.hmin(). preserve_boundary: Whether to move the vertices on the boundary. Returns: rmesh: The perturbed mesh. """ rmesh = Mesh(mesh) deltax = (np.random.randn(rmesh.num_vertices(), rmesh.geometry().dim()) * percentage * rmesh.hmin()) if preserve_boundary: boundary_mesh = BoundaryMesh(rmesh, "exterior") boundary_vertices = boundary_mesh.entity_map(0).array() deltax[boundary_vertices] = 0.0 rmesh.coordinates()[:] = rmesh.coordinates() + deltax return rmesh
from common import * from weak_form import * # Set output from FEniCS set_log_active(False) # Set ut problem mesh = Mesh( path.join(rel_path, "mesh", "von_karman_street_FSI_structure_refine2.xml")) # Function space V = VectorFunctionSpace(mesh, "CG", 2) VV = V * V # Get the point [0.2,0.6] at the end of bar for coord in mesh.coordinates(): if coord[0] == 0.6 and (0.2 - DOLFIN_EPS <= coord[1] <= 0.2 + DOLFIN_EPS): #print coord break BarLeftSide = AutoSubDomain(lambda x: "on_boundary" and \ (((x[0] - 0.2) * (x[0] - 0.2) + (x[1] - 0.2) * (x[1] - 0.2) < 0.0505*0.0505 ) and x[1] >= 0.19 \ and x[1] <= 0.21 \ and x[0] > 0.2) ) boundaries = FacetFunction("size_t", mesh) boundaries.set_all(0) BarLeftSide.mark(boundaries, 1)
from fenics import Mesh from visualisation import prepare_frame # ============================================================================= # Define mesh name = 'straight_10' mesh_file = '../xml_files/%s.xml' % name mesh = Mesh(mesh_file) # It is possible to save the mesh in a XDMF file, or even a VTK file #file_results = XDMFFile('output/%s.xdmf' % mesh_name) #file_results.write(mesh) coords = mesh.coordinates() coords_x = coords[:, 0] coords_y = coords[:, 1] coords_z = coords[:, 2] cells = mesh.cells() #============================================================================== def plotIt(): ax = Axes3D(pp.figure(figsize=(5, 3.5))) prepare_frame(ax, lambd=15, phi=50) # Connectivity for c in cells: