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 typedef struct gmx_mtop_atomloop_block
189 const gmx_mtop_t* mtop;
191 const t_atoms* atoms;
193 } t_gmx_mtop_atomloop_block;
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 gmx_mtop_ilistloop_t gmx_mtop_ilistloop_init(const gmx_mtop_t& mtop)
261 return gmx_mtop_ilistloop_init(&mtop);
264 static void gmx_mtop_ilistloop_destroy(gmx_mtop_ilistloop_t iloop)
269 const InteractionLists* gmx_mtop_ilistloop_next(gmx_mtop_ilistloop_t iloop, int* nmol)
271 if (iloop == nullptr)
273 gmx_incons("gmx_mtop_ilistloop_next called without calling gmx_mtop_ilistloop_init");
277 if (iloop->mblock >= gmx::ssize(iloop->mtop->molblock))
279 if (iloop->mblock == gmx::ssize(iloop->mtop->molblock) && iloop->mtop->bIntermolecularInteractions)
282 return iloop->mtop->intermolecular_ilist.get();
285 gmx_mtop_ilistloop_destroy(iloop);
289 *nmol = iloop->mtop->molblock[iloop->mblock].nmol;
291 return &iloop->mtop->moltype[iloop->mtop->molblock[iloop->mblock].type].ilist;
293 typedef struct gmx_mtop_ilistloop_all
295 const gmx_mtop_t* mtop;
299 } t_gmx_mtop_ilist_all;
301 int gmx_mtop_ftype_count(const gmx_mtop_t* mtop, int ftype)
306 gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop);
307 while (const InteractionLists* il = gmx_mtop_ilistloop_next(iloop, &nmol))
309 n += nmol * (*il)[ftype].size() / (1 + NRAL(ftype));
312 if (mtop->bIntermolecularInteractions)
314 n += (*mtop->intermolecular_ilist)[ftype].size() / (1 + NRAL(ftype));
320 int gmx_mtop_ftype_count(const gmx_mtop_t& mtop, int ftype)
322 return gmx_mtop_ftype_count(&mtop, ftype);
325 int gmx_mtop_interaction_count(const gmx_mtop_t& mtop, const int unsigned if_flags)
329 gmx_mtop_ilistloop_t iloop = gmx_mtop_ilistloop_init(mtop);
331 while (const InteractionLists* il = gmx_mtop_ilistloop_next(iloop, &nmol))
333 for (int ftype = 0; ftype < F_NRE; ftype++)
335 if ((interaction_function[ftype].flags & if_flags) == if_flags)
337 n += nmol * (*il)[ftype].size() / (1 + NRAL(ftype));
342 if (mtop.bIntermolecularInteractions)
344 for (int ftype = 0; ftype < F_NRE; ftype++)
346 if ((interaction_function[ftype].flags & if_flags) == if_flags)
348 n += (*mtop.intermolecular_ilist)[ftype].size() / (1 + NRAL(ftype));
356 std::array<int, eptNR> gmx_mtop_particletype_count(const gmx_mtop_t& mtop)
358 std::array<int, eptNR> count = { { 0 } };
360 for (const auto& molblock : mtop.molblock)
362 const t_atoms& atoms = mtop.moltype[molblock.type].atoms;
363 for (int a = 0; a < atoms.nr; a++)
365 count[atoms.atom[a].ptype] += molblock.nmol;
372 static void atomcat(t_atoms* dest, const t_atoms* src, int copies, int maxres_renum, int* maxresnr)
374 int i = 0, j = 0, l = 0, size = 0;
376 int destnr = dest->nr;
380 dest->haveMass = src->haveMass;
381 dest->haveType = src->haveType;
382 dest->haveCharge = src->haveCharge;
383 dest->haveBState = src->haveBState;
384 dest->havePdbInfo = src->havePdbInfo;
388 dest->haveMass = dest->haveMass && src->haveMass;
389 dest->haveType = dest->haveType && src->haveType;
390 dest->haveCharge = dest->haveCharge && src->haveCharge;
391 dest->haveBState = dest->haveBState && src->haveBState;
392 dest->havePdbInfo = dest->havePdbInfo && src->havePdbInfo;
397 size = destnr + copies * srcnr;
398 srenew(dest->atom, size);
399 srenew(dest->atomname, size);
402 srenew(dest->atomtype, size);
403 if (dest->haveBState)
405 srenew(dest->atomtypeB, size);
408 if (dest->havePdbInfo)
410 srenew(dest->pdbinfo, size);
415 size = dest->nres + copies * src->nres;
416 srenew(dest->resinfo, size);
419 /* residue information */
420 for (l = dest->nres, j = 0; (j < copies); j++, l += src->nres)
422 memcpy(reinterpret_cast<char*>(&(dest->resinfo[l])),
423 reinterpret_cast<char*>(&(src->resinfo[0])),
424 static_cast<size_t>(src->nres * sizeof(src->resinfo[0])));
427 for (l = destnr, j = 0; (j < copies); j++, l += srcnr)
429 memcpy(reinterpret_cast<char*>(&(dest->atom[l])),
430 reinterpret_cast<char*>(&(src->atom[0])),
431 static_cast<size_t>(srcnr * sizeof(src->atom[0])));
432 memcpy(reinterpret_cast<char*>(&(dest->atomname[l])),
433 reinterpret_cast<char*>(&(src->atomname[0])),
434 static_cast<size_t>(srcnr * sizeof(src->atomname[0])));
437 memcpy(reinterpret_cast<char*>(&(dest->atomtype[l])),
438 reinterpret_cast<char*>(&(src->atomtype[0])),
439 static_cast<size_t>(srcnr * sizeof(src->atomtype[0])));
440 if (dest->haveBState)
442 memcpy(reinterpret_cast<char*>(&(dest->atomtypeB[l])),
443 reinterpret_cast<char*>(&(src->atomtypeB[0])),
444 static_cast<size_t>(srcnr * sizeof(src->atomtypeB[0])));
447 if (dest->havePdbInfo)
449 memcpy(reinterpret_cast<char*>(&(dest->pdbinfo[l])),
450 reinterpret_cast<char*>(&(src->pdbinfo[0])),
451 static_cast<size_t>(srcnr * sizeof(src->pdbinfo[0])));
455 /* Increment residue indices */
456 for (l = destnr, j = 0; (j < copies); j++)
458 for (i = 0; (i < srcnr); i++, l++)
460 dest->atom[l].resind = dest->nres + j * src->nres + src->atom[i].resind;
464 if (src->nres <= maxres_renum)
466 /* Single residue molecule, continue counting residues */
467 for (j = 0; (j < copies); j++)
469 for (l = 0; l < src->nres; l++)
472 dest->resinfo[dest->nres + j * src->nres + l].nr = *maxresnr;
477 dest->nres += copies * src->nres;
478 dest->nr += copies * src->nr;
481 t_atoms gmx_mtop_global_atoms(const gmx_mtop_t* mtop)
485 init_t_atoms(&atoms, 0, FALSE);
487 int maxresnr = mtop->maxResNumberNotRenumbered();
488 for (const gmx_molblock_t& molb : mtop->molblock)
491 &mtop->moltype[molb.type].atoms,
493 mtop->maxResiduesPerMoleculeToTriggerRenumber(),
501 * The cat routines below are old code from src/kernel/topcat.c
504 static void ilistcat(int ftype, InteractionList* dest, const InteractionList& src, int copies, int dnum, int snum)
506 const int nral = NRAL(ftype);
508 size_t destIndex = dest->iatoms.size();
509 dest->iatoms.resize(dest->iatoms.size() + copies * src.size());
511 for (int c = 0; c < copies; c++)
513 for (int i = 0; i < src.size();)
515 dest->iatoms[destIndex++] = src.iatoms[i++];
516 for (int a = 0; a < nral; a++)
518 dest->iatoms[destIndex++] = dnum + src.iatoms[i++];
525 static void ilistcat(int ftype, t_ilist* dest, const InteractionList& src, int copies, int dnum, int snum)
527 const int nral = NRAL(ftype);
529 dest->nalloc = dest->nr + copies * src.size();
530 srenew(dest->iatoms, dest->nalloc);
532 for (int c = 0; c < copies; c++)
534 for (int i = 0; i < src.size();)
536 dest->iatoms[dest->nr++] = src.iatoms[i++];
537 for (int a = 0; a < nral; a++)
539 dest->iatoms[dest->nr++] = dnum + src.iatoms[i++];
546 static const t_iparams& getIparams(const InteractionDefinitions& idef, const int index)
548 return idef.iparams[index];
551 static const t_iparams& getIparams(const t_idef& idef, const int index)
553 return idef.iparams[index];
556 static void resizeIParams(std::vector<t_iparams>* iparams, const int newSize)
558 iparams->resize(newSize);
561 static void resizeIParams(t_iparams** iparams, const int newSize)
563 srenew(*iparams, newSize);
566 template<typename IdefType>
567 static void set_posres_params(IdefType* idef, const gmx_molblock_t* molb, int i0, int a_offset)
569 auto* il = &idef->il[F_POSRES];
570 int i1 = il->size() / 2;
571 resizeIParams(&idef->iparams_posres, i1);
572 for (int i = i0; i < i1; i++)
574 t_iparams* ip = &idef->iparams_posres[i];
575 /* Copy the force constants */
576 *ip = getIparams(*idef, il->iatoms[i * 2]);
577 int a_molb = il->iatoms[i * 2 + 1] - a_offset;
578 if (molb->posres_xA.empty())
580 gmx_incons("Position restraint coordinates are missing");
582 ip->posres.pos0A[XX] = molb->posres_xA[a_molb][XX];
583 ip->posres.pos0A[YY] = molb->posres_xA[a_molb][YY];
584 ip->posres.pos0A[ZZ] = molb->posres_xA[a_molb][ZZ];
585 if (!molb->posres_xB.empty())
587 ip->posres.pos0B[XX] = molb->posres_xB[a_molb][XX];
588 ip->posres.pos0B[YY] = molb->posres_xB[a_molb][YY];
589 ip->posres.pos0B[ZZ] = molb->posres_xB[a_molb][ZZ];
593 ip->posres.pos0B[XX] = ip->posres.pos0A[XX];
594 ip->posres.pos0B[YY] = ip->posres.pos0A[YY];
595 ip->posres.pos0B[ZZ] = ip->posres.pos0A[ZZ];
597 /* Set the parameter index for idef->iparams_posre */
598 il->iatoms[i * 2] = i;
602 template<typename IdefType>
603 static void set_fbposres_params(IdefType* idef, const gmx_molblock_t* molb, int i0, int a_offset)
605 auto* il = &idef->il[F_FBPOSRES];
606 int i1 = il->size() / 2;
607 resizeIParams(&idef->iparams_fbposres, i1);
608 for (int i = i0; i < i1; i++)
610 t_iparams* ip = &idef->iparams_fbposres[i];
611 /* Copy the force constants */
612 *ip = getIparams(*idef, il->iatoms[i * 2]);
613 int a_molb = il->iatoms[i * 2 + 1] - a_offset;
614 if (molb->posres_xA.empty())
616 gmx_incons("Position restraint coordinates are missing");
618 /* Take flat-bottom posres reference from normal position restraints */
619 ip->fbposres.pos0[XX] = molb->posres_xA[a_molb][XX];
620 ip->fbposres.pos0[YY] = molb->posres_xA[a_molb][YY];
621 ip->fbposres.pos0[ZZ] = molb->posres_xA[a_molb][ZZ];
622 /* Note: no B-type for flat-bottom posres */
624 /* Set the parameter index for idef->iparams_posre */
625 il->iatoms[i * 2] = i;
629 /*! \brief Copy parameters to idef structure from mtop.
631 * Makes a deep copy of the force field parameters data structure from a gmx_mtop_t.
632 * Used to initialize legacy topology types.
634 * \param[in] mtop Reference to input mtop.
635 * \param[in] idef Pointer to idef to populate.
637 static void copyFFParametersFromMtop(const gmx_mtop_t& mtop, t_idef* idef)
639 const gmx_ffparams_t* ffp = &mtop.ffparams;
641 idef->ntypes = ffp->numTypes();
642 idef->atnr = ffp->atnr;
643 /* we can no longer copy the pointers to the mtop members,
644 * because they will become invalid as soon as mtop gets free'd.
645 * We also need to make sure to only operate on valid data!
648 if (!ffp->functype.empty())
650 snew(idef->functype, ffp->functype.size());
651 std::copy(ffp->functype.data(), ffp->functype.data() + ffp->functype.size(), idef->functype);
655 idef->functype = nullptr;
657 if (!ffp->iparams.empty())
659 snew(idef->iparams, ffp->iparams.size());
660 std::copy(ffp->iparams.data(), ffp->iparams.data() + ffp->iparams.size(), idef->iparams);
664 idef->iparams = nullptr;
666 idef->iparams_posres = nullptr;
667 idef->iparams_fbposres = nullptr;
668 idef->fudgeQQ = ffp->fudgeQQ;
669 idef->ilsort = ilsortUNKNOWN;
672 /*! \brief Copy idef structure from mtop.
674 * Makes a deep copy of an idef data structure from a gmx_mtop_t.
675 * Used to initialize legacy topology types.
677 * \param[in] mtop Reference to input mtop.
678 * \param[in] idef Pointer to idef to populate.
679 * \param[in] mergeConstr Decide if constraints will be merged.
681 template<typename IdefType>
682 static void copyIListsFromMtop(const gmx_mtop_t& mtop, IdefType* idef, bool mergeConstr)
685 for (const gmx_molblock_t& molb : mtop.molblock)
687 const gmx_moltype_t& molt = mtop.moltype[molb.type];
689 int srcnr = molt.atoms.nr;
692 int nposre_old = idef->il[F_POSRES].size();
693 int nfbposre_old = idef->il[F_FBPOSRES].size();
694 for (int ftype = 0; ftype < F_NRE; ftype++)
696 if (mergeConstr && ftype == F_CONSTR && !molt.ilist[F_CONSTRNC].empty())
698 /* Merge all constrains into one ilist.
699 * This simplifies the constraint code.
701 for (int mol = 0; mol < molb.nmol; mol++)
703 ilistcat(ftype, &idef->il[F_CONSTR], molt.ilist[F_CONSTR], 1, destnr + mol * srcnr, srcnr);
704 ilistcat(ftype, &idef->il[F_CONSTR], molt.ilist[F_CONSTRNC], 1, destnr + mol * srcnr, srcnr);
707 else if (!(mergeConstr && ftype == F_CONSTRNC))
709 ilistcat(ftype, &idef->il[ftype], molt.ilist[ftype], molb.nmol, destnr, srcnr);
712 if (idef->il[F_POSRES].size() > nposre_old)
714 /* Executing this line line stops gmxdump -sys working
715 * correctly. I'm not aware there's an elegant fix. */
716 set_posres_params(idef, &molb, nposre_old / 2, natoms);
718 if (idef->il[F_FBPOSRES].size() > nfbposre_old)
720 set_fbposres_params(idef, &molb, nfbposre_old / 2, natoms);
723 natoms += molb.nmol * srcnr;
726 if (mtop.bIntermolecularInteractions)
728 for (int ftype = 0; ftype < F_NRE; ftype++)
730 ilistcat(ftype, &idef->il[ftype], (*mtop.intermolecular_ilist)[ftype], 1, 0, mtop.natoms);
734 // We have not (yet) sorted free-energy interactions to the end of the ilists
735 idef->ilsort = ilsortNO_FE;
738 /*! \brief Copy atomtypes from mtop
740 * Makes a deep copy of t_atomtypes from gmx_mtop_t.
741 * Used to initialize legacy topology types.
743 * \param[in] mtop Reference to input mtop.
744 * \param[in] atomtypes Pointer to atomtypes to populate.
746 static void copyAtomtypesFromMtop(const gmx_mtop_t& mtop, t_atomtypes* atomtypes)
748 atomtypes->nr = mtop.atomtypes.nr;
749 if (mtop.atomtypes.atomnumber)
751 snew(atomtypes->atomnumber, mtop.atomtypes.nr);
752 std::copy(mtop.atomtypes.atomnumber,
753 mtop.atomtypes.atomnumber + mtop.atomtypes.nr,
754 atomtypes->atomnumber);
758 atomtypes->atomnumber = nullptr;
762 /*! \brief Generate a single list of lists of exclusions for the whole system
764 * \param[in] mtop Reference to input mtop.
766 static gmx::ListOfLists<int> globalExclusionLists(const gmx_mtop_t& mtop)
768 gmx::ListOfLists<int> excls;
771 for (const gmx_molblock_t& molb : mtop.molblock)
773 const gmx_moltype_t& molt = mtop.moltype[molb.type];
775 for (int mol = 0; mol < molb.nmol; mol++)
777 excls.appendListOfLists(molt.excls, atomIndex);
779 atomIndex += molt.atoms.nr;
786 /*! \brief Updates inter-molecular exclusion lists
788 * This function updates inter-molecular exclusions to exclude all
789 * non-bonded interactions between a given list of atoms
791 * \param[inout] excls existing exclusions in local topology
792 * \param[in] ids list of global IDs of atoms
794 static void addMimicExclusions(gmx::ListOfLists<int>* excls, const gmx::ArrayRef<const int> ids)
796 t_blocka inter_excl{};
797 init_blocka(&inter_excl);
798 size_t n_q = ids.size();
800 inter_excl.nr = excls->ssize();
801 inter_excl.nra = n_q * n_q;
803 size_t total_nra = n_q * n_q;
805 snew(inter_excl.index, excls->ssize() + 1);
806 snew(inter_excl.a, total_nra);
808 for (int i = 0; i < inter_excl.nr; ++i)
810 inter_excl.index[i] = 0;
813 /* Here we loop over the list of QM atom ids
814 * and create exclusions between all of them resulting in
815 * n_q * n_q sized exclusion list
818 for (int k = 0; k < inter_excl.nr; ++k)
820 inter_excl.index[k] = prev_index;
821 for (long i = 0; i < ids.ssize(); i++)
827 size_t index = n_q * i;
828 inter_excl.index[ids[i]] = index;
829 prev_index = index + n_q;
830 for (size_t j = 0; j < n_q; ++j)
832 inter_excl.a[n_q * i + j] = ids[j];
836 inter_excl.index[ids[n_q - 1] + 1] = n_q * n_q;
838 inter_excl.index[inter_excl.nr] = n_q * n_q;
840 std::vector<gmx::ExclusionBlock> qmexcl2(excls->size());
841 gmx::blockaToExclusionBlocks(&inter_excl, qmexcl2);
843 // Merge the created exclusion list with the existing one
844 gmx::mergeExclusions(excls, qmexcl2);
847 static void sortFreeEnergyInteractionsAtEnd(const gmx_mtop_t& mtop, InteractionDefinitions* idef)
849 std::vector<real> qA(mtop.natoms);
850 std::vector<real> qB(mtop.natoms);
851 for (const AtomProxy atomP : AtomRange(mtop))
853 const t_atom& local = atomP.atom();
854 int index = atomP.globalAtomNumber();
856 qB[index] = local.qB;
858 gmx_sort_ilist_fe(idef, qA.data(), qB.data());
861 static void gen_local_top(const gmx_mtop_t& mtop,
862 bool freeEnergyInteractionsAtEnd,
866 copyIListsFromMtop(mtop, &top->idef, bMergeConstr);
867 if (freeEnergyInteractionsAtEnd)
869 sortFreeEnergyInteractionsAtEnd(mtop, &top->idef);
871 top->excls = globalExclusionLists(mtop);
872 if (!mtop.intermolecularExclusionGroup.empty())
874 addMimicExclusions(&top->excls, mtop.intermolecularExclusionGroup);
878 void gmx_mtop_generate_local_top(const gmx_mtop_t& mtop, gmx_localtop_t* top, bool freeEnergyInteractionsAtEnd)
880 gen_local_top(mtop, freeEnergyInteractionsAtEnd, true, top);
883 /*! \brief Fills an array with molecule begin/end atom indices
885 * \param[in] mtop The global topology
886 * \param[out] index Array of size nr. of molecules + 1 to be filled with molecule begin/end indices
888 static void fillMoleculeIndices(const gmx_mtop_t& mtop, gmx::ArrayRef<int> index)
890 int globalAtomIndex = 0;
891 int globalMolIndex = 0;
892 index[globalMolIndex] = globalAtomIndex;
893 for (const gmx_molblock_t& molb : mtop.molblock)
895 int numAtomsPerMolecule = mtop.moltype[molb.type].atoms.nr;
896 for (int mol = 0; mol < molb.nmol; mol++)
898 globalAtomIndex += numAtomsPerMolecule;
900 index[globalMolIndex] = globalAtomIndex;
905 gmx::RangePartitioning gmx_mtop_molecules(const gmx_mtop_t& mtop)
907 gmx::RangePartitioning mols;
909 for (const gmx_molblock_t& molb : mtop.molblock)
911 int numAtomsPerMolecule = mtop.moltype[molb.type].atoms.nr;
912 for (int mol = 0; mol < molb.nmol; mol++)
914 mols.appendBlock(numAtomsPerMolecule);
921 std::vector<gmx::Range<int>> atomRangeOfEachResidue(const gmx_moltype_t& moltype)
923 std::vector<gmx::Range<int>> atomRanges;
924 int currentResidueNumber = moltype.atoms.atom[0].resind;
926 // Go through all atoms in a molecule to store first and last atoms in each residue.
927 for (int i = 0; i < moltype.atoms.nr; i++)
929 int residueOfThisAtom = moltype.atoms.atom[i].resind;
930 if (residueOfThisAtom != currentResidueNumber)
932 // This atom belongs to the next residue, so record the range for the previous residue,
933 // remembering that end points to one place past the last atom.
935 atomRanges.emplace_back(startAtom, endAtom);
936 // Prepare for the current residue
938 currentResidueNumber = residueOfThisAtom;
941 // special treatment for last residue in this molecule.
942 atomRanges.emplace_back(startAtom, moltype.atoms.nr);
947 /*! \brief Creates and returns a deprecated t_block struct with molecule indices
949 * \param[in] mtop The global topology
951 static t_block gmx_mtop_molecules_t_block(const gmx_mtop_t& mtop)
955 mols.nr = gmx_mtop_num_molecules(mtop);
956 mols.nalloc_index = mols.nr + 1;
957 snew(mols.index, mols.nalloc_index);
959 fillMoleculeIndices(mtop, gmx::arrayRefFromArray(mols.index, mols.nr + 1));
964 static void gen_t_topology(const gmx_mtop_t& mtop, bool bMergeConstr, t_topology* top)
966 copyAtomtypesFromMtop(mtop, &top->atomtypes);
967 for (int ftype = 0; ftype < F_NRE; ftype++)
969 top->idef.il[ftype].nr = 0;
970 top->idef.il[ftype].nalloc = 0;
971 top->idef.il[ftype].iatoms = nullptr;
973 copyFFParametersFromMtop(mtop, &top->idef);
974 copyIListsFromMtop(mtop, &top->idef, bMergeConstr);
976 top->name = mtop.name;
977 top->atoms = gmx_mtop_global_atoms(&mtop);
978 top->mols = gmx_mtop_molecules_t_block(mtop);
979 top->bIntermolecularInteractions = mtop.bIntermolecularInteractions;
980 top->symtab = mtop.symtab;
983 t_topology gmx_mtop_t_to_t_topology(gmx_mtop_t* mtop, bool freeMTop)
987 gen_t_topology(*mtop, false, &top);
991 // Clear pointers and counts, such that the pointers copied to top
992 // keep pointing to valid data after destroying mtop.
993 mtop->symtab.symbuf = nullptr;
999 std::vector<int> get_atom_index(const gmx_mtop_t* mtop)
1002 std::vector<int> atom_index;
1003 for (const AtomProxy atomP : AtomRange(*mtop))
1005 const t_atom& local = atomP.atom();
1006 int index = atomP.globalAtomNumber();
1007 if (local.ptype == eptAtom)
1009 atom_index.push_back(index);
1015 void convertAtomsToMtop(t_symtab* symtab, char** name, t_atoms* atoms, gmx_mtop_t* mtop)
1017 mtop->symtab = *symtab;
1021 mtop->moltype.clear();
1022 mtop->moltype.resize(1);
1023 mtop->moltype.back().atoms = *atoms;
1025 mtop->molblock.resize(1);
1026 mtop->molblock[0].type = 0;
1027 mtop->molblock[0].nmol = 1;
1029 mtop->bIntermolecularInteractions = FALSE;
1031 mtop->natoms = atoms->nr;
1033 mtop->haveMoleculeIndices = false;
1038 bool haveFepPerturbedNBInteractions(const gmx_mtop_t& mtop)
1040 for (const gmx_moltype_t& molt : mtop.moltype)
1042 for (int a = 0; a < molt.atoms.nr; a++)
1044 if (PERTURBED(molt.atoms.atom[a]))
1054 bool haveFepPerturbedMasses(const gmx_mtop_t& mtop)
1056 for (const gmx_moltype_t& molt : mtop.moltype)
1058 for (int a = 0; a < molt.atoms.nr; a++)
1060 const t_atom& atom = molt.atoms.atom[a];
1061 if (atom.m != atom.mB)
1071 bool havePerturbedConstraints(const gmx_mtop_t& mtop)
1073 // This code assumes that all perturbed constraints parameters are actually used
1074 const auto& ffparams = mtop.ffparams;
1076 for (gmx::index i = 0; i < gmx::ssize(ffparams.functype); i++)
1078 if (ffparams.functype[i] == F_CONSTR || ffparams.functype[i] == F_CONSTRNC)
1080 const auto& iparams = ffparams.iparams[i];
1081 if (iparams.constr.dA != iparams.constr.dB)