Update instructions in containers.rst
[gromacs.git] / src / gromacs / gmxpreprocess / h_db.cpp
blob9092fc8310f61d1579a6076741dc109644837da4
1 /*
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.
38 /* This file is completely threadsafe - keep it that way! */
39 #include "gmxpre.h"
41 #include "h_db.h"
43 #include <cctype>
44 #include <cstdlib>
45 #include <cstring>
47 #include <algorithm>
48 #include <string>
49 #include <vector>
51 #include "gromacs/gmxpreprocess/fflibutil.h"
52 #include "gromacs/gmxpreprocess/notset.h"
53 #include "gromacs/topology/atoms.h"
54 #include "gromacs/utility/arraysize.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/smalloc.h"
59 #include "gromacs/utility/stringutil.h"
61 #include "hackblock.h"
63 /* Number of control atoms for each 'add' type.
65 * There are 11 types of adding hydrogens, numbered from 1 thru
66 * 11. Each of these has a specific number of control atoms, that
67 * determine how the hydrogens are added. Here these number are
68 * given. Because arrays start at 0, an extra dummy for index 0 is
69 * added.
71 const int ncontrol[] = { -1, 3, 3, 3, 3, 4, 3, 1, 3, 3, 1, 1 };
72 #define maxcontrol asize(ncontrol)
74 void print_ab(FILE* out, const MoleculePatch& hack, const char* nname)
76 fprintf(out, "%d\t%d\t%s", hack.nr, hack.tp, nname);
77 for (int i = 0; (i < hack.nctl); i++)
79 fprintf(out, "\t%s", hack.a[i].c_str());
81 fprintf(out, "\n");
85 void read_ab(char* line, const char* fn, MoleculePatch* hack)
87 int nh, tp, ns;
88 char a[4][12];
89 char hn[32];
91 ns = sscanf(line, "%d%d%s%s%s%s%s", &nh, &tp, hn, a[0], a[1], a[2], a[3]);
92 if (ns < 4)
94 gmx_fatal(FARGS, "wrong format in input file %s on line\n%s\n", fn, line);
97 hack->nr = nh;
98 hack->tp = tp;
99 if ((tp < 1) || (tp >= maxcontrol))
101 gmx_fatal(FARGS, "Error in hdb file %s:\nH-type should be in 1-%d. Offending line:\n%s", fn,
102 maxcontrol - 1, line);
105 hack->nctl = ns - 3;
106 if ((hack->nctl != ncontrol[hack->tp]) && (ncontrol[hack->tp] != -1))
108 gmx_fatal(FARGS,
109 "Error in hdb file %s:\nWrong number of control atoms (%d instead of %d) on "
110 "line:\n%s\n",
111 fn, hack->nctl, ncontrol[hack->tp], line);
113 for (int i = 0; (i < hack->nctl); i++)
115 hack->a[i] = a[i];
117 hack->oname.clear();
118 hack->nname = hn;
119 hack->atom.clear();
120 hack->cgnr = NOTSET;
121 hack->bXSet = false;
122 for (int i = 0; i < DIM; i++)
124 hack->newx[i] = NOTSET;
128 static void read_h_db_file(const char* hfn, std::vector<MoleculePatchDatabase>* globalPatches)
130 char filebase[STRLEN], line[STRLEN], buf[STRLEN];
132 fflib_filename_base(hfn, filebase, STRLEN);
133 /* Currently filebase is read and set, but not used.
134 * hdb entries from any hdb file and be applied to rtp entries
135 * in any rtp file.
138 FILE* in = fflib_open(hfn);
140 while (fgets2(line, STRLEN - 1, in))
142 // Skip lines that are only whitespace
143 if (gmx::countWords(line) == 0)
145 continue;
147 int n;
148 if (sscanf(line, "%s%n", buf, &n) != 1)
150 int size = globalPatches->size();
151 fprintf(stderr, "Error in hdb file: nah = %d\nline = '%s'\n", size, line);
152 break;
154 globalPatches->emplace_back(MoleculePatchDatabase());
155 MoleculePatchDatabase* block = &globalPatches->back();
156 clearModificationBlock(block);
157 block->name = buf;
158 block->filebase = filebase;
160 int nab;
161 if (sscanf(line + n, "%d", &nab) == 1)
163 for (int i = 0; (i < nab); i++)
165 if (feof(in))
167 gmx_fatal(FARGS,
168 "Expected %d lines of hydrogens, found only %d "
169 "while reading Hydrogen Database %s residue %s",
170 nab, i - 1, block->name.c_str(), hfn);
172 if (nullptr == fgets(buf, STRLEN, in))
174 gmx_fatal(FARGS, "Error reading from file %s", hfn);
176 block->hack.emplace_back(MoleculePatch());
177 read_ab(buf, hfn, &block->hack.back());
181 gmx_ffclose(in);
183 if (!globalPatches->empty())
185 /* Sort the list for searching later */
186 std::sort(globalPatches->begin(), globalPatches->end(),
187 [](const MoleculePatchDatabase& a1, const MoleculePatchDatabase& a2) {
188 return std::lexicographical_compare(
189 a1.name.begin(), a1.name.end(), a2.name.begin(), a2.name.end(),
190 [](const char& c1, const char& c2) {
191 return std::toupper(c1) < std::toupper(c2);
197 int read_h_db(const char* ffdir, std::vector<MoleculePatchDatabase>* globalPatches)
199 /* Read the hydrogen database file(s).
200 * Do not generate an error when no files are found.
203 std::vector<std::string> hdbf = fflib_search_file_end(ffdir, ".hdb", FALSE);
204 globalPatches->clear();
205 for (const auto& filename : hdbf)
207 read_h_db_file(filename.c_str(), globalPatches);
209 return globalPatches->size();
212 gmx::ArrayRef<const MoleculePatchDatabase>::iterator
213 search_h_db(gmx::ArrayRef<const MoleculePatchDatabase> globalPatches, const char* key)
215 return std::find_if(
216 globalPatches.begin(), globalPatches.end(),
217 [&key](const MoleculePatchDatabase& a) { return gmx::equalCaseInsensitive(key, a.name); });