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,2015,2016,2017,2018, 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.
46 #include "gromacs/fileio/warninp.h"
47 #include "gromacs/utility/binaryinformation.h"
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/keyvaluetreebuilder.h"
52 #include "gromacs/utility/niceheader.h"
53 #include "gromacs/utility/programcontext.h"
54 #include "gromacs/utility/qsort_threadsafe.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/stringutil.h"
57 #include "gromacs/utility/textreader.h"
58 #include "gromacs/utility/textwriter.h"
60 std::vector<t_inpfile>
61 read_inpfile(gmx::TextInputStream *stream, const char *fn,
64 std::vector<t_inpfile> inp;
68 fprintf(debug, "Reading MDP file %s\n", fn);
71 int indexOfLineReadFromFile = 0;
73 gmx::TextReader reader(stream);
74 reader.setTrimTrailingWhiteSpace(true);
75 reader.setTrimTrailingComment(true, ';');
76 while (reader.readLine(&line))
78 indexOfLineReadFromFile++;
79 set_warning_line(wi, fn, indexOfLineReadFromFile);
86 auto tokens = gmx::splitAndTrimDelimitedString(line, '=');
87 if (tokens.size() < 2)
89 // TODO this seems like it silently ignores the user accidentally deleting an equals sign...
92 fprintf(debug, "No = on line %d in file %s, ignored\n", indexOfLineReadFromFile, fn);
96 if (tokens.size() > 2)
98 // More than one equals symbol in the original line is
99 // valid if the RHS is a free string, and needed for
100 // "define = -DBOOLVAR -DVAR=VALUE".
102 // First, drop all the fields on the RHS of the first equals symbol.
104 // This find cannot return std::string::npos.
105 auto firstEqualsPos = line.find('=');
106 tokens.emplace_back(gmx::stripString(line.substr(firstEqualsPos + 1)));
108 if (tokens[0].empty())
110 // TODO ignoring such lines does not seem like good behaviour
113 fprintf(debug, "Empty left hand side on line %d in file %s, ignored\n", indexOfLineReadFromFile, fn);
117 if (tokens[1].empty())
119 // TODO ignoring such lines does not seem like good behaviour
122 fprintf(debug, "Empty right hand side on line %d in file %s, ignored\n", indexOfLineReadFromFile, fn);
127 /* Now finally something sensible; check for duplicates */
128 int found_index = search_einp(inp, tokens[0].c_str());
130 if (found_index == -1)
133 inp.emplace_back(0, 1, false, false, false,
134 tokens[0], tokens[1]);
138 auto message = gmx::formatString("Parameter \"%s\" doubly defined\n",
140 warning_error(wi, message.c_str());
143 /* This preserves the behaviour of the old code, which issues some
144 warnings after completing parsing. Regenerating regressiontest
145 warning files is not worth the effort. */
146 indexOfLineReadFromFile++;
147 set_warning_line(wi, fn, indexOfLineReadFromFile);
151 fprintf(debug, "Done reading MDP file, there were %zu entries in there\n",
158 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp)
160 gmx::KeyValueTreeBuilder builder;
161 auto root = builder.rootObject();
162 for (auto &local : inp)
164 root.addValue<std::string>(local.name_, !local.value_.empty() ? local.value_ : "");
166 return builder.build();
172 bool operator()(t_inpfile const &a, t_inpfile const &b)
174 return a.count_ < b.count_;
178 static void sort_inp(std::vector<t_inpfile> *inp)
180 std::vector<t_inpfile> &inpRef = *inp;
184 for (const auto &local : inpRef)
186 mm = std::max(mm, local.count_);
188 for (auto &local : inpRef)
190 if (local.count_ == 0)
195 std::sort(inpRef.begin(), inpRef.end(), inp_comp());
198 void write_inpfile(gmx::TextOutputStream *stream, const char *fn, std::vector<t_inpfile> *inp,
199 gmx_bool bHaltOnUnknown,
200 WriteMdpHeader writeHeader,
203 using gmx::formatString;
207 gmx::TextWriter writer(stream);
208 if (writeHeader == WriteMdpHeader::yes)
210 gmx::niceHeader(&writer, fn, ';');
212 gmx::BinaryInformationSettings settings;
213 settings.generatedByHeader(true);
214 settings.linePrefix(";\t");
215 gmx::printBinaryInformation(&writer, gmx::getProgramContext(), settings);
217 for (const auto &local : *inp)
219 if (local.bHandledAsKeyValueTree_)
222 else if (local.bSet_)
224 if (local.name_[0] == ';' || (local.name_.length() > 2 && local.name_[1] == ';'))
226 writer.writeLine(formatString("%-24s", local.name_.c_str()));
230 writer.writeLine(formatString("%-24s = %s", local.name_.c_str(), !local.value_.empty() ? local.value_.c_str() : ""));
233 else if (!local.bObsolete_)
235 auto message = formatString("Unknown left-hand '%s' in parameter file\n",
236 local.name_.c_str());
239 warning_error(wi, message.c_str());
243 warning(wi, message.c_str());
248 check_warning_error(wi, FARGS);
251 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char *old_entry, const char *new_entry)
253 for (auto &local : inp)
255 if (gmx_strcasecmp_min(old_entry, local.name_.c_str()) == 0)
259 fprintf(stderr, "Replacing old mdp entry '%s' by '%s'\n",
260 local.name_.c_str(), new_entry);
262 int foundIndex = search_einp(inp, new_entry);
265 gmx_fatal(FARGS, "A parameter is present with both the old name '%s' and the new name '%s'.", local.name_.c_str(), inp[foundIndex].name_.c_str());
268 local.name_.assign(new_entry);
272 fprintf(stderr, "Ignoring obsolete mdp entry '%s'\n",
273 local.name_.c_str());
274 local.bObsolete_ = TRUE;
280 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char *name)
286 for (gmx::index i = 0; i < inp.size(); i++)
288 if (gmx_strcasecmp_min(name, inp[i].name_.c_str()) == 0)
296 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char *name)
298 int i = search_einp(inp, name);
301 inp[i].count_ = inp.front().inp_count_++;
303 /* Prevent mdp lines being written twice for
304 options that are handled via key-value trees. */
305 inp[i].bHandledAsKeyValueTree_ = TRUE;
309 static int get_einp(std::vector<t_inpfile> *inp, const char *name)
311 std::vector<t_inpfile> &inpRef = *inp;
312 bool notfound = false;
314 int i = search_einp(inpRef, name);
319 inpRef.emplace_back(0, 0, false, true, false,
321 i = inpRef.size() - 1;
322 if (inpRef.size() == 1)
324 inpRef.front().inp_count_ = 1;
327 inpRef[i].count_ = inpRef.front().inp_count_++;
328 inpRef[i].bSet_ = TRUE;
331 fprintf(debug, "Inp %d = %s\n", inpRef[i].count_, inpRef[i].name_.c_str());
344 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
345 int get_eint(std::vector<t_inpfile> *inp, const char *name, int def,
348 std::vector<t_inpfile> &inpRef = *inp;
349 char buf[32], *ptr, warn_buf[STRLEN];
351 int ii = get_einp(inp, name);
355 sprintf(buf, "%d", def);
356 inpRef.back().value_.assign(buf);
362 int ret = std::strtol(inpRef[ii].value_.c_str(), &ptr, 10);
365 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not an integer value\n", inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
366 warning_error(wi, warn_buf);
373 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
374 int64_t get_eint64(std::vector<t_inpfile> *inp,
375 const char *name, int64_t def,
378 std::vector<t_inpfile> &inpRef = *inp;
379 char buf[32], *ptr, warn_buf[STRLEN];
381 int ii = get_einp(inp, name);
385 sprintf(buf, "%" PRId64, def);
386 inpRef.back().value_.assign(buf);
392 int64_t ret = str_to_int64_t(inpRef[ii].value_.c_str(), &ptr);
395 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not an integer value\n", inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
396 warning_error(wi, warn_buf);
403 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
404 double get_ereal(std::vector<t_inpfile> *inp, const char *name, double def,
407 std::vector<t_inpfile> &inpRef = *inp;
408 char buf[32], *ptr, warn_buf[STRLEN];
410 int ii = get_einp(inp, name);
414 sprintf(buf, "%g", def);
415 inpRef.back().value_.assign(buf);
421 double ret = strtod(inpRef[ii].value_.c_str(), &ptr);
424 sprintf(warn_buf, "Right hand side '%s' for parameter '%s' in parameter file is not a real value\n", inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
425 warning_error(wi, warn_buf);
432 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
433 const char *get_estr(std::vector<t_inpfile> *inp, const char *name, const char *def)
435 std::vector<t_inpfile> &inpRef = *inp;
438 int ii = get_einp(inp, name);
444 sprintf(buf, "%s", def);
445 inpRef.back().value_.assign(buf);
449 inpRef.back().value_.clear();
456 return inpRef[ii].value_.c_str();
460 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
461 int get_eeenum(std::vector<t_inpfile> *inp, const char *name, const char **defs,
464 std::vector<t_inpfile> &inpRef = *inp;
468 int ii = get_einp(inp, name);
472 inpRef.back().value_.assign(defs[0]);
477 for (i = 0; (defs[i] != nullptr); i++)
479 if (gmx_strcasecmp_min(defs[i], inpRef[ii].value_.c_str()) == 0)
485 if (defs[i] == nullptr)
487 n += sprintf(buf, "Invalid enum '%s' for variable %s, using '%s'\n",
488 inpRef[ii].value_.c_str(), name, defs[0]);
489 n += sprintf(buf+n, "Next time use one of:");
493 n += sprintf(buf+n, " '%s'", defs[j]);
498 warning_error(wi, buf);
502 fprintf(stderr, "%s\n", buf);
505 inpRef[ii].value_ = gmx_strdup(defs[0]);
513 int get_eenum(std::vector<t_inpfile> *inp, const char *name, const char **defs)
515 return get_eeenum(inp, name, defs, nullptr);
519 printStringNewline(std::vector<t_inpfile> *inp, const char *line)
521 std::string tmp("\n; ");
523 get_estr(inp, tmp.c_str(), nullptr);
527 printStringNoNewline(std::vector<t_inpfile> *inp, const char *line)
529 std::string tmp("; ");
531 get_estr(inp, tmp.c_str(), nullptr);
534 setStringEntry(std::vector<t_inpfile> *inp, const char *name, char *newName, const char *def)
536 const char *found = nullptr;
537 found = get_estr(inp, name, def);
538 if (found != nullptr)
540 std::strcpy(newName, found);