2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2008,2009,2010, The GROMACS development team.
5 * Copyright (c) 2012,2013,2014,2015,2016 The GROMACS development team.
6 * Copyright (c) 2017,2018,2019,2020,2021, 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.
39 #include "mtop_util.h"
47 #include "gromacs/math/vectypes.h"
48 #include "gromacs/topology/atoms.h"
49 #include "gromacs/topology/block.h"
50 #include "gromacs/topology/exclusionblocks.h"
51 #include "gromacs/topology/idef.h"
52 #include "gromacs/topology/ifunc.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/topology/topsort.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/real.h"
58 #include "gromacs/utility/smalloc.h"
60 void gmx_mtop_count_atomtypes(const gmx_mtop_t& mtop, int state, int typecount[])
62 for (int i = 0; i < mtop.ffparams.atnr; ++i)
66 for (const gmx_molblock_t& molb : mtop.molblock)
68 const t_atoms& atoms = mtop.moltype[molb.type].atoms;
69 for (int i = 0; i < atoms.nr; ++i)
71 const int tpi = (state == 0) ? atoms.atom[i].type : atoms.atom[i].typeB;
72 typecount[tpi] += molb.nmol;
77 int gmx_mtop_num_molecules(const gmx_mtop_t& mtop)
80 for (const gmx_molblock_t& molb : mtop.molblock)
82 numMolecules += molb.nmol;
87 int gmx_mtop_nres(const gmx_mtop_t& mtop)
90 for (const gmx_molblock_t& molb : mtop.molblock)
92 nres += molb.nmol * mtop.moltype[molb.type].atoms.nres;
97 AtomIterator::AtomIterator(const gmx_mtop_t& mtop, int globalAtomNumber) :
100 atoms_(&mtop.moltype[mtop.molblock[0].type].atoms),
102 highestResidueNumber_(mtop.maxResNumberNotRenumbered()),
104 globalAtomNumber_(globalAtomNumber)
106 GMX_ASSERT(globalAtomNumber == 0 || globalAtomNumber == mtop.natoms,
107 "Starting at other atoms not implemented yet");
110 AtomIterator& AtomIterator::operator++()
115 if (localAtomNumber_ >= atoms_->nr)
117 if (atoms_->nres <= mtop_->maxResiduesPerMoleculeToTriggerRenumber())
119 /* Single residue molecule, increase the count with one */
120 highestResidueNumber_ += atoms_->nres;
123 localAtomNumber_ = 0;
124 if (currentMolecule_ >= mtop_->molblock[mblock_].nmol)
127 if (mblock_ >= mtop_->molblock.size())
131 atoms_ = &mtop_->moltype[mtop_->molblock[mblock_].type].atoms;
132 currentMolecule_ = 0;
138 bool AtomIterator::operator==(const AtomIterator& o) const
140 return mtop_ == o.mtop_ && globalAtomNumber_ == o.globalAtomNumber_;
143 const t_atom& AtomProxy::atom() const
145 return it_->atoms_->atom[it_->localAtomNumber_];
148 int AtomProxy::globalAtomNumber() const
150 return it_->globalAtomNumber_;
153 const char* AtomProxy::atomName() const
155 return *(it_->atoms_->atomname[it_->localAtomNumber_]);
158 const char* AtomProxy::residueName() const
160 int residueIndexInMolecule = it_->atoms_->atom[it_->localAtomNumber_].resind;
161 return *(it_->atoms_->resinfo[residueIndexInMolecule].name);
164 int AtomProxy::residueNumber() const
166 int residueIndexInMolecule = it_->atoms_->atom[it_->localAtomNumber_].resind;
167 if (it_->atoms_->nres <= it_->mtop_->maxResiduesPerMoleculeToTriggerRenumber())
169 return it_->highestResidueNumber_ + 1 + residueIndexInMolecule;
173 return it_->atoms_->resinfo[residueIndexInMolecule].nr;
177 const gmx_moltype_t& AtomProxy::moleculeType() const
179 return it_->mtop_->moltype[it_->mtop_->molblock[it_->mblock_].type];
182 int AtomProxy::atomNumberInMol() const
184 return it_->localAtomNumber_;
187 struct gmx_mtop_atomloop_block
189 const gmx_mtop_t* mtop;
191 const t_atoms* atoms;
195 gmx_mtop_atomloop_block_t gmx_mtop_atomloop_block_init(const gmx_mtop_t& mtop)
197 struct gmx_mtop_atomloop_block* aloop = nullptr;
203 aloop->atoms = &mtop.moltype[mtop.molblock[aloop->mblock].type].atoms;
204 aloop->at_local = -1;
209 static void gmx_mtop_atomloop_block_destroy(gmx_mtop_atomloop_block_t aloop)
214 gmx_bool gmx_mtop_atomloop_block_next(gmx_mtop_atomloop_block_t aloop, const t_atom** atom, int* nmol)
216 if (aloop == nullptr)
218 gmx_incons("gmx_mtop_atomloop_all_next called without calling gmx_mtop_atomloop_all_init");
223 if (aloop->at_local >= aloop->atoms->nr)
226 if (aloop->mblock >= aloop->mtop->molblock.size())
228 gmx_mtop_atomloop_block_destroy(aloop);
231 aloop->atoms = &aloop->mtop->moltype[aloop->mtop->molblock[aloop->mblock].type].atoms;
235 *atom = &aloop->atoms->atom[aloop->at_local];
236 *nmol = aloop->mtop->molblock[aloop->mblock].nmol;
241 typedef struct gmx_mtop_ilistloop
243 const gmx_mtop_t* mtop;
247 gmx_mtop_ilistloop_t gmx_mtop_ilistloop_init(const gmx_mtop_t& mtop)
249 struct gmx_mtop_ilistloop* iloop = nullptr;
259 static void gmx_mtop_ilistloop_destroy(gmx_mtop_ilistloop_t iloop)
264 const InteractionLists* gmx_mtop_ilistloop_next(gmx_mtop_ilistloop_t iloop, int* nmol)
266 if (iloop == nullptr)
268 gmx_incons("gmx_mtop_ilistloop_next called without calling gmx_mtop_ilistloop_init");
272 if (iloop->mblock >= gmx::ssize(iloop->mtop->molblock))
274 if (iloop->mblock == gmx::ssize(iloop->mtop->molblock) && iloop->mtop->bIntermolecularInteractions)
277 return iloop->mtop->intermolecular_ilist.get();
280 gmx_mtop_ilistloop_destroy(iloop);
284 *nmol = iloop->mtop->molblock[iloop->mblock].nmol;
286 return &iloop->mtop->moltype[iloop->mtop->molblock[iloop->mblock].type].ilist;
288 typedef struct gmx_mtop_ilistloop_all
290 const gmx_mtop_t* mtop;
294 } t_gmx_mtop_ilist_all;
296 int gmx_mtop_ftype_count(const gmx_mtop_t& mtop, int ftype)
301 gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop);
302 while (const InteractionLists* il = gmx_mtop_ilistloop_next(iloop, &nmol))
304 n += nmol * (*il)[ftype].size() / (1 + NRAL(ftype));
307 if (mtop.bIntermolecularInteractions)
309 n += (*mtop.intermolecular_ilist)[ftype].size() / (1 + NRAL(ftype));
315 int gmx_mtop_interaction_count(const gmx_mtop_t& mtop, const int unsigned if_flags)
319 gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop);
321 while (const InteractionLists* il = gmx_mtop_ilistloop_next(iloop, &nmol))
323 for (int ftype = 0; ftype < F_NRE; ftype++)
325 if ((interaction_function[ftype].flags & if_flags) == if_flags)
327 n += nmol * (*il)[ftype].size() / (1 + NRAL(ftype));
332 if (mtop.bIntermolecularInteractions)
334 for (int ftype = 0; ftype < F_NRE; ftype++)
336 if ((interaction_function[ftype].flags & if_flags) == if_flags)
338 n += (*mtop.intermolecular_ilist)[ftype].size() / (1 + NRAL(ftype));
346 gmx::EnumerationArray<ParticleType, int> gmx_mtop_particletype_count(const gmx_mtop_t& mtop)
348 gmx::EnumerationArray<ParticleType, int> count = { { 0 } };
350 for (const auto& molblock : mtop.molblock)
352 const t_atoms& atoms = mtop.moltype[molblock.type].atoms;
353 for (int a = 0; a < atoms.nr; a++)
355 count[atoms.atom[a].ptype] += molblock.nmol;
362 static void atomcat(t_atoms* dest, const t_atoms* src, int copies, int maxres_renum, int* maxresnr)
364 int i = 0, j = 0, l = 0, size = 0;
366 int destnr = dest->nr;
370 dest->haveMass = src->haveMass;
371 dest->haveType = src->haveType;
372 dest->haveCharge = src->haveCharge;
373 dest->haveBState = src->haveBState;
374 dest->havePdbInfo = src->havePdbInfo;
378 dest->haveMass = dest->haveMass && src->haveMass;
379 dest->haveType = dest->haveType && src->haveType;
380 dest->haveCharge = dest->haveCharge && src->haveCharge;
381 dest->haveBState = dest->haveBState && src->haveBState;
382 dest->havePdbInfo = dest->havePdbInfo && src->havePdbInfo;
387 size = destnr + copies * srcnr;
388 srenew(dest->atom, size);
389 srenew(dest->atomname, size);
392 srenew(dest->atomtype, size);
393 if (dest->haveBState)
395 srenew(dest->atomtypeB, size);
398 if (dest->havePdbInfo)
400 srenew(dest->pdbinfo, size);
405 size = dest->nres + copies * src->nres;
406 srenew(dest->resinfo, size);
409 /* residue information */
410 for (l = dest->nres, j = 0; (j < copies); j++, l += src->nres)
412 memcpy(reinterpret_cast<char*>(&(dest->resinfo[l])),
413 reinterpret_cast<char*>(&(src->resinfo[0])),
414 static_cast<size_t>(src->nres * sizeof(src->resinfo[0])));
417 for (l = destnr, j = 0; (j < copies); j++, l += srcnr)
419 memcpy(reinterpret_cast<char*>(&(dest->atom[l])),
420 reinterpret_cast<char*>(&(src->atom[0])),
421 static_cast<size_t>(srcnr * sizeof(src->atom[0])));
422 memcpy(reinterpret_cast<char*>(&(dest->atomname[l])),
423 reinterpret_cast<char*>(&(src->atomname[0])),
424 static_cast<size_t>(srcnr * sizeof(src->atomname[0])));
427 memcpy(reinterpret_cast<char*>(&(dest->atomtype[l])),
428 reinterpret_cast<char*>(&(src->atomtype[0])),
429 static_cast<size_t>(srcnr * sizeof(src->atomtype[0])));
430 if (dest->haveBState)
432 memcpy(reinterpret_cast<char*>(&(dest->atomtypeB[l])),
433 reinterpret_cast<char*>(&(src->atomtypeB[0])),
434 static_cast<size_t>(srcnr * sizeof(src->atomtypeB[0])));
437 if (dest->havePdbInfo)
439 memcpy(reinterpret_cast<char*>(&(dest->pdbinfo[l])),
440 reinterpret_cast<char*>(&(src->pdbinfo[0])),
441 static_cast<size_t>(srcnr * sizeof(src->pdbinfo[0])));
445 /* Increment residue indices */
446 for (l = destnr, j = 0; (j < copies); j++)
448 for (i = 0; (i < srcnr); i++, l++)
450 dest->atom[l].resind = dest->nres + j * src->nres + src->atom[i].resind;
454 if (src->nres <= maxres_renum)
456 /* Single residue molecule, continue counting residues */
457 for (j = 0; (j < copies); j++)
459 for (l = 0; l < src->nres; l++)
462 dest->resinfo[dest->nres + j * src->nres + l].nr = *maxresnr;
467 dest->nres += copies * src->nres;
468 dest->nr += copies * src->nr;
471 t_atoms gmx_mtop_global_atoms(const gmx_mtop_t& mtop)
475 init_t_atoms(&atoms, 0, FALSE);
477 int maxresnr = mtop.maxResNumberNotRenumbered();
478 for (const gmx_molblock_t& molb : mtop.molblock)
481 &mtop.moltype[molb.type].atoms,
483 mtop.maxResiduesPerMoleculeToTriggerRenumber(),
491 * The cat routines below are old code from src/kernel/topcat.c
494 static void ilistcat(int ftype, InteractionList* dest, const InteractionList& src, int copies, int dnum, int snum)
496 const int nral = NRAL(ftype);
498 size_t destIndex = dest->iatoms.size();
499 dest->iatoms.resize(dest->iatoms.size() + copies * src.size());
501 for (int c = 0; c < copies; c++)
503 for (int i = 0; i < src.size();)
505 dest->iatoms[destIndex++] = src.iatoms[i++];
506 for (int a = 0; a < nral; a++)
508 dest->iatoms[destIndex++] = dnum + src.iatoms[i++];
515 static void ilistcat(int ftype, t_ilist* dest, const InteractionList& src, int copies, int dnum, int snum)
517 const int nral = NRAL(ftype);
519 dest->nalloc = dest->nr + copies * src.size();
520 srenew(dest->iatoms, dest->nalloc);
522 for (int c = 0; c < copies; c++)
524 for (int i = 0; i < src.size();)
526 dest->iatoms[dest->nr++] = src.iatoms[i++];
527 for (int a = 0; a < nral; a++)
529 dest->iatoms[dest->nr++] = dnum + src.iatoms[i++];
536 static const t_iparams& getIparams(const InteractionDefinitions& idef, const int index)
538 return idef.iparams[index];
541 static const t_iparams& getIparams(const t_idef& idef, const int index)
543 return idef.iparams[index];
546 static void resizeIParams(std::vector<t_iparams>* iparams, const int newSize)
548 iparams->resize(newSize);
551 static void resizeIParams(t_iparams** iparams, const int newSize)
553 srenew(*iparams, newSize);
556 template<typename IdefType>
557 static void set_posres_params(IdefType* idef, const gmx_molblock_t* molb, int i0, int a_offset)
559 auto* il = &idef->il[F_POSRES];
560 int i1 = il->size() / 2;
561 resizeIParams(&idef->iparams_posres, i1);
562 for (int i = i0; i < i1; i++)
564 t_iparams* ip = &idef->iparams_posres[i];
565 /* Copy the force constants */
566 *ip = getIparams(*idef, il->iatoms[i * 2]);
567 int a_molb = il->iatoms[i * 2 + 1] - a_offset;
568 if (molb->posres_xA.empty())
570 gmx_incons("Position restraint coordinates are missing");
572 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
573 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
574 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
575 if (!molb->posres_xB.empty())
577 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
578 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
579 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
583 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
584 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
585 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
587 /* Set the parameter index for idef->iparams_posre */
588 il->iatoms[i * 2] = i;
592 template<typename IdefType>
593 static void set_fbposres_params(IdefType* idef, const gmx_molblock_t* molb, int i0, int a_offset)
595 auto* il = &idef->il[F_FBPOSRES];
596 int i1 = il->size() / 2;
597 resizeIParams(&idef->iparams_fbposres, i1);
598 for (int i = i0; i < i1; i++)
600 t_iparams* ip = &idef->iparams_fbposres[i];
601 /* Copy the force constants */
602 *ip = getIparams(*idef, il->iatoms[i * 2]);
603 int a_molb = il->iatoms[i * 2 + 1] - a_offset;
604 if (molb->posres_xA.empty())
606 gmx_incons("Position restraint coordinates are missing");
608 /* Take flat-bottom posres reference from normal position restraints */
609 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
610 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
611 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
612 /* Note: no B-type for flat-bottom posres */
614 /* Set the parameter index for idef->iparams_posre */
615 il->iatoms[i * 2] = i;
619 /*! \brief Copy parameters to idef structure from mtop.
621 * Makes a deep copy of the force field parameters data structure from a gmx_mtop_t.
622 * Used to initialize legacy topology types.
624 * \param[in] mtop Reference to input mtop.
625 * \param[in] idef Pointer to idef to populate.
627 static void copyFFParametersFromMtop(const gmx_mtop_t& mtop, t_idef* idef)
629 const gmx_ffparams_t* ffp = &mtop.ffparams;
631 idef->ntypes = ffp->numTypes();
632 idef->atnr = ffp->atnr;
633 /* we can no longer copy the pointers to the mtop members,
634 * because they will become invalid as soon as mtop gets free'd.
635 * We also need to make sure to only operate on valid data!
638 if (!ffp->functype.empty())
640 snew(idef->functype, ffp->functype.size());
641 std::copy(ffp->functype.data(), ffp->functype.data() + ffp->functype.size(), idef->functype);
645 idef->functype = nullptr;
647 if (!ffp->iparams.empty())
649 snew(idef->iparams, ffp->iparams.size());
650 std::copy(ffp->iparams.data(), ffp->iparams.data() + ffp->iparams.size(), idef->iparams);
654 idef->iparams = nullptr;
656 idef->iparams_posres = nullptr;
657 idef->iparams_fbposres = nullptr;
658 idef->fudgeQQ = ffp->fudgeQQ;
659 idef->ilsort = ilsortUNKNOWN;
662 /*! \brief Copy idef structure from mtop.
664 * Makes a deep copy of an idef data structure from a gmx_mtop_t.
665 * Used to initialize legacy topology types.
667 * \param[in] mtop Reference to input mtop.
668 * \param[in] idef Pointer to idef to populate.
669 * \param[in] mergeConstr Decide if constraints will be merged.
671 template<typename IdefType>
672 static void copyIListsFromMtop(const gmx_mtop_t& mtop, IdefType* idef, bool mergeConstr)
675 for (const gmx_molblock_t& molb : mtop.molblock)
677 const gmx_moltype_t& molt = mtop.moltype[molb.type];
679 int srcnr = molt.atoms.nr;
682 int nposre_old = idef->il[F_POSRES].size();
683 int nfbposre_old = idef->il[F_FBPOSRES].size();
684 for (int ftype = 0; ftype < F_NRE; ftype++)
686 if (mergeConstr && ftype == F_CONSTR && !molt.ilist[F_CONSTRNC].empty())
688 /* Merge all constrains into one ilist.
689 * This simplifies the constraint code.
691 for (int mol = 0; mol < molb.nmol; mol++)
693 ilistcat(ftype, &idef->il[F_CONSTR], molt.ilist[F_CONSTR], 1, destnr + mol * srcnr, srcnr);
694 ilistcat(ftype, &idef->il[F_CONSTR], molt.ilist[F_CONSTRNC], 1, destnr + mol * srcnr, srcnr);
697 else if (!(mergeConstr && ftype == F_CONSTRNC))
699 ilistcat(ftype, &idef->il[ftype], molt.ilist[ftype], molb.nmol, destnr, srcnr);
702 if (idef->il[F_POSRES].size() > nposre_old)
704 /* Executing this line line stops gmxdump -sys working
705 * correctly. I'm not aware there's an elegant fix. */
706 set_posres_params(idef, &molb, nposre_old / 2, natoms);
708 if (idef->il[F_FBPOSRES].size() > nfbposre_old)
710 set_fbposres_params(idef, &molb, nfbposre_old / 2, natoms);
713 natoms += molb.nmol * srcnr;
716 if (mtop.bIntermolecularInteractions)
718 for (int ftype = 0; ftype < F_NRE; ftype++)
720 ilistcat(ftype, &idef->il[ftype], (*mtop.intermolecular_ilist)[ftype], 1, 0, mtop.natoms);
724 // We have not (yet) sorted free-energy interactions to the end of the ilists
725 idef->ilsort = ilsortNO_FE;
728 /*! \brief Copy atomtypes from mtop
730 * Makes a deep copy of t_atomtypes from gmx_mtop_t.
731 * Used to initialize legacy topology types.
733 * \param[in] mtop Reference to input mtop.
734 * \param[in] atomtypes Pointer to atomtypes to populate.
736 static void copyAtomtypesFromMtop(const gmx_mtop_t& mtop, t_atomtypes* atomtypes)
738 atomtypes->nr = mtop.atomtypes.nr;
739 if (mtop.atomtypes.atomnumber)
741 snew(atomtypes->atomnumber, mtop.atomtypes.nr);
742 std::copy(mtop.atomtypes.atomnumber,
743 mtop.atomtypes.atomnumber + mtop.atomtypes.nr,
744 atomtypes->atomnumber);
748 atomtypes->atomnumber = nullptr;
752 /*! \brief Generate a single list of lists of exclusions for the whole system
754 * \param[in] mtop Reference to input mtop.
756 static gmx::ListOfLists<int> globalExclusionLists(const gmx_mtop_t& mtop)
758 gmx::ListOfLists<int> excls;
761 for (const gmx_molblock_t& molb : mtop.molblock)
763 const gmx_moltype_t& molt = mtop.moltype[molb.type];
765 for (int mol = 0; mol < molb.nmol; mol++)
767 excls.appendListOfLists(molt.excls, atomIndex);
769 atomIndex += molt.atoms.nr;
776 /*! \brief Updates inter-molecular exclusion lists
778 * This function updates inter-molecular exclusions to exclude all
779 * non-bonded interactions between a given list of atoms
781 * \param[inout] excls existing exclusions in local topology
782 * \param[in] ids list of global IDs of atoms
784 static void addMimicExclusions(gmx::ListOfLists<int>* excls, const gmx::ArrayRef<const int> ids)
786 t_blocka inter_excl{};
787 init_blocka(&inter_excl);
788 size_t n_q = ids.size();
790 inter_excl.nr = excls->ssize();
791 inter_excl.nra = n_q * n_q;
793 size_t total_nra = n_q * n_q;
795 snew(inter_excl.index, excls->ssize() + 1);
796 snew(inter_excl.a, total_nra);
798 for (int i = 0; i < inter_excl.nr; ++i)
800 inter_excl.index[i] = 0;
803 /* Here we loop over the list of QM atom ids
804 * and create exclusions between all of them resulting in
805 * n_q * n_q sized exclusion list
808 for (int k = 0; k < inter_excl.nr; ++k)
810 inter_excl.index[k] = prev_index;
811 for (long i = 0; i < ids.ssize(); i++)
817 size_t index = n_q * i;
818 inter_excl.index[ids[i]] = index;
819 prev_index = index + n_q;
820 for (size_t j = 0; j < n_q; ++j)
822 inter_excl.a[n_q * i + j] = ids[j];
826 inter_excl.index[ids[n_q - 1] + 1] = n_q * n_q;
828 inter_excl.index[inter_excl.nr] = n_q * n_q;
830 std::vector<gmx::ExclusionBlock> qmexcl2(excls->size());
831 gmx::blockaToExclusionBlocks(&inter_excl, qmexcl2);
833 // Merge the created exclusion list with the existing one
834 gmx::mergeExclusions(excls, qmexcl2);
837 static void sortFreeEnergyInteractionsAtEnd(const gmx_mtop_t& mtop, InteractionDefinitions* idef)
839 std::vector<real> qA(mtop.natoms);
840 std::vector<real> qB(mtop.natoms);
841 for (const AtomProxy atomP : AtomRange(mtop))
843 const t_atom& local = atomP.atom();
844 int index = atomP.globalAtomNumber();
846 qB[index] = local.qB;
848 gmx_sort_ilist_fe(idef, qA.data(), qB.data());
851 static void gen_local_top(const gmx_mtop_t& mtop,
852 bool freeEnergyInteractionsAtEnd,
856 copyIListsFromMtop(mtop, &top->idef, bMergeConstr);
857 if (freeEnergyInteractionsAtEnd)
859 sortFreeEnergyInteractionsAtEnd(mtop, &top->idef);
861 top->excls = globalExclusionLists(mtop);
862 if (!mtop.intermolecularExclusionGroup.empty())
864 addMimicExclusions(&top->excls, mtop.intermolecularExclusionGroup);
868 void gmx_mtop_generate_local_top(const gmx_mtop_t& mtop, gmx_localtop_t* top, bool freeEnergyInteractionsAtEnd)
870 gen_local_top(mtop, freeEnergyInteractionsAtEnd, true, top);
873 /*! \brief Fills an array with molecule begin/end atom indices
875 * \param[in] mtop The global topology
876 * \param[out] index Array of size nr. of molecules + 1 to be filled with molecule begin/end indices
878 static void fillMoleculeIndices(const gmx_mtop_t& mtop, gmx::ArrayRef<int> index)
880 int globalAtomIndex = 0;
881 int globalMolIndex = 0;
882 index[globalMolIndex] = globalAtomIndex;
883 for (const gmx_molblock_t& molb : mtop.molblock)
885 int numAtomsPerMolecule = mtop.moltype[molb.type].atoms.nr;
886 for (int mol = 0; mol < molb.nmol; mol++)
888 globalAtomIndex += numAtomsPerMolecule;
890 index[globalMolIndex] = globalAtomIndex;
895 gmx::RangePartitioning gmx_mtop_molecules(const gmx_mtop_t& mtop)
897 gmx::RangePartitioning mols;
899 for (const gmx_molblock_t& molb : mtop.molblock)
901 int numAtomsPerMolecule = mtop.moltype[molb.type].atoms.nr;
902 for (int mol = 0; mol < molb.nmol; mol++)
904 mols.appendBlock(numAtomsPerMolecule);
911 std::vector<gmx::Range<int>> atomRangeOfEachResidue(const gmx_moltype_t& moltype)
913 std::vector<gmx::Range<int>> atomRanges;
914 int currentResidueNumber = moltype.atoms.atom[0].resind;
916 // Go through all atoms in a molecule to store first and last atoms in each residue.
917 for (int i = 0; i < moltype.atoms.nr; i++)
919 int residueOfThisAtom = moltype.atoms.atom[i].resind;
920 if (residueOfThisAtom != currentResidueNumber)
922 // This atom belongs to the next residue, so record the range for the previous residue,
923 // remembering that end points to one place past the last atom.
925 atomRanges.emplace_back(startAtom, endAtom);
926 // Prepare for the current residue
928 currentResidueNumber = residueOfThisAtom;
931 // special treatment for last residue in this molecule.
932 atomRanges.emplace_back(startAtom, moltype.atoms.nr);
937 /*! \brief Creates and returns a deprecated t_block struct with molecule indices
939 * \param[in] mtop The global topology
941 static t_block gmx_mtop_molecules_t_block(const gmx_mtop_t& mtop)
945 mols.nr = gmx_mtop_num_molecules(mtop);
946 mols.nalloc_index = mols.nr + 1;
947 snew(mols.index, mols.nalloc_index);
949 fillMoleculeIndices(mtop, gmx::arrayRefFromArray(mols.index, mols.nr + 1));
954 static void gen_t_topology(const gmx_mtop_t& mtop, bool bMergeConstr, t_topology* top)
956 copyAtomtypesFromMtop(mtop, &top->atomtypes);
957 for (int ftype = 0; ftype < F_NRE; ftype++)
959 top->idef.il[ftype].nr = 0;
960 top->idef.il[ftype].nalloc = 0;
961 top->idef.il[ftype].iatoms = nullptr;
963 copyFFParametersFromMtop(mtop, &top->idef);
964 copyIListsFromMtop(mtop, &top->idef, bMergeConstr);
966 top->name = mtop.name;
967 top->atoms = gmx_mtop_global_atoms(mtop);
968 top->mols = gmx_mtop_molecules_t_block(mtop);
969 top->bIntermolecularInteractions = mtop.bIntermolecularInteractions;
970 top->symtab = mtop.symtab;
973 t_topology gmx_mtop_t_to_t_topology(gmx_mtop_t* mtop, bool freeMTop)
977 gen_t_topology(*mtop, false, &top);
981 // Clear pointers and counts, such that the pointers copied to top
982 // keep pointing to valid data after destroying mtop.
983 mtop->symtab.symbuf = nullptr;
989 std::vector<int> get_atom_index(const gmx_mtop_t& mtop)
992 std::vector<int> atom_index;
993 for (const AtomProxy atomP : AtomRange(mtop))
995 const t_atom& local = atomP.atom();
996 int index = atomP.globalAtomNumber();
997 if (local.ptype == ParticleType::Atom)
999 atom_index.push_back(index);
1005 void convertAtomsToMtop(t_symtab* symtab, char** name, t_atoms* atoms, gmx_mtop_t* mtop)
1007 mtop->symtab = *symtab;
1011 mtop->moltype.clear();
1012 mtop->moltype.resize(1);
1013 mtop->moltype.back().atoms = *atoms;
1015 mtop->molblock.resize(1);
1016 mtop->molblock[0].type = 0;
1017 mtop->molblock[0].nmol = 1;
1019 mtop->bIntermolecularInteractions = FALSE;
1021 mtop->natoms = atoms->nr;
1023 mtop->haveMoleculeIndices = false;
1028 bool haveFepPerturbedNBInteractions(const gmx_mtop_t& mtop)
1030 for (const gmx_moltype_t& molt : mtop.moltype)
1032 for (int a = 0; a < molt.atoms.nr; a++)
1034 if (PERTURBED(molt.atoms.atom[a]))
1044 bool haveFepPerturbedMasses(const gmx_mtop_t& mtop)
1046 for (const gmx_moltype_t& molt : mtop.moltype)
1048 for (int a = 0; a < molt.atoms.nr; a++)
1050 const t_atom& atom = molt.atoms.atom[a];
1051 if (atom.m != atom.mB)
1061 bool havePerturbedConstraints(const gmx_mtop_t& mtop)
1063 // This code assumes that all perturbed constraints parameters are actually used
1064 const auto& ffparams = mtop.ffparams;
1066 for (gmx::index i = 0; i < gmx::ssize(ffparams.functype); i++)
1068 if (ffparams.functype[i] == F_CONSTR || ffparams.functype[i] == F_CONSTRNC)
1070 const auto& iparams = ffparams.iparams[i];
1071 if (iparams.constr.dA != iparams.constr.dB)