nifty8.minimization.energy_adapter module#

class EnergyAdapter(position, op, constants=[], want_metric=False, nanisinf=False)[source]#

Bases: Energy

Helper class which provides the traditional Nifty Energy interface to Nifty operators with a scalar target domain.

Parameters:
  • position (nifty8.field.Field or nifty8.multi_field.MultiField) – The position where the minimization process is started.

  • op (EnergyOperator) – The expression computing the energy from the input data.

  • constants (list of strings) – The component names of the operator’s input domain which are assumed to be constant during the minimization process. If the operator’s input domain is not a MultiField, this must be empty. Default: [].

  • want_metric (bool) – If True, the class will provide a metric property. This should only be enabled if it is required, because it will most likely consume additional resources. Default: False.

  • nanisinf (bool) – If true, nan energies which can happen due to overflows in the forward model are interpreted as inf. Thereby, the code does not crash on these occaisions but rather the minimizer is told that the position it has tried is not sensible.

__init__(position, op, constants=[], want_metric=False, nanisinf=False)[source]#
apply_metric(x)[source]#
Parameters:

x (nifty8.field.Field or nifty8.multi_field.MultiField) – Argument for the metric operator

Returns:

Output of the metric operator

Return type:

nifty8.field.Field or nifty8.multi_field.MultiField

at(position)[source]#

Returns a new Energy object, initialized at position.

Parameters:

position (nifty8.field.Field) – Location in parameter space for the new Energy object.

Returns:

Energy object at new position.

Return type:

Energy

property gradient#

The gradient at given position.

Type:

field

property metric#

implicitly defined metric. A positive semi-definite operator or function describing the metric of the potential at the given position.

Type:

LinearOperator

property value#

value of the functional.

The value of the energy functional at given position.

Type:

float

class StochasticEnergyAdapter(position, op, keys, local_ops, n_samples, comm, nanisinf, noise, _callingfrommake=False)[source]#

Bases: Energy

Provide the energy interface for an energy operator where parts of the input are averaged instead of optimized.

Specifically, a set of standard normal distributed samples are drawn for the input corresponding to keys and each sample is inserted partially into op. The resulting operators are then averaged. The subdomain that is not sampled is left a stochastic average of an energy with the remaining subdomain being the DOFs that are considered to be optimization parameters.

Notes

StochasticEnergyAdapter should never be created using the constructor, but rather via the factory function make.

__init__(position, op, keys, local_ops, n_samples, comm, nanisinf, noise, _callingfrommake=False)[source]#
apply_metric(x)[source]#
Parameters:

x (nifty8.field.Field or nifty8.multi_field.MultiField) – Argument for the metric operator

Returns:

Output of the metric operator

Return type:

nifty8.field.Field or nifty8.multi_field.MultiField

at(position)[source]#

Returns a new Energy object, initialized at position.

Parameters:

position (nifty8.field.Field) – Location in parameter space for the new Energy object.

Returns:

Energy object at new position.

Return type:

Energy

property gradient#

The gradient at given position.

Type:

field

static make(position, op, sampling_keys, n_samples, mirror_samples, comm=None, nanisinf=False)[source]#

Factory function for StochasticEnergyAdapter.

Parameters:
  • position (nifty8.multi_field.MultiField) – Values of the optimization parameters

  • op (Operator) – The objective function of the optimization problem. Must have a scalar target. The domain must be a MultiDomain with its keys being the union of sampling_keys and position.domain.keys().

  • sampling_keys (iterable of String) – The keys of the subdomain over which the stochastic average of op should be performed.

  • n_samples (int) – Number of samples used for the stochastic estimate.

  • mirror_samples (boolean) – Whether the negative of the drawn samples are also used, as they are equally legitimate samples. If true, the number of used samples doubles.

  • comm (MPI communicator or None) – If not None, samples will be distributed as evenly as possible across this communicator. If mirror_samples is set, then a sample and its mirror image will always reside on the same task.

  • nanisinf (bool) – If true, nan energies, which can occur due to overflows in the forward model, are interpreted as inf which can be interpreted by optimizers.

property metric#

implicitly defined metric. A positive semi-definite operator or function describing the metric of the potential at the given position.

Type:

LinearOperator

resample_at(position)[source]#
samples()[source]#
property value#

value of the functional.

The value of the energy functional at given position.

Type:

float