A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 "energy-source.h"
23 
24 #include "ns3/log.h"
25 #include "ns3/simulator.h"
26 #include "ns3/trace-source-accessor.h"
27 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("SimpleDeviceEnergyModel");
32 
33 NS_OBJECT_ENSURE_REGISTERED(SimpleDeviceEnergyModel);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId("ns3::SimpleDeviceEnergyModel")
40  .SetGroupName("Energy")
41  .AddConstructor<SimpleDeviceEnergyModel>()
42  .AddTraceSource("TotalEnergyConsumption",
43  "Total energy consumption of the radio device.",
46  "ns3::TracedValueCallback::Double");
47  return tid;
48 }
49 
51 {
52  NS_LOG_FUNCTION(this);
54  m_actualCurrentA = 0.0;
55  m_source = nullptr;
56 }
57 
59 {
60  NS_LOG_FUNCTION(this);
61 }
62 
63 void
65 {
66  NS_LOG_FUNCTION(this << source);
67  NS_ASSERT(source);
68  m_source = source;
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION(this << node);
75  NS_ASSERT(node);
76  m_node = node;
77 }
78 
81 {
82  NS_LOG_FUNCTION(this);
83  return m_node;
84 }
85 
86 double
88 {
89  NS_LOG_FUNCTION(this);
90  Time duration = Simulator::Now() - m_lastUpdateTime;
91 
92  double energyToDecrease = 0.0;
93  double supplyVoltage = m_source->GetSupplyVoltage();
94  energyToDecrease = duration.GetSeconds() * m_actualCurrentA * supplyVoltage;
95 
96  m_source->UpdateEnergySource();
97 
98  return m_totalEnergyConsumption + energyToDecrease;
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION(this << current);
105  Time duration = Simulator::Now() - m_lastUpdateTime;
106 
107  double energyToDecrease = 0.0;
108  double supplyVoltage = m_source->GetSupplyVoltage();
109  energyToDecrease = duration.GetSeconds() * m_actualCurrentA * supplyVoltage;
110 
111  // update total energy consumption
112  m_totalEnergyConsumption += energyToDecrease;
113  // update last update time stamp
115  // update the current drain
116  m_actualCurrentA = current;
117  // notify energy source
118  m_source->UpdateEnergySource();
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION(this);
125  m_source = nullptr;
126 }
127 
128 double
130 {
131  NS_LOG_FUNCTION(this);
132  return m_actualCurrentA;
133 }
134 
135 } // namespace ns3
Base class for device energy models.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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 EnergySource installed on node.
Ptr< EnergySource > m_source
Energy source.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
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:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:1326
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.