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.
49 #include "gromacs/fileio/filenm.h"
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/fileio/pdbio.h"
52 #include "gromacs/fileio/tpxio.h"
53 #include "gromacs/fileio/trx.h"
54 #include "gromacs/fileio/trxio.h"
55 #include "gromacs/fileio/xdrf.h"
56 #include "gromacs/legacyheaders/copyrite.h"
57 #include "gromacs/legacyheaders/macros.h"
58 #include "gromacs/legacyheaders/typedefs.h"
59 #include "gromacs/math/vec.h"
60 #include "gromacs/pbcutil/pbc.h"
61 #include "gromacs/topology/mtop_util.h"
62 #include "gromacs/topology/symtab.h"
63 #include "gromacs/topology/topology.h"
64 #include "gromacs/utility/cstringutil.h"
65 #include "gromacs/utility/fatalerror.h"
66 #include "gromacs/utility/futil.h"
67 #include "gromacs/utility/smalloc.h"
71 static int read_g96_pos(char line[], t_symtab *symtab,
72 FILE *fp, const char *infile,
77 int nwanted, natoms, atnr, resnr = 0, oldres, newres, shift;
78 char anm[STRLEN], resnm[STRLEN];
99 oldres = -666; /* Unlikely number for the first residue! */
101 while (!bEnd && fgets2(line, STRLEN, fp))
103 bEnd = (std::strncmp(line, "END", 3) == 0);
104 if (!bEnd && (line[0] != '#'))
106 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
108 gmx_fatal(FARGS, "Did not find 3 coordinates for atom %d in %s\n",
111 if ((nwanted != -1) && (natoms >= nwanted))
114 "Found more coordinates (%d) in %s than expected %d\n",
115 natoms, infile, nwanted);
120 (sscanf(line, "%5d%c%5s%c%5s%7d", &resnr, &c1, resnm, &c2, anm, &atnr)
130 strncpy(resnm, "???", sizeof(resnm)-1);
132 strncpy(anm, "???", sizeof(anm)-1);
134 atoms->atomname[natoms] = put_symtab(symtab, anm);
139 if (newres >= atoms->nr)
141 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
144 atoms->atom[natoms].resind = newres;
145 if (newres+1 > atoms->nres)
147 atoms->nres = newres+1;
149 t_atoms_set_resinfo(atoms, natoms, symtab, resnm, resnr, ' ', 0, ' ');
153 atoms->atom[natoms].resind = newres;
158 fr->x[natoms][0] = db1;
159 fr->x[natoms][1] = db2;
160 fr->x[natoms][2] = db3;
165 if ((nwanted != -1) && natoms != nwanted)
168 "Warning: found less coordinates (%d) in %s than expected %d\n",
169 natoms, infile, nwanted);
178 static int read_g96_vel(char line[], FILE *fp, const char *infile,
182 int nwanted, natoms = -1, shift;
183 double db1, db2, db3;
185 nwanted = fr->natoms;
189 if (strcmp(line, "VELOCITYRED") == 0)
199 while (!bEnd && fgets2(line, STRLEN, fp))
201 bEnd = (strncmp(line, "END", 3) == 0);
202 if (!bEnd && (line[0] != '#'))
204 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
206 gmx_fatal(FARGS, "Did not find 3 velocities for atom %d in %s",
209 if ((nwanted != -1) && (natoms >= nwanted))
211 gmx_fatal(FARGS, "Found more velocities (%d) in %s than expected %d\n",
212 natoms, infile, nwanted);
216 fr->v[natoms][0] = db1;
217 fr->v[natoms][1] = db2;
218 fr->v[natoms][2] = db3;
223 if ((nwanted != -1) && (natoms != nwanted))
226 "Warning: found less velocities (%d) in %s than expected %d\n",
227 natoms, infile, nwanted);
234 int read_g96_conf(FILE *fp, const char *infile, t_trxframe *fr, char *line)
236 t_symtab *symtab = NULL;
237 gmx_bool bAtStart, bTime, bAtoms, bPos, bVel, bBox, bEnd, bFinished;
239 double db1, db2, db3, db4, db5, db6, db7, db8, db9;
241 bAtStart = (ftell(fp) == 0);
243 clear_trxframe(fr, FALSE);
255 while (!fr->bTitle && fgets2(line, STRLEN, fp))
257 fr->bTitle = (std::strcmp(line, "TITLE") == 0);
259 if (fr->title == NULL)
261 fgets2(line, STRLEN, fp);
262 fr->title = gmx_strdup(line);
265 while (!bEnd && fgets2(line, STRLEN, fp))
267 bEnd = (std::strcmp(line, "END") == 0);
269 fgets2(line, STRLEN, fp);
272 /* Do not get a line if we are not at the start of the file, *
273 * because without a parameter file we don't know what is in *
274 * the trajectory and we have already read the line in the *
275 * previous call (VERY DIRTY). */
279 bTime = (std::strcmp(line, "TIMESTEP") == 0);
280 bAtoms = (std::strcmp(line, "POSITION") == 0);
281 bPos = (bAtoms || (strcmp(line, "POSITIONRED") == 0));
282 bVel = (std::strncmp(line, "VELOCITY", 8) == 0);
283 bBox = (std::strcmp(line, "BOX") == 0);
286 if (!fr->bTime && !fr->bX)
292 bFinished = (fgets2(line, STRLEN, fp) == NULL);
294 while (!bFinished && (line[0] == '#'));
295 sscanf(line, "%15d%15lf", &(fr->step), &db1);
309 natoms = read_g96_pos(line, symtab, fp, infile, fr);
319 natoms = read_g96_vel(line, fp, infile, fr);
326 while (!bEnd && fgets2(line, STRLEN, fp))
328 bEnd = (strncmp(line, "END", 3) == 0);
329 if (!bEnd && (line[0] != '#'))
331 nbp = sscanf(line, "%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf",
332 &db1, &db2, &db3, &db4, &db5, &db6, &db7, &db8, &db9);
335 gmx_fatal(FARGS, "Found a BOX line, but no box in %s", infile);
337 fr->box[XX][XX] = db1;
338 fr->box[YY][YY] = db2;
339 fr->box[ZZ][ZZ] = db3;
342 fr->box[XX][YY] = db4;
343 fr->box[XX][ZZ] = db5;
344 fr->box[YY][XX] = db6;
345 fr->box[YY][ZZ] = db7;
346 fr->box[ZZ][XX] = db8;
347 fr->box[ZZ][YY] = db9;
354 while (!bFinished && fgets2(line, STRLEN, fp));
363 void write_g96_conf(FILE *out, t_trxframe *fr,
364 int nindex, const atom_id *index)
382 fprintf(out, "TITLE\n%s\nEND\n", fr->title);
384 if (fr->bStep || fr->bTime)
386 /* Officially the time format is %15.9, which is not enough for 10 ns */
387 fprintf(out, "TIMESTEP\n%15d%15.6f\nEND\n", fr->step, fr->time);
393 fprintf(out, "POSITION\n");
394 for (i = 0; i < nout; i++)
404 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
405 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
406 *atoms->resinfo[atoms->atom[a].resind].name,
407 *atoms->atomname[a], (i+1) % 10000000,
408 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
413 fprintf(out, "POSITIONRED\n");
414 for (i = 0; i < nout; i++)
424 fprintf(out, "%15.9f%15.9f%15.9f\n",
425 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
428 fprintf(out, "END\n");
434 fprintf(out, "VELOCITY\n");
435 for (i = 0; i < nout; i++)
445 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
446 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
447 *atoms->resinfo[atoms->atom[a].resind].name,
448 *atoms->atomname[a], (i+1) % 10000000,
449 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
454 fprintf(out, "VELOCITYRED\n");
455 for (i = 0; i < nout; i++)
465 fprintf(out, "%15.9f%15.9f%15.9f\n",
466 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
469 fprintf(out, "END\n");
473 fprintf(out, "BOX\n");
474 fprintf(out, "%15.9f%15.9f%15.9f",
475 fr->box[XX][XX], fr->box[YY][YY], fr->box[ZZ][ZZ]);
476 if (fr->box[XX][YY] || fr->box[XX][ZZ] || fr->box[YY][XX] ||
477 fr->box[YY][ZZ] || fr->box[ZZ][XX] || fr->box[ZZ][YY])
479 fprintf(out, "%15.9f%15.9f%15.9f%15.9f%15.9f%15.9f",
480 fr->box[XX][YY], fr->box[XX][ZZ], fr->box[YY][XX],
481 fr->box[YY][ZZ], fr->box[ZZ][XX], fr->box[ZZ][YY]);
484 fprintf(out, "END\n");
488 static int get_espresso_word(FILE *fp, char word[])
500 if (i == ' ' || i == '\n' || i == '\t')
525 word[nc++] = (char)i;
529 while (i != EOF && ret == 0);
533 /* printf("'%s'\n",word); */
538 static int check_open_parenthesis(FILE *fp, int r,
539 const char *infile, const char *keyword)
551 r = get_espresso_word(fp, word);
558 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'",
566 static int check_close_parenthesis(FILE *fp, int r,
567 const char *infile, const char *keyword)
579 r = get_espresso_word(fp, word);
586 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'",
595 espID, espPOS, espTYPE, espQ, espV, espF, espMOLECULE, espNR
597 const char *esp_prop[espNR] = {
598 "id", "pos", "type", "q", "v", "f",
602 static void read_espresso_conf(const char *infile,
603 t_atoms *atoms, rvec x[], rvec *v, matrix box)
605 t_symtab *symtab = NULL;
607 char word[STRLEN], buf[STRLEN];
608 int level, r, nprop, p, i, m, molnr;
611 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
621 fp = gmx_fio_fopen(infile, "r");
623 bFoundParticles = FALSE;
624 bFoundVariable = FALSE;
627 while ((r = get_espresso_word(fp, word)))
629 if (level == 1 && std::strcmp(word, "particles") == 0 && !bFoundParticles)
631 bFoundParticles = TRUE;
632 level += check_open_parenthesis(fp, r, infile, "particles");
634 while (level == 2 && (r = get_espresso_word(fp, word)))
637 for (p = 0; p < espNR; p++)
639 if (strcmp(word, esp_prop[p]) == 0)
643 /* printf(" prop[%d] = %s\n",nprop-1,esp_prop[prop[nprop-1]]); */
646 if (!bFoundProp && word[0] != '}')
648 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
650 if (bFoundProp && p == espMOLECULE)
661 while (level > 0 && (r = get_espresso_word(fp, word)))
673 for (p = 0; p < nprop; p++)
678 r = get_espresso_word(fp, word);
682 for (m = 0; m < 3; m++)
684 r = get_espresso_word(fp, word);
685 sscanf(word, "%lf", &d);
690 r = get_espresso_word(fp, word);
691 atoms->atom[i].type = std::strtol(word, NULL, 10);
694 r = get_espresso_word(fp, word);
695 sscanf(word, "%lf", &d);
696 atoms->atom[i].q = d;
699 for (m = 0; m < 3; m++)
701 r = get_espresso_word(fp, word);
702 sscanf(word, "%lf", &d);
707 for (m = 0; m < 3; m++)
709 r = get_espresso_word(fp, word);
714 r = get_espresso_word(fp, word);
715 molnr = std::strtol(word, NULL, 10);
717 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
719 atoms->atom[i].resind =
720 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
721 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
722 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
723 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
724 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
728 atoms->atom[i].resind = atoms->atom[i-1].resind;
733 /* Generate an atom name from the particle type */
734 sprintf(buf, "T%d", atoms->atom[i].type);
735 atoms->atomname[i] = put_symtab(symtab, buf);
738 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
740 atoms->resinfo[atoms->atom[i].resind].name =
741 put_symtab(symtab, "MOL");
746 /* Residue number is the atom number */
747 atoms->atom[i].resind = i;
748 /* Generate an residue name from the particle type */
749 if (atoms->atom[i].type < 26)
751 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
755 sprintf(buf, "T%c%c",
756 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
758 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
768 atoms->nres = atoms->nr;
772 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
775 else if (level == 1 && std::strcmp(word, "variable") == 0 && !bFoundVariable)
777 bFoundVariable = TRUE;
778 level += check_open_parenthesis(fp, r, infile, "variable");
779 while (level == 2 && (r = get_espresso_word(fp, word)))
781 if (level == 2 && std::strcmp(word, "box_l") == 0)
783 for (m = 0; m < 3; m++)
785 r = get_espresso_word(fp, word);
786 sscanf(word, "%lf", &d);
789 level += check_close_parenthesis(fp, r, infile, "box_l");
803 if (!bFoundParticles)
805 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
812 static int get_espresso_coordnum(const char *infile)
816 int natoms, level, r;
817 gmx_bool bFoundParticles;
821 fp = gmx_fio_fopen(infile, "r");
823 bFoundParticles = FALSE;
825 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
827 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
829 bFoundParticles = TRUE;
830 level += check_open_parenthesis(fp, r, infile, "particles");
831 while (level > 0 && (r = get_espresso_word(fp, word)))
856 if (!bFoundParticles)
858 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
867 static void write_espresso_conf_indexed(FILE *out, const char *title,
868 t_atoms *atoms, int nx, atom_id *index,
869 rvec *x, rvec *v, matrix box)
873 fprintf(out, "# %s\n", title);
876 gmx_warning("The Espresso format does not support triclinic unit-cells");
878 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
880 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
881 for (i = 0; i < nx; i++)
891 fprintf(out, "\t{%d %f %f %f %d %g",
892 j, x[j][XX], x[j][YY], x[j][ZZ],
893 atoms->atom[j].type, atoms->atom[j].q);
896 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
903 static void get_coordnum_fp (FILE *in, char *title, int *natoms)
907 fgets2 (title, STRLEN, in);
908 fgets2 (line, STRLEN, in);
909 if (sscanf (line, "%d", natoms) != 1)
911 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
915 static void get_coordnum (const char *infile, int *natoms)
920 in = gmx_fio_fopen(infile, "r");
921 get_coordnum_fp(in, title, natoms);
925 static gmx_bool get_w_conf(FILE *in, const char *infile, char *title,
926 t_symtab *symtab, t_atoms *atoms, int *ndec,
927 rvec x[], rvec *v, matrix box)
930 char resname[6], oldresname[6];
931 char line[STRLEN+1], *ptr;
933 double x1, y1, z1, x2, y2, z2;
935 int natoms, i, m, resnr, newres, oldres, ddist, c;
936 gmx_bool bFirst, bVel;
940 oldres = NOTSET; /* Unlikely number for the first residue! */
943 /* Read the title and number of atoms */
944 get_coordnum_fp(in, title, &natoms);
946 if (natoms > atoms->nr)
948 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)",
951 else if (natoms < atoms->nr)
953 fprintf(stderr, "Warning: gro file contains less atoms (%d) than expected"
954 " (%d)\n", natoms, atoms->nr);
962 oldresname[0] = '\0';
964 /* just pray the arrays are big enough */
965 for (i = 0; (i < natoms); i++)
967 if ((fgets2 (line, STRLEN, in)) == NULL)
969 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d",
972 if (strlen(line) < 39)
974 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i+1, line);
977 /* determine read precision from distance between periods
982 p1 = strchr(line, '.');
985 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
987 p2 = strchr(&p1[1], '.');
990 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
995 p3 = strchr(&p2[1], '.');
998 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
1001 if (p3 - p2 != ddist)
1003 gmx_fatal(FARGS, "The spacing of the decimal points in file %s is not consistent for x, y and z", infile);
1008 memcpy(name, line, 5);
1010 sscanf(name, "%d", &resnr);
1011 sscanf(line+5, "%5s", resname);
1013 if (resnr != oldres || strncmp(resname, oldresname, sizeof(resname)))
1017 if (newres >= natoms)
1019 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
1022 atoms->atom[i].resind = newres;
1023 t_atoms_set_resinfo(atoms, i, symtab, resname, resnr, ' ', 0, ' ');
1027 atoms->atom[i].resind = newres;
1031 std::memcpy(name, line+10, 5);
1032 atoms->atomname[i] = put_symtab(symtab, name);
1034 /* Copy resname to oldresname after we are done with the sanity check above */
1035 std::strncpy(oldresname, resname, sizeof(oldresname));
1037 /* eventueel controle atomnumber met i+1 */
1039 /* coordinates (start after residue data) */
1041 /* Read fixed format */
1042 for (m = 0; m < DIM; m++)
1044 for (c = 0; (c < ddist && ptr[0]); c++)
1050 if (sscanf (buf, "%lf %lf", &x1, &x2) != 1)
1052 gmx_fatal(FARGS, "Something is wrong in the coordinate formatting of file %s. Note that gro is fixed format (see the manual)", infile);
1060 /* velocities (start after residues and coordinates) */
1063 /* Read fixed format */
1064 for (m = 0; m < DIM; m++)
1066 for (c = 0; (c < ddist && ptr[0]); c++)
1072 if (sscanf (buf, "%lf", &x1) != 1)
1084 atoms->nres = newres + 1;
1087 fgets2 (line, STRLEN, in);
1088 if (sscanf (line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
1090 gmx_warning("Bad box in file %s", infile);
1092 /* Generate a cubic box */
1093 for (m = 0; (m < DIM); m++)
1095 xmin[m] = xmax[m] = x[0][m];
1097 for (i = 1; (i < atoms->nr); i++)
1099 for (m = 0; (m < DIM); m++)
1101 xmin[m] = std::min(xmin[m], x[i][m]);
1102 xmax[m] = std::max(xmax[m], x[i][m]);
1105 for (i = 0; i < DIM; i++)
1107 for (m = 0; m < DIM; m++)
1112 for (m = 0; (m < DIM); m++)
1114 box[m][m] = (xmax[m]-xmin[m]);
1116 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n",
1117 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1121 /* We found the first three values, the diagonal elements */
1125 if (sscanf (line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf",
1126 &x1, &y1, &z1, &x2, &y2, &z2) != 6)
1128 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
1141 static void read_whole_conf(const char *infile, char *title,
1142 t_atoms *atoms, rvec x[], rvec *v, matrix box)
1149 in = gmx_fio_fopen(infile, "r");
1151 open_symtab(&symtab);
1152 get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box);
1153 /* We can't free the symbols, as they are still used in atoms, so
1154 * the only choice is to leak them. */
1155 free_symtab(&symtab);
1160 static gmx_bool gmx_one_before_eof(FILE *fp)
1165 if ((beof = fread(data, 1, 1, fp)) == 1)
1167 gmx_fseek(fp, -1, SEEK_CUR);
1172 gmx_bool gro_next_x_or_v(FILE *status, t_trxframe *fr)
1176 char title[STRLEN], *p;
1180 if (gmx_one_before_eof(status))
1185 open_symtab(&symtab);
1186 atoms.nr = fr->natoms;
1187 snew(atoms.atom, fr->natoms);
1188 atoms.nres = fr->natoms;
1189 snew(atoms.resinfo, fr->natoms);
1190 snew(atoms.atomname, fr->natoms);
1192 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
1195 /* prec = 10^ndec: */
1196 for (i = 0; i < ndec; i++)
1206 sfree(atoms.resinfo);
1207 sfree(atoms.atomname);
1208 done_symtab(&symtab);
1210 if ((p = strstr(title, "t=")) != NULL)
1213 if (sscanf(p, "%lf", &tt) == 1)
1225 if (atoms.nr != fr->natoms)
1227 gmx_fatal(FARGS, "Number of atoms in gro frame (%d) doesn't match the number in the previous frame (%d)", atoms.nr, fr->natoms);
1233 int gro_first_x_or_v(FILE *status, t_trxframe *fr)
1238 fprintf(stderr, "Reading frames from gro file");
1239 get_coordnum_fp(status, title, &fr->natoms);
1241 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
1244 if (fr->natoms == 0)
1246 gmx_file("No coordinates in gro file");
1249 snew(fr->x, fr->natoms);
1250 snew(fr->v, fr->natoms);
1251 gro_next_x_or_v(status, fr);
1256 static void make_hconf_format(int pr, gmx_bool bVel, char format[])
1260 /* build format string for printing,
1261 something like "%8.3f" for x and "%8.4f" for v */
1274 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1275 l, pr, l, pr, l, pr, l, vpr, l, vpr, l, vpr);
1279 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1284 static void write_hconf_box(FILE *out, int pr, matrix box)
1295 if (box[XX][YY] || box[XX][ZZ] || box[YY][XX] || box[YY][ZZ] ||
1296 box[ZZ][XX] || box[ZZ][YY])
1298 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df"
1299 "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1300 l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr);
1301 fprintf(out, format,
1302 box[XX][XX], box[YY][YY], box[ZZ][ZZ],
1303 box[XX][YY], box[XX][ZZ], box[YY][XX],
1304 box[YY][ZZ], box[ZZ][XX], box[ZZ][YY]);
1308 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1309 fprintf(out, format,
1310 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1314 void write_hconf_indexed_p(FILE *out, const char *title, t_atoms *atoms,
1315 int nx, const atom_id index[], int pr,
1316 rvec *x, rvec *v, matrix box)
1318 char resnm[6], nm[6], format[100];
1319 int ai, i, resind, resnr;
1321 bromacs(format, 99);
1322 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1323 fprintf (out, "%5d\n", nx);
1325 make_hconf_format(pr, v != NULL, format);
1327 for (i = 0; (i < nx); i++)
1331 resind = atoms->atom[ai].resind;
1332 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
1333 if (resind < atoms->nres)
1335 std::strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
1336 resnr = atoms->resinfo[resind].nr;
1340 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
1346 std::strncpy(nm, *atoms->atomname[ai], sizeof(nm)-1);
1350 std::strncpy(nm, " ??? ", sizeof(nm)-1);
1353 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr%100000, resnm, nm, (ai+1)%100000);
1354 /* next fprintf uses built format string */
1357 fprintf(out, format,
1358 x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
1362 fprintf(out, format,
1363 x[ai][XX], x[ai][YY], x[ai][ZZ]);
1367 write_hconf_box(out, pr, box);
1372 static void write_hconf_mtop(FILE *out, const char *title, gmx_mtop_t *mtop,
1374 rvec *x, rvec *v, matrix box)
1378 gmx_mtop_atomloop_all_t aloop;
1380 char *atomname, *resname;
1382 bromacs(format, 99);
1383 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1384 fprintf (out, "%5d\n", mtop->natoms);
1386 make_hconf_format(pr, v != NULL, format);
1388 aloop = gmx_mtop_atomloop_all_init(mtop);
1389 while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
1391 gmx_mtop_atomloop_all_names(aloop, &atomname, &resnr, &resname);
1393 fprintf(out, "%5d%-5.5s%5.5s%5d",
1394 resnr%100000, resname, atomname, (i+1)%100000);
1395 /* next fprintf uses built format string */
1398 fprintf(out, format,
1399 x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
1403 fprintf(out, format,
1404 x[i][XX], x[i][YY], x[i][ZZ]);
1408 write_hconf_box(out, pr, box);
1413 void write_hconf_p(FILE *out, const char *title, t_atoms *atoms, int pr,
1414 rvec *x, rvec *v, matrix box)
1419 snew(aa, atoms->nr);
1420 for (i = 0; (i < atoms->nr); i++)
1424 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, pr, x, v, box);
1428 void write_conf_p(const char *outfile, const char *title,
1429 t_atoms *atoms, int pr,
1430 rvec *x, rvec *v, matrix box)
1434 out = gmx_fio_fopen(outfile, "w");
1435 write_hconf_p(out, title, atoms, pr, x, v, box);
1437 gmx_fio_fclose (out);
1440 static void write_conf(const char *outfile, const char *title, t_atoms *atoms,
1441 rvec *x, rvec *v, matrix box)
1443 write_conf_p(outfile, title, atoms, 3, x, v, box);
1446 void write_sto_conf_indexed(const char *outfile, const char *title,
1448 rvec x[], rvec *v, int ePBC, matrix box,
1449 atom_id nindex, atom_id index[])
1455 ftp = fn2ftp(outfile);
1459 out = gmx_fio_fopen(outfile, "w");
1460 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
1461 gmx_fio_fclose(out);
1464 clear_trxframe(&fr, TRUE);
1467 fr.natoms = atoms->nr;
1478 copy_mat(box, fr.box);
1479 out = gmx_fio_fopen(outfile, "w");
1480 write_g96_conf(out, &fr, nindex, index);
1481 gmx_fio_fclose(out);
1487 out = gmx_fio_fopen(outfile, "w");
1488 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
1489 gmx_fio_fclose(out);
1492 out = gmx_fio_fopen(outfile, "w");
1493 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
1494 gmx_fio_fclose(out);
1497 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1500 gmx_incons("Not supported in write_sto_conf_indexed");
1504 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
1505 rvec x[], rvec *v, int ePBC, matrix box)
1511 ftp = fn2ftp(outfile);
1515 write_conf(outfile, title, atoms, x, v, box);
1518 clear_trxframe(&fr, TRUE);
1521 fr.natoms = atoms->nr;
1532 copy_mat(box, fr.box);
1533 out = gmx_fio_fopen(outfile, "w");
1534 write_g96_conf(out, &fr, -1, NULL);
1535 gmx_fio_fclose(out);
1540 out = gmx_fio_fopen(outfile, "w");
1541 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
1542 gmx_fio_fclose(out);
1545 out = gmx_fio_fopen(outfile, "w");
1546 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
1547 gmx_fio_fclose(out);
1550 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1553 gmx_incons("Not supported in write_sto_conf");
1557 void write_sto_conf_mtop(const char *outfile, const char *title,
1559 rvec x[], rvec *v, int ePBC, matrix box)
1565 ftp = fn2ftp(outfile);
1569 out = gmx_fio_fopen(outfile, "w");
1570 write_hconf_mtop(out, title, mtop, 3, x, v, box);
1571 gmx_fio_fclose(out);
1574 /* This is a brute force approach which requires a lot of memory.
1575 * We should implement mtop versions of all writing routines.
1577 atoms = gmx_mtop_global_atoms(mtop);
1579 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
1586 void get_stx_coordnum(const char *infile, int *natoms)
1589 int ftp, tpxver, tpxgen;
1591 char g96_line[STRLEN+1];
1593 ftp = fn2ftp(infile);
1594 range_check(ftp, 0, efNR);
1598 get_coordnum(infile, natoms);
1601 in = gmx_fio_fopen(infile, "r");
1608 *natoms = read_g96_conf(in, infile, &fr, g96_line);
1614 in = gmx_fio_fopen(infile, "r");
1615 get_pdb_coordnum(in, natoms);
1619 *natoms = get_espresso_coordnum(infile);
1625 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
1626 *natoms = tpx.natoms;
1630 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
1635 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
1636 rvec x[], rvec *v, int *ePBC, matrix box)
1643 char g96_line[STRLEN+1];
1647 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
1649 else if (atoms->atom == NULL)
1651 gmx_mem("Uninitialized array atom");
1659 ftp = fn2ftp(infile);
1663 read_whole_conf(infile, title, atoms, x, v, box);
1667 fr.natoms = atoms->nr;
1672 in = gmx_fio_fopen(infile, "r");
1673 read_g96_conf(in, infile, &fr, g96_line);
1675 copy_mat(fr.box, box);
1676 std::strncpy(title, fr.title, STRLEN);
1681 read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL);
1684 read_espresso_conf(infile, atoms, x, v, box);
1688 i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop);
1694 strcpy(title, *(mtop->name));
1696 /* Free possibly allocated memory */
1699 *atoms = gmx_mtop_global_atoms(mtop);
1700 top = gmx_mtop_t_to_t_topology(mtop);
1701 tpx_make_chain_identifiers(atoms, &top.mols);
1704 /* The strings in the symtab are still in use in the returned t_atoms
1705 * structure, so we should not free them. But there is no place to put the
1706 * symbols; the only choice is to leak the memory...
1707 * So we clear the symbol table before freeing the topology structure. */
1708 free_symtab(&top.symtab);
1713 gmx_incons("Not supported in read_stx_conf");