open-ideas / IDEAS

Modelica library allowing simultaneous transient simulation of thermal and electrical systems at both building and feeder level.
131 stars 52 forks source link

CPU-aimed optimisation #295

Closed rubenbaetens closed 9 years ago

rubenbaetens commented 9 years ago

This issue (and related branch) will be used to evaluate #152, #289, #284 and #282 for cpu-time.

rubenbaetens commented 9 years ago

All tests are performed with Radau IIa and 1e-6 tolerance in Dymola 2015 FD01 on a Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz with 16.0 GB RAM on a 64-bit Operating System (i.e. Windows). The simulation time is 1 year, the asked output resolution is an equidistant 600 seconds.

rubenbaetens commented 9 years ago

@Mathadon in the attempt to run my reference model with #289, i get the error

Failed in sorting the initialization equations.
Please, contact support.

when starting a simulation. You experienced a similar problem once in this branch ?

rubenbaetens commented 9 years ago

I started with InDiCES.TestBed.Numerics.SingleBuilding, a two-zone dwelling with hydronic heating system, central thermostat in zone one and thermostatic radiator valves in zone two.

The reference model and results is current develop-branch, denoting

Translated Model
Constants: 4143 scalars
Free parameters: 2108 scalars
Parameter depending: 3366 scalars
Continuous time states: 100 scalars
Time-varying variables: 1654 scalars
Alias variables: 3178 scalars
Assumed default initial conditions: 86
Number of mixed real/discrete systems of equations: 0
Sizes of linear systems of equations: {2, 2, 2, 2, 7, 7, 7, 7, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 7, 7, 7, 7, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8}
Sizes after manipulation of the linear systems: {0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}
Sizes of nonlinear systems of equations: {8, 102, 95, 1, 1}
Sizes after manipulation of the nonlinear systems: {2, 15, 14, 0, 0}
Number of numerical Jacobians: 0

with a log-file denoting

Integration terminated successfully at T = 3.1536e+007
   CPU time for integration                  : 871.056 seconds
   Number of result points                   : 86328
   Number of grid points                     : 52559
   Number of (successful) steps              : 152422
   Number of rejected steps                  : 59896
   Number of F-evaluations                   : 2558651
   Number of crossing function evaluations   : 615377
   Number of Jacobian-evaluations            : 83469
   Number of (model) time events             : 0
   Number of state    events                 : 16885
   Number of step     events                 : 0
rubenbaetens commented 9 years ago

The results for the issue152_groupedEnvironmentCalculations do not show a significant improvement for the single-building model:

Translated Model
Constants: 4676 scalars
Free parameters: 2100 scalars
Parameter depending: 3177 scalars
Continuous time states: 100 scalars
Time-varying variables: 1721 scalars
Alias variables: 6464 scalars
Assumed default initial conditions: 86
Number of mixed real/discrete systems of equations: 0
Sizes of linear systems of equations: {7, 7, 7, 7, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 4, 2, 2, 2, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 8}
Sizes after manipulation of the linear systems: {2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}
Sizes of nonlinear systems of equations: {1, 95, 102, 1, 8}
Sizes after manipulation of the nonlinear systems: {0, 14, 15, 0, 2}
Number of numerical Jacobians: 0

with a log-file denoting

Integration terminated successfully at T = 3.1536e+007
   CPU time for integration                  : 854.842 seconds
   Number of result points                   : 86290
   Number of grid points                     : 52559
   Number of (successful) steps              : 152350
   Number of rejected steps                  : 59808
   Number of F-evaluations                   : 2556185
   Number of crossing function evaluations   : 614987
   Number of Jacobian-evaluations            : 83358
   Number of (model) time events             : 0
   Number of state    events                 : 16865
   Number of step     events                 : 0

if we would see this relative to the develop-branch, this gives

Integration terminated successfully at T = 3.1536e+007
   CPU time for integration                  : -1.86 %
   Number of result points                   : -0.04 %
   Number of grid points                     : 0
   Number of (successful) steps              : -0.05 %
   Number of rejected steps                  : -0.15 %
   Number of F-evaluations                   : -0.10 %
   Number of crossing function evaluations   : -0.06 %
   Number of Jacobian-evaluations            : -0.13 %
   Number of (model) time events             : 0
   Number of state    events                 : -0.12 %
   Number of step     events                 : 0
rubenbaetens commented 9 years ago

@Mathadon concerning the error in #289, i also get following error or warning which might cause the error denoting the inability to sort th initialization equations:

The iteration variable building.building.dayzone.propsBus[4].surfRad.T has been selected to have the guess value 289.15.
However, the start value has been selected from a set of alias variables having conflicting start values of the same precedence.
289.15, the start value of building.building.int_wall[1].layMul.port_b.T given as 289.15.
293.15, the start value of building.building.int_wall[1].intCon_a.port_a.T given as 293.15.
Mathadon commented 9 years ago

