﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
4006	SimCode scales as O(N^2) in models with many when clauses and events	Francesco Casella	Willi Braun	"Consider these two models from the ScalableTestSuite
{{{
model ManyEvents ""Model with many events in when clauses""
  parameter Integer N;
  Real x[N](each start = 0, each fixed = true);
  Boolean e[N](each start = false, each fixed = true);
equation
  for i in 1:N loop
	der(x[i]) = 1/i;
	when x[i] > 1 then
	  e[i] = true;
	end when;
  end for;
end ManyEvents;

model ManyEventsManyConditions ""Model with many events in when clauses and a when clause with many triggering conditions""
  parameter Integer N;
  Real x[N](each start = 0, each fixed = true);
  Boolean e[N](each start = false, each fixed = true);
  Integer v(start = 0, fixed = true);
equation
  for i in 1:N loop
	der(x[i]) = 1/i;
	when x[i] > 1 then
	  e[i] = true;
	end when;
  end for;
  when e then
	v = pre(v) + 1;
  end when;
end ManyEventsManyConditions;
}}}

It is apparent that the second one has one additional scalar equation, whose when clause is triggered by a vector {{{e}}} with {{{N}}} components.

This is the performance of SimCode: create system equations on my pc for the first model
||=N=||=Time [s]=||
||1000|| 4.3||
||2000|| 18.0||
||4000|| 87.6||
which scales as O(N^2.5^). For the second model, instead, I get:
||=N=||=Time [s]=||
||1000|| 0.02||
||2000|| 0.05||
||4000|| 0.14||
which seems quite weird, as a model with one additional equation takes two order of magnitudes less time. 

This actually happens at the expense of the time spent by {{{encapsulateWhenConditions}}}, see #4005.

Is there a way to avoid the blow up with N of the combined SimCode and encapsulateWhenConditions execution time?"	defect	assigned	normal		Backend				Willi Braun
