Skip to content

Commit

Permalink
fix in algloopsystem of cpp runtime for Modelica function calls
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10292 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Nov 3, 2011
1 parent c216f8e commit a65d972
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
73 changes: 61 additions & 12 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -6436,7 +6436,7 @@ algorithm
states_lst2 =listAppend(states_lst,der_states_lst);
Debug.fcall("cpp",print,"replace index in states \n");
states_2 = replaceindex1(stateVars,states_lst);
//Debug.fcall("cppvar",print," replace der varibales: \n " +&dumpVarinfoList(states_lst2));
// Debug.fcall("cppvar",print," replace der varibales: \n " +&dumpVarinfoList(states_lst2));
Debug.fcall("cpp",print,"replace index in der states \n");
derivatives_2=replaceindex1(derivative,der_states_lst);
//Debug.fcall("cppvar",print,"state varibales: \n " +&dumpVarinfoList(states_lst2));
Expand Down Expand Up @@ -10685,23 +10685,67 @@ Author bz 2008-06
This function investigates the system of equations finding an order for derivative variables.
It only selects variables that have an derivative order, order=0 (no derivative) will not be included.
"
input BackendDAE.BackendDAE inDlow;
input BackendDAE.BackendDAE inDlow;
input BackendDAE.EqSystems inEqSystems;
output list<tuple<DAE.ComponentRef, Integer>> outOrder;
algorithm outOrder := matchcontinue(inDlow)
algorithm outOrder := matchcontinue(inDlow,inEqSystems)
local
BackendDAE.Variables dovars;
BackendDAE.EquationArray deqns;
list<BackendDAE.Equation> eqns;
list<BackendDAE.Var> vars;
list<Expression.Exp> derExps;
list<tuple<DAE.ComponentRef, Integer>> variableIndex;
list<tuple<DAE.ComponentRef, Integer>> variableIndex2;
list<tuple<DAE.ComponentRef, Integer>> variableIndex3;
list<list<DAE.ComponentRef>> firstOrderVars;
list<DAE.ComponentRef> firstOrderVarsFiltered;
String eq_str;
BackendDAE.EqSystem syst;
case(inDlow as BackendDAE.DAE(eqs={syst}))
BackendDAE.EqSystems systs;
case(inDlow,{})
then
{};
case(inDlow,syst::systs)
equation

Debug.fcall("cppvar",print, " set variabale der index for eqsystem"+& "\n");
variableIndex = setVariableDerIndex2(inDlow,syst);
variableIndex2 = setVariableDerIndex(inDlow,systs);
variableIndex3 = listAppend(variableIndex,variableIndex2);
then
variableIndex3;
case(_,_)
equation
print(" Failure in setVariableDerIndex \n");
then fail();
end matchcontinue;
end setVariableDerIndex;


protected function setVariableDerIndex2 "
Author bz 2008-06
This function investigates the system of equations finding an order for derivative variables.
It only selects variables that have an derivative order, order=0 (no derivative) will not be included.
"
input BackendDAE.BackendDAE inDlow;
input BackendDAE.EqSystem syst;
output list<tuple<DAE.ComponentRef, Integer>> outOrder;
algorithm outOrder := matchcontinue(inDlow,syst)
local
BackendDAE.Variables dovars;
BackendDAE.EquationArray deqns;
list<BackendDAE.Equation> eqns;
list<BackendDAE.Var> vars;
list<Expression.Exp> derExps;
list<tuple<DAE.ComponentRef, Integer>> variableIndex;
list<list<DAE.ComponentRef>> firstOrderVars;
list<DAE.ComponentRef> firstOrderVarsFiltered;
String eq_str;
BackendDAE.EqSystem syst;
BackendDAE.EqSystems systs;
case(inDlow,syst)
equation
Debug.fcall("cppvar",print, " set variabale der index"+& "\n");
dovars = BackendVariable.daeVars(syst);
deqns = BackendEquation.daeEqns(syst);
vars = BackendDAEUtil.varList(dovars);
Expand All @@ -10719,12 +10763,15 @@ algorithm outOrder := matchcontinue(inDlow)
// Debug.fcall("cppvar",print,"Deriving Variable indexis:\n" +& dumpVariableindex(variableIndex) +& "\n");
then
variableIndex;
case(_)
case(_,_)
equation
print(" Failure in setVariableDerIndex \n");
print(" Failure in setVariableDerIndex2 \n");
then fail();
end matchcontinue;
end setVariableDerIndex;
end setVariableDerIndex2;




