Precision fix for rescbt code.
[gromacs.git] / src / gromacs / gmxpreprocess / h_db.cpp
blobb19d942c59921c79896977a7a89d829d835cf69a
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,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.
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
40 #include "h_db.h"
42 #include <cctype>
43 #include <cstdlib>
44 #include <cstring>
46 #include <algorithm>
47 #include <string>
48 #include <vector>
50 #include "gromacs/gmxpreprocess/fflibutil.h"
51 #include "gromacs/gmxpreprocess/notset.h"
52 #include "gromacs/topology/atoms.h"
53 #include "gromacs/utility/arraysize.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/futil.h"
57 #include "gromacs/utility/smalloc.h"
58 #include "gromacs/utility/stringutil.h"
60 #include "hackblock.h"
62 /* Number of control atoms for each 'add' type.
64 * There are 11 types of adding hydrogens, numbered from 1 thru
65 * 11. Each of these has a specific number of control atoms, that
66 * determine how the hydrogens are added. Here these number are
67 * given. Because arrays start at 0, an extra dummy for index 0 is
68 * added.
70 const int ncontrol[] = { -1, 3, 3, 3, 3, 4, 3, 1, 3, 3, 1, 1 };
71 #define maxcontrol asize(ncontrol)
73 void print_ab(FILE *out, const MoleculePatch &hack, const char *nname)
75 fprintf(out, "%d\t%d\t%s", hack.nr, hack.tp, nname);
76 for (int i = 0; (i < hack.nctl); i++)
78 fprintf(out, "\t%s", hack.a[i].c_str());
80 fprintf(out, "\n");
84 void read_ab(char *line, const char *fn, MoleculePatch *hack)
86 int nh, tp, ns;
87 char a[4][12];
88 char hn[32];
90 ns = sscanf(line, "%d%d%s%s%s%s%s", &nh, &tp, hn, a[0], a[1], a[2], a[3]);
91 if (ns < 4)
93 gmx_fatal(FARGS, "wrong format in input file %s on line\n%s\n", fn, line);
96 hack->nr = nh;
97 hack->tp = tp;
98 if ((tp < 1) || (tp >= maxcontrol))
100 gmx_fatal(FARGS, "Error in hdb file %s:\nH-type should be in 1-%d. Offending line:\n%s", fn, maxcontrol-1, line);
103 hack->nctl = ns - 3;
104 if ((hack->nctl != ncontrol[hack->tp]) && (ncontrol[hack->tp] != -1))
106 gmx_fatal(FARGS, "Error in hdb file %s:\nWrong number of control atoms (%d instead of %d) on line:\n%s\n", fn, hack->nctl, ncontrol[hack->tp], line);
108 for (int i = 0; (i < hack->nctl); i++)
110 hack->a[i] = a[i];
112 hack->oname.clear();
113 hack->nname = hn;
114 hack->atom.clear();
115 hack->cgnr = NOTSET;
116 hack->bXSet = false;
117 for (int i = 0; i < DIM; i++)
119 hack->newx[i] = NOTSET;
123 static void read_h_db_file(const char *hfn, std::vector<MoleculePatchDatabase> *globalPatches)
125 char filebase[STRLEN], line[STRLEN], buf[STRLEN];
127 fflib_filename_base(hfn, filebase, STRLEN);
128 /* Currently filebase is read and set, but not used.
129 * hdb entries from any hdb file and be applied to rtp entries
130 * in any rtp file.
133 FILE *in = fflib_open(hfn);
135 while (fgets2(line, STRLEN-1, in))
137 // Skip lines that are only whitespace
138 if (gmx::countWords(line) == 0)
140 continue;
142 int n;
143 if (sscanf(line, "%s%n", buf, &n) != 1)
145 int size = globalPatches->size();
146 fprintf(stderr, "Error in hdb file: nah = %d\nline = '%s'\n",
147 size, line);
148 break;
150 globalPatches->emplace_back(MoleculePatchDatabase());
151 MoleculePatchDatabase *block = &globalPatches->back();
152 clearModificationBlock(block);
153 block->name = buf;
154 block->filebase = filebase;
156 int nab;
157 if (sscanf(line+n, "%d", &nab) == 1)
159 for (int i = 0; (i < nab); i++)
161 if (feof(in))
163 gmx_fatal(FARGS, "Expected %d lines of hydrogens, found only %d "
164 "while reading Hydrogen Database %s residue %s",
165 nab, i-1, block->name.c_str(), hfn);
167 if (nullptr == fgets(buf, STRLEN, in))
169 gmx_fatal(FARGS, "Error reading from file %s", hfn);
171 block->hack.emplace_back(MoleculePatch());
172 read_ab(buf, hfn, &block->hack.back());
176 gmx_ffclose(in);
178 if (!globalPatches->empty())
180 /* Sort the list for searching later */
181 std::sort(globalPatches->begin(), globalPatches->end(),
182 [](const MoleculePatchDatabase &a1, const MoleculePatchDatabase &a2)
183 { return std::lexicographical_compare(a1.name.begin(), a1.name.end(),
184 a2.name.begin(), a2.name.end(),
185 [](const char &c1, const char &c2)
186 { return std::toupper(c1) < std::toupper(c2); }); });
190 int read_h_db(const char *ffdir, std::vector<MoleculePatchDatabase> *globalPatches)
192 /* Read the hydrogen database file(s).
193 * Do not generate an error when no files are found.
196 std::vector<std::string> hdbf = fflib_search_file_end(ffdir, ".hdb", FALSE);
197 globalPatches->clear();
198 for (const auto &filename : hdbf)
200 read_h_db_file(filename.c_str(), globalPatches);
202 return globalPatches->size();
205 gmx::ArrayRef<MoleculePatchDatabase>::iterator
206 search_h_db(gmx::ArrayRef<MoleculePatchDatabase> globalPatches, char *key)
208 return std::find_if(globalPatches.begin(), globalPatches.end(),
209 [&key](const MoleculePatchDatabase &a)
210 { return gmx::equalCaseInsensitive(key, a.name); });