src.acoustools.HighLevel.Contexts
1from acoustools.Solvers import wgs 2from acoustools.Utilities import create_points, TRANSDUCERS, transducers, create_board, add_lev_sig 3from acoustools.Visualiser import Visualise, ABC 4from acoustools.Levitator import LevitatorController 5 6from acoustools.Mesh import load_scatterer, translate 7from acoustools.BEM import get_cache_or_compute_H, propagate_BEM_pressure, compute_E 8 9import torch 10from torch import Tensor 11 12from typing import Iterable, Self 13 14from vedo.mesh import Mesh 15 16from vedo.mesh import Mesh 17 18 19class AcousToolsContext(): 20 21 def __init__(self, origin:Iterable|Tensor=(0,0,0)) -> None: 22 ''' 23 Base Class for AcousTools Context Objects \n 24 :param origin: The position of the origin for the setup \n 25 26 Should be used in a `with` block 27 ''' 28 29 if type(origin) is not Tensor: 30 self.origin: Tensor = create_points(1,1,origin[0], origin[1], origin[2]) 31 32 self.lev = None 33 34 self.solution=None 35 36 def __enter__(self) -> Self: 37 return self 38 39 def __exit__(self, exc_type, exc_value, traceback) -> None: 40 if self.lev is not None: 41 self.lev.disconnect() 42 43 44 45 46class TopBottomContext(AcousToolsContext): 47 48 49 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs) -> None: 50 ''' 51 AcousTools Context Objects for 2 boards with a seperation of 23.65cm\n 52 :param origin: The position of the origin for the setup \n 53 :param default_solver: The solver to use when creating a focus, default wgs 54 55 Should be used in a `with` block eg 56 ``` 57 from acoustools.HighLevel import TopBottomContext 58 59 with TopBottomContext() as ctx: 60 ctx.create_focus() 61 ctx.send_solution() 62 ``` 63 ''' 64 65 super().__init__(origin) 66 67 self.solver = default_solver 68 self.board: Tensor = TRANSDUCERS 69 70 def add_lev_sig(self): 71 self.solution = add_lev_sig(self.solution) 72 73 def create_focus(self, location:None|Tensor|Iterable = None) -> None: 74 ''' 75 Create a focus at `location` using the default solver defined in class creation and stores the result in the object. \n 76 The user does not need to know how the focus is created or what the ouput of the computation is \n 77 Should be called before `send_solution` or `visualise` 78 :param location: The location of the focus 79 ''' 80 81 if type(location) not in (None, Tensor, Iterable): 82 raise TypeError("Locations should be one of None, Tensor, Iterable") 83 84 if location is None: 85 location: Tensor = self.origin 86 87 if type(location) is not Tensor: 88 location: Tensor = create_points(1,1,location[0], location[1], location[2]) 89 90 self.solution = self.solver(location, board = self.board) 91 92 def visualise(self, size:float=0.03, origin:Tensor=None, plane:str='xz') -> None: 93 ''' 94 Visualises the sound field that has been previously computed, see `acoustools.Visualise.Visualiser` for parameter details \n 95 :param size: Size of the visualisation window from the origin (window will be 2*size) 96 :param origin: Central point of the visualisation 97 :param plane: Plane to visulaise in 98 ''' 99 if self.solution is None: 100 raise ValueError("No solution found, call `create_focus` before use") 101 abc: tuple[Tensor] = ABC(size, origin=origin, plane=plane) 102 Visualise(*abc, self.solution) 103 104 105 def send_solution(self, ids=(1000,999)) -> None: 106 ''' 107 Sends the solution to a connected physical device 108 :param ids: The ids for the device connected 109 ''' 110 if self.solution is None: 111 raise ValueError("No solution found, call `create_focus` before use") 112 113 if self.lev is None: self.lev = LevitatorController(ids=ids) 114 115 self.lev.levitate(self.solution) 116 117 118 119class TopWithReflectorContext(AcousToolsContext): 120 121 122 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs, path:str='../BEMMedia/', reflector_path:str = './flat-lam2.stl', height:float = 0.12) -> None: 123 ''' 124 AcousTools Context Objects for 1 board above a reflector\n 125 :param origin: The position of the origin for the setup \n 126 :param default_solver: The solver to use when creating a focus, default wgs 127 :param path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache 128 :param reflector_path: The Path to the reflector to use 129 :param height: The seperation to use 130 131 Should be used in a `with` block eg 132 ``` 133 from acoustools.HighLevel import TopWithReflectorContext 134 135 with TopWithReflectorContext() as ctx: 136 tb.create_focus() 137 tb.send_solution() 138 ``` 139 ''' 140 super().__init__(origin) 141 142 self.solver = default_solver 143 self.board: Tensor = create_board(16, z=height/2) 144 145 self.path: str = path 146 self.reflector: Mesh = load_scatterer(path + reflector_path) 147 translate(self.reflector, dz=-1*height/2) 148 149 150 self.H: Tensor = get_cache_or_compute_H(self.reflector, self.board, path=path) 151 152 153 def create_focus(self, location:None|Tensor|Iterable = None, solver_params=None) -> None: 154 ''' 155 Create a focus at `location` using the default solver defined in class creation and stores the result in the object. \n 156 The user does not need to know how the focus is created or what the ouput of the computation is \n 157 Should be called before `send_solution` or `visualise` 158 :param location: The location of the focus 159 ''' 160 if location is None: 161 location: Tensor = self.origin 162 163 if type(location) is not Tensor: 164 location: Tensor = create_points(1,1,location[0], location[1], location[2]) 165 166 167 E: Tensor = compute_E(self.reflector, location, self.board, path=self.path, H=self.H) 168 self.solution = self.solver(location, board = self.board, A=E, **solver_params) 169 170 171 def visualise(self, size:float=0.03, origin:Tensor=None, plane:str='xz') -> None: 172 ''' 173 Visualises the sound field that has been previously computed, see `acoustools.Visualise.Visualiser` for parameter details \n 174 :param size: Size of the visualisation window from the origin (window will be 2*size) 175 :param origin: Central point of the visualisation 176 :param plane: Plane to visulaise in 177 ''' 178 abc: tuple[Tensor] = ABC(size, origin=origin, plane=plane) 179 Visualise(*abc, self.solution, colour_functions=[propagate_BEM_pressure], colour_function_args=[{'path':self.path, "H":self.H, 'board':self.board, 'scatterer':self.reflector}]) 180 181 def send_solution(self, ids=(1000)) -> None: 182 ''' 183 Sends the solution to a connected physical device 184 :param ids: The ids for the device connected 185 ''' 186 if self.lev is None: self.lev = LevitatorController(ids=ids) 187 188 self.lev.levitate(self.solution) 189 190 191 192class BottomWithReflectorContext(TopWithReflectorContext): 193 194 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs, path='../BEMMedia/', reflector_path = './flat-lam2.stl', height = 0.12) -> None: 195 ''' 196 AcousTools Context Objects for 1 board below a reflector\n 197 :param origin: The position of the origin for the setup \n 198 :param default_solver: The solver to use when creating a focus, default wgs 199 :param path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache 200 :param reflector_path: The Path to the reflector to use 201 :param height: The seperation to use 202 203 Should be used in a `with` block eg 204 ``` 205 from acoustools.HighLevel import TopWithReflectorContext 206 207 with TopWithReflectorContext() as ctx: 208 tb.create_focus() 209 tb.send_solution() 210 ``` 211 ''' 212 super().__init__(origin, default_solver, path, reflector_path, height) 213 214 self.board: Tensor = create_board(16, z=-1*height/2) 215 216 self.reflector: Mesh = load_scatterer(path + reflector_path) 217 translate(self.reflector, dz=height/2) 218 219 220 self.H: Tensor = get_cache_or_compute_H(self.reflector, self.board, path=path)
20class AcousToolsContext(): 21 22 def __init__(self, origin:Iterable|Tensor=(0,0,0)) -> None: 23 ''' 24 Base Class for AcousTools Context Objects \n 25 :param origin: The position of the origin for the setup \n 26 27 Should be used in a `with` block 28 ''' 29 30 if type(origin) is not Tensor: 31 self.origin: Tensor = create_points(1,1,origin[0], origin[1], origin[2]) 32 33 self.lev = None 34 35 self.solution=None 36 37 def __enter__(self) -> Self: 38 return self 39 40 def __exit__(self, exc_type, exc_value, traceback) -> None: 41 if self.lev is not None: 42 self.lev.disconnect()
22 def __init__(self, origin:Iterable|Tensor=(0,0,0)) -> None: 23 ''' 24 Base Class for AcousTools Context Objects \n 25 :param origin: The position of the origin for the setup \n 26 27 Should be used in a `with` block 28 ''' 29 30 if type(origin) is not Tensor: 31 self.origin: Tensor = create_points(1,1,origin[0], origin[1], origin[2]) 32 33 self.lev = None 34 35 self.solution=None
Base Class for AcousTools Context Objects
Parameters
- origin: The position of the origin for the setup
Should be used in a with block
47class TopBottomContext(AcousToolsContext): 48 49 50 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs) -> None: 51 ''' 52 AcousTools Context Objects for 2 boards with a seperation of 23.65cm\n 53 :param origin: The position of the origin for the setup \n 54 :param default_solver: The solver to use when creating a focus, default wgs 55 56 Should be used in a `with` block eg 57 ``` 58 from acoustools.HighLevel import TopBottomContext 59 60 with TopBottomContext() as ctx: 61 ctx.create_focus() 62 ctx.send_solution() 63 ``` 64 ''' 65 66 super().__init__(origin) 67 68 self.solver = default_solver 69 self.board: Tensor = TRANSDUCERS 70 71 def add_lev_sig(self): 72 self.solution = add_lev_sig(self.solution) 73 74 def create_focus(self, location:None|Tensor|Iterable = None) -> None: 75 ''' 76 Create a focus at `location` using the default solver defined in class creation and stores the result in the object. \n 77 The user does not need to know how the focus is created or what the ouput of the computation is \n 78 Should be called before `send_solution` or `visualise` 79 :param location: The location of the focus 80 ''' 81 82 if type(location) not in (None, Tensor, Iterable): 83 raise TypeError("Locations should be one of None, Tensor, Iterable") 84 85 if location is None: 86 location: Tensor = self.origin 87 88 if type(location) is not Tensor: 89 location: Tensor = create_points(1,1,location[0], location[1], location[2]) 90 91 self.solution = self.solver(location, board = self.board) 92 93 def visualise(self, size:float=0.03, origin:Tensor=None, plane:str='xz') -> None: 94 ''' 95 Visualises the sound field that has been previously computed, see `acoustools.Visualise.Visualiser` for parameter details \n 96 :param size: Size of the visualisation window from the origin (window will be 2*size) 97 :param origin: Central point of the visualisation 98 :param plane: Plane to visulaise in 99 ''' 100 if self.solution is None: 101 raise ValueError("No solution found, call `create_focus` before use") 102 abc: tuple[Tensor] = ABC(size, origin=origin, plane=plane) 103 Visualise(*abc, self.solution) 104 105 106 def send_solution(self, ids=(1000,999)) -> None: 107 ''' 108 Sends the solution to a connected physical device 109 :param ids: The ids for the device connected 110 ''' 111 if self.solution is None: 112 raise ValueError("No solution found, call `create_focus` before use") 113 114 if self.lev is None: self.lev = LevitatorController(ids=ids) 115 116 self.lev.levitate(self.solution)
50 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs) -> None: 51 ''' 52 AcousTools Context Objects for 2 boards with a seperation of 23.65cm\n 53 :param origin: The position of the origin for the setup \n 54 :param default_solver: The solver to use when creating a focus, default wgs 55 56 Should be used in a `with` block eg 57 ``` 58 from acoustools.HighLevel import TopBottomContext 59 60 with TopBottomContext() as ctx: 61 ctx.create_focus() 62 ctx.send_solution() 63 ``` 64 ''' 65 66 super().__init__(origin) 67 68 self.solver = default_solver 69 self.board: Tensor = TRANSDUCERS
AcousTools Context Objects for 2 boards with a seperation of 23.65cm
Parameters
origin: The position of the origin for the setup
default_solver: The solver to use when creating a focus, default wgs
Should be used in a with block eg
from acoustools.HighLevel import TopBottomContext
with TopBottomContext() as ctx:
ctx.create_focus()
ctx.send_solution()
74 def create_focus(self, location:None|Tensor|Iterable = None) -> None: 75 ''' 76 Create a focus at `location` using the default solver defined in class creation and stores the result in the object. \n 77 The user does not need to know how the focus is created or what the ouput of the computation is \n 78 Should be called before `send_solution` or `visualise` 79 :param location: The location of the focus 80 ''' 81 82 if type(location) not in (None, Tensor, Iterable): 83 raise TypeError("Locations should be one of None, Tensor, Iterable") 84 85 if location is None: 86 location: Tensor = self.origin 87 88 if type(location) is not Tensor: 89 location: Tensor = create_points(1,1,location[0], location[1], location[2]) 90 91 self.solution = self.solver(location, board = self.board)
Create a focus at location using the default solver defined in class creation and stores the result in the object.
The user does not need to know how the focus is created or what the ouput of the computation is
Should be called before send_solution or visualise
Parameters
- location: The location of the focus
93 def visualise(self, size:float=0.03, origin:Tensor=None, plane:str='xz') -> None: 94 ''' 95 Visualises the sound field that has been previously computed, see `acoustools.Visualise.Visualiser` for parameter details \n 96 :param size: Size of the visualisation window from the origin (window will be 2*size) 97 :param origin: Central point of the visualisation 98 :param plane: Plane to visulaise in 99 ''' 100 if self.solution is None: 101 raise ValueError("No solution found, call `create_focus` before use") 102 abc: tuple[Tensor] = ABC(size, origin=origin, plane=plane) 103 Visualise(*abc, self.solution)
Visualises the sound field that has been previously computed, see acoustools.Visualise.Visualiser for parameter details
Parameters
- size: Size of the visualisation window from the origin (window will be 2*size)
- origin: Central point of the visualisation
- plane: Plane to visulaise in
106 def send_solution(self, ids=(1000,999)) -> None: 107 ''' 108 Sends the solution to a connected physical device 109 :param ids: The ids for the device connected 110 ''' 111 if self.solution is None: 112 raise ValueError("No solution found, call `create_focus` before use") 113 114 if self.lev is None: self.lev = LevitatorController(ids=ids) 115 116 self.lev.levitate(self.solution)
Sends the solution to a connected physical device
Parameters
- ids: The ids for the device connected
Inherited Members
120class TopWithReflectorContext(AcousToolsContext): 121 122 123 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs, path:str='../BEMMedia/', reflector_path:str = './flat-lam2.stl', height:float = 0.12) -> None: 124 ''' 125 AcousTools Context Objects for 1 board above a reflector\n 126 :param origin: The position of the origin for the setup \n 127 :param default_solver: The solver to use when creating a focus, default wgs 128 :param path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache 129 :param reflector_path: The Path to the reflector to use 130 :param height: The seperation to use 131 132 Should be used in a `with` block eg 133 ``` 134 from acoustools.HighLevel import TopWithReflectorContext 135 136 with TopWithReflectorContext() as ctx: 137 tb.create_focus() 138 tb.send_solution() 139 ``` 140 ''' 141 super().__init__(origin) 142 143 self.solver = default_solver 144 self.board: Tensor = create_board(16, z=height/2) 145 146 self.path: str = path 147 self.reflector: Mesh = load_scatterer(path + reflector_path) 148 translate(self.reflector, dz=-1*height/2) 149 150 151 self.H: Tensor = get_cache_or_compute_H(self.reflector, self.board, path=path) 152 153 154 def create_focus(self, location:None|Tensor|Iterable = None, solver_params=None) -> None: 155 ''' 156 Create a focus at `location` using the default solver defined in class creation and stores the result in the object. \n 157 The user does not need to know how the focus is created or what the ouput of the computation is \n 158 Should be called before `send_solution` or `visualise` 159 :param location: The location of the focus 160 ''' 161 if location is None: 162 location: Tensor = self.origin 163 164 if type(location) is not Tensor: 165 location: Tensor = create_points(1,1,location[0], location[1], location[2]) 166 167 168 E: Tensor = compute_E(self.reflector, location, self.board, path=self.path, H=self.H) 169 self.solution = self.solver(location, board = self.board, A=E, **solver_params) 170 171 172 def visualise(self, size:float=0.03, origin:Tensor=None, plane:str='xz') -> None: 173 ''' 174 Visualises the sound field that has been previously computed, see `acoustools.Visualise.Visualiser` for parameter details \n 175 :param size: Size of the visualisation window from the origin (window will be 2*size) 176 :param origin: Central point of the visualisation 177 :param plane: Plane to visulaise in 178 ''' 179 abc: tuple[Tensor] = ABC(size, origin=origin, plane=plane) 180 Visualise(*abc, self.solution, colour_functions=[propagate_BEM_pressure], colour_function_args=[{'path':self.path, "H":self.H, 'board':self.board, 'scatterer':self.reflector}]) 181 182 def send_solution(self, ids=(1000)) -> None: 183 ''' 184 Sends the solution to a connected physical device 185 :param ids: The ids for the device connected 186 ''' 187 if self.lev is None: self.lev = LevitatorController(ids=ids) 188 189 self.lev.levitate(self.solution)
123 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs, path:str='../BEMMedia/', reflector_path:str = './flat-lam2.stl', height:float = 0.12) -> None: 124 ''' 125 AcousTools Context Objects for 1 board above a reflector\n 126 :param origin: The position of the origin for the setup \n 127 :param default_solver: The solver to use when creating a focus, default wgs 128 :param path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache 129 :param reflector_path: The Path to the reflector to use 130 :param height: The seperation to use 131 132 Should be used in a `with` block eg 133 ``` 134 from acoustools.HighLevel import TopWithReflectorContext 135 136 with TopWithReflectorContext() as ctx: 137 tb.create_focus() 138 tb.send_solution() 139 ``` 140 ''' 141 super().__init__(origin) 142 143 self.solver = default_solver 144 self.board: Tensor = create_board(16, z=height/2) 145 146 self.path: str = path 147 self.reflector: Mesh = load_scatterer(path + reflector_path) 148 translate(self.reflector, dz=-1*height/2) 149 150 151 self.H: Tensor = get_cache_or_compute_H(self.reflector, self.board, path=path)
AcousTools Context Objects for 1 board above a reflector
Parameters
origin: The position of the origin for the setup
default_solver: The solver to use when creating a focus, default wgs
- path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache
- reflector_path: The Path to the reflector to use
- height: The seperation to use
Should be used in a with block eg
from acoustools.HighLevel import TopWithReflectorContext
with TopWithReflectorContext() as ctx:
tb.create_focus()
tb.send_solution()
154 def create_focus(self, location:None|Tensor|Iterable = None, solver_params=None) -> None: 155 ''' 156 Create a focus at `location` using the default solver defined in class creation and stores the result in the object. \n 157 The user does not need to know how the focus is created or what the ouput of the computation is \n 158 Should be called before `send_solution` or `visualise` 159 :param location: The location of the focus 160 ''' 161 if location is None: 162 location: Tensor = self.origin 163 164 if type(location) is not Tensor: 165 location: Tensor = create_points(1,1,location[0], location[1], location[2]) 166 167 168 E: Tensor = compute_E(self.reflector, location, self.board, path=self.path, H=self.H) 169 self.solution = self.solver(location, board = self.board, A=E, **solver_params)
Create a focus at location using the default solver defined in class creation and stores the result in the object.
The user does not need to know how the focus is created or what the ouput of the computation is
Should be called before send_solution or visualise
Parameters
- location: The location of the focus
172 def visualise(self, size:float=0.03, origin:Tensor=None, plane:str='xz') -> None: 173 ''' 174 Visualises the sound field that has been previously computed, see `acoustools.Visualise.Visualiser` for parameter details \n 175 :param size: Size of the visualisation window from the origin (window will be 2*size) 176 :param origin: Central point of the visualisation 177 :param plane: Plane to visulaise in 178 ''' 179 abc: tuple[Tensor] = ABC(size, origin=origin, plane=plane) 180 Visualise(*abc, self.solution, colour_functions=[propagate_BEM_pressure], colour_function_args=[{'path':self.path, "H":self.H, 'board':self.board, 'scatterer':self.reflector}])
Visualises the sound field that has been previously computed, see acoustools.Visualise.Visualiser for parameter details
Parameters
- size: Size of the visualisation window from the origin (window will be 2*size)
- origin: Central point of the visualisation
- plane: Plane to visulaise in
182 def send_solution(self, ids=(1000)) -> None: 183 ''' 184 Sends the solution to a connected physical device 185 :param ids: The ids for the device connected 186 ''' 187 if self.lev is None: self.lev = LevitatorController(ids=ids) 188 189 self.lev.levitate(self.solution)
Sends the solution to a connected physical device
Parameters
- ids: The ids for the device connected
Inherited Members
193class BottomWithReflectorContext(TopWithReflectorContext): 194 195 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs, path='../BEMMedia/', reflector_path = './flat-lam2.stl', height = 0.12) -> None: 196 ''' 197 AcousTools Context Objects for 1 board below a reflector\n 198 :param origin: The position of the origin for the setup \n 199 :param default_solver: The solver to use when creating a focus, default wgs 200 :param path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache 201 :param reflector_path: The Path to the reflector to use 202 :param height: The seperation to use 203 204 Should be used in a `with` block eg 205 ``` 206 from acoustools.HighLevel import TopWithReflectorContext 207 208 with TopWithReflectorContext() as ctx: 209 tb.create_focus() 210 tb.send_solution() 211 ``` 212 ''' 213 super().__init__(origin, default_solver, path, reflector_path, height) 214 215 self.board: Tensor = create_board(16, z=-1*height/2) 216 217 self.reflector: Mesh = load_scatterer(path + reflector_path) 218 translate(self.reflector, dz=height/2) 219 220 221 self.H: Tensor = get_cache_or_compute_H(self.reflector, self.board, path=path)
195 def __init__(self, origin:Iterable|Tensor=(0,0,0), default_solver=wgs, path='../BEMMedia/', reflector_path = './flat-lam2.stl', height = 0.12) -> None: 196 ''' 197 AcousTools Context Objects for 1 board below a reflector\n 198 :param origin: The position of the origin for the setup \n 199 :param default_solver: The solver to use when creating a focus, default wgs 200 :param path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache 201 :param reflector_path: The Path to the reflector to use 202 :param height: The seperation to use 203 204 Should be used in a `with` block eg 205 ``` 206 from acoustools.HighLevel import TopWithReflectorContext 207 208 with TopWithReflectorContext() as ctx: 209 tb.create_focus() 210 tb.send_solution() 211 ``` 212 ''' 213 super().__init__(origin, default_solver, path, reflector_path, height) 214 215 self.board: Tensor = create_board(16, z=-1*height/2) 216 217 self.reflector: Mesh = load_scatterer(path + reflector_path) 218 translate(self.reflector, dz=height/2) 219 220 221 self.H: Tensor = get_cache_or_compute_H(self.reflector, self.board, path=path)
AcousTools Context Objects for 1 board below a reflector
Parameters
origin: The position of the origin for the setup
default_solver: The solver to use when creating a focus, default wgs
- path: The path where BEM Medis is stored, this folder should have a subfolder called BEMCache
- reflector_path: The Path to the reflector to use
- height: The seperation to use
Should be used in a with block eg
from acoustools.HighLevel import TopWithReflectorContext
with TopWithReflectorContext() as ctx:
tb.create_focus()
tb.send_solution()