diff --git a/tutorial/energy-model.cc b/tutorial/energy-model.cc new file mode 100644 index 000000000..3f8629bf1 --- /dev/null +++ b/tutorial/energy-model.cc @@ -0,0 +1,132 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 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 + */ + +#include "ns3/log.h" +#include "energy-model.h" + +NS_LOG_COMPONENT_DEFINE("EnergyModel"); + +namespace ns3 { + +// +// Some dimensional analysis ... +// +// 1 [watt] = 1 [joule] / [second] +// 1 [watt] = 1 [volt] * 1 [amp] +// 1 [amp] = 1 [coulomb] / 1 [second] +// 1 [watt-second] = 1 [joule] / [second] * [second] = 1 [joule] +// +// A watt has dimensions of energy per second. A watt-second has dimensions +// of energy. +// +// 1 [amp-hour] = 1 [coulomb] / 1 [second] * 1 [hour] +// 1 [amp-hour] = 1 [coulomb] / 1 [second] * 3600 [seconds] +// 1 [amp-hour] = 3600 [amp-seconds] +// 1 [watt-second] = 1 [amp-seconds] * 1 [volt] +// +// To get the energy capacity of your battery in watt-seconds from its +// amp-hour rating, multiply by 3600 and then the voltage. For example, your +// Alkaline AAA battery may be rated at 1.5 volts and 900 milli-amp-hours; +// so the energy capacity will be .9 * 3600 * 1.5 = 4860 watt-seconds. +// +// In a very simple battery model, we'll take this naively to mean that this +// battery can supply one watt continuously for 4860 seconds and then will die +// instantaneously. +// +// We'll assume our transmitter is measured in watts. When it is turned on +// it draws some amount of power, 100 milliwatts for example. If it transmits +// for one second, it will suck up .1 watt-seconds of our precious energy. +// + +const InterfaceId EnergyModel::iid = + MakeInterfaceId ("EnergyModel", Object::iid); + + +EnergyModel::EnergyModel ( + double ampHours, + double volts, + double idlePower, + double receivePower, + double transmitPower) +: + m_capacity (ampHours * 3600. * volts), + m_idlePower (idlePower), + m_receivePower (receivePower), + m_transmitPower (transmitPower), + m_totalTransmitPower (0.), + m_totalReceivePower (0.) +{ + NS_LOG_FUNCTION; + SetInterfaceId (EnergyModel::iid); +} + +EnergyModel::~EnergyModel () +{ + NS_LOG_FUNCTION; +} + + double +EnergyModel::GetCapacity (Time t) +{ + NS_LOG_FUNCTION; + double c = m_capacity - m_idlePower * t.GetSeconds (); + return c >= 0. ? c : 0.; +} + + double +EnergyModel::GetTotalIdlePower (Time t) +{ + NS_LOG_FUNCTION; + return m_idlePower * t.GetSeconds (); +} + + double +EnergyModel::GetTotalReceivePower (void) +{ + NS_LOG_FUNCTION; + return m_totalReceivePower; +} + + double +EnergyModel::GetTotalTransmitPower (void) +{ + NS_LOG_FUNCTION; + return m_totalTransmitPower; +} + + bool +EnergyModel::DrawTransmitPower (Time t) +{ + NS_LOG_FUNCTION; + double power = m_transmitPower * t.GetSeconds (); + m_totalTransmitPower += power; + m_capacity -= power; + return m_capacity - m_idlePower * t.GetSeconds () >= 0. ? true : false; +} + + bool +EnergyModel::DrawReceivePower (Time t) +{ + NS_LOG_FUNCTION; + double power = m_receivePower * t.GetSeconds (); + m_totalReceivePower += power; + m_capacity -= power; + return m_capacity - m_idlePower * t.GetSeconds () >= 0. ? true : false; +} + +}; // namespace ns3 diff --git a/tutorial/energy-model.h b/tutorial/energy-model.h new file mode 100644 index 000000000..3ef2fd44a --- /dev/null +++ b/tutorial/energy-model.h @@ -0,0 +1,62 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 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 + */ + +#ifndef ENERGY_MODEL_H +#define ENERGY_MODEL_H + +#include "ns3/object.h" +#include "ns3/ptr.h" +#include "ns3/nstime.h" + +namespace ns3 { + +class EnergyModel : public Object +{ +public: + static const InterfaceId iid; + + EnergyModel ( + double ampHours, + double volts, + double idlePower, + double receivePower, + double transmitPower); + + virtual ~EnergyModel (); + + double GetCapacity (Time t); + + double GetTotalIdlePower (Time t); + double GetTotalTransmitPower (void); + double GetTotalReceivePower (void); + + bool DrawTransmitPower (Time t); + bool DrawReceivePower (Time t); + +private: + double m_capacity; + double m_idlePower; + double m_receivePower; + double m_transmitPower; + double m_totalTransmitPower; + double m_totalReceivePower; +}; + +}; // namespace ns3 + +#endif /* ENERGY_MODEL_H */ diff --git a/tutorial/energy.cc b/tutorial/energy.cc new file mode 100644 index 000000000..a8eb5a65f --- /dev/null +++ b/tutorial/energy.cc @@ -0,0 +1,124 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * 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 + */ + +#include "ns3/log.h" +#include "ns3/ptr.h" +#include "ns3/internet-node.h" +#include "ns3/point-to-point-channel.h" +#include "ns3/mac48-address.h" +#include "ns3/point-to-point-net-device.h" +#include "ns3/point-to-point-topology.h" +#include "ns3/udp-echo-client.h" +#include "ns3/udp-echo-server.h" +#include "ns3/simulator.h" +#include "ns3/nstime.h" +#include "ns3/ascii-trace.h" +#include "ns3/pcap-trace.h" +#include "ns3/global-route-manager.h" + +#include "energy-model.h" + +NS_LOG_COMPONENT_DEFINE ("EnergyModelInterfaceExample"); + +using namespace ns3; + +// Network topology +// +// point to point +// +--------------+ +// | | +// n0 n1 +// +int +main (int argc, char *argv[]) +{ + LogComponentEnable ("EnergyModelInterfaceExample", LOG_LEVEL_ALL); + // LogComponentEnable ("EnergyModel", LOG_LEVEL_ALL); + + NS_LOG_INFO ("Energy Model Interface Example"); + + NS_LOG_INFO ("Creating Nodes"); + Ptr n0 = CreateObject (); + Ptr n1 = CreateObject (); + + NS_LOG_INFO ("Creating Channel"); + Ptr link = PointToPointTopology::AddPointToPointLink ( + n0, n1, DataRate (38400), MilliSeconds (20)); + + PointToPointTopology::AddIpv4Addresses (link, n0, "10.1.1.1", + n1, "10.1.1.2"); + + NS_LOG_INFO ("Creating Applications"); + uint16_t port = 7; + + Ptr client = + CreateObject (n0, "10.1.1.2", port, 1, Seconds(1.), 1024); + + Ptr server = CreateObject (n1, port); + + server->Start(Seconds(1.)); + client->Start(Seconds(2.)); + + server->Stop (Seconds(10.)); + client->Stop (Seconds(10.)); + + NS_LOG_INFO ("Initializing Tracing"); + AsciiTrace asciitrace ("energy.tr"); + asciitrace.TraceAllQueues (); + asciitrace.TraceAllNetDeviceRx (); +// +// Pick a battery out of the air and use some somewhat real numbers found on +// data sheets on the web. +// +// 2 AAA battery (900 mAh * 2, with imaginary wireless device that uses +// 0.350 W at idle power, 0.025 W additional during receive, and 0.2 W +// additional power during transmission (10 mW TX power). +// + NS_LOG_INFO ("Initializing Energy Models"); + Ptr e0 = CreateObject (1.8, 1.5, 0.35, 0.025, 0.2); + n0->AddInterface (e0); + + Ptr e1 = CreateObject (1.8, 1.5, 0.35, 0.025, 0.2); + n1->AddInterface (e1); + +#if 0 +// +// As simulation progresses, the battereis draw idle power. Down in the +// net device, we will want to call DrawTransmitPower and DrawReceivePower +// as required. +// +// This is just some example code showing how to draw power and check +// consumption. +// + NS_LOG_INFO("Node zero energy: " << e0->GetCapacity (Seconds (0.)) << + " watt-seconds"); + NS_LOG_INFO("Node one energy: " << e1->GetCapacity (Seconds (0.)) << + " watt-seconds"); + + e0->DrawTransmitPower (Seconds (0.1)); + e1->DrawReceivePower (Seconds (0.1)); + e1->DrawTransmitPower (Seconds (0.1)); + e0->DrawReceivePower (Seconds (0.1)); + + NS_LOG_INFO("Node zero energy: " << e0->GetCapacity (Seconds (10.)) << + " watt-seconds"); + NS_LOG_INFO("Node one energy: " << e1->GetCapacity (Seconds (10.)) << + " watt-seconds"); +#endif + + Simulator::Run (); + Simulator::Destroy (); +}