public void TestCheckPoints() { var context = new PhysicalContext <int>(0.5, 1); context.AddEntity(0, new PointMass()); context.Tick(); ThrowsException <ArgumentOutOfRangeException>(() => new ContextProgressTracker <int>(context, 10, 0)); ThrowsException <ArgumentOutOfRangeException>(() => new ContextProgressTracker <int>(context, 10, 12)); context.Tick(); var trackerInt = new ContextProgressTracker <int>(context, 10, 3, 9); var trackerDouble = ContextProgressTracker <int> .FromTime(context, 10, 3, 9); int reachedInt = 0, reachedDouble = 0; trackerInt.OnCheckPoint += (c, _) => reachedInt++; trackerDouble.OnCheckPoint += (c, _) => reachedDouble++; for (int i = 0; i < 12; i++) { context.Tick(); } AreEqual(2, reachedInt); AreEqual(1, reachedDouble); }
public void TestGoal() { var context = new PhysicalContext <int>(1, 1); context.AddEntity(0, new PointMass()); context.Tick(); context.Tick(); var tracker = new ContextProgressTracker <int>(context, 10); AreEqual(0, tracker.Progress); bool goal = false; tracker.OnGoal += (c, e) => goal = true; context.Tick(4); AreEqual(0.5, tracker.Progress); context.Tick(4); AreEqual(1, tracker.Progress); IsFalse(tracker.IsActive); IsTrue(goal); }
private async void EvalButton_Click(object sender, RoutedEventArgs e) { try { SetPanelEnablity(false); _isCancelationRequested = false; var k = double.Parse(kValBox.Text); var alpha = double.Parse(alphaValueBox.Text); var dt = double.Parse(dtBox.Text); var timeSpan = double.Parse(timeSpanBox.Text); var planetRadius = double.Parse(planetRadiusBox.Text); var interval = ulong.Parse(renderingIntervalBox.Text); var progressBarSteps = uint.Parse(progressBarStepsBox.Text); var context = new PhysicalContext <Entities>(timePerTick: dt, capacity: 2); context.AddEntity ( Entities.Planet, new PointMass(0, 0, 0, 1) ); context.AddEntity ( Entities.Ship, new PointMass ( new AxisStatus(double.Parse(xPosBox.Text), double.Parse(xVelBox.Text)), new AxisStatus(double.Parse(yPosBox.Text), double.Parse(yVelBox.Text)), new AxisStatus(), 1 ), c => ModifiedGravityLaw <Entities>(c[Entities.Ship], c[Entities.Planet], alpha, k) ); var xTracker = new ContextTracker <Entities, double>(context, c => c[Entities.Ship].X.Position, interval); var yTracker = new ContextTracker <Entities, double>(context, c => c[Entities.Ship].Y.Position, interval); var progress = ContextProgressTracker <Entities> .FromTime ( context, timeSpan, Enumerable.Range(1, (int)progressBarSteps).Select(i => timeSpan *i / progressBarSteps).ToArray() ); progress.OnCheckPoint += (c, _) => Dispatcher.Invoke(() => progressBar.Value = progress.Progress); bool isSuccesful = await Task.Run ( () => context.Tick ( timeSpan, c => !_isCancelationRequested && ( c[Entities.Ship].X.Position *c[Entities.Ship].X.Position + c[Entities.Ship].Y.Position *c[Entities.Ship].Y.Position >= planetRadius *planetRadius ), false ) ); if (!isSuccesful && !_isCancelationRequested) { Task.Run(() => MessageBox.Show($"Ship has crashed at {context.Timer}")); } var title = $"Start position = ({xPosBox.Text}; {yPosBox.Text})\n" + $"Start velocity = ({xVelBox.Text}; {yVelBox.Text})\n" + $"dt = {dtBox.Text}, K = {kValBox.Text}, Alpha = {alphaValueBox.Text}"; var xSeries = new LineSeries() { CanTrackerInterpolatePoints = false, Title = title }; var ySeries = new LineSeries() { CanTrackerInterpolatePoints = false, Title = title }; var orbitSeries = new LineSeries() { CanTrackerInterpolatePoints = false, Title = title }; xSeries.Points.AddRange(from p in xTracker select new DataPoint(p.Key * dt, p.Value)); ySeries.Points.AddRange(from p in yTracker select new DataPoint(p.Key * dt, p.Value)); orbitSeries.Points.AddRange(xTracker.Zip(yTracker, (x, y) => new DataPoint(x.Value, y.Value))); plotX.Model.Series.Add(xSeries); plotY.Model.Series.Add(ySeries); orbitPlot.Model.Series.Add(orbitSeries); orbitPlot.Model.PlotType = PlotType.Cartesian; plotX.InvalidatePlot(); plotY.InvalidatePlot(); orbitPlot.InvalidatePlot(); progressBar.Value = 0; SetPanelEnablity(true); } catch (Exception ex) { MessageBox.Show(ex.Message); } }