src.acoustools.Export.lcode

Export points to lcode Each line starts with a command (see below) followed by the arguments for that command. The command should be followed by a colon (:). Each line should end with a semi-colon (;), each argument is seperated by a comma (,) and groups or arguments can be seperated with a colon (:)

Commands

  • L0:<X> <Y> <Z>; Create Focal Point at (X,Y,Z)
  • L1:<X> <Y> <Z>; Create Trap Point at (X,Y,Z)
  • L2:<X> <Y> <Z>; Create Twin Trap Point at (X,Y,Z)
  • L3:<X> <Y> <Z>; Create Vortex Trap Point at (X,Y,Z)
  • L4; Turn off Transducers
  • C0;Dispense Droplet
  • C1; Activate UV
  • C2; Turn off UV
  • C3:<T>; Delay for T ms
  • C4:<T>; Set delay for T ms between all commands
  • C5:<Solver>; Change to specific solver. Should be one of "IB", "WGS", "GSPAT", "NAIVE", "GORKOV_TARGET"
  • C6:I<I>:U<U>:P<P>; Set parameters for the solver, I: Iterations. U:target Gorkov, P:Target Pressure. Note not all solvers support all options
  • C7; Set to two board setup
  • C8; Set to top board setup
  • C9; Set to bottom board setup
  • C10; Update BEM to use layer at last z position
  • C11:<Frame-rate>; Set the framerate of the levitator device
  • C12:<z>; Use a reflector and set the position

  • O0; End of droplet

  • function F<x> ... end define a function that can latter be called by name

  1'''
  2Export points to lcode 
  3Each line starts with a command (see below) followed by the arguments for that command. The command should be followed by a colon (:). 
  4Each line should end with a semi-colon (;), each argument is seperated by a comma (,) and groups or arguments can be seperated with a colon (:)
  5
  6Commands
  7* `L0:<X> <Y> <Z>;` Create Focal Point at (X,Y,Z)
  8* `L1:<X> <Y> <Z>;` Create Trap Point at (X,Y,Z)
  9* `L2:<X> <Y> <Z>;` Create Twin Trap Point at (X,Y,Z)
 10* `L3:<X> <Y> <Z>;` Create Vortex Trap Point at (X,Y,Z)
 11* `L4;` Turn off Transducers
 12
 13* `C0;`Dispense Droplet
 14* `C1;` Activate UV
 15* `C2;` Turn off UV
 16* `C3:<T>;` Delay for T ms
 17* `C4:<T>;` Set delay for T ms between all commands
 18* `C5:<Solver>;` Change to specific solver. Should be one of "IB", "WGS", "GSPAT", "NAIVE", "GORKOV_TARGET"
 19* `C6:I<I>:U<U>:P<P>;` Set parameters for the solver, I: Iterations. U:target Gorkov, P:Target Pressure. Note not all solvers support all options
 20* `C7;` Set to two board setup
 21* `C8;` Set to top board setup
 22* `C9;` Set to bottom board setup
 23* `C10;` Update BEM to use layer at last z position 
 24* `C11:<Frame-rate>;` Set the framerate of the levitator device
 25* `C12:<z>;` Use a reflector and set the position
 26
 27* `O0;` End of droplet
 28
 29* `function F<x>
 30...
 31end` define a function that can latter be called by name
 32'''
 33
 34from torch import Tensor
 35from typing import Literal
 36from types import FunctionType
 37
 38from acoustools.Solvers import wgs, iterative_backpropagation, gspat, naive, gorkov_target
 39from acoustools.Utilities import TOP_BOARD, BOTTOM_BOARD, TRANSDUCERS
 40
 41def point_to_lcode(points:Tensor|list[Tensor], sig_type:Literal['Focal', 'Trap', 'Vortex','Twin']='Focal') -> str:
 42    '''
 43    Converts AcousTools points to lcode string \n
 44    :param points: The points to export. Each batch will be a line in the reuslting lcode
 45    :param sig_type: The type of trap to create (defines L-command to use)
 46    :returns lcode: Lcode as a string
 47    '''
 48
 49
 50
 51    l_command = {'Focal':'L0','Trap':'L1','Twin':'L2','Vortex':'L3'}[sig_type.capitalize()]
 52
 53    lcode = ''
 54    if type(points) == Tensor:
 55         points = [points,]
 56    for batches in points:    
 57        for batch in batches: #Each batch should be a line
 58            N = batch.shape[1]
 59            lcode += '' + l_command
 60        
 61            for i in range(N):
 62                    lcode += ':'
 63                    p = batch[:,i]
 64                    command = str(p[0].item()) + "," + str(p[1].item()) + ',' + str(p[2].item())
 65
 66                    lcode += command
 67            
 68            lcode += ';\n'
 69
 70    return lcode.rstrip()
 71
 72def get_setup_commands(solver:FunctionType|None|str = None, I:int=200, U:float|None=None, P:int|None=None,
 73                       board:Tensor|None=None, frame_rate:int=200, flat_reflector_z:int|None=None) -> str:
 74    '''
 75    Creates setup commands
 76    :param Solver: String name or function of the solver to use
 77    :param I: Iterations
 78    :param U: Target Gorkov Value
 79    :param P: Target Pressure
 80    :param board: Board to use
 81    :param frame_rate: Levitator frame rate
 82    :param flat_reflector_z: if not None will use flat reflrctor at z position given\n
 83    :returns lcode:
 84    '''
 85
 86    command = ''
 87    #Solver -> C5
 88    if solver is None:
 89        command += 'C5:WGS;\n'
 90    else:
 91        if type(solver) == FunctionType:
 92            solver = {iterative_backpropagation:"IB", wgs:"WGS", gspat:"GSPAT", naive:"NAIVE", gorkov_target:"GORKOV_TARGET"}[solver]
 93        command += f'C5:{solver};\n'
 94
 95    #solver params
 96    command += f"C6:I{I}"
 97    if U is not None:
 98         command += f":U{U}"
 99    if P is not None:
