derivatives_approx module¶
Finite differences approximation.
- class gemseo.utils.derivatives.derivatives_approx.DisciplineJacApprox(discipline, approx_method='finite_differences', step=1e-07, parallel=False, n_processes=2, use_threading=False, wait_time_between_fork=0)[source]¶
Bases:
object
Approximates a discipline Jacobian using finite differences or Complex step.
- Parameters
discipline (MDODiscipline) – The discipline for which the Jacobian approximation shall be made.
approx_method (str) –
The approximation method, either
complex_step
orfinite_differences
.By default it is set to finite_differences.
step (Number | Iterable[Number]) –
The differentiation step. The
finite_differences
takes either a float or an iterable of floats with the same length as the inputs. Thecomplex_step
method takes either a complex or a float as input.By default it is set to 1e-07.
parallel (bool) –
Whether to differentiate the discipline in parallel.
By default it is set to False.
n_processes (int) –
The maximum simultaneous number of threads, if
use_threading
is True, or processes otherwise, used to parallelize the execution.By default it is set to 2.
use_threading (bool) –
Whether to use threads instead of processes to parallelize the execution; multiprocessing will copy (serialize) all the disciplines, while threading will share all the memory This is important to note if you want to execute the same discipline multiple times, you shall use multiprocessing.
By default it is set to False.
wait_time_between_fork (float) –
The time waited between two forks of the process / thread.
By default it is set to 0.
- auto_set_step(outputs, inputs, print_errors=True, numerical_error=2.220446049250313e-16)[source]¶
Compute the optimal step.
Require a first evaluation of the perturbed functions values.
The optimal step is reached when the truncation error (cut in the Taylor development), and the numerical cancellation errors (round-off when doing \(f(x+step)-f(x))\) are equal.
See also
https://en.wikipedia.org/wiki/Numerical_differentiation and Numerical Algorithms and Digital Representation, Knut Morken, Chapter 11, “Numerical Differentiation”
- Parameters
inputs (Sequence[str]) – The names of the inputs used to differentiate the outputs.
outputs (Sequence[str]) – The names of the outputs to be differentiated.
print_errors (bool) –
Whether to log the cancellation and truncation error estimates.
By default it is set to True.
numerical_error (float) –
The numerical error associated to the calculation of \(f\). By default Machine epsilon (appx 1e-16), but can be higher. when the calculation of \(f\) requires a numerical resolution.
By default it is set to 2.220446049250313e-16.
- Returns
The Jacobian of the function.
- Return type
- check_jacobian(analytic_jacobian, outputs, inputs, discipline, threshold=1e-08, plot_result=False, file_path='jacobian_errors.pdf', show=False, fig_size_x=10.0, fig_size_y=10.0, reference_jacobian_path=None, save_reference_jacobian=False, indices=None)[source]¶
Check if the analytical Jacobian is correct with respect to a reference one.
If reference_jacobian_path is not None and save_reference_jacobian is True, compute the reference Jacobian with the approximation method and save it in reference_jacobian_path.
If reference_jacobian_path is not None and save_reference_jacobian is False, do not compute the reference Jacobian but read it from reference_jacobian_path.
If reference_jacobian_path is None, compute the reference Jacobian without saving it.
- Parameters
analytic_jacobian (dict[str, dict[str, ndarray]]) – The Jacobian to validate.
inputs (Iterable[str]) – The names of the inputs used to differentiate the outputs.
outputs (Iterable[str]) – The names of the outputs to be differentiated.
discipline (MDODiscipline) – The discipline to be differentiated.
threshold (float) –
The acceptance threshold for the Jacobian error.
By default it is set to 1e-08.
plot_result (bool) –
Whether to plot the result of the validation (computed vs approximated Jacobians).
By default it is set to False.
file_path (str | Path) –
The path to the output file if
plot_result
isTrue
.By default it is set to jacobian_errors.pdf.
show (bool) –
Whether to open the figure.
By default it is set to False.
fig_size_x (float) –
The x-size of the figure in inches.
By default it is set to 10.0.
fig_size_y (float) –
The y-size of the figure in inches.
By default it is set to 10.0.
reference_jacobian_path (str | Path | None) –
The path of the reference Jacobian file.
By default it is set to None.
save_reference_jacobian (bool) –
Whether to save the reference Jacobian.
By default it is set to False.
indices (int | Sequence[int] | slice | Ellipsis | None) –
The indices of the inputs and outputs for the different sub-Jacobian matrices, formatted as
{variable_name: variable_components}
wherevariable_components
can be either an integer, e.g. 2 a sequence of integers, e.g. [0, 3], a slice, e.g. slice(0,3), the ellipsis symbol (…) or None, which is the same as ellipsis. If a variable name is missing, consider all its components. If None, consider all the components of all theinputs
andoutputs
.By default it is set to None.
- Returns
Whether the analytical Jacobian is correct.
- Return type
- compute_approx_jac(outputs, inputs, x_indices=None)[source]¶
Approximate the Jacobian.
- Parameters
outputs (Iterable[str]) – The names of the outputs to be differentiated.
inputs (Iterable[str]) – The names of the inputs used to differentiate the outputs.
x_indices (Sequence[int] | None) –
The components of the input vector to be used for the differentiation. If None, use all the components.
By default it is set to None.
- Returns
The approximated Jacobian.
- Return type
- plot_jac_errors(computed_jac, approx_jac, file_path='jacobian_errors.pdf', show=False, fig_size_x=10.0, fig_size_y=10.0)[source]¶
Generate a plot of the exact vs approximated Jacobian.
- Parameters
computed_jac (ndarray) – The Jacobian to validate.
approx_jac (ndarray) – The approximated Jacobian.
file_path (str | Path) –
The path to the output file if
plot_result
isTrue
.By default it is set to jacobian_errors.pdf.
show (bool) –
Whether to open the figure.
By default it is set to False.
fig_size_x (float) –
The x-size of the figure in inches.
By default it is set to 10.0.
fig_size_y (float) –
The y-size of the figure in inches.
By default it is set to 10.0.
- Return type
Figure
- COMPLEX_STEP = 'complex_step'¶
- FINITE_DIFFERENCES = 'finite_differences'¶
- N_CPUS = 2¶
- gemseo.utils.derivatives.derivatives_approx.approx_hess(f_p, f_x, f_m, step)[source]¶
Compute the second-order approximation of the Hessian matrix \(d^2f/dx^2\).
- Parameters
f_p (numpy.ndarray) – The value of the function \(f\) at the next step \(x+\\delta_x\).
f_x (numpy.ndarray) – The value of the function \(f\) at the current step \(x\).
f_m (numpy.ndarray) – The value of the function \(f\) at the previous step \(x-\\delta_x\).
step (float) – The differentiation step \(\\delta_x\).
- Returns
The approximation of the Hessian matrix at the current step \(x\).
- Return type
- gemseo.utils.derivatives.derivatives_approx.comp_best_step(f_p, f_x, f_m, step, epsilon_mach=2.220446049250313e-16)[source]¶
Compute the optimal step for finite differentiation.
Applied to a forward first order finite differences gradient approximation.
Require a first evaluation of the perturbed functions values.
The optimal step is reached when the truncation error (cut in the Taylor development), and the numerical cancellation errors (round-off when doing \(f(x+step)-f(x))\) are equal.
See also
https://en.wikipedia.org/wiki/Numerical_differentiation and Numerical Algorithms and Digital Representation, Knut Morken, Chapter 11, “Numerical Differenciation”
- Parameters
f_p (ndarray) – The value of the function \(f\) at the next step \(x+\\delta_x\).
f_x (ndarray) – The value of the function \(f\) at the current step \(x\).
f_m (ndarray) – The value of the function \(f\) at the previous step \(x-\\delta_x\).
step (float) – The differentiation step \(\\delta_x\).
epsilon_mach (float) –
By default it is set to 2.220446049250313e-16.
- Returns
The estimation of the truncation error. None if the Hessian approximation is too small to compute the optimal step. The estimation of the cancellation error. None if the Hessian approximation is too small to compute the optimal step. The optimal step.
- Return type
- gemseo.utils.derivatives.derivatives_approx.compute_cancellation_error(f_x, step, epsilon_mach=2.220446049250313e-16)[source]¶
Estimate the cancellation error.
This is the round-off when doing \(f(x+\\delta_x)-f(x)\).
- Parameters
f_x (numpy.ndarray) – The value of the function at the current step \(x\).
step (float) – The step used for the calculations of the perturbed functions values.
epsilon_mach –
The machine epsilon.
By default it is set to 2.220446049250313e-16.
- Returns
The cancellation error.
- Return type
- gemseo.utils.derivatives.derivatives_approx.compute_truncature_error(hess, step)[source]¶
Estimate the truncation error.
Defined for a first order finite differences scheme.
- Parameters
hess (numpy.ndarray) – The second-order derivative \(d^2f/dx^2\).
step (float) – The differentiation step.
- Returns
The truncation error.
- Return type