Esempio n. 1
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);
            }
        }
        IIntegrator createIntegratorFromAnnotation(ClassDefinition ast)
        {
            IIntegrator integrator = new BDF2();

            var annot = ast.Elements.FirstOrDefault(e => e is Annotation) as Annotation;

            if (annot != null)
            {
                var experiment = annot.Modification.Modifications.FirstOrDefault(m => m.Reference.ID == "experiment");
                if (experiment != null && experiment.Modification != null)
                {
                    foreach (var mod in experiment.Modification.Modifications)
                    {
                        if (mod.Reference.ID == "Integrator")
                        {
                            switch (mod.Modification.Value.ToString().Replace("\"", ""))
                            {
                            case "BDF1":
                                integrator = new ImplicitEuler();
                                break;

                            case "BDF2":
                                integrator = new BDF2();
                                break;

                            case "BDF2A":
                                integrator = new BDF2A();
                                break;
                            }
                        }

                        if (mod.Reference.ID == "Solver")
                        {
                            integrator.UseDulmageMendelsohnSolver = mod.Modification.Value.ToString() == "DMD";
                        }
                    }
                }
            }

            return(integrator);
        }
Esempio n. 3
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);
                }
            }
        }