Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / mdlib / init.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROwing Monsters And Cloning Shrimps
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <stdio.h>
41 #include "typedefs.h"
42 #include "tpxio.h"
43 #include "smalloc.h"
44 #include "vec.h"
45 #include "main.h"
46 #include "mvdata.h"
47 #include "gmx_fatal.h"
48 #include "symtab.h"
49 #include "txtdump.h"
50 #include "mdatoms.h"
51 #include "mdrun.h"
52 #include "statutil.h"
53 #include "names.h"
54 #include "calcgrid.h"
55 #include "gmx_random.h"
56 #include "update.h"
57 #include "mdebin.h"
58
59 #define BUFSIZE 256
60
61 #define NOT_FINISHED(l1,l2) \
62   printf("not finished yet: lines %d .. %d in %s\n",l1,l2,__FILE__)
63
64 void set_state_entries(t_state *state,const t_inputrec *ir,int nnodes)
65 {
66   int nnhpres;
67
68   /* The entries in the state in the tpx file might not correspond
69    * with what is needed, so we correct this here.
70    */
71   state->flags = 0;
72   if (ir->efep != efepNO)
73     state->flags |= (1<<estLAMBDA);
74   state->flags |= (1<<estX);
75   if (state->x == NULL)
76     snew(state->x,state->nalloc);
77   if (EI_DYNAMICS(ir->eI)) {
78     state->flags |= (1<<estV);
79     if (state->v == NULL)
80       snew(state->v,state->nalloc);
81   }
82   if (ir->eI == eiSD2) {
83     state->flags |= (1<<estSDX);
84     if (state->sd_X == NULL) {
85       /* sd_X is not stored in the tpx file, so we need to allocate it */
86       snew(state->sd_X,state->nalloc);
87     }
88   }
89     if (ir->eI == eiCG)
90     {
91         state->flags |= (1<<estCGP);
92         if (state->cg_p == NULL)
93         {
94             /* cg_p is not stored in the tpx file, so we need to allocate it */
95             snew(state->cg_p,state->nalloc);
96         }
97     }
98   if (EI_SD(ir->eI) || ir->eI == eiBD || ir->etc == etcVRESCALE) {
99     state->nrng  = gmx_rng_n();
100     state->nrngi = 1;
101     if (EI_SD(ir->eI) || ir->eI == eiBD) {
102       /* This will be correct later with DD */
103       state->nrng  *= nnodes;
104       state->nrngi *= nnodes;
105     }
106     state->flags |= ((1<<estLD_RNG) | (1<<estLD_RNGI));
107     snew(state->ld_rng, state->nrng);
108     snew(state->ld_rngi,state->nrngi);
109   } else {
110     state->nrng = 0;
111   }
112   state->nnhpres = 0;
113   if (ir->ePBC != epbcNONE) {
114     state->flags |= (1<<estBOX);
115     if (PRESERVE_SHAPE(*ir)) {
116       state->flags |= (1<<estBOX_REL);
117     }
118     if ((ir->epc == epcPARRINELLORAHMAN) || (ir->epc == epcMTTK)) {
119       state->flags |= (1<<estBOXV);
120     }
121     if (ir->epc != epcNO) {
122       if (IR_NPT_TROTTER(ir)) {
123         state->nnhpres = 1;
124         state->flags |= (1<<estNHPRES_XI);
125         state->flags |= (1<<estNHPRES_VXI);
126         state->flags |= (1<<estSVIR_PREV);
127         state->flags |= (1<<estFVIR_PREV);
128         state->flags |= (1<<estVETA);
129         state->flags |= (1<<estVOL0);
130       } else {
131         state->flags |= (1<<estPRES_PREV);
132       }
133     }
134   }
135
136   if (ir->etc == etcNOSEHOOVER) {
137     state->flags |= (1<<estNH_XI);
138     state->flags |= (1<<estNH_VXI);
139   }
140   
141   if (ir->etc == etcVRESCALE) {
142     state->flags |= (1<<estTC_INT);
143   }
144   
145   init_gtc_state(state,state->ngtc,state->nnhpres,ir->opts.nhchainlength); /* allocate the space for nose-hoover chains */
146   init_ekinstate(&state->ekinstate,ir);
147
148   init_energyhistory(&state->enerhist);
149 }
150
151
152 void init_parallel(FILE *log, t_commrec *cr, t_inputrec *inputrec,
153                    gmx_mtop_t *mtop)
154 {
155   bcast_ir_mtop(cr,inputrec,mtop);
156
157
158   if (inputrec->eI == eiBD || EI_SD(inputrec->eI)) {
159     /* Make sure the random seeds are different on each node */
160     inputrec->ld_seed += cr->nodeid;
161   }
162 }
163
164