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, char *title,
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;
618 // TODO: The code does not understand titles it writes...
623 fp = gmx_fio_fopen(infile, "r");
625 bFoundParticles = FALSE;
626 bFoundVariable = FALSE;
629 while ((r = get_espresso_word(fp, word)))
631 if (level == 1 && std::strcmp(word, "particles") == 0 && !bFoundParticles)
633 bFoundParticles = TRUE;
634 level += check_open_parenthesis(fp, r, infile, "particles");
636 while (level == 2 && (r = get_espresso_word(fp, word)))
639 for (p = 0; p < espNR; p++)
641 if (strcmp(word, esp_prop[p]) == 0)
645 /* printf(" prop[%d] = %s\n",nprop-1,esp_prop[prop[nprop-1]]); */
648 if (!bFoundProp && word[0] != '}')
650 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
652 if (bFoundProp && p == espMOLECULE)
663 while (level > 0 && (r = get_espresso_word(fp, word)))
675 for (p = 0; p < nprop; p++)
680 r = get_espresso_word(fp, word);
684 for (m = 0; m < 3; m++)
686 r = get_espresso_word(fp, word);
687 sscanf(word, "%lf", &d);
692 r = get_espresso_word(fp, word);
693 atoms->atom[i].type = std::strtol(word, NULL, 10);
696 r = get_espresso_word(fp, word);
697 sscanf(word, "%lf", &d);
698 atoms->atom[i].q = d;
701 for (m = 0; m < 3; m++)
703 r = get_espresso_word(fp, word);
704 sscanf(word, "%lf", &d);
709 for (m = 0; m < 3; m++)
711 r = get_espresso_word(fp, word);
716 r = get_espresso_word(fp, word);
717 molnr = std::strtol(word, NULL, 10);
719 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
721 atoms->atom[i].resind =
722 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
723 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
724 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
725 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
726 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
730 atoms->atom[i].resind = atoms->atom[i-1].resind;
735 /* Generate an atom name from the particle type */
736 sprintf(buf, "T%d", atoms->atom[i].type);
737 atoms->atomname[i] = put_symtab(symtab, buf);
740 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
742 atoms->resinfo[atoms->atom[i].resind].name =
743 put_symtab(symtab, "MOL");
748 /* Residue number is the atom number */
749 atoms->atom[i].resind = i;
750 /* Generate an residue name from the particle type */
751 if (atoms->atom[i].type < 26)
753 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
757 sprintf(buf, "T%c%c",
758 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
760 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
770 atoms->nres = atoms->nr;
774 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
777 else if (level == 1 && std::strcmp(word, "variable") == 0 && !bFoundVariable)
779 bFoundVariable = TRUE;
780 level += check_open_parenthesis(fp, r, infile, "variable");
781 while (level == 2 && (r = get_espresso_word(fp, word)))
783 if (level == 2 && std::strcmp(word, "box_l") == 0)
785 for (m = 0; m < 3; m++)
787 r = get_espresso_word(fp, word);
788 sscanf(word, "%lf", &d);
791 level += check_close_parenthesis(fp, r, infile, "box_l");
805 if (!bFoundParticles)
807 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
814 static int get_espresso_coordnum(const char *infile)
818 int natoms, level, r;
819 gmx_bool bFoundParticles;
823 fp = gmx_fio_fopen(infile, "r");
825 bFoundParticles = FALSE;
827 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
829 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
831 bFoundParticles = TRUE;
832 level += check_open_parenthesis(fp, r, infile, "particles");
833 while (level > 0 && (r = get_espresso_word(fp, word)))
858 if (!bFoundParticles)
860 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
869 static void write_espresso_conf_indexed(FILE *out, const char *title,
870 t_atoms *atoms, int nx, atom_id *index,
871 rvec *x, rvec *v, matrix box)
875 fprintf(out, "# %s\n", title);
878 gmx_warning("The Espresso format does not support triclinic unit-cells");
880 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
882 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
883 for (i = 0; i < nx; i++)
893 fprintf(out, "\t{%d %f %f %f %d %g",
894 j, x[j][XX], x[j][YY], x[j][ZZ],
895 atoms->atom[j].type, atoms->atom[j].q);
898 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
905 static void get_coordnum_fp (FILE *in, char *title, int *natoms)
909 fgets2 (title, STRLEN, in);
910 fgets2 (line, STRLEN, in);
911 if (sscanf (line, "%d", natoms) != 1)
913 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
917 static void get_coordnum (const char *infile, int *natoms)
922 in = gmx_fio_fopen(infile, "r");
923 get_coordnum_fp(in, title, natoms);
927 static gmx_bool get_w_conf(FILE *in, const char *infile, char *title,
928 t_symtab *symtab, t_atoms *atoms, int *ndec,
929 rvec x[], rvec *v, matrix box)
932 char resname[6], oldresname[6];
933 char line[STRLEN+1], *ptr;
935 double x1, y1, z1, x2, y2, z2;
937 int natoms, i, m, resnr, newres, oldres, ddist, c;
938 gmx_bool bFirst, bVel;
942 oldres = NOTSET; /* Unlikely number for the first residue! */
945 /* Read the title and number of atoms */
946 get_coordnum_fp(in, title, &natoms);
948 if (natoms > atoms->nr)
950 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)",
953 else if (natoms < atoms->nr)
955 fprintf(stderr, "Warning: gro file contains less atoms (%d) than expected"
956 " (%d)\n", natoms, atoms->nr);
964 oldresname[0] = '\0';
966 /* just pray the arrays are big enough */
967 for (i = 0; (i < natoms); i++)
969 if ((fgets2 (line, STRLEN, in)) == NULL)
971 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d",
974 if (strlen(line) < 39)
976 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i+1, line);
979 /* determine read precision from distance between periods
984 p1 = strchr(line, '.');
987 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
989 p2 = strchr(&p1[1], '.');
992 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
997 p3 = strchr(&p2[1], '.');
1000 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
1003 if (p3 - p2 != ddist)
1005 gmx_fatal(FARGS, "The spacing of the decimal points in file %s is not consistent for x, y and z", infile);
1010 memcpy(name, line, 5);
1012 sscanf(name, "%d", &resnr);
1013 sscanf(line+5, "%5s", resname);
1015 if (resnr != oldres || strncmp(resname, oldresname, sizeof(resname)))
1019 if (newres >= natoms)
1021 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
1024 atoms->atom[i].resind = newres;
1025 t_atoms_set_resinfo(atoms, i, symtab, resname, resnr, ' ', 0, ' ');
1029 atoms->atom[i].resind = newres;
1033 std::memcpy(name, line+10, 5);
1034 atoms->atomname[i] = put_symtab(symtab, name);
1036 /* Copy resname to oldresname after we are done with the sanity check above */
1037 std::strncpy(oldresname, resname, sizeof(oldresname));
1039 /* eventueel controle atomnumber met i+1 */
1041 /* coordinates (start after residue data) */
1043 /* Read fixed format */
1044 for (m = 0; m < DIM; m++)
1046 for (c = 0; (c < ddist && ptr[0]); c++)
1052 if (sscanf (buf, "%lf %lf", &x1, &x2) != 1)
1054 gmx_fatal(FARGS, "Something is wrong in the coordinate formatting of file %s. Note that gro is fixed format (see the manual)", infile);
1062 /* velocities (start after residues and coordinates) */
1065 /* Read fixed format */
1066 for (m = 0; m < DIM; m++)
1068 for (c = 0; (c < ddist && ptr[0]); c++)
1074 if (sscanf (buf, "%lf", &x1) != 1)
1086 atoms->nres = newres + 1;
1089 fgets2 (line, STRLEN, in);
1090 if (sscanf (line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
1092 gmx_warning("Bad box in file %s", infile);
1094 /* Generate a cubic box */
1095 for (m = 0; (m < DIM); m++)
1097 xmin[m] = xmax[m] = x[0][m];
1099 for (i = 1; (i < atoms->nr); i++)
1101 for (m = 0; (m < DIM); m++)
1103 xmin[m] = std::min(xmin[m], x[i][m]);
1104 xmax[m] = std::max(xmax[m], x[i][m]);
1107 for (i = 0; i < DIM; i++)
1109 for (m = 0; m < DIM; m++)
1114 for (m = 0; (m < DIM); m++)
1116 box[m][m] = (xmax[m]-xmin[m]);
1118 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n",
1119 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1123 /* We found the first three values, the diagonal elements */
1127 if (sscanf (line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf",
1128 &x1, &y1, &z1, &x2, &y2, &z2) != 6)
1130 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
1143 static void read_whole_conf(const char *infile, char *title,
1144 t_atoms *atoms, rvec x[], rvec *v, matrix box)
1151 in = gmx_fio_fopen(infile, "r");
1153 open_symtab(&symtab);
1154 get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box);
1155 /* We can't free the symbols, as they are still used in atoms, so
1156 * the only choice is to leak them. */
1157 free_symtab(&symtab);
1162 static gmx_bool gmx_one_before_eof(FILE *fp)
1167 if ((beof = fread(data, 1, 1, fp)) == 1)
1169 gmx_fseek(fp, -1, SEEK_CUR);
1174 gmx_bool gro_next_x_or_v(FILE *status, t_trxframe *fr)
1178 char title[STRLEN], *p;
1182 if (gmx_one_before_eof(status))
1187 open_symtab(&symtab);
1188 atoms.nr = fr->natoms;
1189 snew(atoms.atom, fr->natoms);
1190 atoms.nres = fr->natoms;
1191 snew(atoms.resinfo, fr->natoms);
1192 snew(atoms.atomname, fr->natoms);
1194 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
1197 /* prec = 10^ndec: */
1198 for (i = 0; i < ndec; i++)
1208 sfree(atoms.resinfo);
1209 sfree(atoms.atomname);
1210 done_symtab(&symtab);
1212 if ((p = strstr(title, "t=")) != NULL)
1215 if (sscanf(p, "%lf", &tt) == 1)
1227 if (atoms.nr != fr->natoms)
1229 gmx_fatal(FARGS, "Number of atoms in gro frame (%d) doesn't match the number in the previous frame (%d)", atoms.nr, fr->natoms);
1235 int gro_first_x_or_v(FILE *status, t_trxframe *fr)
1240 fprintf(stderr, "Reading frames from gro file");
1241 get_coordnum_fp(status, title, &fr->natoms);
1243 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
1246 if (fr->natoms == 0)
1248 gmx_file("No coordinates in gro file");
1251 snew(fr->x, fr->natoms);
1252 snew(fr->v, fr->natoms);
1253 gro_next_x_or_v(status, fr);
1258 static void make_hconf_format(int pr, gmx_bool bVel, char format[])
1262 /* build format string for printing,
1263 something like "%8.3f" for x and "%8.4f" for v */
1276 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1277 l, pr, l, pr, l, pr, l, vpr, l, vpr, l, vpr);
1281 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1286 static void write_hconf_box(FILE *out, int pr, matrix box)
1297 if (box[XX][YY] || box[XX][ZZ] || box[YY][XX] || box[YY][ZZ] ||
1298 box[ZZ][XX] || box[ZZ][YY])
1300 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df"
1301 "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1302 l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr);
1303 fprintf(out, format,
1304 box[XX][XX], box[YY][YY], box[ZZ][ZZ],
1305 box[XX][YY], box[XX][ZZ], box[YY][XX],
1306 box[YY][ZZ], box[ZZ][XX], box[ZZ][YY]);
1310 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1311 fprintf(out, format,
1312 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1316 void write_hconf_indexed_p(FILE *out, const char *title, t_atoms *atoms,
1317 int nx, const atom_id index[], int pr,
1318 rvec *x, rvec *v, matrix box)
1320 char resnm[6], nm[6], format[100];
1321 int ai, i, resind, resnr;
1323 bromacs(format, 99);
1324 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1325 fprintf (out, "%5d\n", nx);
1327 make_hconf_format(pr, v != NULL, format);
1329 for (i = 0; (i < nx); i++)
1333 resind = atoms->atom[ai].resind;
1334 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
1335 if (resind < atoms->nres)
1337 std::strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
1338 resnr = atoms->resinfo[resind].nr;
1342 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
1348 std::strncpy(nm, *atoms->atomname[ai], sizeof(nm)-1);
1352 std::strncpy(nm, " ??? ", sizeof(nm)-1);
1355 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr%100000, resnm, nm, (ai+1)%100000);
1356 /* next fprintf uses built format string */
1359 fprintf(out, format,
1360 x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
1364 fprintf(out, format,
1365 x[ai][XX], x[ai][YY], x[ai][ZZ]);
1369 write_hconf_box(out, pr, box);
1374 static void write_hconf_mtop(FILE *out, const char *title, gmx_mtop_t *mtop,
1376 rvec *x, rvec *v, matrix box)
1380 gmx_mtop_atomloop_all_t aloop;
1382 char *atomname, *resname;
1384 bromacs(format, 99);
1385 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1386 fprintf (out, "%5d\n", mtop->natoms);
1388 make_hconf_format(pr, v != NULL, format);
1390 aloop = gmx_mtop_atomloop_all_init(mtop);
1391 while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
1393 gmx_mtop_atomloop_all_names(aloop, &atomname, &resnr, &resname);
1395 fprintf(out, "%5d%-5.5s%5.5s%5d",
1396 resnr%100000, resname, atomname, (i+1)%100000);
1397 /* next fprintf uses built format string */
1400 fprintf(out, format,
1401 x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
1405 fprintf(out, format,
1406 x[i][XX], x[i][YY], x[i][ZZ]);
1410 write_hconf_box(out, pr, box);
1415 void write_hconf_p(FILE *out, const char *title, t_atoms *atoms, int pr,
1416 rvec *x, rvec *v, matrix box)
1421 snew(aa, atoms->nr);
1422 for (i = 0; (i < atoms->nr); i++)
1426 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, pr, x, v, box);
1430 void write_conf_p(const char *outfile, const char *title,
1431 t_atoms *atoms, int pr,
1432 rvec *x, rvec *v, matrix box)
1436 out = gmx_fio_fopen(outfile, "w");
1437 write_hconf_p(out, title, atoms, pr, x, v, box);
1439 gmx_fio_fclose (out);
1442 static void write_conf(const char *outfile, const char *title, t_atoms *atoms,
1443 rvec *x, rvec *v, matrix box)
1445 write_conf_p(outfile, title, atoms, 3, x, v, box);
1448 void write_sto_conf_indexed(const char *outfile, const char *title,
1450 rvec x[], rvec *v, int ePBC, matrix box,
1451 atom_id nindex, atom_id index[])
1457 ftp = fn2ftp(outfile);
1461 out = gmx_fio_fopen(outfile, "w");
1462 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
1463 gmx_fio_fclose(out);
1466 clear_trxframe(&fr, TRUE);
1469 fr.natoms = atoms->nr;
1480 copy_mat(box, fr.box);
1481 out = gmx_fio_fopen(outfile, "w");
1482 write_g96_conf(out, &fr, nindex, index);
1483 gmx_fio_fclose(out);
1489 out = gmx_fio_fopen(outfile, "w");
1490 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
1491 gmx_fio_fclose(out);
1494 out = gmx_fio_fopen(outfile, "w");
1495 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
1496 gmx_fio_fclose(out);
1499 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1502 gmx_incons("Not supported in write_sto_conf_indexed");
1506 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
1507 rvec x[], rvec *v, int ePBC, matrix box)
1513 ftp = fn2ftp(outfile);
1517 write_conf(outfile, title, atoms, x, v, box);
1520 clear_trxframe(&fr, TRUE);
1523 fr.natoms = atoms->nr;
1534 copy_mat(box, fr.box);
1535 out = gmx_fio_fopen(outfile, "w");
1536 write_g96_conf(out, &fr, -1, NULL);
1537 gmx_fio_fclose(out);
1542 out = gmx_fio_fopen(outfile, "w");
1543 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
1544 gmx_fio_fclose(out);
1547 out = gmx_fio_fopen(outfile, "w");
1548 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
1549 gmx_fio_fclose(out);
1552 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1555 gmx_incons("Not supported in write_sto_conf");
1559 void write_sto_conf_mtop(const char *outfile, const char *title,
1561 rvec x[], rvec *v, int ePBC, matrix box)
1567 ftp = fn2ftp(outfile);
1571 out = gmx_fio_fopen(outfile, "w");
1572 write_hconf_mtop(out, title, mtop, 3, x, v, box);
1573 gmx_fio_fclose(out);
1576 /* This is a brute force approach which requires a lot of memory.
1577 * We should implement mtop versions of all writing routines.
1579 atoms = gmx_mtop_global_atoms(mtop);
1581 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
1588 void get_stx_coordnum(const char *infile, int *natoms)
1591 int ftp, tpxver, tpxgen;
1593 char g96_line[STRLEN+1];
1595 ftp = fn2ftp(infile);
1596 range_check(ftp, 0, efNR);
1600 get_coordnum(infile, natoms);
1603 in = gmx_fio_fopen(infile, "r");
1610 *natoms = read_g96_conf(in, infile, &fr, g96_line);
1616 in = gmx_fio_fopen(infile, "r");
1617 get_pdb_coordnum(in, natoms);
1621 *natoms = get_espresso_coordnum(infile);
1627 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
1628 *natoms = tpx.natoms;
1632 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
1637 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
1638 rvec x[], rvec *v, int *ePBC, matrix box)
1645 char g96_line[STRLEN+1];
1649 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
1651 else if (atoms->atom == NULL)
1653 gmx_mem("Uninitialized array atom");
1661 ftp = fn2ftp(infile);
1665 read_whole_conf(infile, title, atoms, x, v, box);
1669 fr.natoms = atoms->nr;
1674 in = gmx_fio_fopen(infile, "r");
1675 read_g96_conf(in, infile, &fr, g96_line);
1677 copy_mat(fr.box, box);
1678 std::strncpy(title, fr.title, STRLEN);
1683 read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL);
1686 read_espresso_conf(infile, title, atoms, x, v, box);
1690 i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop);
1696 strcpy(title, *(mtop->name));
1698 /* Free possibly allocated memory */
1701 *atoms = gmx_mtop_global_atoms(mtop);
1702 top = gmx_mtop_t_to_t_topology(mtop);
1703 tpx_make_chain_identifiers(atoms, &top.mols);
1706 /* The strings in the symtab are still in use in the returned t_atoms
1707 * structure, so we should not free them. But there is no place to put the
1708 * symbols; the only choice is to leak the memory...
1709 * So we clear the symbol table before freeing the topology structure. */
1710 free_symtab(&top.symtab);
1715 gmx_incons("Not supported in read_stx_conf");