A Discrete-Event Network Simulator
API
simple-device-energy-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Andrea Sacco
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
18  */
19 
21 
22 #include "ns3/energy-source.h"
23 #include "ns3/log.h"
24 #include "ns3/simulator.h"
25 #include "ns3/trace-source-accessor.h"
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("SimpleDeviceEnergyModel");
31 
32 NS_OBJECT_ENSURE_REGISTERED(SimpleDeviceEnergyModel);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId("ns3::SimpleDeviceEnergyModel")
39  .SetGroupName("Energy")
40  .AddConstructor<SimpleDeviceEnergyModel>()
41  .AddTraceSource("TotalEnergyConsumption",
42  "Total energy consumption of the radio device.",
45  "ns3::TracedValueCallback::Double");
46  return tid;
47 }
48 
50 {
51  NS_LOG_FUNCTION(this);
53  m_actualCurrentA = 0.0;
54  m_source = nullptr;
55 }
56 
58 {
59  NS_LOG_FUNCTION(this);
60 }
61 
62 void
64 {
65  NS_LOG_FUNCTION(this << source);
66  NS_ASSERT(source);
67  m_source = source;
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION(this << node);
74  NS_ASSERT(node);
75  m_node = node;
76 }
77 
80 {
81  NS_LOG_FUNCTION(this);
82  return m_node;
83 }
84 
85 double
87 {
88  NS_LOG_FUNCTION(this);
89  Time duration = Simulator::Now() - m_lastUpdateTime;
90 
91  double energyToDecrease = 0.0;
92  double supplyVoltage = m_source->GetSupplyVoltage();
93  energyToDecrease = duration.GetSeconds() * m_actualCurrentA * supplyVoltage;
94 
95  m_source->UpdateEnergySource();
96 
97  return m_totalEnergyConsumption + energyToDecrease;
98 }
99 
100 void
102 {
103  NS_LOG_FUNCTION(this << current);
104  Time duration = Simulator::Now() - m_lastUpdateTime;
105 
106  double energyToDecrease = 0.0;
107  double supplyVoltage = m_source->GetSupplyVoltage();
108  energyToDecrease = duration.GetSeconds() * m_actualCurrentA * supplyVoltage;
109 
110  // update total energy consumption
111  m_totalEnergyConsumption += energyToDecrease;
112  // update last update time stamp
114  // update the current drain
115  m_actualCurrentA = current;
116  // notify energy source
117  m_source->UpdateEnergySource();
118 }
119 
120 void
122 {
123  NS_LOG_FUNCTION(this);
124  m_source = nullptr;
125 }
126 
127 double
129 {
130  NS_LOG_FUNCTION(this);
131  return m_actualCurrentA;
132 }
133 
134 } // namespace ns3
Base class for device energy models.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
A simple device energy model where current drain can be set by the user.
static TypeId GetTypeId()
Get the type ID.
virtual Ptr< Node > GetNode() const
Gets pointer to node.
TracedValue< double > m_totalEnergyConsumption
Total energy consumption trace.
double GetTotalEnergyConsumption() const override
void DoDispose() override
Destructor implementation.
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
double m_actualCurrentA
actual curred (in Ampere)
void SetEnergySource(Ptr< EnergySource > source) override
Sets pointer to EnergySouce installed on node.
Ptr< EnergySource > m_source
Energy source.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.