#
# KIM-API: An API for interatomic models
# Copyright (c) 2013--2022, Regents of the University of Minnesota.
# All rights reserved.
#
# Contributors:
#    Ryan S. Elliott
#    Ellad B. Tadmor
#
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#


This directory (utility_forces_numer_deriv) contains a Verification Check that
compares the forces computed by a KIM-compliant Model with those obtained using
numerical differentiation of the Model energies.

Calling format:

   printf "model_name" | ./utility_forces_numer_deriv

Notes:

1. It checks a single configuration: a perturbed fcc cluster made of a random
   arrangement of the supported species with spacing scaled by a factor based
   on the Model's cutoff value.

2. The code computes the DIM*N components of the force vector in two ways:
   directly using the Model's force calculation (`force_model') and by
   numerical differentiation using Ridders' method (`force_numer').  The
   difference between these values in absolute value is also printed (`Force
   diff') as well as the error predicted by Ridders' method (`pred error').
   Ideally, one would expect the force difference to be less than the predicted
   error.  If that is the case the term `ok' is printed at the end of the
   line.  (Most potentials fail this check for most degrees of freedom, so this
   is not a good criterion.)

   In addition, the following normalized measure is computed:

   \alpha = 1/(DIM*N) sqrt( \sum_i w_i (f^{model}_i - f^{numer}_i)^2 / \sum_i w_i ),

   where w_i is the weight associated with term i:

   w_i = 1/\hat{\epsilon}_i,

   and \hat{\epsilon}_i is the normalized error in the numerical calculation:

   \hat{\epsilon}_i = max(\epsilon^{numer}_i, \epsilon^{prec}) /
                      max(\force^{numer}_i, \epsilon^{prec})

   The max functions impose a lower limit on computed values equal to the
   numerical precision of the computer (\epsilon^{prec}).

   \alpha has units of force and can be understood as the average error per
   degree of freedom.  The smaller the value of \alpha the more accurate the
   force calculation of the Model.

   In the expression for \alpha each term is weighted by the relative accuracy
   of the numerical estimate for the derivative.  Thus if f^{numer}_i is a poor
   estimate for the derivative (i.e. \epsilon^{numer}_i is large relative to
   f^{numer}_i) then term i contributes less to \alpha since it is less
   trustworthy.

   In addition to \alpha, the maximum term contributing to \alpha is
   identified.  For this degree of freedom, the error and normalized error are
   printed:

   forcediff_i = |f^{model}_i - f^{numer}_i}|

   forcediff_i/|f^{model}_i|

   This is *not* the largest error (i.e. the largest value of forcediff_i) due
   to the weighting term w_i.  Simply looking at the maximum value of
   forcediff_i could be misleading since the error in the numerical term,
   \epsilon^{numer}_i, may be large for the component.  Instead the maximum of
   w_i*forcediff_i is a compromise that in some sense seeks the largest error
   for components where the numerical derivative are also as accurate possible.
