Refactor SD update
[gromacs.git] / src / gromacs / fileio / espio.cpp
blob34eaa711b3aedd51f4864ec1aa067ce2224b748e
1 /*
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, 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.
36 #include "gmxpre.h"
38 #include "espio.h"
40 #include <cstdio>
41 #include <cstdlib>
42 #include <cstring>
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[])
56 int ret, nc, i;
58 ret = 0;
59 nc = 0;
63 i = fgetc(fp);
64 if (i != EOF)
66 if (i == ' ' || i == '\n' || i == '\t')
68 if (nc > 0)
70 ret = 1;
73 else if (i == '{')
75 if (nc == 0)
77 word[nc++] = '{';
79 ret = 2;
81 else if (i == '}')
83 if (nc == 0)
85 word[nc++] = '}';
87 ret = 3;
89 else
91 word[nc++] = (char)i;
95 while (i != EOF && ret == 0);
97 word[nc] = '\0';
99 return ret;
102 static int check_open_parenthesis(FILE *fp, int r,
103 const char *infile, const char *keyword)
105 int level_inc;
106 char word[STRLEN];
108 level_inc = 0;
109 if (r == 2)
111 level_inc++;
113 else
115 r = get_espresso_word(fp, word);
116 if (r == 2)
118 level_inc++;
120 else
122 gmx_fatal(FARGS, "Expected '{' after '%s' in file '%s'",
123 keyword, infile);
127 return level_inc;
130 static int check_close_parenthesis(FILE *fp, int r,
131 const char *infile, const char *keyword)
133 int level_inc;
134 char word[STRLEN];
136 level_inc = 0;
137 if (r == 3)
139 level_inc--;
141 else
143 r = get_espresso_word(fp, word);
144 if (r == 3)
146 level_inc--;
148 else
150 gmx_fatal(FARGS, "Expected '}' after section '%s' in file '%s'",
151 keyword, infile);
155 return level_inc;
158 enum {
159 espID, espPOS, espTYPE, espQ, espV, espF, espMOLECULE, espNR
161 static const char *const esp_prop[espNR] = {
162 "id", "pos", "type", "q", "v", "f",
163 "molecule"
166 void gmx_espresso_read_conf(const char *infile,
167 t_symtab *symtab, char **name, t_atoms *atoms,
168 rvec x[], rvec *v, matrix box)
170 FILE *fp;
171 char word[STRLEN], buf[STRLEN];
172 int level, r, nprop, p, i, m, molnr;
173 int prop[32];
174 double d;
175 gmx_bool bFoundParticles, bFoundProp, bFoundVariable, bMol;
177 if (name != nullptr)
179 // No title reading implemented for espresso files
180 *name = gmx_strdup("");
183 clear_mat(box);
185 atoms->haveMass = FALSE;
186 atoms->haveCharge = FALSE;
187 atoms->haveType = FALSE;
188 atoms->haveBState = FALSE;
189 atoms->havePdbInfo = FALSE;
191 fp = gmx_fio_fopen(infile, "r");
193 bFoundParticles = FALSE;
194 bFoundVariable = FALSE;
195 bMol = FALSE;
196 level = 0;
197 while ((r = get_espresso_word(fp, word)))
199 if (level == 1 && std::strcmp(word, "particles") == 0 && !bFoundParticles)
201 bFoundParticles = TRUE;
202 level += check_open_parenthesis(fp, r, infile, "particles");
203 nprop = 0;
204 while (level == 2 && (r = get_espresso_word(fp, word)))
206 bFoundProp = FALSE;
207 for (p = 0; p < espNR; p++)
209 if (strcmp(word, esp_prop[p]) == 0)
211 bFoundProp = TRUE;
212 prop[nprop++] = p;
213 if (p == espQ)
215 atoms->haveCharge = TRUE;
218 if (debug)
220 fprintf(debug, " prop[%d] = %s\n", nprop-1, esp_prop[prop[nprop-1]]);
224 if (!bFoundProp && word[0] != '}')
226 gmx_fatal(FARGS, "Can not read Espresso files with particle property '%s'", word);
228 if (bFoundProp && p == espMOLECULE)
230 bMol = TRUE;
232 if (r == 3)
234 level--;
238 i = 0;
239 while (level > 0 && (r = get_espresso_word(fp, word)))
241 if (r == 2)
243 level++;
245 else if (r == 3)
247 level--;
249 if (level == 2)
251 for (p = 0; p < nprop; p++)
253 switch (prop[p])
255 case espID:
256 r = get_espresso_word(fp, word);
257 /* Not used */
258 break;
259 case espPOS:
260 for (m = 0; m < 3; m++)
262 r = get_espresso_word(fp, word);
263 sscanf(word, "%lf", &d);
264 x[i][m] = d;
266 break;
267 case espTYPE:
268 r = get_espresso_word(fp, word);
269 atoms->atom[i].type = std::strtol(word, nullptr, 10);
270 break;
271 case espQ:
272 r = get_espresso_word(fp, word);
273 sscanf(word, "%lf", &d);
274 atoms->atom[i].q = d;
275 break;
276 case espV:
277 for (m = 0; m < 3; m++)
279 r = get_espresso_word(fp, word);
280 sscanf(word, "%lf", &d);
281 v[i][m] = d;
283 break;
284 case espF:
285 for (m = 0; m < 3; m++)
287 r = get_espresso_word(fp, word);
288 /* not used */
290 break;
291 case espMOLECULE:
292 r = get_espresso_word(fp, word);
293 molnr = std::strtol(word, nullptr, 10);
294 if (i == 0 ||
295 atoms->resinfo[atoms->atom[i-1].resind].nr != molnr)
297 atoms->atom[i].resind =
298 (i == 0 ? 0 : atoms->atom[i-1].resind+1);
299 atoms->resinfo[atoms->atom[i].resind].nr = molnr;
300 atoms->resinfo[atoms->atom[i].resind].ic = ' ';
301 atoms->resinfo[atoms->atom[i].resind].chainid = ' ';
302 atoms->resinfo[atoms->atom[i].resind].chainnum = molnr; /* Not sure if this is right? */
304 else
306 atoms->atom[i].resind = atoms->atom[i-1].resind;
308 break;
311 /* Generate an atom name from the particle type */
312 sprintf(buf, "T%hu", atoms->atom[i].type);
313 atoms->atomname[i] = put_symtab(symtab, buf);
314 if (bMol)
316 if (i == 0 || atoms->atom[i].resind != atoms->atom[i-1].resind)
318 atoms->resinfo[atoms->atom[i].resind].name =
319 put_symtab(symtab, "MOL");
322 else
324 /* Residue number is the atom number */
325 atoms->atom[i].resind = i;
326 /* Generate an residue name from the particle type */
327 if (atoms->atom[i].type < 26)
329 sprintf(buf, "T%c", 'A'+atoms->atom[i].type);
331 else
333 sprintf(buf, "T%c%c",
334 'A'+atoms->atom[i].type/26, 'A'+atoms->atom[i].type%26);
336 t_atoms_set_resinfo(atoms, i, symtab, buf, i, ' ', 0, ' ');
339 if (r == 3)
341 level--;
343 i++;
346 atoms->nres = atoms->nr;
348 if (i != atoms->nr)
350 gmx_fatal(FARGS, "Internal inconsistency in Espresso routines, read %d atoms, expected %d atoms", i, atoms->nr);
353 else if (level == 1 && std::strcmp(word, "variable") == 0 && !bFoundVariable)
355 bFoundVariable = TRUE;
356 level += check_open_parenthesis(fp, r, infile, "variable");
357 while (level == 2 && (r = get_espresso_word(fp, word)))
359 if (level == 2 && std::strcmp(word, "box_l") == 0)
361 for (m = 0; m < 3; m++)
363 r = get_espresso_word(fp, word);
364 sscanf(word, "%lf", &d);
365 box[m][m] = d;
367 level += check_close_parenthesis(fp, r, infile, "box_l");
371 else if (r == 2)
373 level++;
375 else if (r == 3)
377 level--;
381 if (!bFoundParticles)
383 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
384 infile);
387 gmx_fio_fclose(fp);
390 int get_espresso_coordnum(const char *infile)
392 FILE *fp;
393 char word[STRLEN];
394 int natoms, level, r;
395 gmx_bool bFoundParticles;
397 natoms = 0;
399 fp = gmx_fio_fopen(infile, "r");
401 bFoundParticles = FALSE;
402 level = 0;
403 while ((r = get_espresso_word(fp, word)) && !bFoundParticles)
405 if (level == 1 && strcmp(word, "particles") == 0 && !bFoundParticles)
407 bFoundParticles = TRUE;
408 level += check_open_parenthesis(fp, r, infile, "particles");
409 while (level > 0 && (r = get_espresso_word(fp, word)))
411 if (r == 2)
413 level++;
414 if (level == 2)
416 natoms++;
419 else if (r == 3)
421 level--;
425 else if (r == 2)
427 level++;
429 else if (r == 3)
431 level--;
434 if (!bFoundParticles)
436 fprintf(stderr, "Did not find a particles section in Espresso file '%s'\n",
437 infile);
440 gmx_fio_fclose(fp);
442 return natoms;
445 void write_espresso_conf_indexed(FILE *out, const char *title,
446 const t_atoms *atoms, int nx, int *index,
447 const rvec *x, const rvec *v, const matrix box)
449 int i, j;
451 fprintf(out, "# %s\n", title);
452 if (TRICLINIC(box))
454 gmx_warning("The Espresso format does not support triclinic unit-cells");
456 fprintf(out, "{variable {box_l %f %f %f}}\n", box[0][0], box[1][1], box[2][2]);
458 fprintf(out, "{particles {id pos type q%s}\n", v ? " v" : "");
459 for (i = 0; i < nx; i++)
461 if (index)
463 j = index[i];
465 else
467 j = i;
469 fprintf(out, "\t{%d %f %f %f %hu %g",
470 j, x[j][XX], x[j][YY], x[j][ZZ],
471 atoms->atom[j].type, atoms->atom[j].q);
472 if (v)
474 fprintf(out, " %f %f %f", v[j][XX], v[j][YY], v[j][ZZ]);
476 fprintf(out, "}\n");
478 fprintf(out, "}\n");