Skip to content

Commit

Permalink
Fix Cpp reduction for array, remove obsolete create/fill_array_from_s…
Browse files Browse the repository at this point in the history
…hape

See ModelicaTest.Fluid.TestComponents.ModelicaTest.Fluid.TestComponents.Machines.TestWaterPumpCharacteristics
in particular pumpPolynomial using Modelica.Fluid.Machines.BaseClasses.PumpCharacteristics.polynomialFlow
  • Loading branch information
rfranke authored and adrpo committed Nov 17, 2021
1 parent 2067331 commit c829d34
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 161 deletions.
28 changes: 4 additions & 24 deletions OMCompiler/Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -1068,31 +1068,11 @@ template daeExpReduction(Exp exp, Context context, Text &preExp,
let rec_name = '<%underscorePath(ClassInf.getStateName(record_state))%>'
'<%rec_name%>_array_get(<%res%>, 1, <%arrIndex%>++) = <%reductionBodyExpr%>;'
case T_ARRAY(__) then
let tmp_shape = tempDecl("vector<size_t>", &varDecls /*BUFD*/)
let tmp_indeces = tempDecl("idx_type", &varDecls /*BUFD*/)
/*let idx_str = (dims |> dim =>
let tmp_idx = tempDecl("vector<size_t>", &varDecls)
let &preExp += '<%tmp_shape%>.push_back(1);<%\n%>
<%tmp_indeces%>.push_back(<%tmp_idx%>);<%\n%>'
''
)*/
let tmp_idx = tempDecl("vector<size_t>", &varDecls /*BUFD*/)
/*let &preExp += '<%tmp_shape%>.push_back(0);<%\n%>
<%tmp_idx%>.push_back(<%arrIndex%>++);<%\n%>
<%tmp_indeces%>.push_back(<%tmp_idx%>);<%\n%>'
let tmp = 'make_pair(<%tmp_shape%>,<%tmp_indeces%>)'
*/

let tmp_slice = tempDecl("vector<Slice>", &varDecls)
<<
<%(dims |> dim =>
let tmp_idx = tempDecl("vector<size_t>", &varDecls /*BUFD*/)
'<%tmp_shape%>.push_back(1);<%\n%>
<%tmp_indeces%>.push_back(<%tmp_idx%>);<%\n%>'
)%>
<%tmp_shape%>.push_back(0);<%\n%>
<%tmp_idx%>.push_back(<%arrIndex%>++);<%\n%>
<%tmp_indeces%>.push_back(<%tmp_idx%>);<%\n%>
fill_array_from_shape(make_pair(<%tmp_shape%>,<%tmp_indeces%>),<%reductionBodyExpr%>,<%res%>);
<%tmp_slice%>.clear();
<%tmp_slice%>.push_back(Slice(<%arrIndex%>++));
ArraySlice<<%expTypeShort(ty)%>>(<%res%>, <%tmp_slice%>).assign(<%reductionBodyExpr%>);
>>
else
'<%res%>(<%arrIndex%>++) = <%reductionBodyExpr%>;'
Expand Down
127 changes: 3 additions & 124 deletions OMCompiler/SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp
Expand Up @@ -86,120 +86,6 @@ void cat_array(int k, const vector<const BaseArray<T>*>& x, BaseArray<T>& a)
}
}

/*
creates an array (d) for passed multi array shape (sp) and initialized it with elements from passed source array (s)
s source array
d destination array
sp (shape,indices) of source array
*/
template < typename T >
void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d)
{
//alocate target array
vector<size_t> shape;
vector<size_t>::const_iterator iter;
for(iter = (sp.first).begin();iter!=(sp.first).end();++iter)
{
if(*iter!=0)
shape.push_back(*iter);

}
d.setDims(shape);

//Check if the dimension of passed indices match the dimension of target array
if(sp.second.size()!=s.getNumDims())
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Erro in create array from shape, number of dimensions does not match");

T* data = new T[d.getNumElems()];

idx_type::const_iterator spec_iter;
//calc number of indeces
size_t n =1;
for(spec_iter = sp.second.begin();spec_iter!=sp.second.end();++spec_iter)
{

n*=spec_iter->size();
}
size_t k =0;
size_t index=0;
vector<size_t>::const_iterator indeces_iter;

//initialize target array with elements of source array using passed indices
vector<size_t> idx;
for(int i=0;i<n;i++)
{
spec_iter = sp.second.begin();
for(int dim=0;dim<s.getNumDims();dim++)
{
size_t idx1 = getNextIndex(*spec_iter,i);
idx.push_back(idx1);
spec_iter++;
}
if(index>(d.getNumElems()-1))
{
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Erro in create array from shape, number of dimensions does not match");
}
data[index] = s(idx);
idx.clear();
index++;
}
//assign elemets to target array
d.assign( data );
delete [] data;
}




