Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / h_db.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
39
40 #include "h_db.h"
41
42 #include <cctype>
43 #include <cstdlib>
44 #include <cstring>
45
46 #include <algorithm>
47 #include <string>
48 #include <vector>
49
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"
59
60 #include "hackblock.h"
61
62 /* Number of control atoms for each 'add' type.
63  *
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.
69  */
70 const int ncontrol[] = { -1, 3, 3, 3, 3, 4, 3, 1, 3, 3, 1, 1 };
71 #define maxcontrol asize(ncontrol)
72
73 void print_ab(FILE* out, const MoleculePatch& hack, const char* nname)
74 {
75     fprintf(out, "%d\t%d\t%s", hack.nr, hack.tp, nname);
76     for (int i = 0; (i < hack.nctl); i++)
77     {
78         fprintf(out, "\t%s", hack.a[i].c_str());
79     }
80     fprintf(out, "\n");
81 }
82
83
84 void read_ab(char* line, const char* fn, MoleculePatch* hack)
85 {
86     int  nh, tp, ns;
87     char a[4][12];
88     char hn[32];
89
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)
92     {
93         gmx_fatal(FARGS, "wrong format in input file %s on line\n%s\n", fn, line);
94     }
95
96     hack->nr = nh;
97     hack->tp = tp;
98     if ((tp < 1) || (tp >= maxcontrol))
99     {
100         gmx_fatal(FARGS, "Error in hdb file %s:\nH-type should be in 1-%d. Offending line:\n%s", fn,
101                   maxcontrol - 1, line);
102     }
103
104     hack->nctl = ns - 3;
105     if ((hack->nctl != ncontrol[hack->tp]) && (ncontrol[hack->tp] != -1))
106     {
107         gmx_fatal(FARGS,
108                   "Error in hdb file %s:\nWrong number of control atoms (%d instead of %d) on "
109                   "line:\n%s\n",
110                   fn, hack->nctl, ncontrol[hack->tp], line);
111     }
112     for (int i = 0; (i < hack->nctl); i++)
113     {
114         hack->a[i] = a[i];
115     }
116     hack->oname.clear();
117     hack->nname = hn;
118     hack->atom.clear();
119     hack->cgnr  = NOTSET;
120     hack->bXSet = false;
121     for (int i = 0; i < DIM; i++)
122     {
123         hack->newx[i] = NOTSET;
124     }
125 }
126
127 static void read_h_db_file(const char* hfn, std::vector<MoleculePatchDatabase>* globalPatches)
128 {
129     char filebase[STRLEN], line[STRLEN], buf[STRLEN];
130
131     fflib_filename_base(hfn, filebase, STRLEN);
132     /* Currently filebase is read and set, but not used.
133      * hdb entries from any hdb file and be applied to rtp entries
134      * in any rtp file.
135      */
136
137     FILE* in = fflib_open(hfn);
138
139     while (fgets2(line, STRLEN - 1, in))
140     {
141         // Skip lines that are only whitespace
142         if (gmx::countWords(line) == 0)
143         {
144             continue;
145         }
146         int n;
147         if (sscanf(line, "%s%n", buf, &n) != 1)
148         {
149             int size = globalPatches->size();
150             fprintf(stderr, "Error in hdb file: nah = %d\nline = '%s'\n", size, line);
151             break;
152         }
153         globalPatches->emplace_back(MoleculePatchDatabase());
154         MoleculePatchDatabase* block = &globalPatches->back();
155         clearModificationBlock(block);
156         block->name     = buf;
157         block->filebase = filebase;
158
159         int nab;
160         if (sscanf(line + n, "%d", &nab) == 1)
161         {
162             for (int i = 0; (i < nab); i++)
163             {
164                 if (feof(in))
165                 {
166                     gmx_fatal(FARGS,
167                               "Expected %d lines of hydrogens, found only %d "
168                               "while reading Hydrogen Database %s residue %s",
169                               nab, i - 1, block->name.c_str(), hfn);
170                 }
171                 if (nullptr == fgets(buf, STRLEN, in))
172                 {
173                     gmx_fatal(FARGS, "Error reading from file %s", hfn);
174                 }
175                 block->hack.emplace_back(MoleculePatch());
176                 read_ab(buf, hfn, &block->hack.back());
177             }
178         }
179     }
180     gmx_ffclose(in);
181
182     if (!globalPatches->empty())
183     {
184         /* Sort the list for searching later */
185         std::sort(globalPatches->begin(), globalPatches->end(),
186                   [](const MoleculePatchDatabase& a1, const MoleculePatchDatabase& a2) {
187                       return std::lexicographical_compare(
188                               a1.name.begin(), a1.name.end(), a2.name.begin(), a2.name.end(),
189                               [](const char& c1, const char& c2) {
190                                   return std::toupper(c1) < std::toupper(c2);
191                               });
192                   });
193     }
194 }
195
196 int read_h_db(const char* ffdir, std::vector<MoleculePatchDatabase>* globalPatches)
197 {
198     /* Read the hydrogen database file(s).
199      * Do not generate an error when no files are found.
200      */
201
202     std::vector<std::string> hdbf = fflib_search_file_end(ffdir, ".hdb", FALSE);
203     globalPatches->clear();
204     for (const auto& filename : hdbf)
205     {
206         read_h_db_file(filename.c_str(), globalPatches);
207     }
208     return globalPatches->size();
209 }
210
211 gmx::ArrayRef<const MoleculePatchDatabase>::iterator
212 search_h_db(gmx::ArrayRef<const MoleculePatchDatabase> globalPatches, const char* key)
213 {
214     return std::find_if(
215             globalPatches.begin(), globalPatches.end(),
216             [&key](const MoleculePatchDatabase& a) { return gmx::equalCaseInsensitive(key, a.name); });
217 }