protected function flattenEqns "
This function flattens all equations
Expand Down Expand Up @@ -11019,9 +11066,10 @@ algorithm (OutInteger1,OutInteger2):= matchcontinue(dae_low)
local
Integer nvar1,nvar2;
list<tuple<DAE.ComponentRef, Integer>> ordered_states;
case(dae_low)
BackendDAE.EqSystems eqsystems;
case(dae_low as BackendDAE.DAE(eqs=eqsystems))
equation
ordered_states=setVariableDerIndex(dae_low);
ordered_states=setVariableDerIndex(dae_low,eqsystems);
(nvar1,nvar2)=calculateVariableDimensions(ordered_states);
then
(nvar1,nvar2);
Expand Down Expand Up @@ -11114,10 +11162,11 @@ algorithm (outSimVar):= matchcontinue(stateVars,dae_low)
Causality causality;
Option<Integer> variable_index;
list <String> numArrayElement;
case(SIMVAR(name=name,varKind=varKind,comment=comment,unit=unit,displayUnit=displayUnit,index=index,initialValue=initialValue,nominalValue=nominalValue,isFixed=isFixed,type_=type_,isDiscrete=isDiscrete,arrayCref=arrayCref,aliasvar=aliasvar,source=source,causality=causality,variable_index=variable_index,numArrayElement=numArrayElement),dae_low)
BackendDAE.EqSystems eqsystems;
case(SIMVAR(name=name,varKind=varKind,comment=comment,unit=unit,displayUnit=displayUnit,index=index,initialValue=initialValue,nominalValue=nominalValue,isFixed=isFixed,type_=type_,isDiscrete=isDiscrete,arrayCref=arrayCref,aliasvar=aliasvar,source=source,causality=causality,variable_index=variable_index,numArrayElement=numArrayElement),dae_low as BackendDAE.DAE(eqs=eqsystems))
equation
Debug.fcall("cppvarindex",BackendDump.debugStrCrefStr,(" search index for state variable ",name,"\n"));
ordered_states=setVariableDerIndex(dae_low);
ordered_states=setVariableDerIndex(dae_low,eqsystems);
new_index=stateindex(name,ordered_states);

then
Expand Down
14 changes: 6 additions & 8 deletions Compiler/susan_codegen/SimCode/SimCodeCpp.tpl
Expand Up @@ -266,6 +266,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
let expPart = daeExp(eq2.exp, contextSimulationDiscrete,
&preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
'<%preExp%>_residuals[<%i0%>] = <%expPart%>;'
;separator="\n")
<<
Expand All @@ -274,7 +275,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%varDecls%>
if(command & IContinous::CONTINOUS)
{
<%varDecls%>
<%algs%>
<%prebody%>
<%body%>
Expand Down Expand Up @@ -958,6 +959,7 @@ case SIMCODE(modelInfo=MODELINFO(__), extObjInfo=EXTOBJINFO(__)) then
#define BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL BOOST_EXTENSION_IMPORT_DECL
#include "System/Interfaces/IDAESystem.h"
#include "System/Implementation/AlgLoopDefaultImplementation.h"
#include "Functions.h"
>>
end generateAlgloopHeaderInlcudeString;
Expand Down Expand Up @@ -1015,6 +1017,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%generateAlgloopMethodDeclarationCode(simCode)%>
private:
Functions _functions;
double *_residuals; ///< Auxillary variables
<%algvars%>
};
Expand Down Expand Up @@ -3027,13 +3030,8 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
case CALL(attr=CALL_ATTR(ty=ET_NORETCALL(__))) then ""
else tempDecl(retType, &varDecls)
let &preExp += match context case FUNCTION_CONTEXT(__) then'<%if retVar then '<%retVar%> = '%><%funName%>(<%argStr%>);<%\n%>'
let &preExp += match context case OTHER(__) then'<%if retVar then '<%retType%> <%retVar%> = '%>_functions.<%funName%>(<%argStr%>);<%\n%>'
let &preExp += if not attr.builtin then match context case SIMULATION(__) then
<<
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ACC_FN(<%funName%>_index);
#endif<%\n%>
>>
/*let &preExp += match context case OTHER(__) then*/ else '<%if retVar then '<%retType%> <%retVar%> = '%>_functions.<%funName%>(<%argStr%>);<%\n%>'
match exp
// no return calls
case CALL(attr=CALL_ATTR(ty=ET_NORETCALL(__))) then '/* NORETCALL */'
Expand Down
Expand Up @@ -13,7 +13,7 @@ AlgLoopSolverFactory::~AlgLoopSolverFactory()
}

/// Creates a solver according to given system of equations of type algebraic loop
IAlgLoopSolver* AlgLoopSolverFactory::createAlgLoopSolver(IAlgLoop* algLoop)
boost::shared_ptr<IAlgLoopSolver> AlgLoopSolverFactory::createAlgLoopSolver(IAlgLoop* algLoop)
{
if(algLoop->getDimVars(IAlgLoop::REAL) > 0)
{
Expand All @@ -40,11 +40,11 @@ IAlgLoopSolver* AlgLoopSolverFactory::createAlgLoopSolver(IAlgLoop* algLoop)
}

_algsolver= boost::shared_ptr<IAlgLoopSolver>(iter->second.create(algLoop,_algsolversettings.get()));
return _algsolver.get();
return _algsolver;
}
else
{
// TODO: Throw an error message here.
return 0;
throw std::invalid_argument("No Newton solver found");
}
}
Expand Up @@ -25,7 +25,7 @@ class AlgLoopSolverFactory : public IAlgLoopSolverFactory
~AlgLoopSolverFactory();

/// Creates a solver according to given system of equations of type algebraic loop
virtual IAlgLoopSolver* createAlgLoopSolver(IAlgLoop* algLoop);
virtual boost::shared_ptr<IAlgLoopSolver> createAlgLoopSolver(IAlgLoop* algLoop);

private:
boost::shared_ptr<INewtonSettings>_algsolversettings;
Expand Down
Expand Up @@ -14,6 +14,7 @@
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/extension/extension.hpp>
#include <boost/extension/factory.hpp>
#include <boost/extension/type_map.hpp>
Expand Down

0 comments on commit a65d972

Please sign in to comment.