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
,
105 eenhENERGY_DELTA_H_STARTTIME
,
106 eenhENERGY_DELTA_H_STARTLAMBDA
,
109 const char *eenh_names
[eenhNR
]=
111 "energy_n", "energy_aver", "energy_sum", "energy_nsum",
112 "energy_sum_sim", "energy_nsum_sim",
113 "energy_nsteps", "energy_nsteps_sim",
115 "energy_delta_h_list",
116 "energy_delta_h_start_time",
117 "energy_delta_h_start_lambda"
122 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
124 gmx_wintruncate(const char *filename
, __int64 size
)
127 /*we do this elsewhere*/
133 fp
=fopen(filename
,"rb+");
140 return _chsize_s( fileno(fp
), size
);
146 enum { ecprREAL
, ecprRVEC
, ecprMATRIX
};
148 static const char *st_names(int cptp
,int ecpt
)
152 case 0: return est_names
[ecpt
]; break;
153 case 1: return eeks_names
[ecpt
]; break;
154 case 2: return eenh_names
[ecpt
]; break;
160 static void cp_warning(FILE *fp
)
162 fprintf(fp
,"\nWARNING: Checkpoint file is corrupted or truncated\n\n");
165 static void cp_error()
167 gmx_fatal(FARGS
,"Checkpoint file corrupted/truncated, or maybe you are out of quota?");
170 static void do_cpt_string_err(XDR
*xd
,gmx_bool bRead
,const char *desc
,char **s
,FILE *list
)
172 #define CPTSTRLEN 1024
179 res
= xdr_string(xd
,s
,CPTSTRLEN
);
186 fprintf(list
,"%s = %s\n",desc
,*s
);
191 static int do_cpt_int(XDR
*xd
,const char *desc
,int *i
,FILE *list
)
202 fprintf(list
,"%s = %d\n",desc
,*i
);
207 static int do_cpt_u_chars(XDR
*xd
,const char *desc
,int n
,unsigned char *i
,FILE *list
)
213 fprintf(list
,"%s = ",desc
);
215 for (j
=0; j
<n
&& res
; j
++)
217 res
&= xdr_u_char(xd
,&i
[j
]);
220 fprintf(list
,"%02x",i
[j
]);
235 static void do_cpt_int_err(XDR
*xd
,const char *desc
,int *i
,FILE *list
)
237 if (do_cpt_int(xd
,desc
,i
,list
) < 0)
243 static void do_cpt_step_err(XDR
*xd
,const char *desc
,gmx_large_int_t
*i
,FILE *list
)
246 char buf
[STEPSTRSIZE
];
248 res
= xdr_gmx_large_int(xd
,i
,"reading checkpoint file");
255 fprintf(list
,"%s = %s\n",desc
,gmx_step_str(*i
,buf
));
259 static void do_cpt_double_err(XDR
*xd
,const char *desc
,double *f
,FILE *list
)
263 res
= xdr_double(xd
,f
);
270 fprintf(list
,"%s = %f\n",desc
,*f
);
274 /* If nval >= 0, nval is used; on read this should match the passed value.
275 * If nval n<0, *nptr is used; on read the value is stored in nptr
277 static int do_cpte_reals_low(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
278 int nval
,int *nptr
,real
**v
,
279 FILE *list
,int erealtype
)
283 int dtc
=xdr_datatype_float
;
285 int dtc
=xdr_datatype_double
;
302 gmx_incons("*ntpr=NULL in do_cpte_reals_low");
307 res
= xdr_int(xd
,&nf
);
318 gmx_fatal(FARGS
,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp
,ecpt
),nval
,nf
);
327 res
= xdr_int(xd
,&dt
);
334 fprintf(stderr
,"Precision mismatch for state entry %s, code precision is %s, file precision is %s\n",
335 st_names(cptp
,ecpt
),xdr_datatype_names
[dtc
],
336 xdr_datatype_names
[dt
]);
338 if (list
|| !(sflags
& (1<<ecpt
)))
351 if (dt
== xdr_datatype_float
)
353 if (dtc
== xdr_datatype_float
)
361 res
= xdr_vector(xd
,(char *)vf
,nf
,
362 (unsigned int)sizeof(float),(xdrproc_t
)xdr_float
);
367 if (dtc
!= xdr_datatype_float
)
378 if (dtc
== xdr_datatype_double
)
386 res
= xdr_vector(xd
,(char *)vd
,nf
,
387 (unsigned int)sizeof(double),(xdrproc_t
)xdr_double
);
392 if (dtc
!= xdr_datatype_double
)
407 pr_reals(list
,0,st_names(cptp
,ecpt
),vp
,nf
);
410 pr_rvecs(list
,0,st_names(cptp
,ecpt
),(rvec
*)vp
,nf
/3);
413 gmx_incons("Unknown checkpoint real type");
425 /* This function stores n along with the reals for reading,
426 * but on reading it assumes that n matches the value in the checkpoint file,
427 * a fatal error is generated when this is not the case.
429 static int do_cpte_reals(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
430 int n
,real
**v
,FILE *list
)
432 return do_cpte_reals_low(xd
,cptp
,ecpt
,sflags
,n
,NULL
,v
,list
,ecprREAL
);
435 /* This function does the same as do_cpte_reals,
436 * except that on reading it ignores the passed value of *n
437 * and stored the value read from the checkpoint file in *n.
439 static int do_cpte_n_reals(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
440 int *n
,real
**v
,FILE *list
)
442 return do_cpte_reals_low(xd
,cptp
,ecpt
,sflags
,-1,n
,v
,list
,ecprREAL
);
445 static int do_cpte_real(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
450 return do_cpte_reals_low(xd
,cptp
,ecpt
,sflags
,1,NULL
,&r
,list
,ecprREAL
);
453 static int do_cpte_ints(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
454 int n
,int **v
,FILE *list
)
457 int dtc
=xdr_datatype_int
;
462 res
= xdr_int(xd
,&nf
);
467 if (list
== NULL
&& v
!= NULL
&& nf
!= n
)
469 gmx_fatal(FARGS
,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp
,ecpt
),n
,nf
);
472 res
= xdr_int(xd
,&dt
);
479 gmx_fatal(FARGS
,"Type mismatch for state entry %s, code type is %s, file type is %s\n",
480 st_names(cptp
,ecpt
),xdr_datatype_names
[dtc
],
481 xdr_datatype_names
[dt
]);
483 if (list
|| !(sflags
& (1<<ecpt
)) || v
== NULL
)
496 res
= xdr_vector(xd
,(char *)vp
,nf
,
497 (unsigned int)sizeof(int),(xdrproc_t
)xdr_int
);
504 pr_ivec(list
,0,st_names(cptp
,ecpt
),vp
,nf
,TRUE
);
514 static int do_cpte_int(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
517 return do_cpte_ints(xd
,cptp
,ecpt
,sflags
,1,&i
,list
);
520 static int do_cpte_doubles(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
521 int n
,double **v
,FILE *list
)
524 int dtc
=xdr_datatype_double
;
529 res
= xdr_int(xd
,&nf
);
534 if (list
== NULL
&& nf
!= n
)
536 gmx_fatal(FARGS
,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp
,ecpt
),n
,nf
);
539 res
= xdr_int(xd
,&dt
);
546 gmx_fatal(FARGS
,"Precision mismatch for state entry %s, code precision is %s, file precision is %s\n",
547 st_names(cptp
,ecpt
),xdr_datatype_names
[dtc
],
548 xdr_datatype_names
[dt
]);
550 if (list
|| !(sflags
& (1<<ecpt
)))
563 res
= xdr_vector(xd
,(char *)vp
,nf
,
564 (unsigned int)sizeof(double),(xdrproc_t
)xdr_double
);
571 pr_doubles(list
,0,st_names(cptp
,ecpt
),vp
,nf
);
581 static int do_cpte_double(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
582 double *r
,FILE *list
)
584 return do_cpte_doubles(xd
,cptp
,ecpt
,sflags
,1,&r
,list
);
588 static int do_cpte_rvecs(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
589 int n
,rvec
**v
,FILE *list
)
593 return do_cpte_reals_low(xd
,cptp
,ecpt
,sflags
,
594 n
*DIM
,NULL
,(real
**)v
,list
,ecprRVEC
);
597 static int do_cpte_matrix(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
603 vr
= (real
*)&(v
[0][0]);
604 ret
= do_cpte_reals_low(xd
,cptp
,ecpt
,sflags
,
605 DIM
*DIM
,NULL
,&vr
,NULL
,ecprMATRIX
);
607 if (list
&& ret
== 0)
609 pr_rvecs(list
,0,st_names(cptp
,ecpt
),v
,DIM
);
615 static int do_cpte_matrices(XDR
*xd
,int cptp
,int ecpt
,int sflags
,
616 int n
,matrix
**v
,FILE *list
)
625 res
= xdr_int(xd
,&nf
);
630 if (list
== NULL
&& nf
!= n
)
632 gmx_fatal(FARGS
,"Count mismatch for state entry %s, code count is %d, file count is %d\n",st_names(cptp
,ecpt
),n
,nf
);
634 if (list
|| !(sflags
& (1<<ecpt
)))
654 vr
[(i
*DIM
+j
)*DIM
+k
] = vp
[i
][j
][k
];
658 ret
= do_cpte_reals_low(xd
,cptp
,ecpt
,sflags
,
659 nf
*DIM
*DIM
,NULL
,&vr
,NULL
,ecprMATRIX
);
666 vp
[i
][j
][k
] = vr
[(i
*DIM
+j
)*DIM
+k
];
672 if (list
&& ret
== 0)
676 pr_rvecs(list
,0,st_names(cptp
,ecpt
),vp
[i
],DIM
);
687 static void do_cpt_header(XDR
*xd
,gmx_bool bRead
,int *file_version
,
688 char **version
,char **btime
,char **buser
,char **bmach
,
689 char **fprog
,char **ftime
,
690 int *eIntegrator
,int *simulation_part
,
691 gmx_large_int_t
*step
,double *t
,
692 int *nnodes
,int *dd_nc
,int *npme
,
693 int *natoms
,int *ngtc
, int *nnhpres
, int *nhchainlength
,
694 int *flags_state
,int *flags_eks
,int *flags_enh
,
711 res
= xdr_int(xd
,&magic
);
714 gmx_fatal(FARGS
,"The checkpoint file is empty/corrupted, or maybe you are out of quota?");
716 if (magic
!= CPT_MAGIC1
)
718 gmx_fatal(FARGS
,"Start of file magic number mismatch, checkpoint file has %d, should be %d\n"
719 "The checkpoint file is corrupted or not a checkpoint file",
726 if (gethostname(fhost
,255) != 0)
728 sprintf(fhost
,"unknown");
731 sprintf(fhost
,"unknown");
734 do_cpt_string_err(xd
,bRead
,"GROMACS version" ,version
,list
);
735 do_cpt_string_err(xd
,bRead
,"GROMACS build time" ,btime
,list
);
736 do_cpt_string_err(xd
,bRead
,"GROMACS build user" ,buser
,list
);
737 do_cpt_string_err(xd
,bRead
,"GROMACS build machine" ,bmach
,list
);
738 do_cpt_string_err(xd
,bRead
,"generating program" ,fprog
,list
);
739 do_cpt_string_err(xd
,bRead
,"generation time" ,ftime
,list
);
740 *file_version
= cpt_version
;
741 do_cpt_int_err(xd
,"checkpoint file version",file_version
,list
);
742 if (*file_version
> cpt_version
)
744 gmx_fatal(FARGS
,"Attempting to read a checkpoint file of version %d with code of version %d\n",*file_version
,cpt_version
);
746 if (*file_version
>= 12)
748 do_cpt_string_err(xd
,bRead
,"generating host" ,&fhost
,list
);
754 do_cpt_int_err(xd
,"#atoms" ,natoms
,list
);
755 do_cpt_int_err(xd
,"#T-coupling groups",ngtc
,list
);
756 if (*file_version
>= 10)
758 do_cpt_int_err(xd
,"#Nose-Hoover T-chains",nhchainlength
,list
);
764 if (*file_version
>= 11)
766 do_cpt_int_err(xd
,"#Nose-Hoover T-chains for barostat ",nnhpres
,list
);
772 do_cpt_int_err(xd
,"integrator" ,eIntegrator
,list
);
773 if (*file_version
>= 3)
775 do_cpt_int_err(xd
,"simulation part #", simulation_part
,list
);
779 *simulation_part
= 1;
781 if (*file_version
>= 5)
783 do_cpt_step_err(xd
,"step" ,step
,list
);
787 do_cpt_int_err(xd
,"step" ,&idum
,list
);
790 do_cpt_double_err(xd
,"t" ,t
,list
);
791 do_cpt_int_err(xd
,"#PP-nodes" ,nnodes
,list
);
793 do_cpt_int_err(xd
,"dd_nc[x]",dd_nc
? &(dd_nc
[0]) : &idum
,list
);
794 do_cpt_int_err(xd
,"dd_nc[y]",dd_nc
? &(dd_nc
[1]) : &idum
,list
);
795 do_cpt_int_err(xd
,"dd_nc[z]",dd_nc
? &(dd_nc
[2]) : &idum
,list
);
796 do_cpt_int_err(xd
,"#PME-only nodes",npme
,list
);
797 do_cpt_int_err(xd
,"state flags",flags_state
,list
);
798 if (*file_version
>= 4)
800 do_cpt_int_err(xd
,"ekin data flags",flags_eks
,list
);
801 do_cpt_int_err(xd
,"energy history flags",flags_enh
,list
);
806 *flags_enh
= (*flags_state
>> (estORIRE_DTAV
+1));
807 *flags_state
= (*flags_state
& ~((1<<(estORIRE_DTAV
+1)) |
808 (1<<(estORIRE_DTAV
+2)) |
809 (1<<(estORIRE_DTAV
+3))));
813 static int do_cpt_footer(XDR
*xd
,gmx_bool bRead
,int file_version
)
818 if (file_version
>= 2)
821 res
= xdr_int(xd
,&magic
);
826 if (magic
!= CPT_MAGIC2
)
835 static int do_cpt_state(XDR
*xd
,gmx_bool bRead
,
836 int fflags
,t_state
*state
,
837 gmx_bool bReadRNG
,FILE *list
)
840 int **rng_p
,**rngi_p
;
847 nnht
= state
->nhchainlength
*state
->ngtc
;
848 nnhtp
= state
->nhchainlength
*state
->nnhpres
;
852 rng_p
= (int **)&state
->ld_rng
;
853 rngi_p
= &state
->ld_rngi
;
857 /* Do not read the RNG data */
862 sflags
= state
->flags
;
863 for(i
=0; (i
<estNR
&& ret
== 0); i
++)
869 case estLAMBDA
: ret
= do_cpte_real(xd
,0,i
,sflags
,&state
->lambda
,list
); break;
870 case estBOX
: ret
= do_cpte_matrix(xd
,0,i
,sflags
,state
->box
,list
); break;
871 case estBOX_REL
: ret
= do_cpte_matrix(xd
,0,i
,sflags
,state
->box_rel
,list
); break;
872 case estBOXV
: ret
= do_cpte_matrix(xd
,0,i
,sflags
,state
->boxv
,list
); break;
873 case estPRES_PREV
: ret
= do_cpte_matrix(xd
,0,i
,sflags
,state
->pres_prev
,list
); break;
874 case estSVIR_PREV
: ret
= do_cpte_matrix(xd
,0,i
,sflags
,state
->svir_prev
,list
); break;
875 case estFVIR_PREV
: ret
= do_cpte_matrix(xd
,0,i
,sflags
,state
->fvir_prev
,list
); break;
876 case estNH_XI
: ret
= do_cpte_doubles(xd
,0,i
,sflags
,nnht
,&state
->nosehoover_xi
,list
); break;
877 case estNH_VXI
: ret
= do_cpte_doubles(xd
,0,i
,sflags
,nnht
,&state
->nosehoover_vxi
,list
); break;
878 case estNHPRES_XI
: ret
= do_cpte_doubles(xd
,0,i
,sflags
,nnhtp
,&state
->nhpres_xi
,list
); break;
879 case estNHPRES_VXI
: ret
= do_cpte_doubles(xd
,0,i
,sflags
,nnhtp
,&state
->nhpres_vxi
,list
); break;
880 case estTC_INT
: ret
= do_cpte_doubles(xd
,0,i
,sflags
,state
->ngtc
,&state
->therm_integral
,list
); break;
881 case estVETA
: ret
= do_cpte_real(xd
,0,i
,sflags
,&state
->veta
,list
); break;
882 case estVOL0
: ret
= do_cpte_real(xd
,0,i
,sflags
,&state
->vol0
,list
); break;
883 case estX
: ret
= do_cpte_rvecs(xd
,0,i
,sflags
,state
->natoms
,&state
->x
,list
); break;
884 case estV
: ret
= do_cpte_rvecs(xd
,0,i
,sflags
,state
->natoms
,&state
->v
,list
); break;
885 case estSDX
: ret
= do_cpte_rvecs(xd
,0,i
,sflags
,state
->natoms
,&state
->sd_X
,list
); break;
886 case estLD_RNG
: ret
= do_cpte_ints(xd
,0,i
,sflags
,state
->nrng
,rng_p
,list
); break;
887 case estLD_RNGI
: ret
= do_cpte_ints(xd
,0,i
,sflags
,state
->nrngi
,rngi_p
,list
); break;
888 case estDISRE_INITF
: ret
= do_cpte_real (xd
,0,i
,sflags
,&state
->hist
.disre_initf
,list
); break;
889 case estDISRE_RM3TAV
: ret
= do_cpte_n_reals(xd
,0,i
,sflags
,&state
->hist
.ndisrepairs
,&state
->hist
.disre_rm3tav
,list
); break;
890 case estORIRE_INITF
: ret
= do_cpte_real (xd
,0,i
,sflags
,&state
->hist
.orire_initf
,list
); break;
891 case estORIRE_DTAV
: ret
= do_cpte_n_reals(xd
,0,i
,sflags
,&state
->hist
.norire_Dtav
,&state
->hist
.orire_Dtav
,list
); break;
893 gmx_fatal(FARGS
,"Unknown state entry %d\n"
894 "You are probably reading a new checkpoint file with old code",i
);
902 static int do_cpt_ekinstate(XDR
*xd
,gmx_bool bRead
,
903 int fflags
,ekinstate_t
*ekins
,
911 for(i
=0; (i
<eeksNR
&& ret
== 0); i
++)
918 case eeksEKIN_N
: ret
= do_cpte_int(xd
,1,i
,fflags
,&ekins
->ekin_n
,list
); break;
919 case eeksEKINH
: ret
= do_cpte_matrices(xd
,1,i
,fflags
,ekins
->ekin_n
,&ekins
->ekinh
,list
); break;
920 case eeksEKINF
: ret
= do_cpte_matrices(xd
,1,i
,fflags
,ekins
->ekin_n
,&ekins
->ekinf
,list
); break;
921 case eeksEKINO
: ret
= do_cpte_matrices(xd
,1,i
,fflags
,ekins
->ekin_n
,&ekins
->ekinh_old
,list
); break;
922 case eeksEKINTOTAL
: ret
= do_cpte_matrix(xd
,1,i
,fflags
,ekins
->ekin_total
,list
); break;
923 case eeksEKINSCALEF
: ret
= do_cpte_doubles(xd
,1,i
,fflags
,ekins
->ekin_n
,&ekins
->ekinscalef_nhc
,list
); break;
924 case eeksVSCALE
: ret
= do_cpte_doubles(xd
,1,i
,fflags
,ekins
->ekin_n
,&ekins
->vscale_nhc
,list
); break;
925 case eeksEKINSCALEH
: ret
= do_cpte_doubles(xd
,1,i
,fflags
,ekins
->ekin_n
,&ekins
->ekinscaleh_nhc
,list
); break;
926 case eeksDEKINDL
: ret
= do_cpte_real(xd
,1,i
,fflags
,&ekins
->dekindl
,list
); break;
927 case eeksMVCOS
: ret
= do_cpte_real(xd
,1,i
,fflags
,&ekins
->mvcos
,list
); break;
929 gmx_fatal(FARGS
,"Unknown ekin data state entry %d\n"
930 "You are probably reading a new checkpoint file with old code",i
);
939 static int do_cpt_enerhist(XDR
*xd
,gmx_bool bRead
,
940 int fflags
,energyhistory_t
*enerhist
,
951 enerhist
->nsteps
= 0;
953 enerhist
->nsteps_sim
= 0;
954 enerhist
->nsum_sim
= 0;
955 enerhist
->dht
= NULL
;
957 if (fflags
& (1<< eenhENERGY_DELTA_H_NN
) )
959 snew(enerhist
->dht
,1);
960 enerhist
->dht
->ndh
= NULL
;
961 enerhist
->dht
->dh
= NULL
;
962 enerhist
->dht
->start_lambda_set
=FALSE
;
966 for(i
=0; (i
<eenhNR
&& ret
== 0); i
++)
972 case eenhENERGY_N
: ret
= do_cpte_int(xd
,2,i
,fflags
,&enerhist
->nener
,list
); break;
973 case eenhENERGY_AVER
: ret
= do_cpte_doubles(xd
,2,i
,fflags
,enerhist
->nener
,&enerhist
->ener_ave
,list
); break;
974 case eenhENERGY_SUM
: ret
= do_cpte_doubles(xd
,2,i
,fflags
,enerhist
->nener
,&enerhist
->ener_sum
,list
); break;
975 case eenhENERGY_NSUM
: do_cpt_step_err(xd
,eenh_names
[i
],&enerhist
->nsum
,list
); break;
976 case eenhENERGY_SUM_SIM
: ret
= do_cpte_doubles(xd
,2,i
,fflags
,enerhist
->nener
,&enerhist
->ener_sum_sim
,list
); break;
977 case eenhENERGY_NSUM_SIM
: do_cpt_step_err(xd
,eenh_names
[i
],&enerhist
->nsum_sim
,list
); break;
978 case eenhENERGY_NSTEPS
: do_cpt_step_err(xd
,eenh_names
[i
],&enerhist
->nsteps
,list
); break;
979 case eenhENERGY_NSTEPS_SIM
: do_cpt_step_err(xd
,eenh_names
[i
],&enerhist
->nsteps_sim
,list
); break;
980 case eenhENERGY_DELTA_H_NN
: do_cpt_int_err(xd
,eenh_names
[i
], &(enerhist
->dht
->nndh
), list
);
981 if (bRead
) /* now allocate memory for it */
983 snew(enerhist
->dht
->dh
, enerhist
->dht
->nndh
);
984 snew(enerhist
->dht
->ndh
, enerhist
->dht
->nndh
);
985 for(j
=0;j
<enerhist
->dht
->nndh
;j
++)
987 enerhist
->dht
->ndh
[j
] = 0;
988 enerhist
->dht
->dh
[j
] = NULL
;
992 case eenhENERGY_DELTA_H_LIST
:
993 for(j
=0;j
<enerhist
->dht
->nndh
;j
++)
995 ret
=do_cpte_n_reals(xd
, 2, i
, fflags
, &enerhist
->dht
->ndh
[j
], &(enerhist
->dht
->dh
[j
]), list
);
998 case eenhENERGY_DELTA_H_STARTTIME
:
999 ret
=do_cpte_double(xd
, 2, i
, fflags
, &(enerhist
->dht
->start_time
), list
); break;
1000 case eenhENERGY_DELTA_H_STARTLAMBDA
:
1001 ret
=do_cpte_double(xd
, 2, i
, fflags
, &(enerhist
->dht
->start_lambda
), list
); break;
1002 enerhist
->dht
->start_lambda_set
=TRUE
;
1004 gmx_fatal(FARGS
,"Unknown energy history entry %d\n"
1005 "You are probably reading a new checkpoint file with old code",i
);
1010 if ((fflags
& (1<<eenhENERGY_SUM
)) && !(fflags
& (1<<eenhENERGY_SUM_SIM
)))
1012 /* Assume we have an old file format and copy sum to sum_sim */
1013 srenew(enerhist
->ener_sum_sim
,enerhist
->nener
);
1014 for(i
=0; i
<enerhist
->nener
; i
++)
1016 enerhist
->ener_sum_sim
[i
] = enerhist
->ener_sum
[i
];
1018 fflags
|= (1<<eenhENERGY_SUM_SIM
);
1021 if ( (fflags
& (1<<eenhENERGY_NSUM
)) &&
1022 !(fflags
& (1<<eenhENERGY_NSTEPS
)))
1024 /* Assume we have an old file format and copy nsum to nsteps */
1025 enerhist
->nsteps
= enerhist
->nsum
;
1026 fflags
|= (1<<eenhENERGY_NSTEPS
);
1028 if ( (fflags
& (1<<eenhENERGY_NSUM_SIM
)) &&
1029 !(fflags
& (1<<eenhENERGY_NSTEPS_SIM
)))
1031 /* Assume we have an old file format and copy nsum to nsteps */
1032 enerhist
->nsteps_sim
= enerhist
->nsum_sim
;
1033 fflags
|= (1<<eenhENERGY_NSTEPS_SIM
);
1039 static int do_cpt_files(XDR
*xd
, gmx_bool bRead
,
1040 gmx_file_position_t
**p_outputfiles
, int *nfiles
,
1041 FILE *list
, int file_version
)
1045 gmx_off_t mask
= 0xFFFFFFFFL
;
1046 int offset_high
,offset_low
;
1048 gmx_file_position_t
*outputfiles
;
1050 if (do_cpt_int(xd
,"number of output files",nfiles
,list
) != 0)
1057 snew(*p_outputfiles
,*nfiles
);
1060 outputfiles
= *p_outputfiles
;
1062 for(i
=0;i
<*nfiles
;i
++)
1064 /* 64-bit XDR numbers are not portable, so it is stored as separate high/low fractions */
1067 do_cpt_string_err(xd
,bRead
,"output filename",&buf
,list
);
1068 strncpy(outputfiles
[i
].filename
,buf
,CPTSTRLEN
-1);
1074 if (do_cpt_int(xd
,"file_offset_high",&offset_high
,list
) != 0)
1078 if (do_cpt_int(xd
,"file_offset_low",&offset_low
,list
) != 0)
1082 #if (SIZEOF_GMX_OFF_T > 4)
1083 outputfiles
[i
].offset
= ( ((gmx_off_t
) offset_high
) << 32 ) | ( (gmx_off_t
) offset_low
& mask
);
1085 outputfiles
[i
].offset
= offset_low
;
1090 buf
= outputfiles
[i
].filename
;
1091 do_cpt_string_err(xd
,bRead
,"output filename",&buf
,list
);
1093 offset
= outputfiles
[i
].offset
;
1101 #if (SIZEOF_GMX_OFF_T > 4)
1102 offset_low
= (int) (offset
& mask
);
1103 offset_high
= (int) ((offset
>> 32) & mask
);
1105 offset_low
= offset
;
1109 if (do_cpt_int(xd
,"file_offset_high",&offset_high
,list
) != 0)
1113 if (do_cpt_int(xd
,"file_offset_low",&offset_low
,list
) != 0)
1118 if (file_version
>= 8)
1120 if (do_cpt_int(xd
,"file_checksum_size",&(outputfiles
[i
].chksum_size
),
1125 if (do_cpt_u_chars(xd
,"file_checksum",16,outputfiles
[i
].chksum
,list
) != 0)
1132 outputfiles
[i
].chksum_size
= -1;
1139 void write_checkpoint(const char *fn
,gmx_bool bNumberAndKeep
,
1140 FILE *fplog
,t_commrec
*cr
,
1141 int eIntegrator
,int simulation_part
,
1142 gmx_large_int_t step
,double t
,t_state
*state
)
1151 char *fntemp
; /* the temporary checkpoint file name */
1153 char timebuf
[STRLEN
];
1154 int nppnodes
,npmenodes
,flag_64bit
;
1155 char buf
[1024],suffix
[5+STEPSTRSIZE
],sbuf
[STEPSTRSIZE
];
1156 gmx_file_position_t
*outputfiles
;
1159 int flags_eks
,flags_enh
,i
;
1164 if (DOMAINDECOMP(cr
))
1166 nppnodes
= cr
->dd
->nnodes
;
1167 npmenodes
= cr
->npmenodes
;
1171 nppnodes
= cr
->nnodes
;
1181 /* make the new temporary filename */
1182 snew(fntemp
, strlen(fn
)+5+STEPSTRSIZE
);
1184 fntemp
[strlen(fn
) - strlen(ftp2ext(fn2ftp(fn
))) - 1] = '\0';
1185 sprintf(suffix
,"_%s%s","step",gmx_step_str(step
,sbuf
));
1186 strcat(fntemp
,suffix
);
1187 strcat(fntemp
,fn
+strlen(fn
) - strlen(ftp2ext(fn2ftp(fn
))) - 1);
1190 gmx_ctime_r(&now
,timebuf
,STRLEN
);
1194 fprintf(fplog
,"Writing checkpoint, step %s at %s\n\n",
1195 gmx_step_str(step
,buf
),timebuf
);
1198 /* Get offsets for open files */
1199 gmx_fio_get_output_file_positions(&outputfiles
, &noutputfiles
);
1201 fp
= gmx_fio_open(fntemp
,"w");
1203 if (state
->ekinstate
.bUpToDate
)
1206 ((1<<eeksEKIN_N
) | (1<<eeksEKINH
) | (1<<eeksEKINF
) |
1207 (1<<eeksEKINO
) | (1<<eeksEKINSCALEF
) | (1<<eeksEKINSCALEH
) |
1208 (1<<eeksVSCALE
) | (1<<eeksDEKINDL
) | (1<<eeksMVCOS
));
1216 if (state
->enerhist
.nsum
> 0 || state
->enerhist
.nsum_sim
> 0)
1218 flags_enh
|= (1<<eenhENERGY_N
);
1219 if (state
->enerhist
.nsum
> 0)
1221 flags_enh
|= ((1<<eenhENERGY_AVER
) | (1<<eenhENERGY_SUM
) |
1222 (1<<eenhENERGY_NSTEPS
) | (1<<eenhENERGY_NSUM
));
1224 if (state
->enerhist
.nsum_sim
> 0)
1226 flags_enh
|= ((1<<eenhENERGY_SUM_SIM
) | (1<<eenhENERGY_NSTEPS_SIM
) |
1227 (1<<eenhENERGY_NSUM_SIM
));
1229 if (state
->enerhist
.dht
)
1231 flags_enh
|= ( (1<< eenhENERGY_DELTA_H_NN
) |
1232 (1<< eenhENERGY_DELTA_H_LIST
) |
1233 (1<< eenhENERGY_DELTA_H_STARTTIME
) |
1234 (1<< eenhENERGY_DELTA_H_STARTLAMBDA
) );
1239 version
= strdup(VERSION
);
1240 btime
= strdup(BUILD_TIME
);
1241 buser
= strdup(BUILD_USER
);
1242 bmach
= strdup(BUILD_MACHINE
);
1243 fprog
= strdup(Program());
1245 ftime
= &(timebuf
[0]);
1247 do_cpt_header(gmx_fio_getxdr(fp
),FALSE
,&file_version
,
1248 &version
,&btime
,&buser
,&bmach
,&fprog
,&ftime
,
1249 &eIntegrator
,&simulation_part
,&step
,&t
,&nppnodes
,
1250 DOMAINDECOMP(cr
) ? cr
->dd
->nc
: NULL
,&npmenodes
,
1251 &state
->natoms
,&state
->ngtc
,&state
->nnhpres
,
1252 &state
->nhchainlength
, &state
->flags
,&flags_eks
,&flags_enh
,
1261 if((do_cpt_state(gmx_fio_getxdr(fp
),FALSE
,state
->flags
,state
,TRUE
,NULL
) < 0) ||
1262 (do_cpt_ekinstate(gmx_fio_getxdr(fp
),FALSE
,flags_eks
,&state
->ekinstate
,NULL
) < 0)||
1263 (do_cpt_enerhist(gmx_fio_getxdr(fp
),FALSE
,flags_enh
,&state
->enerhist
,NULL
) < 0) ||
1264 (do_cpt_files(gmx_fio_getxdr(fp
),FALSE
,&outputfiles
,&noutputfiles
,NULL
,
1267 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1270 do_cpt_footer(gmx_fio_getxdr(fp
),FALSE
,file_version
);
1272 /* we really, REALLY, want to make sure to physically write the checkpoint,
1273 and all the files it depends on, out to disk. Because we've
1274 opened the checkpoint with gmx_fio_open(), it's in our list
1276 ret
=gmx_fio_all_output_fsync();
1282 "Cannot fsync '%s'; maybe you are out of disk space or quota?",
1283 gmx_fio_getname(ret
));
1285 if (getenv(GMX_IGNORE_FSYNC_FAILURE_ENV
)==NULL
)
1295 if( gmx_fio_close(fp
) != 0)
1297 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1300 /* we don't move the checkpoint if the user specified they didn't want it,
1301 or if the fsyncs failed */
1302 if (!bNumberAndKeep
&& !ret
)
1306 /* Rename the previous checkpoint file */
1308 buf
[strlen(fn
) - strlen(ftp2ext(fn2ftp(fn
))) - 1] = '\0';
1309 strcat(buf
,"_prev");
1310 strcat(buf
,fn
+strlen(fn
) - strlen(ftp2ext(fn2ftp(fn
))) - 1);
1312 /* we copy here so that if something goes wrong between now and
1313 * the rename below, there's always a state.cpt.
1314 * If renames are atomic (such as in POSIX systems),
1315 * this copying should be unneccesary.
1317 gmx_file_copy(fn
, buf
, FALSE
);
1318 /* We don't really care if this fails:
1319 * there's already a new checkpoint.
1322 gmx_file_rename(fn
, buf
);
1325 if (gmx_file_rename(fntemp
, fn
) != 0)
1327 gmx_file("Cannot rename checkpoint file; maybe you are out of quota?");
1335 /*code for alternate checkpointing scheme. moved from top of loop over
1337 fcRequestCheckPoint();
1338 if ( fcCheckPointParallel( cr
->nodeid
, NULL
,0) == 0 ) {
1339 gmx_fatal( 3,__FILE__
,__LINE__
, "Checkpoint error on step %d\n", step
);
1341 #endif /* end GMX_FAHCORE block */
1344 static void print_flag_mismatch(FILE *fplog
,int sflags
,int fflags
)
1348 fprintf(fplog
,"\nState entry mismatch between the simulation and the checkpoint file\n");
1349 fprintf(fplog
,"Entries which are not present in the checkpoint file will not be updated\n");
1350 fprintf(fplog
," %24s %11s %11s\n","","simulation","checkpoint");
1351 for(i
=0; i
<estNR
; i
++)
1353 if ((sflags
& (1<<i
)) || (fflags
& (1<<i
)))
1355 fprintf(fplog
," %24s %11s %11s\n",
1357 (sflags
& (1<<i
)) ? " present " : "not present",
1358 (fflags
& (1<<i
)) ? " present " : "not present");
1363 static void check_int(FILE *fplog
,const char *type
,int p
,int f
,gmx_bool
*mm
)
1365 FILE *fp
= fplog
? fplog
: stderr
;
1369 fprintf(fp
," %s mismatch,\n",type
);
1370 fprintf(fp
," current program: %d\n",p
);
1371 fprintf(fp
," checkpoint file: %d\n",f
);
1377 static void check_string(FILE *fplog
,const char *type
,const char *p
,
1378 const char *f
,gmx_bool
*mm
)
1380 FILE *fp
= fplog
? fplog
: stderr
;
1382 if (strcmp(p
,f
) != 0)
1384 fprintf(fp
," %s mismatch,\n",type
);
1385 fprintf(fp
," current program: %s\n",p
);
1386 fprintf(fp
," checkpoint file: %s\n",f
);
1392 static void check_match(FILE *fplog
,
1394 char *btime
,char *buser
,char *bmach
,char *fprog
,
1395 t_commrec
*cr
,gmx_bool bPartDecomp
,int npp_f
,int npme_f
,
1396 ivec dd_nc
,ivec dd_nc_f
)
1403 check_string(fplog
,"Version" ,VERSION
,version
,&mm
);
1404 check_string(fplog
,"Build time" ,BUILD_TIME
,btime
,&mm
);
1405 check_string(fplog
,"Build user" ,BUILD_USER
,buser
,&mm
);
1406 check_string(fplog
,"Build machine",BUILD_MACHINE
,bmach
,&mm
);
1407 check_string(fplog
,"Program name" ,Program() ,fprog
,&mm
);
1409 npp
= cr
->nnodes
- cr
->npmenodes
;
1410 check_int (fplog
,"#nodes" ,cr
->nnodes
,npp_f
+npme_f
,&mm
);
1419 check_int (fplog
,"#PME-nodes" ,cr
->npmenodes
,npme_f
,&mm
);
1422 check_int (fplog
,"#DD-cells[x]",dd_nc
[XX
] ,dd_nc_f
[XX
],&mm
);
1423 check_int (fplog
,"#DD-cells[y]",dd_nc
[YY
] ,dd_nc_f
[YY
],&mm
);
1424 check_int (fplog
,"#DD-cells[z]",dd_nc
[ZZ
] ,dd_nc_f
[ZZ
],&mm
);
1431 "Gromacs binary or parallel settings not identical to previous run.\n"
1432 "Continuation is exact, but is not guaranteed to be binary identical%s.\n\n",
1433 fplog
? ",\n see the log file for details" : "");
1438 "Gromacs binary or parallel settings not identical to previous run.\n"
1439 "Continuation is exact, but is not guaranteed to be binary identical.\n\n");
1444 static void read_checkpoint(const char *fn
,FILE **pfplog
,
1445 t_commrec
*cr
,gmx_bool bPartDecomp
,ivec dd_nc
,
1446 int eIntegrator
,gmx_large_int_t
*step
,double *t
,
1447 t_state
*state
,gmx_bool
*bReadRNG
,gmx_bool
*bReadEkin
,
1448 int *simulation_part
,gmx_bool bAppendOutputFiles
)
1453 char *version
,*btime
,*buser
,*bmach
,*fprog
,*ftime
;
1454 char filename
[STRLEN
],buf
[STEPSTRSIZE
];
1455 int nppnodes
,eIntegrator_f
,nppnodes_f
,npmenodes_f
;
1457 int natoms
,ngtc
,nnhpres
,nhchainlength
,fflags
,flags_eks
,flags_enh
;
1460 gmx_file_position_t
*outputfiles
;
1462 t_fileio
*chksum_file
;
1463 FILE* fplog
= *pfplog
;
1464 unsigned char digest
[16];
1465 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1466 struct flock fl
; /* don't initialize here: the struct order is OS
1470 const char *int_warn
=
1471 "WARNING: The checkpoint file was generated with integrator %s,\n"
1472 " while the simulation uses integrator %s\n\n";
1473 const char *sd_note
=
1474 "NOTE: The checkpoint file was for %d nodes doing SD or BD,\n"
1475 " while the simulation uses %d SD or BD nodes,\n"
1476 " continuation will be exact, except for the random state\n\n";
1478 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1480 fl
.l_whence
=SEEK_SET
;
1489 "read_checkpoint not (yet) supported with particle decomposition");
1492 fp
= gmx_fio_open(fn
,"r");
1493 do_cpt_header(gmx_fio_getxdr(fp
),TRUE
,&file_version
,
1494 &version
,&btime
,&buser
,&bmach
,&fprog
,&ftime
,
1495 &eIntegrator_f
,simulation_part
,step
,t
,
1496 &nppnodes_f
,dd_nc_f
,&npmenodes_f
,
1497 &natoms
,&ngtc
,&nnhpres
,&nhchainlength
,
1498 &fflags
,&flags_eks
,&flags_enh
,NULL
);
1500 if (cr
== NULL
|| MASTER(cr
))
1502 fprintf(stderr
,"\nReading checkpoint file %s generated: %s\n\n",
1506 /* This will not be written if we do appending, since fplog is still NULL then */
1509 fprintf(fplog
,"\n");
1510 fprintf(fplog
,"Reading checkpoint file %s\n",fn
);
1511 fprintf(fplog
," file generated by: %s\n",fprog
);
1512 fprintf(fplog
," file generated at: %s\n",ftime
);
1513 fprintf(fplog
," GROMACS build time: %s\n",btime
);
1514 fprintf(fplog
," GROMACS build user: %s\n",buser
);
1515 fprintf(fplog
," GROMACS build machine: %s\n",bmach
);
1516 fprintf(fplog
," simulation part #: %d\n",*simulation_part
);
1517 fprintf(fplog
," step: %s\n",gmx_step_str(*step
,buf
));
1518 fprintf(fplog
," time: %f\n",*t
);
1519 fprintf(fplog
,"\n");
1522 if (natoms
!= state
->natoms
)
1524 gmx_fatal(FARGS
,"Checkpoint file is for a system of %d atoms, while the current system consists of %d atoms",natoms
,state
->natoms
);
1526 if (ngtc
!= state
->ngtc
)
1528 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
);
1530 if (nnhpres
!= state
->nnhpres
)
1532 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
);
1535 init_gtc_state(state
,state
->ngtc
,state
->nnhpres
,nhchainlength
); /* need to keep this here to keep the tpr format working */
1536 /* write over whatever was read; we use the number of Nose-Hoover chains from the checkpoint */
1538 if (eIntegrator_f
!= eIntegrator
)
1542 fprintf(stderr
,int_warn
,EI(eIntegrator_f
),EI(eIntegrator
));
1544 if(bAppendOutputFiles
)
1547 "Output file appending requested, but input/checkpoint integrators do not match.\n"
1548 "Stopping the run to prevent you from ruining all your data...\n"
1549 "If you _really_ know what you are doing, try with the -noappend option.\n");
1553 fprintf(fplog
,int_warn
,EI(eIntegrator_f
),EI(eIntegrator
));
1561 else if (bPartDecomp
)
1563 nppnodes
= cr
->nnodes
;
1565 else if (cr
->nnodes
== nppnodes_f
+ npmenodes_f
)
1567 if (cr
->npmenodes
< 0)
1569 cr
->npmenodes
= npmenodes_f
;
1571 nppnodes
= cr
->nnodes
- cr
->npmenodes
;
1572 if (nppnodes
== nppnodes_f
)
1574 for(d
=0; d
<DIM
; d
++)
1578 dd_nc
[d
] = dd_nc_f
[d
];
1585 /* The number of PP nodes has not been set yet */
1589 if ((EI_SD(eIntegrator
) || eIntegrator
== eiBD
) && nppnodes
> 0)
1591 /* Correct the RNG state size for the number of PP nodes.
1592 * Such assignments should all be moved to one central function.
1594 state
->nrng
= nppnodes
*gmx_rng_n();
1595 state
->nrngi
= nppnodes
;
1599 if (fflags
!= state
->flags
)
1604 if(bAppendOutputFiles
)
1607 "Output file appending requested, but input and checkpoint states are not identical.\n"
1608 "Stopping the run to prevent you from ruining all your data...\n"
1609 "You can try with the -noappend option, and get more info in the log file.\n");
1612 if (getenv("GMX_ALLOW_CPT_MISMATCH") == NULL
)
1614 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");
1619 "WARNING: The checkpoint state entries do not match the simulation,\n"
1620 " see the log file for details\n\n");
1626 print_flag_mismatch(fplog
,state
->flags
,fflags
);
1631 if ((EI_SD(eIntegrator
) || eIntegrator
== eiBD
) &&
1632 nppnodes
!= nppnodes_f
)
1637 fprintf(stderr
,sd_note
,nppnodes_f
,nppnodes
);
1641 fprintf(fplog
,sd_note
,nppnodes_f
,nppnodes
);
1646 check_match(fplog
,version
,btime
,buser
,bmach
,fprog
,
1647 cr
,bPartDecomp
,nppnodes_f
,npmenodes_f
,dd_nc
,dd_nc_f
);
1650 ret
= do_cpt_state(gmx_fio_getxdr(fp
),TRUE
,fflags
,state
,*bReadRNG
,NULL
);
1655 ret
= do_cpt_ekinstate(gmx_fio_getxdr(fp
),TRUE
,
1656 flags_eks
,&state
->ekinstate
,NULL
);
1661 *bReadEkin
= ((flags_eks
& (1<<eeksEKINH
)) || (flags_eks
& (1<<eeksEKINF
)) || (flags_eks
& (1<<eeksEKINO
)) ||
1662 (flags_eks
& (1<<eeksEKINSCALEF
)) | (flags_eks
& (1<<eeksEKINSCALEH
)) | (flags_eks
& (1<<eeksVSCALE
)));
1664 ret
= do_cpt_enerhist(gmx_fio_getxdr(fp
),TRUE
,
1665 flags_enh
,&state
->enerhist
,NULL
);
1671 if (file_version
< 6)
1673 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.";
1675 fprintf(stderr
,"\nWARNING: %s\n\n",warn
);
1678 fprintf(fplog
,"\nWARNING: %s\n\n",warn
);
1680 state
->enerhist
.nsum
= *step
;
1681 state
->enerhist
.nsum_sim
= *step
;
1684 ret
= do_cpt_files(gmx_fio_getxdr(fp
),TRUE
,&outputfiles
,&nfiles
,NULL
,file_version
);
1690 ret
= do_cpt_footer(gmx_fio_getxdr(fp
),TRUE
,file_version
);
1695 if( gmx_fio_close(fp
) != 0)
1697 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1706 /* If the user wants to append to output files,
1707 * we use the file pointer positions of the output files stored
1708 * in the checkpoint file and truncate the files such that any frames
1709 * written after the checkpoint time are removed.
1710 * All files are md5sum checked such that we can be sure that
1711 * we do not truncate other (maybe imprortant) files.
1713 if (bAppendOutputFiles
)
1715 if (fn2ftp(outputfiles
[0].filename
)!=efLOG
)
1717 /* make sure first file is log file so that it is OK to use it for
1720 gmx_fatal(FARGS
,"The first output file should always be the log "
1721 "file but instead is: %s", outputfiles
[0].filename
);
1723 for(i
=0;i
<nfiles
;i
++)
1725 if (outputfiles
[i
].offset
< 0)
1727 gmx_fatal(FARGS
,"The original run wrote a file called '%s' which "
1728 "is larger than 2 GB, but mdrun did not support large file"
1729 " offsets. Can not append. Run mdrun with -noappend",
1730 outputfiles
[i
].filename
);
1733 chksum_file
=gmx_fio_open(outputfiles
[i
].filename
,"a");
1736 chksum_file
=gmx_fio_open(outputfiles
[i
].filename
,"r+");
1741 #if !((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1742 if (fcntl(fileno(gmx_fio_getfp(chksum_file
)), F_SETLK
, &fl
)
1745 if (_locking(fileno(gmx_fio_getfp(chksum_file
)), _LK_NBLCK
, LONG_MAX
)==-1)
1748 if (errno
!=EACCES
&& errno
!=EAGAIN
)
1750 gmx_fatal(FARGS
,"Failed to lock: %s. %s.",
1751 outputfiles
[i
].filename
, strerror(errno
));
1755 gmx_fatal(FARGS
,"Failed to lock: %s. Already running "
1756 "simulation?", outputfiles
[i
].filename
);
1761 /* compute md5 chksum */
1762 if (outputfiles
[i
].chksum_size
!= -1)
1764 if (gmx_fio_get_file_md5(chksum_file
,outputfiles
[i
].offset
,
1765 digest
) != outputfiles
[i
].chksum_size
) /*at the end of the call the file position is at the end of the file*/
1767 gmx_fatal(FARGS
,"Can't read %d bytes of '%s' to compute checksum. The file has been replaced or its contents has been modified.",
1768 outputfiles
[i
].chksum_size
,
1769 outputfiles
[i
].filename
);
1772 if (i
==0) /*log file needs to be seeked in case we need to truncate (other files are truncated below)*/
1774 if (gmx_fio_seek(chksum_file
,outputfiles
[i
].offset
))
1776 gmx_fatal(FARGS
,"Seek error! Failed to truncate log-file: %s.", strerror(errno
));
1781 if (i
==0) /*open log file here - so that lock is never lifted
1782 after chksum is calculated */
1784 *pfplog
= gmx_fio_getfp(chksum_file
);
1788 gmx_fio_close(chksum_file
);
1791 /* compare md5 chksum */
1792 if (outputfiles
[i
].chksum_size
!= -1 &&
1793 memcmp(digest
,outputfiles
[i
].chksum
,16)!=0)
1797 fprintf(debug
,"chksum for %s: ",outputfiles
[i
].filename
);
1798 for (j
=0; j
<16; j
++)
1800 fprintf(debug
,"%02x",digest
[j
]);
1802 fprintf(debug
,"\n");
1804 gmx_fatal(FARGS
,"Checksum wrong for '%s'. The file has been replaced or its contents has been modified.",
1805 outputfiles
[i
].filename
);
1810 if (i
!=0) /*log file is already seeked to correct position */
1812 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
1813 rc
= gmx_wintruncate(outputfiles
[i
].filename
,outputfiles
[i
].offset
);
1815 rc
= truncate(outputfiles
[i
].filename
,outputfiles
[i
].offset
);
1819 gmx_fatal(FARGS
,"Truncation of file %s failed.",outputfiles
[i
].filename
);
1829 void load_checkpoint(const char *fn
,FILE **fplog
,
1830 t_commrec
*cr
,gmx_bool bPartDecomp
,ivec dd_nc
,
1831 t_inputrec
*ir
,t_state
*state
,
1832 gmx_bool
*bReadRNG
,gmx_bool
*bReadEkin
,gmx_bool bAppend
)
1834 gmx_large_int_t step
;
1837 if (SIMMASTER(cr
)) {
1838 /* Read the state from the checkpoint file */
1839 read_checkpoint(fn
,fplog
,
1840 cr
,bPartDecomp
,dd_nc
,
1841 ir
->eI
,&step
,&t
,state
,bReadRNG
,bReadEkin
,
1842 &ir
->simulation_part
,bAppend
);
1845 gmx_bcast(sizeof(cr
->npmenodes
),&cr
->npmenodes
,cr
);
1846 gmx_bcast(DIM
*sizeof(dd_nc
[0]),dd_nc
,cr
);
1847 gmx_bcast(sizeof(step
),&step
,cr
);
1848 gmx_bcast(sizeof(*bReadRNG
),bReadRNG
,cr
);
1849 gmx_bcast(sizeof(*bReadEkin
),bReadEkin
,cr
);
1851 ir
->bContinuation
= TRUE
;
1852 if (ir
->nsteps
>= 0)
1854 ir
->nsteps
+= ir
->init_step
- step
;
1856 ir
->init_step
= step
;
1857 ir
->simulation_part
+= 1;
1860 static void read_checkpoint_data(t_fileio
*fp
,int *simulation_part
,
1861 gmx_large_int_t
*step
,double *t
,t_state
*state
,
1863 int *nfiles
,gmx_file_position_t
**outputfiles
)
1866 char *version
,*btime
,*buser
,*bmach
,*fprog
,*ftime
;
1870 int flags_eks
,flags_enh
;
1872 gmx_file_position_t
*files_loc
=NULL
;
1875 do_cpt_header(gmx_fio_getxdr(fp
),TRUE
,&file_version
,
1876 &version
,&btime
,&buser
,&bmach
,&fprog
,&ftime
,
1877 &eIntegrator
,simulation_part
,step
,t
,&nppnodes
,dd_nc
,&npme
,
1878 &state
->natoms
,&state
->ngtc
,&state
->nnhpres
,&state
->nhchainlength
,
1879 &state
->flags
,&flags_eks
,&flags_enh
,NULL
);
1881 do_cpt_state(gmx_fio_getxdr(fp
),TRUE
,state
->flags
,state
,bReadRNG
,NULL
);
1886 ret
= do_cpt_ekinstate(gmx_fio_getxdr(fp
),TRUE
,
1887 flags_eks
,&state
->ekinstate
,NULL
);
1892 ret
= do_cpt_enerhist(gmx_fio_getxdr(fp
),TRUE
,
1893 flags_enh
,&state
->enerhist
,NULL
);
1899 ret
= do_cpt_files(gmx_fio_getxdr(fp
),TRUE
,
1900 outputfiles
!= NULL
? outputfiles
: &files_loc
,
1901 outputfiles
!= NULL
? nfiles
: &nfiles_loc
,
1903 if (files_loc
!= NULL
)
1913 ret
= do_cpt_footer(gmx_fio_getxdr(fp
),TRUE
,file_version
);
1927 read_checkpoint_state(const char *fn
,int *simulation_part
,
1928 gmx_large_int_t
*step
,double *t
,t_state
*state
)
1932 fp
= gmx_fio_open(fn
,"r");
1933 read_checkpoint_data(fp
,simulation_part
,step
,t
,state
,TRUE
,NULL
,NULL
);
1934 if( gmx_fio_close(fp
) != 0)
1936 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
1940 void read_checkpoint_trxframe(t_fileio
*fp
,t_trxframe
*fr
)
1943 int simulation_part
;
1944 gmx_large_int_t step
;
1947 init_state(&state
,0,0,0,0);
1949 read_checkpoint_data(fp
,&simulation_part
,&step
,&t
,&state
,FALSE
,NULL
,NULL
);
1951 fr
->natoms
= state
.natoms
;
1954 fr
->step
= gmx_large_int_to_int(step
,
1955 "conversion of checkpoint to trajectory");
1959 fr
->lambda
= state
.lambda
;
1961 fr
->bX
= (state
.flags
& (1<<estX
));
1967 fr
->bV
= (state
.flags
& (1<<estV
));
1974 fr
->bBox
= (state
.flags
& (1<<estBOX
));
1977 copy_mat(state
.box
,fr
->box
);
1982 void list_checkpoint(const char *fn
,FILE *out
)
1986 char *version
,*btime
,*buser
,*bmach
,*fprog
,*ftime
;
1987 int eIntegrator
,simulation_part
,nppnodes
,npme
;
1988 gmx_large_int_t step
;
1992 int flags_eks
,flags_enh
;
1996 gmx_file_position_t
*outputfiles
;
1999 init_state(&state
,-1,-1,-1,-1);
2001 fp
= gmx_fio_open(fn
,"r");
2002 do_cpt_header(gmx_fio_getxdr(fp
),TRUE
,&file_version
,
2003 &version
,&btime
,&buser
,&bmach
,&fprog
,&ftime
,
2004 &eIntegrator
,&simulation_part
,&step
,&t
,&nppnodes
,dd_nc
,&npme
,
2005 &state
.natoms
,&state
.ngtc
,&state
.nnhpres
,&state
.nhchainlength
,
2006 &state
.flags
,&flags_eks
,&flags_enh
,out
);
2007 ret
= do_cpt_state(gmx_fio_getxdr(fp
),TRUE
,state
.flags
,&state
,TRUE
,out
);
2012 ret
= do_cpt_ekinstate(gmx_fio_getxdr(fp
),TRUE
,
2013 flags_eks
,&state
.ekinstate
,out
);
2018 ret
= do_cpt_enerhist(gmx_fio_getxdr(fp
),TRUE
,
2019 flags_enh
,&state
.enerhist
,out
);
2023 do_cpt_files(gmx_fio_getxdr(fp
),TRUE
,&outputfiles
,&nfiles
,out
,file_version
);
2028 ret
= do_cpt_footer(gmx_fio_getxdr(fp
),TRUE
,file_version
);
2035 if( gmx_fio_close(fp
) != 0)
2037 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
2044 static gmx_bool
exist_output_file(const char *fnm_cp
,int nfile
,const t_filenm fnm
[])
2048 /* Check if the output file name stored in the checkpoint file
2049 * is one of the output file names of mdrun.
2053 !(is_output(&fnm
[i
]) && strcmp(fnm_cp
,fnm
[i
].fns
[0]) == 0))
2058 return (i
< nfile
&& gmx_fexist(fnm_cp
));
2061 /* This routine cannot print tons of data, since it is called before the log file is opened. */
2062 gmx_bool
read_checkpoint_simulation_part(const char *filename
, int *simulation_part
,
2063 gmx_large_int_t
*cpt_step
,t_commrec
*cr
,
2064 gmx_bool bAppendReq
,
2065 int nfile
,const t_filenm fnm
[],
2066 const char *part_suffix
,gmx_bool
*bAddPart
)
2069 gmx_large_int_t step
=0;
2073 gmx_file_position_t
*outputfiles
;
2076 char *fn
,suf_up
[STRLEN
];
2080 if (SIMMASTER(cr
)) {
2081 if(!gmx_fexist(filename
) || (!(fp
= gmx_fio_open(filename
,"r")) ))
2083 *simulation_part
= 0;
2087 init_state(&state
,0,0,0,0);
2089 read_checkpoint_data(fp
,simulation_part
,&step
,&t
,&state
,FALSE
,
2090 &nfiles
,&outputfiles
);
2091 if( gmx_fio_close(fp
) != 0)
2093 gmx_file("Cannot read/write checkpoint; corrupt file, or maybe you are out of quota?");
2100 for(f
=0; f
<nfiles
; f
++)
2102 if (exist_output_file(outputfiles
[f
].filename
,nfile
,fnm
))
2107 if (nexist
== nfiles
)
2109 bAppend
= bAppendReq
;
2111 else if (nexist
> 0)
2114 "Output file appending has been requested,\n"
2115 "but some output files listed in the checkpoint file %s\n"
2116 "are not present or are named differently by the current program:\n",
2118 fprintf(stderr
,"output files present:");
2119 for(f
=0; f
<nfiles
; f
++)
2121 if (exist_output_file(outputfiles
[f
].filename
,
2124 fprintf(stderr
," %s",outputfiles
[f
].filename
);
2127 fprintf(stderr
,"\n");
2128 fprintf(stderr
,"output files not present or named differently:");
2129 for(f
=0; f
<nfiles
; f
++)
2131 if (!exist_output_file(outputfiles
[f
].filename
,
2134 fprintf(stderr
," %s",outputfiles
[f
].filename
);
2137 fprintf(stderr
,"\n");
2139 gmx_fatal(FARGS
,"File appending requested, but only %d of the %d output files are present",nexist
,nfiles
);
2147 gmx_fatal(FARGS
,"File appending requested, but no output file information is stored in the checkpoint file");
2149 fn
= outputfiles
[0].filename
;
2150 if (strlen(fn
) < 4 ||
2151 gmx_strcasecmp(fn
+strlen(fn
)-4,ftp2ext(efLOG
)) == 0)
2153 gmx_fatal(FARGS
,"File appending requested, but the log file is not the first file listed in the checkpoint file");
2155 /* Set bAddPart to whether the suffix string '.part' is present
2156 * in the log file name.
2158 strcpy(suf_up
,part_suffix
);
2160 *bAddPart
= (strstr(fn
,part_suffix
) != NULL
||
2161 strstr(fn
,suf_up
) != NULL
);
2169 gmx_bcast(sizeof(*simulation_part
),simulation_part
,cr
);
2171 if (*simulation_part
> 0 && bAppendReq
)
2173 gmx_bcast(sizeof(bAppend
),&bAppend
,cr
);
2174 gmx_bcast(sizeof(*bAddPart
),bAddPart
,cr
);
2177 if (NULL
!= cpt_step
)