Skip to content

Commit

Permalink
Fix size of result of multiply_array in Cpp runtime (#7588)
Browse files Browse the repository at this point in the history
Closes #7582
  • Loading branch information
rfranke committed Jun 21, 2021
1 parent 170459c commit a6ec77a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
11 changes: 10 additions & 1 deletion OMCompiler/SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp
Expand Up @@ -272,12 +272,14 @@ void multiply_array(const BaseArray<T> &leftArray, const BaseArray<T> &rightArra
size_t leftNumDims = leftArray.getNumDims();
size_t rightNumDims = rightArray.getNumDims();
size_t matchDim = rightArray.getDim(1);
resultArray.setDims(leftArray.getDims());
if (leftArray.getDim(leftNumDims) != matchDim)
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
"Wrong sizes in multiply_array");
if (leftNumDims == 1 && rightNumDims == 2) {
size_t rightDim = rightArray.getDim(2);
vector<size_t> dims;
dims.push_back(rightDim);
resultArray.setDims(dims);
for (size_t j = 1; j <= rightDim; j++) {
T val = T();
for (size_t k = 1; k <= matchDim; k++)
Expand All @@ -287,6 +289,9 @@ void multiply_array(const BaseArray<T> &leftArray, const BaseArray<T> &rightArra
}
else if (leftNumDims == 2 && rightNumDims == 1) {
size_t leftDim = leftArray.getDim(1);
vector<size_t> dims;
dims.push_back(leftDim);
resultArray.setDims(dims);
for (size_t i = 1; i <= leftDim; i++) {
T val = T();
for (size_t k = 1; k <= matchDim; k++)
Expand All @@ -297,6 +302,10 @@ void multiply_array(const BaseArray<T> &leftArray, const BaseArray<T> &rightArra
else if (leftNumDims == 2 && rightNumDims == 2) {
size_t leftDim = leftArray.getDim(1);
size_t rightDim = rightArray.getDim(2);
vector<size_t> dims;
dims.push_back(leftDim);
dims.push_back(rightDim);
resultArray.setDims(dims);
for (size_t i = 1; i <= leftDim; i++) {
for (size_t j = 1; j <= rightDim; j++) {
T val = T();
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/cppruntime/Makefile
Expand Up @@ -5,6 +5,7 @@ tearnonlin1.mos \
WhenStatement1.mos \
WhenTuple.mos \
BouncingBall.mos \
arrayOperationsTest.mos \
arraySliceTest.mos \
clockedAlgloopTest.mos \
clockedEventTest.mos \
Expand Down
46 changes: 46 additions & 0 deletions testsuite/openmodelica/cppruntime/arrayOperationsTest.mos
@@ -0,0 +1,46 @@
// name: arrayOperationsTest
// keywords: array operations
// status: correct
// teardown_command: rm -f *ArrayOperations.Test*
// cflags: -d=-newInst

setCommandLineOptions("+simCodeTarget=Cpp");

loadString("
package ArrayOperations
model Test
input Real u = 0;
Real y = f({u + 1, u + 2, u + 3});
annotation(experiment(StopTime = 0));
end Test;
function f
input Real[:] v;
output Real y;
protected
Real[:,:] m;
algorithm
// see e.g. Modelica.Mechanics.MultiBody.Examples.Rotational3DEffects.MovingActuatedDrive
m := matrix(v) * transpose(matrix(v));
y := m[3,1];
end f;
end ArrayOperations;
");
getErrorString();

simulate(ArrayOperations.Test);
getErrorString();

val(y, 0);

// Result:
// true
// true
// ""
// record SimulationResult
// resultFile = "ArrayOperations.Test_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'ArrayOperations.Test', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = ""
// end SimulationResult;
// ""
// 3.0
// endResult

0 comments on commit a6ec77a

Please sign in to comment.