For the error, can you try removing the initial equation in IDEAS.Buildings.Components.BaseClasses.MonoLayerOpaqueNf?

I did expect a larger speed increase for these models. However, judging from the non-linear system size I expect that they dominate the computation time so that's probably the cause why other changes do not help much.

Mathadon commented 9 years ago

Can you check which systems dominate computation time by settings Advanced.GenerateBlockTimers=true?

rubenbaetens commented 9 years ago

The results for the issue issue289_layStateSurface do show a significant improvement for the single-building model:

Translated Model
Constants: 4143 scalars
Free parameters: 2108 scalars
Parameter depending: 3366 scalars
Continuous time states: 90 scalars
Time-varying variables: 1612 scalars
Alias variables: 3174 scalars
Assumed default initial conditions: 76
Number of mixed real/discrete systems of equations: 0
Sizes of linear systems of equations: {2, 2, 2, 7, 7, 7, 7, 2, 2, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 8}
Sizes after manipulation of the linear systems: {0, 0, 0, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}
Sizes of nonlinear systems of equations: {8, 75, 1, 92, 1}
Sizes after manipulation of the nonlinear systems: {2, 10, 0, 13, 0}
Number of numerical Jacobians: 0

you can clearly see the set of linear equations changing, summarized as

## develop
Sizes of linear systems of equations: {52 of them, max 8}
## states on surface
Sizes of linear systems of equations: {32 of them, max 8}

and the non-linear equations changing, summarized as

## develop
Sizes of nonlinear systems of equations: {1, 95, 102, 1, 8}
Sizes after manipulation of the nonlinear systems: {0, 14, 15, 0, 2}
## states on surface
Sizes of nonlinear systems of equations: {8, 75, 1, 92, 1}
Sizes after manipulation of the nonlinear systems: {2, 10, 0, 13, 0}

with a log file denoting

Integration terminated successfully at T = 3.1536e+007
   CPU time for integration                  : 704.832 seconds
   Number of result points                   : 83936
   Number of grid points                     : 52559
   Number of (successful) steps              : 141516
   Number of rejected steps                  : 54659
   Number of F-evaluations                   : 2384981
   Number of crossing function evaluations   : 571748
   Number of Jacobian-evaluations            : 79218
   Number of (model) time events             : 0
   Number of state    events                 : 15688
   Number of step     events                 : 0

if we would see this relative to the develop-branch, this gives

Integration terminated successfully at T = 3.1536e+007
   CPU time for integration                  : -19.08 %
   Number of result points                   : -2.77 %
   Number of grid points                     : 0
   Number of (successful) steps              : -7.16 %
   Number of rejected steps                  : -8.74 %
   Number of F-evaluations                   : -6.78 %
   Number of crossing function evaluations   : -7.09 %
   Number of Jacobian-evaluations            : -5.09 %
   Number of (model) time events             : 0
   Number of state    events                 : -7.09 %
   Number of step     events                 : 0

The changing number of state events do show that my result is slightly different of course. I should check if my energy consumption didn't change significantly.

Mathadon commented 9 years ago

Okay, in this case a possible explanation would be that you have very small systems to start from (lumping walls into 1?) so then these changes do not cause any efficiency gains. However in my models I have 16 surfaces connected to a zone and then the change is more noticeable.

rubenbaetens commented 9 years ago

@Mathadon i don't lump walls into 1 here.

rubenbaetens commented 9 years ago

@Mathadon you can clearly see issue289_layStateSurface adds thermal mass to a zone compared to develop. It's consistent that when develop switches the heat pump on 10 times, issue289 switches the heat pump on 9 times in exactly the same period, but the durations of the on state are about 10% longer resulting in a comparable energy consumption. It could also mean my -19 % cpu gain is mainly due to the -7 % in number of state events. I'll check with Advanced.GenerateBlockTimers=true if this is true.

rubenbaetens commented 9 years ago

