2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
43 #include "gromacs/fileio/gmxfio.h"
44 #include "gromacs/legacyheaders/names.h"
45 #include "gromacs/legacyheaders/txtdump.h"
46 #include "gromacs/utility/fatalerror.h"
47 #include "gromacs/utility/futil.h"
48 #include "gromacs/utility/smalloc.h"
51 #define GROMACS_MAGIC 1993
53 static int nFloatSize(t_trnheader *sh)
59 nflsize = sh->box_size/(DIM*DIM);
63 nflsize = sh->x_size/(sh->natoms*DIM);
67 nflsize = sh->v_size/(sh->natoms*DIM);
71 nflsize = sh->f_size/(sh->natoms*DIM);
75 gmx_file("Can not determine precision of trn file");
78 if (((nflsize != sizeof(float)) && (nflsize != sizeof(double))))
80 gmx_fatal(FARGS, "Float size %d. Maybe different CPU?", nflsize);
86 static gmx_bool do_trnheader(t_fileio *fio, gmx_bool bRead, t_trnheader *sh, gmx_bool *bOK)
88 int magic = GROMACS_MAGIC;
89 static gmx_bool bFirst = TRUE;
94 gmx_fio_checktype(fio);
96 if (!gmx_fio_do_int(fio, magic) || magic != GROMACS_MAGIC)
103 *bOK = *bOK && gmx_fio_do_string(fio, buf);
106 fprintf(stderr, "trn version: %s ", buf);
111 sprintf(buf, "GMX_trn_file");
112 *bOK = *bOK && gmx_fio_do_string(fio, buf);
114 *bOK = *bOK && gmx_fio_do_int(fio, sh->ir_size);
115 *bOK = *bOK && gmx_fio_do_int(fio, sh->e_size);
116 *bOK = *bOK && gmx_fio_do_int(fio, sh->box_size);
117 *bOK = *bOK && gmx_fio_do_int(fio, sh->vir_size);
118 *bOK = *bOK && gmx_fio_do_int(fio, sh->pres_size);
119 *bOK = *bOK && gmx_fio_do_int(fio, sh->top_size);
120 *bOK = *bOK && gmx_fio_do_int(fio, sh->sym_size);
121 *bOK = *bOK && gmx_fio_do_int(fio, sh->x_size);
122 *bOK = *bOK && gmx_fio_do_int(fio, sh->v_size);
123 *bOK = *bOK && gmx_fio_do_int(fio, sh->f_size);
124 *bOK = *bOK && gmx_fio_do_int(fio, sh->natoms);
130 sh->bDouble = (nFloatSize(sh) == sizeof(double));
131 gmx_fio_setprecision(fio, sh->bDouble);
135 fprintf(stderr, "(%s precision)\n", sh->bDouble ? "double" : "single");
139 *bOK = *bOK && gmx_fio_do_int(fio, sh->step);
140 *bOK = *bOK && gmx_fio_do_int(fio, sh->nre);
141 *bOK = *bOK && gmx_fio_do_real(fio, sh->t);
142 *bOK = *bOK && gmx_fio_do_real(fio, sh->lambda);
147 void pr_trnheader(FILE *fp, int indent, char *title, t_trnheader *sh)
151 indent = pr_title(fp, indent, title);
152 (void) pr_indent(fp, indent);
153 (void) fprintf(fp, "box_size = %d\n", sh->box_size);
154 (void) pr_indent(fp, indent);
155 (void) fprintf(fp, "x_size = %d\n", sh->x_size);
156 (void) pr_indent(fp, indent);
157 (void) fprintf(fp, "v_size = %d\n", sh->v_size);
158 (void) pr_indent(fp, indent);
159 (void) fprintf(fp, "f_size = %d\n", sh->f_size);
161 (void) pr_indent(fp, indent);
162 (void) fprintf(fp, "natoms = %d\n", sh->natoms);
163 (void) pr_indent(fp, indent);
164 (void) fprintf(fp, "step = %d\n", sh->step);
165 (void) pr_indent(fp, indent);
166 (void) fprintf(fp, "t = %e\n", sh->t);
167 (void) pr_indent(fp, indent);
168 (void) fprintf(fp, "lambda = %e\n", sh->lambda);
172 static gmx_bool do_htrn(t_fileio *fio, t_trnheader *sh,
173 rvec *box, rvec *x, rvec *v, rvec *f)
179 if (sh->box_size != 0)
181 bOK = bOK && gmx_fio_ndo_rvec(fio, box, DIM);
183 if (sh->vir_size != 0)
185 bOK = bOK && gmx_fio_ndo_rvec(fio, pv, DIM);
187 if (sh->pres_size != 0)
189 bOK = bOK && gmx_fio_ndo_rvec(fio, pv, DIM);
193 bOK = bOK && gmx_fio_ndo_rvec(fio, x, sh->natoms);
197 bOK = bOK && gmx_fio_ndo_rvec(fio, v, sh->natoms);
201 bOK = bOK && gmx_fio_ndo_rvec(fio, f, sh->natoms);
207 static gmx_bool do_trn(t_fileio *fio, gmx_bool bRead, int *step, real *t, real *lambda,
208 rvec *box, int *natoms, rvec *x, rvec *v, rvec *f)
216 sh->box_size = (box) ? sizeof(matrix) : 0;
217 sh->x_size = ((x) ? (*natoms*sizeof(x[0])) : 0);
218 sh->v_size = ((v) ? (*natoms*sizeof(v[0])) : 0);
219 sh->f_size = ((f) ? (*natoms*sizeof(f[0])) : 0);
220 sh->natoms = *natoms;
224 sh->lambda = *lambda;
226 if (!do_trnheader(fio, bRead, sh, &bOK))
232 *natoms = sh->natoms;
235 *lambda = sh->lambda;
238 gmx_file("inputrec in trn file");
242 gmx_file("energies in trn file");
246 gmx_file("topology in trn file");
250 gmx_file("symbol table in trn file");
253 bOK = do_htrn(fio, sh, box, x, v, f);
260 /************************************************************
262 * The following routines are the exported ones
264 ************************************************************/
266 void read_trnheader(const char *fn, t_trnheader *trn)
271 fio = open_trn(fn, "r");
272 if (!do_trnheader(fio, TRUE, trn, &bOK))
274 gmx_fatal(FARGS, "Empty file %s", fn);
279 gmx_bool fread_trnheader(t_fileio *fio, t_trnheader *trn, gmx_bool *bOK)
281 return do_trnheader(fio, TRUE, trn, bOK);
284 void write_trn(const char *fn, int step, real t, real lambda,
285 rvec *box, int natoms, rvec *x, rvec *v, rvec *f)
289 fio = open_trn(fn, "w");
290 do_trn(fio, FALSE, &step, &t, &lambda, box, &natoms, x, v, f);
294 void read_trn(const char *fn, int *step, real *t, real *lambda,
295 rvec *box, int *natoms, rvec *x, rvec *v, rvec *f)
299 fio = open_trn(fn, "r");
300 (void) do_trn(fio, TRUE, step, t, lambda, box, natoms, x, v, f);
304 void fwrite_trn(t_fileio *fio, int step, real t, real lambda,
305 rvec *box, int natoms, rvec *x, rvec *v, rvec *f)
307 if (do_trn(fio, FALSE, &step, &t, &lambda, box, &natoms, x, v, f) == FALSE)
309 gmx_file("Cannot write trajectory frame; maybe you are out of disk space?");
314 gmx_bool fread_trn(t_fileio *fio, int *step, real *t, real *lambda,
315 rvec *box, int *natoms, rvec *x, rvec *v, rvec *f)
317 return do_trn(fio, TRUE, step, t, lambda, box, natoms, x, v, f);
320 gmx_bool fread_htrn(t_fileio *fio, t_trnheader *trn, rvec *box, rvec *x, rvec *v,
323 return do_htrn(fio, trn, box, x, v, f);
326 t_fileio *open_trn(const char *fn, const char *mode)
328 return gmx_fio_open(fn, mode);
331 void close_trn(t_fileio *fio)