Opened 4 years ago

Closed 4 years ago

#6266 closed defect (fixed)

Index out of bounds for array returned by built-in function

Reported by: Andreas Heuermann Owned by: Andreas Heuermann
Priority: high Milestone: 1.17.0
Component: Backend Version: v1.17.0-dev
Keywords: array, assertion, newFrontend Cc: kabdelhak

Description (last modified by Andreas Heuermann)

I have a model that simulates with the old frontend but fails to simulate with the new frontend.

setCommandLineOptions("-d=newInst");
loadString("
model mwe 
  parameter Integer dim=4;
  Real vectorA[1,dim];
  Real vectorB[1,dim];
equation
  vectorA = {{10,20,30,40}};
  vectorB = tanh(vectorA) .* ones(1,dim);
end mwe;
"); getErrorString();
simulate(mwe); getErrorString();
true
true
""
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'mwe', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Simulation execution failed for model: mwe
assert            | debug   | Index 2 out of bounds [1..1] for array {tanh(10.0)}
assert            | info    | simulation terminated by an assertion at initialization
",
    timeFrontend = 0.0007499000000000001,
    timeBackend = 0.0085439,
    timeSimCode = 0.0006612,
    timeTemplates = 0.0020973,
    timeCompile = 0.3537083,
    timeSimulation = 0.009690300000000001,
    timeTotal = 0.3755453
end SimulationResult;
""

Not sure if this is a frontend, backend or runtime problem. We'll can push it around a bit.

Change History (8)

comment:1 by Andreas Heuermann, 4 years ago

Component: FrontendBackend
Owner: changed from Per Östlund to Andreas Heuermann
Status: newassigned

At the beginning of the backend we have

unknown partition
========================================

Variables (8)
========================================
1: vectorB[1,4]:VARIABLE()  type: Real [1,4]
2: vectorB[1,3]:VARIABLE()  type: Real [1,4]
3: vectorB[1,2]:VARIABLE()  type: Real [1,4]
4: vectorB[1,1]:VARIABLE()  type: Real [1,4]
5: vectorA[1,4]:VARIABLE()  type: Real [1,4]
6: vectorA[1,3]:VARIABLE()  type: Real [1,4]
7: vectorA[1,2]:VARIABLE()  type: Real [1,4]
8: vectorA[1,1]:VARIABLE()  type: Real [1,4]


Equations (5, 8)
========================================
1/1 (1): vectorA[1,1] = 10.0   [dynamic |0|0|0|0|]
2/2 (1): vectorA[1,2] = 20.0   [dynamic |0|0|0|0|]
3/3 (1): vectorA[1,3] = 30.0   [dynamic |0|0|0|0|]
4/4 (1): vectorA[1,4] = 40.0   [dynamic |0|0|0|0|]
5/5 (4): vectorB = {{tanh(vectorA[1,1])}, {tanh(vectorA[1,2])}, {tanh(vectorA[1,3])}, {tanh(vectorA[1,4])}} .* {{1.0, 1.0, 1.0, 1.0}}   [dynamic |0|0|0|0|]

which is looking fine.

But at the end we have this:

Known variables only depending on parameters and constants - globalKnownVars (9)
========================================
1: dim:PARAM(final = true )  = 4  type: Integer
2: vectorA[1,4]:VARIABLE(fixed = true )  = 40.0  type: Real [1,4]
3: vectorA[1,3]:VARIABLE(fixed = true )  = 30.0  type: Real [1,4]
4: vectorA[1,2]:VARIABLE(fixed = true )  = 20.0  type: Real [1,4]
5: vectorA[1,1]:VARIABLE(fixed = true )  = 10.0  type: Real [1,4]
6: vectorB[1,1]:VARIABLE(fixed = true )  = tanh(10.0)  type: Real [1,4]
7: vectorB[1,2]:VARIABLE(fixed = true )  = {tanh(10.0)}[2]  type: Real [1,4]
8: vectorB[1,3]:VARIABLE(fixed = true )  = {tanh(10.0)}[3]  type: Real [1,4]
9: vectorB[1,4]:VARIABLE(fixed = true )  = {tanh(10.0)}[4]  type: Real [1,4]

All elements of vectorB are 10.0.

So this is most definitely a backend issue.

comment:2 by Andreas Heuermann, 4 years ago

Cc: kabdelhak added

And the winner is: removeSimpleEquations

Known variables only depending on parameters and constants - globalKnownVars (9)
========================================
1: vectorB[1,4]:VARIABLE()  = {tanh(10.0)}[4]  type: Real [1,4]
2: vectorB[1,3]:VARIABLE()  = {tanh(10.0)}[3]  type: Real [1,4]
3: vectorB[1,2]:VARIABLE()  = {tanh(10.0)}[2]  type: Real [1,4]
4: vectorB[1,1]:VARIABLE()  = tanh(10.0)  type: Real [1,4]
5: vectorA[1,1]:VARIABLE()  = 10.0  type: Real [1,4]
6: vectorA[1,2]:VARIABLE()  = 20.0  type: Real [1,4]
7: vectorA[1,3]:VARIABLE()  = 30.0  type: Real [1,4]
8: vectorA[1,4]:VARIABLE()  = 40.0  type: Real [1,4]
9: dim:PARAM(final = true )  = 4  type: Integer

@kabdelhak Do you know how to solve this immediately? Otherwise I will do this

Last edited 4 years ago by Andreas Heuermann (previous) (diff)

comment:3 by Andreas Heuermann, 4 years ago

So I traced the error down. The replacement of A is not working:

Equations (5, 8)
========================================
1/1 (1): vectorA[1,1] = 10.0   [dynamic |0|0|0|0|]
2/2 (1): vectorA[1,2] = 20.0   [dynamic |0|0|0|0|]
3/3 (1): vectorA[1,3] = 30.0   [dynamic |0|0|0|0|]
4/4 (1): vectorA[1,4] = 40.0   [dynamic |0|0|0|0|]
5/5 (4): vectorB = {{tanh(vectorA[1,1])}, {tanh(vectorA[1,2])}, {tanh(vectorA[1,3])}, {tanh(vectorA[1,4])}} .* {{1.0, 1.0, 1.0, 1.0}}   [dynamic |0|0|0|0|]

will become

1/1 (1): vectorB[1,4] = {tanh(vectorA[1,1])}[4]   [dynamic |0|0|0|0|]
2/2 (1): vectorB[1,3] = {tanh(vectorA[1,1])}[3]   [dynamic |0|0|0|0|]
3/3 (1): vectorB[1,2] = {tanh(vectorA[1,1])}[2]   [dynamic |0|0|0|0|]
4/4 (1): vectorB[1,1] = tanh(vectorA[1,1])   [dynamic |0|0|0|0|]

when A get's replaced with constants.

But the actual error is hard to pin down. It has to be in handleSets called from causalFinder in RemoveSimpleEquations.mo.

comment:4 by Andreas Heuermann, 4 years ago

Okay, the first half of this is fixed, see https://github.com/OpenModelica/OpenModelica/pull/7015
I replaced Expression.applyExpSubscriptsFoldCheckSimplify with Expression.subscriptExp.

Now I'm getting

Equations (1, 4)
========================================
1/1 (4): vectorB = {{tanh(10.0)}, {tanh(20.0)}, {tanh(30.0)}, {tanh(40.0)}} .* {{1.0, 1.0, 1.0, 1.0}}   [dynamic |0|0|0|0|]

which has the wrong dimension [4,1] instead of [1,4].

So the simulation will fail

true
true
""
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'mwe', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Simulation execution failed for model: mwe
src->dim_size[0] != dst->dim_size[0], 4 != 1
assert            | debug   | Failed to copy array. Dimension sizes are not equal and destination array is not flexible.
assert            | info    | simulation terminated by an assertion at initialization
",
    timeFrontend = 0.0006816000000000001,
    timeBackend = 0.0026292,
    timeSimCode = 0.0005546,
    timeTemplates = 0.0029034,
    timeCompile = 0.3454419,
    timeSimulation = 0.009934100000000001,
    timeTotal = 0.3622272
end SimulationResult;
""

Last edited 4 years ago by Andreas Heuermann (previous) (diff)

comment:5 by Andreas Heuermann, 4 years ago

Description: modified (diff)

comment:6 by Andreas Heuermann, 4 years ago

Description: modified (diff)

comment:7 by Andreas Heuermann, 4 years ago

Related ticket: #6268

comment:8 by Per Östlund, 4 years ago

Resolution: fixed
Status: assignedclosed

Fixed in 620b4e42.

Note: See TracTickets for help on using tickets.