4. integrator
This library provides a set of integrators for simulations. The implementation of ETDRK method is modified from exponax
ETDRK¤
torchfsm.integrator.ETDRKIntegrator
¤
Bases: Enum
ETDRK Integrator Provides a unified interface for all ETDRK methods.
Source code in torchfsm/integrator/_etdrk.py
246 247 248 249 250 251 252 253 254 255 |
|
torchfsm.integrator._etdrk._ETDRKBase
¤
Bases: ABC
Solve $$ u_t=Lu+N(u) $$ using the ETDRK method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt(float)
|
Time step. |
required | |
linear_coef(torch.Tensor)
|
Coefficient of the linear term, i.e., \(L\). |
required | |
nonlinear_func(Callable[[torch.Tensor],torch.Tensor])
|
Function that computes the nonlinear term, i.e., \(N(u)\). |
required | |
num_circle_points(int)
|
Number of points on the unit circle. See [2] for details. |
required | |
circle_radius(float)
|
Radius of the unit circle. See [2] for details. |
required |
Reference
[1] Cox, Steven M., and Paul C. Matthews. "Exponential time differencing for stiff systems." Journal of Computational Physics 176.2 (2002): 430-455. [2] Kassam, Aly-Khan, and Lloyd N. Trefethen. "Fourth-order time-stepping for stiff PDEs." SIAM Journal on Scientific Computing 26.4 (2005): 1214-1233.
Source code in torchfsm/integrator/_etdrk.py
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 |
|
LR
instance-attribute
¤
LR = (
circle_radius
* roots_of_unity(
num_circle_points, device=device, dtype=dtype
)
+ unsqueeze(-1) * dt
)
__init__
¤
__init__(
dt: float,
linear_coef: torch.Tensor,
nonlinear_func: Callable[[torch.Tensor], torch.Tensor],
num_circle_points: int = 16,
circle_radius: float = 1.0,
)
Source code in torchfsm/integrator/_etdrk.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
step
abstractmethod
¤
step(u_hat)
Advance the state in Fourier space.
Source code in torchfsm/integrator/_etdrk.py
56 57 58 59 60 61 62 63 |
|
torchfsm.integrator._etdrk.ETDRK0
¤
Exactly solve a linear PDE in Fourier space
Source code in torchfsm/integrator/_etdrk.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
__init__
¤
__init__(dt: float, linear_coef: torch.Tensor)
Source code in torchfsm/integrator/_etdrk.py
69 70 71 72 73 74 75 |
|
step
¤
step(u_hat)
Source code in torchfsm/integrator/_etdrk.py
77 78 79 80 81 |
|
torchfsm.integrator._etdrk.ETDRK1
¤
Bases: _ETDRKBase
First-order ETDRK method.
Source code in torchfsm/integrator/_etdrk.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
LR
instance-attribute
¤
LR = (
circle_radius
* roots_of_unity(
num_circle_points, device=device, dtype=dtype
)
+ unsqueeze(-1) * dt
)
__init__
¤
__init__(
dt: float,
linear_coef: torch.Tensor,
nonlinear_func: Callable[[torch.Tensor], torch.Tensor],
num_circle_points: int = 16,
circle_radius: float = 1.0,
)
Source code in torchfsm/integrator/_etdrk.py
89 90 91 92 93 94 95 96 97 98 |
|
step
¤
step(u_hat)
Source code in torchfsm/integrator/_etdrk.py
100 101 102 103 104 |
|
torchfsm.integrator._etdrk.ETDRK2
¤
Bases: _ETDRKBase
Second-order ETDRK method.
Source code in torchfsm/integrator/_etdrk.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
LR
instance-attribute
¤
LR = (
circle_radius
* roots_of_unity(
num_circle_points, device=device, dtype=dtype
)
+ unsqueeze(-1) * dt
)
__init__
¤
__init__(
dt: float,
linear_coef: torch.Tensor,
nonlinear_func: Callable[[torch.Tensor], torch.Tensor],
num_circle_points: int = 16,
circle_radius: float = 1.0,
)
Source code in torchfsm/integrator/_etdrk.py
111 112 113 114 115 116 117 118 119 120 121 |
|
step
¤
step(u_hat)
Source code in torchfsm/integrator/_etdrk.py
123 124 125 126 127 128 129 130 131 132 133 |
|
torchfsm.integrator._etdrk.ETDRK3
¤
Bases: _ETDRKBase
Third-order ETDRK method.
Source code in torchfsm/integrator/_etdrk.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 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 |
|
LR
instance-attribute
¤
LR = (
circle_radius
* roots_of_unity(
num_circle_points, device=device, dtype=dtype
)
+ unsqueeze(-1) * dt
)
__init__
¤
__init__(
dt: float,
linear_coef: torch.Tensor,
nonlinear_func: Callable[[torch.Tensor], torch.Tensor],
num_circle_points: int = 16,
circle_radius: float = 1.0,
)
Source code in torchfsm/integrator/_etdrk.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
step
¤
step(u_hat)
Source code in torchfsm/integrator/_etdrk.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
torchfsm.integrator._etdrk.ETDRK4
¤
Bases: _ETDRKBase
Fourth-order ETDRK method.
Source code in torchfsm/integrator/_etdrk.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
LR
instance-attribute
¤
LR = (
circle_radius
* roots_of_unity(
num_circle_points, device=device, dtype=dtype
)
+ unsqueeze(-1) * dt
)
__init__
¤
__init__(
dt: float,
linear_coef: torch.Tensor,
nonlinear_func: Callable[[torch.Tensor], torch.Tensor],
num_circle_points: int = 16,
circle_radius: float = 1.0,
)
Source code in torchfsm/integrator/_etdrk.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
step
¤
step(u_hat)
Source code in torchfsm/integrator/_etdrk.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
RK¤
torchfsm.integrator.RKIntegrator
¤
Bases: Enum
Enum class for Runge-Kutta integrators. This class provides a set of predefined Runge-Kutta integrators for solving ordinary differential equations (ODEs). Each integrator is represented as a member of the enum, and can be used to create an instance of the corresponding integrator class. The integrators include: - Euler: First-order Euler method. - Midpoint: Second-order Midpoint method. - Heun12: Second-order Heun method. - Ralston12: Second-order Ralston method. - BogackiShampine23: Third-order - RK4: Fourth-order Runge-Kutta method. - RK4_38Rule: Fourth-order Runge-Kutta method with ⅜ rule. - Dorpi45: Fifth-order Dormand-Prince method. - Fehlberg45: Fifth-order Fehlberg method. - CashKarp45: Fifth-order Cash-Karp method.
Source code in torchfsm/integrator/_rk.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
torchfsm.integrator._rk._RKBase
¤
Base class for Runge-Kutta integrators. This class implements the Runge-Kutta method for solving ordinary differential equations (ODEs). The Runge-Kutta method is a numerical technique used to solve ODEs by approximating the solution at discrete time steps. The class provides a flexible interface for defining different Runge-Kutta methods by specifying the coefficients and weights. The class also supports adaptive step size control, allowing for dynamic adjustment of the time step based on the estimated error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ca
|
Sequence[float]
|
Coefficients for the Runge-Kutta method. |
required |
b
|
Sequence[float]
|
Weights for the Runge-Kutta method. |
required |
b_star
|
Optional[Sequence]
|
Optional coefficients for error estimation. |
None
|
adaptive
|
bool
|
If True, enables adaptive step size control. |
False
|
atol
|
float
|
Absolute tolerance for adaptive step size control. |
1e-06
|
rtol
|
float
|
Relative tolerance for adaptive step size control. |
1e-05
|
Source code in torchfsm/integrator/_rk.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 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 |
|
__init__
¤
__init__(
ca: Sequence[float],
b: Sequence[float],
b_star: Optional[Sequence] = None,
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
torchfsm.integrator._rk.Euler
¤
Bases: _RKBase
First-order Euler method.
Source code in torchfsm/integrator/_rk.py
82 83 84 85 86 87 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__()
Source code in torchfsm/integrator/_rk.py
86 87 |
|
torchfsm.integrator._rk.Midpoint
¤
Bases: _RKBase
Midpoint method.
Source code in torchfsm/integrator/_rk.py
90 91 92 93 94 95 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__()
Source code in torchfsm/integrator/_rk.py
94 95 |
|
torchfsm.integrator._rk.Heun12
¤
Bases: _RKBase
Heun's second-order method.
Source code in torchfsm/integrator/_rk.py
98 99 100 101 102 103 104 105 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
102 103 104 105 |
|
torchfsm.integrator._rk.Ralston12
¤
Bases: _RKBase
Ralston's second-order method.
Source code in torchfsm/integrator/_rk.py
108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
112 113 114 115 116 117 118 119 120 |
|
torchfsm.integrator._rk.BogackiShampine23
¤
Bases: _RKBase
Third-order Bogack and Shampine method.
Source code in torchfsm/integrator/_rk.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
torchfsm.integrator._rk.RK4
¤
Bases: _RKBase
Fourth-order Runge-Kutta method.
Source code in torchfsm/integrator/_rk.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__()
Source code in torchfsm/integrator/_rk.py
146 147 148 149 150 151 152 153 154 155 |
|
torchfsm.integrator._rk.RK4_38Rule
¤
Bases: _RKBase
Fourth-order Runge-Kutta method with ⅜ rule.
Source code in torchfsm/integrator/_rk.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__()
Source code in torchfsm/integrator/_rk.py
162 163 164 165 166 167 168 169 170 171 |
|
torchfsm.integrator._rk.Dorpi45
¤
Bases: _RKBase
Fifth-order Dormand-Prince method.
Source code in torchfsm/integrator/_rk.py
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 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
torchfsm.integrator._rk.Fehlberg45
¤
Bases: _RKBase
Fehlberg 4(5) method for adaptive step size control. This method is a Runge-Kutta method that provides a fourth-order and fifth-order approximation of the solution to an ODE.
Source code in torchfsm/integrator/_rk.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
torchfsm.integrator._rk.CashKarp45
¤
Bases: _RKBase
Cash-Karp 4(5) method for adaptive step size control. This method is a Runge-Kutta method that provides a fourth-order and fifth-order approximation of the solution to an ODE.
Source code in torchfsm/integrator/_rk.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
_rk_step
¤
_rk_step(
f: Callable[[Tensor], Tensor],
x_t: Tensor,
dt: float,
return_error: bool = False,
)
Source code in torchfsm/integrator/_rk.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
_adaptive_step
¤
_adaptive_step(
f: Callable[[Tensor], Tensor], x_t: Tensor, dt: float
)
Source code in torchfsm/integrator/_rk.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
__init__
¤
__init__(
adaptive: bool = False,
atol: float = 1e-06,
rtol: float = 1e-05,
)
Source code in torchfsm/integrator/_rk.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|