src.acoustools.Mesh.CHIEF

  1from acoustools.Utilities import device, DTYPE
  2import acoustools.Constants as Constants
  3
  4import torch
  5from torch import Tensor
  6from vedo import Mesh
  7
  8from typing import Literal
  9
 10from acoustools.Mesh.Features import get_normals_as_points, get_centre_of_mass_as_points, get_diameter
 11from acoustools.Mesh.Load import load_scatterer, merge_scatterers
 12from acoustools.Mesh.Transform import centre_scatterer, translate, scale_to_diameter
 13
 14def get_CHIEF_points(scatterer:Mesh, P=30, method:Literal['random', 'uniform', 'volume-random', 'tetra-random']='random', start:Literal['surface', 'centre']='surface', scale=0.001, scale_mode:Literal['abs','diameter-scale']='abs') -> Mesh:
 15    '''
 16    Generates internal points that can be used for the CHIEF BEM formulation (or any other reason)\n
 17    :param scatterer: The scatterer to insert points into
 18    :param P: Number of points. if P=-1 then P= number of mesh elements
 19    :param method: The method used to generate points \n
 20        - random: will move scale metres along each of P randomly selected normals \n
 21        - uniform:  will move scale metres along each of P uniformly spaced normals (based on order coming from `Mesh.get_normals_as_points`) \n
 22        - volume-random: will use `vedo.Mesh..generate_random_points` to generate P internal points
 23    :param start: The point to use as the basis for generating points \n
 24         - surface: Will step along normals from surface (will step in the -ve normal direction)
 25         - centre: Will step along normal from centre of mass (will step in +ve normal direction)
 26    :param scale: The distance in m to step 
 27    :returns internal points:
 28    '''
 29
 30    centre_norms = get_normals_as_points(scatterer, permute_to_points=False)
 31
 32    if scale_mode.lower() == 'diameter-scale':
 33        d = get_diameter(scatterer)
 34        scale = scale * d
 35    
 36
 37    if start.lower() == 'centre':
 38        centres = get_centre_of_mass_as_points(scatterer, permute_to_points=False).unsqueeze(1)
 39        internal_points = centres + centre_norms * scale      
 40
 41    else:
 42        centres = torch.tensor(scatterer.cell_centers().points, dtype=DTYPE, device=device)
 43        internal_points = centres - centre_norms * scale
 44
 45    M = centre_norms.shape[1]
 46    
 47    if P == -1: P = M
 48
 49   
 50
 51
 52    
 53    if method.lower() == 'random':
 54        indices = torch.randperm(M)[:P]
 55        internal_points = internal_points[:, indices,:]
 56
 57    elif method.lower()== 'uniform':
 58        idx = [i for i in range(M) if i%(int(M/P)) == 0]
 59        internal_points = internal_points[:, idx,:]
 60    elif method.lower() == 'volume-random':
 61        internal_points = torch.Tensor(scatterer.generate_random_points(P).points).unsqueeze(0)
 62    elif method.lower() == 'tetra-random':
 63        tetra = get_tetra_centroids(scatterer, side=0.01)
 64        indices = torch.randperm(tetra.shape[2])[:P]
 65        internal_points = tetra[:,:,indices]
 66        return internal_points
 67    elif method.lower() == 'tetra-all':
 68        tetra = get_tetra_centroids(scatterer)
 69        return tetra
 70
 71    internal_points = internal_points.permute(0,2,1)
 72
 73
 74    return internal_points
 75
 76
 77def get_tetra_centroids(scatterer:Mesh, side=0.02) -> Tensor:
 78
 79    tetra = scatterer.tetralize(side=side)
 80    
 81    cell_centres = tetra.cell_centers()
 82    
 83    
 84    centres =  torch.tensor(cell_centres.points).to(DTYPE).to(device)
 85    
 86
 87    return centres.T.unsqueeze_(0)
 88
 89
 90
 91
 92def insert_parasite(scatterer:Mesh, parasite_path:str = '/Sphere-lam1.stl', root_path:str="../BEMMedia", parasite_size:float=Constants.wavelength/4, parasite_offset:Tensor=None) -> Mesh:
 93    '''
 94    Inserts a parasitic body into an existing scatterer. Used to supress the resonance from BEM \n
 95    See https://doi.org/10.1109/8.310000 \n
 96    :param scatterer: The scatterer to insert parasite into
 97    :param parasite_path: The path to the mesh to load and use as parasite
 98    :param root_path: The folder to load the file from
 99    :param parasite_size: The diameter to scale the parasite to
100    :param parasite_offset: Tensor of offsets for the parasite from the (0,0,0) point
101    :returns: Scatterer with parasite inserted
102    '''
103    parasite = load_scatterer(parasite_path, root_path=root_path)
104    centre_scatterer(parasite)
105    if parasite_offset is None:
106        parasite_offset = get_centre_of_mass_as_points(scatterer)
107
108    dx = parasite_offset[:,0].item()
109    dy = parasite_offset[:,1].item()
110    dz = parasite_offset[:,2].item()
111
112    translate(parasite, dx=dx, dy=dy, dz=dz)
113
114    scale_to_diameter(parasite, parasite_size)
115
116    infected_scatterer = merge_scatterers(scatterer, parasite)
117
118    return infected_scatterer
def get_CHIEF_points( scatterer: vedo.mesh.core.Mesh, P=30, method: Literal['random', 'uniform', 'volume-random', 'tetra-random'] = 'random', start: Literal['surface', 'centre'] = 'surface', scale=0.001, scale_mode: Literal['abs', 'diameter-scale'] = 'abs') -> vedo.mesh.core.Mesh:
17def get_CHIEF_points(scatterer:Mesh, P=30, method:Literal['random', 'uniform', 'volume-random', 'tetra-random']='random', start:Literal['surface', 'centre']='surface', scale=0.001, scale_mode:Literal['abs','diameter-scale']='abs') -> Mesh:
18    '''
19    Generates internal points that can be used for the CHIEF BEM formulation (or any other reason)\n
20    :param scatterer: The scatterer to insert points into
21    :param P: Number of points. if P=-1 then P= number of mesh elements
22    :param method: The method used to generate points \n
23        - random: will move scale metres along each of P randomly selected normals \n
24        - uniform:  will move scale metres along each of P uniformly spaced normals (based on order coming from `Mesh.get_normals_as_points`) \n
25        - volume-random: will use `vedo.Mesh..generate_random_points` to generate P internal points
26    :param start: The point to use as the basis for generating points \n
27         - surface: Will step along normals from surface (will step in the -ve normal direction)
28         - centre: Will step along normal from centre of mass (will step in +ve normal direction)
29    :param scale: The distance in m to step 
30    :returns internal points:
31    '''
32
33    centre_norms = get_normals_as_points(scatterer, permute_to_points=False)
34
35    if scale_mode.lower() == 'diameter-scale':
36        d = get_diameter(scatterer)
37        scale = scale * d
38    
39
40    if start.lower() == 'centre':
41        centres = get_centre_of_mass_as_points(scatterer, permute_to_points=False).unsqueeze(1)
42        internal_points = centres + centre_norms * scale      
43
44    else:
45        centres = torch.tensor(scatterer.cell_centers().points, dtype=DTYPE, device=device)
46        internal_points = centres - centre_norms * scale
47
48    M = centre_norms.shape[1]
49    
50    if P == -1: P = M
51
52   
53
54
55    
56    if method.lower() == 'random':
57        indices = torch.randperm(M)[:P]
58        internal_points = internal_points[:, indices,:]
59
60    elif method.lower()== 'uniform':
61        idx = [i for i in range(M) if i%(int(M/P)) == 0]
62        internal_points = internal_points[:, idx,:]
63    elif method.lower() == 'volume-random':
64        internal_points = torch.Tensor(scatterer.generate_random_points(P).points).unsqueeze(0)
65    elif method.lower() == 'tetra-random':
66        tetra = get_tetra_centroids(scatterer, side=0.01)
67        indices = torch.randperm(tetra.shape[2])[:P]
68        internal_points = tetra[:,:,indices]
69        return internal_points
70    elif method.lower() == 'tetra-all':
71        tetra = get_tetra_centroids(scatterer)
72        return tetra
73
74    internal_points = internal_points.permute(0,2,1)
75
76
77    return internal_points

