1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This file is part of Gromacs Copyright (c) 1991-2008
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * To help us fund GROMACS development, we humbly ask that you cite
13 * the research papers on the package. Check out http://www.gromacs.org
16 * Gnomes, ROck Monsters And Chili Sauce
19 /* The source code in this file should be thread-safe.
20 Please keep it that way. */
30 #ifdef HAVE_SYS_TIME_H
35 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
38 #include <sys/locking.h>
52 #include "gmx_random.h"
53 #include "checkpoint.h"
64 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
66 gmx_ctime_r(const time_t *clock,char *buf, int n);
69 #define CPT_MAGIC1 171817
70 #define CPT_MAGIC2 171819
72 /* cpt_version should normally only be changed
73 * when the header of footer format changes.
74 * The state data format itself is backward and forward compatible.
75 * But old code can not read a new entry that is present in the file
76 * (but can read a new format when new entries are not present).
78 static const int cpt_version = 12;
81 const char *est_names[estNR]=
84 "box", "box-rel", "box-v", "pres_prev",
85 "nosehoover-xi", "thermostat-integral",
86 "x", "v", "SDx", "CGp", "LD-rng", "LD-rng-i",
87 "disre_initf", "disre_rm3tav",
88 "orire_initf", "orire_Dtav",
89 "svir_prev", "nosehoover-vxi", "v_eta", "vol0", "nhpres_xi", "nhpres_vxi", "fvir_prev",
92 enum { eeksEKIN_N, eeksEKINH, eeksDEKINDL, eeksMVCOS, eeksEKINF, eeksEKINO, eeksEKINSCALEF, eeksEKINSCALEH, eeksVSCALE, eeksEKINTOTAL, eeksNR };
94 const char *eeks_names[eeksNR]=
96 "Ekin_n", "Ekinh", "dEkindlambda", "mv_cos",
97 "Ekinf", "Ekinh_old", "EkinScaleF_NHC", "EkinScaleH_NHC","Vscale_NHC","Ekin_Total"
100 enum { eenhENERGY_N, eenhENERGY_AVER, eenhENERGY_SUM, eenhENERGY_NSUM,
101 eenhENERGY_SUM_SIM, eenhENERGY_NSUM_SIM,
102 eenhENERGY_NSTEPS, eenhENERGY_NSTEPS_SIM,
103 eenhENERGY_DELTA_H_NN,
104 eenhENERGY_DELTA_H_LIST, eenhENERGY_DELTA_H_STARTTIME,
107 const char *eenh_names[eenhNR]=
109 "energy_n", "energy_aver", "energy_sum", "energy_nsum",
110 "energy_sum_sim", "energy_nsum_sim",
111 "energy_nsteps", "energy_nsteps_sim",
113 "energy_delta_h_list", "energy_delta_h_starttime"
118 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
120 gmx_wintruncate(const char *filename, __int64 size)
123 /*we do this elsewhere*/
129 fp=fopen(filename,"rb+");
136 return _chsize_s( fileno(fp), size);
142 enum { ecprREAL, ecprRVEC, ecprMATRIX };
144 static const char *st_names(int cptp,int ecpt)
148 case 0: return est_names [ecpt]; break;
149 case 1: return eeks_names[ecpt]; break;
150 case 2: return eenh_names[ecpt]; break;
156 static void cp_warning(FILE *fp)
158 fprintf(fp,"\nWARNING: Checkpoint file is corrupted or truncated\n\n");
161 static void cp_error()
163 gmx_fatal(FARGS,"Checkpoint file corrupted/truncated, or maybe you are out of quota?");
166 static void do_cpt_string_err(XDR *xd,gmx_bool bRead,const char *desc,char **s,FILE *list)
168 #define CPTSTRLEN 1024
175 res = xdr_string(xd,s,CPTSTRLEN);
182 fprintf(list,"%s = %s\n",desc,*s);
187 static int do_cpt_int(XDR *xd,const char *desc,int *i,FILE *list)
198 fprintf(list,"%s = %d\n",desc,*i);
203 static int do_cpt_u_chars(XDR *xd,const char *desc,int n,unsigned char *i,FILE *list)
209 fprintf(list,"%s = ",desc);
211 for (j=0; j<n && res; j++)
213 res &= xdr_u_char(xd,&i[j]);
216 fprintf(list,"%02x",i[j]);
231 static void do_cpt_int_err(XDR *xd,const char *desc,int *i,FILE *list)
233 if (do_cpt_int(xd,desc,i,list) < 0)
239 static void do_cpt_step_err(XDR *xd,const char *desc,gmx_large_int_t *i,FILE *list)
242 char buf[STEPSTRSIZE];
244 res = xdr_gmx_large_int(xd,i,"reading checkpoint file");
251 fprintf(list,"%s = %s\n",desc,gmx_step_str(*i,buf));
255 static void do_cpt_double_err(XDR *xd,const char *desc,double *f,FILE *list)
259 res = xdr_double(xd,f);
266 fprintf(list,"%s = %f\n",desc,*f);
270 /* If nval >= 0, nval is used; on read this should match the passed value.
271 * If nval n<0, *nptr is used; on read the value is stored in nptr
273 static int do_cpte_reals_low(XDR *xd,int cptp,int ecpt,int sflags,
274 int nval,int *nptr,real **v,
275 FILE *list,int erealtype)
279 int dtc=xdr_datatype_float;
281 int dtc=xdr_datatype_double;
298 gmx_incons("*ntpr=NULL in do_cpte_reals_low");
303 res = xdr_int(xd,&nf);
314 gmx_fatal(FARGS,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp,ecpt),nval,nf);
323 res = xdr_int(xd,&dt);
330 fprintf(stderr,"Precision mismatch for state entry %s, code precision is %s, file precision is %s\n",
331 st_names(cptp,ecpt),xdr_datatype_names[dtc],
332 xdr_datatype_names[dt]);
334 if (list || !(sflags & (1<<ecpt)))
347 if (dt == xdr_datatype_float)
349 if (dtc == xdr_datatype_float)
357 res = xdr_vector(xd,(char *)vf,nf,
358 (unsigned int)sizeof(float),(xdrproc_t)xdr_float);
363 if (dtc != xdr_datatype_float)
374 if (dtc == xdr_datatype_double)
382 res = xdr_vector(xd,(char *)vd,nf,
383 (unsigned int)sizeof(double),(xdrproc_t)xdr_double);
388 if (dtc != xdr_datatype_double)
403 pr_reals(list,0,st_names(cptp,ecpt),vp,nf);
406 pr_rvecs(list,0,st_names(cptp,ecpt),(rvec *)vp,nf/3);
409 gmx_incons("Unknown checkpoint real type");
421 /* This function stores n along with the reals for reading,
422 * but on reading it assumes that n matches the value in the checkpoint file,
423 * a fatal error is generated when this is not the case.
425 static int do_cpte_reals(XDR *xd,int cptp,int ecpt,int sflags,
426 int n,real **v,FILE *list)
428 return do_cpte_reals_low(xd,cptp,ecpt,sflags,n,NULL,v,list,ecprREAL);
431 /* This function does the same as do_cpte_reals,
432 * except that on reading it ignores the passed value of *n
433 * and stored the value read from the checkpoint file in *n.
435 static int do_cpte_n_reals(XDR *xd,int cptp,int ecpt,int sflags,
436 int *n,real **v,FILE *list)
438 return do_cpte_reals_low(xd,cptp,ecpt,sflags,-1,n,v,list,ecprREAL);
441 static int do_cpte_real(XDR *xd,int cptp,int ecpt,int sflags,
446 return do_cpte_reals_low(xd,cptp,ecpt,sflags,1,NULL,&r,list,ecprREAL);
449 static int do_cpte_ints(XDR *xd,int cptp,int ecpt,int sflags,
450 int n,int **v,FILE *list)
453 int dtc=xdr_datatype_int;
458 res = xdr_int(xd,&nf);
463 if (list == NULL && v != NULL && nf != n)
465 gmx_fatal(FARGS,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp,ecpt),n,nf);
468 res = xdr_int(xd,&dt);
475 gmx_fatal(FARGS,"Type mismatch for state entry %s, code type is %s, file type is %s\n",
476 st_names(cptp,ecpt),xdr_datatype_names[dtc],
477 xdr_datatype_names[dt]);
479 if (list || !(sflags & (1<<ecpt)) || v == NULL)
492 res = xdr_vector(xd,(char *)vp,nf,
493 (unsigned int)sizeof(int),(xdrproc_t)xdr_int);
500 pr_ivec(list,0,st_names(cptp,ecpt),vp,nf,TRUE);
510 static int do_cpte_int(XDR *xd,int cptp,int ecpt,int sflags,
513 return do_cpte_ints(xd,cptp,ecpt,sflags,1,&i,list);
516 static int do_cpte_doubles(XDR *xd,int cptp,int ecpt,int sflags,
517 int n,double **v,FILE *list)
520 int dtc=xdr_datatype_double;
525 res = xdr_int(xd,&nf);
530 if (list == NULL && nf != n)
532 gmx_fatal(FARGS,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp,ecpt),n,nf);
535 res = xdr_int(xd,&dt);
542 gmx_fatal(FARGS,"Precision mismatch for state entry %s, code precision is %s, file precision is %s\n",
543 st_names(cptp,ecpt),xdr_datatype_names[dtc],
544 xdr_datatype_names[dt]);
546 if (list || !(sflags & (1<<ecpt)))
559 res = xdr_vector(xd,(char *)vp,nf,
560 (unsigned int)sizeof(double),(xdrproc_t)xdr_double);
567 pr_doubles(list,0,st_names(cptp,ecpt),vp,nf);
578 static int do_cpte_rvecs(XDR *xd,int cptp,int ecpt,int sflags,
579 int n,rvec **v,FILE *list)
583 return do_cpte_reals_low(xd,cptp,ecpt,sflags,
584 n*DIM,NULL,(real **)v,list,ecprRVEC);
587 static int do_cpte_matrix(XDR *xd,int cptp,int ecpt,int sflags,
593 vr = (real *)&(v[0][0]);
594 ret = do_cpte_reals_low(xd,cptp,ecpt,sflags,
595 DIM*DIM,NULL,&vr,NULL,ecprMATRIX);
597 if (list && ret == 0)
599 pr_rvecs(list,0,st_names(cptp,ecpt),v,DIM);
605 static int do_cpte_matrices(XDR *xd,int cptp,int ecpt,int sflags,
606 int n,matrix **v,FILE *list)
615 res = xdr_int(xd,&nf);
620 if (list == NULL && nf != n)
622 gmx_fatal(FARGS,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp,ecpt),n,nf);
624 if (list || !(sflags & (1<<ecpt)))
644 vr[(i*DIM+j)*DIM+k] = vp[i][j][k];
648 ret = do_cpte_reals_low(xd,cptp,ecpt,sflags,
649 nf*DIM*DIM,NULL,&vr,NULL,ecprMATRIX);
656 vp[i][j][k] = vr[(i*DIM+j)*DIM+k];
662 if (list && ret == 0)
666 pr_rvecs(list,0,st_names(cptp,ecpt),vp[i],DIM);
677 static void do_cpt_header(XDR *xd,gmx_bool bRead,int *file_version,
678 char **version,char **btime,char **buser,char **bmach,
679 char **fprog,char **ftime,
680 int *eIntegrator,int *simulation_part,
681 gmx_large_int_t *step,double *t,
682 int *nnodes,int *dd_nc,int *npme,
683 int *natoms,int *ngtc, int *nnhpres, int *nhchainlength,
684 int *flags_state,int *flags_eks,int *flags_enh,
701 res = xdr_int(xd,&magic);
704 gmx_fatal(FARGS,"The checkpoint file is empty/corrupted, or maybe you are out of quota?");
706 if (magic != CPT_MAGIC1)
708 gmx_fatal(FARGS,"Start of file magic number mismatch, checkpoint file has %d, should be %d\n"
709 "The checkpoint file is corrupted or not a checkpoint file",
716 if (gethostname(fhost,255) != 0)
718 sprintf(fhost,"unknown");
721 sprintf(fhost,"unknown");
724 do_cpt_string_err(xd,bRead,"GROMACS version" ,version,list);
725 do_cpt_string_err(xd,bRead,"GROMACS build time" ,btime,list);
726 do_cpt_string_err(xd,bRead,"GROMACS build user" ,buser,list);
727 do_cpt_string_err(xd,bRead,"GROMACS build machine" ,bmach,list);
728 do_cpt_string_err(xd,bRead,"generating program" ,fprog,list);
729 do_cpt_string_err(xd,bRead,"generation time" ,ftime,list);
730 *file_version = cpt_version;
731 do_cpt_int_err(xd,"checkpoint file version",file_version,list);
732 if (*file_version > cpt_version)
734 gmx_fatal(FARGS,"Attempting to read a checkpoint file of version %d with code of version %d\n",*file_version,cpt_version);
736 if (*file_version >= 12)
738 do_cpt_string_err(xd,bRead,"generating host" ,&fhost,list);
744 do_cpt_int_err(xd,"#atoms" ,natoms ,list);
745 do_cpt_int_err(xd,"#T-coupling groups",ngtc ,list);
746 if (*file_version >= 10)
748 do_cpt_int_err(xd,"#Nose-Hoover T-chains",nhchainlength,list);
754 if (*file_version >= 11)
756 do_cpt_int_err(xd,"#Nose-Hoover T-chains for barostat ",nnhpres,list);
762 do_cpt_int_err(xd,"integrator" ,eIntegrator,list);
763 if (*file_version >= 3)
765 do_cpt_int_err(xd,"simulation part #", simulation_part,list);
769 *simulation_part = 1;
771 if (*file_version >= 5)
773 do_cpt_step_err(xd,"step" ,step ,list);
777 do_cpt_int_err(xd,"step" ,&idum ,list);
780 do_cpt_double_err(xd,"t" ,t ,list);
781 do_cpt_int_err(xd,"#PP-nodes" ,nnodes ,list);
783 do_cpt_int_err(xd,"dd_nc[x]",dd_nc ? &(dd_nc[0]) : &idum,list);
784 do_cpt_int_err(xd,"dd_nc[y]",dd_nc ? &(dd_nc[1]) : &idum,list);
785 do_cpt_int_err(xd,"dd_nc[z]",dd_nc ? &(dd_nc[2]) : &idum,list);
786 do_cpt_int_err(xd,"#PME-only nodes",npme,list);
787 do_cpt_int_err(xd,"state flags",flags_state,list);
788 if (*file_version >= 4)
790 do_cpt_int_err(xd,"ekin data flags",flags_eks,list);
791 do_cpt_int_err(xd,"energy history flags",flags_enh,list);
796 *flags_enh = (*flags_state >> (estORIRE_DTAV+1));
797 *flags_state = (*flags_state & ~((1<<(estORIRE_DTAV+1)) |
798 (1<<(estORIRE_DTAV+2)) |
799 (1<<(estORIRE_DTAV+3))));
803 static int do_cpt_footer(XDR *xd,gmx_bool bRead,int file_version)
808 if (file_version >= 2)
811 res = xdr_int(xd,&magic);
816 if (magic != CPT_MAGIC2)
825 static int do_cpt_state(XDR *xd,gmx_bool bRead,
826 int fflags,t_state *state,
827 gmx_bool bReadRNG,FILE *list)
830 int **rng_p,**rngi_p;
837 nnht = state->nhchainlength*state->ngtc;
838 nnhtp = state->nhchainlength*state->nnhpres;
842 rng_p = (int **)&state->ld_rng;
843 rngi_p = &state->ld_rngi;
847 /* Do not read the RNG data */
852 sflags = state->flags;
853 for(i=0; (i<estNR && ret == 0); i++)
859 case estLAMBDA: ret = do_cpte_real(xd,0,i,sflags,&state->lambda,list); break;
860 case estBOX: ret = do_cpte_matrix(xd,0,i,sflags,state->box,list); break;
861 case estBOX_REL: ret = do_cpte_matrix(xd,0,i,sflags,state->box_rel,list); break;
862 case estBOXV: ret = do_cpte_matrix(xd,0,i,sflags,state->boxv,list); break;
863 case estPRES_PREV: ret = do_cpte_matrix(xd,0,i,sflags,state->pres_prev,list); break;
864 case estSVIR_PREV: ret = do_cpte_matrix(xd,0,i,sflags,state->svir_prev,list); break;
865 case estFVIR_PREV: ret = do_cpte_matrix(xd,0,i,sflags,state->fvir_prev,list); break;
866 case estNH_XI: ret = do_cpte_doubles(xd,0,i,sflags,nnht,&state->nosehoover_xi,list); break;
867 case estNH_VXI: ret = do_cpte_doubles(xd,0,i,sflags,nnht,&state->nosehoover_vxi,list); break;
868 case estNHPRES_XI: ret = do_cpte_doubles(xd,0,i,sflags,nnhtp,&state->nhpres_xi,list); break;
869 case estNHPRES_VXI: ret = do_cpte_doubles(xd,0,i,sflags,nnhtp,&state->nhpres_vxi,list); break;
870 case estTC_INT: ret = do_cpte_doubles(xd,0,i,sflags,state->ngtc,&state->therm_integral,list); break;
871 case estVETA: ret = do_cpte_real(xd,0,i,sflags,&state->veta,list); break;
872 case estVOL0: ret = do_cpte_real(xd,0,i,sflags,&state->vol0,list); break;
873 case estX: ret = do_cpte_rvecs(xd,0,i,sflags,state->natoms,&state->x,list); break;
874 case estV: ret = do_cpte_rvecs(xd,0,i,sflags,state->natoms,&state->v,list); break;
875 case estSDX: ret = do_cpte_rvecs(xd,0,i,sflags,state->natoms,&state->sd_X,list); break;
876 case estLD_RNG: ret = do_cpte_ints(xd,0,i,sflags,state->nrng,rng_p,list); break;
877 case estLD_RNGI: ret = do_cpte_ints(xd,0,i,sflags,state->nrngi,rngi_p,list); break;
878 case estDISRE_INITF: ret = do_cpte_real (xd,0,i,sflags,&state->hist.disre_initf,list); break;
879 case estDISRE_RM3TAV: ret = do_cpte_n_reals(xd,0,i,sflags,&state->hist.ndisrepairs,&state->hist.disre_rm3tav,list); break;
880 case estORIRE_INITF: ret = do_cpte_real (xd,0,i,sflags,&state->hist.orire_initf,list); break;
881 case estORIRE_DTAV: ret = do_cpte_n_reals(xd,0,i,sflags,&state->hist.norire_Dtav,&state->hist.orire_Dtav,list); break;
883 gmx_fatal(FARGS,"Unknown state entry %d\n"
884 "You are probably reading a new checkpoint file with old code",i);
892 static int do_cpt_ekinstate(XDR *xd,gmx_bool bRead,
893 int fflags,ekinstate_t *ekins,
901 for(i=0; (i<eeksNR && ret == 0); i++)
908 case eeksEKIN_N: ret = do_cpte_int(xd,1,i,fflags,&ekins->ekin_n,list); break;
909 case eeksEKINH : ret = do_cpte_matrices(xd,1,i,fflags,ekins->ekin_n,&ekins->ekinh,list); break;
910 case eeksEKINF: ret = do_cpte_matrices(xd,1,i,fflags,ekins->ekin_n,&ekins->ekinf,list); break;
911 case eeksEKINO: ret = do_cpte_matrices(xd,1,i,fflags,ekins->ekin_n,&ekins->ekinh_old,list); break;
912 case eeksEKINTOTAL: ret = do_cpte_matrix(xd,1,i,fflags,ekins->ekin_total,list); break;
913 case eeksEKINSCALEF: ret = do_cpte_doubles(xd,1,i,fflags,ekins->ekin_n,&ekins->ekinscalef_nhc,list); break;
914 case eeksVSCALE: ret = do_cpte_doubles(xd,1,i,fflags,ekins->ekin_n,&ekins->vscale_nhc,list); break;
915 case eeksEKINSCALEH: ret = do_cpte_doubles(xd,1,i,fflags,ekins->ekin_n,&ekins->ekinscaleh_nhc,list); break;
916 case eeksDEKINDL : ret = do_cpte_real(xd,1,i,fflags,&ekins->dekindl,list); break;
917 case eeksMVCOS: ret = do_cpte_real(xd,1,i,fflags,&ekins->mvcos,list); break;
919 gmx_fatal(FARGS,"Unknown ekin data state entry %d\n"
920 "You are probably reading a new checkpoint file with old code",i);
929 static int do_cpt_enerhist(XDR *xd,gmx_bool bRead,
930 int fflags,energyhistory_t *enerhist,
941 enerhist->nsteps = 0;
943 enerhist->nsteps_sim = 0;
944 enerhist->nsum_sim = 0;
945 enerhist->dht = NULL;
947 if (fflags & (1<< eenhENERGY_DELTA_H_NN) )
949 snew(enerhist->dht,1);
950 enerhist->dht->ndh = NULL;
951 enerhist->dht->dh = NULL;
955 for(i=0; (i<eenhNR && ret == 0); i++)
961 case eenhENERGY_N: ret = do_cpte_int(xd,2,i,fflags,&enerhist->nener,list); break;
962 case eenhENERGY_AVER: ret = do_cpte_doubles(xd,2,i,fflags,enerhist->nener,&enerhist->ener_ave,list); break;
963 case eenhENERGY_SUM: ret = do_cpte_doubles(xd,2,i,fflags,enerhist->nener,&enerhist->ener_sum,list); break;
964 case eenhENERGY_NSUM: do_cpt_step_err(xd,eenh_names[i],&enerhist->nsum,list); break;
965 case eenhENERGY_SUM_SIM: ret = do_cpte_doubles(xd,2,i,fflags,enerhist->nener,&enerhist->ener_sum_sim,list); break;
966 case eenhENERGY_NSUM_SIM: do_cpt_step_err(xd,eenh_names[i],&enerhist->nsum_sim,list); break;
967 case eenhENERGY_NSTEPS: do_cpt_step_err(xd,eenh_names[i],&enerhist->nsteps,list); break;
968 case eenhENERGY_NSTEPS_SIM: do_cpt_step_err(xd,eenh_names[i],&enerhist->nsteps_sim,list); break;
969 case eenhENERGY_DELTA_H_NN: do_cpt_int_err(xd,eenh_names[i], &(enerhist->dht->nndh), list);
970 if (bRead) /* now allocate memory for it */
972 snew(enerhist->dht->dh, enerhist->dht->nndh);
973 snew(enerhist->dht->ndh, enerhist->dht->nndh);
974 for(j=0;j<enerhist->dht->nndh;j++)
976 enerhist->dht->ndh[j] = 0;
977 enerhist->dht->dh[j] = NULL;
981 case eenhENERGY_DELTA_H_LIST:
982 for(j=0;j<enerhist->dht->nndh;j++)
984 ret=do_cpte_n_reals(xd, 2, i, fflags, &enerhist->dht->ndh[j], &(enerhist->dht->dh[j]), list);
987 case eenhENERGY_DELTA_H_STARTTIME:
988 ret=do_cpte_real(xd, 2, i, fflags, &(enerhist->dht->starttime), list); break;
990 gmx_fatal(FARGS,"Unknown energy history entry %d\n"
991 "You are probably reading a new checkpoint file with old code",i);
996 if ((fflags & (1<<eenhENERGY_SUM)) && !(fflags & (1<<eenhENERGY_SUM_SIM)))
998 /* Assume we have an old file format and copy sum to sum_sim */
999 srenew(enerhist->ener_sum_sim,enerhist->nener);
1000 for(i=0; i<enerhist->nener; i++)
1002 enerhist->ener_sum_sim[i] = enerhist->ener_sum[i];
1004 fflags |= (1<<eenhENERGY_SUM_SIM);
1007 if ( (fflags & (1<<eenhENERGY_NSUM)) &&
1008 !(fflags & (1<<eenhENERGY_NSTEPS)))
1010 /* Assume we have an old file format and copy nsum to nsteps */
1011 enerhist->nsteps = enerhist->nsum;
1012 fflags |= (1<<eenhENERGY_NSTEPS);
1014 if ( (fflags & (1<<eenhENERGY_NSUM_SIM)) &&
1015 !(fflags & (1<<eenhENERGY_NSTEPS_SIM)))
1017 /* Assume we have an old file format and copy nsum to nsteps */
1018 enerhist->nsteps_sim = enerhist->nsum_sim;
1019 fflags |= (1<<eenhENERGY_NSTEPS_SIM);
1025 static int do_cpt_files(XDR *xd, gmx_bool bRead,
1026 gmx_file_position_t **p_outputfiles, int *nfiles,
1027 FILE *list, int file_version)
1031 gmx_off_t mask = 0xFFFFFFFFL;
1032 int offset_high,offset_low;
1034 gmx_file_position_t *outputfiles;
1036 if (do_cpt_int(xd,"number of output files",nfiles,list) != 0)
1043 snew(*p_outputfiles,*nfiles);
1046 outputfiles = *p_outputfiles;
1048 for(i=0;i<*nfiles;i++)
1050 /* 64-bit XDR numbers are not portable, so it is stored as separate high/low fractions */
1053 do_cpt_string_err(xd,bRead,"output filename",&buf,list);
1054 strncpy(outputfiles[i].filename,buf,CPTSTRLEN-1);
1060 if (do_cpt_int(xd,"file_offset_high",&offset_high,list) != 0)
1064 if (do_cpt_int(xd,"file_offset_low",&offset_low,list) != 0)
1068 #if (SIZEOF_GMX_OFF_T > 4)
1069 outputfiles[i].offset = ( ((gmx_off_t) offset_high) << 32 ) | ( (gmx_off_t) offset_low & mask );
1071 outputfiles[i].offset = offset_low;
1076 buf = outputfiles[i].filename;
1077 do_cpt_string_err(xd,bRead,"output filename",&buf,list);
1079 offset = outputfiles[i].offset;
1087 #if (SIZEOF_GMX_OFF_T > 4)
1088 offset_low = (int) (offset & mask);
1089 offset_high = (int) ((offset >> 32) & mask);
1091 offset_low = offset;
1095 if (do_cpt_int(xd,"file_offset_high",&offset_high,list) != 0)
1099 if (do_cpt_int(xd,"file_offset_low",&offset_low,list) != 0)
1104 if (file_version >= 8)
1106 if (do_cpt_int(xd,"file_checksum_size",&(outputfiles[i].chksum_size),
1111 if (do_cpt_u_chars(xd,"file_checksum",16,outputfiles[i].chksum,list) != 0)
1118 outputfiles[i].chksum_size = -1;
1125 void write_checkpoint(const char *fn,gmx_bool bNumberAndKeep,
1126 FILE *fplog,t_commrec *cr,
1127 int eIntegrator,int simulation_part,
1128 gmx_large_int_t step,double t,t_state *state)
1137 char *fntemp; /* the temporary checkpoint file name */
1139 char timebuf[STRLEN];
1140 int nppnodes,npmenodes,flag_64bit;
1141 char buf[1024],suffix[5+STEPSTRSIZE],sbuf[STEPSTRSIZE];
1142 gmx_file_position_t *outputfiles;
1145 int flags_eks,flags_enh,i;
1150 if (DOMAINDECOMP(cr))
1152 nppnodes = cr->dd->nnodes;
1153 npmenodes = cr->npmenodes;
1157 nppnodes = cr->nnodes;
1167 /* make the new temporary filename */
1168 snew(fntemp, strlen(fn)+5+STEPSTRSIZE);
1170 fntemp[strlen(fn) - strlen(ftp2ext(fn2ftp(fn))) - 1] = '\0';
1171 sprintf(suffix,"_%s%s","step",gmx_step_str(step,sbuf));
1172 strcat(fntemp,suffix);
1173 strcat(fntemp,fn+strlen(fn) - strlen(ftp2ext(fn2ftp(fn))) - 1);
1176 gmx_ctime_r(&now,timebuf,STRLEN);
1180 fprintf(fplog,"Writing checkpoint, step %s at %s\n\n",
1181 gmx_step_str(step,buf),timebuf);
1184 /* Get offsets for open files */
1185 gmx_fio_get_output_file_positions(&outputfiles, &noutputfiles);
1187 fp = gmx_fio_open(fntemp,"w");
1189 if (state->ekinstate.bUpToDate)
1192 ((1<<eeksEKIN_N) | (1<<eeksEKINH) | (1<<eeksEKINF) |
1193 (1<<eeksEKINO) | (1<<eeksEKINSCALEF) | (1<<eeksEKINSCALEH) |
1194 (1<<eeksVSCALE) | (1<<eeksDEKINDL) | (1<<eeksMVCOS));
1202 if (state->enerhist.nsum > 0 || state->enerhist.nsum_sim > 0)
1204 flags_enh |= (1<<eenhENERGY_N);
1205 if (state->enerhist.nsum > 0)
1207 flags_enh |= ((1<<eenhENERGY_AVER) | (1<<eenhENERGY_SUM) |
1208 (1<<eenhENERGY_NSTEPS) | (1<<eenhENERGY_NSUM));
1210 if (state->enerhist.nsum_sim > 0)
1212 flags_enh |= ((1<<eenhENERGY_SUM_SIM) | (1<<eenhENERGY_NSTEPS_SIM) |
1213 (1<<eenhENERGY_NSUM_SIM));
1215 if (state->enerhist.dht)
1217 flags_enh |= ( (1<< eenhENERGY_DELTA_H_NN) |
1218 (1<< eenhENERGY_DELTA_H_LIST) |
1219 (1<< eenhENERGY_DELTA_H_STARTTIME) );
1224 version = strdup(VERSION);
1225 btime = strdup(BUILD_TIME);
1226 buser = strdup(BUILD_USER);
1227 bmach = strdup(BUILD_MACHINE);
1228 fprog = strdup(Program());
1230 ftime = &(timebuf[0]);
1232 do_cpt_header(gmx_fio_getxdr(fp),FALSE,&file_version,
1233 &version,&btime,&buser,&bmach,&fprog,&ftime,
1234 &eIntegrator,&simulation_part,&step,&t,&nppnodes,
1235 DOMAINDECOMP(cr) ? cr->dd->nc : NULL,&npmenodes,
1236 &state->natoms,&state->ngtc,&state->nnhpres,
1237 &state->nhchainlength, &state->flags,&flags_eks,&flags_enh,
1246 if((do_cpt_state(gmx_fio_getxdr(fp),FALSE,state->flags,state,TRUE,NULL) < 0) ||
1247 (do_cpt_ekinstate(gmx_fio_getxdr(fp),FALSE,flags_eks,&state->ekinstate,NULL) < 0)||
1248 (do_cpt_enerhist(gmx_fio_getxdr(fp),FALSE,flags_enh,&state->enerhist,NULL) < 0) ||
1249 (do_cpt_files(gmx_fio_getxdr(fp),FALSE,&outputfiles,&noutputfiles,NULL,
1252 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1255 do_cpt_footer(gmx_fio_getxdr(fp),FALSE,file_version);
1257 /* we really, REALLY, want to make sure to physically write the checkpoint,
1258 and all the files it depends on, out to disk. Because we've
1259 opened the checkpoint with gmx_fio_open(), it's in our list
1261 ret=gmx_fio_all_output_fsync();
1267 "Cannot fsync '%s'; maybe you are out of disk space or quota?",
1268 gmx_fio_getname(ret));
1270 if (getenv(GMX_IGNORE_FSYNC_FAILURE_ENV)==NULL)
1280 if( gmx_fio_close(fp) != 0)
1282 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1285 /* we don't move the checkpoint if the user specified they didn't want it,
1286 or if the fsyncs failed */
1287 if (!bNumberAndKeep && !ret)
1291 /* Rename the previous checkpoint file */
1293 buf[strlen(fn) - strlen(ftp2ext(fn2ftp(fn))) - 1] = '\0';
1294 strcat(buf,"_prev");
1295 strcat(buf,fn+strlen(fn) - strlen(ftp2ext(fn2ftp(fn))) - 1);
1297 /* we copy here so that if something goes wrong between now and
1298 * the rename below, there's always a state.cpt.
1299 * If renames are atomic (such as in POSIX systems),
1300 * this copying should be unneccesary.
1302 gmx_file_copy(fn, buf, FALSE);
1303 /* We don't really care if this fails:
1304 * there's already a new checkpoint.
1307 gmx_file_rename(fn, buf);
1310 if (gmx_file_rename(fntemp, fn) != 0)
1312 gmx_file("Cannot rename checkpoint file; maybe you are out of quota?");
1320 /*code for alternate checkpointing scheme. moved from top of loop over
1322 fcRequestCheckPoint();
1323 if ( fcCheckPointParallel( cr->nodeid, NULL,0) == 0 ) {
1324 gmx_fatal( 3,__FILE__,__LINE__, "Checkpoint error on step %d\n", step );
1326 #endif /* end GMX_FAHCORE block */
1329 static void print_flag_mismatch(FILE *fplog,int sflags,int fflags)
1333 fprintf(fplog,"\nState entry mismatch between the simulation and the checkpoint file\n");
1334 fprintf(fplog,"Entries which are not present in the checkpoint file will not be updated\n");
1335 fprintf(fplog," %24s %11s %11s\n","","simulation","checkpoint");
1336 for(i=0; i<estNR; i++)
1338 if ((sflags & (1<<i)) || (fflags & (1<<i)))
1340 fprintf(fplog," %24s %11s %11s\n",
1342 (sflags & (1<<i)) ? " present " : "not present",
1343 (fflags & (1<<i)) ? " present " : "not present");
1348 static void check_int(FILE *fplog,const char *type,int p,int f,gmx_bool *mm)
1350 FILE *fp = fplog ? fplog : stderr;
1354 fprintf(fp," %s mismatch,\n",type);
1355 fprintf(fp," current program: %d\n",p);
1356 fprintf(fp," checkpoint file: %d\n",f);
1362 static void check_string(FILE *fplog,const char *type,const char *p,
1363 const char *f,gmx_bool *mm)
1365 FILE *fp = fplog ? fplog : stderr;
1367 if (strcmp(p,f) != 0)
1369 fprintf(fp," %s mismatch,\n",type);
1370 fprintf(fp," current program: %s\n",p);
1371 fprintf(fp," checkpoint file: %s\n",f);
1377 static void check_match(FILE *fplog,
1379 char *btime,char *buser,char *bmach,char *fprog,
1380 t_commrec *cr,gmx_bool bPartDecomp,int npp_f,int npme_f,
1381 ivec dd_nc,ivec dd_nc_f)
1388 check_string(fplog,"Version" ,VERSION ,version,&mm);
1389 check_string(fplog,"Build time" ,BUILD_TIME ,btime ,&mm);
1390 check_string(fplog,"Build user" ,BUILD_USER ,buser ,&mm);
1391 check_string(fplog,"Build machine",BUILD_MACHINE,bmach ,&mm);
1392 check_string(fplog,"Program name" ,Program() ,fprog ,&mm);
1394 npp = cr->nnodes - cr->npmenodes;
1395 check_int (fplog,"#nodes" ,cr->nnodes ,npp_f+npme_f ,&mm);
1404 check_int (fplog,"#PME-nodes" ,cr->npmenodes,npme_f ,&mm);
1407 check_int (fplog,"#DD-cells[x]",dd_nc[XX] ,dd_nc_f[XX],&mm);
1408 check_int (fplog,"#DD-cells[y]",dd_nc[YY] ,dd_nc_f[YY],&mm);
1409 check_int (fplog,"#DD-cells[z]",dd_nc[ZZ] ,dd_nc_f[ZZ],&mm);
1416 "Gromacs binary or parallel settings not identical to previous run.\n"
1417 "Continuation is exact, but is not guaranteed to be binary identical%s.\n\n",
1418 fplog ? ",\n see the log file for details" : "");
1423 "Gromacs binary or parallel settings not identical to previous run.\n"
1424 "Continuation is exact, but is not guaranteed to be binary identical.\n\n");
1429 static void read_checkpoint(const char *fn,FILE **pfplog,
1430 t_commrec *cr,gmx_bool bPartDecomp,ivec dd_nc,
1431 int eIntegrator,gmx_large_int_t *step,double *t,
1432 t_state *state,gmx_bool *bReadRNG,gmx_bool *bReadEkin,
1433 int *simulation_part,gmx_bool bAppendOutputFiles)
1438 char *version,*btime,*buser,*bmach,*fprog,*ftime;
1439 char filename[STRLEN],buf[STEPSTRSIZE];
1440 int nppnodes,eIntegrator_f,nppnodes_f,npmenodes_f;
1442 int natoms,ngtc,nnhpres,nhchainlength,fflags,flags_eks,flags_enh;
1445 gmx_file_position_t *outputfiles;
1447 t_fileio *chksum_file;
1448 FILE* fplog = *pfplog;
1449 unsigned char digest[16];
1450 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1451 struct flock fl = { 0, SEEK_SET, 0, F_WRLCK, 0 };
1454 const char *int_warn=
1455 "WARNING: The checkpoint file was generator with integrator %s,\n"
1456 " while the simulation uses integrator %s\n\n";
1457 const char *sd_note=
1458 "NOTE: The checkpoint file was for %d nodes doing SD or BD,\n"
1459 " while the simulation uses %d SD or BD nodes,\n"
1460 " continuation will be exact, except for the random state\n\n";
1465 "read_checkpoint not (yet) supported with particle decomposition");
1468 fp = gmx_fio_open(fn,"r");
1469 do_cpt_header(gmx_fio_getxdr(fp),TRUE,&file_version,
1470 &version,&btime,&buser,&bmach,&fprog,&ftime,
1471 &eIntegrator_f,simulation_part,step,t,
1472 &nppnodes_f,dd_nc_f,&npmenodes_f,
1473 &natoms,&ngtc,&nnhpres,&nhchainlength,
1474 &fflags,&flags_eks,&flags_enh,NULL);
1476 if (cr == NULL || MASTER(cr))
1478 fprintf(stderr,"\nReading checkpoint file %s generated: %s\n\n",
1482 /* This will not be written if we do appending, since fplog is still NULL then */
1485 fprintf(fplog,"\n");
1486 fprintf(fplog,"Reading checkpoint file %s\n",fn);
1487 fprintf(fplog," file generated by: %s\n",fprog);
1488 fprintf(fplog," file generated at: %s\n",ftime);
1489 fprintf(fplog," GROMACS build time: %s\n",btime);
1490 fprintf(fplog," GROMACS build user: %s\n",buser);
1491 fprintf(fplog," GROMACS build machine: %s\n",bmach);
1492 fprintf(fplog," simulation part #: %d\n",*simulation_part);
1493 fprintf(fplog," step: %s\n",gmx_step_str(*step,buf));
1494 fprintf(fplog," time: %f\n",*t);
1495 fprintf(fplog,"\n");
1498 if (natoms != state->natoms)
1500 gmx_fatal(FARGS,"Checkpoint file is for a system of %d atoms, while the current system consists of %d atoms",natoms,state->natoms);
1502 if (ngtc != state->ngtc)
1504 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);
1506 if (nnhpres != state->nnhpres)
1508 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);
1511 init_gtc_state(state,state->ngtc,state->nnhpres,nhchainlength); /* need to keep this here to keep the tpr format working */
1512 /* write over whatever was read; we use the number of Nose-Hoover chains from the checkpoint */
1514 if (eIntegrator_f != eIntegrator)
1518 fprintf(stderr,int_warn,EI(eIntegrator_f),EI(eIntegrator));
1520 if(bAppendOutputFiles)
1523 "Output file appending requested, but input/checkpoint integrators do not match.\n"
1524 "Stopping the run to prevent you from ruining all your data...\n"
1525 "If you _really_ know what you are doing, try with the -noappend option.\n");
1529 fprintf(fplog,int_warn,EI(eIntegrator_f),EI(eIntegrator));
1537 else if (bPartDecomp)
1539 nppnodes = cr->nnodes;
1541 else if (cr->nnodes == nppnodes_f + npmenodes_f)
1543 if (cr->npmenodes < 0)
1545 cr->npmenodes = npmenodes_f;
1547 nppnodes = cr->nnodes - cr->npmenodes;
1548 if (nppnodes == nppnodes_f)
1550 for(d=0; d<DIM; d++)
1554 dd_nc[d] = dd_nc_f[d];
1561 /* The number of PP nodes has not been set yet */
1565 if ((EI_SD(eIntegrator) || eIntegrator == eiBD) && nppnodes > 0)
1567 /* Correct the RNG state size for the number of PP nodes.
1568 * Such assignments should all be moved to one central function.
1570 state->nrng = nppnodes*gmx_rng_n();
1571 state->nrngi = nppnodes;
1575 if (fflags != state->flags)
1580 if(bAppendOutputFiles)
1583 "Output file appending requested, but input and checkpoint states are not identical.\n"
1584 "Stopping the run to prevent you from ruining all your data...\n"
1585 "You can try with the -noappend option, and get more info in the log file.\n");
1589 "WARNING: The checkpoint state entries do not match the simulation,\n"
1590 " see the log file for details\n\n");
1595 print_flag_mismatch(fplog,state->flags,fflags);
1600 if ((EI_SD(eIntegrator) || eIntegrator == eiBD) &&
1601 nppnodes != nppnodes_f)
1606 fprintf(stderr,sd_note,nppnodes_f,nppnodes);
1610 fprintf(fplog ,sd_note,nppnodes_f,nppnodes);
1615 check_match(fplog,version,btime,buser,bmach,fprog,
1616 cr,bPartDecomp,nppnodes_f,npmenodes_f,dd_nc,dd_nc_f);
1619 ret = do_cpt_state(gmx_fio_getxdr(fp),TRUE,fflags,state,*bReadRNG,NULL);
1624 ret = do_cpt_ekinstate(gmx_fio_getxdr(fp),TRUE,
1625 flags_eks,&state->ekinstate,NULL);
1630 *bReadEkin = ((flags_eks & (1<<eeksEKINH)) || (flags_eks & (1<<eeksEKINF)) || (flags_eks & (1<<eeksEKINO)) ||
1631 (flags_eks & (1<<eeksEKINSCALEF)) | (flags_eks & (1<<eeksEKINSCALEH)) | (flags_eks & (1<<eeksVSCALE)));
1633 ret = do_cpt_enerhist(gmx_fio_getxdr(fp),TRUE,
1634 flags_enh,&state->enerhist,NULL);
1640 if (file_version < 6)
1642 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.";
1644 fprintf(stderr,"\nWARNING: %s\n\n",warn);
1647 fprintf(fplog,"\nWARNING: %s\n\n",warn);
1649 state->enerhist.nsum = *step;
1650 state->enerhist.nsum_sim = *step;
1653 ret = do_cpt_files(gmx_fio_getxdr(fp),TRUE,&outputfiles,&nfiles,NULL,file_version);
1659 ret = do_cpt_footer(gmx_fio_getxdr(fp),TRUE,file_version);
1664 if( gmx_fio_close(fp) != 0)
1666 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1675 /* If the user wants to append to output files,
1676 * we use the file pointer positions of the output files stored
1677 * in the checkpoint file and truncate the files such that any frames
1678 * written after the checkpoint time are removed.
1679 * All files are md5sum checked such that we can be sure that
1680 * we do not truncate other (maybe imprortant) files.
1682 if (bAppendOutputFiles)
1684 if (fn2ftp(outputfiles[0].filename)!=efLOG)
1686 /* make sure first file is log file so that it is OK to use it for
1689 gmx_fatal(FARGS,"The first output file should always be the log "
1690 "file but instead is: %s", outputfiles[0].filename);
1692 for(i=0;i<nfiles;i++)
1694 if (outputfiles[i].offset < 0)
1696 gmx_fatal(FARGS,"The original run wrote a file called '%s' which "
1697 "is larger than 2 GB, but mdrun did not support large file"
1698 " offsets. Can not append. Run mdrun with -noappend",
1699 outputfiles[i].filename);
1702 chksum_file=gmx_fio_open(outputfiles[i].filename,"a");
1705 chksum_file=gmx_fio_open(outputfiles[i].filename,"r+");
1710 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1711 if (fcntl(fileno(gmx_fio_getfp(chksum_file)), F_SETLK, &fl)
1714 if (_locking(fileno(gmx_fio_getfp(chksum_file)), _LK_NBLCK, LONG_MAX)==-1)
1717 gmx_fatal(FARGS,"Failed to lock: %s. Already running "
1718 "simulation?", outputfiles[i].filename);
1722 /* compute md5 chksum */
1723 if (outputfiles[i].chksum_size != -1)
1725 if (gmx_fio_get_file_md5(chksum_file,outputfiles[i].offset,
1726 digest) != outputfiles[i].chksum_size)
1728 gmx_fatal(FARGS,"Can't read %d bytes of '%s' to compute checksum. The file has been replaced or its contents has been modified.",
1729 outputfiles[i].chksum_size,
1730 outputfiles[i].filename);
1733 else if (i==0) /*log file need to be seeked even when not reading md5*/
1735 gmx_fio_seek(chksum_file,outputfiles[i].offset);
1739 if (i==0) /*open log file here - so that lock is never lifted
1740 after chksum is calculated */
1742 *pfplog = gmx_fio_getfp(chksum_file);
1746 gmx_fio_close(chksum_file);
1749 /* compare md5 chksum */
1750 if (outputfiles[i].chksum_size != -1 &&
1751 memcmp(digest,outputfiles[i].chksum,16)!=0)
1755 fprintf(debug,"chksum for %s: ",outputfiles[i].filename);
1756 for (j=0; j<16; j++)
1758 fprintf(debug,"%02x",digest[j]);
1760 fprintf(debug,"\n");
1762 gmx_fatal(FARGS,"Checksum wrong for '%s'. The file has been replaced or its contents has been modified.",
1763 outputfiles[i].filename);
1768 if (i!=0) /*log file is already seeked to correct position */
1770 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1771 rc = gmx_wintruncate(outputfiles[i].filename,outputfiles[i].offset);
1773 rc = truncate(outputfiles[i].filename,outputfiles[i].offset);
1777 gmx_fatal(FARGS,"Truncation of file %s failed.",outputfiles[i].filename);
1787 void load_checkpoint(const char *fn,FILE **fplog,
1788 t_commrec *cr,gmx_bool bPartDecomp,ivec dd_nc,
1789 t_inputrec *ir,t_state *state,
1790 gmx_bool *bReadRNG,gmx_bool *bReadEkin,gmx_bool bAppend)
1792 gmx_large_int_t step;
1795 if (SIMMASTER(cr)) {
1796 /* Read the state from the checkpoint file */
1797 read_checkpoint(fn,fplog,
1798 cr,bPartDecomp,dd_nc,
1799 ir->eI,&step,&t,state,bReadRNG,bReadEkin,
1800 &ir->simulation_part,bAppend);
1803 gmx_bcast(sizeof(cr->npmenodes),&cr->npmenodes,cr);
1804 gmx_bcast(DIM*sizeof(dd_nc[0]),dd_nc,cr);
1805 gmx_bcast(sizeof(step),&step,cr);
1806 gmx_bcast(sizeof(*bReadRNG),bReadRNG,cr);
1807 gmx_bcast(sizeof(*bReadEkin),bReadEkin,cr);
1809 ir->bContinuation = TRUE;
1810 if (ir->nsteps >= 0)
1812 ir->nsteps += ir->init_step - step;
1814 ir->init_step = step;
1815 ir->simulation_part += 1;
1818 static void read_checkpoint_data(t_fileio *fp,int *simulation_part,
1819 gmx_large_int_t *step,double *t,t_state *state,
1821 int *nfiles,gmx_file_position_t **outputfiles)
1824 char *version,*btime,*buser,*bmach,*fprog,*ftime;
1828 int flags_eks,flags_enh;
1830 gmx_file_position_t *files_loc=NULL;
1833 do_cpt_header(gmx_fio_getxdr(fp),TRUE,&file_version,
1834 &version,&btime,&buser,&bmach,&fprog,&ftime,
1835 &eIntegrator,simulation_part,step,t,&nppnodes,dd_nc,&npme,
1836 &state->natoms,&state->ngtc,&state->nnhpres,&state->nhchainlength,
1837 &state->flags,&flags_eks,&flags_enh,NULL);
1839 do_cpt_state(gmx_fio_getxdr(fp),TRUE,state->flags,state,bReadRNG,NULL);
1844 ret = do_cpt_ekinstate(gmx_fio_getxdr(fp),TRUE,
1845 flags_eks,&state->ekinstate,NULL);
1850 ret = do_cpt_enerhist(gmx_fio_getxdr(fp),TRUE,
1851 flags_enh,&state->enerhist,NULL);
1857 ret = do_cpt_files(gmx_fio_getxdr(fp),TRUE,
1858 outputfiles != NULL ? outputfiles : &files_loc,
1859 outputfiles != NULL ? nfiles : &nfiles_loc,
1861 if (files_loc != NULL)
1871 ret = do_cpt_footer(gmx_fio_getxdr(fp),TRUE,file_version);
1885 read_checkpoint_state(const char *fn,int *simulation_part,
1886 gmx_large_int_t *step,double *t,t_state *state)
1890 fp = gmx_fio_open(fn,"r");
1891 read_checkpoint_data(fp,simulation_part,step,t,state,TRUE,NULL,NULL);
1892 if( gmx_fio_close(fp) != 0)
1894 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1898 void read_checkpoint_trxframe(t_fileio *fp,t_trxframe *fr)
1901 int simulation_part;
1902 gmx_large_int_t step;
1905 init_state(&state,0,0,0,0);
1907 read_checkpoint_data(fp,&simulation_part,&step,&t,&state,FALSE,NULL,NULL);
1909 fr->natoms = state.natoms;
1912 fr->step = gmx_large_int_to_int(step,
1913 "conversion of checkpoint to trajectory");
1917 fr->lambda = state.lambda;
1919 fr->bX = (state.flags & (1<<estX));
1925 fr->bV = (state.flags & (1<<estV));
1932 fr->bBox = (state.flags & (1<<estBOX));
1935 copy_mat(state.box,fr->box);
1940 void list_checkpoint(const char *fn,FILE *out)
1944 char *version,*btime,*buser,*bmach,*fprog,*ftime;
1945 int eIntegrator,simulation_part,nppnodes,npme;
1946 gmx_large_int_t step;
1950 int flags_eks,flags_enh;
1954 gmx_file_position_t *outputfiles;
1957 init_state(&state,-1,-1,-1,-1);
1959 fp = gmx_fio_open(fn,"r");
1960 do_cpt_header(gmx_fio_getxdr(fp),TRUE,&file_version,
1961 &version,&btime,&buser,&bmach,&fprog,&ftime,
1962 &eIntegrator,&simulation_part,&step,&t,&nppnodes,dd_nc,&npme,
1963 &state.natoms,&state.ngtc,&state.nnhpres,&state.nhchainlength,
1964 &state.flags,&flags_eks,&flags_enh,out);
1965 ret = do_cpt_state(gmx_fio_getxdr(fp),TRUE,state.flags,&state,TRUE,out);
1970 ret = do_cpt_ekinstate(gmx_fio_getxdr(fp),TRUE,
1971 flags_eks,&state.ekinstate,out);
1976 ret = do_cpt_enerhist(gmx_fio_getxdr(fp),TRUE,
1977 flags_enh,&state.enerhist,out);
1981 do_cpt_files(gmx_fio_getxdr(fp),TRUE,&outputfiles,&nfiles,out,file_version);
1986 ret = do_cpt_footer(gmx_fio_getxdr(fp),TRUE,file_version);
1993 if( gmx_fio_close(fp) != 0)
1995 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
2002 static gmx_bool exist_output_file(const char *fnm_cp,int nfile,const t_filenm fnm[])
2006 /* Check if the output file name stored in the checkpoint file
2007 * is one of the output file names of mdrun.
2011 !(is_output(&fnm[i]) && strcmp(fnm_cp,fnm[i].fns[0]) == 0))
2016 return (i < nfile && gmx_fexist(fnm_cp));
2019 /* This routine cannot print tons of data, since it is called before the log file is opened. */
2020 gmx_bool read_checkpoint_simulation_part(const char *filename, int *simulation_part,
2021 gmx_large_int_t *cpt_step,t_commrec *cr,
2022 gmx_bool bAppendReq,
2023 int nfile,const t_filenm fnm[],
2024 const char *part_suffix,gmx_bool *bAddPart)
2027 gmx_large_int_t step=0;
2031 gmx_file_position_t *outputfiles;
2034 char *fn,suf_up[STRLEN];
2038 if (SIMMASTER(cr)) {
2039 if(!gmx_fexist(filename) || (!(fp = gmx_fio_open(filename,"r")) ))
2041 *simulation_part = 0;
2045 init_state(&state,0,0,0,0);
2047 read_checkpoint_data(fp,simulation_part,&step,&t,&state,FALSE,
2048 &nfiles,&outputfiles);
2049 if( gmx_fio_close(fp) != 0)
2051 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
2058 for(f=0; f<nfiles; f++)
2060 if (exist_output_file(outputfiles[f].filename,nfile,fnm))
2065 if (nexist == nfiles)
2067 bAppend = bAppendReq;
2069 else if (nexist > 0)
2072 "Output file appending has been requested,\n"
2073 "but some output files listed in the checkpoint file %s\n"
2074 "are not present or are named differently by the current program:\n",
2076 fprintf(stderr,"output files present:");
2077 for(f=0; f<nfiles; f++)
2079 if (exist_output_file(outputfiles[f].filename,
2082 fprintf(stderr," %s",outputfiles[f].filename);
2085 fprintf(stderr,"\n");
2086 fprintf(stderr,"output files not present or named differently:");
2087 for(f=0; f<nfiles; f++)
2089 if (!exist_output_file(outputfiles[f].filename,
2092 fprintf(stderr," %s",outputfiles[f].filename);
2095 fprintf(stderr,"\n");
2097 gmx_fatal(FARGS,"File appending requested, but only %d of the %d output files are present",nexist,nfiles);
2105 gmx_fatal(FARGS,"File appending requested, but no output file information is stored in the checkpoint file");
2107 fn = outputfiles[0].filename;
2108 if (strlen(fn) < 4 ||
2109 gmx_strcasecmp(fn+strlen(fn)-4,ftp2ext(efLOG)) == 0)
2111 gmx_fatal(FARGS,"File appending requested, but the log file is not the first file listed in the checkpoint file");
2113 /* Set bAddPart to whether the suffix string '.part' is present
2114 * in the log file name.
2116 strcpy(suf_up,part_suffix);
2118 *bAddPart = (strstr(fn,part_suffix) != NULL ||
2119 strstr(fn,suf_up) != NULL);
2127 gmx_bcast(sizeof(*simulation_part),simulation_part,cr);
2129 if (*simulation_part > 0 && bAppendReq)
2131 gmx_bcast(sizeof(bAppend),&bAppend,cr);
2132 gmx_bcast(sizeof(*bAddPart),bAddPart,cr);
2135 if (NULL != cpt_step)