SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
simlib2D.cc
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//! \file simlib2D.cc 2D vector block extension
3//
4// Copyright (c) 1997-2004 Petr Peringer
5//
6// This library is licensed under GNU Library GPL. See the file COPYING.
7//
8
9//
10// 2D extension
11//
12// - Integrator2D
13// - blocks for + - * / operations
14//
15
16
17////////////////////////////////////////////////////////////////////////////
18// interface
19//
20
21#include "simlib.h"
22#include "simlib2D.h"
23#include "internal.h"
24#include <cmath>
25
26////////////////////////////////////////////////////////////////////////////
27// implementation
28//
29
30namespace simlib3 {
31
33
34////////////////////////////////////////////////////////////////////////////
35// non-block operations
36//
37
38double abs(const Value2D &a)
39{
40 return sqrt( a._x * a._x + a._y * a._y );
41}
42
43Value2D operator +(const Value2D& a, const Value2D &b)
44{
45 return Value2D( a._x + b._x, a._y + b._y );
46}
47
48Value2D operator -(const Value2D& a, const Value2D &b)
49{
50 return Value2D( a._x - b._x, a._y - b._y );
51}
52
54{
55 return Value2D( -a._x, -a._y);
56}
57
58/*
59Value2D operator *(const Value2D& a, const Value2D &b)
60{
61 return Value2D( a._y * b._z - a._z * b._y,
62 a._z * b._x - a._x * b._z,
63 a._x * b._y - a._y * b._x
64 );
65}
66*/
67
68Value2D operator *(const Value2D& a, const double b)
69{
70 return Value2D( a._x * b, a._y * b );
71}
72
73Value2D operator *(const double a, const Value2D& b)
74{
75 return b * a;
76}
77
78double scalar_product(const Value2D& a, const Value2D &b)
79{
80 return a._x * b._x + a._y * b._y ;
81}
82
83Value2D operator /(const Value2D& a, const double b)
84{
85 return Value2D( a._x / b, a._y / b );
86}
87
88
89////////////////////////////////////////////////////////////////////////////
90// aContiBlock2D --- constructor & destructor
91//
93 isEvaluated(false)
94{
95 Dprintf(("2Dblock-ctr"));
96}
97
99 Dprintf(("2Dblock-dtr"));
100}
101
102////////////////////////////////////////////////////////////////////////////
103// aContiBlock2D::Print --- print of 2D block output value
104//
106 Value2D a = Value();
107 a.Print();
108}
109
110////////////////////////////////////////////////////////////////////////////
111// aContiBlock::_Eval --- evaluation with algebraic loop detection ###
112//
114{
115 if(isEvaluated) // was evaluated
116 SIMLIB_error(AlgLoopDetected); // recursive call
117 isEvaluated = true; // eval-flag
118 Eval(); // evaluation of block
119 isEvaluated = false;
120}
121
122////////////////////////////////////////////////////////////////////////////
123// Parameter2D --- parameter of model
124//
128 value = x;
129 return *this;
130}
131
132
133////////////////////////////////////////////////////////////////////////////
134// aContiBlock1 --- base for blocks with one input
135//
137{
138 if(input==this)
140// LoopCheck();
141}
142
143
144////////////////////////////////////////////////////////////////////////////
145// Integrator2D::special_input::Value() --- expects 2 subsequent evaluations
146//
147// return scalar value for scalar integrator
148// two evaluations are guaranteed by Integrator2D
149//
150double Integrator2D::special_input::Value() { // interface class
151 if(count == 0)
152 a = in.Value(); // get vector input (x,y)
153 switch(++count) {
154 case 1: return a.x(); // first call (x)
155 case 2: count=0; // second call (y)
156 return a.y();
157 } // switch
158 SIMLIB_internal_error(); // *** never reached ***
159 return 0; // compiler warning elimination
160}
161
162////////////////////////////////////////////////////////////////////////////
163// Integrator2D --- constructors
164//
166 _x(in, initial_value.x()), // linking of inputs to integrators
167 _y(in, initial_value.y()),
168 in(i) {}
169
171 _x(in), _y(in),
172 in(i) {}
173
174// --- special copy constructor
177 _x(in, initial_value.x()), // linking of inputs to integrators
178 _y(in, initial_value.y()),
179 in(i) {}
180
181// zero input for default Integrator2D constructor
182// we need it for creating arrays of integrators
183static Constant2D Zero2D(0,0);
184
186 _x(in), _y(in),
187 in(Zero2D) {}
188
189
190////////////////////////////////////////////////////////////////////////////
191// operator = --- assign new value
192//
194{
195 _x=a.x();
196 _y=a.y();
197 return *this;
198}
199
201{
202 Value2D a = i.Value();
203 _x=a.x();
204 _y=a.y();
205 return *this;
206}
207
208
209////////////////////////////////////////////////////////////////////////////
210// Integrator2D output method
211//
213{
214 return Value2D(_x.Value(), _y.Value());
215}
216
217////////////////////////////////////////////////////////////////////////////
218// aContiBlock2 --- base for 2 input blocks
219//
221 : input1(i1), input2(i2)
222{
223 if(input1==this || input2==this)
225// LoopCheck();
226}
227
229 : aContiBlock2D2(i1,i2), input3(i3)
230{
231 if(input3==this )
233}
234
235
236////////////////////////////////////////////////////////////////////////////
237// Add --- sum of two inputs
238//
239class _Add2D : public aContiBlock2D2 {
240 virtual void _Eval() override {}
241public:
243 Dprintf(("ctr: _Add2D[%p](in1,in2)", this));
244 }
245 virtual Value2D Value() override {
246 Value2D a = Input1Value();
247 Value2D b = Input2Value();
248 return a + b;
249 }
250};
251
252
253////////////////////////////////////////////////////////////////////////////
254// Sub --- subtract two inputs
255//
256class _Sub2D : public aContiBlock2D2 {
257 virtual void _Eval() override {}
258public:
260 Dprintf(("ctr: _Sub2D[%p](in1,in2)", this));
261 }
262 virtual Value2D Value() override {
263 Value2D a = Input1Value();
264 Value2D b = Input2Value();
265 return a - b;
266 }
267};
268
269////////////////////////////////////////////////////////////////////////////
270// Mul --- multiplier vector * scalar
271//
272class _Mul2D1D : public aContiBlock2D1 {
274 virtual void _Eval() override {}
275public:
277 Dprintf(("ctr: _Mul2D1D[%p](in1,in2)", this));
278 }
279 virtual Value2D Value() override {
280 Value2D a = InputValue();
281 double b = _b.Value();
282 return a * b;
283 }
284};
285
286
287////////////////////////////////////////////////////////////////////////////
288// Div --- divider vector / scalar
289//
290class _Div2D : public aContiBlock2D1 {
292 virtual void _Eval() override {}
293public:
295 Dprintf(("ctr: _Div2D[%p](in1_2D,in2)", this));
296 }
297 virtual Value2D Value() override {
298 Value2D a = InputValue();
299 double b = _b.Value();
300 return a / b;
301 }
302};
303
304////////////////////////////////////////////////////////////////////////////
305// binary operators ...
306//
307// wrapper operator functions for block expressions
308//
309
310Input2D operator + (Input2D a, Input2D b) { return new _Add2D(a,b); }
311Input2D operator - (Input2D a, Input2D b) { return new _Sub2D(a,b); }
312Input2D operator * (Input2D a, Input b) { return new _Mul2D1D(a,b); }
313Input2D operator * (Input a, Input2D b) { return new _Mul2D1D(b,a); }
314Input2D operator / (Input2D a, Input b) { return new _Div2D(a,b); }
315
316////////////////////////////////////////////////////////////////////////////
317// UMinus --- unary minus
318//
320 virtual void _Eval() override {}
321public:
323 Dprintf(("ctr: _UMinus2D[%p](in)", this));
324 }
325 virtual Value2D Value() override {
326 Value2D a = InputValue();
327 return -a;
328 }
329};
330
331////////////////////////////////////////////////////////////////////////////
332// unary operators ...
333//
335
336////////////////////////////////////////////////////////////////////////////
337// functions for block expressions
338//
339
340////////////////////////////////////////////////////////////////////////////
341// _Abs2D --- absolute value of vector x
342//
343class _Abs2D: public aContiBlock {
344 virtual void _Eval() override {}
346public:
347 _Abs2D(Input2D a): in(a) {}
348 virtual double Value() override {
349 Value2D a = in.Value();
350 return abs(a);
351 }
352};
353
354////////////////////////////////////////////////////////////////////////////
355// _Norm2D --- unit vector = vector x/Abs(x)
356//
357class _Norm2D: public aContiBlock2D1 {
358 virtual void _Eval() override {}
359public:
361 virtual Value2D Value() override {
362 Value2D a = InputValue();
363 return a/abs(a);
364 }
365};
366
367////////////////////////////////////////////////////////////////////////////
368// _ScalarProduct2D --- scalar multiply
369//
371 virtual void _Eval() override {}
374public:
376 virtual double Value() override {
377 Value2D a = input1.Value();
378 Value2D b = input2.Value();
379 return scalar_product(a,b);
380 }
381};
382
383////////////////////////////////////////////////////////////////////////////
384// wrapper functions for block expressions
385//
386
387Input Abs(Input2D x) { return new _Abs2D(x); } // absolute value of vector x
388Input2D UnitVector(Input2D x) { return new _Norm2D(x); }
390
391
392////////////////////////////////////////////////////////////////////////////
393// _XYpart --- vector part
394//
395class _XYpart: public aContiBlock {
396 virtual void _Eval() override {}
398 public:
399 enum WhichPart { x,y }; // part identification
401 virtual double Value() override {
402 Value2D a = input.Value();
403 switch(which) {
404 case x: return a.x();
405 case y: return a.y();
406 }
407 SIMLIB_internal_error(); // *** never reached ***
408 return 0; // compiler warning elimination
409 }
410 private:
412};
413
414////////////////////////////////////////////////////////////////////////////
415// get x,y part of vector block
416//
417// wrapper functions for block expressions
418//
419
420Input Xpart(Input2D a) { return new _XYpart(a, _XYpart::x); }
421Input Ypart(Input2D a) { return new _XYpart(a, _XYpart::y); }
422
423} // end
424
2D vector value that can't be changed
Definition: simlib2D.h:76
Continuous block connection (transparent reference)
Definition: simlib2D.h:109
Value2D Value()
Definition: simlib2D.h:117
continuous block connection (transparent reference) wrapper for pointer to objects of aContiBlock der...
Definition: simlib.h:895
double Value() const
get target block value
Definition: simlib.h:931
double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib2D.cc:150
2D vector integrator
Definition: simlib2D.h:179
Integrator2D & operator=(const Value2D &a)
Definition: simlib2D.cc:193
virtual Value2D Value() override
Definition: simlib2D.cc:212
simlib3::Integrator2D::special_input in
double Value() override
the state of integrator
Definition: intg.cc:249
Special variable (can't be changed at simulation time)
Definition: simlib2D.h:100
Parameter2D & operator=(const Value2D &x)
Definition: simlib2D.cc:125
2D vector value
Definition: simlib2D.h:31
double y() const
Definition: simlib2D.h:36
double x() const
Definition: simlib2D.h:35
void Print()
Definition: simlib2D.h:42
_Abs2D(Input2D a)
Definition: simlib2D.cc:347
virtual void _Eval() override
evaluate block (with loop detection)
Definition: simlib2D.cc:344
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib2D.cc:348
_Add2D(Input2D a, Input2D b)
Definition: simlib2D.cc:242
virtual void _Eval() override
Definition: simlib2D.cc:240
virtual Value2D Value() override
Definition: simlib2D.cc:245
virtual void _Eval() override
Definition: simlib2D.cc:292
_Div2D(Input2D a, Input b)
Definition: simlib2D.cc:294
virtual Value2D Value() override
Definition: simlib2D.cc:297
virtual void _Eval() override
Definition: simlib2D.cc:274
_Mul2D1D(Input2D a, Input b)
Definition: simlib2D.cc:276
virtual Value2D Value() override
Definition: simlib2D.cc:279
virtual void _Eval() override
Definition: simlib2D.cc:358
virtual Value2D Value() override
Definition: simlib2D.cc:361
_Norm2D(Input2D a)
Definition: simlib2D.cc:360
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib2D.cc:376
_ScalarProduct2D(Input2D a, Input2D b)
Definition: simlib2D.cc:375
virtual void _Eval() override
evaluate block (with loop detection)
Definition: simlib2D.cc:371
virtual void _Eval() override
Definition: simlib2D.cc:257
_Sub2D(Input2D a, Input2D b)
Definition: simlib2D.cc:259
virtual Value2D Value() override
Definition: simlib2D.cc:262
virtual void _Eval() override
Definition: simlib2D.cc:320
virtual Value2D Value() override
Definition: simlib2D.cc:325
_UMinus2D(Input2D a)
Definition: simlib2D.cc:322
WhichPart which
Definition: simlib2D.cc:411
_XYpart(Input2D a, WhichPart w)
Definition: simlib2D.cc:400
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock
Definition: simlib2D.cc:401
virtual void _Eval() override
evaluate block (with loop detection)
Definition: simlib2D.cc:396
continuous block with single input and alg.
Definition: simlib2D.h:124
aContiBlock2D1(Input2D i)
Definition: simlib2D.cc:136
continuous block vith 2 inputs and alg.
Definition: simlib2D.h:143
aContiBlock2D2(Input2D i1, Input2D i2)
Definition: simlib2D.cc:220
aContiBlock2D3(Input2D i1, Input2D i2, Input2D i3)
Definition: simlib2D.cc:228
Abstract 2D block with single 2D output.
Definition: simlib2D.h:60
virtual void _Eval()
Definition: simlib2D.cc:113
virtual void Eval()
Definition: simlib2D.h:64
virtual Value2D Value()=0
abstract base for continuous blocks with single output suitable for expression-tree building and eval...
Definition: simlib.h:832
@ ParameterChangeErr
Definition: errors.h:89
@ AlgLoopDetected
Definition: errors.h:65
Internal header file for SIMLIB/C++.
#define Dprintf(f)
Definition: internal.h:100
#define SIMLIB_internal_error()
Definition: internal.h:167
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition: algloop.cc:32
Input operator-(Input a, Input b)
block operator -
Definition: continuous.cc:226
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
Input operator+(Input a, Input b)
block operator +
Definition: continuous.cc:224
@ SIMULATION
Definition: internal.h:79
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
Input Abs(Input x)
Definition: fun.cc:94
double abs(const Value2D &a)
Definition: simlib2D.cc:38
Input Ypart(Input2D a)
get y part of (x,y) vector value
Definition: simlib2D.cc:421
Input Xpart(Input2D a)
get x part of (x,y) vector value
Definition: simlib2D.cc:420
Input operator*(Input a, Input b)
block operator *
Definition: continuous.cc:228
double scalar_product(const Value2D &a, const Value2D &b)
Definition: simlib2D.cc:78
Input ScalarProduct(Input2D x, Input2D y)
dot product: xvec . yvec
Definition: simlib2D.cc:389
static Constant2D Zero2D(0, 0)
SIMLIB_Phase_t SIMLIB_Phase
Definition: run.cc:57
Input operator/(Input a, Input b)
block operator /
Definition: continuous.cc:230
Input2D UnitVector(Input2D x)
make unit vector from input (Abs(output_vec)==1)
Definition: simlib2D.cc:388
2D vector block extension
Main SIMLIB/C++ interface.