Skip to content

Commit

Permalink
- last 1.4.2 commit
Browse files Browse the repository at this point in the history
  + DrModelica, UG, ModelQueryAPIexamples.onb updates
  + val(var_name, time) implementation from Anders Sandholm


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2511 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Sep 2, 2006
1 parent 1bdf550 commit 097dbbe
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 4 deletions.
33 changes: 29 additions & 4 deletions Compiler/Ceval.mo
Expand Up @@ -2025,13 +2025,38 @@ algorithm
then
(cache,Values.STRING("Unknown error while plotting"),st);

case (cache,env,Exp.CALL(path = Absyn.IDENT(name = "val"),expLst = {Exp.ARRAY(array = vars)}),(st as Interactive.SYMBOLTABLE(ast = p,explodedAst = sp,instClsLst = ic,lstVarVal = iv,compiledFunctions = cf)),msg)
local list<Exp.Exp> vars;
case (cache,env,Exp.CALL(path = Absyn.IDENT(name = "val"),expLst = {Exp.ARRAY(array = {varName, varTimeStamp})}),(st as Interactive.SYMBOLTABLE(ast = p,explodedAst = sp,instClsLst = ic,lstVarVal = iv,compiledFunctions = cf)),msg)
local
Exp.Exp varName, varTimeStamp;
String var;
Integer res;
Real timeStamp;
list<Values.Value> varValues, timeValues;
list<Real> tV, vV;
Real val;

equation
var = Exp.printExpStr(Exp.CodeVarToCref(varName));
(cache,Values.REAL(timeStamp),SOME(st)) = ceval(cache,env, varTimeStamp, true, SOME(st), NONE, msg);
(cache,Values.RECORD(_,{Values.STRING(filename)},_),_) = ceval(cache,env,
Exp.CREF(Exp.CREF_IDENT("currentSimulationResult",{}),Exp.OTHER()), true, SOME(st), NONE, msg);
Values.ARRAY({Values.ARRAY(varValues)}) = System.readPtolemyplotDataset(filename, {var}, 0);
Values.ARRAY({Values.ARRAY(timeValues)}) = System.readPtolemyplotDataset(filename, {"time"}, 0);
tV = Values.valueReals(timeValues);
vV = Values.valueReals(varValues);
val = System.getVariableValue(timeStamp, tV, vV);
then
(cache,Values.STRING("Implementation of val() not yet completed"),st);
(cache,Values.REAL(val),st);

case (cache,env,Exp.CALL(path = Absyn.IDENT(name = "val"),expLst = {Exp.ARRAY(array = vars)}),(st as Interactive.SYMBOLTABLE(ast = p,explodedAst = sp,instClsLst = ic,lstVarVal = iv,compiledFunctions = cf)),msg)
local
list<Exp.Exp> vars;
then
(cache,Values.STRING("Error, check variable name and time variables"),st);


/* plotparametric This rule represents the normal case when an array of at least two elements
* is given as an argument
* is given as an argument
*/
case (cache,env,Exp.CALL(path = Absyn.IDENT(name = "plotParametric"),expLst = vars),(st as Interactive.SYMBOLTABLE(ast = p,explodedAst = sp,instClsLst = ic,lstVarVal = iv,compiledFunctions = cf)),msg)
local
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Static.mo
Expand Up @@ -4370,17 +4370,23 @@ algorithm
case (cache,env,Absyn.CREF_IDENT(name = "val"),{(cr as Absyn.CREF(componentReg = _)),(cd as Absyn.REAL(value = _))},{},impl,SOME(st))
local
Absyn.Exp cr,cd;
Exp.Exp cd1;
equation
vars_1 = elabVariablenames({cr});
(cache,cd1,ptop,st_1) = elabExp(cache,env, cd, false, SOME(st));
vars_1 = listAppend(vars_1, {cd1});
then
(cache,Exp.CALL(Absyn.IDENT("val"),{Exp.ARRAY(Exp.OTHER(),false,vars_1)},
false,true),Types.PROP((Types.T_BOOL({}),NONE),Types.C_VAR()),SOME(st));

case (cache,env,Absyn.CREF_IDENT(name = "val"),{(cr as Absyn.CREF(componentReg = _)),(cd as Absyn.INTEGER(value = _))},{},impl,SOME(st))
local
Absyn.Exp cr,cd;
Exp.Exp cd1;
equation
vars_1 = elabVariablenames({cr});
(cache,cd1,ptop,st_1) = elabExp(cache,env, cd, false, SOME(st));
vars_1 = listAppend(vars_1, {cd1});
then
(cache,Exp.CALL(Absyn.IDENT("val"),{Exp.ARRAY(Exp.OTHER(),false,vars_1)},
false,true),Types.PROP((Types.T_BOOL({}),NONE),Types.C_VAR()),SOME(st));
Expand Down
10 changes: 10 additions & 0 deletions Compiler/System.mo
Expand Up @@ -351,5 +351,15 @@ public function setClassnamesForSimulation

external "C" ;
end setClassnamesForSimulation;

public function getVariableValue
input Real timeStamp;
input list<Real> timeValues;
input list<Real> varValues;
output Real outValue;

external "C" ;
end getVariableValue;

end System;

35 changes: 35 additions & 0 deletions Compiler/Values.mo
Expand Up @@ -756,6 +756,41 @@ algorithm
end matchcontinue;
end valueReal;

