Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / fileio / mdoutf.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 #include "mdoutf.h"
36
37 #include "gromacs/legacyheaders/xvgr.h"
38 #include "trnio.h"
39 #include "xtcio.h"
40 #include "gromacs/legacyheaders/mdrun.h"
41 #include "gromacs/legacyheaders/smalloc.h"
42
43 gmx_mdoutf_t *init_mdoutf(int nfile, const t_filenm fnm[], int mdrun_flags,
44                           const t_commrec *cr, const t_inputrec *ir,
45                           const output_env_t oenv)
46 {
47     gmx_mdoutf_t *of;
48     char          filemode[3];
49     gmx_bool      bAppendFiles;
50
51     snew(of, 1);
52
53     of->fp_trn   = NULL;
54     of->fp_ene   = NULL;
55     of->fp_xtc   = NULL;
56     of->fp_dhdl  = NULL;
57     of->fp_field = NULL;
58
59     of->eIntegrator     = ir->eI;
60     of->bExpanded       = ir->bExpanded;
61     of->elamstats       = ir->expandedvals->elamstats;
62     of->simulation_part = ir->simulation_part;
63
64     if (MASTER(cr))
65     {
66         bAppendFiles = (mdrun_flags & MD_APPENDFILES);
67
68         of->bKeepAndNumCPT = (mdrun_flags & MD_KEEPANDNUMCPT);
69
70         sprintf(filemode, bAppendFiles ? "a+" : "w+");
71
72         if ((EI_DYNAMICS(ir->eI) || EI_ENERGY_MINIMIZATION(ir->eI))
73 #ifndef GMX_FAHCORE
74             &&
75             !(EI_DYNAMICS(ir->eI) &&
76               ir->nstxout == 0 &&
77               ir->nstvout == 0 &&
78               ir->nstfout == 0)
79 #endif
80             )
81         {
82             of->fp_trn = open_trn(ftp2fn(efTRN, nfile, fnm), filemode);
83         }
84         if (EI_DYNAMICS(ir->eI) &&
85             ir->nstxtcout > 0)
86         {
87             of->fp_xtc   = open_xtc(ftp2fn(efXTC, nfile, fnm), filemode);
88             of->xtc_prec = ir->xtcprec;
89         }
90         if (EI_DYNAMICS(ir->eI) || EI_ENERGY_MINIMIZATION(ir->eI))
91         {
92             of->fp_ene = open_enx(ftp2fn(efEDR, nfile, fnm), filemode);
93         }
94         of->fn_cpt = opt2fn("-cpo", nfile, fnm);
95
96         if ((ir->efep != efepNO || ir->bSimTemp) && ir->fepvals->nstdhdl > 0 &&
97             (ir->fepvals->separate_dhdl_file == esepdhdlfileYES ) &&
98             EI_DYNAMICS(ir->eI))
99         {
100             if (bAppendFiles)
101             {
102                 of->fp_dhdl = gmx_fio_fopen(opt2fn("-dhdl", nfile, fnm), filemode);
103             }
104             else
105             {
106                 of->fp_dhdl = open_dhdl(opt2fn("-dhdl", nfile, fnm), ir, oenv);
107             }
108         }
109
110         if (opt2bSet("-field", nfile, fnm) &&
111             (ir->ex[XX].n || ir->ex[YY].n || ir->ex[ZZ].n))
112         {
113             if (bAppendFiles)
114             {
115                 of->fp_dhdl = gmx_fio_fopen(opt2fn("-field", nfile, fnm),
116                                             filemode);
117             }
118             else
119             {
120                 of->fp_field = xvgropen(opt2fn("-field", nfile, fnm),
121                                         "Applied electric field", "Time (ps)",
122                                         "E (V/nm)", oenv);
123             }
124         }
125     }
126
127     return of;
128 }
129
130 void done_mdoutf(gmx_mdoutf_t *of)
131 {
132     if (of->fp_ene != NULL)
133     {
134         close_enx(of->fp_ene);
135     }
136     if (of->fp_xtc)
137     {
138         close_xtc(of->fp_xtc);
139     }
140     if (of->fp_trn)
141     {
142         close_trn(of->fp_trn);
143     }
144     if (of->fp_dhdl != NULL)
145     {
146         gmx_fio_fclose(of->fp_dhdl);
147     }
148     if (of->fp_field != NULL)
149     {
150         gmx_fio_fclose(of->fp_field);
151     }
152
153     sfree(of);
154 }