WallGo.helpers
Helper functions that are used by WallGo. This includes the derivative functions, a function to compute the step size for solving detonations and other small functions.
Functions
|
Lorentz-transformed velocity |
|
Computes numerical derivatives of a callable function. |
|
Lorentz factor \(\gamma^2\) corresponding to velocity \(v\) |
|
Computes the gradient of a callable function. |
|
Computes the hessian of a callable function. |
|
Function used in |
- boostVelocity(xi, v)[source]
Lorentz-transformed velocity
- Parameters:
xi (float)
v (float)
- Return type:
float
- derivative(f, x, n=1, order=4, bounds=None, epsilon=1e-16, scale=1.0, dx=None, args=None)[source]
Computes numerical derivatives of a callable function. Uses the
epsilonandscaleparameters to estimate the optimal value ofdx, if the latter is not provided.- Parameters:
f (function) – Function to differentiate. Should take a float or an array as argument and return a float or array (the returned array can have a different shape as the input, but the first axis must match).
x (float or array-like) – The position at which to evaluate the derivative.
n (int, optional) – The number of derivatives to take. Can be 0, 1, 2. The default is 1.
order (int, optional) – The accuracy order of the scheme. Errors are of order \(\mathcal{O}({\rm d}x^{\text{order}/(\text{order+n})})\). Can be 2 or 4. Note that the order at the endpoints is reduced by 1 as it would require more function evaluations to keep the same order. The default is 4.
bounds (tuple or None, optional) – Interval in which f can be called. If None, can be evaluated anywhere. The default is None.
epsilon (float, optional) – Fractional accuracy at which f can be evaluated. If f is a simple function, should be close to the machine precision. Default is 1e-16.
scale (float, optional) – Typical scale at which f(x) change by order 1. Default is 1.
dx (float or None, optional) – The magnitude of finite differences. If None, use epsilon and scale to estimate the optimal dx. Default is None.
args (list, optional) – List of other fixed arguments passed to the function \(f\).
- Returns:
res – The value of the derivative of
fevaluated atx.- Return type:
float
- gammaSq(v)[source]
Lorentz factor \(\gamma^2\) corresponding to velocity \(v\)
- Parameters:
v (float)
- Return type:
float
- gradient(f, x, order=4, epsilon=1e-16, scale=1.0, dx=None, axis=None, args=None)[source]
Computes the gradient of a callable function. Uses the
epsilonandscaleparameters to estimate the optimal value ofdx, if the latter is not provided.- Parameters:
f (function) – Function to differentiate. Should take an array as argument and return an array.
x (array-like) – The position at which to evaluate the derivative. The size of the last axis must correspond to the number of variables on which f depends.
order (int, optional) – The accuracy order of the scheme. Errors are of order \(\mathcal{O}({\rm d}x^{\text{order}/(\text{order}+1)})\). Can be 2 or 4. The default is 4.
epsilon (float, optional) – Fractional accuracy at which f can be evaluated. If f is a simple function, should be close to the machine precision. Default is 1e-16.
scale (float or array-like, optional) – Typical scale at which f(x) change by order 1. Can be an array, in which case each element corresponds to the scale of a different variable. Default is 1.
dx (float or np.ndarray or None, optional) – The magnitude of finite differences. Can be an array, in which case each element corresponds to the dx of a different variable.If None, use epsilon and scale to estimate the optimal dx. Default is None.
axis (list, int or None, optional) – Element of the gradient to return. If None, returns the whole gradient. Default is None.
args (list, optional) – List of other fixed arguments passed to the function \(f\).
- Returns:
res – The value of the gradient of
fevaluated atx.- Return type:
float
- hessian(f, x, order=4, epsilon=1e-16, scale=1.0, dx=None, xAxis=None, yAxis=None, args=None)[source]
Computes the hessian of a callable function. Uses the
epsilonandscaleparameters to estimate the optimal value ofdx, if the latter is not provided.- Parameters:
f (function) – Function to differentiate. Should take an array as argument and return an array.
x (array-like) – The position at which to evaluate the derivative. The size of the last axis must correspond to the number of variables on which f depends.
order (int, optional) – The accuracy order of the scheme. Errors are of order \(\mathcal{O}({\rm d}x^{\text{order}/(\text{order}+2)})\). Can be 2 or 4. The default is 4.
epsilon (float, optional) – Fractional accuracy at which f can be evaluated. If f is a simple function, should be close to the machine precision. Default is 1e-16.
scale (float, optional) – Typical scale at which f(x) change by order 1. Default is 1.
dx (float or None, optional) – The magnitude of finite differences. If None, use epsilon and scale to estimate the optimal dx. Default is None.
xAxis (list, int or None, optional) – Lines of the hessian matrix to return. If None, returns all the lines. Default is None.
yAxis (list, int or None, optional) – Columns of the hessian matrix to return. If None, returns all the columns. Default is None.
args (list, optional) – List of other fixed arguments passed to the function \(f\).
- Returns:
res – The value of the hessian of
fevaluated atx.- Return type:
float
- nextStepDeton(pos1, pos2, pressure1, pressure2, mean2ndDeriv, std2ndDeriv, pressureTol, posMax, overshootProb=0.05)[source]
Function used in
EOMto find detonation solutions. It finds the next point to be sampled to try to bracket a root in such a way that the probability of overshooting a root is roughly equal to overshootProb.To estimate the overshoot probability, it fits the pressure to a quadratic which is equal to
pressure2at :py:data`pos2`, but with uncertain 1st and 2nd derivatives which are assumed to be normally distributed. The mean of the 1st derivative is computed by finite differences from the last 2 points.- Parameters:
pos1 (float) – Position of the first sampled point.
pos2 (float) – position of the second sampled point.
pressure1 (float) – Pressure at pos1.
pressure2 (float) – Pressure at pos2.
mean2ndDeriv (float) – Estimate of the 2nd derivative at pos2.
std2ndDeriv (float) – Uncertainty on the 2nd derivative at pos2.
pressureTol (float) – Relative accuracy at which pressure1 and pressure2 are computed.
posMax (float) – Maximal position that the next step can have.
overshootProb (float, optional) – Desired overshoot probability. A smaller value will lead to smaller step sizes which will take longer to evaluate, but with less chances of missing a root. The default is 0.05.
- Returns:
Position where the overshoot probability is overshootProb (or posMax if there is no solution).
- Return type:
float