Basic support for transforming KeyValueTrees
[gromacs.git] / src / gromacs / mdlib / mdoutf.cpp
blob1335ad8fe2737c2184bd80247c234f564a5622f0
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016, 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.
35 #include "gmxpre.h"
37 #include "mdoutf.h"
39 #include "gromacs/commandline/filenm.h"
40 #include "gromacs/domdec/domdec.h"
41 #include "gromacs/domdec/domdec_struct.h"
42 #include "gromacs/fileio/checkpoint.h"
43 #include "gromacs/fileio/gmxfio.h"
44 #include "gromacs/fileio/tngio.h"
45 #include "gromacs/fileio/trrio.h"
46 #include "gromacs/fileio/xtcio.h"
47 #include "gromacs/fileio/xvgr.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/mdlib/mdrun.h"
50 #include "gromacs/mdlib/trajectory_writing.h"
51 #include "gromacs/mdtypes/commrec.h"
52 #include "gromacs/mdtypes/inputrec.h"
53 #include "gromacs/mdtypes/md_enums.h"
54 #include "gromacs/timing/wallcycle.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/pleasecite.h"
57 #include "gromacs/utility/smalloc.h"
59 struct gmx_mdoutf {
60 t_fileio *fp_trn;
61 t_fileio *fp_xtc;
62 tng_trajectory_t tng;
63 tng_trajectory_t tng_low_prec;
64 int x_compression_precision; /* only used by XTC output */
65 ener_file_t fp_ene;
66 const char *fn_cpt;
67 gmx_bool bKeepAndNumCPT;
68 int eIntegrator;
69 gmx_bool bExpanded;
70 int elamstats;
71 int simulation_part;
72 FILE *fp_dhdl;
73 FILE *fp_field;
74 int natoms_global;
75 int natoms_x_compressed;
76 gmx_groups_t *groups; /* for compressed position writing */
77 gmx_wallcycle_t wcycle;
78 rvec *f_global;
82 gmx_mdoutf_t init_mdoutf(FILE *fplog, int nfile, const t_filenm fnm[],
83 int mdrun_flags, const t_commrec *cr,
84 const t_inputrec *ir, gmx_mtop_t *top_global,
85 const gmx_output_env_t *oenv, gmx_wallcycle_t wcycle)
87 gmx_mdoutf_t of;
88 const char *appendMode = "a+", *writeMode = "w+", *filemode;
89 gmx_bool bAppendFiles, bCiteTng = FALSE;
90 int i;
92 snew(of, 1);
94 of->fp_trn = NULL;
95 of->fp_ene = NULL;
96 of->fp_xtc = NULL;
97 of->tng = NULL;
98 of->tng_low_prec = NULL;
99 of->fp_dhdl = NULL;
100 of->fp_field = NULL;
102 of->eIntegrator = ir->eI;
103 of->bExpanded = ir->bExpanded;
104 of->elamstats = ir->expandedvals->elamstats;
105 of->simulation_part = ir->simulation_part;
106 of->x_compression_precision = static_cast<int>(ir->x_compression_precision);
107 of->wcycle = wcycle;
108 of->f_global = NULL;
110 if (MASTER(cr))
112 bAppendFiles = (mdrun_flags & MD_APPENDFILES);
114 of->bKeepAndNumCPT = (mdrun_flags & MD_KEEPANDNUMCPT);
116 filemode = bAppendFiles ? appendMode : writeMode;
118 if (EI_DYNAMICS(ir->eI) &&
119 ir->nstxout_compressed > 0)
121 const char *filename;
122 filename = ftp2fn(efCOMPRESSED, nfile, fnm);
123 switch (fn2ftp(filename))
125 case efXTC:
126 of->fp_xtc = open_xtc(filename, filemode);
127 break;
128 case efTNG:
129 gmx_tng_open(filename, filemode[0], &of->tng_low_prec);
130 if (filemode[0] == 'w')
132 gmx_tng_prepare_low_prec_writing(of->tng_low_prec, top_global, ir);
134 bCiteTng = TRUE;
135 break;
136 default:
137 gmx_incons("Invalid reduced precision file format");
140 if ((EI_DYNAMICS(ir->eI) || EI_ENERGY_MINIMIZATION(ir->eI))
141 #ifndef GMX_FAHCORE
143 !(EI_DYNAMICS(ir->eI) &&
144 ir->nstxout == 0 &&
145 ir->nstvout == 0 &&
146 ir->nstfout == 0)
147 #endif
150 const char *filename;
151 filename = ftp2fn(efTRN, nfile, fnm);
152 switch (fn2ftp(filename))
154 case efTRR:
155 case efTRN:
156 /* If there is no uncompressed coordinate output and
157 there is compressed TNG output write forces
158 and/or velocities to the TNG file instead. */
159 if (ir->nstxout != 0 || ir->nstxout_compressed == 0 ||
160 !of->tng_low_prec)
162 of->fp_trn = gmx_trr_open(filename, filemode);
164 break;
165 case efTNG:
166 gmx_tng_open(filename, filemode[0], &of->tng);
167 if (filemode[0] == 'w')
169 gmx_tng_prepare_md_writing(of->tng, top_global, ir);
171 bCiteTng = TRUE;
172 break;
173 default:
174 gmx_incons("Invalid full precision file format");
177 if (EI_DYNAMICS(ir->eI) || EI_ENERGY_MINIMIZATION(ir->eI))
179 of->fp_ene = open_enx(ftp2fn(efEDR, nfile, fnm), filemode);
181 of->fn_cpt = opt2fn("-cpo", nfile, fnm);
183 if ((ir->efep != efepNO || ir->bSimTemp) && ir->fepvals->nstdhdl > 0 &&
184 (ir->fepvals->separate_dhdl_file == esepdhdlfileYES ) &&
185 EI_DYNAMICS(ir->eI))
187 if (bAppendFiles)
189 of->fp_dhdl = gmx_fio_fopen(opt2fn("-dhdl", nfile, fnm), filemode);
191 else
193 of->fp_dhdl = open_dhdl(opt2fn("-dhdl", nfile, fnm), ir, oenv);
197 if (opt2bSet("-field", nfile, fnm) &&
198 (ir->ex[XX].n || ir->ex[YY].n || ir->ex[ZZ].n))
200 if (bAppendFiles)
202 of->fp_field = gmx_fio_fopen(opt2fn("-field", nfile, fnm),
203 filemode);
205 else
207 of->fp_field = xvgropen(opt2fn("-field", nfile, fnm),
208 "Applied electric field", "Time (ps)",
209 "E (V/nm)", oenv);
213 /* Set up atom counts so they can be passed to actual
214 trajectory-writing routines later. Also, XTC writing needs
215 to know what (and how many) atoms might be in the XTC
216 groups, and how to look up later which ones they are. */
217 of->natoms_global = top_global->natoms;
218 of->groups = &top_global->groups;
219 of->natoms_x_compressed = 0;
220 for (i = 0; (i < top_global->natoms); i++)
222 if (ggrpnr(of->groups, egcCompressedX, i) == 0)
224 of->natoms_x_compressed++;
228 if (ir->nstfout && DOMAINDECOMP(cr))
230 snew(of->f_global, top_global->natoms);
234 if (bCiteTng)
236 please_cite(fplog, "Lundborg2014");
239 return of;
242 FILE *mdoutf_get_fp_field(gmx_mdoutf_t of)
244 return of->fp_field;
247 ener_file_t mdoutf_get_fp_ene(gmx_mdoutf_t of)
249 return of->fp_ene;
252 FILE *mdoutf_get_fp_dhdl(gmx_mdoutf_t of)
254 return of->fp_dhdl;
257 gmx_wallcycle_t mdoutf_get_wcycle(gmx_mdoutf_t of)
259 return of->wcycle;
262 void mdoutf_write_to_trajectory_files(FILE *fplog, t_commrec *cr,
263 gmx_mdoutf_t of,
264 int mdof_flags,
265 gmx_mtop_t *top_global,
266 gmx_int64_t step, double t,
267 t_state *state_local, t_state *state_global,
268 rvec *f_local)
270 rvec *local_v;
271 rvec *global_v;
272 rvec *f_global;
274 /* MRS -- defining these variables is to manage the difference
275 * between half step and full step velocities, but there must be a better way . . . */
277 local_v = state_local->v;
278 global_v = state_global->v;
280 if (DOMAINDECOMP(cr))
282 if (mdof_flags & MDOF_CPT)
284 dd_collect_state(cr->dd, state_local, state_global);
286 else
288 if (mdof_flags & (MDOF_X | MDOF_X_COMPRESSED))
290 dd_collect_vec(cr->dd, state_local, state_local->x,
291 state_global->x);
293 if (mdof_flags & MDOF_V)
295 dd_collect_vec(cr->dd, state_local, local_v,
296 global_v);
299 f_global = of->f_global;
300 if (mdof_flags & MDOF_F)
302 dd_collect_vec(cr->dd, state_local, f_local, f_global);
305 else
307 if (mdof_flags & MDOF_CPT)
309 /* All pointers in state_local are equal to state_global,
310 * but we need to copy the non-pointer entries.
312 state_global->lambda = state_local->lambda;
313 state_global->veta = state_local->veta;
314 state_global->vol0 = state_local->vol0;
315 copy_mat(state_local->box, state_global->box);
316 copy_mat(state_local->boxv, state_global->boxv);
317 copy_mat(state_local->svir_prev, state_global->svir_prev);
318 copy_mat(state_local->fvir_prev, state_global->fvir_prev);
319 copy_mat(state_local->pres_prev, state_global->pres_prev);
321 f_global = f_local;
324 if (MASTER(cr))
326 if (mdof_flags & MDOF_CPT)
328 fflush_tng(of->tng);
329 fflush_tng(of->tng_low_prec);
330 ivec one_ivec = { 1, 1, 1 };
331 write_checkpoint(of->fn_cpt, of->bKeepAndNumCPT,
332 fplog, cr,
333 DOMAINDECOMP(cr) ? cr->dd->nc : one_ivec,
334 DOMAINDECOMP(cr) ? cr->dd->nnodes : cr->nnodes,
335 of->eIntegrator, of->simulation_part,
336 of->bExpanded, of->elamstats, step, t, state_global);
339 if (mdof_flags & (MDOF_X | MDOF_V | MDOF_F))
341 if (of->fp_trn)
343 gmx_trr_write_frame(of->fp_trn, step, t, state_local->lambda[efptFEP],
344 state_local->box, top_global->natoms,
345 (mdof_flags & MDOF_X) ? state_global->x : NULL,
346 (mdof_flags & MDOF_V) ? global_v : NULL,
347 (mdof_flags & MDOF_F) ? f_global : NULL);
348 if (gmx_fio_flush(of->fp_trn) != 0)
350 gmx_file("Cannot write trajectory; maybe you are out of disk space?");
354 /* If a TNG file is open for uncompressed coordinate output also write
355 velocities and forces to it. */
356 else if (of->tng)
358 gmx_fwrite_tng(of->tng, FALSE, step, t, state_local->lambda[efptFEP],
359 state_local->box,
360 top_global->natoms,
361 (mdof_flags & MDOF_X) ? state_global->x : NULL,
362 (mdof_flags & MDOF_V) ? global_v : NULL,
363 (mdof_flags & MDOF_F) ? f_global : NULL);
365 /* If only a TNG file is open for compressed coordinate output (no uncompressed
366 coordinate output) also write forces and velocities to it. */
367 else if (of->tng_low_prec)
369 gmx_fwrite_tng(of->tng_low_prec, FALSE, step, t, state_local->lambda[efptFEP],
370 state_local->box,
371 top_global->natoms,
372 (mdof_flags & MDOF_X) ? state_global->x : NULL,
373 (mdof_flags & MDOF_V) ? global_v : NULL,
374 (mdof_flags & MDOF_F) ? f_global : NULL);
377 if (mdof_flags & MDOF_X_COMPRESSED)
379 rvec *xxtc = NULL;
381 if (of->natoms_x_compressed == of->natoms_global)
383 /* We are writing the positions of all of the atoms to
384 the compressed output */
385 xxtc = state_global->x;
387 else
389 /* We are writing the positions of only a subset of
390 the atoms to the compressed output, so we have to
391 make a copy of the subset of coordinates. */
392 int i, j;
394 snew(xxtc, of->natoms_x_compressed);
395 for (i = 0, j = 0; (i < of->natoms_global); i++)
397 if (ggrpnr(of->groups, egcCompressedX, i) == 0)
399 copy_rvec(state_global->x[i], xxtc[j++]);
403 if (write_xtc(of->fp_xtc, of->natoms_x_compressed, step, t,
404 state_local->box, xxtc, of->x_compression_precision) == 0)
406 gmx_fatal(FARGS, "XTC error - maybe you are out of disk space?");
408 gmx_fwrite_tng(of->tng_low_prec,
409 TRUE,
410 step,
412 state_local->lambda[efptFEP],
413 state_local->box,
414 of->natoms_x_compressed,
415 xxtc,
416 NULL,
417 NULL);
418 if (of->natoms_x_compressed != of->natoms_global)
420 sfree(xxtc);
426 void mdoutf_tng_close(gmx_mdoutf_t of)
428 if (of->tng || of->tng_low_prec)
430 wallcycle_start(of->wcycle, ewcTRAJ);
431 gmx_tng_close(&of->tng);
432 gmx_tng_close(&of->tng_low_prec);
433 wallcycle_stop(of->wcycle, ewcTRAJ);
437 void done_mdoutf(gmx_mdoutf_t of)
439 if (of->fp_ene != NULL)
441 close_enx(of->fp_ene);
443 if (of->fp_xtc)
445 close_xtc(of->fp_xtc);
447 if (of->fp_trn)
449 gmx_trr_close(of->fp_trn);
451 if (of->fp_dhdl != NULL)
453 gmx_fio_fclose(of->fp_dhdl);
455 if (of->fp_field != NULL)
457 /* This is opened sometimes with xvgropen, sometimes with
458 * gmx_fio_fopen, so we use the least common denominator for closing.
460 gmx_fio_fclose(of->fp_field);
462 if (of->f_global != NULL)
464 sfree(of->f_global);
467 gmx_tng_close(&of->tng);
468 gmx_tng_close(&of->tng_low_prec);
470 sfree(of);