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