Avoid numerical overflow with overlapping atoms
[gromacs.git] / src / gromacs / mdlib / main.cpp
blob737a589737555d09232db8c010330e83def0ffa6
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) 2013,2014,2015,2016, 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.
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.
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.
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.
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.
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.
37 #include "gmxpre.h"
39 #include "main.h"
41 #include "config.h"
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
47 #include "gromacs/commandline/filenm.h"
48 #include "gromacs/fileio/gmxfio.h"
49 #include "gromacs/gmxlib/network.h"
50 #include "gromacs/mdtypes/commrec.h"
51 #include "gromacs/utility/binaryinformation.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/futil.h"
56 #include "gromacs/utility/gmxmpi.h"
57 #include "gromacs/utility/programcontext.h"
58 #include "gromacs/utility/smalloc.h"
59 #include "gromacs/utility/snprintf.h"
60 #include "gromacs/utility/sysinfo.h"
62 /* The source code in this file should be thread-safe.
63 Please keep it that way. */
65 #define BUFSIZE 1024
67 static void par_fn(char *base, int ftp, const t_commrec *cr,
68 gmx_bool bAppendSimId, gmx_bool bAppendNodeId,
69 char buf[], int bufsize)
71 if (static_cast<std::size_t>(bufsize) < (std::strlen(base)+10))
73 gmx_mem("Character buffer too small!");
76 /* Copy to buf, and strip extension */
77 std::strcpy(buf, base);
78 buf[strlen(base) - std::strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
80 if (bAppendSimId)
82 sprintf(buf+strlen(buf), "%d", cr->ms->sim);
84 if (bAppendNodeId)
86 std::strcat(buf, "_rank");
87 sprintf(buf+strlen(buf), "%d", cr->nodeid);
89 std::strcat(buf, ".");
91 /* Add extension again */
92 std::strcat(buf, (ftp == efTPR) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
93 if (debug)
95 fprintf(debug, "rank %d par_fn '%s'\n", cr->nodeid, buf);
96 if (fn2ftp(buf) == efLOG)
98 fprintf(debug, "log\n");
103 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
104 const char *name,
105 gmx_bool bQuiet)
107 int *ibuf, p;
108 gmx_bool bCompatible;
110 if (NULL != log && !bQuiet)
112 fprintf(log, "Multi-checking %s ... ", name);
115 if (ms == NULL)
117 gmx_fatal(FARGS,
118 "check_multi_int called with a NULL communication pointer");
121 snew(ibuf, ms->nsim);
122 ibuf[ms->sim] = val;
123 gmx_sumi_sim(ms->nsim, ibuf, ms);
125 bCompatible = TRUE;
126 for (p = 1; p < ms->nsim; p++)
128 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
131 if (bCompatible)
133 if (NULL != log && !bQuiet)
135 fprintf(log, "OK\n");
138 else
140 if (NULL != log)
142 fprintf(log, "\n%s is not equal for all subsystems\n", name);
143 for (p = 0; p < ms->nsim; p++)
145 fprintf(log, " subsystem %d: %d\n", p, ibuf[p]);
148 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
151 sfree(ibuf);
154 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
155 gmx_int64_t val, const char *name,
156 gmx_bool bQuiet)
158 gmx_int64_t *ibuf;
159 int p;
160 gmx_bool bCompatible;
162 if (NULL != log && !bQuiet)
164 fprintf(log, "Multi-checking %s ... ", name);
167 if (ms == NULL)
169 gmx_fatal(FARGS,
170 "check_multi_int called with a NULL communication pointer");
173 snew(ibuf, ms->nsim);
174 ibuf[ms->sim] = val;
175 gmx_sumli_sim(ms->nsim, ibuf, ms);
177 bCompatible = TRUE;
178 for (p = 1; p < ms->nsim; p++)
180 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
183 if (bCompatible)
185 if (NULL != log && !bQuiet)
187 fprintf(log, "OK\n");
190 else
192 if (NULL != log)
194 fprintf(log, "\n%s is not equal for all subsystems\n", name);
195 for (p = 0; p < ms->nsim; p++)
197 char strbuf[255];
198 /* first make the format string */
199 snprintf(strbuf, 255, " subsystem %%d: %s\n",
200 "%" GMX_PRId64);
201 fprintf(log, strbuf, p, ibuf[p]);
204 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
207 sfree(ibuf);
211 void gmx_log_open(const char *lognm, const t_commrec *cr,
212 gmx_bool bAppendFiles, FILE** fplog)
214 int pid;
215 char host[256];
216 char timebuf[STRLEN];
217 FILE *fp = *fplog;
219 if (!bAppendFiles)
221 fp = gmx_fio_fopen(lognm, bAppendFiles ? "a+" : "w+" );
224 gmx_fatal_set_log_file(fp);
226 /* Get some machine parameters */
227 gmx_gethostname(host, 256);
228 pid = gmx_getpid();
229 gmx_format_current_time(timebuf, STRLEN);
231 if (bAppendFiles)
233 fprintf(fp,
234 "\n"
235 "\n"
236 "-----------------------------------------------------------\n"
237 "Restarting from checkpoint, appending to previous log file.\n"
238 "\n"
242 fprintf(fp,
243 "Log file opened on %s"
244 "Host: %s pid: %d rank ID: %d number of ranks: %d\n",
245 timebuf, host, pid, cr->nodeid, cr->nnodes);
248 gmx::BinaryInformationSettings settings;
249 settings.extendedInfo(true);
250 settings.copyright(!bAppendFiles);
251 gmx::printBinaryInformation(fp, gmx::getProgramContext(), settings);
253 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
254 fprintf(fp, "\n");
256 fflush(fp);
258 *fplog = fp;
261 void gmx_log_close(FILE *fp)
263 if (fp)
265 gmx_fatal_set_log_file(NULL);
266 gmx_fio_fclose(fp);
270 void init_multisystem(t_commrec *cr, int nsim, char **multidirs,
271 int nfile, const t_filenm fnm[], gmx_bool bParFn)
273 gmx_multisim_t *ms;
274 int nnodes, nnodpersim, sim, i, ftp;
275 char buf[256];
276 #if GMX_MPI
277 MPI_Group mpi_group_world;
278 int *rank;
279 #endif
281 #if !GMX_MPI
282 if (nsim > 1)
284 gmx_fatal(FARGS, "This binary is compiled without MPI support, can not do multiple simulations.");
286 #endif
288 nnodes = cr->nnodes;
289 if (nnodes % nsim != 0)
291 gmx_fatal(FARGS, "The number of ranks (%d) is not a multiple of the number of simulations (%d)", nnodes, nsim);
294 nnodpersim = nnodes/nsim;
295 sim = cr->nodeid/nnodpersim;
297 if (debug)
299 fprintf(debug, "We have %d simulations, %d ranks per simulation, local simulation is %d\n", nsim, nnodpersim, sim);
302 snew(ms, 1);
303 cr->ms = ms;
304 ms->nsim = nsim;
305 ms->sim = sim;
306 #if GMX_MPI
307 /* Create a communicator for the master nodes */
308 snew(rank, ms->nsim);
309 for (i = 0; i < ms->nsim; i++)
311 rank[i] = i*nnodpersim;
313 MPI_Comm_group(MPI_COMM_WORLD, &mpi_group_world);
314 MPI_Group_incl(mpi_group_world, nsim, rank, &ms->mpi_group_masters);
315 sfree(rank);
316 MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
317 &ms->mpi_comm_masters);
319 #if !MPI_IN_PLACE_EXISTS
320 /* initialize the MPI_IN_PLACE replacement buffers */
321 snew(ms->mpb, 1);
322 ms->mpb->ibuf = NULL;
323 ms->mpb->libuf = NULL;
324 ms->mpb->fbuf = NULL;
325 ms->mpb->dbuf = NULL;
326 ms->mpb->ibuf_alloc = 0;
327 ms->mpb->libuf_alloc = 0;
328 ms->mpb->fbuf_alloc = 0;
329 ms->mpb->dbuf_alloc = 0;
330 #endif
332 #endif
334 /* Reduce the intra-simulation communication */
335 cr->sim_nodeid = cr->nodeid % nnodpersim;
336 cr->nnodes = nnodpersim;
337 #if GMX_MPI
338 MPI_Comm_split(MPI_COMM_WORLD, sim, cr->sim_nodeid, &cr->mpi_comm_mysim);
339 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
340 cr->nodeid = cr->sim_nodeid;
341 #endif
343 if (debug)
345 fprintf(debug, "This is simulation %d", cr->ms->sim);
346 if (PAR(cr))
348 fprintf(debug, ", local number of ranks %d, local rank ID %d",
349 cr->nnodes, cr->sim_nodeid);
351 fprintf(debug, "\n\n");
354 if (multidirs)
356 if (debug)
358 fprintf(debug, "Changing to directory %s\n", multidirs[cr->ms->sim]);
360 gmx_chdir(multidirs[cr->ms->sim]);
362 else if (bParFn)
364 /* Patch output and tpx, cpt and rerun input file names */
365 for (i = 0; (i < nfile); i++)
367 /* Because of possible multiple extensions per type we must look
368 * at the actual file name
370 if (is_output(&fnm[i]) ||
371 fnm[i].ftp == efTPR || fnm[i].ftp == efCPT ||
372 strcmp(fnm[i].opt, "-rerun") == 0)
374 ftp = fn2ftp(fnm[i].fns[0]);
375 par_fn(fnm[i].fns[0], ftp, cr, TRUE, FALSE, buf, 255);
376 sfree(fnm[i].fns[0]);
377 fnm[i].fns[0] = gmx_strdup(buf);