7. traj_recorder
torchfsm.traj_recorder.IntervalController
¤
A class to control the recording of trajectories at specified intervals.
This class can be used as an input for the control_func
parameters of recorder objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval
|
int
|
The interval at which to record the trajectory. |
1
|
start
|
int
|
The step at which to start recording the trajectory. |
0
|
Source code in torchfsm/traj_recorder.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
__init__
¤
__init__(interval: int = 1, start: int = 0) -> None
Source code in torchfsm/traj_recorder.py
17 18 19 |
|
__call__
¤
__call__(step: int) -> bool
Source code in torchfsm/traj_recorder.py
21 22 |
|
torchfsm.traj_recorder._TrajRecorder
¤
A base class for trajectory recorders. A recorder is an object that helps to control the recording of trajectories during a simulation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control_func
|
Optional[Callable[[int], bool]]
|
A function that takes a step as input and returns a boolean indicating whether to record the trajectory at that step. |
None
|
include_initial_state
|
bool
|
If True, the initial state will be included in the trajectory. |
True
|
Source code in torchfsm/traj_recorder.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
trajectory
property
¤
trajectory
Get the recorded trajectory. This method should be implemented by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
return_in_fourier
|
bool
|
If True, return the trajectory in Fourier space. Default is False. |
required |
Returns:
Type | Description |
---|---|
torch.tensor: The recorded trajectory. |
__init__
¤
__init__(
control_func: Optional[Callable[[int], bool]] = None,
include_initial_state: bool = True,
)
Source code in torchfsm/traj_recorder.py
35 36 37 38 39 40 41 42 43 44 |
|
record
¤
record(step: int, frame: torch.tensor)
Record the trajectory at a given step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step
|
int
|
The current step. |
required |
frame
|
tensor
|
The current frame to be recorded. |
required |
Source code in torchfsm/traj_recorder.py
46 47 48 49 50 51 52 53 54 55 |
|
_record
¤
_record(step: int, frame: torch.tensor)
Record the trajectory at a given step. This method should be implemented by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step
|
int
|
The current step. |
required |
frame
|
tensor
|
The current frame to be recorded. |
required |
Source code in torchfsm/traj_recorder.py
57 58 59 60 61 62 63 64 65 66 |
|
_traj_ifft
¤
_traj_ifft(trajectory: torch.tensor)
Perform an inverse FFT on the trajectory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trajectory
|
tensor
|
The trajectory to be transformed. |
required |
Returns:
Type | Description |
---|---|
torch.tensor: The transformed trajectory. |
Source code in torchfsm/traj_recorder.py
68 69 70 71 72 73 74 75 76 77 78 79 |
|
torchfsm.traj_recorder.AutoRecorder
¤
Bases: _TrajRecorder
A recorder that save the trajectory at the same devices as the simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control_func
|
Optional[Callable[[int], bool]]
|
A function that takes a step as input and returns a boolean indicating whether to record the trajectory at that step. |
None
|
include_initial_state
|
bool
|
If True, the initial state will be included in the trajectory. |
True
|
Source code in torchfsm/traj_recorder.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
record
¤
record(step: int, frame: torch.tensor)
Record the trajectory at a given step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step
|
int
|
The current step. |
required |
frame
|
tensor
|
The current frame to be recorded. |
required |
Source code in torchfsm/traj_recorder.py
46 47 48 49 50 51 52 53 54 55 |
|
_traj_ifft
¤
_traj_ifft(trajectory: torch.tensor)
Perform an inverse FFT on the trajectory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trajectory
|
tensor
|
The trajectory to be transformed. |
required |
Returns:
Type | Description |
---|---|
torch.tensor: The transformed trajectory. |
Source code in torchfsm/traj_recorder.py
68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
control_func: Optional[Callable[[int], bool]] = None,
include_initial_state: bool = True,
)
Source code in torchfsm/traj_recorder.py
105 106 107 108 109 110 |
|
_record
¤
_record(step: int, frame: torch.tensor)
Source code in torchfsm/traj_recorder.py
112 113 114 115 116 |
|
torchfsm.traj_recorder.CPURecorder
¤
Bases: AutoRecorder
A recorder that saves the trajectory on the CPU memory. This is useful for large trajectories that may not fit in GPU memory during simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control_func
|
Optional[Callable[[int], bool]]
|
A function that takes a step as input and returns a boolean indicating whether to record the trajectory at that step. |
None
|
include_initial_state
|
bool
|
If True, the initial state will be included in the trajectory. |
True
|
Source code in torchfsm/traj_recorder.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
record
¤
record(step: int, frame: torch.tensor)
Record the trajectory at a given step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step
|
int
|
The current step. |
required |
frame
|
tensor
|
The current frame to be recorded. |
required |
Source code in torchfsm/traj_recorder.py
46 47 48 49 50 51 52 53 54 55 |
|
_traj_ifft
¤
_traj_ifft(trajectory: torch.tensor)
Perform an inverse FFT on the trajectory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trajectory
|
tensor
|
The trajectory to be transformed. |
required |
Returns:
Type | Description |
---|---|
torch.tensor: The transformed trajectory. |
Source code in torchfsm/traj_recorder.py
68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
control_func: Optional[Callable[[int], bool]] = None,
include_initial_state: bool = True,
)
Source code in torchfsm/traj_recorder.py
138 139 140 141 142 |
|
_record
¤
_record(step: int, frame: torch.tensor)
Source code in torchfsm/traj_recorder.py
144 145 146 147 148 |
|
torchfsm.traj_recorder.DiskRecorder
¤
Bases: _TrajRecorder
A recorder that saves the trajectory on the disk. This is useful for large trajectories that may not fit in GPU memory during simulation. The trajectory is saved in a temporary cache and then written to disk at specified intervals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control_func
|
Optional[Callable[[int], bool]]
|
A function that takes a step as input and returns a boolean indicating whether to record the trajectory at that step. |
None
|
include_initial_state
|
bool
|
If True, the initial state will be included in the trajectory. |
True
|
cache_dir
|
Optional[str]
|
The directory where the trajectory will be saved. Default is "./saved_traj/". |
None
|
cache_freq
|
int
|
The frequency at which to save the trajectory to disk. Default is 1. |
1
|
temp_cache_loc
|
Literal['auto', 'cpu']
|
The location of the temporary cache. Default is "cpu". |
'cpu'
|
save_format
|
Literal['numpy', 'torch']
|
The format in which to save the trajectory. Default is "torch". |
'torch'
|
Source code in torchfsm/traj_recorder.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
record
¤
record(step: int, frame: torch.tensor)
Record the trajectory at a given step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step
|
int
|
The current step. |
required |
frame
|
tensor
|
The current frame to be recorded. |
required |
Source code in torchfsm/traj_recorder.py
46 47 48 49 50 51 52 53 54 55 |
|
_traj_ifft
¤
_traj_ifft(trajectory: torch.tensor)
Perform an inverse FFT on the trajectory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trajectory
|
tensor
|
The trajectory to be transformed. |
required |
Returns:
Type | Description |
---|---|
torch.tensor: The transformed trajectory. |
Source code in torchfsm/traj_recorder.py
68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
control_func: Optional[Callable[[int], bool]] = None,
include_initial_state: bool = True,
cache_dir: Optional[str] = None,
cache_freq: int = 1,
temp_cache_loc: Literal["auto", "cpu"] = "cpu",
save_format: Literal["numpy", "torch"] = "torch",
)
Source code in torchfsm/traj_recorder.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
_record
¤
_record(step: int, frame: torch.tensor)
Source code in torchfsm/traj_recorder.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|