Simplify code structure for C++ analysis tools
[alexxy/gromacs.git] / src / programs / mdrun / trajectory_writing.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "typedefs.h"
40 #include "smalloc.h"
41 #include "sysstuff.h"
42 #include "vec.h"
43 #include "sim_util.h"
44 #include "gmx_wallcycle.h"
45 #include "mdrun.h"
46 #include "confio.h"
47
48 void
49 do_trajectory_writing(FILE           *fplog,
50                       t_commrec      *cr,
51                       int             nfile,
52                       const t_filenm  fnm[],
53                       gmx_large_int_t step,
54                       gmx_large_int_t step_rel,
55                       double          t,
56                       t_inputrec     *ir,
57                       t_state        *state,
58                       t_state        *state_global,
59                       gmx_mtop_t     *top_global,
60                       t_forcerec     *fr,
61                       gmx_update_t    upd,
62                       gmx_mdoutf_t   *outf,
63                       t_mdebin       *mdebin,
64                       gmx_ekindata_t *ekind,
65                       rvec           *f,
66                       rvec           *f_global,
67                       gmx_wallcycle_t wcycle,
68                       gmx_rng_t       mcrng,
69                       int            *nchkpt,
70                       gmx_bool        bCPT,
71                       gmx_bool        bRerunMD,
72                       gmx_bool        bLastStep,
73                       gmx_bool        bDoConfOut,
74                       gmx_bool        bSumEkinhOld
75                       )
76 {
77     int   mdof_flags;
78     int   n_xtc    = -1;
79     rvec *x_xtc    = NULL;
80
81     mdof_flags = 0;
82     if (do_per_step(step, ir->nstxout))
83     {
84         mdof_flags |= MDOF_X;
85     }
86     if (do_per_step(step, ir->nstvout))
87     {
88         mdof_flags |= MDOF_V;
89     }
90     if (do_per_step(step, ir->nstfout))
91     {
92         mdof_flags |= MDOF_F;
93     }
94     if (do_per_step(step, ir->nstxtcout))
95     {
96         mdof_flags |= MDOF_XTC;
97     }
98     if (bCPT)
99     {
100         mdof_flags |= MDOF_CPT;
101     }
102     ;
103
104 #if defined(GMX_FAHCORE) || defined(GMX_WRITELASTSTEP)
105     if (bLastStep)
106     {
107         /* Enforce writing positions and velocities at end of run */
108         mdof_flags |= (MDOF_X | MDOF_V);
109     }
110 #endif
111 #ifdef GMX_FAHCORE
112     if (MASTER(cr))
113     {
114         fcReportProgress( ir->nsteps, step );
115     }
116
117     /* sync bCPT and fc record-keeping */
118     if (bCPT && MASTER(cr))
119     {
120         fcRequestCheckPoint();
121     }
122 #endif
123
124     if (mdof_flags != 0)
125     {
126         wallcycle_start(wcycle, ewcTRAJ);
127         if (bCPT)
128         {
129             if (state->flags & (1<<estLD_RNG))
130             {
131                 get_stochd_state(upd, state);
132             }
133             if (state->flags  & (1<<estMC_RNG))
134             {
135                 get_mc_state(mcrng, state);
136             }
137             if (MASTER(cr))
138             {
139                 if (bSumEkinhOld)
140                 {
141                     state_global->ekinstate.bUpToDate = FALSE;
142                 }
143                 else
144                 {
145                     update_ekinstate(&state_global->ekinstate, ekind);
146                     state_global->ekinstate.bUpToDate = TRUE;
147                 }
148                 update_energyhistory(&state_global->enerhist, mdebin);
149             }
150         }
151         write_traj(fplog, cr, outf, mdof_flags, top_global,
152                    step, t, state, state_global, f, f_global, &n_xtc, &x_xtc);
153         if (bCPT)
154         {
155             (*nchkpt)++;
156             bCPT = FALSE;
157         }
158         debug_gmx();
159         if (bLastStep && step_rel == ir->nsteps &&
160             bDoConfOut && MASTER(cr) &&
161             !bRerunMD)
162         {
163             /* x and v have been collected in write_traj,
164              * because a checkpoint file will always be written
165              * at the last step.
166              */
167             fprintf(stderr, "\nWriting final coordinates.\n");
168             if (fr->bMolPBC)
169             {
170                 /* Make molecules whole only for confout writing */
171                 do_pbc_mtop(fplog, ir->ePBC, state->box, top_global, state_global->x);
172             }
173             write_sto_conf_mtop(ftp2fn(efSTO, nfile, fnm),
174                                 *top_global->name, top_global,
175                                 state_global->x, state_global->v,
176                                 ir->ePBC, state->box);
177             debug_gmx();
178         }
179         wallcycle_stop(wcycle, ewcTRAJ);
180     }
181 }