private double UpdateState(
            double tPrev,
            double t,
            Vector <double> xPrev,
            Vector <double> x,
            Dictionary <string, int> variableMap,
            ref Equations.DAE.Implicit.DAEIAnalytic system)
        {
            double dt = t - tPrev;

            //for each event interpolate values and update stuff
            while (events.Count > 0)
            {
                TransientEvent _event = events.First <TransientEvent>();
                if (_event.Time > t)
                {
                    return(t);
                }
#if TEST || DEBUG
                if (_event.Time < tPrev)
                {
                    throw new Exception("UpdateState exception");
                }
#endif
                events.RemoveAt(0);
                Vector <double> xCurrent     = MathUtils.MathUtils.Interpolate(xPrev, x, (_event.Time - tPrev) / dt);
                TransientState  currentState = new TransientState(xCurrent, variableMap);
                if (_event.Execute(currentState))
                {
                    system.UpdateParameters(_event.GetParameters());
                    double eventTime = _event.Time;
                    while (events.Count > 0)
                    {
                        _event = events.First <TransientEvent>();
                        if (_event.Time > eventTime)
                        {
                            break;
                        }
                        events.RemoveAt(0);
                        if (_event.Execute(currentState))
                        {
                            system.UpdateParameters(_event.GetParameters());
                        }
                    }
                    stateChangedCount++;
                    return(eventTime);
                }
            }
            return(t);
        }
 public abstract bool Execute(TransientState stateValues);
        private TransientSolution Solve(string equations)
        {
            stateChangedCount = 0;
            List <IScopeElement> scopeElements = new List <IScopeElement>();

            foreach (var element in elements)
            {
                if (element is IScopeElement)
                {
                    scopeElements.Add(element as IScopeElement);
                }
            }
            Equations.DAE.Compiler compiler = new Equations.DAE.Compiler();
            Equations.DAE.Implicit.DAEIDescription compiledEquation = compiler.CompileDAEImplicit(equations);
            double t = t0;

            Equations.DAE.Implicit.DAEIAnalytic system = new Equations.DAE.Implicit.DAEIAnalytic(compiledEquation);
            Vector <double> x = Vector <double> .Build.SparseOfArray(compiledEquation.InitialValues);

            List <string> variableNames = new List <string>();

            foreach (var scope in scopeElements)
            {
                variableNames.AddRange(scope.GetTransientVariableNames());
            }
            //generate events
            InitEvents(ref system);
            TransientSolution result             = new TransientSolution(variableNames.ToArray());
            int scopeVariableCount               = variableNames.Count;
            Dictionary <string, int> variableMap = compiledEquation.GetVariableDictionary();

            {
                TransientState currentState  = new TransientState(x, variableMap);
                List <double>  currentValues = new List <double>();
                foreach (var scopeElement in scopeElements)
                {
                    currentValues.AddRange(scopeElement.GetReading(currentState));
                }
                result.AddPoint(currentValues.ToArray(), t);
            }
            while (t < t1 && maxPoints > (scopeVariableCount + 1) * result.GetPointCount())
            {
                Vector <double> xPrev = x;
                double          tPrev = t;
                //make step
                x  = solver.IntegrateStep(system, x, t);
                t += solver.Step;
                //update t
                if (events.Count != 0)
                {
                    //if state happened during [tprev,tcurr]
                    t = UpdateState(tPrev, t, xPrev, x, variableMap, ref system);
                    x = MathUtils.MathUtils.Interpolate(xPrev, x, (t - tPrev) / solver.Step);
                }
                TransientState currentState  = new TransientState(x, variableMap);
                List <double>  currentValues = new List <double>();
                foreach (var scopeElement in scopeElements)
                {
                    currentValues.AddRange(scopeElement.GetReading(currentState));
                }
                result.AddPoint(currentValues.ToArray(), t);
            }
            result.SetStateChangedCount(stateChangedCount);
            return(result);
        }