Generates internal points that can be used for the CHIEF BEM formulation (or any other reason)

Parameters
  • scatterer: The scatterer to insert points into
  • P: Number of points. if P=-1 then P= number of mesh elements
  • method: The method used to generate points

    • random: will move scale metres along each of P randomly selected normals

    • uniform: will move scale metres along each of P uniformly spaced normals (based on order coming from Mesh.get_normals_as_points)

    • volume-random: will use vedo.Mesh..generate_random_points to generate P internal points

  • start: The point to use as the basis for generating points

    • surface: Will step along normals from surface (will step in the -ve normal direction)
    • centre: Will step along normal from centre of mass (will step in +ve normal direction)
  • scale: The distance in m to step :returns internal points:
def get_tetra_centroids(scatterer: vedo.mesh.core.Mesh, side=0.02) -> torch.Tensor:
80def get_tetra_centroids(scatterer:Mesh, side=0.02) -> Tensor:
81
82    tetra = scatterer.tetralize(side=side)
83    
84    cell_centres = tetra.cell_centers()
85    
86    
87    centres =  torch.tensor(cell_centres.points).to(DTYPE).to(device)
88    
89
90    return centres.T.unsqueeze_(0)
def insert_parasite( scatterer: vedo.mesh.core.Mesh, parasite_path: str = '/Sphere-lam1.stl', root_path: str = '../BEMMedia', parasite_size: float = 0.00214375, parasite_offset: torch.Tensor = None) -> vedo.mesh.core.Mesh:
 95def insert_parasite(scatterer:Mesh, parasite_path:str = '/Sphere-lam1.stl', root_path:str="../BEMMedia", parasite_size:float=Constants.wavelength/4, parasite_offset:Tensor=None) -> Mesh:
 96    '''
 97    Inserts a parasitic body into an existing scatterer. Used to supress the resonance from BEM \n
 98    See https://doi.org/10.1109/8.310000 \n
 99    :param scatterer: The scatterer to insert parasite into
100    :param parasite_path: The path to the mesh to load and use as parasite
101    :param root_path: The folder to load the file from
102    :param parasite_size: The diameter to scale the parasite to
103    :param parasite_offset: Tensor of offsets for the parasite from the (0,0,0) point
104    :returns: Scatterer with parasite inserted
105    '''
106    parasite = load_scatterer(parasite_path, root_path=root_path)
107    centre_scatterer(parasite)
108    if parasite_offset is None:
109        parasite_offset = get_centre_of_mass_as_points(scatterer)
110
111    dx = parasite_offset[:,0].item()
112    dy = parasite_offset[:,1].item()
113    dz = parasite_offset[:,2].item()
114
115    translate(parasite, dx=dx, dy=dy, dz=dz)
116
117    scale_to_diameter(parasite, parasite_size)
118
119    infected_scatterer = merge_scatterers(scatterer, parasite)
120
121    return infected_scatterer

Inserts a parasitic body into an existing scatterer. Used to supress the resonance from BEM

See https://doi.org/10.1109/8.310000

Parameters
  • scatterer: The scatterer to insert parasite into
  • parasite_path: The path to the mesh to load and use as parasite
  • root_path: The folder to load the file from
  • parasite_size: The diameter to scale the parasite to
  • parasite_offset: Tensor of offsets for the parasite from the (0,0,0) point :returns: Scatterer with parasite inserted