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.
44 #include "gromacs/utility/smalloc.h"
48 #include "gromacs/utility/cstringutil.h"
58 #include "gmx_fatal.h"
61 #include "mtop_util.h"
66 static int read_g96_pos(char line[], t_symtab *symtab,
67 FILE *fp, const char *infile,
72 int nwanted, natoms, atnr, resnr = 0, oldres, newres, shift;
73 char anm[STRLEN], resnm[STRLEN];
94 oldres = -666; /* Unlikely number for the first residue! */
96 while (!bEnd && fgets2(line, STRLEN, fp))
98 bEnd = (strncmp(line, "END", 3) == 0);
99 if (!bEnd && (line[0] != '#'))
101 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
103 gmx_fatal(FARGS, "Did not find 3 coordinates for atom %d in %s\n",
106 if ((nwanted != -1) && (natoms >= nwanted))
109 "Found more coordinates (%d) in %s than expected %d\n",
110 natoms, infile, nwanted);
115 (sscanf(line, "%5d%c%5s%c%5s%7d", &resnr, &c1, resnm, &c2, anm, &atnr)
125 strncpy(resnm, "???", sizeof(resnm)-1);
127 strncpy(anm, "???", sizeof(anm)-1);
129 atoms->atomname[natoms] = put_symtab(symtab, anm);
134 if (newres >= atoms->nr)
136 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
139 atoms->atom[natoms].resind = newres;
140 if (newres+1 > atoms->nres)
142 atoms->nres = newres+1;
144 t_atoms_set_resinfo(atoms, natoms, symtab, resnm, resnr, ' ', 0, ' ');
148 atoms->atom[natoms].resind = newres;
153 fr->x[natoms][0] = db1;
154 fr->x[natoms][1] = db2;
155 fr->x[natoms][2] = db3;
160 if ((nwanted != -1) && natoms != nwanted)
163 "Warning: found less coordinates (%d) in %s than expected %d\n",
164 natoms, infile, nwanted);
173 static int read_g96_vel(char line[], FILE *fp, const char *infile,
177 int nwanted, natoms = -1, shift;
178 double db1, db2, db3;
180 nwanted = fr->natoms;
184 if (strcmp(line, "VELOCITYRED") == 0)
194 while (!bEnd && fgets2(line, STRLEN, fp))
196 bEnd = (strncmp(line, "END", 3) == 0);
197 if (!bEnd && (line[0] != '#'))
199 if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3)
201 gmx_fatal(FARGS, "Did not find 3 velocities for atom %d in %s",
204 if ((nwanted != -1) && (natoms >= nwanted))
206 gmx_fatal(FARGS, "Found more velocities (%d) in %s than expected %d\n",
207 natoms, infile, nwanted);
211 fr->v[natoms][0] = db1;
212 fr->v[natoms][1] = db2;
213 fr->v[natoms][2] = db3;
218 if ((nwanted != -1) && (natoms != nwanted))
221 "Warning: found less velocities (%d) in %s than expected %d\n",
222 natoms, infile, nwanted);
229 int read_g96_conf(FILE *fp, const char *infile, t_trxframe *fr, char *line)
231 t_symtab *symtab = NULL;
232 gmx_bool bAtStart, bTime, bAtoms, bPos, bVel, bBox, bEnd, bFinished;
234 double db1, db2, db3, db4, db5, db6, db7, db8, db9;
236 bAtStart = (ftell(fp) == 0);
238 clear_trxframe(fr, FALSE);
250 while (!fr->bTitle && fgets2(line, STRLEN, fp))
252 fr->bTitle = (strcmp(line, "TITLE") == 0);
254 if (fr->title == NULL)
256 fgets2(line, STRLEN, fp);
257 fr->title = strdup(line);
260 while (!bEnd && fgets2(line, STRLEN, fp))
262 bEnd = (strcmp(line, "END") == 0);
264 fgets2(line, STRLEN, fp);
267 /* Do not get a line if we are not at the start of the file, *
268 * because without a parameter file we don't know what is in *
269 * the trajectory and we have already read the line in the *
270 * previous call (VERY DIRTY). */
274 bTime = (strcmp(line, "TIMESTEP") == 0);
275 bAtoms = (strcmp(line, "POSITION") == 0);
276 bPos = (bAtoms || (strcmp(line, "POSITIONRED") == 0));
277 bVel = (strncmp(line, "VELOCITY", 8) == 0);
278 bBox = (strcmp(line, "BOX") == 0);
281 if (!fr->bTime && !fr->bX)
287 bFinished = (fgets2(line, STRLEN, fp) == NULL);
289 while (!bFinished && (line[0] == '#'));
290 sscanf(line, "%15d%15lf", &(fr->step), &db1);
304 natoms = read_g96_pos(line, symtab, fp, infile, fr);
314 natoms = read_g96_vel(line, fp, infile, fr);
321 while (!bEnd && fgets2(line, STRLEN, fp))
323 bEnd = (strncmp(line, "END", 3) == 0);
324 if (!bEnd && (line[0] != '#'))
326 nbp = sscanf(line, "%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf%15lf",
327 &db1, &db2, &db3, &db4, &db5, &db6, &db7, &db8, &db9);
330 gmx_fatal(FARGS, "Found a BOX line, but no box in %s", infile);
332 fr->box[XX][XX] = db1;
333 fr->box[YY][YY] = db2;
334 fr->box[ZZ][ZZ] = db3;
337 fr->box[XX][YY] = db4;
338 fr->box[XX][ZZ] = db5;
339 fr->box[YY][XX] = db6;
340 fr->box[YY][ZZ] = db7;
341 fr->box[ZZ][XX] = db8;
342 fr->box[ZZ][YY] = db9;
349 while (!bFinished && fgets2(line, STRLEN, fp));
358 void write_g96_conf(FILE *out, t_trxframe *fr,
359 int nindex, const atom_id *index)
377 fprintf(out, "TITLE\n%s\nEND\n", fr->title);
379 if (fr->bStep || fr->bTime)
381 /* Officially the time format is %15.9, which is not enough for 10 ns */
382 fprintf(out, "TIMESTEP\n%15d%15.6f\nEND\n", fr->step, fr->time);
388 fprintf(out, "POSITION\n");
389 for (i = 0; i < nout; i++)
399 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
400 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
401 *atoms->resinfo[atoms->atom[a].resind].name,
402 *atoms->atomname[a], (i+1) % 10000000,
403 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
408 fprintf(out, "POSITIONRED\n");
409 for (i = 0; i < nout; i++)
419 fprintf(out, "%15.9f%15.9f%15.9f\n",
420 fr->x[a][XX], fr->x[a][YY], fr->x[a][ZZ]);
423 fprintf(out, "END\n");
429 fprintf(out, "VELOCITY\n");
430 for (i = 0; i < nout; i++)
440 fprintf(out, "%5d %-5s %-5s%7d%15.9f%15.9f%15.9f\n",
441 (atoms->resinfo[atoms->atom[a].resind].nr) % 100000,
442 *atoms->resinfo[atoms->atom[a].resind].name,
443 *atoms->atomname[a], (i+1) % 10000000,
444 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
449 fprintf(out, "VELOCITYRED\n");
450 for (i = 0; i < nout; i++)
460 fprintf(out, "%15.9f%15.9f%15.9f\n",
461 fr->v[a][XX], fr->v[a][YY], fr->v[a][ZZ]);
464 fprintf(out, "END\n");
468 fprintf(out, "BOX\n");
469 fprintf(out, "%15.9f%15.9f%15.9f",
470 fr->box[XX][XX], fr->box[YY][YY], fr->box[ZZ][ZZ]);
471 if (fr->box[XX][YY] || fr->box[XX][ZZ] || fr->box[YY][XX] ||
472 fr->box[YY][ZZ] || fr->box[ZZ][XX] || fr->box[ZZ][YY])
474 fprintf(out, "%15.9f%15.9f%15.9f%15.9f%15.9f%15.9f",
475 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]);
479 fprintf(out, "END\n");
483 static int get_espresso_word(FILE *fp, char word[])
495 if (i == ' ' || i == '\n' || i == '\t')
520 word[nc++] = (char)i;
524 while (i != EOF && ret == 0);
528 /* printf("'%s'\n",word); */
533 static int check_open_parenthesis(FILE *fp, int r,
534 const char *infile, const char *keyword)
546 r = get_espresso_word(fp, word);
553 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'",
561 static int check_close_parenthesis(FILE *fp, int r,
562 const char *infile, const char *keyword)
574 r = get_espresso_word(fp, word);
581 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'",
590 espID, espPOS, espTYPE, espQ, espV, espF, espMOLECULE, espNR
592 const char *esp_prop[espNR] = {
593 "id", "pos", "type", "q", "v", "f",
597 static void read_espresso_conf(const char *infile,
598 t_atoms *atoms, rvec x[], rvec *v, matrix box)
600 t_symtab *symtab = NULL;
602 char word[STRLEN], buf[STRLEN];
603 int natoms, level, npar, r, nprop, p, i, m, molnr;
606 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
616 fp = gmx_fio_fopen(infile, "r");
618 bFoundParticles = FALSE;
619 bFoundVariable = FALSE;
622 while ((r = get_espresso_word(fp, word)))
624 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
626 bFoundParticles = TRUE;
627 level += check_open_parenthesis(fp, r, infile, "particles");
629 while (level == 2 && (r = get_espresso_word(fp, word)))
632 for (p = 0; p < espNR; p++)
634 if (strcmp(word, esp_prop[p]) == 0)
638 /* printf(" prop[%d] = %s\n",nprop-1,esp_prop[prop[nprop-1]]); */
641 if (!bFoundProp && word[0] != '}')
643 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
645 if (bFoundProp && p == espMOLECULE)
656 while (level > 0 && (r = get_espresso_word(fp, word)))
668 for (p = 0; p < nprop; p++)
673 r = get_espresso_word(fp, word);
677 for (m = 0; m < 3; m++)
679 r = get_espresso_word(fp, word);
680 sscanf(word, "%lf", &d);
685 r = get_espresso_word(fp, word);
686 atoms->atom[i].type = strtol(word, NULL, 10);
689 r = get_espresso_word(fp, word);
690 sscanf(word, "%lf", &d);
691 atoms->atom[i].q = d;
694 for (m = 0; m < 3; m++)
696 r = get_espresso_word(fp, word);
697 sscanf(word, "%lf", &d);
702 for (m = 0; m < 3; m++)
704 r = get_espresso_word(fp, word);
709 r = get_espresso_word(fp, word);
710 molnr = strtol(word, NULL, 10);
712 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
714 atoms->atom[i].resind =
715 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
716 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
717 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
718 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
719 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
723 atoms->atom[i].resind = atoms->atom[i-1].resind;
728 /* Generate an atom name from the particle type */
729 sprintf(buf, "T%d", atoms->atom[i].type);
730 atoms->atomname[i] = put_symtab(symtab, buf);
733 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
735 atoms->resinfo[atoms->atom[i].resind].name =
736 put_symtab(symtab, "MOL");
741 /* Residue number is the atom number */
742 atoms->atom[i].resind = i;
743 /* Generate an residue name from the particle type */
744 if (atoms->atom[i].type < 26)
746 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
750 sprintf(buf, "T%c%c",
751 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
753 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
763 atoms->nres = atoms->nr;
767 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
770 else if (level == 1 && strcmp(word, "variable") == 0 && !bFoundVariable)
772 bFoundVariable = TRUE;
773 level += check_open_parenthesis(fp, r, infile, "variable");
774 while (level == 2 && (r = get_espresso_word(fp, word)))
776 if (level == 2 && strcmp(word, "box_l") == 0)
778 for (m = 0; m < 3; m++)
780 r = get_espresso_word(fp, word);
781 sscanf(word, "%lf", &d);
784 level += check_close_parenthesis(fp, r, infile, "box_l");
798 if (!bFoundParticles)
800 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
807 static int get_espresso_coordnum(const char *infile)
811 int natoms, level, r;
812 gmx_bool bFoundParticles;
816 fp = gmx_fio_fopen(infile, "r");
818 bFoundParticles = FALSE;
820 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
822 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
824 bFoundParticles = TRUE;
825 level += check_open_parenthesis(fp, r, infile, "particles");
826 while (level > 0 && (r = get_espresso_word(fp, word)))
851 if (!bFoundParticles)
853 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
862 static void write_espresso_conf_indexed(FILE *out, const char *title,
863 t_atoms *atoms, int nx, atom_id *index,
864 rvec *x, rvec *v, matrix box)
868 fprintf(out, "# %s\n", title);
871 gmx_warning("The Espresso format does not support triclinic unit-cells");
873 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
875 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
876 for (i = 0; i < nx; i++)
886 fprintf(out, "\t{%d %f %f %f %d %g",
887 j, x[j][XX], x[j][YY], x[j][ZZ],
888 atoms->atom[j].type, atoms->atom[j].q);
891 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
898 static void get_coordnum_fp (FILE *in, char *title, int *natoms)
902 fgets2 (title, STRLEN, in);
903 fgets2 (line, STRLEN, in);
904 if (sscanf (line, "%d", natoms) != 1)
906 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
910 static void get_coordnum (const char *infile, int *natoms)
915 in = gmx_fio_fopen(infile, "r");
916 get_coordnum_fp(in, title, natoms);
920 static gmx_bool get_w_conf(FILE *in, const char *infile, char *title,
921 t_symtab *symtab, t_atoms *atoms, int *ndec,
922 rvec x[], rvec *v, matrix box)
925 char line[STRLEN+1], *ptr;
927 double x1, y1, z1, x2, y2, z2;
929 int natoms, i, m, resnr, newres, oldres, ddist, c;
930 gmx_bool bFirst, bVel;
934 oldres = NOTSET; /* Unlikely number for the first residue! */
937 /* Read the title and number of atoms */
938 get_coordnum_fp(in, title, &natoms);
940 if (natoms > atoms->nr)
942 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)",
945 else if (natoms < atoms->nr)
947 fprintf(stderr, "Warning: gro file contains less atoms (%d) than expected"
948 " (%d)\n", natoms, atoms->nr);
955 /* just pray the arrays are big enough */
956 for (i = 0; (i < natoms); i++)
958 if ((fgets2 (line, STRLEN, in)) == NULL)
960 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d",
963 if (strlen(line) < 39)
965 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i+1, line);
968 /* determine read precision from distance between periods
973 p1 = strchr(line, '.');
976 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
978 p2 = strchr(&p1[1], '.');
981 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
986 p3 = strchr(&p2[1], '.');
989 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
992 if (p3 - p2 != ddist)
994 gmx_fatal(FARGS, "The spacing of the decimal points in file %s is not consistent for x, y and z", infile);
999 memcpy(name, line, 5);
1001 sscanf(name, "%d", &resnr);
1002 memcpy(name, line+5, 5);
1004 if (resnr != oldres)
1008 if (newres >= natoms)
1010 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
1013 atoms->atom[i].resind = newres;
1014 t_atoms_set_resinfo(atoms, i, symtab, name, resnr, ' ', 0, ' ');
1018 atoms->atom[i].resind = newres;
1022 memcpy(name, line+10, 5);
1023 atoms->atomname[i] = put_symtab(symtab, name);
1025 /* eventueel controle atomnumber met i+1 */
1027 /* coordinates (start after residue data) */
1029 /* Read fixed format */
1030 for (m = 0; m < DIM; m++)
1032 for (c = 0; (c < ddist && ptr[0]); c++)
1038 if (sscanf (buf, "%lf %lf", &x1, &x2) != 1)
1040 gmx_fatal(FARGS, "Something is wrong in the coordinate formatting of file %s. Note that gro is fixed format (see the manual)", infile);
1048 /* velocities (start after residues and coordinates) */
1051 /* Read fixed format */
1052 for (m = 0; m < DIM; m++)
1054 for (c = 0; (c < ddist && ptr[0]); c++)
1060 if (sscanf (buf, "%lf", &x1) != 1)
1072 atoms->nres = newres + 1;
1075 fgets2 (line, STRLEN, in);
1076 if (sscanf (line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
1078 gmx_warning("Bad box in file %s", infile);
1080 /* Generate a cubic box */
1081 for (m = 0; (m < DIM); m++)
1083 xmin[m] = xmax[m] = x[0][m];
1085 for (i = 1; (i < atoms->nr); i++)
1087 for (m = 0; (m < DIM); m++)
1089 xmin[m] = min(xmin[m], x[i][m]);
1090 xmax[m] = max(xmax[m], x[i][m]);
1093 for (i = 0; i < DIM; i++)
1095 for (m = 0; m < DIM; m++)
1100 for (m = 0; (m < DIM); m++)
1102 box[m][m] = (xmax[m]-xmin[m]);
1104 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n",
1105 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1109 /* We found the first three values, the diagonal elements */
1113 if (sscanf (line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf",
1114 &x1, &y1, &z1, &x2, &y2, &z2) != 6)
1116 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
1129 static void read_whole_conf(const char *infile, char *title,
1130 t_atoms *atoms, rvec x[], rvec *v, matrix box)
1137 in = gmx_fio_fopen(infile, "r");
1139 open_symtab(&symtab);
1140 get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box);
1141 /* We can't free the symbols, as they are still used in atoms, so
1142 * the only choice is to leak them. */
1143 free_symtab(&symtab);
1148 gmx_bool gro_next_x_or_v(FILE *status, t_trxframe *fr)
1152 char title[STRLEN], *p;
1156 if (gmx_eof(status))
1161 open_symtab(&symtab);
1162 atoms.nr = fr->natoms;
1163 snew(atoms.atom, fr->natoms);
1164 atoms.nres = fr->natoms;
1165 snew(atoms.resinfo, fr->natoms);
1166 snew(atoms.atomname, fr->natoms);
1168 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
1171 /* prec = 10^ndec: */
1172 for (i = 0; i < ndec; i++)
1182 sfree(atoms.resinfo);
1183 sfree(atoms.atomname);
1184 done_symtab(&symtab);
1186 if ((p = strstr(title, "t=")) != NULL)
1189 if (sscanf(p, "%lf", &tt) == 1)
1201 if (atoms.nr != fr->natoms)
1203 gmx_fatal(FARGS, "Number of atoms in gro frame (%d) doesn't match the number in the previous frame (%d)", atoms.nr, fr->natoms);
1209 int gro_first_x_or_v(FILE *status, t_trxframe *fr)
1214 fprintf(stderr, "Reading frames from gro file");
1215 get_coordnum_fp(status, title, &fr->natoms);
1217 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
1220 if (fr->natoms == 0)
1222 gmx_file("No coordinates in gro file");
1225 snew(fr->x, fr->natoms);
1226 snew(fr->v, fr->natoms);
1227 gro_next_x_or_v(status, fr);
1232 static void make_hconf_format(int pr, gmx_bool bVel, char format[])
1236 /* build format string for printing,
1237 something like "%8.3f" for x and "%8.4f" for v */
1250 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1251 l, pr, l, pr, l, pr, l, vpr, l, vpr, l, vpr);
1255 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1260 static void write_hconf_box(FILE *out, int pr, matrix box)
1271 if (box[XX][YY] || box[XX][ZZ] || box[YY][XX] || box[YY][ZZ] ||
1272 box[ZZ][XX] || box[ZZ][YY])
1274 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df"
1275 "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
1276 l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr);
1277 fprintf(out, format,
1278 box[XX][XX], box[YY][YY], box[ZZ][ZZ],
1279 box[XX][YY], box[XX][ZZ], box[YY][XX],
1280 box[YY][ZZ], box[ZZ][XX], box[ZZ][YY]);
1284 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
1285 fprintf(out, format,
1286 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
1290 void write_hconf_indexed_p(FILE *out, const char *title, t_atoms *atoms,
1291 int nx, const atom_id index[], int pr,
1292 rvec *x, rvec *v, matrix box)
1294 char resnm[6], nm[6], format[100];
1295 int ai, i, resind, resnr;
1297 bromacs(format, 99);
1298 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1299 fprintf (out, "%5d\n", nx);
1301 make_hconf_format(pr, v != NULL, format);
1303 for (i = 0; (i < nx); i++)
1307 resind = atoms->atom[ai].resind;
1308 strncpy(resnm, " ??? ", sizeof(resnm)-1);
1309 if (resind < atoms->nres)
1311 strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
1312 resnr = atoms->resinfo[resind].nr;
1316 strncpy(resnm, " ??? ", sizeof(resnm)-1);
1322 strncpy(nm, *atoms->atomname[ai], sizeof(nm)-1);
1326 strncpy(nm, " ??? ", sizeof(nm)-1);
1329 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr%100000, resnm, nm, (ai+1)%100000);
1330 /* next fprintf uses built format string */
1333 fprintf(out, format,
1334 x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
1338 fprintf(out, format,
1339 x[ai][XX], x[ai][YY], x[ai][ZZ]);
1343 write_hconf_box(out, pr, box);
1348 static void write_hconf_mtop(FILE *out, const char *title, gmx_mtop_t *mtop,
1350 rvec *x, rvec *v, matrix box)
1354 gmx_mtop_atomloop_all_t aloop;
1356 char *atomname, *resname;
1358 bromacs(format, 99);
1359 fprintf (out, "%s\n", (title && title[0]) ? title : format);
1360 fprintf (out, "%5d\n", mtop->natoms);
1362 make_hconf_format(pr, v != NULL, format);
1364 aloop = gmx_mtop_atomloop_all_init(mtop);
1365 while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
1367 gmx_mtop_atomloop_all_names(aloop, &atomname, &resnr, &resname);
1369 fprintf(out, "%5d%-5.5s%5.5s%5d",
1370 resnr%100000, resname, atomname, (i+1)%100000);
1371 /* next fprintf uses built format string */
1374 fprintf(out, format,
1375 x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
1379 fprintf(out, format,
1380 x[i][XX], x[i][YY], x[i][ZZ]);
1384 write_hconf_box(out, pr, box);
1389 void write_hconf_p(FILE *out, const char *title, t_atoms *atoms, int pr,
1390 rvec *x, rvec *v, matrix box)
1395 snew(aa, atoms->nr);
1396 for (i = 0; (i < atoms->nr); i++)
1400 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, pr, x, v, box);
1404 void write_conf_p(const char *outfile, const char *title,
1405 t_atoms *atoms, int pr,
1406 rvec *x, rvec *v, matrix box)
1410 out = gmx_fio_fopen(outfile, "w");
1411 write_hconf_p(out, title, atoms, pr, x, v, box);
1413 gmx_fio_fclose (out);
1416 static void write_conf(const char *outfile, const char *title, t_atoms *atoms,
1417 rvec *x, rvec *v, matrix box)
1419 write_conf_p(outfile, title, atoms, 3, x, v, box);
1422 void write_sto_conf_indexed(const char *outfile, const char *title,
1424 rvec x[], rvec *v, int ePBC, matrix box,
1425 atom_id nindex, atom_id index[])
1431 ftp = fn2ftp(outfile);
1435 out = gmx_fio_fopen(outfile, "w");
1436 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
1437 gmx_fio_fclose(out);
1440 clear_trxframe(&fr, TRUE);
1443 fr.natoms = atoms->nr;
1454 copy_mat(box, fr.box);
1455 out = gmx_fio_fopen(outfile, "w");
1456 write_g96_conf(out, &fr, nindex, index);
1457 gmx_fio_fclose(out);
1463 out = gmx_fio_fopen(outfile, "w");
1464 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
1465 gmx_fio_fclose(out);
1468 out = gmx_fio_fopen(outfile, "w");
1469 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
1470 gmx_fio_fclose(out);
1475 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1478 gmx_incons("Not supported in write_sto_conf_indexed");
1482 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
1483 rvec x[], rvec *v, int ePBC, matrix box)
1489 ftp = fn2ftp(outfile);
1493 write_conf(outfile, title, atoms, x, v, box);
1496 clear_trxframe(&fr, TRUE);
1499 fr.natoms = atoms->nr;
1510 copy_mat(box, fr.box);
1511 out = gmx_fio_fopen(outfile, "w");
1512 write_g96_conf(out, &fr, -1, NULL);
1513 gmx_fio_fclose(out);
1518 out = gmx_fio_fopen(outfile, "w");
1519 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
1520 gmx_fio_fclose(out);
1523 out = gmx_fio_fopen(outfile, "w");
1524 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
1525 gmx_fio_fclose(out);
1530 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1533 gmx_incons("Not supported in write_sto_conf");
1537 void write_sto_conf_mtop(const char *outfile, const char *title,
1539 rvec x[], rvec *v, int ePBC, matrix box)
1545 ftp = fn2ftp(outfile);
1549 out = gmx_fio_fopen(outfile, "w");
1550 write_hconf_mtop(out, title, mtop, 3, x, v, box);
1551 gmx_fio_fclose(out);
1554 /* This is a brute force approach which requires a lot of memory.
1555 * We should implement mtop versions of all writing routines.
1557 atoms = gmx_mtop_global_atoms(mtop);
1559 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
1566 void get_stx_coordnum(const char *infile, int *natoms)
1569 int ftp, tpxver, tpxgen;
1571 char g96_line[STRLEN+1];
1573 ftp = fn2ftp(infile);
1574 range_check(ftp, 0, efNR);
1578 get_coordnum(infile, natoms);
1581 in = gmx_fio_fopen(infile, "r");
1588 *natoms = read_g96_conf(in, infile, &fr, g96_line);
1594 in = gmx_fio_fopen(infile, "r");
1595 get_pdb_coordnum(in, natoms);
1599 *natoms = get_espresso_coordnum(infile);
1607 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
1608 *natoms = tpx.natoms;
1612 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
1617 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
1618 rvec x[], rvec *v, int *ePBC, matrix box)
1627 char g96_line[STRLEN+1];
1631 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
1633 else if (atoms->atom == NULL)
1635 gmx_mem("Uninitialized array atom");
1643 ftp = fn2ftp(infile);
1647 read_whole_conf(infile, title, atoms, x, v, box);
1651 fr.natoms = atoms->nr;
1656 in = gmx_fio_fopen(infile, "r");
1657 read_g96_conf(in, infile, &fr, g96_line);
1659 copy_mat(fr.box, box);
1660 strncpy(title, fr.title, STRLEN);
1665 read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL);
1668 read_espresso_conf(infile, atoms, x, v, box);
1674 i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop);
1680 strcpy(title, *(mtop->name));
1682 /* Free possibly allocated memory */
1685 *atoms = gmx_mtop_global_atoms(mtop);
1686 top = gmx_mtop_t_to_t_topology(mtop);
1687 tpx_make_chain_identifiers(atoms, &top.mols);
1690 /* The strings in the symtab are still in use in the returned t_atoms
1691 * structure, so we should not free them. But there is no place to put the
1692 * symbols; the only choice is to leak the memory...
1693 * So we clear the symbol table before freeing the topology structure. */
1694 free_symtab(&top.symtab);
1699 gmx_incons("Not supported in read_stx_conf");