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,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 /* This file is completely threadsafe - keep it that way! */
51 #include "gromacs/fileio/filetypes.h"
52 #include "gromacs/fileio/gmxfio.h"
53 #include "gromacs/fileio/gmxfio_xdr.h"
54 #include "gromacs/math/units.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/mdtypes/awh_history.h"
57 #include "gromacs/mdtypes/awh_params.h"
58 #include "gromacs/mdtypes/inputrec.h"
59 #include "gromacs/mdtypes/md_enums.h"
60 #include "gromacs/mdtypes/pull_params.h"
61 #include "gromacs/mdtypes/state.h"
62 #include "gromacs/pbcutil/boxutilities.h"
63 #include "gromacs/pbcutil/pbc.h"
64 #include "gromacs/topology/block.h"
65 #include "gromacs/topology/ifunc.h"
66 #include "gromacs/topology/mtop_util.h"
67 #include "gromacs/topology/symtab.h"
68 #include "gromacs/topology/topology.h"
69 #include "gromacs/utility/arraysize.h"
70 #include "gromacs/utility/baseversion.h"
71 #include "gromacs/utility/cstringutil.h"
72 #include "gromacs/utility/fatalerror.h"
73 #include "gromacs/utility/futil.h"
74 #include "gromacs/utility/gmxassert.h"
75 #include "gromacs/utility/keyvaluetreebuilder.h"
76 #include "gromacs/utility/keyvaluetreeserializer.h"
77 #include "gromacs/utility/smalloc.h"
78 #include "gromacs/utility/snprintf.h"
79 #include "gromacs/utility/txtdump.h"
81 #define TPX_TAG_RELEASE "release"
83 /*! \brief Tag string for the file format written to run input files
84 * written by this version of the code.
86 * Change this if you want to change the run input format in a feature
87 * branch. This ensures that there will not be different run input
88 * formats around which cannot be distinguished, while not causing
89 * problems rebasing the feature branch onto upstream changes. When
90 * merging with mainstream GROMACS, set this tag string back to
91 * TPX_TAG_RELEASE, and instead add an element to tpxv.
93 static const char *tpx_tag = TPX_TAG_RELEASE;
95 /*! \brief Enum of values that describe the contents of a tpr file
96 * whose format matches a version number
98 * The enum helps the code be more self-documenting and ensure merges
99 * do not silently resolve when two patches make the same bump. When
100 * adding new functionality, add a new element just above tpxv_Count
101 * in this enumeration, and write code below that does the right thing
102 * according to the value of file_version.
106 tpxv_ComputationalElectrophysiology = 96, /**< support for ion/water position swaps (computational electrophysiology) */
107 tpxv_Use64BitRandomSeed, /**< change ld_seed from int to int64_t */
108 tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, /**< potentials for supporting coarse-grained force fields */
109 tpxv_InteractiveMolecularDynamics, /**< interactive molecular dynamics (IMD) */
110 tpxv_RemoveObsoleteParameters1, /**< remove optimize_fft, dihre_fc, nstcheckpoint */
111 tpxv_PullCoordTypeGeom, /**< add pull type and geometry per group and flat-bottom */
112 tpxv_PullGeomDirRel, /**< add pull geometry direction-relative */
113 tpxv_IntermolecularBondeds, /**< permit inter-molecular bonded interactions in the topology */
114 tpxv_CompElWithSwapLayerOffset, /**< added parameters for improved CompEl setups */
115 tpxv_CompElPolyatomicIonsAndMultipleIonTypes, /**< CompEl now can handle polyatomic ions and more than two types of ions */
116 tpxv_RemoveAdress, /**< removed support for AdResS */
117 tpxv_PullCoordNGroup, /**< add ngroup to pull coord */
118 tpxv_RemoveTwinRange, /**< removed support for twin-range interactions */
119 tpxv_ReplacePullPrintCOM12, /**< Replaced print-com-1, 2 with pull-print-com */
120 tpxv_PullExternalPotential, /**< Added pull type external potential */
121 tpxv_GenericParamsForElectricField, /**< Introduced KeyValueTree and moved electric field parameters */
122 tpxv_AcceleratedWeightHistogram, /**< sampling with accelerated weight histogram method (AWH) */
123 tpxv_RemoveImplicitSolvation, /**< removed support for implicit solvation */
124 tpxv_PullPrevStepCOMAsReference, /**< Enabled using the COM of the pull group of the last frame as reference for PBC */
125 tpxv_MimicQMMM, /**< Introduced support for MiMiC QM/MM interface */
126 tpxv_PullAverage, /**< Added possibility to output average pull force and position */
127 tpxv_GenericInternalParameters, /**< Added internal parameters for mdrun modules*/
128 tpxv_VSite2FD, /**< Added 2FD type virtual site */
129 tpxv_Count /**< the total number of tpxv versions */
132 /*! \brief Version number of the file format written to run input
133 * files by this version of the code.
135 * The tpx_version increases whenever the file format in the main
136 * development branch changes, due to an extension of the tpxv enum above.
137 * Backward compatibility for reading old run input files is maintained
138 * by checking this version number against that of the file and then using
139 * the correct code path.
141 * When developing a feature branch that needs to change the run input
142 * file format, change tpx_tag instead. */
143 static const int tpx_version = tpxv_Count - 1;
146 /* This number should only be increased when you edit the TOPOLOGY section
147 * or the HEADER of the tpx format.
148 * This way we can maintain forward compatibility too for all analysis tools
149 * and/or external programs that only need to know the atom/residue names,
150 * charges, and bond connectivity.
152 * It first appeared in tpx version 26, when I also moved the inputrecord
153 * to the end of the tpx file, so we can just skip it if we only
156 * In particular, it must be increased when adding new elements to
157 * ftupd, so that old code can read new .tpr files.
159 static const int tpx_generation = 26;
161 /* This number should be the most recent backwards incompatible version
162 * I.e., if this number is 9, we cannot read tpx version 9 with this code.
164 static const int tpx_incompatible_version = 57; // GMX4.0 has version 58
168 /* Struct used to maintain tpx compatibility when function types are added */
170 int fvnr; /* file version number in which the function type first appeared */
171 int ftype; /* function type */
175 * TODO The following three lines make little sense, please clarify if
176 * you've had to work out how ftupd works.
178 * The entries should be ordered in:
179 * 1. ascending function type number
180 * 2. ascending file version number
182 * Because we support reading of old .tpr file versions (even when
183 * mdrun can no longer run the simulation), we need to be able to read
184 * obsolete t_interaction_function types. Any data read from such
185 * fields is discarded. Their names have _NOLONGERUSED appended to
186 * them to make things clear.
188 static const t_ftupd ftupd[] = {
189 { 70, F_RESTRBONDS },
190 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_RESTRANGLES },
191 { 76, F_LINEAR_ANGLES },
192 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_RESTRDIHS },
193 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_CBTDIHS },
195 { 60, F_GB12_NOLONGERUSED },
196 { 61, F_GB13_NOLONGERUSED },
197 { 61, F_GB14_NOLONGERUSED },
198 { 72, F_GBPOL_NOLONGERUSED },
199 { 72, F_NPSOLVATION_NOLONGERUSED },
202 { 69, F_VTEMP_NOLONGERUSED},
204 { 76, F_ANHARM_POL },
207 { 79, F_DVDL_BONDED, },
208 { 79, F_DVDL_RESTRAINT },
209 { 79, F_DVDL_TEMPERATURE },
210 { tpxv_GenericInternalParameters, F_DENSITYFITTING },
211 { tpxv_VSite2FD, F_VSITE2FD },
213 #define NFTUPD asize(ftupd)
215 /* Needed for backward compatibility */
218 /**************************************************************
220 * Now the higer level routines that do io of the structures and arrays
222 **************************************************************/
223 static void do_pullgrp_tpx_pre95(gmx::ISerializer *serializer,
229 serializer->doInt(&pgrp->nat);
230 if (serializer->reading())
232 snew(pgrp->ind, pgrp->nat);
234 serializer->doIntArray(pgrp->ind, pgrp->nat);
235 serializer->doInt(&pgrp->nweight);
236 if (serializer->reading())
238 snew(pgrp->weight, pgrp->nweight);
240 serializer->doRealArray(pgrp->weight, pgrp->nweight);
241 serializer->doInt(&pgrp->pbcatom);
242 serializer->doRvec(&pcrd->vec);
243 clear_rvec(pcrd->origin);
244 serializer->doRvec(&tmp);
246 serializer->doReal(&pcrd->rate);
247 serializer->doReal(&pcrd->k);
248 serializer->doReal(&pcrd->kB);
251 static void do_pull_group(gmx::ISerializer *serializer, t_pull_group *pgrp)
253 serializer->doInt(&pgrp->nat);
254 if (serializer->reading())
256 snew(pgrp->ind, pgrp->nat);
258 serializer->doIntArray(pgrp->ind, pgrp->nat);
259 serializer->doInt(&pgrp->nweight);
260 if (serializer->reading())
262 snew(pgrp->weight, pgrp->nweight);
264 serializer->doRealArray(pgrp->weight, pgrp->nweight);
265 serializer->doInt(&pgrp->pbcatom);
268 static void do_pull_coord(gmx::ISerializer *serializer,
271 int ePullOld, int eGeomOld, ivec dimOld)
273 if (file_version >= tpxv_PullCoordNGroup)
275 serializer->doInt(&pcrd->eType);
276 if (file_version >= tpxv_PullExternalPotential)
278 if (pcrd->eType == epullEXTERNAL)
281 if (serializer->reading())
283 serializer->doString(&buf);
284 pcrd->externalPotentialProvider = gmx_strdup(buf.c_str());
288 buf = pcrd->externalPotentialProvider;
289 serializer->doString(&buf);
294 pcrd->externalPotentialProvider = nullptr;
299 if (serializer->reading())
301 pcrd->externalPotentialProvider = nullptr;
304 /* Note that we try to support adding new geometries without
305 * changing the tpx version. This requires checks when printing the
306 * geometry string and a check and fatal_error in init_pull.
308 serializer->doInt(&pcrd->eGeom);
309 serializer->doInt(&pcrd->ngroup);
310 if (pcrd->ngroup <= c_pullCoordNgroupMax)
312 serializer->doIntArray(pcrd->group, pcrd->ngroup);
316 /* More groups in file than supported, this must be a new geometry
317 * that is not supported by our current code. Since we will not
318 * use the groups for this coord (checks in the pull and WHAM code
319 * ensure this), we can ignore the groups and set ngroup=0.
322 snew(dum, pcrd->ngroup);
323 serializer->doIntArray(dum, pcrd->ngroup);
328 serializer->doIvec(&pcrd->dim);
333 serializer->doInt(&pcrd->group[0]);
334 serializer->doInt(&pcrd->group[1]);
335 if (file_version >= tpxv_PullCoordTypeGeom)
337 pcrd->ngroup = (pcrd->eGeom == epullgDIRRELATIVE ? 4 : 2);
338 serializer->doInt(&pcrd->eType);
339 serializer->doInt(&pcrd->eGeom);
340 if (pcrd->ngroup == 4)
342 serializer->doInt(&pcrd->group[2]);
343 serializer->doInt(&pcrd->group[3]);
345 serializer->doIvec(&pcrd->dim);
349 pcrd->eType = ePullOld;
350 pcrd->eGeom = eGeomOld;
351 copy_ivec(dimOld, pcrd->dim);
354 serializer->doRvec(&pcrd->origin);
355 serializer->doRvec(&pcrd->vec);
356 if (file_version >= tpxv_PullCoordTypeGeom)
358 serializer->doBool(&pcrd->bStart);
362 /* This parameter is only printed, but not actually used by mdrun */
363 pcrd->bStart = FALSE;
365 serializer->doReal(&pcrd->init);
366 serializer->doReal(&pcrd->rate);
367 serializer->doReal(&pcrd->k);
368 serializer->doReal(&pcrd->kB);
371 static void do_expandedvals(gmx::ISerializer *serializer,
376 int n_lambda = fepvals->n_lambda;
378 /* reset the lambda calculation window */
379 fepvals->lambda_start_n = 0;
380 fepvals->lambda_stop_n = n_lambda;
381 if (file_version >= 79)
385 if (serializer->reading())
387 snew(expand->init_lambda_weights, n_lambda);
389 serializer->doRealArray(expand->init_lambda_weights, n_lambda);
390 serializer->doBool(&expand->bInit_weights);
393 serializer->doInt(&expand->nstexpanded);
394 serializer->doInt(&expand->elmcmove);
395 serializer->doInt(&expand->elamstats);
396 serializer->doInt(&expand->lmc_repeats);
397 serializer->doInt(&expand->gibbsdeltalam);
398 serializer->doInt(&expand->lmc_forced_nstart);
399 serializer->doInt(&expand->lmc_seed);
400 serializer->doReal(&expand->mc_temp);
401 serializer->doBool(&expand->bSymmetrizedTMatrix);
402 serializer->doInt(&expand->nstTij);
403 serializer->doInt(&expand->minvarmin);
404 serializer->doInt(&expand->c_range);
405 serializer->doReal(&expand->wl_scale);
406 serializer->doReal(&expand->wl_ratio);
407 serializer->doReal(&expand->init_wl_delta);
408 serializer->doBool(&expand->bWLoneovert);
409 serializer->doInt(&expand->elmceq);
410 serializer->doInt(&expand->equil_steps);
411 serializer->doInt(&expand->equil_samples);
412 serializer->doInt(&expand->equil_n_at_lam);
413 serializer->doReal(&expand->equil_wl_delta);
414 serializer->doReal(&expand->equil_ratio);
418 static void do_simtempvals(gmx::ISerializer *serializer,
423 if (file_version >= 79)
425 serializer->doInt(&simtemp->eSimTempScale);
426 serializer->doReal(&simtemp->simtemp_high);
427 serializer->doReal(&simtemp->simtemp_low);
430 if (serializer->reading())
432 snew(simtemp->temperatures, n_lambda);
434 serializer->doRealArray(simtemp->temperatures, n_lambda);
439 static void do_imd(gmx::ISerializer *serializer, t_IMD *imd)
441 serializer->doInt(&imd->nat);
442 if (serializer->reading())
444 snew(imd->ind, imd->nat);
446 serializer->doIntArray(imd->ind, imd->nat);
449 static void do_fepvals(gmx::ISerializer *serializer,
453 /* i is defined in the ndo_double macro; use g to iterate. */
457 /* free energy values */
459 if (file_version >= 79)
461 serializer->doInt(&fepvals->init_fep_state);
462 serializer->doDouble(&fepvals->init_lambda);
463 serializer->doDouble(&fepvals->delta_lambda);
465 else if (file_version >= 59)
467 serializer->doDouble(&fepvals->init_lambda);
468 serializer->doDouble(&fepvals->delta_lambda);
472 serializer->doReal(&rdum);
473 fepvals->init_lambda = rdum;
474 serializer->doReal(&rdum);
475 fepvals->delta_lambda = rdum;
477 if (file_version >= 79)
479 serializer->doInt(&fepvals->n_lambda);
480 if (serializer->reading())
482 snew(fepvals->all_lambda, efptNR);
484 for (g = 0; g < efptNR; g++)
486 if (fepvals->n_lambda > 0)
488 if (serializer->reading())
490 snew(fepvals->all_lambda[g], fepvals->n_lambda);
492 serializer->doDoubleArray(fepvals->all_lambda[g], fepvals->n_lambda);
493 serializer->doBoolArray(fepvals->separate_dvdl, efptNR);
495 else if (fepvals->init_lambda >= 0)
497 fepvals->separate_dvdl[efptFEP] = TRUE;
501 else if (file_version >= 64)
503 serializer->doInt(&fepvals->n_lambda);
504 if (serializer->reading())
508 snew(fepvals->all_lambda, efptNR);
509 /* still allocate the all_lambda array's contents. */
510 for (g = 0; g < efptNR; g++)
512 if (fepvals->n_lambda > 0)
514 snew(fepvals->all_lambda[g], fepvals->n_lambda);
518 serializer->doDoubleArray(fepvals->all_lambda[efptFEP], fepvals->n_lambda);
519 if (fepvals->init_lambda >= 0)
523 fepvals->separate_dvdl[efptFEP] = TRUE;
525 if (serializer->reading())
527 /* copy the contents of the efptFEP lambda component to all
528 the other components */
529 for (g = 0; g < efptNR; g++)
531 for (h = 0; h < fepvals->n_lambda; h++)
535 fepvals->all_lambda[g][h] =
536 fepvals->all_lambda[efptFEP][h];
545 fepvals->n_lambda = 0;
546 fepvals->all_lambda = nullptr;
547 if (fepvals->init_lambda >= 0)
549 fepvals->separate_dvdl[efptFEP] = TRUE;
552 serializer->doReal(&fepvals->sc_alpha);
553 serializer->doInt(&fepvals->sc_power);
554 if (file_version >= 79)
556 serializer->doReal(&fepvals->sc_r_power);
560 fepvals->sc_r_power = 6.0;
562 serializer->doReal(&fepvals->sc_sigma);
563 if (serializer->reading())
565 if (file_version >= 71)
567 fepvals->sc_sigma_min = fepvals->sc_sigma;
571 fepvals->sc_sigma_min = 0;
574 if (file_version >= 79)
576 serializer->doBool(&fepvals->bScCoul);
580 fepvals->bScCoul = TRUE;
582 if (file_version >= 64)
584 serializer->doInt(&fepvals->nstdhdl);
588 fepvals->nstdhdl = 1;
591 if (file_version >= 73)
593 serializer->doInt(&fepvals->separate_dhdl_file);
594 serializer->doInt(&fepvals->dhdl_derivatives);
598 fepvals->separate_dhdl_file = esepdhdlfileYES;
599 fepvals->dhdl_derivatives = edhdlderivativesYES;
601 if (file_version >= 71)
603 serializer->doInt(&fepvals->dh_hist_size);
604 serializer->doDouble(&fepvals->dh_hist_spacing);
608 fepvals->dh_hist_size = 0;
609 fepvals->dh_hist_spacing = 0.1;
611 if (file_version >= 79)
613 serializer->doInt(&fepvals->edHdLPrintEnergy);
617 fepvals->edHdLPrintEnergy = edHdLPrintEnergyNO;
620 /* handle lambda_neighbors */
621 if ((file_version >= 83 && file_version < 90) || file_version >= 92)
623 serializer->doInt(&fepvals->lambda_neighbors);
624 if ( (fepvals->lambda_neighbors >= 0) && (fepvals->init_fep_state >= 0) &&
625 (fepvals->init_lambda < 0) )
627 fepvals->lambda_start_n = (fepvals->init_fep_state -
628 fepvals->lambda_neighbors);
629 fepvals->lambda_stop_n = (fepvals->init_fep_state +
630 fepvals->lambda_neighbors + 1);
631 if (fepvals->lambda_start_n < 0)
633 fepvals->lambda_start_n = 0;;
635 if (fepvals->lambda_stop_n >= fepvals->n_lambda)
637 fepvals->lambda_stop_n = fepvals->n_lambda;
642 fepvals->lambda_start_n = 0;
643 fepvals->lambda_stop_n = fepvals->n_lambda;
648 fepvals->lambda_start_n = 0;
649 fepvals->lambda_stop_n = fepvals->n_lambda;
653 static void do_awhBias(gmx::ISerializer *serializer, gmx::AwhBiasParams *awhBiasParams,
654 int gmx_unused file_version)
656 serializer->doInt(&awhBiasParams->eTarget);
657 serializer->doDouble(&awhBiasParams->targetBetaScaling);
658 serializer->doDouble(&awhBiasParams->targetCutoff);
659 serializer->doInt(&awhBiasParams->eGrowth);
660 serializer->doInt(&awhBiasParams->bUserData);
661 serializer->doDouble(&awhBiasParams->errorInitial);
662 serializer->doInt(&awhBiasParams->ndim);
663 serializer->doInt(&awhBiasParams->shareGroup);
664 serializer->doBool(&awhBiasParams->equilibrateHistogram);
666 if (serializer->reading())
668 snew(awhBiasParams->dimParams, awhBiasParams->ndim);
671 for (int d = 0; d < awhBiasParams->ndim; d++)
673 gmx::AwhDimParams *dimParams = &awhBiasParams->dimParams[d];
675 serializer->doInt(&dimParams->eCoordProvider);
676 serializer->doInt(&dimParams->coordIndex);
677 serializer->doDouble(&dimParams->origin);
678 serializer->doDouble(&dimParams->end);
679 serializer->doDouble(&dimParams->period);
680 serializer->doDouble(&dimParams->forceConstant);
681 serializer->doDouble(&dimParams->diffusion);
682 serializer->doDouble(&dimParams->coordValueInit);
683 serializer->doDouble(&dimParams->coverDiameter);
687 static void do_awh(gmx::ISerializer *serializer,
688 gmx::AwhParams *awhParams,
689 int gmx_unused file_version)
691 serializer->doInt(&awhParams->numBias);
692 serializer->doInt(&awhParams->nstOut);
693 serializer->doInt64(&awhParams->seed);
694 serializer->doInt(&awhParams->nstSampleCoord);
695 serializer->doInt(&awhParams->numSamplesUpdateFreeEnergy);
696 serializer->doInt(&awhParams->ePotential);
697 serializer->doBool(&awhParams->shareBiasMultisim);
699 if (awhParams->numBias > 0)
701 if (serializer->reading())
703 snew(awhParams->awhBiasParams, awhParams->numBias);
706 for (int k = 0; k < awhParams->numBias; k++)
708 do_awhBias(serializer, &awhParams->awhBiasParams[k], file_version);
713 static void do_pull(gmx::ISerializer *serializer,
722 if (file_version >= 95)
724 serializer->doInt(&pull->ngroup);
726 serializer->doInt(&pull->ncoord);
727 if (file_version < 95)
729 pull->ngroup = pull->ncoord + 1;
731 if (file_version < tpxv_PullCoordTypeGeom)
735 serializer->doInt(&eGeomOld);
736 serializer->doIvec(&dimOld);
737 /* The inner cylinder radius, now removed */
738 serializer->doReal(&dum);
740 serializer->doReal(&pull->cylinder_r);
741 serializer->doReal(&pull->constr_tol);
742 if (file_version >= 95)
744 serializer->doBool(&pull->bPrintCOM);
745 /* With file_version < 95 this value is set below */
747 if (file_version >= tpxv_ReplacePullPrintCOM12)
749 serializer->doBool(&pull->bPrintRefValue);
750 serializer->doBool(&pull->bPrintComp);
752 else if (file_version >= tpxv_PullCoordTypeGeom)
755 serializer->doInt(&idum); /* used to be bPrintCOM2 */
756 serializer->doBool(&pull->bPrintRefValue);
757 serializer->doBool(&pull->bPrintComp);
761 pull->bPrintRefValue = FALSE;
762 pull->bPrintComp = TRUE;
764 serializer->doInt(&pull->nstxout);
765 serializer->doInt(&pull->nstfout);
766 if (file_version >= tpxv_PullPrevStepCOMAsReference)
768 serializer->doBool(&pull->bSetPbcRefToPrevStepCOM);
772 pull->bSetPbcRefToPrevStepCOM = FALSE;
774 if (serializer->reading())
776 snew(pull->group, pull->ngroup);
777 snew(pull->coord, pull->ncoord);
779 if (file_version < 95)
781 /* epullgPOS for position pulling, before epullgDIRPBC was removed */
782 if (eGeomOld == epullgDIRPBC)
784 gmx_fatal(FARGS, "pull-geometry=position is no longer supported");
786 if (eGeomOld > epullgDIRPBC)
791 for (g = 0; g < pull->ngroup; g++)
793 /* We read and ignore a pull coordinate for group 0 */
794 do_pullgrp_tpx_pre95(serializer, &pull->group[g], &pull->coord[std::max(g-1, 0)]);
797 pull->coord[g-1].group[0] = 0;
798 pull->coord[g-1].group[1] = g;
802 pull->bPrintCOM = (pull->group[0].nat > 0);
806 for (g = 0; g < pull->ngroup; g++)
808 do_pull_group(serializer, &pull->group[g]);
810 for (g = 0; g < pull->ncoord; g++)
812 do_pull_coord(serializer, &pull->coord[g],
813 file_version, ePullOld, eGeomOld, dimOld);
816 if (file_version >= tpxv_PullAverage)
820 v = pull->bXOutAverage;
821 serializer->doBool(&v);
822 pull->bXOutAverage = v;
823 v = pull->bFOutAverage;
824 serializer->doBool(&v);
825 pull->bFOutAverage = v;
830 static void do_rotgrp(gmx::ISerializer *serializer,
833 serializer->doInt(&rotg->eType);
834 serializer->doInt(&rotg->bMassW);
835 serializer->doInt(&rotg->nat);
836 if (serializer->reading())
838 snew(rotg->ind, rotg->nat);
840 serializer->doIntArray(rotg->ind, rotg->nat);
841 if (serializer->reading())
843 snew(rotg->x_ref, rotg->nat);
845 serializer->doRvecArray(rotg->x_ref, rotg->nat);
846 serializer->doRvec(&rotg->inputVec);
847 serializer->doRvec(&rotg->pivot);
848 serializer->doReal(&rotg->rate);
849 serializer->doReal(&rotg->k);
850 serializer->doReal(&rotg->slab_dist);
851 serializer->doReal(&rotg->min_gaussian);
852 serializer->doReal(&rotg->eps);
853 serializer->doInt(&rotg->eFittype);
854 serializer->doInt(&rotg->PotAngle_nstep);
855 serializer->doReal(&rotg->PotAngle_step);
858 static void do_rot(gmx::ISerializer *serializer,
863 serializer->doInt(&rot->ngrp);
864 serializer->doInt(&rot->nstrout);
865 serializer->doInt(&rot->nstsout);
866 if (serializer->reading())
868 snew(rot->grp, rot->ngrp);
870 for (g = 0; g < rot->ngrp; g++)
872 do_rotgrp(serializer, &rot->grp[g]);
877 static void do_swapgroup(gmx::ISerializer *serializer,
881 /* Name of the group or molecule */
883 if (serializer->reading())
885 serializer->doString(&buf);
886 g->molname = gmx_strdup(buf.c_str());
891 serializer->doString(&buf);
894 /* Number of atoms in the group */
895 serializer->doInt(&g->nat);
897 /* The group's atom indices */
898 if (serializer->reading())
900 snew(g->ind, g->nat);
902 serializer->doIntArray(g->ind, g->nat);
904 /* Requested counts for compartments A and B */
905 serializer->doIntArray(g->nmolReq, eCompNR);
908 static void do_swapcoords_tpx(gmx::ISerializer *serializer,
912 /* Enums for better readability of the code */
917 eChannel0 = 0, eChannel1
921 if (file_version >= tpxv_CompElPolyatomicIonsAndMultipleIonTypes)
923 /* The total number of swap groups is the sum of the fixed groups
924 * (split0, split1, solvent), and the user-defined groups (2+ types of ions)
926 serializer->doInt(&swap->ngrp);
927 if (serializer->reading())
929 snew(swap->grp, swap->ngrp);
931 for (int ig = 0; ig < swap->ngrp; ig++)
933 do_swapgroup(serializer, &swap->grp[ig]);
935 serializer->doBool(&swap->massw_split[eChannel0]);
936 serializer->doBool(&swap->massw_split[eChannel1]);
937 serializer->doInt(&swap->nstswap);
938 serializer->doInt(&swap->nAverage);
939 serializer->doReal(&swap->threshold);
940 serializer->doReal(&swap->cyl0r);
941 serializer->doReal(&swap->cyl0u);
942 serializer->doReal(&swap->cyl0l);
943 serializer->doReal(&swap->cyl1r);
944 serializer->doReal(&swap->cyl1u);
945 serializer->doReal(&swap->cyl1l);
949 /*** Support reading older CompEl .tpr files ***/
951 /* In the original CompEl .tpr files, we always have 5 groups: */
953 snew(swap->grp, swap->ngrp);
955 swap->grp[eGrpSplit0 ].molname = gmx_strdup("split0" ); // group 0: split0
956 swap->grp[eGrpSplit1 ].molname = gmx_strdup("split1" ); // group 1: split1
957 swap->grp[eGrpSolvent].molname = gmx_strdup("solvent"); // group 2: solvent
958 swap->grp[3 ].molname = gmx_strdup("anions" ); // group 3: anions
959 swap->grp[4 ].molname = gmx_strdup("cations"); // group 4: cations
961 serializer->doInt(&swap->grp[3].nat);
962 serializer->doInt(&swap->grp[eGrpSolvent].nat);
963 serializer->doInt(&swap->grp[eGrpSplit0].nat);
964 serializer->doBool(&swap->massw_split[eChannel0]);
965 serializer->doInt(&swap->grp[eGrpSplit1].nat);
966 serializer->doBool(&swap->massw_split[eChannel1]);
967 serializer->doInt(&swap->nstswap);
968 serializer->doInt(&swap->nAverage);
969 serializer->doReal(&swap->threshold);
970 serializer->doReal(&swap->cyl0r);
971 serializer->doReal(&swap->cyl0u);
972 serializer->doReal(&swap->cyl0l);
973 serializer->doReal(&swap->cyl1r);
974 serializer->doReal(&swap->cyl1u);
975 serializer->doReal(&swap->cyl1l);
977 // The order[] array keeps compatibility with older .tpr files
978 // by reading in the groups in the classic order
980 const int order[4] = {3, eGrpSolvent, eGrpSplit0, eGrpSplit1 };
982 for (int ig = 0; ig < 4; ig++)
985 snew(swap->grp[g].ind, swap->grp[g].nat);
986 serializer->doIntArray(swap->grp[g].ind, swap->grp[g].nat);
990 for (int j = eCompA; j <= eCompB; j++)
992 serializer->doInt(&swap->grp[3].nmolReq[j]); // group 3 = anions
993 serializer->doInt(&swap->grp[4].nmolReq[j]); // group 4 = cations
995 } /* End support reading older CompEl .tpr files */
997 if (file_version >= tpxv_CompElWithSwapLayerOffset)
999 serializer->doReal(&swap->bulkOffset[eCompA]);
1000 serializer->doReal(&swap->bulkOffset[eCompB]);
1005 static void do_legacy_efield(gmx::ISerializer *serializer, gmx::KeyValueTreeObjectBuilder *root)
1007 const char *const dimName[] = { "x", "y", "z" };
1009 auto appliedForcesObj = root->addObject("applied-forces");
1010 auto efieldObj = appliedForcesObj.addObject("electric-field");
1011 // The content of the tpr file for this feature has
1012 // been the same since gromacs 4.0 that was used for
1014 for (int j = 0; j < DIM; ++j)
1017 serializer->doInt(&n);
1018 serializer->doInt(&nt);
1019 std::vector<real> aa(n+1), phi(nt+1), at(nt+1), phit(nt+1);
1020 serializer->doRealArray(aa.data(), n);
1021 serializer->doRealArray(phi.data(), n);
1022 serializer->doRealArray(at.data(), nt);
1023 serializer->doRealArray(phit.data(), nt);
1026 if (n > 1 || nt > 1)
1028 gmx_fatal(FARGS, "Can not handle tpr files with more than one electric field term per direction.");
1030 auto dimObj = efieldObj.addObject(dimName[j]);
1031 dimObj.addValue<real>("E0", aa[0]);
1032 dimObj.addValue<real>("omega", at[0]);
1033 dimObj.addValue<real>("t0", phi[0]);
1034 dimObj.addValue<real>("sigma", phit[0]);
1040 static void do_inputrec(gmx::ISerializer *serializer,
1044 int i, j, k, idum = 0;
1046 gmx_bool bdum = false;
1048 if (file_version != tpx_version)
1050 /* Give a warning about features that are not accessible */
1051 fprintf(stderr, "Note: file tpx version %d, software tpx version %d\n",
1052 file_version, tpx_version);
1055 if (file_version == 0)
1060 gmx::KeyValueTreeBuilder paramsBuilder;
1061 gmx::KeyValueTreeObjectBuilder paramsObj = paramsBuilder.rootObject();
1063 /* Basic inputrec stuff */
1064 serializer->doInt(&ir->eI);
1065 if (file_version >= 62)
1067 serializer->doInt64(&ir->nsteps);
1071 serializer->doInt(&idum);
1075 if (file_version >= 62)
1077 serializer->doInt64(&ir->init_step);
1081 serializer->doInt(&idum);
1082 ir->init_step = idum;
1085 serializer->doInt(&ir->simulation_part);
1087 if (file_version >= 67)
1089 serializer->doInt(&ir->nstcalcenergy);
1093 ir->nstcalcenergy = 1;
1095 if (file_version >= 81)
1097 serializer->doInt(&ir->cutoff_scheme);
1098 if (file_version < 94)
1100 ir->cutoff_scheme = 1 - ir->cutoff_scheme;
1105 ir->cutoff_scheme = ecutsGROUP;
1107 serializer->doInt(&ir->ns_type);
1108 serializer->doInt(&ir->nstlist);
1109 serializer->doInt(&idum); /* used to be ndelta; not used anymore */
1111 serializer->doReal(&ir->rtpi);
1113 serializer->doInt(&ir->nstcomm);
1114 serializer->doInt(&ir->comm_mode);
1116 /* ignore nstcheckpoint */
1117 if (file_version < tpxv_RemoveObsoleteParameters1)
1119 serializer->doInt(&idum);
1122 serializer->doInt(&ir->nstcgsteep);
1124 serializer->doInt(&ir->nbfgscorr);
1126 serializer->doInt(&ir->nstlog);
1127 serializer->doInt(&ir->nstxout);
1128 serializer->doInt(&ir->nstvout);
1129 serializer->doInt(&ir->nstfout);
1130 serializer->doInt(&ir->nstenergy);
1131 serializer->doInt(&ir->nstxout_compressed);
1132 if (file_version >= 59)
1134 serializer->doDouble(&ir->init_t);
1135 serializer->doDouble(&ir->delta_t);
1139 serializer->doReal(&rdum);
1141 serializer->doReal(&rdum);
1144 serializer->doReal(&ir->x_compression_precision);
1145 if (file_version >= 81)
1147 serializer->doReal(&ir->verletbuf_tol);
1151 ir->verletbuf_tol = 0;
1153 serializer->doReal(&ir->rlist);
1154 if (file_version >= 67 && file_version < tpxv_RemoveTwinRange)
1156 if (serializer->reading())
1158 // Reading such a file version could invoke the twin-range
1159 // scheme, about which mdrun should give a fatal error.
1160 real dummy_rlistlong = -1;
1161 serializer->doReal(&dummy_rlistlong);
1163 if (ir->rlist > 0 && (dummy_rlistlong == 0 || dummy_rlistlong > ir->rlist))
1165 // Get mdrun to issue an error (regardless of
1166 // ir->cutoff_scheme).
1167 ir->useTwinRange = true;
1171 // grompp used to set rlistlong actively. Users were
1172 // probably also confused and set rlistlong == rlist.
1173 // However, in all remaining cases, it is safe to let
1174 // mdrun proceed normally.
1175 ir->useTwinRange = false;
1181 // No need to read or write anything
1182 ir->useTwinRange = false;
1184 if (file_version >= 82 && file_version != 90)
1186 // Multiple time-stepping is no longer enabled, but the old
1187 // support required the twin-range scheme, for which mdrun
1188 // already emits a fatal error.
1189 int dummy_nstcalclr = -1;
1190 serializer->doInt(&dummy_nstcalclr);
1192 serializer->doInt(&ir->coulombtype);
1193 if (file_version >= 81)
1195 serializer->doInt(&ir->coulomb_modifier);
1199 ir->coulomb_modifier = (ir->cutoff_scheme == ecutsVERLET ? eintmodPOTSHIFT : eintmodNONE);
1201 serializer->doReal(&ir->rcoulomb_switch);
1202 serializer->doReal(&ir->rcoulomb);
1203 serializer->doInt(&ir->vdwtype);
1204 if (file_version >= 81)
1206 serializer->doInt(&ir->vdw_modifier);
1210 ir->vdw_modifier = (ir->cutoff_scheme == ecutsVERLET ? eintmodPOTSHIFT : eintmodNONE);
1212 serializer->doReal(&ir->rvdw_switch);
1213 serializer->doReal(&ir->rvdw);
1214 serializer->doInt(&ir->eDispCorr);
1215 serializer->doReal(&ir->epsilon_r);
1216 serializer->doReal(&ir->epsilon_rf);
1217 serializer->doReal(&ir->tabext);
1219 // This permits reading a .tpr file that used implicit solvent,
1220 // and later permitting mdrun to refuse to run it.
1221 if (serializer->reading())
1223 if (file_version < tpxv_RemoveImplicitSolvation)
1225 serializer->doInt(&idum);
1226 serializer->doInt(&idum);
1227 serializer->doReal(&rdum);
1228 serializer->doReal(&rdum);
1229 serializer->doInt(&idum);
1230 ir->implicit_solvent = (idum > 0);
1234 ir->implicit_solvent = false;
1236 if (file_version < tpxv_RemoveImplicitSolvation)
1238 serializer->doReal(&rdum);
1239 serializer->doReal(&rdum);
1240 serializer->doReal(&rdum);
1241 serializer->doReal(&rdum);
1242 if (file_version >= 60)
1244 serializer->doReal(&rdum);
1245 serializer->doInt(&idum);
1247 serializer->doReal(&rdum);
1251 if (file_version >= 81)
1253 serializer->doReal(&ir->fourier_spacing);
1257 ir->fourier_spacing = 0.0;
1259 serializer->doInt(&ir->nkx);
1260 serializer->doInt(&ir->nky);
1261 serializer->doInt(&ir->nkz);
1262 serializer->doInt(&ir->pme_order);
1263 serializer->doReal(&ir->ewald_rtol);
1265 if (file_version >= 93)
1267 serializer->doReal(&ir->ewald_rtol_lj);
1271 ir->ewald_rtol_lj = ir->ewald_rtol;
1273 serializer->doInt(&ir->ewald_geometry);
1274 serializer->doReal(&ir->epsilon_surface);
1276 /* ignore bOptFFT */
1277 if (file_version < tpxv_RemoveObsoleteParameters1)
1279 serializer->doBool(&bdum);
1282 if (file_version >= 93)
1284 serializer->doInt(&ir->ljpme_combination_rule);
1286 serializer->doBool(&ir->bContinuation);
1287 serializer->doInt(&ir->etc);
1288 /* before version 18, ir->etc was a gmx_bool (ir->btc),
1289 * but the values 0 and 1 still mean no and
1290 * berendsen temperature coupling, respectively.
1292 if (file_version >= 79)
1294 serializer->doBool(&ir->bPrintNHChains);
1296 if (file_version >= 71)
1298 serializer->doInt(&ir->nsttcouple);
1302 ir->nsttcouple = ir->nstcalcenergy;
1304 serializer->doInt(&ir->epc);
1305 serializer->doInt(&ir->epct);
1306 if (file_version >= 71)
1308 serializer->doInt(&ir->nstpcouple);
1312 ir->nstpcouple = ir->nstcalcenergy;
1314 serializer->doReal(&ir->tau_p);
1315 serializer->doRvec(&ir->ref_p[XX]);
1316 serializer->doRvec(&ir->ref_p[YY]);
1317 serializer->doRvec(&ir->ref_p[ZZ]);
1318 serializer->doRvec(&ir->compress[XX]);
1319 serializer->doRvec(&ir->compress[YY]);
1320 serializer->doRvec(&ir->compress[ZZ]);
1321 serializer->doInt(&ir->refcoord_scaling);
1322 serializer->doRvec(&ir->posres_com);
1323 serializer->doRvec(&ir->posres_comB);
1325 if (file_version < 79)
1327 serializer->doInt(&ir->andersen_seed);
1331 ir->andersen_seed = 0;
1334 serializer->doReal(&ir->shake_tol);
1336 serializer->doInt(&ir->efep);
1337 do_fepvals(serializer, ir->fepvals, file_version);
1339 if (file_version >= 79)
1341 serializer->doBool(&ir->bSimTemp);
1344 ir->bSimTemp = TRUE;
1349 ir->bSimTemp = FALSE;
1353 do_simtempvals(serializer, ir->simtempvals, ir->fepvals->n_lambda, file_version);
1356 if (file_version >= 79)
1358 serializer->doBool(&ir->bExpanded);
1361 ir->bExpanded = TRUE;
1365 ir->bExpanded = FALSE;
1370 do_expandedvals(serializer, ir->expandedvals, ir->fepvals, file_version);
1373 serializer->doInt(&ir->eDisre);
1374 serializer->doInt(&ir->eDisreWeighting);
1375 serializer->doBool(&ir->bDisreMixed);
1376 serializer->doReal(&ir->dr_fc);
1377 serializer->doReal(&ir->dr_tau);
1378 serializer->doInt(&ir->nstdisreout);
1379 serializer->doReal(&ir->orires_fc);
1380 serializer->doReal(&ir->orires_tau);
1381 serializer->doInt(&ir->nstorireout);
1383 /* ignore dihre_fc */
1384 if (file_version < 79)
1386 serializer->doReal(&rdum);
1389 serializer->doReal(&ir->em_stepsize);
1390 serializer->doReal(&ir->em_tol);
1391 serializer->doBool(&ir->bShakeSOR);
1392 serializer->doInt(&ir->niter);
1393 serializer->doReal(&ir->fc_stepsize);
1394 serializer->doInt(&ir->eConstrAlg);
1395 serializer->doInt(&ir->nProjOrder);
1396 serializer->doReal(&ir->LincsWarnAngle);
1397 serializer->doInt(&ir->nLincsIter);
1398 serializer->doReal(&ir->bd_fric);
1399 if (file_version >= tpxv_Use64BitRandomSeed)
1401 serializer->doInt64(&ir->ld_seed);
1405 serializer->doInt(&idum);
1409 for (i = 0; i < DIM; i++)
1411 serializer->doRvec(&ir->deform[i]);
1413 serializer->doReal(&ir->cos_accel);
1415 serializer->doInt(&ir->userint1);
1416 serializer->doInt(&ir->userint2);
1417 serializer->doInt(&ir->userint3);
1418 serializer->doInt(&ir->userint4);
1419 serializer->doReal(&ir->userreal1);
1420 serializer->doReal(&ir->userreal2);
1421 serializer->doReal(&ir->userreal3);
1422 serializer->doReal(&ir->userreal4);
1424 /* AdResS is removed, but we need to be able to read old files,
1425 and let mdrun refuse to run them */
1426 if (file_version >= 77 && file_version < tpxv_RemoveAdress)
1428 serializer->doBool(&ir->bAdress);
1431 int idum, numThermoForceGroups, numEnergyGroups;
1434 serializer->doInt(&idum);
1435 serializer->doReal(&rdum);
1436 serializer->doReal(&rdum);
1437 serializer->doReal(&rdum);
1438 serializer->doInt(&idum);
1439 serializer->doInt(&idum);
1440 serializer->doRvec(&rvecdum);
1441 serializer->doInt(&numThermoForceGroups);
1442 serializer->doReal(&rdum);
1443 serializer->doInt(&numEnergyGroups);
1444 serializer->doInt(&idum);
1446 if (numThermoForceGroups > 0)
1448 std::vector<int> idumn(numThermoForceGroups);
1449 serializer->doIntArray(idumn.data(), idumn.size());
1451 if (numEnergyGroups > 0)
1453 std::vector<int> idumn(numEnergyGroups);
1454 serializer->doIntArray(idumn.data(), idumn.size());
1460 ir->bAdress = FALSE;
1467 if (file_version >= tpxv_PullCoordTypeGeom)
1469 serializer->doBool(&ir->bPull);
1473 serializer->doInt(&ePullOld);
1474 ir->bPull = (ePullOld > 0);
1475 /* We removed the first ePull=ePullNo for the enum */
1480 if (serializer->reading())
1484 do_pull(serializer, ir->pull, file_version, ePullOld);
1488 if (file_version >= tpxv_AcceleratedWeightHistogram)
1490 serializer->doBool(&ir->bDoAwh);
1494 if (serializer->reading())
1496 snew(ir->awhParams, 1);
1498 do_awh(serializer, ir->awhParams, file_version);
1506 /* Enforced rotation */
1507 if (file_version >= 74)
1509 serializer->doBool(&ir->bRot);
1512 if (serializer->reading())
1516 do_rot(serializer, ir->rot);
1524 /* Interactive molecular dynamics */
1525 if (file_version >= tpxv_InteractiveMolecularDynamics)
1527 serializer->doBool(&ir->bIMD);
1530 if (serializer->reading())
1534 do_imd(serializer, ir->imd);
1539 /* We don't support IMD sessions for old .tpr files */
1544 serializer->doInt(&ir->opts.ngtc);
1545 if (file_version >= 69)
1547 serializer->doInt(&ir->opts.nhchainlength);
1551 ir->opts.nhchainlength = 1;
1553 serializer->doInt(&ir->opts.ngacc);
1554 serializer->doInt(&ir->opts.ngfrz);
1555 serializer->doInt(&ir->opts.ngener);
1557 if (serializer->reading())
1559 snew(ir->opts.nrdf, ir->opts.ngtc);
1560 snew(ir->opts.ref_t, ir->opts.ngtc);
1561 snew(ir->opts.annealing, ir->opts.ngtc);
1562 snew(ir->opts.anneal_npoints, ir->opts.ngtc);
1563 snew(ir->opts.anneal_time, ir->opts.ngtc);
1564 snew(ir->opts.anneal_temp, ir->opts.ngtc);
1565 snew(ir->opts.tau_t, ir->opts.ngtc);
1566 snew(ir->opts.nFreeze, ir->opts.ngfrz);
1567 snew(ir->opts.acc, ir->opts.ngacc);
1568 snew(ir->opts.egp_flags, ir->opts.ngener*ir->opts.ngener);
1570 if (ir->opts.ngtc > 0)
1572 serializer->doRealArray(ir->opts.nrdf, ir->opts.ngtc);
1573 serializer->doRealArray(ir->opts.ref_t, ir->opts.ngtc);
1574 serializer->doRealArray(ir->opts.tau_t, ir->opts.ngtc);
1576 if (ir->opts.ngfrz > 0)
1578 serializer->doIvecArray(ir->opts.nFreeze, ir->opts.ngfrz);
1580 if (ir->opts.ngacc > 0)
1582 serializer->doRvecArray(ir->opts.acc, ir->opts.ngacc);
1584 serializer->doIntArray(ir->opts.egp_flags,
1585 ir->opts.ngener*ir->opts.ngener);
1587 /* First read the lists with annealing and npoints for each group */
1588 serializer->doIntArray(ir->opts.annealing, ir->opts.ngtc);
1589 serializer->doIntArray(ir->opts.anneal_npoints, ir->opts.ngtc);
1590 for (j = 0; j < (ir->opts.ngtc); j++)
1592 k = ir->opts.anneal_npoints[j];
1593 if (serializer->reading())
1595 snew(ir->opts.anneal_time[j], k);
1596 snew(ir->opts.anneal_temp[j], k);
1598 serializer->doRealArray(ir->opts.anneal_time[j], k);
1599 serializer->doRealArray(ir->opts.anneal_temp[j], k);
1603 serializer->doInt(&ir->nwall);
1604 serializer->doInt(&ir->wall_type);
1605 serializer->doReal(&ir->wall_r_linpot);
1606 serializer->doInt(&ir->wall_atomtype[0]);
1607 serializer->doInt(&ir->wall_atomtype[1]);
1608 serializer->doReal(&ir->wall_density[0]);
1609 serializer->doReal(&ir->wall_density[1]);
1610 serializer->doReal(&ir->wall_ewald_zfac);
1613 /* Cosine stuff for electric fields */
1614 if (file_version < tpxv_GenericParamsForElectricField)
1616 do_legacy_efield(serializer, ¶msObj);
1620 if (file_version >= tpxv_ComputationalElectrophysiology)
1622 serializer->doInt(&ir->eSwapCoords);
1623 if (ir->eSwapCoords != eswapNO)
1625 if (serializer->reading())
1629 do_swapcoords_tpx(serializer, ir->swap, file_version);
1635 serializer->doBool(&ir->bQMMM);
1636 serializer->doInt(&ir->QMMMscheme);
1637 serializer->doReal(&ir->scalefactor);
1638 serializer->doInt(&ir->opts.ngQM);
1639 if (serializer->reading())
1641 snew(ir->opts.QMmethod, ir->opts.ngQM);
1642 snew(ir->opts.QMbasis, ir->opts.ngQM);
1643 snew(ir->opts.QMcharge, ir->opts.ngQM);
1644 snew(ir->opts.QMmult, ir->opts.ngQM);
1645 snew(ir->opts.bSH, ir->opts.ngQM);
1646 snew(ir->opts.CASorbitals, ir->opts.ngQM);
1647 snew(ir->opts.CASelectrons, ir->opts.ngQM);
1648 snew(ir->opts.SAon, ir->opts.ngQM);
1649 snew(ir->opts.SAoff, ir->opts.ngQM);
1650 snew(ir->opts.SAsteps, ir->opts.ngQM);
1652 if (ir->opts.ngQM > 0 && ir->bQMMM)
1654 serializer->doIntArray(ir->opts.QMmethod, ir->opts.ngQM);
1655 serializer->doIntArray(ir->opts.QMbasis, ir->opts.ngQM);
1656 serializer->doIntArray(ir->opts.QMcharge, ir->opts.ngQM);
1657 serializer->doIntArray(ir->opts.QMmult, ir->opts.ngQM);
1658 serializer->doBoolArray(ir->opts.bSH, ir->opts.ngQM);
1659 serializer->doIntArray(ir->opts.CASorbitals, ir->opts.ngQM);
1660 serializer->doIntArray(ir->opts.CASelectrons, ir->opts.ngQM);
1661 serializer->doRealArray(ir->opts.SAon, ir->opts.ngQM);
1662 serializer->doRealArray(ir->opts.SAoff, ir->opts.ngQM);
1663 serializer->doIntArray(ir->opts.SAsteps, ir->opts.ngQM);
1664 /* We leave in dummy i/o for removed parameters to avoid
1665 * changing the tpr format for every QMMM change.
1667 std::vector<int> dummy(ir->opts.ngQM, 0);
1668 serializer->doIntArray(dummy.data(), ir->opts.ngQM);
1669 serializer->doIntArray(dummy.data(), ir->opts.ngQM);
1671 /* end of QMMM stuff */
1674 if (file_version >= tpxv_GenericParamsForElectricField)
1676 if (serializer->reading())
1678 paramsObj.mergeObject(
1679 gmx::deserializeKeyValueTree(serializer));
1683 GMX_RELEASE_ASSERT(ir->params != nullptr,
1684 "Parameters should be present when writing inputrec");
1685 gmx::serializeKeyValueTree(*ir->params, serializer);
1688 if (serializer->reading())
1690 ir->params = new gmx::KeyValueTreeObject(paramsBuilder.build());
1691 // Initialize internal parameters to an empty kvt for all tpr versions
1692 ir->internalParameters = std::make_unique<gmx::KeyValueTreeObject>();
1695 if (file_version >= tpxv_GenericInternalParameters)
1697 if (serializer->reading())
1699 ir->internalParameters = std::make_unique<gmx::KeyValueTreeObject>(gmx::deserializeKeyValueTree(serializer));
1703 GMX_RELEASE_ASSERT(ir->internalParameters != nullptr,
1704 "Parameters should be present when writing inputrec");
1705 gmx::serializeKeyValueTree(*ir->internalParameters, serializer);
1711 static void do_harm(gmx::ISerializer *serializer, t_iparams *iparams)
1713 serializer->doReal(&iparams->harmonic.rA);
1714 serializer->doReal(&iparams->harmonic.krA);
1715 serializer->doReal(&iparams->harmonic.rB);
1716 serializer->doReal(&iparams->harmonic.krB);
1719 static void do_iparams(gmx::ISerializer *serializer,
1735 do_harm(serializer, iparams);
1736 if ((ftype == F_ANGRES || ftype == F_ANGRESZ) && serializer->reading())
1738 /* Correct incorrect storage of parameters */
1739 iparams->pdihs.phiB = iparams->pdihs.phiA;
1740 iparams->pdihs.cpB = iparams->pdihs.cpA;
1744 serializer->doReal(&iparams->harmonic.rA);
1745 serializer->doReal(&iparams->harmonic.krA);
1747 case F_LINEAR_ANGLES:
1748 serializer->doReal(&iparams->linangle.klinA);
1749 serializer->doReal(&iparams->linangle.aA);
1750 serializer->doReal(&iparams->linangle.klinB);
1751 serializer->doReal(&iparams->linangle.aB);
1754 serializer->doReal(&iparams->fene.bm);
1755 serializer->doReal(&iparams->fene.kb);
1759 serializer->doReal(&iparams->restraint.lowA);
1760 serializer->doReal(&iparams->restraint.up1A);
1761 serializer->doReal(&iparams->restraint.up2A);
1762 serializer->doReal(&iparams->restraint.kA);
1763 serializer->doReal(&iparams->restraint.lowB);
1764 serializer->doReal(&iparams->restraint.up1B);
1765 serializer->doReal(&iparams->restraint.up2B);
1766 serializer->doReal(&iparams->restraint.kB);
1772 serializer->doReal(&iparams->tab.kA);
1773 serializer->doInt(&iparams->tab.table);
1774 serializer->doReal(&iparams->tab.kB);
1776 case F_CROSS_BOND_BONDS:
1777 serializer->doReal(&iparams->cross_bb.r1e);
1778 serializer->doReal(&iparams->cross_bb.r2e);
1779 serializer->doReal(&iparams->cross_bb.krr);
1781 case F_CROSS_BOND_ANGLES:
1782 serializer->doReal(&iparams->cross_ba.r1e);
1783 serializer->doReal(&iparams->cross_ba.r2e);
1784 serializer->doReal(&iparams->cross_ba.r3e);
1785 serializer->doReal(&iparams->cross_ba.krt);
1787 case F_UREY_BRADLEY:
1788 serializer->doReal(&iparams->u_b.thetaA);
1789 serializer->doReal(&iparams->u_b.kthetaA);
1790 serializer->doReal(&iparams->u_b.r13A);
1791 serializer->doReal(&iparams->u_b.kUBA);
1792 if (file_version >= 79)
1794 serializer->doReal(&iparams->u_b.thetaB);
1795 serializer->doReal(&iparams->u_b.kthetaB);
1796 serializer->doReal(&iparams->u_b.r13B);
1797 serializer->doReal(&iparams->u_b.kUBB);
1801 iparams->u_b.thetaB = iparams->u_b.thetaA;
1802 iparams->u_b.kthetaB = iparams->u_b.kthetaA;
1803 iparams->u_b.r13B = iparams->u_b.r13A;
1804 iparams->u_b.kUBB = iparams->u_b.kUBA;
1807 case F_QUARTIC_ANGLES:
1808 serializer->doReal(&iparams->qangle.theta);
1809 serializer->doRealArray(iparams->qangle.c, 5);
1812 serializer->doReal(&iparams->bham.a);
1813 serializer->doReal(&iparams->bham.b);
1814 serializer->doReal(&iparams->bham.c);
1817 serializer->doReal(&iparams->morse.b0A);
1818 serializer->doReal(&iparams->morse.cbA);
1819 serializer->doReal(&iparams->morse.betaA);
1820 if (file_version >= 79)
1822 serializer->doReal(&iparams->morse.b0B);
1823 serializer->doReal(&iparams->morse.cbB);
1824 serializer->doReal(&iparams->morse.betaB);
1828 iparams->morse.b0B = iparams->morse.b0A;
1829 iparams->morse.cbB = iparams->morse.cbA;
1830 iparams->morse.betaB = iparams->morse.betaA;
1834 serializer->doReal(&iparams->cubic.b0);
1835 serializer->doReal(&iparams->cubic.kb);
1836 serializer->doReal(&iparams->cubic.kcub);
1840 case F_POLARIZATION:
1841 serializer->doReal(&iparams->polarize.alpha);
1844 serializer->doReal(&iparams->anharm_polarize.alpha);
1845 serializer->doReal(&iparams->anharm_polarize.drcut);
1846 serializer->doReal(&iparams->anharm_polarize.khyp);
1849 serializer->doReal(&iparams->wpol.al_x);
1850 serializer->doReal(&iparams->wpol.al_y);
1851 serializer->doReal(&iparams->wpol.al_z);
1852 serializer->doReal(&iparams->wpol.rOH);
1853 serializer->doReal(&iparams->wpol.rHH);
1854 serializer->doReal(&iparams->wpol.rOD);
1857 serializer->doReal(&iparams->thole.a);
1858 serializer->doReal(&iparams->thole.alpha1);
1859 serializer->doReal(&iparams->thole.alpha2);
1860 serializer->doReal(&iparams->thole.rfac);
1863 serializer->doReal(&iparams->lj.c6);
1864 serializer->doReal(&iparams->lj.c12);
1867 serializer->doReal(&iparams->lj14.c6A);
1868 serializer->doReal(&iparams->lj14.c12A);
1869 serializer->doReal(&iparams->lj14.c6B);
1870 serializer->doReal(&iparams->lj14.c12B);
1873 serializer->doReal(&iparams->ljc14.fqq);
1874 serializer->doReal(&iparams->ljc14.qi);
1875 serializer->doReal(&iparams->ljc14.qj);
1876 serializer->doReal(&iparams->ljc14.c6);
1877 serializer->doReal(&iparams->ljc14.c12);
1879 case F_LJC_PAIRS_NB:
1880 serializer->doReal(&iparams->ljcnb.qi);
1881 serializer->doReal(&iparams->ljcnb.qj);
1882 serializer->doReal(&iparams->ljcnb.c6);
1883 serializer->doReal(&iparams->ljcnb.c12);
1889 serializer->doReal(&iparams->pdihs.phiA);
1890 serializer->doReal(&iparams->pdihs.cpA);
1891 serializer->doReal(&iparams->pdihs.phiB);
1892 serializer->doReal(&iparams->pdihs.cpB);
1893 serializer->doInt(&iparams->pdihs.mult);
1896 serializer->doReal(&iparams->pdihs.phiA);
1897 serializer->doReal(&iparams->pdihs.cpA);
1900 serializer->doInt(&iparams->disres.label);
1901 serializer->doInt(&iparams->disres.type);
1902 serializer->doReal(&iparams->disres.low);
1903 serializer->doReal(&iparams->disres.up1);
1904 serializer->doReal(&iparams->disres.up2);
1905 serializer->doReal(&iparams->disres.kfac);
1908 serializer->doInt(&iparams->orires.ex);
1909 serializer->doInt(&iparams->orires.label);
1910 serializer->doInt(&iparams->orires.power);
1911 serializer->doReal(&iparams->orires.c);
1912 serializer->doReal(&iparams->orires.obs);
1913 serializer->doReal(&iparams->orires.kfac);
1916 if (file_version < 82)
1918 serializer->doInt(&idum);
1919 serializer->doInt(&idum);
1921 serializer->doReal(&iparams->dihres.phiA);
1922 serializer->doReal(&iparams->dihres.dphiA);
1923 serializer->doReal(&iparams->dihres.kfacA);
1924 if (file_version >= 82)
1926 serializer->doReal(&iparams->dihres.phiB);
1927 serializer->doReal(&iparams->dihres.dphiB);
1928 serializer->doReal(&iparams->dihres.kfacB);
1932 iparams->dihres.phiB = iparams->dihres.phiA;
1933 iparams->dihres.dphiB = iparams->dihres.dphiA;
1934 iparams->dihres.kfacB = iparams->dihres.kfacA;
1938 serializer->doRvec(&iparams->posres.pos0A);
1939 serializer->doRvec(&iparams->posres.fcA);
1940 serializer->doRvec(&iparams->posres.pos0B);
1941 serializer->doRvec(&iparams->posres.fcB);
1944 serializer->doInt(&iparams->fbposres.geom);
1945 serializer->doRvec(&iparams->fbposres.pos0);
1946 serializer->doReal(&iparams->fbposres.r);
1947 serializer->doReal(&iparams->fbposres.k);
1950 serializer->doRealArray(iparams->cbtdihs.cbtcA, NR_CBTDIHS);
1953 serializer->doRealArray(iparams->rbdihs.rbcA, NR_RBDIHS);
1954 serializer->doRealArray(iparams->rbdihs.rbcB, NR_RBDIHS);
1957 /* Fourier dihedrals are internally represented
1958 * as Ryckaert-Bellemans since those are faster to compute.
1960 serializer->doRealArray(iparams->rbdihs.rbcA, NR_RBDIHS);
1961 serializer->doRealArray(iparams->rbdihs.rbcB, NR_RBDIHS);
1965 serializer->doReal(&iparams->constr.dA);
1966 serializer->doReal(&iparams->constr.dB);
1969 serializer->doReal(&iparams->settle.doh);
1970 serializer->doReal(&iparams->settle.dhh);
1974 serializer->doReal(&iparams->vsite.a);
1979 serializer->doReal(&iparams->vsite.a);
1980 serializer->doReal(&iparams->vsite.b);
1985 serializer->doReal(&iparams->vsite.a);
1986 serializer->doReal(&iparams->vsite.b);
1987 serializer->doReal(&iparams->vsite.c);
1990 serializer->doInt(&iparams->vsiten.n);
1991 serializer->doReal(&iparams->vsiten.a);
1993 case F_GB12_NOLONGERUSED:
1994 case F_GB13_NOLONGERUSED:
1995 case F_GB14_NOLONGERUSED:
1996 // Implicit solvent parameters can still be read, but never used
1997 if (serializer->reading())
1999 if (file_version < 68)
2001 serializer->doReal(&rdum);
2002 serializer->doReal(&rdum);
2003 serializer->doReal(&rdum);
2004 serializer->doReal(&rdum);
2006 if (file_version < tpxv_RemoveImplicitSolvation)
2008 serializer->doReal(&rdum);
2009 serializer->doReal(&rdum);
2010 serializer->doReal(&rdum);
2011 serializer->doReal(&rdum);
2012 serializer->doReal(&rdum);
2017 serializer->doInt(&iparams->cmap.cmapA);
2018 serializer->doInt(&iparams->cmap.cmapB);
2021 gmx_fatal(FARGS, "unknown function type %d (%s) in %s line %d",
2022 ftype, interaction_function[ftype].name, __FILE__, __LINE__);
2026 static void do_ilist(gmx::ISerializer *serializer, InteractionList *ilist)
2028 int nr = ilist->size();
2029 serializer->doInt(&nr);
2030 if (serializer->reading())
2032 ilist->iatoms.resize(nr);
2034 serializer->doIntArray(ilist->iatoms.data(), ilist->size());
2037 static void do_ffparams(gmx::ISerializer *serializer,
2038 gmx_ffparams_t *ffparams,
2041 serializer->doInt(&ffparams->atnr);
2042 int numTypes = ffparams->numTypes();
2043 serializer->doInt(&numTypes);
2044 if (serializer->reading())
2046 ffparams->functype.resize(numTypes);
2047 ffparams->iparams.resize(numTypes);
2049 /* Read/write all the function types */
2050 serializer->doIntArray(ffparams->functype.data(), ffparams->functype.size());
2052 if (file_version >= 66)
2054 serializer->doDouble(&ffparams->reppow);
2058 ffparams->reppow = 12.0;
2061 serializer->doReal(&ffparams->fudgeQQ);
2063 /* Check whether all these function types are supported by the code.
2064 * In practice the code is backwards compatible, which means that the
2065 * numbering may have to be altered from old numbering to new numbering
2067 for (int i = 0; i < ffparams->numTypes(); i++)
2069 if (serializer->reading())
2071 /* Loop over file versions */
2072 for (int k = 0; k < NFTUPD; k++)
2074 /* Compare the read file_version to the update table */
2075 if ((file_version < ftupd[k].fvnr) &&
2076 (ffparams->functype[i] >= ftupd[k].ftype))
2078 ffparams->functype[i] += 1;
2083 do_iparams(serializer, ffparams->functype[i], &ffparams->iparams[i],
2088 static void add_settle_atoms(InteractionList *ilist)
2092 /* Settle used to only store the first atom: add the other two */
2093 ilist->iatoms.resize(2*ilist->size());
2094 for (i = ilist->size()/4 - 1; i >= 0; i--)
2096 ilist->iatoms[4*i+0] = ilist->iatoms[2*i+0];
2097 ilist->iatoms[4*i+1] = ilist->iatoms[2*i+1];
2098 ilist->iatoms[4*i+2] = ilist->iatoms[2*i+1] + 1;
2099 ilist->iatoms[4*i+3] = ilist->iatoms[2*i+1] + 2;
2103 static void do_ilists(gmx::ISerializer *serializer,
2104 InteractionLists *ilists,
2107 GMX_RELEASE_ASSERT(ilists, "Need a valid ilists object");
2108 GMX_RELEASE_ASSERT(ilists->size() == F_NRE, "The code needs to be in sync with InteractionLists");
2110 for (int j = 0; j < F_NRE; j++)
2112 InteractionList &ilist = (*ilists)[j];
2113 gmx_bool bClear = FALSE;
2114 if (serializer->reading())
2116 for (int k = 0; k < NFTUPD; k++)
2118 if ((file_version < ftupd[k].fvnr) && (j == ftupd[k].ftype))
2126 ilist.iatoms.clear();
2130 do_ilist(serializer, &ilist);
2131 if (file_version < 78 && j == F_SETTLE && ilist.size() > 0)
2133 add_settle_atoms(&ilist);
2139 static void do_block(gmx::ISerializer *serializer, t_block *block)
2141 serializer->doInt(&block->nr);
2142 if (serializer->reading())
2144 if ((block->nalloc_index > 0) && (nullptr != block->index))
2146 sfree(block->index);
2148 block->nalloc_index = block->nr+1;
2149 snew(block->index, block->nalloc_index);
2151 serializer->doIntArray(block->index, block->nr+1);
2154 static void do_blocka(gmx::ISerializer *serializer, t_blocka *block)
2156 serializer->doInt(&block->nr);
2157 serializer->doInt(&block->nra);
2158 if (serializer->reading())
2160 block->nalloc_index = block->nr+1;
2161 snew(block->index, block->nalloc_index);
2162 block->nalloc_a = block->nra;
2163 snew(block->a, block->nalloc_a);
2165 serializer->doIntArray(block->index, block->nr+1);
2166 serializer->doIntArray(block->a, block->nra);
2169 /* This is a primitive routine to make it possible to translate atomic numbers
2170 * to element names when reading TPR files, without making the Gromacs library
2171 * directory a dependency on mdrun (which is the case if we need elements.dat).
2174 atomicnumber_to_element(int atomicnumber)
2178 /* This does not have to be complete, so we only include elements likely
2179 * to occur in PDB files.
2181 switch (atomicnumber)
2183 case 1: p = "H"; break;
2184 case 5: p = "B"; break;
2185 case 6: p = "C"; break;
2186 case 7: p = "N"; break;
2187 case 8: p = "O"; break;
2188 case 9: p = "F"; break;
2189 case 11: p = "Na"; break;
2190 case 12: p = "Mg"; break;
2191 case 15: p = "P"; break;
2192 case 16: p = "S"; break;
2193 case 17: p = "Cl"; break;
2194 case 18: p = "Ar"; break;
2195 case 19: p = "K"; break;
2196 case 20: p = "Ca"; break;
2197 case 25: p = "Mn"; break;
2198 case 26: p = "Fe"; break;
2199 case 28: p = "Ni"; break;
2200 case 29: p = "Cu"; break;
2201 case 30: p = "Zn"; break;
2202 case 35: p = "Br"; break;
2203 case 47: p = "Ag"; break;
2204 default: p = ""; break;
2210 static void do_atom(gmx::ISerializer *serializer, t_atom *atom)
2212 serializer->doReal(&atom->m);
2213 serializer->doReal(&atom->q);
2214 serializer->doReal(&atom->mB);
2215 serializer->doReal(&atom->qB);
2216 serializer->doUShort(&atom->type);
2217 serializer->doUShort(&atom->typeB);
2218 serializer->doInt(&atom->ptype);
2219 serializer->doInt(&atom->resind);
2220 serializer->doInt(&atom->atomnumber);
2221 if (serializer->reading())
2223 /* Set element string from atomic number if present.
2224 * This routine returns an empty string if the name is not found.
2226 std::strncpy(atom->elem, atomicnumber_to_element(atom->atomnumber), 4);
2227 /* avoid warnings about potentially unterminated string */
2228 atom->elem[3] = '\0';
2232 static void do_grps(gmx::ISerializer *serializer,
2233 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,
2270 for (j = 0; (j < nstr); j++)
2272 do_symstr(serializer, &(nm[j]), symtab);
2276 static void do_resinfo(gmx::ISerializer *serializer,
2284 for (j = 0; (j < n); j++)
2286 do_symstr(serializer, &(ri[j].name), symtab);
2287 if (file_version >= 63)
2289 serializer->doInt(&ri[j].nr);
2290 serializer->doUChar(&ri[j].ic);
2300 static void do_atoms(gmx::ISerializer *serializer,
2307 serializer->doInt(&atoms->nr);
2308 serializer->doInt(&atoms->nres);
2309 if (serializer->reading())
2311 /* Since we have always written all t_atom properties in the tpr file
2312 * (at least for all backward compatible versions), we don't store
2313 * but simple set the booleans here.
2315 atoms->haveMass = TRUE;
2316 atoms->haveCharge = TRUE;
2317 atoms->haveType = TRUE;
2318 atoms->haveBState = TRUE;
2319 atoms->havePdbInfo = FALSE;
2321 snew(atoms->atom, atoms->nr);
2322 snew(atoms->atomname, atoms->nr);
2323 snew(atoms->atomtype, atoms->nr);
2324 snew(atoms->atomtypeB, atoms->nr);
2325 snew(atoms->resinfo, atoms->nres);
2326 atoms->pdbinfo = nullptr;
2330 GMX_RELEASE_ASSERT(atoms->haveMass && atoms->haveCharge && atoms->haveType && atoms->haveBState, "Mass, charge, atomtype and B-state parameters should be present in t_atoms when writing a tpr file");
2332 for (i = 0; (i < atoms->nr); i++)
2334 do_atom(serializer, &atoms->atom[i]);
2336 do_strstr(serializer, atoms->nr, atoms->atomname, symtab);
2337 do_strstr(serializer, atoms->nr, atoms->atomtype, symtab);
2338 do_strstr(serializer, atoms->nr, atoms->atomtypeB, symtab);
2340 do_resinfo(serializer, atoms->nres, atoms->resinfo, symtab, file_version);
2343 static void do_groups(gmx::ISerializer *serializer,
2344 SimulationGroups *groups,
2347 do_grps(serializer, groups->groups);
2348 int numberOfGroupNames = groups->groupNames.size();
2349 serializer->doInt(&numberOfGroupNames);
2350 if (serializer->reading())
2352 groups->groupNames.resize(numberOfGroupNames);
2354 do_strstr(serializer, numberOfGroupNames, groups->groupNames.data(), symtab);
2355 for (auto group : gmx::keysOf(groups->groupNumbers))
2357 int numberOfGroupNumbers = groups->numberOfGroupNumbers(group);
2358 serializer->doInt(&numberOfGroupNumbers);
2359 if (numberOfGroupNumbers != 0)
2361 if (serializer->reading())
2363 groups->groupNumbers[group].resize(numberOfGroupNumbers);
2365 serializer->doUCharArray(groups->groupNumbers[group].data(), numberOfGroupNumbers);
2370 static void do_atomtypes(gmx::ISerializer *serializer, t_atomtypes *atomtypes,
2375 serializer->doInt(&atomtypes->nr);
2377 if (serializer->reading())
2379 snew(atomtypes->atomnumber, j);
2381 if (serializer->reading() && file_version < tpxv_RemoveImplicitSolvation)
2383 std::vector<real> dummy(atomtypes->nr, 0);
2384 serializer->doRealArray(dummy.data(), dummy.size());
2385 serializer->doRealArray(dummy.data(), dummy.size());
2386 serializer->doRealArray(dummy.data(), dummy.size());
2388 serializer->doIntArray(atomtypes->atomnumber, j);
2390 if (serializer->reading() && file_version >= 60 && file_version < tpxv_RemoveImplicitSolvation)
2392 std::vector<real> dummy(atomtypes->nr, 0);
2393 serializer->doRealArray(dummy.data(), dummy.size());
2394 serializer->doRealArray(dummy.data(), dummy.size());
2398 static void do_symtab(gmx::ISerializer *serializer, t_symtab *symtab)
2403 serializer->doInt(&symtab->nr);
2405 if (serializer->reading())
2407 snew(symtab->symbuf, 1);
2408 symbuf = symtab->symbuf;
2409 symbuf->bufsize = nr;
2410 snew(symbuf->buf, nr);
2411 for (i = 0; (i < nr); i++)
2414 serializer->doString(&buf);
2415 symbuf->buf[i] = gmx_strdup(buf.c_str());
2420 symbuf = symtab->symbuf;
2421 while (symbuf != nullptr)
2423 for (i = 0; (i < symbuf->bufsize) && (i < nr); i++)
2425 std::string buf = symbuf->buf[i];
2426 serializer->doString(&buf);
2429 symbuf = symbuf->next;
2433 gmx_fatal(FARGS, "nr of symtab strings left: %d", nr);
2438 static void do_cmap(gmx::ISerializer *serializer, gmx_cmap_t *cmap_grid)
2441 int ngrid = cmap_grid->cmapdata.size();
2442 serializer->doInt(&ngrid);
2443 serializer->doInt(&cmap_grid->grid_spacing);
2445 int gs = cmap_grid->grid_spacing;
2446 int nelem = gs * gs;
2448 if (serializer->reading())
2450 cmap_grid->cmapdata.resize(ngrid);
2452 for (int i = 0; i < ngrid; i++)
2454 cmap_grid->cmapdata[i].cmap.resize(4*nelem);
2458 for (int i = 0; i < ngrid; i++)
2460 for (int j = 0; j < nelem; j++)
2462 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j*4]);
2463 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j*4+1]);
2464 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j*4+2]);
2465 serializer->doReal(&cmap_grid->cmapdata[i].cmap[j*4+3]);
2471 static void do_moltype(gmx::ISerializer *serializer,
2472 gmx_moltype_t *molt,
2476 do_symstr(serializer, &(molt->name), symtab);
2478 do_atoms(serializer, &molt->atoms, symtab, file_version);
2480 do_ilists(serializer, &molt->ilist, file_version);
2482 do_block(serializer, &molt->cgs);
2484 /* This used to be in the atoms struct */
2485 do_blocka(serializer, &molt->excls);
2488 static void do_molblock(gmx::ISerializer *serializer,
2489 gmx_molblock_t *molb,
2490 int numAtomsPerMolecule)
2492 serializer->doInt(&molb->type);
2493 serializer->doInt(&molb->nmol);
2494 /* To maintain forward topology reading compatibility, we store #atoms.
2495 * TODO: Change this to conditional reading of a dummy int when we
2496 * increase tpx_generation.
2498 serializer->doInt(&numAtomsPerMolecule);
2499 /* Position restraint coordinates */
2500 int numPosres_xA = molb->posres_xA.size();
2501 serializer->doInt(&numPosres_xA);
2502 if (numPosres_xA > 0)
2504 if (serializer->reading())
2506 molb->posres_xA.resize(numPosres_xA);
2508 serializer->doRvecArray(as_rvec_array(molb->posres_xA.data()), numPosres_xA);
2510 int numPosres_xB = molb->posres_xB.size();
2511 serializer->doInt(&numPosres_xB);
2512 if (numPosres_xB > 0)
2514 if (serializer->reading())
2516 molb->posres_xB.resize(numPosres_xB);
2518 serializer->doRvecArray(as_rvec_array(molb->posres_xB.data()), numPosres_xB);
2523 static void set_disres_npair(gmx_mtop_t *mtop)
2525 gmx_mtop_ilistloop_t iloop;
2528 gmx::ArrayRef<t_iparams> ip = mtop->ffparams.iparams;
2530 iloop = gmx_mtop_ilistloop_init(mtop);
2531 while (const InteractionLists *ilist = gmx_mtop_ilistloop_next(iloop, &nmol))
2533 const InteractionList &il = (*ilist)[F_DISRES];
2537 gmx::ArrayRef<const int> a = il.iatoms;
2539 for (int i = 0; i < il.size(); i += 3)
2542 if (i+3 == il.size() || ip[a[i]].disres.label != ip[a[i+3]].disres.label)
2544 ip[a[i]].disres.npair = npair;
2552 static void do_mtop(gmx::ISerializer *serializer,
2556 do_symtab(serializer, &(mtop->symtab));
2558 do_symstr(serializer, &(mtop->name), &(mtop->symtab));
2560 do_ffparams(serializer, &mtop->ffparams, file_version);
2562 int nmoltype = mtop->moltype.size();
2563 serializer->doInt(&nmoltype);
2564 if (serializer->reading())
2566 mtop->moltype.resize(nmoltype);
2568 for (gmx_moltype_t &moltype : mtop->moltype)
2570 do_moltype(serializer, &moltype, &mtop->symtab, file_version);
2573 int nmolblock = mtop->molblock.size();
2574 serializer->doInt(&nmolblock);
2575 if (serializer->reading())
2577 mtop->molblock.resize(nmolblock);
2579 for (gmx_molblock_t &molblock : mtop->molblock)
2581 int numAtomsPerMolecule = (serializer->reading() ? 0 : mtop->moltype[molblock.type].atoms.nr);
2582 do_molblock(serializer, &molblock, numAtomsPerMolecule);
2584 serializer->doInt(&mtop->natoms);
2586 if (file_version >= tpxv_IntermolecularBondeds)
2588 serializer->doBool(&mtop->bIntermolecularInteractions);
2589 if (mtop->bIntermolecularInteractions)
2591 if (serializer->reading())
2593 mtop->intermolecular_ilist = std::make_unique<InteractionLists>();
2595 do_ilists(serializer, mtop->intermolecular_ilist.get(), file_version);
2600 mtop->bIntermolecularInteractions = FALSE;
2603 do_atomtypes (serializer, &(mtop->atomtypes), file_version);
2605 if (file_version >= 65)
2607 do_cmap(serializer, &mtop->ffparams.cmap_grid);
2611 mtop->ffparams.cmap_grid.grid_spacing = 0;
2612 mtop->ffparams.cmap_grid.cmapdata.clear();
2615 do_groups(serializer, &mtop->groups, &(mtop->symtab));
2617 mtop->haveMoleculeIndices = true;
2619 if (serializer->reading())
2621 close_symtab(&(mtop->symtab));
2626 * Read the first part of the TPR file to find general system information.
2628 * If \p TopOnlyOK is true then we can read even future versions
2629 * of tpx files, provided the \p fileGeneration hasn't changed.
2630 * If it is false, we need the \p ir too, and bail out
2631 * if the file is newer than the program.
2633 * The version and generation of the topology (see top of this file)
2634 * are returned in the two last arguments, if those arguments are non-nullptr.
2636 * If possible, we will read the \p ir even when \p TopOnlyOK is true.
2638 * \param[in,out] serializer The serializer used to handle header processing.
2639 * \param[in,out] tpx File header datastructure.
2640 * \param[in] filename The name of the file being read/written
2641 * \param[in,out] fio File handle.
2642 * \param[in] TopOnlyOK If not reading \p ir is fine or not.
2644 static void do_tpxheader(gmx::FileIOXdrSerializer *serializer,
2646 const char *filename,
2655 /* XDR binary topology file */
2656 precision = sizeof(real);
2658 std::string fileTag;
2659 if (serializer->reading())
2661 serializer->doString(&buf);
2662 if (std::strncmp(buf.c_str(), "VERSION", 7) != 0)
2664 gmx_fatal(FARGS, "Can not read file %s,\n"
2665 " this file is from a GROMACS version which is older than 2.0\n"
2666 " Make a new one with grompp or use a gro or pdb file, if possible",
2669 serializer->doInt(&precision);
2670 bDouble = (precision == sizeof(double));
2671 if ((precision != sizeof(float)) && !bDouble)
2673 gmx_fatal(FARGS, "Unknown precision in file %s: real is %d bytes "
2674 "instead of %zu or %zu",
2675 filename, precision, sizeof(float), sizeof(double));
2677 gmx_fio_setprecision(fio, bDouble);
2678 fprintf(stderr, "Reading file %s, %s (%s precision)\n",
2679 filename, buf.c_str(), bDouble ? "double" : "single");
2683 buf = gmx::formatString("VERSION %s", gmx_version());
2684 serializer->doString(&buf);
2685 bDouble = (precision == sizeof(double));
2686 gmx_fio_setprecision(fio, bDouble);
2687 serializer->doInt(&precision);
2688 fileTag = gmx::formatString("%s", tpx_tag);
2691 /* Check versions! */
2692 serializer->doInt(&tpx->fileVersion);
2694 /* This is for backward compatibility with development versions 77-79
2695 * where the tag was, mistakenly, placed before the generation,
2696 * which would cause a segv instead of a proper error message
2697 * when reading the topology only from tpx with <77 code.
2699 if (tpx->fileVersion >= 77 && tpx->fileVersion <= 79)
2701 serializer->doString(&fileTag);
2704 serializer->doInt(&tpx->fileGeneration);
2706 if (tpx->fileVersion >= 81)
2708 serializer->doString(&fileTag);
2710 if (serializer->reading())
2712 if (tpx->fileVersion < 77)
2714 /* Versions before 77 don't have the tag, set it to release */
2715 fileTag = gmx::formatString("%s", TPX_TAG_RELEASE);
2718 if (fileTag != tpx_tag)
2720 fprintf(stderr, "Note: file tpx tag '%s', software tpx tag '%s'\n",
2721 fileTag.c_str(), tpx_tag);
2723 /* We only support reading tpx files with the same tag as the code
2724 * or tpx files with the release tag and with lower version number.
2726 if (fileTag != TPX_TAG_RELEASE && tpx->fileVersion < tpx_version)
2728 gmx_fatal(FARGS, "tpx tag/version mismatch: reading tpx file (%s) version %d, tag '%s' with program for tpx version %d, tag '%s'",
2729 filename, tpx->fileVersion, fileTag.c_str(),
2730 tpx_version, tpx_tag);
2735 if ((tpx->fileVersion <= tpx_incompatible_version) ||
2736 ((tpx->fileVersion > tpx_version) && !TopOnlyOK) ||
2737 (tpx->fileGeneration > tpx_generation) ||
2738 tpx_version == 80) /*80 was used by both 5.0-dev and 4.6-dev*/
2740 gmx_fatal(FARGS, "reading tpx file (%s) version %d with version %d program",
2741 filename, tpx->fileVersion, tpx_version);
2744 serializer->doInt(&tpx->natoms);
2745 serializer->doInt(&tpx->ngtc);
2747 if (tpx->fileVersion < 62)
2749 serializer->doInt(&idum);
2750 serializer->doReal(&rdum);
2752 if (tpx->fileVersion >= 79)
2754 serializer->doInt(&tpx->fep_state);
2756 serializer->doReal(&tpx->lambda);
2757 serializer->doBool(&tpx->bIr);
2758 serializer->doBool(&tpx->bTop);
2759 serializer->doBool(&tpx->bX);
2760 serializer->doBool(&tpx->bV);
2761 serializer->doBool(&tpx->bF);
2762 serializer->doBool(&tpx->bBox);
2764 if ((tpx->fileGeneration > tpx_generation))
2766 /* This can only happen if TopOnlyOK=TRUE */
2771 static int do_tpx_body(gmx::ISerializer *serializer,
2780 gmx_bool bPeriodicMols;
2782 if (!serializer->reading())
2784 GMX_RELEASE_ASSERT(x == nullptr && v == nullptr, "Passing separate x and v pointers to do_tpx() is not supported when writing");
2788 GMX_RELEASE_ASSERT(!(x == nullptr && v != nullptr), "Passing x==NULL and v!=NULL is not supported");
2791 if (serializer->reading())
2794 init_gtc_state(state, tpx->ngtc, 0, 0);
2797 // v is also nullptr by the above assertion, so we may
2798 // need to make memory in state for storing the contents
2802 state->flags |= (1 << estX);
2806 state->flags |= (1 << estV);
2808 state_change_natoms(state, tpx->natoms);
2814 x = state->x.rvec_array();
2815 v = state->v.rvec_array();
2818 #define do_test(serializer, b, p) if ((serializer)->reading() && ((p) != nullptr) && !(b)) gmx_fatal(FARGS, "No %s in input file",#p)
2820 do_test(serializer, tpx->bBox, state->box);
2823 serializer->doRvecArray(state->box, DIM);
2824 if (tpx->fileVersion >= 51)
2826 serializer->doRvecArray(state->box_rel, DIM);
2830 /* We initialize box_rel after reading the inputrec */
2831 clear_mat(state->box_rel);
2833 serializer->doRvecArray(state->boxv, DIM);
2834 if (tpx->fileVersion < 56)
2837 serializer->doRvecArray(mdum, DIM);
2841 if (state->ngtc > 0)
2844 snew(dumv, state->ngtc);
2845 if (tpx->fileVersion < 69)
2847 serializer->doRealArray(dumv, state->ngtc);
2849 /* These used to be the Berendsen tcoupl_lambda's */
2850 serializer->doRealArray(dumv, state->ngtc);
2854 do_test(serializer, tpx->bTop, mtop);
2859 do_mtop(serializer, mtop, tpx->fileVersion);
2864 do_mtop(serializer, &dum_top, tpx->fileVersion);
2867 do_test(serializer, tpx->bX, x);
2870 if (serializer->reading())
2872 state->flags |= (1<<estX);
2874 serializer->doRvecArray(x, tpx->natoms);
2877 do_test(serializer, tpx->bV, v);
2880 if (serializer->reading())
2882 state->flags |= (1<<estV);
2886 std::vector<gmx::RVec> dummyVelocities(tpx->natoms);
2887 serializer->doRvecArray(as_rvec_array(dummyVelocities.data()), tpx->natoms);
2891 serializer->doRvecArray(v, tpx->natoms);
2895 // No need to run do_test when the last argument is NULL
2898 std::vector<gmx::RVec> dummyForces(state->natoms);
2899 serializer->doRvecArray(as_rvec_array(dummyForces.data()), tpx->natoms);
2902 /* Starting with tpx version 26, we have the inputrec
2903 * at the end of the file, so we can ignore it
2904 * if the file is never than the software (but still the
2905 * same generation - see comments at the top of this file.
2910 bPeriodicMols = FALSE;
2912 do_test(serializer, tpx->bIr, ir);
2915 if (tpx->fileVersion >= 53)
2917 /* Removed the pbc info from do_inputrec, since we always want it */
2918 if (!serializer->reading())
2921 bPeriodicMols = ir->bPeriodicMols;
2923 serializer->doInt(&ePBC);
2924 serializer->doBool(&bPeriodicMols);
2926 if (tpx->fileGeneration <= tpx_generation && ir)
2928 do_inputrec(serializer, ir, tpx->fileVersion);
2929 if (tpx->fileVersion < 51)
2931 set_box_rel(ir, state);
2933 if (tpx->fileVersion < 53)
2936 bPeriodicMols = ir->bPeriodicMols;
2939 if (serializer->reading() && ir && tpx->fileVersion >= 53)
2941 /* We need to do this after do_inputrec, since that initializes ir */
2943 ir->bPeriodicMols = bPeriodicMols;
2947 if (serializer->reading())
2951 if (state->ngtc == 0)
2953 /* Reading old version without tcoupl state data: set it */
2954 init_gtc_state(state, ir->opts.ngtc, 0, ir->opts.nhchainlength);
2956 if (tpx->bTop && mtop)
2958 if (tpx->fileVersion < 57)
2960 if (mtop->moltype[0].ilist[F_DISRES].size() > 0)
2962 ir->eDisre = edrSimple;
2966 ir->eDisre = edrNone;
2969 set_disres_npair(mtop);
2973 if (tpx->bTop && mtop)
2975 gmx_mtop_finalize(mtop);
2982 static t_fileio *open_tpx(const char *fn, const char *mode)
2984 return gmx_fio_open(fn, mode);
2987 static void close_tpx(t_fileio *fio)
2993 * Fill information into the header only from state before writing.
2995 * Populating the header needs to be independent from writing the information
2996 * to file to allow things like writing the raw byte stream.
2998 * \param[in] state The current simulation state. Can't write without it.
2999 * \param[in] ir Parameter and system information.
3000 * \param[in] mtop Global topology.
3001 * \returns Fully populated header.
3003 static TpxFileHeader populateTpxHeader(const t_state &state,
3004 const t_inputrec *ir,
3005 const gmx_mtop_t *mtop)
3007 TpxFileHeader header;
3008 header.natoms = state.natoms;
3009 header.ngtc = state.ngtc;
3010 header.fep_state = state.fep_state;
3011 header.lambda = state.lambda[efptFEP];
3012 header.bIr = ir != nullptr;
3013 header.bTop = mtop != nullptr;
3014 header.bX = (state.flags & (1 << estX)) != 0;
3015 header.bV = (state.flags & (1 << estV)) != 0;
3018 header.fileVersion = tpx_version;
3019 header.fileGeneration = tpx_generation;
3024 /************************************************************
3026 * The following routines are the exported ones
3028 ************************************************************/
3030 TpxFileHeader readTpxHeader(const char *fileName, bool canReadTopologyOnly)
3034 fio = open_tpx(fileName, "r");
3035 gmx::FileIOXdrSerializer serializer(fio);
3038 do_tpxheader(&serializer, &tpx, fileName, fio, canReadTopologyOnly);
3043 void write_tpx_state(const char *fn,
3044 const t_inputrec *ir, const t_state *state, const gmx_mtop_t *mtop)
3048 fio = open_tpx(fn, "w");
3050 TpxFileHeader tpx = populateTpxHeader(*state,
3054 gmx::FileIOXdrSerializer serializer(fio);
3055 do_tpxheader(&serializer,
3060 do_tpx_body(&serializer,
3062 const_cast<t_inputrec *>(ir),
3063 const_cast<t_state *>(state), nullptr, nullptr,
3064 const_cast<gmx_mtop_t *>(mtop));
3068 void read_tpx_state(const char *fn,
3069 t_inputrec *ir, t_state *state, gmx_mtop_t *mtop)
3074 fio = open_tpx(fn, "r");
3075 gmx::FileIOXdrSerializer serializer(fio);
3076 do_tpxheader(&serializer,
3081 do_tpx_body(&serializer,
3091 int read_tpx(const char *fn,
3092 t_inputrec *ir, matrix box, int *natoms,
3093 rvec *x, rvec *v, gmx_mtop_t *mtop)
3100 fio = open_tpx(fn, "r");
3101 gmx::FileIOXdrSerializer serializer(fio);
3102 do_tpxheader(&serializer,
3107 ePBC = do_tpx_body(&serializer,
3109 ir, &state, x, v, mtop);
3111 if (mtop != nullptr && natoms != nullptr)
3113 *natoms = mtop->natoms;
3117 copy_mat(state.box, box);
3123 int read_tpx_top(const char *fn,
3124 t_inputrec *ir, matrix box, int *natoms,
3125 rvec *x, rvec *v, t_topology *top)
3130 ePBC = read_tpx(fn, ir, box, natoms, x, v, &mtop);
3132 *top = gmx_mtop_t_to_t_topology(&mtop, true);
3137 gmx_bool fn2bTPX(const char *file)
3139 return (efTPR == fn2ftp(file));
3142 void pr_tpxheader(FILE *fp, int indent, const char *title, const TpxFileHeader *sh)
3144 if (available(fp, sh, indent, title))
3146 indent = pr_title(fp, indent, title);
3147 pr_indent(fp, indent);
3148 fprintf(fp, "bIr = %spresent\n", sh->bIr ? "" : "not ");
3149 pr_indent(fp, indent);
3150 fprintf(fp, "bBox = %spresent\n", sh->bBox ? "" : "not ");
3151 pr_indent(fp, indent);
3152 fprintf(fp, "bTop = %spresent\n", sh->bTop ? "" : "not ");
3153 pr_indent(fp, indent);
3154 fprintf(fp, "bX = %spresent\n", sh->bX ? "" : "not ");
3155 pr_indent(fp, indent);
3156 fprintf(fp, "bV = %spresent\n", sh->bV ? "" : "not ");
3157 pr_indent(fp, indent);
3158 fprintf(fp, "bF = %spresent\n", sh->bF ? "" : "not ");
3160 pr_indent(fp, indent);
3161 fprintf(fp, "natoms = %d\n", sh->natoms);
3162 pr_indent(fp, indent);
3163 fprintf(fp, "lambda = %e\n", sh->lambda);