public bool Check(string sourceCode)
        {
            var status = parser.TryParseProgram(sourceCode, out var prog, out var internalerror, out var position);


            if (!status)
            {
                _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage(internalerror, SolverStatus.Error));
            }
            else
            {
                if (prog.ClassDefinitions.Count == 1)
                {
                    var translator      = new ModelTranslatorV1();
                    var printer         = new ModelicaTreePrinter();
                    var pretty          = new ModelicaPrettyPrinter();
                    var instancePrinter = new InstancePrinter();
                    var flattening      = new Flattening();

                    var        astText    = printer.Transform(prog);
                    var        prettyTest = pretty.Transform(prog);
                    DAEProblem model      = null;

                    string prettyflat = "";

                    try
                    {
                        var flatModel = flattening.Transform(prog.ClassDefinitions.First());
                        prettyflat = instancePrinter.Transform(flatModel);
                        //model = translator.Translate(flatModel);
                    }
                    catch (Exception e)
                    {
                        _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage($"{e.Message}", SolverStatus.Error));
                        return(false);
                    }


                    _eventAggregator.PublishOnUIThreadAsync(new UpdateModelAnalysisMessage()
                    {
                        SyntaxTree = astText, FlattenedModel = prettyflat, CalculationModel = model
                    });
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage("Model OK", SolverStatus.OK));
                }
                else
                {
                    throw new InvalidOperationException("Multiple class definitions detected. Flattening can only resolve one class.");
                }
            }

            return(status);
        }
Example #2
0
        static void Simulate(string input, double step, double end)
        {
            var parser     = new ModelicaParser();
            var printer    = new ModelicaTreePrinter();
            var translator = new ModelTranslatorV1();

            var status = parser.TryParseProgram(input, out var prog, out var error, out var position);

            if (status)
            {
                var ast = printer.Transform(prog);
                Console.WriteLine(ast);
                var logger = new ColoredConsoleLogger();
                var model  = translator.Translate(prog.ClassDefinitions.Last());

                var integrator = new ImplicitEuler();
                integrator.Discretize(model);
                integrator.StepSize = step;
                integrator.EndTime  = end;
                model.Initialize(new NoLogger());

                Stopwatch w = new Stopwatch();
                w.Start();
                var results = integrator.Integrate(model, logger);
                w.Stop();
                Console.WriteLine("Integration took " + w.ElapsedMilliseconds + "ms");

                using (var sw = new StreamWriter(Environment.CurrentDirectory + "\\results.csv"))
                {
                    sw.WriteLine("time;" + string.Join("; ", model.AlgebraicVariables.Select(v => v.Name)));

                    foreach (var result in results)
                    {
                        sw.WriteLine(result.ToString());
                    }
                }
            }
            else
            {
                Console.WriteLine(error);
            }
        }
Example #3
0
        static void LotkaVolterraParsed()
        {
            string input =
                @"class LotkaVolterra
                    Real N1 = 10;
                    Real N2 = 5;
                    parameter Real e1 = 0.09;
                    parameter Real g1 = 0.01;
                    parameter Real e2 = 0.04;
                    parameter Real g2 = 0.01;
                    equation
                        der(N1) = N1*(e1-g1*N2);
                        der(N2) = -N2*(e2-g2*N1);                        
                 end LotkaVolterra;
                 ";

            var parser = new ModelicaParser();



            var printer    = new ModelicaTreePrinter();
            var translator = new ModelTranslatorV1();

            var status = parser.TryParseProgram(input, out var prog, out var error, out var position);

            if (status)
            {
                var ast = printer.Transform(prog);
                // Console.WriteLine(ast);
                var logger = new NoLogger();
                var model  = translator.Translate(prog.ClassDefinitions.Last());

                var integrator = new ExplicitEuler();
                integrator.Discretize(model);
                integrator.StepSize = 1;
                integrator.EndTime  = 800;
                model.Initialize(new NoLogger());

                Stopwatch w = new Stopwatch();
                w.Start();
                var results = integrator.Integrate(model, logger);
                w.Stop();
                Console.WriteLine("Integration took " + w.ElapsedMilliseconds + "ms");

                using (var sw = new StreamWriter(Environment.CurrentDirectory + "\\results.csv"))
                {
                    sw.WriteLine("time;" + string.Join("; ", model.AlgebraicVariables.Select(v => v.Name)));

                    foreach (var result in results)
                    {
                        sw.WriteLine(result.ToString());
                        // Console.WriteLine(result.ToString());
                    }
                }
            }
            else
            {
                Console.WriteLine(error);
            }
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //}
        }
