Merged legacyheaders/types/inputrec.h into mdtypes/inputrec.h
[gromacs.git] / src / gromacs / fileio / checkpoint.cpp
blob3a9accedf64cbb44730b1d0fb15aaf21a0af6768
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2008,2009,2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 /* The source code in this file should be thread-safe.
37 Please keep it that way. */
38 #include "gmxpre.h"
40 #include "checkpoint.h"
42 #include "config.h"
44 #include <cerrno>
45 #include <cstdlib>
46 #include <cstring>
48 #include <fcntl.h>
49 #ifdef GMX_NATIVE_WINDOWS
50 #include <io.h>
51 #include <sys/locking.h>
52 #endif
54 #include "buildinfo.h"
55 #include "gromacs/fileio/filenm.h"
56 #include "gromacs/fileio/gmxfio.h"
57 #include "gromacs/fileio/gmxfio-xdr.h"
58 #include "gromacs/fileio/trx.h"
59 #include "gromacs/fileio/txtdump.h"
60 #include "gromacs/fileio/xdr_datatype.h"
61 #include "gromacs/fileio/xdrf.h"
62 #include "gromacs/gmxlib/network.h"
63 #include "gromacs/legacyheaders/names.h"
64 #include "gromacs/legacyheaders/types/commrec.h"
65 #include "gromacs/legacyheaders/types/enums.h"
66 #include "gromacs/math/vec.h"
67 #include "gromacs/mdtypes/df_history.h"
68 #include "gromacs/mdtypes/energyhistory.h"
69 #include "gromacs/mdtypes/inputrec.h"
70 #include "gromacs/mdtypes/state.h"
71 #include "gromacs/utility/baseversion.h"
72 #include "gromacs/utility/cstringutil.h"
73 #include "gromacs/utility/fatalerror.h"
74 #include "gromacs/utility/futil.h"
75 #include "gromacs/utility/int64_to_int.h"
76 #include "gromacs/utility/programcontext.h"
77 #include "gromacs/utility/smalloc.h"
78 #include "gromacs/utility/sysinfo.h"
80 #ifdef GMX_FAHCORE
81 #include "corewrap.h"
82 #endif
84 #define CPT_MAGIC1 171817
85 #define CPT_MAGIC2 171819
86 #define CPTSTRLEN 1024
88 #ifdef GMX_DOUBLE
89 #define GMX_CPT_BUILD_DP 1
90 #else
91 #define GMX_CPT_BUILD_DP 0
92 #endif
94 /* cpt_version should normally only be changed
95 * when the header of footer format changes.
96 * The state data format itself is backward and forward compatible.
97 * But old code can not read a new entry that is present in the file
98 * (but can read a new format when new entries are not present).
100 static const int cpt_version = 16;
103 const char *est_names[estNR] =
105 "FE-lambda",
106 "box", "box-rel", "box-v", "pres_prev",
107 "nosehoover-xi", "thermostat-integral",
108 "x", "v", "SDx", "CGp", "LD-rng", "LD-rng-i",
109 "disre_initf", "disre_rm3tav",
110 "orire_initf", "orire_Dtav",
111 "svir_prev", "nosehoover-vxi", "v_eta", "vol0", "nhpres_xi", "nhpres_vxi", "fvir_prev", "fep_state", "MC-rng", "MC-rng-i"
114 enum {
115 eeksEKIN_N, eeksEKINH, eeksDEKINDL, eeksMVCOS, eeksEKINF, eeksEKINO, eeksEKINSCALEF, eeksEKINSCALEH, eeksVSCALE, eeksEKINTOTAL, eeksNR
118 const char *eeks_names[eeksNR] =
120 "Ekin_n", "Ekinh", "dEkindlambda", "mv_cos",
121 "Ekinf", "Ekinh_old", "EkinScaleF_NHC", "EkinScaleH_NHC", "Vscale_NHC", "Ekin_Total"
124 enum {
125 eenhENERGY_N, eenhENERGY_AVER, eenhENERGY_SUM, eenhENERGY_NSUM,
126 eenhENERGY_SUM_SIM, eenhENERGY_NSUM_SIM,
127 eenhENERGY_NSTEPS, eenhENERGY_NSTEPS_SIM,
128 eenhENERGY_DELTA_H_NN,
129 eenhENERGY_DELTA_H_LIST,
130 eenhENERGY_DELTA_H_STARTTIME,
131 eenhENERGY_DELTA_H_STARTLAMBDA,
132 eenhNR
135 const char *eenh_names[eenhNR] =
137 "energy_n", "energy_aver", "energy_sum", "energy_nsum",
138 "energy_sum_sim", "energy_nsum_sim",
139 "energy_nsteps", "energy_nsteps_sim",
140 "energy_delta_h_nn",
141 "energy_delta_h_list",
142 "energy_delta_h_start_time",
143 "energy_delta_h_start_lambda"
146 /* free energy history variables -- need to be preserved over checkpoint */
147 enum {
148 edfhBEQUIL, edfhNATLAMBDA, edfhWLHISTO, edfhWLDELTA, edfhSUMWEIGHTS, edfhSUMDG, edfhSUMMINVAR, edfhSUMVAR,
149 edfhACCUMP, edfhACCUMM, edfhACCUMP2, edfhACCUMM2, edfhTIJ, edfhTIJEMP, edfhNR
151 /* free energy history variable names */
152 const char *edfh_names[edfhNR] =
154 "bEquilibrated", "N_at_state", "Wang-Landau Histogram", "Wang-Landau Delta", "Weights", "Free Energies", "minvar", "variance",
155 "accumulated_plus", "accumulated_minus", "accumulated_plus_2", "accumulated_minus_2", "Tij", "Tij_empirical"
158 enum {
159 ecprREAL, ecprRVEC, ecprMATRIX
162 enum {
163 cptpEST, cptpEEKS, cptpEENH, cptpEDFH
165 /* enums for the different components of checkpoint variables, replacing the hard coded ones.
166 cptpEST - state variables.
167 cptpEEKS - Kinetic energy state variables.
168 cptpEENH - Energy history state variables.
169 cptpEDFH - free energy history variables.
173 static const char *st_names(int cptp, int ecpt)
175 switch (cptp)
177 case cptpEST: return est_names [ecpt]; break;
178 case cptpEEKS: return eeks_names[ecpt]; break;
179 case cptpEENH: return eenh_names[ecpt]; break;
180 case cptpEDFH: return edfh_names[ecpt]; break;
183 return NULL;
186 static void cp_warning(FILE *fp)
188 fprintf(fp, "\nWARNING: Checkpoint file is corrupted or truncated\n\n");
191 static void cp_error()
193 gmx_fatal(FARGS, "Checkpoint file corrupted/truncated, or maybe you are out of disk space?");
196 static void do_cpt_string_err(XDR *xd, gmx_bool bRead, const char *desc, char **s, FILE *list)
198 if (bRead)
200 snew(*s, CPTSTRLEN);
202 if (xdr_string(xd, s, CPTSTRLEN) == 0)
204 cp_error();
206 if (list)
208 fprintf(list, "%s = %s\n", desc, *s);
209 sfree(*s);
213 static int do_cpt_int(XDR *xd, const char *desc, int *i, FILE *list)
215 if (xdr_int(xd, i) == 0)
217 return -1;
219 if (list)
221 fprintf(list, "%s = %d\n", desc, *i);
223 return 0;
226 static int do_cpt_u_chars(XDR *xd, const char *desc, int n, unsigned char *i, FILE *list)
228 if (list)
230 fprintf(list, "%s = ", desc);
232 bool_t res = 1;
233 for (int j = 0; j < n && res; j++)
235 res &= xdr_u_char(xd, &i[j]);
236 if (list)
238 fprintf(list, "%02x", i[j]);
241 if (list)
243 fprintf(list, "\n");
245 if (res == 0)
247 return -1;
250 return 0;
253 static void do_cpt_int_err(XDR *xd, const char *desc, int *i, FILE *list)
255 if (do_cpt_int(xd, desc, i, list) < 0)
257 cp_error();
261 static void do_cpt_step_err(XDR *xd, const char *desc, gmx_int64_t *i, FILE *list)
263 char buf[STEPSTRSIZE];
265 if (xdr_int64(xd, i) == 0)
267 cp_error();
269 if (list)
271 fprintf(list, "%s = %s\n", desc, gmx_step_str(*i, buf));
275 static void do_cpt_double_err(XDR *xd, const char *desc, double *f, FILE *list)
277 if (xdr_double(xd, f) == 0)
279 cp_error();
281 if (list)
283 fprintf(list, "%s = %f\n", desc, *f);
287 static void do_cpt_real_err(XDR *xd, real *f)
289 #ifdef GMX_DOUBLE
290 bool_t res = xdr_double(xd, f);
291 #else
292 bool_t res = xdr_float(xd, f);
293 #endif
294 if (res == 0)
296 cp_error();
300 static void do_cpt_n_rvecs_err(XDR *xd, const char *desc, int n, rvec f[], FILE *list)
302 for (int i = 0; i < n; i++)
304 for (int j = 0; j < DIM; j++)
306 do_cpt_real_err(xd, &f[i][j]);
310 if (list)
312 pr_rvecs(list, 0, desc, f, n);
316 /* If nval >= 0, nval is used; on read this should match the passed value.
317 * If nval n<0, *nptr is used; on read the value is stored in nptr
319 static int do_cpte_reals_low(XDR *xd, int cptp, int ecpt, int sflags,
320 int nval, int *nptr, real **v,
321 FILE *list, int erealtype)
323 bool_t res = 0;
324 #ifndef GMX_DOUBLE
325 int dtc = xdr_datatype_float;
326 #else
327 int dtc = xdr_datatype_double;
328 #endif
329 real *vp, *va = NULL;
330 float *vf;
331 double *vd;
332 int nf, dt, i;
334 if (list == NULL)
336 if (nval >= 0)
338 nf = nval;
340 else
342 if (nptr == NULL)
344 gmx_incons("*ntpr=NULL in do_cpte_reals_low");
346 nf = *nptr;
349 res = xdr_int(xd, &nf);
350 if (res == 0)
352 return -1;
354 if (list == NULL)
356 if (nval >= 0)
358 if (nf != nval)
360 gmx_fatal(FARGS, "Count mismatch for state entry %s, code count is %d, file count is %d\n", st_names(cptp, ecpt), nval, nf);
363 else
365 *nptr = nf;
368 dt = dtc;
369 res = xdr_int(xd, &dt);
370 if (res == 0)
372 return -1;
374 if (dt != dtc)
376 fprintf(stderr, "Precision mismatch for state entry %s, code precision is %s, file precision is %s\n",
377 st_names(cptp, ecpt), xdr_datatype_names[dtc],
378 xdr_datatype_names[dt]);
380 if (list || !(sflags & (1<<ecpt)))
382 snew(va, nf);
383 vp = va;
385 else
387 if (*v == NULL)
389 snew(*v, nf);
391 vp = *v;
393 if (dt == xdr_datatype_float)
395 if (dtc == xdr_datatype_float)
397 vf = reinterpret_cast<float *>(vp);
399 else
401 snew(vf, nf);
403 res = xdr_vector(xd, reinterpret_cast<char *>(vf), nf,
404 static_cast<unsigned int>(sizeof(float)), (xdrproc_t)xdr_float);
405 if (res == 0)
407 return -1;
409 if (dtc != xdr_datatype_float)
411 for (i = 0; i < nf; i++)
413 vp[i] = vf[i];
415 sfree(vf);
418 else
420 if (dtc == xdr_datatype_double)
422 /* cppcheck-suppress invalidPointerCast
423 * Only executed if real is anyhow double */
424 vd = (double *)vp;
426 else
428 snew(vd, nf);
430 res = xdr_vector(xd, reinterpret_cast<char *>(vd), nf,
431 static_cast<unsigned int>(sizeof(double)), (xdrproc_t)xdr_double);
432 if (res == 0)
434 return -1;
436 if (dtc != xdr_datatype_double)
438 for (i = 0; i < nf; i++)
440 vp[i] = vd[i];
442 sfree(vd);
446 if (list)
448 switch (erealtype)
450 case ecprREAL:
451 pr_reals(list, 0, st_names(cptp, ecpt), vp, nf);
452 break;
453 case ecprRVEC:
454 pr_rvecs(list, 0, st_names(cptp, ecpt), (rvec *)vp, nf/3);
455 break;
456 default:
457 gmx_incons("Unknown checkpoint real type");
460 if (va)
462 sfree(va);
465 return 0;
469 /* This function stores n along with the reals for reading,
470 * but on reading it assumes that n matches the value in the checkpoint file,
471 * a fatal error is generated when this is not the case.
473 static int do_cpte_reals(XDR *xd, int cptp, int ecpt, int sflags,
474 int n, real **v, FILE *list)
476 return do_cpte_reals_low(xd, cptp, ecpt, sflags, n, NULL, v, list, ecprREAL);
479 /* This function does the same as do_cpte_reals,
480 * except that on reading it ignores the passed value of *n
481 * and stored the value read from the checkpoint file in *n.
483 static int do_cpte_n_reals(XDR *xd, int cptp, int ecpt, int sflags,
484 int *n, real **v, FILE *list)
486 return do_cpte_reals_low(xd, cptp, ecpt, sflags, -1, n, v, list, ecprREAL);
489 static int do_cpte_real(XDR *xd, int cptp, int ecpt, int sflags,
490 real *r, FILE *list)
492 return do_cpte_reals_low(xd, cptp, ecpt, sflags, 1, NULL, &r, list, ecprREAL);
495 static int do_cpte_ints(XDR *xd, int cptp, int ecpt, int sflags,
496 int n, int **v, FILE *list)
498 bool_t res = 0;
499 int dtc = xdr_datatype_int;
500 int *vp, *va = NULL;
501 int nf, dt;
503 nf = n;
504 res = xdr_int(xd, &nf);
505 if (res == 0)
507 return -1;
509 if (list == NULL && v != NULL && nf != n)
511 gmx_fatal(FARGS, "Count mismatch for state entry %s, code count is %d, file count is %d\n", st_names(cptp, ecpt), n, nf);
513 dt = dtc;
514 res = xdr_int(xd, &dt);
515 if (res == 0)
517 return -1;
519 if (dt != dtc)
521 gmx_fatal(FARGS, "Type mismatch for state entry %s, code type is %s, file type is %s\n",
522 st_names(cptp, ecpt), xdr_datatype_names[dtc],
523 xdr_datatype_names[dt]);
525 if (list || !(sflags & (1<<ecpt)) || v == NULL)
527 snew(va, nf);
528 vp = va;
530 else
532 if (*v == NULL)
534 snew(*v, nf);
536 vp = *v;
538 res = xdr_vector(xd, reinterpret_cast<char *>(vp), nf,
539 static_cast<unsigned int>(sizeof(int)), (xdrproc_t)xdr_int);
540 if (res == 0)
542 return -1;
544 if (list)
546 pr_ivec(list, 0, st_names(cptp, ecpt), vp, nf, TRUE);
548 if (va)
550 sfree(va);
553 return 0;
556 static int do_cpte_int(XDR *xd, int cptp, int ecpt, int sflags,
557 int *i, FILE *list)
559 return do_cpte_ints(xd, cptp, ecpt, sflags, 1, &i, list);
562 static int do_cpte_doubles(XDR *xd, int cptp, int ecpt, int sflags,
563 int n, double **v, FILE *list)
565 bool_t res = 0;
566 int dtc = xdr_datatype_double;
567 double *vp, *va = NULL;
568 int nf, dt;
570 nf = n;
571 res = xdr_int(xd, &nf);
572 if (res == 0)
574 return -1;
576 if (list == NULL && nf != n)
578 gmx_fatal(FARGS, "Count mismatch for state entry %s, code count is %d, file count is %d\n", st_names(cptp, ecpt), n, nf);
580 dt = dtc;
581 res = xdr_int(xd, &dt);
582 if (res == 0)
584 return -1;
586 if (dt != dtc)
588 gmx_fatal(FARGS, "Precision mismatch for state entry %s, code precision is %s, file precision is %s\n",
589 st_names(cptp, ecpt), xdr_datatype_names[dtc],
590 xdr_datatype_names[dt]);
592 if (list || !(sflags & (1<<ecpt)))
594 snew(va, nf);
595 vp = va;
597 else
599 if (*v == NULL)
601 snew(*v, nf);
603 vp = *v;
605 res = xdr_vector(xd, reinterpret_cast<char *>(vp), nf,
606 static_cast<unsigned int>(sizeof(double)), (xdrproc_t)xdr_double);
607 if (res == 0)
609 return -1;
611 if (list)
613 pr_doubles(list, 0, st_names(cptp, ecpt), vp, nf);
615 if (va)
617 sfree(va);
620 return 0;
623 static int do_cpte_double(XDR *xd, int cptp, int ecpt, int sflags,
624 double *r, FILE *list)
626 return do_cpte_doubles(xd, cptp, ecpt, sflags, 1, &r, list);
630 static int do_cpte_rvecs(XDR *xd, int cptp, int ecpt, int sflags,
631 int n, rvec **v, FILE *list)
633 return do_cpte_reals_low(xd, cptp, ecpt, sflags,
634 n*DIM, NULL, (real **)v, list, ecprRVEC);
637 static int do_cpte_matrix(XDR *xd, int cptp, int ecpt, int sflags,
638 matrix v, FILE *list)
640 real *vr;
641 int ret;
643 vr = &(v[0][0]);
644 ret = do_cpte_reals_low(xd, cptp, ecpt, sflags,
645 DIM*DIM, NULL, &vr, NULL, ecprMATRIX);
647 if (list && ret == 0)
649 pr_rvecs(list, 0, st_names(cptp, ecpt), v, DIM);
652 return ret;
656 static int do_cpte_nmatrix(XDR *xd, int cptp, int ecpt, int sflags,
657 int n, real **v, FILE *list)
659 int i;
660 int ret, reti;
661 char name[CPTSTRLEN];
663 ret = 0;
664 if (v == NULL)
666 snew(v, n);
668 for (i = 0; i < n; i++)
670 reti = do_cpte_reals_low(xd, cptp, ecpt, sflags, n, NULL, &(v[i]), NULL, ecprREAL);
671 if (list && reti == 0)
673 sprintf(name, "%s[%d]", st_names(cptp, ecpt), i);
674 pr_reals(list, 0, name, v[i], n);
676 if (reti != 0)
678 ret = reti;
681 return ret;
684 static int do_cpte_matrices(XDR *xd, int cptp, int ecpt, int sflags,
685 int n, matrix **v, FILE *list)
687 bool_t res = 0;
688 matrix *vp, *va = NULL;
689 real *vr;
690 int nf, i, j, k;
691 int ret;
693 nf = n;
694 res = xdr_int(xd, &nf);
695 if (res == 0)
697 return -1;
699 if (list == NULL && nf != n)
701 gmx_fatal(FARGS, "Count mismatch for state entry %s, code count is %d, file count is %d\n", st_names(cptp, ecpt), n, nf);
703 if (list || !(sflags & (1<<ecpt)))
705 snew(va, nf);
706 vp = va;
708 else
710 if (*v == NULL)
712 snew(*v, nf);
714 vp = *v;
716 snew(vr, nf*DIM*DIM);
717 for (i = 0; i < nf; i++)
719 for (j = 0; j < DIM; j++)
721 for (k = 0; k < DIM; k++)
723 vr[(i*DIM+j)*DIM+k] = vp[i][j][k];
727 ret = do_cpte_reals_low(xd, cptp, ecpt, sflags,
728 nf*DIM*DIM, NULL, &vr, NULL, ecprMATRIX);
729 for (i = 0; i < nf; i++)
731 for (j = 0; j < DIM; j++)
733 for (k = 0; k < DIM; k++)
735 vp[i][j][k] = vr[(i*DIM+j)*DIM+k];
739 sfree(vr);
741 if (list && ret == 0)
743 for (i = 0; i < nf; i++)
745 pr_rvecs(list, 0, st_names(cptp, ecpt), vp[i], DIM);
748 if (va)
750 sfree(va);
753 return ret;
756 static void do_cpt_header(XDR *xd, gmx_bool bRead, int *file_version,
757 char **version, char **btime, char **buser, char **bhost,
758 int *double_prec,
759 char **fprog, char **ftime,
760 int *eIntegrator, int *simulation_part,
761 gmx_int64_t *step, double *t,
762 int *nnodes, int *dd_nc, int *npme,
763 int *natoms, int *ngtc, int *nnhpres, int *nhchainlength,
764 int *nlambda, int *flags_state,
765 int *flags_eks, int *flags_enh, int *flags_dfh,
766 int *nED, int *eSwapCoords,
767 FILE *list)
769 bool_t res = 0;
770 int magic;
771 int idum = 0;
772 char *fhost;
774 if (bRead)
776 magic = -1;
778 else
780 magic = CPT_MAGIC1;
782 res = xdr_int(xd, &magic);
783 if (res == 0)
785 gmx_fatal(FARGS, "The checkpoint file is empty/corrupted, or maybe you are out of disk space?");
787 if (magic != CPT_MAGIC1)
789 gmx_fatal(FARGS, "Start of file magic number mismatch, checkpoint file has %d, should be %d\n"
790 "The checkpoint file is corrupted or not a checkpoint file",
791 magic, CPT_MAGIC1);
793 if (!bRead)
795 snew(fhost, 255);
796 gmx_gethostname(fhost, 255);
798 do_cpt_string_err(xd, bRead, "GROMACS version", version, list);
799 do_cpt_string_err(xd, bRead, "GROMACS build time", btime, list);
800 do_cpt_string_err(xd, bRead, "GROMACS build user", buser, list);
801 do_cpt_string_err(xd, bRead, "GROMACS build host", bhost, list);
802 do_cpt_string_err(xd, bRead, "generating program", fprog, list);
803 do_cpt_string_err(xd, bRead, "generation time", ftime, list);
804 *file_version = cpt_version;
805 do_cpt_int_err(xd, "checkpoint file version", file_version, list);
806 if (*file_version > cpt_version)
808 gmx_fatal(FARGS, "Attempting to read a checkpoint file of version %d with code of version %d\n", *file_version, cpt_version);
810 if (*file_version >= 13)
812 do_cpt_int_err(xd, "GROMACS double precision", double_prec, list);
814 else
816 *double_prec = -1;
818 if (*file_version >= 12)
820 do_cpt_string_err(xd, bRead, "generating host", &fhost, list);
821 if (list == NULL)
823 sfree(fhost);
826 do_cpt_int_err(xd, "#atoms", natoms, list);
827 do_cpt_int_err(xd, "#T-coupling groups", ngtc, list);
828 if (*file_version >= 10)
830 do_cpt_int_err(xd, "#Nose-Hoover T-chains", nhchainlength, list);
832 else
834 *nhchainlength = 1;
836 if (*file_version >= 11)
838 do_cpt_int_err(xd, "#Nose-Hoover T-chains for barostat ", nnhpres, list);
840 else
842 *nnhpres = 0;
844 if (*file_version >= 14)
846 do_cpt_int_err(xd, "# of total lambda states ", nlambda, list);
848 else
850 *nlambda = 0;
852 do_cpt_int_err(xd, "integrator", eIntegrator, list);
853 if (*file_version >= 3)
855 do_cpt_int_err(xd, "simulation part #", simulation_part, list);
857 else
859 *simulation_part = 1;
861 if (*file_version >= 5)
863 do_cpt_step_err(xd, "step", step, list);
865 else
867 do_cpt_int_err(xd, "step", &idum, list);
868 *step = idum;
870 do_cpt_double_err(xd, "t", t, list);
871 do_cpt_int_err(xd, "#PP-ranks", nnodes, list);
872 idum = 1;
873 do_cpt_int_err(xd, "dd_nc[x]", dd_nc ? &(dd_nc[0]) : &idum, list);
874 do_cpt_int_err(xd, "dd_nc[y]", dd_nc ? &(dd_nc[1]) : &idum, list);
875 do_cpt_int_err(xd, "dd_nc[z]", dd_nc ? &(dd_nc[2]) : &idum, list);
876 do_cpt_int_err(xd, "#PME-only ranks", npme, list);
877 do_cpt_int_err(xd, "state flags", flags_state, list);
878 if (*file_version >= 4)
880 do_cpt_int_err(xd, "ekin data flags", flags_eks, list);
881 do_cpt_int_err(xd, "energy history flags", flags_enh, list);
883 else
885 *flags_eks = 0;
886 *flags_enh = (*flags_state >> (estORIRE_DTAV+1));
887 *flags_state = (*flags_state & ~((1<<(estORIRE_DTAV+1)) |
888 (1<<(estORIRE_DTAV+2)) |
889 (1<<(estORIRE_DTAV+3))));
891 if (*file_version >= 14)
893 do_cpt_int_err(xd, "df history flags", flags_dfh, list);
895 else
897 *flags_dfh = 0;
900 if (*file_version >= 15)
902 do_cpt_int_err(xd, "ED data sets", nED, list);
904 else
906 *nED = 0;
908 if (*file_version >= 16)
910 do_cpt_int_err(xd, "swap", eSwapCoords, list);
914 static int do_cpt_footer(XDR *xd, int file_version)
916 bool_t res = 0;
917 int magic;
919 if (file_version >= 2)
921 magic = CPT_MAGIC2;
922 res = xdr_int(xd, &magic);
923 if (res == 0)
925 cp_error();
927 if (magic != CPT_MAGIC2)
929 return -1;
933 return 0;
936 static int do_cpt_state(XDR *xd, gmx_bool bRead,
937 int fflags, t_state *state,
938 FILE *list)
940 int sflags;
941 int i;
942 int ret;
943 int nnht, nnhtp;
945 ret = 0;
947 nnht = state->nhchainlength*state->ngtc;
948 nnhtp = state->nhchainlength*state->nnhpres;
950 if (bRead) /* we need to allocate space for dfhist if we are reading */
952 init_df_history(&state->dfhist, state->dfhist.nlambda);
955 sflags = state->flags;
956 for (i = 0; (i < estNR && ret == 0); i++)
958 if (fflags & (1<<i))
960 switch (i)
962 case estLAMBDA: ret = do_cpte_reals(xd, cptpEST, i, sflags, efptNR, &(state->lambda), list); break;
963 case estFEPSTATE: ret = do_cpte_int (xd, cptpEST, i, sflags, &state->fep_state, list); break;
964 case estBOX: ret = do_cpte_matrix(xd, cptpEST, i, sflags, state->box, list); break;
965 case estBOX_REL: ret = do_cpte_matrix(xd, cptpEST, i, sflags, state->box_rel, list); break;
966 case estBOXV: ret = do_cpte_matrix(xd, cptpEST, i, sflags, state->boxv, list); break;
967 case estPRES_PREV: ret = do_cpte_matrix(xd, cptpEST, i, sflags, state->pres_prev, list); break;
968 case estSVIR_PREV: ret = do_cpte_matrix(xd, cptpEST, i, sflags, state->svir_prev, list); break;
969 case estFVIR_PREV: ret = do_cpte_matrix(xd, cptpEST, i, sflags, state->fvir_prev, list); break;
970 case estNH_XI: ret = do_cpte_doubles(xd, cptpEST, i, sflags, nnht, &state->nosehoover_xi, list); break;
971 case estNH_VXI: ret = do_cpte_doubles(xd, cptpEST, i, sflags, nnht, &state->nosehoover_vxi, list); break;
972 case estNHPRES_XI: ret = do_cpte_doubles(xd, cptpEST, i, sflags, nnhtp, &state->nhpres_xi, list); break;
973 case estNHPRES_VXI: ret = do_cpte_doubles(xd, cptpEST, i, sflags, nnhtp, &state->nhpres_vxi, list); break;
974 case estTC_INT: ret = do_cpte_doubles(xd, cptpEST, i, sflags, state->ngtc, &state->therm_integral, list); break;
975 case estVETA: ret = do_cpte_real(xd, cptpEST, i, sflags, &state->veta, list); break;
976 case estVOL0: ret = do_cpte_real(xd, cptpEST, i, sflags, &state->vol0, list); break;
977 case estX: ret = do_cpte_rvecs(xd, cptpEST, i, sflags, state->natoms, &state->x, list); break;
978 case estV: ret = do_cpte_rvecs(xd, cptpEST, i, sflags, state->natoms, &state->v, list); break;
979 case estSDX: ret = do_cpte_rvecs(xd, cptpEST, i, sflags, state->natoms, &state->sd_X, list); break;
980 /* The RNG entries are no longer written,
981 * the next 4 lines are only for reading old files.
983 case estLD_RNG: ret = do_cpte_ints(xd, cptpEST, i, sflags, 0, NULL, list); break;
984 case estLD_RNGI: ret = do_cpte_ints(xd, cptpEST, i, sflags, 0, NULL, list); break;
985 case estMC_RNG: ret = do_cpte_ints(xd, cptpEST, i, sflags, 0, NULL, list); break;
986 case estMC_RNGI: ret = do_cpte_ints(xd, cptpEST, i, sflags, 0, NULL, list); break;
987 case estDISRE_INITF: ret = do_cpte_real (xd, cptpEST, i, sflags, &state->hist.disre_initf, list); break;
988 case estDISRE_RM3TAV: ret = do_cpte_n_reals(xd, cptpEST, i, sflags, &state->hist.ndisrepairs, &state->hist.disre_rm3tav, list); break;
989 case estORIRE_INITF: ret = do_cpte_real (xd, cptpEST, i, sflags, &state->hist.orire_initf, list); break;
990 case estORIRE_DTAV: ret = do_cpte_n_reals(xd, cptpEST, i, sflags, &state->hist.norire_Dtav, &state->hist.orire_Dtav, list); break;
991 default:
992 gmx_fatal(FARGS, "Unknown state entry %d\n"
993 "You are probably reading a new checkpoint file with old code", i);
998 return ret;
1001 static int do_cpt_ekinstate(XDR *xd, int fflags, ekinstate_t *ekins,
1002 FILE *list)
1004 int i;
1005 int ret;
1007 ret = 0;
1009 for (i = 0; (i < eeksNR && ret == 0); i++)
1011 if (fflags & (1<<i))
1013 switch (i)
1016 case eeksEKIN_N: ret = do_cpte_int(xd, cptpEEKS, i, fflags, &ekins->ekin_n, list); break;
1017 case eeksEKINH: ret = do_cpte_matrices(xd, cptpEEKS, i, fflags, ekins->ekin_n, &ekins->ekinh, list); break;
1018 case eeksEKINF: ret = do_cpte_matrices(xd, cptpEEKS, i, fflags, ekins->ekin_n, &ekins->ekinf, list); break;
1019 case eeksEKINO: ret = do_cpte_matrices(xd, cptpEEKS, i, fflags, ekins->ekin_n, &ekins->ekinh_old, list); break;
1020 case eeksEKINTOTAL: ret = do_cpte_matrix(xd, cptpEEKS, i, fflags, ekins->ekin_total, list); break;
1021 case eeksEKINSCALEF: ret = do_cpte_doubles(xd, cptpEEKS, i, fflags, ekins->ekin_n, &ekins->ekinscalef_nhc, list); break;
1022 case eeksVSCALE: ret = do_cpte_doubles(xd, 1, cptpEEKS, fflags, ekins->ekin_n, &ekins->vscale_nhc, list); break;
1023 case eeksEKINSCALEH: ret = do_cpte_doubles(xd, 1, cptpEEKS, fflags, ekins->ekin_n, &ekins->ekinscaleh_nhc, list); break;
1024 case eeksDEKINDL: ret = do_cpte_real(xd, 1, cptpEEKS, fflags, &ekins->dekindl, list); break;
1025 case eeksMVCOS: ret = do_cpte_real(xd, 1, cptpEEKS, fflags, &ekins->mvcos, list); break;
1026 default:
1027 gmx_fatal(FARGS, "Unknown ekin data state entry %d\n"
1028 "You are probably reading a new checkpoint file with old code", i);
1033 return ret;
1037 static int do_cpt_swapstate(XDR *xd, gmx_bool bRead, swapstate_t *swapstate, FILE *list)
1039 int ret = 0;
1040 int swap_cpt_version = 2;
1043 if (eswapNO == swapstate->eSwapCoords)
1045 return ret;
1048 swapstate->bFromCpt = bRead;
1050 do_cpt_int_err(xd, "swap checkpoint version", &swap_cpt_version, list);
1051 if (bRead && swap_cpt_version < 2)
1053 gmx_fatal(FARGS, "Cannot read checkpoint files that were written with old versions"
1054 "of the ion/water position swapping protocol.\n");
1057 do_cpt_int_err(xd, "swap coupling steps", &swapstate->nAverage, list);
1059 /* When reading, init_swapcoords has not been called yet,
1060 * so we have to allocate memory first. */
1061 do_cpt_int_err(xd, "number of ion types", &swapstate->nIonTypes, list);
1062 if (bRead)
1064 snew(swapstate->ionType, swapstate->nIonTypes);
1067 for (int ic = 0; ic < eCompNR; ic++)
1069 for (int ii = 0; ii < swapstate->nIonTypes; ii++)
1071 swapstateIons_t *gs = &swapstate->ionType[ii];
1073 if (bRead)
1075 do_cpt_int_err(xd, "swap requested atoms", &gs->nMolReq[ic], list);
1077 else
1079 do_cpt_int_err(xd, "swap requested atoms p", gs->nMolReq_p[ic], list);
1082 if (bRead)
1084 do_cpt_int_err(xd, "swap influx net", &gs->inflow_net[ic], list);
1086 else
1088 do_cpt_int_err(xd, "swap influx net p", gs->inflow_net_p[ic], list);
1091 if (bRead && (NULL == gs->nMolPast[ic]) )
1093 snew(gs->nMolPast[ic], swapstate->nAverage);
1096 for (int j = 0; j < swapstate->nAverage; j++)
1098 if (bRead)
1100 do_cpt_int_err(xd, "swap past atom counts", &gs->nMolPast[ic][j], list);
1102 else
1104 do_cpt_int_err(xd, "swap past atom counts p", &gs->nMolPast_p[ic][j], list);
1110 /* Ion flux per channel */
1111 for (int ic = 0; ic < eChanNR; ic++)
1113 for (int ii = 0; ii < swapstate->nIonTypes; ii++)
1115 swapstateIons_t *gs = &swapstate->ionType[ii];
1117 if (bRead)
1119 do_cpt_int_err(xd, "channel flux A->B", &gs->fluxfromAtoB[ic], list);
1121 else
1123 do_cpt_int_err(xd, "channel flux A->B p", gs->fluxfromAtoB_p[ic], list);
1128 /* Ion flux leakage */
1129 if (bRead)
1131 do_cpt_int_err(xd, "flux leakage", &swapstate->fluxleak, list);
1133 else
1135 do_cpt_int_err(xd, "flux leakage", swapstate->fluxleak_p, list);
1138 /* Ion history */
1139 for (int ii = 0; ii < swapstate->nIonTypes; ii++)
1141 swapstateIons_t *gs = &swapstate->ionType[ii];
1143 do_cpt_int_err(xd, "number of ions", &gs->nMol, list);
1145 if (bRead)
1147 snew(gs->channel_label, gs->nMol);
1148 snew(gs->comp_from, gs->nMol);
1151 do_cpt_u_chars(xd, "channel history", gs->nMol, gs->channel_label, list);
1152 do_cpt_u_chars(xd, "domain history", gs->nMol, gs->comp_from, list);
1155 /* Save the last known whole positions to checkpoint
1156 * file to be able to also make multimeric channels whole in PBC */
1157 do_cpt_int_err(xd, "Ch0 atoms", &swapstate->nat[eChan0], list);
1158 do_cpt_int_err(xd, "Ch1 atoms", &swapstate->nat[eChan1], list);
1159 if (bRead)
1161 snew(swapstate->xc_old_whole[eChan0], swapstate->nat[eChan0]);
1162 snew(swapstate->xc_old_whole[eChan1], swapstate->nat[eChan1]);
1163 do_cpt_n_rvecs_err(xd, "Ch0 whole x", swapstate->nat[eChan0], swapstate->xc_old_whole[eChan0], list);
1164 do_cpt_n_rvecs_err(xd, "Ch1 whole x", swapstate->nat[eChan1], swapstate->xc_old_whole[eChan1], list);
1166 else
1168 do_cpt_n_rvecs_err(xd, "Ch0 whole x", swapstate->nat[eChan0], *swapstate->xc_old_whole_p[eChan0], list);
1169 do_cpt_n_rvecs_err(xd, "Ch1 whole x", swapstate->nat[eChan1], *swapstate->xc_old_whole_p[eChan1], list);
1172 return ret;
1176 static int do_cpt_enerhist(XDR *xd, gmx_bool bRead,
1177 int fflags, energyhistory_t *enerhist,
1178 FILE *list)
1180 int i;
1181 int j;
1182 int ret;
1184 ret = 0;
1186 if (bRead)
1188 enerhist->nsteps = 0;
1189 enerhist->nsum = 0;
1190 enerhist->nsteps_sim = 0;
1191 enerhist->nsum_sim = 0;
1192 enerhist->dht = NULL;
1194 if (fflags & (1<< eenhENERGY_DELTA_H_NN) )
1196 snew(enerhist->dht, 1);
1197 enerhist->dht->ndh = NULL;
1198 enerhist->dht->dh = NULL;
1199 enerhist->dht->start_lambda_set = FALSE;
1203 for (i = 0; (i < eenhNR && ret == 0); i++)
1205 if (fflags & (1<<i))
1207 switch (i)
1209 case eenhENERGY_N: ret = do_cpte_int(xd, cptpEENH, i, fflags, &enerhist->nener, list); break;
1210 case eenhENERGY_AVER: ret = do_cpte_doubles(xd, cptpEENH, i, fflags, enerhist->nener, &enerhist->ener_ave, list); break;
1211 case eenhENERGY_SUM: ret = do_cpte_doubles(xd, cptpEENH, i, fflags, enerhist->nener, &enerhist->ener_sum, list); break;
1212 case eenhENERGY_NSUM: do_cpt_step_err(xd, eenh_names[i], &enerhist->nsum, list); break;
1213 case eenhENERGY_SUM_SIM: ret = do_cpte_doubles(xd, cptpEENH, i, fflags, enerhist->nener, &enerhist->ener_sum_sim, list); break;
1214 case eenhENERGY_NSUM_SIM: do_cpt_step_err(xd, eenh_names[i], &enerhist->nsum_sim, list); break;
1215 case eenhENERGY_NSTEPS: do_cpt_step_err(xd, eenh_names[i], &enerhist->nsteps, list); break;
1216 case eenhENERGY_NSTEPS_SIM: do_cpt_step_err(xd, eenh_names[i], &enerhist->nsteps_sim, list); break;
1217 case eenhENERGY_DELTA_H_NN: do_cpt_int_err(xd, eenh_names[i], &(enerhist->dht->nndh), list);
1218 if (bRead) /* now allocate memory for it */
1220 snew(enerhist->dht->dh, enerhist->dht->nndh);
1221 snew(enerhist->dht->ndh, enerhist->dht->nndh);
1222 for (j = 0; j < enerhist->dht->nndh; j++)
1224 enerhist->dht->ndh[j] = 0;
1225 enerhist->dht->dh[j] = NULL;
1228 break;
1229 case eenhENERGY_DELTA_H_LIST:
1230 for (j = 0; j < enerhist->dht->nndh; j++)
1232 ret = do_cpte_n_reals(xd, cptpEENH, i, fflags, &enerhist->dht->ndh[j], &(enerhist->dht->dh[j]), list);
1234 break;
1235 case eenhENERGY_DELTA_H_STARTTIME:
1236 ret = do_cpte_double(xd, cptpEENH, i, fflags, &(enerhist->dht->start_time), list); break;
1237 case eenhENERGY_DELTA_H_STARTLAMBDA:
1238 ret = do_cpte_double(xd, cptpEENH, i, fflags, &(enerhist->dht->start_lambda), list); break;
1239 default:
1240 gmx_fatal(FARGS, "Unknown energy history entry %d\n"
1241 "You are probably reading a new checkpoint file with old code", i);
1246 if ((fflags & (1<<eenhENERGY_SUM)) && !(fflags & (1<<eenhENERGY_SUM_SIM)))
1248 /* Assume we have an old file format and copy sum to sum_sim */
1249 srenew(enerhist->ener_sum_sim, enerhist->nener);
1250 for (i = 0; i < enerhist->nener; i++)
1252 enerhist->ener_sum_sim[i] = enerhist->ener_sum[i];
1256 if ( (fflags & (1<<eenhENERGY_NSUM)) &&
1257 !(fflags & (1<<eenhENERGY_NSTEPS)))
1259 /* Assume we have an old file format and copy nsum to nsteps */
1260 enerhist->nsteps = enerhist->nsum;
1262 if ( (fflags & (1<<eenhENERGY_NSUM_SIM)) &&
1263 !(fflags & (1<<eenhENERGY_NSTEPS_SIM)))
1265 /* Assume we have an old file format and copy nsum to nsteps */
1266 enerhist->nsteps_sim = enerhist->nsum_sim;
1269 return ret;
1272 static int do_cpt_df_hist(XDR *xd, int fflags, df_history_t *dfhist, FILE *list)
1274 int i, nlambda;
1275 int ret;
1277 nlambda = dfhist->nlambda;
1278 ret = 0;
1280 for (i = 0; (i < edfhNR && ret == 0); i++)
1282 if (fflags & (1<<i))
1284 switch (i)
1286 case edfhBEQUIL: ret = do_cpte_int(xd, cptpEDFH, i, fflags, &dfhist->bEquil, list); break;
1287 case edfhNATLAMBDA: ret = do_cpte_ints(xd, cptpEDFH, i, fflags, nlambda, &dfhist->n_at_lam, list); break;
1288 case edfhWLHISTO: ret = do_cpte_reals(xd, cptpEDFH, i, fflags, nlambda, &dfhist->wl_histo, list); break;
1289 case edfhWLDELTA: ret = do_cpte_real(xd, cptpEDFH, i, fflags, &dfhist->wl_delta, list); break;
1290 case edfhSUMWEIGHTS: ret = do_cpte_reals(xd, cptpEDFH, i, fflags, nlambda, &dfhist->sum_weights, list); break;
1291 case edfhSUMDG: ret = do_cpte_reals(xd, cptpEDFH, i, fflags, nlambda, &dfhist->sum_dg, list); break;
1292 case edfhSUMMINVAR: ret = do_cpte_reals(xd, cptpEDFH, i, fflags, nlambda, &dfhist->sum_minvar, list); break;
1293 case edfhSUMVAR: ret = do_cpte_reals(xd, cptpEDFH, i, fflags, nlambda, &dfhist->sum_variance, list); break;
1294 case edfhACCUMP: ret = do_cpte_nmatrix(xd, cptpEDFH, i, fflags, nlambda, dfhist->accum_p, list); break;
1295 case edfhACCUMM: ret = do_cpte_nmatrix(xd, cptpEDFH, i, fflags, nlambda, dfhist->accum_m, list); break;
1296 case edfhACCUMP2: ret = do_cpte_nmatrix(xd, cptpEDFH, i, fflags, nlambda, dfhist->accum_p2, list); break;
1297 case edfhACCUMM2: ret = do_cpte_nmatrix(xd, cptpEDFH, i, fflags, nlambda, dfhist->accum_m2, list); break;
1298 case edfhTIJ: ret = do_cpte_nmatrix(xd, cptpEDFH, i, fflags, nlambda, dfhist->Tij, list); break;
1299 case edfhTIJEMP: ret = do_cpte_nmatrix(xd, cptpEDFH, i, fflags, nlambda, dfhist->Tij_empirical, list); break;
1301 default:
1302 gmx_fatal(FARGS, "Unknown df history entry %d\n"
1303 "You are probably reading a new checkpoint file with old code", i);
1308 return ret;
1312 /* This function stores the last whole configuration of the reference and
1313 * average structure in the .cpt file
1315 static int do_cpt_EDstate(XDR *xd, gmx_bool bRead,
1316 edsamstate_t *EDstate, FILE *list)
1318 int i;
1319 int ret = 0;
1320 char buf[STRLEN];
1323 EDstate->bFromCpt = bRead;
1325 if (EDstate->nED <= 0)
1327 return ret;
1330 /* When reading, init_edsam has not been called yet,
1331 * so we have to allocate memory first. */
1332 if (bRead)
1334 snew(EDstate->nref, EDstate->nED);
1335 snew(EDstate->old_sref, EDstate->nED);
1336 snew(EDstate->nav, EDstate->nED);
1337 snew(EDstate->old_sav, EDstate->nED);
1340 /* Read/write the last whole conformation of SREF and SAV for each ED dataset (usually only one) */
1341 for (i = 0; i < EDstate->nED; i++)
1343 /* Reference structure SREF */
1344 sprintf(buf, "ED%d # of atoms in reference structure", i+1);
1345 do_cpt_int_err(xd, buf, &EDstate->nref[i], list);
1346 sprintf(buf, "ED%d x_ref", i+1);
1347 if (bRead)
1349 snew(EDstate->old_sref[i], EDstate->nref[i]);
1350 do_cpt_n_rvecs_err(xd, buf, EDstate->nref[i], EDstate->old_sref[i], list);
1352 else
1354 do_cpt_n_rvecs_err(xd, buf, EDstate->nref[i], EDstate->old_sref_p[i], list);
1357 /* Average structure SAV */
1358 sprintf(buf, "ED%d # of atoms in average structure", i+1);
1359 do_cpt_int_err(xd, buf, &EDstate->nav[i], list);
1360 sprintf(buf, "ED%d x_av", i+1);
1361 if (bRead)
1363 snew(EDstate->old_sav[i], EDstate->nav[i]);
1364 do_cpt_n_rvecs_err(xd, buf, EDstate->nav[i], EDstate->old_sav[i], list);
1366 else
1368 do_cpt_n_rvecs_err(xd, buf, EDstate->nav[i], EDstate->old_sav_p[i], list);
1372 return ret;
1376 static int do_cpt_files(XDR *xd, gmx_bool bRead,
1377 gmx_file_position_t **p_outputfiles, int *nfiles,
1378 FILE *list, int file_version)
1380 int i;
1381 gmx_off_t offset;
1382 gmx_off_t mask = 0xFFFFFFFFL;
1383 int offset_high, offset_low;
1384 char *buf;
1385 gmx_file_position_t *outputfiles;
1387 if (do_cpt_int(xd, "number of output files", nfiles, list) != 0)
1389 return -1;
1392 if (bRead)
1394 snew(*p_outputfiles, *nfiles);
1397 outputfiles = *p_outputfiles;
1399 for (i = 0; i < *nfiles; i++)
1401 /* 64-bit XDR numbers are not portable, so it is stored as separate high/low fractions */
1402 if (bRead)
1404 do_cpt_string_err(xd, bRead, "output filename", &buf, list);
1405 std::strncpy(outputfiles[i].filename, buf, CPTSTRLEN-1);
1406 if (list == NULL)
1408 sfree(buf);
1411 if (do_cpt_int(xd, "file_offset_high", &offset_high, list) != 0)
1413 return -1;
1415 if (do_cpt_int(xd, "file_offset_low", &offset_low, list) != 0)
1417 return -1;
1419 outputfiles[i].offset = (static_cast<gmx_off_t>(offset_high) << 32 ) | ( static_cast<gmx_off_t>(offset_low) & mask );
1421 else
1423 buf = outputfiles[i].filename;
1424 do_cpt_string_err(xd, bRead, "output filename", &buf, list);
1425 /* writing */
1426 offset = outputfiles[i].offset;
1427 if (offset == -1)
1429 offset_low = -1;
1430 offset_high = -1;
1432 else
1434 offset_low = static_cast<int>(offset & mask);
1435 offset_high = static_cast<int>((offset >> 32) & mask);
1437 if (do_cpt_int(xd, "file_offset_high", &offset_high, list) != 0)
1439 return -1;
1441 if (do_cpt_int(xd, "file_offset_low", &offset_low, list) != 0)
1443 return -1;
1446 if (file_version >= 8)
1448 if (do_cpt_int(xd, "file_checksum_size", &(outputfiles[i].chksum_size),
1449 list) != 0)
1451 return -1;
1453 if (do_cpt_u_chars(xd, "file_checksum", 16, outputfiles[i].chksum, list) != 0)
1455 return -1;
1458 else
1460 outputfiles[i].chksum_size = -1;
1463 return 0;
1467 void write_checkpoint(const char *fn, gmx_bool bNumberAndKeep,
1468 FILE *fplog, t_commrec *cr,
1469 int eIntegrator, int simulation_part,
1470 gmx_bool bExpanded, int elamstats,
1471 gmx_int64_t step, double t, t_state *state)
1473 t_fileio *fp;
1474 int file_version;
1475 char *version;
1476 char *btime;
1477 char *buser;
1478 char *bhost;
1479 int double_prec;
1480 char *fprog;
1481 char *fntemp; /* the temporary checkpoint file name */
1482 char timebuf[STRLEN];
1483 int nppnodes, npmenodes;
1484 char buf[1024], suffix[5+STEPSTRSIZE], sbuf[STEPSTRSIZE];
1485 gmx_file_position_t *outputfiles;
1486 int noutputfiles;
1487 char *ftime;
1488 int flags_eks, flags_enh, flags_dfh;
1489 t_fileio *ret;
1491 if (DOMAINDECOMP(cr))
1493 nppnodes = cr->dd->nnodes;
1494 npmenodes = cr->npmenodes;
1496 else
1498 nppnodes = 1;
1499 npmenodes = 0;
1502 #if !GMX_NO_RENAME
1503 /* make the new temporary filename */
1504 snew(fntemp, std::strlen(fn)+5+STEPSTRSIZE);
1505 std::strcpy(fntemp, fn);
1506 fntemp[std::strlen(fn) - std::strlen(ftp2ext(fn2ftp(fn))) - 1] = '\0';
1507 sprintf(suffix, "_%s%s", "step", gmx_step_str(step, sbuf));
1508 std::strcat(fntemp, suffix);
1509 std::strcat(fntemp, fn+std::strlen(fn) - std::strlen(ftp2ext(fn2ftp(fn))) - 1);
1510 #else
1511 /* if we can't rename, we just overwrite the cpt file.
1512 * dangerous if interrupted.
1514 snew(fntemp, std::strlen(fn));
1515 std::strcpy(fntemp, fn);
1516 #endif
1517 gmx_format_current_time(timebuf, STRLEN);
1519 if (fplog)
1521 fprintf(fplog, "Writing checkpoint, step %s at %s\n\n",
1522 gmx_step_str(step, buf), timebuf);
1525 /* Get offsets for open files */
1526 gmx_fio_get_output_file_positions(&outputfiles, &noutputfiles);
1528 fp = gmx_fio_open(fntemp, "w");
1530 if (state->ekinstate.bUpToDate)
1532 flags_eks =
1533 ((1<<eeksEKIN_N) | (1<<eeksEKINH) | (1<<eeksEKINF) |
1534 (1<<eeksEKINO) | (1<<eeksEKINSCALEF) | (1<<eeksEKINSCALEH) |
1535 (1<<eeksVSCALE) | (1<<eeksDEKINDL) | (1<<eeksMVCOS));
1537 else
1539 flags_eks = 0;
1542 flags_enh = 0;
1543 if (state->enerhist->nsum > 0 || state->enerhist->nsum_sim > 0)
1545 flags_enh |= (1<<eenhENERGY_N) | (1<<eenhENERGY_NSTEPS) | (1<<eenhENERGY_NSTEPS_SIM);
1546 if (state->enerhist->nsum > 0)
1548 flags_enh |= ((1<<eenhENERGY_AVER) | (1<<eenhENERGY_SUM) |
1549 (1<<eenhENERGY_NSUM));
1551 if (state->enerhist->nsum_sim > 0)
1553 flags_enh |= ((1<<eenhENERGY_SUM_SIM) | (1<<eenhENERGY_NSUM_SIM));
1555 if (state->enerhist->dht)
1557 flags_enh |= ( (1<< eenhENERGY_DELTA_H_NN) |
1558 (1<< eenhENERGY_DELTA_H_LIST) |
1559 (1<< eenhENERGY_DELTA_H_STARTTIME) |
1560 (1<< eenhENERGY_DELTA_H_STARTLAMBDA) );
1564 if (bExpanded)
1566 flags_dfh = ((1<<edfhBEQUIL) | (1<<edfhNATLAMBDA) | (1<<edfhSUMWEIGHTS) | (1<<edfhSUMDG) |
1567 (1<<edfhTIJ) | (1<<edfhTIJEMP));
1568 if (EWL(elamstats))
1570 flags_dfh |= ((1<<edfhWLDELTA) | (1<<edfhWLHISTO));
1572 if ((elamstats == elamstatsMINVAR) || (elamstats == elamstatsBARKER) || (elamstats == elamstatsMETROPOLIS))
1574 flags_dfh |= ((1<<edfhACCUMP) | (1<<edfhACCUMM) | (1<<edfhACCUMP2) | (1<<edfhACCUMM2)
1575 | (1<<edfhSUMMINVAR) | (1<<edfhSUMVAR));
1578 else
1580 flags_dfh = 0;
1583 /* We can check many more things now (CPU, acceleration, etc), but
1584 * it is highly unlikely to have two separate builds with exactly
1585 * the same version, user, time, and build host!
1588 version = gmx_strdup(gmx_version());
1589 btime = gmx_strdup(BUILD_TIME);
1590 buser = gmx_strdup(BUILD_USER);
1591 bhost = gmx_strdup(BUILD_HOST);
1593 double_prec = GMX_CPT_BUILD_DP;
1594 fprog = gmx_strdup(gmx::getProgramContext().fullBinaryPath());
1596 ftime = &(timebuf[0]);
1598 do_cpt_header(gmx_fio_getxdr(fp), FALSE, &file_version,
1599 &version, &btime, &buser, &bhost, &double_prec, &fprog, &ftime,
1600 &eIntegrator, &simulation_part, &step, &t, &nppnodes,
1601 DOMAINDECOMP(cr) ? cr->dd->nc : NULL, &npmenodes,
1602 &state->natoms, &state->ngtc, &state->nnhpres,
1603 &state->nhchainlength, &(state->dfhist.nlambda), &state->flags, &flags_eks, &flags_enh, &flags_dfh,
1604 &state->edsamstate.nED, &state->swapstate.eSwapCoords,
1605 NULL);
1607 sfree(version);
1608 sfree(btime);
1609 sfree(buser);
1610 sfree(bhost);
1611 sfree(fprog);
1613 if ((do_cpt_state(gmx_fio_getxdr(fp), FALSE, state->flags, state, NULL) < 0) ||
1614 (do_cpt_ekinstate(gmx_fio_getxdr(fp), flags_eks, &state->ekinstate, NULL) < 0) ||
1615 (do_cpt_enerhist(gmx_fio_getxdr(fp), FALSE, flags_enh, state->enerhist, NULL) < 0) ||
1616 (do_cpt_df_hist(gmx_fio_getxdr(fp), flags_dfh, &state->dfhist, NULL) < 0) ||
1617 (do_cpt_EDstate(gmx_fio_getxdr(fp), FALSE, &state->edsamstate, NULL) < 0) ||
1618 (do_cpt_swapstate(gmx_fio_getxdr(fp), FALSE, &state->swapstate, NULL) < 0) ||
1619 (do_cpt_files(gmx_fio_getxdr(fp), FALSE, &outputfiles, &noutputfiles, NULL,
1620 file_version) < 0))
1622 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of disk space?");
1625 do_cpt_footer(gmx_fio_getxdr(fp), file_version);
1627 /* we really, REALLY, want to make sure to physically write the checkpoint,
1628 and all the files it depends on, out to disk. Because we've
1629 opened the checkpoint with gmx_fio_open(), it's in our list
1630 of open files. */
1631 ret = gmx_fio_all_output_fsync();
1633 if (ret)
1635 char buf[STRLEN];
1636 sprintf(buf,
1637 "Cannot fsync '%s'; maybe you are out of disk space?",
1638 gmx_fio_getname(ret));
1640 if (getenv(GMX_IGNORE_FSYNC_FAILURE_ENV) == NULL)
1642 gmx_file(buf);
1644 else
1646 gmx_warning(buf);
1650 if (gmx_fio_close(fp) != 0)
1652 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of disk space?");
1655 /* we don't move the checkpoint if the user specified they didn't want it,
1656 or if the fsyncs failed */
1657 #if !GMX_NO_RENAME
1658 if (!bNumberAndKeep && !ret)
1660 if (gmx_fexist(fn))
1662 /* Rename the previous checkpoint file */
1663 std::strcpy(buf, fn);
1664 buf[std::strlen(fn) - std::strlen(ftp2ext(fn2ftp(fn))) - 1] = '\0';
1665 std::strcat(buf, "_prev");
1666 std::strcat(buf, fn+std::strlen(fn) - std::strlen(ftp2ext(fn2ftp(fn))) - 1);
1667 #ifndef GMX_FAHCORE
1668 /* we copy here so that if something goes wrong between now and
1669 * the rename below, there's always a state.cpt.
1670 * If renames are atomic (such as in POSIX systems),
1671 * this copying should be unneccesary.
1673 gmx_file_copy(fn, buf, FALSE);
1674 /* We don't really care if this fails:
1675 * there's already a new checkpoint.
1677 #else
1678 gmx_file_rename(fn, buf);
1679 #endif
1681 if (gmx_file_rename(fntemp, fn) != 0)
1683 gmx_file("Cannot rename checkpoint file; maybe you are out of disk space?");
1686 #endif /* GMX_NO_RENAME */
1688 sfree(outputfiles);
1689 sfree(fntemp);
1691 #ifdef GMX_FAHCORE
1692 /*code for alternate checkpointing scheme. moved from top of loop over
1693 steps */
1694 fcRequestCheckPoint();
1695 if (fcCheckPointParallel( cr->nodeid, NULL, 0) == 0)
1697 gmx_fatal( 3, __FILE__, __LINE__, "Checkpoint error on step %d\n", step );
1699 #endif /* end GMX_FAHCORE block */
1702 static void print_flag_mismatch(FILE *fplog, int sflags, int fflags)
1704 int i;
1706 fprintf(fplog, "\nState entry mismatch between the simulation and the checkpoint file\n");
1707 fprintf(fplog, "Entries which are not present in the checkpoint file will not be updated\n");
1708 fprintf(fplog, " %24s %11s %11s\n", "", "simulation", "checkpoint");
1709 for (i = 0; i < estNR; i++)
1711 if ((sflags & (1<<i)) || (fflags & (1<<i)))
1713 fprintf(fplog, " %24s %11s %11s\n",
1714 est_names[i],
1715 (sflags & (1<<i)) ? " present " : "not present",
1716 (fflags & (1<<i)) ? " present " : "not present");
1721 static void check_int(FILE *fplog, const char *type, int p, int f, gmx_bool *mm)
1723 FILE *fp = fplog ? fplog : stderr;
1725 if (p != f)
1727 fprintf(fp, " %s mismatch,\n", type);
1728 fprintf(fp, " current program: %d\n", p);
1729 fprintf(fp, " checkpoint file: %d\n", f);
1730 fprintf(fp, "\n");
1731 *mm = TRUE;
1735 static void check_string(FILE *fplog, const char *type, const char *p,
1736 const char *f, gmx_bool *mm)
1738 FILE *fp = fplog ? fplog : stderr;
1740 if (std::strcmp(p, f) != 0)
1742 fprintf(fp, " %s mismatch,\n", type);
1743 fprintf(fp, " current program: %s\n", p);
1744 fprintf(fp, " checkpoint file: %s\n", f);
1745 fprintf(fp, "\n");
1746 *mm = TRUE;
1750 static void check_match(FILE *fplog,
1751 char *version,
1752 char *btime, char *buser, char *bhost, int double_prec,
1753 char *fprog,
1754 t_commrec *cr, int npp_f, int npme_f,
1755 ivec dd_nc, ivec dd_nc_f)
1757 int npp;
1758 gmx_bool mm = FALSE;
1759 gmx_bool patchlevel_differs = FALSE;
1760 gmx_bool version_differs = FALSE;
1762 check_string(fplog, "Version", gmx_version(), version, &mm);
1763 patchlevel_differs = mm;
1765 if (patchlevel_differs)
1767 /* Gromacs should be able to continue from checkpoints between
1768 * different patch level versions, but we do not guarantee
1769 * compatibility between different major/minor versions - check this.
1771 int gmx_major, gmx_minor;
1772 int cpt_major, cpt_minor;
1773 sscanf(gmx_version(), "VERSION %5d.%5d", &gmx_major, &gmx_minor);
1774 int ret = sscanf(version, "VERSION %5d.%5d", &cpt_major, &cpt_minor);
1775 version_differs = (ret < 2 || gmx_major != cpt_major ||
1776 gmx_minor != cpt_minor);
1779 check_string(fplog, "Build time", BUILD_TIME, btime, &mm);
1780 check_string(fplog, "Build user", BUILD_USER, buser, &mm);
1781 check_string(fplog, "Build host", BUILD_HOST, bhost, &mm);
1782 check_int (fplog, "Double prec.", GMX_CPT_BUILD_DP, double_prec, &mm);
1783 check_string(fplog, "Program name", gmx::getProgramContext().fullBinaryPath(), fprog, &mm);
1785 check_int (fplog, "#ranks", cr->nnodes, npp_f+npme_f, &mm);
1786 if (cr->nnodes > 1)
1788 check_int (fplog, "#PME-ranks", cr->npmenodes, npme_f, &mm);
1790 npp = cr->nnodes;
1791 if (cr->npmenodes >= 0)
1793 npp -= cr->npmenodes;
1795 if (npp == npp_f)
1797 check_int (fplog, "#DD-cells[x]", dd_nc[XX], dd_nc_f[XX], &mm);
1798 check_int (fplog, "#DD-cells[y]", dd_nc[YY], dd_nc_f[YY], &mm);
1799 check_int (fplog, "#DD-cells[z]", dd_nc[ZZ], dd_nc_f[ZZ], &mm);
1803 if (mm)
1805 const char msg_version_difference[] =
1806 "The current GROMACS major & minor version are not identical to those that\n"
1807 "generated the checkpoint file. In principle GROMACS does not support\n"
1808 "continuation from checkpoints between different versions, so we advise\n"
1809 "against this. If you still want to try your luck we recommend that you use\n"
1810 "the -noappend flag to keep your output files from the two versions separate.\n"
1811 "This might also work around errors where the output fields in the energy\n"
1812 "file have changed between the different major & minor versions.\n";
1814 const char msg_mismatch_notice[] =
1815 "GROMACS patchlevel, binary or parallel settings differ from previous run.\n"
1816 "Continuation is exact, but not guaranteed to be binary identical.\n";
1818 const char msg_logdetails[] =
1819 "See the log file for details.\n";
1821 if (version_differs)
1823 fprintf(stderr, "%s%s\n", msg_version_difference, fplog ? msg_logdetails : "");
1825 if (fplog)
1827 fprintf(fplog, "%s\n", msg_version_difference);
1830 else
1832 /* Major & minor versions match at least, but something is different. */
1833 fprintf(stderr, "%s%s\n", msg_mismatch_notice, fplog ? msg_logdetails : "");
1834 if (fplog)
1836 fprintf(fplog, "%s\n", msg_mismatch_notice);
1842 static void read_checkpoint(const char *fn, FILE **pfplog,
1843 t_commrec *cr, ivec dd_nc,
1844 int eIntegrator, int *init_fep_state, gmx_int64_t *step, double *t,
1845 t_state *state, gmx_bool *bReadEkin,
1846 int *simulation_part,
1847 gmx_bool bAppendOutputFiles, gmx_bool bForceAppend)
1849 t_fileio *fp;
1850 int i, j, rc;
1851 int file_version;
1852 char *version, *btime, *buser, *bhost, *fprog, *ftime;
1853 int double_prec;
1854 char buf[STEPSTRSIZE];
1855 int eIntegrator_f, nppnodes_f, npmenodes_f;
1856 ivec dd_nc_f;
1857 int natoms, ngtc, nnhpres, nhchainlength, nlambda, fflags, flags_eks, flags_enh, flags_dfh;
1858 int d;
1859 int ret;
1860 gmx_file_position_t *outputfiles;
1861 int nfiles;
1862 t_fileio *chksum_file;
1863 FILE * fplog = *pfplog;
1864 unsigned char digest[16];
1865 #if !defined __native_client__ && !defined GMX_NATIVE_WINDOWS
1866 struct flock fl; /* don't initialize here: the struct order is OS
1867 dependent! */
1868 #endif
1870 const char *int_warn =
1871 "WARNING: The checkpoint file was generated with integrator %s,\n"
1872 " while the simulation uses integrator %s\n\n";
1874 #if !defined __native_client__ && !defined GMX_NATIVE_WINDOWS
1875 fl.l_type = F_WRLCK;
1876 fl.l_whence = SEEK_SET;
1877 fl.l_start = 0;
1878 fl.l_len = 0;
1879 fl.l_pid = 0;
1880 #endif
1882 fp = gmx_fio_open(fn, "r");
1883 do_cpt_header(gmx_fio_getxdr(fp), TRUE, &file_version,
1884 &version, &btime, &buser, &bhost, &double_prec, &fprog, &ftime,
1885 &eIntegrator_f, simulation_part, step, t,
1886 &nppnodes_f, dd_nc_f, &npmenodes_f,
1887 &natoms, &ngtc, &nnhpres, &nhchainlength, &nlambda,
1888 &fflags, &flags_eks, &flags_enh, &flags_dfh,
1889 &state->edsamstate.nED, &state->swapstate.eSwapCoords, NULL);
1891 if (bAppendOutputFiles &&
1892 file_version >= 13 && double_prec != GMX_CPT_BUILD_DP)
1894 gmx_fatal(FARGS, "Output file appending requested, but the code and checkpoint file precision (single/double) don't match");
1897 if (cr == NULL || MASTER(cr))
1899 fprintf(stderr, "\nReading checkpoint file %s generated: %s\n\n",
1900 fn, ftime);
1903 /* This will not be written if we do appending, since fplog is still NULL then */
1904 if (fplog)
1906 fprintf(fplog, "\n");
1907 fprintf(fplog, "Reading checkpoint file %s\n", fn);
1908 fprintf(fplog, " file generated by: %s\n", fprog);
1909 fprintf(fplog, " file generated at: %s\n", ftime);
1910 fprintf(fplog, " GROMACS build time: %s\n", btime);
1911 fprintf(fplog, " GROMACS build user: %s\n", buser);
1912 fprintf(fplog, " GROMACS build host: %s\n", bhost);
1913 fprintf(fplog, " GROMACS double prec.: %d\n", double_prec);
1914 fprintf(fplog, " simulation part #: %d\n", *simulation_part);
1915 fprintf(fplog, " step: %s\n", gmx_step_str(*step, buf));
1916 fprintf(fplog, " time: %f\n", *t);
1917 fprintf(fplog, "\n");
1920 if (natoms != state->natoms)
1922 gmx_fatal(FARGS, "Checkpoint file is for a system of %d atoms, while the current system consists of %d atoms", natoms, state->natoms);
1924 if (ngtc != state->ngtc)
1926 gmx_fatal(FARGS, "Checkpoint file is for a system of %d T-coupling groups, while the current system consists of %d T-coupling groups", ngtc, state->ngtc);
1928 if (nnhpres != state->nnhpres)
1930 gmx_fatal(FARGS, "Checkpoint file is for a system of %d NH-pressure-coupling variables, while the current system consists of %d NH-pressure-coupling variables", nnhpres, state->nnhpres);
1933 if (nlambda != state->dfhist.nlambda)
1935 gmx_fatal(FARGS, "Checkpoint file is for a system with %d lambda states, while the current system consists of %d lambda states", nlambda, state->dfhist.nlambda);
1938 init_gtc_state(state, state->ngtc, state->nnhpres, nhchainlength); /* need to keep this here to keep the tpr format working */
1939 /* write over whatever was read; we use the number of Nose-Hoover chains from the checkpoint */
1941 if (eIntegrator_f != eIntegrator)
1943 if (MASTER(cr))
1945 fprintf(stderr, int_warn, EI(eIntegrator_f), EI(eIntegrator));
1947 if (bAppendOutputFiles)
1949 gmx_fatal(FARGS,
1950 "Output file appending requested, but input/checkpoint integrators do not match.\n"
1951 "Stopping the run to prevent you from ruining all your data...\n"
1952 "If you _really_ know what you are doing, try with the -noappend option.\n");
1954 if (fplog)
1956 fprintf(fplog, int_warn, EI(eIntegrator_f), EI(eIntegrator));
1960 if (!PAR(cr))
1962 cr->npmenodes = 0;
1964 else if (cr->nnodes == nppnodes_f + npmenodes_f)
1966 if (cr->npmenodes < 0)
1968 cr->npmenodes = npmenodes_f;
1970 int nppnodes = cr->nnodes - cr->npmenodes;
1971 if (nppnodes == nppnodes_f)
1973 for (d = 0; d < DIM; d++)
1975 if (dd_nc[d] == 0)
1977 dd_nc[d] = dd_nc_f[d];
1983 if (fflags != state->flags)
1986 if (MASTER(cr))
1988 if (bAppendOutputFiles)
1990 gmx_fatal(FARGS,
1991 "Output file appending requested, but input and checkpoint states are not identical.\n"
1992 "Stopping the run to prevent you from ruining all your data...\n"
1993 "You can try with the -noappend option, and get more info in the log file.\n");
1996 if (getenv("GMX_ALLOW_CPT_MISMATCH") == NULL)
1998 gmx_fatal(FARGS, "You seem to have switched ensemble, integrator, T and/or P-coupling algorithm between the cpt and tpr file. The recommended way of doing this is passing the cpt file to grompp (with option -t) instead of to mdrun. If you know what you are doing, you can override this error by setting the env.var. GMX_ALLOW_CPT_MISMATCH");
2000 else
2002 fprintf(stderr,
2003 "WARNING: The checkpoint state entries do not match the simulation,\n"
2004 " see the log file for details\n\n");
2008 if (fplog)
2010 print_flag_mismatch(fplog, state->flags, fflags);
2013 else
2015 if (MASTER(cr))
2017 check_match(fplog, version, btime, buser, bhost, double_prec, fprog,
2018 cr, nppnodes_f, npmenodes_f, dd_nc, dd_nc_f);
2021 ret = do_cpt_state(gmx_fio_getxdr(fp), TRUE, fflags, state, NULL);
2022 *init_fep_state = state->fep_state; /* there should be a better way to do this than setting it here.
2023 Investigate for 5.0. */
2024 if (ret)
2026 cp_error();
2028 ret = do_cpt_ekinstate(gmx_fio_getxdr(fp), flags_eks, &state->ekinstate, NULL);
2029 if (ret)
2031 cp_error();
2033 *bReadEkin = ((flags_eks & (1<<eeksEKINH)) || (flags_eks & (1<<eeksEKINF)) || (flags_eks & (1<<eeksEKINO)) ||
2034 ((flags_eks & (1<<eeksEKINSCALEF)) | (flags_eks & (1<<eeksEKINSCALEH)) | (flags_eks & (1<<eeksVSCALE))));
2036 ret = do_cpt_enerhist(gmx_fio_getxdr(fp), TRUE,
2037 flags_enh, state->enerhist, NULL);
2038 if (ret)
2040 cp_error();
2043 if (file_version < 6)
2045 const char *warn = "Reading checkpoint file in old format, assuming that the run that generated this file started at step 0, if this is not the case the averages stored in the energy file will be incorrect.";
2047 fprintf(stderr, "\nWARNING: %s\n\n", warn);
2048 if (fplog)
2050 fprintf(fplog, "\nWARNING: %s\n\n", warn);
2052 state->enerhist->nsum = *step;
2053 state->enerhist->nsum_sim = *step;
2056 ret = do_cpt_df_hist(gmx_fio_getxdr(fp), flags_dfh, &state->dfhist, NULL);
2057 if (ret)
2059 cp_error();
2062 ret = do_cpt_EDstate(gmx_fio_getxdr(fp), TRUE, &state->edsamstate, NULL);
2063 if (ret)
2065 cp_error();
2068 ret = do_cpt_swapstate(gmx_fio_getxdr(fp), TRUE, &state->swapstate, NULL);
2069 if (ret)
2071 cp_error();
2074 ret = do_cpt_files(gmx_fio_getxdr(fp), TRUE, &outputfiles, &nfiles, NULL, file_version);
2075 if (ret)
2077 cp_error();
2080 ret = do_cpt_footer(gmx_fio_getxdr(fp), file_version);
2081 if (ret)
2083 cp_error();
2085 if (gmx_fio_close(fp) != 0)
2087 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of disk space?");
2090 sfree(fprog);
2091 sfree(ftime);
2092 sfree(btime);
2093 sfree(buser);
2094 sfree(bhost);
2096 /* If the user wants to append to output files,
2097 * we use the file pointer positions of the output files stored
2098 * in the checkpoint file and truncate the files such that any frames
2099 * written after the checkpoint time are removed.
2100 * All files are md5sum checked such that we can be sure that
2101 * we do not truncate other (maybe imprortant) files.
2103 if (bAppendOutputFiles)
2105 if (fn2ftp(outputfiles[0].filename) != efLOG)
2107 /* make sure first file is log file so that it is OK to use it for
2108 * locking
2110 gmx_fatal(FARGS, "The first output file should always be the log "
2111 "file but instead is: %s. Cannot do appending because of this condition.", outputfiles[0].filename);
2113 for (i = 0; i < nfiles; i++)
2115 if (outputfiles[i].offset < 0)
2117 gmx_fatal(FARGS, "The original run wrote a file called '%s' which "
2118 "is larger than 2 GB, but mdrun did not support large file"
2119 " offsets. Can not append. Run mdrun with -noappend",
2120 outputfiles[i].filename);
2122 #ifdef GMX_FAHCORE
2123 chksum_file = gmx_fio_open(outputfiles[i].filename, "a");
2125 #else
2126 chksum_file = gmx_fio_open(outputfiles[i].filename, "r+");
2128 /* lock log file */
2129 if (i == 0)
2131 /* Note that there are systems where the lock operation
2132 * will succeed, but a second process can also lock the file.
2133 * We should probably try to detect this.
2135 #if defined __native_client__
2136 errno = ENOSYS;
2137 if (1)
2139 #elif defined GMX_NATIVE_WINDOWS
2140 if (_locking(fileno(gmx_fio_getfp(chksum_file)), _LK_NBLCK, LONG_MAX) == -1)
2141 #else
2142 if (fcntl(fileno(gmx_fio_getfp(chksum_file)), F_SETLK, &fl) == -1)
2143 #endif
2145 if (errno == ENOSYS)
2147 if (!bForceAppend)
2149 gmx_fatal(FARGS, "File locking is not supported on this system. Use -noappend or specify -append explicitly to append anyhow.");
2151 else
2153 fprintf(stderr, "\nNOTE: File locking is not supported on this system, will not lock %s\n\n", outputfiles[i].filename);
2154 if (fplog)
2156 fprintf(fplog, "\nNOTE: File locking not supported on this system, will not lock %s\n\n", outputfiles[i].filename);
2160 else if (errno == EACCES || errno == EAGAIN)
2162 gmx_fatal(FARGS, "Failed to lock: %s. Already running "
2163 "simulation?", outputfiles[i].filename);
2165 else
2167 gmx_fatal(FARGS, "Failed to lock: %s. %s.",
2168 outputfiles[i].filename, std::strerror(errno));
2173 /* compute md5 chksum */
2174 if (outputfiles[i].chksum_size != -1)
2176 if (gmx_fio_get_file_md5(chksum_file, outputfiles[i].offset,
2177 digest) != outputfiles[i].chksum_size) /*at the end of the call the file position is at the end of the file*/
2179 gmx_fatal(FARGS, "Can't read %d bytes of '%s' to compute checksum. The file has been replaced or its contents have been modified. Cannot do appending because of this condition.",
2180 outputfiles[i].chksum_size,
2181 outputfiles[i].filename);
2184 if (i == 0) /*log file needs to be seeked in case we need to truncate (other files are truncated below)*/
2186 if (gmx_fio_seek(chksum_file, outputfiles[i].offset))
2188 gmx_fatal(FARGS, "Seek error! Failed to truncate log-file: %s.", std::strerror(errno));
2191 #endif
2193 if (i == 0) /*open log file here - so that lock is never lifted
2194 after chksum is calculated */
2196 *pfplog = gmx_fio_getfp(chksum_file);
2198 else
2200 gmx_fio_close(chksum_file);
2202 #ifndef GMX_FAHCORE
2203 /* compare md5 chksum */
2204 if (outputfiles[i].chksum_size != -1 &&
2205 memcmp(digest, outputfiles[i].chksum, 16) != 0)
2207 if (debug)
2209 fprintf(debug, "chksum for %s: ", outputfiles[i].filename);
2210 for (j = 0; j < 16; j++)
2212 fprintf(debug, "%02x", digest[j]);
2214 fprintf(debug, "\n");
2216 gmx_fatal(FARGS, "Checksum wrong for '%s'. The file has been replaced or its contents have been modified. Cannot do appending because of this condition.",
2217 outputfiles[i].filename);
2219 #endif
2222 if (i != 0) /*log file is already seeked to correct position */
2224 #if !defined(GMX_NATIVE_WINDOWS) || !defined(GMX_FAHCORE)
2225 /* For FAHCORE, we do this elsewhere*/
2226 rc = gmx_truncate(outputfiles[i].filename, outputfiles[i].offset);
2227 if (rc != 0)
2229 gmx_fatal(FARGS, "Truncation of file %s failed. Cannot do appending because of this failure.", outputfiles[i].filename);
2231 #endif
2236 sfree(outputfiles);
2240 void load_checkpoint(const char *fn, FILE **fplog,
2241 t_commrec *cr, ivec dd_nc,
2242 t_inputrec *ir, t_state *state,
2243 gmx_bool *bReadEkin,
2244 gmx_bool bAppend, gmx_bool bForceAppend)
2246 gmx_int64_t step;
2247 double t;
2249 if (SIMMASTER(cr))
2251 /* Read the state from the checkpoint file */
2252 read_checkpoint(fn, fplog,
2253 cr, dd_nc,
2254 ir->eI, &(ir->fepvals->init_fep_state), &step, &t, state, bReadEkin,
2255 &ir->simulation_part, bAppend, bForceAppend);
2257 if (PAR(cr))
2259 gmx_bcast(sizeof(cr->npmenodes), &cr->npmenodes, cr);
2260 gmx_bcast(DIM*sizeof(dd_nc[0]), dd_nc, cr);
2261 gmx_bcast(sizeof(step), &step, cr);
2262 gmx_bcast(sizeof(*bReadEkin), bReadEkin, cr);
2264 ir->bContinuation = TRUE;
2265 if (ir->nsteps >= 0)
2267 ir->nsteps += ir->init_step - step;
2269 ir->init_step = step;
2270 ir->simulation_part += 1;
2273 void read_checkpoint_part_and_step(const char *filename,
2274 int *simulation_part,
2275 gmx_int64_t *step)
2277 int file_version;
2278 char *version, *btime, *buser, *bhost, *fprog, *ftime;
2279 int double_prec;
2280 int eIntegrator;
2281 int nppnodes, npme;
2282 ivec dd_nc;
2283 int flags_eks, flags_enh, flags_dfh;
2284 double t;
2285 t_state state;
2286 t_fileio *fp;
2288 if (filename == NULL ||
2289 !gmx_fexist(filename) ||
2290 (!(fp = gmx_fio_open(filename, "r"))))
2292 *simulation_part = 0;
2293 *step = 0;
2294 return;
2297 /* Not calling initializing state before use is nasty, but all we
2298 do is read into its member variables and throw the struct away
2299 again immediately. */
2301 do_cpt_header(gmx_fio_getxdr(fp), TRUE, &file_version,
2302 &version, &btime, &buser, &bhost, &double_prec, &fprog, &ftime,
2303 &eIntegrator, simulation_part, step, &t, &nppnodes, dd_nc, &npme,
2304 &state.natoms, &state.ngtc, &state.nnhpres, &state.nhchainlength,
2305 &(state.dfhist.nlambda), &state.flags, &flags_eks, &flags_enh, &flags_dfh,
2306 &state.edsamstate.nED, &state.swapstate.eSwapCoords, NULL);
2308 gmx_fio_close(fp);
2311 static void read_checkpoint_data(t_fileio *fp, int *simulation_part,
2312 gmx_int64_t *step, double *t, t_state *state,
2313 int *nfiles, gmx_file_position_t **outputfiles)
2315 int file_version;
2316 char *version, *btime, *buser, *bhost, *fprog, *ftime;
2317 int double_prec;
2318 int eIntegrator;
2319 int nppnodes, npme;
2320 ivec dd_nc;
2321 int flags_eks, flags_enh, flags_dfh;
2322 int nfiles_loc;
2323 gmx_file_position_t *files_loc = NULL;
2324 int ret;
2326 do_cpt_header(gmx_fio_getxdr(fp), TRUE, &file_version,
2327 &version, &btime, &buser, &bhost, &double_prec, &fprog, &ftime,
2328 &eIntegrator, simulation_part, step, t, &nppnodes, dd_nc, &npme,
2329 &state->natoms, &state->ngtc, &state->nnhpres, &state->nhchainlength,
2330 &(state->dfhist.nlambda), &state->flags, &flags_eks, &flags_enh, &flags_dfh,
2331 &state->edsamstate.nED, &state->swapstate.eSwapCoords, NULL);
2332 ret =
2333 do_cpt_state(gmx_fio_getxdr(fp), TRUE, state->flags, state, NULL);
2334 if (ret)
2336 cp_error();
2338 ret = do_cpt_ekinstate(gmx_fio_getxdr(fp), flags_eks, &state->ekinstate, NULL);
2339 if (ret)
2341 cp_error();
2343 ret = do_cpt_enerhist(gmx_fio_getxdr(fp), TRUE,
2344 flags_enh, state->enerhist, NULL);
2345 if (ret)
2347 cp_error();
2349 ret = do_cpt_df_hist(gmx_fio_getxdr(fp), flags_dfh, &state->dfhist, NULL);
2350 if (ret)
2352 cp_error();
2355 ret = do_cpt_EDstate(gmx_fio_getxdr(fp), TRUE, &state->edsamstate, NULL);
2356 if (ret)
2358 cp_error();
2361 ret = do_cpt_swapstate(gmx_fio_getxdr(fp), TRUE, &state->swapstate, NULL);
2362 if (ret)
2364 cp_error();
2367 ret = do_cpt_files(gmx_fio_getxdr(fp), TRUE,
2368 outputfiles != NULL ? outputfiles : &files_loc,
2369 outputfiles != NULL ? nfiles : &nfiles_loc,
2370 NULL, file_version);
2371 if (files_loc != NULL)
2373 sfree(files_loc);
2376 if (ret)
2378 cp_error();
2381 ret = do_cpt_footer(gmx_fio_getxdr(fp), file_version);
2382 if (ret)
2384 cp_error();
2387 sfree(fprog);
2388 sfree(ftime);
2389 sfree(btime);
2390 sfree(buser);
2391 sfree(bhost);
2394 void
2395 read_checkpoint_state(const char *fn, int *simulation_part,
2396 gmx_int64_t *step, double *t, t_state *state)
2398 t_fileio *fp;
2400 fp = gmx_fio_open(fn, "r");
2401 read_checkpoint_data(fp, simulation_part, step, t, state, NULL, NULL);
2402 if (gmx_fio_close(fp) != 0)
2404 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of disk space?");
2408 void read_checkpoint_trxframe(t_fileio *fp, t_trxframe *fr)
2410 /* This next line is nasty because the sub-structures of t_state
2411 * cannot be assumed to be zeroed (or even initialized in ways the
2412 * rest of the code might assume). Using snew would be better, but
2413 * this will all go away for 5.0. */
2414 t_state state;
2415 int simulation_part;
2416 gmx_int64_t step;
2417 double t;
2419 init_state(&state, 0, 0, 0, 0, 0);
2421 read_checkpoint_data(fp, &simulation_part, &step, &t, &state, NULL, NULL);
2423 fr->natoms = state.natoms;
2424 fr->bTitle = FALSE;
2425 fr->bStep = TRUE;
2426 fr->step = gmx_int64_to_int(step,
2427 "conversion of checkpoint to trajectory");
2428 fr->bTime = TRUE;
2429 fr->time = t;
2430 fr->bLambda = TRUE;
2431 fr->lambda = state.lambda[efptFEP];
2432 fr->fep_state = state.fep_state;
2433 fr->bAtoms = FALSE;
2434 fr->bX = (state.flags & (1<<estX));
2435 if (fr->bX)
2437 fr->x = state.x;
2438 state.x = NULL;
2440 fr->bV = (state.flags & (1<<estV));
2441 if (fr->bV)
2443 fr->v = state.v;
2444 state.v = NULL;
2446 fr->bF = FALSE;
2447 fr->bBox = (state.flags & (1<<estBOX));
2448 if (fr->bBox)
2450 copy_mat(state.box, fr->box);
2452 done_state(&state);
2455 void list_checkpoint(const char *fn, FILE *out)
2457 t_fileio *fp;
2458 int file_version;
2459 char *version, *btime, *buser, *bhost, *fprog, *ftime;
2460 int double_prec;
2461 int eIntegrator, simulation_part, nppnodes, npme;
2462 gmx_int64_t step;
2463 double t;
2464 ivec dd_nc;
2465 t_state state;
2466 int flags_eks, flags_enh, flags_dfh;
2467 int ret;
2468 gmx_file_position_t *outputfiles;
2469 int nfiles;
2471 init_state(&state, -1, -1, -1, -1, 0);
2473 fp = gmx_fio_open(fn, "r");
2474 do_cpt_header(gmx_fio_getxdr(fp), TRUE, &file_version,
2475 &version, &btime, &buser, &bhost, &double_prec, &fprog, &ftime,
2476 &eIntegrator, &simulation_part, &step, &t, &nppnodes, dd_nc, &npme,
2477 &state.natoms, &state.ngtc, &state.nnhpres, &state.nhchainlength,
2478 &(state.dfhist.nlambda), &state.flags,
2479 &flags_eks, &flags_enh, &flags_dfh, &state.edsamstate.nED,
2480 &state.swapstate.eSwapCoords, out);
2481 ret = do_cpt_state(gmx_fio_getxdr(fp), TRUE, state.flags, &state, out);
2482 if (ret)
2484 cp_error();
2486 ret = do_cpt_ekinstate(gmx_fio_getxdr(fp), flags_eks, &state.ekinstate, out);
2487 if (ret)
2489 cp_error();
2491 ret = do_cpt_enerhist(gmx_fio_getxdr(fp), TRUE,
2492 flags_enh, state.enerhist, out);
2494 if (ret == 0)
2496 ret = do_cpt_df_hist(gmx_fio_getxdr(fp),
2497 flags_dfh, &state.dfhist, out);
2500 if (ret == 0)
2502 ret = do_cpt_EDstate(gmx_fio_getxdr(fp), TRUE, &state.edsamstate, out);
2505 if (ret == 0)
2507 ret = do_cpt_swapstate(gmx_fio_getxdr(fp), TRUE, &state.swapstate, out);
2510 if (ret == 0)
2512 do_cpt_files(gmx_fio_getxdr(fp), TRUE, &outputfiles, &nfiles, out, file_version);
2515 if (ret == 0)
2517 ret = do_cpt_footer(gmx_fio_getxdr(fp), file_version);
2520 if (ret)
2522 cp_warning(out);
2524 if (gmx_fio_close(fp) != 0)
2526 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of disk space?");
2529 done_state(&state);
2532 /* This routine cannot print tons of data, since it is called before the log file is opened. */
2533 void
2534 read_checkpoint_simulation_part_and_filenames(t_fileio *fp,
2535 int *simulation_part,
2536 int *nfiles,
2537 gmx_file_position_t **outputfiles)
2539 gmx_int64_t step = 0;
2540 double t;
2541 t_state state;
2543 init_state(&state, 0, 0, 0, 0, 0);
2545 read_checkpoint_data(fp, simulation_part, &step, &t, &state,
2546 nfiles, outputfiles);
2547 if (gmx_fio_close(fp) != 0)
2549 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of disk space?");
2551 done_state(&state);