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 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