/*
fills an array (d) with passed multi array shape (sp) and initialized it with elements from passed source array (s)
s source array
d destination array
sp (shape,indices) of source array
*/
template < typename T >
void fill_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d)
{


T* data = new T[d.getNumElems()];

idx_type::const_iterator spec_iter;
//calc number of indeces
size_t n =1;
for(spec_iter = sp.second.begin();spec_iter!=sp.second.end();++spec_iter)
{

n*=spec_iter->size();
}
size_t k =0;
size_t index=0;
vector<size_t>::const_iterator indeces_iter;

//initialize target array with elements of source array using passed indices
vector<size_t> idx;
for(int i=0;i<n;i++)
{
spec_iter = sp.second.begin();
for(int dim=0;dim<s.getNumDims();dim++)
{
size_t idx1 = getNextIndex(*spec_iter,i);
idx.push_back(idx1);
spec_iter++;
}
if(index>(d.getNumElems()-1))
{
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,"Erro in create array from shape, number of dimensions does not match");
}
data[index] = s(idx);
idx.clear();
index++;
}
//assign elemets to target array
d.assign( data );
delete [] data;
}

void identity_alloc(size_t n, DynArrayDim2<int>& I)
{
I.setDims(n, n);
Expand All @@ -226,7 +112,9 @@ void diagonal_alloc(const BaseArray<T>& v, BaseArray<T>& D)
D_data[i * n + i] = v_data[i];
}

//template < typename T , size_t NumDims, size_t NumDims2 >
/**
* appends dimensions of size 1 to the right of s up to dimension n
*/
template <typename T>
void promote_array(size_t n, const BaseArray<T>& s, BaseArray<T>& d)
{
Expand Down Expand Up @@ -644,15 +532,6 @@ promote_array(size_t n, const BaseArray<int>& s, BaseArray<int>& d);
template void BOOST_EXTENSION_EXPORT_DECL
promote_array(size_t n, const BaseArray<bool>& s, BaseArray<bool>& d);

template void BOOST_EXTENSION_EXPORT_DECL create_array_from_shape(const spec_type& sp, BaseArray<double>& s, BaseArray<double>& d);
template void BOOST_EXTENSION_EXPORT_DECL create_array_from_shape(const spec_type& sp, BaseArray<int>& s, BaseArray<int>& d);
template void BOOST_EXTENSION_EXPORT_DECL create_array_from_shape(const spec_type& sp, BaseArray<bool>& s, BaseArray<bool>& d);


template void BOOST_EXTENSION_EXPORT_DECL fill_array_from_shape(const spec_type& sp, BaseArray<double>& s, BaseArray<double>& d);
template void BOOST_EXTENSION_EXPORT_DECL fill_array_from_shape(const spec_type& sp, BaseArray<int>& s, BaseArray<int>& d);
template void BOOST_EXTENSION_EXPORT_DECL fill_array_from_shape(const spec_type& sp, BaseArray<bool>& s, BaseArray<bool>& d);

template void BOOST_EXTENSION_EXPORT_DECL
multiply_array(const BaseArray<double>& inputArray, const double &b, BaseArray<double>& outputArray);
template void BOOST_EXTENSION_EXPORT_DECL
Expand Down
Expand Up @@ -36,19 +36,6 @@ void cat_array(int k, const vector<const BaseArray<T>*>& x, BaseArray<T>& a);
template <typename T>
void transpose_array(const BaseArray<T>& x, BaseArray<T>& a);

/*
creates an array (d) for passed multi array shape (sp) and initialized it with elements from passed source array (s)
s source array
d destination array
sp (shape,indices) of source array
*/

template < typename T >
void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d);

template < typename T >
void fill_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d);

void BOOST_EXTENSION_EXPORT_DECL identity_alloc(size_t n, DynArrayDim2<int>& I);

template <typename T>
Expand Down

0 comments on commit c829d34

Please sign in to comment.