SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
src
sampler.cc
Go to the documentation of this file.
1
/////////////////////////////////////////////////////////////////////////////
2
//! \file sampler.cc Periodic sampler
3
//
4
// Copyright (c) 1994-2004 Petr Peringer
5
//
6
// This library is licensed under GNU Library GPL. See the file COPYING.
7
//
8
9
//
10
// description:
11
// implementation of class Sampler
12
// - calls global function (function) periodicaly (step)
13
// - automatic initialization by Init() (set on)
14
// - automatic activation by Run() (if on)
15
// - Start/Stop
16
//
17
18
////////////////////////////////////////////////////////////////////////////
19
// interface
20
//
21
22
#include "
simlib.h
"
23
#include "
internal.h
"
24
25
////////////////////////////////////////////////////////////////////////////
26
// implementation
27
//
28
29
namespace
simlib3
{
30
31
SIMLIB_IMPLEMENTATION
;
32
33
// init
34
Sampler
*
Sampler::First
= 0;
35
36
////////////////////////////////////////////////////////////////////////////
37
// constructor
38
//
39
Sampler::Sampler
(
void
(*pf)(),
double
dt) :
40
Next
(0),
41
function
(pf),
42
last
(-1),
43
step
((dt>0.0) ? dt : 0.0),
44
on
(true)
45
{
46
Dprintf
((
"Sampler::Sampler(%p,%g)"
, pf, dt));
47
if
(
First
==0 ) {
// first created sampler
48
INSTALL_HOOK
( SamplerInit,
Sampler::InitAll
);
49
INSTALL_HOOK
( SamplerAct,
Sampler::ActivateAll
);
50
}
51
// insert into list:
52
Next
=
First
;
53
First
=
this
;
54
}
55
56
////////////////////////////////////////////////////////////////////////////
57
// destructor
58
//
59
Sampler::~Sampler
()
60
{
61
Dprintf
((
"Sampler::~Sampler() // \"%p\" "
,
function
));
62
// remove from list:
63
if
(
this
==
First
)
64
First
=
Next
;
65
else
66
{
67
Sampler
*i;
68
for
(i=
First
; i && i->
Next
!=
this
; i=i->
Next
) {
/*empty*/
}
69
if
(i) i->
Next
= this->
Next
;
70
}
71
if
(
First
==0 ) {
72
INSTALL_HOOK
( SamplerInit, 0 );
73
INSTALL_HOOK
( SamplerAct, 0 );
74
}
75
}
76
77
////////////////////////////////////////////////////////////////////////////
78
// Output
79
//
80
void
Sampler::Output
()
const
81
{
82
Print
(
"Sampler%p, function=%p, last=%g, on=%i\n"
,
83
this
,
function
,
last
,
on
);
84
}
85
86
87
////////////////////////////////////////////////////////////////////////////
88
// Sample::Behavior --- call function and reactivate sample event
89
//
90
void
Sampler::Behavior
() {
91
Dprintf
((
"Sampler::Behavior()"
));
92
Sample
();
// call of global function
93
if
(
on
&&
step
> 0.0 )
94
Activate
(
Time
+
step
);
// schedule next sample
95
else
96
Passivate
();
// should be passivated before ###????
97
}
98
99
100
////////////////////////////////////////////////////////////////////////////
101
// Sampler::InitAll --- init all samplers - called by Init()
102
// static
103
void
Sampler::InitAll
() {
104
for
(
Sampler
*i =
First
; i; i = i->
Next
) {
105
i->last = -1;
106
i->on =
true
;
107
}
108
}
109
110
////////////////////////////////////////////////////////////////////////////
111
// ActivateAll - activate all Samplers - called by Run()
112
// static
113
void
Sampler::ActivateAll
()
114
{
115
for
(
Sampler
*i=
First
; i; i=i->
Next
)
116
{
117
i->last = -1;
// really needed ???? ######
118
if
(i->on)
// activate this one
119
i->Activate();
120
}
121
}
122
123
////////////////////////////////////////////////////////////////////////////
124
// Start() -- start of sampling
125
//
126
void
Sampler::Start
()
127
{
128
on
=
true
;
129
Activate
();
130
}
131
132
////////////////////////////////////////////////////////////////////////////
133
// Stop -- end of sampling
134
//
135
void
Sampler::Stop
()
136
{
137
on
=
false
;
138
if
(
last
==
Time
)
// was sample at this time
139
Passivate
();
140
else
141
Activate
();
142
}
143
144
////////////////////////////////////////////////////////////////////////////
145
// Sample -- explicit sample
146
//
147
void
Sampler::Sample
()
148
{
149
if
(
function
)
150
function
();
// call global function
151
last
=
Time
;
152
}
153
154
////////////////////////////////////////////////////////////////////////////
155
// SetStep -- step of sampling added 13.5.95
156
//
157
double
Sampler::SetStep
(
double
dt)
158
{
159
double
laststep=
step
;
160
step
= (dt>0.0) ? dt : 0.0;
161
return
laststep;
162
}
163
164
}
// end of SAMPLER.CPP
165
simlib3::Entity::Passivate
virtual void Passivate()
deactivation
Definition
entity.cc:68
simlib3::Event::Activate
virtual void Activate(double t) override
activate at time t (schedule)
Definition
event.cc:62
simlib3::Sampler
objects of this class call global function periodically (typicaly used for output of continuous model...
Definition
simlib.h:511
simlib3::Sampler::First
static Sampler * First
Definition
simlib.h:512
simlib3::Sampler::SetStep
double SetStep(double dt=0.0)
change step
Definition
sampler.cc:157
simlib3::Sampler::InitAll
static void InitAll()
initialize all samplers (Init)
Definition
sampler.cc:103
simlib3::Sampler::function
void(*) function()
function to call periodically
Definition
simlib.h:515
simlib3::Sampler::~Sampler
virtual ~Sampler()
Definition
sampler.cc:59
simlib3::Sampler::Sampler
Sampler(void(*pf)(), double dt=0.0)
Definition
sampler.cc:39
simlib3::Sampler::step
double step
step of sampling
Definition
simlib.h:517
simlib3::Sampler::last
double last
last sample time – prevents sample duplication
Definition
simlib.h:516
simlib3::Sampler::Output
virtual void Output() const override
print object to default output
Definition
sampler.cc:80
simlib3::Sampler::Start
void Start()
start + sample
Definition
sampler.cc:126
simlib3::Sampler::Stop
void Stop()
sample + stop
Definition
sampler.cc:135
simlib3::Sampler::Behavior
virtual void Behavior() override
behavior description
Definition
sampler.cc:90
simlib3::Sampler::on
bool on
switch on/off
Definition
simlib.h:518
simlib3::Sampler::ActivateAll
static void ActivateAll()
start all samplers (Run)
Definition
sampler.cc:113
simlib3::Sampler::Next
Sampler * Next
Definition
simlib.h:513
simlib3::Sampler::Sample
void Sample()
performs sample (function call)
Definition
sampler.cc:147
internal.h
Internal header file for SIMLIB/C++.
Dprintf
#define Dprintf(f)
Definition
internal.h:100
INSTALL_HOOK
#define INSTALL_HOOK(name, function)
Definition
internal.h:255
simlib3
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition
algloop.cc:32
simlib3::Time
const double & Time
model time (is NOT the block)
Definition
run.cc:48
simlib3::SIMLIB_IMPLEMENTATION
SIMLIB_IMPLEMENTATION
Definition
algloop.cc:34
simlib3::Print
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition
print.cc:92
simlib.h
Main SIMLIB/C++ interface.
Generated on
for SIMLIB/C++ by
1.18.0