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! */
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
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());
85 void read_ab(char* line
, const char* fn
, MoleculePatch
* hack
)
91 ns
= sscanf(line
, "%d%d%s%s%s%s%s", &nh
, &tp
, hn
, a
[0], a
[1], a
[2], a
[3]);
94 gmx_fatal(FARGS
, "wrong format in input file %s on line\n%s\n", fn
, line
);
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
);
106 if ((hack
->nctl
!= ncontrol
[hack
->tp
]) && (ncontrol
[hack
->tp
] != -1))
109 "Error in hdb file %s:\nWrong number of control atoms (%d instead of %d) on "
111 fn
, hack
->nctl
, ncontrol
[hack
->tp
], line
);
113 for (int i
= 0; (i
< hack
->nctl
); i
++)
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
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)
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
);
154 globalPatches
->emplace_back(MoleculePatchDatabase());
155 MoleculePatchDatabase
* block
= &globalPatches
->back();
156 clearModificationBlock(block
);
158 block
->filebase
= filebase
;
161 if (sscanf(line
+ n
, "%d", &nab
) == 1)
163 for (int i
= 0; (i
< nab
); i
++)
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());
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
)
216 globalPatches
.begin(), globalPatches
.end(),
217 [&key
](const MoleculePatchDatabase
& a
) { return gmx::equalCaseInsensitive(key
, a
.name
); });