Move types/block.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/topology/block.h"
53 #include "gromacs/topology/symtab.h"
54 #include "gromacs/utility/smalloc.h"
55
56 /* The source code in this file should be thread-safe.
57       Please keep it that way. */
58
59
60
61 static gmx_bool            bOverAllocDD     = FALSE;
62 static tMPI_Thread_mutex_t over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
63
64
65 void set_over_alloc_dd(gmx_bool set)
66 {
67     tMPI_Thread_mutex_lock(&over_alloc_mutex);
68     /* we just make sure that we don't set this at the same time.
69        We don't worry too much about reading this rarely-set variable */
70     bOverAllocDD = set;
71     tMPI_Thread_mutex_unlock(&over_alloc_mutex);
72 }
73
74 int over_alloc_dd(int n)
75 {
76     if (bOverAllocDD)
77     {
78         return OVER_ALLOC_FAC*n + 100;
79     }
80     else
81     {
82         return n;
83     }
84 }
85
86 int gmx_int64_to_int(gmx_int64_t step, const char *warn)
87 {
88     int i;
89
90     i = (int)step;
91
92     if (warn != NULL && (step < INT_MIN || step > INT_MAX))
93     {
94         fprintf(stderr, "\nWARNING during %s:\n", warn);
95         fprintf(stderr, "step value ");
96         fprintf(stderr, "%"GMX_PRId64, step);
97         fprintf(stderr, " does not fit in int, converted to %d\n\n", i);
98     }
99
100     return i;
101 }
102
103 char *gmx_step_str(gmx_int64_t i, char *buf)
104 {
105     sprintf(buf, "%"GMX_PRId64, i);
106
107     return buf;
108 }
109
110 void init_atom(t_atoms *at)
111 {
112     int i;
113
114     at->nr        = 0;
115     at->nres      = 0;
116     at->atom      = NULL;
117     at->resinfo   = NULL;
118     at->atomname  = NULL;
119     at->atomtype  = NULL;
120     at->atomtypeB = NULL;
121     at->pdbinfo   = NULL;
122 }
123
124 void init_atomtypes(t_atomtypes *at)
125 {
126     at->nr         = 0;
127     at->radius     = NULL;
128     at->vol        = NULL;
129     at->atomnumber = NULL;
130     at->gb_radius  = NULL;
131     at->S_hct      = NULL;
132 }
133
134 void init_groups(gmx_groups_t *groups)
135 {
136     int g;
137
138     groups->ngrpname = 0;
139     groups->grpname  = NULL;
140     for (g = 0; (g < egcNR); g++)
141     {
142         groups->grps[g].nm_ind = NULL;
143         groups->ngrpnr[g]      = 0;
144         groups->grpnr[g]       = NULL;
145     }
146
147 }
148
149 void init_mtop(gmx_mtop_t *mtop)
150 {
151     mtop->name         = NULL;
152     mtop->nmoltype     = 0;
153     mtop->moltype      = NULL;
154     mtop->nmolblock    = 0;
155     mtop->molblock     = NULL;
156     mtop->maxres_renum = 0;
157     mtop->maxresnr     = -1;
158     init_groups(&mtop->groups);
159     init_block(&mtop->mols);
160     open_symtab(&mtop->symtab);
161 }
162
163 void init_top(t_topology *top)
164 {
165     int i;
166
167     top->name = NULL;
168     init_atom (&(top->atoms));
169     init_atomtypes(&(top->atomtypes));
170     init_block(&top->cgs);
171     init_block(&top->mols);
172     init_blocka(&top->excls);
173     open_symtab(&top->symtab);
174 }
175
176 void init_inputrec(t_inputrec *ir)
177 {
178     memset(ir, 0, (size_t)sizeof(*ir));
179     snew(ir->fepvals, 1);
180     snew(ir->expandedvals, 1);
181     snew(ir->simtempvals, 1);
182 }
183
184 void done_atom (t_atoms *at)
185 {
186     at->nr       = 0;
187     at->nres     = 0;
188     sfree(at->atom);
189     sfree(at->resinfo);
190     sfree(at->atomname);
191     sfree(at->atomtype);
192     sfree(at->atomtypeB);
193     if (at->pdbinfo)
194     {
195         sfree(at->pdbinfo);
196     }
197 }
198
199 void done_atomtypes(t_atomtypes *atype)
200 {
201     atype->nr = 0;
202     sfree(atype->radius);
203     sfree(atype->vol);
204     sfree(atype->surftens);
205     sfree(atype->atomnumber);
206     sfree(atype->gb_radius);
207     sfree(atype->S_hct);
208 }
209
210 void done_moltype(gmx_moltype_t *molt)
211 {
212     int f;
213
214     done_atom(&molt->atoms);
215     done_block(&molt->cgs);
216     done_blocka(&molt->excls);
217
218     for (f = 0; f < F_NRE; f++)
219     {
220         sfree(molt->ilist[f].iatoms);
221         molt->ilist[f].nalloc = 0;
222     }
223 }
224
225 void done_molblock(gmx_molblock_t *molb)
226 {
227     if (molb->nposres_xA > 0)
228     {
229         molb->nposres_xA = 0;
230         sfree(molb->posres_xA);
231     }
232     if (molb->nposres_xB > 0)
233     {
234         molb->nposres_xB = 0;
235         sfree(molb->posres_xB);
236     }
237 }
238
239 void done_mtop(gmx_mtop_t *mtop, gmx_bool bDoneSymtab)
240 {
241     int i;
242
243     if (bDoneSymtab)
244     {
245         done_symtab(&mtop->symtab);
246     }
247
248     sfree(mtop->ffparams.functype);
249     sfree(mtop->ffparams.iparams);
250
251     for (i = 0; i < mtop->nmoltype; i++)
252     {
253         done_moltype(&mtop->moltype[i]);
254     }
255     sfree(mtop->moltype);
256     for (i = 0; i < mtop->nmolblock; i++)
257     {
258         done_molblock(&mtop->molblock[i]);
259     }
260     sfree(mtop->molblock);
261     done_block(&mtop->mols);
262 }
263
264 void done_top(t_topology *top)
265 {
266     int f;
267
268     sfree(top->idef.functype);
269     sfree(top->idef.iparams);
270     for (f = 0; f < F_NRE; ++f)
271     {
272         sfree(top->idef.il[f].iatoms);
273         top->idef.il[f].iatoms = NULL;
274         top->idef.il[f].nalloc = 0;
275     }
276
277     done_atom (&(top->atoms));
278
279     /* For GB */
280     done_atomtypes(&(top->atomtypes));
281
282     done_symtab(&(top->symtab));
283     done_block(&(top->cgs));
284     done_block(&(top->mols));
285     done_blocka(&(top->excls));
286 }
287
288 static void done_pull_group(t_pull_group *pgrp)
289 {
290     if (pgrp->nat > 0)
291     {
292         sfree(pgrp->ind);
293         sfree(pgrp->ind_loc);
294         sfree(pgrp->weight);
295         sfree(pgrp->weight_loc);
296     }
297 }
298
299 static void done_pull(t_pull *pull)
300 {
301     int i;
302
303     for (i = 0; i < pull->ngroup+1; i++)
304     {
305         done_pull_group(pull->group);
306         done_pull_group(pull->dyna);
307     }
308 }
309
310 void done_inputrec(t_inputrec *ir)
311 {
312     int m;
313
314     for (m = 0; (m < DIM); m++)
315     {
316         if (ir->ex[m].a)
317         {
318             sfree(ir->ex[m].a);
319         }
320         if (ir->ex[m].phi)
321         {
322             sfree(ir->ex[m].phi);
323         }
324         if (ir->et[m].a)
325         {
326             sfree(ir->et[m].a);
327         }
328         if (ir->et[m].phi)
329         {
330             sfree(ir->et[m].phi);
331         }
332     }
333
334     sfree(ir->opts.nrdf);
335     sfree(ir->opts.ref_t);
336     sfree(ir->opts.annealing);
337     sfree(ir->opts.anneal_npoints);
338     sfree(ir->opts.anneal_time);
339     sfree(ir->opts.anneal_temp);
340     sfree(ir->opts.tau_t);
341     sfree(ir->opts.acc);
342     sfree(ir->opts.nFreeze);
343     sfree(ir->opts.QMmethod);
344     sfree(ir->opts.QMbasis);
345     sfree(ir->opts.QMcharge);
346     sfree(ir->opts.QMmult);
347     sfree(ir->opts.bSH);
348     sfree(ir->opts.CASorbitals);
349     sfree(ir->opts.CASelectrons);
350     sfree(ir->opts.SAon);
351     sfree(ir->opts.SAoff);
352     sfree(ir->opts.SAsteps);
353     sfree(ir->opts.bOPT);
354     sfree(ir->opts.bTS);
355
356     if (ir->pull)
357     {
358         done_pull(ir->pull);
359         sfree(ir->pull);
360     }
361 }
362
363 static void zero_history(history_t *hist)
364 {
365     hist->disre_initf  = 0;
366     hist->ndisrepairs  = 0;
367     hist->disre_rm3tav = NULL;
368     hist->orire_initf  = 0;
369     hist->norire_Dtav  = 0;
370     hist->orire_Dtav   = NULL;
371 }
372
373 static void zero_ekinstate(ekinstate_t *eks)
374 {
375     eks->ekin_n         = 0;
376     eks->ekinh          = NULL;
377     eks->ekinf          = NULL;
378     eks->ekinh_old      = NULL;
379     eks->ekinscalef_nhc = NULL;
380     eks->ekinscaleh_nhc = NULL;
381     eks->vscale_nhc     = NULL;
382     eks->dekindl        = 0;
383     eks->mvcos          = 0;
384 }
385
386 static void init_swapstate(swapstate_t *swapstate)
387 {
388     int ii, ic;
389
390     swapstate->eSwapCoords = 0;
391     swapstate->nAverage    = 0;
392
393     /* Ion/water position swapping */
394     for (ic = 0; ic < eCompNR; ic++)
395     {
396         for (ii = 0; ii < eIonNR; ii++)
397         {
398             swapstate->nat_req[ic][ii]        = 0;
399             swapstate->nat_req_p[ic][ii]      = NULL;
400             swapstate->inflow_netto[ic][ii]   = 0;
401             swapstate->inflow_netto_p[ic][ii] = NULL;
402             swapstate->nat_past[ic][ii]       = NULL;
403             swapstate->nat_past_p[ic][ii]     = NULL;
404             swapstate->fluxfromAtoB[ic][ii]   = 0;
405             swapstate->fluxfromAtoB_p[ic][ii] = NULL;
406         }
407     }
408     swapstate->fluxleak               = NULL;
409     swapstate->nions                  = 0;
410     swapstate->comp_from              = NULL;
411     swapstate->channel_label          = NULL;
412     swapstate->bFromCpt               = 0;
413     swapstate->nat[eChan0]            = 0;
414     swapstate->nat[eChan1]            = 0;
415     swapstate->xc_old_whole[eChan0]   = NULL;
416     swapstate->xc_old_whole[eChan1]   = NULL;
417     swapstate->xc_old_whole_p[eChan0] = NULL;
418     swapstate->xc_old_whole_p[eChan1] = NULL;
419 }
420
421 void init_energyhistory(energyhistory_t * enerhist)
422 {
423     enerhist->nener = 0;
424
425     enerhist->ener_ave     = NULL;
426     enerhist->ener_sum     = NULL;
427     enerhist->ener_sum_sim = NULL;
428     enerhist->dht          = NULL;
429
430     enerhist->nsteps     = 0;
431     enerhist->nsum       = 0;
432     enerhist->nsteps_sim = 0;
433     enerhist->nsum_sim   = 0;
434
435     enerhist->dht = NULL;
436 }
437
438 static void done_delta_h_history(delta_h_history_t *dht)
439 {
440     int i;
441
442     for (i = 0; i < dht->nndh; i++)
443     {
444         sfree(dht->dh[i]);
445     }
446     sfree(dht->dh);
447     sfree(dht->ndh);
448 }
449
450 void done_energyhistory(energyhistory_t * enerhist)
451 {
452     sfree(enerhist->ener_ave);
453     sfree(enerhist->ener_sum);
454     sfree(enerhist->ener_sum_sim);
455
456     if (enerhist->dht != NULL)
457     {
458         done_delta_h_history(enerhist->dht);
459         sfree(enerhist->dht);
460     }
461 }
462
463 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
464 {
465     int i, j;
466
467     state->ngtc          = ngtc;
468     state->nnhpres       = nnhpres;
469     state->nhchainlength = nhchainlength;
470     if (state->ngtc > 0)
471     {
472         snew(state->nosehoover_xi, state->nhchainlength*state->ngtc);
473         snew(state->nosehoover_vxi, state->nhchainlength*state->ngtc);
474         snew(state->therm_integral, state->ngtc);
475         for (i = 0; i < state->ngtc; i++)
476         {
477             for (j = 0; j < state->nhchainlength; j++)
478             {
479                 state->nosehoover_xi[i*state->nhchainlength + j]   = 0.0;
480                 state->nosehoover_vxi[i*state->nhchainlength + j]  = 0.0;
481             }
482         }
483         for (i = 0; i < state->ngtc; i++)
484         {
485             state->therm_integral[i]  = 0.0;
486         }
487     }
488     else
489     {
490         state->nosehoover_xi  = NULL;
491         state->nosehoover_vxi = NULL;
492         state->therm_integral = NULL;
493     }
494
495     if (state->nnhpres > 0)
496     {
497         snew(state->nhpres_xi, state->nhchainlength*nnhpres);
498         snew(state->nhpres_vxi, state->nhchainlength*nnhpres);
499         for (i = 0; i < nnhpres; i++)
500         {
501             for (j = 0; j < state->nhchainlength; j++)
502             {
503                 state->nhpres_xi[i*nhchainlength + j]   = 0.0;
504                 state->nhpres_vxi[i*nhchainlength + j]  = 0.0;
505             }
506         }
507     }
508     else
509     {
510         state->nhpres_xi  = NULL;
511         state->nhpres_vxi = NULL;
512     }
513 }
514
515
516 void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int nlambda)
517 {
518     int i;
519
520     state->natoms = natoms;
521     state->flags  = 0;
522     state->lambda = 0;
523     snew(state->lambda, efptNR);
524     for (i = 0; i < efptNR; i++)
525     {
526         state->lambda[i] = 0;
527     }
528     state->veta   = 0;
529     clear_mat(state->box);
530     clear_mat(state->box_rel);
531     clear_mat(state->boxv);
532     clear_mat(state->pres_prev);
533     clear_mat(state->svir_prev);
534     clear_mat(state->fvir_prev);
535     init_gtc_state(state, ngtc, nnhpres, nhchainlength);
536     state->nalloc = state->natoms;
537     if (state->nalloc > 0)
538     {
539         snew(state->x, state->nalloc);
540         snew(state->v, state->nalloc);
541     }
542     else
543     {
544         state->x = NULL;
545         state->v = NULL;
546     }
547     state->sd_X = NULL;
548     state->cg_p = NULL;
549     zero_history(&state->hist);
550     zero_ekinstate(&state->ekinstate);
551     init_energyhistory(&state->enerhist);
552     init_df_history(&state->dfhist, nlambda);
553     init_swapstate(&state->swapstate);
554     state->ddp_count       = 0;
555     state->ddp_count_cg_gl = 0;
556     state->cg_gl           = NULL;
557     state->cg_gl_nalloc    = 0;
558 }
559
560 void done_state(t_state *state)
561 {
562     if (state->x)
563     {
564         sfree(state->x);
565     }
566     if (state->v)
567     {
568         sfree(state->v);
569     }
570     if (state->sd_X)
571     {
572         sfree(state->sd_X);
573     }
574     if (state->cg_p)
575     {
576         sfree(state->cg_p);
577     }
578     state->nalloc = 0;
579     if (state->cg_gl)
580     {
581         sfree(state->cg_gl);
582     }
583     state->cg_gl_nalloc = 0;
584     if (state->lambda)
585     {
586         sfree(state->lambda);
587     }
588     if (state->ngtc > 0)
589     {
590         sfree(state->nosehoover_xi);
591         sfree(state->nosehoover_vxi);
592         sfree(state->therm_integral);
593     }
594 }
595
596 t_state *serial_init_local_state(t_state *state_global)
597 {
598     int      i;
599     t_state *state_local;
600
601     snew(state_local, 1);
602
603     /* Copy all the contents */
604     *state_local = *state_global;
605     snew(state_local->lambda, efptNR);
606     /* local storage for lambda */
607     for (i = 0; i < efptNR; i++)
608     {
609         state_local->lambda[i] = state_global->lambda[i];
610     }
611
612     return state_local;
613 }
614
615 static void do_box_rel(t_inputrec *ir, matrix box_rel, matrix b, gmx_bool bInit)
616 {
617     int d, d2;
618
619     for (d = YY; d <= ZZ; d++)
620     {
621         for (d2 = XX; d2 <= (ir->epct == epctSEMIISOTROPIC ? YY : ZZ); d2++)
622         {
623             /* We need to check if this box component is deformed
624              * or if deformation of another component might cause
625              * changes in this component due to box corrections.
626              */
627             if (ir->deform[d][d2] == 0 &&
628                 !(d == ZZ && d2 == XX && ir->deform[d][YY] != 0 &&
629                   (b[YY][d2] != 0 || ir->deform[YY][d2] != 0)))
630             {
631                 if (bInit)
632                 {
633                     box_rel[d][d2] = b[d][d2]/b[XX][XX];
634                 }
635                 else
636                 {
637                     b[d][d2] = b[XX][XX]*box_rel[d][d2];
638                 }
639             }
640         }
641     }
642 }
643
644 void set_box_rel(t_inputrec *ir, t_state *state)
645 {
646     /* Make sure the box obeys the restrictions before we fix the ratios */
647     correct_box(NULL, 0, state->box, NULL);
648
649     clear_mat(state->box_rel);
650
651     if (PRESERVE_SHAPE(*ir))
652     {
653         do_box_rel(ir, state->box_rel, state->box, TRUE);
654     }
655 }
656
657 void preserve_box_shape(t_inputrec *ir, matrix box_rel, matrix b)
658 {
659     if (PRESERVE_SHAPE(*ir))
660     {
661         do_box_rel(ir, box_rel, b, FALSE);
662     }
663 }
664
665 void add_t_atoms(t_atoms *atoms, int natom_extra, int nres_extra)
666 {
667     int i;
668
669     if (natom_extra > 0)
670     {
671         srenew(atoms->atomname, atoms->nr+natom_extra);
672         srenew(atoms->atom, atoms->nr+natom_extra);
673         if (NULL != atoms->pdbinfo)
674         {
675             srenew(atoms->pdbinfo, atoms->nr+natom_extra);
676         }
677         if (NULL != atoms->atomtype)
678         {
679             srenew(atoms->atomtype, atoms->nr+natom_extra);
680         }
681         if (NULL != atoms->atomtypeB)
682         {
683             srenew(atoms->atomtypeB, atoms->nr+natom_extra);
684         }
685         for (i = atoms->nr; (i < atoms->nr+natom_extra); i++)
686         {
687             atoms->atomname[i] = NULL;
688             memset(&atoms->atom[i], 0, sizeof(atoms->atom[i]));
689             if (NULL != atoms->pdbinfo)
690             {
691                 memset(&atoms->pdbinfo[i], 0, sizeof(atoms->pdbinfo[i]));
692             }
693             if (NULL != atoms->atomtype)
694             {
695                 atoms->atomtype[i] = NULL;
696             }
697             if (NULL != atoms->atomtypeB)
698             {
699                 atoms->atomtypeB[i] = NULL;
700             }
701         }
702         atoms->nr += natom_extra;
703     }
704     if (nres_extra > 0)
705     {
706         srenew(atoms->resinfo, atoms->nres+nres_extra);
707         for (i = atoms->nres; (i < atoms->nres+nres_extra); i++)
708         {
709             memset(&atoms->resinfo[i], 0, sizeof(atoms->resinfo[i]));
710         }
711         atoms->nres += nres_extra;
712     }
713 }
714
715 void init_t_atoms(t_atoms *atoms, int natoms, gmx_bool bPdbinfo)
716 {
717     atoms->nr   = natoms;
718     atoms->nres = 0;
719     snew(atoms->atomname, natoms);
720     atoms->atomtype  = NULL;
721     atoms->atomtypeB = NULL;
722     snew(atoms->resinfo, natoms);
723     snew(atoms->atom, natoms);
724     if (bPdbinfo)
725     {
726         snew(atoms->pdbinfo, natoms);
727     }
728     else
729     {
730         atoms->pdbinfo = NULL;
731     }
732 }
733
734 t_atoms *copy_t_atoms(t_atoms *src)
735 {
736     t_atoms *dst;
737     int      i;
738
739     snew(dst, 1);
740     init_t_atoms(dst, src->nr, (NULL != src->pdbinfo));
741     dst->nr = src->nr;
742     if (NULL != src->atomname)
743     {
744         snew(dst->atomname, src->nr);
745     }
746     if (NULL != src->atomtype)
747     {
748         snew(dst->atomtype, src->nr);
749     }
750     if (NULL != src->atomtypeB)
751     {
752         snew(dst->atomtypeB, src->nr);
753     }
754     for (i = 0; (i < src->nr); i++)
755     {
756         dst->atom[i] = src->atom[i];
757         if (NULL != src->pdbinfo)
758         {
759             dst->pdbinfo[i] = src->pdbinfo[i];
760         }
761         if (NULL != src->atomname)
762         {
763             dst->atomname[i]  = src->atomname[i];
764         }
765         if (NULL != src->atomtype)
766         {
767             dst->atomtype[i] = src->atomtype[i];
768         }
769         if (NULL != src->atomtypeB)
770         {
771             dst->atomtypeB[i] = src->atomtypeB[i];
772         }
773     }
774     dst->nres = src->nres;
775     for (i = 0; (i < src->nres); i++)
776     {
777         dst->resinfo[i] = src->resinfo[i];
778     }
779     return dst;
780 }
781
782 void t_atoms_set_resinfo(t_atoms *atoms, int atom_ind, t_symtab *symtab,
783                          const char *resname, int resnr, unsigned char ic,
784                          int chainnum, char chainid)
785 {
786     t_resinfo *ri;
787
788     ri           = &atoms->resinfo[atoms->atom[atom_ind].resind];
789     ri->name     = put_symtab(symtab, resname);
790     ri->rtp      = NULL;
791     ri->nr       = resnr;
792     ri->ic       = ic;
793     ri->chainnum = chainnum;
794     ri->chainid  = chainid;
795 }
796
797 void free_t_atoms(t_atoms *atoms, gmx_bool bFreeNames)
798 {
799     int i;
800
801     if (bFreeNames && atoms->atomname != NULL)
802     {
803         for (i = 0; i < atoms->nr; i++)
804         {
805             if (atoms->atomname[i] != NULL)
806             {
807                 sfree(*atoms->atomname[i]);
808                 *atoms->atomname[i] = NULL;
809             }
810         }
811     }
812     if (bFreeNames && atoms->resinfo != NULL)
813     {
814         for (i = 0; i < atoms->nres; i++)
815         {
816             if (atoms->resinfo[i].name != NULL)
817             {
818                 sfree(*atoms->resinfo[i].name);
819                 *atoms->resinfo[i].name = NULL;
820             }
821         }
822     }
823     if (bFreeNames && atoms->atomtype != NULL)
824     {
825         for (i = 0; i < atoms->nr; i++)
826         {
827             if (atoms->atomtype[i] != NULL)
828             {
829                 sfree(*atoms->atomtype[i]);
830                 *atoms->atomtype[i] = NULL;
831             }
832         }
833     }
834     if (bFreeNames && atoms->atomtypeB != NULL)
835     {
836         for (i = 0; i < atoms->nr; i++)
837         {
838             if (atoms->atomtypeB[i] != NULL)
839             {
840                 sfree(*atoms->atomtypeB[i]);
841                 *atoms->atomtypeB[i] = NULL;
842             }
843         }
844     }
845     sfree(atoms->atomname);
846     sfree(atoms->atomtype);
847     sfree(atoms->atomtypeB);
848     sfree(atoms->resinfo);
849     sfree(atoms->atom);
850     sfree(atoms->pdbinfo);
851     atoms->nr        = 0;
852     atoms->nres      = 0;
853     atoms->atomname  = NULL;
854     atoms->atomtype  = NULL;
855     atoms->atomtypeB = NULL;
856     atoms->resinfo   = NULL;
857     atoms->atom      = NULL;
858     atoms->pdbinfo   = NULL;
859 }
860
861 real max_cutoff(real cutoff1, real cutoff2)
862 {
863     if (cutoff1 == 0 || cutoff2 == 0)
864     {
865         return 0;
866     }
867     else
868     {
869         return max(cutoff1, cutoff2);
870     }
871 }
872
873 void init_df_history(df_history_t *dfhist, int nlambda)
874 {
875     int i;
876
877     dfhist->nlambda  = nlambda;
878     dfhist->bEquil   = 0;
879     dfhist->wl_delta = 0;
880
881     if (nlambda > 0)
882     {
883         snew(dfhist->sum_weights, dfhist->nlambda);
884         snew(dfhist->sum_dg, dfhist->nlambda);
885         snew(dfhist->sum_minvar, dfhist->nlambda);
886         snew(dfhist->sum_variance, dfhist->nlambda);
887         snew(dfhist->n_at_lam, dfhist->nlambda);
888         snew(dfhist->wl_histo, dfhist->nlambda);
889
890         /* allocate transition matrices here */
891         snew(dfhist->Tij, dfhist->nlambda);
892         snew(dfhist->Tij_empirical, dfhist->nlambda);
893
894         /* allocate accumulators for various transition matrix
895            free energy methods here */
896         snew(dfhist->accum_p, dfhist->nlambda);
897         snew(dfhist->accum_m, dfhist->nlambda);
898         snew(dfhist->accum_p2, dfhist->nlambda);
899         snew(dfhist->accum_m2, dfhist->nlambda);
900
901         for (i = 0; i < dfhist->nlambda; i++)
902         {
903             snew(dfhist->Tij[i], dfhist->nlambda);
904             snew(dfhist->Tij_empirical[i], dfhist->nlambda);
905             snew((dfhist->accum_p)[i], dfhist->nlambda);
906             snew((dfhist->accum_m)[i], dfhist->nlambda);
907             snew((dfhist->accum_p2)[i], dfhist->nlambda);
908             snew((dfhist->accum_m2)[i], dfhist->nlambda);
909         }
910     }
911 }
912
913 extern void copy_df_history(df_history_t *df_dest, df_history_t *df_source)
914 {
915     int i, j;
916
917     /* Currently, there should not be any difference in nlambda between the two,
918        but this is included for completeness for potential later functionality */
919     df_dest->nlambda  = df_source->nlambda;
920     df_dest->bEquil   = df_source->bEquil;
921     df_dest->wl_delta = df_source->wl_delta;
922
923     for (i = 0; i < df_dest->nlambda; i++)
924     {
925         df_dest->sum_weights[i]  = df_source->sum_weights[i];
926         df_dest->sum_dg[i]       = df_source->sum_dg[i];
927         df_dest->sum_minvar[i]   = df_source->sum_minvar[i];
928         df_dest->sum_variance[i] = df_source->sum_variance[i];
929         df_dest->n_at_lam[i]     = df_source->n_at_lam[i];
930         df_dest->wl_histo[i]     = df_source->wl_histo[i];
931     }
932
933     for (i = 0; i < df_dest->nlambda; i++)
934     {
935         for (j = 0; j < df_dest->nlambda; j++)
936         {
937             df_dest->accum_p[i][j]        = df_source->accum_p[i][j];
938             df_dest->accum_m[i][j]        = df_source->accum_m[i][j];
939             df_dest->accum_p2[i][j]       = df_source->accum_p2[i][j];
940             df_dest->accum_m2[i][j]       = df_source->accum_m2[i][j];
941             df_dest->Tij[i][j]            = df_source->Tij[i][j];
942             df_dest->Tij_empirical[i][j]  = df_source->Tij_empirical[i][j];
943         }
944     }
945 }
946
947 void done_df_history(df_history_t *dfhist)
948 {
949     int i;
950
951     if (dfhist->nlambda > 0)
952     {
953         sfree(dfhist->n_at_lam);
954         sfree(dfhist->wl_histo);
955         sfree(dfhist->sum_weights);
956         sfree(dfhist->sum_dg);
957         sfree(dfhist->sum_minvar);
958         sfree(dfhist->sum_variance);
959
960         for (i = 0; i < dfhist->nlambda; i++)
961         {
962             sfree(dfhist->Tij[i]);
963             sfree(dfhist->Tij_empirical[i]);
964             sfree(dfhist->accum_p[i]);
965             sfree(dfhist->accum_m[i]);
966             sfree(dfhist->accum_p2[i]);
967             sfree(dfhist->accum_m2[i]);
968         }
969     }
970     dfhist->bEquil   = 0;
971     dfhist->nlambda  = 0;
972     dfhist->wl_delta = 0;
973 }