@Mathadon with issue289_layStateSurface i get following

  Limit stepsize, Dominate error, Exceeds 10% of error Component (#number)
  54672 12297 51996 building.heatingSystem.pump.flowRegulator.filter.x[1] (#  1) 
  55643 47016 58250 building.heatingSystem.pump.flowRegulator.filter.x[2] (#  2) 
  48194  4279 45442 building.heatingSystem.hP_AirWater.Tin.T (#  3) 
  66819 16693 62690 building.heatingSystem.hP_AirWater.TOut.T (#  4) 
  56712 24273 58026 building.heatingSystem.floCtrl_2.senTemSup.T (#  5) 
  44245 12505 33116 building.heatingSystem.floCtrl_2.senTemRet.T (#  6) 
  62178 12789 59921 building.heatingSystem.floCtrl_1.senTemSup.T (#  9) 
  51912  7656 39233 building.heatingSystem.floCtrl_1.senTemRet.T (# 10) 
  59545     0 48792 building.heatingSystem.pump.flowRegulator.vol.dynBal.medium.T (# 71) 
  49668     0 42348 building.heatingSystem.hP_AirWater.pipe_HeatPort.vol.dynBal.medium.T (# 72) 
  49812  1374 39538 building.heatingSystem.emi_1.vol[1].dynBal.medium.T (# 73) 
  43954    90 37103 building.heatingSystem.emi_1.vol[2].dynBal.medium.T (# 74) 
  39609   717 35846 building.heatingSystem.emi_1.vol[3].dynBal.medium.T (# 75) 
  36187   676 31045 building.heatingSystem.emi_1.vol[4].dynBal.medium.T (# 76) 
  35227     2 33513 building.heatingSystem.emi_1.vol[5].dynBal.medium.T (# 77) 
  45844  8384 36764 building.heatingSystem.emi_2.vol[1].dynBal.medium.T (# 78) 
  39683  1137 35978 building.heatingSystem.emi_2.vol[2].dynBal.medium.T (# 79) 
  35880  1086 34040 building.heatingSystem.emi_2.vol[3].dynBal.medium.T (# 80) 
  33001   225 31432 building.heatingSystem.emi_2.vol[4].dynBal.medium.T (# 81) 
  29932     0 31537 building.heatingSystem.emi_2.vol[5].dynBal.medium.T (# 82) 
  53534     0 47572 building.heatingSystem.coll_1.spl_supply.vol.dynBal.medium.T (# 87) 
  36017     4 31762 building.heatingSystem.coll_1.spl_return.vol.dynBal.medium.T (# 88) 
  50975     3 46902 building.heatingSystem.coll_2.spl_supply.vol.dynBal.medium.T (# 89) 
  30425   319 27119 building.heatingSystem.coll_2.spl_return.vol.dynBal.medium.T (# 90) 

and also

Name of block,  Block, Total CPU[s], Mean[us]    ( Min[us]    to Max[us]    )    
Entire model:       0,      555.243,       85.68 (       0.00 to   115000.00),  6480470
Event handling:     1,        2.874,       85.97 (       0.00 to   115000.00),    33430
Outside of model:   3,      166.049,       25.62 (       0.00 to    55000.00),  6480469
DynamicsSection:   34,      505.123,       77.95 (       0.00 to     2000.00),  6480470
Dynamics code:     35,        8.601,        1.33 (       0.00 to     2000.00),  6480470
Nonlin sys(2):     36,       33.390,        5.15 (       0.00 to     2000.00),  6480470
Dynamics code:     37,       36.559,        5.64 (       0.00 to     2000.00),  6480470
Nonlin sys(10):    38,      106.059,       16.37 (       0.00 to     2000.00),  6480470
Nonlin sys(13):    56,      152.674,       23.56 (       0.00 to     2000.00),  6480470

So it seems to me here that my non-linear system is dominant, not my event-handling, or am i wrong in reading it this way ?

rubenbaetens commented 9 years ago

@Mathadon after all, i got it down to the finding that 41% of my cpu-time is event-handling and the remaining 59% is my model of which 48%-pt is the dynamic section.

The Whalhalla-solution would be a hysteresis function that doesn't generate state-events though gives the correct solution.

Mathadon commented 9 years ago

Did you try turning off all events (ie disable heating system controller) for finding this figure? Otherwise I’d be interested to see that result as well!

rubenbaetens commented 9 years ago

@Mathadon Well, no: Given that Advanced.GenerateBlockTimers=true there is a line that says Outside of model block 3. If I looked for that in dsmodel.c, i found it was the time required for modelica in the model once the event was detected (if i understood it correctly).

Mathadon commented 9 years ago

Oh, I never looked into that, interesting.

However do note that due to events the number of time steps increases so the total amount of overhead caused by the events is more than just this block.

rubenbaetens commented 9 years ago

@Mathadon is there a way to see what exactly caused your event at a certain time ? I have a few which i can't seem to explain based on my hysteresis function.

capture

Mathadon commented 9 years ago

In options in tab ‘Debug’ enable ‘Events during simulation'

rubenbaetens commented 9 years ago

Of course, shame on me.

rubenbaetens commented 9 years ago

@Mathadon ok, so ALL my events are hysteresis functions, i.e. one for my setpoint temperature of my room and one in the heatpump to check the modulation.

rubenbaetens commented 9 years ago

@Mathadon i think this Outside of model block 3 in Advanced.GenerateBlockTimers=true means something else: If i do't ask my solver to output at 600 seconds but only at 2 hours (equidistant and including events), my simulation time drops by 32%.

Mathadon commented 9 years ago

And how much of the ‘outside of block’ remains in this case? Because it may be both. Can you try outputting only 1 time step?

rubenbaetens commented 9 years ago

@Mathadon mm, the ratio remains 40% of the complete time.