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.
45 #include "types/simple.h"
47 static void nrerror(const char error_text[], gmx_bool bExit)
49 fprintf(stderr, "Numerical Recipes run-time error...\n");
50 fprintf(stderr, "%s\n", error_text);
53 fprintf(stderr, "...now exiting to system...\n");
58 /* dont use the keyword vector - it will clash with the
59 * altivec extensions used for powerpc processors.
62 static real *rvector(int nl, int nh)
66 v = (real *)malloc((unsigned) (nh-nl+1)*sizeof(real));
69 nrerror("allocation failure in rvector()", TRUE);
71 /* cppcheck-suppress memleak
72 * free_vector does the same vector arithmetic */
76 static int *ivector(int nl, int nh)
80 v = (int *)malloc((unsigned) (nh-nl+1)*sizeof(int));
83 nrerror("allocation failure in ivector()", TRUE);
85 /* cppcheck-suppress memleak
86 * free_vector does the same vector arithmetic */
91 static real **matrix1(int nrl, int nrh, int ncl, int nch)
96 m = (real **) malloc((unsigned) (nrh-nrl+1)*sizeof(real*));
99 nrerror("allocation failure 1 in matrix1()", TRUE);
103 for (i = nrl; i <= nrh; i++)
105 m[i] = (real *) malloc((unsigned) (nch-ncl+1)*sizeof(real));
108 nrerror("allocation failure 2 in matrix1()", TRUE);
115 static double **dmatrix(int nrl, int nrh, int ncl, int nch)
120 m = (double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
123 nrerror("allocation failure 1 in dmatrix()", TRUE);
127 for (i = nrl; i <= nrh; i++)
129 m[i] = (double *) malloc((unsigned) (nch-ncl+1)*sizeof(double));
132 nrerror("allocation failure 2 in dmatrix()", TRUE);
139 static int **imatrix1(int nrl, int nrh, int ncl, int nch)
143 m = (int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*));
146 nrerror("allocation failure 1 in imatrix1()", TRUE);
150 for (i = nrl; i <= nrh; i++)
152 m[i] = (int *)malloc((unsigned) (nch-ncl+1)*sizeof(int));
155 nrerror("allocation failure 2 in imatrix1()", TRUE);
164 static real **submatrix(real **a, int oldrl, int oldrh, int oldcl,
165 int newrl, int newcl)
170 m = (real **) malloc((unsigned) (oldrh-oldrl+1)*sizeof(real*));
173 nrerror("allocation failure in submatrix()", TRUE);
177 for (i = oldrl, j = newrl; i <= oldrh; i++, j++)
179 m[j] = a[i]+oldcl-newcl;
187 static void free_vector(real *v, int nl)
189 free((char*) (v+nl));
192 static void free_ivector(int *v, int nl)
194 free((char*) (v+nl));
197 static void free_dvector(int *v, int nl)
199 free((char*) (v+nl));
204 static void free_matrix(real **m, int nrl, int nrh, int ncl)
208 for (i = nrh; i >= nrl; i--)
210 free((char*) (m[i]+ncl));
212 free((char*) (m+nrl));
215 static real **convert_matrix(real *a, int nrl, int nrh, int ncl, int nch)
217 int i, j, nrow, ncol;
222 m = (real **) malloc((unsigned) (nrow)*sizeof(real*));
225 nrerror("allocation failure in convert_matrix()", TRUE);
228 for (i = 0, j = nrl; i <= nrow-1; i++, j++)
237 static void free_convert_matrix(real **b, int nrl)
239 free((char*) (b+nrl));
242 #define SWAP(a, b) {real temp = (a); (a) = (b); (b) = temp; }
244 static void dump_mat(int n, real **a)
248 for (i = 1; (i <= n); i++)
250 for (j = 1; (j <= n); j++)
252 fprintf(stderr, " %10.3f", a[i][j]);
254 fprintf(stderr, "\n");
258 gmx_bool gaussj(real **a, int n, real **b, int m)
260 int *indxc, *indxr, *ipiv;
261 int i, icol = 0, irow = 0, j, k, l, ll;
262 real big, dum, pivinv;
264 indxc = ivector(1, n);
265 indxr = ivector(1, n);
266 ipiv = ivector(1, n);
267 for (j = 1; j <= n; j++)
271 for (i = 1; i <= n; i++)
274 for (j = 1; j <= n; j++)
278 for (k = 1; k <= n; k++)
282 if (fabs(a[j][k]) >= big)
289 else if (ipiv[k] > 1)
291 nrerror("GAUSSJ: Singular Matrix-1", FALSE);
300 for (l = 1; l <= n; l++)
302 SWAP(a[irow][l], a[icol][l]);
304 for (l = 1; l <= m; l++)
306 SWAP(b[irow][l], b[icol][l]);
311 if (a[icol][icol] == 0.0)
313 fprintf(stderr, "irow = %d, icol = %d\n", irow, icol);
315 nrerror("GAUSSJ: Singular Matrix-2", FALSE);
318 pivinv = 1.0/a[icol][icol];
320 for (l = 1; l <= n; l++)
322 a[icol][l] *= pivinv;
324 for (l = 1; l <= m; l++)
326 b[icol][l] *= pivinv;
328 for (ll = 1; ll <= n; ll++)
334 for (l = 1; l <= n; l++)
336 a[ll][l] -= a[icol][l]*dum;
338 for (l = 1; l <= m; l++)
340 b[ll][l] -= b[icol][l]*dum;
345 for (l = n; l >= 1; l--)
347 if (indxr[l] != indxc[l])
349 for (k = 1; k <= n; k++)
351 SWAP(a[k][indxr[l]], a[k][indxc[l]]);
355 free_ivector(ipiv, 1);
356 free_ivector(indxr, 1);
357 free_ivector(indxc, 1);
365 static void covsrt(real **covar, int ma, int lista[], int mfit)
370 for (j = 1; j < ma; j++)
372 for (i = j+1; i <= ma; i++)
377 for (i = 1; i < mfit; i++)
379 for (j = i+1; j <= mfit; j++)
381 if (lista[j] > lista[i])
383 covar[lista[j]][lista[i]] = covar[i][j];
387 covar[lista[i]][lista[j]] = covar[i][j];
392 for (j = 1; j <= ma; j++)
394 covar[1][j] = covar[j][j];
397 covar[lista[1]][lista[1]] = swap;
398 for (j = 2; j <= mfit; j++)
400 covar[lista[j]][lista[j]] = covar[1][j];
402 for (j = 2; j <= ma; j++)
404 for (i = 1; i <= j-1; i++)
406 covar[i][j] = covar[j][i];
411 #define SWAP(a, b) {swap = (a); (a) = (b); (b) = swap; }
413 static void covsrt_new(real **covar, int ma, int ia[], int mfit)
414 /* Expand in storage the covariance matrix covar, so as to take
415 * into account parameters that are being held fixed. (For the
416 * latter, return zero covariances.)
421 for (i = mfit+1; i <= ma; i++)
423 for (j = 1; j <= i; j++)
425 covar[i][j] = covar[j][i] = 0.0;
429 for (j = ma; j >= 1; j--)
433 for (i = 1; i <= ma; i++)
435 SWAP(covar[i][k], covar[i][j]);
437 for (i = 1; i <= ma; i++)
439 SWAP(covar[k][i], covar[j][i]);
447 static void mrqcof(real x[], real y[], real sig[], int ndata, real a[],
448 int ma, int lista[], int mfit,
449 real **alpha, real beta[], real *chisq,
450 void (*funcs)(real, real *, real *, real *))
453 real ymod, wt, sig2i, dy, *dyda;
455 dyda = rvector(1, ma);
456 for (j = 1; j <= mfit; j++)
458 for (k = 1; k <= j; k++)
465 for (i = 1; i <= ndata; i++)
467 (*funcs)(x[i], a, &ymod, dyda);
468 sig2i = 1.0/(sig[i]*sig[i]);
470 for (j = 1; j <= mfit; j++)
472 wt = dyda[lista[j]]*sig2i;
473 for (k = 1; k <= j; k++)
475 alpha[j][k] += wt*dyda[lista[k]];
479 (*chisq) += dy*dy*sig2i;
481 for (j = 2; j <= mfit; j++)
483 for (k = 1; k <= j-1; k++)
485 alpha[k][j] = alpha[j][k];
488 free_vector(dyda, 1);
492 gmx_bool mrqmin(real x[], real y[], real sig[], int ndata, real a[],
493 int ma, int lista[], int mfit,
494 real **covar, real **alpha, real *chisq,
495 void (*funcs)(real, real *, real *, real *),
499 static real *da, *atry, **oneda, *beta, ochisq;
503 oneda = matrix1(1, mfit, 1, 1);
504 atry = rvector(1, ma);
506 beta = rvector(1, ma);
508 for (j = 1; j <= ma; j++)
511 for (k = 1; k <= mfit; k++)
524 nrerror("Bad LISTA permutation in MRQMIN-1", FALSE);
530 nrerror("Bad LISTA permutation in MRQMIN-2", FALSE);
534 mrqcof(x, y, sig, ndata, a, ma, lista, mfit, alpha, beta, chisq, funcs);
537 for (j = 1; j <= mfit; j++)
539 for (k = 1; k <= mfit; k++)
541 covar[j][k] = alpha[j][k];
543 covar[j][j] = alpha[j][j]*(1.0+(*alamda));
544 oneda[j][1] = beta[j];
546 if (!gaussj(covar, mfit, oneda, 1))
550 for (j = 1; j <= mfit; j++)
556 covsrt(covar, ma, lista, mfit);
557 free_vector(beta, 1);
559 free_vector(atry, 1);
560 free_matrix(oneda, 1, mfit, 1);
563 for (j = 1; j <= ma; j++)
567 for (j = 1; j <= mfit; j++)
569 atry[lista[j]] = a[lista[j]]+da[j];
571 mrqcof(x, y, sig, ndata, atry, ma, lista, mfit, covar, da, chisq, funcs);
576 for (j = 1; j <= mfit; j++)
578 for (k = 1; k <= mfit; k++)
580 alpha[j][k] = covar[j][k];
583 a[lista[j]] = atry[lista[j]];
595 gmx_bool mrqmin_new(real x[], real y[], real sig[], int ndata, real a[],
596 int ia[], int ma, real **covar, real **alpha, real *chisq,
597 void (*funcs)(real, real [], real *, real []),
599 /* Levenberg-Marquardt method, attempting to reduce the value Chi^2
600 * of a fit between a set of data points x[1..ndata], y[1..ndata]
601 * with individual standard deviations sig[1..ndata], and a nonlinear
602 * function dependent on ma coefficients a[1..ma]. The input array
603 * ia[1..ma] indicates by nonzero entries those components of a that
604 * should be fitted for, and by zero entries those components that
605 * should be held fixed at their input values. The program returns
606 * current best-fit values for the parameters a[1..ma], and
607 * Chi^2 = chisq. The arrays covar[1..ma][1..ma], alpha[1..ma][1..ma]
608 * are used as working space during most iterations. Supply a routine
609 * funcs(x,a,yfit,dyda,ma) that evaluates the fitting function yfit,
610 * and its derivatives dyda[1..ma] with respect to the fitting
611 * parameters a at x. On the first call provide an initial guess for
612 * the parameters a, and set alamda < 0 for initialization (which then
613 * sets alamda=.001). If a step succeeds chisq becomes smaller and
614 * alamda de-creases by a factor of 10. If a step fails alamda grows by
615 * a factor of 10. You must call this routine repeatedly until
616 * convergence is achieved. Then, make one final call with alamda=0,
617 * so that covar[1..ma][1..ma] returns the covariance matrix, and alpha
618 * the curvature matrix.
619 * (Parameters held fixed will return zero covariances.)
622 void covsrt(real **covar, int ma, int ia[], int mfit);
623 gmx_bool gaussj(real **a, int n, real **b, int m);
624 void mrqcof_new(real x[], real y[], real sig[], int ndata, real a[],
625 int ia[], int ma, real **alpha, real beta[], real *chisq,
626 void (*funcs)(real, real [], real *, real []));
629 static real ochisq, *atry, *beta, *da, **oneda;
631 if (*alamda < 0.0) /* Initialization. */
633 atry = rvector(1, ma);
634 beta = rvector(1, ma);
636 for (mfit = 0, j = 1; j <= ma; j++)
643 oneda = matrix1(1, mfit, 1, 1);
645 mrqcof_new(x, y, sig, ndata, a, ia, ma, alpha, beta, chisq, funcs);
647 for (j = 1; j <= ma; j++)
652 for (j = 1; j <= mfit; j++) /* Alter linearized fitting matrix, by augmenting. */
654 for (k = 1; k <= mfit; k++)
656 covar[j][k] = alpha[j][k]; /* diagonal elements. */
658 covar[j][j] = alpha[j][j]*(1.0+(*alamda));
659 oneda[j][1] = beta[j];
661 if (!gaussj(covar, mfit, oneda, 1)) /* Matrix solution. */
665 for (j = 1; j <= mfit; j++)
669 if (*alamda == 0.0) /* Once converged, evaluate covariance matrix. */
671 covsrt_new(covar, ma, ia, mfit);
672 free_matrix(oneda, 1, mfit, 1);
674 free_vector(beta, 1);
675 free_vector(atry, 1);
678 for (j = 0, l = 1; l <= ma; l++) /* Did the trial succeed? */
682 atry[l] = a[l]+da[++j];
685 mrqcof_new(x, y, sig, ndata, atry, ia, ma, covar, da, chisq, funcs);
688 /* Success, accept the new solution. */
691 for (j = 1; j <= mfit; j++)
693 for (k = 1; k <= mfit; k++)
695 alpha[j][k] = covar[j][k];
699 for (l = 1; l <= ma; l++)
704 else /* Failure, increase alamda and return. */
712 void mrqcof_new(real x[], real y[], real sig[], int ndata, real a[],
713 int ia[], int ma, real **alpha, real beta[], real *chisq,
714 void (*funcs)(real, real [], real *, real[]))
715 /* Used by mrqmin to evaluate the linearized fitting matrix alpha, and
716 * vector beta as in (15.5.8), and calculate Chi^2.
719 int i, j, k, l, m, mfit = 0;
720 real ymod, wt, sig2i, dy, *dyda;
722 dyda = rvector(1, ma);
723 for (j = 1; j <= ma; j++)
730 for (j = 1; j <= mfit; j++) /* Initialize (symmetric) alpha), beta. */
732 for (k = 1; k <= j; k++)
739 for (i = 1; i <= ndata; i++) /* Summation loop over all data. */
741 (*funcs)(x[i], a, &ymod, dyda);
742 sig2i = 1.0/(sig[i]*sig[i]);
744 for (j = 0, l = 1; l <= ma; l++)
749 for (j++, k = 0, m = 1; m <= l; m++)
753 alpha[j][++k] += wt*dyda[m];
759 *chisq += dy*dy*sig2i; /* And find Chi^2. */
761 for (j = 2; j <= mfit; j++) /* Fill in the symmetric side. */
763 for (k = 1; k < j; k++)
765 alpha[k][j] = alpha[j][k];
768 free_vector(dyda, 1);