2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2008,2009,2010,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "mtop_util.h"
42 #include "gromacs/legacyheaders/types/ifunc.h"
43 #include "gromacs/legacyheaders/types/inputrec.h"
44 #include "gromacs/topology/block.h"
45 #include "gromacs/topology/symtab.h"
46 #include "gromacs/topology/topology.h"
47 #include "gromacs/topology/topsort.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/smalloc.h"
51 static int gmx_mtop_maxresnr(const gmx_mtop_t *mtop, int maxres_renum)
58 for (mt = 0; mt < mtop->nmoltype; mt++)
60 atoms = &mtop->moltype[mt].atoms;
61 if (atoms->nres > maxres_renum)
63 for (r = 0; r < atoms->nres; r++)
65 if (atoms->resinfo[r].nr > maxresnr)
67 maxresnr = atoms->resinfo[r].nr;
76 void gmx_mtop_finalize(gmx_mtop_t *mtop)
80 mtop->maxres_renum = 1;
82 env = getenv("GMX_MAXRESRENUM");
85 sscanf(env, "%d", &mtop->maxres_renum);
87 if (mtop->maxres_renum == -1)
89 /* -1 signals renumber residues in all molecules */
90 mtop->maxres_renum = INT_MAX;
93 mtop->maxresnr = gmx_mtop_maxresnr(mtop, mtop->maxres_renum);
96 void gmx_mtop_count_atomtypes(const gmx_mtop_t *mtop, int state, int typecount[])
101 for (i = 0; i < mtop->ffparams.atnr; ++i)
105 for (mb = 0; mb < mtop->nmolblock; ++mb)
107 nmol = mtop->molblock[mb].nmol;
108 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
109 for (i = 0; i < atoms->nr; ++i)
113 tpi = atoms->atom[i].type;
117 tpi = atoms->atom[i].typeB;
119 typecount[tpi] += nmol;
124 int ncg_mtop(const gmx_mtop_t *mtop)
130 for (mb = 0; mb < mtop->nmolblock; mb++)
133 mtop->molblock[mb].nmol*
134 mtop->moltype[mtop->molblock[mb].type].cgs.nr;
140 void gmx_mtop_remove_chargegroups(gmx_mtop_t *mtop)
146 for (mt = 0; mt < mtop->nmoltype; mt++)
148 cgs = &mtop->moltype[mt].cgs;
149 if (cgs->nr < mtop->moltype[mt].atoms.nr)
151 cgs->nr = mtop->moltype[mt].atoms.nr;
152 srenew(cgs->index, cgs->nr+1);
153 for (i = 0; i < cgs->nr+1; i++)
169 typedef struct gmx_mtop_atomlookup
171 const gmx_mtop_t *mtop;
175 } t_gmx_mtop_atomlookup;
178 gmx_mtop_atomlookup_t
179 gmx_mtop_atomlookup_init(const gmx_mtop_t *mtop)
181 t_gmx_mtop_atomlookup *alook;
183 int a_start, a_end, na, na_start = -1;
188 alook->nmb = mtop->nmolblock;
190 snew(alook->mba, alook->nmb);
193 for (mb = 0; mb < mtop->nmolblock; mb++)
195 na = mtop->molblock[mb].nmol*mtop->molblock[mb].natoms_mol;
196 a_end = a_start + na;
198 alook->mba[mb].a_start = a_start;
199 alook->mba[mb].a_end = a_end;
200 alook->mba[mb].na_mol = mtop->molblock[mb].natoms_mol;
202 /* We start the binary search with the largest block */
203 if (mb == 0 || na > na_start)
205 alook->mb_start = mb;
215 gmx_mtop_atomlookup_t
216 gmx_mtop_atomlookup_settle_init(const gmx_mtop_t *mtop)
218 t_gmx_mtop_atomlookup *alook;
220 int na, na_start = -1;
222 alook = gmx_mtop_atomlookup_init(mtop);
224 /* Check if the starting molblock has settle */
225 if (mtop->moltype[mtop->molblock[alook->mb_start].type].ilist[F_SETTLE].nr == 0)
227 /* Search the largest molblock with settle */
228 alook->mb_start = -1;
229 for (mb = 0; mb < mtop->nmolblock; mb++)
231 if (mtop->moltype[mtop->molblock[mb].type].ilist[F_SETTLE].nr > 0)
233 na = alook->mba[mb].a_end - alook->mba[mb].a_start;
234 if (alook->mb_start == -1 || na > na_start)
236 alook->mb_start = mb;
242 if (alook->mb_start == -1)
244 gmx_incons("gmx_mtop_atomlookup_settle_init called without settles");
252 gmx_mtop_atomlookup_destroy(gmx_mtop_atomlookup_t alook)
258 void gmx_mtop_atomnr_to_atom(const gmx_mtop_atomlookup_t alook,
263 int a_start, atnr_mol;
266 if (atnr_global < 0 || atnr_global >= mtop->natoms)
268 gmx_fatal(FARGS, "gmx_mtop_atomnr_to_moltype was called with atnr_global=%d which is not in the atom range of this system (%d-%d)",
269 atnr_global, 0, mtop->natoms-1);
275 mb = alook->mb_start;
279 a_start = alook->mba[mb].a_start;
280 if (atnr_global < a_start)
284 else if (atnr_global >= alook->mba[mb].a_end)
292 mb = ((mb0 + mb1 + 1)>>1);
295 atnr_mol = (atnr_global - a_start) % alook->mba[mb].na_mol;
297 *atom = &alook->mtop->moltype[alook->mtop->molblock[mb].type].atoms.atom[atnr_mol];
300 void gmx_mtop_atomnr_to_ilist(const gmx_mtop_atomlookup_t alook,
302 t_ilist **ilist_mol, int *atnr_offset)
305 int a_start, atnr_local;
308 if (atnr_global < 0 || atnr_global >= mtop->natoms)
310 gmx_fatal(FARGS, "gmx_mtop_atomnr_to_moltype was called with atnr_global=%d which is not in the atom range of this system (%d-%d)",
311 atnr_global, 0, mtop->natoms-1);
317 mb = alook->mb_start;
321 a_start = alook->mba[mb].a_start;
322 if (atnr_global < a_start)
326 else if (atnr_global >= alook->mba[mb].a_end)
334 mb = ((mb0 + mb1 + 1)>>1);
337 *ilist_mol = alook->mtop->moltype[alook->mtop->molblock[mb].type].ilist;
339 atnr_local = (atnr_global - a_start) % alook->mba[mb].na_mol;
341 *atnr_offset = atnr_global - atnr_local;
344 void gmx_mtop_atomnr_to_molblock_ind(const gmx_mtop_atomlookup_t alook,
346 int *molb, int *molnr, int *atnr_mol)
352 if (atnr_global < 0 || atnr_global >= mtop->natoms)
354 gmx_fatal(FARGS, "gmx_mtop_atomnr_to_moltype was called with atnr_global=%d which is not in the atom range of this system (%d-%d)",
355 atnr_global, 0, mtop->natoms-1);
361 mb = alook->mb_start;
365 a_start = alook->mba[mb].a_start;
366 if (atnr_global < a_start)
370 else if (atnr_global >= alook->mba[mb].a_end)
378 mb = ((mb0 + mb1 + 1)>>1);
382 *molnr = (atnr_global - a_start) / alook->mba[mb].na_mol;
383 *atnr_mol = atnr_global - a_start - (*molnr)*alook->mba[mb].na_mol;
386 void gmx_mtop_atominfo_global(const gmx_mtop_t *mtop, int atnr_global,
387 char **atomname, int *resnr, char **resname)
389 int mb, a_start, a_end, maxresnr, at_loc;
390 gmx_molblock_t *molb;
391 t_atoms *atoms = NULL;
393 if (atnr_global < 0 || atnr_global >= mtop->natoms)
395 gmx_fatal(FARGS, "gmx_mtop_atominfo_global was called with atnr_global=%d which is not in the atom range of this system (%d-%d)",
396 atnr_global, 0, mtop->natoms-1);
401 maxresnr = mtop->maxresnr;
406 if (atoms->nres <= mtop->maxres_renum)
408 /* Single residue molecule, keep counting */
409 maxresnr += mtop->molblock[mb].nmol*atoms->nres;
413 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
415 a_end = a_start + mtop->molblock[mb].nmol*atoms->nr;
417 while (atnr_global >= a_end);
419 at_loc = (atnr_global - a_start) % atoms->nr;
420 *atomname = *(atoms->atomname[at_loc]);
421 if (atoms->nres > mtop->maxres_renum)
423 *resnr = atoms->resinfo[atoms->atom[at_loc].resind].nr;
427 /* Single residue molecule, keep counting */
428 *resnr = maxresnr + 1 + (atnr_global - a_start)/atoms->nr*atoms->nres + atoms->atom[at_loc].resind;
430 *resname = *(atoms->resinfo[atoms->atom[at_loc].resind].name);
433 typedef struct gmx_mtop_atomloop_all
435 const gmx_mtop_t *mtop;
442 } t_gmx_mtop_atomloop_all;
444 gmx_mtop_atomloop_all_t
445 gmx_mtop_atomloop_all_init(const gmx_mtop_t *mtop)
447 struct gmx_mtop_atomloop_all *aloop;
454 &mtop->moltype[mtop->molblock[aloop->mblock].type].atoms;
456 aloop->maxresnr = mtop->maxresnr;
457 aloop->at_local = -1;
458 aloop->at_global = -1;
463 static void gmx_mtop_atomloop_all_destroy(gmx_mtop_atomloop_all_t aloop)
468 gmx_bool gmx_mtop_atomloop_all_next(gmx_mtop_atomloop_all_t aloop,
469 int *at_global, t_atom **atom)
473 gmx_incons("gmx_mtop_atomloop_all_next called without calling gmx_mtop_atomloop_all_init");
479 if (aloop->at_local >= aloop->atoms->nr)
481 if (aloop->atoms->nres <= aloop->mtop->maxres_renum)
483 /* Single residue molecule, increase the count with one */
484 aloop->maxresnr += aloop->atoms->nres;
488 if (aloop->mol >= aloop->mtop->molblock[aloop->mblock].nmol)
491 if (aloop->mblock >= aloop->mtop->nmolblock)
493 gmx_mtop_atomloop_all_destroy(aloop);
496 aloop->atoms = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type].atoms;
501 *at_global = aloop->at_global;
502 *atom = &aloop->atoms->atom[aloop->at_local];
507 void gmx_mtop_atomloop_all_names(gmx_mtop_atomloop_all_t aloop,
508 char **atomname, int *resnr, char **resname)
512 *atomname = *(aloop->atoms->atomname[aloop->at_local]);
513 resind_mol = aloop->atoms->atom[aloop->at_local].resind;
514 *resnr = aloop->atoms->resinfo[resind_mol].nr;
515 if (aloop->atoms->nres <= aloop->mtop->maxres_renum)
517 *resnr = aloop->maxresnr + 1 + resind_mol;
519 *resname = *(aloop->atoms->resinfo[resind_mol].name);
522 void gmx_mtop_atomloop_all_moltype(gmx_mtop_atomloop_all_t aloop,
523 gmx_moltype_t **moltype, int *at_mol)
525 *moltype = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type];
526 *at_mol = aloop->at_local;
529 typedef struct gmx_mtop_atomloop_block
531 const gmx_mtop_t *mtop;
535 } t_gmx_mtop_atomloop_block;
537 gmx_mtop_atomloop_block_t
538 gmx_mtop_atomloop_block_init(const gmx_mtop_t *mtop)
540 struct gmx_mtop_atomloop_block *aloop;
546 aloop->atoms = &mtop->moltype[mtop->molblock[aloop->mblock].type].atoms;
547 aloop->at_local = -1;
552 static void gmx_mtop_atomloop_block_destroy(gmx_mtop_atomloop_block_t aloop)
557 gmx_bool gmx_mtop_atomloop_block_next(gmx_mtop_atomloop_block_t aloop,
558 t_atom **atom, int *nmol)
562 gmx_incons("gmx_mtop_atomloop_all_next called without calling gmx_mtop_atomloop_all_init");
567 if (aloop->at_local >= aloop->atoms->nr)
570 if (aloop->mblock >= aloop->mtop->nmolblock)
572 gmx_mtop_atomloop_block_destroy(aloop);
575 aloop->atoms = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type].atoms;
579 *atom = &aloop->atoms->atom[aloop->at_local];
580 *nmol = aloop->mtop->molblock[aloop->mblock].nmol;
585 typedef struct gmx_mtop_ilistloop
587 const gmx_mtop_t *mtop;
592 gmx_mtop_ilistloop_init(const gmx_mtop_t *mtop)
594 struct gmx_mtop_ilistloop *iloop;
604 static void gmx_mtop_ilistloop_destroy(gmx_mtop_ilistloop_t iloop)
609 gmx_bool gmx_mtop_ilistloop_next(gmx_mtop_ilistloop_t iloop,
610 t_ilist **ilist_mol, int *nmol)
614 gmx_incons("gmx_mtop_ilistloop_next called without calling gmx_mtop_ilistloop_init");
618 if (iloop->mblock == iloop->mtop->nmolblock)
620 gmx_mtop_ilistloop_destroy(iloop);
625 iloop->mtop->moltype[iloop->mtop->molblock[iloop->mblock].type].ilist;
627 *nmol = iloop->mtop->molblock[iloop->mblock].nmol;
631 typedef struct gmx_mtop_ilistloop_all
633 const gmx_mtop_t *mtop;
637 } t_gmx_mtop_ilist_all;
639 gmx_mtop_ilistloop_all_t
640 gmx_mtop_ilistloop_all_init(const gmx_mtop_t *mtop)
642 struct gmx_mtop_ilistloop_all *iloop;
654 static void gmx_mtop_ilistloop_all_destroy(gmx_mtop_ilistloop_all_t iloop)
659 gmx_bool gmx_mtop_ilistloop_all_next(gmx_mtop_ilistloop_all_t iloop,
660 t_ilist **ilist_mol, int *atnr_offset)
662 gmx_molblock_t *molb;
666 gmx_incons("gmx_mtop_ilistloop_all_next called without calling gmx_mtop_ilistloop_all_init");
671 iloop->a_offset += iloop->mtop->molblock[iloop->mblock].natoms_mol;
676 if (iloop->mol >= iloop->mtop->molblock[iloop->mblock].nmol)
680 if (iloop->mblock == iloop->mtop->nmolblock)
682 gmx_mtop_ilistloop_all_destroy(iloop);
688 iloop->mtop->moltype[iloop->mtop->molblock[iloop->mblock].type].ilist;
690 *atnr_offset = iloop->a_offset;
695 int gmx_mtop_ftype_count(const gmx_mtop_t *mtop, int ftype)
697 gmx_mtop_ilistloop_t iloop;
703 iloop = gmx_mtop_ilistloop_init(mtop);
704 while (gmx_mtop_ilistloop_next(iloop, &il, &nmol))
706 n += nmol*il[ftype].nr/(1+NRAL(ftype));
712 t_block gmx_mtop_global_cgs(const gmx_mtop_t *mtop)
714 t_block cgs_gl, *cgs_mol;
716 gmx_molblock_t *molb;
719 /* In most cases this is too much, but we realloc at the end */
720 snew(cgs_gl.index, mtop->natoms+1);
724 for (mb = 0; mb < mtop->nmolblock; mb++)
726 molb = &mtop->molblock[mb];
727 cgs_mol = &mtop->moltype[molb->type].cgs;
728 for (mol = 0; mol < molb->nmol; mol++)
730 for (cg = 0; cg < cgs_mol->nr; cg++)
732 cgs_gl.index[cgs_gl.nr+1] =
733 cgs_gl.index[cgs_gl.nr] +
734 cgs_mol->index[cg+1] - cgs_mol->index[cg];
739 cgs_gl.nalloc_index = cgs_gl.nr + 1;
740 srenew(cgs_gl.index, cgs_gl.nalloc_index);
745 static void atomcat(t_atoms *dest, t_atoms *src, int copies,
746 int maxres_renum, int *maxresnr)
750 int destnr = dest->nr;
754 size = destnr+copies*srcnr;
755 srenew(dest->atom, size);
756 srenew(dest->atomname, size);
757 srenew(dest->atomtype, size);
758 srenew(dest->atomtypeB, size);
762 size = dest->nres+copies*src->nres;
763 srenew(dest->resinfo, size);
766 /* residue information */
767 for (l = dest->nres, j = 0; (j < copies); j++, l += src->nres)
769 memcpy((char *) &(dest->resinfo[l]), (char *) &(src->resinfo[0]),
770 (size_t)(src->nres*sizeof(src->resinfo[0])));
773 for (l = destnr, j = 0; (j < copies); j++, l += srcnr)
775 memcpy((char *) &(dest->atomname[l]), (char *) &(src->atomname[0]),
776 (size_t)(srcnr*sizeof(src->atomname[0])));
777 memcpy((char *) &(dest->atomtype[l]), (char *) &(src->atomtype[0]),
778 (size_t)(srcnr*sizeof(src->atomtype[0])));
779 memcpy((char *) &(dest->atomtypeB[l]), (char *) &(src->atomtypeB[0]),
780 (size_t)(srcnr*sizeof(src->atomtypeB[0])));
781 memcpy((char *) &(dest->atom[l]), (char *) &(src->atom[0]),
782 (size_t)(srcnr*sizeof(src->atom[0])));
785 /* Increment residue indices */
786 for (l = destnr, j = 0; (j < copies); j++)
788 for (i = 0; (i < srcnr); i++, l++)
790 dest->atom[l].resind = dest->nres+j*src->nres+src->atom[i].resind;
794 if (src->nres <= maxres_renum)
796 /* Single residue molecule, continue counting residues */
797 for (j = 0; (j < copies); j++)
799 for (l = 0; l < src->nres; l++)
802 dest->resinfo[dest->nres+j*src->nres+l].nr = *maxresnr;
807 dest->nres += copies*src->nres;
808 dest->nr += copies*src->nr;
811 t_atoms gmx_mtop_global_atoms(const gmx_mtop_t *mtop)
815 gmx_molblock_t *molb;
817 init_t_atoms(&atoms, 0, FALSE);
819 maxresnr = mtop->maxresnr;
820 for (mb = 0; mb < mtop->nmolblock; mb++)
822 molb = &mtop->molblock[mb];
823 atomcat(&atoms, &mtop->moltype[molb->type].atoms, molb->nmol,
824 mtop->maxres_renum, &maxresnr);
830 void gmx_mtop_make_atomic_charge_groups(gmx_mtop_t *mtop,
831 gmx_bool bKeepSingleMolCG)
836 for (mb = 0; mb < mtop->nmolblock; mb++)
838 cgs_mol = &mtop->moltype[mtop->molblock[mb].type].cgs;
839 if (!(bKeepSingleMolCG && cgs_mol->nr == 1))
841 cgs_mol->nr = mtop->molblock[mb].natoms_mol;
842 cgs_mol->nalloc_index = cgs_mol->nr + 1;
843 srenew(cgs_mol->index, cgs_mol->nalloc_index);
844 for (cg = 0; cg < cgs_mol->nr+1; cg++)
846 cgs_mol->index[cg] = cg;
853 * The cat routines below are old code from src/kernel/topcat.c
856 static void blockcat(t_block *dest, t_block *src, int copies)
858 int i, j, l, nra, size;
862 size = (dest->nr+copies*src->nr+1);
863 srenew(dest->index, size);
866 nra = dest->index[dest->nr];
867 for (l = dest->nr, j = 0; (j < copies); j++)
869 for (i = 0; (i < src->nr); i++)
871 dest->index[l++] = nra + src->index[i];
873 nra += src->index[src->nr];
875 dest->nr += copies*src->nr;
876 dest->index[dest->nr] = nra;
879 static void blockacat(t_blocka *dest, t_blocka *src, int copies,
883 int destnr = dest->nr;
884 int destnra = dest->nra;
888 size = (dest->nr+copies*src->nr+1);
889 srenew(dest->index, size);
893 size = (dest->nra+copies*src->nra);
894 srenew(dest->a, size);
897 for (l = destnr, j = 0; (j < copies); j++)
899 for (i = 0; (i < src->nr); i++)
901 dest->index[l++] = dest->nra+src->index[i];
903 dest->nra += src->nra;
905 for (l = destnra, j = 0; (j < copies); j++)
907 for (i = 0; (i < src->nra); i++)
909 dest->a[l++] = dnum+src->a[i];
914 dest->index[dest->nr] = dest->nra;
917 static void ilistcat(int ftype, t_ilist *dest, t_ilist *src, int copies,
924 dest->nalloc = dest->nr + copies*src->nr;
925 srenew(dest->iatoms, dest->nalloc);
927 for (c = 0; c < copies; c++)
929 for (i = 0; i < src->nr; )
931 dest->iatoms[dest->nr++] = src->iatoms[i++];
932 for (a = 0; a < nral; a++)
934 dest->iatoms[dest->nr++] = dnum + src->iatoms[i++];
941 static void set_posres_params(t_idef *idef, gmx_molblock_t *molb,
942 int i0, int a_offset)
948 il = &idef->il[F_POSRES];
950 idef->iparams_posres_nalloc = i1;
951 srenew(idef->iparams_posres, idef->iparams_posres_nalloc);
952 for (i = i0; i < i1; i++)
954 ip = &idef->iparams_posres[i];
955 /* Copy the force constants */
956 *ip = idef->iparams[il->iatoms[i*2]];
957 a_molb = il->iatoms[i*2+1] - a_offset;
958 if (molb->nposres_xA == 0)
960 gmx_incons("Position restraint coordinates are missing");
962 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
963 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
964 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
965 if (molb->nposres_xB > 0)
967 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
968 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
969 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
973 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
974 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
975 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
977 /* Set the parameter index for idef->iparams_posre */
982 static void set_fbposres_params(t_idef *idef, gmx_molblock_t *molb,
983 int i0, int a_offset)
989 il = &idef->il[F_FBPOSRES];
991 idef->iparams_fbposres_nalloc = i1;
992 srenew(idef->iparams_fbposres, idef->iparams_fbposres_nalloc);
993 for (i = i0; i < i1; i++)
995 ip = &idef->iparams_fbposres[i];
996 /* Copy the force constants */
997 *ip = idef->iparams[il->iatoms[i*2]];
998 a_molb = il->iatoms[i*2+1] - a_offset;
999 if (molb->nposres_xA == 0)
1001 gmx_incons("Position restraint coordinates are missing");
1003 /* Take flat-bottom posres reference from normal position restraints */
1004 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
1005 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
1006 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
1007 /* Note: no B-type for flat-bottom posres */
1009 /* Set the parameter index for idef->iparams_posre */
1010 il->iatoms[i*2] = i;
1014 static void gen_local_top(const gmx_mtop_t *mtop, const t_inputrec *ir,
1015 gmx_bool bMergeConstr,
1016 gmx_localtop_t *top)
1018 int mb, srcnr, destnr, ftype, ftype_dest, mt, natoms, mol, nposre_old, nfbposre_old;
1019 gmx_molblock_t *molb;
1020 gmx_moltype_t *molt;
1021 const gmx_ffparams_t *ffp;
1024 gmx_mtop_atomloop_all_t aloop;
1028 top->atomtypes = mtop->atomtypes;
1030 ffp = &mtop->ffparams;
1033 idef->ntypes = ffp->ntypes;
1034 idef->atnr = ffp->atnr;
1035 idef->functype = ffp->functype;
1036 idef->iparams = ffp->iparams;
1037 idef->iparams_posres = NULL;
1038 idef->iparams_posres_nalloc = 0;
1039 idef->iparams_fbposres = NULL;
1040 idef->iparams_fbposres_nalloc = 0;
1041 idef->fudgeQQ = ffp->fudgeQQ;
1042 idef->cmap_grid = ffp->cmap_grid;
1043 idef->ilsort = ilsortUNKNOWN;
1045 init_block(&top->cgs);
1046 init_blocka(&top->excls);
1047 for (ftype = 0; ftype < F_NRE; ftype++)
1049 idef->il[ftype].nr = 0;
1050 idef->il[ftype].nalloc = 0;
1051 idef->il[ftype].iatoms = NULL;
1055 for (mb = 0; mb < mtop->nmolblock; mb++)
1057 molb = &mtop->molblock[mb];
1058 molt = &mtop->moltype[molb->type];
1060 srcnr = molt->atoms.nr;
1063 blockcat(&top->cgs, &molt->cgs, molb->nmol);
1065 blockacat(&top->excls, &molt->excls, molb->nmol, destnr, srcnr);
1067 nposre_old = idef->il[F_POSRES].nr;
1068 nfbposre_old = idef->il[F_FBPOSRES].nr;
1069 for (ftype = 0; ftype < F_NRE; ftype++)
1072 ftype == F_CONSTR && molt->ilist[F_CONSTRNC].nr > 0)
1074 /* Merge all constrains into one ilist.
1075 * This simplifies the constraint code.
1077 for (mol = 0; mol < molb->nmol; mol++)
1079 ilistcat(ftype, &idef->il[F_CONSTR], &molt->ilist[F_CONSTR],
1080 1, destnr+mol*srcnr, srcnr);
1081 ilistcat(ftype, &idef->il[F_CONSTR], &molt->ilist[F_CONSTRNC],
1082 1, destnr+mol*srcnr, srcnr);
1085 else if (!(bMergeConstr && ftype == F_CONSTRNC))
1087 ilistcat(ftype, &idef->il[ftype], &molt->ilist[ftype],
1088 molb->nmol, destnr, srcnr);
1091 if (idef->il[F_POSRES].nr > nposre_old)
1093 /* Executing this line line stops gmxdump -sys working
1094 * correctly. I'm not aware there's an elegant fix. */
1095 set_posres_params(idef, molb, nposre_old/2, natoms);
1097 if (idef->il[F_FBPOSRES].nr > nfbposre_old)
1099 set_fbposres_params(idef, molb, nfbposre_old/2, natoms);
1102 natoms += molb->nmol*srcnr;
1107 top->idef.ilsort = ilsortUNKNOWN;
1111 if (ir->efep != efepNO && gmx_mtop_bondeds_free_energy(mtop))
1113 snew(qA, mtop->natoms);
1114 snew(qB, mtop->natoms);
1115 aloop = gmx_mtop_atomloop_all_init(mtop);
1116 while (gmx_mtop_atomloop_all_next(aloop, &ag, &atom))
1121 gmx_sort_ilist_fe(&top->idef, qA, qB);
1127 top->idef.ilsort = ilsortNO_FE;
1132 gmx_localtop_t *gmx_mtop_generate_local_top(const gmx_mtop_t *mtop,
1133 const t_inputrec *ir)
1135 gmx_localtop_t *top;
1139 gen_local_top(mtop, ir, TRUE, top);
1144 t_topology gmx_mtop_t_to_t_topology(gmx_mtop_t *mtop)
1147 gmx_localtop_t ltop;
1150 gen_local_top(mtop, NULL, FALSE, <op);
1152 top.name = mtop->name;
1153 top.idef = ltop.idef;
1154 top.atomtypes = ltop.atomtypes;
1156 top.excls = ltop.excls;
1157 top.atoms = gmx_mtop_global_atoms(mtop);
1158 top.mols = mtop->mols;
1159 top.symtab = mtop->symtab;
1161 /* We only need to free the moltype and molblock data,
1162 * all other pointers have been copied to top.
1164 * Well, except for the group data, but we can't free those, because they
1165 * are used somewhere even after a call to this function.
1167 for (mt = 0; mt < mtop->nmoltype; mt++)
1169 done_moltype(&mtop->moltype[mt]);
1171 sfree(mtop->moltype);
1173 for (mb = 0; mb < mtop->nmolblock; mb++)
1175 done_molblock(&mtop->molblock[mb]);
1177 sfree(mtop->molblock);