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, 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/legacyheaders/typedefs.h"
48 #include "gromacs/legacyheaders/macros.h"
49 #include "gromacs/utility/futil.h"
55 #include "gromacs/legacyheaders/copyrite.h"
56 #include "gromacs/topology/mtop_util.h"
59 #include "gromacs/fileio/trx.h"
60 #include "gromacs/math/vec.h"
61 #include "gromacs/pbcutil/pbc.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/smalloc.h"
70 static int read_g96_pos(char line[], t_symtab *symtab,
71 FILE *fp, const char *infile,
76 int nwanted, natoms, atnr, resnr = 0, oldres, newres, shift;
77 char anm[STRLEN], resnm[STRLEN];
98 oldres = -666; /* Unlikely number for the first residue! */
100 while (!bEnd && fgets2(line, STRLEN, fp))
102 bEnd = (strncmp(line, "END", 3) == 0);
103 if (!bEnd && (line[0] != '#'))
105 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
107 gmx_fatal(FARGS, "Did not find 3 coordinates for atom %d in %s\n",
110 if ((nwanted != -1) && (natoms >= nwanted))
113 "Found more coordinates (%d) in %s than expected %d\n",
114 natoms, infile, nwanted);
119 (sscanf(line, "%5d%c%5s%c%5s%7d", &resnr, &c1, resnm, &c2, anm, &atnr)
129 strncpy(resnm, "???", sizeof(resnm)-1);
131 strncpy(anm, "???", sizeof(anm)-1);
133 atoms->atomname[natoms] = put_symtab(symtab, anm);
138 if (newres >= atoms->nr)
140 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
143 atoms->atom[natoms].resind = newres;
144 if (newres+1 > atoms->nres)
146 atoms->nres = newres+1;
148 t_atoms_set_resinfo(atoms, natoms, symtab, resnm, resnr, ' ', 0, ' ');
152 atoms->atom[natoms].resind = newres;
157 fr->x[natoms][0] = db1;
158 fr->x[natoms][1] = db2;
159 fr->x[natoms][2] = db3;
164 if ((nwanted != -1) && natoms != nwanted)
167 "Warning: found less coordinates (%d) in %s than expected %d\n",
168 natoms, infile, nwanted);
177 static int read_g96_vel(char line[], FILE *fp, const char *infile,
181 int nwanted, natoms = -1, shift;
182 double db1, db2, db3;
184 nwanted = fr->natoms;
188 if (strcmp(line, "VELOCITYRED") == 0)
198 while (!bEnd && fgets2(line, STRLEN, fp))
200 bEnd = (strncmp(line, "END", 3) == 0);
201 if (!bEnd && (line[0] != '#'))
203 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
205 gmx_fatal(FARGS, "Did not find 3 velocities for atom %d in %s",
208 if ((nwanted != -1) && (natoms >= nwanted))
210 gmx_fatal(FARGS, "Found more velocities (%d) in %s than expected %d\n",
211 natoms, infile, nwanted);
215 fr->v[natoms][0] = db1;
216 fr->v[natoms][1] = db2;
217 fr->v[natoms][2] = db3;
222 if ((nwanted != -1) && (natoms != nwanted))
225 "Warning: found less velocities (%d) in %s than expected %d\n",
226 natoms, infile, nwanted);
233 int read_g96_conf(FILE *fp, const char *infile, t_trxframe *fr, char *line)
235 t_symtab *symtab = NULL;
236 gmx_bool bAtStart, bTime, bAtoms, bPos, bVel, bBox, bEnd, bFinished;
238 double db1, db2, db3, db4, db5, db6, db7, db8, db9;
240 bAtStart = (ftell(fp) == 0);
242 clear_trxframe(fr, FALSE);
254 while (!fr->bTitle && fgets2(line, STRLEN, fp))
256 fr->bTitle = (strcmp(line, "TITLE") == 0);
258 if (fr->title == NULL)
260 fgets2(line, STRLEN, fp);
261 fr->title = gmx_strdup(line);
264 while (!bEnd && fgets2(line, STRLEN, fp))
266 bEnd = (strcmp(line, "END") == 0);
268 fgets2(line, STRLEN, fp);
271 /* Do not get a line if we are not at the start of the file, *
272 * because without a parameter file we don't know what is in *
273 * the trajectory and we have already read the line in the *
274 * previous call (VERY DIRTY). */
278 bTime = (strcmp(line, "TIMESTEP") == 0);
279 bAtoms = (strcmp(line, "POSITION") == 0);
280 bPos = (bAtoms || (strcmp(line, "POSITIONRED") == 0));
281 bVel = (strncmp(line, "VELOCITY", 8) == 0);
282 bBox = (strcmp(line, "BOX") == 0);
285 if (!fr->bTime && !fr->bX)
291 bFinished = (fgets2(line, STRLEN, fp) == NULL);
293 while (!bFinished && (line[0] == '#'));
294 sscanf(line, "%15d%15lf", &(fr->step), &db1);
308 natoms = read_g96_pos(line, symtab, fp, infile, fr);
318 natoms = read_g96_vel(line, fp, infile, fr);
325 while (!bEnd && fgets2(line, STRLEN, fp))
327 bEnd = (strncmp(line, "END", 3) == 0);
328 if (!bEnd && (line[0] != '#'))
330 nbp = sscanf(line, "%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf",
331 &db1, &db2, &db3, &db4, &db5, &db6, &db7, &db8, &db9);
334 gmx_fatal(FARGS, "Found a BOX line, but no box in %s", infile);
336 fr->box[XX][XX] = db1;
337 fr->box[YY][YY] = db2;
338 fr->box[ZZ][ZZ] = db3;
341 fr->box[XX][YY] = db4;
342 fr->box[XX][ZZ] = db5;
343 fr->box[YY][XX] = db6;
344 fr->box[YY][ZZ] = db7;
345 fr->box[ZZ][XX] = db8;
346 fr->box[ZZ][YY] = db9;
353 while (!bFinished && fgets2(line, STRLEN, fp));
362 void write_g96_conf(FILE *out, t_trxframe *fr,
363 int nindex, const atom_id *index)
381 fprintf(out, "TITLE\n%s\nEND\n", fr->title);
383 if (fr->bStep || fr->bTime)
385 /* Officially the time format is %15.9, which is not enough for 10 ns */
386 fprintf(out, "TIMESTEP\n%15d%15.6f\nEND\n", fr->step, fr->time);
392 fprintf(out, "POSITION\n");
393 for (i = 0; i < nout; i++)
403 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
404 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
405 *atoms->resinfo[atoms->atom[a].resind].name,
406 *atoms->atomname[a], (i+1) % 10000000,
407 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
412 fprintf(out, "POSITIONRED\n");
413 for (i = 0; i < nout; i++)
423 fprintf(out, "%15.9f%15.9f%15.9f\n",
424 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
427 fprintf(out, "END\n");
433 fprintf(out, "VELOCITY\n");
434 for (i = 0; i < nout; i++)
444 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
445 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
446 *atoms->resinfo[atoms->atom[a].resind].name,
447 *atoms->atomname[a], (i+1) % 10000000,
448 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
453 fprintf(out, "VELOCITYRED\n");
454 for (i = 0; i < nout; i++)
464 fprintf(out, "%15.9f%15.9f%15.9f\n",
465 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
468 fprintf(out, "END\n");
472 fprintf(out, "BOX\n");
473 fprintf(out, "%15.9f%15.9f%15.9f",
474 fr->box[XX][XX], fr->box[YY][YY], fr->box[ZZ][ZZ]);
475 if (fr->box[XX][YY] || fr->box[XX][ZZ] || fr->box[YY][XX] ||
476 fr->box[YY][ZZ] || fr->box[ZZ][XX] || fr->box[ZZ][YY])
478 fprintf(out, "%15.9f%15.9f%15.9f%15.9f%15.9f%15.9f",
479 fr->box[XX][YY], fr->box[XX][ZZ], fr->box[YY][XX],
480 fr->box[YY][ZZ], fr->box[ZZ][XX], fr->box[ZZ][YY]);
483 fprintf(out, "END\n");
487 static int get_espresso_word(FILE *fp, char word[])
499 if (i == ' ' || i == '\n' || i == '\t')
524 word[nc++] = (char)i;
528 while (i != EOF && ret == 0);
532 /* printf("'%s'\n",word); */
537 static int check_open_parenthesis(FILE *fp, int r,
538 const char *infile, const char *keyword)
550 r = get_espresso_word(fp, word);
557 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'",
565 static int check_close_parenthesis(FILE *fp, int r,
566 const char *infile, const char *keyword)
578 r = get_espresso_word(fp, word);
585 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'",
594 espID, espPOS, espTYPE, espQ, espV, espF, espMOLECULE, espNR
596 const char *esp_prop[espNR] = {
597 "id", "pos", "type", "q", "v", "f",
601 static void read_espresso_conf(const char *infile,
602 t_atoms *atoms, rvec x[], rvec *v, matrix box)
604 t_symtab *symtab = NULL;
606 char word[STRLEN], buf[STRLEN];
607 int natoms, level, npar, r, nprop, p, i, m, molnr;
610 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
620 fp = gmx_fio_fopen(infile, "r");
622 bFoundParticles = FALSE;
623 bFoundVariable = FALSE;
626 while ((r = get_espresso_word(fp, word)))
628 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
630 bFoundParticles = TRUE;
631 level += check_open_parenthesis(fp, r, infile, "particles");
633 while (level == 2 && (r = get_espresso_word(fp, word)))
636 for (p = 0; p < espNR; p++)
638 if (strcmp(word, esp_prop[p]) == 0)
642 /* printf(" prop[%d] = %s\n",nprop-1,esp_prop[prop[nprop-1]]); */
645 if (!bFoundProp && word[0] != '}')
647 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
649 if (bFoundProp && p == espMOLECULE)
660 while (level > 0 && (r = get_espresso_word(fp, word)))
672 for (p = 0; p < nprop; p++)
677 r = get_espresso_word(fp, word);
681 for (m = 0; m < 3; m++)
683 r = get_espresso_word(fp, word);
684 sscanf(word, "%lf", &d);
689 r = get_espresso_word(fp, word);
690 atoms->atom[i].type = strtol(word, NULL, 10);
693 r = get_espresso_word(fp, word);
694 sscanf(word, "%lf", &d);
695 atoms->atom[i].q = d;
698 for (m = 0; m < 3; m++)
700 r = get_espresso_word(fp, word);
701 sscanf(word, "%lf", &d);
706 for (m = 0; m < 3; m++)
708 r = get_espresso_word(fp, word);
713 r = get_espresso_word(fp, word);
714 molnr = strtol(word, NULL, 10);
716 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
718 atoms->atom[i].resind =
719 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
720 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
721 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
722 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
723 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
727 atoms->atom[i].resind = atoms->atom[i-1].resind;
732 /* Generate an atom name from the particle type */
733 sprintf(buf, "T%d", atoms->atom[i].type);
734 atoms->atomname[i] = put_symtab(symtab, buf);
737 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
739 atoms->resinfo[atoms->atom[i].resind].name =
740 put_symtab(symtab, "MOL");
745 /* Residue number is the atom number */
746 atoms->atom[i].resind = i;
747 /* Generate an residue name from the particle type */
748 if (atoms->atom[i].type < 26)
750 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
754 sprintf(buf, "T%c%c",
755 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
757 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
767 atoms->nres = atoms->nr;
771 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
774 else if (level == 1 && strcmp(word, "variable") == 0 && !bFoundVariable)
776 bFoundVariable = TRUE;
777 level += check_open_parenthesis(fp, r, infile, "variable");
778 while (level == 2 && (r = get_espresso_word(fp, word)))
780 if (level == 2 && strcmp(word, "box_l") == 0)
782 for (m = 0; m < 3; m++)
784 r = get_espresso_word(fp, word);
785 sscanf(word, "%lf", &d);
788 level += check_close_parenthesis(fp, r, infile, "box_l");
802 if (!bFoundParticles)
804 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
811 static int get_espresso_coordnum(const char *infile)
815 int natoms, level, r;
816 gmx_bool bFoundParticles;
820 fp = gmx_fio_fopen(infile, "r");
822 bFoundParticles = FALSE;
824 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
826 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
828 bFoundParticles = TRUE;
829 level += check_open_parenthesis(fp, r, infile, "particles");
830 while (level > 0 && (r = get_espresso_word(fp, word)))
855 if (!bFoundParticles)
857 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
866 static void write_espresso_conf_indexed(FILE *out, const char *title,
867 t_atoms *atoms, int nx, atom_id *index,
868 rvec *x, rvec *v, matrix box)
872 fprintf(out, "# %s\n", title);
875 gmx_warning("The Espresso format does not support triclinic unit-cells");
877 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
879 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
880 for (i = 0; i < nx; i++)
890 fprintf(out, "\t{%d %f %f %f %d %g",
891 j, x[j][XX], x[j][YY], x[j][ZZ],
892 atoms->atom[j].type, atoms->atom[j].q);
895 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
902 static void get_coordnum_fp (FILE *in, char *title, int *natoms)
906 fgets2 (title, STRLEN, in);
907 fgets2 (line, STRLEN, in);
908 if (sscanf (line, "%d", natoms) != 1)
910 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
914 static void get_coordnum (const char *infile, int *natoms)
919 in = gmx_fio_fopen(infile, "r");
920 get_coordnum_fp(in, title, natoms);
924 static gmx_bool get_w_conf(FILE *in, const char *infile, char *title,
925 t_symtab *symtab, t_atoms *atoms, int *ndec,
926 rvec x[], rvec *v, matrix box)
929 char line[STRLEN+1], *ptr;
931 double x1, y1, z1, x2, y2, z2;
933 int natoms, i, m, resnr, newres, oldres, ddist, c;
934 gmx_bool bFirst, bVel;
938 oldres = NOTSET; /* Unlikely number for the first residue! */
941 /* Read the title and number of atoms */
942 get_coordnum_fp(in, title, &natoms);
944 if (natoms > atoms->nr)
946 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)",
949 else if (natoms < atoms->nr)
951 fprintf(stderr, "Warning: gro file contains less atoms (%d) than expected"
952 " (%d)\n", natoms, atoms->nr);
959 /* just pray the arrays are big enough */
960 for (i = 0; (i < natoms); i++)
962 if ((fgets2 (line, STRLEN, in)) == NULL)
964 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d",
967 if (strlen(line) < 39)
969 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i+1, line);
972 /* determine read precision from distance between periods
977 p1 = strchr(line, '.');
980 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
982 p2 = strchr(&p1[1], '.');
985 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
990 p3 = strchr(&p2[1], '.');
993 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
996 if (p3 - p2 != ddist)
998 gmx_fatal(FARGS, "The spacing of the decimal points in file %s is not consistent for x, y and z", infile);
1003 memcpy(name, line, 5);
1005 sscanf(name, "%d", &resnr);
1006 memcpy(name, line+5, 5);
1008 if (resnr != oldres)
1012 if (newres >= natoms)
1014 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
1017 atoms->atom[i].resind = newres;
1018 t_atoms_set_resinfo(atoms, i, symtab, name, resnr, ' ', 0, ' ');
1022 atoms->atom[i].resind = newres;
1026 memcpy(name, line+10, 5);
1027 atoms->atomname[i] = put_symtab(symtab, name);
1029 /* eventueel controle atomnumber met i+1 */
1031 /* coordinates (start after residue data) */
1033 /* Read fixed format */
1034 for (m = 0; m < DIM; m++)
1036 for (c = 0; (c < ddist && ptr[0]); c++)
1042 if (sscanf (buf, "%lf %lf", &x1, &x2) != 1)
1044 gmx_fatal(FARGS, "Something is wrong in the coordinate formatting of file %s. Note that gro is fixed format (see the manual)", infile);
1052 /* velocities (start after residues and coordinates) */
1055 /* Read fixed format */
1056 for (m = 0; m < DIM; m++)
1058 for (c = 0; (c < ddist && ptr[0]); c++)
1064 if (sscanf (buf, "%lf", &x1) != 1)
1076 atoms->nres = newres + 1;
1079 fgets2 (line, STRLEN, in);
1080 if (sscanf (line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
1082 gmx_warning("Bad box in file %s", infile);
1084 /* Generate a cubic box */
1085 for (m = 0; (m < DIM); m++)
1087 xmin[m] = xmax[m] = x[0][m];
1089 for (i = 1; (i < atoms->nr); i++)
1091 for (m = 0; (m < DIM); m++)
1093 xmin[m] = min(xmin[m], x[i][m]);
1094 xmax[m] = max(xmax[m], x[i][m]);
1097 for (i = 0; i < DIM; i++)
1099 for (m = 0; m < DIM; m++)
1104 for (m = 0; (m < DIM); m++)
1106 box[m][m] = (xmax[m]-xmin[m]);
1108 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n",
1109 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1113 /* We found the first three values, the diagonal elements */
1117 if (sscanf (line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf",
1118 &x1, &y1, &z1, &x2, &y2, &z2) != 6)
1120 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
1133 static void read_whole_conf(const char *infile, char *title,
1134 t_atoms *atoms, rvec x[], rvec *v, matrix box)
1141 in = gmx_fio_fopen(infile, "r");
1143 open_symtab(&symtab);
1144 get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box);
1145 /* We can't free the symbols, as they are still used in atoms, so
1146 * the only choice is to leak them. */
1147 free_symtab(&symtab);
1152 static gmx_bool gmx_one_before_eof(FILE *fp)
1157 if ((beof = fread(data, 1, 1, fp)) == 1)
1159 gmx_fseek(fp, -1, SEEK_CUR);
1164 gmx_bool gro_next_x_or_v(FILE *status, t_trxframe *fr)
1168 char title[STRLEN], *p;
1172 if (gmx_one_before_eof(status))
1177 open_symtab(&symtab);
1178 atoms.nr = fr->natoms;
1179 snew(atoms.atom, fr->natoms);
1180 atoms.nres = fr->natoms;
1181 snew(atoms.resinfo, fr->natoms);
1182 snew(atoms.atomname, fr->natoms);
1184 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
1187 /* prec = 10^ndec: */
1188 for (i = 0; i < ndec; i++)
1198 sfree(atoms.resinfo);
1199 sfree(atoms.atomname);
1200 done_symtab(&symtab);
1202 if ((p = strstr(title, "t=")) != NULL)
1205 if (sscanf(p, "%lf", &tt) == 1)
1217 if (atoms.nr != fr->natoms)
1219 gmx_fatal(FARGS, "Number of atoms in gro frame (%d) doesn't match the number in the previous frame (%d)", atoms.nr, fr->natoms);
1225 int gro_first_x_or_v(FILE *status, t_trxframe *fr)
1230 fprintf(stderr, "Reading frames from gro file");
1231 get_coordnum_fp(status, title, &fr->natoms);
1233 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
1236 if (fr->natoms == 0)
1238 gmx_file("No coordinates in gro file");
1241 snew(fr->x, fr->natoms);
1242 snew(fr->v, fr->natoms);
1243 gro_next_x_or_v(status, fr);
1248 static void make_hconf_format(int pr, gmx_bool bVel, char format[])
1252 /* build format string for printing,
1253 something like "%8.3f" for x and "%8.4f" for v */
1266 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1267 l, pr, l, pr, l, pr, l, vpr, l, vpr, l, vpr);
1271 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1276 static void write_hconf_box(FILE *out, int pr, matrix box)
1287 if (box[XX][YY] || box[XX][ZZ] || box[YY][XX] || box[YY][ZZ] ||
1288 box[ZZ][XX] || box[ZZ][YY])
1290 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df"
1291 "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1292 l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr);
1293 fprintf(out, format,
1294 box[XX][XX], box[YY][YY], box[ZZ][ZZ],
1295 box[XX][YY], box[XX][ZZ], box[YY][XX],
1296 box[YY][ZZ], box[ZZ][XX], box[ZZ][YY]);
1300 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1301 fprintf(out, format,
1302 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1306 void write_hconf_indexed_p(FILE *out, const char *title, t_atoms *atoms,
1307 int nx, const atom_id index[], int pr,
1308 rvec *x, rvec *v, matrix box)
1310 char resnm[6], nm[6], format[100];
1311 int ai, i, resind, resnr;
1313 bromacs(format, 99);
1314 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1315 fprintf (out, "%5d\n", nx);
1317 make_hconf_format(pr, v != NULL, format);
1319 for (i = 0; (i < nx); i++)
1323 resind = atoms->atom[ai].resind;
1324 strncpy(resnm, " ??? ", sizeof(resnm)-1);
1325 if (resind < atoms->nres)
1327 strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
1328 resnr = atoms->resinfo[resind].nr;
1332 strncpy(resnm, " ??? ", sizeof(resnm)-1);
1338 strncpy(nm, *atoms->atomname[ai], sizeof(nm)-1);
1342 strncpy(nm, " ??? ", sizeof(nm)-1);
1345 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr%100000, resnm, nm, (ai+1)%100000);
1346 /* next fprintf uses built format string */
1349 fprintf(out, format,
1350 x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
1354 fprintf(out, format,
1355 x[ai][XX], x[ai][YY], x[ai][ZZ]);
1359 write_hconf_box(out, pr, box);
1364 static void write_hconf_mtop(FILE *out, const char *title, gmx_mtop_t *mtop,
1366 rvec *x, rvec *v, matrix box)
1370 gmx_mtop_atomloop_all_t aloop;
1372 char *atomname, *resname;
1374 bromacs(format, 99);
1375 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1376 fprintf (out, "%5d\n", mtop->natoms);
1378 make_hconf_format(pr, v != NULL, format);
1380 aloop = gmx_mtop_atomloop_all_init(mtop);
1381 while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
1383 gmx_mtop_atomloop_all_names(aloop, &atomname, &resnr, &resname);
1385 fprintf(out, "%5d%-5.5s%5.5s%5d",
1386 resnr%100000, resname, atomname, (i+1)%100000);
1387 /* next fprintf uses built format string */
1390 fprintf(out, format,
1391 x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
1395 fprintf(out, format,
1396 x[i][XX], x[i][YY], x[i][ZZ]);
1400 write_hconf_box(out, pr, box);
1405 void write_hconf_p(FILE *out, const char *title, t_atoms *atoms, int pr,
1406 rvec *x, rvec *v, matrix box)
1411 snew(aa, atoms->nr);
1412 for (i = 0; (i < atoms->nr); i++)
1416 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, pr, x, v, box);
1420 void write_conf_p(const char *outfile, const char *title,
1421 t_atoms *atoms, int pr,
1422 rvec *x, rvec *v, matrix box)
1426 out = gmx_fio_fopen(outfile, "w");
1427 write_hconf_p(out, title, atoms, pr, x, v, box);
1429 gmx_fio_fclose (out);
1432 static void write_conf(const char *outfile, const char *title, t_atoms *atoms,
1433 rvec *x, rvec *v, matrix box)
1435 write_conf_p(outfile, title, atoms, 3, x, v, box);
1438 void write_sto_conf_indexed(const char *outfile, const char *title,
1440 rvec x[], rvec *v, int ePBC, matrix box,
1441 atom_id nindex, atom_id index[])
1447 ftp = fn2ftp(outfile);
1451 out = gmx_fio_fopen(outfile, "w");
1452 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
1453 gmx_fio_fclose(out);
1456 clear_trxframe(&fr, TRUE);
1459 fr.natoms = atoms->nr;
1470 copy_mat(box, fr.box);
1471 out = gmx_fio_fopen(outfile, "w");
1472 write_g96_conf(out, &fr, nindex, index);
1473 gmx_fio_fclose(out);
1479 out = gmx_fio_fopen(outfile, "w");
1480 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
1481 gmx_fio_fclose(out);
1484 out = gmx_fio_fopen(outfile, "w");
1485 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
1486 gmx_fio_fclose(out);
1491 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1494 gmx_incons("Not supported in write_sto_conf_indexed");
1498 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
1499 rvec x[], rvec *v, int ePBC, matrix box)
1505 ftp = fn2ftp(outfile);
1509 write_conf(outfile, title, atoms, x, v, box);
1512 clear_trxframe(&fr, TRUE);
1515 fr.natoms = atoms->nr;
1526 copy_mat(box, fr.box);
1527 out = gmx_fio_fopen(outfile, "w");
1528 write_g96_conf(out, &fr, -1, NULL);
1529 gmx_fio_fclose(out);
1534 out = gmx_fio_fopen(outfile, "w");
1535 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
1536 gmx_fio_fclose(out);
1539 out = gmx_fio_fopen(outfile, "w");
1540 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
1541 gmx_fio_fclose(out);
1546 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1549 gmx_incons("Not supported in write_sto_conf");
1553 void write_sto_conf_mtop(const char *outfile, const char *title,
1555 rvec x[], rvec *v, int ePBC, matrix box)
1561 ftp = fn2ftp(outfile);
1565 out = gmx_fio_fopen(outfile, "w");
1566 write_hconf_mtop(out, title, mtop, 3, x, v, box);
1567 gmx_fio_fclose(out);
1570 /* This is a brute force approach which requires a lot of memory.
1571 * We should implement mtop versions of all writing routines.
1573 atoms = gmx_mtop_global_atoms(mtop);
1575 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
1582 void get_stx_coordnum(const char *infile, int *natoms)
1585 int ftp, tpxver, tpxgen;
1587 char g96_line[STRLEN+1];
1589 ftp = fn2ftp(infile);
1590 range_check(ftp, 0, efNR);
1594 get_coordnum(infile, natoms);
1597 in = gmx_fio_fopen(infile, "r");
1604 *natoms = read_g96_conf(in, infile, &fr, g96_line);
1610 in = gmx_fio_fopen(infile, "r");
1611 get_pdb_coordnum(in, natoms);
1615 *natoms = get_espresso_coordnum(infile);
1623 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
1624 *natoms = tpx.natoms;
1628 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
1633 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
1634 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 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);
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");