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,2019, 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/smalloc.h"
55 #include "gromacs/utility/stringutil.h"
56 #include "gromacs/utility/textreader.h"
57 #include "gromacs/utility/textwriter.h"
59 std::vector<t_inpfile> read_inpfile(gmx::TextInputStream* stream, const char* fn, warninp_t wi)
61 std::vector<t_inpfile> inp;
65 fprintf(debug, "Reading MDP file %s\n", fn);
68 int indexOfLineReadFromFile = 0;
70 gmx::TextReader reader(stream);
71 reader.setTrimTrailingWhiteSpace(true);
72 reader.setTrimTrailingComment(true, ';');
73 while (reader.readLine(&line))
75 indexOfLineReadFromFile++;
76 set_warning_line(wi, fn, indexOfLineReadFromFile);
83 auto tokens = gmx::splitAndTrimDelimitedString(line, '=');
84 if (tokens.size() < 2)
86 auto message = gmx::formatString(
87 "No '=' to separate .mdp parameter key and value was found on line:\n'%s'",
89 warning_error(wi, message);
92 if (tokens.size() > 2)
94 // More than one equals symbol in the original line is
95 // valid if the RHS is a free string, and needed for
96 // "define = -DBOOLVAR -DVAR=VALUE".
98 // First, drop all the fields on the RHS of the first equals symbol.
100 // This find cannot return std::string::npos.
101 auto firstEqualsPos = line.find('=');
102 tokens.emplace_back(gmx::stripString(line.substr(firstEqualsPos + 1)));
104 GMX_RELEASE_ASSERT(tokens.size() == 2, "Must have tokens for key and value");
105 if (tokens[0].empty() && tokens[1].empty())
107 auto message = gmx::formatString(
108 "No .mdp parameter name or value was found on line:\n'%s'", line.c_str());
109 warning_error(wi, message);
112 if (tokens[0].empty())
114 auto message = gmx::formatString(
115 "No .mdp parameter name was found on the left-hand side of '=' on line:\n'%s'",
117 warning_error(wi, message);
120 if (tokens[1].empty())
122 // Users are probably using this for lines like
123 // tcoupl = ;v-rescale
125 // so we accept their intent to use the default behavior.
129 /* Now finally something sensible; check for duplicates */
130 int found_index = search_einp(inp, tokens[0].c_str());
132 if (found_index == -1)
135 inp.emplace_back(0, 1, false, false, false, tokens[0], tokens[1]);
139 auto message = gmx::formatString("Parameter \"%s\" doubly defined\n", tokens[0].c_str());
140 warning_error(wi, message);
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", inp.size());
157 gmx::KeyValueTreeObject flatKeyValueTreeFromInpFile(gmx::ArrayRef<const t_inpfile> inp)
159 gmx::KeyValueTreeBuilder builder;
160 auto root = builder.rootObject();
161 for (auto& local : inp)
163 root.addValue<std::string>(local.name_, !local.value_.empty() ? local.value_ : "");
165 return builder.build();
171 bool operator()(t_inpfile const& a, t_inpfile const& b) { return a.count_ < b.count_; }
174 static void sort_inp(std::vector<t_inpfile>* inp)
176 std::vector<t_inpfile>& inpRef = *inp;
180 for (const auto& local : inpRef)
182 mm = std::max(mm, local.count_);
184 for (auto& local : inpRef)
186 if (local.count_ == 0)
191 std::sort(inpRef.begin(), inpRef.end(), inp_comp());
194 void write_inpfile(gmx::TextOutputStream* stream,
196 std::vector<t_inpfile>* inp,
197 gmx_bool bHaltOnUnknown,
198 WriteMdpHeader writeHeader,
201 using gmx::formatString;
205 gmx::TextWriter writer(stream);
206 if (writeHeader == WriteMdpHeader::yes)
208 gmx::niceHeader(&writer, fn, ';');
210 gmx::BinaryInformationSettings settings;
211 settings.generatedByHeader(true);
212 settings.linePrefix(";\t");
213 gmx::printBinaryInformation(&writer, gmx::getProgramContext(), settings);
215 for (const auto& local : *inp)
217 if (local.bHandledAsKeyValueTree_) {}
218 else if (local.bSet_)
220 if (local.name_[0] == ';' || (local.name_.length() > 2 && local.name_[1] == ';'))
222 writer.writeLine(formatString("%-24s", local.name_.c_str()));
226 writer.writeLine(formatString("%-24s = %s", local.name_.c_str(),
227 !local.value_.empty() ? local.value_.c_str() : ""));
230 else if (!local.bObsolete_)
233 formatString("Unknown left-hand '%s' in parameter file\n", local.name_.c_str());
236 warning_error(wi, message.c_str());
240 warning(wi, message.c_str());
245 check_warning_error(wi, FARGS);
248 void replace_inp_entry(gmx::ArrayRef<t_inpfile> inp, const char* old_entry, const char* new_entry)
250 for (auto& local : inp)
252 if (gmx_strcasecmp_min(old_entry, local.name_.c_str()) == 0)
256 fprintf(stderr, "Replacing old mdp entry '%s' by '%s'\n", local.name_.c_str(), new_entry);
258 int foundIndex = search_einp(inp, new_entry);
262 "A parameter is present with both the old name '%s' and the new name "
264 local.name_.c_str(), inp[foundIndex].name_.c_str());
267 local.name_.assign(new_entry);
271 fprintf(stderr, "Ignoring obsolete mdp entry '%s'\n", local.name_.c_str());
272 local.bObsolete_ = TRUE;
278 int search_einp(gmx::ArrayRef<const t_inpfile> inp, const char* name)
284 for (gmx::index i = 0; i < inp.ssize(); i++)
286 if (gmx_strcasecmp_min(name, inp[i].name_.c_str()) == 0)
294 void mark_einp_set(gmx::ArrayRef<t_inpfile> inp, const char* name)
296 int i = search_einp(inp, name);
299 inp[i].count_ = inp.front().inp_count_++;
301 /* Prevent mdp lines being written twice for
302 options that are handled via key-value trees. */
303 inp[i].bHandledAsKeyValueTree_ = TRUE;
307 static int get_einp(std::vector<t_inpfile>* inp, const char* name)
309 std::vector<t_inpfile>& inpRef = *inp;
310 bool notfound = false;
312 int i = search_einp(inpRef, name);
317 inpRef.emplace_back(0, 0, false, true, false, name, "");
318 i = inpRef.size() - 1;
319 if (inpRef.size() == 1)
321 inpRef.front().inp_count_ = 1;
324 inpRef[i].count_ = inpRef.front().inp_count_++;
325 inpRef[i].bSet_ = TRUE;
328 fprintf(debug, "Inp %d = %s\n", inpRef[i].count_, inpRef[i].name_.c_str());
341 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
342 int get_eint(std::vector<t_inpfile>* inp, const char* name, int def, warninp_t wi)
344 std::vector<t_inpfile>& inpRef = *inp;
345 char buf[32], *ptr, warn_buf[STRLEN];
347 int ii = get_einp(inp, name);
351 sprintf(buf, "%d", def);
352 inpRef.back().value_.assign(buf);
358 int ret = std::strtol(inpRef[ii].value_.c_str(), &ptr, 10);
362 "Right hand side '%s' for parameter '%s' in parameter file is not an integer "
364 inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
365 warning_error(wi, warn_buf);
372 int get_eint(std::vector<t_inpfile>* inp, const std::string& name, int def, warninp_t wi)
374 return get_eint(inp, name.c_str(), def, wi);
377 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
378 int64_t get_eint64(std::vector<t_inpfile>* inp, const char* name, int64_t def, warninp_t wi)
380 std::vector<t_inpfile>& inpRef = *inp;
381 char buf[32], *ptr, warn_buf[STRLEN];
383 int ii = get_einp(inp, name);
387 sprintf(buf, "%" PRId64, def);
388 inpRef.back().value_.assign(buf);
394 int64_t ret = str_to_int64_t(inpRef[ii].value_.c_str(), &ptr);
398 "Right hand side '%s' for parameter '%s' in parameter file is not an integer "
400 inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
401 warning_error(wi, warn_buf);
408 int64_t get_eint64(std::vector<t_inpfile>* inp, const std::string& name, int64_t def, warninp_t wi)
410 return get_eint64(inp, name.c_str(), def, wi);
413 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
414 double get_ereal(std::vector<t_inpfile>* inp, const char* name, double def, warninp_t wi)
416 std::vector<t_inpfile>& inpRef = *inp;
417 char buf[32], *ptr, warn_buf[STRLEN];
419 int ii = get_einp(inp, name);
423 sprintf(buf, "%g", def);
424 inpRef.back().value_.assign(buf);
430 double ret = strtod(inpRef[ii].value_.c_str(), &ptr);
434 "Right hand side '%s' for parameter '%s' in parameter file is not a real "
436 inpRef[ii].value_.c_str(), inpRef[ii].name_.c_str());
437 warning_error(wi, warn_buf);
444 double get_ereal(std::vector<t_inpfile>* inp, const std::string& name, double def, warninp_t wi)
446 return get_ereal(inp, name.c_str(), def, wi);
449 /* Note that sanitizing the trailing part of inp[ii].value was the responsibility of read_inpfile() */
450 const char* get_estr(std::vector<t_inpfile>* inp, const char* name, const char* def)
452 std::vector<t_inpfile>& inpRef = *inp;
455 int ii = get_einp(inp, name);
461 sprintf(buf, "%s", def);
462 inpRef.back().value_.assign(buf);
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);