Skip to content

Commit

Permalink
fix for usub function in cpp template
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20884 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed May 28, 2014
1 parent cb7cfd1 commit f7ed18b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
63 changes: 35 additions & 28 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -5293,34 +5293,34 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__)))
<<
void <%lastIdentOfPath(modelInfo.name)%>::initPreVars(unordered_map<string,unsigned int>& vars1, unordered_map<string,unsigned int>& vars2)
{
vars1 = map_list_of
insert( vars1 )
<%{(vars.algVars |> SIMVAR(__) hasindex i0 =>
'("<%cref(name)%>",<%i0%>)'
;separator=" "; align=10;alignSeparator=" \n" ),
;separator=" "; align=10;alignSeparator=";\n insert( vars1 ) \n" ),
(vars.intAlgVars |> SIMVAR(__) hasindex i1 =>
'("<%cref(name)%>",(<%i1%>+<%n1%>))'
;separator=" "; align=10;alignSeparator=" \n" ),
;separator=" "; align=10;alignSeparator=";\n insert( vars1 ) \n" ),
(vars.boolAlgVars |> SIMVAR(__) hasindex i2=>
'("<%cref(name)%>",(<%i2%>+<%n2%>))'
;separator=" "; align=10;alignSeparator=" \n" ),
;separator=" "; align=10;alignSeparator=";\n insert( vars1 ) \n" ),
(vars.stateVars |> SIMVAR(__) hasindex i3 =>
'("<%cref(name)%>",(<%i3%>+<%n3%>))'
;separator=" "; align=10;alignSeparator=" \n" )}
;separator=" "; align=10;alignSeparator="\n" )}
;separator=" "%>;
vars2 = map_list_of
insert( vars2 )
<%{
(vars.algVars |> SIMVAR(__) hasindex i0 =>
'("<%cref(name)%>",<%i0%>)'
;separator=" ";align=10;alignSeparator=" \n"),
;separator=" ";align=10;alignSeparator=";\n insert( vars2 ) \n"),
(vars.intAlgVars |> SIMVAR(__) hasindex i1=>
'("<%cref(name)%>",(<%i1%>+<%n1%>))'
;separator=" ";align=10;alignSeparator=" \n"),
;separator=" ";align=10;alignSeparator=";\n insert( vars2 ) \n"),
(vars.boolAlgVars |> SIMVAR(__) hasindex i2 =>
'("<%cref(name)%>",(<%i2%>+<%n2%>))'
;separator=" ";align=10;alignSeparator=" \n")}
;separator=" ";align=10;alignSeparator=";\n insert( vars2 ) \n")}
;separator=" ";align=10;alignSeparator=" \n"
%>;
Expand Down Expand Up @@ -5904,13 +5904,13 @@ template equation_(SimEqSystem eq, Context context, Text &varDecls, SimCode simC
unsigned int dim<%index%> = _algLoop<%index%>->getDimReal();
double* algloop<%index%>Vars = new double[dim<%index%>];
_algLoop<%index%>->getReal(algloop<%index%>Vars );

bool restatDiscrete<%index%>= false;
try
{
_algLoop<%index%>->evaluate();
if( _callType == IContinuous::DISCRETE )
{
while(restart<%index%> && !(iterations<%index%>++>500))
Expand All @@ -5935,14 +5935,12 @@ template equation_(SimEqSystem eq, Context context, Text &varDecls, SimCode simC
}
catch(std::exception &ex)
{
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
throw std::invalid_argument("Nonlinear solver stopped at time " + boost::lexical_cast<string>(_simTime) + " with error: " + ex.what());
restatDiscrete<%index%>=true;
}
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
if(restart<%index%>&& iterations<%index%> > 0)

if((restart<%index%>&& iterations<%index%> > 0)|| restatDiscrete<%index%>)
{
try
{ //workaround: try to solve algoop discrete (evaluate all zero crossing conditions) since we do not have the information which zercrossing contains a algloop var
Expand All @@ -5955,11 +5953,15 @@ template equation_(SimEqSystem eq, Context context, Text &varDecls, SimCode simC
catch(std::exception &ex)
{
delete[] algloop<%index%>Vars;
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
throw std::invalid_argument("Nonlinear solver stopped at time " + boost::lexical_cast<string>(_simTime) + " with error: " + ex.what());
}

}
delete[] algloop<%index%>Vars;
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
>>
end match

Expand Down Expand Up @@ -6835,7 +6837,7 @@ template equationLinearOrNonLinear(SimEqSystem eq, Context context,Text &varDecl
unsigned int dim<%index%> = _algLoop<%index%>->getDimReal();
double* algloop<%index%>Vars = new double[dim<%index%>];
_algLoop<%index%>->getReal(algloop<%index%>Vars );

bool restatDiscrete<%index%>= false;
try
{
_algLoop<%index%>->evaluate();
Expand Down Expand Up @@ -6864,15 +6866,12 @@ template equationLinearOrNonLinear(SimEqSystem eq, Context context,Text &varDecl
}
catch(std::exception &ex)
{
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
throw std::invalid_argument("Nonlinear solver stopped at time " + boost::lexical_cast<string>(_simTime) + " with error: " + ex.what());
restatDiscrete<%index%>=true;
}

delete[] conditions0<%index%>;
delete[] conditions1<%index%>;


if(restart<%index%> && iterations<%index%> > 0)
if((restart<%index%>&& iterations<%index%> > 0)|| restatDiscrete<%index%>)
{
try
{ //workaround: try to solve algoop discrete (evaluate all zero crossing conditions) since we do not have the information which zercrossing contains a algloop var
Expand All @@ -6885,11 +6884,15 @@ template equationLinearOrNonLinear(SimEqSystem eq, Context context,Text &varDecl
catch(std::exception& ex)
{
delete[] algloop<%index%>Vars;
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
throw std::invalid_argument("Nonlinear solver stopped at time " + boost::lexical_cast<string>(_simTime) + " with error: " + ex.what());
}

}
delete[] algloop<%index%>Vars;
delete[] conditions0<%index%>;
delete[] conditions1<%index%>;
>>
end match
end match
Expand Down Expand Up @@ -8109,11 +8112,15 @@ case UNARY(__) then
match operator
case UMINUS(__) then '(-<%e%>)'
case UMINUS_ARR(ty=T_ARRAY(ty=T_REAL(__))) then
let &preExp += 'usub_array<double,<%listLength(ty.dims)%>>(<%e%>);<%\n%>'
'<%e%>'
let tmp_type_str = 'multi_array<double,<%listLength(ty.dims)%>>'
let tvar = tempDecl(tmp_type_str, &varDecls /*BUFD*/)
let &preExp += 'usub_array<double,<%listLength(ty.dims)%>>(<%e%>,<%tvar%>);<%\n%>'
'<%tvar%>'
case UMINUS_ARR(ty=T_ARRAY(ty=T_INTEGER(__))) then
let &preExp += 'usub_array<int,<%listLength(ty.dims)%>>(<%e%>);<%\n%>'
'<%e%>'
let tmp_type_str = 'multi_array<int,<%listLength(ty.dims)%>>'
let tvar = tempDecl(tmp_type_str, &varDecls /*BUFD*/)
let &preExp += 'usub_array<int,<%listLength(ty.dims)%>>(<%e%>,<%tvar%>);<%\n%>'
'<%tvar%>'
case UMINUS_ARR(__) then 'unary minus for non-real arrays not implemented'
else "daeExpUnary:ERR"
end daeExpUnary;
Expand Down
5 changes: 3 additions & 2 deletions SimulationRuntime/cpp/Include/Core/Math/ArrayOperations.h
Expand Up @@ -149,9 +149,10 @@ boost::multi_array< T, 1 > cross_array( boost::multi_array_ref< T, 1 > a , boos
- array (a)
*/
template < typename T, size_t dims >
void usub_array(boost::multi_array_ref< T, dims > &a)
void usub_array(boost::multi_array_ref< T, dims > a,boost::multi_array< T, dims >& b)
{
for ( typename boost::multi_array< T, dims >::iterator i = a.begin(); i != a.end(); i++)
assign_array(b,a);
for ( typename boost::multi_array< T, dims >::iterator i = b.begin(); i != b.end(); i++)
(*i) = -(*i);
}

Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/Include/Core/Modelica.h
Expand Up @@ -78,6 +78,7 @@ using namespace std;
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/unordered_map.hpp>
#include <boost/program_options.hpp>
#include <boost/assign/list_inserter.hpp>
/*Namespaces*/
using namespace boost::extensions;
namespace fs = boost::filesystem;
Expand Down
12 changes: 7 additions & 5 deletions SimulationRuntime/cpp/Include/Core/ModelicaDefine.h
@@ -1,3 +1,4 @@

typedef double modelica_real ;
typedef int modelica_integer;
typedef bool modelica_boolean;
Expand Down Expand Up @@ -27,20 +28,20 @@ typedef double coshRetType;



#if !defined(_MSC_VER)
#if !defined(_MSC_VER) && !defined( __APPLE__)
//extern template class boost::shared_ptr<IAlgLoopSolver>;
//extern template class boost::shared_ptr<IAlgLoop>;
//extern template class boost::shared_ptr<IAlgLoopSolverFactory>;
//extern template class boost::shared_ptr<ISimData>;
extern template class boost::multi_array<double,2>;
extern template class boost::multi_array<double,1>;
extern template class boost::multi_array<int,2>;
extern template class boost::multi_array<int,2>;
extern template class boost::multi_array<int,1>;
extern template class ublas::vector<double>;
extern template class ublas::vector<int>;
extern template class ublas::vector<int>;
extern template class uBlas::compressed_matrix<double, uBlas::column_major, 0, uBlas::unbounded_array<int>, uBlas::unbounded_array<double> > ;
extern template class std::vector<int>;
extern template class std::vector<double>;
extern template class std::vector<int>;
extern template class std::vector<double>;
extern template class unordered_map<string,unsigned int>;
extern template class map<unsigned int,string>;
extern template class vector<string>;
Expand All @@ -53,4 +54,5 @@ extern template class boost::function<void (unordered_map<string,unsigned int>&
//extern template class ublas::shallow_array_adaptor<double>;
//extern template class ublas::vector<double, adaptor_t>;
//extern template class ublas::matrix<double, adaptor_t> shared_matrix_t;

#endif

0 comments on commit f7ed18b

Please sign in to comment.