Update instructions in containers.rst
[gromacs.git] / src / gromacs / gmxpreprocess / pgutil.cpp
blobc9387601d51dfbc63a0e8b7086e698902923a6f2
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) 2012,2014,2015,2017,2018 by the GROMACS development team.
7 * Copyright (c) 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! */
40 #include "gmxpre.h"
42 #include "pgutil.h"
44 #include <cstring>
46 #include <algorithm>
48 #include "gromacs/topology/atoms.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/snprintf.h"
53 #define BUFSIZE 1024
54 static void atom_not_found(int fatal_errno,
55 const char* file,
56 int line,
57 const char* atomname,
58 int resind,
59 const char* resname,
60 const char* bondtype,
61 bool bAllowMissing)
63 char message_buffer[BUFSIZE];
64 if (strcmp(bondtype, "check") != 0)
66 if (0 != strcmp(bondtype, "atom"))
68 snprintf(message_buffer, 1024,
69 "Residue %d named %s of a molecule in the input file was mapped\n"
70 "to an entry in the topology database, but the atom %s used in\n"
71 "an interaction of type %s in that entry is not found in the\n"
72 "input file. Perhaps your atom and/or residue naming needs to be\n"
73 "fixed.\n",
74 resind + 1, resname, atomname, bondtype);
76 else
78 snprintf(message_buffer, 1024,
79 "Residue %d named %s of a molecule in the input file was mapped\n"
80 "to an entry in the topology database, but the atom %s used in\n"
81 "that entry is not found in the input file. Perhaps your atom\n"
82 "and/or residue naming needs to be fixed.\n",
83 resind + 1, resname, atomname);
85 if (bAllowMissing)
87 gmx_warning("WARNING: %s", message_buffer);
89 else
91 gmx_fatal(fatal_errno, file, line, "%s", message_buffer);
96 int search_atom(const char* type,
97 int start,
98 const t_atoms* atoms,
99 const char* bondtype,
100 bool bAllowMissing,
101 gmx::ArrayRef<const int> cyclicBondsIndex)
103 int i, resind = -1;
104 bool bPrevious, bNext, bOverring;
105 int natoms = atoms->nr;
106 t_atom* at = atoms->atom;
107 char** const* anm = atoms->atomname;
108 gmx::ArrayRef<const int>::iterator cyclicBondsIterator;
110 bPrevious = (strchr(type, '-') != nullptr);
111 bNext = (strchr(type, '+') != nullptr);
113 if (!bPrevious)
115 resind = at[start].resind;
116 if (bNext)
118 /* The next residue */
119 type++;
120 bOverring = !cyclicBondsIndex.empty()
121 && (cyclicBondsIterator =
122 std::find(cyclicBondsIndex.begin(), cyclicBondsIndex.end(), resind))
123 != cyclicBondsIndex.end();
124 if (bOverring && ((cyclicBondsIterator - cyclicBondsIndex.begin()) & 1))
126 resind = *(--cyclicBondsIterator);
127 return search_res_atom(type, resind, atoms, bondtype, false);
129 else
131 while ((start < natoms) && (at[start].resind == resind))
133 start++;
135 if (start < natoms)
137 resind = at[start].resind;
142 for (i = start; (i < natoms) && (bNext || (at[i].resind == resind)); i++)
144 if (anm[i] && gmx_strcasecmp(type, *(anm[i])) == 0)
146 return i;
149 if (!(bNext && at[start].resind == at[natoms - 1].resind))
151 atom_not_found(FARGS, type, at[start].resind, *atoms->resinfo[resind].name, bondtype,
152 bAllowMissing);
155 else
157 /* The previous residue */
158 type++;
159 resind = at[start].resind;
160 bOverring = !cyclicBondsIndex.empty()
161 && (cyclicBondsIterator =
162 std::find(cyclicBondsIndex.begin(), cyclicBondsIndex.end(), resind))
163 != cyclicBondsIndex.end();
165 if (bOverring && !((cyclicBondsIterator - cyclicBondsIndex.begin()) & 1))
167 resind = *(++cyclicBondsIterator);
168 return search_res_atom(type, resind, atoms, bondtype, false);
170 else
172 while ((start >= 0) && (at[start].resind == resind))
174 start--;
176 if (start >= 0)
178 resind = at[start].resind;
179 start++;
182 for (i = start - 1; (i >= 0) && (at[i].resind == resind); i--)
184 if (gmx_strcasecmp(type, *(anm[i])) == 0)
186 return i;
189 if (start > 0)
191 atom_not_found(FARGS, type, at[start].resind, *atoms->resinfo[resind].name, bondtype,
192 bAllowMissing);
195 return -1;
198 int search_res_atom(const char* type, int resind, const t_atoms* atoms, const char* bondtype, bool bAllowMissing)
200 int i;
202 for (i = 0; (i < atoms->nr); i++)
204 if (atoms->atom[i].resind == resind)
206 return search_atom(type, i, atoms, bondtype, bAllowMissing, gmx::ArrayRef<const int>());
210 return -1;