00001
00011 #include <stdlib.h>
00012 #include <math.h>
00013 #include <stdio.h>
00014 #include <time.h>
00015 #include <stdarg.h>
00016 #include <memory.h>
00017 #include <float.h>
00018 #include <string.h>
00019
00020 #ifdef _WINDOWS
00021 #define isfinite _finite
00022 #endif
00023
00024 #define int64 long long
00025
00026 typedef enum {I='i',J='j',A='d'} CNOTATION;
00027 #define CNOTATION_DEFAULT J
00028 #define PI 3.1415926535897932384626433832795
00029 #define E 2.71828182845905
00030
00031
00032 class complex {
00033 private:
00034 double r;
00035 double i;
00036 CNOTATION f;
00037 public:
00039 inline complex()
00040 {
00041 r = 0;
00042 i = 0;
00043 f = CNOTATION_DEFAULT;
00044 };
00045 inline complex(double re)
00046 {
00047 r = re;
00048 i = 0;
00049 f = CNOTATION_DEFAULT;
00050 };
00051 inline complex(double re, double im, CNOTATION nf=CNOTATION_DEFAULT)
00052 {
00053 r = re;
00054 i = im;
00055 f = nf;
00056 };
00057
00058
00059 inline complex &operator = (complex x)
00060 {
00061 r = x.r;
00062 i = x.i;
00063 f = x.f;
00064 return *this;
00065 };
00066 inline complex &operator = (double x)
00067 {
00068 r = x;
00069 i = 0;
00070 f = CNOTATION_DEFAULT;
00071 return *this;
00072 };
00073
00074
00075 inline double & Re(void)
00076 {
00077 return r;
00078 };
00079 inline double & Im(void)
00080 {
00081 return i;
00082 };
00083 inline CNOTATION & Notation(void)
00084 {
00085 return f;
00086 };
00087 inline double Mag(void) const
00088 {
00089 return sqrt(r*r+i*i);
00090 };
00091 inline double Mag(double m)
00092 {
00093 double old = sqrt(r*r+i*i);
00094 r *= m/old;
00095 i *= m/old;
00096 return m;
00097 };
00098 inline double Arg(void) const
00099 {
00100 if (r==0)
00101 {
00102 if (i>0)
00103 return PI/2;
00104 else if (i==0)
00105 return 0;
00106 else
00107 return -PI/2;
00108 }
00109 else if (r>0)
00110 return atan(i/r);
00111 else
00112 return PI+atan(i/r);
00113 };
00114 inline double Arg(double a)
00115 {
00116 SetPolar(Mag(),a,f);
00117 return a;
00118 };
00119 inline complex Log(void) const
00120 {
00121 return complex(log(Mag()),Arg(),f);
00122 };
00123 inline void SetReal(double v)
00124 {
00125 r = v;
00126 };
00127 inline void SetImag(double v)
00128 {
00129 i = v;
00130 };
00131 inline void SetNotation(CNOTATION nf)
00132 {
00133 f = nf;
00134 }
00135 inline void SetRect(double rp, double ip, CNOTATION nf=CNOTATION_DEFAULT)
00136 {
00137 r = rp;
00138 i = ip;
00139 f = nf;
00140 };
00141 inline void SetPolar(double m, double a, CNOTATION nf=A)
00142 {
00143 r = (m*cos(a));
00144 i = (m*sin(a));
00145 f = nf;
00146 };
00147
00148 #if 0
00149
00150
00151
00152
00153 #endif
00154
00155 inline complex operator - (void)
00156 {
00157 return complex(-r,-i,f);
00158 };
00159 inline complex operator ~ (void)
00160 {
00161 return complex(r,-i,f);
00162 };
00163
00164
00165 inline complex &operator += (double x)
00166 {
00167 r += x;
00168 return *this;
00169 };
00170 inline complex &operator -= (double x)
00171 {
00172 r -= x;
00173 return *this;
00174 };
00175 inline complex &operator *= (double x)
00176 {
00177 r *= x;
00178 i *= x;
00179 return *this;
00180 };
00181 inline complex &operator /= (double x)
00182 {
00183 r /= x;
00184 i /= x;
00185 return *this;
00186 };
00187 inline complex &operator ^= (double x)
00188 {
00189 double lm = log(Mag()), a = Arg(), b = exp(x*lm), c = x*a;
00190 r = (b*cos(c));
00191 i = (b*sin(c));
00192 return *this;
00193 };
00194 inline complex &operator += (complex x)
00195 {
00196 r += x.r;
00197 i += x.i;
00198 return *this;
00199 };
00200 inline complex &operator -= (complex x)
00201 {
00202 r -= x.r;
00203 i -= x.i;
00204 return *this;
00205 };
00206 inline complex &operator *= (complex x)
00207 {
00208 double pr=r;
00209 r = pr * x.r - i * x.i;
00210 i = pr * x.i + i * x.r;
00211 return *this;
00212 };
00213 inline complex &operator /= (complex y)
00214 {
00215 double xr=r;
00216 double a = y.r*y.r+y.i*y.i;
00217 r = (xr*y.r+i*y.i)/a;
00218 i = (i*y.r-xr*y.i)/a;
00219 return *this;
00220 };
00221 inline complex &operator ^= (complex x)
00222 {
00223 double lm = log(Mag()), a = Arg(), b = exp(x.r*lm-x.i*a), c = x.r*a+x.i*lm;
00224 r = (b*cos(c));
00225 i = (b*sin(c));
00226 return *this;
00227 };
00228
00229
00230 inline complex operator + (double y)
00231 {
00232 complex x(*this);
00233 return x+=y;
00234 };
00235 inline complex operator - (double y)
00236 {
00237 complex x(*this);
00238 return x-=y;
00239 };
00240 inline complex operator * (double y)
00241 {
00242 complex x(*this);
00243 return x*=y;
00244 };
00245 inline complex operator / (double y)
00246 {
00247 complex x(*this);
00248 return x/=y;
00249 };
00250 inline complex operator ^ (double y)
00251 {
00252 complex x(*this);
00253 return x^=y;
00254 };
00255 inline complex operator + (complex y)
00256 {
00257 complex x(*this);
00258 return x+=y;
00259 };
00260 inline complex operator - (complex y)
00261 {
00262 complex x(*this);
00263 return x-=y;
00264 };
00265 inline complex operator * (complex y)
00266 {
00267 complex x(*this);
00268 return x*=y;
00269 };
00270 inline complex operator / (complex y)
00271 {
00272 complex x(*this);
00273 return x/=y;
00274 };
00275 inline complex operator ^ (complex y)
00276 {
00277 complex x(*this);
00278 return x^=y;
00279 };
00280
00282 inline complex SetPowerFactor(double mag,
00283 double pf,
00284 CNOTATION n=J)
00285 {
00286 SetPolar(mag/pf, acos(pf),n);
00287 return *this;
00288 }
00289
00290
00291
00292 inline bool IsZero(double err=0.0)
00293 {
00294 return Mag()<=err;
00295 }
00296
00297
00298 inline bool operator == (double m) { return Mag()==m; };
00299 inline bool operator != (double m) { return Mag()!=m; };
00300 inline bool operator < (double m) { return Mag()<m; };
00301 inline bool operator <= (double m) { return Mag()<=m; };
00302 inline bool operator > (double m) { return Mag()>m; };
00303 inline bool operator >= (double m) { return Mag()>=m; };
00304
00305
00306 inline complex operator == (complex y) { return fmod(y.Arg()-Arg(),2*PI)==0.0;};
00307 inline complex operator != (complex y) { return fmod(y.Arg()-Arg(),2*PI)!=0.0;};
00308 inline complex operator < (complex y) { return fmod(y.Arg()-Arg(),2*PI)<PI;};
00309 inline complex operator <= (complex y) { return fmod(y.Arg()-Arg(),2*PI)<=PI;};
00310 inline complex operator > (complex y) { return fmod(y.Arg()-Arg(),2*PI)>PI;};
00311 inline complex operator >= (complex y) { return fmod(y.Arg()-Arg(),2*PI)>=PI;};
00312 inline bool IsFinite(void) { return isfinite(r) && isfinite(i); };
00313 };
00314
00315 #ifdef REAL4
00316 typedef float real;
00317 #else
00318 typedef double real;
00319 #endif
00320
00321 typedef int64 TIMESTAMP;
00322 #define TS_TPS ((int64)1)
00323 #define TS_REALNOW ((TIMESTAMP)(time(NULL))*TS_TPS)
00324 #define TS_SIMNOW (*(callback->global_clock))
00325 #define TS_ZERO ((int64)0)
00326 #define Ts_INIT ((int64)3600*12)
00327 #define TS_MAX (32482080000LL)
00328 #define TS_INVALID ((int64)-1)
00329 #define TS_NEVER ((int64)(((unsigned int64)-1)>>1))
00330 #define MINYEAR 1970
00331 #define MAXYEAR 2969
00332
00333 inline TIMESTAMP gl_timestamp(double t) { return (TIMESTAMP)(t*TS_TPS);}
00334 inline double gl_seconds(TIMESTAMP t) { return (double)t/(double)TS_TPS;}
00335 inline double gl_minutes(TIMESTAMP t) { return (double)t/(double)(TS_TPS*60);}
00336 inline double gl_hours(TIMESTAMP t) { return (double)t/(double)(TS_TPS*60*60);}
00337 inline double gl_days(TIMESTAMP t) { return (double)t/(double)(TS_TPS*60*60*24);}
00338 inline double gl_weeks(TIMESTAMP t) { return (double)t/(double)(TS_TPS*60*60*24*7);}
00339
00340 typedef union {
00341 struct {
00342 unsigned int calendar:4;
00343 unsigned int minute:20;
00344 };
00345 unsigned int index;
00346 } SCHEDULEINDEX;
00347
00348 typedef char char1024[1025];
00349 typedef char char256[257];
00350 typedef char char32[33];
00351 typedef char char8[9];
00352 typedef char int8;
00353 typedef short int16;
00354 typedef int int32;
00355 typedef long enumeration;
00356 typedef TIMESTAMP timestamp;
00357 typedef struct s_object_list* object;
00358 typedef unsigned int64 set;
00359 typedef double triplet[3];
00360 typedef complex triplex[3];
00361
00362
00363 typedef enum {_PT_FIRST=-1,
00364 PT_void,
00365 PT_double,
00366 PT_complex,
00367 PT_enumeration,
00368 PT_set,
00369 PT_int16,
00370 PT_int32,
00371 PT_int64,
00372 PT_char8,
00373 PT_char32,
00374 PT_char256,
00375 PT_char1024,
00376 PT_object,
00377 PT_delegated,
00378 PT_bool,
00379 PT_timestamp,
00380 PT_double_array,
00381 PT_complex_array,
00382
00383 PT_float,
00384 PT_real,
00385 PT_loadshape,
00386 PT_enduse,
00387 #ifdef USE_TRIPLETS
00388 PT_triple,
00389 PT_triplex,
00390 #endif
00391 _PT_LAST,
00392
00393 PT_AGGREGATE,
00394 PT_KEYWORD,
00395 PT_ACCESS,
00396 PT_SIZE,
00397 PT_FLAGS,
00398 PT_INHERIT,
00399 PT_UNITS,
00400 PT_DESCRIPTION,
00401 PT_EXTEND,
00402 PT_EXTENDBY,
00403 } PROPERTYTYPE;
00404 typedef char CLASSNAME[64];
00405 typedef unsigned int OBJECTRANK;
00406 typedef unsigned short OBJECTSIZE;
00407 typedef unsigned int OBJECTNUM;
00408 typedef char * OBJECTNAME;
00409 typedef char FUNCTIONNAME[64];
00410 typedef void* PROPERTYADDR;
00411 typedef char PROPERTYNAME[64];
00412 typedef enum {
00413 PA_PUBLIC,
00414 PA_REFERENCE,
00415 PA_PROTECTED,
00416 PA_PRIVATE,
00417 } PROPERTYACCESS;
00418 typedef int64 (*FUNCTIONADDR)(void*,...);
00419 typedef struct s_unit UNIT;
00420 typedef struct s_delegatedtype DELEGATEDTYPE;
00421 typedef struct s_delegatedvalue DELEGATEDVALUE;
00422 typedef DELEGATEDVALUE* delegated;
00423 typedef struct s_object_list OBJECT;
00424 typedef struct s_module_list MODULE;
00425 typedef struct s_class_list CLASS;
00426 typedef struct s_property_map PROPERTY;
00427 typedef struct s_function_map FUNCTION;
00428 typedef struct s_schedule SCHEDULE;
00429 typedef struct s_loadshape_core loadshape;
00430 typedef struct s_enduse enduse;
00431
00432 #define MAXBLOCKS 4
00433 #define MAXVALUES 64
00434
00435
00436 typedef unsigned char PASSCONFIG;
00437 #define PC_NOSYNC 0x00
00438 #define PC_PRETOPDOWN 0x01
00439 #define PC_BOTTOMUP 0x02
00440 #define PC_POSTTOPDOWN 0x04
00441 #define PC_FORCE_NAME 0x20
00442 #define PC_PARENT_OVERRIDE_OMIT 0x40
00443 #define PC_UNSAFE_OVERRIDE_OMIT 0x80
00445 #ifndef FALSE
00446 #define FALSE (0)
00447 #define TRUE (!FALSE)
00448 #endif
00449
00450 typedef enum {FAILED=FALSE, SUCCESS=TRUE} STATUS;
00451
00452 typedef struct s_globalvar {
00453 char name[256];
00454 PROPERTY *prop;
00455 struct s_globalvar *next;
00456 unsigned long flags;
00457 } GLOBALVAR;
00458
00459 typedef enum {
00460 NM_PREUPDATE = 0,
00461 NM_POSTUPDATE = 1,
00462 NM_RESET = 2,
00463 } NOTIFYMODULE;
00465 #include "setjmp.h"
00466 typedef struct s_exception_handler {
00467 int id;
00468 jmp_buf buf;
00469 char msg[1024];
00470 struct s_exception_handler *next;
00471 } EXCEPTIONHANDLER;
00473 typedef struct s_keyword KEYWORD;
00474
00475 typedef unsigned long PROPERTYFLAGS;
00476 #define PF_RECALC 0x0001
00477 #define PF_CHARSET 0x0002
00479 struct s_property_map {
00480 CLASS *oclass;
00481 PROPERTYNAME name;
00482 PROPERTYTYPE ptype;
00483 unsigned long size;
00484 PROPERTYACCESS access;
00485 UNIT *unit;
00486 PROPERTYADDR addr;
00487 DELEGATEDTYPE *delegation;
00488 KEYWORD *keywords;
00489 char *description;
00490 PROPERTY *next;
00491 PROPERTYFLAGS flags;
00492 };
00494 struct s_unit {
00495 char name[64];
00496 double c,e,h,k,m,s,a,b;
00497 int prec;
00498 UNIT *next;
00499 };
00500 struct s_delegatedtype {
00501 char32 type;
00502 CLASS *oclass;
00503 int (*from_string)(void *addr, char *value);
00504 int (*to_string)(void *addr, char *value, int size);
00505 };
00506 struct s_delegatedvalue {
00507 char *data;
00508 DELEGATEDTYPE *type;
00509 };
00510 struct s_keyword {
00511 char name[32];
00512 unsigned long value;
00513 struct s_keyword *next;
00514 };
00515 struct s_module_list {
00516 void *hLib;
00517 char name[1024];
00518 CLASS *oclass;
00519 unsigned short major;
00520 unsigned short minor;
00521 void* (*getvar)(char *varname,char *value,unsigned int size);
00522 int (*setvar)(char *varname,char *value);
00523 int (*import_file)(char *file);
00524 int (*export_file)(char *file);
00525 int (*check)();
00526 #ifndef _NO_CPPUNIT
00527 int (*module_test)(void *callbacks,int argc,char* argv[]);
00528 #endif
00529 int (*cmdargs)(int,char**);
00530 int (*kmldump)(FILE*fp,OBJECT*);
00531 void (*test)(int argc, char *argv[]);
00532 MODULE *(*subload)(char *, MODULE **, CLASS **, int, char **);
00533 PROPERTY *globals;
00534 MODULE *next;
00535 };
00536
00537 typedef enum {CLASSVALID=0xc44d822e} CLASSMAGIC;
00538
00539 struct s_class_list {
00540 CLASSMAGIC magic;
00541 CLASSNAME name;
00542 unsigned int size;
00543 MODULE *module;
00544 PROPERTY *pmap;
00545 FUNCTION *fmap;
00546 FUNCTIONADDR create;
00547 FUNCTIONADDR init;
00548 FUNCTIONADDR sync;
00549 FUNCTIONADDR commit;
00550 FUNCTIONADDR notify;
00551 FUNCTIONADDR isa;
00552 FUNCTIONADDR plc;
00553 PASSCONFIG passconfig;
00554 FUNCTIONADDR recalc;
00555 CLASS *parent;
00556 struct {
00557 int32 numobjs;
00558 int64 clocks;
00559 int32 count;
00560 } profiler;
00561 CLASS *next;
00562 };
00563
00564 typedef char FULLNAME[1024];
00565 typedef struct s_namespace {
00566 FULLNAME name;
00567 struct s_namespace *next;
00568 } NAMESPACE;
00569
00570 struct s_object_list {
00571 OBJECTNUM id;
00572 char32 groupid;
00573 CLASS *oclass;
00574 OBJECT *next;
00575 OBJECT *parent;
00576 OBJECTRANK rank;
00577 TIMESTAMP clock;
00578 TIMESTAMP valid_to;
00579 double latitude, longitude;
00580 TIMESTAMP in_svc,
00581 out_svc;
00582 OBJECTNAME name;
00583 int tp_affinity;
00584 NAMESPACE *space;
00585 unsigned long flags;
00586
00587 };
00589 struct s_function_map {
00590 CLASS *oclass;
00591 FUNCTIONNAME name;
00592 FUNCTIONADDR addr;
00593 FUNCTION *next;
00594 };
00595
00596 typedef struct s_datetime {
00597 unsigned short year;
00598 unsigned short month;
00599 unsigned short day;
00600 unsigned short hour;
00601 unsigned short minute;
00602 unsigned short second;
00603 unsigned int microsecond;
00604 unsigned short is_dst;
00605 char tz[5];
00606 unsigned short weekday;
00607 unsigned short yearday;
00608 TIMESTAMP timestamp;
00609 } DATETIME;
00610
00611 struct s_schedule {
00612 char name[64];
00613 char definition[65536];
00614 char blockname[MAXBLOCKS][64];
00615 unsigned char block;
00616 unsigned char index[14][366*24*60];
00617 unsigned long dtnext[14][366*24*60];
00618 double data[MAXBLOCKS*MAXVALUES];
00619 unsigned int weight[MAXBLOCKS*MAXVALUES];
00620 double sum[MAXBLOCKS];
00621 double abs[MAXBLOCKS];
00622 unsigned int count[MAXBLOCKS];
00623 unsigned int minutes[MAXBLOCKS];
00624 TIMESTAMP next_t;
00625 double value;
00626 double duration;
00627 int flags;
00628 SCHEDULE *next;
00629 };
00630
00631 typedef enum {
00632 MT_UNKNOWN=0,
00633 MT_ANALOG,
00634 MT_PULSED,
00635 MT_MODULATED,
00636 MT_QUEUED,
00637 } MACHINETYPE;
00638 typedef enum {
00639 MPT_UNKNOWN=0,
00640 MPT_TIME,
00641 MPT_POWER,
00642 } MACHINEPULSETYPE;
00644 struct s_loadshape_core {
00645 double load;
00647
00648 SCHEDULE *schedule;
00649 MACHINETYPE type;
00650 union {
00651 struct {
00652 double energy;
00653 } analog;
00654 struct {
00655 double energy;
00656 double scalar;
00657 MACHINEPULSETYPE pulsetype;
00658 double pulsevalue;
00659 } pulsed;
00660 struct {
00661 double energy;
00662 double scalar;
00663 MACHINEPULSETYPE pulsetype;
00664 double pulsevalue;
00665 } modulated;
00666 struct {
00667 double energy;
00668 double scalar;
00669 MACHINEPULSETYPE pulsetype;
00670 double pulsevalue;
00671 double q_on, q_off;
00672 } queued;
00673 } params;
00675
00676 double r;
00677 double re[2];
00678 double d[2];
00679 double de[2];
00680 double dPdV;
00682
00683 double q;
00684 unsigned int s;
00685 TIMESTAMP t0;
00686 TIMESTAMP t2;
00688 loadshape *next;
00689 };
00690
00691 struct s_enduse {
00692 loadshape *shape;
00693 complex power;
00694 complex energy;
00695 complex demand;
00696 double impedance_fraction;
00697 double current_fraction;
00698 double power_fraction;
00699 double power_factor;
00700 double voltage_factor;
00701 double heatgain;
00702 double heatgain_fraction;
00703
00704 enduse *next;
00705 };
00706
00707 typedef enum {
00708 RT_INVALID=-1,
00709 RT_DEGENERATE,
00710 RT_UNIFORM,
00711 RT_NORMAL,
00712 RT_LOGNORMAL,
00713 RT_BERNOULLI,
00714 RT_PARETO,
00715 RT_EXPONENTIAL,
00716 RT_SAMPLED,
00717 RT_RAYLEIGH,
00718 RT_WEIBULL,
00719 } RANDOMTYPE;
00720
00721 typedef struct s_callbacks {
00722 TIMESTAMP *global_clock;
00723 int (*output_verbose)(char *format, ...);
00724 int (*output_message)(char *format, ...);
00725 int (*output_warning)(char *format, ...);
00726 int (*output_error)(char *format, ...);
00727 int (*output_debug)(char *format, ...);
00728 int (*output_test)(char *format, ...);
00729 CLASS *(*register_class)(MODULE *,CLASSNAME,unsigned int,PASSCONFIG);
00730 struct {
00731 OBJECT *(*single)(CLASS*);
00732 OBJECT *(*array)(CLASS*,unsigned int);
00733 OBJECT *(*foreign)(OBJECT *obj);
00734 } create;
00735 int (*define_map)(CLASS*,...);
00736 CLASS *(*class_getname)(char*);
00737 struct {
00738 FUNCTION *(*define)(CLASS*,FUNCTIONNAME,FUNCTIONADDR);
00739 FUNCTIONADDR (*get)(char*,char*);
00740 } function;
00741 int (*define_enumeration_member)(CLASS*,char*,char*,enumeration);
00742 int (*define_set_member)(CLASS*,char*,char*,unsigned long);
00743 int (*set_dependent)(OBJECT*,OBJECT*);
00744 int (*set_parent)(OBJECT*,OBJECT*);
00745 int (*set_rank)(OBJECT*,unsigned int);
00746 struct {
00747 PROPERTY *(*get_property)(OBJECT*,PROPERTYNAME);
00748 int (*set_value_by_addr)(OBJECT *, void*, char*,PROPERTY*);
00749 int (*get_value_by_addr)(OBJECT *, void*, char*, int size,PROPERTY*);
00750 int (*set_value_by_name)(OBJECT *, char*, char*);
00751 int (*get_value_by_name)(OBJECT *, char*, char*, int size);
00752 OBJECT *(*get_reference)(OBJECT *, char*);
00753 char *(*get_unit)(OBJECT *, char *);
00754 void *(*get_addr)(OBJECT *, char *);
00755 int (*set_value_by_type)(PROPERTYTYPE,void *data,char *);
00756 } properties;
00757 struct {
00758 struct s_findlist *(*objects)(struct s_findlist *,...);
00759 OBJECT *(*next)(struct s_findlist *,OBJECT *obj);
00760 struct s_findlist *(*copy)(struct s_findlist *);
00761 void (*add)(struct s_findlist*, OBJECT*);
00762 void (*del)(struct s_findlist*, OBJECT*);
00763 void (*clear)(struct s_findlist*);
00764 } find;
00765 PROPERTY *(*find_property)(CLASS *, PROPERTYNAME);
00766 void *(*malloc)(size_t);
00767 void (*free)(void*);
00768 struct s_aggregate *(*create_aggregate)(char *aggregator, char *group_expression);
00769 double (*run_aggregate)(struct s_aggregate *aggregate);
00770 double *(*get_module_var)(MODULE *module, char *varname);
00771 int (*depends)(char *name, unsigned char major, unsigned char minor, unsigned short build);
00772 struct {
00773 double (*uniform)(double a, double b);
00774 double (*normal)(double m, double s);
00775 double (*bernoulli)(double p);
00776 double (*pareto)(double m, double a);
00777 double (*lognormal)(double m, double s);
00778 double (*sampled)(unsigned int n, double *x);
00779 double (*exponential)(double l);
00780 RANDOMTYPE (*type)(char *name);
00781 double (*value)(RANDOMTYPE type, ...);
00782 double (*pseudo)(RANDOMTYPE type, unsigned int *state, ...);
00783 double (*triangle)(double a, double b);
00784 double (*beta)(double a, double b);
00785 double (*gamma)(double a);
00786 double (*weibull)(double a, double b);
00787 double (*rayleigh)(double a);
00788 } random;
00789 int (*object_isa)(OBJECT *obj, char *type);
00790 DELEGATEDTYPE* (*register_type)(CLASS *oclass, char *type,int (*from_string)(void*,char*),int (*to_string)(void*,char*,int));
00791 int (*define_type)(CLASS*,DELEGATEDTYPE*,...);
00792 struct {
00793 TIMESTAMP (*mkdatetime)(DATETIME *dt);
00794 int (*strdatetime)(DATETIME *t, char *buffer, int size);
00795 double (*timestamp_to_days)(TIMESTAMP t);
00796 double (*timestamp_to_hours)(TIMESTAMP t);
00797 double (*timestamp_to_minutes)(TIMESTAMP t);
00798 double (*timestamp_to_seconds)(TIMESTAMP t);
00799 int (*local_datetime)(TIMESTAMP ts, DATETIME *dt);
00800 TIMESTAMP (*convert_to_timestamp)(char *value);
00801 int (*convert_from_timestamp)(TIMESTAMP ts, char *buffer, int size);
00802 } time;
00803 int (*unit_convert)(char *from, char *to, double *value);
00804 int (*unit_convert_ex)(UNIT *pFrom, UNIT *pTo, double *pValue);
00805 UNIT *(*unit_find)(char *unit_name);
00806 struct {
00807 EXCEPTIONHANDLER *(*create_exception_handler)();
00808 void (*delete_exception_handler)(EXCEPTIONHANDLER *ptr);
00809 void (*throw_exception)(char *msg, ...);
00810 char *(*exception_msg)(void);
00811 } exception;
00812 struct {
00813 GLOBALVAR *(*create)(char *name, ...);
00814 STATUS (*setvar)(char *def,...);
00815 char *(*getvar)(char *name, char *buffer, int size);
00816 GLOBALVAR *(*find)(char *name);
00817 } global;
00818 #ifndef NOLOCKS
00819 int64 *lock_count, *lock_spin;
00820 #endif
00821 struct {
00822 char *(*find_file)(char *name, char *path, int mode);
00823 } file;
00824 struct s_objvar_struct {
00825 complex *(*complex_var)(OBJECT *obj, PROPERTY *prop);
00826 enumeration *(*enum_var)(OBJECT *obj, PROPERTY *prop);
00827 int16 *(*int16_var)(OBJECT *obj, PROPERTY *prop);
00828 int32 *(*int32_var)(OBJECT *obj, PROPERTY *prop);
00829 int64 *(*int64_var)(OBJECT *obj, PROPERTY *prop);
00830 double *(*double_var)(OBJECT *obj, PROPERTY *prop);
00831 char *(*string_var)(OBJECT *obj, PROPERTY *prop);
00832 OBJECT *(*object_var)(OBJECT *obj, PROPERTY *prop);
00833 } objvar;
00834 struct s_objvar_name_struct {
00835 complex *(*complex_var)(OBJECT *obj, char *name);
00836 enumeration *(*enum_var)(OBJECT *obj, char *name);
00837 int16 *(*int16_var)(OBJECT *obj, char *name);
00838 int32 *(*int32_var)(OBJECT *obj, char *name);
00839 int64 *(*int64_var)(OBJECT *obj, char *name);
00840 double *(*double_var)(OBJECT *obj, char *name);
00841 char *(*string_var)(OBJECT *obj, char *name);
00842 } objvarname;
00843 struct {
00844 int (*string_to_property)(PROPERTY *prop, void *addr, char *value);
00845 int (*property_to_string)(PROPERTY *prop, void *addr, char *value, int size);
00846 } convert;
00847 MODULE *(*module_find)(char *name);
00848 OBJECT *(*get_object)(char *name);
00849 int (*name_object)(OBJECT *obj, char *buffer, int len);
00850 int (*get_oflags)(KEYWORD **extflags);
00851 unsigned int (*object_count)(void);
00852 struct {
00853 SCHEDULE *(*create)(char *name, char *definition);
00854 SCHEDULEINDEX (*index)(SCHEDULE *sch, TIMESTAMP ts);
00855 double (*value)(SCHEDULE *sch, SCHEDULEINDEX index);
00856 long (*dtnext)(SCHEDULE *sch, SCHEDULEINDEX index);
00857 SCHEDULE *(*find)(char *name);
00858 } schedule;
00859 struct {
00860 int (*create)(loadshape *s);
00861 int (*init)(loadshape *s);
00862 } loadshape;
00863 struct {
00864 int (*create)(struct s_enduse *e);
00865 TIMESTAMP (*sync)(enduse *e, PASSCONFIG pass, TIMESTAMP t0, TIMESTAMP t1);
00866 } enduse;
00867 struct {
00868 double (*linear)(double t, double x0, double y0, double x1, double y1);
00869 double (*quadratic)(double t, double x0, double y0, double x1, double y1, double x2, double y2);
00870 } interpolate;
00871 } CALLBACKS;
00873 extern CALLBACKS *callback;
00874
00875 typedef FUNCTIONADDR function;
00876
00877 #define gl_verbose (*callback->output_verbose)
00878 #define gl_output (*callback->output_message)
00879 #define gl_warning (*callback->output_warning)
00880 #define gl_error (*callback->output_error)
00881 #ifdef _DEBUG
00882 #define gl_debug (*callback->output_debug)
00883 #else
00884 #define gl_debug
00885 #endif
00886 #define gl_testmsg (*callback->output_test)
00887
00888 #define gl_globalclock (*(callback->global_clock))
00889
00892 inline char* gl_name(OBJECT *my)
00893 {
00894 static char buffer[1024];
00895 if (my->name==NULL)
00896 sprintf(buffer,"%s:%d", my->oclass->name, my->id);
00897 else
00898 sprintf(buffer,"%s", my->name);
00899 return buffer;
00900 }
00901
00904 inline void gl_throw(char *msg, ...)
00905 {
00906 va_list ptr;
00907 va_start(ptr,msg);
00908 static char buffer[1024];
00909 vsprintf(buffer,msg,ptr);
00910 throw buffer;
00911 va_end(ptr);
00912 }
00913
00916 inline char *gl_global_getvar(char *name)
00917 {
00918 return callback->global.getvar(name,NULL,0);
00919 }
00920
00923 inline OBJECT *gl_create_object(CLASS *oclass)
00924 {
00925 return (*callback->create.single)(oclass);
00926 }
00927
00928 inline OBJECT *gl_create_foreign(OBJECT *obj) {return (*callback->create.foreign)(obj);};
00929
00932 inline int gl_set_parent(OBJECT *obj,
00933 OBJECT *parent)
00934 {
00935 return (*callback->set_parent)(obj,parent);
00936 }
00937
00940 inline int gl_set_rank(OBJECT* obj,
00941 unsigned int rank)
00942 {
00943 return (*callback->set_rank)(obj,rank);
00944 }
00945
00948 inline void *gl_get_addr(OBJECT *obj,
00949 char *name)
00950 {
00951 return callback->properties.get_addr(obj,name);
00952 }
00953
00956 template <class T> inline void gl_get_value(OBJECT *obj,
00957 char *propname,
00958 T &value)
00959 {
00960 T *ptr = (T*)gl_get_addr(obj,propname);
00961
00962 if (ptr==NULL)
00963 gl_throw("property %s not found in object %s", propname, gl_name(obj));
00964 value = *ptr;
00965 }
00966
00969 template <class T> inline void gl_set_value(OBJECT *obj,
00970 char *propname,
00971 T &value)
00972 {
00973 T *ptr = (T*)gl_get_addr(obj,propname);
00974
00975 if (ptr==NULL)
00976 gl_throw("property %s not found in object %s", propname, gl_name(obj));
00977 *ptr = value;
00978 }
00979
00980 inline FUNCTIONADDR gl_get_function(OBJECT *obj, char *fname) { return callback->function.get(obj->oclass->name,fname);};
00981 inline FUNCTIONADDR gl_get_function(CLASS *oclass, char *fname) { return callback->function.get(oclass->name,fname);};
00982 inline FUNCTIONADDR gl_get_function(char *classname, char *fname) { return callback->function.get(classname,fname);};
00983
00984 inline double gl_random_uniform(const double lo, const double hi) { return callback->random.uniform(lo,hi);};
00985 inline double gl_random_normal(const double mu, const double sigma) { return callback->random.normal(mu,sigma);};
00986 inline double gl_random_lognormal(const double gmu, const double gsigma) { return callback->random.lognormal(gmu,gsigma);};
00987 inline double gl_random_exponential(const double lambda) { return callback->random.exponential(lambda);};
00988 inline double gl_random_pareto(const double m, const double k) { return callback->random.pareto(m,k);};
00989 inline double gl_random_bernoulli(double p) { return callback->random.bernoulli(p);};
00990 inline double gl_random_sampled(unsigned int n, double *x) { return callback->random.sampled(n,x);};
00991 inline double gl_random_triangle(double a, double b) { return callback->random.triangle(a,b);};
00992 inline double gl_random_beta(double a, double b) { return callback->random.beta(a,b);};
00993 inline double gl_random_gamma(double a) { return callback->random.gamma(a);};
00994 inline double gl_random_weibull(double a, double b) { return callback->random.weibull(a,b);};
00995 inline double gl_random_rayleigh(double a) { return callback->random.rayleigh(a);};
00996
00997 inline bool gl_object_isa(OBJECT *obj, char *type) { return callback->object_isa(obj,type)==1;};
00998 inline DATETIME *gl_localtime(TIMESTAMP ts,DATETIME *dt) { return callback->time.local_datetime(ts,dt)?dt:NULL;};
00999 inline TIMESTAMP gl_mkdatetime(DATETIME *dt) { return callback->time.mkdatetime(dt);};
01000 inline TIMESTAMP gl_mkdatetime(short year, short month, short day, short hour=0, short minute=0, short second=0, char *tz=NULL, short usec=0)
01001 {
01002 DATETIME dt;
01003 dt.year = year;
01004 dt.month = month;
01005 dt.day = day;
01006 dt.hour = hour;
01007 dt.minute = minute;
01008 dt.second = second;
01009 dt.microsecond = usec;
01010 strncpy(dt.tz,tz,sizeof(dt.tz));
01011 return callback->time.mkdatetime(&dt);
01012 };
01013
01014 inline int gl_unit_convert(char *from, char *to, double &value) { return callback->unit_convert(from, to, &value);};
01015 inline int gl_unit_convert(UNIT *from, UNIT *to, double &value) { return callback->unit_convert_ex(from, to, &value);};
01016 inline UNIT *gl_unit_find(char *name) { return callback->unit_find(name);};
01017 inline char *gl_find_file(char *name, char *path, int mode) { return callback->file.find_file(name,path,mode); };
01018
01019 #define gl_printtime (*callback->time.convert_from_timestamp)
01020
01021 inline char *gl_strftime(DATETIME *dt, char *buffer, int size) { return callback->time.strdatetime(dt,buffer,size)?buffer:NULL;};
01022 inline char *gl_strftime(TIMESTAMP ts)
01023 {
01024 static char buffer[64];
01025 strcpy(buffer,"(invalid time)");
01026 DATETIME dt;
01027 gl_localtime(ts,&dt);
01028 gl_strftime(&dt,buffer,sizeof(buffer));
01029 return buffer;
01030 }
01031
01032 inline void trace(char *fn, OBJECT *obj)
01033 {
01034 callback->output_message("TRACE: %s(%s[%s:%d])", fn, obj->name?obj->name:"<unnamed>", obj->oclass->name,obj->id);
01035 }
01036
01037 inline void *gl_malloc(size_t size) { return callback->malloc(size);};
01038 inline void gl_free(void *ptr) { return callback->free(ptr);};
01039
01040 #define gl_find_objects (*callback->find.object)
01041 inline OBJECT *gl_find_next(struct s_findlist *list,OBJECT *obj) { return callback->find.next(list,obj);};
01042 inline struct s_findlist *gl_find_copy(struct s_findlist *list) { return callback->find.copy(list);};
01043 inline void gl_find_add(struct s_findlist *list, OBJECT *obj) { callback->find.add(list,obj);};
01044 inline void gl_find_del(struct s_findlist *list, OBJECT *obj) { callback->find.del(list,obj);};
01045 inline void gl_find_clear(struct s_findlist *list) { callback->find.clear(list);};
01046
01047 inline SCHEDULE *gl_schedule_create(char *name, char *definition)
01048 {
01049 return callback->schedule.create(name,definition);
01050 }
01051
01052 inline SCHEDULEINDEX gl_schedule_index(SCHEDULE *sch, TIMESTAMP ts)
01053 {
01054 return callback->schedule.index(sch,ts);
01055 }
01056
01057 inline double gl_schedule_value(SCHEDULE *sch, SCHEDULEINDEX index)
01058 {
01059 return callback->schedule.value(sch,index);
01060 }
01061
01062 inline long gl_schedule_dtnext(SCHEDULE *sch, SCHEDULEINDEX index)
01063 {
01064 return callback->schedule.dtnext(sch,index);
01065 }
01066
01067 inline TIMESTAMP gl_enduse_sync(enduse *e, TIMESTAMP t1)
01068 {
01069 return callback->enduse.sync(e,PC_BOTTOMUP,*(callback->global_clock),t1);
01070 }
01071
01072