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/groio.h"
53 #include "gromacs/fileio/pdbio.h"
54 #include "gromacs/fileio/tpxio.h"
55 #include "gromacs/fileio/trx.h"
56 #include "gromacs/fileio/trxio.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 void write_sto_conf_indexed(const char *outfile, const char *title,
488 rvec x[], rvec *v, int ePBC, matrix box,
489 atom_id nindex, atom_id index[])
495 ftp = fn2ftp(outfile);
499 out = gmx_fio_fopen(outfile, "w");
500 write_hconf_indexed_p(out, title, atoms, nindex, index, 3, x, v, box);
504 clear_trxframe(&fr, TRUE);
507 fr.natoms = atoms->nr;
518 copy_mat(box, fr.box);
519 out = gmx_fio_fopen(outfile, "w");
520 write_g96_conf(out, &fr, nindex, index);
527 out = gmx_fio_fopen(outfile, "w");
528 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, NULL, TRUE);
532 out = gmx_fio_fopen(outfile, "w");
533 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
537 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
540 gmx_incons("Not supported in write_sto_conf_indexed");
544 void write_sto_conf(const char *outfile, const char *title, t_atoms *atoms,
545 rvec x[], rvec *v, int ePBC, matrix box)
551 ftp = fn2ftp(outfile);
555 write_conf_p(outfile, title, atoms, 3, x, v, box);
558 clear_trxframe(&fr, TRUE);
561 fr.natoms = atoms->nr;
572 copy_mat(box, fr.box);
573 out = gmx_fio_fopen(outfile, "w");
574 write_g96_conf(out, &fr, -1, NULL);
580 out = gmx_fio_fopen(outfile, "w");
581 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, NULL, TRUE);
585 out = gmx_fio_fopen(outfile, "w");
586 write_espresso_conf_indexed(out, title, atoms, atoms->nr, NULL, x, v, box);
590 gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
593 gmx_incons("Not supported in write_sto_conf");
597 void write_sto_conf_mtop(const char *outfile, const char *title,
599 rvec x[], rvec *v, int ePBC, matrix box)
605 ftp = fn2ftp(outfile);
609 out = gmx_fio_fopen(outfile, "w");
610 write_hconf_mtop(out, title, mtop, 3, x, v, box);
614 /* This is a brute force approach which requires a lot of memory.
615 * We should implement mtop versions of all writing routines.
617 atoms = gmx_mtop_global_atoms(mtop);
619 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
626 void get_stx_coordnum(const char *infile, int *natoms)
629 int ftp, tpxver, tpxgen;
631 char g96_line[STRLEN+1];
633 ftp = fn2ftp(infile);
634 range_check(ftp, 0, efNR);
638 get_coordnum(infile, natoms);
641 in = gmx_fio_fopen(infile, "r");
648 *natoms = read_g96_conf(in, infile, &fr, g96_line);
654 in = gmx_fio_fopen(infile, "r");
655 get_pdb_coordnum(in, natoms);
659 *natoms = get_espresso_coordnum(infile);
665 read_tpxheader(infile, &tpx, TRUE, &tpxver, &tpxgen);
666 *natoms = tpx.natoms;
670 gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum",
675 static void tpx_make_chain_identifiers(t_atoms *atoms, t_block *mols)
681 /* We always assign a new chain number, but save the chain id characters
682 * for larger molecules.
684 #define CHAIN_MIN_ATOMS 15
688 for (m = 0; m < mols->nr; m++)
691 a1 = mols->index[m+1];
692 if ((a1-a0 >= CHAIN_MIN_ATOMS) && (chainid <= 'Z'))
701 for (a = a0; a < a1; a++)
703 atoms->resinfo[atoms->atom[a].resind].chainnum = chainnum;
704 atoms->resinfo[atoms->atom[a].resind].chainid = c;
709 /* Blank out the chain id if there was only one chain */
712 for (r = 0; r < atoms->nres; r++)
714 atoms->resinfo[r].chainid = ' ';
719 void read_stx_conf(const char *infile, char *title, t_atoms *atoms,
720 rvec x[], rvec *v, int *ePBC, matrix box)
727 char g96_line[STRLEN+1];
731 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
733 else if (atoms->atom == NULL)
735 gmx_mem("Uninitialized array atom");
743 ftp = fn2ftp(infile);
747 read_whole_conf(infile, title, atoms, x, v, box);
751 fr.natoms = atoms->nr;
756 in = gmx_fio_fopen(infile, "r");
757 read_g96_conf(in, infile, &fr, g96_line);
759 copy_mat(fr.box, box);
760 std::strncpy(title, fr.title, STRLEN);
765 read_pdb_conf(infile, title, atoms, x, ePBC, box, TRUE, NULL);
768 read_espresso_conf(infile, title, atoms, x, v, box);
772 i = read_tpx(infile, NULL, box, &natoms, x, v, NULL, mtop);
778 strcpy(title, *(mtop->name));
780 /* Free possibly allocated memory */
783 *atoms = gmx_mtop_global_atoms(mtop);
784 top = gmx_mtop_t_to_t_topology(mtop);
785 tpx_make_chain_identifiers(atoms, &top.mols);
788 /* The strings in the symtab are still in use in the returned t_atoms
789 * structure, so we should not free them. But there is no place to put the
790 * symbols; the only choice is to leak the memory...
791 * So we clear the symbol table before freeing the topology structure. */
792 free_symtab(&top.symtab);
797 gmx_incons("Not supported in read_stx_conf");
801 static void done_gmx_groups_t(gmx_groups_t *g)
805 for (i = 0; (i < egcNR); i++)
807 if (NULL != g->grps[i].nm_ind)
809 sfree(g->grps[i].nm_ind);
810 g->grps[i].nm_ind = NULL;
812 if (NULL != g->grpnr[i])
818 /* The contents of this array is in symtab, don't free it here */
822 gmx_bool read_tps_conf(const char *infile, char *title, t_topology *top, int *ePBC,
823 rvec **x, rvec **v, matrix box, gmx_bool bMass)
826 int natoms, i, version, generation;
827 gmx_bool bTop, bXNULL = FALSE;
831 bTop = fn2bTPX(infile);
835 read_tpxheader(infile, &header, TRUE, &version, &generation);
838 snew(*x, header.natoms);
842 snew(*v, header.natoms);
845 *ePBC = read_tpx(infile, NULL, box, &natoms,
846 (x == NULL) ? NULL : *x, (v == NULL) ? NULL : *v, NULL, mtop);
847 *top = gmx_mtop_t_to_t_topology(mtop);
848 /* In this case we need to throw away the group data too */
849 done_gmx_groups_t(&mtop->groups);
851 std::strcpy(title, *top->name);
852 tpx_make_chain_identifiers(&top->atoms, &top->mols);
856 get_stx_coordnum(infile, &natoms);
857 init_t_atoms(&top->atoms, natoms, (fn2ftp(infile) == efPDB));
868 read_stx_conf(infile, title, &top->atoms, *x, (v == NULL) ? NULL : *v, ePBC, box);
876 aps = gmx_atomprop_init();
877 for (i = 0; (i < natoms); i++)
879 if (!gmx_atomprop_query(aps, epropMass,
880 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
881 *top->atoms.atomname[i],
882 &(top->atoms.atom[i].m)))
886 fprintf(debug, "Can not find mass for atom %s %d %s, setting to 1\n",
887 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
888 top->atoms.resinfo[top->atoms.atom[i].resind].nr,
889 *top->atoms.atomname[i]);
893 gmx_atomprop_destroy(aps);
895 top->idef.ntypes = -1;