2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2005, The GROMACS development team.
5 * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
44 #include "gromacs/fileio/gmxfio.h"
45 #include "gromacs/math/vec.h"
46 #include "gromacs/pbcutil/pbc.h"
47 #include "gromacs/topology/atoms.h"
48 #include "gromacs/topology/symtab.h"
49 #include "gromacs/topology/topology.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/fatalerror.h"
52 #include "gromacs/utility/smalloc.h"
54 static int get_espresso_word(FILE* fp, char word[])
66 if (i == ' ' || i == '\n' || i == '\t')
91 word[nc++] = static_cast<char>(i);
94 } while (i != EOF && ret == 0);
101 static int check_open_parenthesis(FILE* fp, int r, const char* infile, const char* keyword)
113 r = get_espresso_word(fp, word);
120 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'", keyword, infile);
127 static int check_close_parenthesis(FILE* fp, int r, const char* infile, const char* keyword)
139 r = get_espresso_word(fp, word);
146 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'", keyword, infile);
164 static const char* const esp_prop[espNR] = { "id", "pos", "type", "q", "v", "f", "molecule" };
166 void gmx_espresso_read_conf(const char* infile, t_symtab* symtab, char** name, t_atoms* atoms, rvec x[], rvec* v, matrix box)
169 char word[STRLEN], buf[STRLEN];
170 int level, r, nprop, p, i, m, molnr;
173 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
177 // No title reading implemented for espresso files
178 *name = gmx_strdup("");
183 atoms->haveMass = FALSE;
184 atoms->haveCharge = FALSE;
185 atoms->haveType = FALSE;
186 atoms->haveBState = FALSE;
187 atoms->havePdbInfo = FALSE;
189 fp = gmx_fio_fopen(infile, "r");
191 bFoundParticles = FALSE;
192 bFoundVariable = FALSE;
195 while ((r = get_espresso_word(fp, word)))
197 if (level == 1 && std::strcmp(word, "particles") == 0 && !bFoundParticles)
199 bFoundParticles = TRUE;
200 level += check_open_parenthesis(fp, r, infile, "particles");
202 while (level == 2 && (r = get_espresso_word(fp, word)))
205 for (p = 0; p < espNR; p++)
207 if (strcmp(word, esp_prop[p]) == 0)
213 atoms->haveCharge = TRUE;
218 fprintf(debug, " prop[%d] = %s\n", nprop - 1, esp_prop[prop[nprop - 1]]);
222 if (!bFoundProp && word[0] != '}')
224 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
226 if (bFoundProp && p == espMOLECULE)
237 while (level > 0 && (r = get_espresso_word(fp, word)))
249 for (p = 0; p < nprop; p++)
254 r = get_espresso_word(fp, word);
258 for (m = 0; m < 3; m++)
260 r = get_espresso_word(fp, word);
261 sscanf(word, "%lf", &d);
266 r = get_espresso_word(fp, word);
267 atoms->atom[i].type = std::strtol(word, nullptr, 10);
270 r = get_espresso_word(fp, word);
271 sscanf(word, "%lf", &d);
272 atoms->atom[i].q = d;
275 for (m = 0; m < 3; m++)
277 r = get_espresso_word(fp, word);
278 sscanf(word, "%lf", &d);
283 for (m = 0; m < 3; m++)
285 r = get_espresso_word(fp, word);
290 r = get_espresso_word(fp, word);
291 molnr = std::strtol(word, nullptr, 10);
292 if (i == 0 || atoms->resinfo[atoms->atom[i - 1].resind].nr != molnr)
294 atoms->atom[i].resind = (i == 0 ? 0 : atoms->atom[i - 1].resind + 1);
295 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
296 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
297 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
298 atoms->resinfo[atoms->atom[i].resind].chainnum =
299 molnr; /* Not sure if this is right? */
303 atoms->atom[i].resind = atoms->atom[i - 1].resind;
308 /* Generate an atom name from the particle type */
309 sprintf(buf, "T%hu", atoms->atom[i].type);
310 atoms->atomname[i] = put_symtab(symtab, buf);
313 if (i == 0 || atoms->atom[i].resind != atoms->atom[i - 1].resind)
315 atoms->resinfo[atoms->atom[i].resind].name = put_symtab(symtab, "MOL");
320 /* Residue number is the atom number */
321 atoms->atom[i].resind = i;
322 /* Generate an residue name from the particle type */
323 if (atoms->atom[i].type < 26)
325 sprintf(buf, "T%c", 'A' + atoms->atom[i].type);
329 sprintf(buf, "T%c%c", 'A' + atoms->atom[i].type / 26,
330 'A' + atoms->atom[i].type % 26);
332 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
342 atoms->nres = atoms->nr;
347 "Internal inconsistency in Espresso routines, read %d atoms, expected %d "
352 else if (level == 1 && std::strcmp(word, "variable") == 0 && !bFoundVariable)
354 bFoundVariable = TRUE;
355 level += check_open_parenthesis(fp, r, infile, "variable");
356 while (level == 2 && (r = get_espresso_word(fp, word)))
358 if (level == 2 && std::strcmp(word, "box_l") == 0)
360 for (m = 0; m < 3; m++)
362 r = get_espresso_word(fp, word);
363 sscanf(word, "%lf", &d);
366 level += check_close_parenthesis(fp, r, infile, "box_l");
380 if (!bFoundParticles)
382 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n", infile);
388 int get_espresso_coordnum(const char* infile)
392 int natoms, level, r;
393 gmx_bool bFoundParticles;
397 fp = gmx_fio_fopen(infile, "r");
399 bFoundParticles = FALSE;
401 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
403 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
405 bFoundParticles = TRUE;
406 level += check_open_parenthesis(fp, r, infile, "particles");
407 while (level > 0 && (r = get_espresso_word(fp, word)))
432 if (!bFoundParticles)
434 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n", infile);
442 void write_espresso_conf_indexed(FILE* out,
444 const t_atoms* atoms,
453 fprintf(out, "# %s\n", title);
456 gmx_warning("The Espresso format does not support triclinic unit-cells");
458 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
460 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
461 for (i = 0; i < nx; i++)
471 fprintf(out, "\t{%d %f %f %f %hu %g", j, x[j][XX], x[j][YY], x[j][ZZ], atoms->atom[j].type,
475 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);