1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
36 /* This file is completely threadsafe - keep it that way! */
49 #include "thread_mpi.h"
52 /* The source code in this file should be thread-safe.
53 Please keep it that way. */
57 static gmx_bool bOverAllocDD = FALSE;
59 static tMPI_Thread_mutex_t over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
63 void set_over_alloc_dd(gmx_bool set)
66 tMPI_Thread_mutex_lock(&over_alloc_mutex);
67 /* we just make sure that we don't set this at the same time.
68 We don't worry too much about reading this rarely-set variable */
72 tMPI_Thread_mutex_unlock(&over_alloc_mutex);
76 int over_alloc_dd(int n)
80 return OVER_ALLOC_FAC*n + 100;
88 int gmx_large_int_to_int(gmx_large_int_t step, const char *warn)
94 if (warn != NULL && (step < INT_MIN || step > INT_MAX))
96 fprintf(stderr, "\nWARNING during %s:\n", warn);
97 fprintf(stderr, "step value ");
98 fprintf(stderr, gmx_large_int_pfmt, step);
99 fprintf(stderr, " does not fit in int, converted to %d\n\n", i);
105 char *gmx_step_str(gmx_large_int_t i, char *buf)
107 sprintf(buf, gmx_large_int_pfmt, i);
112 void init_block(t_block *block)
117 block->nalloc_index = 1;
118 snew(block->index, block->nalloc_index);
122 void init_blocka(t_blocka *block)
128 block->nalloc_index = 1;
129 snew(block->index, block->nalloc_index);
135 void init_atom(t_atoms *at)
145 at->atomtypeB = NULL;
149 void init_atomtypes(t_atomtypes *at)
154 at->atomnumber = NULL;
155 at->gb_radius = NULL;
159 void init_groups(gmx_groups_t *groups)
163 groups->ngrpname = 0;
164 groups->grpname = NULL;
165 for (g = 0; (g < egcNR); g++)
167 groups->grps[g].nm_ind = NULL;
168 groups->ngrpnr[g] = 0;
169 groups->grpnr[g] = NULL;
174 void init_mtop(gmx_mtop_t *mtop)
178 mtop->moltype = NULL;
180 mtop->molblock = NULL;
181 mtop->maxres_renum = 0;
183 init_groups(&mtop->groups);
184 init_block(&mtop->mols);
185 open_symtab(&mtop->symtab);
188 void init_top (t_topology *top)
193 init_atom (&(top->atoms));
194 init_atomtypes(&(top->atomtypes));
195 init_block(&top->cgs);
196 init_block(&top->mols);
197 init_blocka(&top->excls);
198 open_symtab(&top->symtab);
201 void init_inputrec(t_inputrec *ir)
203 memset(ir, 0, (size_t)sizeof(*ir));
204 snew(ir->fepvals, 1);
205 snew(ir->expandedvals, 1);
206 snew(ir->simtempvals, 1);
209 void stupid_fill_block(t_block *grp, int natom, gmx_bool bOneIndexGroup)
215 grp->nalloc_index = 2;
216 snew(grp->index, grp->nalloc_index);
218 grp->index[1] = natom;
223 grp->nalloc_index = natom+1;
224 snew(grp->index, grp->nalloc_index);
225 snew(grp->index, natom+1);
226 for (i = 0; (i <= natom); i++)
234 void stupid_fill_blocka(t_blocka *grp, int natom)
238 grp->nalloc_a = natom;
239 snew(grp->a, grp->nalloc_a);
240 for (i = 0; (i < natom); i++)
246 grp->nalloc_index = natom + 1;
247 snew(grp->index, grp->nalloc_index);
248 for (i = 0; (i <= natom); i++)
255 void copy_blocka(const t_blocka *src, t_blocka *dest)
260 dest->nalloc_index = dest->nr + 1;
261 snew(dest->index, dest->nalloc_index);
262 for (i = 0; i < dest->nr+1; i++)
264 dest->index[i] = src->index[i];
266 dest->nra = src->nra;
267 dest->nalloc_a = dest->nra + 1;
268 snew(dest->a, dest->nalloc_a);
269 for (i = 0; i < dest->nra+1; i++)
271 dest->a[i] = src->a[i];
275 void done_block(t_block *block)
279 block->nalloc_index = 0;
282 void done_blocka(t_blocka *block)
290 block->nalloc_index = 0;
294 void done_atom (t_atoms *at)
302 sfree(at->atomtypeB);
309 void done_atomtypes(t_atomtypes *atype)
312 sfree(atype->radius);
314 sfree(atype->surftens);
315 sfree(atype->atomnumber);
316 sfree(atype->gb_radius);
320 void done_moltype(gmx_moltype_t *molt)
324 done_atom(&molt->atoms);
325 done_block(&molt->cgs);
326 done_blocka(&molt->excls);
328 for (f = 0; f < F_NRE; f++)
330 sfree(molt->ilist[f].iatoms);
331 molt->ilist[f].nalloc = 0;
335 void done_molblock(gmx_molblock_t *molb)
337 if (molb->nposres_xA > 0)
339 molb->nposres_xA = 0;
340 free(molb->posres_xA);
342 if (molb->nposres_xB > 0)
344 molb->nposres_xB = 0;
345 free(molb->posres_xB);
349 void done_mtop(gmx_mtop_t *mtop, gmx_bool bDoneSymtab)
355 done_symtab(&mtop->symtab);
358 sfree(mtop->ffparams.functype);
359 sfree(mtop->ffparams.iparams);
361 for (i = 0; i < mtop->nmoltype; i++)
363 done_moltype(&mtop->moltype[i]);
365 sfree(mtop->moltype);
366 for (i = 0; i < mtop->nmolblock; i++)
368 done_molblock(&mtop->molblock[i]);
370 sfree(mtop->molblock);
371 done_block(&mtop->mols);
374 void done_top(t_topology *top)
378 sfree(top->idef.functype);
379 sfree(top->idef.iparams);
380 for (f = 0; f < F_NRE; ++f)
382 sfree(top->idef.il[f].iatoms);
383 top->idef.il[f].iatoms = NULL;
384 top->idef.il[f].nalloc = 0;
387 done_atom (&(top->atoms));
390 done_atomtypes(&(top->atomtypes));
392 done_symtab(&(top->symtab));
393 done_block(&(top->cgs));
394 done_block(&(top->mols));
395 done_blocka(&(top->excls));
398 static void done_pullgrp(t_pullgrp *pgrp)
401 sfree(pgrp->ind_loc);
403 sfree(pgrp->weight_loc);
406 static void done_pull(t_pull *pull)
410 for (i = 0; i < pull->ngrp+1; i++)
412 done_pullgrp(pull->grp);
413 done_pullgrp(pull->dyna);
417 void done_inputrec(t_inputrec *ir)
421 for (m = 0; (m < DIM); m++)
429 sfree(ir->ex[m].phi);
437 sfree(ir->et[m].phi);
441 sfree(ir->opts.nrdf);
442 sfree(ir->opts.ref_t);
443 sfree(ir->opts.annealing);
444 sfree(ir->opts.anneal_npoints);
445 sfree(ir->opts.anneal_time);
446 sfree(ir->opts.anneal_temp);
447 sfree(ir->opts.tau_t);
449 sfree(ir->opts.nFreeze);
450 sfree(ir->opts.QMmethod);
451 sfree(ir->opts.QMbasis);
452 sfree(ir->opts.QMcharge);
453 sfree(ir->opts.QMmult);
455 sfree(ir->opts.CASorbitals);
456 sfree(ir->opts.CASelectrons);
457 sfree(ir->opts.SAon);
458 sfree(ir->opts.SAoff);
459 sfree(ir->opts.SAsteps);
460 sfree(ir->opts.bOPT);
470 static void zero_history(history_t *hist)
472 hist->disre_initf = 0;
473 hist->ndisrepairs = 0;
474 hist->disre_rm3tav = NULL;
475 hist->orire_initf = 0;
476 hist->norire_Dtav = 0;
477 hist->orire_Dtav = NULL;
480 static void zero_ekinstate(ekinstate_t *eks)
485 eks->ekinh_old = NULL;
486 eks->ekinscalef_nhc = NULL;
487 eks->ekinscaleh_nhc = NULL;
488 eks->vscale_nhc = NULL;
493 void init_energyhistory(energyhistory_t * enerhist)
497 enerhist->ener_ave = NULL;
498 enerhist->ener_sum = NULL;
499 enerhist->ener_sum_sim = NULL;
500 enerhist->dht = NULL;
502 enerhist->nsteps = 0;
504 enerhist->nsteps_sim = 0;
505 enerhist->nsum_sim = 0;
507 enerhist->dht = NULL;
510 static void done_delta_h_history(delta_h_history_t *dht)
514 for (i = 0; i < dht->nndh; i++)
522 void done_energyhistory(energyhistory_t * enerhist)
524 sfree(enerhist->ener_ave);
525 sfree(enerhist->ener_sum);
526 sfree(enerhist->ener_sum_sim);
528 if (enerhist->dht != NULL)
530 done_delta_h_history(enerhist->dht);
531 sfree(enerhist->dht);
535 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
540 state->nnhpres = nnhpres;
541 state->nhchainlength = nhchainlength;
544 snew(state->nosehoover_xi, state->nhchainlength*state->ngtc);
545 snew(state->nosehoover_vxi, state->nhchainlength*state->ngtc);
546 snew(state->therm_integral, state->ngtc);
547 for (i = 0; i < state->ngtc; i++)
549 for (j = 0; j < state->nhchainlength; j++)
551 state->nosehoover_xi[i*state->nhchainlength + j] = 0.0;
552 state->nosehoover_vxi[i*state->nhchainlength + j] = 0.0;
555 for (i = 0; i < state->ngtc; i++)
557 state->therm_integral[i] = 0.0;
562 state->nosehoover_xi = NULL;
563 state->nosehoover_vxi = NULL;
564 state->therm_integral = NULL;
567 if (state->nnhpres > 0)
569 snew(state->nhpres_xi, state->nhchainlength*nnhpres);
570 snew(state->nhpres_vxi, state->nhchainlength*nnhpres);
571 for (i = 0; i < nnhpres; i++)
573 for (j = 0; j < state->nhchainlength; j++)
575 state->nhpres_xi[i*nhchainlength + j] = 0.0;
576 state->nhpres_vxi[i*nhchainlength + j] = 0.0;
582 state->nhpres_xi = NULL;
583 state->nhpres_vxi = NULL;
588 void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int nlambda)
592 state->natoms = natoms;
596 snew(state->lambda, efptNR);
597 for (i = 0; i < efptNR; i++)
599 state->lambda[i] = 0;
602 clear_mat(state->box);
603 clear_mat(state->box_rel);
604 clear_mat(state->boxv);
605 clear_mat(state->pres_prev);
606 clear_mat(state->svir_prev);
607 clear_mat(state->fvir_prev);
608 init_gtc_state(state, ngtc, nnhpres, nhchainlength);
609 state->nalloc = state->natoms;
610 if (state->nalloc > 0)
612 snew(state->x, state->nalloc);
613 snew(state->v, state->nalloc);
622 zero_history(&state->hist);
623 zero_ekinstate(&state->ekinstate);
624 init_energyhistory(&state->enerhist);
625 init_df_history(&state->dfhist,nlambda);
626 state->ddp_count = 0;
627 state->ddp_count_cg_gl = 0;
629 state->cg_gl_nalloc = 0;
632 void done_state(t_state *state)
655 state->cg_gl_nalloc = 0;
658 sfree(state->lambda);
662 sfree(state->nosehoover_xi);
663 sfree(state->nosehoover_vxi);
664 sfree(state->therm_integral);
668 static void do_box_rel(t_inputrec *ir, matrix box_rel, matrix b, gmx_bool bInit)
672 for (d = YY; d <= ZZ; d++)
674 for (d2 = XX; d2 <= (ir->epct == epctSEMIISOTROPIC ? YY : ZZ); d2++)
676 /* We need to check if this box component is deformed
677 * or if deformation of another component might cause
678 * changes in this component due to box corrections.
680 if (ir->deform[d][d2] == 0 &&
681 !(d == ZZ && d2 == XX && ir->deform[d][YY] != 0 &&
682 (b[YY][d2] != 0 || ir->deform[YY][d2] != 0)))
686 box_rel[d][d2] = b[d][d2]/b[XX][XX];
690 b[d][d2] = b[XX][XX]*box_rel[d][d2];
697 void set_box_rel(t_inputrec *ir, t_state *state)
699 /* Make sure the box obeys the restrictions before we fix the ratios */
700 correct_box(NULL, 0, state->box, NULL);
702 clear_mat(state->box_rel);
704 if (PRESERVE_SHAPE(*ir))
706 do_box_rel(ir, state->box_rel, state->box, TRUE);
710 void preserve_box_shape(t_inputrec *ir, matrix box_rel, matrix b)
712 if (PRESERVE_SHAPE(*ir))
714 do_box_rel(ir, box_rel, b, FALSE);
718 void add_t_atoms(t_atoms *atoms, int natom_extra, int nres_extra)
724 srenew(atoms->atomname, atoms->nr+natom_extra);
725 srenew(atoms->atom, atoms->nr+natom_extra);
726 if (NULL != atoms->pdbinfo)
728 srenew(atoms->pdbinfo, atoms->nr+natom_extra);
730 if (NULL != atoms->atomtype)
732 srenew(atoms->atomtype, atoms->nr+natom_extra);
734 if (NULL != atoms->atomtypeB)
736 srenew(atoms->atomtypeB, atoms->nr+natom_extra);
738 for (i = atoms->nr; (i < atoms->nr+natom_extra); i++)
740 atoms->atomname[i] = NULL;
741 memset(&atoms->atom[i], 0, sizeof(atoms->atom[i]));
742 if (NULL != atoms->pdbinfo)
744 memset(&atoms->pdbinfo[i], 0, sizeof(atoms->pdbinfo[i]));
746 if (NULL != atoms->atomtype)
748 atoms->atomtype[i] = NULL;
750 if (NULL != atoms->atomtypeB)
752 atoms->atomtypeB[i] = NULL;
755 atoms->nr += natom_extra;
759 srenew(atoms->resinfo, atoms->nres+nres_extra);
760 for (i = atoms->nres; (i < atoms->nres+nres_extra); i++)
762 memset(&atoms->resinfo[i], 0, sizeof(atoms->resinfo[i]));
764 atoms->nres += nres_extra;
768 void init_t_atoms(t_atoms *atoms, int natoms, gmx_bool bPdbinfo)
772 snew(atoms->atomname, natoms);
773 atoms->atomtype = NULL;
774 atoms->atomtypeB = NULL;
775 snew(atoms->resinfo, natoms);
776 snew(atoms->atom, natoms);
779 snew(atoms->pdbinfo, natoms);
783 atoms->pdbinfo = NULL;
787 t_atoms *copy_t_atoms(t_atoms *src)
793 init_t_atoms(dst, src->nr, (NULL != src->pdbinfo));
795 if (NULL != src->atomname)
797 snew(dst->atomname, src->nr);
799 if (NULL != src->atomtype)
801 snew(dst->atomtype, src->nr);
803 if (NULL != src->atomtypeB)
805 snew(dst->atomtypeB, src->nr);
807 for (i = 0; (i < src->nr); i++)
809 dst->atom[i] = src->atom[i];
810 if (NULL != src->pdbinfo)
812 dst->pdbinfo[i] = src->pdbinfo[i];
814 if (NULL != src->atomname)
816 dst->atomname[i] = src->atomname[i];
818 if (NULL != src->atomtype)
820 dst->atomtype[i] = src->atomtype[i];
822 if (NULL != src->atomtypeB)
824 dst->atomtypeB[i] = src->atomtypeB[i];
827 dst->nres = src->nres;
828 for (i = 0; (i < src->nres); i++)
830 dst->resinfo[i] = src->resinfo[i];
835 void t_atoms_set_resinfo(t_atoms *atoms, int atom_ind, t_symtab *symtab,
836 const char *resname, int resnr, unsigned char ic,
837 int chainnum, char chainid)
841 ri = &atoms->resinfo[atoms->atom[atom_ind].resind];
842 ri->name = put_symtab(symtab, resname);
846 ri->chainnum = chainnum;
847 ri->chainid = chainid;
850 void free_t_atoms(t_atoms *atoms, gmx_bool bFreeNames)
854 if (bFreeNames && atoms->atomname != NULL)
856 for (i = 0; i < atoms->nr; i++)
858 if (atoms->atomname[i] != NULL)
860 sfree(*atoms->atomname[i]);
861 *atoms->atomname[i] = NULL;
865 if (bFreeNames && atoms->resinfo != NULL)
867 for (i = 0; i < atoms->nres; i++)
869 if (atoms->resinfo[i].name != NULL)
871 sfree(*atoms->resinfo[i].name);
872 *atoms->resinfo[i].name = NULL;
876 if (bFreeNames && atoms->atomtype != NULL)
878 for (i = 0; i < atoms->nr; i++)
880 if (atoms->atomtype[i] != NULL)
882 sfree(*atoms->atomtype[i]);
883 *atoms->atomtype[i] = NULL;
887 if (bFreeNames && atoms->atomtypeB != NULL)
889 for (i = 0; i < atoms->nr; i++)
891 if (atoms->atomtypeB[i] != NULL)
893 sfree(*atoms->atomtypeB[i]);
894 *atoms->atomtypeB[i] = NULL;
898 sfree(atoms->atomname);
899 sfree(atoms->atomtype);
900 sfree(atoms->atomtypeB);
901 sfree(atoms->resinfo);
903 sfree(atoms->pdbinfo);
906 atoms->atomname = NULL;
907 atoms->atomtype = NULL;
908 atoms->atomtypeB = NULL;
909 atoms->resinfo = NULL;
911 atoms->pdbinfo = NULL;
914 real max_cutoff(real cutoff1, real cutoff2)
916 if (cutoff1 == 0 || cutoff2 == 0)
922 return max(cutoff1, cutoff2);
926 void init_df_history(df_history_t *dfhist, int nlambda)
930 dfhist->nlambda = nlambda;
932 dfhist->wl_delta = 0;
936 snew(dfhist->sum_weights, dfhist->nlambda);
937 snew(dfhist->sum_dg, dfhist->nlambda);
938 snew(dfhist->sum_minvar, dfhist->nlambda);
939 snew(dfhist->sum_variance, dfhist->nlambda);
940 snew(dfhist->n_at_lam, dfhist->nlambda);
941 snew(dfhist->wl_histo, dfhist->nlambda);
943 /* allocate transition matrices here */
944 snew(dfhist->Tij, dfhist->nlambda);
945 snew(dfhist->Tij_empirical, dfhist->nlambda);
947 /* allocate accumulators for various transition matrix
948 free energy methods here */
949 snew(dfhist->accum_p, dfhist->nlambda);
950 snew(dfhist->accum_m, dfhist->nlambda);
951 snew(dfhist->accum_p2, dfhist->nlambda);
952 snew(dfhist->accum_m2, dfhist->nlambda);
954 for (i = 0; i < dfhist->nlambda; i++)
956 snew(dfhist->Tij[i], dfhist->nlambda);
957 snew(dfhist->Tij_empirical[i], dfhist->nlambda);
958 snew((dfhist->accum_p)[i], dfhist->nlambda);
959 snew((dfhist->accum_m)[i], dfhist->nlambda);
960 snew((dfhist->accum_p2)[i], dfhist->nlambda);
961 snew((dfhist->accum_m2)[i], dfhist->nlambda);
966 extern void copy_df_history(df_history_t *df_dest, df_history_t *df_source)
970 /* Currently, there should not be any difference in nlambda between the two,
971 but this is included for completeness for potential later functionality */
972 df_dest->nlambda = df_source->nlambda;
973 df_dest->bEquil = df_source->bEquil;
974 df_dest->wl_delta = df_source->wl_delta;
976 for (i = 0; i < df_dest->nlambda; i++)
978 df_dest->sum_weights[i] = df_source->sum_weights[i];
979 df_dest->sum_dg[i] = df_source->sum_dg[i];
980 df_dest->sum_minvar[i] = df_source->sum_minvar[i];
981 df_dest->sum_variance[i] = df_source->sum_variance[i];
982 df_dest->n_at_lam[i] = df_source->n_at_lam[i];
983 df_dest->wl_histo[i] = df_source->wl_histo[i];
986 for (i = 0; i < df_dest->nlambda; i++)
988 for (j = 0; j < df_dest->nlambda; j++)
990 df_dest->accum_p[i][j] = df_source->accum_p[i][j];
991 df_dest->accum_m[i][j] = df_source->accum_m[i][j];
992 df_dest->accum_p2[i][j] = df_source->accum_p2[i][j];
993 df_dest->accum_m2[i][j] = df_source->accum_m2[i][j];
994 df_dest->Tij[i][j] = df_source->Tij[i][j];
995 df_dest->Tij_empirical[i][j] = df_source->Tij_empirical[i][j];
1000 void done_df_history(df_history_t *dfhist)
1004 if (dfhist->nlambda > 0)
1006 sfree(dfhist->n_at_lam);
1007 sfree(dfhist->wl_histo);
1008 sfree(dfhist->sum_weights);
1009 sfree(dfhist->sum_dg);
1010 sfree(dfhist->sum_minvar);
1011 sfree(dfhist->sum_variance);
1013 for (i=0;i<dfhist->nlambda;i++)
1015 sfree(dfhist->Tij[i]);
1016 sfree(dfhist->Tij_empirical[i]);
1017 sfree(dfhist->accum_p[i]);
1018 sfree(dfhist->accum_m[i]);
1019 sfree(dfhist->accum_p2[i]);
1020 sfree(dfhist->accum_m2[i]);
1024 dfhist->nlambda = 0;
1025 dfhist->wl_delta = 0;