SIMLIB/C++  3.07
continuous.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file continuous.cc continuous blocks definitions
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // description: news to version 2.0
11 //
12 // - blocks for + - * / operations
13 // - dynamical section replacement: _Dynamic()
14 // - evaluation with alg. loop detections
15 
16 
17 ////////////////////////////////////////////////////////////////////////////
18 // interface
19 //
20 
21 #include "simlib.h"
22 #include "internal.h"
23 
24 namespace simlib3 {
25 
26 
27 ////////////////////////////////////////////////////////////////////////////
28 // implementation
29 //
30 
32 
33 ////////////////////////////////////////////////////////////////////////////
34 /// performs evaluation of integrators and status blocks
35 void SIMLIB_Dynamic() // called each step
36 {
37  StatusContainer::ClearAllValueOK(); // zero flags ###
38  StatusContainer::EvaluateAll(); // evaluation (with loop detection) ???
39  IntegratorContainer::EvaluateAll(); // evaluation without loop detection
40 }
41 
42 
43 ////////////////////////////////////////////////////////////////////////////
44 /// evaluation with algebraic loop detection
45 //
46 // TODO: use new implementation -- AlgLoopDetector, or remove
47 //
49 {
50  if(isEvaluated) // was evaluated
51  SIMLIB_error(AlgLoopDetected); // recursive call
52  isEvaluated = true; // eval-flag
53  Eval(); // evaluation of block
54  isEvaluated = false;
55 }
56 
57 
58 ////////////////////////////////////////////////////////////////////////////
59 /// constructor for blocks with single input
60 //
62 {
63  if(input==this)
65 }
66 
67 /*
68 Input aContiBlock1::SetInput(Input i)
69 {
70  if(i==this) SIMLIB_error(AlgLoopDetected);
71 // LoopCheck(); // detect algebraic loops
72  return input.Set(i);
73 }
74 */
75 
76 ////////////////////////////////////////////////////////////////////////////
77 /// ctr for blocks with 2 inputs
78 //
79 aContiBlock2::aContiBlock2(Input i1, Input i2) : input1(i1), input2(i2)
80 {
81  if(input1==this || input2==this)
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////
86 /// ctr for blocks with 3 inputs
87 //
89  : input1(i1), input2(i2), input3(i3)
90 {
91  if(input1==this || input2==this || input3==this )
93 }
94 
95 /*
96 void aContiBlock2::SetInputs(Input i1, Input i2)
97 {
98  input1.Set(i1);
99  input2.Set(i2);
100  if(input1==this || input2==this) SIMLIB_error(AlgLoopDetected);
101 // LoopCheck(); // detect algebraic loops
102 }
103 
104 Input aContiBlock2::SetInput1(Input inp)
105 {
106  if(inp==this) SIMLIB_error(AlgLoopDetected);
107  Input old = input1.Set(inp);
108 // LoopCheck(); // detect algebraic loops
109  return old;
110 }
111 
112 Input aContiBlock2::SetInput2(Input inp)
113 {
114  if(inp==this) SIMLIB_error(AlgLoopDetected);
115  Input old = input2.Set(inp);
116 // LoopCheck(); // detect algebraic loops
117  return old;
118 }
119 */
120 
121 
122 /// check expression for algebraic loops
123 double Expression::Value() { AlgLoopDetector _(this); return InputValue(); }
124 
125 
126 ////////////////////////////////////////////////////////////////////////////
127 // _Xxxx classes are for internal use only -
128 // the objects are created automatically in block expressions,
129 // so there is no need to check algebraic loops
130 
131 ////////////////////////////////////////////////////////////////////////////
132 /// block sum of two inputs
133 class _Add : public aContiBlock2 {
134  virtual void Eval() override {}
135  virtual void _Eval() override {}
136 public:
137  _Add(Input a, Input b): aContiBlock2(a,b) {
138  Dprintf(("ctr: _Add[%p](in1,in2)", this));
139  }
140  ~_Add() {
141  Dprintf(("dtr: _Add[%p]", this));
142  }
143  virtual double Value() override { return Input1Value() + Input2Value(); }
144 #if 0
145  virtual const char *Name() const {
146  if(HasName()) return _name;
147  else return SIMLIB_create_tmp_name("_Add{%p}", this);
148  }
149 #endif
150 };
151 
152 
153 ////////////////////////////////////////////////////////////////////////////
154 /// block to subtract two inputs
155 class _Sub : public aContiBlock2 {
156  virtual void Eval() override {}
157  virtual void _Eval() override {}
158 public:
159  _Sub(Input a, Input b): aContiBlock2(a,b) {
160  Dprintf(("ctr: _Sub[%p](in1,in2)", this));
161  }
162  ~_Sub() {
163  Dprintf(("dtr: _Sub[%p]", this));
164  }
165  virtual double Value() override { return Input1Value() - Input2Value(); }
166 #if 0
167  virtual const char *Name() const {
168  if(HasName()) return _name;
169  else return SIMLIB_create_tmp_name("_Sub{%p}", this);
170  }
171 #endif
172 };
173 
174 
175 ////////////////////////////////////////////////////////////////////////////
176 /// multiplier block
177 class _Mul : public aContiBlock2 {
178  virtual void Eval() override {}
179  virtual void _Eval() override {}
180 public:
181  _Mul(Input a, Input b): aContiBlock2(a,b) {
182  Dprintf(("ctr: _Mul[%p](in1,in2)", this));
183  }
184  ~_Mul() {
185  Dprintf(("dtr: _Mul[%p]", this));
186  }
187  virtual double Value() override { return Input1Value() * Input2Value(); }
188 #if 0
189  virtual const char *Name() const {
190  if(HasName()) return _name;
191  else return SIMLIB_create_tmp_name("_Mul{%p}", this);
192  }
193 #endif
194 };
195 
196 
197 ////////////////////////////////////////////////////////////////////////////
198 /// divider block
199 class _Div : public aContiBlock2 {
200  virtual void Eval() override {}
201  virtual void _Eval() override {}
202 public:
203  _Div(Input a, Input b): aContiBlock2(a,b) {
204  Dprintf(("ctr: _Div[%p](in1,in2)", this));
205  }
206  ~_Div() {
207  Dprintf(("dtr: _Div[%p]", this));
208  }
209  virtual double Value() override { return Input1Value() / Input2Value(); }
210 #if 0
211  virtual const char *Name() const {
212  if(HasName()) return _name;
213  else return SIMLIB_create_tmp_name("_Div{%p}", this);
214  }
215 #endif
216 };
217 
218 
219 ////////////////////////////////////////////////////////////////////////////
220 // binary operators ...
221 ////////////////////////////////////////////////////////////////////////////
222 
223 /// block operator +
224 Input operator + (Input a, Input b) { return new _Add(a,b); }
225 /// block operator -
226 Input operator - (Input a, Input b) { return new _Sub(a,b); }
227 /// block operator *
228 Input operator * (Input a, Input b) { return new _Mul(a,b); }
229 /// block operator /
230 Input operator / (Input a, Input b) { return new _Div(a,b); }
231 
232 /// square function
233 Input Sqr(Input x) { return new _Mul(x,x); }
234 
235 ////////////////////////////////////////////////////////////////////////////
236 /// unary minus block
237 class _UMinus: public aContiBlock1 {
238  virtual void Eval() override {}
239  virtual void _Eval() override {}
240 public:
242  Dprintf(("ctr: _UMinus[%p](in)", this));
243  }
245  Dprintf(("dtr: _UMinus[%p]", this));
246  }
247  virtual double Value() override { return -InputValue(); }
248 #if 0
249  virtual const char *Name() const {
250  if(HasName()) return _name;
251  else return SIMLIB_create_tmp_name("_UMinus{%p}", this);
252  }
253 #endif
254 };
255 
256 
257 ////////////////////////////////////////////////////////////////////////////
258 // unary operators ...
259 ////////////////////////////////////////////////////////////////////////////
260 
261 /// unary - block operator
262 Input operator - (Input a) { return new _UMinus(a); }
263 
264 ////////////////////////////////////////////////////////////////////////////
265 /// block wrapper for simulation time
266 class _Time: public aContiBlock {
267  public:
268  _Time() {}
269  virtual double Value () override { return Time; }
270  virtual std::string Name() const override { return "T(Time)"; }
271 };
272 
273 /// block -- simulation time
274 static class _Time _T;
275 /// simulation time block reference
276 aContiBlock & T = _T; // TODO try Input
277 
278 } // namespace
279 
virtual void Eval() override
evaluate without loop detection
Definition: continuous.cc:156
multiplier block
Definition: continuous.cc:177
base for continuous blocks with two inputs
Definition: simlib.h:959
Input Sqr(Input x)
square function
Definition: continuous.cc:233
double Value() override
Evaluate expression and return the value.
Definition: continuous.cc:123
Input operator/(Input a, Input b)
block operator /
Definition: continuous.cc:230
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
static void EvaluateAll()
Definition: intg.cc:573
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: continuous.cc:165
block wrapper for simulation time
Definition: continuous.cc:266
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: continuous.cc:247
virtual void _Eval()
evaluate block (with loop detection)
Definition: continuous.cc:48
void SIMLIB_Dynamic()
performs evaluation of integrators and status blocks
Definition: continuous.cc:35
continuous block connection (transparent reference) wrapper for pointer to objects of aContiBlock der...
Definition: simlib.h:895
virtual void Eval() override
evaluate without loop detection
Definition: continuous.cc:238
block to subtract two inputs
Definition: continuous.cc:155
aContiBlock & T
simulation time block reference
Definition: continuous.cc:276
_Sub(Input a, Input b)
Definition: continuous.cc:159
aContiBlock3(Input i1, Input i2, Input i3)
ctr for blocks with 3 inputs
Definition: continuous.cc:88
bool HasName() const
Definition: simlib.h:321
aContiBlock1(Input i)
constructor for blocks with single input
Definition: continuous.cc:61
Input operator+(Input a, Input b)
block operator +
Definition: continuous.cc:224
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
class for algebraic loop detection AlgLoopDetector object should be used in Value() method only it ch...
Definition: internal.h:307
virtual std::string Name() const override
get object name
Definition: continuous.cc:270
std::string SIMLIB_create_tmp_name(const char *fmt,...)
printf-like function to create temporary name (the length of temporary names is limited) used only ...
Definition: name.cc:80
const double & Time
model time (is NOT the block)
Definition: run.cc:48
static void ClearAllValueOK()
Definition: intg.cc:589
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: continuous.cc:269
aContiBlock2(Input i1, Input i2)
ctr for blocks with 2 inputs
Definition: continuous.cc:79
virtual void _Eval() override
evaluate block (with loop detection)
Definition: continuous.cc:157
Input operator*(Input a, Input b)
block operator *
Definition: continuous.cc:228
virtual void Eval()
evaluate without loop detection
Definition: simlib.h:834
abstract base for continuous blocks with single output suitable for expression-tree building and eval...
Definition: simlib.h:832
_UMinus(Input a)
Definition: continuous.cc:241
Internal header file for SIMLIB/C++.
virtual void Eval() override
evaluate without loop detection
Definition: continuous.cc:178
block sum of two inputs
Definition: continuous.cc:133
virtual void _Eval() override
evaluate block (with loop detection)
Definition: continuous.cc:201
virtual void Eval() override
evaluate without loop detection
Definition: continuous.cc:134
Main SIMLIB/C++ interface.
virtual std::string Name() const
get object name
Definition: object.cc:134
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: continuous.cc:143
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
unary minus block
Definition: continuous.cc:237
divider block
Definition: continuous.cc:199
Input operator-(Input a, Input b)
block operator -
Definition: continuous.cc:226
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: continuous.cc:209
virtual void _Eval() override
evaluate block (with loop detection)
Definition: continuous.cc:179
_Div(Input a, Input b)
Definition: continuous.cc:203
double Input1Value()
Definition: simlib.h:977
base for continuous blocks with single input and algebraic loop check
Definition: simlib.h:940
virtual void _Eval() override
evaluate block (with loop detection)
Definition: continuous.cc:135
static class _Time _T
block – simulation time
Definition: continuous.cc:274
#define Dprintf(f)
Definition: internal.h:100
_Add(Input a, Input b)
Definition: continuous.cc:137
double Input2Value()
Definition: simlib.h:978
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
Definition: continuous.cc:187
virtual void _Eval() override
evaluate block (with loop detection)
Definition: continuous.cc:239
virtual void Eval() override
evaluate without loop detection
Definition: continuous.cc:200
_Mul(Input a, Input b)
Definition: continuous.cc:181
static void EvaluateAll()
Definition: intg.cc:363