コード例 #1
0
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
コード例 #2
0
 def __init__(self, alpha_params: GeneralizedAlphaParameters,
              time_params: TimeSteppingParameters, fem_solver: FemSolver,
              boundary_excitation: ExternalExcitation,
              field_updates: FieldUpdates, fields: DDDbFields,
              mesh: fenics.Mesh, spaces: Spaces, dddb_file_name: str,
              initial_material_parameters: np.ndarray,
              in_checkpoint_file_name: str, out_checkpoint_file_name: str):
     super().__init__(alpha_params, time_params, fem_solver,
                      boundary_excitation, field_updates, fields)
     self.in_checkpoint_file = XDMFCheckpointHandler(
         file_name=in_checkpoint_file_name,
         append_to_existing=False,
         field=fields.imported_displacement_field,
         field_name=fields.u_new.name())
     self.out_checkpoint_file = XDMFCheckpointHandler(
         file_name=out_checkpoint_file_name,
         append_to_existing=False,
         field=fields.u_new,
         field_name=fields.u_new.name())
     self.v_out_checkpoint_file = XDMFCheckpointHandler(
         file_name='v_{}'.format(out_checkpoint_file_name),
         append_to_existing=False,
         field=fields.v_old,
         field_name=fields.v_old.name())
     self.a_out_checkpoint_file = XDMFCheckpointHandler(
         file_name='a_{}'.format(out_checkpoint_file_name),
         append_to_existing=False,
         field=fields.a_old,
         field_name=fields.a_old.name())
     self.np_files = NPYFile(file_name=dddb_file_name)
     self.tensor_space = spaces.tensor_space
     self.number_of_vertices = mesh.num_vertices()
     self.fields = fields
     initial_values_for_optimizer = np.random.random(
         self.fields.new_constitutive_relation_multiplicative_parameters.
         function_space().dim() - self.fields.function_space.dim())
     self.optimizer = ScipyOptimizer(
         fields=self.fields,
         initial_values=initial_values_for_optimizer,
         fem_solver=fem_solver,
         spaces=spaces)
コード例 #3
0
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