-static int gmx_mtop_maxresnr(const gmx_mtop_t* mtop, int maxres_renum)
-{
- int maxresnr = 0;
-
- for (const gmx_moltype_t& moltype : mtop->moltype)
- {
- const t_atoms& atoms = moltype.atoms;
- if (atoms.nres > maxres_renum)
- {
- for (int r = 0; r < atoms.nres; r++)
- {
- if (atoms.resinfo[r].nr > maxresnr)
- {
- maxresnr = atoms.resinfo[r].nr;
- }
- }
- }
- }
-
- return maxresnr;
-}
-
-static void buildMolblockIndices(gmx_mtop_t* mtop)
-{
- mtop->moleculeBlockIndices.resize(mtop->molblock.size());
-
- int atomIndex = 0;
- int residueIndex = 0;
- int residueNumberStart = mtop->maxresnr + 1;
- int moleculeIndexStart = 0;
- for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
- {
- const gmx_molblock_t& molb = mtop->molblock[mb];
- MoleculeBlockIndices& indices = mtop->moleculeBlockIndices[mb];
- const int numResPerMol = mtop->moltype[molb.type].atoms.nres;
-
- indices.numAtomsPerMolecule = mtop->moltype[molb.type].atoms.nr;
- indices.globalAtomStart = atomIndex;
- indices.globalResidueStart = residueIndex;
- atomIndex += molb.nmol * indices.numAtomsPerMolecule;
- residueIndex += molb.nmol * numResPerMol;
- indices.globalAtomEnd = atomIndex;
- indices.residueNumberStart = residueNumberStart;
- if (numResPerMol <= mtop->maxres_renum)
- {
- residueNumberStart += molb.nmol * numResPerMol;
- }
- indices.moleculeIndexStart = moleculeIndexStart;
- moleculeIndexStart += molb.nmol;
- }
-}
-
-void gmx_mtop_finalize(gmx_mtop_t* mtop)
-{
- char* env;
-
- if (mtop->molblock.size() == 1 && mtop->molblock[0].nmol == 1)
- {
- /* We have a single molecule only, no renumbering needed.
- * This case also covers an mtop converted from pdb/gro/... input,
- * so we retain the original residue numbering.
- */
- mtop->maxres_renum = 0;
- }
- else
- {
- /* We only renumber single residue molecules. Their intra-molecular
- * residue numbering is anyhow irrelevant.
- */
- mtop->maxres_renum = 1;
- }
-
- env = getenv("GMX_MAXRESRENUM");
- if (env != nullptr)
- {
- sscanf(env, "%d", &mtop->maxres_renum);
- }
- if (mtop->maxres_renum == -1)
- {
- /* -1 signals renumber residues in all molecules */
- mtop->maxres_renum = INT_MAX;
- }
-
- mtop->maxresnr = gmx_mtop_maxresnr(mtop, mtop->maxres_renum);
-
- buildMolblockIndices(mtop);
-}
-