Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / gmxana / nrama.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 #include "gmxpre.h"
39
40 #include "nrama.h"
41
42 #include <cstdlib>
43 #include <cstring>
44
45 #include <algorithm>
46
47 #include "gromacs/listed_forces/bonded.h"
48 #include "gromacs/pbcutil/rmpbc.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/smalloc.h"
53
54 static const char* pp_pat[] = { "C", "N", "CA", "C", "N" };
55 #define NPP (sizeof(pp_pat) / sizeof(pp_pat[0]))
56
57 static bool d_comp(const t_dih& a, const t_dih& b)
58 {
59     if (a.ai[1] < b.ai[1])
60     {
61         return true;
62     }
63     else if (a.ai[1] == b.ai[1])
64     {
65         return a.ai[2] < b.ai[2];
66     }
67     else
68     {
69         return false;
70     }
71 }
72
73
74 static void calc_dihs(t_xrama* xr)
75 {
76     int         i, t1, t2, t3;
77     rvec        r_ij, r_kj, r_kl, m, n;
78     t_dih*      dd;
79     gmx_rmpbc_t gpbc = nullptr;
80
81     gpbc = gmx_rmpbc_init(xr->idef, xr->pbcType, xr->natoms);
82     gmx_rmpbc(gpbc, xr->natoms, xr->box, xr->x);
83     gmx_rmpbc_done(gpbc);
84
85     for (i = 0; (i < xr->ndih); i++)
86     {
87         dd      = &(xr->dih[i]);
88         dd->ang = dih_angle(xr->x[dd->ai[0]],
89                             xr->x[dd->ai[1]],
90                             xr->x[dd->ai[2]],
91                             xr->x[dd->ai[3]],
92                             nullptr,
93                             r_ij,
94                             r_kj,
95                             r_kl,
96                             m,
97                             n,
98                             &t1,
99                             &t2,
100                             &t3);
101     }
102 }
103
104 gmx_bool new_data(t_xrama* xr)
105 {
106     if (!read_next_x(xr->oenv, xr->traj, &xr->t, xr->x, xr->box))
107     {
108         return FALSE;
109     }
110
111     calc_dihs(xr);
112
113     return TRUE;
114 }
115
116 static int find_atom(const char* find, char*** names, int start, int nr)
117 {
118     int i;
119
120     for (i = start; (i < nr); i++)
121     {
122         if (std::strcmp(find, *names[i]) == 0)
123         {
124             return i;
125         }
126     }
127     return -1;
128 }
129
130 static void add_xr(t_xrama* xr, const int ff[5], const t_atoms* atoms)
131 {
132     char buf[12];
133     int  i;
134
135     srenew(xr->dih, xr->ndih + 2LL);
136     for (i = 0; (i < 4); i++)
137     {
138         xr->dih[xr->ndih].ai[i] = ff[i];
139     }
140     for (i = 0; (i < 4); i++)
141     {
142         xr->dih[xr->ndih + 1].ai[i] = ff[i + 1];
143     }
144     xr->ndih += 2;
145
146     srenew(xr->pp, xr->npp + 1LL);
147     xr->pp[xr->npp].iphi  = xr->ndih - 2;
148     xr->pp[xr->npp].ipsi  = xr->ndih - 1;
149     xr->pp[xr->npp].bShow = FALSE;
150     sprintf(buf,
151             "%s-%d",
152             *atoms->resinfo[atoms->atom[ff[1]].resind].name,
153             atoms->resinfo[atoms->atom[ff[1]].resind].nr);
154     xr->pp[xr->npp].label = gmx_strdup(buf);
155     xr->npp++;
156 }
157
158 static void get_dih(t_xrama* xr, const t_atoms* atoms)
159 {
160     int    found, ff[NPP];
161     int    i;
162     size_t j;
163
164     for (i = 0; (i < atoms->nr);)
165     {
166         found = i;
167         for (j = 0; (j < NPP); j++)
168         {
169             if ((ff[j] = find_atom(pp_pat[j], atoms->atomname, found, atoms->nr)) == -1)
170             {
171                 break;
172             }
173             found = ff[j] + 1;
174         }
175         if (j != NPP)
176         {
177             break;
178         }
179         add_xr(xr, ff, atoms);
180         i = ff[0] + 1;
181     }
182     fprintf(stderr, "Found %d phi-psi combinations\n", xr->npp);
183 }
184
185 static void min_max(t_xrama* xr)
186 {
187     int ai, i, j;
188
189     xr->amin = xr->natoms;
190     xr->amax = 0;
191     for (i = 0; (i < xr->ndih); i++)
192     {
193         for (j = 0; (j < 4); j++)
194         {
195             MSVC_DIAGNOSTIC_IGNORE(28182) // false positive in 2019 (16.5.4)
196             ai = xr->dih[i].ai[j];
197             MSVC_DIAGNOSTIC_RESET
198             if (ai < xr->amin)
199             {
200                 xr->amin = ai;
201             }
202             else if (ai > xr->amax)
203             {
204                 xr->amax = ai;
205             }
206         }
207     }
208 }
209
210 static void get_dih_props(t_xrama* xr, const t_idef* idef, int mult)
211 {
212     int      i, ft, ftype, nra;
213     t_iatom* ia;
214     t_dih *  dd, key;
215
216     ia = idef->il[F_PDIHS].iatoms;
217     for (i = 0; (i < idef->il[F_PDIHS].nr);)
218     {
219         ft    = ia[0];
220         ftype = idef->functype[ft];
221         nra   = interaction_function[ftype].nratoms;
222
223         if (ftype != F_PDIHS)
224         {
225             gmx_incons("ftype is not a dihedral");
226         }
227
228         key.ai[1] = ia[2];
229         key.ai[2] = ia[3];
230         dd        = std::lower_bound(xr->dih, xr->dih + xr->ndih, key, d_comp);
231         if (dd < xr->dih + xr->ndih && !d_comp(key, *dd))
232         {
233             dd->mult = idef->iparams[ft].pdihs.mult;
234             dd->phi0 = idef->iparams[ft].pdihs.phiA;
235         }
236
237         i += nra + 1;
238         ia += nra + 1LL;
239     }
240     /* Fill in defaults for values not in the topology */
241     for (i = 0; (i < xr->ndih); i++)
242     {
243         if (xr->dih[i].mult == 0)
244         {
245             fprintf(stderr,
246                     "Dihedral around %d,%d not found in topology. Using mult=%d\n",
247                     xr->dih[i].ai[1],
248                     xr->dih[i].ai[2],
249                     mult);
250             xr->dih[i].mult = mult;
251             xr->dih[i].phi0 = 180;
252         }
253     }
254 }
255
256
257 t_topology* init_rama(gmx_output_env_t* oenv, const char* infile, const char* topfile, t_xrama* xr, int mult)
258 {
259     t_topology* top;
260     real        t;
261
262     top = read_top(topfile, &xr->pbcType);
263
264     /*get_dih2(xr,top->idef.functype,&(top->idef.bondeds),&(top->atoms));*/
265     get_dih(xr, &(top->atoms));
266     get_dih_props(xr, &(top->idef), mult);
267     xr->natoms = read_first_x(oenv, &xr->traj, infile, &t, &(xr->x), xr->box);
268     xr->idef   = &(top->idef);
269     xr->oenv   = oenv;
270
271     min_max(xr);
272     calc_dihs(xr);
273
274     return top;
275 }