2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
40 /* This file is completely threadsafe - keep it that way! */
52 #include "gromacs/fileio/filetypes.h"
53 #include "gromacs/fileio/gmxfio.h"
54 #include "gromacs/fileio/gmxfio_xdr.h"
55 #include "gromacs/math/units.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/mdtypes/awh_history.h"
58 #include "gromacs/mdtypes/awh_params.h"
59 #include "gromacs/mdtypes/inputrec.h"
60 #include "gromacs/mdtypes/md_enums.h"
61 #include "gromacs/mdtypes/multipletimestepping.h"
62 #include "gromacs/mdtypes/pull_params.h"
63 #include "gromacs/mdtypes/state.h"
64 #include "gromacs/pbcutil/boxutilities.h"
65 #include "gromacs/pbcutil/pbc.h"
66 #include "gromacs/topology/block.h"
67 #include "gromacs/topology/ifunc.h"
68 #include "gromacs/topology/mtop_util.h"
69 #include "gromacs/topology/symtab.h"
70 #include "gromacs/topology/topology.h"
71 #include "gromacs/utility/arraysize.h"
72 #include "gromacs/utility/baseversion.h"
73 #include "gromacs/utility/cstringutil.h"
74 #include "gromacs/utility/fatalerror.h"
75 #include "gromacs/utility/futil.h"
76 #include "gromacs/utility/gmxassert.h"
77 #include "gromacs/utility/inmemoryserializer.h"
78 #include "gromacs/utility/keyvaluetreebuilder.h"
79 #include "gromacs/utility/keyvaluetreeserializer.h"
80 #include "gromacs/utility/smalloc.h"
81 #include "gromacs/utility/snprintf.h"
82 #include "gromacs/utility/txtdump.h"
84 #define TPX_TAG_RELEASE "release"
86 /*! \brief Tag string for the file format written to run input files
87 * written by this version of the code.
89 * Change this if you want to change the run input format in a feature
90 * branch. This ensures that there will not be different run input
91 * formats around which cannot be distinguished, while not causing
92 * problems rebasing the feature branch onto upstream changes. When
93 * merging with mainstream GROMACS, set this tag string back to
94 * TPX_TAG_RELEASE, and instead add an element to tpxv.
96 static const char* tpx_tag = TPX_TAG_RELEASE;
98 /*! \brief Enum of values that describe the contents of a tpr file
99 * whose format matches a version number
101 * The enum helps the code be more self-documenting and ensure merges
102 * do not silently resolve when two patches make the same bump. When
103 * adding new functionality, add a new element just above tpxv_Count
104 * in this enumeration, and write code below that does the right thing
105 * according to the value of file_version.
109 tpxv_ComputationalElectrophysiology =
110 96, /**< support for ion/water position swaps (computational electrophysiology) */
111 tpxv_Use64BitRandomSeed, /**< change ld_seed from int to int64_t */
112 tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, /**< potentials for supporting coarse-grained force fields */
113 tpxv_InteractiveMolecularDynamics, /**< interactive molecular dynamics (IMD) */
114 tpxv_RemoveObsoleteParameters1, /**< remove optimize_fft, dihre_fc, nstcheckpoint */
115 tpxv_PullCoordTypeGeom, /**< add pull type and geometry per group and flat-bottom */
116 tpxv_PullGeomDirRel, /**< add pull geometry direction-relative */
117 tpxv_IntermolecularBondeds, /**< permit inter-molecular bonded interactions in the topology */
118 tpxv_CompElWithSwapLayerOffset, /**< added parameters for improved CompEl setups */
119 tpxv_CompElPolyatomicIonsAndMultipleIonTypes, /**< CompEl now can handle polyatomic ions and more than two types of ions */
120 tpxv_RemoveAdress, /**< removed support for AdResS */
121 tpxv_PullCoordNGroup, /**< add ngroup to pull coord */
122 tpxv_RemoveTwinRange, /**< removed support for twin-range interactions */
123 tpxv_ReplacePullPrintCOM12, /**< Replaced print-com-1, 2 with pull-print-com */
124 tpxv_PullExternalPotential, /**< Added pull type external potential */
125 tpxv_GenericParamsForElectricField, /**< Introduced KeyValueTree and moved electric field parameters */
126 tpxv_AcceleratedWeightHistogram, /**< sampling with accelerated weight histogram method (AWH) */
127 tpxv_RemoveImplicitSolvation, /**< removed support for implicit solvation */
128 tpxv_PullPrevStepCOMAsReference, /**< Enabled using the COM of the pull group of the last frame as reference for PBC */
129 tpxv_MimicQMMM, /**< Introduced support for MiMiC QM/MM interface */
130 tpxv_PullAverage, /**< Added possibility to output average pull force and position */
131 tpxv_GenericInternalParameters, /**< Added internal parameters for mdrun modules*/
132 tpxv_VSite2FD, /**< Added 2FD type virtual site */
133 tpxv_AddSizeField, /**< Added field with information about the size of the serialized tpr file in bytes, excluding the header */
134 tpxv_StoreNonBondedInteractionExclusionGroup, /**< Store the non bonded interaction exclusion group in the topology */
135 tpxv_VSite1, /**< Added 1 type virtual site */
136 tpxv_MTS, /**< Added multiple time stepping */
137 tpxv_Count /**< the total number of tpxv versions */
140 /*! \brief Version number of the file format written to run input
141 * files by this version of the code.
143 * The tpx_version increases whenever the file format in the main
144 * development branch changes, due to an extension of the tpxv enum above.
145 * Backward compatibility for reading old run input files is maintained
146 * by checking this version number against that of the file and then using
147 * the correct code path.
149 * When developing a feature branch that needs to change the run input
150 * file format, change tpx_tag instead. */
151 static const int tpx_version = tpxv_Count - 1;
155 * Enum keeping track of incompatible changes for older TPR versions.
157 * The enum should be updated with a new field when editing the TOPOLOGY
158 * or HEADER of the tpx format. In particular, updating ftupd or
159 * changing the fields of TprHeaderVersion often trigger such needs.
161 * This way we can maintain forward compatibility too for all analysis tools
162 * and/or external programs that only need to know the atom/residue names,
163 * charges, and bond connectivity.
165 * It first appeared in tpx version 26, when I also moved the inputrecord
166 * to the end of the tpx file, so we can just skip it if we only
169 * In particular, it must be increased when adding new elements to
170 * ftupd, so that old code can read new .tpr files.
172 enum class TpxGeneration : int
174 Initial = 26, //! First version is 26
175 AddSizeField, //! TPR header modified for writing as a block.
176 AddVSite1, //! ftupd changed to include VSite1 type.
177 Count //! Number of entries.
180 //! Value of Current TPR generation.
181 static const int tpx_generation = static_cast<int>(TpxGeneration::Count) - 1;
183 /* This number should be the most recent backwards incompatible version
184 * I.e., if this number is 9, we cannot read tpx version 9 with this code.
186 static const int tpx_incompatible_version = 57; // GMX4.0 has version 58
189 /* Struct used to maintain tpx compatibility when function types are added */
192 int fvnr; /* file version number in which the function type first appeared */
193 int ftype; /* function type */
197 * TODO The following three lines make little sense, please clarify if
198 * you've had to work out how ftupd works.
200 * The entries should be ordered in:
201 * 1. ascending function type number
202 * 2. ascending file version number
204 * Because we support reading of old .tpr file versions (even when
205 * mdrun can no longer run the simulation), we need to be able to read
206 * obsolete t_interaction_function types. Any data read from such
207 * fields is discarded. Their names have _NOLONGERUSED appended to
208 * them to make things clear.
210 * When adding to or making breaking changes to reading this struct,
211 * update TpxGeneration.
213 static const t_ftupd ftupd[] = {
214 { 70, F_RESTRBONDS },
215 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_RESTRANGLES },
216 { 76, F_LINEAR_ANGLES },
217 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_RESTRDIHS },
218 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_CBTDIHS },
220 { 60, F_GB12_NOLONGERUSED },
221 { 61, F_GB13_NOLONGERUSED },
222 { 61, F_GB14_NOLONGERUSED },
223 { 72, F_GBPOL_NOLONGERUSED },
224 { 72, F_NPSOLVATION_NOLONGERUSED },
226 { 76, F_ANHARM_POL },
228 { tpxv_VSite1, F_VSITE1 },
229 { tpxv_VSite2FD, F_VSITE2FD },
230 { tpxv_GenericInternalParameters, F_DENSITYFITTING },
231 { 69, F_VTEMP_NOLONGERUSED },
242 { 79, F_DVDL_RESTRAINT },
243 { 79, F_DVDL_TEMPERATURE },
245 #define NFTUPD asize(ftupd)
247 /* Needed for backward compatibility */
250 /**************************************************************
252 * Now the higer level routines that do io of the structures and arrays
254 **************************************************************/
255 static void do_pullgrp_tpx_pre95(gmx::ISerializer* serializer, t_pull_group* pgrp, t_pull_coord* pcrd)
259 int numAtoms = pgrp->ind.size();
260 serializer->doInt(&numAtoms);
261 pgrp->ind.resize(numAtoms);
262 serializer->doIntArray(pgrp->ind.data(), numAtoms);
263 int numWeights = pgrp->weight.size();
264 serializer->doInt(&numWeights);
265 pgrp->weight.resize(numWeights);
266 serializer->doRealArray(pgrp->weight.data(), numWeights);
267 serializer->doInt(&pgrp->pbcatom);
268 serializer->doRvec(&pcrd->vec.as_vec());
269 clear_rvec(pcrd->origin);
270 serializer->doRvec(&tmp);
272 serializer->doReal(&pcrd->rate);
273 serializer->doReal(&pcrd->k);
274 serializer->doReal(&pcrd->kB);
277 static void do_pull_group(gmx::ISerializer* serializer, t_pull_group* pgrp)
279 int numAtoms = pgrp->ind.size();
280 serializer->doInt(&numAtoms);
281 pgrp->ind.resize(numAtoms);
282 serializer->doIntArray(pgrp->ind.data(), numAtoms);
283 int numWeights = pgrp->weight.size();
284 serializer->doInt(&numWeights);
285 pgrp->weight.resize(numWeights);
286 serializer->doRealArray(pgrp->weight.data(), numWeights);
287 serializer->doInt(&pgrp->pbcatom);
290 static void do_pull_coord(gmx::ISerializer* serializer,
297 if (file_version >= tpxv_PullCoordNGroup)
299 serializer->doInt(&pcrd->eType);
300 if (file_version >= tpxv_PullExternalPotential)
302 if (pcrd->eType == epullEXTERNAL)
305 if (serializer->reading())
307 serializer->doString(&buf);
308 pcrd->externalPotentialProvider = gmx_strdup(buf.c_str());
312 buf = pcrd->externalPotentialProvider;
313 serializer->doString(&buf);
318 pcrd->externalPotentialProvider.clear();
323 if (serializer->reading())
325 pcrd->externalPotentialProvider.clear();
328 /* Note that we try to support adding new geometries without
329 * changing the tpx version. This requires checks when printing the
330 * geometry string and a check and fatal_error in init_pull.
332 serializer->doInt(&pcrd->eGeom);
333 serializer->doInt(&pcrd->ngroup);
334 if (pcrd->ngroup <= c_pullCoordNgroupMax)
336 serializer->doIntArray(pcrd->group.data(), pcrd->ngroup);
340 /* More groups in file than supported, this must be a new geometry
341 * that is not supported by our current code. Since we will not
342 * use the groups for this coord (checks in the pull and WHAM code
343 * ensure this), we can ignore the groups and set ngroup=0.
346 snew(dum, pcrd->ngroup);
347 serializer->doIntArray(dum, pcrd->ngroup);
352 serializer->doIvec(&pcrd->dim.as_vec());
357 serializer->doInt(&pcrd->group[0]);
358 serializer->doInt(&pcrd->group[1]);
359 if (file_version >= tpxv_PullCoordTypeGeom)
361 pcrd->ngroup = (pcrd->eGeom == epullgDIRRELATIVE ? 4 : 2);
362 serializer->doInt(&pcrd->eType);
363 serializer->doInt(&pcrd->eGeom);
364 if (pcrd->ngroup == 4)
366 serializer->doInt(&pcrd->group[2]);
367 serializer->doInt(&pcrd->group[3]);
369 serializer->doIvec(&pcrd->dim.as_vec());
373 pcrd->eType = ePullOld;
374 pcrd->eGeom = eGeomOld;
375 copy_ivec(dimOld, pcrd->dim);
378 serializer->doRvec(&pcrd->origin.as_vec());
379 serializer->doRvec(&pcrd->vec.as_vec());
380 if (file_version >= tpxv_PullCoordTypeGeom)
382 serializer->doBool(&pcrd->bStart);
386 /* This parameter is only printed, but not actually used by mdrun */
387 pcrd->bStart = FALSE;
389 serializer->doReal(&pcrd->init);
390 serializer->doReal(&pcrd->rate);
391 serializer->doReal(&pcrd->k);
392 serializer->doReal(&pcrd->kB);
395 static void do_expandedvals(gmx::ISerializer* serializer, t_expanded* expand, t_lambda* fepvals, int file_version)
397 int n_lambda = fepvals->n_lambda;
399 /* reset the lambda calculation window */
400 fepvals->lambda_start_n = 0;
401 fepvals->lambda_stop_n = n_lambda;
402 if (file_version >= 79)
406 if (serializer->reading())
408 snew(expand->init_lambda_weights, n_lambda);
410 serializer->doRealArray(expand->init_lambda_weights, n_lambda);
411 serializer->doBool(&expand->bInit_weights);
414 serializer->doInt(&expand->nstexpanded);
415 serializer->doInt(&expand->elmcmove);
416 serializer->doInt(&expand->elamstats);
417 serializer->doInt(&expand->lmc_repeats);
418 serializer->doInt(&expand->gibbsdeltalam);
419 serializer->doInt(&expand->lmc_forced_nstart);
420 serializer->doInt(&expand->lmc_seed);
421 serializer->doReal(&expand->mc_temp);
422 serializer->doBool(&expand->bSymmetrizedTMatrix);
423 serializer->doInt(&expand->nstTij);
424 serializer->doInt(&expand->minvarmin);
425 serializer->doInt(&expand->c_range);
426 serializer->doReal(&expand->wl_scale);
427 serializer->doReal(&expand->wl_ratio);
428 serializer->doReal(&expand->init_wl_delta);
429 serializer->doBool(&expand->bWLoneovert);
430 serializer->doInt(&expand->elmceq);
431 serializer->doInt(&expand->equil_steps);
432 serializer->doInt(&expand->equil_samples);
433 serializer->doInt(&expand->equil_n_at_lam);
434 serializer->doReal(&expand->equil_wl_delta);
435 serializer->doReal(&expand->equil_ratio);
439 static void do_simtempvals(gmx::ISerializer* serializer, t_simtemp* simtemp, int n_lambda, int file_version)
441 if (file_version >= 79)
443 serializer->doInt(&simtemp->eSimTempScale);
444 serializer->doReal(&simtemp->simtemp_high);
445 serializer->doReal(&simtemp->simtemp_low);
448 if (serializer->reading())
450 snew(simtemp->temperatures, n_lambda);
452 serializer->doRealArray(simtemp->temperatures, n_lambda);
457 static void do_imd(gmx::ISerializer* serializer, t_IMD* imd)
459 serializer->doInt(&imd->nat);
460 if (serializer->reading())
462 snew(imd->ind, imd->nat);
464 serializer->doIntArray(imd->ind, imd->nat);
467 static void do_fepvals(gmx::ISerializer* serializer, t_lambda* fepvals, int file_version)
469 /* i is defined in the ndo_double macro; use g to iterate. */
473 /* free energy values */
475 if (file_version >= 79)
477 serializer->doInt(&fepvals->init_fep_state);
478 serializer->doDouble(&fepvals->init_lambda);
479 serializer->doDouble(&fepvals->delta_lambda);
481 else if (file_version >= 59)
483 serializer->doDouble(&fepvals->init_lambda);
484 serializer->doDouble(&fepvals->delta_lambda);
488 serializer->doReal(&rdum);
489 fepvals->init_lambda = rdum;
490 serializer->doReal(&rdum);
491 fepvals->delta_lambda = rdum;
493 if (file_version >= 79)
495 serializer->doInt(&fepvals->n_lambda);
496 if (serializer->reading())
498 snew(fepvals->all_lambda, efptNR);
500 for (g = 0; g < efptNR; g++)
502 if (fepvals->n_lambda > 0)
504 if (serializer->reading())
506 snew(fepvals->all_lambda[g], fepvals->n_lambda);
508 serializer->doDoubleArray(fepvals->all_lambda[g], fepvals->n_lambda);
509 serializer->doBoolArray(fepvals->separate_dvdl, efptNR);
511 else if (fepvals->init_lambda >= 0)
513 fepvals->separate_dvdl[efptFEP] = TRUE;
517 else if (file_version >= 64)
519 serializer->doInt(&fepvals->n_lambda);
520 if (serializer->reading())
524 snew(fepvals->all_lambda, efptNR);
525 /* still allocate the all_lambda array's contents. */
526 for (g = 0; g < efptNR; g++)
528 if (fepvals->n_lambda > 0)
530 snew(fepvals->all_lambda[g], fepvals->n_lambda);
534 serializer->doDoubleArray(fepvals->all_lambda[efptFEP], fepvals->n_lambda);
535 if (fepvals->init_lambda >= 0)
539 fepvals->separate_dvdl[efptFEP] = TRUE;
541 if (serializer->reading())
543 /* copy the contents of the efptFEP lambda component to all
544 the other components */
545 for (g = 0; g < efptNR; g++)
547 for (h = 0; h < fepvals->n_lambda; h++)
551 fepvals->all_lambda[g][h] = fepvals->all_lambda[efptFEP][h];
560 fepvals->n_lambda = 0;
561 fepvals->all_lambda = nullptr;
562 if (fepvals->init_lambda >= 0)
564 fepvals->separate_dvdl[efptFEP] = TRUE;
567 serializer->doReal(&fepvals->sc_alpha);
568 serializer->doInt(&fepvals->sc_power);
569 if (file_version >= 79)
571 serializer->doReal(&fepvals->sc_r_power);
575 fepvals->sc_r_power = 6.0;
577 if (fepvals->sc_r_power != 6.0)
579 gmx_fatal(FARGS, "sc-r-power=48 is no longer supported");
581 serializer->doReal(&fepvals->sc_sigma);
582 if (serializer->reading())
584 if (file_version >= 71)
586 fepvals->sc_sigma_min = fepvals->sc_sigma;
590 fepvals->sc_sigma_min = 0;
593 if (file_version >= 79)
595 serializer->doBool(&fepvals->bScCoul);
599 fepvals->bScCoul = TRUE;
601 if (file_version >= 64)
603 serializer->doInt(&fepvals->nstdhdl);
607 fepvals->nstdhdl = 1;
610 if (file_version >= 73)
612 serializer->doInt(&fepvals->separate_dhdl_file);
613 serializer->doInt(&fepvals->dhdl_derivatives);
617 fepvals->separate_dhdl_file = esepdhdlfileYES;
618 fepvals->dhdl_derivatives = edhdlderivativesYES;
620 if (file_version >= 71)
622 serializer->doInt(&fepvals->dh_hist_size);
623 serializer->doDouble(&fepvals->dh_hist_spacing);
627 fepvals->dh_hist_size = 0;
628 fepvals->dh_hist_spacing = 0.1;
630 if (file_version >= 79)
632 serializer->doInt(&fepvals->edHdLPrintEnergy);
636 fepvals->edHdLPrintEnergy = edHdLPrintEnergyNO;
639 /* handle lambda_neighbors */
640 if ((file_version >= 83 && file_version < 90) || file_version >= 92)
642 serializer->doInt(&fepvals->lambda_neighbors);
643 if ((fepvals->lambda_neighbors >= 0) && (fepvals->init_fep_state >= 0)
644 && (fepvals->init_lambda < 0))
646 fepvals->lambda_start_n = (fepvals->init_fep_state - fepvals->lambda_neighbors);
647 fepvals->lambda_stop_n = (fepvals->init_fep_state + fepvals->lambda_neighbors + 1);
648 if (fepvals->lambda_start_n < 0)
650 fepvals->lambda_start_n = 0;
652 if (fepvals->lambda_stop_n >= fepvals->n_lambda)
654 fepvals->lambda_stop_n = fepvals->n_lambda;
659 fepvals->lambda_start_n = 0;
660 fepvals->lambda_stop_n = fepvals->n_lambda;
665 fepvals->lambda_start_n = 0;
666 fepvals->lambda_stop_n = fepvals->n_lambda;
670 static void do_awhBias(gmx::ISerializer* serializer, gmx::AwhBiasParams* awhBiasParams, int gmx_unused file_version)
672 serializer->doInt(&awhBiasParams->eTarget);
673 serializer->doDouble(&awhBiasParams->targetBetaScaling);
674 serializer->doDouble(&awhBiasParams->targetCutoff);
675 serializer->doInt(&awhBiasParams->eGrowth);
676 serializer->doInt(&awhBiasParams->bUserData);
677 serializer->doDouble(&awhBiasParams->errorInitial);
678 serializer->doInt(&awhBiasParams->ndim);
679 serializer->doInt(&awhBiasParams->shareGroup);
680 serializer->doBool(&awhBiasParams->equilibrateHistogram);
682 if (serializer->reading())
684 snew(awhBiasParams->dimParams, awhBiasParams->ndim);
687 for (int d = 0; d < awhBiasParams->ndim; d++)
689 gmx::AwhDimParams* dimParams = &awhBiasParams->dimParams[d];
691 serializer->doInt(&dimParams->eCoordProvider);
692 serializer->doInt(&dimParams->coordIndex);
693 serializer->doDouble(&dimParams->origin);
694 serializer->doDouble(&dimParams->end);
695 serializer->doDouble(&dimParams->period);
696 serializer->doDouble(&dimParams->forceConstant);
697 serializer->doDouble(&dimParams->diffusion);
698 serializer->doDouble(&dimParams->coordValueInit);
699 serializer->doDouble(&dimParams->coverDiameter);
703 static void do_awh(gmx::ISerializer* serializer, gmx::AwhParams* awhParams, int gmx_unused file_version)
705 serializer->doInt(&awhParams->numBias);
706 serializer->doInt(&awhParams->nstOut);
707 serializer->doInt64(&awhParams->seed);
708 serializer->doInt(&awhParams->nstSampleCoord);
709 serializer->doInt(&awhParams->numSamplesUpdateFreeEnergy);
710 serializer->doInt(&awhParams->ePotential);
711 serializer->doBool(&awhParams->shareBiasMultisim);
713 if (awhParams->numBias > 0)
715 if (serializer->reading())
717 snew(awhParams->awhBiasParams, awhParams->numBias);
720 for (int k = 0; k < awhParams->numBias; k++)
722 do_awhBias(serializer, &awhParams->awhBiasParams[k], file_version);
727 static void do_pull(gmx::ISerializer* serializer, pull_params_t* pull, int file_version, int ePullOld)
733 if (file_version >= 95)
735 serializer->doInt(&pull->ngroup);
737 serializer->doInt(&pull->ncoord);
738 if (file_version < 95)
740 pull->ngroup = pull->ncoord + 1;
742 if (file_version < tpxv_PullCoordTypeGeom)
746 serializer->doInt(&eGeomOld);
747 serializer->doIvec(&dimOld);
748 /* The inner cylinder radius, now removed */
749 serializer->doReal(&dum);
751 serializer->doReal(&pull->cylinder_r);
752 serializer->doReal(&pull->constr_tol);
753 if (file_version >= 95)
755 serializer->doBool(&pull->bPrintCOM);
756 /* With file_version < 95 this value is set below */
758 if (file_version >= tpxv_ReplacePullPrintCOM12)
760 serializer->doBool(&pull->bPrintRefValue);
761 serializer->doBool(&pull->bPrintComp);
763 else if (file_version >= tpxv_PullCoordTypeGeom)
766 serializer->doInt(&idum); /* used to be bPrintCOM2 */
767 serializer->doBool(&pull->bPrintRefValue);
768 serializer->doBool(&pull->bPrintComp);
772 pull->bPrintRefValue = FALSE;
773 pull->bPrintComp = TRUE;
775 serializer->doInt(&pull->nstxout);
776 serializer->doInt(&pull->nstfout);
777 if (file_version >= tpxv_PullPrevStepCOMAsReference)
779 serializer->doBool(&pull->bSetPbcRefToPrevStepCOM);
783 pull->bSetPbcRefToPrevStepCOM = FALSE;
785 pull->group.resize(pull->ngroup);
786 pull->coord.resize(pull->ncoord);
787 if (file_version < 95)
789 /* epullgPOS for position pulling, before epullgDIRPBC was removed */
790 if (eGeomOld == epullgDIRPBC)
792 gmx_fatal(FARGS, "pull-geometry=position is no longer supported");
794 if (eGeomOld > epullgDIRPBC)
799 for (g = 0; g < pull->ngroup; g++)
801 /* We read and ignore a pull coordinate for group 0 */
802 do_pullgrp_tpx_pre95(serializer, &pull->group[g], &pull->coord[std::max(g - 1, 0)]);
805 pull->coord[g - 1].group[0] = 0;
806 pull->coord[g - 1].group[1] = g;
810 pull->bPrintCOM = (!pull->group[0].ind.empty());
814 for (g = 0; g < pull->ngroup; g++)
816 do_pull_group(serializer, &pull->group[g]);
818 for (g = 0; g < pull->ncoord; g++)
820 do_pull_coord(serializer, &pull->coord[g], file_version, ePullOld, eGeomOld, dimOld);
823 if (file_version >= tpxv_PullAverage)
827 v = pull->bXOutAverage;
828 serializer->doBool(&v);
829 pull->bXOutAverage = v;
830 v = pull->bFOutAverage;
831 serializer->doBool(&v);
832 pull->bFOutAverage = v;
837 static void do_rotgrp(gmx::ISerializer* serializer, t_rotgrp* rotg)
839 serializer->doInt(&rotg->eType);
840 serializer->doInt(&rotg->bMassW);
841 serializer->doInt(&rotg->nat);
842 if (serializer->reading())
844 snew(rotg->ind, rotg->nat);
846 serializer->doIntArray(rotg->ind, rotg->nat);
847 if (serializer->reading())
849 snew(rotg->x_ref, rotg->nat);
851 serializer->doRvecArray(rotg->x_ref, rotg->nat);
852 serializer->doRvec(&rotg->inputVec);
853 serializer->doRvec(&rotg->pivot);
854 serializer->doReal(&rotg->rate);
855 serializer->doReal(&rotg->k);
856 serializer->doReal(&rotg->slab_dist);
857 serializer->doReal(&rotg->min_gaussian);
858 serializer->doReal(&rotg->eps);
859 serializer->doInt(&rotg->eFittype);
860 serializer->doInt(&rotg->PotAngle_nstep);
861 serializer->doReal(&rotg->PotAngle_step);
864 static void do_rot(gmx::ISerializer* serializer, t_rot* rot)
868 serializer->doInt(&rot->ngrp);
869 serializer->doInt(&rot->nstrout);
870 serializer->doInt(&rot->nstsout);
871 if (serializer->reading())
873 snew(rot->grp, rot->ngrp);
875 for (g = 0; g < rot->ngrp; g++)
877 do_rotgrp(serializer, &rot->grp[g]);
882 static void do_swapgroup(gmx::ISerializer* serializer, t_swapGroup* g)
885 /* Name of the group or molecule */
887 if (serializer->reading())
889 serializer->doString(&buf);
890 g->molname = gmx_strdup(buf.c_str());
895 serializer->doString(&buf);
898 /* Number of atoms in the group */
899 serializer->doInt(&g->nat);
901 /* The group's atom indices */
902 if (serializer->reading())
904 snew(g->ind, g->nat);
906 serializer->doIntArray(g->ind, g->nat);
908 /* Requested counts for compartments A and B */
909 serializer->doIntArray(g->nmolReq, eCompNR);
912 static void do_swapcoords_tpx(gmx::ISerializer* serializer, t_swapcoords* swap, int file_version)
914 /* Enums for better readability of the code */
927 if (file_version >= tpxv_CompElPolyatomicIonsAndMultipleIonTypes)
929 /* The total number of swap groups is the sum of the fixed groups
930 * (split0, split1, solvent), and the user-defined groups (2+ types of ions)
932 serializer->doInt(&swap->ngrp);
933 if (serializer->reading())
935 snew(swap->grp, swap->ngrp);
937 for (int ig = 0; ig < swap->ngrp; ig++)
939 do_swapgroup(serializer, &swap->grp[ig]);
941 serializer->doBool(&swap->massw_split[eChannel0]);
942 serializer->doBool(&swap->massw_split[eChannel1]);
943 serializer->doInt(&swap->nstswap);
944 serializer->doInt(&swap->nAverage);
945 serializer->doReal(&swap->threshold);
946 serializer->doReal(&swap->cyl0r);
947 serializer->doReal(&swap->cyl0u);
948 serializer->doReal(&swap->cyl0l);
949 serializer->doReal(&swap->cyl1r);
950 serializer->doReal(&swap->cyl1u);
951 serializer->doReal(&swap->cyl1l);
955 /*** Support reading older CompEl .tpr files ***/
957 /* In the original CompEl .tpr files, we always have 5 groups: */
959 snew(swap->grp, swap->ngrp);
961 swap->grp[eGrpSplit0].molname = gmx_strdup("split0"); // group 0: split0
962 swap->grp[eGrpSplit1].molname = gmx_strdup("split1"); // group 1: split1
963 swap->grp[eGrpSolvent].molname = gmx_strdup("solvent"); // group 2: solvent
964 swap->grp[3].molname = gmx_strdup("anions"); // group 3: anions
965 swap->grp[4].molname = gmx_strdup("cations"); // group 4: cations
967 serializer->doInt(&swap->grp[3].nat);
968 serializer->doInt(&swap->grp[eGrpSolvent].nat);
969 serializer->doInt(&swap->grp[eGrpSplit0].nat);
970 serializer->doBool(&swap->massw_split[eChannel0]);
971 serializer->doInt(&swap->grp[eGrpSplit1].nat);
972 serializer->doBool(&swap->massw_split[eChannel1]);
973 serializer->doInt(&swap->nstswap);
974 serializer->doInt(&swap->nAverage);
975 serializer->doReal(&swap->threshold);
976 serializer->doReal(&swap->cyl0r);
977 serializer->doReal(&swap->cyl0u);
978 serializer->doReal(&swap->cyl0l);
979 serializer->doReal(&swap->cyl1r);
980 serializer->doReal(&swap->cyl1u);
981 serializer->doReal(&swap->cyl1l);
983 // The order[] array keeps compatibility with older .tpr files
984 // by reading in the groups in the classic order
986 const int order[4] = { 3, eGrpSolvent, eGrpSplit0, eGrpSplit1 };
988 for (int ig = 0; ig < 4; ig++)
991 snew(swap->grp[g].ind, swap->grp[g].nat);
992 serializer->doIntArray(swap->grp[g].ind, swap->grp[g].nat);
996 for (int j = eCompA; j <= eCompB; j++)
998 serializer->doInt(&swap->grp[3].nmolReq[j]); // group 3 = anions
999 serializer->doInt(&swap->grp[4].nmolReq[j]); // group 4 = cations
1001 } /* End support reading older CompEl .tpr files */
1003 if (file_version >= tpxv_CompElWithSwapLayerOffset)
1005 serializer->doReal(&swap->bulkOffset[eCompA]);
1006 serializer->doReal(&swap->bulkOffset[eCompB]);
1010 static void do_legacy_efield(gmx::ISerializer* serializer, gmx::KeyValueTreeObjectBuilder* root)
1012 const char* const dimName[] = { "x", "y", "z" };
1014 auto appliedForcesObj = root->addObject("applied-forces");
1015 auto efieldObj = appliedForcesObj.addObject("electric-field");
1016 // The content of the tpr file for this feature has
1017 // been the same since gromacs 4.0 that was used for
1019 for (int j = 0; j < DIM; ++j)
1022 serializer->doInt(&n);
1023 serializer->doInt(&nt);
1024 std::vector<real> aa(n + 1), phi(nt + 1), at(nt + 1), phit(nt + 1);
1025 serializer->doRealArray(aa.data(), n);
1026 serializer->doRealArray(phi.data(), n);
1027 serializer->doRealArray(at.data(), nt);
1028 serializer->doRealArray(phit.data(), nt);
1031 if (n > 1 || nt > 1)
1034 "Can not handle tpr files with more than one electric field term per "
1037 auto dimObj = efieldObj.addObject(dimName[j]);
1038 dimObj.addValue<real>("E0", aa[0]);
1039 dimObj.addValue<real>("omega", at[0]);
1040 dimObj.addValue<real>("t0", phi[0]);
1041 dimObj.addValue<real>("sigma", phit[0]);
1047 static void do_inputrec(gmx::ISerializer* serializer, t_inputrec* ir, int file_version)
1049 int i, j, k, idum = 0;
1051 gmx_bool bdum = false;
1053 if (file_version != tpx_version)
1055 /* Give a warning about features that are not accessible */
1056 fprintf(stderr, "Note: file tpx version %d, software tpx version %d\n", file_version, tpx_version);
1059 if (file_version == 0)
1064 gmx::KeyValueTreeBuilder paramsBuilder;
1065 gmx::KeyValueTreeObjectBuilder paramsObj = paramsBuilder.rootObject();
1067 /* Basic inputrec stuff */
1068 serializer->doInt(&ir->eI);
1069 if (file_version >= 62)
1071 serializer->doInt64(&ir->nsteps);
1075 serializer->doInt(&idum);
1079 if (file_version >= 62)
1081 serializer->doInt64(&ir->init_step);
1085 serializer->doInt(&idum);
1086 ir->init_step = idum;
1089 serializer->doInt(&ir->simulation_part);
1091 if (file_version >= tpxv_MTS)
1093 serializer->doBool(&ir->useMts);
1094 int numLevels = ir->mtsLevels.size();
1097 serializer->doInt(&numLevels);
1099 ir->mtsLevels.resize(numLevels);
1100 for (auto& mtsLevel : ir->mtsLevels)
1102 int forceGroups = mtsLevel.forceGroups.to_ulong();
1103 serializer->doInt(&forceGroups);
1104 mtsLevel.forceGroups = std::bitset<static_cast<int>(gmx::MtsForceGroups::Count)>(forceGroups);
1105 serializer->doInt(&mtsLevel.stepFactor);
1111 ir->mtsLevels.clear();
1114 if (file_version >= 67)
1116 serializer->doInt(&ir->nstcalcenergy);
1120 ir->nstcalcenergy = 1;
1122 if (file_version >= 81)
1124 serializer->doInt(&ir->cutoff_scheme);
1125 if (file_version < 94)
1127 ir->cutoff_scheme = 1 - ir->cutoff_scheme;
1132 ir->cutoff_scheme = ecutsGROUP;
1134 serializer->doInt(&idum); /* used to be ns_type; not used anymore */
1135 serializer->doInt(&ir->nstlist);
1136 serializer->doInt(&idum); /* used to be ndelta; not used anymore */
1138 serializer->doReal(&ir->rtpi);
1140 serializer->doInt(&ir->nstcomm);
1141 serializer->doInt(&ir->comm_mode);
1143 /* ignore nstcheckpoint */
1144 if (file_version < tpxv_RemoveObsoleteParameters1)
1146 serializer->doInt(&idum);
1149 serializer->doInt(&ir->nstcgsteep);
1151 serializer->doInt(&ir->nbfgscorr);
1153 serializer->doInt(&ir->nstlog);
1154 serializer->doInt(&ir->nstxout);
1155 serializer->doInt(&ir->nstvout);
1156 serializer->doInt(&ir->nstfout);
1157 serializer->doInt(&ir->nstenergy);
1158 serializer->doInt(&ir->nstxout_compressed);
1159 if (file_version >= 59)
1161 serializer->doDouble(&ir->init_t);
1162 serializer->doDouble(&ir->delta_t);
1166 serializer->doReal(&rdum);
1168 serializer->doReal(&rdum);
1171 serializer->doReal(&ir->x_compression_precision);
1172 if (file_version >= 81)
1174 serializer->doReal(&ir->verletbuf_tol);
1178 ir->verletbuf_tol = 0;
1180 serializer->doReal(&ir->rlist);
1181 if (file_version >= 67 && file_version < tpxv_RemoveTwinRange)
1183 if (serializer->reading())
1185 // Reading such a file version could invoke the twin-range
1186 // scheme, about which mdrun should give a fatal error.
1187 real dummy_rlistlong = -1;
1188 serializer->doReal(&dummy_rlistlong);
1190 ir->useTwinRange = (ir->rlist > 0 && (dummy_rlistlong == 0 || dummy_rlistlong > ir->rlist));
1191 // When true, this forces mdrun to issue an error (regardless of
1192 // ir->cutoff_scheme).
1194 // Otherwise, grompp used to set rlistlong actively. Users
1195 // were probably also confused and set rlistlong == rlist.
1196 // However, in all remaining cases, it is safe to let
1197 // mdrun proceed normally.
1202 // No need to read or write anything
1203 ir->useTwinRange = false;
1205 if (file_version >= 82 && file_version != 90)
1207 // Multiple time-stepping is no longer enabled, but the old
1208 // support required the twin-range scheme, for which mdrun
1209 // already emits a fatal error.
1210 int dummy_nstcalclr = -1;
1211 serializer->doInt(&dummy_nstcalclr);
1213 serializer->doInt(&ir->coulombtype);
1214 if (file_version >= 81)
1216 serializer->doInt(&ir->coulomb_modifier);
1220 ir->coulomb_modifier = (ir->cutoff_scheme == ecutsVERLET ? eintmodPOTSHIFT : eintmodNONE);
1222 serializer->doReal(&ir->rcoulomb_switch);
1223 serializer->doReal(&ir->rcoulomb);
1224 serializer->doInt(&ir->vdwtype);
1225 if (file_version >= 81)
1227 serializer->doInt(&ir->vdw_modifier);
1231 ir->vdw_modifier = (ir->cutoff_scheme == ecutsVERLET ? eintmodPOTSHIFT : eintmodNONE);
1233 serializer->doReal(&ir->rvdw_switch);
1234 serializer->doReal(&ir->rvdw);
1235 serializer->doInt(&ir->eDispCorr);
1236 serializer->doReal(&ir->epsilon_r);
1237 serializer->doReal(&ir->epsilon_rf);
1238 serializer->doReal(&ir->tabext);
1240 // This permits reading a .tpr file that used implicit solvent,
1241 // and later permitting mdrun to refuse to run it.
1242 if (serializer->reading())
1244 if (file_version < tpxv_RemoveImplicitSolvation)
1246 serializer->doInt(&idum);
1247 serializer->doInt(&idum);
1248 serializer->doReal(&rdum);
1249 serializer->doReal(&rdum);
1250 serializer->doInt(&idum);
1251 ir->implicit_solvent = (idum > 0);
1255 ir->implicit_solvent = false;
1257 if (file_version < tpxv_RemoveImplicitSolvation)
1259 serializer->doReal(&rdum);
1260 serializer->doReal(&rdum);
1261 serializer->doReal(&rdum);
1262 serializer->doReal(&rdum);
1263 if (file_version >= 60)
1265 serializer->doReal(&rdum);
1266 serializer->doInt(&idum);
1268 serializer->doReal(&rdum);
1272 if (file_version >= 81)
1274 serializer->doReal(&ir->fourier_spacing);
1278 ir->fourier_spacing = 0.0;
1280 serializer->doInt(&ir->nkx);
1281 serializer->doInt(&ir->nky);
1282 serializer->doInt(&ir->nkz);
1283 serializer->doInt(&ir->pme_order);
1284 serializer->doReal(&ir->ewald_rtol);
1286 if (file_version >= 93)
1288 serializer->doReal(&ir->ewald_rtol_lj);
1292 ir->ewald_rtol_lj = ir->ewald_rtol;
1294 serializer->doInt(&ir->ewald_geometry);
1295 serializer->doReal(&ir->epsilon_surface);
1297 /* ignore bOptFFT */
1298 if (file_version < tpxv_RemoveObsoleteParameters1)
1300 serializer->doBool(&bdum);
1303 if (file_version >= 93)
1305 serializer->doInt(&ir->ljpme_combination_rule);
1307 serializer->doBool(&ir->bContinuation);
1308 serializer->doInt(&ir->etc);
1309 /* before version 18, ir->etc was a gmx_bool (ir->btc),
1310 * but the values 0 and 1 still mean no and
1311 * berendsen temperature coupling, respectively.
1313 if (file_version >= 79)
1315 serializer->doBool(&ir->bPrintNHChains);
1317 if (file_version >= 71)
1319 serializer->doInt(&ir->nsttcouple);
1323 ir->nsttcouple = ir->nstcalcenergy;
1325 serializer->doInt(&ir->epc);
1326 serializer->doInt(&ir->epct);
1327 if (file_version >= 71)
1329 serializer->doInt(&ir->nstpcouple);
1333 ir->nstpcouple = ir->nstcalcenergy;
1335 serializer->doReal(&ir->tau_p);
1336 serializer->doRvec(&ir->ref_p[XX]);
1337 serializer->doRvec(&ir->ref_p[YY]);
1338 serializer->doRvec(&ir->ref_p[ZZ]);
1339 serializer->doRvec(&ir->compress[XX]);
1340 serializer->doRvec(&ir->compress[YY]);
1341 serializer->doRvec(&ir->compress[ZZ]);
1342 serializer->doInt(&ir->refcoord_scaling);
1343 serializer->doRvec(&ir->posres_com);
1344 serializer->doRvec(&ir->posres_comB);
1346 if (file_version < 79)
1348 serializer->doInt(&ir->andersen_seed);
1352 ir->andersen_seed = 0;
1355 serializer->doReal(&ir->shake_tol);
1357 serializer->doInt(&ir->efep);
1358 do_fepvals(serializer, ir->fepvals, file_version);
1360 if (file_version >= 79)
1362 serializer->doBool(&ir->bSimTemp);
1365 ir->bSimTemp = TRUE;
1370 ir->bSimTemp = FALSE;
1374 do_simtempvals(serializer, ir->simtempvals, ir->fepvals->n_lambda, file_version);
1377 if (file_version >= 79)
1379 serializer->doBool(&ir->bExpanded);
1383 do_expandedvals(serializer, ir->expandedvals, ir->fepvals, file_version);
1386 serializer->doInt(&ir->eDisre);
1387 serializer->doInt(&ir->eDisreWeighting);
1388 serializer->doBool(&ir->bDisreMixed);
1389 serializer->doReal(&ir->dr_fc);
1390 serializer->doReal(&ir->dr_tau);
1391 serializer->doInt(&ir->nstdisreout);
1392 serializer->doReal(&ir->orires_fc);
1393 serializer->doReal(&ir->orires_tau);
1394 serializer->doInt(&ir->nstorireout);
1396 /* ignore dihre_fc */
1397 if (file_version < 79)
1399 serializer->doReal(&rdum);
1402 serializer->doReal(&ir->em_stepsize);
1403 serializer->doReal(&ir->em_tol);
1404 serializer->doBool(&ir->bShakeSOR);
1405 serializer->doInt(&ir->niter);
1406 serializer->doReal(&ir->fc_stepsize);
1407 serializer->doInt(&ir->eConstrAlg);
1408 serializer->doInt(&ir->nProjOrder);
1409 serializer->doReal(&ir->LincsWarnAngle);
1410 serializer->doInt(&ir->nLincsIter);
1411 serializer->doReal(&ir->bd_fric);
1412 if (file_version >= tpxv_Use64BitRandomSeed)
1414 serializer->doInt64(&ir->ld_seed);
1418 serializer->doInt(&idum);
1422 for (i = 0; i < DIM; i++)
1424 serializer->doRvec(&ir->deform[i]);
1426 serializer->doReal(&ir->cos_accel);
1428 serializer->doInt(&ir->userint1);
1429 serializer->doInt(&ir->userint2);
1430 serializer->doInt(&ir->userint3);
1431 serializer->doInt(&ir->userint4);
1432 serializer->doReal(&ir->userreal1);
1433 serializer->doReal(&ir->userreal2);
1434 serializer->doReal(&ir->userreal3);
1435 serializer->doReal(&ir->userreal4);
1437 /* AdResS is removed, but we need to be able to read old files,
1438 and let mdrun refuse to run them */
1439 if (file_version >= 77 && file_version < tpxv_RemoveAdress)
1441 serializer->doBool(&ir->bAdress);
1444 int idum, numThermoForceGroups, numEnergyGroups;
1447 serializer->doInt(&idum);
1448 serializer->doReal(&rdum);
1449 serializer->doReal(&rdum);
1450 serializer->doReal(&rdum);
1451 serializer->doInt(&idum);
1452 serializer->doInt(&idum);
1453 serializer->doRvec(&rvecdum);
1454 serializer->doInt(&numThermoForceGroups);
1455 serializer->doReal(&rdum);
1456 serializer->doInt(&numEnergyGroups);
1457 serializer->doInt(&idum);
1459 if (numThermoForceGroups > 0)
1461 std::vector<int> idumn(numThermoForceGroups);
1462 serializer->doIntArray(idumn.data(), idumn.size());
1464 if (numEnergyGroups > 0)
1466 std::vector<int> idumn(numEnergyGroups);
1467 serializer->doIntArray(idumn.data(), idumn.size());
1473 ir->bAdress = FALSE;
1480 if (file_version >= tpxv_PullCoordTypeGeom)
1482 serializer->doBool(&ir->bPull);
1486 serializer->doInt(&ePullOld);
1487 ir->bPull = (ePullOld > 0);
1488 /* We removed the first ePull=ePullNo for the enum */
1493 if (serializer->reading())
1495 ir->pull = std::make_unique<pull_params_t>();
1497 do_pull(serializer, ir->pull.get(), file_version, ePullOld);
1501 if (file_version >= tpxv_AcceleratedWeightHistogram)
1503 serializer->doBool(&ir->bDoAwh);
1507 if (serializer->reading())
1509 snew(ir->awhParams, 1);
1511 do_awh(serializer, ir->awhParams, file_version);
1519 /* Enforced rotation */
1520 if (file_version >= 74)
1522 serializer->doBool(&ir->bRot);
1525 if (serializer->reading())
1529 do_rot(serializer, ir->rot);
1537 /* Interactive molecular dynamics */
1538 if (file_version >= tpxv_InteractiveMolecularDynamics)
1540 serializer->doBool(&ir->bIMD);
1543 if (serializer->reading())
1547 do_imd(serializer, ir->imd);
1552 /* We don't support IMD sessions for old .tpr files */
1557 serializer->doInt(&ir->opts.ngtc);
1558 if (file_version >= 69)
1560 serializer->doInt(&ir->opts.nhchainlength);
1564 ir->opts.nhchainlength = 1;
1566 serializer->doInt(&ir->opts.ngacc);
1567 serializer->doInt(&ir->opts.ngfrz);
1568 serializer->doInt(&ir->opts.ngener);
1570 if (serializer->reading())
1572 snew(ir->opts.nrdf, ir->opts.ngtc);
1573 snew(ir->opts.ref_t, ir->opts.ngtc);
1574 snew(ir->opts.annealing, ir->opts.ngtc);
1575 snew(ir->opts.anneal_npoints, ir->opts.ngtc);
1576 snew(ir->opts.anneal_time, ir->opts.ngtc);
1577 snew(ir->opts.anneal_temp, ir->opts.ngtc);
1578 snew(ir->opts.tau_t, ir->opts.ngtc);
1579 snew(ir->opts.nFreeze, ir->opts.ngfrz);
1580 snew(ir->opts.acc, ir->opts.ngacc);
1581 snew(ir->opts.egp_flags, ir->opts.ngener * ir->opts.ngener);
1583 if (ir->opts.ngtc > 0)
1585 serializer->doRealArray(ir->opts.nrdf, ir->opts.ngtc);
1586 serializer->doRealArray(ir->opts.ref_t, ir->opts.ngtc);
1587 serializer->doRealArray(ir->opts.tau_t, ir->opts.ngtc);
1589 if (ir->opts.ngfrz > 0)
1591 serializer->doIvecArray(ir->opts.nFreeze, ir->opts.ngfrz);
1593 if (ir->opts.ngacc > 0)
1595 serializer->doRvecArray(ir->opts.acc, ir->opts.ngacc);
1597 serializer->doIntArray(ir->opts.egp_flags, ir->opts.ngener * ir->opts.ngener);
1599 /* First read the lists with annealing and npoints for each group */
1600 serializer->doIntArray(ir->opts.annealing, ir->opts.ngtc);
1601 serializer->doIntArray(ir->opts.anneal_npoints, ir->opts.ngtc);
1602 for (j = 0; j < (ir->opts.ngtc); j++)
1604 k = ir->opts.anneal_npoints[j];
1605 if (serializer->reading())
1607 snew(ir->opts.anneal_time[j], k);
1608 snew(ir->opts.anneal_temp[j], k);
1610 serializer->doRealArray(ir->opts.anneal_time[j], k);
1611 serializer->doRealArray(ir->opts.anneal_temp[j], k);
1615 serializer->doInt(&ir->nwall);
1616 serializer->doInt(&ir->wall_type);
1617 serializer->doReal(&ir->wall_r_linpot);
1618 serializer->doInt(&ir->wall_atomtype[0]);
1619 serializer->doInt(&ir->wall_atomtype[1]);
1620 serializer->doReal(&ir->wall_density[0]);
1621 serializer->doReal(&ir->wall_density[1]);
1622 serializer->doReal(&ir->wall_ewald_zfac);
1625 /* Cosine stuff for electric fields */
1626 if (file_version < tpxv_GenericParamsForElectricField)
1628 do_legacy_efield(serializer, ¶msObj);
1632 if (file_version >= tpxv_ComputationalElectrophysiology)
1634 serializer->doInt(&ir->eSwapCoords);
1635 if (ir->eSwapCoords != eswapNO)
1637 if (serializer->reading())
1641 do_swapcoords_tpx(serializer, ir->swap, file_version);
1645 /* QMMM reading - despite defunct we require reading for backwards
1646 * compability and correct serialization
1649 serializer->doBool(&ir->bQMMM);
1651 serializer->doInt(&qmmmScheme);
1652 real unusedScalefactor;
1653 serializer->doReal(&unusedScalefactor);
1655 // this is still used in Mimic
1656 serializer->doInt(&ir->opts.ngQM);
1657 if (ir->opts.ngQM > 0 && ir->bQMMM)
1659 /* We leave in dummy i/o for removed parameters to avoid
1660 * changing the tpr format.
1662 std::vector<int> dummyIntVec(4 * ir->opts.ngQM, 0);
1663 serializer->doIntArray(dummyIntVec.data(), dummyIntVec.size());
1664 dummyIntVec.clear();
1666 // std::vector<bool> has no data()
1667 std::vector<char> dummyBoolVec(ir->opts.ngQM * sizeof(bool) / sizeof(char));
1668 serializer->doBoolArray(reinterpret_cast<bool*>(dummyBoolVec.data()), dummyBoolVec.size());
1669 dummyBoolVec.clear();
1671 dummyIntVec.resize(2 * ir->opts.ngQM, 0);
1672 serializer->doIntArray(dummyIntVec.data(), dummyIntVec.size());
1673 dummyIntVec.clear();
1675 std::vector<real> dummyRealVec(2 * ir->opts.ngQM, 0);
1676 serializer->doRealArray(dummyRealVec.data(), dummyRealVec.size());
1677 dummyRealVec.clear();
1679 dummyIntVec.resize(3 * ir->opts.ngQM, 0);
1680 serializer->doIntArray(dummyIntVec.data(), dummyIntVec.size());
1681 dummyIntVec.clear();
1683 /* end of QMMM stuff */
1686 if (file_version >= tpxv_GenericParamsForElectricField)
1688 if (serializer->reading())
1690 paramsObj.mergeObject(gmx::deserializeKeyValueTree(serializer));
1694 GMX_RELEASE_ASSERT(ir->params != nullptr,
1695 "Parameters should be present when writing inputrec");
1696 gmx::serializeKeyValueTree(*ir->params, serializer);
1699 if (serializer->reading())
1701 ir->params = new gmx::KeyValueTreeObject(paramsBuilder.build());
1702 // Initialize internal parameters to an empty kvt for all tpr versions
1703 ir->internalParameters = std::make_unique<gmx::KeyValueTreeObject>();
1706 if (file_version >= tpxv_GenericInternalParameters)
1708 if (serializer->reading())
1710 ir->internalParameters =
1711 std::make_unique<gmx::KeyValueTreeObject>(gmx::deserializeKeyValueTree(serializer));
1715 GMX_RELEASE_ASSERT(ir->internalParameters != nullptr,
1716 "Parameters should be present when writing inputrec");
1717 gmx::serializeKeyValueTree(*ir->internalParameters, serializer);
1723 static void do_harm(gmx::ISerializer* serializer, t_iparams* iparams)
1725 serializer->doReal(&iparams->harmonic.rA);
1726 serializer->doReal(&iparams->harmonic.krA);
1727 serializer->doReal(&iparams->harmonic.rB);
1728 serializer->doReal(&iparams->harmonic.krB);
1731 static void do_iparams(gmx::ISerializer* serializer, t_functype ftype, t_iparams* iparams, int file_version)
1744 do_harm(serializer, iparams);
1745 if ((ftype == F_ANGRES || ftype == F_ANGRESZ) && serializer->reading())
1747 /* Correct incorrect storage of parameters */
1748 iparams->pdihs.phiB = iparams->pdihs.phiA;
1749 iparams->pdihs.cpB = iparams->pdihs.cpA;
1753 serializer->doReal(&iparams->harmonic.rA);
1754 serializer->doReal(&iparams->harmonic.krA);
1756 case F_LINEAR_ANGLES:
1757 serializer->doReal(&iparams->linangle.klinA);
1758 serializer->doReal(&iparams->linangle.aA);
1759 serializer->doReal(&iparams->linangle.klinB);
1760 serializer->doReal(&iparams->linangle.aB);
1763 serializer->doReal(&iparams->fene.bm);
1764 serializer->doReal(&iparams->fene.kb);
1768 serializer->doReal(&iparams->restraint.lowA);
1769 serializer->doReal(&iparams->restraint.up1A);
1770 serializer->doReal(&iparams->restraint.up2A);
1771 serializer->doReal(&iparams->restraint.kA);
1772 serializer->doReal(&iparams->restraint.lowB);
1773 serializer->doReal(&iparams->restraint.up1B);
1774 serializer->doReal(&iparams->restraint.up2B);
1775 serializer->doReal(&iparams->restraint.kB);
1781 serializer->doReal(&iparams->tab.kA);
1782 serializer->doInt(&iparams->tab.table);
1783 serializer->doReal(&iparams->tab.kB);
1785 case F_CROSS_BOND_BONDS:
1786 serializer->doReal(&iparams->cross_bb.r1e);
1787 serializer->doReal(&iparams->cross_bb.r2e);
1788 serializer->doReal(&iparams->cross_bb.krr);
1790 case F_CROSS_BOND_ANGLES:
1791 serializer->doReal(&iparams->cross_ba.r1e);
1792 serializer->doReal(&iparams->cross_ba.r2e);
1793 serializer->doReal(&iparams->cross_ba.r3e);
1794 serializer->doReal(&iparams->cross_ba.krt);
1796 case F_UREY_BRADLEY:
1797 serializer->doReal(&iparams->u_b.thetaA);
1798 serializer->doReal(&iparams->u_b.kthetaA);
1799 serializer->doReal(&iparams->u_b.r13A);
1800 serializer->doReal(&iparams->u_b.kUBA);
1801 if (file_version >= 79)
1803 serializer->doReal(&iparams->u_b.thetaB);
1804 serializer->doReal(&iparams->u_b.kthetaB);
1805 serializer->doReal(&iparams->u_b.r13B);
1806 serializer->doReal(&iparams->u_b.kUBB);
1810 iparams->u_b.thetaB = iparams->u_b.thetaA;
1811 iparams->u_b.kthetaB = iparams->u_b.kthetaA;
1812 iparams->u_b.r13B = iparams->u_b.r13A;
1813 iparams->u_b.kUBB = iparams->u_b.kUBA;
1816 case F_QUARTIC_ANGLES:
1817 serializer->doReal(&iparams->qangle.theta);
1818 serializer->doRealArray(iparams->qangle.c, 5);
1821 serializer->doReal(&iparams->bham.a);
1822 serializer->doReal(&iparams->bham.b);
1823 serializer->doReal(&iparams->bham.c);
1826 serializer->doReal(&iparams->morse.b0A);
1827 serializer->doReal(&iparams->morse.cbA);
1828 serializer->doReal(&iparams->morse.betaA);
1829 if (file_version >= 79)
1831 serializer->doReal(&iparams->morse.b0B);
1832 serializer->doReal(&iparams->morse.cbB);
1833 serializer->doReal(&iparams->morse.betaB);
1837 iparams->morse.b0B = iparams->morse.b0A;
1838 iparams->morse.cbB = iparams->morse.cbA;
1839 iparams->morse.betaB = iparams->morse.betaA;
1843 serializer->doReal(&iparams->cubic.b0);
1844 serializer->doReal(&iparams->cubic.kb);
1845 serializer->doReal(&iparams->cubic.kcub);
1847 case F_CONNBONDS: break;
1848 case F_POLARIZATION: serializer->doReal(&iparams->polarize.alpha); break;
1850 serializer->doReal(&iparams->anharm_polarize.alpha);
1851 serializer->doReal(&iparams->anharm_polarize.drcut);
1852 serializer->doReal(&iparams->anharm_polarize.khyp);
1855 serializer->doReal(&iparams->wpol.al_x);
1856 serializer->doReal(&iparams->wpol.al_y);
1857 serializer->doReal(&iparams->wpol.al_z);
1858 serializer->doReal(&iparams->wpol.rOH);
1859 serializer->doReal(&iparams->wpol.rHH);
1860 serializer->doReal(&iparams->wpol.rOD);
1863 serializer->doReal(&iparams->thole.a);
1864 serializer->doReal(&iparams->thole.alpha1);
1865 serializer->doReal(&iparams->thole.alpha2);
1866 serializer->doReal(&iparams->thole.rfac);
1869 serializer->doReal(&iparams->lj.c6);
1870 serializer->doReal(&iparams->lj.c12);
1873 serializer->doReal(&iparams->lj14.c6A);
1874 serializer->doReal(&iparams->lj14.c12A);
1875 serializer->doReal(&iparams->lj14.c6B);
1876 serializer->doReal(&iparams->lj14.c12B);
1879 serializer->doReal(&iparams->ljc14.fqq);
1880 serializer->doReal(&iparams->ljc14.qi);
1881 serializer->doReal(&iparams->ljc14.qj);
1882 serializer->doReal(&iparams->ljc14.c6);
1883 serializer->doReal(&iparams->ljc14.c12);
1885 case F_LJC_PAIRS_NB:
1886 serializer->doReal(&iparams->ljcnb.qi);
1887 serializer->doReal(&iparams->ljcnb.qj);
1888 serializer->doReal(&iparams->ljcnb.c6);
1889 serializer->doReal(&iparams->ljcnb.c12);
1895 serializer->doReal(&iparams->pdihs.phiA);
1896 serializer->doReal(&iparams->pdihs.cpA);
1897 serializer->doReal(&iparams->pdihs.phiB);
1898 serializer->doReal(&iparams->pdihs.cpB);
1899 serializer->doInt(&iparams->pdihs.mult);
1902 serializer->doReal(&iparams->pdihs.phiA);
1903 serializer->doReal(&iparams->pdihs.cpA);
1906 serializer->doInt(&iparams->disres.label);
1907 serializer->doInt(&iparams->disres.type);
1908 serializer->doReal(&iparams->disres.low);
1909 serializer->doReal(&iparams->disres.up1);
1910 serializer->doReal(&iparams->disres.up2);
1911 serializer->doReal(&iparams->disres.kfac);
1914 serializer->doInt(&iparams->orires.ex);
1915 serializer->doInt(&iparams->orires.label);
1916 serializer->doInt(&iparams->orires.power);
1917 serializer->doReal(&iparams->orires.c);
1918 serializer->doReal(&iparams->orires.obs);
1919 serializer->doReal(&iparams->orires.kfac);
1922 if (file_version < 82)
1924 serializer->doInt(&idum);
1925 serializer->doInt(&idum);
1927 serializer->doReal(&iparams->dihres.phiA);
1928 serializer->doReal(&iparams->dihres.dphiA);
1929 serializer->doReal(&iparams->dihres.kfacA);
1930 if (file_version >= 82)
1932 serializer->doReal(&iparams->dihres.phiB);
1933 serializer->doReal(&iparams->dihres.dphiB);
1934 serializer->doReal(&iparams->dihres.kfacB);
1938 iparams->dihres.phiB = iparams->dihres.phiA;
1939 iparams->dihres.dphiB = iparams->dihres.dphiA;
1940 iparams->dihres.kfacB = iparams->dihres.kfacA;
1944 serializer->doRvec(&iparams->posres.pos0A);
1945 serializer->doRvec(&iparams->posres.fcA);
1946 serializer->doRvec(&iparams->posres.pos0B);
1947 serializer->doRvec(&iparams->posres.fcB);
1950 serializer->doInt(&iparams->fbposres.geom);
1951 serializer->doRvec(&iparams->fbposres.pos0);
1952 serializer->doReal(&iparams->fbposres.r);
1953 serializer->doReal(&iparams->fbposres.k);
1955 case F_CBTDIHS: serializer->doRealArray(iparams->cbtdihs.cbtcA, NR_CBTDIHS); break;
1957 // Fall-through intended
1959 /* Fourier dihedrals are internally represented
1960 * as Ryckaert-Bellemans since those are faster to compute.
1962 serializer->doRealArray(iparams->rbdihs.rbcA, NR_RBDIHS);
1963 serializer->doRealArray(iparams->rbdihs.rbcB, NR_RBDIHS);
1967 serializer->doReal(&iparams->constr.dA);
1968 serializer->doReal(&iparams->constr.dB);
1971 serializer->doReal(&iparams->settle.doh);
1972 serializer->doReal(&iparams->settle.dhh);
1974 case F_VSITE1: break; // VSite1 has 0 parameters
1976 case F_VSITE2FD: serializer->doReal(&iparams->vsite.a); break;
1980 serializer->doReal(&iparams->vsite.a);
1981 serializer->doReal(&iparams->vsite.b);
1986 serializer->doReal(&iparams->vsite.a);
1987 serializer->doReal(&iparams->vsite.b);
1988 serializer->doReal(&iparams->vsite.c);
1991 serializer->doInt(&iparams->vsiten.n);
1992 serializer->doReal(&iparams->vsiten.a);
1994 case F_GB12_NOLONGERUSED:
1995 case F_GB13_NOLONGERUSED:
1996 case F_GB14_NOLONGERUSED:
1997 // Implicit solvent parameters can still be read, but never used
1998 if (serializer->reading())
2000 if (file_version < 68)
2002 serializer->doReal(&rdum);
2003 serializer->doReal(&rdum);
2004 serializer->doReal(&rdum);
2005 serializer->doReal(&rdum);
2007 if (file_version < tpxv_RemoveImplicitSolvation)
2009 serializer->doReal(&rdum);
2010 serializer->doReal(&rdum);
2011 serializer->doReal(&rdum);
2012 serializer->doReal(&rdum);
2013 serializer->doReal(&rdum);
2018 serializer->doInt(&iparams->cmap.cmapA);
2019 serializer->doInt(&iparams->cmap.cmapB);
2022 gmx_fatal(FARGS, "unknown function type %d (%s) in %s line %d", ftype,
2023 interaction_function[ftype].name, __FILE__, __LINE__);
2027 static void do_ilist(gmx::ISerializer* serializer, InteractionList* ilist)
2029 int nr = ilist->size();
2030 serializer->doInt(&nr);
2031 if (serializer->reading())
2033 ilist->iatoms.resize(nr);
2035 serializer->doIntArray(ilist->iatoms.data(), ilist->size());
2038 static void do_ffparams(gmx::ISerializer* serializer, gmx_ffparams_t* ffparams, int file_version)
2040 serializer->doInt(&ffparams->atnr);
2041 int numTypes = ffparams->numTypes();
2042 serializer->doInt(&numTypes);
2043 if (serializer->reading())
2045 ffparams->functype.resize(numTypes);
2046 ffparams->iparams.resize(numTypes);
2048 /* Read/write all the function types */
2049 serializer->doIntArray(ffparams->functype.data(), ffparams->functype.size());
2051 if (file_version >= 66)
2053 serializer->doDouble(&ffparams->reppow);
2057 ffparams->reppow = 12.0;
2060 serializer->doReal(&ffparams->fudgeQQ);
2062 /* Check whether all these function types are supported by the code.
2063 * In practice the code is backwards compatible, which means that the
2064 * numbering may have to be altered from old numbering to new numbering
2066 for (int i = 0; i < ffparams->numTypes(); i++)
2068 if (serializer->reading())
2070 /* Loop over file versions */
2071 for (int k = 0; k < NFTUPD; k++)
2073 /* Compare the read file_version to the update table */
2074 if ((file_version < ftupd[k].fvnr) && (ffparams->functype[i] >= ftupd[k].ftype))
2076 ffparams->functype[i] += 1;
2081 do_iparams(serializer, ffparams->functype[i], &ffparams->iparams[i], file_version);
2085 static void add_settle_atoms(InteractionList* ilist)
2089 /* Settle used to only store the first atom: add the other two */
2090 ilist->iatoms.resize(2 * ilist->size());
2091 for (i = ilist->size() / 4 - 1; i >= 0; i--)
2093 ilist->iatoms[4 * i + 0] = ilist->iatoms[2 * i + 0];
2094 ilist->iatoms[4 * i + 1] = ilist->iatoms[2 * i + 1];
2095 ilist->iatoms[4 * i + 2] = ilist->iatoms[2 * i + 1] + 1;
2096 ilist->iatoms[4 * i + 3] = ilist->iatoms[2 * i + 1] + 2;
2100 static void do_ilists(gmx::ISerializer* serializer, InteractionLists* ilists, int file_version)
2102 GMX_RELEASE_ASSERT(ilists, "Need a valid ilists object");
2103 GMX_RELEASE_ASSERT(ilists->size() == F_NRE,
2104 "The code needs to be in sync with InteractionLists");
2106 for (int j = 0; j < F_NRE; j++)
2108 InteractionList& ilist = (*ilists)[j];
2109 gmx_bool bClear = FALSE;
2110 if (serializer->reading())
2112 for (int k = 0; k < NFTUPD; k++)
2114 if ((file_version < ftupd[k].fvnr) && (j == ftupd[k].ftype))
2122 ilist.iatoms.clear();
2126 do_ilist(serializer, &ilist);
2127 if (file_version < 78 && j == F_SETTLE && !ilist.empty())
2129 add_settle_atoms(&ilist);
2135 static void do_block(gmx::ISerializer* serializer, t_block* block)
2137 serializer->doInt(&block->nr);
2138 if (serializer->reading())
2140 if ((block->nalloc_index > 0) && (nullptr != block->index))
2142 sfree(block->index);
2144 block->nalloc_index = block->nr + 1;
2145 snew(block->index, block->nalloc_index);
2147 serializer->doIntArray(block->index, block->nr + 1);
2150 static void doListOfLists(gmx::ISerializer* serializer, gmx::ListOfLists<int>* listOfLists)
2152 int numLists = listOfLists->ssize();
2153 serializer->doInt(&numLists);
2154 int numElements = listOfLists->elementsView().ssize();
2155 serializer->doInt(&numElements);
2156 if (serializer->reading())
2158 std::vector<int> listRanges(numLists + 1);
2159 serializer->doIntArray(listRanges.data(), numLists + 1);
2160 std::vector<int> elements(numElements);
2161 serializer->doIntArray(elements.data(), numElements);
2162 *listOfLists = gmx::ListOfLists<int>(std::move(listRanges), std::move(elements));
2166 serializer->doIntArray(const_cast<int*>(listOfLists->listRangesView().data()), numLists + 1);
2167 serializer->doIntArray(const_cast<int*>(listOfLists->elementsView().data()), numElements);
2171 /* This is a primitive routine to make it possible to translate atomic numbers
2172 * to element names when reading TPR files, without making the Gromacs library
2173 * directory a dependency on mdrun (which is the case if we need elements.dat).
2175 static const char* atomicnumber_to_element(int atomicnumber)
2179 /* This does not have to be complete, so we only include elements likely
2180 * to occur in PDB files.
2182 switch (atomicnumber)
2184 case 1: p = "H"; break;
2185 case 5: p = "B"; break;
2186 case 6: p = "C"; break;
2187 case 7: p = "N"; break;
2188 case 8: p = "O"; break;
2189 case 9: p = "F"; break;
2190 case 11: p = "Na"; break;
2191 case 12: p = "Mg"; break;
2192 case 15: p = "P"; break;
2193 case 16: p = "S"; break;
2194 case 17: p = "Cl"; break;
2195 case 18: p = "Ar"; break;
2196 case 19: p = "K"; break;
2197 case 20: p = "Ca"; break;
2198 case 25: p = "Mn"; break;
2199 case 26: p = "Fe"; break;
2200 case 28: p = "Ni"; break;
2201 case 29: p = "Cu"; break;
2202 case 30: p = "Zn"; break;
2203 case 35: p = "Br"; break;
2204 case 47: p = "Ag"; break;
2205 default: p = ""; break;
2211 static void do_atom(gmx::ISerializer* serializer, t_atom* atom)
2213 serializer->doReal(&atom->m);
2214 serializer->doReal(&atom->q);
2215 serializer->doReal(&atom->mB);
2216 serializer->doReal(&atom->qB);
2217 serializer->doUShort(&atom->type);
2218 serializer->doUShort(&atom->typeB);
2219 serializer->doInt(&atom->ptype);
2220 serializer->doInt(&atom->resind);
2221 serializer->doInt(&atom->atomnumber);
2222 if (serializer->reading())
2224 /* Set element string from atomic number if present.
2225 * This routine returns an empty string if the name is not found.
2227 std::strncpy(atom->elem, atomicnumber_to_element(atom->atomnumber), 4);
2228 /* avoid warnings about potentially unterminated string */
2229 atom->elem[3] = '\0';
2233 static void do_grps(gmx::ISerializer* serializer, gmx::ArrayRef<AtomGroupIndices> grps)
2235 for (auto& group : grps)
2237 int size = group.size();
2238 serializer->doInt(&size);
2239 if (serializer->reading())
2243 serializer->doIntArray(group.data(), size);
2247 static void do_symstr(gmx::ISerializer* serializer, char*** nm, t_symtab* symtab)
2251 if (serializer->reading())
2253 serializer->doInt(&ls);
2254 *nm = get_symtab_handle(symtab, ls);
2258 ls = lookup_symtab(symtab, *nm);
2259 serializer->doInt(&ls);
2263 static void do_strstr(gmx::ISerializer* serializer, int nstr, char*** nm, t_symtab* symtab)
2267 for (j = 0; (j < nstr); j++)
2269 do_symstr(serializer, &(nm[j]), symtab);
2273 static void do_resinfo(gmx::ISerializer* serializer, int n, t_resinfo* ri, t_symtab* symtab, int file_version)
2277 for (j = 0; (j < n); j++)
2279 do_symstr(serializer, &(ri[j].name), symtab);
2280 if (file_version >= 63)
2282 serializer->doInt(&ri[j].nr);
2283 serializer->doUChar(&ri[j].ic);
2293 static void do_atoms(gmx::ISerializer* serializer, t_atoms* atoms, t_symtab* symtab, int file_version)
2297 serializer->doInt(&atoms->nr);
2298 serializer->doInt(&atoms->nres);
2299 if (serializer->reading())
2301 /* Since we have always written all t_atom properties in the tpr file
2302 * (at least for all backward compatible versions), we don't store
2303 * but simple set the booleans here.
2305 atoms->haveMass = TRUE;
2306 atoms->haveCharge = TRUE;
2307 atoms->haveType = TRUE;
2308 atoms->haveBState = TRUE;
2309 atoms->havePdbInfo = FALSE;
2311 snew(atoms->atom, atoms->nr);
2312 snew(atoms->atomname, atoms->nr);
2313 snew(atoms->atomtype, atoms->nr);
2314 snew(atoms->atomtypeB, atoms->nr);
2315 snew(atoms->resinfo, atoms->nres);
2316 atoms->pdbinfo = nullptr;
2320 GMX_RELEASE_ASSERT(atoms->haveMass && atoms->haveCharge && atoms->haveType && atoms->haveBState,
2321 "Mass, charge, atomtype and B-state parameters should be present in "
2322 "t_atoms when writing a tpr file");
2324 for (i = 0; (i < atoms->nr); i++)
2326 do_atom(serializer, &atoms->atom[i]);
2328 do_strstr(serializer, atoms->nr, atoms->atomname, symtab);
2329 do_strstr(serializer, atoms->nr, atoms->atomtype, symtab);
2330 do_strstr(serializer, atoms->nr, atoms->atomtypeB, symtab);
2332 do_resinfo(serializer, atoms->nres, atoms->resinfo, symtab, file_version);
2335 static void do_groups(gmx::ISerializer* serializer, SimulationGroups* groups, t_symtab* symtab)
2337 do_grps(serializer, groups->groups);
2338 int numberOfGroupNames = groups->groupNames.size();
2339 serializer->doInt(&numberOfGroupNames);
2340 if (serializer->reading())
2342 groups->groupNames.resize(numberOfGroupNames);
2344 do_strstr(serializer, numberOfGroupNames, groups->groupNames.data(), symtab);
2345 for (auto group : gmx::keysOf(groups->groupNumbers))
2347 int numberOfGroupNumbers = groups->numberOfGroupNumbers(group);
2348 serializer->doInt(&numberOfGroupNumbers);
2349 if (numberOfGroupNumbers != 0)
2351 if (serializer->reading())
2353 groups->groupNumbers[group].resize(numberOfGroupNumbers);
2355 serializer->doUCharArray(groups->groupNumbers[group].data(), numberOfGroupNumbers);
2360 static void do_atomtypes(gmx::ISerializer* serializer, t_atomtypes* atomtypes, int file_version)
2364 serializer->doInt(&atomtypes->nr);
2366 if (serializer->reading())
2368 snew(atomtypes->atomnumber, j);
2370 if (serializer->reading() && file_version < tpxv_RemoveImplicitSolvation)
2372 std::vector<real> dummy(atomtypes->nr, 0);
2373 serializer->doRealArray(dummy.data(), dummy.size());
2374 serializer->doRealArray(dummy.data(), dummy.size());
2375 serializer->doRealArray(dummy.data(), dummy.size());
2377 serializer->doIntArray(atomtypes->atomnumber, j);
2379 if (serializer->reading() && file_version >= 60 && file_version < tpxv_RemoveImplicitSolvation)
2381 std::vector<real> dummy(atomtypes->nr, 0);
2382 serializer->doRealArray(dummy.data(), dummy.size());
2383 serializer->doRealArray(dummy.data(), dummy.size());
2387 static void do_symtab(gmx::ISerializer* serializer, t_symtab* symtab)
2392 serializer->doInt(&symtab->nr);
2394 if (serializer->reading())
2396 snew(symtab->symbuf, 1);
2397 symbuf = symtab->symbuf;
2398 symbuf->bufsize = nr;
2399 snew(symbuf->buf, nr);
2400 for (i = 0; (i < nr); i++)
2403 serializer->doString(&buf);
2404 symbuf->buf[i] = gmx_strdup(buf.c_str());
2409 symbuf = symtab->symbuf;
2410 while (symbuf != nullptr)
2412 for (i = 0; (i < symbuf->bufsize) && (i < nr); i++)
2414 std::string buf = symbuf->buf[i];
2415 serializer->doString(&buf);
2418 symbuf = symbuf->next;
2422 gmx_fatal(FARGS, "nr of symtab strings left: %d", nr);
2427 static void do_cmap(gmx::ISerializer* serializer, gmx_cmap_t* cmap_grid)
2430 int ngrid = cmap_grid->cmapdata.size();
2431 serializer->doInt(&ngrid);
2432 serializer->doInt(&cmap_grid->grid_spacing);
2434 int gs = cmap_grid->grid_spacing;
2435 int nelem = gs * gs;
2437 if (serializer->reading())
2439 cmap_grid->cmapdata.resize(ngrid);
2441 for (int i = 0; i < ngrid; i++)
2443 cmap_grid->cmapdata[i].cmap.resize(4 * nelem);
2447 for (int i = 0; i < ngrid; i++)
2449 for (int j = 0; j < nelem; j++)
2451 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j * 4]);
2452 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j * 4 + 1]);
2453 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j * 4 + 2]);
2454 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j * 4 + 3]);
2460 static void do_moltype(gmx::ISerializer* serializer, gmx_moltype_t* molt, t_symtab* symtab, int file_version)
2462 do_symstr(serializer, &(molt->name), symtab);
2464 do_atoms(serializer, &molt->atoms, symtab, file_version);
2466 do_ilists(serializer, &molt->ilist, file_version);
2468 /* TODO: Remove the obsolete charge group index from the file */
2470 cgs.nr = molt->atoms.nr;
2471 cgs.nalloc_index = cgs.nr + 1;
2472 snew(cgs.index, cgs.nalloc_index);
2473 for (int i = 0; i < cgs.nr + 1; i++)
2477 do_block(serializer, &cgs);
2480 /* This used to be in the atoms struct */
2481 doListOfLists(serializer, &molt->excls);
2484 static void do_molblock(gmx::ISerializer* serializer, gmx_molblock_t* molb, int numAtomsPerMolecule)
2486 serializer->doInt(&molb->type);
2487 serializer->doInt(&molb->nmol);
2488 /* To maintain forward topology reading compatibility, we store #atoms.
2489 * TODO: Change this to conditional reading of a dummy int when we
2490 * increase tpx_generation.
2492 serializer->doInt(&numAtomsPerMolecule);
2493 /* Position restraint coordinates */
2494 int numPosres_xA = molb->posres_xA.size();
2495 serializer->doInt(&numPosres_xA);
2496 if (numPosres_xA > 0)
2498 if (serializer->reading())
2500 molb->posres_xA.resize(numPosres_xA);
2502 serializer->doRvecArray(as_rvec_array(molb->posres_xA.data()), numPosres_xA);
2504 int numPosres_xB = molb->posres_xB.size();
2505 serializer->doInt(&numPosres_xB);
2506 if (numPosres_xB > 0)
2508 if (serializer->reading())
2510 molb->posres_xB.resize(numPosres_xB);
2512 serializer->doRvecArray(as_rvec_array(molb->posres_xB.data()), numPosres_xB);
2516 static void set_disres_npair(gmx_mtop_t* mtop)
2518 gmx_mtop_ilistloop_t iloop;
2521 gmx::ArrayRef<t_iparams> ip = mtop->ffparams.iparams;
2523 iloop = gmx_mtop_ilistloop_init(mtop);
2524 while (const InteractionLists* ilist = gmx_mtop_ilistloop_next(iloop, &nmol))
2526 const InteractionList& il = (*ilist)[F_DISRES];
2530 gmx::ArrayRef<const int> a = il.iatoms;
2532 for (int i = 0; i < il.size(); i += 3)
2535 if (i + 3 == il.size() || ip[a[i]].disres.label != ip[a[i + 3]].disres.label)
2537 ip[a[i]].disres.npair = npair;
2545 static void do_mtop(gmx::ISerializer* serializer, gmx_mtop_t* mtop, int file_version)
2547 do_symtab(serializer, &(mtop->symtab));
2549 do_symstr(serializer, &(mtop->name), &(mtop->symtab));
2551 do_ffparams(serializer, &mtop->ffparams, file_version);
2553 int nmoltype = mtop->moltype.size();
2554 serializer->doInt(&nmoltype);
2555 if (serializer->reading())
2557 mtop->moltype.resize(nmoltype);
2559 for (gmx_moltype_t& moltype : mtop->moltype)
2561 do_moltype(serializer, &moltype, &mtop->symtab, file_version);
2564 int nmolblock = mtop->molblock.size();
2565 serializer->doInt(&nmolblock);
2566 if (serializer->reading())
2568 mtop->molblock.resize(nmolblock);
2570 for (gmx_molblock_t& molblock : mtop->molblock)
2572 int numAtomsPerMolecule = (serializer->reading() ? 0 : mtop->moltype[molblock.type].atoms.nr);
2573 do_molblock(serializer, &molblock, numAtomsPerMolecule);
2575 serializer->doInt(&mtop->natoms);
2577 if (file_version >= tpxv_IntermolecularBondeds)
2579 serializer->doBool(&mtop->bIntermolecularInteractions);
2580 if (mtop->bIntermolecularInteractions)
2582 if (serializer->reading())
2584 mtop->intermolecular_ilist = std::make_unique<InteractionLists>();
2586 do_ilists(serializer, mtop->intermolecular_ilist.get(), file_version);
2591 mtop->bIntermolecularInteractions = FALSE;
2594 do_atomtypes(serializer, &(mtop->atomtypes), file_version);
2596 if (file_version >= 65)
2598 do_cmap(serializer, &mtop->ffparams.cmap_grid);
2602 mtop->ffparams.cmap_grid.grid_spacing = 0;
2603 mtop->ffparams.cmap_grid.cmapdata.clear();
2606 do_groups(serializer, &mtop->groups, &(mtop->symtab));
2608 mtop->haveMoleculeIndices = true;
2610 if (file_version >= tpxv_StoreNonBondedInteractionExclusionGroup)
2612 std::int64_t intermolecularExclusionGroupSize = gmx::ssize(mtop->intermolecularExclusionGroup);
2613 serializer->doInt64(&intermolecularExclusionGroupSize);
2614 GMX_RELEASE_ASSERT(intermolecularExclusionGroupSize >= 0,
2615 "Number of atoms with excluded intermolecular non-bonded interactions "
2617 mtop->intermolecularExclusionGroup.resize(intermolecularExclusionGroupSize); // no effect when writing
2618 serializer->doIntArray(mtop->intermolecularExclusionGroup.data(),
2619 mtop->intermolecularExclusionGroup.size());
2622 if (serializer->reading())
2624 close_symtab(&(mtop->symtab));
2629 * Read the first part of the TPR file to find general system information.
2631 * If \p TopOnlyOK is true then we can read even future versions
2632 * of tpx files, provided the \p fileGeneration hasn't changed.
2633 * If it is false, we need the \p ir too, and bail out
2634 * if the file is newer than the program.
2636 * The version and generation of the topology (see top of this file)
2637 * are returned in the two last arguments, if those arguments are non-nullptr.
2639 * If possible, we will read the \p ir even when \p TopOnlyOK is true.
2641 * \param[in,out] serializer The serializer used to handle header processing.
2642 * \param[in,out] tpx File header datastructure.
2643 * \param[in] filename The name of the file being read/written
2644 * \param[in,out] fio File handle.
2645 * \param[in] TopOnlyOK If not reading \p ir is fine or not.
2647 static void do_tpxheader(gmx::FileIOXdrSerializer* serializer,
2649 const char* filename,
2657 /* XDR binary topology file */
2658 precision = sizeof(real);
2660 std::string fileTag;
2661 if (serializer->reading())
2663 serializer->doString(&buf);
2664 if (std::strncmp(buf.c_str(), "VERSION", 7) != 0)
2668 "Can not read file %s,\n"
2669 " this file is from a GROMACS version which is older than 2.0\n"
2670 " Make a new one with grompp or use a gro or pdb file, if possible",
2673 // We need to know the precision used to write the TPR file, to match it
2674 // to the precision of the currently running binary. If the precisions match
2675 // there is no problem, but mismatching precision needs to be accounted for
2676 // by reading into temporary variables of the correct precision instead
2677 // of the desired target datastructures.
2678 serializer->doInt(&precision);
2679 tpx->isDouble = (precision == sizeof(double));
2680 if ((precision != sizeof(float)) && !tpx->isDouble)
2683 "Unknown precision in file %s: real is %d bytes "
2684 "instead of %zu or %zu",
2685 filename, precision, sizeof(float), sizeof(double));
2687 gmx_fio_setprecision(fio, tpx->isDouble);
2688 fprintf(stderr, "Reading file %s, %s (%s precision)\n", filename, buf.c_str(),
2689 tpx->isDouble ? "double" : "single");
2693 buf = gmx::formatString("VERSION %s", gmx_version());
2694 serializer->doString(&buf);
2695 gmx_fio_setprecision(fio, tpx->isDouble);
2696 serializer->doInt(&precision);
2697 fileTag = gmx::formatString("%s", tpx_tag);
2700 /* Check versions! */
2701 serializer->doInt(&tpx->fileVersion);
2703 /* This is for backward compatibility with development versions 77-79
2704 * where the tag was, mistakenly, placed before the generation,
2705 * which would cause a segv instead of a proper error message
2706 * when reading the topology only from tpx with <77 code.
2708 if (tpx->fileVersion >= 77 && tpx->fileVersion <= 79)
2710 serializer->doString(&fileTag);
2713 serializer->doInt(&tpx->fileGeneration);
2715 if (tpx->fileVersion >= 81)
2717 serializer->doString(&fileTag);
2719 if (serializer->reading())
2721 if (tpx->fileVersion < 77)
2723 /* Versions before 77 don't have the tag, set it to release */
2724 fileTag = gmx::formatString("%s", TPX_TAG_RELEASE);
2727 if (fileTag != tpx_tag)
2729 fprintf(stderr, "Note: file tpx tag '%s', software tpx tag '%s'\n", fileTag.c_str(), tpx_tag);
2731 /* We only support reading tpx files with the same tag as the code
2732 * or tpx files with the release tag and with lower version number.
2734 if (fileTag != TPX_TAG_RELEASE && tpx->fileVersion < tpx_version)
2737 "tpx tag/version mismatch: reading tpx file (%s) version %d, tag '%s' "
2738 "with program for tpx version %d, tag '%s'",
2739 filename, tpx->fileVersion, fileTag.c_str(), tpx_version, tpx_tag);
2744 if ((tpx->fileVersion <= tpx_incompatible_version)
2745 || ((tpx->fileVersion > tpx_version) && !TopOnlyOK) || (tpx->fileGeneration > tpx_generation)
2746 || tpx_version == 80) /*80 was used by both 5.0-dev and 4.6-dev*/
2748 gmx_fatal(FARGS, "reading tpx file (%s) version %d with version %d program", filename,
2749 tpx->fileVersion, tpx_version);
2752 serializer->doInt(&tpx->natoms);
2753 serializer->doInt(&tpx->ngtc);
2755 if (tpx->fileVersion < 62)
2757 serializer->doInt(&idum);
2758 serializer->doReal(&rdum);
2760 if (tpx->fileVersion >= 79)
2762 serializer->doInt(&tpx->fep_state);
2764 serializer->doReal(&tpx->lambda);
2765 serializer->doBool(&tpx->bIr);
2766 serializer->doBool(&tpx->bTop);
2767 serializer->doBool(&tpx->bX);
2768 serializer->doBool(&tpx->bV);
2769 serializer->doBool(&tpx->bF);
2770 serializer->doBool(&tpx->bBox);
2772 if (tpx->fileVersion >= tpxv_AddSizeField && tpx->fileGeneration >= 27)
2774 if (!serializer->reading())
2776 GMX_RELEASE_ASSERT(tpx->sizeOfTprBody != 0,
2777 "Not possible to write new file with zero TPR body size");
2779 serializer->doInt64(&tpx->sizeOfTprBody);
2782 if ((tpx->fileGeneration > tpx_generation))
2784 /* This can only happen if TopOnlyOK=TRUE */
2789 #define do_test(serializer, b, p) \
2790 if ((serializer)->reading() && ((p) != nullptr) && !(b)) \
2791 gmx_fatal(FARGS, "No %s in input file", #p)
2794 * Process the first part of the TPR into the state datastructure.
2796 * Due to the structure of the legacy code, it is necessary
2797 * to split up the state reading into two parts, with the
2798 * box and legacy temperature coupling processed before the
2799 * topology datastructures.
2801 * See the documentation for do_tpx_body for the correct order of
2802 * the operations for reading a tpr file.
2804 * \param[in] serializer Abstract serializer used to read/write data.
2805 * \param[in] tpx The file header data.
2806 * \param[in, out] state Global state data.
2808 static void do_tpx_state_first(gmx::ISerializer* serializer, TpxFileHeader* tpx, t_state* state)
2810 if (serializer->reading())
2813 init_gtc_state(state, tpx->ngtc, 0, 0);
2815 do_test(serializer, tpx->bBox, state->box);
2818 serializer->doRvecArray(state->box, DIM);
2819 if (tpx->fileVersion >= 51)
2821 serializer->doRvecArray(state->box_rel, DIM);
2825 /* We initialize box_rel after reading the inputrec */
2826 clear_mat(state->box_rel);
2828 serializer->doRvecArray(state->boxv, DIM);
2829 if (tpx->fileVersion < 56)
2832 serializer->doRvecArray(mdum, DIM);
2836 if (state->ngtc > 0)
2839 snew(dumv, state->ngtc);
2840 if (tpx->fileVersion < 69)
2842 serializer->doRealArray(dumv, state->ngtc);
2844 /* These used to be the Berendsen tcoupl_lambda's */
2845 serializer->doRealArray(dumv, state->ngtc);
2851 * Process global topology data.
2853 * See the documentation for do_tpx_body for the correct order of
2854 * the operations for reading a tpr file.
2856 * \param[in] serializer Abstract serializer used to read/write data.
2857 * \param[in] tpx The file header data.
2858 * \param[in,out] mtop Global topology.
2860 static void do_tpx_mtop(gmx::ISerializer* serializer, TpxFileHeader* tpx, gmx_mtop_t* mtop)
2862 do_test(serializer, tpx->bTop, mtop);
2867 do_mtop(serializer, mtop, tpx->fileVersion);
2868 set_disres_npair(mtop);
2874 do_mtop(serializer, &dum_top, tpx->fileVersion);
2879 * Process coordinate vectors for state data.
2881 * Main part of state gets processed here.
2883 * See the documentation for do_tpx_body for the correct order of
2884 * the operations for reading a tpr file.
2886 * \param[in] serializer Abstract serializer used to read/write data.
2887 * \param[in] tpx The file header data.
2888 * \param[in,out] state Global state data.
2889 * \param[in,out] x Individual coordinates for processing, deprecated.
2890 * \param[in,out] v Individual velocities for processing, deprecated.
2892 static void do_tpx_state_second(gmx::ISerializer* serializer, TpxFileHeader* tpx, t_state* state, rvec* x, rvec* v)
2894 if (!serializer->reading())
2897 x == nullptr && v == nullptr,
2898 "Passing separate x and v pointers to do_tpx() is not supported when writing");
2902 GMX_RELEASE_ASSERT(!(x == nullptr && v != nullptr),
2903 "Passing x==NULL and v!=NULL is not supported");
2906 if (serializer->reading())
2910 // v is also nullptr by the above assertion, so we may
2911 // need to make memory in state for storing the contents
2915 state->flags |= (1 << estX);
2919 state->flags |= (1 << estV);
2921 state_change_natoms(state, tpx->natoms);
2927 x = state->x.rvec_array();
2928 v = state->v.rvec_array();
2930 do_test(serializer, tpx->bX, x);
2933 if (serializer->reading())
2935 state->flags |= (1 << estX);
2937 serializer->doRvecArray(x, tpx->natoms);
2940 do_test(serializer, tpx->bV, v);
2943 if (serializer->reading())
2945 state->flags |= (1 << estV);
2949 std::vector<gmx::RVec> dummyVelocities(tpx->natoms);
2950 serializer->doRvecArray(as_rvec_array(dummyVelocities.data()), tpx->natoms);
2954 serializer->doRvecArray(v, tpx->natoms);
2958 // No need to run do_test when the last argument is NULL
2961 std::vector<gmx::RVec> dummyForces(state->natoms);
2962 serializer->doRvecArray(as_rvec_array(dummyForces.data()), tpx->natoms);
2966 * Process simulation parameters.
2968 * See the documentation for do_tpx_body for the correct order of
2969 * the operations for reading a tpr file.
2971 * \param[in] serializer Abstract serializer used to read/write data.
2972 * \param[in] tpx The file header data.
2973 * \param[in,out] ir Datastructure with simulation parameters.
2975 static PbcType do_tpx_ir(gmx::ISerializer* serializer, TpxFileHeader* tpx, t_inputrec* ir)
2978 gmx_bool bPeriodicMols;
2980 /* Starting with tpx version 26, we have the inputrec
2981 * at the end of the file, so we can ignore it
2982 * if the file is never than the software (but still the
2983 * same generation - see comments at the top of this file.
2987 pbcType = PbcType::Unset;
2988 bPeriodicMols = FALSE;
2990 do_test(serializer, tpx->bIr, ir);
2993 if (tpx->fileVersion >= 53)
2995 /* Removed the pbc info from do_inputrec, since we always want it */
2996 if (!serializer->reading())
2998 pbcType = ir->pbcType;
2999 bPeriodicMols = ir->bPeriodicMols;
3001 serializer->doInt(reinterpret_cast<int*>(&pbcType));
3002 serializer->doBool(&bPeriodicMols);
3004 if (tpx->fileGeneration <= tpx_generation && ir)
3006 do_inputrec(serializer, ir, tpx->fileVersion);
3007 if (tpx->fileVersion < 53)
3009 pbcType = ir->pbcType;
3010 bPeriodicMols = ir->bPeriodicMols;
3013 if (serializer->reading() && ir && tpx->fileVersion >= 53)
3015 /* We need to do this after do_inputrec, since that initializes ir */
3016 ir->pbcType = pbcType;
3017 ir->bPeriodicMols = bPeriodicMols;
3024 * Correct and finalize read information.
3026 * If \p state is nullptr, skip the parts dependent on it.
3028 * See the documentation for do_tpx_body for the correct order of
3029 * the operations for reading a tpr file.
3031 * \param[in] tpx The file header used to check version numbers.
3032 * \param[out] ir Input rec that needs correction.
3033 * \param[out] state State needing correction.
3034 * \param[out] mtop Topology to finalize.
3036 static void do_tpx_finalize(TpxFileHeader* tpx, t_inputrec* ir, t_state* state, gmx_mtop_t* mtop)
3038 if (tpx->fileVersion < 51 && state)
3040 set_box_rel(ir, state);
3044 if (state && state->ngtc == 0)
3046 /* Reading old version without tcoupl state data: set it */
3047 init_gtc_state(state, ir->opts.ngtc, 0, ir->opts.nhchainlength);
3049 if (tpx->bTop && mtop)
3051 if (tpx->fileVersion < 57)
3053 if (!mtop->moltype[0].ilist[F_DISRES].empty())
3055 ir->eDisre = edrSimple;
3059 ir->eDisre = edrNone;
3067 * Process TPR data for file reading/writing.
3069 * The TPR file gets processed in in four stages due to the organization
3070 * of the data within it.
3072 * First, state data for the box is processed in do_tpx_state_first.
3073 * This is followed by processing the topology in do_tpx_mtop.
3074 * Coordinate and velocity vectors are handled next in do_tpx_state_second.
3075 * The last file information processed is the collection of simulation parameters in do_tpx_ir.
3076 * When reading, a final processing step is undertaken at the end.
3078 * \param[in] serializer Abstract serializer used to read/write data.
3079 * \param[in] tpx The file header data.
3080 * \param[in,out] ir Datastructures with simulation parameters.
3081 * \param[in,out] state Global state data.
3082 * \param[in,out] x Individual coordinates for processing, deprecated.
3083 * \param[in,out] v Individual velocities for processing, deprecated.
3084 * \param[in,out] mtop Global topology.
3086 static PbcType do_tpx_body(gmx::ISerializer* serializer,
3096 do_tpx_state_first(serializer, tpx, state);
3098 do_tpx_mtop(serializer, tpx, mtop);
3101 do_tpx_state_second(serializer, tpx, state, x, v);
3103 PbcType pbcType = do_tpx_ir(serializer, tpx, ir);
3104 if (serializer->reading())
3106 do_tpx_finalize(tpx, ir, state, mtop);
3112 * Overload for do_tpx_body that defaults to state vectors being nullptr.
3114 * \param[in] serializer Abstract serializer used to read/write data.
3115 * \param[in] tpx The file header data.
3116 * \param[in,out] ir Datastructures with simulation parameters.
3117 * \param[in,out] mtop Global topology.
3119 static PbcType do_tpx_body(gmx::ISerializer* serializer, TpxFileHeader* tpx, t_inputrec* ir, gmx_mtop_t* mtop)
3121 return do_tpx_body(serializer, tpx, ir, nullptr, nullptr, nullptr, mtop);
3124 static t_fileio* open_tpx(const char* fn, const char* mode)
3126 return gmx_fio_open(fn, mode);
3129 static void close_tpx(t_fileio* fio)
3135 * Fill information into the header only from state before writing.
3137 * Populating the header needs to be independent from writing the information
3138 * to file to allow things like writing the raw byte stream.
3140 * \param[in] state The current simulation state. Can't write without it.
3141 * \param[in] ir Parameter and system information.
3142 * \param[in] mtop Global topology.
3143 * \returns Fully populated header.
3145 static TpxFileHeader populateTpxHeader(const t_state& state, const t_inputrec* ir, const gmx_mtop_t* mtop)
3147 TpxFileHeader header;
3148 header.natoms = state.natoms;
3149 header.ngtc = state.ngtc;
3150 header.fep_state = state.fep_state;
3151 header.lambda = state.lambda[efptFEP];
3152 header.bIr = ir != nullptr;
3153 header.bTop = mtop != nullptr;
3154 header.bX = (state.flags & (1 << estX)) != 0;
3155 header.bV = (state.flags & (1 << estV)) != 0;
3158 header.fileVersion = tpx_version;
3159 header.fileGeneration = tpx_generation;
3160 header.isDouble = (sizeof(real) == sizeof(double));
3166 * Process the body of a TPR file as an opaque data buffer.
3168 * Reads/writes the information in \p buffer from/to the \p serializer
3169 * provided to the function. Does not interact with the actual
3170 * TPR datastructures but with an in memory representation of the
3171 * data, so that this data can be efficiently read or written from/to
3172 * an original source.
3174 * \param[in] serializer The abstract serializer used for reading or writing
3175 * the information in \p buffer.
3176 * \param[in,out] buffer Information from TPR file as char buffer.
3178 static void doTpxBodyBuffer(gmx::ISerializer* serializer, gmx::ArrayRef<char> buffer)
3180 serializer->doOpaque(buffer.data(), buffer.size());
3184 * Populates simulation datastructures.
3186 * Here the information from the serialization interface \p serializer
3187 * is used to first populate the datastructures containing the simulation
3188 * information. Depending on the version found in the header \p tpx,
3189 * this is done using the new reading of the data as one block from disk,
3190 * followed by complete deserialization of the information read from there.
3191 * Otherwise, the datastructures are populated as before one by one from disk.
3192 * The second version is the default for the legacy tools that read the
3193 * coordinates and velocities separate from the state.
3195 * After reading in the data, a separate buffer is populated from them
3196 * containing only \p ir and \p mtop that can be communicated directly
3197 * to nodes needing the information to set up a simulation.
3199 * \param[in] tpx The file header.
3200 * \param[in] serializer The Serialization interface used to read the TPR.
3201 * \param[out] ir Input rec to populate.
3202 * \param[out] state State vectors to populate.
3203 * \param[out] x Coordinates to populate if needed.
3204 * \param[out] v Velocities to populate if needed.
3205 * \param[out] mtop Global topology to populate.
3207 * \returns Partial de-serialized TPR used for communication to nodes.
3209 static PartialDeserializedTprFile readTpxBody(TpxFileHeader* tpx,
3210 gmx::ISerializer* serializer,
3217 PartialDeserializedTprFile partialDeserializedTpr;
3218 if (tpx->fileVersion >= tpxv_AddSizeField && tpx->fileGeneration >= 27)
3220 partialDeserializedTpr.body.resize(tpx->sizeOfTprBody);
3221 partialDeserializedTpr.header = *tpx;
3222 doTpxBodyBuffer(serializer, partialDeserializedTpr.body);
3224 partialDeserializedTpr.pbcType =
3225 completeTprDeserialization(&partialDeserializedTpr, ir, state, x, v, mtop);
3229 partialDeserializedTpr.pbcType = do_tpx_body(serializer, tpx, ir, state, x, v, mtop);
3231 // Update header to system info for communication to nodes.
3232 // As we only need to communicate the inputrec and mtop to other nodes,
3233 // we prepare a new char buffer with the information we have already read
3235 partialDeserializedTpr.header = populateTpxHeader(*state, ir, mtop);
3236 // Long-term we should move to use little endian in files to avoid extra byte swapping,
3237 // but since we just used the default XDR format (which is big endian) for the TPR
3238 // header it would cause third-party libraries reading our raw data to tear their hair
3239 // if we swap the endian in the middle of the file, so we stick to big endian in the
3240 // TPR file for now - and thus we ask the serializer to swap if this host is little endian.
3241 gmx::InMemorySerializer tprBodySerializer(gmx::EndianSwapBehavior::SwapIfHostIsLittleEndian);
3242 do_tpx_body(&tprBodySerializer, &partialDeserializedTpr.header, ir, mtop);
3243 partialDeserializedTpr.body = tprBodySerializer.finishAndGetBuffer();
3245 return partialDeserializedTpr;
3248 /************************************************************
3250 * The following routines are the exported ones
3252 ************************************************************/
3254 TpxFileHeader readTpxHeader(const char* fileName, bool canReadTopologyOnly)
3258 fio = open_tpx(fileName, "r");
3259 gmx::FileIOXdrSerializer serializer(fio);
3262 do_tpxheader(&serializer, &tpx, fileName, fio, canReadTopologyOnly);
3267 void write_tpx_state(const char* fn, const t_inputrec* ir, const t_state* state, const gmx_mtop_t* mtop)
3269 /* To write a state, we first need to write the state information to a buffer before
3270 * we append the raw bytes to the file. For this, the header information needs to be
3271 * populated before we write the main body because it has some information that is
3272 * otherwise not available.
3277 TpxFileHeader tpx = populateTpxHeader(*state, ir, mtop);
3278 // Long-term we should move to use little endian in files to avoid extra byte swapping,
3279 // but since we just used the default XDR format (which is big endian) for the TPR
3280 // header it would cause third-party libraries reading our raw data to tear their hair
3281 // if we swap the endian in the middle of the file, so we stick to big endian in the
3282 // TPR file for now - and thus we ask the serializer to swap if this host is little endian.
3283 gmx::InMemorySerializer tprBodySerializer(gmx::EndianSwapBehavior::SwapIfHostIsLittleEndian);
3285 do_tpx_body(&tprBodySerializer, &tpx, const_cast<t_inputrec*>(ir), const_cast<t_state*>(state),
3286 nullptr, nullptr, const_cast<gmx_mtop_t*>(mtop));
3288 std::vector<char> tprBody = tprBodySerializer.finishAndGetBuffer();
3289 tpx.sizeOfTprBody = tprBody.size();
3291 fio = open_tpx(fn, "w");
3292 gmx::FileIOXdrSerializer serializer(fio);
3293 do_tpxheader(&serializer, &tpx, fn, fio, ir == nullptr);
3294 doTpxBodyBuffer(&serializer, tprBody);
3299 PbcType completeTprDeserialization(PartialDeserializedTprFile* partialDeserializedTpr,
3306 // Long-term we should move to use little endian in files to avoid extra byte swapping,
3307 // but since we just used the default XDR format (which is big endian) for the TPR
3308 // header it would cause third-party libraries reading our raw data to tear their hair
3309 // if we swap the endian in the middle of the file, so we stick to big endian in the
3310 // TPR file for now - and thus we ask the serializer to swap if this host is little endian.
3311 gmx::InMemoryDeserializer tprBodyDeserializer(partialDeserializedTpr->body,
3312 partialDeserializedTpr->header.isDouble,
3313 gmx::EndianSwapBehavior::SwapIfHostIsLittleEndian);
3314 return do_tpx_body(&tprBodyDeserializer, &partialDeserializedTpr->header, ir, state, x, v, mtop);
3317 PbcType completeTprDeserialization(PartialDeserializedTprFile* partialDeserializedTpr,
3321 return completeTprDeserialization(partialDeserializedTpr, ir, nullptr, nullptr, nullptr, mtop);
3324 PartialDeserializedTprFile read_tpx_state(const char* fn, t_inputrec* ir, t_state* state, gmx_mtop_t* mtop)
3327 fio = open_tpx(fn, "r");
3328 gmx::FileIOXdrSerializer serializer(fio);
3329 PartialDeserializedTprFile partialDeserializedTpr;
3330 do_tpxheader(&serializer, &partialDeserializedTpr.header, fn, fio, ir == nullptr);
3331 partialDeserializedTpr =
3332 readTpxBody(&partialDeserializedTpr.header, &serializer, ir, state, nullptr, nullptr, mtop);
3334 return partialDeserializedTpr;
3337 PbcType read_tpx(const char* fn, t_inputrec* ir, matrix box, int* natoms, rvec* x, rvec* v, gmx_mtop_t* mtop)
3343 fio = open_tpx(fn, "r");
3344 gmx::FileIOXdrSerializer serializer(fio);
3345 do_tpxheader(&serializer, &tpx, fn, fio, ir == nullptr);
3346 PartialDeserializedTprFile partialDeserializedTpr =
3347 readTpxBody(&tpx, &serializer, ir, &state, x, v, mtop);
3349 if (mtop != nullptr && natoms != nullptr)
3351 *natoms = mtop->natoms;
3355 copy_mat(state.box, box);
3357 return partialDeserializedTpr.pbcType;
3360 PbcType read_tpx_top(const char* fn, t_inputrec* ir, matrix box, int* natoms, rvec* x, rvec* v, t_topology* top)
3365 pbcType = read_tpx(fn, ir, box, natoms, x, v, &mtop);
3367 *top = gmx_mtop_t_to_t_topology(&mtop, true);
3372 gmx_bool fn2bTPX(const char* file)
3374 return (efTPR == fn2ftp(file));
3377 void pr_tpxheader(FILE* fp, int indent, const char* title, const TpxFileHeader* sh)
3379 if (available(fp, sh, indent, title))
3381 indent = pr_title(fp, indent, title);
3382 pr_indent(fp, indent);
3383 fprintf(fp, "bIr = %spresent\n", sh->bIr ? "" : "not ");
3384 pr_indent(fp, indent);
3385 fprintf(fp, "bBox = %spresent\n", sh->bBox ? "" : "not ");
3386 pr_indent(fp, indent);
3387 fprintf(fp, "bTop = %spresent\n", sh->bTop ? "" : "not ");
3388 pr_indent(fp, indent);
3389 fprintf(fp, "bX = %spresent\n", sh->bX ? "" : "not ");
3390 pr_indent(fp, indent);
3391 fprintf(fp, "bV = %spresent\n", sh->bV ? "" : "not ");
3392 pr_indent(fp, indent);
3393 fprintf(fp, "bF = %spresent\n", sh->bF ? "" : "not ");
3395 pr_indent(fp, indent);
3396 fprintf(fp, "natoms = %d\n", sh->natoms);
3397 pr_indent(fp, indent);
3398 fprintf(fp, "lambda = %e\n", sh->lambda);
3399 pr_indent(fp, indent);
3400 fprintf(fp, "buffer size = %" PRId64 "\n", sh->sizeOfTprBody);