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, 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);
74 static int *ivector(int nl, int nh)
78 v = (int *)malloc((unsigned) (nh-nl+1)*sizeof(int));
81 nrerror("allocation failure in ivector()", TRUE);
87 static real **matrix1(int nrl, int nrh, int ncl, int nch)
92 m = (real **) malloc((unsigned) (nrh-nrl+1)*sizeof(real*));
95 nrerror("allocation failure 1 in matrix1()", TRUE);
99 for (i = nrl; i <= nrh; i++)
101 m[i] = (real *) malloc((unsigned) (nch-ncl+1)*sizeof(real));
104 nrerror("allocation failure 2 in matrix1()", TRUE);
111 static double **dmatrix(int nrl, int nrh, int ncl, int nch)
116 m = (double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
119 nrerror("allocation failure 1 in dmatrix()", TRUE);
123 for (i = nrl; i <= nrh; i++)
125 m[i] = (double *) malloc((unsigned) (nch-ncl+1)*sizeof(double));
128 nrerror("allocation failure 2 in dmatrix()", TRUE);
135 static int **imatrix1(int nrl, int nrh, int ncl, int nch)
139 m = (int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*));
142 nrerror("allocation failure 1 in imatrix1()", TRUE);
146 for (i = nrl; i <= nrh; i++)
148 m[i] = (int *)malloc((unsigned) (nch-ncl+1)*sizeof(int));
151 nrerror("allocation failure 2 in imatrix1()", TRUE);
160 static real **submatrix(real **a, int oldrl, int oldrh, int oldcl,
161 int newrl, int newcl)
166 m = (real **) malloc((unsigned) (oldrh-oldrl+1)*sizeof(real*));
169 nrerror("allocation failure in submatrix()", TRUE);
173 for (i = oldrl, j = newrl; i <= oldrh; i++, j++)
175 m[j] = a[i]+oldcl-newcl;
183 static void free_vector(real *v, int nl)
185 free((char*) (v+nl));
188 static void free_ivector(int *v, int nl)
190 free((char*) (v+nl));
193 static void free_dvector(int *v, int nl)
195 free((char*) (v+nl));
200 static void free_matrix(real **m, int nrl, int nrh, int ncl)
204 for (i = nrh; i >= nrl; i--)
206 free((char*) (m[i]+ncl));
208 free((char*) (m+nrl));
211 static real **convert_matrix(real *a, int nrl, int nrh, int ncl, int nch)
213 int i, j, nrow, ncol;
218 m = (real **) malloc((unsigned) (nrow)*sizeof(real*));
221 nrerror("allocation failure in convert_matrix()", TRUE);
224 for (i = 0, j = nrl; i <= nrow-1; i++, j++)
233 static void free_convert_matrix(real **b, int nrl)
235 free((char*) (b+nrl));
238 #define SWAP(a, b) {real temp = (a); (a) = (b); (b) = temp; }
240 static void dump_mat(int n, real **a)
244 for (i = 1; (i <= n); i++)
246 for (j = 1; (j <= n); j++)
248 fprintf(stderr, " %10.3f", a[i][j]);
250 fprintf(stderr, "\n");
254 gmx_bool gaussj(real **a, int n, real **b, int m)
256 int *indxc, *indxr, *ipiv;
257 int i, icol = 0, irow = 0, j, k, l, ll;
258 real big, dum, pivinv;
260 indxc = ivector(1, n);
261 indxr = ivector(1, n);
262 ipiv = ivector(1, n);
263 for (j = 1; j <= n; j++)
267 for (i = 1; i <= n; i++)
270 for (j = 1; j <= n; j++)
274 for (k = 1; k <= n; k++)
278 if (fabs(a[j][k]) >= big)
285 else if (ipiv[k] > 1)
287 nrerror("GAUSSJ: Singular Matrix-1", FALSE);
296 for (l = 1; l <= n; l++)
298 SWAP(a[irow][l], a[icol][l]);
300 for (l = 1; l <= m; l++)
302 SWAP(b[irow][l], b[icol][l]);
307 if (a[icol][icol] == 0.0)
309 fprintf(stderr, "irow = %d, icol = %d\n", irow, icol);
311 nrerror("GAUSSJ: Singular Matrix-2", FALSE);
314 pivinv = 1.0/a[icol][icol];
316 for (l = 1; l <= n; l++)
318 a[icol][l] *= pivinv;
320 for (l = 1; l <= m; l++)
322 b[icol][l] *= pivinv;
324 for (ll = 1; ll <= n; ll++)
330 for (l = 1; l <= n; l++)
332 a[ll][l] -= a[icol][l]*dum;
334 for (l = 1; l <= m; l++)
336 b[ll][l] -= b[icol][l]*dum;
341 for (l = n; l >= 1; l--)
343 if (indxr[l] != indxc[l])
345 for (k = 1; k <= n; k++)
347 SWAP(a[k][indxr[l]], a[k][indxc[l]]);
351 free_ivector(ipiv, 1);
352 free_ivector(indxr, 1);
353 free_ivector(indxc, 1);
361 static void covsrt(real **covar, int ma, int lista[], int mfit)
366 for (j = 1; j < ma; j++)
368 for (i = j+1; i <= ma; i++)
373 for (i = 1; i < mfit; i++)
375 for (j = i+1; j <= mfit; j++)
377 if (lista[j] > lista[i])
379 covar[lista[j]][lista[i]] = covar[i][j];
383 covar[lista[i]][lista[j]] = covar[i][j];
388 for (j = 1; j <= ma; j++)
390 covar[1][j] = covar[j][j];
393 covar[lista[1]][lista[1]] = swap;
394 for (j = 2; j <= mfit; j++)
396 covar[lista[j]][lista[j]] = covar[1][j];
398 for (j = 2; j <= ma; j++)
400 for (i = 1; i <= j-1; i++)
402 covar[i][j] = covar[j][i];
407 #define SWAP(a, b) {swap = (a); (a) = (b); (b) = swap; }
409 static void covsrt_new(real **covar, int ma, int ia[], int mfit)
410 /* Expand in storage the covariance matrix covar, so as to take
411 * into account parameters that are being held fixed. (For the
412 * latter, return zero covariances.)
417 for (i = mfit+1; i <= ma; i++)
419 for (j = 1; j <= i; j++)
421 covar[i][j] = covar[j][i] = 0.0;
425 for (j = ma; j >= 1; j--)
429 for (i = 1; i <= ma; i++)
431 SWAP(covar[i][k], covar[i][j]);
433 for (i = 1; i <= ma; i++)
435 SWAP(covar[k][i], covar[j][i]);
443 static void mrqcof(real x[], real y[], real sig[], int ndata, real a[],
444 int ma, int lista[], int mfit,
445 real **alpha, real beta[], real *chisq,
446 void (*funcs)(real, real *, real *, real *))
449 real ymod, wt, sig2i, dy, *dyda;
451 dyda = rvector(1, ma);
452 for (j = 1; j <= mfit; j++)
454 for (k = 1; k <= j; k++)
461 for (i = 1; i <= ndata; i++)
463 (*funcs)(x[i], a, &ymod, dyda);
464 sig2i = 1.0/(sig[i]*sig[i]);
466 for (j = 1; j <= mfit; j++)
468 wt = dyda[lista[j]]*sig2i;
469 for (k = 1; k <= j; k++)
471 alpha[j][k] += wt*dyda[lista[k]];
475 (*chisq) += dy*dy*sig2i;
477 for (j = 2; j <= mfit; j++)
479 for (k = 1; k <= j-1; k++)
481 alpha[k][j] = alpha[j][k];
484 free_vector(dyda, 1);
488 gmx_bool mrqmin(real x[], real y[], real sig[], int ndata, real a[],
489 int ma, int lista[], int mfit,
490 real **covar, real **alpha, real *chisq,
491 void (*funcs)(real, real *, real *, real *),
495 static real *da, *atry, **oneda, *beta, ochisq;
499 oneda = matrix1(1, mfit, 1, 1);
500 atry = rvector(1, ma);
502 beta = rvector(1, ma);
504 for (j = 1; j <= ma; j++)
507 for (k = 1; k <= mfit; k++)
520 nrerror("Bad LISTA permutation in MRQMIN-1", FALSE);
526 nrerror("Bad LISTA permutation in MRQMIN-2", FALSE);
530 mrqcof(x, y, sig, ndata, a, ma, lista, mfit, alpha, beta, chisq, funcs);
533 for (j = 1; j <= mfit; j++)
535 for (k = 1; k <= mfit; k++)
537 covar[j][k] = alpha[j][k];
539 covar[j][j] = alpha[j][j]*(1.0+(*alamda));
540 oneda[j][1] = beta[j];
542 if (!gaussj(covar, mfit, oneda, 1))
546 for (j = 1; j <= mfit; j++)
552 covsrt(covar, ma, lista, mfit);
553 free_vector(beta, 1);
555 free_vector(atry, 1);
556 free_matrix(oneda, 1, mfit, 1);
559 for (j = 1; j <= ma; j++)
563 for (j = 1; j <= mfit; j++)
565 atry[lista[j]] = a[lista[j]]+da[j];
567 mrqcof(x, y, sig, ndata, atry, ma, lista, mfit, covar, da, chisq, funcs);
572 for (j = 1; j <= mfit; j++)
574 for (k = 1; k <= mfit; k++)
576 alpha[j][k] = covar[j][k];
579 a[lista[j]] = atry[lista[j]];
591 gmx_bool mrqmin_new(real x[], real y[], real sig[], int ndata, real a[],
592 int ia[], int ma, real **covar, real **alpha, real *chisq,
593 void (*funcs)(real, real [], real *, real []),
595 /* Levenberg-Marquardt method, attempting to reduce the value Chi^2
596 * of a fit between a set of data points x[1..ndata], y[1..ndata]
597 * with individual standard deviations sig[1..ndata], and a nonlinear
598 * function dependent on ma coefficients a[1..ma]. The input array
599 * ia[1..ma] indicates by nonzero entries those components of a that
600 * should be fitted for, and by zero entries those components that
601 * should be held fixed at their input values. The program returns
602 * current best-fit values for the parameters a[1..ma], and
603 * Chi^2 = chisq. The arrays covar[1..ma][1..ma], alpha[1..ma][1..ma]
604 * are used as working space during most iterations. Supply a routine
605 * funcs(x,a,yfit,dyda,ma) that evaluates the fitting function yfit,
606 * and its derivatives dyda[1..ma] with respect to the fitting
607 * parameters a at x. On the first call provide an initial guess for
608 * the parameters a, and set alamda < 0 for initialization (which then
609 * sets alamda=.001). If a step succeeds chisq becomes smaller and
610 * alamda de-creases by a factor of 10. If a step fails alamda grows by
611 * a factor of 10. You must call this routine repeatedly until
612 * convergence is achieved. Then, make one final call with alamda=0,
613 * so that covar[1..ma][1..ma] returns the covariance matrix, and alpha
614 * the curvature matrix.
615 * (Parameters held fixed will return zero covariances.)
618 void covsrt(real **covar, int ma, int ia[], int mfit);
619 gmx_bool gaussj(real **a, int n, real **b, int m);
620 void mrqcof_new(real x[], real y[], real sig[], int ndata, real a[],
621 int ia[], int ma, real **alpha, real beta[], real *chisq,
622 void (*funcs)(real, real [], real *, real []));
625 static real ochisq, *atry, *beta, *da, **oneda;
627 if (*alamda < 0.0) /* Initialization. */
629 atry = rvector(1, ma);
630 beta = rvector(1, ma);
632 for (mfit = 0, j = 1; j <= ma; j++)
639 oneda = matrix1(1, mfit, 1, 1);
641 mrqcof_new(x, y, sig, ndata, a, ia, ma, alpha, beta, chisq, funcs);
643 for (j = 1; j <= ma; j++)
648 for (j = 1; j <= mfit; j++) /* Alter linearized fitting matrix, by augmenting. */
650 for (k = 1; k <= mfit; k++)
652 covar[j][k] = alpha[j][k]; /* diagonal elements. */
654 covar[j][j] = alpha[j][j]*(1.0+(*alamda));
655 oneda[j][1] = beta[j];
657 if (!gaussj(covar, mfit, oneda, 1)) /* Matrix solution. */
661 for (j = 1; j <= mfit; j++)
665 if (*alamda == 0.0) /* Once converged, evaluate covariance matrix. */
667 covsrt_new(covar, ma, ia, mfit);
668 free_matrix(oneda, 1, mfit, 1);
670 free_vector(beta, 1);
671 free_vector(atry, 1);
674 for (j = 0, l = 1; l <= ma; l++) /* Did the trial succeed? */
678 atry[l] = a[l]+da[++j];
681 mrqcof_new(x, y, sig, ndata, atry, ia, ma, covar, da, chisq, funcs);
684 /* Success, accept the new solution. */
687 for (j = 1; j <= mfit; j++)
689 for (k = 1; k <= mfit; k++)
691 alpha[j][k] = covar[j][k];
695 for (l = 1; l <= ma; l++)
700 else /* Failure, increase alamda and return. */
708 void mrqcof_new(real x[], real y[], real sig[], int ndata, real a[],
709 int ia[], int ma, real **alpha, real beta[], real *chisq,
710 void (*funcs)(real, real [], real *, real[]))
711 /* Used by mrqmin to evaluate the linearized fitting matrix alpha, and
712 * vector beta as in (15.5.8), and calculate Chi^2.
715 int i, j, k, l, m, mfit = 0;
716 real ymod, wt, sig2i, dy, *dyda;
718 dyda = rvector(1, ma);
719 for (j = 1; j <= ma; j++)
726 for (j = 1; j <= mfit; j++) /* Initialize (symmetric) alpha), beta. */
728 for (k = 1; k <= j; k++)
735 for (i = 1; i <= ndata; i++) /* Summation loop over all data. */
737 (*funcs)(x[i], a, &ymod, dyda);
738 sig2i = 1.0/(sig[i]*sig[i]);
740 for (j = 0, l = 1; l <= ma; l++)
745 for (j++, k = 0, m = 1; m <= l; m++)
749 alpha[j][++k] += wt*dyda[m];
755 *chisq += dy*dy*sig2i; /* And find Chi^2. */
757 for (j = 2; j <= mfit; j++) /* Fill in the symmetric side. */
759 for (k = 1; k < j; k++)
761 alpha[k][j] = alpha[j][k];
764 free_vector(dyda, 1);