9. utils
torchfsm.utils.clean_up_memory
¤
clean_up_memory()
Clean up the memory by calling garbage collector and emptying the cache.
Source code in torchfsm/_utils.py
43 44 45 46 47 48 | |
torchfsm.utils.print_gpu_memory
¤
print_gpu_memory(prefix='', device='cuda:1')
Source code in torchfsm/_utils.py
50 51 52 53 | |
torchfsm.utils.statistics_traj
¤
statistics_traj(
traj: ValueList[Union[Tensor, ndarray]],
) -> Tuple[float, float, float, float]
Compute the mean, std, min, and max of a trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj
|
ValueList[Union[Tensor, ndarray]]
|
The trajectory to compute statistics for. The trajectory can be a list of tensors or numpy arrays, or a single tensor or numpy array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[float, float, float, float]
|
A tuple containing the mean, std, min, and max of the trajectory. The mean, std, min, and max are computed along the first dimension of the trajectory. |
Source code in torchfsm/utils/traj_manipulate.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
torchfsm.utils.randomly_clip_traj
¤
randomly_clip_traj(
traj: Union[
SpatialTensor["B T C H ..."],
SpatialArray["B T C H ..."],
FourierTensor["B T C H ..."],
FourierArray["B T C H ..."],
],
length: int,
) -> Union[
SpatialTensor["B length C H ..."],
SpatialArray["B length C H ..."],
FourierTensor["B length C H ..."],
FourierArray["B length C H ..."],
]
Randomly clip a trajectory to a specified length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj
|
Union[SpatialTensor, SpatialArray, FourierTensor, FourierArray]
|
The trajectory to clip. The trajectory can be a tensor or numpy array with shape [B, T, C, H, ...]. |
required |
length
|
int
|
The length to clip the trajectory to. The length should be less than the original length of the trajectory. |
required |
Returns:
| Type | Description |
|---|---|
Union[SpatialTensor['B length C H ...'], SpatialArray['B length C H ...'], FourierTensor['B length C H ...'], FourierArray['B length C H ...']]
|
Union[SpatialTensor, SpatialArray, FourierTensor, FourierArray]: The clipped trajectory. The clipped trajectory has shape [B, length, C, H, ...]. |
Source code in torchfsm/utils/traj_manipulate.py
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 | |
torchfsm.utils.randomly_select_frames
¤
randomly_select_frames(
traj: Union[
SpatialTensor["B T C H ..."],
SpatialArray["B T C H ..."],
FourierTensor["B T C H ..."],
FourierArray["B T C H ..."],
],
n_frames: int,
return_frame_indices: bool = False,
) -> Union[
SpatialTensor["B n_frames C H ..."],
SpatialArray["B n_frames C H ..."],
FourierTensor["B n_frames C H ..."],
FourierArray["B n_frames C H ..."],
]
Randomly select a specified number of frames from a trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj
|
Union[SpatialTensor, SpatialArray, FourierTensor, FourierArray]
|
The trajectory to select frames from. The trajectory can be a tensor or numpy array with shape [B, T, C, H, ...]. |
required |
n_frames
|
int
|
The number of frames to select. The number of frames should be less than the original length of the trajectory. |
required |
Returns:
| Type | Description |
|---|---|
Union[SpatialTensor['B n_frames C H ...'], SpatialArray['B n_frames C H ...'], FourierTensor['B n_frames C H ...'], FourierArray['B n_frames C H ...']]
|
Union[SpatialTensor, SpatialArray, FourierTensor, FourierArray]: The selected frames. The selected frames have shape [B, n_frames, C, H, ...]. |
Source code in torchfsm/utils/traj_manipulate.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
torchfsm.utils.uniformly_select_frames
¤
uniformly_select_frames(
traj: Union[
SpatialTensor["B T C H ..."],
SpatialArray["B T C H ..."],
FourierTensor["B T C H ..."],
FourierArray["B T C H ..."],
],
n_frames: int,
return_frame_indices: bool = False,
) -> Union[
SpatialTensor["B n_frames C H ..."],
SpatialArray["B n_frames C H ..."],
FourierTensor["B n_frames C H ..."],
FourierArray["B n_frames C H ..."],
]
Uniformly select a specified number of frames from a trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj
|
Union[SpatialTensor, SpatialArray, FourierTensor, FourierArray]
|
The trajectory to select frames from. The trajectory can be a tensor or numpy array with shape [B, T, C, H, ...]. |
required |
n_frames
|
int
|
The number of frames to select. The number of frames should be less than the original length of the trajectory. |
required |
Returns:
| Type | Description |
|---|---|
Union[SpatialTensor['B n_frames C H ...'], SpatialArray['B n_frames C H ...'], FourierTensor['B n_frames C H ...'], FourierArray['B n_frames C H ...']]
|
Union[SpatialTensor, SpatialArray, FourierTensor, FourierArray]: The selected frames. The selected frames have shape [B, n_frames, C, H, ...]. |
Source code in torchfsm/utils/traj_manipulate.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
torchfsm.utils.default
¤
default(value, default)
Return the default value if the value is None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The value to check. |
required | |
default
|
The default value to return if value is None. |
required |
Returns:
| Type | Description |
|---|---|
|
The value if it is not None, otherwise the default value. |
Source code in torchfsm/_utils.py
7 8 9 10 11 12 13 14 15 16 17 18 | |
torchfsm.utils.format_device_dtype
¤
format_device_dtype(
device: Optional[Union[device, str]] = None,
dtype: Optional[dtype] = None,
) -> Tuple[torch.device, torch.dtype]
Format the device and dtype for PyTorch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
Optional[Union[device, str]]
|
The device to use. If None, defaults to CPU. |
None
|
dtype
|
Optional[dtype]
|
The data type to use. If None, defaults to float32. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[device, dtype]
|
tuple[torch.device, torch.dtype]: The formatted device and dtype. |
Source code in torchfsm/_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
torchfsm.utils.traj_slices
¤
traj_slices(
traj: Union[
SpatialTensor["B T C H ..."],
SpatialArray["B T C H ..."],
],
slice_control: Sequence[Optional[Union[int, float]]],
) -> Sequence[
Union[
SpatialTensor["B T C H ..."],
SpatialArray["B T C H ..."],
]
]
Slice a trajectory along specified dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traj
|
Union[SpatialTensor['B T C H ...'], SpatialArray['B T C H ...']]
|
The trajectory to slice. |
required |
slice_control
|
Sequence[Optional[Union[int, float]]]
|
A sequence of slice values for each dimension. If a value is None, that dimension will not be sliced. If a value is negative, it will slice from the end of that dimension. If a value is positive, it will slice from the start of that dimension. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[Union[SpatialTensor['B T C H ...'], SpatialArray['B T C H ...']]]
|
Union[Sequence[SpatialTensor["B T C H ..."], SpatialArray["B T C H ..."]]]: A sequence of sliced trajectories. Each element corresponds to a slice along one dimension. |
Source code in torchfsm/utils/slice.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 | |
torchfsm.utils.field_slices
¤
field_slices(
field: Union[
SpatialTensor["B C H ..."],
SpatialArray["B C H ..."],
],
slice_control: Sequence[Optional[Union[int, float]]],
) -> Sequence[
Union[
SpatialTensor["B C H ..."],
SpatialArray["B C H ..."],
]
]
Slice a field along specified dimensions. Args: field (Union[SpatialTensor["B C H ..."], SpatialArray["B C H ..."]]): The field to slice. slice_control (Sequence[Optional[Union[int,float]]]): A sequence of slice values for each dimension. If a value is None, that dimension will not be sliced. If a value is negative, it will slice from the end of that dimension. If a value is positive, it will slice from the start of that dimension. Returns: Union[Sequence[SpatialTensor["B C H ..."], SpatialArray["B C H ..."]]]: A sequence of sliced fields. Each element corresponds to a slice along one dimension.
Source code in torchfsm/utils/slice.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
torchfsm.utils.test_sim_dt
¤
test_sim_dt(
operator: Operator,
u_0: Tensor,
max_sim_dt: float,
min_sim_dt: float,
mesh: Optional[
Union[
Sequence[tuple[float, float, int]],
MeshGrid,
FourierMesh,
]
] = None,
stop_criteria: float = None,
initial_step: int = 1,
dt_decrement: float = 0.5,
**kwargs
) -> tuple[dict, dict, dict]
Test the simulation with varying time steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operator
|
Operator
|
The operator to be tested. |
required |
u_0
|
Tensor
|
Initial condition tensor. |
required |
max_sim_dt
|
float
|
Maximum simulation time step. |
required |
min_sim_dt
|
float
|
Minimum simulation time step. |
required |
mesh
|
Optional[Union[Sequence[tuple[float, float, int]], MeshGrid, FourierMesh]]
|
Mesh information or mesh object. |
None
|
stop_criteria
|
float
|
Criteria to stop the simulation if reached. |
None
|
initial_step
|
int
|
Initial step size for the simulation. |
1
|
dt_decrement
|
float
|
Factor by which to reduce the time step on failure. |
0.5
|
**kwargs
|
Additional keyword arguments for the operator's integrate method. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[dict, dict, dict]
|
tuple[dict, dict, dict]: A tuple containing: - errors: Dictionary of errors for each time step. - mean_differences: Mean differences between frames for each time step. - std_differences: Standard deviations of differences for each time step. |
Source code in torchfsm/utils/test.py
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
torchfsm.utils.collect_energy_spectrum
¤
collect_energy_spectrum(
u: SpatialTensor["1 C H ..."],
mesh: Optional[
Union[
Sequence[tuple[float, float, int]],
MeshGrid,
FourierMesh,
]
] = None,
progressive: bool = False,
exact=False,
n_bins: Optional[int] = None,
)
Collect the energy spectrum from a spatial tensor with batch size 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
SpatialTensor['1 C H ...']
|
The input spatial tensor with batch size 1. |
required |
mesh
|
Optional[Union[Sequence[tuple[float, float, int]], MeshGrid, FourierMesh]]
|
The mesh grid or Fourier mesh to use for FFT. If None, a default mesh is created based on the shape of u. Default is None. |
None
|
progressive
|
bool
|
Whether to show a progress bar during computation. Default is False. |
False
|
exact
|
bool
|
Whether to compute the exact energy spectrum without binning. Default is False. |
False
|
n_bins
|
Optional[int]
|
Number of bins to smooth the spectrum if exact is False. If None, it defaults to min(50, u.shape[-1] // 2). Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
|
Tuple[List[float], List[float]]: Two lists containing the wave numbers and their corresponding energy spectrum values. |
Source code in torchfsm/utils/spectrum.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 | |