Skip to content

Commit

Permalink
Add wrapper function curve_tyd, for use in GROMACS.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwuttke committed Nov 24, 2015
1 parent ab2b1c2 commit 137e8f0
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ man/*.html
# executables

demo/*1
demo/*2
test/test_qr
40 changes: 24 additions & 16 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
Changes in lmfit-5.3, released 24nov15:
lmfit-6.0, released 24nov15:

- API changes:
- a few more 'const' qualifiers in lmmin and lmcurve.
- code cleanup:
- further unify code layout
- use temporary variable for readability in lmcurve (suggested by T. Klauser)

lmfit-5.3, released 24nov15:

- autotools:
- set default optimization -O3
Expand All @@ -14,14 +22,14 @@ Changes in lmfit-5.3, released 24nov15:
- testing:
- new unit test directory test/, for development purposes only

Changes in lmfit-5.2, released 23oct14:
lmfit-5.2, released 23oct14:

- bug fix:
- correct man page (5.0 API change)
- acceleration(?):
- revert to just one malloc for all workspaces, now based on a char array

Changes in lmfit-5.1, released 29aug13:
lmfit-5.1, released 29aug13:

- bug fix:
- FILE** initialization from &stdout was not standard compliant
Expand All @@ -30,7 +38,7 @@ Changes in lmfit-5.1, released 29aug13:
- code cleanup:
- better wrapper for "extern C" declaration

Changes in lmfit-5.0, released 9aug13:
lmfit-5.0, released 9aug13:

- API changes:
- removed callback function for printout
Expand All @@ -48,7 +56,7 @@ Changes in lmfit-5.0, released 9aug13:
- otherwise testsuite started as new project fittest
- packaging: configure now supports pkg-config

Changes in lmfit-4.0, released 16jul13:
lmfit-4.0, released 16jul13:

- binary library renamed: lmmin -> lmfit
- API changes:
Expand All @@ -71,13 +79,13 @@ Changes in lmfit-4.0, released 16jul13:
- new entry page (dokuwiki) instead of man page
- man page split and largely rewritten: lmcurve(3), lmmin(3)

Changes in lmfit-3.5, released 3feb13:
lmfit-3.5, released 3feb13:

- changed overall license to FreeBSD to facilitate inclusion in Linux
distributions (request by M. Hroncok)
- directory doc renamed man

Changes in lmfit-3.4, released 21jan13:
lmfit-3.4, released 21jan13:

- only one malloc call to allocate all arrays (proposal by F. Polchow)
- liblmmin_la_LDFLAGS += -lm (required by Ubuntu, report by J. M. Agosta)
Expand All @@ -86,25 +94,25 @@ Changes in lmfit-3.4, released 21jan13:
- long message for degenerate case now suggests increasing epsilon
(suggestion by Nghia Ho)

Changes in lmfit-3.3, released 12sep12:
lmfit-3.3, released 12sep12:

- moved "int" declarations to top of lm_printout_std() and lmmin()
(required by Visual Studio 2005: bug report M. Beek)
- corrected typo 0.55 -> 0.5 (bug reports M. Kardas and D. Pescia)
- man pages now produced by make; finally got rid of dmake
- untabified code (hint by W. Pimenta)

Changes in lmfit-3.2, released 28mar10 (preview 27mar10):
lmfit-3.2, released 28mar10 (preview 27mar10):

- shared library is given correct version number (set in configure.ac)
- man page lmfit(3) is copied by dmake to lmmin(3) and lmcurve_fit(3)

Changes in lmfit-3.1, released 12mar10:
lmfit-3.1, released 12mar10:

- debian directory not part of tgz distribution
- temporarily removed lmmin.3 (was symlink -> lmfit.3)

Changes in lmfit-3.0, released 2mar10 (preview 27feb10):
lmfit-3.0, released 2mar10 (preview 27feb10):

- for parameter close to zero, change step width for calculation of jabobian
- outer iteration counter starting at zero (C style)
Expand All @@ -124,27 +132,27 @@ Changes in lmfit-3.0, released 2mar10 (preview 27feb10):
- provide vector minimization demos
- test for fnorm<=LM_DWARF, renumbered info values 0, 10, 11

Changes in lmfit-2.6, released 29sep09:
lmfit-2.6, released 29sep09:

- nfev incrementation even if printout=0 (bug report C. Tanner)
- correct t->tvec, y->yvec in man page (bug report C. Tanner)

Changes in lmfit-2.5, released 20sep09:
lmfit-2.5, released 20sep09:

- malloc==NULL ? (suggestion R. Strickland)
- allow printout=NULL (suggestion J. Lodewyck)
- improve infmsg (correspondence B. McHoull)
- eliminate some static variables (p25, p5, p75, ..)
- eliminate function data types; they obscure more than they help

Changes in lmfit-2.4, released 27jul08:
lmfit-2.4, released 27jul08:

- clearer file headers
- clearer licence (everything has ever been in the public domain)
- machine-dependent arithmetic constants from float.h (suggestion M. Bydder)
- minor improvements of comments

Changes in lmfit-2.3, released 5apr08:
lmfit-2.3, released 5apr08:

- fixed bug sf#1817631: replaced confusing variable names in lmmin.c:lm_qrsolv()
- accepted feature request 1814970: inclusion protection in headers
Expand All @@ -155,7 +163,7 @@ Changes in lmfit-2.3, released 5apr08:
- reverted comments in lmmin.c to pure C style
- improved documentation of interfaces

Changes up to lmfit-2.2, released 20sep07:
lmfit-2.2, released 20sep07:

- no change log has been maintained,
there are just the sourceforge release notes
2 changes: 1 addition & 1 deletion COPYING
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The package lmfit is distributed under the FreeBSD License:
--
Copyright (c) 1980-1999 University of Chicago,
as operator of Argonne National Laboratory
Copyright (c) 2004-2016 Joachim Wuttke, Forschungszentrum Juelich GmbH
Copyright (c) 2004-2015 Joachim Wuttke, Forschungszentrum Juelich GmbH

All rights reserved.

Expand Down
3 changes: 2 additions & 1 deletion demo/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AM_CFLAGS = -I$(srcdir)/../lib
noinst_PROGRAMS = curve1 nonlin1 surface1
noinst_PROGRAMS = curve1 curve2 nonlin1 surface1
curve1_SOURCES = curve1.c
curve2_SOURCES = curve2.c
nonlin1_SOURCES = nonlin1.c
surface1_SOURCES = surface1.c
AM_LDFLAGS = -lm -L../lib -llmfit
17 changes: 14 additions & 3 deletions demo/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
noinst_PROGRAMS = curve1$(EXEEXT) nonlin1$(EXEEXT) surface1$(EXEEXT)
noinst_PROGRAMS = curve1$(EXEEXT) curve2$(EXEEXT) nonlin1$(EXEEXT) \
surface1$(EXEEXT)
subdir = demo
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
Expand All @@ -110,6 +111,9 @@ AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
am__v_lt_0 = --silent
am__v_lt_1 =
am_curve2_OBJECTS = curve2.$(OBJEXT)
curve2_OBJECTS = $(am_curve2_OBJECTS)
curve2_LDADD = $(LDADD)
am_nonlin1_OBJECTS = nonlin1.$(OBJEXT)
nonlin1_OBJECTS = $(am_nonlin1_OBJECTS)
nonlin1_LDADD = $(LDADD)
Expand Down Expand Up @@ -150,8 +154,9 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
am__v_CCLD_0 = @echo " CCLD " $@;
am__v_CCLD_1 =
SOURCES = $(curve1_SOURCES) $(nonlin1_SOURCES) $(surface1_SOURCES)
DIST_SOURCES = $(curve1_SOURCES) $(nonlin1_SOURCES) \
SOURCES = $(curve1_SOURCES) $(curve2_SOURCES) $(nonlin1_SOURCES) \
$(surface1_SOURCES)
DIST_SOURCES = $(curve1_SOURCES) $(curve2_SOURCES) $(nonlin1_SOURCES) \
$(surface1_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
Expand Down Expand Up @@ -298,6 +303,7 @@ top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
curve1_SOURCES = curve1.c
curve2_SOURCES = curve2.c
nonlin1_SOURCES = nonlin1.c
surface1_SOURCES = surface1.c
AM_LDFLAGS = -lm -L../lib -llmfit
Expand Down Expand Up @@ -348,6 +354,10 @@ curve1$(EXEEXT): $(curve1_OBJECTS) $(curve1_DEPENDENCIES) $(EXTRA_curve1_DEPENDE
@rm -f curve1$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(curve1_OBJECTS) $(curve1_LDADD) $(LIBS)

curve2$(EXEEXT): $(curve2_OBJECTS) $(curve2_DEPENDENCIES) $(EXTRA_curve2_DEPENDENCIES)
@rm -f curve2$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(curve2_OBJECTS) $(curve2_LDADD) $(LIBS)

nonlin1$(EXEEXT): $(nonlin1_OBJECTS) $(nonlin1_DEPENDENCIES) $(EXTRA_nonlin1_DEPENDENCIES)
@rm -f nonlin1$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(nonlin1_OBJECTS) $(nonlin1_LDADD) $(LIBS)
Expand All @@ -363,6 +373,7 @@ distclean-compile:
-rm -f *.tab.c

@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/curve1.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/curve2.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nonlin1.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/surface1.Po@am__quote@

Expand Down
63 changes: 63 additions & 0 deletions demo/curve2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Library: lmfit (Levenberg-Marquardt least squares fitting)
*
* File: demo/curve1.c
*
* Contents: Example for fitting data with error bars:
* fit a data set (x,y+-dy) by a curve f(x;p).
*
* Author: Joachim Wuttke <[email protected]> 2004-2013
*
* Licence: see ../COPYING (FreeBSD)
*
* Homepage: apps.jcns.fz-juelich.de/lmfit
*/

