A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
basic-energy-source.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19
*/
20
21
#include "
basic-energy-source.h
"
22
#include "ns3/log.h"
23
#include "ns3/assert.h"
24
#include "ns3/double.h"
25
#include "ns3/trace-source-accessor.h"
26
#include "ns3/simulator.h"
27
28
NS_LOG_COMPONENT_DEFINE
(
"BasicEnergySource"
);
29
30
namespace
ns3 {
31
32
NS_OBJECT_ENSURE_REGISTERED
(BasicEnergySource);
33
34
TypeId
35
BasicEnergySource::GetTypeId
(
void
)
36
{
37
static
TypeId
tid =
TypeId
(
"ns3::BasicEnergySource"
)
38
.
SetParent
<
EnergySource
> ()
39
.AddConstructor<BasicEnergySource> ()
40
.AddAttribute (
"BasicEnergySourceInitialEnergyJ"
,
41
"Initial energy stored in basic energy source."
,
42
DoubleValue
(10),
// in Joules
43
MakeDoubleAccessor (&
BasicEnergySource::SetInitialEnergy
,
44
&
BasicEnergySource::GetInitialEnergy
),
45
MakeDoubleChecker<double> ())
46
.AddAttribute (
"BasicEnergySupplyVoltageV"
,
47
"Initial supply voltage for basic energy source."
,
48
DoubleValue
(3.0),
// in Volts
49
MakeDoubleAccessor (&
BasicEnergySource::SetSupplyVoltage
,
50
&
BasicEnergySource::GetSupplyVoltage
),
51
MakeDoubleChecker<double> ())
52
.AddAttribute (
"PeriodicEnergyUpdateInterval"
,
53
"Time between two consecutive periodic energy updates."
,
54
TimeValue
(Seconds (1.0)),
55
MakeTimeAccessor (&
BasicEnergySource::SetEnergyUpdateInterval
,
56
&
BasicEnergySource::GetEnergyUpdateInterval
),
57
MakeTimeChecker
())
58
.AddTraceSource (
"RemainingEnergy"
,
59
"Remaining energy at BasicEnergySource."
,
60
MakeTraceSourceAccessor
(&
BasicEnergySource::m_remainingEnergyJ
))
61
;
62
return
tid;
63
}
64
65
BasicEnergySource::BasicEnergySource
()
66
{
67
NS_LOG_FUNCTION
(
this
);
68
m_lastUpdateTime
= Seconds (0.0);
69
}
70
71
BasicEnergySource::~BasicEnergySource
()
72
{
73
NS_LOG_FUNCTION
(
this
);
74
}
75
76
void
77
BasicEnergySource::SetInitialEnergy
(
double
initialEnergyJ)
78
{
79
NS_LOG_FUNCTION
(
this
<< initialEnergyJ);
80
NS_ASSERT
(initialEnergyJ >= 0);
81
m_initialEnergyJ
= initialEnergyJ;
82
m_remainingEnergyJ
=
m_initialEnergyJ
;
83
}
84
85
void
86
BasicEnergySource::SetSupplyVoltage
(
double
supplyVoltageV)
87
{
88
NS_LOG_FUNCTION
(
this
<< supplyVoltageV);
89
m_supplyVoltageV
= supplyVoltageV;
90
}
91
92
void
93
BasicEnergySource::SetEnergyUpdateInterval
(
Time
interval)
94
{
95
NS_LOG_FUNCTION
(
this
<< interval);
96
m_energyUpdateInterval
= interval;
97
}
98
99
Time
100
BasicEnergySource::GetEnergyUpdateInterval
(
void
)
const
101
{
102
NS_LOG_FUNCTION
(
this
);
103
return
m_energyUpdateInterval
;
104
}
105
106
double
107
BasicEnergySource::GetSupplyVoltage
(
void
)
const
108
{
109
NS_LOG_FUNCTION
(
this
);
110
return
m_supplyVoltageV
;
111
}
112
113
double
114
BasicEnergySource::GetInitialEnergy
(
void
)
const
115
{
116
NS_LOG_FUNCTION
(
this
);
117
return
m_initialEnergyJ
;
118
}
119
120
double
121
BasicEnergySource::GetRemainingEnergy
(
void
)
122
{
123
NS_LOG_FUNCTION
(
this
);
124
// update energy source to get the latest remaining energy.
125
UpdateEnergySource
();
126
return
m_remainingEnergyJ
;
127
}
128
129
double
130
BasicEnergySource::GetEnergyFraction
(
void
)
131
{
132
NS_LOG_FUNCTION
(
this
);
133
// update energy source to get the latest remaining energy.
134
UpdateEnergySource
();
135
return
m_remainingEnergyJ
/
m_initialEnergyJ
;
136
}
137
138
void
139
BasicEnergySource::UpdateEnergySource
(
void
)
140
{
141
NS_LOG_FUNCTION
(
this
);
142
NS_LOG_DEBUG
(
"BasicEnergySource:Updating remaining energy."
);
143
144
// do not update if simulation has finished
145
if
(
Simulator::IsFinished
())
146
{
147
return
;
148
}
149
150
m_energyUpdateEvent
.
Cancel
();
151
152
CalculateRemainingEnergy
();
153
154
if
(
m_remainingEnergyJ
<= 0)
155
{
156
HandleEnergyDrainedEvent
();
157
return
;
// stop periodic update
158
}
159
160
m_lastUpdateTime
=
Simulator::Now
();
161
162
m_energyUpdateEvent
=
Simulator::Schedule
(
m_energyUpdateInterval
,
163
&
BasicEnergySource::UpdateEnergySource
,
164
this
);
165
}
166
167
/*
168
* Private functions start here.
169
*/
170
171
void
172
BasicEnergySource::DoInitialize
(
void
)
173
{
174
NS_LOG_FUNCTION
(
this
);
175
UpdateEnergySource
();
// start periodic update
176
}
177
178
void
179
BasicEnergySource::DoDispose
(
void
)
180
{
181
NS_LOG_FUNCTION
(
this
);
182
BreakDeviceEnergyModelRefCycle
();
// break reference cycle
183
}
184
185
void
186
BasicEnergySource::HandleEnergyDrainedEvent
(
void
)
187
{
188
NS_LOG_FUNCTION
(
this
);
189
NS_LOG_DEBUG
(
"BasicEnergySource:Energy depleted!"
);
190
NotifyEnergyDrained
();
// notify DeviceEnergyModel objects
191
m_remainingEnergyJ
= 0;
// energy never goes below 0
192
}
193
194
void
195
BasicEnergySource::CalculateRemainingEnergy
(
void
)
196
{
197
NS_LOG_FUNCTION
(
this
);
198
double
totalCurrentA =
CalculateTotalCurrent
();
199
Time
duration =
Simulator::Now
() -
m_lastUpdateTime
;
200
NS_ASSERT
(duration.
GetSeconds
() >= 0);
201
// energy = current * voltage * time
202
double
energyToDecreaseJ = totalCurrentA *
m_supplyVoltageV
* duration.
GetSeconds
();
203
m_remainingEnergyJ
-= energyToDecreaseJ;
204
NS_LOG_DEBUG
(
"BasicEnergySource:Remaining energy = "
<<
m_remainingEnergyJ
);
205
}
206
207
}
// namespace ns3
ns3::BasicEnergySource::CalculateRemainingEnergy
void CalculateRemainingEnergy(void)
Definition:
basic-energy-source.cc:195
ns3::Time
keep track of time values and allow control of global simulation resolution
Definition:
nstime.h:81
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:311
ns3::BasicEnergySource::GetEnergyUpdateInterval
Time GetEnergyUpdateInterval(void) const
Definition:
basic-energy-source.cc:100
ns3::BasicEnergySource::GetSupplyVoltage
virtual double GetSupplyVoltage(void) const
Definition:
basic-energy-source.cc:107
basic-energy-source.h
ns3::EnergySource
Energy source base class.
Definition:
energy-source.h:71
ns3::BasicEnergySource::GetRemainingEnergy
virtual double GetRemainingEnergy(void)
Definition:
basic-energy-source.cc:121
NS_ASSERT
#define NS_ASSERT(condition)
Definition:
assert.h:64
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Definition:
log.h:122
ns3::BasicEnergySource::UpdateEnergySource
virtual void UpdateEnergySource(void)
Definition:
basic-energy-source.cc:139
ns3::Simulator::Schedule
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition:
simulator.h:824
ns3::BasicEnergySource::m_supplyVoltageV
double m_supplyVoltageV
Definition:
basic-energy-source.h:132
ns3::BasicEnergySource::HandleEnergyDrainedEvent
void HandleEnergyDrainedEvent(void)
Definition:
basic-energy-source.cc:186
ns3::BasicEnergySource::m_energyUpdateInterval
Time m_energyUpdateInterval
Definition:
basic-energy-source.h:136
ns3::EnergySource::BreakDeviceEnergyModelRefCycle
void BreakDeviceEnergyModelRefCycle(void)
Definition:
energy-source.cc:175
ns3::Time::GetSeconds
double GetSeconds(void) const
Definition:
nstime.h:266
ns3::BasicEnergySource::GetInitialEnergy
virtual double GetInitialEnergy(void) const
Definition:
basic-energy-source.cc:114
ns3::BasicEnergySource::DoDispose
void DoDispose(void)
Defined in ns3::Object.
Definition:
basic-energy-source.cc:179
ns3::BasicEnergySource::m_lastUpdateTime
Time m_lastUpdateTime
Definition:
basic-energy-source.h:135
ns3::TimeValue
hold objects of type ns3::Time
Definition:
nstime.h:828
ns3::BasicEnergySource::m_remainingEnergyJ
TracedValue< double > m_remainingEnergyJ
Definition:
basic-energy-source.h:133
ns3::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
ns3::BasicEnergySource::GetTypeId
static TypeId GetTypeId(void)
Definition:
basic-energy-source.cc:35
ns3::EnergySource::CalculateTotalCurrent
double CalculateTotalCurrent(void)
Definition:
energy-source.cc:150
ns3::MakeTraceSourceAccessor
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Definition:
trace-source-accessor.h:135
ns3::BasicEnergySource::DoInitialize
void DoInitialize(void)
Defined in ns3::Object.
Definition:
basic-energy-source.cc:172
ns3::Simulator::Now
static Time Now(void)
Definition:
simulator.cc:180
ns3::BasicEnergySource::m_energyUpdateEvent
EventId m_energyUpdateEvent
Definition:
basic-energy-source.h:134
ns3::BasicEnergySource::m_initialEnergyJ
double m_initialEnergyJ
Definition:
basic-energy-source.h:131
ns3::EnergySource::NotifyEnergyDrained
void NotifyEnergyDrained(void)
Definition:
energy-source.cc:163
ns3::BasicEnergySource::SetSupplyVoltage
void SetSupplyVoltage(double supplyVoltageV)
Definition:
basic-energy-source.cc:86
ns3::BasicEnergySource::SetEnergyUpdateInterval
void SetEnergyUpdateInterval(Time interval)
Definition:
basic-energy-source.cc:93
ns3::BasicEnergySource::~BasicEnergySource
virtual ~BasicEnergySource()
Definition:
basic-energy-source.cc:71
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Definition:
log.h:255
ns3::EventId::Cancel
void Cancel(void)
Definition:
event-id.cc:47
ns3::Simulator::IsFinished
static bool IsFinished(void)
Definition:
simulator.cc:150
ns3::MakeTimeChecker
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range. Both limits are inclusive.
Definition:
time.cc:404
ns3::BasicEnergySource::SetInitialEnergy
void SetInitialEnergy(double initialEnergyJ)
Definition:
basic-energy-source.cc:77
ns3::BasicEnergySource::BasicEnergySource
BasicEnergySource()
Definition:
basic-energy-source.cc:65
ns3::DoubleValue
Hold an floating point type.
Definition:
double.h:41
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:610
ns3::BasicEnergySource::GetEnergyFraction
virtual double GetEnergyFraction(void)
Definition:
basic-energy-source.cc:130
src
energy
model
basic-energy-source.cc
Generated on Sat Nov 16 2013 12:55:26 for ns-3 by
1.8.5