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.
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/topology/atoms.h"
49 #include "gromacs/topology/mtop_util.h"
50 #include "gromacs/topology/symtab.h"
51 #include "gromacs/topology/topology.h"
52 #include "gromacs/trajectory/trajectoryframe.h"
53 #include "gromacs/utility/coolstuff.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/futil.h"
57 #include "gromacs/utility/smalloc.h"
59 static void get_coordnum_fp(FILE* in, char* title, int* natoms)
61 char line[STRLEN + 1];
63 fgets2(title, STRLEN, in);
64 fgets2(line, STRLEN, in);
65 if (sscanf(line, "%d", natoms) != 1)
67 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
71 void get_coordnum(const char* infile, int* natoms)
76 in = gmx_fio_fopen(infile, "r");
77 get_coordnum_fp(in, title, natoms);
81 /* Note that the .gro reading routine still support variable precision
82 * for backward compatibility with old .gro files.
83 * We have removed writing of variable precision to avoid compatibility
84 * issues with other software packages.
86 static gmx_bool get_w_conf(FILE* in,
97 char resname[6], oldresname[6];
98 char line[STRLEN + 1], *ptr;
100 double x1, y1, z1, x2, y2, z2;
102 int natoms, i, m, resnr, newres, oldres, ddist, c;
103 gmx_bool bFirst, bVel, oldResFirst;
111 /* Read the title and number of atoms */
112 get_coordnum_fp(in, title, &natoms);
114 if (natoms > atoms->nr)
116 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)", natoms, atoms->nr);
118 else if (natoms < atoms->nr)
121 "Warning: gro file contains less atoms (%d) than expected"
126 atoms->haveMass = FALSE;
127 atoms->haveCharge = FALSE;
128 atoms->haveType = FALSE;
129 atoms->haveBState = FALSE;
130 atoms->havePdbInfo = FALSE;
137 oldresname[0] = '\0';
139 /* just pray the arrays are big enough */
140 for (i = 0; (i < natoms); i++)
142 if ((fgets2(line, STRLEN, in)) == nullptr)
144 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d", infile, i + 2);
146 if (strlen(line) < 39)
148 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i + 1, line);
151 /* determine read precision from distance between periods
156 p1 = strchr(line, '.');
159 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
161 p2 = strchr(&p1[1], '.');
164 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
169 p3 = strchr(&p2[1], '.');
172 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
175 if (p3 - p2 != ddist)
178 "The spacing of the decimal points in file %s is not consistent for x, y "
185 memcpy(name, line, 5);
187 sscanf(name, "%d", &resnr);
188 sscanf(line + 5, "%5s", resname);
190 if (!oldResFirst || oldres != resnr || strncmp(resname, oldresname, sizeof(resname)) != 0)
195 if (newres >= natoms)
197 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)", infile, natoms);
199 atoms->atom[i].resind = newres;
200 t_atoms_set_resinfo(atoms, i, symtab, resname, resnr, ' ', 0, ' ');
204 atoms->atom[i].resind = newres;
208 std::memcpy(name, line + 10, 5);
209 atoms->atomname[i] = put_symtab(symtab, name);
211 /* Copy resname to oldresname after we are done with the sanity check above */
212 std::strncpy(oldresname, resname, sizeof(oldresname));
214 /* eventueel controle atomnumber met i+1 */
216 /* coordinates (start after residue data) */
218 /* Read fixed format */
219 for (m = 0; m < DIM; m++)
221 for (c = 0; (c < ddist && ptr[0]); c++)
227 if (sscanf(buf, "%lf %lf", &x1, &x2) != 1)
230 "Something is wrong in the coordinate formatting of file %s. Note that "
231 "gro is fixed format (see the manual)",
240 /* velocities (start after residues and coordinates) */
243 /* Read fixed format */
244 for (m = 0; m < DIM; m++)
246 for (c = 0; (c < ddist && ptr[0]); c++)
252 if (sscanf(buf, "%lf", &x1) != 1)
264 atoms->nres = newres + 1;
267 fgets2(line, STRLEN, in);
268 if (sscanf(line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
270 gmx_warning("Bad box in file %s", infile);
272 /* Generate a cubic box */
273 for (m = 0; (m < DIM); m++)
275 xmin[m] = xmax[m] = x[0][m];
277 for (i = 1; (i < atoms->nr); i++)
279 for (m = 0; (m < DIM); m++)
281 xmin[m] = std::min(xmin[m], x[i][m]);
282 xmax[m] = std::max(xmax[m], x[i][m]);
285 for (i = 0; i < DIM; i++)
287 for (m = 0; m < DIM; m++)
292 for (m = 0; (m < DIM); m++)
294 box[m][m] = (xmax[m] - xmin[m]);
296 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n", box[XX][XX], box[YY][YY],
301 /* We found the first three values, the diagonal elements */
305 if (sscanf(line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf", &x1, &y1, &z1, &x2, &y2, &z2) != 6)
307 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
320 void gmx_gro_read_conf(const char* infile, t_symtab* symtab, char** name, t_atoms* atoms, rvec x[], rvec* v, matrix box)
322 FILE* in = gmx_fio_fopen(infile, "r");
325 get_w_conf(in, infile, title, symtab, atoms, &ndec, x, v, box);
328 *name = gmx_strdup(title);
333 static gmx_bool gmx_one_before_eof(FILE* fp)
336 gmx_bool beof = fread(data, 1, 1, fp) != 1;
340 gmx_fseek(fp, -1, SEEK_CUR);
345 gmx_bool gro_next_x_or_v(FILE* status, t_trxframe* fr)
349 char title[STRLEN], *p;
353 if (gmx_one_before_eof(status))
358 open_symtab(&symtab);
359 atoms.nr = fr->natoms;
360 snew(atoms.atom, fr->natoms);
361 atoms.nres = fr->natoms;
362 snew(atoms.resinfo, fr->natoms);
363 snew(atoms.atomname, fr->natoms);
365 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
368 /* prec = 10^ndec: */
369 for (i = 0; i < ndec; i++)
377 sfree(atoms.resinfo);
378 sfree(atoms.atomname);
379 done_symtab(&symtab);
381 if ((p = strstr(title, "t=")) != nullptr)
384 if (sscanf(p, "%lf", &tt) == 1)
396 if ((p = std::strstr(title, "step=")) != nullptr)
399 fr->step = 0; // Default value if fr-bStep is false
400 fr->bStep = (sscanf(p, "%" SCNd64, &fr->step) == 1);
403 if (atoms.nr != fr->natoms)
406 "Number of atoms in gro frame (%d) doesn't match the number in the previous "
408 atoms.nr, fr->natoms);
414 int gro_first_x_or_v(FILE* status, t_trxframe* fr)
419 fprintf(stderr, "Reading frames from gro file");
420 get_coordnum_fp(status, title, &fr->natoms);
422 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
425 gmx_file("No coordinates in gro file");
428 snew(fr->x, fr->natoms);
429 snew(fr->v, fr->natoms);
430 gro_next_x_or_v(status, fr);
435 static const char* get_hconf_format(bool haveVelocities)
439 return "%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f\n";
443 return "%8.3f%8.3f%8.3f\n";
447 static void write_hconf_box(FILE* out, const matrix box)
449 if ((box[XX][YY] != 0.0F) || (box[XX][ZZ] != 0.0F) || (box[YY][XX] != 0.0F)
450 || (box[YY][ZZ] != 0.0F) || (box[ZZ][XX] != 0.0F) || (box[ZZ][YY] != 0.0F))
452 fprintf(out, "%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f\n", box[XX][XX],
453 box[YY][YY], box[ZZ][ZZ], box[XX][YY], box[XX][ZZ], box[YY][XX], box[YY][ZZ],
454 box[ZZ][XX], box[ZZ][YY]);
458 fprintf(out, "%10.5f%10.5f%10.5f\n", box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
462 void write_hconf_indexed_p(FILE* out,
464 const t_atoms* atoms,
471 int ai, i, resind, resnr;
473 fprintf(out, "%s\n", (title && title[0]) ? title : gmx::bromacs().c_str());
474 fprintf(out, "%5d\n", nx);
476 const char* format = get_hconf_format(v != nullptr);
478 for (i = 0; (i < nx); i++)
482 resind = atoms->atom[ai].resind;
484 if (resind < atoms->nres)
486 resnm = *atoms->resinfo[resind].name;
487 resnr = atoms->resinfo[resind].nr;
498 nm = *atoms->atomname[ai];
505 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr % 100000, resnm.c_str(), nm.c_str(), (ai + 1) % 100000);
506 /* next fprintf uses built format string */
509 fprintf(out, format, x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
513 fprintf(out, format, x[ai][XX], x[ai][YY], x[ai][ZZ]);
517 write_hconf_box(out, box);
522 void write_hconf_mtop(FILE* out, const char* title, const gmx_mtop_t* mtop, const rvec* x, const rvec* v, const matrix box)
524 fprintf(out, "%s\n", (title && title[0]) ? title : gmx::bromacs().c_str());
525 fprintf(out, "%5d\n", mtop->natoms);
527 const char* format = get_hconf_format(v != nullptr);
529 for (const AtomProxy atomP : AtomRange(*mtop))
531 int i = atomP.globalAtomNumber();
532 int residueNumber = atomP.residueNumber();
533 const char* atomName = atomP.atomName();
534 const char* residueName = atomP.residueName();
536 fprintf(out, "%5d%-5.5s%5.5s%5d", residueNumber % 100000, residueName, atomName, (i + 1) % 100000);
537 /* next fprintf uses built format string */
540 fprintf(out, format, x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
544 fprintf(out, format, x[i][XX], x[i][YY], x[i][ZZ]);
548 write_hconf_box(out, box);
553 void write_hconf_p(FILE* out, const char* title, const t_atoms* atoms, const rvec* x, const rvec* v, const matrix box)
559 for (i = 0; (i < atoms->nr); i++)
563 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, x, v, box);
567 void write_conf_p(const char* outfile,
569 const t_atoms* atoms,
576 out = gmx_fio_fopen(outfile, "w");
577 write_hconf_p(out, title, atoms, x, v, box);