INET Framework for OMNeT++/OMNEST
inet::DriftingOscillatorBase Class Reference

#include <DriftingOscillatorBase.h>

Inheritance diagram for inet::DriftingOscillatorBase:
inet::OscillatorBase inet::IScriptable inet::IOscillator inet::StringFormat::IDirectiveResolver inet::ConstantDriftOscillator inet::RandomDriftOscillator

Public Member Functions

virtual simtime_t getComputationOrigin () const override
 Returns the oscillator computation origin from which the ticks are measured. More...
 
virtual simtime_t getNominalTickLength () const override
 Returns the nominal time interval between subsequent ticks. More...
 
virtual simtime_t getCurrentTickLength () const
 
virtual void setDriftRate (double driftRate)
 
virtual void setTickOffset (simtime_t tickOffset)
 
virtual int64_t computeTicksForInterval (simtime_t timeInterval) const override
 Returns the number of ticks in the specified time interval measured from the oscillator computation origin. More...
 
virtual simtime_t computeIntervalForTicks (int64_t numTicks) const override
 Returns the smallest simulation time interval for the specified number of ticks measured from the oscillator computation origin. More...
 
virtual const char * resolveDirective (char directive) const override
 
- Public Member Functions inherited from inet::IOscillator
virtual ~IOscillator ()
 
- Public Member Functions inherited from inet::IScriptable
virtual ~IScriptable ()
 

Protected Member Functions

virtual void initialize (int stage) override
 
virtual void processCommand (const cXMLElement &node) override
 Called by ScenarioManager whenever a script command needs to be carried out by the module. More...
 
double invertDriftRate (double driftRate) const
 
int64_t increaseWithDriftRate (int64_t value) const
 
int64_t increaseWithDriftRate (int64_t value, double driftRate) const
 
int64_t decreaseWithDriftRate (int64_t value) const
 
int64_t decreaseWithDriftRate (int64_t value, double inverseDriftRate) const
 
- Protected Member Functions inherited from inet::OscillatorBase
virtual int numInitStages () const override
 
virtual void updateDisplayString () const
 

Protected Attributes

simtime_t nominalTickLength
 
double driftRate = NaN
 
double inverseDriftRate = NaN
 
simtime_t origin
 
simtime_t nextTickFromOrigin
 
- Protected Attributes inherited from inet::OscillatorBase
const char * displayStringTextFormat = nullptr
 

Additional Inherited Members

- Static Public Attributes inherited from inet::OscillatorBase
static simsignal_t driftRateChangedSignal = cComponent::registerSignal("driftRateChanged")
 
- Static Public Attributes inherited from inet::IOscillator
static simsignal_t preOscillatorStateChangedSignal = cComponent::registerSignal("preOscillatorStateChanged")
 
static simsignal_t postOscillatorStateChangedSignal = cComponent::registerSignal("postOscillatorStateChanged")
 

Member Function Documentation

◆ computeIntervalForTicks()

simtime_t inet::DriftingOscillatorBase::computeIntervalForTicks ( int64_t  numTicks) const
overridevirtual

Returns the smallest simulation time interval for the specified number of ticks measured from the oscillator computation origin.

For example, for 0 ticks returns 0, for 1 tick returns the remaining time till the next tick, and so on.

Implements inet::IOscillator.

89 {
90  if (numTicks == 0)
91  return 0;
92  else if (nextTickFromOrigin == 0)
93  return SimTime::fromRaw(decreaseWithDriftRate(nominalTickLength.raw() * numTicks));
94  else
95  return SimTime::fromRaw(decreaseWithDriftRate(nominalTickLength.raw() * (numTicks - 1))) + nextTickFromOrigin;
96 }

◆ computeTicksForInterval()

int64_t inet::DriftingOscillatorBase::computeTicksForInterval ( simtime_t  timeInterval) const
overridevirtual

Returns the number of ticks in the specified time interval measured from the oscillator computation origin.

Note that each tick interval taken into consideration may be different from the nominal tick length. Returns the #ticks in (computation origin, computation origin + time interval].

Implements inet::IOscillator.

83 {
84  ASSERT(timeInterval >= 0);
85  return increaseWithDriftRate(timeInterval.raw() + nextTickFromOrigin.raw()) / nominalTickLength.raw();
86 }

◆ decreaseWithDriftRate() [1/2]

int64_t inet::DriftingOscillatorBase::decreaseWithDriftRate ( int64_t  value) const
inlineprotected

◆ decreaseWithDriftRate() [2/2]

int64_t inet::DriftingOscillatorBase::decreaseWithDriftRate ( int64_t  value,
double  inverseDriftRate 
) const
inlineprotected
39 { return value + (int64_t)(value * inverseDriftRate); }

◆ getComputationOrigin()

virtual simtime_t inet::DriftingOscillatorBase::getComputationOrigin ( ) const
inlineoverridevirtual

