9fa36722eb21fde3dbcf4c3b0f8c14400cbf009e
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / hackblock.h
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,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 /*! \file
38  * \libinternal \brief
39  * Methods to modify atoms during preprocessing.
40  */
41 #ifndef GMX_GMXPREPROCESS_HACKBLOCK_H
42 #define GMX_GMXPREPROCESS_HACKBLOCK_H
43
44 #include <cstdio>
45
46 #include <string>
47 #include <vector>
48
49 #include "gromacs/gmxpreprocess/notset.h"
50 #include "gromacs/topology/ifunc.h"
51 #include "gromacs/utility/arrayref.h"
52
53 struct t_atom;
54 struct t_symtab;
55 /*! \brief
56  * Used for reading .rtp/.tdb
57  * ebtsBONDS must be the first, new types can be added to the end
58  * these *MUST* correspond to the arrays in hackblock.cpp
59  */
60 enum
61 {
62     ebtsBONDS,
63     ebtsANGLES,
64     ebtsPDIHS,
65     ebtsIDIHS,
66     ebtsEXCLS,
67     ebtsCMAP,
68     ebtsNR
69 };
70 //! Names for interaction type entries
71 extern const char* btsNames[ebtsNR];
72 //! Numbers for atoms in the interactions.
73 extern const int btsNiatoms[ebtsNR];
74
75 /* if changing any of these structs, make sure that all of the
76    free/clear/copy/merge_t_* functions stay updated */
77
78 /*! \libinternal \brief
79  * Information about single bonded interaction.
80  */
81 struct BondedInteraction
82 {
83     //! Atom names in the bond.
84     std::array<std::string, MAXATOMLIST> a;
85     /*! \brief
86      * Optional define string which gets copied from
87      * .rtp/.tdb to .top and will be parsed by cpp
88      * during grompp.
89      */
90     std::string s;
91     //! Has the entry been found?
92     bool match = false;
93     //! Get name of first atom in bonded interaction.
94     const std::string& ai() const { return a[0]; }
95     //! Get name of second atom in bonded interaction..
96     const std::string& aj() const { return a[1]; }
97     //! Get name of third atom in bonded interaction.
98     const std::string& ak() const { return a[2]; }
99     //! Get name of fourth atom in bonded interaction.
100     const std::string& al() const { return a[3]; }
101     //! Get name of fifth atom in bonded interaction.
102     const std::string& am() const { return a[4]; }
103 };
104
105 /*! \libinternal \brief
106  * Accumulation of different bonded types for preprocessing.
107  * \todo This should be merged with BondedInteraction.
108  */
109 struct BondedInteractionList
110 {
111     //! The type of bonded interaction.
112     int type = -1;
113     //! The actual bonded interactions.
114     std::vector<BondedInteraction> b;
115 };
116
117 /*! \libinternal \brief
118  * Information about preprocessing residues.
119  */
120 struct PreprocessResidue
121 {
122     //! Name of the residue.
123     std::string resname;
124     //! The base file name this rtp entry was read from.
125     std::string filebase;
126     //! Atom data.
127     std::vector<t_atom> atom;
128     //! Atom names.
129     std::vector<char**> atomname;
130     //! Charge group numbers.
131     std::vector<int> cgnr;
132     //! Delete autogenerated dihedrals or not.
133     bool bKeepAllGeneratedDihedrals = false;
134     //! Number of bonded exclusions.
135     int nrexcl = -1;
136     //! If Hydrogen only 1-4 interactions should be generated.
137     bool bGenerateHH14Interactions = false;
138     //! Delete dihedrals also defined by impropers.
139     bool bRemoveDihedralIfWithImproper = false;
140     //! List of bonded interactions to potentially add.
141     std::array<BondedInteractionList, ebtsNR> rb;
142     //! Get number of atoms in residue.
143     int natom() const { return atom.size(); }
144 };
145
146 //! Declare different types of hacks for later check.
147 enum class MoleculePatchType
148 {
149     //! Hack adds atom to structure/rtp.
150     Add,
151     //! Hack deletes atom.
152     Delete,
153     //! Hack replaces atom.
154     Replace
155 };
156
157 /*! \libinternal \brief
158  * Block to modify individual residues
159  */
160 struct MoleculePatch
161 {
162     //! Number of new are deleted atoms. NOT always equal to atom.size()!
163     int nr;
164     //! Old name for entry.
165     std::string oname;
166     //! New name for entry.
167     std::string nname;
168     //! New atom data.
169     std::vector<t_atom> atom;
170     //! Chargegroup number.
171     int cgnr = NOTSET;
172     //! Type of attachement.
173     int tp = 0;
174     //! Number of control atoms.
175     int nctl = 0;
176     //! Name of control atoms.
177     std::array<std::string, 4> a;
178     //! Is an atom to be hacked already present?
179     bool bAlreadyPresent = false;
180     //! Are coordinates for a new atom already set?
181     bool bXSet = false;
182     //! New position for hacked atom.
183     rvec newx = { NOTSET };
184     //! New atom index number after additions.
185     int newi = -1;
186
187     /*! \brief
188      * Get type of hack.
189      *
190      * This depends on the setting of oname and nname
191      * for legacy reasons. If oname is empty, we are adding,
192      * if oname is set and nname is empty, an atom is deleted,
193      * if both are set replacement is going on. If both are unset,
194      * an error is thrown.
195      */
196     MoleculePatchType type() const;
197
198     //! Control atom i name.
199     const std::string& ai() const { return a[0]; }
200     //! Control atom j name.
201     const std::string& aj() const { return a[1]; }
202     //! Control atom k name.
203     const std::string& ak() const { return a[2]; }
204     //! Control atom l name.
205     const std::string& al() const { return a[3]; }
206 };
207 /*! \libinternal \brief
208  * A set of modifications to apply to atoms.
209  */
210 struct MoleculePatchDatabase
211 {
212     //! Name of block
213     std::string name;
214     //! File that entry was read from.
215     std::string filebase;
216     //! List of changes to atoms.
217     std::vector<MoleculePatch> hack;
218     //! List of bonded interactions to potentially add.
219     std::array<BondedInteractionList, ebtsNR> rb;
220     //! Number of atoms to modify
221     int nhack() const { return hack.size(); }
222 };
223
224 /*!\brief
225  * Reset modification block.
226  *
227  * \param[inout] globalPatches Block to reset.
228  * \todo Remove once constructor/destructor takes care of all of this.
229  */
230 void clearModificationBlock(MoleculePatchDatabase* globalPatches);
231
232 /*!\brief
233  * Copy residue information.
234  *
235  * \param[in] s Source information.
236  * \param[in] d Destination to copy to.
237  * \param[inout] symtab Symbol table for names.
238  * \todo Remove once copy can be done directly.
239  */
240 void copyPreprocessResidues(const PreprocessResidue& s, PreprocessResidue* d, t_symtab* symtab);
241
242 /*! \brief
243  * Add bond information in \p s to \p d.
244  *
245  * \param[in] s Source information to copy.
246  * \param[inout] d Destination to copy to.
247  * \param[in] bMin don't copy bondeds with atoms starting with '-'.
248  * \param[in] bPlus don't copy bondeds with atoms starting with '+'.
249  * \returns if bonds were removed at the termini.
250  */
251 bool mergeBondedInteractionList(gmx::ArrayRef<const BondedInteractionList> s,
252                                 gmx::ArrayRef<BondedInteractionList>       d,
253                                 bool                                       bMin,
254                                 bool                                       bPlus);
255
256 /*! \brief
257  * Copy all information from datastructure.
258  *
259  * \param[in] s Source information.
260  * \param[inout] d Destination to copy to.
261  */
262 void copyModificationBlocks(const MoleculePatchDatabase& s, MoleculePatchDatabase* d);
263
264 /*!\brief
265  * Add the individual modifications in \p s to \p d.
266  *
267  * \param[in] s Source information.
268  * \param[inout] d Destination to copy to.
269  */
270 void mergeAtomModifications(const MoleculePatchDatabase& s, MoleculePatchDatabase* d);
271
272 //! \copydoc mergeAtomModifications
273 void mergeAtomAndBondModifications(const MoleculePatchDatabase& s, MoleculePatchDatabase* d);
274
275 #endif