#include "lmcurve.h"
#include <stdio.h>

/* model function: a parabola */

double f( double t, const double *p )
{
return p[0] + p[1]*t + p[2]*t*t;
}

int main()
{
int n = 3; /* number of parameters in model function f */
double par[3] = { 100, 0, -10 }; /* really bad starting value */

/* data points: a slightly distorted standard parabola */
int m = 9;
int i;
double t[9] = { -4., -3., -2., -1., 0., 1., 2., 3., 4. };
double y[9] = { 16.6, 9.9, 4.4, 1.1, 0., 1.1, 4.2, 9.3, 16.4 };
double dy[9] = { 4, 3, 2, 1, 2, 3, 4, 5, 6 };

lm_control_struct control = lm_control_double;
lm_status_struct status;
control.verbosity = 1;

printf( "Fitting ...\n" );
/* now the call to lmfit */
lmcurve_tyd( n, par, m, t, y, dy, f, &control, &status );

printf( "Results:\n" );
printf( "status after %d function evaluations:\n %s\n",
status.nfev, lm_infmsg[status.outcome] );

printf("obtained parameters:\n");
for ( i = 0; i < n; ++i)
printf(" par[%i] = %12g\n", i, par[i]);
printf("obtained norm:\n %12g\n", status.fnorm );

printf("fitting data as follows:\n");
for ( i = 0; i < m; ++i)
printf(
" t[%1d]=%2g y=%5.1f+-%4.1f fit=%8.5f residue=%8.4f weighed=%8.4f\n",
i, t[i], y[i], dy[i], f(t[i],par), y[i] - f(t[i],par),
(y[i] - f(t[i],par))/dy[i] );

return 0;
}
54 changes: 47 additions & 7 deletions lib/lmcurve.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,67 @@