public function valueReals "function: valueReals
Return the real value of a Value. If the value is an integer,
it is cast to a real.
"
input list<Value> inValue;
output list<Real> outReal;
algorithm
outReal:=
matchcontinue (inValue)
local
Real r;
list<Value> rest;
list<Real> res;
Integer i;
case ({}) then {};
case (REAL(real = r)::rest)
equation
res = valueReals(rest);
then
r::res;
case (INTEGER(integer = i)::rest)
equation
r = intReal(i);
res = valueReals(rest);
then
r::res;
case (_::rest)
equation
res = valueReals(rest);
then
res;
end matchcontinue;
end valueReals;

public function valueNeg "function: valueNeg
author: PA
Expand Down
78 changes: 78 additions & 0 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -1145,6 +1145,84 @@ RML_END_LABEL

char* compile_command = NULL;

RML_BEGIN_LABEL(System__getVariableValue)
{
double timeStamp = rml_prim_get_real(rmlA0);
void *timeValues = rmlA1;
void *varValues = rmlA2;

// values to find the correct range
double preValue = 0.0;
double preTime = 0.0;
double nowValue = 0.0;
double nowTime = 0.0;

// linjear interpolation data
double timedif = 0.0;
double valuedif = 0.0;
double valueSlope = 0.0;
double timeDifTimeStamp = 0.0;

// break loop and return value
int valueFound = 0;
double returnValue = 0.0;


for(; RML_GETHDR(timeValues) == RML_CONSHDR ; timeValues = RML_CDR(timeValues), varValues = RML_CDR(varValues)) {

nowValue = rml_prim_get_real(RML_CAR(varValues));
nowTime = rml_prim_get_real(RML_CAR(timeValues));

if(timeStamp == nowTime){
valueFound = 1;
returnValue = nowValue;

} else if (timeStamp >= preTime && timeStamp <= nowTime) { // need to do interpolation
valueFound = 1;
timedif = nowTime - preTime;
valuedif = nowValue - preValue;
valueSlope = valuedif / timedif;
timeDifTimeStamp = timeStamp - preTime;
returnValue = preValue + (valueSlope*timeDifTimeStamp);
/*
printf("\t ### Interpolation ###");
printf("nowTime: %f", nowTime);
printf("\n");
printf("preTime: %f", preTime);
printf("\n");
printf("nowValue: %f", nowValue);
printf("\n");
printf("preValue: %f", preValue);
printf("\n");
printf("timedif: %f", timedif);
printf("\n");
printf("valuedif: %f", valuedif);
printf("\n");
printf("valueSlope: %f", valueSlope);
printf("\n");
printf("timeDifTimeStamp: %f", timeDifTimeStamp);
printf("\n");
printf("returnValue: %f", returnValue);
printf("\n");
*/
} else {
preValue = nowValue;
preTime = nowTime;
}
}

if(valueFound == 0){
// value could not be found in the dataset, what do we do?
printf("\n WARNING: timestamp outside simualtion timeline \n");
RML_TAILCALLK(rmlFC);
} else {

rmlA0 = (void*)mk_rcon(returnValue);
RML_TAILCALLK(rmlSC);
}
}
RML_END_LABEL

#endif /* MINGW32 */

80 changes: 80 additions & 0 deletions Compiler/winruntime/systemimpl.c
Expand Up @@ -1108,3 +1108,83 @@ RML_BEGIN_LABEL(System__setClassnamesForSimulation)
}
RML_END_LABEL

RML_BEGIN_LABEL(System__getVariableValue)
{
double timeStamp = rml_prim_get_real(rmlA0);
void *timeValues = rmlA1;
void *varValues = rmlA2;

// values to find the correct range
double preValue = 0.0;
double preTime = 0.0;
double nowValue = 0.0;
double nowTime = 0.0;

// linjear interpolation data
double timedif = 0.0;
double valuedif = 0.0;
double valueSlope = 0.0;
double timeDifTimeStamp = 0.0;

// break loop and return value
int valueFound = 0;
double returnValue = 0.0;

for(; RML_GETHDR(timeValues) == RML_CONSHDR ; timeValues = RML_CDR(timeValues), varValues = RML_CDR(varValues)) {


nowValue = rml_prim_get_real(RML_CAR(varValues));
nowTime = rml_prim_get_real(RML_CAR(timeValues));


if(timeStamp == nowTime){
valueFound = 1;
returnValue = nowValue;

} else if (timeStamp >= preTime && timeStamp <= nowTime) { // need to do interpolation
valueFound = 1;
timedif = nowTime - preTime;
valuedif = nowValue - preValue;
valueSlope = valuedif / timedif;
timeDifTimeStamp = timeStamp - preTime;
returnValue = preValue + (valueSlope*timeDifTimeStamp);
/*
printf("\t ### Interpolation ###");
printf("nowTime: %f", nowTime);
printf("\n");
printf("preTime: %f", preTime);
printf("\n");
printf("nowValue: %f", nowValue);
printf("\n");
printf("preValue: %f", preValue);
printf("\n");
printf("timedif: %f", timedif);
printf("\n");
printf("valuedif: %f", valuedif);
printf("\n");
printf("valueSlope: %f", valueSlope);
printf("\n");
printf("timeDifTimeStamp: %f", timeDifTimeStamp);
printf("\n");
printf("returnValue: %f", returnValue);
printf("\n");
*/
} else {
preValue = nowValue;
preTime = nowTime;

}

}
if(valueFound == 0){
// value could not be found in the dataset, what do we do?
printf("\n WARNING: timestamp outside simualtion timeline \n");
RML_TAILCALLK(rmlFC);
} else {

rmlA0 = (void*)mk_rcon(returnValue);
RML_TAILCALLK(rmlSC);
}
}
RML_END_LABEL

0 comments on commit 097dbbe

Please sign in to comment.