Example #4
0
        void Test(ModelicaTestElement node)
        {
            if (node.ElementType == ModelicaTestElementType.Folder)
            {
                foreach (var child in node.Children)
                {
                    Test(child);
                }

                node.TotalSubelements = node.Children.Sum(n => n.TotalSubelements);

                if (node.Children.Count > 0 && node.TotalSubelements > 0)
                {
                    node.PercentParsed     = node.Children.Sum(n => n.PercentParsed * n.TotalSubelements) / node.TotalSubelements;
                    node.PercentTranslated = node.Children.Sum(n => n.PercentTranslated * n.TotalSubelements) / node.TotalSubelements;
                    node.PercentSolved     = node.Children.Sum(n => n.PercentSolved * n.TotalSubelements) / node.TotalSubelements;
                }
                else
                {
                    node.PercentParsed     = 1.0;
                    node.PercentTranslated = 1.0;
                    node.PercentSolved     = 1.0;
                }
            }
            else
            {
                node.TotalSubelements = 1;
                var input  = File.ReadAllText(node.Path);
                var parser = new ModelicaParser();
                try
                {
                    var status = parser.TryParseProgram(input, out var prog, out var error, out var position);
                    if (status)
                    {
                        node.PercentParsed = 1.0;
                        var translator = new ModelTranslatorV1();

                        var model = translator.Translate(prog.ClassDefinitions.Last());
                        node.PercentTranslated = 1.0;

                        var integrator = new ImplicitEuler();
                        integrator.StepSize = 0.1;
                        integrator.EndTime  = 0.1;
                        var logger = new NoLogger();
                        integrator.Discretize(model);
                        model.Initialize(logger);
                        var results = integrator.Integrate(model, logger);
                        node.PercentSolved = 1.0;
                    }
                    else
                    {
                        Console.WriteLine($"Error parsing file {node.Path}");
                        Console.WriteLine(error);
                        Console.WriteLine();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        public bool Simulate(string sourceCode)
        {
            var status = parser.TryParseProgram(sourceCode, out var prog, out var error, out var position);

            if (status)
            {
                var translator = new ModelTranslatorV1();
                var flattening = new Flattening();

                var ast = prog.ClassDefinitions.Last();

                var instance = flattening.Transform(ast);

                DAEProblem model = null;

                try
                {
                    model = translator.Translate(instance);
                }
                catch (Exception e)
                {
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage($"{e.Message}", SolverStatus.Error));
                }



                var integrator = createIntegratorFromAnnotation(ast);

                integrator.StepSize     = 0.1;
                integrator.EndTime      = 1;
                integrator.OnIteration += (i) => OnIteration(integrator, i);

                updateIntegratorSettings(ast, integrator);

                var logger = new NoLogger();

                try
                {
                    integrator.Discretize(model);
                    model.Initialize(logger);
                }
                catch (Exception e)
                {
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage($"{e.Message}", SolverStatus.Error));
                    return(false);
                }


                if (model.SystemToSolve.NumberOfEquations != model.SystemToSolve.NumberOfVariables)
                {
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage($"System not square! V={model.SystemToSolve.NumberOfVariables}, E={model.SystemToSolve.NumberOfEquations}", SolverStatus.Error));
                    return(false);
                }

                Stopwatch w = new Stopwatch();
                _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage("Integration started", SolverStatus.Busy));

                try
                {
                    w.Start();
                    var results = integrator.Integrate(model, logger);
                    w.Stop();
                    Console.WriteLine("Integration took " + w.ElapsedMilliseconds + "ms");
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateModelResultMessage()
                    {
                        ModelName = model.Name, AlgebraicStates = model.AlgebraicVariables, DifferentialStates = model.DifferentialVariables, TimeSteps = results
                    });
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage($"Integration finished ({w.ElapsedMilliseconds} ms)", SolverStatus.OK));
                }
                catch (Exception e)
                {
                    _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage($"{e.Message}", SolverStatus.Error));
                    return(false);
                }
            }
            else
            {
                _eventAggregator.PublishOnUIThreadAsync(new UpdateSolverStatusMessage(error, SolverStatus.Error));
            }


            return(true);
        }