base type for sparsity and Jacobian computations.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
logical, | private | :: | exception_raised | = | .false. |
if true, an exception has been raised |
|
integer, | private | :: | istat | = | 0 |
a non-zero value will cause all routine to exit.
this can be set to |
|
character(len=:), | private, | allocatable | :: | error_msg |
error message (if |
||
integer, | private | :: | n | = | 0 |
number of |
|
integer, | private | :: | m | = | 0 |
number of |
|
real(kind=wp), | private, | dimension(:), allocatable | :: | xlow |
lower bounds on |
||
real(kind=wp), | private, | dimension(:), allocatable | :: | xhigh |
upper bounds on |
||
logical, | private | :: | print_messages | = | .true. |
if true, warning messages are printed
to the |
|
integer, | private | :: | chunk_size | = | 100 |
chuck size for allocating the arrays (>0) |
|
integer, | private | :: | perturb_mode | = | 1 |
perturbation mode: 1 - perturbation is |
|
real(kind=wp), | private, | dimension(:), allocatable | :: | dpert |
perturbation vector for |
||
logical, | private | :: | partition_sparsity_pattern | = | .false. |
to partition the sparsity pattern using dsm |
|
type(sparsity_pattern), | private | :: | sparsity |
the sparsity pattern |
|||
real(kind=wp), | private, | dimension(:), allocatable | :: | xlow_for_sparsity |
lower bounds on |
||
real(kind=wp), | private, | dimension(:), allocatable | :: | xhigh_for_sparsity |
upper bounds on |
||
real(kind=wp), | private, | dimension(:), allocatable | :: | dpert_for_sparsity |
perturbation vector for |
||
integer, | private | :: | num_sparsity_points | = | 3 |
for |
|
integer, | private | :: | sparsity_perturb_mode | = | 1 |
perturbation mode (if 1 - perturbation is |
|
logical, | private | :: | compute_linear_sparsity_pattern | = | .false. |
to also compute the linear sparsity pattern |
|
real(kind=wp), | private | :: | linear_sparsity_tol | = | epsilon(1.0_wp) |
the equality tolerance for derivatives to indicate a constant jacobian element (linear sparsity) |
|
real(kind=wp), | private | :: | function_precision_tol | = | epsilon(1.0_wp) |
the function precision. two functions values
that are the within this tolerance are
considered the same value. This is used
when estimating the sparsity pattern when
|
|
integer, | private | :: | mode | = | 1 |
1 = use |
|
type(finite_diff_method), | private, | dimension(:), allocatable | :: | meth |
the finite difference method to use
compute the |
||
integer, | private, | dimension(:), allocatable | :: | class |
the class of method to use to
compute the |
||
type(meth_array), | private, | dimension(:), allocatable | :: | class_meths |
array of methods for the specified classes.
used with |
||
real(kind=wp), | private | :: | eps | = | 1.0e-9_wp |
tolerance parameter for diff |
|
real(kind=wp), | private | :: | acc | = | 0.0_wp |
tolerance parameter for diff |
|
type(function_cache), | private | :: | cache |
if using the function cache |
|||
logical, | private | :: | use_diff | = | .false. |
if we are using the Neville's process method, rather than finite differences |
|
procedure(func), | private, | pointer | :: | problem_func | => | null() |
the user-defined function |
procedure(func), | private, | pointer | :: | compute_function | => | null() |
compute the user-defined function
this can point to the |
procedure(spars_f), | private, | pointer | :: | compute_sparsity | => | null() |
for computing the sparsity pattern |
procedure(info_f), | private, | pointer | :: | info_function | => | null() |
an optional function the user can define which is called when each column of the jacobian is computed. It can be used to perform any setup operations. |
procedure(jacobian_f), | private, | pointer | :: | jacobian_function | => | null() |
the low-level function called by compute_jacobian that actually computes the jacobian. |
initialize the class
Initialize a numdiff_type class. This must be called first.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | n |
number of |
||
integer, | intent(in) | :: | m |
number of |
||
real(kind=wp), | intent(in), | dimension(n) | :: | xlow |
lower bounds on |
|
real(kind=wp), | intent(in), | dimension(n) | :: | xhigh |
upper bounds on |
|
integer, | intent(in) | :: | perturb_mode |
perturbation mode:
1 - perturbation is |
||
real(kind=wp), | intent(in), | dimension(n) | :: | dpert |
perturbation vector for |
|
procedure(func) | :: | problem_func |
the user function that defines the problem
(returns |
|||
integer, | intent(in) | :: | sparsity_mode |
the sparsity computation method:
1 - assume dense,
2 - three-point simple method,
3 - will be specified by the user in
a subsequent call to set_sparsity_pattern.
4 - computes a two-point jacobian
at |
||
integer, | intent(in), | optional | :: | jacobian_method |
|
|
integer, | intent(in), | optional, | dimension(n) | :: | jacobian_methods |
|
integer, | intent(in), | optional | :: | class |
method class for the finite difference method
to use for all |
|
integer, | intent(in), | optional, | dimension(n) | :: | classes |
method class for the finite difference methods to use for each variable. see get_finite_difference_method |
procedure(info_f), | optional | :: | info |
a function the user can define which is called when each column of the jacobian is computed. It can be used to perform any setup operations. |
||
integer, | intent(in), | optional | :: | chunk_size |
chunk size for allocating the arrays (must be >0) [default is 100] |
|
logical, | intent(in), | optional | :: | partition_sparsity_pattern |
if the sparisty pattern is to be partitioned using DSM [default is False] |
|
integer, | intent(in), | optional | :: | cache_size |
if present, this is the cache size for the function cache (default is not to enable cache) |
|
real(kind=wp), | intent(in), | optional, | dimension(n) | :: | xlow_for_sparsity |
lower bounds on |
real(kind=wp), | intent(in), | optional, | dimension(n) | :: | xhigh_for_sparsity |
upper bounds on |
real(kind=wp), | intent(in), | optional, | dimension(n) | :: | dpert_for_sparsity |
for |
integer, | intent(in), | optional | :: | sparsity_perturb_mode |
perturbation mode (required if |
|
real(kind=wp), | intent(in), | optional | :: | linear_sparsity_tol |
the equality tolerance for derivatives to indicate a constant jacobian element (linear sparsity) |
|
real(kind=wp), | intent(in), | optional | :: | function_precision_tol |
the function precision. two functions values
that are the within this tolerance are
considered the same value. This is used
when estimating the sparsity pattern when
|
|
integer, | intent(in), | optional | :: | num_sparsity_points |
for |
initialize the class
Alternate version of initialize_numdiff routine when using diff to compute the Jacobian.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | n |
number of |
||
integer, | intent(in) | :: | m |
number of |
||
real(kind=wp), | intent(in), | dimension(n) | :: | xlow |
lower bounds on |
|
real(kind=wp), | intent(in), | dimension(n) | :: | xhigh |
upper bounds on |
|
procedure(func) | :: | problem_func |
the user function that defines the problem
(returns |
|||
integer, | intent(in) | :: | sparsity_mode |
the sparsity computation method: |
||
procedure(info_f), | optional | :: | info |
a function the user can define which is called when each column of the jacobian is computed. It can be used to perform any setup operations. |
||
integer, | intent(in), | optional | :: | chunk_size |
chunk size for allocating the arrays (must be >0) [default is 100] |
|
real(kind=wp), | intent(in), | optional | :: | eps |
tolerance parameter for diff
if not present, default is |
|
real(kind=wp), | intent(in), | optional | :: | acc |
tolerance parameter for diff
if not present, default is |
|
integer, | intent(in), | optional | :: | cache_size |
if present, this is the cache size for the function cache (default is not to enable cache) |
|
real(kind=wp), | intent(in), | optional, | dimension(n) | :: | xlow_for_sparsity |
lower bounds on |
real(kind=wp), | intent(in), | optional, | dimension(n) | :: | xhigh_for_sparsity |
upper bounds on |
real(kind=wp), | intent(in), | optional, | dimension(n) | :: | dpert_for_sparsity |
required if |
integer, | intent(in), | optional | :: | sparsity_perturb_mode |
perturbation mode (required if |
|
real(kind=wp), | intent(in), | optional | :: | linear_sparsity_tol |
the equality tolerance for derivatives to indicate a constant jacobian element (linear sparsity) |
|
real(kind=wp), | intent(in), | optional | :: | function_precision_tol |
the function precision. two functions values
that are the within this tolerance are
considered the same value. This is used
when estimating the sparsity pattern when
|
|
integer, | intent(in), | optional | :: | num_sparsity_points |
for |
|
logical, | intent(in), | optional | :: | print_messages |
if true, print error messages to |
main routine to compute the Jacobian using the selected options. It returns the sparse (vector) form.
Compute the Jacobian.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(out), | dimension(:), allocatable | :: | jac |
sparse jacobian vector |
return the dense size(m,n)
matrix form of the Jacobian.
just a wrapper for compute_jacobian, that returns a dense (m x n
) matrix.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(out), | dimension(:,:), allocatable | :: | jac |
the jacobian matrix |
returns the product of the Jacobian matrix and an input vector
Returns the product J*v
, where J
is the m x n
Jacobian matrix
and v
is an n x 1
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(in), | dimension(:) | :: | v |
vector (size |
|
real(kind=wp), | intent(out), | dimension(:) | :: | z |
The product |
destroy the class
destroy the numdiff_type class.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(out) | :: | me |
print the sparsity pattern in vector form to a file
Print the sparsity pattern in vector form (irow
, icol
).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | iunit |
file unit to write to. (assumed to be already opened) |
print the sparsity pattern in matrix form to a file
Print the sparsity pattern in matrix form.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | iunit |
file unit to write to. (assumed to be already opened) |
manually set the sparsity pattern
To specify the sparsity pattern directly if it is already known.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in), | dimension(:) | :: | irow |
sparsity pattern nonzero elements row indices |
|
integer, | intent(in), | dimension(:) | :: | icol |
sparsity pattern nonzero elements column indices |
|
integer, | intent(in), | optional, | dimension(:) | :: | linear_irow |
linear sparsity pattern nonzero elements row indices |
integer, | intent(in), | optional, | dimension(:) | :: | linear_icol |
linear sparsity pattern nonzero elements column indices |
real(kind=wp), | intent(in), | optional, | dimension(:) | :: | linear_vals |
linear sparsity values (constant elements of the Jacobian) |
integer, | intent(in), | optional | :: | maxgrp |
DSM sparsity partition
[only used if |
|
integer, | intent(in), | optional, | dimension(:) | :: | ngrp |
DSM sparsity partition (size |
select a method in a specified class so that the variable bounds are not violated when by the perturbations.
Select a finite diff method of a given class
so that the perturbations
of x
will not violate the variable bounds.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in) | :: | x |
the variable value |
||
real(kind=wp), | intent(in) | :: | xlow |
the variable lower bound |
||
real(kind=wp), | intent(in) | :: | xhigh |
the variable upper bound |
||
real(kind=wp), | intent(in) | :: | dx |
the perturbation value (>0) |
||
type(meth_array), | intent(in) | :: | list_of_methods |
list of available methods to choose from |
||
type(finite_diff_method), | intent(out) | :: | fd |
this method can be used |
||
logical, | intent(out) | :: | status_ok |
true if it really doesn't violate the bounds
(say, the bounds are very close to each other)
if |
version of select_finite_diff_method for partitioned sparsity pattern.
Select a finite diff method of a given class
so that the perturbations
of x
will not violate the variable bounds for any variable in the group.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
the variable values |
|
real(kind=wp), | intent(in), | dimension(:) | :: | xlow |
the variable lower bounds |
|
real(kind=wp), | intent(in), | dimension(:) | :: | xhigh |
the variable upper bounds |
|
real(kind=wp), | intent(in), | dimension(:) | :: | dx |
the perturbation values (>0) |
|
type(meth_array), | intent(in) | :: | list_of_methods |
list of available methods to choose from |
||
type(finite_diff_method), | intent(out) | :: | fd |
this method can be used |
||
logical, | intent(out) | :: | status_ok |
true if it really doesn't violate the bounds
(say, the bounds are very close to each other)
if |
can be called to change the variable bounds.
Change the variable bounds in a numdiff_type.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | xlow |
lower bounds on |
|
real(kind=wp), | intent(in), | dimension(:) | :: | xhigh |
upper bounds on |
if the user needs to compute the sparsity pattern manually. (otherwise it will be done the first time the Jacobian is computed)
Computes the sparsity pattern and return it. Uses the settings currently in the class.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
vector of variables (size |
|
integer, | intent(out), | dimension(:), allocatable | :: | irow |
sparsity pattern nonzero elements row indices |
|
integer, | intent(out), | dimension(:), allocatable | :: | icol |
sparsity pattern nonzero elements column indices |
|
integer, | intent(out), | optional, | dimension(:), allocatable | :: | linear_irow |
linear sparsity pattern nonzero elements row indices |
integer, | intent(out), | optional, | dimension(:), allocatable | :: | linear_icol |
linear sparsity pattern nonzero elements column indices |
real(kind=wp), | intent(out), | optional, | dimension(:), allocatable | :: | linear_vals |
linear sparsity values (constant elements of the Jacobian) |
returns the sparsity pattern (if it is allocated)
Returns the sparsity pattern from the class. If it hasn't been computed, the output arrays will not be allocated.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(out), | dimension(:), allocatable | :: | irow |
sparsity pattern nonzero elements row indices |
|
integer, | intent(out), | dimension(:), allocatable | :: | icol |
sparsity pattern nonzero elements column indices |
|
integer, | intent(out), | optional, | dimension(:), allocatable | :: | linear_irow |
linear sparsity pattern nonzero elements row indices |
integer, | intent(out), | optional, | dimension(:), allocatable | :: | linear_icol |
linear sparsity pattern nonzero elements column indices |
real(kind=wp), | intent(out), | optional, | dimension(:), allocatable | :: | linear_vals |
linear sparsity values (constant elements of the Jacobian) |
integer, | intent(out), | optional | :: | maxgrp |
DSM sparsity partition |
|
integer, | intent(out), | optional, | dimension(:), allocatable | :: | ngrp |
DSM sparsity partition |
can be called by user to stop the computation
A user-callable routine. When called, it will terminate
all computations and return. The istat
return code will be
set to -1
. This can be called in the function or the info function.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me |
to check if an exception was raised.
Returns True if an exception has been raised.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(in) | :: | me |
the status of error condition
Returns the current error code and message.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(in) | :: | me | |||
integer, | intent(out), | optional | :: | istat |
error code ( |
|
character(len=:), | intent(out), | optional, | allocatable | :: | error_msg |
error message string. |
change the dpert
values
Change the dpert
vector. Can be used after the class has been initialized
to change the perturbation step sizes (e.g., after an iteration).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | dpert |
perturbation vector for |
destroy the sparsity pattern
Destroy the sparsity pattern in the class.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me |
Compute dx
, the perturbation vector for x
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(me%n) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(in), | dimension(me%n) | :: | dpert | ||
integer, | intent(in) | :: | perturb_mode | |||
real(kind=wp), | intent(out), | dimension(me%n) | :: | dx |
absolute perturbation (>0) for each variable |
computes the variable perturbation factor
Compute dx
, the perturbation vector for x
used
when computing the gradients.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(me%n) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(out), | dimension(me%n) | :: | dx |
absolute perturbation (>0) for each variable |
Compute dx
, the perturbation vector for x
used
when computing the sparsity pattern.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(me%n) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(out), | dimension(me%n) | :: | dx |
absolute perturbation (>0) for each variable |
Perturb the specified optimization variable, and compute the function.
This routine is designed so that df
is accumulated as each function
evaluation is done, to avoid having to allocate more temporary storage.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
nominal variable vector |
|
real(kind=wp), | intent(in) | :: | dx_factor |
factor to multiply |
||
real(kind=wp), | intent(in), | dimension(:) | :: | dx |
the perturbation value for this column |
|
real(kind=wp), | intent(in) | :: | df_factor |
factor to multiply function value |
||
integer, | intent(in) | :: | column |
the variable to perturb |
||
integer, | intent(in), | dimension(:) | :: | idx |
the elements in this column of the Jacobian to compute (passed to function) |
|
real(kind=wp), | intent(inout), | dimension(me%m) | :: | df |
the accumulated function value note: for the first call, this should be set to zero |
Perturb the specified optimization variable, and compute the function.
This routine is designed so that df
is accumulated as each function
evaluation is done, to avoid having to allocate more temporary storage.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
nominal variable vector |
|
real(kind=wp), | intent(in) | :: | dx_factor |
factor to multiply |
||
real(kind=wp), | intent(in), | dimension(:) | :: | dx |
the perturbation value for this column |
|
real(kind=wp), | intent(in) | :: | df_factor |
factor to multiply function value |
||
integer, | intent(in), | dimension(:) | :: | columns |
the variables to perturb |
|
integer, | intent(in), | dimension(:) | :: | idx |
the elements in this column of the Jacobian to compute (passed to function) |
|
real(kind=wp), | intent(inout), | dimension(me%m) | :: | df |
the accumulated function value note: for the first call, this should be set to zero |
Sets the variable bounds for sparsity in a numdiff_type.
These are only used for sparsity_mode=2
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
real(kind=wp), | intent(in), | optional, | dimension(:) | :: | xlow |
lower bounds on |
real(kind=wp), | intent(in), | optional, | dimension(:) | :: | xhigh |
upper bounds on |
Set sparsity mode.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | sparsity_mode |
the sparsity computation method:
1 - assume dense,
2 - three-point simple method,
3 - will be specified by the user in
a subsequent call to set_sparsity_pattern.
4 - computes a two-point jacobian
at |
||
real(kind=wp), | intent(in), | optional, | dimension(:) | :: | xlow_for_sparsity |
lower bounds on |
real(kind=wp), | intent(in), | optional, | dimension(:) | :: | xhigh_for_sparsity |
upper bounds on |
Generate a "dense" sparsity partition.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me |
A separate version of compute_jacobian to be used only when
computing the sparsity pattern in compute_sparsity_random_2.
It uses class_meths
and the sparsity dperts and bounds.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | i |
the column being computed |
||
type(meth_array), | intent(in) | :: | class_meths |
set of finite diff methods to use |
||
real(kind=wp), | intent(in), | dimension(:) | :: | x |
vector of variables (size |
|
real(kind=wp), | intent(out), | dimension(:), allocatable | :: | jac |
sparse jacobian vector |
Resize the sparsity arrays after accumulating them.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(inout) | :: | n_icol | |||
integer, | intent(inout) | :: | n_irow | |||
integer, | intent(inout) | :: | n_linear_icol | |||
integer, | intent(inout) | :: | n_linear_irow | |||
integer, | intent(inout) | :: | n_linear_vals |
Raise an exception.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | istat |
error code. |
||
character(len=*), | intent(in) | :: | routine |
the routine where the error was raised. |
||
character(len=*), | intent(in) | :: | error_msg |
error message string. |
Clear all exceptions.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(numdiff_type), | intent(inout) | :: | me |
type,public :: numdiff_type !! base type for sparsity and Jacobian computations. private logical :: exception_raised = .false. !! if true, an exception has been raised integer :: istat = 0 !! a non-zero value will cause all routine to exit. !! this can be set to `-1` by calling [[terminate]]. character(len=:),allocatable :: error_msg !! error message (if `istat/=0`) integer :: n = 0 !! number of `x` variables integer :: m = 0 !! number of `f` functions real(wp),dimension(:),allocatable :: xlow !! lower bounds on `x` real(wp),dimension(:),allocatable :: xhigh !! upper bounds on `x` logical :: print_messages = .true. !! if true, warning messages are printed !! to the `error_unit` for any errors. integer :: chunk_size = 100 !! chuck size for allocating the arrays (>0) integer :: perturb_mode = 1 !! perturbation mode: !! !! **1** - perturbation is `dx=dpert`, !! **2** - perturbation is `dx=dpert*x`, !! **3** - perturbation is `dx=dpert*(1+x)` real(wp),dimension(:),allocatable :: dpert !! perturbation vector for `x` logical :: partition_sparsity_pattern = .false. !! to partition the sparsity pattern using [[dsm]] type(sparsity_pattern) :: sparsity !! the sparsity pattern real(wp),dimension(:),allocatable :: xlow_for_sparsity !! lower bounds on `x` for computing sparsity (optional) real(wp),dimension(:),allocatable :: xhigh_for_sparsity !! upper bounds on `x` for computing sparsity (optional) real(wp),dimension(:),allocatable :: dpert_for_sparsity !! perturbation vector for `x` when computing !! sparsity for `sparsity_mode=4` integer :: num_sparsity_points = 3 !! for `sparsity_mode=4`, the number of jacobian !! evaluations used to estimate the sparsity pattern. !! See [[compute_sparsity_random_2]] integer :: sparsity_perturb_mode = 1 !! perturbation mode (if `sparsity_mode=4`): !! !! **1** - perturbation is `dx=dpert`, !! **2** - perturbation is `dx=dpert*x`, !! **3** - perturbation is `dx=dpert*(1+x)` ! if computing the sparsity pattern, we also have an option to ! compute the linear pattern, which indicates constant elements ! of the jacobian. these elements don't need to be computed again. logical :: compute_linear_sparsity_pattern = .false. !! to also compute the linear sparsity pattern real(wp) :: linear_sparsity_tol = epsilon(1.0_wp) !! the equality tolerance for derivatives to !! indicate a constant jacobian element (linear sparsity) real(wp) :: function_precision_tol = epsilon(1.0_wp) !! the function precision. two functions values !! that are the within this tolerance are !! considered the same value. This is used !! when estimating the sparsity pattern when !! `sparsity_mode=2` in [[compute_sparsity_random]] integer :: mode = 1 !! **1** = use `meth` (specified methods), !! **2** = use `class` (specified class, method is selected on-the-fly). type(finite_diff_method),dimension(:),allocatable :: meth !! the finite difference method to use !! compute the `n`th column of the Jacobian !! `size(n)`. Either this or `class` is used integer,dimension(:),allocatable :: class !! the class of method to use to !! compute the `n`th column of the Jacobian !! `size(n)`. Either this or `meth` is used type(meth_array),dimension(:),allocatable :: class_meths !! array of methods for the specified classes. !! used with `class` when `mode=2` ! parameters when using diff: real(wp) :: eps = 1.0e-9_wp !! tolerance parameter for [[diff]] real(wp) :: acc = 0.0_wp !! tolerance parameter for [[diff]] type(function_cache) :: cache !! if using the function cache logical :: use_diff = .false. !! if we are using the Neville's process method, !! rather than finite differences procedure(func),pointer :: problem_func => null() !! the user-defined function ! these are required to be defined by the user: procedure(func),pointer :: compute_function => null() !! compute the user-defined function !! this can point to the `problem_func` or, if using !! the cache, it points to [[compute_function_with_cache]]. procedure(spars_f),pointer :: compute_sparsity => null() !! for computing the sparsity pattern procedure(info_f),pointer :: info_function => null() !! an optional function the user can define !! which is called when each column of the jacobian is computed. !! It can be used to perform any setup operations. procedure(jacobian_f),pointer :: jacobian_function => null() !! the low-level function called by [[compute_jacobian]] !! that actually computes the jacobian. contains private procedure,public :: initialize => initialize_numdiff !! initialize the class procedure,public :: diff_initialize => initialize_numdiff_for_diff !! initialize the class procedure,public :: compute_jacobian !! main routine to compute the Jacobian !! using the selected options. It !! returns the sparse (vector) form. procedure,public :: compute_jacobian_dense !! return the dense `size(m,n)` !! matrix form of the Jacobian. procedure,public :: compute_jacobian_times_vector !! returns the product of the Jacobian !! matrix and an input vector procedure,public :: destroy => destroy_numdiff_type !! destroy the class procedure,public :: print_sparsity_pattern !! print the sparsity pattern in vector form to a file procedure,public :: print_sparsity_matrix !! print the sparsity pattern in matrix form to a file procedure,public :: set_sparsity_pattern !! manually set the sparsity pattern procedure,public :: select_finite_diff_method !! select a method in a specified class so !! that the variable bounds are not violated !! when by the perturbations. procedure,public :: select_finite_diff_method_for_partition_group !! version of [[select_finite_diff_method]] !! for partitioned sparsity pattern. procedure,public :: set_numdiff_bounds !! can be called to change the variable bounds. procedure,public :: compute_sparsity_pattern !! if the user needs to compute the sparsity pattern manually. !! (otherwise it will be done the first time the Jacobian is computed) procedure,public :: get_sparsity_pattern !! returns the sparsity pattern (if it is allocated) procedure,public :: terminate !! can be called by user to stop the computation procedure,public :: failed !! to check if an exception was raised. procedure,public :: get_error_status !! the status of error condition procedure,public :: set_dpert !! change the `dpert` values ! internal routines: procedure :: destroy_sparsity_pattern !! destroy the sparsity pattern procedure :: compute_perturb_vector procedure :: compute_perturbation_vector !! computes the variable perturbation factor procedure :: compute_sparsity_perturbation_vector procedure :: perturb_x_and_compute_f procedure :: perturb_x_and_compute_f_partitioned procedure :: set_numdiff_sparsity_bounds procedure :: set_sparsity_mode procedure :: generate_dense_sparsity_partition procedure :: compute_jacobian_for_sparsity procedure :: resize_sparsity_vectors procedure :: raise_exception procedure :: clear_exceptions end type numdiff_type