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,2016,2017,2018,2019, 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.
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/fileio/oenv.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/utility/binaryinformation.h"
51 #include "gromacs/utility/coolstuff.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/futil.h"
56 #include "gromacs/utility/smalloc.h"
57 #include "gromacs/utility/sysinfo.h"
59 gmx_bool output_env_get_print_xvgr_codes(const gmx_output_env_t* oenv)
63 xvg_format = output_env_get_xvg_format(oenv);
65 return (xvg_format == exvgXMGRACE || xvg_format == exvgXMGR);
68 static char* xvgrstr(const std::string& gmx, const gmx_output_env_t* oenv, char* buf, int buflen)
70 /* Supported greek letter names and corresponding xmgrace/xmgr symbols */
71 const char* sym[] = { "beta", "chi", "delta", "eta", "lambda", "mu",
72 "omega", "phi", "psi", "rho", "theta", nullptr };
73 const char symc[] = { 'b', 'c', 'd', 'h', 'l', 'm', 'w', 'f', 'y', 'r', 'q', '\0' };
79 xvgf = output_env_get_xvg_format(oenv);
80 bXVGR = (xvgf == exvgXMGRACE || xvgf == exvgXMGR);
84 while (gmx[g] != '\0')
86 /* Check with the largest string we have ("lambda"), add one for \0 */
87 if (b + 6 + 1 >= buflen)
90 "Output buffer length in xvgstr (%d) too small to process xvg input string "
111 else if (gmx[g] == 'S')
125 else if (gmx[g] == 'N')
127 /* End sub/superscript */
135 if (gmx[g + 1] != ' ')
142 else if (gmx[g] == '4')
144 /* Backward compatibility for xmgr normal font "\4" */
147 case exvgXMGRACE: sprintf(buf + b, "%s", "\\f{}"); break;
148 case exvgXMGR: sprintf(buf + b, "%s", "\\4"); break;
149 default: buf[b] = '\0'; break;
154 else if (gmx[g] == '8')
156 /* Backward compatibility for xmgr symbol font "\8" */
159 case exvgXMGRACE: sprintf(buf + b, "%s", "\\x"); break;
160 case exvgXMGR: sprintf(buf + b, "%s", "\\8"); break;
161 default: buf[b] = '\0'; break;
164 b = std::strlen(buf);
168 /* Check for special symbol */
170 while (sym[i] != nullptr && gmx_strncasecmp(sym[i], &gmx[g], std::strlen(sym[i])) != 0)
174 if (sym[i] != nullptr)
177 if (std::isupper(gmx[g]))
183 case exvgXMGRACE: sprintf(buf + b, "%s%c%s", "\\x", c, "\\f{}"); break;
184 case exvgXMGR: sprintf(buf + b, "%s%c%s", "\\8", c, "\\4"); break;
186 std::strncat(buf + b, &gmx[g], std::strlen(sym[i]));
187 b += std::strlen(sym[i]);
188 if (gmx[g + std::strlen(sym[i])] != ' ')
195 g += std::strlen(sym[i]);
196 b = std::strlen(buf);
200 /* Unknown escape sequence, this should not happen.
201 * We do not generate a fatal error, since that might
202 * stop otherwise functioning code from working.
203 * Copy the backslash to the output and continue processing.
220 void xvgr_header(FILE* fp,
222 const std::string& xaxis,
223 const std::string& yaxis,
225 const gmx_output_env_t* oenv)
229 if (output_env_get_print_xvgr_codes(oenv))
231 fprintf(fp, "# This file was created %s", gmx_format_current_time().c_str());
234 gmx::BinaryInformationSettings settings;
235 settings.generatedByHeader(true);
236 settings.linePrefix("# ");
237 gmx::printBinaryInformation(fp, output_env_get_program_context(oenv), settings);
239 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
240 fprintf(fp, "# %s is part of G R O M A C S:\n#\n", output_env_get_program_display_name(oenv));
241 fprintf(fp, "# %s\n#\n", gmx::bromacs().c_str());
242 fprintf(fp, "@ title \"%s\"\n", xvgrstr(title, oenv, buf, STRLEN));
243 fprintf(fp, "@ xaxis label \"%s\"\n", xvgrstr(xaxis, oenv, buf, STRLEN));
244 fprintf(fp, "@ yaxis label \"%s\"\n", xvgrstr(yaxis, oenv, buf, STRLEN));
245 switch (exvg_graph_type)
248 if (output_env_get_xvg_format(oenv) == exvgXMGR)
250 fprintf(fp, "@TYPE nxy\n");
254 fprintf(fp, "@TYPE xy\n");
257 case exvggtXYDY: fprintf(fp, "@TYPE xydy\n"); break;
258 case exvggtXYDYDY: fprintf(fp, "@TYPE xydydy\n"); break;
263 FILE* xvgropen_type(const char* fn,
265 const std::string& xaxis,
266 const std::string& yaxis,
268 const gmx_output_env_t* oenv)
272 fp = gmx_fio_fopen(fn, "w");
274 xvgr_header(fp, title, xaxis, yaxis, exvg_graph_type, oenv);
279 FILE* xvgropen(const char* fn,
281 const std::string& xaxis,
282 const std::string& yaxis,
283 const gmx_output_env_t* oenv)
285 return xvgropen_type(fn, title, xaxis, yaxis, exvggtXNY, oenv);
288 void xvgrclose(FILE* fp)
293 void xvgr_subtitle(FILE* out, const char* subtitle, const gmx_output_env_t* oenv)
297 if (output_env_get_print_xvgr_codes(oenv))
299 fprintf(out, "@ subtitle \"%s\"\n", xvgrstr(subtitle, oenv, buf, STRLEN));
303 void xvgr_view(FILE* out, real xmin, real ymin, real xmax, real ymax, const gmx_output_env_t* oenv)
305 if (output_env_get_print_xvgr_codes(oenv))
307 fprintf(out, "@ view %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
311 void xvgr_world(FILE* out, real xmin, real ymin, real xmax, real ymax, const gmx_output_env_t* oenv)
313 if (output_env_get_print_xvgr_codes(oenv))
320 xmin, ymin, xmax, ymax);
324 static bool stringIsEmpty(const std::string& s)
329 static bool stringIsEmpty(const char* s)
331 return (s == nullptr || s[0] == '\0');
335 static void xvgr_legend(FILE* out, int nsets, const T* setname, const gmx_output_env_t* oenv)
340 if (output_env_get_print_xvgr_codes(oenv))
342 xvgr_view(out, 0.15, 0.15, 0.75, 0.85, oenv);
343 fprintf(out, "@ legend on\n");
344 fprintf(out, "@ legend box on\n");
345 fprintf(out, "@ legend loctype view\n");
346 fprintf(out, "@ legend %g, %g\n", 0.78, 0.8);
347 fprintf(out, "@ legend length %d\n", 2);
348 for (i = 0; (i < nsets); i++)
350 if (!stringIsEmpty(setname[i]))
352 if (output_env_get_xvg_format(oenv) == exvgXMGR)
354 fprintf(out, "@ legend string %d \"%s\"\n", i, xvgrstr(setname[i], oenv, buf, STRLEN));
358 fprintf(out, "@ s%d legend \"%s\"\n", i, xvgrstr(setname[i], oenv, buf, STRLEN));
365 void xvgrLegend(FILE* out, const std::vector<std::string>& setNames, const struct gmx_output_env_t* oenv)
367 xvgr_legend(out, setNames.size(), setNames.data(), oenv);
369 void xvgr_legend(FILE* out, int nsets, const char* const* setnames, const struct gmx_output_env_t* oenv)
371 xvgr_legend<const char*>(out, nsets, setnames, oenv);
374 void xvgr_new_dataset(FILE* out, int nr_first, int nsets, const char** setname, const gmx_output_env_t* oenv)
379 if (output_env_get_print_xvgr_codes(oenv))
382 for (i = 0; (i < nsets); i++)
386 if (output_env_get_xvg_format(oenv) == exvgXMGR)
388 fprintf(out, "@ legend string %d \"%s\"\n", i + nr_first,
389 xvgrstr(setname[i], oenv, buf, STRLEN));
393 fprintf(out, "@ s%d legend \"%s\"\n", i + nr_first,
394 xvgrstr(setname[i], oenv, buf, STRLEN));
405 void xvgr_line_props(FILE* out, int NrSet, int LineStyle, int LineColor, const gmx_output_env_t* oenv)
407 if (output_env_get_print_xvgr_codes(oenv))
409 fprintf(out, "@ with g0\n");
410 fprintf(out, "@ s%d linestyle %d\n", NrSet, LineStyle);
411 fprintf(out, "@ s%d color %d\n", NrSet, LineColor);
415 static const char* LocTypeStr[] = { "view", "world" };
416 static const char* BoxFillStr[] = { "none", "color", "pattern" };
418 void xvgr_box(FILE* out,
430 const gmx_output_env_t* oenv)
432 if (output_env_get_print_xvgr_codes(oenv))
434 fprintf(out, "@with box\n");
435 fprintf(out, "@ box on\n");
436 fprintf(out, "@ box loctype %s\n", LocTypeStr[LocType]);
437 fprintf(out, "@ box %g, %g, %g, %g\n", xmin, ymin, xmax, ymax);
438 fprintf(out, "@ box linestyle %d\n", LineStyle);
439 fprintf(out, "@ box linewidth %d\n", LineWidth);
440 fprintf(out, "@ box color %d\n", LineColor);
441 fprintf(out, "@ box fill %s\n", BoxFillStr[BoxFill]);
442 fprintf(out, "@ box fill color %d\n", BoxColor);
443 fprintf(out, "@ box fill pattern %d\n", BoxPattern);
444 fprintf(out, "@box def\n");
448 /* reads a line into ptr, adjusting len and renewing ptr if neccesary */
449 static char* fgets3(FILE* fp, char** ptr, int* len, int maxlen)
451 int len_remaining = *len; /* remaining amount of allocated bytes in buf */
452 int curp = 0; /* current position in buf to read into */
456 if (len_remaining < 2)
458 if (*len + STRLEN < maxlen)
460 /* This line is longer than len characters, let's increase len! */
462 len_remaining += STRLEN;
467 /*something is wrong, we'll just keep reading and return NULL*/
468 len_remaining = STRLEN;
472 if (fgets(*ptr + curp, len_remaining, fp) == nullptr)
474 /* if last line, skip */
477 curp += len_remaining - 1; /* overwrite the nul char in next iteration */
479 } while ((std::strchr(*ptr, '\n') == nullptr) && (feof(fp) == 0));
481 if (*len + STRLEN >= maxlen)
483 return nullptr; /* this line was too long */
488 /* We reached EOF before '\n', skip this last line. */
492 /* now remove newline */
493 int slen = std::strlen(*ptr);
494 if ((*ptr)[slen - 1] == '\n')
496 (*ptr)[slen - 1] = '\0';
503 static int wordcount(char* ptr)
507 #define prev (1 - cur)
511 for (i = 0; (ptr[i] != '\0'); i++)
513 is[cur] = std::isspace(ptr[i]);
514 if ((0 == i) && !is[cur])
518 else if ((i > 0) && (!is[cur] && is[prev]))
528 static char* read_xvgr_string(const char* line)
530 const char *ptr0, *ptr1;
533 ptr0 = std::strchr(line, '"');
537 ptr1 = std::strchr(ptr0, '"');
540 str = gmx_strdup(ptr0);
541 str[ptr1 - ptr0] = '\0';
545 str = gmx_strdup("");
550 str = gmx_strdup("");
556 int read_xvg_legend(const char* fn, double*** y, int* ny, char** subtitle, char*** legend)
560 char* base = nullptr;
562 int k, line = 0, nny, nx, maxx, rval, legend_nalloc, set, nchar;
564 double** yy = nullptr;
571 fp = gmx_fio_fopen(fn, "r");
574 if (subtitle != nullptr)
579 if (legend != nullptr)
584 while ((ptr = fgets3(fp, &tmpbuf, &len, 10 * STRLEN)) != nullptr && ptr[0] != '&')
590 if (legend != nullptr)
595 if (std::strncmp(ptr, "subtitle", 8) == 0)
598 if (subtitle != nullptr)
600 *subtitle = read_xvgr_string(ptr);
603 else if (std::strncmp(ptr, "legend string", 13) == 0)
606 sscanf(ptr, "%d%n", &set, &nchar);
609 else if (ptr[0] == 's')
612 sscanf(ptr, "%d%n", &set, &nchar);
615 if (std::strncmp(ptr, "legend", 6) == 0)
626 if (set >= legend_nalloc)
628 legend_nalloc = set + 1;
629 srenew(*legend, legend_nalloc);
630 (*legend)[set] = read_xvgr_string(ptr);
635 else if (ptr[0] != '#')
639 (*ny) = nny = wordcount(ptr);
640 /* fprintf(stderr,"There are %d columns in your file\n",nny);*/
646 snew(fmt, 3 * nny + 1);
647 snew(base, 3 * nny + 1);
649 /* Allocate column space */
653 for (k = 0; (k < nny); k++)
658 /* Initiate format string */
662 /* fprintf(stderr,"ptr='%s'\n",ptr);*/
663 for (k = 0; (k < nny); k++)
665 std::strcpy(fmt, base);
666 std::strcat(fmt, "%lf");
667 rval = sscanf(ptr, fmt, &lf);
668 /* fprintf(stderr,"rval = %d\n",rval);*/
669 if ((rval == EOF) || (rval == 0))
674 srenew(fmt, 3 * (nny + 1) + 1);
675 srenew(base, 3 * nny + 1);
676 std::strcat(base, "%*s");
680 fprintf(stderr, "Only %d columns on line %d in file %s\n", k, line, fn);
681 for (; (k < nny); k++)
696 if (legend_nalloc > 0)
698 if (*ny - 1 > legend_nalloc)
701 srenew(*legend, *ny - 1);
702 for (set = legend_nalloc; set < *ny - 1; set++)
704 (*legend)[set] = nullptr;
712 int read_xvg(const char* fn, double*** y, int* ny)
714 return read_xvg_legend(fn, y, ny, nullptr, nullptr);
717 void write_xvg(const char* fn, const char* title, int nx, int ny, real** y, const char** leg, const gmx_output_env_t* oenv)
722 fp = xvgropen(fn, title, "X", "Y", oenv);
725 xvgr_legend(fp, ny - 1, leg, oenv);
727 for (i = 0; (i < nx); i++)
729 for (j = 0; (j < ny); j++)
731 fprintf(fp, " %12.5e", y[j][i]);
738 real** read_xvg_time(const char* fn,
751 #define MAXLINELEN 16384
752 char line0[MAXLINELEN];
754 int t_nalloc, *val_nalloc, a, narg, n, sin, set, nchar;
756 gmx_bool bEndOfSet, bTimeInRange, bFirstLine = TRUE;
762 val_nalloc = nullptr;
765 fp = gmx_fio_fopen(fn, "r");
766 for (sin = 0; sin < nsets_in; sin++)
774 narg = bHaveT ? 2 : 1;
778 while (!bEndOfSet && fgets(line0, MAXLINELEN, fp))
781 /* Remove whitespace */
782 while (line[0] == ' ' || line[0] == '\t')
786 bEndOfSet = (line[0] == '&');
787 if (line[0] != '#' && line[0] != '@' && line[0] != '\n' && !bEndOfSet)
789 if (bFirstLine && bHaveT)
791 /* Check the first line that should contain data */
792 a = sscanf(line, "%lf%lf", &dbl, &dbl);
795 gmx_fatal(FARGS, "Expected a number in %s on line:\n%s", fn, line0);
800 "Found only 1 number on line, "
801 "assuming no time is present.\n");
812 while ((a < narg || (nsets_in == 1 && n == 0))
813 && sscanf(line, "%lf%n", &dbl, &nchar) == 1 && bTimeInRange)
815 /* Use set=-1 as the time "set" */
818 if (!bHaveT || (a > 0))
838 if (set == -1 && ((bTB && dbl < tb) || (bTE && dbl > te)))
840 bTimeInRange = FALSE;
855 srenew(val_nalloc, *nset);
866 t_nalloc = over_alloc_small(n);
867 srenew(*t, t_nalloc);
871 /* else we should check the time of the next sets with set 0 */
875 if (n >= val_nalloc[set])
877 val_nalloc[set] = over_alloc_small(n);
878 srenew(val[set], val_nalloc[set]);
880 val[set][n] = static_cast<real>(dbl);
886 if (line0[strlen(line0) - 1] != '\n')
888 fprintf(stderr, "File %s does not end with a newline, ignoring the last line\n", fn);
890 else if (bTimeInRange)
894 fprintf(stderr, "Ignoring invalid line in %s:\n%s", fn, line0);
901 "Invalid line in %s:\n%s"
902 "Using zeros for the last %d sets\n",
903 fn, line0, narg - a);
920 for (a = 0; a < n; a++)
927 *dt = static_cast<real>((*t)[n - 1] - (*t)[0]) / (n - 1.0);
938 fprintf(stderr, "Set %d is shorter (%d) than the previous set (%d)\n", sin + 1, n, *nval);
940 fprintf(stderr, "Will use only the first %d points of every set\n", *nval);