Skip to content

Commit

Permalink
- simulation_delay.cpp -> c
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10406 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Nov 10, 2011
1 parent 6aa3db7 commit 242bfaf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
4 changes: 2 additions & 2 deletions SimulationRuntime/c/simulation/math-support/CMakeLists.txt
Expand Up @@ -5,8 +5,8 @@
SET(math_support_sources bigden.c enorm.c hybrd1.c nelmead.c qform.c r1updt.c
biglag.c dogleg.c fdjac1.c hybrj.c newuoa.c qrfac.c trsapp.c
dpmpar.c hybrd.c lsame.c newuob.c r1mpyq.c update.c
simulation_events.cpp simulation_delay.cpp
simulation_init.cpp dgesv_aux.c ringbuffer.c )
simulation_events.cpp simulation_delay.c
simulation_init.c dgesv_aux.c ringbuffer.c )

SET(math_support_headers blaswrap.h matrix.h ringbuffer.h simulation_delay.h
simulation_events.h simulation_init.h)
Expand Down
Expand Up @@ -32,9 +32,8 @@
#include "ringbuffer.h"

#include "assert.h"
#include "string.h"
#include <stdio.h>
#include <string>


double tStart = 0;

Expand All @@ -51,21 +50,21 @@ typedef struct _ExpressionDelayBuffer
t_TimeAndValue *expressionDelayBuffer;
} t_ExpressionDelayBuffer;

// the delayStructure looks like a matrix (rows = expressionNumber+currentColumnIndex, columns={time, value})
/*the delayStructure looks like a matrix (rows = expressionNumber+currentColumnIndex, columns={time, value})*/
RINGBUFFER **delayStructure;
extern "C" {
extern const int numDelayExpressionIndex;
}

extern const int numDelayExpressionIndex;

