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); }
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); }