1 """ @package forcebalance.abinitio Ab-initio fitting module (energies, forces, resp). 6 from __future__
import division
8 from builtins
import range
12 from forcebalance.nifty import col, eqcgmx, flat, floatornan, fqcgmx, invert_svd, kb, printcool, bohr2ang
13 from forcebalance.target
import Target
15 from re
import match, sub
17 from subprocess
import PIPE
20 from forcebalance.output
import getLogger
21 logger = getLogger(__name__)
35 """ Subclass of Target for general least squares fitting. """ 37 def __init__(self,options,tgt_opts,forcefield):
38 super(LeastSquares,self).
__init__(options,tgt_opts,forcefield)
42 MAD = np.mean(np.abs(self.
D))
43 logger.info(
"\rTarget: %-15s MeanAbsErr/MeanExact: %.5e Objective = %.5e" % (self.name, MAD / self.
MAQ, self.
objective))
46 def get(self, mvals, AGrad=False, AHess=False):
50 This subroutine builds the objective function (and optionally 51 its derivatives) from a general software. 53 This subroutine interfaces with simulation software 'drivers'. 54 The driver is expected to give exact values, fitting values, and weights. 56 @param[in] mvals Mathematical parameter values 57 @param[in] AGrad Switch to turn on analytic gradient 58 @param[in] AHess Switch to turn on analytic Hessian 59 @return Answer Contribution to the objective function 61 global LAST_MVALS, CHECK_BASIS
65 if LAST_MVALS
is None or not (mvals == LAST_MVALS).
all():
77 pvals = self.FF.make(mvals)
78 if float(
'Inf')
in pvals:
79 return {
'X' : 1e10,
'G' : G,
'H' : H}
86 self.
MAQ = np.mean(np.abs(Q))
101 dM_arr =
f1d2p(
fdwrap(callM, mvals, p), h = self.h, f0 = M)
102 if np.max(np.abs(dM_arr)) == 0.0
and (
not self.evaluated):
103 logger.info(
"\r Simulation %s will skip over parameter %i in subsequent steps\n" % (self.name, p))
106 dM[p] = dM_arr.copy()
109 Objective = np.dot(W, D**2) * Fac
112 G[p] = 2 * np.dot(W, D*dM[p])
113 if not AHess:
continue 114 H[p, p] = 2 * np.dot(W, dM[p]**2)
116 if q
not in self.pgrad:
continue 117 GNP = 2 * np.dot(W, dM[p] * dM[q])
122 Answer = {
'X':Objective,
'G':G,
'H':H}
126 LAST_MVALS = mvals.copy()
Nifty functions, intended to be imported by any module within ForceBalance.
Subclass of Target for general least squares fitting.
def get(self, mvals, AGrad=False, AHess=False)
LPW 05-30-2012.
def in_fd()
Invoking this function from anywhere will tell us whether we're being called by a finite-difference f...
def fdwrap(func, mvals0, pidx, key=None, kwargs)
A function wrapper for finite difference designed for differentiating 'get'-type functions.
def __init__(self, options, tgt_opts, forcefield)
MAQ
Dictionary for derivative terms.
def f1d2p(f, h, f0=None)
A two-point finite difference stencil.