derivatives_approx module¶
Finite differences approximation.
- class gemseo.utils.derivatives.derivatives_approx.DisciplineJacApprox(discipline, approx_method=ApproximationMode.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 (ApproximationMode) –
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.
- 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.
- Return type:
See also
https://en.wikipedia.org/wiki/Numerical_differentiation and Numerical Algorithms and Digital Representation, Knut Morken, Chapter 11, “Numerical Differentiation”
- 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.
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. IfNone
, consider all the components of all theinputs
andoutputs
.
- Returns:
Whether the analytical Jacobian is correct.
- Return type:
- compute_approx_jac(outputs, inputs, x_indices=None)[source]¶
Approximate the Jacobian.
- Parameters:
- 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
- N_CPUS = 2¶
- approximator: GradientApproximator | None¶
The gradient approximation method.