100         command += f":P{P}"
101    command += ';\n'
102
103    #Board 
104    if board is None or ((board.shape == TRANSDUCERS.shape) and  (board == TRANSDUCERS).all()):
105        command += 'C7;\n'
106    
107    elif (board.shape == TOP_BOARD.shape) and  (board == TOP_BOARD).all():
108        command += 'C8;\n'
109    
110    elif (board.shape == BOTTOM_BOARD.shape) and (board == BOTTOM_BOARD).all():
111        command += 'C9;\n'
112    else:
113         raise ValueError("Unknown board")
114    
115    #Frame Rate
116    command += f"C11:{frame_rate};\n"
117
118    #Reflector
119    if flat_reflector_z is not None:
120         command += f"C12:{flat_reflector_z};\n"
121
122    return command.rstrip()
123
124
125def export_to_lcode(fname, points:Tensor, sig_type:Literal['Focal', 'Trap', 'Vortex','Twin']='Focal',
126                    solver:FunctionType|None|str = None, I:int=200, U:float|None=None, P:int|None=None,
127                    board:Tensor|None=None, frame_rate:int=200, flat_reflector_z:int|None=None):
128    
129    setup_commands = get_setup_commands(solver, I, U, P,board, frame_rate, flat_reflector_z)
130    point_command = point_to_lcode(points, sig_type)
131
132    lcode = setup_commands + '\n' + point_command
133
134    with open(fname,'w') as f:
135         f.write(lcode)
136
137         
def point_to_lcode( points: torch.Tensor | list[torch.Tensor], sig_type: Literal['Focal', 'Trap', 'Vortex', 'Twin'] = 'Focal') -> str:
42def point_to_lcode(points:Tensor|list[Tensor], sig_type:Literal['Focal', 'Trap', 'Vortex','Twin']='Focal') -> str:
43    '''
44    Converts AcousTools points to lcode string \n
45    :param points: The points to export. Each batch will be a line in the reuslting lcode
46    :param sig_type: The type of trap to create (defines L-command to use)
47    :returns lcode: Lcode as a string
48    '''
49
50
51
52    l_command = {'Focal':'L0','Trap':'L1','Twin':'L2','Vortex':'L3'}[sig_type.capitalize()]
53
54    lcode = ''
55    if type(points) == Tensor:
56         points = [points,]
57    for batches in points:    
58        for batch in batches: #Each batch should be a line
59            N = batch.shape[1]
60            lcode += '' + l_command
61        
62            for i in range(N):
63                    lcode += ':'
64                    p = batch[:,i]
65                    command = str(p[0].item()) + "," + str(p[1].item()) + ',' + str(p[2].item())
66
67                    lcode += command
68            
69            lcode += ';\n'
70
71    return lcode.rstrip()

