132 {
133 double tolerance, result, chiSqr, sigLevel;
134 int32_t nEvalMax = 5000, nPassMax = 100;
135 double guess[3];
136 double a[3], da[3];
137 double alo[3], ahi[3];
138 long n_dimen = 3, guessGiven, startGiven;
140 SCANNED_ARG *s_arg;
141 long i_arg, clue, fullOutput;
142 int64_t i;
143 char *input, *output, *xName, *yName, *syName;
144 long xIndex, yIndex, fitIndex, residualIndex, retval;
145 double *fitData, *residualData, rmsResidual;
146 unsigned long guessFlags, pipeFlags, dummyFlags, majorOrderFlag;
147 double constantStart, factorStart, rateStart;
148 short disable[3] = {0, 0, 0};
149 short autoOffset = 0;
150 short columnMajorOrder = -1;
151
153 argc =
scanargs(&s_arg, argc, argv);
154 if (argc < 2 || argc > (2 + N_OPTIONS))
156
157 input = output = NULL;
158 tolerance = 1e-6;
159 verbosity = fullOutput = guessGiven = startGiven = 0;
160 clue = -1;
161 xName = yName = syName = NULL;
162 pipeFlags = guessFlags = 0;
163 constantStart = factorStart = rateStart = 0;
164
165 for (i_arg = 1; i_arg < argc; i_arg++) {
166 if (s_arg[i_arg].arg_type == OPTION) {
167 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
168 case SET_MAJOR_ORDER:
169 majorOrderFlag = 0;
170 s_arg[i_arg].n_items--;
171 if (s_arg[i_arg].n_items > 0 && (!
scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
"row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
"column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
172 SDDS_Bomb(
"invalid -majorOrder syntax/values");
173 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
174 columnMajorOrder = 1;
175 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
176 columnMajorOrder = 0;
177 break;
178 case SET_AUTOOFFSET:
179 autoOffset = 1;
180 break;
181 case SET_TOLERANCE:
182 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%lf", &tolerance) != 1)
183 SDDS_Bomb(
"incorrect -tolerance syntax");
184 break;
185 case SET_VERBOSITY:
186 if (s_arg[i_arg].n_items != 2 || sscanf(s_arg[i_arg].list[1], "%ld", &verbosity) != 1)
187 SDDS_Bomb(
"incorrect -verbosity syntax");
188 break;
189 case SET_CLUE:
190 if (s_arg[i_arg].n_items != 2 || (clue =
match_string(s_arg[i_arg].list[1], clue_name, N_CLUE_TYPES, 0)) < 0)
192 break;
193 case SET_GUESS:
194 if (startGiven)
195 SDDS_Bomb(
"can't have -startValues and -guess at once");
196 if (s_arg[i_arg].n_items != 4 || sscanf(s_arg[i_arg].list[1], "%lf", guess + 0) != 1 || sscanf(s_arg[i_arg].list[2], "%lf", guess + 1) != 1 || sscanf(s_arg[i_arg].list[3], "%lf", guess + 2) != 1)
198 guessGiven = 1;
199 break;
200 case SET_STARTVALUES:
201 if (s_arg[i_arg].n_items < 2)
202 SDDS_Bomb(
"incorrect -startValues syntax");
203 if (guessGiven)
204 SDDS_Bomb(
"can't have -startValues and -guess at once");
205 s_arg[i_arg].n_items -= 1;
206 dummyFlags = guessFlags;
207 if (!
scanItemList(&guessFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
"constant",
SDDS_DOUBLE, &constantStart, 1, START_CONSTANT_GIVEN,
"factor",
SDDS_DOUBLE, &factorStart, 1, START_FACTOR_GIVEN,
"rate",
SDDS_DOUBLE, &rateStart, 1, START_RATE_GIVEN, NULL))
209 if ((dummyFlags >> 3) & (guessFlags))
210 SDDS_Bomb(
"can't have -fixValue and -startValue for the same item");
211 guessFlags |= dummyFlags;
212 startGiven = 1;
213 break;
214 case SET_FIXVALUE:
215 if (s_arg[i_arg].n_items < 2)
217 s_arg[i_arg].n_items -= 1;
218 dummyFlags = guessFlags;
219 if (!
scanItemList(&guessFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
"constant",
SDDS_DOUBLE, &constantStart, 1, FIX_CONSTANT_GIVEN,
"factor",
SDDS_DOUBLE, &factorStart, 1, FIX_FACTOR_GIVEN,
"rate",
SDDS_DOUBLE, &rateStart, 1, FIX_RATE_GIVEN, NULL))
221 if ((dummyFlags) & (guessFlags >> 3))
222 SDDS_Bomb(
"can't have -fixValue and -startValue for the same item");
223 guessFlags |= dummyFlags;
224 break;
225 case SET_COLUMNS:
226 if (s_arg[i_arg].n_items != 3 && s_arg[i_arg].n_items != 4)
228 xName = s_arg[i_arg].list[1];
229 yName = s_arg[i_arg].list[2];
230 s_arg[i_arg].n_items -= 3;
231 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 3, &s_arg[i_arg].n_items, 0,
"ysigma",
SDDS_STRING, &syName, 1, 0, NULL))
233 break;
234 case SET_FULLOUTPUT:
235 fullOutput = 1;
236 break;
237 case SET_PIPE:
238 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
240 break;
241 case SET_LIMITS:
242 if (s_arg[i_arg].n_items < 2)
244 s_arg[i_arg].n_items -= 1;
245 if (!
scanItemList(&dummyFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
"evaluations",
SDDS_LONG, &nEvalMax, 1, 0,
"passes",
SDDS_LONG, &nPassMax, 1, 0, NULL) || nEvalMax <= 0 || nPassMax <= 0)
247 break;
248 default:
249 fprintf(stderr, "error: unknown/ambiguous option: %s\n", s_arg[i_arg].list[0]);
250 exit(EXIT_FAILURE);
251 break;
252 }
253 } else {
254 if (input == NULL)
255 input = s_arg[i_arg].list[0];
256 else if (output == NULL)
257 output = s_arg[i_arg].list[0];
258 else
260 }
261 }
262
264
265 for (i = 0; i < 3; i++) {
266 if ((guessFlags >> 3) & (1 << i)) {
267 disable[i] = 1;
268 }
269 }
270
271 if (!xName || !yName)
272 SDDS_Bomb(
"-columns option must be given");
273
277
278 setupOutputFile(&OutputTable, &xIndex, &yIndex, &fitIndex, &residualIndex, output, fullOutput, &InputTable, xName, yName, columnMajorOrder);
279
281 fitData = residualData = NULL;
282 xData = yData = syData = NULL;
288 continue;
289
290 if (xData[0] > xData[nData - 1])
291 fputs("warning: data reverse-ordered", stderr);
292
295 for (i = 0; i < nData; i++)
296 xData[i] -= xMin;
297
300
301 if (!guessGiven) {
302 if (clue == CLUE_GROWS) {
303 a[0] = 0.9 * yData[0];
304 a[1] = yData[nData - 1] - yData[0];
305 a[2] = 1 / (xData[nData - 1] - xData[0]);
306 alo[2] = 0;
307 if (a[1] > 0)
308 alo[1] = 0;
309 else
310 ahi[1] = 0;
311 } else if (clue == CLUE_DECAYS) {
312 a[0] = 0.9 * yData[nData - 1];
313 a[1] = yData[0] - yData[nData - 1];
314 a[2] = 0;
315 ahi[2] = 0;
316 if (a[1] > 0)
317 alo[1] = 0;
318 else
319 ahi[1] = 0;
320 } else {
321 a[0] = yMin * 0.9;
322 a[1] = yMax - yMin;
323 a[2] = 0;
324 }
325 } else {
326 a[0] = guess[0];
327 a[1] = guess[1];
328 a[2] = guess[2];
329 }
330
331 if (guessFlags & (START_CONSTANT_GIVEN + FIX_CONSTANT_GIVEN))
332 a[0] = constantStart;
333 if (guessFlags & (START_FACTOR_GIVEN + FIX_FACTOR_GIVEN))
334 a[1] = factorStart;
335 if (guessFlags & (START_RATE_GIVEN + FIX_RATE_GIVEN))
336 a[2] = rateStart;
337
338 da[0] = da[1] = fabs(a[1] - a[0]) / 20.0;
339 da[2] = 0.1 / (xData[nData - 1] - xData[0]);
340 if (verbosity > 3)
341 fprintf(stderr, "starting guess: %e, %e, %e\n", a[0], a[1], a[2]);
342
343 simplexMin(&result, a, da, alo, ahi, disable, n_dimen, -DBL_MAX, tolerance, fitFunction, (verbosity > 0 ? report : NULL), nEvalMax, nPassMax, 12, 3, 1.0, 0);
344
345 da[0] = a[0] / 10;
346 da[1] = a[1] / 10;
347 da[2] = a[2] / 10;
348 simplexMin(&result, a, da, alo, ahi, disable, n_dimen, -DBL_MAX, tolerance, fitFunction, (verbosity > 0 ? report : NULL), nEvalMax, nPassMax, 12, 3, 1.0, 0);
349
350 if (!autoOffset) {
351
352 a[1] *= exp(-a[2] * xMin);
353 for (i = 0; i < nData; i++)
354 xData[i] += xMin;
355 }
356
357 fitData =
trealloc(fitData,
sizeof(*fitData) * nData);
358 residualData =
trealloc(residualData,
sizeof(*residualData) * nData);
359 for (i = result = 0; i < nData; i++) {
360 fitData[i] = a[0] + a[1] * exp(a[2] * xData[i]);
361 residualData[i] = yData[i] - fitData[i];
362 result += sqr(residualData[i]);
363 }
364 rmsResidual = sqrt(result / nData);
365 if (syData) {
366 for (i = chiSqr = 0; i < nData; i++)
367 chiSqr += sqr(residualData[i] / syData[i]);
368 } else {
369 double sy2;
370 sy2 = result / (nData - 3);
371 for (i = chiSqr = 0; i < nData; i++)
372 chiSqr += sqr(residualData[i]) / sy2;
373 }
375 if (verbosity > 1) {
376 fprintf(stderr, "RMS deviation: %.15e\n", rmsResidual);
377 fprintf(stderr, "(RMS deviation)/(largest value): %.15e\n", rmsResidual / MAX(fabs(yMin), fabs(yMax)));
378 if (syData)
379 fprintf(stderr, "Significance level: %.5e\n", sigLevel);
380 }
381 if (verbosity > 0) {
382 fprintf(stderr, "coefficients of fit to the form y = a0 + a1*exp(a2*x), a = \n");
383 for (i = 0; i < 3; i++)
384 fprintf(stderr, "%.8e ", a[i]);
385 fprintf(stderr, "\n");
386 }
387
390 !
SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, xData, nData, xIndex) ||
391 !
SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, fitData, nData, fitIndex) ||
393 "expfitConstant", a[0],
394 "expfitFactor", a[1],
395 "expfitRate", a[2],
396 "expfitRmsResidual", rmsResidual,
397 "expfitSigLevel", sigLevel, NULL) ||
398 (fullOutput && (!
SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, yData, nData, yIndex) ||
399 !
SDDS_SetColumn(&OutputTable, SDDS_SET_BY_INDEX, residualData, nData, residualIndex))) ||
402 if (xData)
403 free(xData);
404 if (yData)
405 free(yData);
406 if (syData)
407 free(syData);
408 if (fitData)
409 free(fitData);
410 if (residualData)
411 free(residualData);
412 }
415 exit(EXIT_FAILURE);
416 }
417 return EXIT_SUCCESS;
418}
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
void fill_double_array(double *array, long n, double value)
Fills a double array with the specified value.
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
double ChiSqrSigLevel(double ChiSquared0, long nu)
Computes the probability that a chi-squared variable exceeds a given value.
long simplexMin(double *yReturn, double *xGuess, double *dxGuess, double *xLowerLimit, double *xUpperLimit, short *disable, long dimensions, double target, double tolerance, double(*func)(double *x, long *invalid), void(*report)(double ymin, double *xmin, long pass, long evals, long dims), long maxEvaluations, long maxPasses, long maxDivisions, double divisorFactor, double passRangeFactor, unsigned long flags)
Top-level convenience function for simplex-based minimization.