void initDelay(double startTime)
{
int i;

// get the start time of the simulation: time.start.
/* get the start time of the simulation: time.start.*/
tStart = startTime;

// allocate the memory for rows
delayStructure = new RINGBUFFER*[numDelayExpressionIndex];
/* allocate the memory for rows*/
delayStructure = (RINGBUFFER**) calloc(numDelayExpressionIndex,sizeof( RINGBUFFER));
assert(delayStructure);

for(i=0; i<numDelayExpressionIndex; i++)
{
Expand Down Expand Up @@ -123,20 +122,20 @@ void storeDelayedExpression(int exprNumber, double exprValue)

if(exprNumber < 0)
{
// fprintf(stderr, "storeDelayedExpression: Invalid expression number %d.\n", exprNumber);
/* fprintf(stderr, "storeDelayedExpression: Invalid expression number %d.\n", exprNumber);*/
return;
}


if(time < tStart)
{
// fprintf(stderr, "storeDelayedExpression: Time is smaller than starting time. Value ignored.\n");
/* fprintf(stderr, "storeDelayedExpression: Time is smaller than starting time. Value ignored.\n");*/
return;
}

// fprintf(stderr, "storeDelayed[%d] %g:%g\n", exprNumber, time, exprValue);
/* fprintf(stderr, "storeDelayed[%d] %g:%g\n", exprNumber, time, exprValue);*/

// Allocate more space for expressions
/* Allocate more space for expressions*/
assert(exprNumber < numDelayExpressionIndex);

tpl.time = time;
Expand All @@ -147,61 +146,64 @@ void storeDelayedExpression(int exprNumber, double exprValue)
double delayImpl(int exprNumber, double exprValue, double time, double delayTime, double delayMax /* Unused */)
{
RINGBUFFER* delayStruct = delayStructure[exprNumber];
int length = 0;

// fprintf(stderr, "delayImpl: exprNumber = %d, exprValue = %lf, time = %lf, delayTime = %lf\n", exprNumber, exprValue, time, delayTime);
/* fprintf(stderr, "delayImpl: exprNumber = %d, exprValue = %lf, time = %lf, delayTime = %lf\n", exprNumber, exprValue, time, delayTime); */

// Check for errors
/* Check for errors*/
assert(0 <= exprNumber);
assert(exprNumber < numDelayExpressionIndex);

if(time <= tStart)
{
// fprintf(stderr, "delayImpl: Entered at time < starting time: %g.\n", exprValue);
/* fprintf(stderr, "delayImpl: Entered at time < starting time: %g.\n", exprValue);*/
return exprValue;
}

if(delayTime < 0.0)
{
throw TerminateSimulationException(globalData->timeValue,
std::string("Negative delay requested.\n"));
assert(delayTime > 0.0);
/* throw TerminateSimulationException(globalData->timeValue,
std::string("Negative delay requested.\n")); */
}

int length = ringBufferLength(delayStruct);
length = ringBufferLength(delayStruct);

if(length == 0)
{
// This occurs in the initialization phase
// fprintf(stderr, "delayImpl: Missing initial value, using argument value %g instead.\n", exprValue);
/* This occurs in the initialization phase*/
/* fprintf(stderr, "delayImpl: Missing initial value, using argument value %g instead.\n", exprValue);*/
return exprValue;
}

// Returns: expr(time?delayTime) for time>time.start + delayTime and
// expr(time.start) for time <= time.start + delayTime.
// The arguments, i.e., expr, delayTime and delayMax, need to be subtypes of Real.
// DelayMax needs to be additionally a parameter expression.
// The following relation shall hold: 0 <= delayTime <= delayMax,
// otherwise an error occurs. If delayMax is not supplied in the argument list,
// delayTime need to be a parameter expression. See also Section 3.7.2.1.
// For non-scalar arguments the function is vectorized according to Section 10.6.12.
/* Returns: expr(time?delayTime) for time>time.start + delayTime and
expr(time.start) for time <= time.start + delayTime.
The arguments, i.e., expr, delayTime and delayMax, need to be subtypes of Real.
DelayMax needs to be additionally a parameter expression.
The following relation shall hold: 0 <= delayTime <= delayMax,
otherwise an error occurs. If delayMax is not supplied in the argument list,
delayTime need to be a parameter expression. See also Section 3.7.2.1.
For non-scalar arguments the function is vectorized according to Section 10.6.12.*/
if(time <= tStart + delayTime)
{
double res = ((t_TimeAndValue*)getRingData(delayStruct, 0))->value;
// fprintf(stderr, "findTime: time <= tStart + delayTime: [%d] = %g\n",exprNumber,res);
/* fprintf(stderr, "findTime: time <= tStart + delayTime: [%d] = %g\n",exprNumber,res);*/
return res;
}
else
{
// return expr(time-delayTime)
assert(delayTime >= 0.0);
/* return expr(time-delayTime)*/
double timeStamp = time - delayTime;
double time0, time1, value0, value1;

int i;

assert(delayTime >= 0.0);

// find the row for the lower limit
if(timeStamp > ((t_TimeAndValue*)getRingData(delayStruct, length - 1))->time)
{
// delay between the last accepted time step and the current time
/* delay between the last accepted time step and the current time*/
time0 = ((t_TimeAndValue*)getRingData(delayStruct, length - 1))->time;
value0 = ((t_TimeAndValue*)getRingData(delayStruct, length - 1))->value;
time1 = time;
Expand All @@ -214,7 +216,7 @@ double delayImpl(int exprNumber, double exprValue, double time, double delayTime
time0 = ((t_TimeAndValue*)getRingData(delayStruct, i))->time;
value0 = ((t_TimeAndValue*)getRingData(delayStruct, i))->value;

// Was it the last value?
/* Was it the last value?*/
if(i+1 == length)
{
if(i>0 && delayMax == delayTime)
Expand All @@ -226,10 +228,10 @@ double delayImpl(int exprNumber, double exprValue, double time, double delayTime
if(i>0 && delayMax == delayTime)
dequeueNFirstRingDatas(delayStruct, i-1);
}
// was it an exact match?
/* was it an exact match?*/
if(time0 == timeStamp)
{
//fprintf(stderr, "delayImpl: Exact match at %lf\n", currentTime);
/*fprintf(stderr, "delayImpl: Exact match at %lf\n", currentTime);*/
return value0;
}
else if(time1 == timeStamp)
Expand All @@ -238,9 +240,9 @@ double delayImpl(int exprNumber, double exprValue, double time, double delayTime
}
else
{
//fprintf(stderr, "delayImpl: Linear interpolation of %lf between %lf and %lf\n", timeStamp, time0, time1);
/*fprintf(stderr, "delayImpl: Linear interpolation of %lf between %lf and %lf\n", timeStamp, time0, time1);*/

// linear interpolation
/* linear interpolation*/
double timedif = time1 - time0;
double dt0 = time1 - timeStamp;
double dt1 = timeStamp - time0;
Expand Down

0 comments on commit 242bfaf

Please sign in to comment.