1#ifndef MADNESS_GNUPLOT_H__INCUDED
2#define MADNESS_GNUPLOT_H__INCUDED
27 snprintf(buf,
sizeof(buf),
"%16.8e",
double(t[i]));
32 template <
typename T,
typename... Ts>
33 void dbvalue(
size_t i,
const T& t, Ts... values) {
35 snprintf(buf,
sizeof(buf),
"%16.8e ",
double(t[i]));
41 template <
int n,
typename T>
44 snprintf(buf,
sizeof(buf),
"%s using 1:%d",
name, n);
49 template <
int n,
typename T,
typename... Ts>
50 void doplot(
const char*
name,
const T& value0, Ts... values) {
52 snprintf(buf,
sizeof(buf),
"%s using 1:%d, ",
name, n);
62 Gnuplot(
const std::string& cmd =
"",
const std::string& teefile =
"") :
f(0),
ftee(0) {
70 dup2(
p[0],STDIN_FILENO);
72 if (execlp (
"gnuplot",
"gnuplot",
"-persist", NULL) == -1) {
74 fprintf(stderr,
"Gnuplot: execlp failed for gnuplot ... plotting disabled\n");
78 else if (
pid < (pid_t) 0) {
83 f = fdopen (
p[1],
"w");
85 if (teefile.size() > 0) {
86 ftee = fopen(teefile.c_str(),
"w");
88 fprintf(stderr,
"Gnuplot: fopen failed for tee file %s ... tee of plotting disabled\n",teefile.c_str());
92 if (cmd.size() > 0) (*this)(cmd);
99 if (!fprintf(
f,
"%s",cmd)) {
100 fprintf(stderr,
"Gnuplot: failed writing to gnuplot pipe ... plotting disabled\n");
107 const int n = strlen(cmd);
108 if (EOL && ((n==0) || (cmd[n-1] !=
'\n') ) ) {
110 if (!fprintf(
f,
"\n")) {
111 fprintf(stderr,
"Gnuplot: failed writing newline to gnuplot pipe ... plotting disabled\n");
123 (*this)(cmd.c_str(), EOL);
127 template <
typename T,
typename... Ts>
128 void db(
const std::string&
name,
size_t size,
const T& x, Ts... values) {
132 for (
size_t i = 0; i<size; ++i) {
139 template <
typename T,
typename... Ts>
140 void db(
const std::string&
name,
const T& x, Ts... values) {
141 db(
name,(
size_t) x.size(),x,values...);
149 template <
typename T,
typename... Ts>
150 void plot(
const T& x, Ts... values) {
151 db(
"data", x, values...);
152 (*this)(
"plot ",
false);
153 doplot<2,Ts...>(
"$data", values...);
165 std::vector<double> x = {1.0,2.0,3.0};
166 std::vector<double> y = {-1.0,-2.0,3.0};
167 std::vector<double> z = {10.0,11.0,12.0};
169 Gnuplot g(
"set style data lp; set grid");
177 g(
"plot $xyz using 1:2 with linespoints");
181 Gnuplot g(
"set term x11; set xrange [-10:10]; set yrange [0:1]; set grid; set style data l",
"test3.gnuplot");
183 std::vector<double> x(npts), y(npts);
184 for (
size_t i = 0; i<npts; ++i) x[i] = -10.0 + 20.0 * i / (npts-1);
185 for (
int step=0; step<40; step++) {
186 double phase = step*0.3;
187 for (
size_t i = 0; i<npts; ++i) y[i] = 0.5 + 0.5 * std::sin(x[i]+
phase);
189 snprintf(buf,
sizeof(buf),
"set title \"step %d\"",step);
Gnuplot & operator=(Gnuplot &&)=delete
void doplot(const char *name, const T &value0)
Definition gnuplot.h:42
Gnuplot(const std::string &cmd="", const std::string &teefile="")
Definition gnuplot.h:62
void dbvalue(size_t i, const T &t, Ts... values)
Definition gnuplot.h:33
void dbvalue(size_t i, const T &t)
Definition gnuplot.h:25
static void test()
Definition gnuplot.h:164
Gnuplot & operator=(const Gnuplot &)=delete
void db(const std::string &name, const T &x, Ts... values)
Definition gnuplot.h:140
void db(const std::string &name, size_t size, const T &x, Ts... values)
Definition gnuplot.h:128
void doplot(const char *name, const T &value0, Ts... values)
Definition gnuplot.h:50
FILE * f
Definition gnuplot.h:19
void operator()(const char *cmd, bool EOL=true)
Definition gnuplot.h:96
FILE * ftee
Definition gnuplot.h:21
pid_t pid
Definition gnuplot.h:20
Gnuplot(const Gnuplot &)=delete
void operator()(const std::string &cmd, bool EOL=true)
Definition gnuplot.h:122
void plot(const T &x, Ts... values)
Definition gnuplot.h:150
~Gnuplot()
Definition gnuplot.h:156
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
NDIM const Function< R, NDIM > & g
Definition mra.h:2416
std::string name(const FuncType &type, const int ex=-1)
Definition ccpairfunction.h:28
static double phase(long i)
Definition twoscale.cc:85