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.
41 #include "gromacs/utility/smalloc.h"
43 #include "mtop_util.h"
46 #include "gmx_fatal.h"
48 static int gmx_mtop_maxresnr(const gmx_mtop_t *mtop, int maxres_renum)
55 for (mt = 0; mt < mtop->nmoltype; mt++)
57 atoms = &mtop->moltype[mt].atoms;
58 if (atoms->nres > maxres_renum)
60 for (r = 0; r < atoms->nres; r++)
62 if (atoms->resinfo[r].nr > maxresnr)
64 maxresnr = atoms->resinfo[r].nr;
73 void gmx_mtop_finalize(gmx_mtop_t *mtop)
77 mtop->maxres_renum = 1;
79 env = getenv("GMX_MAXRESRENUM");
82 sscanf(env, "%d", &mtop->maxres_renum);
84 if (mtop->maxres_renum == -1)
86 /* -1 signals renumber residues in all molecules */
87 mtop->maxres_renum = INT_MAX;
90 mtop->maxresnr = gmx_mtop_maxresnr(mtop, mtop->maxres_renum);
93 void gmx_mtop_count_atomtypes(const gmx_mtop_t *mtop, int state, int typecount[])
98 for (i = 0; i < mtop->ffparams.atnr; ++i)
102 for (mb = 0; mb < mtop->nmolblock; ++mb)
104 nmol = mtop->molblock[mb].nmol;
105 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
106 for (i = 0; i < atoms->nr; ++i)
110 tpi = atoms->atom[i].type;
114 tpi = atoms->atom[i].typeB;
116 typecount[tpi] += nmol;
121 int ncg_mtop(const gmx_mtop_t *mtop)
127 for (mb = 0; mb < mtop->nmolblock; mb++)
130 mtop->molblock[mb].nmol*
131 mtop->moltype[mtop->molblock[mb].type].cgs.nr;
137 void gmx_mtop_remove_chargegroups(gmx_mtop_t *mtop)
143 for (mt = 0; mt < mtop->nmoltype; mt++)
145 cgs = &mtop->moltype[mt].cgs;
146 if (cgs->nr < mtop->moltype[mt].atoms.nr)
148 cgs->nr = mtop->moltype[mt].atoms.nr;
149 srenew(cgs->index, cgs->nr+1);
150 for (i = 0; i < cgs->nr+1; i++)
166 typedef struct gmx_mtop_atomlookup
168 const gmx_mtop_t *mtop;
172 } t_gmx_mtop_atomlookup;
175 gmx_mtop_atomlookup_t
176 gmx_mtop_atomlookup_init(const gmx_mtop_t *mtop)
178 t_gmx_mtop_atomlookup *alook;
180 int a_start, a_end, na, na_start = -1;
185 alook->nmb = mtop->nmolblock;
187 snew(alook->mba, alook->nmb);
190 for (mb = 0; mb < mtop->nmolblock; mb++)
192 na = mtop->molblock[mb].nmol*mtop->molblock[mb].natoms_mol;
193 a_end = a_start + na;
195 alook->mba[mb].a_start = a_start;
196 alook->mba[mb].a_end = a_end;
197 alook->mba[mb].na_mol = mtop->molblock[mb].natoms_mol;
199 /* We start the binary search with the largest block */
200 if (mb == 0 || na > na_start)
202 alook->mb_start = mb;
212 gmx_mtop_atomlookup_t
213 gmx_mtop_atomlookup_settle_init(const gmx_mtop_t *mtop)
215 t_gmx_mtop_atomlookup *alook;
217 int na, na_start = -1;
219 alook = gmx_mtop_atomlookup_init(mtop);
221 /* Check if the starting molblock has settle */
222 if (mtop->moltype[mtop->molblock[alook->mb_start].type].ilist[F_SETTLE].nr == 0)
224 /* Search the largest molblock with settle */
225 alook->mb_start = -1;
226 for (mb = 0; mb < mtop->nmolblock; mb++)
228 if (mtop->moltype[mtop->molblock[mb].type].ilist[F_SETTLE].nr > 0)
230 na = alook->mba[mb].a_end - alook->mba[mb].a_start;
231 if (alook->mb_start == -1 || na > na_start)
233 alook->mb_start = mb;
239 if (alook->mb_start == -1)
241 gmx_incons("gmx_mtop_atomlookup_settle_init called without settles");
249 gmx_mtop_atomlookup_destroy(gmx_mtop_atomlookup_t alook)
255 void gmx_mtop_atomnr_to_atom(const gmx_mtop_atomlookup_t alook,
260 int a_start, atnr_mol;
263 if (atnr_global < 0 || atnr_global >= mtop->natoms)
265 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)",
266 atnr_global, 0, mtop->natoms-1);
272 mb = alook->mb_start;
276 a_start = alook->mba[mb].a_start;
277 if (atnr_global < a_start)
281 else if (atnr_global >= alook->mba[mb].a_end)
289 mb = ((mb0 + mb1 + 1)>>1);
292 atnr_mol = (atnr_global - a_start) % alook->mba[mb].na_mol;
294 *atom = &alook->mtop->moltype[alook->mtop->molblock[mb].type].atoms.atom[atnr_mol];
297 void gmx_mtop_atomnr_to_ilist(const gmx_mtop_atomlookup_t alook,
299 t_ilist **ilist_mol, int *atnr_offset)
302 int a_start, atnr_local;
305 if (atnr_global < 0 || atnr_global >= mtop->natoms)
307 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)",
308 atnr_global, 0, mtop->natoms-1);
314 mb = alook->mb_start;
318 a_start = alook->mba[mb].a_start;
319 if (atnr_global < a_start)
323 else if (atnr_global >= alook->mba[mb].a_end)
331 mb = ((mb0 + mb1 + 1)>>1);
334 *ilist_mol = alook->mtop->moltype[alook->mtop->molblock[mb].type].ilist;
336 atnr_local = (atnr_global - a_start) % alook->mba[mb].na_mol;
338 *atnr_offset = atnr_global - atnr_local;
341 void gmx_mtop_atomnr_to_molblock_ind(const gmx_mtop_atomlookup_t alook,
343 int *molb, int *molnr, int *atnr_mol)
349 if (atnr_global < 0 || atnr_global >= mtop->natoms)
351 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)",
352 atnr_global, 0, mtop->natoms-1);
358 mb = alook->mb_start;
362 a_start = alook->mba[mb].a_start;
363 if (atnr_global < a_start)
367 else if (atnr_global >= alook->mba[mb].a_end)
375 mb = ((mb0 + mb1 + 1)>>1);
379 *molnr = (atnr_global - a_start) / alook->mba[mb].na_mol;
380 *atnr_mol = atnr_global - a_start - (*molnr)*alook->mba[mb].na_mol;
383 void gmx_mtop_atominfo_global(const gmx_mtop_t *mtop, int atnr_global,
384 char **atomname, int *resnr, char **resname)
386 int mb, a_start, a_end, maxresnr, at_loc;
387 gmx_molblock_t *molb;
388 t_atoms *atoms = NULL;
390 if (atnr_global < 0 || atnr_global >= mtop->natoms)
392 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)",
393 atnr_global, 0, mtop->natoms-1);
398 maxresnr = mtop->maxresnr;
403 if (atoms->nres <= mtop->maxres_renum)
405 /* Single residue molecule, keep counting */
406 maxresnr += mtop->molblock[mb].nmol*atoms->nres;
410 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
412 a_end = a_start + mtop->molblock[mb].nmol*atoms->nr;
414 while (atnr_global >= a_end);
416 at_loc = (atnr_global - a_start) % atoms->nr;
417 *atomname = *(atoms->atomname[at_loc]);
418 if (atoms->nres > mtop->maxres_renum)
420 *resnr = atoms->resinfo[atoms->atom[at_loc].resind].nr;
424 /* Single residue molecule, keep counting */
425 *resnr = maxresnr + 1 + (atnr_global - a_start)/atoms->nr*atoms->nres + atoms->atom[at_loc].resind;
427 *resname = *(atoms->resinfo[atoms->atom[at_loc].resind].name);
430 typedef struct gmx_mtop_atomloop_all
432 const gmx_mtop_t *mtop;
439 } t_gmx_mtop_atomloop_all;
441 gmx_mtop_atomloop_all_t
442 gmx_mtop_atomloop_all_init(const gmx_mtop_t *mtop)
444 struct gmx_mtop_atomloop_all *aloop;
451 &mtop->moltype[mtop->molblock[aloop->mblock].type].atoms;
453 aloop->maxresnr = mtop->maxresnr;
454 aloop->at_local = -1;
455 aloop->at_global = -1;
460 static void gmx_mtop_atomloop_all_destroy(gmx_mtop_atomloop_all_t aloop)
465 gmx_bool gmx_mtop_atomloop_all_next(gmx_mtop_atomloop_all_t aloop,
466 int *at_global, t_atom **atom)
470 gmx_incons("gmx_mtop_atomloop_all_next called without calling gmx_mtop_atomloop_all_init");
476 if (aloop->at_local >= aloop->atoms->nr)
478 if (aloop->atoms->nres <= aloop->mtop->maxres_renum)
480 /* Single residue molecule, increase the count with one */
481 aloop->maxresnr += aloop->atoms->nres;
485 if (aloop->mol >= aloop->mtop->molblock[aloop->mblock].nmol)
488 if (aloop->mblock >= aloop->mtop->nmolblock)
490 gmx_mtop_atomloop_all_destroy(aloop);
493 aloop->atoms = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type].atoms;
498 *at_global = aloop->at_global;
499 *atom = &aloop->atoms->atom[aloop->at_local];
504 void gmx_mtop_atomloop_all_names(gmx_mtop_atomloop_all_t aloop,
505 char **atomname, int *resnr, char **resname)
509 *atomname = *(aloop->atoms->atomname[aloop->at_local]);
510 resind_mol = aloop->atoms->atom[aloop->at_local].resind;
511 *resnr = aloop->atoms->resinfo[resind_mol].nr;
512 if (aloop->atoms->nres <= aloop->mtop->maxres_renum)
514 *resnr = aloop->maxresnr + 1 + resind_mol;
516 *resname = *(aloop->atoms->resinfo[resind_mol].name);
519 void gmx_mtop_atomloop_all_moltype(gmx_mtop_atomloop_all_t aloop,
520 gmx_moltype_t **moltype, int *at_mol)
522 *moltype = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type];
523 *at_mol = aloop->at_local;
526 typedef struct gmx_mtop_atomloop_block
528 const gmx_mtop_t *mtop;
532 } t_gmx_mtop_atomloop_block;
534 gmx_mtop_atomloop_block_t
535 gmx_mtop_atomloop_block_init(const gmx_mtop_t *mtop)
537 struct gmx_mtop_atomloop_block *aloop;
543 aloop->atoms = &mtop->moltype[mtop->molblock[aloop->mblock].type].atoms;
544 aloop->at_local = -1;
549 static void gmx_mtop_atomloop_block_destroy(gmx_mtop_atomloop_block_t aloop)
554 gmx_bool gmx_mtop_atomloop_block_next(gmx_mtop_atomloop_block_t aloop,
555 t_atom **atom, int *nmol)
559 gmx_incons("gmx_mtop_atomloop_all_next called without calling gmx_mtop_atomloop_all_init");
564 if (aloop->at_local >= aloop->atoms->nr)
567 if (aloop->mblock >= aloop->mtop->nmolblock)
569 gmx_mtop_atomloop_block_destroy(aloop);
572 aloop->atoms = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type].atoms;
576 *atom = &aloop->atoms->atom[aloop->at_local];
577 *nmol = aloop->mtop->molblock[aloop->mblock].nmol;
582 typedef struct gmx_mtop_ilistloop
584 const gmx_mtop_t *mtop;
589 gmx_mtop_ilistloop_init(const gmx_mtop_t *mtop)
591 struct gmx_mtop_ilistloop *iloop;
601 static void gmx_mtop_ilistloop_destroy(gmx_mtop_ilistloop_t iloop)
606 gmx_bool gmx_mtop_ilistloop_next(gmx_mtop_ilistloop_t iloop,
607 t_ilist **ilist_mol, int *nmol)
611 gmx_incons("gmx_mtop_ilistloop_next called without calling gmx_mtop_ilistloop_init");
615 if (iloop->mblock == iloop->mtop->nmolblock)
617 gmx_mtop_ilistloop_destroy(iloop);
622 iloop->mtop->moltype[iloop->mtop->molblock[iloop->mblock].type].ilist;
624 *nmol = iloop->mtop->molblock[iloop->mblock].nmol;
628 typedef struct gmx_mtop_ilistloop_all
630 const gmx_mtop_t *mtop;
634 } t_gmx_mtop_ilist_all;
636 gmx_mtop_ilistloop_all_t
637 gmx_mtop_ilistloop_all_init(const gmx_mtop_t *mtop)
639 struct gmx_mtop_ilistloop_all *iloop;
651 static void gmx_mtop_ilistloop_all_destroy(gmx_mtop_ilistloop_all_t iloop)
656 gmx_bool gmx_mtop_ilistloop_all_next(gmx_mtop_ilistloop_all_t iloop,
657 t_ilist **ilist_mol, int *atnr_offset)
659 gmx_molblock_t *molb;
663 gmx_incons("gmx_mtop_ilistloop_all_next called without calling gmx_mtop_ilistloop_all_init");
668 iloop->a_offset += iloop->mtop->molblock[iloop->mblock].natoms_mol;
673 if (iloop->mol >= iloop->mtop->molblock[iloop->mblock].nmol)
677 if (iloop->mblock == iloop->mtop->nmolblock)
679 gmx_mtop_ilistloop_all_destroy(iloop);
685 iloop->mtop->moltype[iloop->mtop->molblock[iloop->mblock].type].ilist;
687 *atnr_offset = iloop->a_offset;
692 int gmx_mtop_ftype_count(const gmx_mtop_t *mtop, int ftype)
694 gmx_mtop_ilistloop_t iloop;
700 iloop = gmx_mtop_ilistloop_init(mtop);
701 while (gmx_mtop_ilistloop_next(iloop, &il, &nmol))
703 n += nmol*il[ftype].nr/(1+NRAL(ftype));
709 t_block gmx_mtop_global_cgs(const gmx_mtop_t *mtop)
711 t_block cgs_gl, *cgs_mol;
713 gmx_molblock_t *molb;
716 /* In most cases this is too much, but we realloc at the end */
717 snew(cgs_gl.index, mtop->natoms+1);
721 for (mb = 0; mb < mtop->nmolblock; mb++)
723 molb = &mtop->molblock[mb];
724 cgs_mol = &mtop->moltype[molb->type].cgs;
725 for (mol = 0; mol < molb->nmol; mol++)
727 for (cg = 0; cg < cgs_mol->nr; cg++)
729 cgs_gl.index[cgs_gl.nr+1] =
730 cgs_gl.index[cgs_gl.nr] +
731 cgs_mol->index[cg+1] - cgs_mol->index[cg];
736 cgs_gl.nalloc_index = cgs_gl.nr + 1;
737 srenew(cgs_gl.index, cgs_gl.nalloc_index);
742 static void atomcat(t_atoms *dest, t_atoms *src, int copies,
743 int maxres_renum, int *maxresnr)
747 int destnr = dest->nr;
751 size = destnr+copies*srcnr;
752 srenew(dest->atom, size);
753 srenew(dest->atomname, size);
754 srenew(dest->atomtype, size);
755 srenew(dest->atomtypeB, size);
759 size = dest->nres+copies*src->nres;
760 srenew(dest->resinfo, size);
763 /* residue information */
764 for (l = dest->nres, j = 0; (j < copies); j++, l += src->nres)
766 memcpy((char *) &(dest->resinfo[l]), (char *) &(src->resinfo[0]),
767 (size_t)(src->nres*sizeof(src->resinfo[0])));
770 for (l = destnr, j = 0; (j < copies); j++, l += srcnr)
772 memcpy((char *) &(dest->atomname[l]), (char *) &(src->atomname[0]),
773 (size_t)(srcnr*sizeof(src->atomname[0])));
774 memcpy((char *) &(dest->atomtype[l]), (char *) &(src->atomtype[0]),
775 (size_t)(srcnr*sizeof(src->atomtype[0])));
776 memcpy((char *) &(dest->atomtypeB[l]), (char *) &(src->atomtypeB[0]),
777 (size_t)(srcnr*sizeof(src->atomtypeB[0])));
778 memcpy((char *) &(dest->atom[l]), (char *) &(src->atom[0]),
779 (size_t)(srcnr*sizeof(src->atom[0])));
782 /* Increment residue indices */
783 for (l = destnr, j = 0; (j < copies); j++)
785 for (i = 0; (i < srcnr); i++, l++)
787 dest->atom[l].resind = dest->nres+j*src->nres+src->atom[i].resind;
791 if (src->nres <= maxres_renum)
793 /* Single residue molecule, continue counting residues */
794 for (j = 0; (j < copies); j++)
796 for (l = 0; l < src->nres; l++)
799 dest->resinfo[dest->nres+j*src->nres+l].nr = *maxresnr;
804 dest->nres += copies*src->nres;
805 dest->nr += copies*src->nr;
808 t_atoms gmx_mtop_global_atoms(const gmx_mtop_t *mtop)
812 gmx_molblock_t *molb;
814 init_t_atoms(&atoms, 0, FALSE);
816 maxresnr = mtop->maxresnr;
817 for (mb = 0; mb < mtop->nmolblock; mb++)
819 molb = &mtop->molblock[mb];
820 atomcat(&atoms, &mtop->moltype[molb->type].atoms, molb->nmol,
821 mtop->maxres_renum, &maxresnr);
827 void gmx_mtop_make_atomic_charge_groups(gmx_mtop_t *mtop,
828 gmx_bool bKeepSingleMolCG)
833 for (mb = 0; mb < mtop->nmolblock; mb++)
835 cgs_mol = &mtop->moltype[mtop->molblock[mb].type].cgs;
836 if (!(bKeepSingleMolCG && cgs_mol->nr == 1))
838 cgs_mol->nr = mtop->molblock[mb].natoms_mol;
839 cgs_mol->nalloc_index = cgs_mol->nr + 1;
840 srenew(cgs_mol->index, cgs_mol->nalloc_index);
841 for (cg = 0; cg < cgs_mol->nr+1; cg++)
843 cgs_mol->index[cg] = cg;
850 * The cat routines below are old code from src/kernel/topcat.c
853 static void blockcat(t_block *dest, t_block *src, int copies)
855 int i, j, l, nra, size;
859 size = (dest->nr+copies*src->nr+1);
860 srenew(dest->index, size);
863 nra = dest->index[dest->nr];
864 for (l = dest->nr, j = 0; (j < copies); j++)
866 for (i = 0; (i < src->nr); i++)
868 dest->index[l++] = nra + src->index[i];
870 nra += src->index[src->nr];
872 dest->nr += copies*src->nr;
873 dest->index[dest->nr] = nra;
876 static void blockacat(t_blocka *dest, t_blocka *src, int copies,
880 int destnr = dest->nr;
881 int destnra = dest->nra;
885 size = (dest->nr+copies*src->nr+1);
886 srenew(dest->index, size);
890 size = (dest->nra+copies*src->nra);
891 srenew(dest->a, size);
894 for (l = destnr, j = 0; (j < copies); j++)
896 for (i = 0; (i < src->nr); i++)
898 dest->index[l++] = dest->nra+src->index[i];
900 dest->nra += src->nra;
902 for (l = destnra, j = 0; (j < copies); j++)
904 for (i = 0; (i < src->nra); i++)
906 dest->a[l++] = dnum+src->a[i];
911 dest->index[dest->nr] = dest->nra;
914 static void ilistcat(int ftype, t_ilist *dest, t_ilist *src, int copies,
921 dest->nalloc = dest->nr + copies*src->nr;
922 srenew(dest->iatoms, dest->nalloc);
924 for (c = 0; c < copies; c++)
926 for (i = 0; i < src->nr; )
928 dest->iatoms[dest->nr++] = src->iatoms[i++];
929 for (a = 0; a < nral; a++)
931 dest->iatoms[dest->nr++] = dnum + src->iatoms[i++];
938 static void set_posres_params(t_idef *idef, gmx_molblock_t *molb,
939 int i0, int a_offset)
945 il = &idef->il[F_POSRES];
947 idef->iparams_posres_nalloc = i1;
948 srenew(idef->iparams_posres, idef->iparams_posres_nalloc);
949 for (i = i0; i < i1; i++)
951 ip = &idef->iparams_posres[i];
952 /* Copy the force constants */
953 *ip = idef->iparams[il->iatoms[i*2]];
954 a_molb = il->iatoms[i*2+1] - a_offset;
955 if (molb->nposres_xA == 0)
957 gmx_incons("Position restraint coordinates are missing");
959 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
960 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
961 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
962 if (molb->nposres_xB > 0)
964 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
965 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
966 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
970 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
971 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
972 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
974 /* Set the parameter index for idef->iparams_posre */
979 static void set_fbposres_params(t_idef *idef, gmx_molblock_t *molb,
980 int i0, int a_offset)
986 il = &idef->il[F_FBPOSRES];
988 idef->iparams_fbposres_nalloc = i1;
989 srenew(idef->iparams_fbposres, idef->iparams_fbposres_nalloc);
990 for (i = i0; i < i1; i++)
992 ip = &idef->iparams_fbposres[i];
993 /* Copy the force constants */
994 *ip = idef->iparams[il->iatoms[i*2]];
995 a_molb = il->iatoms[i*2+1] - a_offset;
996 if (molb->nposres_xA == 0)
998 gmx_incons("Position restraint coordinates are missing");
1000 /* Take flat-bottom posres reference from normal position restraints */
1001 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
1002 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
1003 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
1004 /* Note: no B-type for flat-bottom posres */
1006 /* Set the parameter index for idef->iparams_posre */
1007 il->iatoms[i*2] = i;
1011 static void gen_local_top(const gmx_mtop_t *mtop, const t_inputrec *ir,
1012 gmx_bool bMergeConstr,
1013 gmx_localtop_t *top)
1015 int mb, srcnr, destnr, ftype, ftype_dest, mt, natoms, mol, nposre_old, nfbposre_old;
1016 gmx_molblock_t *molb;
1017 gmx_moltype_t *molt;
1018 const gmx_ffparams_t *ffp;
1021 gmx_mtop_atomloop_all_t aloop;
1025 top->atomtypes = mtop->atomtypes;
1027 ffp = &mtop->ffparams;
1030 idef->ntypes = ffp->ntypes;
1031 idef->atnr = ffp->atnr;
1032 idef->functype = ffp->functype;
1033 idef->iparams = ffp->iparams;
1034 idef->iparams_posres = NULL;
1035 idef->iparams_posres_nalloc = 0;
1036 idef->iparams_fbposres = NULL;
1037 idef->iparams_fbposres_nalloc = 0;
1038 idef->fudgeQQ = ffp->fudgeQQ;
1039 idef->cmap_grid = ffp->cmap_grid;
1040 idef->ilsort = ilsortUNKNOWN;
1042 init_block(&top->cgs);
1043 init_blocka(&top->excls);
1044 for (ftype = 0; ftype < F_NRE; ftype++)
1046 idef->il[ftype].nr = 0;
1047 idef->il[ftype].nalloc = 0;
1048 idef->il[ftype].iatoms = NULL;
1052 for (mb = 0; mb < mtop->nmolblock; mb++)
1054 molb = &mtop->molblock[mb];
1055 molt = &mtop->moltype[molb->type];
1057 srcnr = molt->atoms.nr;
1060 blockcat(&top->cgs, &molt->cgs, molb->nmol);
1062 blockacat(&top->excls, &molt->excls, molb->nmol, destnr, srcnr);
1064 nposre_old = idef->il[F_POSRES].nr;
1065 nfbposre_old = idef->il[F_FBPOSRES].nr;
1066 for (ftype = 0; ftype < F_NRE; ftype++)
1069 ftype == F_CONSTR && molt->ilist[F_CONSTRNC].nr > 0)
1071 /* Merge all constrains into one ilist.
1072 * This simplifies the constraint code.
1074 for (mol = 0; mol < molb->nmol; mol++)
1076 ilistcat(ftype, &idef->il[F_CONSTR], &molt->ilist[F_CONSTR],
1077 1, destnr+mol*srcnr, srcnr);
1078 ilistcat(ftype, &idef->il[F_CONSTR], &molt->ilist[F_CONSTRNC],
1079 1, destnr+mol*srcnr, srcnr);
1082 else if (!(bMergeConstr && ftype == F_CONSTRNC))
1084 ilistcat(ftype, &idef->il[ftype], &molt->ilist[ftype],
1085 molb->nmol, destnr, srcnr);
1088 if (idef->il[F_POSRES].nr > nposre_old)
1090 /* Executing this line line stops gmxdump -sys working
1091 * correctly. I'm not aware there's an elegant fix. */
1092 set_posres_params(idef, molb, nposre_old/2, natoms);
1094 if (idef->il[F_FBPOSRES].nr > nfbposre_old)
1096 set_fbposres_params(idef, molb, nfbposre_old/2, natoms);
1099 natoms += molb->nmol*srcnr;
1104 top->idef.ilsort = ilsortUNKNOWN;
1108 if (ir->efep != efepNO && gmx_mtop_bondeds_free_energy(mtop))
1110 snew(qA, mtop->natoms);
1111 snew(qB, mtop->natoms);
1112 aloop = gmx_mtop_atomloop_all_init(mtop);
1113 while (gmx_mtop_atomloop_all_next(aloop, &ag, &atom))
1118 gmx_sort_ilist_fe(&top->idef, qA, qB);
1124 top->idef.ilsort = ilsortNO_FE;
1129 gmx_localtop_t *gmx_mtop_generate_local_top(const gmx_mtop_t *mtop,
1130 const t_inputrec *ir)
1132 gmx_localtop_t *top;
1136 gen_local_top(mtop, ir, TRUE, top);
1141 t_topology gmx_mtop_t_to_t_topology(gmx_mtop_t *mtop)
1144 gmx_localtop_t ltop;
1147 gen_local_top(mtop, NULL, FALSE, <op);
1149 top.name = mtop->name;
1150 top.idef = ltop.idef;
1151 top.atomtypes = ltop.atomtypes;
1153 top.excls = ltop.excls;
1154 top.atoms = gmx_mtop_global_atoms(mtop);
1155 top.mols = mtop->mols;
1156 top.symtab = mtop->symtab;
1158 /* We only need to free the moltype and molblock data,
1159 * all other pointers have been copied to top.
1161 * Well, except for the group data, but we can't free those, because they
1162 * are used somewhere even after a call to this function.
1164 for (mt = 0; mt < mtop->nmoltype; mt++)
1166 done_moltype(&mtop->moltype[mt]);
1168 sfree(mtop->moltype);
1170 for (mb = 0; mb < mtop->nmolblock; mb++)
1172 done_molblock(&mtop->molblock[mb]);
1174 sfree(mtop->molblock);