3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
43 #include "gromacs/fileio/futil.h"
47 #include "gromacs/fileio/gmxfio.h"
50 #include "gmx_fatal.h"
52 /* find an entry; return index, or -1 if not found */
53 static int search_einp(int ninp, const t_inpfile *inp, const char *name);
56 t_inpfile *read_inpfile(const char *fn, int *ninp,
60 char buf[STRLEN], lbuf[STRLEN], rbuf[STRLEN], warn_buf[STRLEN];
62 t_inpfile *inp = NULL;
64 /* setting cppopts from command-line options would be cooler */
65 gmx_bool allow_override = FALSE;
70 fprintf(debug, "Reading MDP file %s\n", fn);
78 ptr = fgets2(buf, STRLEN-1, in);
80 set_warning_line(wi, fn, lc);
84 if ((cptr = strchr(buf, COMMENTSIGN)) != NULL)
91 for (j = 0; (buf[j] != '=') && (buf[j] != '\0'); j++)
101 fprintf(debug, "No = on line %d in file %s, ignored\n", lc, fn);
107 for (i = 0; (i < j); i++)
117 fprintf(debug, "Empty left hand side on line %d in file %s, ignored\n", lc, fn);
122 for (i = j+1, k = 0; (buf[i] != '\0'); i++, k++)
132 fprintf(debug, "Empty right hand side on line %d in file %s, ignored\n", lc, fn);
137 /* Now finally something sensible */
140 /* first check whether we hit the 'multiple_entries' option */
141 if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptName], lbuf) == 0)
143 /* we now check whether to allow overrides from here or not */
144 if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptNo], rbuf) == 0)
146 allow_override = FALSE;
148 else if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptLast], rbuf) == 0)
150 allow_override = TRUE;
155 "Parameter \"%s\" should either be %s or %s\n",
157 eMultentOpt_names[eMultentOptNo],
158 eMultentOpt_names[eMultentOptLast]);
159 warning_error(wi, warn_buf);
164 /* it is a regular option; check for duplicates */
165 found_index = search_einp(nin, inp, lbuf);
167 if (found_index == -1)
171 inp[nin-1].inp_count = 1;
172 inp[nin-1].count = 0;
173 inp[nin-1].bObsolete = FALSE;
174 inp[nin-1].bSet = FALSE;
175 inp[nin-1].name = strdup(lbuf);
176 inp[nin-1].value = strdup(rbuf);
183 "Parameter \"%s\" doubly defined (and multiple assignments not allowed)\n",
185 warning_error(wi, warn_buf);
190 sfree(inp[found_index].value);
191 inp[found_index].value = strdup(rbuf);
193 "Overriding existing parameter \"%s\" with value \"%s\"\n",
195 warning_note(wi, warn_buf);
210 fprintf(debug, "Done reading MDP file, there were %d entries in there\n",
222 static int inp_comp(const void *a, const void *b)
224 return ((t_inpfile *)a)->count - ((t_inpfile *)b)->count;
227 static void sort_inp(int ninp, t_inpfile inp[])
232 for (i = 0; (i < ninp); i++)
234 mm = max(mm, inp[i].count);
236 for (i = 0; (i < ninp); i++)
238 if (inp[i].count == 0)
243 qsort(inp, ninp, (size_t)sizeof(inp[0]), inp_comp);
246 void write_inpfile(const char *fn, int ninp, t_inpfile inp[], gmx_bool bHaltOnUnknown,
251 char warn_buf[STRLEN];
254 out = gmx_fio_fopen(fn, "w");
255 nice_header(out, fn);
256 for (i = 0; (i < ninp); i++)
260 if (inp[i].name[0] == ';' || (strlen(inp[i].name) > 2 && inp[i].name[1] == ';'))
262 fprintf(out, "%-24s\n", inp[i].name);
266 fprintf(out, "%-24s = %s\n", inp[i].name, inp[i].value ? inp[i].value : "");
269 else if (!inp[i].bObsolete)
271 sprintf(warn_buf, "Unknown left-hand '%s' in parameter file\n",
275 warning_error(wi, warn_buf);
279 warning(wi, warn_buf);
285 check_warning_error(wi, FARGS);
288 void replace_inp_entry(int ninp, t_inpfile *inp, const char *old_entry, const char *new_entry)
292 for (i = 0; (i < ninp); i++)
294 if (gmx_strcasecmp_min(old_entry, inp[i].name) == 0)
298 fprintf(stderr, "Replacing old mdp entry '%s' by '%s'\n",
299 inp[i].name, new_entry);
301 inp[i].name = strdup(new_entry);
305 fprintf(stderr, "Ignoring obsolete mdp entry '%s'\n",
307 inp[i].bObsolete = TRUE;
313 static int search_einp(int ninp, const t_inpfile *inp, const char *name)
321 for (i = 0; i < ninp; i++)
323 if (gmx_strcasecmp_min(name, inp[i].name) == 0)
331 static int get_einp(int *ninp, t_inpfile **inp, const char *name)
334 int notfound = FALSE;
335 char warn_buf[STRLEN];
339 for(i=0; (i<(*ninp)); i++)
340 if (gmx_strcasecmp_min(name,(*inp)[i].name) == 0)
342 if (i == (*ninp)) {*/
343 i = search_einp(*ninp, *inp, name);
348 srenew(*inp, (*ninp));
349 (*inp)[i].name = strdup(name);
350 (*inp)[i].bSet = TRUE;
352 (*inp)[i].count = (*inp)[0].inp_count++;
353 (*inp)[i].bSet = TRUE;
356 fprintf(debug, "Inp %d = %s\n", (*inp)[i].count, (*inp)[i].name);
359 /*if (i == (*ninp)-1)*/
370 int get_eint(int *ninp, t_inpfile **inp, const char *name, int def,
373 char buf[32], *ptr, warn_buf[STRLEN];
377 ii = get_einp(ninp, inp, name);
381 sprintf(buf, "%d", def);
382 (*inp)[(*ninp)-1].value = strdup(buf);
388 ret = strtol((*inp)[ii].value, &ptr, 10);
389 if (ptr == (*inp)[ii].value)
391 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not an integer value\n", (*inp)[ii].value, (*inp)[ii].name);
392 warning_error(wi, warn_buf);
399 gmx_int64_t get_eint64(int *ninp, t_inpfile **inp,
400 const char *name, gmx_int64_t def,
403 char buf[32], *ptr, warn_buf[STRLEN];
407 ii = get_einp(ninp, inp, name);
411 sprintf(buf, "%"GMX_PRId64, def);
412 (*inp)[(*ninp)-1].value = strdup(buf);
418 ret = str_to_int64_t((*inp)[ii].value, &ptr);
419 if (ptr == (*inp)[ii].value)
421 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not an integer value\n", (*inp)[ii].value, (*inp)[ii].name);
422 warning_error(wi, warn_buf);
429 double get_ereal(int *ninp, t_inpfile **inp, const char *name, double def,
432 char buf[32], *ptr, warn_buf[STRLEN];
436 ii = get_einp(ninp, inp, name);
440 sprintf(buf, "%g", def);
441 (*inp)[(*ninp)-1].value = strdup(buf);
447 ret = strtod((*inp)[ii].value, &ptr);
448 if (ptr == (*inp)[ii].value)
450 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not a real value\n", (*inp)[ii].value, (*inp)[ii].name);
451 warning_error(wi, warn_buf);
458 const char *get_estr(int *ninp, t_inpfile **inp, const char *name, const char *def)
463 ii = get_einp(ninp, inp, name);
469 sprintf(buf, "%s", def);
470 (*inp)[(*ninp)-1].value = strdup(buf);
474 (*inp)[(*ninp)-1].value = NULL;
481 return (*inp)[ii].value;
485 int get_eeenum(int *ninp, t_inpfile **inp, const char *name, const char **defs,
492 ii = get_einp(ninp, inp, name);
496 (*inp)[(*ninp)-1].value = strdup(defs[0]);
501 for (i = 0; (defs[i] != NULL); i++)
503 if (gmx_strcasecmp_min(defs[i], (*inp)[ii].value) == 0)
511 n += sprintf(buf, "Invalid enum '%s' for variable %s, using '%s'\n",
512 (*inp)[ii].value, name, defs[0]);
513 n += sprintf(buf+n, "Next time use one of:");
517 n += sprintf(buf+n, " '%s'", defs[j]);
522 warning_error(wi, buf);
526 fprintf(stderr, "%s\n", buf);
529 (*inp)[ii].value = strdup(defs[0]);
537 int get_eenum(int *ninp, t_inpfile **inp, const char *name, const char **defs)
541 return get_eeenum(ninp, inp, name, defs, NULL);