Skip to content

Commit

Permalink
- Added the general utility function unique (removes duplicate elemen…
Browse files Browse the repository at this point in the history
…ts from a sorted C array).

- Added a stub for initSample with some code that could be used to generate all the times when a call to sample() generates an event.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5516 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed May 12, 2010
1 parent ebc025d commit e5504c5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion c_runtime/simulation_delay.cpp
Expand Up @@ -151,7 +151,7 @@ void storeDelayedExpression(int exprNumber, double exprValue)
int index = delayStruct->currentIndex;
if (index >= delayStruct->maxExpressionBuffer) {
// it doesn't fit anymore, we need to re-alloc:
delayStruct->maxExpressionBuffer += 500;
delayStruct->maxExpressionBuffer *= 2;
delayStruct->expressionDelayBuffer = (t_TimeAndValue*)realloc(
delayStruct->expressionDelayBuffer,
delayStruct->maxExpressionBuffer * sizeof(t_TimeAndValue));
Expand Down
48 changes: 47 additions & 1 deletion c_runtime/simulation_events.cpp
Expand Up @@ -350,7 +350,7 @@ double sample(double start, double interval) {
* below should be: if (tmp >= -0.0001 && tmp < 0.0001) but needs more testing as some models from
* testsuite fail.
*/
static double eps = 0.0001;
static const double eps = 0.0001;
/*
* sjoelund - do not sample before the start value !
*/
Expand All @@ -369,6 +369,52 @@ double sample(double start, double interval) {
}
}

void initSample(double start) {
if (sim_verbose)
printf("Calculating time of sample events is not performed yet\n");

/* This code will generate an array of time values when sample generates events.
* The only problem is our backend does not generate this array.
* Sample() and sample() also need to be changed, but this should be easy to fix. */

/*
int i;
double stop = 1.0;
double d;
double samples[][2] = {
{0.0,0.1},
{0.3,0.3},
{0.5,0.2},
{0.0,0.15},
};
double* events;
int num_samples = sizeof(samples)/(2*sizeof(double));
int max_events = 0;
int ix = 0;
int nuniq;
for (i=0; i<num_samples; i++) {
if (stop >= samples[i][0])
max_events += (stop - samples[i][0])/samples[i][1]+1;
}
events = malloc(max_events * sizeof(double));
for (i=0; i<num_samples; i++) {
for (d=samples[i][0]; ix<max_events && d<stop; d+= samples[i][1]) {
events[ix++] = d;
}
}
assert(ix == max_events);
qsort(events,max_events,sizeof(double),compdbl);
nuniq = unique(events,max_events,sizeof(double),compdbl);
printf("Sorted, unique events\n");
for (i=0; i<nuniq; i++) {
printf("%d %f\n", i, events[i]);
}
free(events);
return 0;
*/
}

void saveall() {
int i;
for (i = 0; i < globalData->nStates; i++) {
Expand Down
1 change: 1 addition & 0 deletions c_runtime/simulation_events.h
Expand Up @@ -64,6 +64,7 @@ bool change(double& var);

double Sample(double t, double start ,double interval);
double sample(double start ,double interval);
void initSample(double start);

double Less(double a,double b);
double LessEq(double a,double b);
Expand Down
1 change: 1 addition & 0 deletions c_runtime/simulation_runtime.cpp
Expand Up @@ -247,6 +247,7 @@ int startNonInteractiveSimulation(int argc, char**argv){
globalData->lastEmittedTime = start;
globalData->forceEmit=0;

initSample(start);
initDelay(start);

if (measure_time_flag)
Expand Down
28 changes: 28 additions & 0 deletions c_runtime/utility.c
Expand Up @@ -65,3 +65,31 @@ modelica_real rem(modelica_real x, modelica_real y)
return fmod(x, y);
}

int compdbl(const void* a, const void* b) {
const double *v1 = (const double *) a;
const double *v2 = (const double *) b;
const double diff = *v1 - *v2;
const double epsilon = 0.00000000000001;

if (diff < epsilon && diff > -epsilon)
return 0;
return (*v1 > *v2) - (*v1 < *v2);
}

int unique(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)) {
size_t nuniq = 0;
size_t i,j;
void *a, *b, *c;
a = base;
for (i=1; i<nmemb; i++) {
b = ((char*)base)+i*size;
if (0 == compar(a,b)) {
nuniq++;
} else {
a = b;
c = ((char*)base)+(i-nuniq)*size;
memcpy(c, b, size);
}
}
return nmemb-nuniq;
}
5 changes: 5 additions & 0 deletions c_runtime/utility.h
Expand Up @@ -47,4 +47,9 @@ modelica_real modelica_div(modelica_real x, modelica_real y);
modelica_real modelica_mod(modelica_real x, modelica_real y);
modelica_real rem(modelica_real x, modelica_real y);

/* Compatible with C-standard qsort and unique */
int compdbl(const void* a, const void* b);
/* Input is a sorted array with nmemb elements. Returns the number of elements after removing unique elements */
int unique(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));

#endif

0 comments on commit e5504c5

Please sign in to comment.