2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, 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.
44 #include "gromacs/fileio/trxio.h"
45 #include "gromacs/math/vec.h"
46 #include "gromacs/topology/atoms.h"
47 #include "gromacs/topology/symtab.h"
48 #include "gromacs/trajectory/trajectoryframe.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/smalloc.h"
55 static int read_g96_pos(char line[], t_symtab *symtab,
56 FILE *fp, const char *infile,
61 int nwanted, natoms, atnr, resnr = 0, oldres, newres, shift;
62 char anm[STRLEN], resnm[STRLEN];
71 atoms->haveMass = FALSE;
72 atoms->haveCharge = FALSE;
73 atoms->haveType = FALSE;
74 atoms->haveBState = FALSE;
75 atoms->havePdbInfo = FALSE;
91 oldres = -666; /* Unlikely number for the first residue! */
93 while (!bEnd && fgets2(line, STRLEN, fp))
95 bEnd = (std::strncmp(line, "END", 3) == 0);
96 if (!bEnd && (line[0] != '#'))
98 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
100 gmx_fatal(FARGS, "Did not find 3 coordinates for atom %d in %s\n",
103 if ((nwanted != -1) && (natoms >= nwanted))
106 "Found more coordinates (%d) in %s than expected %d\n",
107 natoms, infile, nwanted);
112 (sscanf(line, "%5d%c%5s%c%5s%7d", &resnr, &c1, resnm, &c2, anm, &atnr)
122 strncpy(resnm, "???", sizeof(resnm)-1);
124 strncpy(anm, "???", sizeof(anm)-1);
126 atoms->atomname[natoms] = put_symtab(symtab, anm);
131 if (newres >= atoms->nr)
133 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
136 atoms->atom[natoms].resind = newres;
137 if (newres+1 > atoms->nres)
139 atoms->nres = newres+1;
141 t_atoms_set_resinfo(atoms, natoms, symtab, resnm, resnr, ' ', 0, ' ');
145 atoms->atom[natoms].resind = newres;
150 fr->x[natoms][0] = db1;
151 fr->x[natoms][1] = db2;
152 fr->x[natoms][2] = db3;
157 if ((nwanted != -1) && natoms != nwanted)
160 "Warning: found less coordinates (%d) in %s than expected %d\n",
161 natoms, infile, nwanted);
170 static int read_g96_vel(char line[], FILE *fp, const char *infile,
174 int nwanted, natoms = -1, shift;
175 double db1, db2, db3;
177 nwanted = fr->natoms;
181 if (strcmp(line, "VELOCITYRED") == 0)
191 while (!bEnd && fgets2(line, STRLEN, fp))
193 bEnd = (strncmp(line, "END", 3) == 0);
194 if (!bEnd && (line[0] != '#'))
196 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
198 gmx_fatal(FARGS, "Did not find 3 velocities for atom %d in %s",
201 if ((nwanted != -1) && (natoms >= nwanted))
203 gmx_fatal(FARGS, "Found more velocities (%d) in %s than expected %d\n",
204 natoms, infile, nwanted);
208 fr->v[natoms][0] = db1;
209 fr->v[natoms][1] = db2;
210 fr->v[natoms][2] = db3;
215 if ((nwanted != -1) && (natoms != nwanted))
218 "Warning: found less velocities (%d) in %s than expected %d\n",
219 natoms, infile, nwanted);
226 int read_g96_conf(FILE *fp, const char *infile, t_trxframe *fr,
227 t_symtab *symtab, char *line)
229 gmx_bool bAtStart, bTime, bAtoms, bPos, bVel, bBox, bEnd, bFinished;
231 double db1, db2, db3, db4, db5, db6, db7, db8, db9;
233 bAtStart = (ftell(fp) == 0);
235 clear_trxframe(fr, FALSE);
241 while (!fr->bTitle && fgets2(line, STRLEN, fp))
243 fr->bTitle = (std::strcmp(line, "TITLE") == 0);
245 if (fr->title == nullptr)
247 fgets2(line, STRLEN, fp);
248 fr->title = gmx_strdup(line);
251 while (!bEnd && fgets2(line, STRLEN, fp))
253 bEnd = (std::strcmp(line, "END") == 0);
255 fgets2(line, STRLEN, fp);
258 /* Do not get a line if we are not at the start of the file, *
259 * because without a parameter file we don't know what is in *
260 * the trajectory and we have already read the line in the *
261 * previous call (VERY DIRTY). */
265 bTime = (std::strcmp(line, "TIMESTEP") == 0);
266 bAtoms = (std::strcmp(line, "POSITION") == 0);
267 bPos = (bAtoms || (strcmp(line, "POSITIONRED") == 0));
268 bVel = (std::strncmp(line, "VELOCITY", 8) == 0);
269 bBox = (std::strcmp(line, "BOX") == 0);
272 if (!fr->bTime && !fr->bX)
278 bFinished = (fgets2(line, STRLEN, fp) == nullptr);
280 while (!bFinished && (line[0] == '#'));
281 sscanf(line, "%15" GMX_SCNd64 "%15lf", &(fr->step), &db1);
295 natoms = read_g96_pos(line, symtab, fp, infile, fr);
305 natoms = read_g96_vel(line, fp, infile, fr);
312 while (!bEnd && fgets2(line, STRLEN, fp))
314 bEnd = (strncmp(line, "END", 3) == 0);
315 if (!bEnd && (line[0] != '#'))
317 nbp = sscanf(line, "%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf",
318 &db1, &db2, &db3, &db4, &db5, &db6, &db7, &db8, &db9);
321 gmx_fatal(FARGS, "Found a BOX line, but no box in %s", infile);
323 fr->box[XX][XX] = db1;
324 fr->box[YY][YY] = db2;
325 fr->box[ZZ][ZZ] = db3;
328 fr->box[XX][YY] = db4;
329 fr->box[XX][ZZ] = db5;
330 fr->box[YY][XX] = db6;
331 fr->box[YY][ZZ] = db7;
332 fr->box[ZZ][XX] = db8;
333 fr->box[ZZ][YY] = db9;
340 while (!bFinished && fgets2(line, STRLEN, fp));
347 void write_g96_conf(FILE *out, const t_trxframe *fr,
348 int nindex, const int *index)
366 fprintf(out, "TITLE\n%s\nEND\n", fr->title);
368 if (fr->bStep || fr->bTime)
370 /* Officially the time format is %15.9, which is not enough for 10 ns */
371 fprintf(out, "TIMESTEP\n%15" GMX_PRId64 "%15.6f\nEND\n", fr->step, fr->time);
377 fprintf(out, "POSITION\n");
378 for (i = 0; i < nout; i++)
388 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
389 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
390 *atoms->resinfo[atoms->atom[a].resind].name,
391 *atoms->atomname[a], (i+1) % 10000000,
392 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
397 fprintf(out, "POSITIONRED\n");
398 for (i = 0; i < nout; i++)
408 fprintf(out, "%15.9f%15.9f%15.9f\n",
409 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
412 fprintf(out, "END\n");
418 fprintf(out, "VELOCITY\n");
419 for (i = 0; i < nout; i++)
429 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
430 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
431 *atoms->resinfo[atoms->atom[a].resind].name,
432 *atoms->atomname[a], (i+1) % 10000000,
433 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
438 fprintf(out, "VELOCITYRED\n");
439 for (i = 0; i < nout; i++)
449 fprintf(out, "%15.9f%15.9f%15.9f\n",
450 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
453 fprintf(out, "END\n");
457 fprintf(out, "BOX\n");
458 fprintf(out, "%15.9f%15.9f%15.9f",
459 fr->box[XX][XX], fr->box[YY][YY], fr->box[ZZ][ZZ]);
460 if (fr->box[XX][YY] || fr->box[XX][ZZ] || fr->box[YY][XX] ||
461 fr->box[YY][ZZ] || fr->box[ZZ][XX] || fr->box[ZZ][YY])
463 fprintf(out, "%15.9f%15.9f%15.9f%15.9f%15.9f%15.9f",
464 fr->box[XX][YY], fr->box[XX][ZZ], fr->box[YY][XX],
465 fr->box[YY][ZZ], fr->box[ZZ][XX], fr->box[ZZ][YY]);
468 fprintf(out, "END\n");