Ejemplo n.º 1
0
        public async Task RunBatterySimulation()
        {
            // arrange
            var runtime = new RuntimeController(new EnergyValidator());

            // TODO: seem to have a bug when setting up a higher cutoff voltage --> test and fix it
            _battery.CutoffVoltage = 1.2F;
            var s           = _battery.State;
            var batteryArgs = new BatteryArgs();

            batteryArgs.Batteries.Add(_battery);

            var netArgs = new NetworkArgs();
            var device  = new SimpleDevice();

            device.Parts.Add(_battery);
            netArgs.Network.Add(device);

            var batterySim = new BatteryPackSimulator();

            batterySim
            .With(batteryArgs)
            .With(netArgs)
            .With(runtime.Arguments);

            var repo = new SimulatorRepository();

            repo.Add(batterySim);
            if (!runtime.BindSimulators(repo).Validate())
            {
                Assert.Fail("error on validating the simulation");
            }

            // act
            await runtime.RunAsync((rt) =>
            {
                // assert
                return(!_battery.State.IsDepleted);
            });

            Assert.AreNotEqual(s.Initial.ElapsedTime, s.Now.ElapsedTime, "ElapsedTime should not be equal");
            Assert.AreNotEqual(s.Initial.Charge, s.Now.Charge, "Charge should not be equal");
            Assert.AreNotEqual(s.Initial.SoD, s.Now.SoD, "SoD should not be equal");
            Assert.AreNotEqual(s.Initial.Voltage, s.Now.Voltage, "Voltage should not be equal");
            Assert.IsTrue(_battery.State.IsDepleted, "battery should be empty");
        }
Ejemplo n.º 2
0
        public async Task RunNetwork()
        {
            // arrange
            var runtime = new RuntimeController(new BasicValidator());


            var netSim  = new PeerToPeerNetworkSimulator(runtime);
            var netArgs = new NetworkArgs();

            netSim.OnExecuting += (o, e) =>
            {
                _network.AssociationMatrix.Each(ApplyAssociations);
            };
            netSim.With(netArgs);
            _network = netArgs.Network;
            _network.AddRange(ImportDevices().First());

            var simRepo = new SimulatorRepository();

            simRepo.Add(netSim);


            var pass      = 0;
            var maxPasses = 3;

            runtime.IterationPassed += (o, e) =>
            {
                pass++;
                if (pass == maxPasses)
                {
                    var dev = netArgs.Network.Items[0];
                    dev.Controls.Off();
                    _log.Trace($"Device '{dev.Name}' was turned off.");
                }
            };
            runtime.BindSimulators(simRepo)
            .Validate();

            // act iterations until availability is below 100%
            await runtime.RunAsync((r) =>
            {
                if (netArgs.Network.Availability() < 1)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            });

            // assert
            Assert.IsTrue(pass >= maxPasses, $"The iterations should be more than {maxPasses}.");
            netArgs.Network.DistanceMatrix.Each((r, c, v) =>
            {
                Assert.IsTrue(v > 0, $"position at row '{r}' and col '{c}' should not be '{v}'");
                _log.Trace($"{r}:{c} -> distance: {v}");
                return(v);
            });

            netArgs.Network.AngleMatrix.Each((r, c, v) =>
            {
                Assert.IsTrue(!float.IsNaN(v.Azimuth), $"Azimuth at position at row '{r}' and col '{c}' should not be NaN");
                Assert.IsTrue(!float.IsNaN(v.Elevation), $"Elevation at position at row '{r}' and col '{c}' should not be NaN");
                _log.Trace($"{r}:{c} -> angle: {v}");
                return(v);
            });
        }