2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gromacs/legacyheaders/xvgr.h"
38 #include "gromacs/legacyheaders/mdrun.h"
39 #include "gromacs/legacyheaders/smalloc.h"
40 #include "gromacs/legacyheaders/types/commrec.h"
41 #include "gromacs/legacyheaders/mvdata.h"
42 #include "gromacs/legacyheaders/domdec.h"
46 #include "trajectory_writing.h"
47 #include "checkpoint.h"
54 tng_trajectory_t tng_low_prec;
55 int x_compression_precision; /* only used by XTC output */
58 gmx_bool bKeepAndNumCPT;
66 int natoms_x_compressed;
67 gmx_groups_t *groups; /* for compressed position writing */
71 gmx_mdoutf_t init_mdoutf(FILE *fplog, int nfile, const t_filenm fnm[],
72 int mdrun_flags, const t_commrec *cr,
73 const t_inputrec *ir, gmx_mtop_t *top_global,
74 const output_env_t oenv)
78 gmx_bool bAppendFiles, bCiteTng = FALSE;
87 of->tng_low_prec = NULL;
91 of->eIntegrator = ir->eI;
92 of->bExpanded = ir->bExpanded;
93 of->elamstats = ir->expandedvals->elamstats;
94 of->simulation_part = ir->simulation_part;
95 of->x_compression_precision = ir->x_compression_precision;
99 bAppendFiles = (mdrun_flags & MD_APPENDFILES);
101 of->bKeepAndNumCPT = (mdrun_flags & MD_KEEPANDNUMCPT);
103 sprintf(filemode, bAppendFiles ? "a+" : "w+");
105 if ((EI_DYNAMICS(ir->eI) || EI_ENERGY_MINIMIZATION(ir->eI))
108 !(EI_DYNAMICS(ir->eI) &&
115 const char *filename;
116 filename = ftp2fn(efTRN, nfile, fnm);
117 switch (fn2ftp(filename))
121 of->fp_trn = open_trn(filename, filemode);
124 gmx_tng_open(filename, filemode[0], &of->tng);
125 if (filemode[0] == 'w')
127 gmx_tng_prepare_md_writing(of->tng, top_global, ir);
132 gmx_incons("Invalid full precision file format");
135 if (EI_DYNAMICS(ir->eI) &&
136 ir->nstxout_compressed > 0)
138 const char *filename;
139 filename = ftp2fn(efCOMPRESSED, nfile, fnm);
140 switch (fn2ftp(filename))
143 of->fp_xtc = open_xtc(filename, filemode);
146 gmx_tng_open(filename, filemode[0], &of->tng_low_prec);
147 if (filemode[0] == 'w')
149 gmx_tng_prepare_low_prec_writing(of->tng_low_prec, top_global, ir);
154 gmx_incons("Invalid reduced precision file format");
157 if (EI_DYNAMICS(ir->eI) || EI_ENERGY_MINIMIZATION(ir->eI))
159 of->fp_ene = open_enx(ftp2fn(efEDR, nfile, fnm), filemode);
161 of->fn_cpt = opt2fn("-cpo", nfile, fnm);
163 if ((ir->efep != efepNO || ir->bSimTemp) && ir->fepvals->nstdhdl > 0 &&
164 (ir->fepvals->separate_dhdl_file == esepdhdlfileYES ) &&
169 of->fp_dhdl = gmx_fio_fopen(opt2fn("-dhdl", nfile, fnm), filemode);
173 of->fp_dhdl = open_dhdl(opt2fn("-dhdl", nfile, fnm), ir, oenv);
177 if (opt2bSet("-field", nfile, fnm) &&
178 (ir->ex[XX].n || ir->ex[YY].n || ir->ex[ZZ].n))
182 of->fp_dhdl = gmx_fio_fopen(opt2fn("-field", nfile, fnm),
187 of->fp_field = xvgropen(opt2fn("-field", nfile, fnm),
188 "Applied electric field", "Time (ps)",
193 /* Set up atom counts so they can be passed to actual
194 trajectory-writing routines later. Also, XTC writing needs
195 to know what (and how many) atoms might be in the XTC
196 groups, and how to look up later which ones they are. */
197 of->natoms_global = top_global->natoms;
198 of->groups = &top_global->groups;
199 of->natoms_x_compressed = 0;
200 for (i = 0; (i < top_global->natoms); i++)
202 if (ggrpnr(of->groups, egcCompressedX, i) == 0)
204 of->natoms_x_compressed++;
211 please_cite(fplog, "Lundborg2014");
217 FILE *mdoutf_get_fp_field(gmx_mdoutf_t of)
222 ener_file_t mdoutf_get_fp_ene(gmx_mdoutf_t of)
227 FILE *mdoutf_get_fp_dhdl(gmx_mdoutf_t of)
232 void mdoutf_write_to_trajectory_files(FILE *fplog, t_commrec *cr,
235 gmx_mtop_t *top_global,
236 gmx_int64_t step, double t,
237 t_state *state_local, t_state *state_global,
238 rvec *f_local, rvec *f_global)
243 /* MRS -- defining these variables is to manage the difference
244 * between half step and full step velocities, but there must be a better way . . . */
246 local_v = state_local->v;
247 global_v = state_global->v;
249 if (DOMAINDECOMP(cr))
251 if (mdof_flags & MDOF_CPT)
253 dd_collect_state(cr->dd, state_local, state_global);
257 if (mdof_flags & (MDOF_X | MDOF_X_COMPRESSED))
259 dd_collect_vec(cr->dd, state_local, state_local->x,
262 if (mdof_flags & MDOF_V)
264 dd_collect_vec(cr->dd, state_local, local_v,
268 if (mdof_flags & MDOF_F)
270 dd_collect_vec(cr->dd, state_local, f_local, f_global);
275 if (mdof_flags & MDOF_CPT)
277 /* All pointers in state_local are equal to state_global,
278 * but we need to copy the non-pointer entries.
280 state_global->lambda = state_local->lambda;
281 state_global->veta = state_local->veta;
282 state_global->vol0 = state_local->vol0;
283 copy_mat(state_local->box, state_global->box);
284 copy_mat(state_local->boxv, state_global->boxv);
285 copy_mat(state_local->svir_prev, state_global->svir_prev);
286 copy_mat(state_local->fvir_prev, state_global->fvir_prev);
287 copy_mat(state_local->pres_prev, state_global->pres_prev);
293 if (mdof_flags & MDOF_CPT)
296 fflush_tng(of->tng_low_prec);
297 write_checkpoint(of->fn_cpt, of->bKeepAndNumCPT,
298 fplog, cr, of->eIntegrator, of->simulation_part,
299 of->bExpanded, of->elamstats, step, t, state_global);
302 if (mdof_flags & (MDOF_X | MDOF_V | MDOF_F))
306 fwrite_trn(of->fp_trn, step, t, state_local->lambda[efptFEP],
307 state_local->box, top_global->natoms,
308 (mdof_flags & MDOF_X) ? state_global->x : NULL,
309 (mdof_flags & MDOF_V) ? global_v : NULL,
310 (mdof_flags & MDOF_F) ? f_global : NULL);
311 if (gmx_fio_flush(of->fp_trn) != 0)
313 gmx_file("Cannot write trajectory; maybe you are out of disk space?");
317 gmx_fwrite_tng(of->tng, FALSE, step, t, state_local->lambda[efptFEP],
318 (const rvec *) state_local->box,
320 (mdof_flags & MDOF_X) ? (const rvec *) state_global->x : NULL,
321 (mdof_flags & MDOF_V) ? (const rvec *) global_v : NULL,
322 (mdof_flags & MDOF_F) ? (const rvec *) f_global : NULL);
324 if (mdof_flags & MDOF_X_COMPRESSED)
328 if (of->natoms_x_compressed == of->natoms_global)
330 /* We are writing the positions of all of the atoms to
331 the compressed output */
332 xxtc = state_global->x;
336 /* We are writing the positions of only a subset of
337 the atoms to the compressed output, so we have to
338 make a copy of the subset of coordinates. */
341 snew(xxtc, of->natoms_x_compressed);
342 for (i = 0, j = 0; (i < of->natoms_x_compressed); i++)
344 if (ggrpnr(of->groups, egcCompressedX, i) == 0)
346 copy_rvec(state_global->x[i], xxtc[j++]);
350 if (write_xtc(of->fp_xtc, of->natoms_x_compressed, step, t,
351 state_local->box, xxtc, of->x_compression_precision) == 0)
353 gmx_fatal(FARGS, "XTC error - maybe you are out of disk space?");
355 gmx_fwrite_tng(of->tng_low_prec,
359 state_local->lambda[efptFEP],
360 (const rvec *) state_local->box,
361 of->natoms_x_compressed,
365 if (of->natoms_x_compressed != of->natoms_global)
373 void done_mdoutf(gmx_mdoutf_t of)
375 if (of->fp_ene != NULL)
377 close_enx(of->fp_ene);
381 close_xtc(of->fp_xtc);
385 close_trn(of->fp_trn);
387 if (of->fp_dhdl != NULL)
389 gmx_fio_fclose(of->fp_dhdl);
391 if (of->fp_field != NULL)
393 gmx_fio_fclose(of->fp_field);
395 gmx_tng_close(&of->tng);
396 gmx_tng_close(&of->tng_low_prec);