コード例 #1
0
        static void LoktaVolterraHardoded()
        {
            var logger     = new NoLogger();
            var model      = new DAEProblem();
            var integrator = new ExplicitEuler();

            integrator.StepSize = 0.1;
            integrator.EndTime  = 200;

            var N1  = new Variable("N1", 5);
            var N2  = new Variable("N2", 10);
            var dN1 = new Variable("dN1dt", 0);
            var dN2 = new Variable("dN2dt", 0);
            var e1  = new Variable("e1", 0.09);
            var g1  = new Variable("g1", 0.01);
            var e2  = new Variable("e2", 0.04);
            var g2  = new Variable("g2", 0.01);

            var eq1 = new Equation(N1 * (e1 - g1 * N2) - dN1);
            var eq2 = new Equation(-N2 * (e2 - g2 * N1) - dN2);


            model.AlgebraicVariables.Add(N1);
            model.AlgebraicVariables.Add(N2);
            model.DifferentialVariables.Add(dN1);
            model.DifferentialVariables.Add(dN2);
            model.Equations.Add(eq1);
            model.Equations.Add(eq2);
            model.Mapping.Add(dN1, N1);
            model.Mapping.Add(dN2, N2);
            model.Initialize(new NoLogger());

            var results = integrator.Integrate(model, logger);

            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());
                }
            }
        }
コード例 #2
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);
            //}
        }