8 #define __SIMLIB__ 0x0308 // version of SIMLIB (numerical) 9 #define SIMLIB_VERSION "3.08" // version of SIMLIB (string) 11 #define SIMLIB_COPYRIGHT "(c) Petr Peringer, 1991-2021" 43 # error "Use C++ compiler, please." 46 #if (__cplusplus < 201103L) 47 # error "Use C++11 (or newer) compiler, please." 67 #if defined(__MSDOS__) 68 # error "MSDOS not supported - please, use older version of SIMLIB" 70 #elif defined(__linux__) && defined(__GNUC__) 72 # define SIMLIB_SYSTEM "Linux" 73 # define SIMLIB_COMPILER "GNU C++" 75 #elif defined(__WIN32__) 77 # define SIMLIB_SYSTEM "WIN32" 78 # if defined(__BCPLUSPLUS__) 79 # define SIMLIB_COMPILER "Borland C++" 80 # elif defined(__GNUC__) 81 # define SIMLIB_COMPILER "GNU C++" 84 #elif defined(__FreeBSD__) && defined(__GNUC__) 86 # define SIMLIB_SYSTEM "FreeBSD" 87 # define SIMLIB_COMPILER "GNU C++" 90 # error "SIMLIB is not implemented for this system/compiler" 98 unsigned long Debug(
unsigned long mode=~0UL);
162 extern const double &
Time;
181 inline void Init(
double t0,
double t1=SIMLIB_MAXTIME) {
182 extern void SIMLIB_Init(
double t0,
double t1,
unsigned version);
193 void SetStep(
double dtmin,
double dtmax);
215 [[noreturn]]
void Error(
const char *fmt, ...);
219 int Print(
const char *fmt, ...);
222 int _Print(
const char *fmt, ...);
226 int Print(
const double x);
230 int Print(
const double x,
const double y);
235 int Print(
const double x,
const double y,
const double z);
264 double Beta(
double th,
double fi,
double min,
double max);
266 double Erlang(
double alfa,
int beta);
270 double Gamma(
double alfa,
double beta);
274 double Logar(
double mi,
double delta);
276 int NegBin(
double q,
int k);
279 double Normal(
double mi,
double sigma);
282 double Rayle(
double delta);
283 double Triag(
double mod,
double min,
double max);
285 double Uniform(
double l,
double h);
287 double Weibul(
double lambda,
double alfa);
313 void *
operator new(
size_t size);
314 void operator delete(
void *ptr);
315 void *
operator new[](
size_t size) =
delete;
316 void operator delete[](
void *ptr) =
delete;
320 virtual std::string
Name()
const;
322 void SetName(
const std::string &name);
325 virtual void Output()
const;
346 virtual void Into(
List *l);
396 unsigned long id()
const {
return _Ident; }
406 double ActivationTime();
411 virtual void Terminate() = 0;
412 bool Idle() {
return _evn==0; }
415 virtual void Out()
override;
419 virtual void _Run() noexcept = 0;
442 virtual void _Run() noexcept override;
446 _PREPARED=1, _RUNNING, _INTERRUPTED, _TERMINATED
455 void _WaitUntilRemove();
460 virtual void Behavior() = 0;
461 virtual void Output()
const override;
462 virtual std::string
Name()
const override;
464 virtual void Activate(
double t)
override;
466 virtual void Wait(
double dtime);
467 bool _WaitUntil(
bool test);
468 #ifdef I_REALLY_KNOW_HOW_TO_USE_WAITUNTIL 470 # define WaitUntil(condition) while(_WaitUntil(condition)) ; 473 virtual void Terminate()
override;
475 void Seize(
Facility &f, ServicePriority_t sp=0);
477 void Enter(
Store &s,
unsigned long ReqCap=1);
478 void Leave(
Store &s,
unsigned long ReqCap=1);
481 virtual void Into(
Queue &q);
489 virtual void _Run() noexcept
override;
490 virtual void Terminate()
override;
494 virtual void Behavior() = 0;
495 virtual void Output()
const override;
496 virtual std::string
Name()
const override;
498 virtual void Activate(
double t)
override;
514 virtual void Behavior()
override;
516 Sampler(
void (*pf)(),
double dt=0.0);
518 virtual void Output()
const override;
525 static void InitAll();
526 static void ActivateAll();
545 aStat(
const char *name);
547 virtual void Clear() = 0;
549 virtual void operator () (
double x) = 0;
550 virtual void Record(
double x) = 0;
552 virtual double MeanValue() = 0;
553 double Min()
const {
return min; }
554 double Max()
const {
return max; }
555 unsigned long NumberOfRecords()
const {
return n; }
556 virtual void Output()
const override;
572 explicit Stat(
const char *name);
574 virtual void Clear();
575 void operator () (
double x);
578 virtual void Output()
const override;
579 unsigned long Number()
const {
return n; }
582 double Sum()
const {
return sx; }
584 double MeanValue()
const;
585 double StdDev()
const;
606 explicit TStat(
double initval=0.0);
607 explicit TStat(
const char *name,
double initval=0.0);
609 virtual void Clear(
double initval=0.0);
610 virtual void Output()
const override;
611 virtual void operator () (
double x);
612 unsigned long Number()
const {
return n; }
615 double Sum()
const {
return sxt; }
620 double MeanValue()
const;
657 explicit List(
const char *_name);
659 virtual void Output()
const override;
661 void InsFirst(
Link *e);
662 void InsLast (
Link *e);
671 unsigned size()
const {
return n; }
694 explicit Queue(
const char *_name);
697 virtual void Output()
const override;
710 virtual void Insert (
Entity *e);
713 void PredIns (
Entity *e, iterator pos);
714 void PostIns (
Entity *e, iterator pos);
733 Histogram(
double low,
double step,
unsigned count=10);
734 Histogram(
const char *_name,
double low,
double step,
unsigned count=10);
736 virtual void Output()
const override;
737 void Init(
double low,
double step,
unsigned count);
738 void operator () (
double x);
739 virtual void Clear();
740 double Low()
const {
return low; }
741 double High()
const {
return low + step*count; }
742 double Step()
const {
return step; }
743 unsigned Count()
const {
return count; }
744 unsigned operator [](
unsigned i)
const;
762 explicit Facility(
const char *_name);
766 virtual void Output()
const override;
768 void SetQueue(
Queue *queue1);
769 bool OwnQueue()
const;
770 bool Busy()
const {
return in!=
nullptr; }
774 virtual void Release(
Entity *e);
775 virtual void QueueIn(
Entity *e, ServicePriority_t sp);
776 virtual void Clear();
778 virtual void QueueIn2(
Entity *e);
794 explicit Store(
unsigned long _capacity);
795 Store(
const char *_name,
unsigned long _capacity);
797 Store(
const char *_name,
unsigned long _capacity,
Queue *queue);
799 virtual void Output()
const override;
801 void SetCapacity(
unsigned long _capacity);
802 void SetQueue(
Queue *queue);
803 unsigned long Free()
const {
return capacity - used; }
804 unsigned long Used()
const {
return used; }
805 unsigned long Capacity()
const {
return capacity; }
806 bool Full()
const {
return Free() == 0; }
807 bool Empty()
const {
return Used() == 0; }
808 bool OwnQueue()
const;
810 virtual void Enter(
Entity *e,
unsigned long rcap);
811 virtual void Leave(
unsigned long rcap);
812 virtual void QueueIn(
Entity *e,
unsigned long c);
813 virtual void Clear();
834 virtual void Eval() {};
839 virtual void _Eval();
842 virtual double Value() = 0;
852 virtual double Value ()
override {
return value; }
863 virtual double Value ()
override {
return value; }
874 virtual double Value ()
override {
return value; }
882 #define RegisterReference(aContiBlockPtr) 883 #define UnRegisterReference(aContiBlockPtr) 953 double Value()
override;
990 static std::list<Integrator*> * Instance(
void);
995 return ListPtr!=0 && !(ListPtr->empty());
999 return (ListPtr!=0) ? (ListPtr->size()) : 0;
1003 return Instance()->begin();
1007 return Instance()->end();
1010 static void Erase(iterator it);
1011 static void InitAll();
1012 static void EvaluateAll();
1025 static std::list<Status*>* Instance(
void);
1030 return ListPtr!=0 && !(ListPtr->empty());
1034 return (ListPtr!=0) ? (ListPtr->size()) : 0;
1038 return Instance()->begin();
1042 return Instance()->end();
1044 static iterator Insert(
Status* ptr);
1045 static void Erase(iterator it);
1046 static void InitAll();
1047 static void ClearAllValueOK();
1048 static void EvaluateAll();
1064 std::list<IntegrationMethod*>::iterator
ItList;
1075 static bool Prepare(
void);
1076 static void Iterate(
void);
1077 static void Summarize(
void);
1087 static bool StateCond(
void);
1102 explicit Memory(std::list<Memory*> * PtrList=PtrMList);
1109 virtual void Resize(
size_t cs);
1115 virtual bool IsSingleStep(
void)=0;
1116 virtual void TurnOff(
void);
1117 virtual void Integrate(
void) = 0;
1118 virtual bool PrepareStep(
void);
1119 virtual void Resize(
size_t size);
1120 static void StepSim(
void);
1124 static void SetMethod(
const char* name);
1129 static void InitStep(
double step_frag);
1130 static void FunCall(
double step_frag);
1131 static void SetOptStep(
double opt_step);
1132 static void SetStepSize(
double step_size);
1133 static bool IsConditionFlag(
void);
1134 static int GetErrNo(
void);
1149 StartMode(false) { }
1152 StartMode=start_mode;
1174 static void SetStarter(
const char* name,
const char* slave_name);
1176 virtual void SetStarter(
const char* slave_name);
1178 static const char*
GetStarter(
const char* name);
1179 virtual bool PrepareStep(
void)
override;
1180 virtual void TurnOff(
void)
override;
1211 explicit StatusMemory(std::list<Memory*>* PtrList=PtrStatusMList) : Memory(PtrList) { }
1221 static void RestoreState(
double dthlf,
Memory& di,
1229 virtual void TurnOff(
void)
override;
1230 virtual bool PrepareStep(
void)
override;
1231 virtual void StatusResize(
size_t size);
1305 void Init(
double initvalue);
1307 void Set(
double value);
1312 void Eval()
override;
1313 double Value()
override;
1318 void Save(
void) { ddl=dd; ssl=ss; }
1346 void Init(
double initvalue);
1348 void Set(
double value);
1349 virtual void Eval()
override;
1350 virtual double Value()
override;
1374 virtual void Eval()
override;
1376 Hyst(
Input i,
double p1,
double p2,
double y1,
double y2,
double tga);
1391 Blash(
Input i,
double p1,
double p2,
double tga);
1392 virtual void Eval()
override;
1407 Relay(
Input i,
double p1,
double p2,
double p3,
double p4,
1408 double y1,
double y2);
1409 virtual void Eval()
override;
1454 double (*f)(double);
1457 virtual double Value()
override;
1468 double (*f)(double,double);
1471 virtual double Value()
override;
1492 static void InitAll();
1493 static void SetAll();
1494 static void TestAll();
1495 static void AllActions();
1496 static bool isAny();
1498 virtual void Init()=0;
1499 virtual void SetNewStatus()=0;
1500 virtual bool Test()=0;
1501 virtual void Action()=0;
1510 virtual void Init()
override;
1511 virtual void SetNewStatus()
override;
1513 virtual bool Test()
override;
1514 bool Up() {
return ccl<cc; }
1561 Lim(
Input in,
double l,
double h,
double tga=1.0);
1562 virtual void _Eval()
override;
1563 virtual double Value()
override;
1578 Insv(
Input in,
double l,
double h,
double tga=1.0,
double tgb=1.0);
1579 virtual void _Eval()
override;
1580 virtual double Value()
override;
1595 virtual void _Eval()
override;
1596 virtual double Value()
override;
1611 Frict(
Input in,
double l,
double h,
double tga=1.0);
1612 virtual void _Eval()
override;
1613 virtual double Value()
override;
1630 Rline(
Input in,
int num,
double *X,
double *Y);
1633 virtual void _Eval()
override;
1634 virtual double Value()
override;
1658 double t_min,
double t_max,
double t0);
1659 void Set(
double eps,
unsigned long max_it,
1660 double t_min,
double t_max,
double t0);
1661 void Set(
double eps,
unsigned long max_it,
1662 double t_min,
double t_max);
1674 double t_min,
double t_max,
double t0):
1675 AlgLoop(i, eps, max_it, t_min, t_max, t0)
1677 virtual double Value()
override;
1688 double t_min,
double t_max):
1689 AlgLoop(i, eps, max_it, t_min, t_max, t_min)
1691 virtual double Value()
override;
1703 double t_min,
double t_max):
1704 AlgLoop(i, eps, max_it, t_min, t_max, t_min),
1707 virtual double Value()
override;
1720 double t_min,
double t_max,
double t0):
1721 AlgLoop(i, eps, max_it, t_min, t_max, t0),
1725 virtual double Value()
override;
1766 virtual void Output()
const override;
1784 Barrier(
const char *_name,
unsigned N);
1786 void ChangeHeight(
unsigned new_maxn);
1788 virtual void Enter(
Entity *e);
1789 virtual bool Wait();
1790 virtual int Break();
1792 virtual void Output()
const override;
1823 #endif // ifndef __SIMLIB__
bool isInterrupted() const
SingleStepMethod * Slave_Ptr
std::list< Integrator * >::iterator iterator
void Init()
use preset initial value
StatusContainer::iterator it_list
position in list of status variables
unsigned QueueLen() const
base for continuous blocks with two inputs
std::list< Status * >::iterator iterator
(SOL-like) facility Facility with exclusive access and service priority
unsigned long _RequiredCapacity
nonlinear block: dead zone
double Random()
base uniform generator (range 0-0.999999...) the default implementation is simple LCG 32bit ...
bool Empty() const
store is empty
Input Sqr(Input x)
square function
const double SIMLIB_MAXTIME
maximum time (1e30 works for float, too)
void SetAccuracy(double _abserr, double _relerr)
set max.
static bool IsEndStepEvent
List * head
pointer to List (if any)
unsigned n
current number of waiting entities
nonlinear block: limitation
virtual bool Test() override
Condition::operator ()
double step
step of sampling
block: parameter (can not be changed during simulation run)
const char * GetStarter(const char *name)
get the name of the method which is used to start given multistep method
double Rayle(double delta)
Input operator/(Input a, Input b)
block operator /
unsigned char ServicePriority_t
Service priority (see Facility::Seize)
static unsigned long _Number
current number of entities
void DebugON()
start debugging output
int _Print(const char *fmt,...)
output of messages to stdout, too
const double & AbsoluteError
max. abs. error of integration
nonlinear block: function defined by given table of values
double Exponential(double mv)
Exponential distribution generator.
Input Min(Input x, Input y)
(SOL-like) store store capacity can be changed dynamically
int Poisson(double lambda)
Poisson distribution generator.
State variables (memory) base for blocks with internal state (Relay, ...)
IntegrationMethod - Abstract base class for integration methods.
void * _context
process context pointer
synchronization tool for processes
Abstract base class for integration methods with status auxiliary memories.
void SetName(const std::string &name)
assign the name
nonlinear block: friction
Input Max(Input x, Input y)
bool Full() const
store is full
unsigned QueueLen() const
Priority_t Priority
priority of the entity (scheduling,queues)
const double & StepSize
actual integration step
double stl
status from previous step
solve using bisection method
void SetOldState(double s)
SimObject & operator=(const SimObject &)=delete
disable assign operation
double & operator[](size_t ind)
void Stop()
stop current simulation run
bool Test() override
Condition::operator ()
double last
last sample time – prevents sample duplication
signed char EntityPriority_t
Priority of the process/event.
Newton(Input i, double eps, unsigned long max_it, double t_min, double t_max, double t0)
EventNotice * GetEventNotice()
unsigned long used
Currently used capacity.
bool Busy() const
in service
double Weibul(double lambda, double alfa)
Weibul distribution generator.
int NegBin(double q, int k)
bool Test() override
Condition::operator ()
static void SetStarter(const char *name, const char *slave_name)
set starting method for given multi-step method
void Run()
run simulation experiment
aContiBlock & T
simulation time block reference
static void IntegrationDone(void)
const double & OptStep
optimal integration step
static iterator End(void)
double Triag(double mod, double min, double max)
class for statistical information gathering
base for single-step integration methods
base for continuous blocks vith three inputs and algebraic loop check
void ContractStep()
contract step of integration
unsigned _flags
bool flags for internal use (TODO bitfield?)
virtual void Passivate()
deactivation
Entity * In() const
current entity or nullptr
void Get(Entity *e)
remove selected entity activation record from calendar
std::list< Memory * >::iterator it_list
solve using modified Newton's method
EventNotice * _evn
points to calendar item, iff scheduled
int Print(const char *fmt,...)
for Output methods, can be redirected
objects of this class call global function periodically (typicaly used for output of continuous model...
unsigned long Debug(unsigned long mode)
bool isTerminated() const
double GetStep() const
get current step
bool Value()
boolean state of condition
double Beta(double th, double fi, double min, double max)
Beta distribution generator.
unsigned char _Qflag
true if store is owner of input queue
Input operator+(Input a, Input b)
block operator +
static iterator Begin(void)
std::list< Memory * > * ListPtr
base for multi-step integration method
void SetStarter(const char *name, const char *slave_name)
set the method which will be used to start given multistep method
static std::list< IntegrationMethod * > * MthLstPtr
reference to block expression This block can be used as handle for block expression trees ...
block: variable (value can be changed)
base class for all double-linked list items item can be at single place only (identified by where()...
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
double max(double a, double b)
virtual void Output() const
print object to default output
virtual void Into(List *l)
insert last
static void SetMethod(const char *name)
set method which will be used
Bisect(Input i, double eps, unsigned long max_it, double t_min, double t_max)
const double & RelativeError
max. rel. error
unsigned long _Ident
unique identification number of entity
iterator & operator=(const iterator &x)
static Link * next(Link *p)
abstract base class for active entities (Process, Event) instances of derived classes provide Behavio...
Entity *const & Current
pointer to active (now running) entity
Base class for almost all SIMLIB classes.
virtual bool isInQueue()
present in queue
static std::list< Memory * > * PtrMList
abstract base for algebraic loop solvers
const double & Time
model time (is NOT the block)
void Error(const char *fmt,...)
print message and terminate program
void RandomSeed(long seed)
initialize random number seed
void SetStep(double _dtmin, double _dtmax)
Set integration step interval.
void SetOldState(double s)
double initval
initial value: y(t0)
std::list< Memory * > StatusMList
void RemoveName(SimObject &o)
remove name
void Passivate(Entity *e)
passivate entity e
std::string GetName(SimObject &o)
get name of object
StatusMemory(std::list< Memory *> *PtrList=PtrStatusMList)
void SIMLIB_Init(double T0, double T1, unsigned version)
virtual ~SimObject()
virtual destructor
Link * succ
next object in List
static aCondition * First
unsigned Height() const
barrier size
static std::list< Memory * > * PtrStatusMList
nonlinear block - hysteresis
unsigned long Number() const
Link * pred
previous object in List
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
basic synchronization tool for processes simplified implementation
unsigned char _Qflag
true if facility is owner of input queue
static iterator Begin(void)
Input operator*(Input a, Input b)
block operator *
const char * SIMLIB_version_string()
Get version of SIMLIB.
virtual bool IsSingleStep(void) override
unsigned long Used() const
used capacity
double Normal(double mi, double sigma)
Gauss distribution generator.
static IntegrationMethod * CurrentMethodPtr
pointer to the method currently used "rke" is a predefined method (historical reasons, we need rk45)
bool operator==(const ParameterVector &p1, const ParameterVector &p2)
const double & EndTime
time of simulation end
double min(double a, double b)
double Gamma(double alfa, double beta)
Gamma distribution generator.
abstract base for continuous blocks with single output suitable for expression-tree building and eval...
static iterator End(void)
void SetOldDiff(double d)
Entity * GetFirst()
remove entity with minimum activation time
internal flags (bitmasks: 1bit set)
Relay this implementation detects exact time of switch.
change detector - Boolean version (int)
TStat tstat
usage statistics
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
virtual void TurnOff(void)
turn off integration method: flush memories etc...
void InstallBreak(void(*f)())
InstallBreak — set function for checking if user breaks simulation.
int HyperGeom(double p, int n, int m)
StatusContainer - internal container of status variables (singleton)
static Iterator LastIntegrator(void)
iterator(const iterator &x)
unsigned long Capacity() const
max capacity
void Init(double t0, double t1=SIMLIB_MAXTIME)
Initialize simulator and model time.
nonlinear block: quantizer
const double & NextTime
next-event time
std::list< Memory * > MList
abstract base class for events Event behavior is simple function (can not be interrupted) ...
void Activate(Entity *e)
activate entity e
Iterations(Input i, double eps, unsigned long max_it, double t_min, double t_max, double t0)
abstract base class for all state-condition blocks State event in combined model is executed at the t...
void SetMethod(const char *name)
select the integration method
static Iterator FirstIntegrator(void)
double Erlang(double alfa, int beta)
Erlang distribution generator.
unsigned maxn
barrier height/size
unsigned long Number() const
#define RegisterReference(aContiBlockPtr)
void SetCalendar(const char *name)
choose calendar implementation default is list
virtual std::string Name() const
get object name
double Uniform(double l, double h)
Uniform distribution generator.
RegulaFalsi(Input i, double eps, unsigned long max_it, double t_min, double t_max)
void Cancel()
end Behavior() and remove entity
void SetOutput(const char *name)
redirects Output(), Print() to file
IntegratorContainer::iterator Iterator
Input ATan2(Input y, Input x)
const char * GetMethod(void)
get the name of the method which is used
block parametrized by function with two arguments
IntegratorContainer - internal container of integrators (singleton)
Input input
input expression: f(t,y)
static const size_t page_size
static std::list< Status * > * ListPtr
list of status variables
void SetBaseRandomGenerator(double(*new_gen)())
set another random generator default Random() implementation can be replaced
bool Idle()
entity activation is not scheduled in calendar
Input operator-(Input a, Input b)
block operator -
bool TestAndSetFlag(bool new_value, unsigned n)
internal method for flag manipulation is used for algebraic loop checking in continuous blocks ...
#define UnRegisterReference(aContiBlockPtr)
IntegratorContainer::iterator it_list
position in list of integrators
static std::list< Integrator * > * ListPtr
list of integrators
abstract base class for all blocksblocks/SimObject have not copy semantics (TODO: add move semantics)...
List * Where()
where is linked
Entity ** waiting
array of waiting entities (fixed size)
Input Pow(Input x, Input y)
Queue * Q2
Interrupted requests queue.
double InputValue()
current input value
Input SetInput(Input inp)
set integrator input block expression
virtual double Value() override
get block output value this method should be defined in classes derived from aContiBlock ...
int NegBinM(double p, int m)
static Link * previous(Link *p)
block: constant (value can not be changed)
base for continuous blocks with single input and algebraic loop check
unsigned char ccl
old state
void Activate()
activate now
Abstract base class for all simulation processesProcess behavior is specified by Behavior method and ...
EntityPriority_t Priority_t
static const char * GetStarter(const char *name)
obtain the name of the starting method of given method
Input SetInput(Input inp)
double Logar(double mi, double delta)
virtual double Value()=0
get block output value this method should be defined in classes derived from aContiBlock ...
block for numerical integration input is derivative, output is state
ServicePriority_t _SPrio
priority of service in Facility
void DebugOFF()
stop debugging output
list of Link* items, uses data from Link
static const char * GetMethod(void)
const double & MinStep
minimal integration step
virtual bool IsSingleStep(void) override
std::list< IntegrationMethod * >::iterator ItList
const double SIMLIB_MINTIME
minimal time value
void SetStartMode(bool start_mode)
ProcessStatus_t
possible process status values
solve using regula falsi method
TStat tstat
usage statistics
StatusContainer::iterator StatusIterator
const SIMLIB_statistics_t & SIMLIB_statistics
interface to internal run-time statistics structure
block parametrized by function with single argument
static StatusIterator FirstStatus(void)
void Abort()
end simulation program
Entity * in
Entity currently in service.
nonlinear block - backlash
unsigned long capacity
Capacity of store.
const double & MaxStep
maximal integration step
internal statistics structure contains basic statistics of simulator execution
unsigned long Free() const
free capacity
SingleStepMethod(const char *name)
const double & StartTime
time of simulation start
const unsigned SIMLIB_version
library version
static StatusIterator LastStatus(void)
double initval
initial value
void Clear()
remove all scheduled entities