Returns the oscillator computation origin from which the ticks are measured.

There may or may not be an actual tick at the returned simulation time.

Implements inet::IOscillator.

42 { return origin; }

◆ getCurrentTickLength()

virtual simtime_t inet::DriftingOscillatorBase::getCurrentTickLength ( ) const
inlinevirtual
44 { return SimTime::fromRaw(decreaseWithDriftRate(nominalTickLength.raw())); }

Referenced by initialize(), resolveDirective(), setDriftRate(), and setTickOffset().

◆ getNominalTickLength()

virtual simtime_t inet::DriftingOscillatorBase::getNominalTickLength ( ) const
inlineoverridevirtual

Returns the nominal time interval between subsequent ticks.

This value is usually different from the actual amount of elapsed time between ticks.

Implements inet::IOscillator.

43 { return nominalTickLength; }

◆ increaseWithDriftRate() [1/2]

int64_t inet::DriftingOscillatorBase::increaseWithDriftRate ( int64_t  value) const
inlineprotected

◆ increaseWithDriftRate() [2/2]

int64_t inet::DriftingOscillatorBase::increaseWithDriftRate ( int64_t  value,
double  driftRate 
) const
inlineprotected
36 { return value + (int64_t)(value * driftRate); }

◆ initialize()

void inet::DriftingOscillatorBase::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OscillatorBase.

Reimplemented in inet::RandomDriftOscillator, and inet::ConstantDriftOscillator.

13 {
15  if (stage == INITSTAGE_LOCAL) {
16  nominalTickLength = par("nominalTickLength");
17  if (nominalTickLength == 0)
18  nominalTickLength.setRaw(1);
20  origin = simTime();
21  simtime_t currentTickLength = getCurrentTickLength();
22  simtime_t tickOffset = par("tickOffset");
23  if (tickOffset < 0 || tickOffset >= currentTickLength)
24  throw cRuntimeError("First tick offset must be in the range [0, currentTickLength)");
25  if (tickOffset == 0)
27  else
28  nextTickFromOrigin = currentTickLength - tickOffset;
29  WATCH(nominalTickLength);
30  WATCH(driftRate);
31  WATCH(inverseDriftRate);
32  WATCH(nextTickFromOrigin);
33  }
34 }

Referenced by inet::ConstantDriftOscillator::initialize(), and inet::RandomDriftOscillator::initialize().

◆ invertDriftRate()

double inet::DriftingOscillatorBase::invertDriftRate ( double  driftRate) const
inlineprotected
33 { return 1 / (1 + driftRate) - 1; }

Referenced by initialize(), and setDriftRate().

◆ processCommand()

void inet::DriftingOscillatorBase::processCommand ( const cXMLElement &  node)
overrideprotectedvirtual

Called by ScenarioManager whenever a script command needs to be carried out by the module.

The command is represented by the XML element or element tree. The command name can be obtained as:

const char *command = node->getTagName()

Parameters are XML attributes, e.g. a "neighbour" parameter can be retrieved as:

const char *attr = node->getAttribute("neighbour")

More complex input can be passed in child elements.

See also
cXMLElement

Implements inet::IScriptable.

99 {
100  Enter_Method("processCommand");
101  if (!strcmp(node.getTagName(), "set-oscillator")) {
102  if (const char *driftRateStr = node.getAttribute("drift-rate")) {
103  double newDriftRate = strtod(driftRateStr, nullptr) / 1E+6;
104  setDriftRate(newDriftRate);
105  }
106  if (const char *tickOffsetStr = node.getAttribute("tick-offset")) {
107  simtime_t newTickOffset = SimTime::parse(tickOffsetStr);
108  setTickOffset(newTickOffset);
109  }
110  }
111  else
112  throw cRuntimeError("Invalid command: %s", node.getTagName());
113 }

◆ resolveDirective()

const char * inet::DriftingOscillatorBase::resolveDirective ( char  directive) const
overridevirtual

Reimplemented from inet::OscillatorBase.

116 {
117  static std::string result;
118  switch (directive) {
119  case 'c':
120  result = getCurrentTickLength().str() + " s";
121  break;
122  case 'd':
123  result = std::to_string((int64_t)(driftRate * 1E+6)) + " ppm";
124  break;
125  default:
126  return OscillatorBase::resolveDirective(directive);
127  }
128  return result.c_str();
129 }

◆ setDriftRate()

