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/atomprop.h"
62 #include "gromacs/topology/mtop_util.h"
63 #include "gromacs/topology/symtab.h"
64 #include "gromacs/topology/topology.h"
65 #include "gromacs/utility/cstringutil.h"
66 #include "gromacs/utility/fatalerror.h"
67 #include "gromacs/utility/futil.h"
68 #include "gromacs/utility/smalloc.h"
72 static int read_g96_pos(char line[], t_symtab *symtab,
73 FILE *fp, const char *infile,
78 int nwanted, natoms, atnr, resnr = 0, oldres, newres, shift;
79 char anm[STRLEN], resnm[STRLEN];
100 oldres = -666; /* Unlikely number for the first residue! */
102 while (!bEnd && fgets2(line, STRLEN, fp))
104 bEnd = (std::strncmp(line, "END", 3) == 0);
105 if (!bEnd && (line[0] != '#'))
107 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
109 gmx_fatal(FARGS, "Did not find 3 coordinates for atom %d in %s\n",
112 if ((nwanted != -1) && (natoms >= nwanted))
115 "Found more coordinates (%d) in %s than expected %d\n",
116 natoms, infile, nwanted);
121 (sscanf(line, "%5d%c%5s%c%5s%7d", &resnr, &c1, resnm, &c2, anm, &atnr)
131 strncpy(resnm, "???", sizeof(resnm)-1);
133 strncpy(anm, "???", sizeof(anm)-1);
135 atoms->atomname[natoms] = put_symtab(symtab, anm);
140 if (newres >= atoms->nr)
142 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
145 atoms->atom[natoms].resind = newres;
146 if (newres+1 > atoms->nres)
148 atoms->nres = newres+1;
150 t_atoms_set_resinfo(atoms, natoms, symtab, resnm, resnr, ' ', 0, ' ');
154 atoms->atom[natoms].resind = newres;
159 fr->x[natoms][0] = db1;
160 fr->x[natoms][1] = db2;
161 fr->x[natoms][2] = db3;
166 if ((nwanted != -1) && natoms != nwanted)
169 "Warning: found less coordinates (%d) in %s than expected %d\n",
170 natoms, infile, nwanted);
179 static int read_g96_vel(char line[], FILE *fp, const char *infile,
183 int nwanted, natoms = -1, shift;
184 double db1, db2, db3;
186 nwanted = fr->natoms;
190 if (strcmp(line, "VELOCITYRED") == 0)
200 while (!bEnd && fgets2(line, STRLEN, fp))
202 bEnd = (strncmp(line, "END", 3) == 0);
203 if (!bEnd && (line[0] != '#'))
205 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
207 gmx_fatal(FARGS, "Did not find 3 velocities for atom %d in %s",
210 if ((nwanted != -1) && (natoms >= nwanted))
212 gmx_fatal(FARGS, "Found more velocities (%d) in %s than expected %d\n",
213 natoms, infile, nwanted);
217 fr->v[natoms][0] = db1;
218 fr->v[natoms][1] = db2;
219 fr->v[natoms][2] = db3;
224 if ((nwanted != -1) && (natoms != nwanted))
227 "Warning: found less velocities (%d) in %s than expected %d\n",
228 natoms, infile, nwanted);
235 int read_g96_conf(FILE *fp, const char *infile, t_trxframe *fr, char *line)
237 t_symtab *symtab = NULL;
238 gmx_bool bAtStart, bTime, bAtoms, bPos, bVel, bBox, bEnd, bFinished;
240 double db1, db2, db3, db4, db5, db6, db7, db8, db9;
242 bAtStart = (ftell(fp) == 0);
244 clear_trxframe(fr, FALSE);
256 while (!fr->bTitle && fgets2(line, STRLEN, fp))
258 fr->bTitle = (std::strcmp(line, "TITLE") == 0);
260 if (fr->title == NULL)
262 fgets2(line, STRLEN, fp);
263 fr->title = gmx_strdup(line);
266 while (!bEnd && fgets2(line, STRLEN, fp))
268 bEnd = (std::strcmp(line, "END") == 0);
270 fgets2(line, STRLEN, fp);
273 /* Do not get a line if we are not at the start of the file, *
274 * because without a parameter file we don't know what is in *
275 * the trajectory and we have already read the line in the *
276 * previous call (VERY DIRTY). */
280 bTime = (std::strcmp(line, "TIMESTEP") == 0);
281 bAtoms = (std::strcmp(line, "POSITION") == 0);
282 bPos = (bAtoms || (strcmp(line, "POSITIONRED") == 0));
283 bVel = (std::strncmp(line, "VELOCITY", 8) == 0);
284 bBox = (std::strcmp(line, "BOX") == 0);
287 if (!fr->bTime && !fr->bX)
293 bFinished = (fgets2(line, STRLEN, fp) == NULL);
295 while (!bFinished && (line[0] == '#'));
296 sscanf(line, "%15d%15lf", &(fr->step), &db1);
310 natoms = read_g96_pos(line, symtab, fp, infile, fr);
320 natoms = read_g96_vel(line, fp, infile, fr);
327 while (!bEnd && fgets2(line, STRLEN, fp))
329 bEnd = (strncmp(line, "END", 3) == 0);
330 if (!bEnd && (line[0] != '#'))
332 nbp = sscanf(line, "%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf",
333 &db1, &db2, &db3, &db4, &db5, &db6, &db7, &db8, &db9);
336 gmx_fatal(FARGS, "Found a BOX line, but no box in %s", infile);
338 fr->box[XX][XX] = db1;
339 fr->box[YY][YY] = db2;
340 fr->box[ZZ][ZZ] = db3;
343 fr->box[XX][YY] = db4;
344 fr->box[XX][ZZ] = db5;
345 fr->box[YY][XX] = db6;
346 fr->box[YY][ZZ] = db7;
347 fr->box[ZZ][XX] = db8;
348 fr->box[ZZ][YY] = db9;
355 while (!bFinished && fgets2(line, STRLEN, fp));
364 void write_g96_conf(FILE *out, t_trxframe *fr,
365 int nindex, const atom_id *index)
383 fprintf(out, "TITLE\n%s\nEND\n", fr->title);
385 if (fr->bStep || fr->bTime)
387 /* Officially the time format is %15.9, which is not enough for 10 ns */
388 fprintf(out, "TIMESTEP\n%15d%15.6f\nEND\n", fr->step, fr->time);
394 fprintf(out, "POSITION\n");
395 for (i = 0; i < nout; i++)
405 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
406 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
407 *atoms->resinfo[atoms->atom[a].resind].name,
408 *atoms->atomname[a], (i+1) % 10000000,
409 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
414 fprintf(out, "POSITIONRED\n");
415 for (i = 0; i < nout; i++)
425 fprintf(out, "%15.9f%15.9f%15.9f\n",
426 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
429 fprintf(out, "END\n");
435 fprintf(out, "VELOCITY\n");
436 for (i = 0; i < nout; i++)
446 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
447 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
448 *atoms->resinfo[atoms->atom[a].resind].name,
449 *atoms->atomname[a], (i+1) % 10000000,
450 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
455 fprintf(out, "VELOCITYRED\n");
456 for (i = 0; i < nout; i++)
466 fprintf(out, "%15.9f%15.9f%15.9f\n",
467 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
470 fprintf(out, "END\n");
474 fprintf(out, "BOX\n");
475 fprintf(out, "%15.9f%15.9f%15.9f",
476 fr->box[XX][XX], fr->box[YY][YY], fr->box[ZZ][ZZ]);
477 if (fr->box[XX][YY] || fr->box[XX][ZZ] || fr->box[YY][XX] ||
478 fr->box[YY][ZZ] || fr->box[ZZ][XX] || fr->box[ZZ][YY])
480 fprintf(out, "%15.9f%15.9f%15.9f%15.9f%15.9f%15.9f",
481 fr->box[XX][YY], fr->box[XX][ZZ], fr->box[YY][XX],
482 fr->box[YY][ZZ], fr->box[ZZ][XX], fr->box[ZZ][YY]);
485 fprintf(out, "END\n");
489 static int get_espresso_word(FILE *fp, char word[])
501 if (i == ' ' || i == '\n' || i == '\t')
526 word[nc++] = (char)i;
530 while (i != EOF && ret == 0);
534 /* printf("'%s'\n",word); */
539 static int check_open_parenthesis(FILE *fp, int r,
540 const char *infile, const char *keyword)
552 r = get_espresso_word(fp, word);
559 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'",
567 static int check_close_parenthesis(FILE *fp, int r,
568 const char *infile, const char *keyword)
580 r = get_espresso_word(fp, word);
587 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'",
596 espID, espPOS, espTYPE, espQ, espV, espF, espMOLECULE, espNR
598 const char *esp_prop[espNR] = {
599 "id", "pos", "type", "q", "v", "f",
603 static void read_espresso_conf(const char *infile, char *title,
604 t_atoms *atoms, rvec x[], rvec *v, matrix box)
606 t_symtab *symtab = NULL;
608 char word[STRLEN], buf[STRLEN];
609 int level, r, nprop, p, i, m, molnr;
612 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
619 // TODO: The code does not understand titles it writes...
624 fp = gmx_fio_fopen(infile, "r");
626 bFoundParticles = FALSE;
627 bFoundVariable = FALSE;
630 while ((r = get_espresso_word(fp, word)))
632 if (level == 1 && std::strcmp(word, "particles") == 0 && !bFoundParticles)
634 bFoundParticles = TRUE;
635 level += check_open_parenthesis(fp, r, infile, "particles");
637 while (level == 2 && (r = get_espresso_word(fp, word)))
640 for (p = 0; p < espNR; p++)
642 if (strcmp(word, esp_prop[p]) == 0)
646 /* printf(" prop[%d] = %s\n",nprop-1,esp_prop[prop[nprop-1]]); */
649 if (!bFoundProp && word[0] != '}')
651 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
653 if (bFoundProp && p == espMOLECULE)
664 while (level > 0 && (r = get_espresso_word(fp, word)))
676 for (p = 0; p < nprop; p++)
681 r = get_espresso_word(fp, word);
685 for (m = 0; m < 3; m++)
687 r = get_espresso_word(fp, word);
688 sscanf(word, "%lf", &d);
693 r = get_espresso_word(fp, word);
694 atoms->atom[i].type = std::strtol(word, NULL, 10);
697 r = get_espresso_word(fp, word);
698 sscanf(word, "%lf", &d);
699 atoms->atom[i].q = d;
702 for (m = 0; m < 3; m++)
704 r = get_espresso_word(fp, word);
705 sscanf(word, "%lf", &d);
710 for (m = 0; m < 3; m++)
712 r = get_espresso_word(fp, word);
717 r = get_espresso_word(fp, word);
718 molnr = std::strtol(word, NULL, 10);
720 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
722 atoms->atom[i].resind =
723 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
724 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
725 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
726 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
727 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
731 atoms->atom[i].resind = atoms->atom[i-1].resind;
736 /* Generate an atom name from the particle type */
737 sprintf(buf, "T%d", atoms->atom[i].type);
738 atoms->atomname[i] = put_symtab(symtab, buf);
741 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
743 atoms->resinfo[atoms->atom[i].resind].name =
744 put_symtab(symtab, "MOL");
749 /* Residue number is the atom number */
750 atoms->atom[i].resind = i;
751 /* Generate an residue name from the particle type */
752 if (atoms->atom[i].type < 26)
754 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
758 sprintf(buf, "T%c%c",
759 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
761 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
771 atoms->nres = atoms->nr;
775 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
778 else if (level == 1 && std::strcmp(word, "variable") == 0 && !bFoundVariable)
780 bFoundVariable = TRUE;
781 level += check_open_parenthesis(fp, r, infile, "variable");
782 while (level == 2 && (r = get_espresso_word(fp, word)))
784 if (level == 2 && std::strcmp(word, "box_l") == 0)
786 for (m = 0; m < 3; m++)
788 r = get_espresso_word(fp, word);
789 sscanf(word, "%lf", &d);
792 level += check_close_parenthesis(fp, r, infile, "box_l");
806 if (!bFoundParticles)
808 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
815 static int get_espresso_coordnum(const char *infile)
819 int natoms, level, r;
820 gmx_bool bFoundParticles;
824 fp = gmx_fio_fopen(infile, "r");
826 bFoundParticles = FALSE;
828 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
830 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
832 bFoundParticles = TRUE;
833 level += check_open_parenthesis(fp, r, infile, "particles");
834 while (level > 0 && (r = get_espresso_word(fp, word)))
859 if (!bFoundParticles)
861 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
870 static void write_espresso_conf_indexed(FILE *out, const char *title,
871 t_atoms *atoms, int nx, atom_id *index,
872 rvec *x, rvec *v, matrix box)
876 fprintf(out, "# %s\n", title);
879 gmx_warning("The Espresso format does not support triclinic unit-cells");
881 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
883 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
884 for (i = 0; i < nx; i++)
894 fprintf(out, "\t{%d %f %f %f %d %g",
895 j, x[j][XX], x[j][YY], x[j][ZZ],
896 atoms->atom[j].type, atoms->atom[j].q);
899 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
906 static void get_coordnum_fp (FILE *in, char *title, int *natoms)
910 fgets2 (title, STRLEN, in);
911 fgets2 (line, STRLEN, in);
912 if (sscanf (line, "%d", natoms) != 1)
914 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
918 static void get_coordnum (const char *infile, int *natoms)
923 in = gmx_fio_fopen(infile, "r");
924 get_coordnum_fp(in, title, natoms);
928 static gmx_bool get_w_conf(FILE *in, const char *infile, char *title,
929 t_symtab *symtab, t_atoms *atoms, int *ndec,
930 rvec x[], rvec *v, matrix box)
933 char resname[6], oldresname[6];
934 char line[STRLEN+1], *ptr;
936 double x1, y1, z1, x2, y2, z2;
938 int natoms, i, m, resnr, newres, oldres, ddist, c;
939 gmx_bool bFirst, bVel;
943 oldres = NOTSET; /* Unlikely number for the first residue! */
946 /* Read the title and number of atoms */
947 get_coordnum_fp(in, title, &natoms);
949 if (natoms > atoms->nr)
951 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)",
954 else if (natoms < atoms->nr)
956 fprintf(stderr, "Warning: gro file contains less atoms (%d) than expected"
957 " (%d)\n", natoms, atoms->nr);
965 oldresname[0] = '\0';
967 /* just pray the arrays are big enough */
968 for (i = 0; (i < natoms); i++)
970 if ((fgets2 (line, STRLEN, in)) == NULL)
972 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d",
975 if (strlen(line) < 39)
977 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i+1, line);
980 /* determine read precision from distance between periods
985 p1 = strchr(line, '.');
988 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
990 p2 = strchr(&p1[1], '.');
993 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
998 p3 = strchr(&p2[1], '.');
1001 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
1004 if (p3 - p2 != ddist)
1006 gmx_fatal(FARGS, "The spacing of the decimal points in file %s is not consistent for x, y and z", infile);
1011 memcpy(name, line, 5);
1013 sscanf(name, "%d", &resnr);
1014 sscanf(line+5, "%5s", resname);
1016 if (resnr != oldres || strncmp(resname, oldresname, sizeof(resname)))
1020 if (newres >= natoms)
1022 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
1025 atoms->atom[i].resind = newres;
1026 t_atoms_set_resinfo(atoms, i, symtab, resname, resnr, ' ', 0, ' ');
1030 atoms->atom[i].resind = newres;
1034 std::memcpy(name, line+10, 5);
1035 atoms->atomname[i] = put_symtab(symtab, name);
1037 /* Copy resname to oldresname after we are done with the sanity check above */
1038 std::strncpy(oldresname, resname, sizeof(oldresname));
1040 /* eventueel controle atomnumber met i+1 */
1042 /* coordinates (start after residue data) */
1044 /* Read fixed format */
1045 for (m = 0; m < DIM; m++)
1047 for (c = 0; (c < ddist && ptr[0]); c++)
1053 if (sscanf (buf, "%lf %lf", &x1, &x2) != 1)
1055 gmx_fatal(FARGS, "Something is wrong in the coordinate formatting of file %s. Note that gro is fixed format (see the manual)", infile);
1063 /* velocities (start after residues and coordinates) */
1066 /* Read fixed format */
1067 for (m = 0; m < DIM; m++)
1069 for (c = 0; (c < ddist && ptr[0]); c++)
1075 if (sscanf (buf, "%lf", &x1) != 1)
1087 atoms->nres = newres + 1;
1090 fgets2 (line, STRLEN, in);
1091 if (sscanf (line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
1093 gmx_warning("Bad box in file %s", infile);
1095 /* Generate a cubic box */
1096 for (m = 0; (m < DIM); m++)
1098 xmin[m] = xmax[m] = x[0][m];
1100 for (i = 1; (i < atoms->nr); i++)
1102 for (m = 0; (m < DIM); m++)
1104 xmin[m] = std::min(xmin[m], x[i][m]);
1105 xmax[m] = std::max(xmax[m], x[i][m]);
1108 for (i = 0; i < DIM; i++)
1110 for (m = 0; m < DIM; m++)
1115 for (m = 0; (m < DIM); m++)
1117 box[m][m] = (xmax[m]-xmin[m]);
1119 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n",
1120 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1124 /* We found the first three values, the diagonal elements */
1128 if (sscanf (line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf",
1129 &x1, &y1, &z1, &x2, &y2, &z2) != 6)
1131 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
1144 static void read_whole_conf(const char *infile, char *title,
1145 t_atoms *atoms, rvec x[], rvec *v, matrix box)
1152 in = gmx_fio_fopen(infile, "r");
1154 open_symtab(&symtab);
1155 get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box);
1156 /* We can't free the symbols, as they are still used in atoms, so
1157 * the only choice is to leak them. */
1158 free_symtab(&symtab);
1163 static gmx_bool gmx_one_before_eof(FILE *fp)
1168 if ((beof = fread(data, 1, 1, fp)) == 1)
1170 gmx_fseek(fp, -1, SEEK_CUR);
1175 gmx_bool gro_next_x_or_v(FILE *status, t_trxframe *fr)
1179 char title[STRLEN], *p;
1183 if (gmx_one_before_eof(status))
1188 open_symtab(&symtab);
1189 atoms.nr = fr->natoms;
1190 snew(atoms.atom, fr->natoms);
1191 atoms.nres = fr->natoms;
1192 snew(atoms.resinfo, fr->natoms);
1193 snew(atoms.atomname, fr->natoms);
1195 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
1198 /* prec = 10^ndec: */
1199 for (i = 0; i < ndec; i++)
1209 sfree(atoms.resinfo);
1210 sfree(atoms.atomname);
1211 done_symtab(&symtab);
1213 if ((p = strstr(title, "t=")) != NULL)
1216 if (sscanf(p, "%lf", &tt) == 1)
1228 if (atoms.nr != fr->natoms)
1230 gmx_fatal(FARGS, "Number of atoms in gro frame (%d) doesn't match the number in the previous frame (%d)", atoms.nr, fr->natoms);
1236 int gro_first_x_or_v(FILE *status, t_trxframe *fr)
1241 fprintf(stderr, "Reading frames from gro file");
1242 get_coordnum_fp(status, title, &fr->natoms);
1244 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
1247 if (fr->natoms == 0)
1249 gmx_file("No coordinates in gro file");
1252 snew(fr->x, fr->natoms);
1253 snew(fr->v, fr->natoms);
1254 gro_next_x_or_v(status, fr);
1259 static void make_hconf_format(int pr, gmx_bool bVel, char format[])
1263 /* build format string for printing,
1264 something like "%8.3f" for x and "%8.4f" for v */
1277 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1278 l, pr, l, pr, l, pr, l, vpr, l, vpr, l, vpr);
1282 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1287 static void write_hconf_box(FILE *out, int pr, matrix box)
1298 if (box[XX][YY] || box[XX][ZZ] || box[YY][XX] || box[YY][ZZ] ||
1299 box[ZZ][XX] || box[ZZ][YY])
1301 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df"
1302 "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1303 l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr);
1304 fprintf(out, format,
1305 box[XX][XX], box[YY][YY], box[ZZ][ZZ],
1306 box[XX][YY], box[XX][ZZ], box[YY][XX],
1307 box[YY][ZZ], box[ZZ][XX], box[ZZ][YY]);
1311 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1312 fprintf(out, format,
1313 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1317 void write_hconf_indexed_p(FILE *out, const char *title, t_atoms *atoms,
1318 int nx, const atom_id index[], int pr,
1319 rvec *x, rvec *v, matrix box)
1321 char resnm[6], nm[6], format[100];
1322 int ai, i, resind, resnr;
1324 bromacs(format, 99);
1325 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1326 fprintf (out, "%5d\n", nx);
1328 make_hconf_format(pr, v != NULL, format);
1330 for (i = 0; (i < nx); i++)
1334 resind = atoms->atom[ai].resind;
1335 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
1336 if (resind < atoms->nres)
1338 std::strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
1339 resnr = atoms->resinfo[resind].nr;
1343 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
1349 std::strncpy(nm, *atoms->atomname[ai], sizeof(nm)-1);
1353 std::strncpy(nm, " ??? ", sizeof(nm)-1);
1356 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr%100000, resnm, nm, (ai+1)%100000);
1357 /* next fprintf uses built format string */
1360 fprintf(out, format,
1361 x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
1365 fprintf(out, format,
1366 x[ai][XX], x[ai][YY], x[ai][ZZ]);
1370 write_hconf_box(out, pr, box);
1375 static void write_hconf_mtop(FILE *out, const char *title, gmx_mtop_t *mtop,
1377 rvec *x, rvec *v, matrix box)
1381 gmx_mtop_atomloop_all_t aloop;
1383 char *atomname, *resname;
1385 bromacs(format, 99);
1386 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1387 fprintf (out, "%5d\n", mtop->natoms);
1389 make_hconf_format(pr, v != NULL, format);
1391 aloop = gmx_mtop_atomloop_all_init(mtop);
1392 while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
1394 gmx_mtop_atomloop_all_names(aloop, &atomname, &resnr, &resname);
1396 fprintf(out, "%5d%-5.5s%5.5s%5d",
1397 resnr%100000, resname, atomname, (i+1)%100000);
1398 /* next fprintf uses built format string */
1401 fprintf(out, format,
1402 x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
1406 fprintf(out, format,
1407 x[i][XX], x[i][YY], x[i][ZZ]);
1411 write_hconf_box(out, pr, box);
1416 void write_hconf_p(FILE *out, const char *title, t_atoms *atoms, int pr,
1417 rvec *x, rvec *v, matrix box)
1422 snew(aa, atoms->nr);
1423 for (i = 0; (i < atoms->nr); i++)
1427 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, pr, x, v, box);
1431 void write_conf_p(const char *outfile, const char *title,
1432 t_atoms *atoms, int pr,
1433 rvec *x, rvec *v, matrix box)
1437 out = gmx_fio_fopen(outfile, "w");
1438 write_hconf_p(out, title, atoms, pr, x, v, box);
1440 gmx_fio_fclose (out);
1443 static void write_conf(const char *outfile, const char *title, t_atoms *atoms,
1444 rvec *x, rvec *v, matrix box)
1446 write_conf_p(outfile, title, atoms, 3, x, v, box);
1449 void write_sto_conf_indexed(const char *outfile, const char *title,
1451 rvec x[], rvec *v, int ePBC, matrix box,
1452 atom_id nindex, atom_id index[])
1458 ftp = fn2ftp(outfile);
1462 out = gmx_fio_fopen(outfile, "w");
1463 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
1464 gmx_fio_fclose(out);
1467 clear_trxframe(&fr, TRUE);
1470 fr.natoms = atoms->nr;
1481 copy_mat(box, fr.box);
1482 out = gmx_fio_fopen(outfile, "w");
1483 write_g96_conf(out, &fr, nindex, index);
1484 gmx_fio_fclose(out);
1490 out = gmx_fio_fopen(outfile, "w");
1491 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
1492 gmx_fio_fclose(out);
1495 out = gmx_fio_fopen(outfile, "w");
1496 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
1497 gmx_fio_fclose(out);
1500 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1503 gmx_incons("Not supported in write_sto_conf_indexed");
1507 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
1508 rvec x[], rvec *v, int ePBC, matrix box)
1514 ftp = fn2ftp(outfile);
1518 write_conf(outfile, title, atoms, x, v, box);
1521 clear_trxframe(&fr, TRUE);
1524 fr.natoms = atoms->nr;
1535 copy_mat(box, fr.box);
1536 out = gmx_fio_fopen(outfile, "w");
1537 write_g96_conf(out, &fr, -1, NULL);
1538 gmx_fio_fclose(out);
1543 out = gmx_fio_fopen(outfile, "w");
1544 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
1545 gmx_fio_fclose(out);
1548 out = gmx_fio_fopen(outfile, "w");
1549 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
1550 gmx_fio_fclose(out);
1553 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1556 gmx_incons("Not supported in write_sto_conf");
1560 void write_sto_conf_mtop(const char *outfile, const char *title,
1562 rvec x[], rvec *v, int ePBC, matrix box)
1568 ftp = fn2ftp(outfile);
1572 out = gmx_fio_fopen(outfile, "w");
1573 write_hconf_mtop(out, title, mtop, 3, x, v, box);
1574 gmx_fio_fclose(out);
1577 /* This is a brute force approach which requires a lot of memory.
1578 * We should implement mtop versions of all writing routines.
1580 atoms = gmx_mtop_global_atoms(mtop);
1582 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
1589 void get_stx_coordnum(const char *infile, int *natoms)
1592 int ftp, tpxver, tpxgen;
1594 char g96_line[STRLEN+1];
1596 ftp = fn2ftp(infile);
1597 range_check(ftp, 0, efNR);
1601 get_coordnum(infile, natoms);
1604 in = gmx_fio_fopen(infile, "r");
1611 *natoms = read_g96_conf(in, infile, &fr, g96_line);
1617 in = gmx_fio_fopen(infile, "r");
1618 get_pdb_coordnum(in, natoms);
1622 *natoms = get_espresso_coordnum(infile);
1628 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
1629 *natoms = tpx.natoms;
1633 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
1638 static void tpx_make_chain_identifiers(t_atoms *atoms, t_block *mols)
1640 int m, a, a0, a1, r;
1644 /* We always assign a new chain number, but save the chain id characters
1645 * for larger molecules.
1647 #define CHAIN_MIN_ATOMS 15
1651 for (m = 0; m < mols->nr; m++)
1653 a0 = mols->index[m];
1654 a1 = mols->index[m+1];
1655 if ((a1-a0 >= CHAIN_MIN_ATOMS) && (chainid <= 'Z'))
1664 for (a = a0; a < a1; a++)
1666 atoms->resinfo[atoms->atom[a].resind].chainnum = chainnum;
1667 atoms->resinfo[atoms->atom[a].resind].chainid = c;
1672 /* Blank out the chain id if there was only one chain */
1675 for (r = 0; r < atoms->nres; r++)
1677 atoms->resinfo[r].chainid = ' ';
1682 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
1683 rvec x[], rvec *v, int *ePBC, matrix box)
1690 char g96_line[STRLEN+1];
1694 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
1696 else if (atoms->atom == NULL)
1698 gmx_mem("Uninitialized array atom");
1706 ftp = fn2ftp(infile);
1710 read_whole_conf(infile, title, atoms, x, v, box);
1714 fr.natoms = atoms->nr;
1719 in = gmx_fio_fopen(infile, "r");
1720 read_g96_conf(in, infile, &fr, g96_line);
1722 copy_mat(fr.box, box);
1723 std::strncpy(title, fr.title, STRLEN);
1728 read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL);
1731 read_espresso_conf(infile, title, atoms, x, v, box);
1735 i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop);
1741 strcpy(title, *(mtop->name));
1743 /* Free possibly allocated memory */
1746 *atoms = gmx_mtop_global_atoms(mtop);
1747 top = gmx_mtop_t_to_t_topology(mtop);
1748 tpx_make_chain_identifiers(atoms, &top.mols);
1751 /* The strings in the symtab are still in use in the returned t_atoms
1752 * structure, so we should not free them. But there is no place to put the
1753 * symbols; the only choice is to leak the memory...
1754 * So we clear the symbol table before freeing the topology structure. */
1755 free_symtab(&top.symtab);
1760 gmx_incons("Not supported in read_stx_conf");
1764 static void done_gmx_groups_t(gmx_groups_t *g)
1768 for (i = 0; (i < egcNR); i++)
1770 if (NULL != g->grps[i].nm_ind)
1772 sfree(g->grps[i].nm_ind);
1773 g->grps[i].nm_ind = NULL;
1775 if (NULL != g->grpnr[i])
1781 /* The contents of this array is in symtab, don't free it here */
1785 gmx_bool read_tps_conf(const char *infile, char *title, t_topology *top, int *ePBC,
1786 rvec **x, rvec **v, matrix box, gmx_bool bMass)
1789 int natoms, i, version, generation;
1790 gmx_bool bTop, bXNULL = FALSE;
1794 bTop = fn2bTPX(infile);
1798 read_tpxheader(infile, &header, TRUE, &version, &generation);
1801 snew(*x, header.natoms);
1805 snew(*v, header.natoms);
1808 *ePBC = read_tpx(infile, NULL, box, &natoms,
1809 (x == NULL) ? NULL : *x, (v == NULL) ? NULL : *v, NULL, mtop);
1810 *top = gmx_mtop_t_to_t_topology(mtop);
1811 /* In this case we need to throw away the group data too */
1812 done_gmx_groups_t(&mtop->groups);
1814 std::strcpy(title, *top->name);
1815 tpx_make_chain_identifiers(&top->atoms, &top->mols);
1819 get_stx_coordnum(infile, &natoms);
1820 init_t_atoms(&top->atoms, natoms, (fn2ftp(infile) == efPDB));
1831 read_stx_conf(infile, title, &top->atoms, *x, (v == NULL) ? NULL : *v, ePBC, box);
1839 aps = gmx_atomprop_init();
1840 for (i = 0; (i < natoms); i++)
1842 if (!gmx_atomprop_query(aps, epropMass,
1843 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
1844 *top->atoms.atomname[i],
1845 &(top->atoms.atom[i].m)))
1849 fprintf(debug, "Can not find mass for atom %s %d %s, setting to 1\n",
1850 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
1851 top->atoms.resinfo[top->atoms.atom[i].resind].nr,
1852 *top->atoms.atomname[i]);
1856 gmx_atomprop_destroy(aps);
1858 top->idef.ntypes = -1;