OpenModelica / OpenModelica

OpenModelica is an open-source Modelica-based modeling and simulation environment intended for industrial and academic usage.
https://openmodelica.org
Other
840 stars 306 forks source link

Wrong Jacobians generated by the NB in ScalableTestSuite heat exchanger models #12906

Open casella opened 1 month ago

casella commented 1 month ago

The ScalableTestSuite_OB test report shows the results of running the tests with the old backend. Cocurrent and Countercurrent heat exchanger models are tested with size up to 1280 volumes. Code generation is O(N) so it grows up to about 16 seconds for the largest two cases, but the simulation time remains under two seconds. For example, the simulation of ScalableTestSuite.Thermal.HeatExchanger.ScaledExperiments.CounterCurrentHeatExchangerEquations_N_1280 has these solver statistics:

LOG_STATS         | info    | ### STATISTICS ###
|                 | |       | | timer
|                 | |       | | |     0.124837s          reading init.xml
|                 | |       | | |     0.010039s          reading info.xml
|                 | |       | | |   0.00126439s [  0.1%] pre-initialization
|                 | |       | | |   0.00114645s [  0.1%] initialization
|                 | |       | | |   0.00019212s [  0.0%] steps
|                 | |       | | |     0.460836s [ 26.9%] solver (excl. callbacks)
|                 | |       | | |    0.0696473s [  4.1%] creating output-file
|                 | |       | | |   0.00107971s [  0.1%] event-handling
|                 | |       | | |   0.00173401s [  0.1%] overhead
|                 | |       | | |      1.17655s [ 68.7%] simulation
|                 | |       | | |      1.71245s [100.0%] total
|                 | |       | | events
|                 | |       | | |     2 state events
|                 | |       | | |     0 time events
|                 | |       | | solver: ida
|                 | |       | | |  2424 steps taken
|                 | |       | | |  2904 calls of functionODE
|                 | |       | | |    79 evaluations of jacobian
|                 | |       | | |     6 error test failures
|                 | |       | | |     0 convergence test failures
|                 | |       | | | 0.0532971s time of jacobian evaluation

Now, if you check the ScalableTestSuite_NB test report, obtained with the NB, the same models show 10X lower code generation times, but much large simulation times. For example, the simulaiton statitstics of ScalableTestSuite.Thermal.HeatExchanger.ScaledExperiments.CounterCurrentHeatExchangerEquations_N_1280 now is:

LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_STATS         | info    | ### STATISTICS ###
|                 | |       | | timer
|                 | |       | | |     0.114485s          reading init.xml
|                 | |       | | |   0.00256166s          reading info.xml
|                 | |       | | |   0.00139364s [  0.0%] pre-initialization
|                 | |       | | |  0.000279344s [  0.0%] initialization
|                 | |       | | |    0.0165049s [  0.0%] steps
|                 | |       | | |      97.5727s [100.5%] solver (excl. callbacks)
|                 | |       | | |    0.0723463s [  0.1%] creating output-file
|                 | |       | | |   0.00011335s [  0.0%] event-handling
|                 | |       | | |  0.000517808s [  0.0%] overhead
|                 | |       | | |    -0.621314s [ -0.6%] simulation
|                 | |       | | |      97.0425s [100.0%] total
|                 | |       | | events
|                 | |       | | |     0 state events
|                 | |       | | |     2 time events
|                 | |       | | solver: ida
|                 | |       | | | 118113 steps taken
|                 | |       | | | 247454 calls of functionODE
|                 | |       | | | 174740 evaluations of jacobian
|                 | |       | | |    10 error test failures
|                 | |       | | | 58088 convergence test failures
|                 | |       | | | 54.5186s time of jacobian evaluation

Clearly, the Jacobian used by IDA is grossly wrong, which causes a lot of convergence test failures and forces the solver to take very short steps in order to achieve convergence when solving the BDF formula.

casella commented 1 month ago

Here is a MWE:

model Test
  extends ScalableTestSuite.Thermal.HeatExchanger.ScaledExperiments.CounterCurrentHeatExchangerEquations_N_10(N = 2);
annotation(
    __OpenModelica_simulationFlags(lv = "LOG_STDOUT,LOG_ASSERT,LOG_JAC,LOG_STATS", s = "dassl", variableFilter = ".*"));
end Test;

The system has three states: TAtilde[1], TBtilde[1], TW[1]. The correct ODE jacobian at the end of the simulation computed by the OB-generated code is:

-10.2467 0 1.90476 
0 -11.4752 4.7619 
1 2.5 -14.0943 

while the Jacobian computed by the NB-generated code is

-34.8256 0 0 
0 -34.8256 0 
0 0 -34.8256 

which is obviously plain wrong.

If I try to turn on --generateSymbolicJacobian with the NB, the simulation gives the following error:

Process crashed Simulation process failed. Exited with code 0xffffffffc0000005.
Failed to parse Test_info.json
Expected 45 equations, but only found 32 equations.
Process crashed

@phannebohm, @kabdelhak can you please investigate? Thanks!

casella commented 1 week ago

This is probably another low-hanging fruit. Checking what goes wrong in this very simple examples with three states should be easy. Maybe that's an easy fix, and it's going to solve other problems.