#include "lmmin.h"

/******************************************************************************/
/* lmcurve */
/******************************************************************************/

typedef struct {
const double* t;
const double* y;
double (*f)(const double t, const double* par);
} lmcurve_data_struct;

void lmcurve_evaluate(const double* par, const int m_dat, const void* data,
double* fvec, int* info)
void lmcurve_evaluate(
const double* par, const int m_dat, const void* data, double* fvec,
int* info)
{
lmcurve_data_struct* D = (lmcurve_data_struct*)data;
int i;
for (i = 0; i < m_dat; i++)
fvec[i] = D->y[i] - D->f(D->t[i], par);
}

void lmcurve(const int n_par, double* par, const int m_dat, const double* t,
const double* y, double (*f)(const double t, const double* par),
const lm_control_struct* control, lm_status_struct* status)
void lmcurve(
const int n_par, double* par, const int m_dat,
const double* t, const double* y,
double (*f)(const double t, const double* par),
const lm_control_struct* control, lm_status_struct* status)
{
lmcurve_data_struct data = { t, y, f };

lmmin(n_par, par, m_dat, (const void*)&data, lmcurve_evaluate, control,
status);
lmmin(n_par, par, m_dat, (const void*)&data, lmcurve_evaluate,
control, status);
}

/******************************************************************************/
/* lmcurve_tyd */
/******************************************************************************/

typedef struct {
const double* t;
const double* y;
const double* dy;
double (*f)(const double t, const double* par);
} lmcurve_tyd_data_struct;

void lmcurve_tyd_evaluate(
const double* par, const int m_dat, const void* data, double* fvec,
int* info)
{
lmcurve_tyd_data_struct* D = (lmcurve_tyd_data_struct*)data;
int i;
for (i = 0; i < m_dat; i++)
fvec[i] = ( D->y[i] - D->f(D->t[i], par) ) / D->dy[i];
}

void lmcurve_tyd(
const int n_par, double* par, const int m_dat,
const double* t, const double* y, const double* dy,
double (*f)(const double t, const double* par),
const lm_control_struct* control, lm_status_struct* status)
{
lmcurve_tyd_data_struct data = { t, y, dy, f };

lmmin(n_par, par, m_dat, (const void*)&data, lmcurve_tyd_evaluate,
control, status);
}
14 changes: 11 additions & 3 deletions lib/lmcurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@

__BEGIN_DECLS

void lmcurve(const int n_par, double* par, const int m_dat, const double* t,
const double* y, double (*f)(double t, const double* par),
const lm_control_struct* control, lm_status_struct* status);
void lmcurve(
const int n_par, double* par, const int m_dat,
const double* t, const double* y,
double (*f)(double t, const double* par),
const lm_control_struct* control, lm_status_struct* status);

void lmcurve_tyd(
const int n_par, double* par, const int m_dat,
const double* t, const double* y, const double* dy,
double (*f)(double t, const double* par),
const lm_control_struct* control, lm_status_struct* status);

__END_DECLS
#endif /* LMCURVE_H */
Loading

0 comments on commit 137e8f0

Please sign in to comment.