Move types/topology.h to topology/
[alexxy/gromacs.git] / src / gromacs / gmxlib / typedefs.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /* This file is completely threadsafe - keep it that way! */
38 #include "typedefs.h"
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include <string.h>
45
46 #include "thread_mpi/threads.h"
47
48 #include "macros.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/pbcutil/pbc.h"
51 #include "gromacs/random/random.h"
52 #include "gromacs/utility/smalloc.h"
53
54 /* The source code in this file should be thread-safe.
55       Please keep it that way. */
56
57
58
59 static gmx_bool            bOverAllocDD     = FALSE;
60 static tMPI_Thread_mutex_t over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
61
62
63 void set_over_alloc_dd(gmx_bool set)
64 {
65     tMPI_Thread_mutex_lock(&over_alloc_mutex);
66     /* we just make sure that we don't set this at the same time.
67        We don't worry too much about reading this rarely-set variable */
68     bOverAllocDD = set;
69     tMPI_Thread_mutex_unlock(&over_alloc_mutex);
70 }
71
72 int over_alloc_dd(int n)
73 {
74     if (bOverAllocDD)
75     {
76         return OVER_ALLOC_FAC*n + 100;
77     }
78     else
79     {
80         return n;
81     }
82 }
83
84 int gmx_int64_to_int(gmx_int64_t step, const char *warn)
85 {
86     int i;
87
88     i = (int)step;
89
90     if (warn != NULL && (step < INT_MIN || step > INT_MAX))
91     {
92         fprintf(stderr, "\nWARNING during %s:\n", warn);
93         fprintf(stderr, "step value ");
94         fprintf(stderr, "%"GMX_PRId64, step);
95         fprintf(stderr, " does not fit in int, converted to %d\n\n", i);
96     }
97
98     return i;
99 }
100
101 char *gmx_step_str(gmx_int64_t i, char *buf)
102 {
103     sprintf(buf, "%"GMX_PRId64, i);
104
105     return buf;
106 }
107
108 void init_inputrec(t_inputrec *ir)
109 {
110     memset(ir, 0, (size_t)sizeof(*ir));
111     snew(ir->fepvals, 1);
112     snew(ir->expandedvals, 1);
113     snew(ir->simtempvals, 1);
114 }
115
116 static void done_pull_group(t_pull_group *pgrp)
117 {
118     if (pgrp->nat > 0)
119     {
120         sfree(pgrp->ind);
121         sfree(pgrp->ind_loc);
122         sfree(pgrp->weight);
123         sfree(pgrp->weight_loc);
124     }
125 }
126
127 static void done_pull(t_pull *pull)
128 {
129     int i;
130
131     for (i = 0; i < pull->ngroup+1; i++)
132     {
133         done_pull_group(pull->group);
134         done_pull_group(pull->dyna);
135     }
136 }
137
138 void done_inputrec(t_inputrec *ir)
139 {
140     int m;
141
142     for (m = 0; (m < DIM); m++)
143     {
144         if (ir->ex[m].a)
145         {
146             sfree(ir->ex[m].a);
147         }
148         if (ir->ex[m].phi)
149         {
150             sfree(ir->ex[m].phi);
151         }
152         if (ir->et[m].a)
153         {
154             sfree(ir->et[m].a);
155         }
156         if (ir->et[m].phi)
157         {
158             sfree(ir->et[m].phi);
159         }
160     }
161
162     sfree(ir->opts.nrdf);
163     sfree(ir->opts.ref_t);
164     sfree(ir->opts.annealing);
165     sfree(ir->opts.anneal_npoints);
166     sfree(ir->opts.anneal_time);
167     sfree(ir->opts.anneal_temp);
168     sfree(ir->opts.tau_t);
169     sfree(ir->opts.acc);
170     sfree(ir->opts.nFreeze);
171     sfree(ir->opts.QMmethod);
172     sfree(ir->opts.QMbasis);
173     sfree(ir->opts.QMcharge);
174     sfree(ir->opts.QMmult);
175     sfree(ir->opts.bSH);
176     sfree(ir->opts.CASorbitals);
177     sfree(ir->opts.CASelectrons);
178     sfree(ir->opts.SAon);
179     sfree(ir->opts.SAoff);
180     sfree(ir->opts.SAsteps);
181     sfree(ir->opts.bOPT);
182     sfree(ir->opts.bTS);
183
184     if (ir->pull)
185     {
186         done_pull(ir->pull);
187         sfree(ir->pull);
188     }
189 }
190
191 static void zero_history(history_t *hist)
192 {
193     hist->disre_initf  = 0;
194     hist->ndisrepairs  = 0;
195     hist->disre_rm3tav = NULL;
196     hist->orire_initf  = 0;
197     hist->norire_Dtav  = 0;
198     hist->orire_Dtav   = NULL;
199 }
200
201 static void zero_ekinstate(ekinstate_t *eks)
202 {
203     eks->ekin_n         = 0;
204     eks->ekinh          = NULL;
205     eks->ekinf          = NULL;
206     eks->ekinh_old      = NULL;
207     eks->ekinscalef_nhc = NULL;
208     eks->ekinscaleh_nhc = NULL;
209     eks->vscale_nhc     = NULL;
210     eks->dekindl        = 0;
211     eks->mvcos          = 0;
212 }
213
214 static void init_swapstate(swapstate_t *swapstate)
215 {
216     int ii, ic;
217
218     swapstate->eSwapCoords = 0;
219     swapstate->nAverage    = 0;
220
221     /* Ion/water position swapping */
222     for (ic = 0; ic < eCompNR; ic++)
223     {
224         for (ii = 0; ii < eIonNR; ii++)
225         {
226             swapstate->nat_req[ic][ii]        = 0;
227             swapstate->nat_req_p[ic][ii]      = NULL;
228             swapstate->inflow_netto[ic][ii]   = 0;
229             swapstate->inflow_netto_p[ic][ii] = NULL;
230             swapstate->nat_past[ic][ii]       = NULL;
231             swapstate->nat_past_p[ic][ii]     = NULL;
232             swapstate->fluxfromAtoB[ic][ii]   = 0;
233             swapstate->fluxfromAtoB_p[ic][ii] = NULL;
234         }
235     }
236     swapstate->fluxleak               = NULL;
237     swapstate->nions                  = 0;
238     swapstate->comp_from              = NULL;
239     swapstate->channel_label          = NULL;
240     swapstate->bFromCpt               = 0;
241     swapstate->nat[eChan0]            = 0;
242     swapstate->nat[eChan1]            = 0;
243     swapstate->xc_old_whole[eChan0]   = NULL;
244     swapstate->xc_old_whole[eChan1]   = NULL;
245     swapstate->xc_old_whole_p[eChan0] = NULL;
246     swapstate->xc_old_whole_p[eChan1] = NULL;
247 }
248
249 void init_energyhistory(energyhistory_t * enerhist)
250 {
251     enerhist->nener = 0;
252
253     enerhist->ener_ave     = NULL;
254     enerhist->ener_sum     = NULL;
255     enerhist->ener_sum_sim = NULL;
256     enerhist->dht          = NULL;
257
258     enerhist->nsteps     = 0;
259     enerhist->nsum       = 0;
260     enerhist->nsteps_sim = 0;
261     enerhist->nsum_sim   = 0;
262
263     enerhist->dht = NULL;
264 }
265
266 static void done_delta_h_history(delta_h_history_t *dht)
267 {
268     int i;
269
270     for (i = 0; i < dht->nndh; i++)
271     {
272         sfree(dht->dh[i]);
273     }
274     sfree(dht->dh);
275     sfree(dht->ndh);
276 }
277
278 void done_energyhistory(energyhistory_t * enerhist)
279 {
280     sfree(enerhist->ener_ave);
281     sfree(enerhist->ener_sum);
282     sfree(enerhist->ener_sum_sim);
283
284     if (enerhist->dht != NULL)
285     {
286         done_delta_h_history(enerhist->dht);
287         sfree(enerhist->dht);
288     }
289 }
290
291 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
292 {
293     int i, j;
294
295     state->ngtc          = ngtc;
296     state->nnhpres       = nnhpres;
297     state->nhchainlength = nhchainlength;
298     if (state->ngtc > 0)
299     {
300         snew(state->nosehoover_xi, state->nhchainlength*state->ngtc);
301         snew(state->nosehoover_vxi, state->nhchainlength*state->ngtc);
302         snew(state->therm_integral, state->ngtc);
303         for (i = 0; i < state->ngtc; i++)
304         {
305             for (j = 0; j < state->nhchainlength; j++)
306             {
307                 state->nosehoover_xi[i*state->nhchainlength + j]   = 0.0;
308                 state->nosehoover_vxi[i*state->nhchainlength + j]  = 0.0;
309             }
310         }
311         for (i = 0; i < state->ngtc; i++)
312         {
313             state->therm_integral[i]  = 0.0;
314         }
315     }
316     else
317     {
318         state->nosehoover_xi  = NULL;
319         state->nosehoover_vxi = NULL;
320         state->therm_integral = NULL;
321     }
322
323     if (state->nnhpres > 0)
324     {
325         snew(state->nhpres_xi, state->nhchainlength*nnhpres);
326         snew(state->nhpres_vxi, state->nhchainlength*nnhpres);
327         for (i = 0; i < nnhpres; i++)
328         {
329             for (j = 0; j < state->nhchainlength; j++)
330             {
331                 state->nhpres_xi[i*nhchainlength + j]   = 0.0;
332                 state->nhpres_vxi[i*nhchainlength + j]  = 0.0;
333             }
334         }
335     }
336     else
337     {
338         state->nhpres_xi  = NULL;
339         state->nhpres_vxi = NULL;
340     }
341 }
342
343
344 void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int nlambda)
345 {
346     int i;
347
348     state->natoms = natoms;
349     state->flags  = 0;
350     state->lambda = 0;
351     snew(state->lambda, efptNR);
352     for (i = 0; i < efptNR; i++)
353     {
354         state->lambda[i] = 0;
355     }
356     state->veta   = 0;
357     clear_mat(state->box);
358     clear_mat(state->box_rel);
359     clear_mat(state->boxv);
360     clear_mat(state->pres_prev);
361     clear_mat(state->svir_prev);
362     clear_mat(state->fvir_prev);
363     init_gtc_state(state, ngtc, nnhpres, nhchainlength);
364     state->nalloc = state->natoms;
365     if (state->nalloc > 0)
366     {
367         snew(state->x, state->nalloc);
368         snew(state->v, state->nalloc);
369     }
370     else
371     {
372         state->x = NULL;
373         state->v = NULL;
374     }
375     state->sd_X = NULL;
376     state->cg_p = NULL;
377     zero_history(&state->hist);
378     zero_ekinstate(&state->ekinstate);
379     init_energyhistory(&state->enerhist);
380     init_df_history(&state->dfhist, nlambda);
381     init_swapstate(&state->swapstate);
382     state->ddp_count       = 0;
383     state->ddp_count_cg_gl = 0;
384     state->cg_gl           = NULL;
385     state->cg_gl_nalloc    = 0;
386 }
387
388 void done_state(t_state *state)
389 {
390     if (state->x)
391     {
392         sfree(state->x);
393     }
394     if (state->v)
395     {
396         sfree(state->v);
397     }
398     if (state->sd_X)
399     {
400         sfree(state->sd_X);
401     }
402     if (state->cg_p)
403     {
404         sfree(state->cg_p);
405     }
406     state->nalloc = 0;
407     if (state->cg_gl)
408     {
409         sfree(state->cg_gl);
410     }
411     state->cg_gl_nalloc = 0;
412     if (state->lambda)
413     {
414         sfree(state->lambda);
415     }
416     if (state->ngtc > 0)
417     {
418         sfree(state->nosehoover_xi);
419         sfree(state->nosehoover_vxi);
420         sfree(state->therm_integral);
421     }
422 }
423
424 t_state *serial_init_local_state(t_state *state_global)
425 {
426     int      i;
427     t_state *state_local;
428
429     snew(state_local, 1);
430
431     /* Copy all the contents */
432     *state_local = *state_global;
433     snew(state_local->lambda, efptNR);
434     /* local storage for lambda */
435     for (i = 0; i < efptNR; i++)
436     {
437         state_local->lambda[i] = state_global->lambda[i];
438     }
439
440     return state_local;
441 }
442
443 static void do_box_rel(t_inputrec *ir, matrix box_rel, matrix b, gmx_bool bInit)
444 {
445     int d, d2;
446
447     for (d = YY; d <= ZZ; d++)
448     {
449         for (d2 = XX; d2 <= (ir->epct == epctSEMIISOTROPIC ? YY : ZZ); d2++)
450         {
451             /* We need to check if this box component is deformed
452              * or if deformation of another component might cause
453              * changes in this component due to box corrections.
454              */
455             if (ir->deform[d][d2] == 0 &&
456                 !(d == ZZ && d2 == XX && ir->deform[d][YY] != 0 &&
457                   (b[YY][d2] != 0 || ir->deform[YY][d2] != 0)))
458             {
459                 if (bInit)
460                 {
461                     box_rel[d][d2] = b[d][d2]/b[XX][XX];
462                 }
463                 else
464                 {
465                     b[d][d2] = b[XX][XX]*box_rel[d][d2];
466                 }
467             }
468         }
469     }
470 }
471
472 void set_box_rel(t_inputrec *ir, t_state *state)
473 {
474     /* Make sure the box obeys the restrictions before we fix the ratios */
475     correct_box(NULL, 0, state->box, NULL);
476
477     clear_mat(state->box_rel);
478
479     if (PRESERVE_SHAPE(*ir))
480     {
481         do_box_rel(ir, state->box_rel, state->box, TRUE);
482     }
483 }
484
485 void preserve_box_shape(t_inputrec *ir, matrix box_rel, matrix b)
486 {
487     if (PRESERVE_SHAPE(*ir))
488     {
489         do_box_rel(ir, box_rel, b, FALSE);
490     }
491 }
492
493 real max_cutoff(real cutoff1, real cutoff2)
494 {
495     if (cutoff1 == 0 || cutoff2 == 0)
496     {
497         return 0;
498     }
499     else
500     {
501         return max(cutoff1, cutoff2);
502     }
503 }
504
505 void init_df_history(df_history_t *dfhist, int nlambda)
506 {
507     int i;
508
509     dfhist->nlambda  = nlambda;
510     dfhist->bEquil   = 0;
511     dfhist->wl_delta = 0;
512
513     if (nlambda > 0)
514     {
515         snew(dfhist->sum_weights, dfhist->nlambda);
516         snew(dfhist->sum_dg, dfhist->nlambda);
517         snew(dfhist->sum_minvar, dfhist->nlambda);
518         snew(dfhist->sum_variance, dfhist->nlambda);
519         snew(dfhist->n_at_lam, dfhist->nlambda);
520         snew(dfhist->wl_histo, dfhist->nlambda);
521
522         /* allocate transition matrices here */
523         snew(dfhist->Tij, dfhist->nlambda);
524         snew(dfhist->Tij_empirical, dfhist->nlambda);
525
526         /* allocate accumulators for various transition matrix
527            free energy methods here */
528         snew(dfhist->accum_p, dfhist->nlambda);
529         snew(dfhist->accum_m, dfhist->nlambda);
530         snew(dfhist->accum_p2, dfhist->nlambda);
531         snew(dfhist->accum_m2, dfhist->nlambda);
532
533         for (i = 0; i < dfhist->nlambda; i++)
534         {
535             snew(dfhist->Tij[i], dfhist->nlambda);
536             snew(dfhist->Tij_empirical[i], dfhist->nlambda);
537             snew((dfhist->accum_p)[i], dfhist->nlambda);
538             snew((dfhist->accum_m)[i], dfhist->nlambda);
539             snew((dfhist->accum_p2)[i], dfhist->nlambda);
540             snew((dfhist->accum_m2)[i], dfhist->nlambda);
541         }
542     }
543 }
544
545 extern void copy_df_history(df_history_t *df_dest, df_history_t *df_source)
546 {
547     int i, j;
548
549     /* Currently, there should not be any difference in nlambda between the two,
550        but this is included for completeness for potential later functionality */
551     df_dest->nlambda  = df_source->nlambda;
552     df_dest->bEquil   = df_source->bEquil;
553     df_dest->wl_delta = df_source->wl_delta;
554
555     for (i = 0; i < df_dest->nlambda; i++)
556     {
557         df_dest->sum_weights[i]  = df_source->sum_weights[i];
558         df_dest->sum_dg[i]       = df_source->sum_dg[i];
559         df_dest->sum_minvar[i]   = df_source->sum_minvar[i];
560         df_dest->sum_variance[i] = df_source->sum_variance[i];
561         df_dest->n_at_lam[i]     = df_source->n_at_lam[i];
562         df_dest->wl_histo[i]     = df_source->wl_histo[i];
563     }
564
565     for (i = 0; i < df_dest->nlambda; i++)
566     {
567         for (j = 0; j < df_dest->nlambda; j++)
568         {
569             df_dest->accum_p[i][j]        = df_source->accum_p[i][j];
570             df_dest->accum_m[i][j]        = df_source->accum_m[i][j];
571             df_dest->accum_p2[i][j]       = df_source->accum_p2[i][j];
572             df_dest->accum_m2[i][j]       = df_source->accum_m2[i][j];
573             df_dest->Tij[i][j]            = df_source->Tij[i][j];
574             df_dest->Tij_empirical[i][j]  = df_source->Tij_empirical[i][j];
575         }
576     }
577 }
578
579 void done_df_history(df_history_t *dfhist)
580 {
581     int i;
582
583     if (dfhist->nlambda > 0)
584     {
585         sfree(dfhist->n_at_lam);
586         sfree(dfhist->wl_histo);
587         sfree(dfhist->sum_weights);
588         sfree(dfhist->sum_dg);
589         sfree(dfhist->sum_minvar);
590         sfree(dfhist->sum_variance);
591
592         for (i = 0; i < dfhist->nlambda; i++)
593         {
594             sfree(dfhist->Tij[i]);
595             sfree(dfhist->Tij_empirical[i]);
596             sfree(dfhist->accum_p[i]);
597             sfree(dfhist->accum_m[i]);
598             sfree(dfhist->accum_p2[i]);
599             sfree(dfhist->accum_m2[i]);
600         }
601     }
602     dfhist->bEquil   = 0;
603     dfhist->nlambda  = 0;
604     dfhist->wl_delta = 0;
605 }