2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
43 #include "gromacs/utility/basedefinitions.h"
44 #include "gromacs/utility/real.h"
46 static void nrerror(const char error_text[], gmx_bool bExit)
48 fprintf(stderr, "Numerical Recipes run-time error...\n");
49 fprintf(stderr, "%s\n", error_text);
52 fprintf(stderr, "...now exiting to system...\n");
57 /* dont use the keyword vector - it will clash with the
58 * altivec extensions used for powerpc processors.
61 static real *rvector(int nl, int nh)
65 v = (real *)malloc((unsigned) (nh-nl+1)*sizeof(real));
68 nrerror("allocation failure in rvector()", TRUE);
70 /* cppcheck-suppress memleak
71 * free_vector does the same vector arithmetic */
75 static int *ivector(int nl, int nh)
79 v = (int *)malloc((unsigned) (nh-nl+1)*sizeof(int));
82 nrerror("allocation failure in ivector()", TRUE);
84 /* cppcheck-suppress memleak
85 * free_vector does the same vector arithmetic */
90 static real **matrix1(int nrl, int nrh, int ncl, int nch)
95 m = (real **) malloc((unsigned) (nrh-nrl+1)*sizeof(real*));
98 nrerror("allocation failure 1 in matrix1()", TRUE);
102 for (i = nrl; i <= nrh; i++)
104 m[i] = (real *) malloc((unsigned) (nch-ncl+1)*sizeof(real));
107 nrerror("allocation failure 2 in matrix1()", TRUE);
114 static double **dmatrix(int nrl, int nrh, int ncl, int nch)
119 m = (double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
122 nrerror("allocation failure 1 in dmatrix()", TRUE);
126 for (i = nrl; i <= nrh; i++)
128 m[i] = (double *) malloc((unsigned) (nch-ncl+1)*sizeof(double));
131 nrerror("allocation failure 2 in dmatrix()", TRUE);
138 static int **imatrix1(int nrl, int nrh, int ncl, int nch)
142 m = (int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*));
145 nrerror("allocation failure 1 in imatrix1()", TRUE);
149 for (i = nrl; i <= nrh; i++)
151 m[i] = (int *)malloc((unsigned) (nch-ncl+1)*sizeof(int));
154 nrerror("allocation failure 2 in imatrix1()", TRUE);
163 static real **submatrix(real **a, int oldrl, int oldrh, int oldcl,
164 int newrl, int newcl)
169 m = (real **) malloc((unsigned) (oldrh-oldrl+1)*sizeof(real*));
172 nrerror("allocation failure in submatrix()", TRUE);
176 for (i = oldrl, j = newrl; i <= oldrh; i++, j++)
178 m[j] = a[i]+oldcl-newcl;
186 static void free_vector(real *v, int nl)
188 free((char*) (v+nl));
191 static void free_ivector(int *v, int nl)
193 free((char*) (v+nl));
196 static void free_dvector(int *v, int nl)
198 free((char*) (v+nl));
203 static void free_matrix(real **m, int nrl, int nrh, int ncl)
207 for (i = nrh; i >= nrl; i--)
209 free((char*) (m[i]+ncl));
211 free((char*) (m+nrl));
214 static real **convert_matrix(real *a, int nrl, int nrh, int ncl, int nch)
216 int i, j, nrow, ncol;
221 m = (real **) malloc((unsigned) (nrow)*sizeof(real*));
224 nrerror("allocation failure in convert_matrix()", TRUE);
227 for (i = 0, j = nrl; i <= nrow-1; i++, j++)
236 static void free_convert_matrix(real **b, int nrl)
238 free((char*) (b+nrl));
241 #define SWAP(a, b) {real temp = (a); (a) = (b); (b) = temp; }
243 static void dump_mat(int n, real **a)
247 for (i = 1; (i <= n); i++)
249 for (j = 1; (j <= n); j++)
251 fprintf(stderr, " %10.3f", a[i][j]);
253 fprintf(stderr, "\n");
257 gmx_bool gaussj(real **a, int n, real **b, int m)
259 int *indxc, *indxr, *ipiv;
260 int i, icol = 0, irow = 0, j, k, l, ll;
261 real big, dum, pivinv;
263 indxc = ivector(1, n);
264 indxr = ivector(1, n);
265 ipiv = ivector(1, n);
266 for (j = 1; j <= n; j++)
270 for (i = 1; i <= n; i++)
273 for (j = 1; j <= n; j++)
277 for (k = 1; k <= n; k++)
281 if (fabs(a[j][k]) >= big)
288 else if (ipiv[k] > 1)
290 nrerror("GAUSSJ: Singular Matrix-1", FALSE);
299 for (l = 1; l <= n; l++)
301 SWAP(a[irow][l], a[icol][l]);
303 for (l = 1; l <= m; l++)
305 SWAP(b[irow][l], b[icol][l]);
310 if (a[icol][icol] == 0.0)
312 fprintf(stderr, "irow = %d, icol = %d\n", irow, icol);
314 nrerror("GAUSSJ: Singular Matrix-2", FALSE);
317 pivinv = 1.0/a[icol][icol];
319 for (l = 1; l <= n; l++)
321 a[icol][l] *= pivinv;
323 for (l = 1; l <= m; l++)
325 b[icol][l] *= pivinv;
327 for (ll = 1; ll <= n; ll++)
333 for (l = 1; l <= n; l++)
335 a[ll][l] -= a[icol][l]*dum;
337 for (l = 1; l <= m; l++)
339 b[ll][l] -= b[icol][l]*dum;
344 for (l = n; l >= 1; l--)
346 if (indxr[l] != indxc[l])
348 for (k = 1; k <= n; k++)
350 SWAP(a[k][indxr[l]], a[k][indxc[l]]);
354 free_ivector(ipiv, 1);
355 free_ivector(indxr, 1);
356 free_ivector(indxc, 1);
364 static void covsrt(real **covar, int ma, int lista[], int mfit)
369 for (j = 1; j < ma; j++)
371 for (i = j+1; i <= ma; i++)
376 for (i = 1; i < mfit; i++)
378 for (j = i+1; j <= mfit; j++)
380 if (lista[j] > lista[i])
382 covar[lista[j]][lista[i]] = covar[i][j];
386 covar[lista[i]][lista[j]] = covar[i][j];
391 for (j = 1; j <= ma; j++)
393 covar[1][j] = covar[j][j];
396 covar[lista[1]][lista[1]] = swap;
397 for (j = 2; j <= mfit; j++)
399 covar[lista[j]][lista[j]] = covar[1][j];
401 for (j = 2; j <= ma; j++)
403 for (i = 1; i <= j-1; i++)
405 covar[i][j] = covar[j][i];
410 #define SWAP(a, b) {swap = (a); (a) = (b); (b) = swap; }
412 static void covsrt_new(real **covar, int ma, int ia[], int mfit)
413 /* Expand in storage the covariance matrix covar, so as to take
414 * into account parameters that are being held fixed. (For the
415 * latter, return zero covariances.)
420 for (i = mfit+1; i <= ma; i++)
422 for (j = 1; j <= i; j++)
424 covar[i][j] = covar[j][i] = 0.0;
428 for (j = ma; j >= 1; j--)
432 for (i = 1; i <= ma; i++)
434 SWAP(covar[i][k], covar[i][j]);
436 for (i = 1; i <= ma; i++)
438 SWAP(covar[k][i], covar[j][i]);
446 static void mrqcof(real x[], real y[], real sig[], int ndata, real a[],
447 int ma, int lista[], int mfit,
448 real **alpha, real beta[], real *chisq,
449 void (*funcs)(real, real *, real *, real *))
452 real ymod, wt, sig2i, dy, *dyda;
454 dyda = rvector(1, ma);
455 for (j = 1; j <= mfit; j++)
457 for (k = 1; k <= j; k++)
464 for (i = 1; i <= ndata; i++)
466 (*funcs)(x[i], a, &ymod, dyda);
467 sig2i = 1.0/(sig[i]*sig[i]);
469 for (j = 1; j <= mfit; j++)
471 wt = dyda[lista[j]]*sig2i;
472 for (k = 1; k <= j; k++)
474 alpha[j][k] += wt*dyda[lista[k]];
478 (*chisq) += dy*dy*sig2i;
480 for (j = 2; j <= mfit; j++)
482 for (k = 1; k <= j-1; k++)
484 alpha[k][j] = alpha[j][k];
487 free_vector(dyda, 1);
491 gmx_bool mrqmin(real x[], real y[], real sig[], int ndata, real a[],
492 int ma, int lista[], int mfit,
493 real **covar, real **alpha, real *chisq,
494 void (*funcs)(real, real *, real *, real *),
498 static real *da, *atry, **oneda, *beta, ochisq;
502 oneda = matrix1(1, mfit, 1, 1);
503 atry = rvector(1, ma);
505 beta = rvector(1, ma);
507 for (j = 1; j <= ma; j++)
510 for (k = 1; k <= mfit; k++)
523 nrerror("Bad LISTA permutation in MRQMIN-1", FALSE);
529 nrerror("Bad LISTA permutation in MRQMIN-2", FALSE);
533 mrqcof(x, y, sig, ndata, a, ma, lista, mfit, alpha, beta, chisq, funcs);
536 for (j = 1; j <= mfit; j++)
538 for (k = 1; k <= mfit; k++)
540 covar[j][k] = alpha[j][k];
542 covar[j][j] = alpha[j][j]*(1.0+(*alamda));
543 oneda[j][1] = beta[j];
545 if (!gaussj(covar, mfit, oneda, 1))
549 for (j = 1; j <= mfit; j++)
555 covsrt(covar, ma, lista, mfit);
556 free_vector(beta, 1);
558 free_vector(atry, 1);
559 free_matrix(oneda, 1, mfit, 1);
562 for (j = 1; j <= ma; j++)
566 for (j = 1; j <= mfit; j++)
568 atry[lista[j]] = a[lista[j]]+da[j];
570 mrqcof(x, y, sig, ndata, atry, ma, lista, mfit, covar, da, chisq, funcs);
575 for (j = 1; j <= mfit; j++)
577 for (k = 1; k <= mfit; k++)
579 alpha[j][k] = covar[j][k];
582 a[lista[j]] = atry[lista[j]];
594 gmx_bool mrqmin_new(real x[], real y[], real sig[], int ndata, real a[],
595 int ia[], int ma, real **covar, real **alpha, real *chisq,
596 void (*funcs)(real, real [], real *, real []),
598 /* Levenberg-Marquardt method, attempting to reduce the value Chi^2
599 * of a fit between a set of data points x[1..ndata], y[1..ndata]
600 * with individual standard deviations sig[1..ndata], and a nonlinear
601 * function dependent on ma coefficients a[1..ma]. The input array
602 * ia[1..ma] indicates by nonzero entries those components of a that
603 * should be fitted for, and by zero entries those components that
604 * should be held fixed at their input values. The program returns
605 * current best-fit values for the parameters a[1..ma], and
606 * Chi^2 = chisq. The arrays covar[1..ma][1..ma], alpha[1..ma][1..ma]
607 * are used as working space during most iterations. Supply a routine
608 * funcs(x,a,yfit,dyda,ma) that evaluates the fitting function yfit,
609 * and its derivatives dyda[1..ma] with respect to the fitting
610 * parameters a at x. On the first call provide an initial guess for
611 * the parameters a, and set alamda < 0 for initialization (which then
612 * sets alamda=.001). If a step succeeds chisq becomes smaller and
613 * alamda de-creases by a factor of 10. If a step fails alamda grows by
614 * a factor of 10. You must call this routine repeatedly until
615 * convergence is achieved. Then, make one final call with alamda=0,
616 * so that covar[1..ma][1..ma] returns the covariance matrix, and alpha
617 * the curvature matrix.
618 * (Parameters held fixed will return zero covariances.)
621 void covsrt(real **covar, int ma, int ia[], int mfit);
622 gmx_bool gaussj(real **a, int n, real **b, int m);
623 void mrqcof_new(real x[], real y[], real sig[], int ndata, real a[],
624 int ia[], int ma, real **alpha, real beta[], real *chisq,
625 void (*funcs)(real, real [], real *, real []));
628 static real ochisq, *atry, *beta, *da, **oneda;
630 if (*alamda < 0.0) /* Initialization. */
632 atry = rvector(1, ma);
633 beta = rvector(1, ma);
635 for (mfit = 0, j = 1; j <= ma; j++)
642 oneda = matrix1(1, mfit, 1, 1);
644 mrqcof_new(x, y, sig, ndata, a, ia, ma, alpha, beta, chisq, funcs);
646 for (j = 1; j <= ma; j++)
651 for (j = 1; j <= mfit; j++) /* Alter linearized fitting matrix, by augmenting. */
653 for (k = 1; k <= mfit; k++)
655 covar[j][k] = alpha[j][k]; /* diagonal elements. */
657 covar[j][j] = alpha[j][j]*(1.0+(*alamda));
658 oneda[j][1] = beta[j];
660 if (!gaussj(covar, mfit, oneda, 1)) /* Matrix solution. */
664 for (j = 1; j <= mfit; j++)
668 if (*alamda == 0.0) /* Once converged, evaluate covariance matrix. */
670 covsrt_new(covar, ma, ia, mfit);
671 free_matrix(oneda, 1, mfit, 1);
673 free_vector(beta, 1);
674 free_vector(atry, 1);
677 for (j = 0, l = 1; l <= ma; l++) /* Did the trial succeed? */
681 atry[l] = a[l]+da[++j];
684 mrqcof_new(x, y, sig, ndata, atry, ia, ma, covar, da, chisq, funcs);
687 /* Success, accept the new solution. */
690 for (j = 1; j <= mfit; j++)
692 for (k = 1; k <= mfit; k++)
694 alpha[j][k] = covar[j][k];
698 for (l = 1; l <= ma; l++)
703 else /* Failure, increase alamda and return. */
711 void mrqcof_new(real x[], real y[], real sig[], int ndata, real a[],
712 int ia[], int ma, real **alpha, real beta[], real *chisq,
713 void (*funcs)(real, real [], real *, real[]))
714 /* Used by mrqmin to evaluate the linearized fitting matrix alpha, and
715 * vector beta as in (15.5.8), and calculate Chi^2.
718 int i, j, k, l, m, mfit = 0;
719 real ymod, wt, sig2i, dy, *dyda;
721 dyda = rvector(1, ma);
722 for (j = 1; j <= ma; j++)
729 for (j = 1; j <= mfit; j++) /* Initialize (symmetric) alpha), beta. */
731 for (k = 1; k <= j; k++)
738 for (i = 1; i <= ndata; i++) /* Summation loop over all data. */
740 (*funcs)(x[i], a, &ymod, dyda);
741 sig2i = 1.0/(sig[i]*sig[i]);
743 for (j = 0, l = 1; l <= ma; l++)
748 for (j++, k = 0, m = 1; m <= l; m++)
752 alpha[j][++k] += wt*dyda[m];
758 *chisq += dy*dy*sig2i; /* And find Chi^2. */
760 for (j = 2; j <= mfit; j++) /* Fill in the symmetric side. */
762 for (k = 1; k < j; k++)
764 alpha[k][j] = alpha[j][k];
767 free_vector(dyda, 1);