Converts AcousTools points to lcode string

Parameters
  • points: The points to export. Each batch will be a line in the reuslting lcode
  • sig_type: The type of trap to create (defines L-command to use) :returns lcode: Lcode as a string
def get_setup_commands( solver: function | None | str = None, I: int = 200, U: float | None = None, P: int | None = None, board: torch.Tensor | None = None, frame_rate: int = 200, flat_reflector_z: int | None = None) -> str:
 73def get_setup_commands(solver:FunctionType|None|str = None, I:int=200, U:float|None=None, P:int|None=None,
 74                       board:Tensor|None=None, frame_rate:int=200, flat_reflector_z:int|None=None) -> str:
 75    '''
 76    Creates setup commands
 77    :param Solver: String name or function of the solver to use
 78    :param I: Iterations
 79    :param U: Target Gorkov Value
 80    :param P: Target Pressure
 81    :param board: Board to use
 82    :param frame_rate: Levitator frame rate
 83    :param flat_reflector_z: if not None will use flat reflrctor at z position given\n
 84    :returns lcode:
 85    '''
 86
 87    command = ''
 88    #Solver -> C5
 89    if solver is None:
 90        command += 'C5:WGS;\n'
 91    else:
 92        if type(solver) == FunctionType:
 93            solver = {iterative_backpropagation:"IB", wgs:"WGS", gspat:"GSPAT", naive:"NAIVE", gorkov_target:"GORKOV_TARGET"}[solver]
 94        command += f'C5:{solver};\n'
 95
 96    #solver params
 97    command += f"C6:I{I}"
 98    if U is not None:
 99         command += f":U{U}"
100    if P is not None:
101         command += f":P{P}"
102    command += ';\n'
103
104    #Board 
105    if board is None or ((board.shape == TRANSDUCERS.shape) and  (board == TRANSDUCERS).all()):
106        command += 'C7;\n'
107    
108    elif (board.shape == TOP_BOARD.shape) and  (board == TOP_BOARD).all():
109        command += 'C8;\n'
110    
111    elif (board.shape == BOTTOM_BOARD.shape) and (board == BOTTOM_BOARD).all():
112        command += 'C9;\n'
113    else:
114         raise ValueError("Unknown board")
115    
116    #Frame Rate
117    command += f"C11:{frame_rate};\n"
118
119    #Reflector
120    if flat_reflector_z is not None:
121         command += f"C12:{flat_reflector_z};\n"
122
123    return command.rstrip()

Creates setup commands

Parameters
  • Solver: String name or function of the solver to use
  • I: Iterations
  • U: Target Gorkov Value
  • P: Target Pressure
  • board: Board to use
  • frame_rate: Levitator frame rate
  • flat_reflector_z: if not None will use flat reflrctor at z position given

:returns lcode:

def export_to_lcode( fname, points: torch.Tensor, sig_type: Literal['Focal', 'Trap', 'Vortex', 'Twin'] = 'Focal', solver: function | None | str = None, I: int = 200, U: float | None = None, P: int | None = None, board: torch.Tensor | None = None, frame_rate: int = 200, flat_reflector_z: int | None = None):
126def export_to_lcode(fname, points:Tensor, sig_type:Literal['Focal', 'Trap', 'Vortex','Twin']='Focal',
127                    solver:FunctionType|None|str = None, I:int=200, U:float|None=None, P:int|None=None,
128                    board:Tensor|None=None, frame_rate:int=200, flat_reflector_z:int|None=None):
129    
130    setup_commands = get_setup_commands(solver, I, U, P,board, frame_rate, flat_reflector_z)
131    point_command = point_to_lcode(points, sig_type)
132
133    lcode = setup_commands + '\n' + point_command
134
135    with open(fname,'w') as f:
136         f.write(lcode)