2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013, 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.
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.
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.
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.
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.
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.
37 /* This file is completely threadsafe - keep it that way! */
49 #include "gromacs/legacyheaders/thread_mpi/threads.h"
51 /* The source code in this file should be thread-safe.
52 Please keep it that way. */
56 static gmx_bool bOverAllocDD = FALSE;
57 static tMPI_Thread_mutex_t over_alloc_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
60 void set_over_alloc_dd(gmx_bool set)
62 tMPI_Thread_mutex_lock(&over_alloc_mutex);
63 /* we just make sure that we don't set this at the same time.
64 We don't worry too much about reading this rarely-set variable */
66 tMPI_Thread_mutex_unlock(&over_alloc_mutex);
69 int over_alloc_dd(int n)
73 return OVER_ALLOC_FAC*n + 100;
81 int gmx_int64_to_int(gmx_int64_t step, const char *warn)
87 if (warn != NULL && (step < INT_MIN || step > INT_MAX))
89 fprintf(stderr, "\nWARNING during %s:\n", warn);
90 fprintf(stderr, "step value ");
91 fprintf(stderr, "%"GMX_PRId64, step);
92 fprintf(stderr, " does not fit in int, converted to %d\n\n", i);
98 char *gmx_step_str(gmx_int64_t i, char *buf)
100 sprintf(buf, "%"GMX_PRId64, i);
105 void init_block(t_block *block)
110 block->nalloc_index = 1;
111 snew(block->index, block->nalloc_index);
115 void init_blocka(t_blocka *block)
121 block->nalloc_index = 1;
122 snew(block->index, block->nalloc_index);
128 void init_atom(t_atoms *at)
138 at->atomtypeB = NULL;
142 void init_atomtypes(t_atomtypes *at)
147 at->atomnumber = NULL;
148 at->gb_radius = NULL;
152 void init_groups(gmx_groups_t *groups)
156 groups->ngrpname = 0;
157 groups->grpname = NULL;
158 for (g = 0; (g < egcNR); g++)
160 groups->grps[g].nm_ind = NULL;
161 groups->ngrpnr[g] = 0;
162 groups->grpnr[g] = NULL;
167 void init_mtop(gmx_mtop_t *mtop)
171 mtop->moltype = NULL;
173 mtop->molblock = NULL;
174 mtop->maxres_renum = 0;
176 init_groups(&mtop->groups);
177 init_block(&mtop->mols);
178 open_symtab(&mtop->symtab);
181 void init_top (t_topology *top)
186 init_atom (&(top->atoms));
187 init_atomtypes(&(top->atomtypes));
188 init_block(&top->cgs);
189 init_block(&top->mols);
190 init_blocka(&top->excls);
191 open_symtab(&top->symtab);
194 void init_inputrec(t_inputrec *ir)
196 memset(ir, 0, (size_t)sizeof(*ir));
197 snew(ir->fepvals, 1);
198 snew(ir->expandedvals, 1);
199 snew(ir->simtempvals, 1);
202 void stupid_fill_block(t_block *grp, int natom, gmx_bool bOneIndexGroup)
208 grp->nalloc_index = 2;
209 snew(grp->index, grp->nalloc_index);
211 grp->index[1] = natom;
216 grp->nalloc_index = natom+1;
217 snew(grp->index, grp->nalloc_index);
218 snew(grp->index, natom+1);
219 for (i = 0; (i <= natom); i++)
227 void stupid_fill_blocka(t_blocka *grp, int natom)
231 grp->nalloc_a = natom;
232 snew(grp->a, grp->nalloc_a);
233 for (i = 0; (i < natom); i++)
239 grp->nalloc_index = natom + 1;
240 snew(grp->index, grp->nalloc_index);
241 for (i = 0; (i <= natom); i++)
248 void copy_blocka(const t_blocka *src, t_blocka *dest)
253 dest->nalloc_index = dest->nr + 1;
254 snew(dest->index, dest->nalloc_index);
255 for (i = 0; i < dest->nr+1; i++)
257 dest->index[i] = src->index[i];
259 dest->nra = src->nra;
260 dest->nalloc_a = dest->nra + 1;
261 snew(dest->a, dest->nalloc_a);
262 for (i = 0; i < dest->nra+1; i++)
264 dest->a[i] = src->a[i];
268 void done_block(t_block *block)
272 block->nalloc_index = 0;
275 void done_blocka(t_blocka *block)
283 block->nalloc_index = 0;
287 void done_atom (t_atoms *at)
295 sfree(at->atomtypeB);
302 void done_atomtypes(t_atomtypes *atype)
305 sfree(atype->radius);
307 sfree(atype->surftens);
308 sfree(atype->atomnumber);
309 sfree(atype->gb_radius);
313 void done_moltype(gmx_moltype_t *molt)
317 done_atom(&molt->atoms);
318 done_block(&molt->cgs);
319 done_blocka(&molt->excls);
321 for (f = 0; f < F_NRE; f++)
323 sfree(molt->ilist[f].iatoms);
324 molt->ilist[f].nalloc = 0;
328 void done_molblock(gmx_molblock_t *molb)
330 if (molb->nposres_xA > 0)
332 molb->nposres_xA = 0;
333 free(molb->posres_xA);
335 if (molb->nposres_xB > 0)
337 molb->nposres_xB = 0;
338 free(molb->posres_xB);
342 void done_mtop(gmx_mtop_t *mtop, gmx_bool bDoneSymtab)
348 done_symtab(&mtop->symtab);
351 sfree(mtop->ffparams.functype);
352 sfree(mtop->ffparams.iparams);
354 for (i = 0; i < mtop->nmoltype; i++)
356 done_moltype(&mtop->moltype[i]);
358 sfree(mtop->moltype);
359 for (i = 0; i < mtop->nmolblock; i++)
361 done_molblock(&mtop->molblock[i]);
363 sfree(mtop->molblock);
364 done_block(&mtop->mols);
367 void done_top(t_topology *top)
371 sfree(top->idef.functype);
372 sfree(top->idef.iparams);
373 for (f = 0; f < F_NRE; ++f)
375 sfree(top->idef.il[f].iatoms);
376 top->idef.il[f].iatoms = NULL;
377 top->idef.il[f].nalloc = 0;
380 done_atom (&(top->atoms));
383 done_atomtypes(&(top->atomtypes));
385 done_symtab(&(top->symtab));
386 done_block(&(top->cgs));
387 done_block(&(top->mols));
388 done_blocka(&(top->excls));
391 static void done_pull_group(t_pull_group *pgrp)
396 sfree(pgrp->ind_loc);
398 sfree(pgrp->weight_loc);
402 static void done_pull(t_pull *pull)
406 for (i = 0; i < pull->ngroup+1; i++)
408 done_pull_group(pull->group);
409 done_pull_group(pull->dyna);
413 void done_inputrec(t_inputrec *ir)
417 for (m = 0; (m < DIM); m++)
425 sfree(ir->ex[m].phi);
433 sfree(ir->et[m].phi);
437 sfree(ir->opts.nrdf);
438 sfree(ir->opts.ref_t);
439 sfree(ir->opts.annealing);
440 sfree(ir->opts.anneal_npoints);
441 sfree(ir->opts.anneal_time);
442 sfree(ir->opts.anneal_temp);
443 sfree(ir->opts.tau_t);
445 sfree(ir->opts.nFreeze);
446 sfree(ir->opts.QMmethod);
447 sfree(ir->opts.QMbasis);
448 sfree(ir->opts.QMcharge);
449 sfree(ir->opts.QMmult);
451 sfree(ir->opts.CASorbitals);
452 sfree(ir->opts.CASelectrons);
453 sfree(ir->opts.SAon);
454 sfree(ir->opts.SAoff);
455 sfree(ir->opts.SAsteps);
456 sfree(ir->opts.bOPT);
466 static void zero_history(history_t *hist)
468 hist->disre_initf = 0;
469 hist->ndisrepairs = 0;
470 hist->disre_rm3tav = NULL;
471 hist->orire_initf = 0;
472 hist->norire_Dtav = 0;
473 hist->orire_Dtav = NULL;
476 static void zero_ekinstate(ekinstate_t *eks)
481 eks->ekinh_old = NULL;
482 eks->ekinscalef_nhc = NULL;
483 eks->ekinscaleh_nhc = NULL;
484 eks->vscale_nhc = NULL;
489 void init_energyhistory(energyhistory_t * enerhist)
493 enerhist->ener_ave = NULL;
494 enerhist->ener_sum = NULL;
495 enerhist->ener_sum_sim = NULL;
496 enerhist->dht = NULL;
498 enerhist->nsteps = 0;
500 enerhist->nsteps_sim = 0;
501 enerhist->nsum_sim = 0;
503 enerhist->dht = NULL;
506 static void done_delta_h_history(delta_h_history_t *dht)
510 for (i = 0; i < dht->nndh; i++)
518 void done_energyhistory(energyhistory_t * enerhist)
520 sfree(enerhist->ener_ave);
521 sfree(enerhist->ener_sum);
522 sfree(enerhist->ener_sum_sim);
524 if (enerhist->dht != NULL)
526 done_delta_h_history(enerhist->dht);
527 sfree(enerhist->dht);
531 void init_gtc_state(t_state *state, int ngtc, int nnhpres, int nhchainlength)
536 state->nnhpres = nnhpres;
537 state->nhchainlength = nhchainlength;
540 snew(state->nosehoover_xi, state->nhchainlength*state->ngtc);
541 snew(state->nosehoover_vxi, state->nhchainlength*state->ngtc);
542 snew(state->therm_integral, state->ngtc);
543 for (i = 0; i < state->ngtc; i++)
545 for (j = 0; j < state->nhchainlength; j++)
547 state->nosehoover_xi[i*state->nhchainlength + j] = 0.0;
548 state->nosehoover_vxi[i*state->nhchainlength + j] = 0.0;
551 for (i = 0; i < state->ngtc; i++)
553 state->therm_integral[i] = 0.0;
558 state->nosehoover_xi = NULL;
559 state->nosehoover_vxi = NULL;
560 state->therm_integral = NULL;
563 if (state->nnhpres > 0)
565 snew(state->nhpres_xi, state->nhchainlength*nnhpres);
566 snew(state->nhpres_vxi, state->nhchainlength*nnhpres);
567 for (i = 0; i < nnhpres; i++)
569 for (j = 0; j < state->nhchainlength; j++)
571 state->nhpres_xi[i*nhchainlength + j] = 0.0;
572 state->nhpres_vxi[i*nhchainlength + j] = 0.0;
578 state->nhpres_xi = NULL;
579 state->nhpres_vxi = NULL;
584 void init_state(t_state *state, int natoms, int ngtc, int nnhpres, int nhchainlength, int nlambda)
588 state->natoms = natoms;
592 snew(state->lambda, efptNR);
593 for (i = 0; i < efptNR; i++)
595 state->lambda[i] = 0;
598 clear_mat(state->box);
599 clear_mat(state->box_rel);
600 clear_mat(state->boxv);
601 clear_mat(state->pres_prev);
602 clear_mat(state->svir_prev);
603 clear_mat(state->fvir_prev);
604 init_gtc_state(state, ngtc, nnhpres, nhchainlength);
605 state->nalloc = state->natoms;
606 if (state->nalloc > 0)
608 snew(state->x, state->nalloc);
609 snew(state->v, state->nalloc);
618 zero_history(&state->hist);
619 zero_ekinstate(&state->ekinstate);
620 init_energyhistory(&state->enerhist);
621 init_df_history(&state->dfhist, nlambda);
622 state->ddp_count = 0;
623 state->ddp_count_cg_gl = 0;
625 state->cg_gl_nalloc = 0;
628 void done_state(t_state *state)
651 state->cg_gl_nalloc = 0;
654 sfree(state->lambda);
658 sfree(state->nosehoover_xi);
659 sfree(state->nosehoover_vxi);
660 sfree(state->therm_integral);
664 static void do_box_rel(t_inputrec *ir, matrix box_rel, matrix b, gmx_bool bInit)
668 for (d = YY; d <= ZZ; d++)
670 for (d2 = XX; d2 <= (ir->epct == epctSEMIISOTROPIC ? YY : ZZ); d2++)
672 /* We need to check if this box component is deformed
673 * or if deformation of another component might cause
674 * changes in this component due to box corrections.
676 if (ir->deform[d][d2] == 0 &&
677 !(d == ZZ && d2 == XX && ir->deform[d][YY] != 0 &&
678 (b[YY][d2] != 0 || ir->deform[YY][d2] != 0)))
682 box_rel[d][d2] = b[d][d2]/b[XX][XX];
686 b[d][d2] = b[XX][XX]*box_rel[d][d2];
693 void set_box_rel(t_inputrec *ir, t_state *state)
695 /* Make sure the box obeys the restrictions before we fix the ratios */
696 correct_box(NULL, 0, state->box, NULL);
698 clear_mat(state->box_rel);
700 if (PRESERVE_SHAPE(*ir))
702 do_box_rel(ir, state->box_rel, state->box, TRUE);
706 void preserve_box_shape(t_inputrec *ir, matrix box_rel, matrix b)
708 if (PRESERVE_SHAPE(*ir))
710 do_box_rel(ir, box_rel, b, FALSE);
714 void add_t_atoms(t_atoms *atoms, int natom_extra, int nres_extra)
720 srenew(atoms->atomname, atoms->nr+natom_extra);
721 srenew(atoms->atom, atoms->nr+natom_extra);
722 if (NULL != atoms->pdbinfo)
724 srenew(atoms->pdbinfo, atoms->nr+natom_extra);
726 if (NULL != atoms->atomtype)
728 srenew(atoms->atomtype, atoms->nr+natom_extra);
730 if (NULL != atoms->atomtypeB)
732 srenew(atoms->atomtypeB, atoms->nr+natom_extra);
734 for (i = atoms->nr; (i < atoms->nr+natom_extra); i++)
736 atoms->atomname[i] = NULL;
737 memset(&atoms->atom[i], 0, sizeof(atoms->atom[i]));
738 if (NULL != atoms->pdbinfo)
740 memset(&atoms->pdbinfo[i], 0, sizeof(atoms->pdbinfo[i]));
742 if (NULL != atoms->atomtype)
744 atoms->atomtype[i] = NULL;
746 if (NULL != atoms->atomtypeB)
748 atoms->atomtypeB[i] = NULL;
751 atoms->nr += natom_extra;
755 srenew(atoms->resinfo, atoms->nres+nres_extra);
756 for (i = atoms->nres; (i < atoms->nres+nres_extra); i++)
758 memset(&atoms->resinfo[i], 0, sizeof(atoms->resinfo[i]));
760 atoms->nres += nres_extra;
764 void init_t_atoms(t_atoms *atoms, int natoms, gmx_bool bPdbinfo)
768 snew(atoms->atomname, natoms);
769 atoms->atomtype = NULL;
770 atoms->atomtypeB = NULL;
771 snew(atoms->resinfo, natoms);
772 snew(atoms->atom, natoms);
775 snew(atoms->pdbinfo, natoms);
779 atoms->pdbinfo = NULL;
783 t_atoms *copy_t_atoms(t_atoms *src)
789 init_t_atoms(dst, src->nr, (NULL != src->pdbinfo));
791 if (NULL != src->atomname)
793 snew(dst->atomname, src->nr);
795 if (NULL != src->atomtype)
797 snew(dst->atomtype, src->nr);
799 if (NULL != src->atomtypeB)
801 snew(dst->atomtypeB, src->nr);
803 for (i = 0; (i < src->nr); i++)
805 dst->atom[i] = src->atom[i];
806 if (NULL != src->pdbinfo)
808 dst->pdbinfo[i] = src->pdbinfo[i];
810 if (NULL != src->atomname)
812 dst->atomname[i] = src->atomname[i];
814 if (NULL != src->atomtype)
816 dst->atomtype[i] = src->atomtype[i];
818 if (NULL != src->atomtypeB)
820 dst->atomtypeB[i] = src->atomtypeB[i];
823 dst->nres = src->nres;
824 for (i = 0; (i < src->nres); i++)
826 dst->resinfo[i] = src->resinfo[i];
831 void t_atoms_set_resinfo(t_atoms *atoms, int atom_ind, t_symtab *symtab,
832 const char *resname, int resnr, unsigned char ic,
833 int chainnum, char chainid)
837 ri = &atoms->resinfo[atoms->atom[atom_ind].resind];
838 ri->name = put_symtab(symtab, resname);
842 ri->chainnum = chainnum;
843 ri->chainid = chainid;
846 void free_t_atoms(t_atoms *atoms, gmx_bool bFreeNames)
850 if (bFreeNames && atoms->atomname != NULL)
852 for (i = 0; i < atoms->nr; i++)
854 if (atoms->atomname[i] != NULL)
856 sfree(*atoms->atomname[i]);
857 *atoms->atomname[i] = NULL;
861 if (bFreeNames && atoms->resinfo != NULL)
863 for (i = 0; i < atoms->nres; i++)
865 if (atoms->resinfo[i].name != NULL)
867 sfree(*atoms->resinfo[i].name);
868 *atoms->resinfo[i].name = NULL;
872 if (bFreeNames && atoms->atomtype != NULL)
874 for (i = 0; i < atoms->nr; i++)
876 if (atoms->atomtype[i] != NULL)
878 sfree(*atoms->atomtype[i]);
879 *atoms->atomtype[i] = NULL;
883 if (bFreeNames && atoms->atomtypeB != NULL)
885 for (i = 0; i < atoms->nr; i++)
887 if (atoms->atomtypeB[i] != NULL)
889 sfree(*atoms->atomtypeB[i]);
890 *atoms->atomtypeB[i] = NULL;
894 sfree(atoms->atomname);
895 sfree(atoms->atomtype);
896 sfree(atoms->atomtypeB);
897 sfree(atoms->resinfo);
899 sfree(atoms->pdbinfo);
902 atoms->atomname = NULL;
903 atoms->atomtype = NULL;
904 atoms->atomtypeB = NULL;
905 atoms->resinfo = NULL;
907 atoms->pdbinfo = NULL;
910 real max_cutoff(real cutoff1, real cutoff2)
912 if (cutoff1 == 0 || cutoff2 == 0)
918 return max(cutoff1, cutoff2);
922 void init_df_history(df_history_t *dfhist, int nlambda)
926 dfhist->nlambda = nlambda;
928 dfhist->wl_delta = 0;
932 snew(dfhist->sum_weights, dfhist->nlambda);
933 snew(dfhist->sum_dg, dfhist->nlambda);
934 snew(dfhist->sum_minvar, dfhist->nlambda);
935 snew(dfhist->sum_variance, dfhist->nlambda);
936 snew(dfhist->n_at_lam, dfhist->nlambda);
937 snew(dfhist->wl_histo, dfhist->nlambda);
939 /* allocate transition matrices here */
940 snew(dfhist->Tij, dfhist->nlambda);
941 snew(dfhist->Tij_empirical, dfhist->nlambda);
943 /* allocate accumulators for various transition matrix
944 free energy methods here */
945 snew(dfhist->accum_p, dfhist->nlambda);
946 snew(dfhist->accum_m, dfhist->nlambda);
947 snew(dfhist->accum_p2, dfhist->nlambda);
948 snew(dfhist->accum_m2, dfhist->nlambda);
950 for (i = 0; i < dfhist->nlambda; i++)
952 snew(dfhist->Tij[i], dfhist->nlambda);
953 snew(dfhist->Tij_empirical[i], dfhist->nlambda);
954 snew((dfhist->accum_p)[i], dfhist->nlambda);
955 snew((dfhist->accum_m)[i], dfhist->nlambda);
956 snew((dfhist->accum_p2)[i], dfhist->nlambda);
957 snew((dfhist->accum_m2)[i], dfhist->nlambda);
962 extern void copy_df_history(df_history_t *df_dest, df_history_t *df_source)
966 /* Currently, there should not be any difference in nlambda between the two,
967 but this is included for completeness for potential later functionality */
968 df_dest->nlambda = df_source->nlambda;
969 df_dest->bEquil = df_source->bEquil;
970 df_dest->wl_delta = df_source->wl_delta;
972 for (i = 0; i < df_dest->nlambda; i++)
974 df_dest->sum_weights[i] = df_source->sum_weights[i];
975 df_dest->sum_dg[i] = df_source->sum_dg[i];
976 df_dest->sum_minvar[i] = df_source->sum_minvar[i];
977 df_dest->sum_variance[i] = df_source->sum_variance[i];
978 df_dest->n_at_lam[i] = df_source->n_at_lam[i];
979 df_dest->wl_histo[i] = df_source->wl_histo[i];
982 for (i = 0; i < df_dest->nlambda; i++)
984 for (j = 0; j < df_dest->nlambda; j++)
986 df_dest->accum_p[i][j] = df_source->accum_p[i][j];
987 df_dest->accum_m[i][j] = df_source->accum_m[i][j];
988 df_dest->accum_p2[i][j] = df_source->accum_p2[i][j];
989 df_dest->accum_m2[i][j] = df_source->accum_m2[i][j];
990 df_dest->Tij[i][j] = df_source->Tij[i][j];
991 df_dest->Tij_empirical[i][j] = df_source->Tij_empirical[i][j];
996 void done_df_history(df_history_t *dfhist)
1000 if (dfhist->nlambda > 0)
1002 sfree(dfhist->n_at_lam);
1003 sfree(dfhist->wl_histo);
1004 sfree(dfhist->sum_weights);
1005 sfree(dfhist->sum_dg);
1006 sfree(dfhist->sum_minvar);
1007 sfree(dfhist->sum_variance);
1009 for (i = 0; i < dfhist->nlambda; i++)
1011 sfree(dfhist->Tij[i]);
1012 sfree(dfhist->Tij_empirical[i]);
1013 sfree(dfhist->accum_p[i]);
1014 sfree(dfhist->accum_m[i]);
1015 sfree(dfhist->accum_p2[i]);
1016 sfree(dfhist->accum_m2[i]);
1020 dfhist->nlambda = 0;
1021 dfhist->wl_delta = 0;