void inet::DriftingOscillatorBase::setDriftRate ( double  driftRate)
virtual
37 {
38  Enter_Method("setDriftRate");
39  if (newDriftRate != driftRate) {
41  simtime_t currentSimTime = simTime();
42  EV_DEBUG << "Setting oscillator drift rate from " << driftRate << " to " << newDriftRate << " at simtime " << currentSimTime << ".\n";
43  simtime_t currentTickLength = getCurrentTickLength();
44  simtime_t baseTickTime = origin + nextTickFromOrigin - currentTickLength;
45  simtime_t elapsedTickTime = fmod(currentSimTime - baseTickTime, currentTickLength);
46  double newInverseDriftRate = invertDriftRate(newDriftRate);
47  if (elapsedTickTime == SIMTIME_ZERO)
49  else {
50  int64_t v = increaseWithDriftRate(currentTickLength.raw() - elapsedTickTime.raw());
51  nextTickFromOrigin = SimTime::fromRaw(decreaseWithDriftRate(v, newInverseDriftRate));
52  }
53  driftRate = newDriftRate;
54  inverseDriftRate = newInverseDriftRate;
55  origin = currentSimTime;
59  }
60 }

Referenced by inet::RandomDriftOscillator::handleMessage(), and processCommand().

◆ setTickOffset()

void inet::DriftingOscillatorBase::setTickOffset ( simtime_t  tickOffset)
virtual
63 {
64  Enter_Method("setTickOffset");
65  simtime_t currentSimTime = simTime();
66  simtime_t currentTickLength = getCurrentTickLength();
67  simtime_t baseTickTime = origin + nextTickFromOrigin - currentTickLength;
68  simtime_t oldTickOffset = fmod(currentSimTime - baseTickTime, currentTickLength);
69  if (newTickOffset != oldTickOffset) {
71  EV_DEBUG << "Setting oscillator tick offset from " << oldTickOffset << " to " << newTickOffset << " at simtime " << currentSimTime << ".\n";
72  origin = currentSimTime;
73  if (newTickOffset == 0)
75  else
76  nextTickFromOrigin = currentTickLength - newTickOffset;
79  }
80 }

Referenced by processCommand().

Member Data Documentation

◆ driftRate

◆ inverseDriftRate

double inet::DriftingOscillatorBase::inverseDriftRate = NaN
protected

Referenced by initialize(), and setDriftRate().

◆ nextTickFromOrigin

simtime_t inet::DriftingOscillatorBase::nextTickFromOrigin
protected

◆ nominalTickLength

simtime_t inet::DriftingOscillatorBase::nominalTickLength
protected

◆ origin

simtime_t inet::DriftingOscillatorBase::origin
protected

The documentation for this class was generated from the following files:
inet::OscillatorBase::resolveDirective
virtual const char * resolveDirective(char directive) const override
Definition: OscillatorBase.cc:30
inet::OscillatorBase::initialize
virtual void initialize(int stage) override
Definition: OscillatorBase.cc:14
inet::DriftingOscillatorBase::setTickOffset
virtual void setTickOffset(simtime_t tickOffset)
Definition: DriftingOscillatorBase.cc:62
inet::DriftingOscillatorBase::getCurrentTickLength
virtual simtime_t getCurrentTickLength() const
Definition: DriftingOscillatorBase.h:44
inet::OscillatorBase::driftRateChangedSignal
static simsignal_t driftRateChangedSignal
Definition: OscillatorBase.h:19
inet::IOscillator::postOscillatorStateChangedSignal
static simsignal_t postOscillatorStateChangedSignal
Definition: IOscillator.h:22
inet::DriftingOscillatorBase::origin
simtime_t origin
Definition: DriftingOscillatorBase.h:24
inet::DriftingOscillatorBase::invertDriftRate
double invertDriftRate(double driftRate) const
Definition: DriftingOscillatorBase.h:33
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::DriftingOscillatorBase::inverseDriftRate
double inverseDriftRate
Definition: DriftingOscillatorBase.h:22
inet::DriftingOscillatorBase::decreaseWithDriftRate
int64_t decreaseWithDriftRate(int64_t value) const
Definition: DriftingOscillatorBase.h:38
inet::OscillatorBase::updateDisplayString
virtual void updateDisplayString() const
Definition: OscillatorBase.cc:22
Enter_Method
#define Enter_Method(...)
Definition: SelfDoc.h:71
inet::DriftingOscillatorBase::setDriftRate
virtual void setDriftRate(double driftRate)
Definition: DriftingOscillatorBase.cc:36
inet::DriftingOscillatorBase::nominalTickLength
simtime_t nominalTickLength
Definition: DriftingOscillatorBase.h:20
inet::IOscillator::preOscillatorStateChangedSignal
static simsignal_t preOscillatorStateChangedSignal
Definition: IOscillator.h:21
inet::DriftingOscillatorBase::driftRate
double driftRate
Definition: DriftingOscillatorBase.h:21
inet::DriftingOscillatorBase::increaseWithDriftRate
int64_t increaseWithDriftRate(int64_t value) const
Definition: DriftingOscillatorBase.h:35
inet::DriftingOscillatorBase::nextTickFromOrigin
simtime_t nextTickFromOrigin
Definition: DriftingOscillatorBase.h:25