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 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
47 #include "gromacs/fileio/warninp.h"
48 #include "gromacs/utility/arrayref.h"
49 #include "gromacs/utility/binaryinformation.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/fatalerror.h"
53 #include "gromacs/utility/keyvaluetreebuilder.h"
54 #include "gromacs/utility/niceheader.h"
55 #include "gromacs/utility/programcontext.h"
56 #include "gromacs/utility/smalloc.h"
57 #include "gromacs/utility/stringutil.h"
58 #include "gromacs/utility/textreader.h"
59 #include "gromacs/utility/textwriter.h"
61 std::vector<t_inpfile> read_inpfile(gmx::TextInputStream* stream, const char* fn, warninp_t wi)
63 std::vector<t_inpfile> inp;
67 fprintf(debug, "Reading MDP file %s\n", fn);
70 int indexOfLineReadFromFile = 0;
72 gmx::TextReader reader(stream);
73 reader.setTrimTrailingWhiteSpace(true);
74 reader.setTrimTrailingComment(true, ';');
75 while (reader.readLine(&line))
77 indexOfLineReadFromFile++;
78 set_warning_line(wi, fn, indexOfLineReadFromFile);
85 auto tokens = gmx::splitAndTrimDelimitedString(line, '=');
86 if (tokens.size() < 2)
88 auto message = gmx::formatString(
89 "No '=' to separate .mdp parameter key and value was found on line:\n'%s'",
91 warning_error(wi, message);
94 if (tokens.size() > 2)
96 // More than one equals symbol in the original line is
97 // valid if the RHS is a free string, and needed for
98 // "define = -DBOOLVAR -DVAR=VALUE".
100 // First, drop all the fields on the RHS of the first equals symbol.
102 // This find cannot return std::string::npos.
103 auto firstEqualsPos = line.find('=');
104 tokens.emplace_back(gmx::stripString(line.substr(firstEqualsPos + 1)));
106 GMX_RELEASE_ASSERT(tokens.size() == 2, "Must have tokens for key and value");
107 if (tokens[0].empty() && tokens[1].empty())
109 auto message = gmx::formatString(
110 "No .mdp parameter name or value was found on line:\n'%s'", line.c_str());
111 warning_error(wi, message);
114 if (tokens[0].empty())
116 auto message = gmx::formatString(
117 "No .mdp parameter name was found on the left-hand side of '=' on line:\n'%s'",
119 warning_error(wi, message);
122 if (tokens[1].empty())
124 // Users are probably using this for lines like
125 // tcoupl = ;v-rescale
127 // so we accept their intent to use the default behavior.
131 /* Now finally something sensible; check for duplicates */
132 int found_index = search_einp(inp, tokens[0].c_str());
134 if (found_index == -1)
137 inp.emplace_back(0, 1, false, false, false, tokens[0], tokens[1]);
141 auto message = gmx::formatString("Parameter \"%s\" doubly defined\n", tokens[0].c_str());
142 warning_error(wi, message);
145 /* This preserves the behaviour of the old code, which issues some
146 warnings after completing parsing. Regenerating regressiontest
147 warning files is not worth the effort. */
148 indexOfLineReadFromFile++;
149 set_warning_line(wi, fn, indexOfLineReadFromFile);
153 fprintf(debug, "Done reading MDP file, there were %zu entries in there\n", inp.size());
159 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp)
161 gmx::KeyValueTreeBuilder builder;
162 auto root = builder.rootObject();
163 for (auto& local : inp)
165 root.addValue<std::string>(local.name_, !local.value_.empty() ? local.value_ : "");
167 return builder.build();
173 bool operator()(t_inpfile const& a, t_inpfile const& b) { return a.count_ < b.count_; }
176 static void sort_inp(std::vector<t_inpfile>* inp)
178 std::vector<t_inpfile>& inpRef = *inp;
182 for (const auto& local : inpRef)
184 mm = std::max(mm, local.count_);
186 for (auto& local : inpRef)
188 if (local.count_ == 0)
193 std::sort(inpRef.begin(), inpRef.end(), inp_comp());
196 void write_inpfile(gmx::TextOutputStream* stream,
198 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_) {}
220 else if (local.bSet_)
222 if (local.name_[0] == ';' || (local.name_.length() > 2 && local.name_[1] == ';'))
224 writer.writeLine(formatString("%-24s", local.name_.c_str()));
228 writer.writeLine(formatString("%-24s = %s", local.name_.c_str(),
229 !local.value_.empty() ? local.value_.c_str() : ""));
232 else if (!local.bObsolete_)
235 formatString("Unknown left-hand '%s' in parameter file\n", local.name_.c_str());
238 warning_error(wi, message.c_str());
242 warning(wi, message.c_str());
247 check_warning_error(wi, FARGS);
250 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry)
252 for (auto& local : inp)
254 if (gmx_strcasecmp_min(old_entry, local.name_.c_str()) == 0)
258 fprintf(stderr, "Replacing old mdp entry '%s' by '%s'\n", local.name_.c_str(), new_entry);
260 int foundIndex = search_einp(inp, new_entry);
264 "A parameter is present with both the old name '%s' and the new name "
266 local.name_.c_str(), inp[foundIndex].name_.c_str());
269 local.name_.assign(new_entry);
273 fprintf(stderr, "Ignoring obsolete mdp entry '%s'\n", 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.ssize(); 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, name, "");
320 i = inpRef.size() - 1;
321 if (inpRef.size() == 1)
323 inpRef.front().inp_count_ = 1;
326 inpRef[i].count_ = inpRef.front().inp_count_++;
327 inpRef[i].bSet_ = TRUE;
330 fprintf(debug, "Inp %d = %s\n", inpRef[i].count_, inpRef[i].name_.c_str());
343 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
344 int get_eint(std::vector<t_inpfile>* inp, const char* name, int def, warninp_t wi)
346 std::vector<t_inpfile>& inpRef = *inp;
347 char buf[32], *ptr, warn_buf[STRLEN];
349 int ii = get_einp(inp, name);
353 sprintf(buf, "%d", def);
354 inpRef.back().value_.assign(buf);
360 int ret = std::strtol(inpRef[ii].value_.c_str(), &ptr, 10);
364 "Right hand side '%s' for parameter '%s' in parameter file is not an integer "
366 inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
367 warning_error(wi, warn_buf);
374 int get_eint(std::vector<t_inpfile>* inp, const std::string& name, int def, warninp_t wi)
376 return get_eint(inp, name.c_str(), def, wi);
379 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
380 int64_t get_eint64(std::vector<t_inpfile>* inp, const char* name, int64_t def, warninp_t wi)
382 std::vector<t_inpfile>& inpRef = *inp;
383 char buf[32], *ptr, warn_buf[STRLEN];
385 int ii = get_einp(inp, name);
389 sprintf(buf, "%" PRId64, def);
390 inpRef.back().value_.assign(buf);
396 int64_t ret = str_to_int64_t(inpRef[ii].value_.c_str(), &ptr);
400 "Right hand side '%s' for parameter '%s' in parameter file is not an integer "
402 inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
403 warning_error(wi, warn_buf);
410 int64_t get_eint64(std::vector<t_inpfile>* inp, const std::string& name, int64_t def, warninp_t wi)
412 return get_eint64(inp, name.c_str(), def, wi);
415 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
416 double get_ereal(std::vector<t_inpfile>* inp, const char* name, double def, warninp_t wi)
418 std::vector<t_inpfile>& inpRef = *inp;
419 char buf[32], *ptr, warn_buf[STRLEN];
421 int ii = get_einp(inp, name);
425 sprintf(buf, "%g", def);
426 inpRef.back().value_.assign(buf);
432 double ret = strtod(inpRef[ii].value_.c_str(), &ptr);
436 "Right hand side '%s' for parameter '%s' in parameter file is not a real "
438 inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
439 warning_error(wi, warn_buf);
446 double get_ereal(std::vector<t_inpfile>* inp, const std::string& name, double def, warninp_t wi)
448 return get_ereal(inp, name.c_str(), def, wi);
451 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
452 const char* get_estr(std::vector<t_inpfile>* inp, const char* name, const char* def)
454 std::vector<t_inpfile>& inpRef = *inp;
456 int ii = get_einp(inp, name);
462 inpRef.back().value_.assign(def);
466 inpRef.back().value_.clear();
473 return inpRef[ii].value_.c_str();
477 const char* get_estr(std::vector<t_inpfile>* inp, const std::string& name, const char* def)
479 return get_estr(inp, name.c_str(), def);
482 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
483 int get_eeenum(std::vector<t_inpfile>* inp, const char* name, const char** defs, warninp_t wi)
485 std::vector<t_inpfile>& inpRef = *inp;
489 int ii = get_einp(inp, name);
493 inpRef.back().value_.assign(defs[0]);
498 for (i = 0; (defs[i] != nullptr); i++)
500 if (gmx_strcasecmp_min(defs[i], inpRef[ii].value_.c_str()) == 0)
506 if (defs[i] == nullptr)
508 n += sprintf(buf, "Invalid enum '%s' for variable %s, using '%s'\n",
509 inpRef[ii].value_.c_str(), name, defs[0]);
510 n += sprintf(buf + n, "Next time use one of:");
514 n += sprintf(buf + n, " '%s'", defs[j]);
519 warning_error(wi, buf);
523 fprintf(stderr, "%s\n", buf);
526 inpRef[ii].value_ = gmx_strdup(defs[0]);
534 int get_eeenum(std::vector<t_inpfile>* inp, const std::string& name, const char** defs, warninp_t wi)
536 return get_eeenum(inp, name.c_str(), defs, wi);
539 int get_eenum(std::vector<t_inpfile>* inp, const char* name, const char** defs)
541 return get_eeenum(inp, name, defs, nullptr);
544 void printStringNewline(std::vector<t_inpfile>* inp, const char* line)
546 std::string tmp("\n; ");
548 get_estr(inp, tmp.c_str(), nullptr);
551 void printStringNoNewline(std::vector<t_inpfile>* inp, const char* line)
553 std::string tmp("; ");
555 get_estr(inp, tmp.c_str(), nullptr);
557 void setStringEntry(std::vector<t_inpfile>* inp, const char* name, char* newName, const char* def)
559 const char* found = nullptr;
560 found = get_estr(inp, name, def);
561 if (found != nullptr)
563 std::strcpy(newName, found);