From f2f5fa1f14008b419188310a73fcdabb866cfbd6 Mon Sep 17 00:00:00 2001 From: Mitch Watrous Date: Wed, 7 Sep 2011 12:18:37 -0700 Subject: [PATCH] Fix bug in gnuplot error bars and create gnuplot example --- src/tools/examples/gnuplot-example.cc | 277 ++++++++++++++++++++++++++ src/tools/examples/wscript | 10 + src/tools/model/gnuplot.cc | 9 +- src/tools/model/gnuplot.h | 10 +- src/tools/test/examples-to-run.py | 20 ++ src/tools/wscript | 3 + 6 files changed, 319 insertions(+), 10 deletions(-) create mode 100644 src/tools/examples/gnuplot-example.cc create mode 100644 src/tools/examples/wscript create mode 100644 src/tools/test/examples-to-run.py diff --git a/src/tools/examples/gnuplot-example.cc b/src/tools/examples/gnuplot-example.cc new file mode 100644 index 000000000..ec8d5440f --- /dev/null +++ b/src/tools/examples/gnuplot-example.cc @@ -0,0 +1,277 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mitch Watrous (watrous@u.washington.edu) + */ + +#include + +#include "ns3/gnuplot.h" + +using namespace ns3; + +namespace { + +//=========================================================================== +// Function: create2DPlotFile +// +// +// This function creates a 2-D plot file. +//=========================================================================== + +void create2DPlotFile () +{ + using namespace std; + + string fileNameWithNoExtension = "plot-2d"; + string graphicsFileName = fileNameWithNoExtension + ".png"; + string plotFileName = fileNameWithNoExtension + ".plt"; + string plotTitle = "2-D Plot"; + string dataTitle = "2-D Data"; + + // Instantiate the plot and set its title. + Gnuplot plot (graphicsFileName); + plot.SetTitle (plotTitle); + + // Make the graphics file, which the plot file will create when it + // is used with Gnuplot, be a PNG file. + plot.SetTerminal ("png"); + + // Set the labels for each axis. + plot.SetLegend ("X Values", "Y Values"); + + // Set the range for the x axis. + plot.AppendExtra ("set xrange [-6:+6]"); + + // Instantiate the dataset, set its title, and make the points be + // plotted along with connecting lines. + Gnuplot2dDataset dataset; + dataset.SetTitle (dataTitle); + dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS); + + double x; + double y; + + // Create the 2-D dataset. + for (x = -5.0; x <= +5.0; x += 1.0) + { + // Calculate the 2-D curve + // + // 2 + // y = x . + // + y = x * x; + + // Add this point. + dataset.Add (x, y); + } + + // Add the dataset to the plot. + plot.AddDataset (dataset); + + // Open the plot file. + ofstream plotFile (plotFileName.c_str()); + + // Write the plot file. + plot.GenerateOutput (plotFile); + + // Close the plot file. + plotFile.close (); +} + + +//=========================================================================== +// Function: create2DPlotWithErrorBarsFile +// +// +// This function creates a 2-D plot with error bars file. +//=========================================================================== + +void create2DPlotWithErrorBarsFile () +{ + using namespace std; + + string fileNameWithNoExtension = "plot-2d-with-error-bars"; + string graphicsFileName = fileNameWithNoExtension + ".png"; + string plotFileName = fileNameWithNoExtension + ".plt"; + string plotTitle = "2-D Plot With Error Bars"; + string dataTitle = "2-D Data With Error Bars"; + + // Instantiate the plot and set its title. + Gnuplot plot (graphicsFileName); + plot.SetTitle (plotTitle); + + // Make the graphics file, which the plot file will create when it + // is used with Gnuplot, be a PNG file. + plot.SetTerminal ("png"); + + // Set the labels for each axis. + plot.SetLegend ("X Values", "Y Values"); + + // Set the range for the x axis. + plot.AppendExtra ("set xrange [-6:+6]"); + + // Instantiate the dataset, set its title, and make the points be + // plotted with no connecting lines. + Gnuplot2dDataset dataset; + dataset.SetTitle (dataTitle); + dataset.SetStyle (Gnuplot2dDataset::POINTS); + + // Make the dataset have error bars in both the x and y directions. + dataset.SetErrorBars (Gnuplot2dDataset::XY); + + double x; + double xErrorDelta; + double y; + double yErrorDelta; + + // Create the 2-D dataset. + for (x = -5.0; x <= +5.0; x += 1.0) + { + // Calculate the 2-D curve + // + // 2 + // y = x . + // + y = x * x; + + // Make the uncertainty in the x direction be constant and make + // the uncertainty in the y direction be a constant fraction of + // y's value. + xErrorDelta = 0.25; + yErrorDelta = 0.1 * y; + + // Add this point with uncertainties in both the x and y + // direction. + dataset.Add (x, y, xErrorDelta, yErrorDelta); + } + + // Add the dataset to the plot. + plot.AddDataset (dataset); + + // Open the plot file. + ofstream plotFile (plotFileName.c_str()); + + // Write the plot file. + plot.GenerateOutput (plotFile); + + // Close the plot file. + plotFile.close (); +} + + +//=========================================================================== +// Function: create3DPlotFile +// +// +// This function creates a 3-D plot file. +//=========================================================================== + +void create3DPlotFile () +{ + using namespace std; + + string fileNameWithNoExtension = "plot-3d"; + string graphicsFileName = fileNameWithNoExtension + ".png"; + string plotFileName = fileNameWithNoExtension + ".plt"; + string plotTitle = "3-D Plot"; + string dataTitle = "3-D Data"; + + // Instantiate the plot and set its title. + Gnuplot plot (graphicsFileName); + plot.SetTitle (plotTitle); + + // Make the graphics file, which the plot file will create when it + // is used with Gnuplot, be a PNG file. + plot.SetTerminal ("png"); + + // Rotate the plot 30 degrees around the x axis and then rotate the + // plot 120 degrees around the new z axis. + plot.AppendExtra ("set view 30, 120, 1.0, 1.0"); + + // Move the zero for the z axis be onto the xy plane. + plot.AppendExtra ("set ticslevel 0"); + + // Set the labels for each axis. + plot.AppendExtra ("set xlabel 'X Values'"); + plot.AppendExtra ("set ylabel 'Y Values'"); + plot.AppendExtra ("set zlabel 'Z Values'"); + + // Set the ranges for the x and y axis. + plot.AppendExtra ("set xrange [-5:+5]"); + plot.AppendExtra ("set yrange [-5:+5]"); + + // Instantiate the dataset, set its title, and make the points be + // connected by lines. + Gnuplot3dDataset dataset; + dataset.SetTitle (dataTitle); + dataset.SetStyle ("with lines"); + + double x; + double y; + double z; + + // Create the 3-D dataset. + for (x = -5.0; x <= +5.0; x += 1.0) + { + for (y = -5.0; y <= +5.0; y += 1.0) + { + // Calculate the 3-D surface + // + // 2 2 + // z = x * y . + // + z = x * x * y * y; + + // Add this point. + dataset.Add (x, y, z); + } + + // The blank line is necessary at the end of each x value's data + // points for the 3-D surface grid to work. + dataset.AddEmptyLine (); + } + + // Add the dataset to the plot. + plot.AddDataset (dataset); + + // Open the plot file. + ofstream plotFile (plotFileName.c_str()); + + // Write the plot file. + plot.GenerateOutput (plotFile); + + // Close the plot file. + plotFile.close (); +} + +} // anonymous namespace + + +int main (int argc, char *argv[]) +{ + // Create a 2-D plot file. + create2DPlotFile(); + + // Create a 2-D plot with error bars file. + create2DPlotWithErrorBarsFile(); + + // Create a 3-D plot file. + create3DPlotFile(); + + return 0; +} diff --git a/src/tools/examples/wscript b/src/tools/examples/wscript new file mode 100644 index 000000000..13024cfc6 --- /dev/null +++ b/src/tools/examples/wscript @@ -0,0 +1,10 @@ +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +def build(bld): + if not bld.env['ENABLE_EXAMPLES']: + return; + + obj = bld.create_ns3_program('gnuplot-example', ['tools']) + obj.source = 'gnuplot-example.cc' + + diff --git a/src/tools/model/gnuplot.cc b/src/tools/model/gnuplot.cc index b0b8c50fa..6c81b7091 100644 --- a/src/tools/model/gnuplot.cc +++ b/src/tools/model/gnuplot.cc @@ -317,17 +317,16 @@ Gnuplot2dDataset::Add (double x, double y, double errorDelta) } void -Gnuplot2dDataset::Add (double x, double y, double minY, double maxY) +Gnuplot2dDataset::Add (double x, double y, double xErrorDelta, double yErrorDelta) { - NS_ASSERT ( reinterpret_cast(m_data)->m_errorBars == X || - reinterpret_cast(m_data)->m_errorBars == Y ); + NS_ASSERT ( reinterpret_cast(m_data)->m_errorBars == XY ); struct Point data; data.empty = false; data.x = x; data.y = y; - data.dx = minY; - data.dy = maxY; + data.dx = xErrorDelta; + data.dy = yErrorDelta; reinterpret_cast(m_data)->m_pointset.push_back (data); } diff --git a/src/tools/model/gnuplot.h b/src/tools/model/gnuplot.h index 0e1ac1d5f..d72dd5e03 100644 --- a/src/tools/model/gnuplot.h +++ b/src/tools/model/gnuplot.h @@ -177,7 +177,7 @@ public: /** * \param x x coord to new data point * \param y y coord to new data point - * \param errorDelta data point error range. + * \param errorDelta x and y data point uncertainty * * Use this method with error bar style X or Y. */ @@ -186,12 +186,12 @@ public: /** * \param x x coord to new data point * \param y y coord to new data point - * \param minY minimum error data point - * \param maxY maximum error data point + * \param xErrorDelta x data point uncertainty + * \param yErrorDelta y data point uncertainty * - * Use this method with error bar style X or Y. + * Use this method with error bar style XY. */ - void Add (double x, double y, double minY, double maxY); + void Add (double x, double y, double xErrorDelta, double yErrorDelta); /** * Add an empty line in the data output sequence. Empty lines in the plot diff --git a/src/tools/test/examples-to-run.py b/src/tools/test/examples-to-run.py new file mode 100644 index 000000000..37e6d7a49 --- /dev/null +++ b/src/tools/test/examples-to-run.py @@ -0,0 +1,20 @@ +#! /usr/bin/env python +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +# A list of C++ examples to run in order to ensure that they remain +# buildable and runnable over time. Each tuple in the list contains +# +# (example_name, do_run, do_valgrind_run). +# +# See test.py for more information. +cpp_examples = [ + ("gnuplot-example", "False", "False"), +] + +# A list of Python examples to run in order to ensure that they remain +# runnable over time. Each tuple in the list contains +# +# (example_name, do_run). +# +# See test.py for more information. +python_examples = [] diff --git a/src/tools/wscript b/src/tools/wscript index a31f6ab89..31f096317 100644 --- a/src/tools/wscript +++ b/src/tools/wscript @@ -23,4 +23,7 @@ def build(bld): 'model/delay-jitter-estimation.h', ] + if (bld.env['ENABLE_EXAMPLES']): + bld.add_subdirs('examples') + bld.ns3_python_bindings()