Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / mdlib / splitter.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,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.
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 "splitter.h"
41
42 #include <cstdlib>
43 #include <cstring>
44
45 #include <algorithm>
46
47 #include "gromacs/pbcutil/mshift.h"
48 #include "gromacs/topology/block.h"
49 #include "gromacs/topology/idef.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/gmxassert.h"
52 #include "gromacs/utility/smalloc.h"
53
54 typedef struct
55 {
56     int atom, sid;
57 } t_sid;
58
59 static bool sid_comp(const t_sid& sa, const t_sid& sb)
60 {
61     if (sa.sid == sb.sid)
62     {
63         return sa.atom < sb.atom;
64     }
65     else
66     {
67         return sa.sid < sb.sid;
68     }
69 }
70
71 static int mk_grey(gmx::ArrayRef<egCol> edgeColor, const t_graph* g, int* AtomI, int maxsid, t_sid sid[])
72 {
73     int ng, ai, g0;
74
75     ng = 0;
76     ai = *AtomI;
77
78     g0 = g->edgeAtomBegin;
79     /* Loop over all the bonds */
80     for (int aj : g->edges[ai])
81     {
82         aj -= g0;
83         /* If there is a white one, make it gray and set pbc */
84         if (edgeColor[aj] == egcolWhite)
85         {
86             if (aj < *AtomI)
87             {
88                 *AtomI = aj;
89             }
90             edgeColor[aj] = egcolGrey;
91
92             /* Check whether this one has been set before... */
93             range_check(aj + g0, 0, maxsid);
94             range_check(ai + g0, 0, maxsid);
95             if (sid[aj + g0].sid != -1)
96             {
97                 gmx_fatal(FARGS,
98                           "sid[%d]=%d, sid[%d]=%d, file %s, line %d",
99                           ai,
100                           sid[ai + g0].sid,
101                           aj,
102                           sid[aj + g0].sid,
103                           __FILE__,
104                           __LINE__);
105             }
106             else
107             {
108                 sid[aj + g0].sid  = sid[ai + g0].sid;
109                 sid[aj + g0].atom = aj + g0;
110             }
111             ng++;
112         }
113     }
114     return ng;
115 }
116
117 static int first_colour(const int fC, const egCol Col, const t_graph* g, gmx::ArrayRef<const egCol> edgeColor)
118 /* Return the first node with colour Col starting at fC.
119  * return -1 if none found.
120  */
121 {
122     int i;
123
124     for (i = fC; i < int(g->edges.size()); i++)
125     {
126         if (!g->edges[i].empty() && edgeColor[i] == Col)
127         {
128             return i;
129         }
130     }
131
132     return -1;
133 }
134
135 static int mk_sblocks(FILE* fp, t_graph* g, int maxsid, t_sid sid[])
136 {
137     int ng;
138     int nW, nG, nB; /* Number of Grey, Black, White     */
139     int fW, fG;     /* First of each category   */
140     int g0, nblock;
141
142     if (!g->numConnectedAtoms)
143     {
144         return 0;
145     }
146
147     nblock = 0;
148
149     std::vector<egCol> edgeColor(g->edges.size(), egcolWhite);
150
151     g0 = g->edgeAtomBegin;
152     nW = g->numConnectedAtoms;
153     nG = 0;
154     nB = 0;
155
156     fW = 0;
157
158     /* We even have a loop invariant:
159      * nW+nG+nB == g->nbound
160      */
161
162     if (fp)
163     {
164         fprintf(fp, "Walking down the molecule graph to make constraint-blocks\n");
165     }
166
167     while (nW > 0)
168     {
169         /* Find the first white, this will allways be a larger
170          * number than before, because no nodes are made white
171          * in the loop
172          */
173         if ((fW = first_colour(fW, egcolWhite, g, edgeColor)) == -1)
174         {
175             gmx_fatal(FARGS, "No WHITE nodes found while nW=%d\n", nW);
176         }
177
178         /* Make the first white node grey, and set the block number */
179         edgeColor[fW] = egcolGrey;
180         range_check(fW + g0, 0, maxsid);
181         sid[fW + g0].sid = nblock++;
182         nG++;
183         nW--;
184
185         /* Initial value for the first grey */
186         fG = fW;
187
188         if (debug)
189         {
190             fprintf(debug, "Starting G loop (nW=%d, nG=%d, nB=%d, total %d)\n", nW, nG, nB, nW + nG + nB);
191         }
192
193         while (nG > 0)
194         {
195             if ((fG = first_colour(fG, egcolGrey, g, edgeColor)) == -1)
196             {
197                 gmx_fatal(FARGS, "No GREY nodes found while nG=%d\n", nG);
198             }
199
200             /* Make the first grey node black */
201             edgeColor[fG] = egcolBlack;
202             nB++;
203             nG--;
204
205             /* Make all the neighbours of this black node grey
206              * and set their block number
207              */
208             ng = mk_grey(edgeColor, g, &fG, maxsid, sid);
209             /* ng is the number of white nodes made grey */
210             nG += ng;
211             nW -= ng;
212         }
213     }
214
215     if (debug)
216     {
217         fprintf(debug, "Found %d shake blocks\n", nblock);
218     }
219
220     return nblock;
221 }
222
223
224 typedef struct
225 {
226     int first, last, sid;
227 } t_merge_sid;
228
229 static int ms_comp(const void* a, const void* b)
230 {
231     const t_merge_sid* ma = reinterpret_cast<const t_merge_sid*>(a);
232     const t_merge_sid* mb = reinterpret_cast<const t_merge_sid*>(b);
233     int                d;
234
235     d = ma->first - mb->first;
236     if (d == 0)
237     {
238         return ma->last - mb->last;
239     }
240     else
241     {
242         return d;
243     }
244 }
245
246 static int merge_sid(int at_start, int at_end, int nsid, t_sid sid[], t_blocka* sblock)
247 {
248     int          i, j, k, n, isid, ndel;
249     t_merge_sid* ms;
250
251     /* We try to remdy the following problem:
252      * Atom: 1  2  3  4  5 6 7 8 9 10
253      * Sid:  0 -1  1  0 -1 1 1 1 1 1
254      */
255
256     /* Determine first and last atom in each shake ID */
257     snew(ms, nsid);
258
259     for (k = 0; (k < nsid); k++)
260     {
261         ms[k].first = at_end + 1;
262         ms[k].last  = -1;
263         ms[k].sid   = k;
264     }
265     for (i = at_start; (i < at_end); i++)
266     {
267         isid = sid[i].sid;
268         range_check(isid, -1, nsid);
269         if (isid >= 0)
270         {
271             ms[isid].first = std::min(ms[isid].first, sid[i].atom);
272             ms[isid].last  = std::max(ms[isid].last, sid[i].atom);
273         }
274     }
275     qsort(ms, nsid, sizeof(ms[0]), ms_comp);
276
277     /* Now merge the overlapping ones */
278     ndel = 0;
279     for (k = 0; (k < nsid);)
280     {
281         for (j = k + 1; (j < nsid);)
282         {
283             if (ms[j].first <= ms[k].last)
284             {
285                 ms[k].last  = std::max(ms[k].last, ms[j].last);
286                 ms[k].first = std::min(ms[k].first, ms[j].first);
287                 ms[j].sid   = -1;
288                 ndel++;
289                 j++;
290             }
291             else
292             {
293                 k = j;
294                 j = k + 1;
295             }
296         }
297         if (j == nsid)
298         {
299             k++;
300         }
301     }
302     for (k = 0; (k < nsid); k++)
303     {
304         while ((k < nsid - 1) && (ms[k].sid == -1))
305         {
306             for (j = k + 1; (j < nsid); j++)
307             {
308                 std::memcpy(&(ms[j - 1]), &(ms[j]), sizeof(ms[0]));
309             }
310             nsid--;
311         }
312     }
313
314     for (k = at_start; (k < at_end); k++)
315     {
316         sid[k].atom = k;
317         sid[k].sid  = -1;
318     }
319     sblock->nr           = nsid;
320     sblock->nalloc_index = sblock->nr + 1;
321     snew(sblock->index, sblock->nalloc_index);
322     sblock->nra      = at_end - at_start;
323     sblock->nalloc_a = sblock->nra;
324     snew(sblock->a, sblock->nalloc_a);
325     sblock->index[0] = 0;
326     for (k = n = 0; (k < nsid); k++)
327     {
328         sblock->index[k + 1] = sblock->index[k] + ms[k].last - ms[k].first + 1;
329         for (j = ms[k].first; (j <= ms[k].last); j++)
330         {
331             range_check(n, 0, sblock->nra);
332             sblock->a[n++] = j;
333             range_check(j, 0, at_end);
334             if (sid[j].sid == -1)
335             {
336                 sid[j].sid = k;
337             }
338             else
339             {
340                 fprintf(stderr, "Double sids (%d, %d) for atom %d\n", sid[j].sid, k, j);
341             }
342         }
343     }
344
345     sblock->nra = n;
346     GMX_RELEASE_ASSERT(sblock->index[k] == sblock->nra, "Internal inconsistency; sid merge failed");
347
348     sfree(ms);
349
350     return nsid;
351 }
352
353 void gen_sblocks(FILE* fp, int at_end, const InteractionDefinitions& idef, t_blocka* sblock, gmx_bool bSettle)
354 {
355     t_graph* g;
356     int      i, i0;
357     t_sid*   sid;
358     int      nsid;
359
360     g = mk_graph(nullptr, idef, at_end, TRUE, bSettle);
361     if (debug)
362     {
363         p_graph(debug, "Graaf Dracula", g);
364     }
365     snew(sid, at_end);
366     for (i = 0; i < at_end; i++)
367     {
368         sid[i].atom = i;
369         sid[i].sid  = -1;
370     }
371     nsid = mk_sblocks(fp, g, at_end, sid);
372
373     if (!nsid)
374     {
375         return;
376     }
377
378     /* Now sort the shake blocks... */
379     std::sort(sid, sid + at_end, sid_comp);
380
381     if (debug)
382     {
383         fprintf(debug, "Sorted shake block\n");
384         for (i = 0; i < at_end; i++)
385         {
386             fprintf(debug, "sid[%5d] = atom:%5d sid:%5d\n", i, sid[i].atom, sid[i].sid);
387         }
388     }
389     /* Now check how many are NOT -1, i.e. how many have to be shaken */
390     for (i0 = 0; i0 < at_end; i0++)
391     {
392         if (sid[i0].sid > -1)
393         {
394             break;
395         }
396     }
397
398     /* Now we have the sids that have to be shaken. We'll check the min and
399      * max atom numbers and this determines the shake block. DvdS 2007-07-19.
400      * For the purpose of making boundaries all atoms in between need to be
401      * part of the shake block too. There may be cases where blocks overlap
402      * and they will have to be merged.
403      */
404     merge_sid(0, at_end, nsid, sid, sblock);
405     sfree(sid);
406     /* Due to unknown reason this free generates a problem sometimes */
407     done_graph(g);
408     if (debug)
409     {
410         fprintf(debug, "Done gen_sblocks\n");
411     }
412 }