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/g96io.h"
51 #include "gromacs/fileio/gmxfio.h"
52 #include "gromacs/fileio/pdbio.h"
53 #include "gromacs/fileio/tpxio.h"
54 #include "gromacs/fileio/trx.h"
55 #include "gromacs/fileio/trxio.h"
56 #include "gromacs/legacyheaders/copyrite.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/pbcutil/pbc.h"
59 #include "gromacs/topology/atomprop.h"
60 #include "gromacs/topology/atoms.h"
61 #include "gromacs/topology/block.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/smalloc.h"
69 static int get_espresso_word(FILE *fp, char word[])
81 if (i == ' ' || i == '\n' || i == '\t')
106 word[nc++] = (char)i;
110 while (i != EOF && ret == 0);
114 /* printf("'%s'\n",word); */
119 static int check_open_parenthesis(FILE *fp, int r,
120 const char *infile, const char *keyword)
132 r = get_espresso_word(fp, word);
139 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'",
147 static int check_close_parenthesis(FILE *fp, int r,
148 const char *infile, const char *keyword)
160 r = get_espresso_word(fp, word);
167 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'",
176 espID, espPOS, espTYPE, espQ, espV, espF, espMOLECULE, espNR
178 const char *esp_prop[espNR] = {
179 "id", "pos", "type", "q", "v", "f",
183 static void read_espresso_conf(const char *infile, char *title,
184 t_atoms *atoms, rvec x[], rvec *v, matrix box)
186 t_symtab *symtab = NULL;
188 char word[STRLEN], buf[STRLEN];
189 int level, r, nprop, p, i, m, molnr;
192 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
199 // TODO: The code does not understand titles it writes...
204 fp = gmx_fio_fopen(infile, "r");
206 bFoundParticles = FALSE;
207 bFoundVariable = FALSE;
210 while ((r = get_espresso_word(fp, word)))
212 if (level == 1 && std::strcmp(word, "particles") == 0 && !bFoundParticles)
214 bFoundParticles = TRUE;
215 level += check_open_parenthesis(fp, r, infile, "particles");
217 while (level == 2 && (r = get_espresso_word(fp, word)))
220 for (p = 0; p < espNR; p++)
222 if (strcmp(word, esp_prop[p]) == 0)
226 /* printf(" prop[%d] = %s\n",nprop-1,esp_prop[prop[nprop-1]]); */
229 if (!bFoundProp && word[0] != '}')
231 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
233 if (bFoundProp && p == espMOLECULE)
244 while (level > 0 && (r = get_espresso_word(fp, word)))
256 for (p = 0; p < nprop; p++)
261 r = get_espresso_word(fp, word);
265 for (m = 0; m < 3; m++)
267 r = get_espresso_word(fp, word);
268 sscanf(word, "%lf", &d);
273 r = get_espresso_word(fp, word);
274 atoms->atom[i].type = std::strtol(word, NULL, 10);
277 r = get_espresso_word(fp, word);
278 sscanf(word, "%lf", &d);
279 atoms->atom[i].q = d;
282 for (m = 0; m < 3; m++)
284 r = get_espresso_word(fp, word);
285 sscanf(word, "%lf", &d);
290 for (m = 0; m < 3; m++)
292 r = get_espresso_word(fp, word);
297 r = get_espresso_word(fp, word);
298 molnr = std::strtol(word, NULL, 10);
300 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
302 atoms->atom[i].resind =
303 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
304 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
305 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
306 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
307 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
311 atoms->atom[i].resind = atoms->atom[i-1].resind;
316 /* Generate an atom name from the particle type */
317 sprintf(buf, "T%d", atoms->atom[i].type);
318 atoms->atomname[i] = put_symtab(symtab, buf);
321 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
323 atoms->resinfo[atoms->atom[i].resind].name =
324 put_symtab(symtab, "MOL");
329 /* Residue number is the atom number */
330 atoms->atom[i].resind = i;
331 /* Generate an residue name from the particle type */
332 if (atoms->atom[i].type < 26)
334 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
338 sprintf(buf, "T%c%c",
339 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
341 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
351 atoms->nres = atoms->nr;
355 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
358 else if (level == 1 && std::strcmp(word, "variable") == 0 && !bFoundVariable)
360 bFoundVariable = TRUE;
361 level += check_open_parenthesis(fp, r, infile, "variable");
362 while (level == 2 && (r = get_espresso_word(fp, word)))
364 if (level == 2 && std::strcmp(word, "box_l") == 0)
366 for (m = 0; m < 3; m++)
368 r = get_espresso_word(fp, word);
369 sscanf(word, "%lf", &d);
372 level += check_close_parenthesis(fp, r, infile, "box_l");
386 if (!bFoundParticles)
388 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
395 static int get_espresso_coordnum(const char *infile)
399 int natoms, level, r;
400 gmx_bool bFoundParticles;
404 fp = gmx_fio_fopen(infile, "r");
406 bFoundParticles = FALSE;
408 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
410 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
412 bFoundParticles = TRUE;
413 level += check_open_parenthesis(fp, r, infile, "particles");
414 while (level > 0 && (r = get_espresso_word(fp, word)))
439 if (!bFoundParticles)
441 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
450 static void write_espresso_conf_indexed(FILE *out, const char *title,
451 t_atoms *atoms, int nx, atom_id *index,
452 rvec *x, rvec *v, matrix box)
456 fprintf(out, "# %s\n", title);
459 gmx_warning("The Espresso format does not support triclinic unit-cells");
461 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
463 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
464 for (i = 0; i < nx; i++)
474 fprintf(out, "\t{%d %f %f %f %d %g",
475 j, x[j][XX], x[j][YY], x[j][ZZ],
476 atoms->atom[j].type, atoms->atom[j].q);
479 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
486 static void get_coordnum_fp (FILE *in, char *title, int *natoms)
490 fgets2 (title, STRLEN, in);
491 fgets2 (line, STRLEN, in);
492 if (sscanf (line, "%d", natoms) != 1)
494 gmx_fatal(FARGS, "gro file does not have the number of atoms on the second line");
498 static void get_coordnum (const char *infile, int *natoms)
503 in = gmx_fio_fopen(infile, "r");
504 get_coordnum_fp(in, title, natoms);
508 static gmx_bool get_w_conf(FILE *in, const char *infile, char *title,
509 t_symtab *symtab, t_atoms *atoms, int *ndec,
510 rvec x[], rvec *v, matrix box)
513 char resname[6], oldresname[6];
514 char line[STRLEN+1], *ptr;
516 double x1, y1, z1, x2, y2, z2;
518 int natoms, i, m, resnr, newres, oldres, ddist, c;
519 gmx_bool bFirst, bVel;
523 oldres = -12345; /* Unlikely number for the first residue! */
526 /* Read the title and number of atoms */
527 get_coordnum_fp(in, title, &natoms);
529 if (natoms > atoms->nr)
531 gmx_fatal(FARGS, "gro file contains more atoms (%d) than expected (%d)",
534 else if (natoms < atoms->nr)
536 fprintf(stderr, "Warning: gro file contains less atoms (%d) than expected"
537 " (%d)\n", natoms, atoms->nr);
545 oldresname[0] = '\0';
547 /* just pray the arrays are big enough */
548 for (i = 0; (i < natoms); i++)
550 if ((fgets2 (line, STRLEN, in)) == NULL)
552 gmx_fatal(FARGS, "Unexpected end of file in file %s at line %d",
555 if (strlen(line) < 39)
557 gmx_fatal(FARGS, "Invalid line in %s for atom %d:\n%s", infile, i+1, line);
560 /* determine read precision from distance between periods
565 p1 = strchr(line, '.');
568 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
570 p2 = strchr(&p1[1], '.');
573 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
578 p3 = strchr(&p2[1], '.');
581 gmx_fatal(FARGS, "A coordinate in file %s does not contain a '.'", infile);
584 if (p3 - p2 != ddist)
586 gmx_fatal(FARGS, "The spacing of the decimal points in file %s is not consistent for x, y and z", infile);
591 memcpy(name, line, 5);
593 sscanf(name, "%d", &resnr);
594 sscanf(line+5, "%5s", resname);
596 if (resnr != oldres || strncmp(resname, oldresname, sizeof(resname)))
600 if (newres >= natoms)
602 gmx_fatal(FARGS, "More residues than atoms in %s (natoms = %d)",
605 atoms->atom[i].resind = newres;
606 t_atoms_set_resinfo(atoms, i, symtab, resname, resnr, ' ', 0, ' ');
610 atoms->atom[i].resind = newres;
614 std::memcpy(name, line+10, 5);
615 atoms->atomname[i] = put_symtab(symtab, name);
617 /* Copy resname to oldresname after we are done with the sanity check above */
618 std::strncpy(oldresname, resname, sizeof(oldresname));
620 /* eventueel controle atomnumber met i+1 */
622 /* coordinates (start after residue data) */
624 /* Read fixed format */
625 for (m = 0; m < DIM; m++)
627 for (c = 0; (c < ddist && ptr[0]); c++)
633 if (sscanf (buf, "%lf %lf", &x1, &x2) != 1)
635 gmx_fatal(FARGS, "Something is wrong in the coordinate formatting of file %s. Note that gro is fixed format (see the manual)", infile);
643 /* velocities (start after residues and coordinates) */
646 /* Read fixed format */
647 for (m = 0; m < DIM; m++)
649 for (c = 0; (c < ddist && ptr[0]); c++)
655 if (sscanf (buf, "%lf", &x1) != 1)
667 atoms->nres = newres + 1;
670 fgets2 (line, STRLEN, in);
671 if (sscanf (line, "%lf%lf%lf", &x1, &y1, &z1) != 3)
673 gmx_warning("Bad box in file %s", infile);
675 /* Generate a cubic box */
676 for (m = 0; (m < DIM); m++)
678 xmin[m] = xmax[m] = x[0][m];
680 for (i = 1; (i < atoms->nr); i++)
682 for (m = 0; (m < DIM); m++)
684 xmin[m] = std::min(xmin[m], x[i][m]);
685 xmax[m] = std::max(xmax[m], x[i][m]);
688 for (i = 0; i < DIM; i++)
690 for (m = 0; m < DIM; m++)
695 for (m = 0; (m < DIM); m++)
697 box[m][m] = (xmax[m]-xmin[m]);
699 fprintf(stderr, "Generated a cubic box %8.3f x %8.3f x %8.3f\n",
700 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
704 /* We found the first three values, the diagonal elements */
708 if (sscanf (line, "%*f%*f%*f%lf%lf%lf%lf%lf%lf",
709 &x1, &y1, &z1, &x2, &y2, &z2) != 6)
711 x1 = y1 = z1 = x2 = y2 = z2 = 0.0;
724 static void read_whole_conf(const char *infile, char *title,
725 t_atoms *atoms, rvec x[], rvec *v, matrix box)
732 in = gmx_fio_fopen(infile, "r");
734 open_symtab(&symtab);
735 get_w_conf(in, infile, title, &symtab, atoms, &ndec, x, v, box);
736 /* We can't free the symbols, as they are still used in atoms, so
737 * the only choice is to leak them. */
738 free_symtab(&symtab);
743 static gmx_bool gmx_one_before_eof(FILE *fp)
748 if ((beof = fread(data, 1, 1, fp)) == 1)
750 gmx_fseek(fp, -1, SEEK_CUR);
755 gmx_bool gro_next_x_or_v(FILE *status, t_trxframe *fr)
759 char title[STRLEN], *p;
763 if (gmx_one_before_eof(status))
768 open_symtab(&symtab);
769 atoms.nr = fr->natoms;
770 snew(atoms.atom, fr->natoms);
771 atoms.nres = fr->natoms;
772 snew(atoms.resinfo, fr->natoms);
773 snew(atoms.atomname, fr->natoms);
775 fr->bV = get_w_conf(status, title, title, &symtab, &atoms, &ndec, fr->x, fr->v, fr->box);
778 /* prec = 10^ndec: */
779 for (i = 0; i < ndec; i++)
789 sfree(atoms.resinfo);
790 sfree(atoms.atomname);
791 done_symtab(&symtab);
793 if ((p = strstr(title, "t=")) != NULL)
796 if (sscanf(p, "%lf", &tt) == 1)
808 if (atoms.nr != fr->natoms)
810 gmx_fatal(FARGS, "Number of atoms in gro frame (%d) doesn't match the number in the previous frame (%d)", atoms.nr, fr->natoms);
816 int gro_first_x_or_v(FILE *status, t_trxframe *fr)
821 fprintf(stderr, "Reading frames from gro file");
822 get_coordnum_fp(status, title, &fr->natoms);
824 fprintf(stderr, " '%s', %d atoms.\n", title, fr->natoms);
829 gmx_file("No coordinates in gro file");
832 snew(fr->x, fr->natoms);
833 snew(fr->v, fr->natoms);
834 gro_next_x_or_v(status, fr);
839 static void make_hconf_format(int pr, gmx_bool bVel, char format[])
843 /* build format string for printing,
844 something like "%8.3f" for x and "%8.4f" for v */
857 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
858 l, pr, l, pr, l, pr, l, vpr, l, vpr, l, vpr);
862 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
867 static void write_hconf_box(FILE *out, int pr, matrix box)
878 if (box[XX][YY] || box[XX][ZZ] || box[YY][XX] || box[YY][ZZ] ||
879 box[ZZ][XX] || box[ZZ][YY])
881 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df"
882 "%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df%%%d.%df\n",
883 l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr, l, pr);
885 box[XX][XX], box[YY][YY], box[ZZ][ZZ],
886 box[XX][YY], box[XX][ZZ], box[YY][XX],
887 box[YY][ZZ], box[ZZ][XX], box[ZZ][YY]);
891 sprintf(format, "%%%d.%df%%%d.%df%%%d.%df\n", l, pr, l, pr, l, pr);
893 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
897 void write_hconf_indexed_p(FILE *out, const char *title, t_atoms *atoms,
898 int nx, const atom_id index[], int pr,
899 rvec *x, rvec *v, matrix box)
901 char resnm[6], nm[6], format[100];
902 int ai, i, resind, resnr;
905 fprintf (out, "%s\n", (title && title[0]) ? title : format);
906 fprintf (out, "%5d\n", nx);
908 make_hconf_format(pr, v != NULL, format);
910 for (i = 0; (i < nx); i++)
914 resind = atoms->atom[ai].resind;
915 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
916 if (resind < atoms->nres)
918 std::strncpy(resnm, *atoms->resinfo[resind].name, sizeof(resnm)-1);
919 resnr = atoms->resinfo[resind].nr;
923 std::strncpy(resnm, " ??? ", sizeof(resnm)-1);
929 std::strncpy(nm, *atoms->atomname[ai], sizeof(nm)-1);
933 std::strncpy(nm, " ??? ", sizeof(nm)-1);
936 fprintf(out, "%5d%-5.5s%5.5s%5d", resnr%100000, resnm, nm, (ai+1)%100000);
937 /* next fprintf uses built format string */
941 x[ai][XX], x[ai][YY], x[ai][ZZ], v[ai][XX], v[ai][YY], v[ai][ZZ]);
946 x[ai][XX], x[ai][YY], x[ai][ZZ]);
950 write_hconf_box(out, pr, box);
955 static void write_hconf_mtop(FILE *out, const char *title, gmx_mtop_t *mtop,
957 rvec *x, rvec *v, matrix box)
961 gmx_mtop_atomloop_all_t aloop;
963 char *atomname, *resname;
966 fprintf (out, "%s\n", (title && title[0]) ? title : format);
967 fprintf (out, "%5d\n", mtop->natoms);
969 make_hconf_format(pr, v != NULL, format);
971 aloop = gmx_mtop_atomloop_all_init(mtop);
972 while (gmx_mtop_atomloop_all_next(aloop, &i, &atom))
974 gmx_mtop_atomloop_all_names(aloop, &atomname, &resnr, &resname);
976 fprintf(out, "%5d%-5.5s%5.5s%5d",
977 resnr%100000, resname, atomname, (i+1)%100000);
978 /* next fprintf uses built format string */
982 x[i][XX], x[i][YY], x[i][ZZ], v[i][XX], v[i][YY], v[i][ZZ]);
987 x[i][XX], x[i][YY], x[i][ZZ]);
991 write_hconf_box(out, pr, box);
996 void write_hconf_p(FILE *out, const char *title, t_atoms *atoms, int pr,
997 rvec *x, rvec *v, matrix box)
1002 snew(aa, atoms->nr);
1003 for (i = 0; (i < atoms->nr); i++)
1007 write_hconf_indexed_p(out, title, atoms, atoms->nr, aa, pr, x, v, box);
1011 void write_conf_p(const char *outfile, const char *title,
1012 t_atoms *atoms, int pr,
1013 rvec *x, rvec *v, matrix box)
1017 out = gmx_fio_fopen(outfile, "w");
1018 write_hconf_p(out, title, atoms, pr, x, v, box);
1020 gmx_fio_fclose (out);
1023 static void write_conf(const char *outfile, const char *title, t_atoms *atoms,
1024 rvec *x, rvec *v, matrix box)
1026 write_conf_p(outfile, title, atoms, 3, x, v, box);
1029 void write_sto_conf_indexed(const char *outfile, const char *title,
1031 rvec x[], rvec *v, int ePBC, matrix box,
1032 atom_id nindex, atom_id index[])
1038 ftp = fn2ftp(outfile);
1042 out = gmx_fio_fopen(outfile, "w");
1043 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
1044 gmx_fio_fclose(out);
1047 clear_trxframe(&fr, TRUE);
1050 fr.natoms = atoms->nr;
1061 copy_mat(box, fr.box);
1062 out = gmx_fio_fopen(outfile, "w");
1063 write_g96_conf(out, &fr, nindex, index);
1064 gmx_fio_fclose(out);
1070 out = gmx_fio_fopen(outfile, "w");
1071 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
1072 gmx_fio_fclose(out);
1075 out = gmx_fio_fopen(outfile, "w");
1076 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
1077 gmx_fio_fclose(out);
1080 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1083 gmx_incons("Not supported in write_sto_conf_indexed");
1087 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
1088 rvec x[], rvec *v, int ePBC, matrix box)
1094 ftp = fn2ftp(outfile);
1098 write_conf(outfile, title, atoms, x, v, box);
1101 clear_trxframe(&fr, TRUE);
1104 fr.natoms = atoms->nr;
1115 copy_mat(box, fr.box);
1116 out = gmx_fio_fopen(outfile, "w");
1117 write_g96_conf(out, &fr, -1, NULL);
1118 gmx_fio_fclose(out);
1123 out = gmx_fio_fopen(outfile, "w");
1124 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
1125 gmx_fio_fclose(out);
1128 out = gmx_fio_fopen(outfile, "w");
1129 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
1130 gmx_fio_fclose(out);
1133 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
1136 gmx_incons("Not supported in write_sto_conf");
1140 void write_sto_conf_mtop(const char *outfile, const char *title,
1142 rvec x[], rvec *v, int ePBC, matrix box)
1148 ftp = fn2ftp(outfile);
1152 out = gmx_fio_fopen(outfile, "w");
1153 write_hconf_mtop(out, title, mtop, 3, x, v, box);
1154 gmx_fio_fclose(out);
1157 /* This is a brute force approach which requires a lot of memory.
1158 * We should implement mtop versions of all writing routines.
1160 atoms = gmx_mtop_global_atoms(mtop);
1162 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
1169 void get_stx_coordnum(const char *infile, int *natoms)
1172 int ftp, tpxver, tpxgen;
1174 char g96_line[STRLEN+1];
1176 ftp = fn2ftp(infile);
1177 range_check(ftp, 0, efNR);
1181 get_coordnum(infile, natoms);
1184 in = gmx_fio_fopen(infile, "r");
1191 *natoms = read_g96_conf(in, infile, &fr, g96_line);
1197 in = gmx_fio_fopen(infile, "r");
1198 get_pdb_coordnum(in, natoms);
1202 *natoms = get_espresso_coordnum(infile);
1208 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
1209 *natoms = tpx.natoms;
1213 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
1218 static void tpx_make_chain_identifiers(t_atoms *atoms, t_block *mols)
1220 int m, a, a0, a1, r;
1224 /* We always assign a new chain number, but save the chain id characters
1225 * for larger molecules.
1227 #define CHAIN_MIN_ATOMS 15
1231 for (m = 0; m < mols->nr; m++)
1233 a0 = mols->index[m];
1234 a1 = mols->index[m+1];
1235 if ((a1-a0 >= CHAIN_MIN_ATOMS) && (chainid <= 'Z'))
1244 for (a = a0; a < a1; a++)
1246 atoms->resinfo[atoms->atom[a].resind].chainnum = chainnum;
1247 atoms->resinfo[atoms->atom[a].resind].chainid = c;
1252 /* Blank out the chain id if there was only one chain */
1255 for (r = 0; r < atoms->nres; r++)
1257 atoms->resinfo[r].chainid = ' ';
1262 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
1263 rvec x[], rvec *v, int *ePBC, matrix box)
1270 char g96_line[STRLEN+1];
1274 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
1276 else if (atoms->atom == NULL)
1278 gmx_mem("Uninitialized array atom");
1286 ftp = fn2ftp(infile);
1290 read_whole_conf(infile, title, atoms, x, v, box);
1294 fr.natoms = atoms->nr;
1299 in = gmx_fio_fopen(infile, "r");
1300 read_g96_conf(in, infile, &fr, g96_line);
1302 copy_mat(fr.box, box);
1303 std::strncpy(title, fr.title, STRLEN);
1308 read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL);
1311 read_espresso_conf(infile, title, atoms, x, v, box);
1315 i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop);
1321 strcpy(title, *(mtop->name));
1323 /* Free possibly allocated memory */
1326 *atoms = gmx_mtop_global_atoms(mtop);
1327 top = gmx_mtop_t_to_t_topology(mtop);
1328 tpx_make_chain_identifiers(atoms, &top.mols);
1331 /* The strings in the symtab are still in use in the returned t_atoms
1332 * structure, so we should not free them. But there is no place to put the
1333 * symbols; the only choice is to leak the memory...
1334 * So we clear the symbol table before freeing the topology structure. */
1335 free_symtab(&top.symtab);
1340 gmx_incons("Not supported in read_stx_conf");
1344 static void done_gmx_groups_t(gmx_groups_t *g)
1348 for (i = 0; (i < egcNR); i++)
1350 if (NULL != g->grps[i].nm_ind)
1352 sfree(g->grps[i].nm_ind);
1353 g->grps[i].nm_ind = NULL;
1355 if (NULL != g->grpnr[i])
1361 /* The contents of this array is in symtab, don't free it here */
1365 gmx_bool read_tps_conf(const char *infile, char *title, t_topology *top, int *ePBC,
1366 rvec **x, rvec **v, matrix box, gmx_bool bMass)
1369 int natoms, i, version, generation;
1370 gmx_bool bTop, bXNULL = FALSE;
1374 bTop = fn2bTPX(infile);
1378 read_tpxheader(infile, &header, TRUE, &version, &generation);
1381 snew(*x, header.natoms);
1385 snew(*v, header.natoms);
1388 *ePBC = read_tpx(infile, NULL, box, &natoms,
1389 (x == NULL) ? NULL : *x, (v == NULL) ? NULL : *v, NULL, mtop);
1390 *top = gmx_mtop_t_to_t_topology(mtop);
1391 /* In this case we need to throw away the group data too */
1392 done_gmx_groups_t(&mtop->groups);
1394 std::strcpy(title, *top->name);
1395 tpx_make_chain_identifiers(&top->atoms, &top->mols);
1399 get_stx_coordnum(infile, &natoms);
1400 init_t_atoms(&top->atoms, natoms, (fn2ftp(infile) == efPDB));
1411 read_stx_conf(infile, title, &top->atoms, *x, (v == NULL) ? NULL : *v, ePBC, box);
1419 aps = gmx_atomprop_init();
1420 for (i = 0; (i < natoms); i++)
1422 if (!gmx_atomprop_query(aps, epropMass,
1423 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
1424 *top->atoms.atomname[i],
1425 &(top->atoms.atom[i].m)))
1429 fprintf(debug, "Can not find mass for atom %s %d %s, setting to 1\n",
1430 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
1431 top->atoms.resinfo[top->atoms.atom[i].resind].nr,
1432 *top->atoms.atomname[i]);
1436 gmx_atomprop_destroy(aps);
1438 top->idef.ntypes = -1;