1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tutorial/energy-model.cc Mon Feb 04 12:27:18 2008 -0800
1.3 @@ -0,0 +1,132 @@
1.4 +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
1.5 +/*
1.6 + * Copyright (c) 2007 University of Washington
1.7 + *
1.8 + * This program is free software; you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License version 2 as
1.10 + * published by the Free Software Foundation;
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program; if not, write to the Free Software
1.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.20 + */
1.21 +
1.22 +#include "ns3/log.h"
1.23 +#include "energy-model.h"
1.24 +
1.25 +NS_LOG_COMPONENT_DEFINE("EnergyModel");
1.26 +
1.27 +namespace ns3 {
1.28 +
1.29 +//
1.30 +// Some dimensional analysis ...
1.31 +//
1.32 +// 1 [watt] = 1 [joule] / [second]
1.33 +// 1 [watt] = 1 [volt] * 1 [amp]
1.34 +// 1 [amp] = 1 [coulomb] / 1 [second]
1.35 +// 1 [watt-second] = 1 [joule] / [second] * [second] = 1 [joule]
1.36 +//
1.37 +// A watt has dimensions of energy per second. A watt-second has dimensions
1.38 +// of energy.
1.39 +//
1.40 +// 1 [amp-hour] = 1 [coulomb] / 1 [second] * 1 [hour]
1.41 +// 1 [amp-hour] = 1 [coulomb] / 1 [second] * 3600 [seconds]
1.42 +// 1 [amp-hour] = 3600 [amp-seconds]
1.43 +// 1 [watt-second] = 1 [amp-seconds] * 1 [volt]
1.44 +//
1.45 +// To get the energy capacity of your battery in watt-seconds from its
1.46 +// amp-hour rating, multiply by 3600 and then the voltage. For example, your
1.47 +// Alkaline AAA battery may be rated at 1.5 volts and 900 milli-amp-hours;
1.48 +// so the energy capacity will be .9 * 3600 * 1.5 = 4860 watt-seconds.
1.49 +//
1.50 +// In a very simple battery model, we'll take this naively to mean that this
1.51 +// battery can supply one watt continuously for 4860 seconds and then will die
1.52 +// instantaneously.
1.53 +//
1.54 +// We'll assume our transmitter is measured in watts. When it is turned on
1.55 +// it draws some amount of power, 100 milliwatts for example. If it transmits
1.56 +// for one second, it will suck up .1 watt-seconds of our precious energy.
1.57 +//
1.58 +
1.59 +const InterfaceId EnergyModel::iid =
1.60 + MakeInterfaceId ("EnergyModel", Object::iid);
1.61 +
1.62 +
1.63 +EnergyModel::EnergyModel (
1.64 + double ampHours,
1.65 + double volts,
1.66 + double idlePower,
1.67 + double receivePower,
1.68 + double transmitPower)
1.69 +:
1.70 + m_capacity (ampHours * 3600. * volts),
1.71 + m_idlePower (idlePower),
1.72 + m_receivePower (receivePower),
1.73 + m_transmitPower (transmitPower),
1.74 + m_totalTransmitPower (0.),
1.75 + m_totalReceivePower (0.)
1.76 +{
1.77 + NS_LOG_FUNCTION;
1.78 + SetInterfaceId (EnergyModel::iid);
1.79 +}
1.80 +
1.81 +EnergyModel::~EnergyModel ()
1.82 +{
1.83 + NS_LOG_FUNCTION;
1.84 +}
1.85 +
1.86 + double
1.87 +EnergyModel::GetCapacity (Time t)
1.88 +{
1.89 + NS_LOG_FUNCTION;
1.90 + double c = m_capacity - m_idlePower * t.GetSeconds ();
1.91 + return c >= 0. ? c : 0.;
1.92 +}
1.93 +
1.94 + double
1.95 +EnergyModel::GetTotalIdlePower (Time t)
1.96 +{
1.97 + NS_LOG_FUNCTION;
1.98 + return m_idlePower * t.GetSeconds ();
1.99 +}
1.100 +
1.101 + double
1.102 +EnergyModel::GetTotalReceivePower (void)
1.103 +{
1.104 + NS_LOG_FUNCTION;
1.105 + return m_totalReceivePower;
1.106 +}
1.107 +
1.108 + double
1.109 +EnergyModel::GetTotalTransmitPower (void)
1.110 +{
1.111 + NS_LOG_FUNCTION;
1.112 + return m_totalTransmitPower;
1.113 +}
1.114 +
1.115 + bool
1.116 +EnergyModel::DrawTransmitPower (Time t)
1.117 +{
1.118 + NS_LOG_FUNCTION;
1.119 + double power = m_transmitPower * t.GetSeconds ();
1.120 + m_totalTransmitPower += power;
1.121 + m_capacity -= power;
1.122 + return m_capacity - m_idlePower * t.GetSeconds () >= 0. ? true : false;
1.123 +}
1.124 +
1.125 + bool
1.126 +EnergyModel::DrawReceivePower (Time t)
1.127 +{
1.128 + NS_LOG_FUNCTION;
1.129 + double power = m_receivePower * t.GetSeconds ();
1.130 + m_totalReceivePower += power;
1.131 + m_capacity -= power;
1.132 + return m_capacity - m_idlePower * t.GetSeconds () >= 0. ? true : false;
1.133 +}
1.134 +
1.135 +}; // namespace ns3
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tutorial/energy-model.h Mon Feb 04 12:27:18 2008 -0800
2.3 @@ -0,0 +1,62 @@
2.4 +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2.5 +/*
2.6 + * Copyright (c) 2007 University of Washington
2.7 + *
2.8 + * This program is free software; you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License version 2 as
2.10 + * published by the Free Software Foundation;
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program; if not, write to the Free Software
2.19 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2.20 + */
2.21 +
2.22 +#ifndef ENERGY_MODEL_H
2.23 +#define ENERGY_MODEL_H
2.24 +
2.25 +#include "ns3/object.h"
2.26 +#include "ns3/ptr.h"
2.27 +#include "ns3/nstime.h"
2.28 +
2.29 +namespace ns3 {
2.30 +
2.31 +class EnergyModel : public Object
2.32 +{
2.33 +public:
2.34 + static const InterfaceId iid;
2.35 +
2.36 + EnergyModel (
2.37 + double ampHours,
2.38 + double volts,
2.39 + double idlePower,
2.40 + double receivePower,
2.41 + double transmitPower);
2.42 +
2.43 + virtual ~EnergyModel ();
2.44 +
2.45 + double GetCapacity (Time t);
2.46 +
2.47 + double GetTotalIdlePower (Time t);
2.48 + double GetTotalTransmitPower (void);
2.49 + double GetTotalReceivePower (void);
2.50 +
2.51 + bool DrawTransmitPower (Time t);
2.52 + bool DrawReceivePower (Time t);
2.53 +
2.54 +private:
2.55 + double m_capacity;
2.56 + double m_idlePower;
2.57 + double m_receivePower;
2.58 + double m_transmitPower;
2.59 + double m_totalTransmitPower;
2.60 + double m_totalReceivePower;
2.61 +};
2.62 +
2.63 +}; // namespace ns3
2.64 +
2.65 +#endif /* ENERGY_MODEL_H */
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tutorial/energy.cc Mon Feb 04 12:27:18 2008 -0800
3.3 @@ -0,0 +1,124 @@
3.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3.5 +/*
3.6 + * This program is free software; you can redistribute it and/or modify
3.7 + * it under the terms of the GNU General Public License version 2 as
3.8 + * published by the Free Software Foundation;
3.9 + *
3.10 + * This program is distributed in the hope that it will be useful,
3.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.13 + * GNU General Public License for more details.
3.14 + *
3.15 + * You should have received a copy of the GNU General Public License
3.16 + * along with this program; if not, write to the Free Software
3.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3.18 + */
3.19 +
3.20 +#include "ns3/log.h"
3.21 +#include "ns3/ptr.h"
3.22 +#include "ns3/internet-node.h"
3.23 +#include "ns3/point-to-point-channel.h"
3.24 +#include "ns3/mac48-address.h"
3.25 +#include "ns3/point-to-point-net-device.h"
3.26 +#include "ns3/point-to-point-topology.h"
3.27 +#include "ns3/udp-echo-client.h"
3.28 +#include "ns3/udp-echo-server.h"
3.29 +#include "ns3/simulator.h"
3.30 +#include "ns3/nstime.h"
3.31 +#include "ns3/ascii-trace.h"
3.32 +#include "ns3/pcap-trace.h"
3.33 +#include "ns3/global-route-manager.h"
3.34 +
3.35 +#include "energy-model.h"
3.36 +
3.37 +NS_LOG_COMPONENT_DEFINE ("EnergyModelInterfaceExample");
3.38 +
3.39 +using namespace ns3;
3.40 +
3.41 +// Network topology
3.42 +//
3.43 +// point to point
3.44 +// +--------------+
3.45 +// | |
3.46 +// n0 n1
3.47 +//
3.48 +int
3.49 +main (int argc, char *argv[])
3.50 +{
3.51 + LogComponentEnable ("EnergyModelInterfaceExample", LOG_LEVEL_ALL);
3.52 + // LogComponentEnable ("EnergyModel", LOG_LEVEL_ALL);
3.53 +
3.54 + NS_LOG_INFO ("Energy Model Interface Example");
3.55 +
3.56 + NS_LOG_INFO ("Creating Nodes");
3.57 + Ptr<Node> n0 = CreateObject<InternetNode> ();
3.58 + Ptr<Node> n1 = CreateObject<InternetNode> ();
3.59 +
3.60 + NS_LOG_INFO ("Creating Channel");
3.61 + Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
3.62 + n0, n1, DataRate (38400), MilliSeconds (20));
3.63 +
3.64 + PointToPointTopology::AddIpv4Addresses (link, n0, "10.1.1.1",
3.65 + n1, "10.1.1.2");
3.66 +
3.67 + NS_LOG_INFO ("Creating Applications");
3.68 + uint16_t port = 7;
3.69 +
3.70 + Ptr<UdpEchoClient> client =
3.71 + CreateObject<UdpEchoClient> (n0, "10.1.1.2", port, 1, Seconds(1.), 1024);
3.72 +
3.73 + Ptr<UdpEchoServer> server = CreateObject<UdpEchoServer> (n1, port);
3.74 +
3.75 + server->Start(Seconds(1.));
3.76 + client->Start(Seconds(2.));
3.77 +
3.78 + server->Stop (Seconds(10.));
3.79 + client->Stop (Seconds(10.));
3.80 +
3.81 + NS_LOG_INFO ("Initializing Tracing");
3.82 + AsciiTrace asciitrace ("energy.tr");
3.83 + asciitrace.TraceAllQueues ();
3.84 + asciitrace.TraceAllNetDeviceRx ();
3.85 +//
3.86 +// Pick a battery out of the air and use some somewhat real numbers found on
3.87 +// data sheets on the web.
3.88 +//
3.89 +// 2 AAA battery (900 mAh * 2, with imaginary wireless device that uses
3.90 +// 0.350 W at idle power, 0.025 W additional during receive, and 0.2 W
3.91 +// additional power during transmission (10 mW TX power).
3.92 +//
3.93 + NS_LOG_INFO ("Initializing Energy Models");
3.94 + Ptr<EnergyModel> e0 = CreateObject<EnergyModel> (1.8, 1.5, 0.35, 0.025, 0.2);
3.95 + n0->AddInterface (e0);
3.96 +
3.97 + Ptr<EnergyModel> e1 = CreateObject<EnergyModel> (1.8, 1.5, 0.35, 0.025, 0.2);
3.98 + n1->AddInterface (e1);
3.99 +
3.100 +#if 0
3.101 +//
3.102 +// As simulation progresses, the battereis draw idle power. Down in the
3.103 +// net device, we will want to call DrawTransmitPower and DrawReceivePower
3.104 +// as required.
3.105 +//
3.106 +// This is just some example code showing how to draw power and check
3.107 +// consumption.
3.108 +//
3.109 + NS_LOG_INFO("Node zero energy: " << e0->GetCapacity (Seconds (0.)) <<
3.110 + " watt-seconds");
3.111 + NS_LOG_INFO("Node one energy: " << e1->GetCapacity (Seconds (0.)) <<
3.112 + " watt-seconds");
3.113 +
3.114 + e0->DrawTransmitPower (Seconds (0.1));
3.115 + e1->DrawReceivePower (Seconds (0.1));
3.116 + e1->DrawTransmitPower (Seconds (0.1));
3.117 + e0->DrawReceivePower (Seconds (0.1));
3.118 +
3.119 + NS_LOG_INFO("Node zero energy: " << e0->GetCapacity (Seconds (10.)) <<
3.120 + " watt-seconds");
3.121 + NS_LOG_INFO("Node one energy: " << e1->GetCapacity (Seconds (10.)) <<
3.122 + " watt-seconds");
3.123 +#endif
3.124 +
3.125 + Simulator::Run ();
3.126 + Simulator::Destroy ();
3.127 +}