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, 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! */
50 #include "gromacs/fileio/filetypes.h"
51 #include "gromacs/fileio/gmxfio.h"
52 #include "gromacs/fileio/gmxfio-xdr.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/mdtypes/inputrec.h"
55 #include "gromacs/mdtypes/md_enums.h"
56 #include "gromacs/mdtypes/pull-params.h"
57 #include "gromacs/mdtypes/state.h"
58 #include "gromacs/pbcutil/boxutilities.h"
59 #include "gromacs/pbcutil/pbc.h"
60 #include "gromacs/topology/block.h"
61 #include "gromacs/topology/ifunc.h"
62 #include "gromacs/topology/mtop_util.h"
63 #include "gromacs/topology/symtab.h"
64 #include "gromacs/topology/topology.h"
65 #include "gromacs/utility/arraysize.h"
66 #include "gromacs/utility/baseversion.h"
67 #include "gromacs/utility/cstringutil.h"
68 #include "gromacs/utility/fatalerror.h"
69 #include "gromacs/utility/futil.h"
70 #include "gromacs/utility/smalloc.h"
71 #include "gromacs/utility/snprintf.h"
72 #include "gromacs/utility/txtdump.h"
74 #define TPX_TAG_RELEASE "release"
76 /*! \brief Tag string for the file format written to run input files
77 * written by this version of the code.
79 * Change this if you want to change the run input format in a feature
80 * branch. This ensures that there will not be different run input
81 * formats around which cannot be distinguished, while not causing
82 * problems rebasing the feature branch onto upstream changes. When
83 * merging with mainstream GROMACS, set this tag string back to
84 * TPX_TAG_RELEASE, and instead add an element to tpxv.
86 static const char *tpx_tag = TPX_TAG_RELEASE;
88 /*! \brief Enum of values that describe the contents of a tpr file
89 * whose format matches a version number
91 * The enum helps the code be more self-documenting and ensure merges
92 * do not silently resolve when two patches make the same bump. When
93 * adding new functionality, add a new element just above tpxv_Count
94 * in this enumeration, and write code below that does the right thing
95 * according to the value of file_version.
98 tpxv_ComputationalElectrophysiology = 96, /**< support for ion/water position swaps (computational electrophysiology) */
99 tpxv_Use64BitRandomSeed, /**< change ld_seed from int to gmx_int64_t */
100 tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, /**< potentials for supporting coarse-grained force fields */
101 tpxv_InteractiveMolecularDynamics, /**< interactive molecular dynamics (IMD) */
102 tpxv_RemoveObsoleteParameters1, /**< remove optimize_fft, dihre_fc, nstcheckpoint */
103 tpxv_PullCoordTypeGeom, /**< add pull type and geometry per group and flat-bottom */
104 tpxv_PullGeomDirRel, /**< add pull geometry direction-relative */
105 tpxv_IntermolecularBondeds, /**< permit inter-molecular bonded interactions in the topology */
106 tpxv_CompElWithSwapLayerOffset, /**< added parameters for improved CompEl setups */
107 tpxv_CompElPolyatomicIonsAndMultipleIonTypes, /**< CompEl now can handle polyatomic ions and more than two types of ions */
108 tpxv_RemoveAdress, /**< removed support for AdResS */
109 tpxv_PullCoordNGroup, /**< add ngroup to pull coord */
110 tpxv_RemoveTwinRange, /**< removed support for twin-range interactions */
111 tpxv_Count /**< the total number of tpxv versions */
114 /*! \brief Version number of the file format written to run input
115 * files by this version of the code.
117 * The tpx_version increases whenever the file format in the main
118 * development branch changes, due to an extension of the tpxv enum above.
119 * Backward compatibility for reading old run input files is maintained
120 * by checking this version number against that of the file and then using
121 * the correct code path.
123 * When developing a feature branch that needs to change the run input
124 * file format, change tpx_tag instead. */
125 static const int tpx_version = tpxv_Count - 1;
128 /* This number should only be increased when you edit the TOPOLOGY section
129 * or the HEADER of the tpx format.
130 * This way we can maintain forward compatibility too for all analysis tools
131 * and/or external programs that only need to know the atom/residue names,
132 * charges, and bond connectivity.
134 * It first appeared in tpx version 26, when I also moved the inputrecord
135 * to the end of the tpx file, so we can just skip it if we only
138 * In particular, it must be increased when adding new elements to
139 * ftupd, so that old code can read new .tpr files.
141 static const int tpx_generation = 26;
143 /* This number should be the most recent backwards incompatible version
144 * I.e., if this number is 9, we cannot read tpx version 9 with this code.
146 static const int tpx_incompatible_version = 9;
150 /* Struct used to maintain tpx compatibility when function types are added */
152 int fvnr; /* file version number in which the function type first appeared */
153 int ftype; /* function type */
157 * The entries should be ordered in:
158 * 1. ascending function type number
159 * 2. ascending file version number
161 static const t_ftupd ftupd[] = {
162 { 20, F_CUBICBONDS },
167 { 43, F_TABBONDSNC },
168 { 70, F_RESTRBONDS },
169 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_RESTRANGLES },
170 { 76, F_LINEAR_ANGLES },
171 { 30, F_CROSS_BOND_BONDS },
172 { 30, F_CROSS_BOND_ANGLES },
173 { 30, F_UREY_BRADLEY },
174 { 34, F_QUARTIC_ANGLES },
176 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_RESTRDIHS },
177 { tpxv_RestrictedBendingAndCombinedAngleTorsionPotentials, F_CBTDIHS },
186 { 72, F_NPSOLVATION },
188 { 41, F_LJC_PAIRS_NB },
189 { 32, F_BHAM_LR_NOLONGERUSED },
191 { 32, F_COUL_RECIP },
194 { 30, F_POLARIZATION },
197 { 22, F_DISRESVIOL },
201 { 26, F_DIHRESVIOL },
206 { 46, F_ECONSERVED },
207 { 69, F_VTEMP_NOLONGERUSED},
209 { 54, F_DVDL_CONSTR },
210 { 76, F_ANHARM_POL },
213 { 79, F_DVDL_BONDED, },
214 { 79, F_DVDL_RESTRAINT },
215 { 79, F_DVDL_TEMPERATURE },
217 #define NFTUPD asize(ftupd)
219 /* Needed for backward compatibility */
222 /**************************************************************
224 * Now the higer level routines that do io of the structures and arrays
226 **************************************************************/
227 static void do_pullgrp_tpx_pre95(t_fileio *fio,
235 gmx_fio_do_int(fio, pgrp->nat);
238 snew(pgrp->ind, pgrp->nat);
240 gmx_fio_ndo_int(fio, pgrp->ind, pgrp->nat);
241 gmx_fio_do_int(fio, pgrp->nweight);
244 snew(pgrp->weight, pgrp->nweight);
246 gmx_fio_ndo_real(fio, pgrp->weight, pgrp->nweight);
247 gmx_fio_do_int(fio, pgrp->pbcatom);
248 gmx_fio_do_rvec(fio, pcrd->vec);
249 clear_rvec(pcrd->origin);
250 gmx_fio_do_rvec(fio, tmp);
252 gmx_fio_do_real(fio, pcrd->rate);
253 gmx_fio_do_real(fio, pcrd->k);
254 if (file_version >= 56)
256 gmx_fio_do_real(fio, pcrd->kB);
264 static void do_pull_group(t_fileio *fio, t_pull_group *pgrp, gmx_bool bRead)
266 gmx_fio_do_int(fio, pgrp->nat);
269 snew(pgrp->ind, pgrp->nat);
271 gmx_fio_ndo_int(fio, pgrp->ind, pgrp->nat);
272 gmx_fio_do_int(fio, pgrp->nweight);
275 snew(pgrp->weight, pgrp->nweight);
277 gmx_fio_ndo_real(fio, pgrp->weight, pgrp->nweight);
278 gmx_fio_do_int(fio, pgrp->pbcatom);
281 static void do_pull_coord(t_fileio *fio, t_pull_coord *pcrd, int file_version,
282 int ePullOld, int eGeomOld, ivec dimOld)
284 if (file_version >= tpxv_PullCoordNGroup)
286 gmx_fio_do_int(fio, pcrd->eType);
287 /* Note that we try to support adding new geometries without
288 * changing the tpx version. This requires checks when printing the
289 * geometry string and a check and fatal_error in init_pull.
291 gmx_fio_do_int(fio, pcrd->eGeom);
292 gmx_fio_do_int(fio, pcrd->ngroup);
293 if (pcrd->ngroup <= PULL_COORD_NGROUP_MAX)
295 gmx_fio_ndo_int(fio, pcrd->group, pcrd->ngroup);
299 /* More groups in file than supported, this must be a new geometry
300 * that is not supported by our current code. Since we will not
301 * use the groups for this coord (checks in the pull and WHAM code
302 * ensure this), we can ignore the groups and set ngroup=0.
305 snew(dum, pcrd->ngroup);
306 gmx_fio_ndo_int(fio, dum, pcrd->ngroup);
311 gmx_fio_do_ivec(fio, pcrd->dim);
316 gmx_fio_do_int(fio, pcrd->group[0]);
317 gmx_fio_do_int(fio, pcrd->group[1]);
318 if (file_version >= tpxv_PullCoordTypeGeom)
320 pcrd->ngroup = (pcrd->eGeom == epullgDIRRELATIVE ? 4 : 2);
321 gmx_fio_do_int(fio, pcrd->eType);
322 gmx_fio_do_int(fio, pcrd->eGeom);
323 if (pcrd->ngroup == 4)
325 gmx_fio_do_int(fio, pcrd->group[2]);
326 gmx_fio_do_int(fio, pcrd->group[3]);
328 gmx_fio_do_ivec(fio, pcrd->dim);
332 pcrd->eType = ePullOld;
333 pcrd->eGeom = eGeomOld;
334 copy_ivec(dimOld, pcrd->dim);
337 gmx_fio_do_rvec(fio, pcrd->origin);
338 gmx_fio_do_rvec(fio, pcrd->vec);
339 if (file_version >= tpxv_PullCoordTypeGeom)
341 gmx_fio_do_gmx_bool(fio, pcrd->bStart);
345 /* This parameter is only printed, but not actually used by mdrun */
346 pcrd->bStart = FALSE;
348 gmx_fio_do_real(fio, pcrd->init);
349 gmx_fio_do_real(fio, pcrd->rate);
350 gmx_fio_do_real(fio, pcrd->k);
351 gmx_fio_do_real(fio, pcrd->kB);
354 static void do_expandedvals(t_fileio *fio, t_expanded *expand, t_lambda *fepvals, gmx_bool bRead, int file_version)
356 int n_lambda = fepvals->n_lambda;
358 /* reset the lambda calculation window */
359 fepvals->lambda_start_n = 0;
360 fepvals->lambda_stop_n = n_lambda;
361 if (file_version >= 79)
367 snew(expand->init_lambda_weights, n_lambda);
369 gmx_fio_ndo_real(fio, expand->init_lambda_weights, n_lambda);
370 gmx_fio_do_gmx_bool(fio, expand->bInit_weights);
373 gmx_fio_do_int(fio, expand->nstexpanded);
374 gmx_fio_do_int(fio, expand->elmcmove);
375 gmx_fio_do_int(fio, expand->elamstats);
376 gmx_fio_do_int(fio, expand->lmc_repeats);
377 gmx_fio_do_int(fio, expand->gibbsdeltalam);
378 gmx_fio_do_int(fio, expand->lmc_forced_nstart);
379 gmx_fio_do_int(fio, expand->lmc_seed);
380 gmx_fio_do_real(fio, expand->mc_temp);
381 gmx_fio_do_int(fio, expand->bSymmetrizedTMatrix);
382 gmx_fio_do_int(fio, expand->nstTij);
383 gmx_fio_do_int(fio, expand->minvarmin);
384 gmx_fio_do_int(fio, expand->c_range);
385 gmx_fio_do_real(fio, expand->wl_scale);
386 gmx_fio_do_real(fio, expand->wl_ratio);
387 gmx_fio_do_real(fio, expand->init_wl_delta);
388 gmx_fio_do_gmx_bool(fio, expand->bWLoneovert);
389 gmx_fio_do_int(fio, expand->elmceq);
390 gmx_fio_do_int(fio, expand->equil_steps);
391 gmx_fio_do_int(fio, expand->equil_samples);
392 gmx_fio_do_int(fio, expand->equil_n_at_lam);
393 gmx_fio_do_real(fio, expand->equil_wl_delta);
394 gmx_fio_do_real(fio, expand->equil_ratio);
398 static void do_simtempvals(t_fileio *fio, t_simtemp *simtemp, int n_lambda, gmx_bool bRead,
401 if (file_version >= 79)
403 gmx_fio_do_int(fio, simtemp->eSimTempScale);
404 gmx_fio_do_real(fio, simtemp->simtemp_high);
405 gmx_fio_do_real(fio, simtemp->simtemp_low);
410 snew(simtemp->temperatures, n_lambda);
412 gmx_fio_ndo_real(fio, simtemp->temperatures, n_lambda);
417 static void do_imd(t_fileio *fio, t_IMD *imd, gmx_bool bRead)
419 gmx_fio_do_int(fio, imd->nat);
422 snew(imd->ind, imd->nat);
424 gmx_fio_ndo_int(fio, imd->ind, imd->nat);
427 static void do_fepvals(t_fileio *fio, t_lambda *fepvals, gmx_bool bRead, int file_version)
429 /* i is defined in the ndo_double macro; use g to iterate. */
433 /* free energy values */
435 if (file_version >= 79)
437 gmx_fio_do_int(fio, fepvals->init_fep_state);
438 gmx_fio_do_double(fio, fepvals->init_lambda);
439 gmx_fio_do_double(fio, fepvals->delta_lambda);
441 else if (file_version >= 59)
443 gmx_fio_do_double(fio, fepvals->init_lambda);
444 gmx_fio_do_double(fio, fepvals->delta_lambda);
448 gmx_fio_do_real(fio, rdum);
449 fepvals->init_lambda = rdum;
450 gmx_fio_do_real(fio, rdum);
451 fepvals->delta_lambda = rdum;
453 if (file_version >= 79)
455 gmx_fio_do_int(fio, fepvals->n_lambda);
458 snew(fepvals->all_lambda, efptNR);
460 for (g = 0; g < efptNR; g++)
462 if (fepvals->n_lambda > 0)
466 snew(fepvals->all_lambda[g], fepvals->n_lambda);
468 gmx_fio_ndo_double(fio, fepvals->all_lambda[g], fepvals->n_lambda);
469 gmx_fio_ndo_int(fio, fepvals->separate_dvdl, efptNR);
471 else if (fepvals->init_lambda >= 0)
473 fepvals->separate_dvdl[efptFEP] = TRUE;
477 else if (file_version >= 64)
479 gmx_fio_do_int(fio, fepvals->n_lambda);
484 snew(fepvals->all_lambda, efptNR);
485 /* still allocate the all_lambda array's contents. */
486 for (g = 0; g < efptNR; g++)
488 if (fepvals->n_lambda > 0)
490 snew(fepvals->all_lambda[g], fepvals->n_lambda);
494 gmx_fio_ndo_double(fio, fepvals->all_lambda[efptFEP],
496 if (fepvals->init_lambda >= 0)
500 fepvals->separate_dvdl[efptFEP] = TRUE;
504 /* copy the contents of the efptFEP lambda component to all
505 the other components */
506 for (g = 0; g < efptNR; g++)
508 for (h = 0; h < fepvals->n_lambda; h++)
512 fepvals->all_lambda[g][h] =
513 fepvals->all_lambda[efptFEP][h];
522 fepvals->n_lambda = 0;
523 fepvals->all_lambda = NULL;
524 if (fepvals->init_lambda >= 0)
526 fepvals->separate_dvdl[efptFEP] = TRUE;
529 if (file_version >= 13)
531 gmx_fio_do_real(fio, fepvals->sc_alpha);
535 fepvals->sc_alpha = 0;
537 if (file_version >= 38)
539 gmx_fio_do_int(fio, fepvals->sc_power);
543 fepvals->sc_power = 2;
545 if (file_version >= 79)
547 gmx_fio_do_real(fio, fepvals->sc_r_power);
551 fepvals->sc_r_power = 6.0;
553 if (file_version >= 15)
555 gmx_fio_do_real(fio, fepvals->sc_sigma);
559 fepvals->sc_sigma = 0.3;
563 if (file_version >= 71)
565 fepvals->sc_sigma_min = fepvals->sc_sigma;
569 fepvals->sc_sigma_min = 0;
572 if (file_version >= 79)
574 gmx_fio_do_int(fio, fepvals->bScCoul);
578 fepvals->bScCoul = TRUE;
580 if (file_version >= 64)
582 gmx_fio_do_int(fio, fepvals->nstdhdl);
586 fepvals->nstdhdl = 1;
589 if (file_version >= 73)
591 gmx_fio_do_int(fio, fepvals->separate_dhdl_file);
592 gmx_fio_do_int(fio, fepvals->dhdl_derivatives);
596 fepvals->separate_dhdl_file = esepdhdlfileYES;
597 fepvals->dhdl_derivatives = edhdlderivativesYES;
599 if (file_version >= 71)
601 gmx_fio_do_int(fio, fepvals->dh_hist_size);
602 gmx_fio_do_double(fio, fepvals->dh_hist_spacing);
606 fepvals->dh_hist_size = 0;
607 fepvals->dh_hist_spacing = 0.1;
609 if (file_version >= 79)
611 gmx_fio_do_int(fio, fepvals->edHdLPrintEnergy);
615 fepvals->edHdLPrintEnergy = edHdLPrintEnergyNO;
618 /* handle lambda_neighbors */
619 if ((file_version >= 83 && file_version < 90) || file_version >= 92)
621 gmx_fio_do_int(fio, fepvals->lambda_neighbors);
622 if ( (fepvals->lambda_neighbors >= 0) && (fepvals->init_fep_state >= 0) &&
623 (fepvals->init_lambda < 0) )
625 fepvals->lambda_start_n = (fepvals->init_fep_state -
626 fepvals->lambda_neighbors);
627 fepvals->lambda_stop_n = (fepvals->init_fep_state +
628 fepvals->lambda_neighbors + 1);
629 if (fepvals->lambda_start_n < 0)
631 fepvals->lambda_start_n = 0;;
633 if (fepvals->lambda_stop_n >= fepvals->n_lambda)
635 fepvals->lambda_stop_n = fepvals->n_lambda;
640 fepvals->lambda_start_n = 0;
641 fepvals->lambda_stop_n = fepvals->n_lambda;
646 fepvals->lambda_start_n = 0;
647 fepvals->lambda_stop_n = fepvals->n_lambda;
651 static void do_pull(t_fileio *fio, pull_params_t *pull, gmx_bool bRead,
652 int file_version, int ePullOld)
658 if (file_version >= 95)
660 gmx_fio_do_int(fio, pull->ngroup);
662 gmx_fio_do_int(fio, pull->ncoord);
663 if (file_version < 95)
665 pull->ngroup = pull->ncoord + 1;
667 if (file_version < tpxv_PullCoordTypeGeom)
671 gmx_fio_do_int(fio, eGeomOld);
672 gmx_fio_do_ivec(fio, dimOld);
673 /* The inner cylinder radius, now removed */
674 gmx_fio_do_real(fio, dum);
676 gmx_fio_do_real(fio, pull->cylinder_r);
677 gmx_fio_do_real(fio, pull->constr_tol);
678 if (file_version >= 95)
680 gmx_fio_do_int(fio, pull->bPrintCOM1);
681 /* With file_version < 95 this value is set below */
683 if (file_version >= tpxv_PullCoordTypeGeom)
685 gmx_fio_do_int(fio, pull->bPrintCOM2);
686 gmx_fio_do_int(fio, pull->bPrintRefValue);
687 gmx_fio_do_int(fio, pull->bPrintComp);
691 pull->bPrintCOM2 = FALSE;
692 pull->bPrintRefValue = FALSE;
693 pull->bPrintComp = TRUE;
695 gmx_fio_do_int(fio, pull->nstxout);
696 gmx_fio_do_int(fio, pull->nstfout);
699 snew(pull->group, pull->ngroup);
700 snew(pull->coord, pull->ncoord);
702 if (file_version < 95)
704 /* epullgPOS for position pulling, before epullgDIRPBC was removed */
705 if (eGeomOld == epullgDIRPBC)
707 gmx_fatal(FARGS, "pull-geometry=position is no longer supported");
709 if (eGeomOld > epullgDIRPBC)
714 for (g = 0; g < pull->ngroup; g++)
716 /* We read and ignore a pull coordinate for group 0 */
717 do_pullgrp_tpx_pre95(fio, &pull->group[g], &pull->coord[std::max(g-1, 0)],
718 bRead, file_version);
721 pull->coord[g-1].group[0] = 0;
722 pull->coord[g-1].group[1] = g;
726 pull->bPrintCOM1 = (pull->group[0].nat > 0);
730 for (g = 0; g < pull->ngroup; g++)
732 do_pull_group(fio, &pull->group[g], bRead);
734 for (g = 0; g < pull->ncoord; g++)
736 do_pull_coord(fio, &pull->coord[g],
737 file_version, ePullOld, eGeomOld, dimOld);
743 static void do_rotgrp(t_fileio *fio, t_rotgrp *rotg, gmx_bool bRead)
745 gmx_fio_do_int(fio, rotg->eType);
746 gmx_fio_do_int(fio, rotg->bMassW);
747 gmx_fio_do_int(fio, rotg->nat);
750 snew(rotg->ind, rotg->nat);
752 gmx_fio_ndo_int(fio, rotg->ind, rotg->nat);
755 snew(rotg->x_ref, rotg->nat);
757 gmx_fio_ndo_rvec(fio, rotg->x_ref, rotg->nat);
758 gmx_fio_do_rvec(fio, rotg->vec);
759 gmx_fio_do_rvec(fio, rotg->pivot);
760 gmx_fio_do_real(fio, rotg->rate);
761 gmx_fio_do_real(fio, rotg->k);
762 gmx_fio_do_real(fio, rotg->slab_dist);
763 gmx_fio_do_real(fio, rotg->min_gaussian);
764 gmx_fio_do_real(fio, rotg->eps);
765 gmx_fio_do_int(fio, rotg->eFittype);
766 gmx_fio_do_int(fio, rotg->PotAngle_nstep);
767 gmx_fio_do_real(fio, rotg->PotAngle_step);
770 static void do_rot(t_fileio *fio, t_rot *rot, gmx_bool bRead)
774 gmx_fio_do_int(fio, rot->ngrp);
775 gmx_fio_do_int(fio, rot->nstrout);
776 gmx_fio_do_int(fio, rot->nstsout);
779 snew(rot->grp, rot->ngrp);
781 for (g = 0; g < rot->ngrp; g++)
783 do_rotgrp(fio, &rot->grp[g], bRead);
788 static void do_swapgroup(t_fileio *fio, t_swapGroup *g, gmx_bool bRead)
791 /* Name of the group or molecule */
796 gmx_fio_do_string(fio, buf);
797 g->molname = gmx_strdup(buf);
801 gmx_fio_do_string(fio, g->molname);
804 /* Number of atoms in the group */
805 gmx_fio_do_int(fio, g->nat);
807 /* The group's atom indices */
810 snew(g->ind, g->nat);
812 gmx_fio_ndo_int(fio, g->ind, g->nat);
814 /* Requested counts for compartments A and B */
815 gmx_fio_ndo_int(fio, g->nmolReq, eCompNR);
818 static void do_swapcoords_tpx(t_fileio *fio, t_swapcoords *swap, gmx_bool bRead, int file_version)
820 /* Enums for better readability of the code */
825 eChannel0 = 0, eChannel1
829 if (file_version >= tpxv_CompElPolyatomicIonsAndMultipleIonTypes)
831 /* The total number of swap groups is the sum of the fixed groups
832 * (split0, split1, solvent), and the user-defined groups (2+ types of ions)
834 gmx_fio_do_int(fio, swap->ngrp);
837 snew(swap->grp, swap->ngrp);
839 for (int ig = 0; ig < swap->ngrp; ig++)
841 do_swapgroup(fio, &swap->grp[ig], bRead);
843 gmx_fio_do_int(fio, swap->massw_split[eChannel0]);
844 gmx_fio_do_int(fio, swap->massw_split[eChannel1]);
845 gmx_fio_do_int(fio, swap->nstswap);
846 gmx_fio_do_int(fio, swap->nAverage);
847 gmx_fio_do_real(fio, swap->threshold);
848 gmx_fio_do_real(fio, swap->cyl0r);
849 gmx_fio_do_real(fio, swap->cyl0u);
850 gmx_fio_do_real(fio, swap->cyl0l);
851 gmx_fio_do_real(fio, swap->cyl1r);
852 gmx_fio_do_real(fio, swap->cyl1u);
853 gmx_fio_do_real(fio, swap->cyl1l);
857 /*** Support reading older CompEl .tpr files ***/
859 /* In the original CompEl .tpr files, we always have 5 groups: */
861 snew(swap->grp, swap->ngrp);
863 swap->grp[eGrpSplit0 ].molname = gmx_strdup("split0" ); // group 0: split0
864 swap->grp[eGrpSplit1 ].molname = gmx_strdup("split1" ); // group 1: split1
865 swap->grp[eGrpSolvent].molname = gmx_strdup("solvent"); // group 2: solvent
866 swap->grp[3 ].molname = gmx_strdup("anions" ); // group 3: anions
867 swap->grp[4 ].molname = gmx_strdup("cations"); // group 4: cations
869 gmx_fio_do_int(fio, swap->grp[3].nat);
870 gmx_fio_do_int(fio, swap->grp[eGrpSolvent].nat);
871 gmx_fio_do_int(fio, swap->grp[eGrpSplit0].nat);
872 gmx_fio_do_int(fio, swap->massw_split[eChannel0]);
873 gmx_fio_do_int(fio, swap->grp[eGrpSplit1].nat);
874 gmx_fio_do_int(fio, swap->massw_split[eChannel1]);
875 gmx_fio_do_int(fio, swap->nstswap);
876 gmx_fio_do_int(fio, swap->nAverage);
877 gmx_fio_do_real(fio, swap->threshold);
878 gmx_fio_do_real(fio, swap->cyl0r);
879 gmx_fio_do_real(fio, swap->cyl0u);
880 gmx_fio_do_real(fio, swap->cyl0l);
881 gmx_fio_do_real(fio, swap->cyl1r);
882 gmx_fio_do_real(fio, swap->cyl1u);
883 gmx_fio_do_real(fio, swap->cyl1l);
885 // The order[] array keeps compatibility with older .tpr files
886 // by reading in the groups in the classic order
888 const int order[4] = {3, eGrpSolvent, eGrpSplit0, eGrpSplit1 };
890 for (int ig = 0; ig < 4; ig++)
893 snew(swap->grp[g].ind, swap->grp[g].nat);
894 gmx_fio_ndo_int(fio, swap->grp[g].ind, swap->grp[g].nat);
898 for (int j = eCompA; j <= eCompB; j++)
900 gmx_fio_do_int(fio, swap->grp[3].nmolReq[j]); // group 3 = anions
901 gmx_fio_do_int(fio, swap->grp[4].nmolReq[j]); // group 4 = cations
903 } /* End support reading older CompEl .tpr files */
905 if (file_version >= tpxv_CompElWithSwapLayerOffset)
907 gmx_fio_do_real(fio, swap->bulkOffset[eCompA]);
908 gmx_fio_do_real(fio, swap->bulkOffset[eCompB]);
914 static void do_inputrec(t_fileio *fio, t_inputrec *ir, gmx_bool bRead,
915 int file_version, real *fudgeQQ)
917 int i, j, k, *tmp, idum = 0;
920 gmx_bool bSimAnn, bdum = 0;
921 real zerotemptime, finish_t, init_temp, finish_temp;
923 if (file_version != tpx_version)
925 /* Give a warning about features that are not accessible */
926 fprintf(stderr, "Note: file tpx version %d, software tpx version %d\n",
927 file_version, tpx_version);
935 if (file_version == 0)
940 /* Basic inputrec stuff */
941 gmx_fio_do_int(fio, ir->eI);
942 if (file_version >= 62)
944 gmx_fio_do_int64(fio, ir->nsteps);
948 gmx_fio_do_int(fio, idum);
952 if (file_version > 25)
954 if (file_version >= 62)
956 gmx_fio_do_int64(fio, ir->init_step);
960 gmx_fio_do_int(fio, idum);
961 ir->init_step = idum;
969 if (file_version >= 58)
971 gmx_fio_do_int(fio, ir->simulation_part);
975 ir->simulation_part = 1;
978 if (file_version >= 67)
980 gmx_fio_do_int(fio, ir->nstcalcenergy);
984 ir->nstcalcenergy = 1;
986 if (file_version < 53)
988 /* The pbc info has been moved out of do_inputrec,
989 * since we always want it, also without reading the inputrec.
991 gmx_fio_do_int(fio, ir->ePBC);
992 if ((file_version <= 15) && (ir->ePBC == 2))
996 if (file_version >= 45)
998 gmx_fio_do_int(fio, ir->bPeriodicMols);
1005 ir->bPeriodicMols = TRUE;
1009 ir->bPeriodicMols = FALSE;
1013 if (file_version >= 81)
1015 gmx_fio_do_int(fio, ir->cutoff_scheme);
1016 if (file_version < 94)
1018 ir->cutoff_scheme = 1 - ir->cutoff_scheme;
1023 ir->cutoff_scheme = ecutsGROUP;
1025 gmx_fio_do_int(fio, ir->ns_type);
1026 gmx_fio_do_int(fio, ir->nstlist);
1027 gmx_fio_do_int(fio, idum); /* used to be ndelta; not used anymore */
1028 if (file_version < 41)
1030 gmx_fio_do_int(fio, idum);
1031 gmx_fio_do_int(fio, idum);
1033 if (file_version >= 45)
1035 gmx_fio_do_real(fio, ir->rtpi);
1041 gmx_fio_do_int(fio, ir->nstcomm);
1042 if (file_version > 34)
1044 gmx_fio_do_int(fio, ir->comm_mode);
1046 else if (ir->nstcomm < 0)
1048 ir->comm_mode = ecmANGULAR;
1052 ir->comm_mode = ecmLINEAR;
1054 ir->nstcomm = abs(ir->nstcomm);
1056 /* ignore nstcheckpoint */
1057 if (file_version > 25 && file_version < tpxv_RemoveObsoleteParameters1)
1059 gmx_fio_do_int(fio, idum);
1062 gmx_fio_do_int(fio, ir->nstcgsteep);
1064 if (file_version >= 30)
1066 gmx_fio_do_int(fio, ir->nbfgscorr);
1073 gmx_fio_do_int(fio, ir->nstlog);
1074 gmx_fio_do_int(fio, ir->nstxout);
1075 gmx_fio_do_int(fio, ir->nstvout);
1076 gmx_fio_do_int(fio, ir->nstfout);
1077 gmx_fio_do_int(fio, ir->nstenergy);
1078 gmx_fio_do_int(fio, ir->nstxout_compressed);
1079 if (file_version >= 59)
1081 gmx_fio_do_double(fio, ir->init_t);
1082 gmx_fio_do_double(fio, ir->delta_t);
1086 gmx_fio_do_real(fio, rdum);
1088 gmx_fio_do_real(fio, rdum);
1091 gmx_fio_do_real(fio, ir->x_compression_precision);
1092 if (file_version < 19)
1094 gmx_fio_do_int(fio, idum);
1095 gmx_fio_do_int(fio, idum);
1097 if (file_version < 18)
1099 gmx_fio_do_int(fio, idum);
1101 if (file_version >= 81)
1103 gmx_fio_do_real(fio, ir->verletbuf_tol);
1107 ir->verletbuf_tol = 0;
1109 gmx_fio_do_real(fio, ir->rlist);
1110 if (file_version >= 67 && file_version < tpxv_RemoveTwinRange)
1114 // Reading such a file version could invoke the twin-range
1115 // scheme, about which mdrun should give a fatal error.
1116 real dummy_rlistlong = -1;
1117 gmx_fio_do_real(fio, dummy_rlistlong);
1119 if (ir->rlist > 0 && (dummy_rlistlong == 0 || dummy_rlistlong > ir->rlist))
1121 // Get mdrun to issue an error (regardless of
1122 // ir->cutoff_scheme).
1123 ir->useTwinRange = true;
1127 // grompp used to set rlistlong actively. Users were
1128 // probably also confused and set rlistlong == rlist.
1129 // However, in all remaining cases, it is safe to let
1130 // mdrun proceed normally.
1131 ir->useTwinRange = false;
1137 // No need to read or write anything
1138 ir->useTwinRange = false;
1140 if (file_version >= 82 && file_version != 90)
1142 // Multiple time-stepping is no longer enabled, but the old
1143 // support required the twin-range scheme, for which mdrun
1144 // already emits a fatal error.
1145 int dummy_nstcalclr = -1;
1146 gmx_fio_do_int(fio, dummy_nstcalclr);
1148 gmx_fio_do_int(fio, ir->coulombtype);
1149 if (file_version < 32 && ir->coulombtype == eelRF)
1151 ir->coulombtype = eelRF_NEC_UNSUPPORTED;
1153 if (file_version >= 81)
1155 gmx_fio_do_int(fio, ir->coulomb_modifier);
1159 ir->coulomb_modifier = (ir->cutoff_scheme == ecutsVERLET ? eintmodPOTSHIFT : eintmodNONE);
1161 gmx_fio_do_real(fio, ir->rcoulomb_switch);
1162 gmx_fio_do_real(fio, ir->rcoulomb);
1163 gmx_fio_do_int(fio, ir->vdwtype);
1164 if (file_version >= 81)
1166 gmx_fio_do_int(fio, ir->vdw_modifier);
1170 ir->vdw_modifier = (ir->cutoff_scheme == ecutsVERLET ? eintmodPOTSHIFT : eintmodNONE);
1172 gmx_fio_do_real(fio, ir->rvdw_switch);
1173 gmx_fio_do_real(fio, ir->rvdw);
1174 gmx_fio_do_int(fio, ir->eDispCorr);
1175 gmx_fio_do_real(fio, ir->epsilon_r);
1176 if (file_version >= 37)
1178 gmx_fio_do_real(fio, ir->epsilon_rf);
1182 if (EEL_RF(ir->coulombtype))
1184 ir->epsilon_rf = ir->epsilon_r;
1185 ir->epsilon_r = 1.0;
1189 ir->epsilon_rf = 1.0;
1192 if (file_version >= 29)
1194 gmx_fio_do_real(fio, ir->tabext);
1201 if (file_version > 25)
1203 gmx_fio_do_int(fio, ir->gb_algorithm);
1204 gmx_fio_do_int(fio, ir->nstgbradii);
1205 gmx_fio_do_real(fio, ir->rgbradii);
1206 gmx_fio_do_real(fio, ir->gb_saltconc);
1207 gmx_fio_do_int(fio, ir->implicit_solvent);
1211 ir->gb_algorithm = egbSTILL;
1214 ir->gb_saltconc = 0;
1215 ir->implicit_solvent = eisNO;
1217 if (file_version >= 55)
1219 gmx_fio_do_real(fio, ir->gb_epsilon_solvent);
1220 gmx_fio_do_real(fio, ir->gb_obc_alpha);
1221 gmx_fio_do_real(fio, ir->gb_obc_beta);
1222 gmx_fio_do_real(fio, ir->gb_obc_gamma);
1223 if (file_version >= 60)
1225 gmx_fio_do_real(fio, ir->gb_dielectric_offset);
1226 gmx_fio_do_int(fio, ir->sa_algorithm);
1230 ir->gb_dielectric_offset = 0.009;
1231 ir->sa_algorithm = esaAPPROX;
1233 gmx_fio_do_real(fio, ir->sa_surface_tension);
1235 /* Override sa_surface_tension if it is not changed in the mpd-file */
1236 if (ir->sa_surface_tension < 0)
1238 if (ir->gb_algorithm == egbSTILL)
1240 ir->sa_surface_tension = 0.0049 * 100 * CAL2JOULE;
1242 else if (ir->gb_algorithm == egbHCT || ir->gb_algorithm == egbOBC)
1244 ir->sa_surface_tension = 0.0054 * 100 * CAL2JOULE;
1251 /* Better use sensible values than insane (0.0) ones... */
1252 ir->gb_epsilon_solvent = 80;
1253 ir->gb_obc_alpha = 1.0;
1254 ir->gb_obc_beta = 0.8;
1255 ir->gb_obc_gamma = 4.85;
1256 ir->sa_surface_tension = 2.092;
1260 if (file_version >= 81)
1262 gmx_fio_do_real(fio, ir->fourier_spacing);
1266 ir->fourier_spacing = 0.0;
1268 gmx_fio_do_int(fio, ir->nkx);
1269 gmx_fio_do_int(fio, ir->nky);
1270 gmx_fio_do_int(fio, ir->nkz);
1271 gmx_fio_do_int(fio, ir->pme_order);
1272 gmx_fio_do_real(fio, ir->ewald_rtol);
1274 if (file_version >= 93)
1276 gmx_fio_do_real(fio, ir->ewald_rtol_lj);
1280 ir->ewald_rtol_lj = ir->ewald_rtol;
1283 if (file_version >= 24)
1285 gmx_fio_do_int(fio, ir->ewald_geometry);
1289 ir->ewald_geometry = eewg3D;
1292 if (file_version <= 17)
1294 ir->epsilon_surface = 0;
1295 if (file_version == 17)
1297 gmx_fio_do_int(fio, idum);
1302 gmx_fio_do_real(fio, ir->epsilon_surface);
1305 /* ignore bOptFFT */
1306 if (file_version < tpxv_RemoveObsoleteParameters1)
1308 gmx_fio_do_gmx_bool(fio, bdum);
1311 if (file_version >= 93)
1313 gmx_fio_do_int(fio, ir->ljpme_combination_rule);
1315 gmx_fio_do_gmx_bool(fio, ir->bContinuation);
1316 gmx_fio_do_int(fio, ir->etc);
1317 /* before version 18, ir->etc was a gmx_bool (ir->btc),
1318 * but the values 0 and 1 still mean no and
1319 * berendsen temperature coupling, respectively.
1321 if (file_version >= 79)
1323 gmx_fio_do_gmx_bool(fio, ir->bPrintNHChains);
1325 if (file_version >= 71)
1327 gmx_fio_do_int(fio, ir->nsttcouple);
1331 ir->nsttcouple = ir->nstcalcenergy;
1333 if (file_version <= 15)
1335 gmx_fio_do_int(fio, idum);
1337 if (file_version <= 17)
1339 gmx_fio_do_int(fio, ir->epct);
1340 if (file_version <= 15)
1344 ir->epct = epctSURFACETENSION;
1346 gmx_fio_do_int(fio, idum);
1349 /* we have removed the NO alternative at the beginning */
1353 ir->epct = epctISOTROPIC;
1357 ir->epc = epcBERENDSEN;
1362 gmx_fio_do_int(fio, ir->epc);
1363 gmx_fio_do_int(fio, ir->epct);
1365 if (file_version >= 71)
1367 gmx_fio_do_int(fio, ir->nstpcouple);
1371 ir->nstpcouple = ir->nstcalcenergy;
1373 gmx_fio_do_real(fio, ir->tau_p);
1374 if (file_version <= 15)
1376 gmx_fio_do_rvec(fio, vdum);
1377 clear_mat(ir->ref_p);
1378 for (i = 0; i < DIM; i++)
1380 ir->ref_p[i][i] = vdum[i];
1385 gmx_fio_do_rvec(fio, ir->ref_p[XX]);
1386 gmx_fio_do_rvec(fio, ir->ref_p[YY]);
1387 gmx_fio_do_rvec(fio, ir->ref_p[ZZ]);
1389 if (file_version <= 15)
1391 gmx_fio_do_rvec(fio, vdum);
1392 clear_mat(ir->compress);
1393 for (i = 0; i < DIM; i++)
1395 ir->compress[i][i] = vdum[i];
1400 gmx_fio_do_rvec(fio, ir->compress[XX]);
1401 gmx_fio_do_rvec(fio, ir->compress[YY]);
1402 gmx_fio_do_rvec(fio, ir->compress[ZZ]);
1404 if (file_version >= 47)
1406 gmx_fio_do_int(fio, ir->refcoord_scaling);
1407 gmx_fio_do_rvec(fio, ir->posres_com);
1408 gmx_fio_do_rvec(fio, ir->posres_comB);
1412 ir->refcoord_scaling = erscNO;
1413 clear_rvec(ir->posres_com);
1414 clear_rvec(ir->posres_comB);
1416 if ((file_version > 25) && (file_version < 79))
1418 gmx_fio_do_int(fio, ir->andersen_seed);
1422 ir->andersen_seed = 0;
1424 if (file_version < 26)
1426 gmx_fio_do_gmx_bool(fio, bSimAnn);
1427 gmx_fio_do_real(fio, zerotemptime);
1430 if (file_version < 37)
1432 gmx_fio_do_real(fio, rdum);
1435 gmx_fio_do_real(fio, ir->shake_tol);
1436 if (file_version < 54)
1438 gmx_fio_do_real(fio, *fudgeQQ);
1441 gmx_fio_do_int(fio, ir->efep);
1442 if (file_version <= 14 && ir->efep != efepNO)
1446 do_fepvals(fio, ir->fepvals, bRead, file_version);
1448 if (file_version >= 79)
1450 gmx_fio_do_gmx_bool(fio, ir->bSimTemp);
1453 ir->bSimTemp = TRUE;
1458 ir->bSimTemp = FALSE;
1462 do_simtempvals(fio, ir->simtempvals, ir->fepvals->n_lambda, bRead, file_version);
1465 if (file_version >= 79)
1467 gmx_fio_do_gmx_bool(fio, ir->bExpanded);
1470 ir->bExpanded = TRUE;
1474 ir->bExpanded = FALSE;
1479 do_expandedvals(fio, ir->expandedvals, ir->fepvals, bRead, file_version);
1481 if (file_version >= 57)
1483 gmx_fio_do_int(fio, ir->eDisre);
1485 gmx_fio_do_int(fio, ir->eDisreWeighting);
1486 if (file_version < 22)
1488 if (ir->eDisreWeighting == 0)
1490 ir->eDisreWeighting = edrwEqual;
1494 ir->eDisreWeighting = edrwConservative;
1497 gmx_fio_do_gmx_bool(fio, ir->bDisreMixed);
1498 gmx_fio_do_real(fio, ir->dr_fc);
1499 gmx_fio_do_real(fio, ir->dr_tau);
1500 gmx_fio_do_int(fio, ir->nstdisreout);
1501 if (file_version >= 22)
1503 gmx_fio_do_real(fio, ir->orires_fc);
1504 gmx_fio_do_real(fio, ir->orires_tau);
1505 gmx_fio_do_int(fio, ir->nstorireout);
1511 ir->nstorireout = 0;
1514 /* ignore dihre_fc */
1515 if (file_version >= 26 && file_version < 79)
1517 gmx_fio_do_real(fio, rdum);
1518 if (file_version < 56)
1520 gmx_fio_do_real(fio, rdum);
1521 gmx_fio_do_int(fio, idum);
1525 gmx_fio_do_real(fio, ir->em_stepsize);
1526 gmx_fio_do_real(fio, ir->em_tol);
1527 if (file_version >= 22)
1529 gmx_fio_do_gmx_bool(fio, ir->bShakeSOR);
1533 ir->bShakeSOR = TRUE;
1535 if (file_version >= 11)
1537 gmx_fio_do_int(fio, ir->niter);
1542 fprintf(stderr, "Note: niter not in run input file, setting it to %d\n",
1545 if (file_version >= 21)
1547 gmx_fio_do_real(fio, ir->fc_stepsize);
1551 ir->fc_stepsize = 0;
1553 gmx_fio_do_int(fio, ir->eConstrAlg);
1554 gmx_fio_do_int(fio, ir->nProjOrder);
1555 gmx_fio_do_real(fio, ir->LincsWarnAngle);
1556 if (file_version <= 14)
1558 gmx_fio_do_int(fio, idum);
1560 if (file_version >= 26)
1562 gmx_fio_do_int(fio, ir->nLincsIter);
1567 fprintf(stderr, "Note: nLincsIter not in run input file, setting it to %d\n",
1570 if (file_version < 33)
1572 gmx_fio_do_real(fio, bd_temp);
1574 gmx_fio_do_real(fio, ir->bd_fric);
1575 if (file_version >= tpxv_Use64BitRandomSeed)
1577 gmx_fio_do_int64(fio, ir->ld_seed);
1581 gmx_fio_do_int(fio, idum);
1584 if (file_version >= 33)
1586 for (i = 0; i < DIM; i++)
1588 gmx_fio_do_rvec(fio, ir->deform[i]);
1593 for (i = 0; i < DIM; i++)
1595 clear_rvec(ir->deform[i]);
1598 if (file_version >= 14)
1600 gmx_fio_do_real(fio, ir->cos_accel);
1606 gmx_fio_do_int(fio, ir->userint1);
1607 gmx_fio_do_int(fio, ir->userint2);
1608 gmx_fio_do_int(fio, ir->userint3);
1609 gmx_fio_do_int(fio, ir->userint4);
1610 gmx_fio_do_real(fio, ir->userreal1);
1611 gmx_fio_do_real(fio, ir->userreal2);
1612 gmx_fio_do_real(fio, ir->userreal3);
1613 gmx_fio_do_real(fio, ir->userreal4);
1615 /* AdResS is removed, but we need to be able to read old files,
1616 and let mdrun refuse to run them */
1617 if (file_version >= 77 && file_version < tpxv_RemoveAdress)
1619 gmx_fio_do_gmx_bool(fio, ir->bAdress);
1622 int idum, numThermoForceGroups, numEnergyGroups;
1625 gmx_fio_do_int(fio, idum);
1626 gmx_fio_do_real(fio, rdum);
1627 gmx_fio_do_real(fio, rdum);
1628 gmx_fio_do_real(fio, rdum);
1629 gmx_fio_do_int(fio, idum);
1630 gmx_fio_do_int(fio, idum);
1631 gmx_fio_do_rvec(fio, rvecdum);
1632 gmx_fio_do_int(fio, numThermoForceGroups);
1633 gmx_fio_do_real(fio, rdum);
1634 gmx_fio_do_int(fio, numEnergyGroups);
1635 gmx_fio_do_int(fio, idum);
1637 if (numThermoForceGroups > 0)
1639 std::vector<int> idumn(numThermoForceGroups);
1640 gmx_fio_ndo_int(fio, idumn.data(), idumn.size());
1642 if (numEnergyGroups > 0)
1644 std::vector<int> idumn(numEnergyGroups);
1645 gmx_fio_ndo_int(fio, idumn.data(), idumn.size());
1651 ir->bAdress = FALSE;
1655 if (file_version >= 48)
1659 if (file_version >= tpxv_PullCoordTypeGeom)
1661 gmx_fio_do_gmx_bool(fio, ir->bPull);
1665 gmx_fio_do_int(fio, ePullOld);
1666 ir->bPull = (ePullOld > 0);
1667 /* We removed the first ePull=ePullNo for the enum */
1676 do_pull(fio, ir->pull, bRead, file_version, ePullOld);
1684 /* Enforced rotation */
1685 if (file_version >= 74)
1687 gmx_fio_do_int(fio, ir->bRot);
1688 if (ir->bRot == TRUE)
1694 do_rot(fio, ir->rot, bRead);
1702 /* Interactive molecular dynamics */
1703 if (file_version >= tpxv_InteractiveMolecularDynamics)
1705 gmx_fio_do_int(fio, ir->bIMD);
1706 if (TRUE == ir->bIMD)
1712 do_imd(fio, ir->imd, bRead);
1717 /* We don't support IMD sessions for old .tpr files */
1722 gmx_fio_do_int(fio, ir->opts.ngtc);
1723 if (file_version >= 69)
1725 gmx_fio_do_int(fio, ir->opts.nhchainlength);
1729 ir->opts.nhchainlength = 1;
1731 gmx_fio_do_int(fio, ir->opts.ngacc);
1732 gmx_fio_do_int(fio, ir->opts.ngfrz);
1733 gmx_fio_do_int(fio, ir->opts.ngener);
1737 snew(ir->opts.nrdf, ir->opts.ngtc);
1738 snew(ir->opts.ref_t, ir->opts.ngtc);
1739 snew(ir->opts.annealing, ir->opts.ngtc);
1740 snew(ir->opts.anneal_npoints, ir->opts.ngtc);
1741 snew(ir->opts.anneal_time, ir->opts.ngtc);
1742 snew(ir->opts.anneal_temp, ir->opts.ngtc);
1743 snew(ir->opts.tau_t, ir->opts.ngtc);
1744 snew(ir->opts.nFreeze, ir->opts.ngfrz);
1745 snew(ir->opts.acc, ir->opts.ngacc);
1746 snew(ir->opts.egp_flags, ir->opts.ngener*ir->opts.ngener);
1748 if (ir->opts.ngtc > 0)
1750 if (bRead && file_version < 13)
1752 snew(tmp, ir->opts.ngtc);
1753 gmx_fio_ndo_int(fio, tmp, ir->opts.ngtc);
1754 for (i = 0; i < ir->opts.ngtc; i++)
1756 ir->opts.nrdf[i] = tmp[i];
1762 gmx_fio_ndo_real(fio, ir->opts.nrdf, ir->opts.ngtc);
1764 gmx_fio_ndo_real(fio, ir->opts.ref_t, ir->opts.ngtc);
1765 gmx_fio_ndo_real(fio, ir->opts.tau_t, ir->opts.ngtc);
1766 if (file_version < 33 && ir->eI == eiBD)
1768 for (i = 0; i < ir->opts.ngtc; i++)
1770 ir->opts.tau_t[i] = bd_temp;
1774 if (ir->opts.ngfrz > 0)
1776 gmx_fio_ndo_ivec(fio, ir->opts.nFreeze, ir->opts.ngfrz);
1778 if (ir->opts.ngacc > 0)
1780 gmx_fio_ndo_rvec(fio, ir->opts.acc, ir->opts.ngacc);
1782 if (file_version >= 12)
1784 gmx_fio_ndo_int(fio, ir->opts.egp_flags,
1785 ir->opts.ngener*ir->opts.ngener);
1788 if (bRead && file_version < 26)
1790 for (i = 0; i < ir->opts.ngtc; i++)
1794 ir->opts.annealing[i] = eannSINGLE;
1795 ir->opts.anneal_npoints[i] = 2;
1796 snew(ir->opts.anneal_time[i], 2);
1797 snew(ir->opts.anneal_temp[i], 2);
1798 /* calculate the starting/ending temperatures from reft, zerotemptime, and nsteps */
1799 finish_t = ir->init_t + ir->nsteps * ir->delta_t;
1800 init_temp = ir->opts.ref_t[i]*(1-ir->init_t/zerotemptime);
1801 finish_temp = ir->opts.ref_t[i]*(1-finish_t/zerotemptime);
1802 ir->opts.anneal_time[i][0] = ir->init_t;
1803 ir->opts.anneal_time[i][1] = finish_t;
1804 ir->opts.anneal_temp[i][0] = init_temp;
1805 ir->opts.anneal_temp[i][1] = finish_temp;
1809 ir->opts.annealing[i] = eannNO;
1810 ir->opts.anneal_npoints[i] = 0;
1816 /* file version 26 or later */
1817 /* First read the lists with annealing and npoints for each group */
1818 gmx_fio_ndo_int(fio, ir->opts.annealing, ir->opts.ngtc);
1819 gmx_fio_ndo_int(fio, ir->opts.anneal_npoints, ir->opts.ngtc);
1820 for (j = 0; j < (ir->opts.ngtc); j++)
1822 k = ir->opts.anneal_npoints[j];
1825 snew(ir->opts.anneal_time[j], k);
1826 snew(ir->opts.anneal_temp[j], k);
1828 gmx_fio_ndo_real(fio, ir->opts.anneal_time[j], k);
1829 gmx_fio_ndo_real(fio, ir->opts.anneal_temp[j], k);
1833 if (file_version >= 45)
1835 gmx_fio_do_int(fio, ir->nwall);
1836 gmx_fio_do_int(fio, ir->wall_type);
1837 if (file_version >= 50)
1839 gmx_fio_do_real(fio, ir->wall_r_linpot);
1843 ir->wall_r_linpot = -1;
1845 gmx_fio_do_int(fio, ir->wall_atomtype[0]);
1846 gmx_fio_do_int(fio, ir->wall_atomtype[1]);
1847 gmx_fio_do_real(fio, ir->wall_density[0]);
1848 gmx_fio_do_real(fio, ir->wall_density[1]);
1849 gmx_fio_do_real(fio, ir->wall_ewald_zfac);
1855 ir->wall_atomtype[0] = -1;
1856 ir->wall_atomtype[1] = -1;
1857 ir->wall_density[0] = 0;
1858 ir->wall_density[1] = 0;
1859 ir->wall_ewald_zfac = 3;
1861 /* Cosine stuff for electric fields */
1862 for (j = 0; (j < DIM); j++)
1864 gmx_fio_do_int(fio, ir->ex[j].n);
1865 gmx_fio_do_int(fio, ir->et[j].n);
1868 snew(ir->ex[j].a, ir->ex[j].n);
1869 snew(ir->ex[j].phi, ir->ex[j].n);
1870 snew(ir->et[j].a, ir->et[j].n);
1871 snew(ir->et[j].phi, ir->et[j].n);
1873 gmx_fio_ndo_real(fio, ir->ex[j].a, ir->ex[j].n);
1874 gmx_fio_ndo_real(fio, ir->ex[j].phi, ir->ex[j].n);
1875 gmx_fio_ndo_real(fio, ir->et[j].a, ir->et[j].n);
1876 gmx_fio_ndo_real(fio, ir->et[j].phi, ir->et[j].n);
1880 if (file_version >= tpxv_ComputationalElectrophysiology)
1882 gmx_fio_do_int(fio, ir->eSwapCoords);
1883 if (ir->eSwapCoords != eswapNO)
1889 do_swapcoords_tpx(fio, ir->swap, bRead, file_version);
1894 if (file_version >= 39)
1896 gmx_fio_do_gmx_bool(fio, ir->bQMMM);
1897 gmx_fio_do_int(fio, ir->QMMMscheme);
1898 gmx_fio_do_real(fio, ir->scalefactor);
1899 gmx_fio_do_int(fio, ir->opts.ngQM);
1902 snew(ir->opts.QMmethod, ir->opts.ngQM);
1903 snew(ir->opts.QMbasis, ir->opts.ngQM);
1904 snew(ir->opts.QMcharge, ir->opts.ngQM);
1905 snew(ir->opts.QMmult, ir->opts.ngQM);
1906 snew(ir->opts.bSH, ir->opts.ngQM);
1907 snew(ir->opts.CASorbitals, ir->opts.ngQM);
1908 snew(ir->opts.CASelectrons, ir->opts.ngQM);
1909 snew(ir->opts.SAon, ir->opts.ngQM);
1910 snew(ir->opts.SAoff, ir->opts.ngQM);
1911 snew(ir->opts.SAsteps, ir->opts.ngQM);
1912 snew(ir->opts.bOPT, ir->opts.ngQM);
1913 snew(ir->opts.bTS, ir->opts.ngQM);
1915 if (ir->opts.ngQM > 0)
1917 gmx_fio_ndo_int(fio, ir->opts.QMmethod, ir->opts.ngQM);
1918 gmx_fio_ndo_int(fio, ir->opts.QMbasis, ir->opts.ngQM);
1919 gmx_fio_ndo_int(fio, ir->opts.QMcharge, ir->opts.ngQM);
1920 gmx_fio_ndo_int(fio, ir->opts.QMmult, ir->opts.ngQM);
1921 gmx_fio_ndo_gmx_bool(fio, ir->opts.bSH, ir->opts.ngQM);
1922 gmx_fio_ndo_int(fio, ir->opts.CASorbitals, ir->opts.ngQM);
1923 gmx_fio_ndo_int(fio, ir->opts.CASelectrons, ir->opts.ngQM);
1924 gmx_fio_ndo_real(fio, ir->opts.SAon, ir->opts.ngQM);
1925 gmx_fio_ndo_real(fio, ir->opts.SAoff, ir->opts.ngQM);
1926 gmx_fio_ndo_int(fio, ir->opts.SAsteps, ir->opts.ngQM);
1927 gmx_fio_ndo_gmx_bool(fio, ir->opts.bOPT, ir->opts.ngQM);
1928 gmx_fio_ndo_gmx_bool(fio, ir->opts.bTS, ir->opts.ngQM);
1930 /* end of QMMM stuff */
1935 static void do_harm(t_fileio *fio, t_iparams *iparams)
1937 gmx_fio_do_real(fio, iparams->harmonic.rA);
1938 gmx_fio_do_real(fio, iparams->harmonic.krA);
1939 gmx_fio_do_real(fio, iparams->harmonic.rB);
1940 gmx_fio_do_real(fio, iparams->harmonic.krB);
1943 void do_iparams(t_fileio *fio, t_functype ftype, t_iparams *iparams,
1944 gmx_bool bRead, int file_version)
1957 do_harm(fio, iparams);
1958 if ((ftype == F_ANGRES || ftype == F_ANGRESZ) && bRead)
1960 /* Correct incorrect storage of parameters */
1961 iparams->pdihs.phiB = iparams->pdihs.phiA;
1962 iparams->pdihs.cpB = iparams->pdihs.cpA;
1966 gmx_fio_do_real(fio, iparams->harmonic.rA);
1967 gmx_fio_do_real(fio, iparams->harmonic.krA);
1969 case F_LINEAR_ANGLES:
1970 gmx_fio_do_real(fio, iparams->linangle.klinA);
1971 gmx_fio_do_real(fio, iparams->linangle.aA);
1972 gmx_fio_do_real(fio, iparams->linangle.klinB);
1973 gmx_fio_do_real(fio, iparams->linangle.aB);
1976 gmx_fio_do_real(fio, iparams->fene.bm);
1977 gmx_fio_do_real(fio, iparams->fene.kb);
1981 gmx_fio_do_real(fio, iparams->restraint.lowA);
1982 gmx_fio_do_real(fio, iparams->restraint.up1A);
1983 gmx_fio_do_real(fio, iparams->restraint.up2A);
1984 gmx_fio_do_real(fio, iparams->restraint.kA);
1985 gmx_fio_do_real(fio, iparams->restraint.lowB);
1986 gmx_fio_do_real(fio, iparams->restraint.up1B);
1987 gmx_fio_do_real(fio, iparams->restraint.up2B);
1988 gmx_fio_do_real(fio, iparams->restraint.kB);
1994 gmx_fio_do_real(fio, iparams->tab.kA);
1995 gmx_fio_do_int(fio, iparams->tab.table);
1996 gmx_fio_do_real(fio, iparams->tab.kB);
1998 case F_CROSS_BOND_BONDS:
1999 gmx_fio_do_real(fio, iparams->cross_bb.r1e);
2000 gmx_fio_do_real(fio, iparams->cross_bb.r2e);
2001 gmx_fio_do_real(fio, iparams->cross_bb.krr);
2003 case F_CROSS_BOND_ANGLES:
2004 gmx_fio_do_real(fio, iparams->cross_ba.r1e);
2005 gmx_fio_do_real(fio, iparams->cross_ba.r2e);
2006 gmx_fio_do_real(fio, iparams->cross_ba.r3e);
2007 gmx_fio_do_real(fio, iparams->cross_ba.krt);
2009 case F_UREY_BRADLEY:
2010 gmx_fio_do_real(fio, iparams->u_b.thetaA);
2011 gmx_fio_do_real(fio, iparams->u_b.kthetaA);
2012 gmx_fio_do_real(fio, iparams->u_b.r13A);
2013 gmx_fio_do_real(fio, iparams->u_b.kUBA);
2014 if (file_version >= 79)
2016 gmx_fio_do_real(fio, iparams->u_b.thetaB);
2017 gmx_fio_do_real(fio, iparams->u_b.kthetaB);
2018 gmx_fio_do_real(fio, iparams->u_b.r13B);
2019 gmx_fio_do_real(fio, iparams->u_b.kUBB);
2023 iparams->u_b.thetaB = iparams->u_b.thetaA;
2024 iparams->u_b.kthetaB = iparams->u_b.kthetaA;
2025 iparams->u_b.r13B = iparams->u_b.r13A;
2026 iparams->u_b.kUBB = iparams->u_b.kUBA;
2029 case F_QUARTIC_ANGLES:
2030 gmx_fio_do_real(fio, iparams->qangle.theta);
2031 gmx_fio_ndo_real(fio, iparams->qangle.c, 5);
2034 gmx_fio_do_real(fio, iparams->bham.a);
2035 gmx_fio_do_real(fio, iparams->bham.b);
2036 gmx_fio_do_real(fio, iparams->bham.c);
2039 gmx_fio_do_real(fio, iparams->morse.b0A);
2040 gmx_fio_do_real(fio, iparams->morse.cbA);
2041 gmx_fio_do_real(fio, iparams->morse.betaA);
2042 if (file_version >= 79)
2044 gmx_fio_do_real(fio, iparams->morse.b0B);
2045 gmx_fio_do_real(fio, iparams->morse.cbB);
2046 gmx_fio_do_real(fio, iparams->morse.betaB);
2050 iparams->morse.b0B = iparams->morse.b0A;
2051 iparams->morse.cbB = iparams->morse.cbA;
2052 iparams->morse.betaB = iparams->morse.betaA;
2056 gmx_fio_do_real(fio, iparams->cubic.b0);
2057 gmx_fio_do_real(fio, iparams->cubic.kb);
2058 gmx_fio_do_real(fio, iparams->cubic.kcub);
2062 case F_POLARIZATION:
2063 gmx_fio_do_real(fio, iparams->polarize.alpha);
2066 gmx_fio_do_real(fio, iparams->anharm_polarize.alpha);
2067 gmx_fio_do_real(fio, iparams->anharm_polarize.drcut);
2068 gmx_fio_do_real(fio, iparams->anharm_polarize.khyp);
2071 if (file_version < 31)
2073 gmx_fatal(FARGS, "Old tpr files with water_polarization not supported. Make a new.");
2075 gmx_fio_do_real(fio, iparams->wpol.al_x);
2076 gmx_fio_do_real(fio, iparams->wpol.al_y);
2077 gmx_fio_do_real(fio, iparams->wpol.al_z);
2078 gmx_fio_do_real(fio, iparams->wpol.rOH);
2079 gmx_fio_do_real(fio, iparams->wpol.rHH);
2080 gmx_fio_do_real(fio, iparams->wpol.rOD);
2083 gmx_fio_do_real(fio, iparams->thole.a);
2084 gmx_fio_do_real(fio, iparams->thole.alpha1);
2085 gmx_fio_do_real(fio, iparams->thole.alpha2);
2086 gmx_fio_do_real(fio, iparams->thole.rfac);
2089 gmx_fio_do_real(fio, iparams->lj.c6);
2090 gmx_fio_do_real(fio, iparams->lj.c12);
2093 gmx_fio_do_real(fio, iparams->lj14.c6A);
2094 gmx_fio_do_real(fio, iparams->lj14.c12A);
2095 gmx_fio_do_real(fio, iparams->lj14.c6B);
2096 gmx_fio_do_real(fio, iparams->lj14.c12B);
2099 gmx_fio_do_real(fio, iparams->ljc14.fqq);
2100 gmx_fio_do_real(fio, iparams->ljc14.qi);
2101 gmx_fio_do_real(fio, iparams->ljc14.qj);
2102 gmx_fio_do_real(fio, iparams->ljc14.c6);
2103 gmx_fio_do_real(fio, iparams->ljc14.c12);
2105 case F_LJC_PAIRS_NB:
2106 gmx_fio_do_real(fio, iparams->ljcnb.qi);
2107 gmx_fio_do_real(fio, iparams->ljcnb.qj);
2108 gmx_fio_do_real(fio, iparams->ljcnb.c6);
2109 gmx_fio_do_real(fio, iparams->ljcnb.c12);
2115 gmx_fio_do_real(fio, iparams->pdihs.phiA);
2116 gmx_fio_do_real(fio, iparams->pdihs.cpA);
2117 if ((ftype == F_ANGRES || ftype == F_ANGRESZ) && file_version < 42)
2119 /* Read the incorrectly stored multiplicity */
2120 gmx_fio_do_real(fio, iparams->harmonic.rB);
2121 gmx_fio_do_real(fio, iparams->harmonic.krB);
2122 iparams->pdihs.phiB = iparams->pdihs.phiA;
2123 iparams->pdihs.cpB = iparams->pdihs.cpA;
2127 gmx_fio_do_real(fio, iparams->pdihs.phiB);
2128 gmx_fio_do_real(fio, iparams->pdihs.cpB);
2129 gmx_fio_do_int(fio, iparams->pdihs.mult);
2133 gmx_fio_do_real(fio, iparams->pdihs.phiA);
2134 gmx_fio_do_real(fio, iparams->pdihs.cpA);
2137 gmx_fio_do_int(fio, iparams->disres.label);
2138 gmx_fio_do_int(fio, iparams->disres.type);
2139 gmx_fio_do_real(fio, iparams->disres.low);
2140 gmx_fio_do_real(fio, iparams->disres.up1);
2141 gmx_fio_do_real(fio, iparams->disres.up2);
2142 gmx_fio_do_real(fio, iparams->disres.kfac);
2145 gmx_fio_do_int(fio, iparams->orires.ex);
2146 gmx_fio_do_int(fio, iparams->orires.label);
2147 gmx_fio_do_int(fio, iparams->orires.power);
2148 gmx_fio_do_real(fio, iparams->orires.c);
2149 gmx_fio_do_real(fio, iparams->orires.obs);
2150 gmx_fio_do_real(fio, iparams->orires.kfac);
2153 if (file_version < 82)
2155 gmx_fio_do_int(fio, idum);
2156 gmx_fio_do_int(fio, idum);
2158 gmx_fio_do_real(fio, iparams->dihres.phiA);
2159 gmx_fio_do_real(fio, iparams->dihres.dphiA);
2160 gmx_fio_do_real(fio, iparams->dihres.kfacA);
2161 if (file_version >= 82)
2163 gmx_fio_do_real(fio, iparams->dihres.phiB);
2164 gmx_fio_do_real(fio, iparams->dihres.dphiB);
2165 gmx_fio_do_real(fio, iparams->dihres.kfacB);
2169 iparams->dihres.phiB = iparams->dihres.phiA;
2170 iparams->dihres.dphiB = iparams->dihres.dphiA;
2171 iparams->dihres.kfacB = iparams->dihres.kfacA;
2175 gmx_fio_do_rvec(fio, iparams->posres.pos0A);
2176 gmx_fio_do_rvec(fio, iparams->posres.fcA);
2177 if (bRead && file_version < 27)
2179 copy_rvec(iparams->posres.pos0A, iparams->posres.pos0B);
2180 copy_rvec(iparams->posres.fcA, iparams->posres.fcB);
2184 gmx_fio_do_rvec(fio, iparams->posres.pos0B);
2185 gmx_fio_do_rvec(fio, iparams->posres.fcB);
2189 gmx_fio_do_int(fio, iparams->fbposres.geom);
2190 gmx_fio_do_rvec(fio, iparams->fbposres.pos0);
2191 gmx_fio_do_real(fio, iparams->fbposres.r);
2192 gmx_fio_do_real(fio, iparams->fbposres.k);
2195 gmx_fio_ndo_real(fio, iparams->cbtdihs.cbtcA, NR_CBTDIHS);
2198 gmx_fio_ndo_real(fio, iparams->rbdihs.rbcA, NR_RBDIHS);
2199 if (file_version >= 25)
2201 gmx_fio_ndo_real(fio, iparams->rbdihs.rbcB, NR_RBDIHS);
2205 /* Fourier dihedrals are internally represented
2206 * as Ryckaert-Bellemans since those are faster to compute.
2208 gmx_fio_ndo_real(fio, iparams->rbdihs.rbcA, NR_RBDIHS);
2209 gmx_fio_ndo_real(fio, iparams->rbdihs.rbcB, NR_RBDIHS);
2213 gmx_fio_do_real(fio, iparams->constr.dA);
2214 gmx_fio_do_real(fio, iparams->constr.dB);
2217 gmx_fio_do_real(fio, iparams->settle.doh);
2218 gmx_fio_do_real(fio, iparams->settle.dhh);
2221 gmx_fio_do_real(fio, iparams->vsite.a);
2226 gmx_fio_do_real(fio, iparams->vsite.a);
2227 gmx_fio_do_real(fio, iparams->vsite.b);
2232 gmx_fio_do_real(fio, iparams->vsite.a);
2233 gmx_fio_do_real(fio, iparams->vsite.b);
2234 gmx_fio_do_real(fio, iparams->vsite.c);
2237 gmx_fio_do_int(fio, iparams->vsiten.n);
2238 gmx_fio_do_real(fio, iparams->vsiten.a);
2243 /* We got rid of some parameters in version 68 */
2244 if (bRead && file_version < 68)
2246 gmx_fio_do_real(fio, rdum);
2247 gmx_fio_do_real(fio, rdum);
2248 gmx_fio_do_real(fio, rdum);
2249 gmx_fio_do_real(fio, rdum);
2251 gmx_fio_do_real(fio, iparams->gb.sar);
2252 gmx_fio_do_real(fio, iparams->gb.st);
2253 gmx_fio_do_real(fio, iparams->gb.pi);
2254 gmx_fio_do_real(fio, iparams->gb.gbr);
2255 gmx_fio_do_real(fio, iparams->gb.bmlt);
2258 gmx_fio_do_int(fio, iparams->cmap.cmapA);
2259 gmx_fio_do_int(fio, iparams->cmap.cmapB);
2262 gmx_fatal(FARGS, "unknown function type %d (%s) in %s line %d",
2263 ftype, interaction_function[ftype].name, __FILE__, __LINE__);
2267 static void do_ilist(t_fileio *fio, t_ilist *ilist, gmx_bool bRead, int file_version)
2271 if (file_version < 44)
2273 for (i = 0; i < MAXNODES; i++)
2275 gmx_fio_do_int(fio, idum);
2278 gmx_fio_do_int(fio, ilist->nr);
2281 snew(ilist->iatoms, ilist->nr);
2283 gmx_fio_ndo_int(fio, ilist->iatoms, ilist->nr);
2286 static void do_ffparams(t_fileio *fio, gmx_ffparams_t *ffparams,
2287 gmx_bool bRead, int file_version)
2292 gmx_fio_do_int(fio, ffparams->atnr);
2293 if (file_version < 57)
2295 gmx_fio_do_int(fio, idum);
2297 gmx_fio_do_int(fio, ffparams->ntypes);
2300 snew(ffparams->functype, ffparams->ntypes);
2301 snew(ffparams->iparams, ffparams->ntypes);
2303 /* Read/write all the function types */
2304 gmx_fio_ndo_int(fio, ffparams->functype, ffparams->ntypes);
2306 if (file_version >= 66)
2308 gmx_fio_do_double(fio, ffparams->reppow);
2312 ffparams->reppow = 12.0;
2315 if (file_version >= 57)
2317 gmx_fio_do_real(fio, ffparams->fudgeQQ);
2320 /* Check whether all these function types are supported by the code.
2321 * In practice the code is backwards compatible, which means that the
2322 * numbering may have to be altered from old numbering to new numbering
2324 for (i = 0; (i < ffparams->ntypes); i++)
2328 /* Loop over file versions */
2329 for (k = 0; (k < NFTUPD); k++)
2331 /* Compare the read file_version to the update table */
2332 if ((file_version < ftupd[k].fvnr) &&
2333 (ffparams->functype[i] >= ftupd[k].ftype))
2335 ffparams->functype[i] += 1;
2340 do_iparams(fio, ffparams->functype[i], &ffparams->iparams[i], bRead,
2345 static void add_settle_atoms(t_ilist *ilist)
2349 /* Settle used to only store the first atom: add the other two */
2350 srenew(ilist->iatoms, 2*ilist->nr);
2351 for (i = ilist->nr/2-1; i >= 0; i--)
2353 ilist->iatoms[4*i+0] = ilist->iatoms[2*i+0];
2354 ilist->iatoms[4*i+1] = ilist->iatoms[2*i+1];
2355 ilist->iatoms[4*i+2] = ilist->iatoms[2*i+1] + 1;
2356 ilist->iatoms[4*i+3] = ilist->iatoms[2*i+1] + 2;
2358 ilist->nr = 2*ilist->nr;
2361 static void do_ilists(t_fileio *fio, t_ilist *ilist, gmx_bool bRead,
2368 for (j = 0; (j < F_NRE); j++)
2373 for (k = 0; k < NFTUPD; k++)
2375 if ((file_version < ftupd[k].fvnr) && (j == ftupd[k].ftype))
2384 ilist[j].iatoms = NULL;
2388 do_ilist(fio, &ilist[j], bRead, file_version);
2389 if (file_version < 78 && j == F_SETTLE && ilist[j].nr > 0)
2391 add_settle_atoms(&ilist[j]);
2397 static void do_idef(t_fileio *fio, gmx_ffparams_t *ffparams, gmx_moltype_t *molt,
2398 gmx_bool bRead, int file_version)
2400 do_ffparams(fio, ffparams, bRead, file_version);
2402 if (file_version >= 54)
2404 gmx_fio_do_real(fio, ffparams->fudgeQQ);
2407 do_ilists(fio, molt->ilist, bRead, file_version);
2410 static void do_block(t_fileio *fio, t_block *block, gmx_bool bRead, int file_version)
2412 int i, idum, dum_nra, *dum_a;
2414 if (file_version < 44)
2416 for (i = 0; i < MAXNODES; i++)
2418 gmx_fio_do_int(fio, idum);
2421 gmx_fio_do_int(fio, block->nr);
2422 if (file_version < 51)
2424 gmx_fio_do_int(fio, dum_nra);
2428 if ((block->nalloc_index > 0) && (NULL != block->index))
2430 sfree(block->index);
2432 block->nalloc_index = block->nr+1;
2433 snew(block->index, block->nalloc_index);
2435 gmx_fio_ndo_int(fio, block->index, block->nr+1);
2437 if (file_version < 51 && dum_nra > 0)
2439 snew(dum_a, dum_nra);
2440 gmx_fio_ndo_int(fio, dum_a, dum_nra);
2445 static void do_blocka(t_fileio *fio, t_blocka *block, gmx_bool bRead,
2450 if (file_version < 44)
2452 for (i = 0; i < MAXNODES; i++)
2454 gmx_fio_do_int(fio, idum);
2457 gmx_fio_do_int(fio, block->nr);
2458 gmx_fio_do_int(fio, block->nra);
2461 block->nalloc_index = block->nr+1;
2462 snew(block->index, block->nalloc_index);
2463 block->nalloc_a = block->nra;
2464 snew(block->a, block->nalloc_a);
2466 gmx_fio_ndo_int(fio, block->index, block->nr+1);
2467 gmx_fio_ndo_int(fio, block->a, block->nra);
2470 /* This is a primitive routine to make it possible to translate atomic numbers
2471 * to element names when reading TPR files, without making the Gromacs library
2472 * directory a dependency on mdrun (which is the case if we need elements.dat).
2475 atomicnumber_to_element(int atomicnumber)
2479 /* This does not have to be complete, so we only include elements likely
2480 * to occur in PDB files.
2482 switch (atomicnumber)
2484 case 1: p = "H"; break;
2485 case 5: p = "B"; break;
2486 case 6: p = "C"; break;
2487 case 7: p = "N"; break;
2488 case 8: p = "O"; break;
2489 case 9: p = "F"; break;
2490 case 11: p = "Na"; break;
2491 case 12: p = "Mg"; break;
2492 case 15: p = "P"; break;
2493 case 16: p = "S"; break;
2494 case 17: p = "Cl"; break;
2495 case 18: p = "Ar"; break;
2496 case 19: p = "K"; break;
2497 case 20: p = "Ca"; break;
2498 case 25: p = "Mn"; break;
2499 case 26: p = "Fe"; break;
2500 case 28: p = "Ni"; break;
2501 case 29: p = "Cu"; break;
2502 case 30: p = "Zn"; break;
2503 case 35: p = "Br"; break;
2504 case 47: p = "Ag"; break;
2505 default: p = ""; break;
2511 static void do_atom(t_fileio *fio, t_atom *atom, int ngrp, gmx_bool bRead,
2512 int file_version, gmx_groups_t *groups, int atnr)
2516 gmx_fio_do_real(fio, atom->m);
2517 gmx_fio_do_real(fio, atom->q);
2518 gmx_fio_do_real(fio, atom->mB);
2519 gmx_fio_do_real(fio, atom->qB);
2520 gmx_fio_do_ushort(fio, atom->type);
2521 gmx_fio_do_ushort(fio, atom->typeB);
2522 gmx_fio_do_int(fio, atom->ptype);
2523 gmx_fio_do_int(fio, atom->resind);
2524 if (file_version >= 52)
2526 gmx_fio_do_int(fio, atom->atomnumber);
2529 /* Set element string from atomic number if present.
2530 * This routine returns an empty string if the name is not found.
2532 std::strncpy(atom->elem, atomicnumber_to_element(atom->atomnumber), 4);
2533 /* avoid warnings about potentially unterminated string */
2534 atom->elem[3] = '\0';
2539 atom->atomnumber = 0;
2541 if (file_version < 23)
2545 else if (file_version < 39)
2554 if (file_version < 57)
2556 unsigned char uchar[egcNR];
2557 gmx_fio_ndo_uchar(fio, uchar, myngrp);
2558 for (i = myngrp; (i < ngrp); i++)
2562 /* Copy the old data format to the groups struct */
2563 for (i = 0; i < ngrp; i++)
2565 groups->grpnr[i][atnr] = uchar[i];
2570 static void do_grps(t_fileio *fio, int ngrp, t_grps grps[], gmx_bool bRead,
2575 if (file_version < 23)
2579 else if (file_version < 39)
2588 for (j = 0; (j < ngrp); j++)
2592 gmx_fio_do_int(fio, grps[j].nr);
2595 snew(grps[j].nm_ind, grps[j].nr);
2597 gmx_fio_ndo_int(fio, grps[j].nm_ind, grps[j].nr);
2602 snew(grps[j].nm_ind, grps[j].nr);
2607 static void do_symstr(t_fileio *fio, char ***nm, gmx_bool bRead, t_symtab *symtab)
2613 gmx_fio_do_int(fio, ls);
2614 *nm = get_symtab_handle(symtab, ls);
2618 ls = lookup_symtab(symtab, *nm);
2619 gmx_fio_do_int(fio, ls);
2623 static void do_strstr(t_fileio *fio, int nstr, char ***nm, gmx_bool bRead,
2628 for (j = 0; (j < nstr); j++)
2630 do_symstr(fio, &(nm[j]), bRead, symtab);
2634 static void do_resinfo(t_fileio *fio, int n, t_resinfo *ri, gmx_bool bRead,
2635 t_symtab *symtab, int file_version)
2639 for (j = 0; (j < n); j++)
2641 do_symstr(fio, &(ri[j].name), bRead, symtab);
2642 if (file_version >= 63)
2644 gmx_fio_do_int(fio, ri[j].nr);
2645 gmx_fio_do_uchar(fio, ri[j].ic);
2655 static void do_atoms(t_fileio *fio, t_atoms *atoms, gmx_bool bRead, t_symtab *symtab,
2657 gmx_groups_t *groups)
2661 gmx_fio_do_int(fio, atoms->nr);
2662 gmx_fio_do_int(fio, atoms->nres);
2663 if (file_version < 57)
2665 gmx_fio_do_int(fio, groups->ngrpname);
2666 for (i = 0; i < egcNR; i++)
2668 groups->ngrpnr[i] = atoms->nr;
2669 snew(groups->grpnr[i], groups->ngrpnr[i]);
2674 snew(atoms->atom, atoms->nr);
2675 snew(atoms->atomname, atoms->nr);
2676 snew(atoms->atomtype, atoms->nr);
2677 snew(atoms->atomtypeB, atoms->nr);
2678 snew(atoms->resinfo, atoms->nres);
2679 if (file_version < 57)
2681 snew(groups->grpname, groups->ngrpname);
2683 atoms->pdbinfo = NULL;
2685 for (i = 0; (i < atoms->nr); i++)
2687 do_atom(fio, &atoms->atom[i], egcNR, bRead, file_version, groups, i);
2689 do_strstr(fio, atoms->nr, atoms->atomname, bRead, symtab);
2690 if (bRead && (file_version <= 20))
2692 for (i = 0; i < atoms->nr; i++)
2694 atoms->atomtype[i] = put_symtab(symtab, "?");
2695 atoms->atomtypeB[i] = put_symtab(symtab, "?");
2700 do_strstr(fio, atoms->nr, atoms->atomtype, bRead, symtab);
2701 do_strstr(fio, atoms->nr, atoms->atomtypeB, bRead, symtab);
2703 do_resinfo(fio, atoms->nres, atoms->resinfo, bRead, symtab, file_version);
2705 if (file_version < 57)
2707 do_strstr(fio, groups->ngrpname, groups->grpname, bRead, symtab);
2709 do_grps(fio, egcNR, groups->grps, bRead, file_version);
2713 static void do_groups(t_fileio *fio, gmx_groups_t *groups,
2714 gmx_bool bRead, t_symtab *symtab,
2719 do_grps(fio, egcNR, groups->grps, bRead, file_version);
2720 gmx_fio_do_int(fio, groups->ngrpname);
2723 snew(groups->grpname, groups->ngrpname);
2725 do_strstr(fio, groups->ngrpname, groups->grpname, bRead, symtab);
2726 for (g = 0; g < egcNR; g++)
2728 gmx_fio_do_int(fio, groups->ngrpnr[g]);
2729 if (groups->ngrpnr[g] == 0)
2733 groups->grpnr[g] = NULL;
2740 snew(groups->grpnr[g], groups->ngrpnr[g]);
2742 gmx_fio_ndo_uchar(fio, groups->grpnr[g], groups->ngrpnr[g]);
2747 static void do_atomtypes(t_fileio *fio, t_atomtypes *atomtypes, gmx_bool bRead,
2752 if (file_version > 25)
2754 gmx_fio_do_int(fio, atomtypes->nr);
2758 snew(atomtypes->radius, j);
2759 snew(atomtypes->vol, j);
2760 snew(atomtypes->surftens, j);
2761 snew(atomtypes->atomnumber, j);
2762 snew(atomtypes->gb_radius, j);
2763 snew(atomtypes->S_hct, j);
2765 gmx_fio_ndo_real(fio, atomtypes->radius, j);
2766 gmx_fio_ndo_real(fio, atomtypes->vol, j);
2767 gmx_fio_ndo_real(fio, atomtypes->surftens, j);
2768 if (file_version >= 40)
2770 gmx_fio_ndo_int(fio, atomtypes->atomnumber, j);
2772 if (file_version >= 60)
2774 gmx_fio_ndo_real(fio, atomtypes->gb_radius, j);
2775 gmx_fio_ndo_real(fio, atomtypes->S_hct, j);
2780 /* File versions prior to 26 cannot do GBSA,
2781 * so they dont use this structure
2784 atomtypes->radius = NULL;
2785 atomtypes->vol = NULL;
2786 atomtypes->surftens = NULL;
2787 atomtypes->atomnumber = NULL;
2788 atomtypes->gb_radius = NULL;
2789 atomtypes->S_hct = NULL;
2793 static void do_symtab(t_fileio *fio, t_symtab *symtab, gmx_bool bRead)
2799 gmx_fio_do_int(fio, symtab->nr);
2803 snew(symtab->symbuf, 1);
2804 symbuf = symtab->symbuf;
2805 symbuf->bufsize = nr;
2806 snew(symbuf->buf, nr);
2807 for (i = 0; (i < nr); i++)
2809 gmx_fio_do_string(fio, buf);
2810 symbuf->buf[i] = gmx_strdup(buf);
2815 symbuf = symtab->symbuf;
2816 while (symbuf != NULL)
2818 for (i = 0; (i < symbuf->bufsize) && (i < nr); i++)
2820 gmx_fio_do_string(fio, symbuf->buf[i]);
2823 symbuf = symbuf->next;
2827 gmx_fatal(FARGS, "nr of symtab strings left: %d", nr);
2832 static void do_cmap(t_fileio *fio, gmx_cmap_t *cmap_grid, gmx_bool bRead)
2834 int i, j, ngrid, gs, nelem;
2836 gmx_fio_do_int(fio, cmap_grid->ngrid);
2837 gmx_fio_do_int(fio, cmap_grid->grid_spacing);
2839 ngrid = cmap_grid->ngrid;
2840 gs = cmap_grid->grid_spacing;
2845 snew(cmap_grid->cmapdata, ngrid);
2847 for (i = 0; i < cmap_grid->ngrid; i++)
2849 snew(cmap_grid->cmapdata[i].cmap, 4*nelem);
2853 for (i = 0; i < cmap_grid->ngrid; i++)
2855 for (j = 0; j < nelem; j++)
2857 gmx_fio_do_real(fio, cmap_grid->cmapdata[i].cmap[j*4]);
2858 gmx_fio_do_real(fio, cmap_grid->cmapdata[i].cmap[j*4+1]);
2859 gmx_fio_do_real(fio, cmap_grid->cmapdata[i].cmap[j*4+2]);
2860 gmx_fio_do_real(fio, cmap_grid->cmapdata[i].cmap[j*4+3]);
2866 static void do_moltype(t_fileio *fio, gmx_moltype_t *molt, gmx_bool bRead,
2867 t_symtab *symtab, int file_version,
2868 gmx_groups_t *groups)
2870 if (file_version >= 57)
2872 do_symstr(fio, &(molt->name), bRead, symtab);
2875 do_atoms(fio, &molt->atoms, bRead, symtab, file_version, groups);
2877 if (file_version >= 57)
2879 do_ilists(fio, molt->ilist, bRead, file_version);
2881 do_block(fio, &molt->cgs, bRead, file_version);
2884 /* This used to be in the atoms struct */
2885 do_blocka(fio, &molt->excls, bRead, file_version);
2888 static void do_molblock(t_fileio *fio, gmx_molblock_t *molb, gmx_bool bRead)
2890 gmx_fio_do_int(fio, molb->type);
2891 gmx_fio_do_int(fio, molb->nmol);
2892 gmx_fio_do_int(fio, molb->natoms_mol);
2893 /* Position restraint coordinates */
2894 gmx_fio_do_int(fio, molb->nposres_xA);
2895 if (molb->nposres_xA > 0)
2899 snew(molb->posres_xA, molb->nposres_xA);
2901 gmx_fio_ndo_rvec(fio, molb->posres_xA, molb->nposres_xA);
2903 gmx_fio_do_int(fio, molb->nposres_xB);
2904 if (molb->nposres_xB > 0)
2908 snew(molb->posres_xB, molb->nposres_xB);
2910 gmx_fio_ndo_rvec(fio, molb->posres_xB, molb->nposres_xB);
2915 static t_block mtop_mols(gmx_mtop_t *mtop)
2921 for (mb = 0; mb < mtop->nmolblock; mb++)
2923 mols.nr += mtop->molblock[mb].nmol;
2925 mols.nalloc_index = mols.nr + 1;
2926 snew(mols.index, mols.nalloc_index);
2931 for (mb = 0; mb < mtop->nmolblock; mb++)
2933 for (mol = 0; mol < mtop->molblock[mb].nmol; mol++)
2935 a += mtop->molblock[mb].natoms_mol;
2944 static void add_posres_molblock(gmx_mtop_t *mtop)
2949 gmx_molblock_t *molb;
2952 /* posres reference positions are stored in ip->posres (if present) and
2953 in ip->fbposres (if present). If normal and flat-bottomed posres are present,
2954 posres.pos0A are identical to fbposres.pos0. */
2955 il = &mtop->moltype[0].ilist[F_POSRES];
2956 ilfb = &mtop->moltype[0].ilist[F_FBPOSRES];
2957 if (il->nr == 0 && ilfb->nr == 0)
2963 for (i = 0; i < il->nr; i += 2)
2965 ip = &mtop->ffparams.iparams[il->iatoms[i]];
2966 am = std::max(am, il->iatoms[i+1]);
2967 if (ip->posres.pos0B[XX] != ip->posres.pos0A[XX] ||
2968 ip->posres.pos0B[YY] != ip->posres.pos0A[YY] ||
2969 ip->posres.pos0B[ZZ] != ip->posres.pos0A[ZZ])
2974 /* This loop is required if we have only flat-bottomed posres:
2976 - bFE == FALSE (no B-state for flat-bottomed posres) */
2979 for (i = 0; i < ilfb->nr; i += 2)
2981 am = std::max(am, ilfb->iatoms[i+1]);
2984 /* Make the posres coordinate block end at a molecule end */
2986 while (am >= mtop->mols.index[mol+1])
2990 molb = &mtop->molblock[0];
2991 molb->nposres_xA = mtop->mols.index[mol+1];
2992 snew(molb->posres_xA, molb->nposres_xA);
2995 molb->nposres_xB = molb->nposres_xA;
2996 snew(molb->posres_xB, molb->nposres_xB);
3000 molb->nposres_xB = 0;
3002 for (i = 0; i < il->nr; i += 2)
3004 ip = &mtop->ffparams.iparams[il->iatoms[i]];
3005 a = il->iatoms[i+1];
3006 molb->posres_xA[a][XX] = ip->posres.pos0A[XX];
3007 molb->posres_xA[a][YY] = ip->posres.pos0A[YY];
3008 molb->posres_xA[a][ZZ] = ip->posres.pos0A[ZZ];
3011 molb->posres_xB[a][XX] = ip->posres.pos0B[XX];
3012 molb->posres_xB[a][YY] = ip->posres.pos0B[YY];
3013 molb->posres_xB[a][ZZ] = ip->posres.pos0B[ZZ];
3018 /* If only flat-bottomed posres are present, take reference pos from them.
3019 Here: bFE == FALSE */
3020 for (i = 0; i < ilfb->nr; i += 2)
3022 ip = &mtop->ffparams.iparams[ilfb->iatoms[i]];
3023 a = ilfb->iatoms[i+1];
3024 molb->posres_xA[a][XX] = ip->fbposres.pos0[XX];
3025 molb->posres_xA[a][YY] = ip->fbposres.pos0[YY];
3026 molb->posres_xA[a][ZZ] = ip->fbposres.pos0[ZZ];
3031 static void set_disres_npair(gmx_mtop_t *mtop)
3034 gmx_mtop_ilistloop_t iloop;
3035 t_ilist *ilist, *il;
3039 ip = mtop->ffparams.iparams;
3041 iloop = gmx_mtop_ilistloop_init(mtop);
3042 while (gmx_mtop_ilistloop_next(iloop, &ilist, &nmol))
3044 il = &ilist[F_DISRES];
3050 for (i = 0; i < il->nr; i += 3)
3053 if (i+3 == il->nr || ip[a[i]].disres.label != ip[a[i+3]].disres.label)
3055 ip[a[i]].disres.npair = npair;
3063 static void do_mtop(t_fileio *fio, gmx_mtop_t *mtop, gmx_bool bRead,
3073 do_symtab(fio, &(mtop->symtab), bRead);
3075 do_symstr(fio, &(mtop->name), bRead, &(mtop->symtab));
3077 if (file_version >= 57)
3079 do_ffparams(fio, &mtop->ffparams, bRead, file_version);
3081 gmx_fio_do_int(fio, mtop->nmoltype);
3089 snew(mtop->moltype, mtop->nmoltype);
3090 if (file_version < 57)
3092 mtop->moltype[0].name = mtop->name;
3095 for (mt = 0; mt < mtop->nmoltype; mt++)
3097 do_moltype(fio, &mtop->moltype[mt], bRead, &mtop->symtab, file_version,
3101 if (file_version >= 57)
3103 gmx_fio_do_int(fio, mtop->nmolblock);
3107 mtop->nmolblock = 1;
3111 snew(mtop->molblock, mtop->nmolblock);
3113 if (file_version >= 57)
3115 for (mb = 0; mb < mtop->nmolblock; mb++)
3117 do_molblock(fio, &mtop->molblock[mb], bRead);
3119 gmx_fio_do_int(fio, mtop->natoms);
3123 mtop->molblock[0].type = 0;
3124 mtop->molblock[0].nmol = 1;
3125 mtop->molblock[0].natoms_mol = mtop->moltype[0].atoms.nr;
3126 mtop->molblock[0].nposres_xA = 0;
3127 mtop->molblock[0].nposres_xB = 0;
3130 if (file_version >= tpxv_IntermolecularBondeds)
3132 gmx_fio_do_gmx_bool(fio, mtop->bIntermolecularInteractions);
3133 if (mtop->bIntermolecularInteractions)
3137 snew(mtop->intermolecular_ilist, F_NRE);
3139 do_ilists(fio, mtop->intermolecular_ilist, bRead, file_version);
3144 mtop->bIntermolecularInteractions = FALSE;
3147 do_atomtypes (fio, &(mtop->atomtypes), bRead, file_version);
3149 if (file_version < 57)
3151 do_idef (fio, &mtop->ffparams, &mtop->moltype[0], bRead, file_version);
3152 mtop->natoms = mtop->moltype[0].atoms.nr;
3155 if (file_version >= 65)
3157 do_cmap(fio, &mtop->ffparams.cmap_grid, bRead);
3161 mtop->ffparams.cmap_grid.ngrid = 0;
3162 mtop->ffparams.cmap_grid.grid_spacing = 0;
3163 mtop->ffparams.cmap_grid.cmapdata = NULL;
3166 if (file_version >= 57)
3168 do_groups(fio, &mtop->groups, bRead, &(mtop->symtab), file_version);
3171 if (file_version < 57)
3173 do_block(fio, &mtop->moltype[0].cgs, bRead, file_version);
3174 do_block(fio, &mtop->mols, bRead, file_version);
3175 /* Add the posres coordinates to the molblock */
3176 add_posres_molblock(mtop);
3180 if (file_version >= 57)
3182 done_block(&mtop->mols);
3183 mtop->mols = mtop_mols(mtop);
3187 if (file_version < 51)
3189 /* Here used to be the shake blocks */
3190 do_blocka(fio, &dumb, bRead, file_version);
3203 close_symtab(&(mtop->symtab));
3207 /* If TopOnlyOK is TRUE then we can read even future versions
3208 * of tpx files, provided the file_generation hasn't changed.
3209 * If it is FALSE, we need the inputrecord too, and bail out
3210 * if the file is newer than the program.
3212 * The version and generation if the topology (see top of this file)
3213 * are returned in the two last arguments.
3215 * If possible, we will read the inputrec even when TopOnlyOK is TRUE.
3217 static void do_tpxheader(t_fileio *fio, gmx_bool bRead, t_tpxheader *tpx,
3218 gmx_bool TopOnlyOK, int *file_version,
3219 int *file_generation)
3222 char file_tag[STRLEN];
3229 /* XDR binary topology file */
3230 precision = sizeof(real);
3233 gmx_fio_do_string(fio, buf);
3234 if (std::strncmp(buf, "VERSION", 7))
3236 gmx_fatal(FARGS, "Can not read file %s,\n"
3237 " this file is from a GROMACS version which is older than 2.0\n"
3238 " Make a new one with grompp or use a gro or pdb file, if possible",
3239 gmx_fio_getname(fio));
3241 gmx_fio_do_int(fio, precision);
3242 bDouble = (precision == sizeof(double));
3243 if ((precision != sizeof(float)) && !bDouble)
3245 gmx_fatal(FARGS, "Unknown precision in file %s: real is %d bytes "
3246 "instead of %d or %d",
3247 gmx_fio_getname(fio), precision, sizeof(float), sizeof(double));
3249 gmx_fio_setprecision(fio, bDouble);
3250 fprintf(stderr, "Reading file %s, %s (%s precision)\n",
3251 gmx_fio_getname(fio), buf, bDouble ? "double" : "single");
3255 snprintf(buf, STRLEN, "VERSION %s", gmx_version());
3256 gmx_fio_write_string(fio, buf);
3257 bDouble = (precision == sizeof(double));
3258 gmx_fio_setprecision(fio, bDouble);
3259 gmx_fio_do_int(fio, precision);
3261 sprintf(file_tag, "%s", tpx_tag);
3262 fgen = tpx_generation;
3265 /* Check versions! */
3266 gmx_fio_do_int(fio, fver);
3268 /* This is for backward compatibility with development versions 77-79
3269 * where the tag was, mistakenly, placed before the generation,
3270 * which would cause a segv instead of a proper error message
3271 * when reading the topology only from tpx with <77 code.
3273 if (fver >= 77 && fver <= 79)
3275 gmx_fio_do_string(fio, file_tag);
3280 gmx_fio_do_int(fio, fgen);
3289 gmx_fio_do_string(fio, file_tag);
3295 /* Versions before 77 don't have the tag, set it to release */
3296 sprintf(file_tag, "%s", TPX_TAG_RELEASE);
3299 if (std::strcmp(file_tag, tpx_tag) != 0)
3301 fprintf(stderr, "Note: file tpx tag '%s', software tpx tag '%s'\n",
3304 /* We only support reading tpx files with the same tag as the code
3305 * or tpx files with the release tag and with lower version number.
3307 if (std::strcmp(file_tag, TPX_TAG_RELEASE) != 0 && fver < tpx_version)
3309 gmx_fatal(FARGS, "tpx tag/version mismatch: reading tpx file (%s) version %d, tag '%s' with program for tpx version %d, tag '%s'",
3310 gmx_fio_getname(fio), fver, file_tag,
3311 tpx_version, tpx_tag);
3316 if (file_version != NULL)
3318 *file_version = fver;
3320 if (file_generation != NULL)
3322 *file_generation = fgen;
3326 if ((fver <= tpx_incompatible_version) ||
3327 ((fver > tpx_version) && !TopOnlyOK) ||
3328 (fgen > tpx_generation) ||
3329 tpx_version == 80) /*80 was used by both 5.0-dev and 4.6-dev*/
3331 gmx_fatal(FARGS, "reading tpx file (%s) version %d with version %d program",
3332 gmx_fio_getname(fio), fver, tpx_version);
3335 gmx_fio_do_int(fio, tpx->natoms);
3338 gmx_fio_do_int(fio, tpx->ngtc);
3346 gmx_fio_do_int(fio, idum);
3347 gmx_fio_do_real(fio, rdum);
3351 gmx_fio_do_int(fio, tpx->fep_state);
3353 gmx_fio_do_real(fio, tpx->lambda);
3354 gmx_fio_do_int(fio, tpx->bIr);
3355 gmx_fio_do_int(fio, tpx->bTop);
3356 gmx_fio_do_int(fio, tpx->bX);
3357 gmx_fio_do_int(fio, tpx->bV);
3358 gmx_fio_do_int(fio, tpx->bF);
3359 gmx_fio_do_int(fio, tpx->bBox);
3361 if ((fgen > tpx_generation))
3363 /* This can only happen if TopOnlyOK=TRUE */
3368 static int do_tpx(t_fileio *fio, gmx_bool bRead,
3369 t_inputrec *ir, t_state *state, gmx_mtop_t *mtop,
3370 gmx_bool bXVallocated)
3376 int file_version, file_generation;
3379 gmx_bool bPeriodicMols;
3383 tpx.natoms = state->natoms;
3384 tpx.ngtc = state->ngtc;
3385 tpx.fep_state = state->fep_state;
3386 tpx.lambda = state->lambda[efptFEP];
3387 tpx.bIr = (ir != NULL);
3388 tpx.bTop = (mtop != NULL);
3389 tpx.bX = (state->x != NULL);
3390 tpx.bV = (state->v != NULL);
3395 TopOnlyOK = (ir == NULL);
3397 do_tpxheader(fio, bRead, &tpx, TopOnlyOK, &file_version, &file_generation);
3406 init_state(state, 0, tpx.ngtc, 0, 0, 0);
3407 state->natoms = tpx.natoms;
3408 state->nalloc = tpx.natoms;
3414 init_state(state, tpx.natoms, tpx.ngtc, 0, 0, 0);
3418 #define do_test(fio, b, p) if (bRead && (p != NULL) && !b) gmx_fatal(FARGS, "No %s in %s",#p, gmx_fio_getname(fio))
3420 do_test(fio, tpx.bBox, state->box);
3423 gmx_fio_ndo_rvec(fio, state->box, DIM);
3424 if (file_version >= 51)
3426 gmx_fio_ndo_rvec(fio, state->box_rel, DIM);
3430 /* We initialize box_rel after reading the inputrec */
3431 clear_mat(state->box_rel);
3433 if (file_version >= 28)
3435 gmx_fio_ndo_rvec(fio, state->boxv, DIM);
3436 if (file_version < 56)
3439 gmx_fio_ndo_rvec(fio, mdum, DIM);
3444 if (state->ngtc > 0 && file_version >= 28)
3447 snew(dumv, state->ngtc);
3448 if (file_version < 69)
3450 gmx_fio_ndo_real(fio, dumv, state->ngtc);
3452 /* These used to be the Berendsen tcoupl_lambda's */
3453 gmx_fio_ndo_real(fio, dumv, state->ngtc);
3457 /* Prior to tpx version 26, the inputrec was here.
3458 * I moved it to enable partial forward-compatibility
3459 * for analysis/viewer programs.
3461 if (file_version < 26)
3463 do_test(fio, tpx.bIr, ir);
3468 do_inputrec(fio, ir, bRead, file_version,
3469 mtop ? &mtop->ffparams.fudgeQQ : NULL);
3473 do_inputrec(fio, &dum_ir, bRead, file_version,
3474 mtop ? &mtop->ffparams.fudgeQQ : NULL);
3475 done_inputrec(&dum_ir);
3481 do_test(fio, tpx.bTop, mtop);
3486 do_mtop(fio, mtop, bRead, file_version);
3490 do_mtop(fio, &dum_top, bRead, file_version);
3491 done_mtop(&dum_top, TRUE);
3494 do_test(fio, tpx.bX, state->x);
3499 state->flags |= (1<<estX);
3501 gmx_fio_ndo_rvec(fio, state->x, state->natoms);
3504 do_test(fio, tpx.bV, state->v);
3509 state->flags |= (1<<estV);
3511 gmx_fio_ndo_rvec(fio, state->v, state->natoms);
3514 // No need to run do_test when the last argument is NULL
3518 snew(dummyForces, state->natoms);
3519 gmx_fio_ndo_rvec(fio, dummyForces, state->natoms);
3523 /* Starting with tpx version 26, we have the inputrec
3524 * at the end of the file, so we can ignore it
3525 * if the file is never than the software (but still the
3526 * same generation - see comments at the top of this file.
3531 bPeriodicMols = FALSE;
3532 if (file_version >= 26)
3534 do_test(fio, tpx.bIr, ir);
3537 if (file_version >= 53)
3539 /* Removed the pbc info from do_inputrec, since we always want it */
3543 bPeriodicMols = ir->bPeriodicMols;
3545 gmx_fio_do_int(fio, ePBC);
3546 gmx_fio_do_gmx_bool(fio, bPeriodicMols);
3548 if (file_generation <= tpx_generation && ir)
3550 do_inputrec(fio, ir, bRead, file_version, mtop ? &mtop->ffparams.fudgeQQ : NULL);
3551 if (file_version < 51)
3553 set_box_rel(ir, state);
3555 if (file_version < 53)
3558 bPeriodicMols = ir->bPeriodicMols;
3561 if (bRead && ir && file_version >= 53)
3563 /* We need to do this after do_inputrec, since that initializes ir */
3565 ir->bPeriodicMols = bPeriodicMols;
3574 if (state->ngtc == 0)
3576 /* Reading old version without tcoupl state data: set it */
3577 init_gtc_state(state, ir->opts.ngtc, 0, ir->opts.nhchainlength);
3579 if (tpx.bTop && mtop)
3581 if (file_version < 57)
3583 if (mtop->moltype[0].ilist[F_DISRES].nr > 0)
3585 ir->eDisre = edrSimple;
3589 ir->eDisre = edrNone;
3592 set_disres_npair(mtop);
3596 if (tpx.bTop && mtop)
3598 gmx_mtop_finalize(mtop);
3605 static t_fileio *open_tpx(const char *fn, const char *mode)
3607 return gmx_fio_open(fn, mode);
3610 static void close_tpx(t_fileio *fio)
3615 /************************************************************
3617 * The following routines are the exported ones
3619 ************************************************************/
3621 void read_tpxheader(const char *fn, t_tpxheader *tpx, gmx_bool TopOnlyOK,
3622 int *file_version, int *file_generation)
3626 fio = open_tpx(fn, "r");
3627 do_tpxheader(fio, TRUE, tpx, TopOnlyOK, file_version, file_generation);
3631 void write_tpx_state(const char *fn,
3632 t_inputrec *ir, t_state *state, gmx_mtop_t *mtop)
3636 fio = open_tpx(fn, "w");
3637 do_tpx(fio, FALSE, ir, state, mtop, FALSE);
3641 void read_tpx_state(const char *fn,
3642 t_inputrec *ir, t_state *state, gmx_mtop_t *mtop)
3646 fio = open_tpx(fn, "r");
3647 do_tpx(fio, TRUE, ir, state, mtop, FALSE);
3651 int read_tpx(const char *fn,
3652 t_inputrec *ir, matrix box, int *natoms,
3653 rvec *x, rvec *v, gmx_mtop_t *mtop)
3661 fio = open_tpx(fn, "r");
3662 ePBC = do_tpx(fio, TRUE, ir, &state, mtop, TRUE);
3664 *natoms = state.natoms;
3667 copy_mat(state.box, box);
3676 int read_tpx_top(const char *fn,
3677 t_inputrec *ir, matrix box, int *natoms,
3678 rvec *x, rvec *v, t_topology *top)
3683 ePBC = read_tpx(fn, ir, box, natoms, x, v, &mtop);
3685 *top = gmx_mtop_t_to_t_topology(&mtop);
3690 gmx_bool fn2bTPX(const char *file)
3692 return (efTPR == fn2ftp(file));
3695 void pr_tpxheader(FILE *fp, int indent, const char *title, const t_tpxheader *sh)
3697 if (available(fp, sh, indent, title))
3699 indent = pr_title(fp, indent, title);
3700 pr_indent(fp, indent);
3701 fprintf(fp, "bIr = %spresent\n", sh->bIr ? "" : "not ");
3702 pr_indent(fp, indent);
3703 fprintf(fp, "bBox = %spresent\n", sh->bBox ? "" : "not ");
3704 pr_indent(fp, indent);
3705 fprintf(fp, "bTop = %spresent\n", sh->bTop ? "" : "not ");
3706 pr_indent(fp, indent);
3707 fprintf(fp, "bX = %spresent\n", sh->bX ? "" : "not ");
3708 pr_indent(fp, indent);
3709 fprintf(fp, "bV = %spresent\n", sh->bV ? "" : "not ");
3710 pr_indent(fp, indent);
3711 fprintf(fp, "bF = %spresent\n", sh->bF ? "" : "not ");
3713 pr_indent(fp, indent);
3714 fprintf(fp, "natoms = %d\n", sh->natoms);
3715 pr_indent(fp, indent);
3716 fprintf(fp, "lambda = %e\n", sh->lambda);