示例#1
0
        public void MakeLoadTypesTest()
        {
            //_calcParameters.CSVCharacter = ";";
            VLoadType vlt = new VLoadType("vlt", string.Empty, "W", "kWh", 1000, 1, new TimeSpan(1, 0, 0), 1,
                                          string.Empty, LoadTypePriority.Mandatory, true, Guid.NewGuid().ToStrGuid(), 1);

            ObservableCollection <VLoadType> vlts = new ObservableCollection <VLoadType> {
                vlt
            };
            var dtoDict = CalcLoadTypeDtoFactory.MakeLoadTypes(vlts, new TimeSpan(0, 0, 1), LoadTypePriority.All);
            var ltdict  = CalcLoadTypeFactory.MakeLoadTypes(dtoDict);
            var dto     = dtoDict.GetLoadtypeDtoByLoadType(vlt);
            var clt     = ltdict.GetCalcLoadTypeByLoadtype(dto);

            clt.ConversionFactor.Should().Be(1.0 / 3600000);
        }
        public void Run()
        {
            //WorkingDir wd = new WorkingDir(Utili.GetCurrentMethodAndClass());
            using (DatabaseSetup db = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                Simulator sim         = new Simulator(db.ConnectionString);
                var       tempProfile = sim.TemperatureProfiles[0];
                var       geoloc      = sim.GeographicLocations[0];
                var       ht          = sim.HouseTypes[0];
                ht.AdjustYearlyEnergy = false;
                ht.HeatingYearlyTotal = 10000;
                if (ht.AdjustYearlyEnergy)
                {
                    throw new LPGException("Invalid test:Adjust energy is turned on");
                }
                House house = new House("house", "desc", tempProfile, geoloc, ht,
                                        sim.ConnectionString, EnergyIntensityType.AsOriginal, "source", CreationType.ManuallyCreated,
                                        Guid.NewGuid().ToStrGuid());
                HouseholdKey myKey        = new HouseholdKey("blub");
                var          start        = new DateTime(2019, 12, 27);
                var          end          = new DateTime(2020, 12, 31);
                var          ltdict       = CalcLoadTypeDtoFactory.MakeLoadTypes(sim.LoadTypes.It, new TimeSpan(0, 1, 0), LoadTypePriority.All);
                var          spaceheating = CalcHouseDtoFactory.CreateSpaceHeatingObject(house, tempProfile, myKey, out var _, start, end, ltdict);
                if (spaceheating == null)
                {
                    throw new LPGException("Spaceheating was null");
                }
                //double sum = 0;
                Dictionary <int, double> sumByYear = new Dictionary <int, double>();
                foreach (var cdd in spaceheating.CalcDegreeDays)
                {
                    if (!sumByYear.ContainsKey(cdd.Year))
                    {
                        sumByYear.Add(cdd.Year, 0);
                    }
                    sumByYear[cdd.Year] += cdd.HeatingAmount;
                }

                foreach (var pair in sumByYear)
                {
                    Logger.Info(pair.Key + ": " + pair.Value);
                }
            }
        }
        public CalcHouseholdDto MakeSingleFactory([NotNull] WorkingDir wd, [NotNull] DatabaseSetup db)
        {
            var builder = new ContainerBuilder();

            wd.InputDataLogger.AddSaver(new CalcPersonDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new CalcAffordanceDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new CalcVariableDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new BridgeDayEntryLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new HouseholdDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new CalcSiteDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new CalcTransportationDeviceDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new CalcTravelRouteDtoLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new TransportationStatusLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new TransportationEventLogger(wd.SqlResultLoggingService));
            wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));

            var sim = new Simulator(db.ConnectionString);

            var mhh       = sim.ModularHouseholds[0];
            var r         = new Random(1);
            var ltdtoDict = CalcLoadTypeDtoFactory.MakeLoadTypes(sim.LoadTypes.It, new TimeSpan(0, 1, 0),
                                                                 LoadTypePriority.RecommendedForHouseholds);
            var ltdict     = CalcLoadTypeFactory.MakeLoadTypes(ltdtoDict);
            var parameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(2018, 1, 1)
                             .SetEndDate(new DateTime(2018, 1, 1, 2, 0, 0)).SetSettlingDays(0).EnableShowSettlingPeriod();

            builder.Register(x => parameters).As <CalcParameters>().SingleInstance();
            builder.Register(x => new DateStampCreator(parameters)).As <DateStampCreator>().SingleInstance();
            builder.Register(c => ltdict).As <CalcLoadTypeDictionary>().SingleInstance();
            builder.Register(c => ltdtoDict).As <CalcLoadTypeDtoDictionary>().SingleInstance();
            builder.Register(c => new NormalRandom(0, 1, r)).As <NormalRandom>().SingleInstance();
            builder.Register(c => new FileFactoryAndTracker(wd.WorkingDirectory, mhh.Name, wd.InputDataLogger))
            .As <FileFactoryAndTracker>()
            .SingleInstance();
            builder.Register(c => new SqlResultLoggingService(wd.WorkingDirectory)).As <SqlResultLoggingService>()
            .SingleInstance();
            builder.Register(c => wd.InputDataLogger).As <IInputDataLogger>().SingleInstance();

            builder.Register(c => new OnlineLoggingData(c.Resolve <DateStampCreator>(), c.Resolve <IInputDataLogger>(),
                                                        c.Resolve <CalcParameters>()))
            .As <OnlineLoggingData>().As <IOnlineLoggingData>().SingleInstance();

            builder.Register(c => new LogFile(parameters,
                                              c.Resolve <FileFactoryAndTracker>())).As <ILogFile>().SingleInstance();
            builder.RegisterType <OnlineDeviceActivationProcessor>().As <IOnlineDeviceActivationProcessor>()
            .SingleInstance();
            builder.RegisterType <CalcModularHouseholdFactory>().As <CalcModularHouseholdFactory>().SingleInstance();
            builder.Register(x => new DeviceCategoryPicker(r, null)).As <IDeviceCategoryPicker>().SingleInstance();

            builder.Register(x => r).As <Random>().SingleInstance();
            builder.RegisterType <CalcDeviceFactory>().As <CalcDeviceFactory>().SingleInstance();
            builder.RegisterType <CalcDeviceDtoFactory>().As <CalcDeviceDtoFactory>().SingleInstance();
            builder.RegisterType <CalcLocationFactory>().As <CalcLocationFactory>().SingleInstance();
            builder.RegisterType <CalcLocationDtoFactory>().As <CalcLocationDtoFactory>().SingleInstance();
            builder.RegisterType <CalcPersonFactory>().As <CalcPersonFactory>().SingleInstance();
            builder.RegisterType <CalcPersonDtoFactory>().As <CalcPersonDtoFactory>().SingleInstance();
            builder.RegisterType <CalcAffordanceFactory>().As <CalcAffordanceFactory>().SingleInstance();
            builder.RegisterType <CalcAffordanceDtoFactory>().As <CalcAffordanceDtoFactory>().SingleInstance();
            builder.RegisterType <CalcTransportationFactory>().As <CalcTransportationFactory>().SingleInstance();
            builder.RegisterType <CalcModularHouseholdDtoFactory>().As <CalcModularHouseholdDtoFactory>()
            .SingleInstance();
            builder.RegisterType <CalcVariableDtoFactory>().As <CalcVariableDtoFactory>().SingleInstance();
            builder.RegisterType <CalcTransportationDtoFactory>().As <CalcTransportationDtoFactory>().SingleInstance();
            builder.RegisterType <VacationDtoFactory>().As <VacationDtoFactory>().SingleInstance();
            builder.RegisterType <AvailabilityDtoRepository>().As <AvailabilityDtoRepository>().SingleInstance();
            builder.RegisterType <CalcVariableRepository>().As <CalcVariableRepository>().SingleInstance();
            builder.RegisterType <CalcRepo>().As <CalcRepo>().SingleInstance();
            var container = builder.Build();

            using (var scope = container.BeginLifetimeScope()) {
                var hhdtofac = scope.Resolve <CalcModularHouseholdDtoFactory>();

                var tds = sim.TransportationDeviceSets[0];
                tds.SaveToDB();
                var trs = sim.TravelRouteSets[0];
                trs.SaveToDB();
                var css = sim.ChargingStationSets[0];
                css.SaveToDB();
                var fft = scope.Resolve <FileFactoryAndTracker>();
                fft.RegisterGeneralHouse();
                var dtohh = hhdtofac.MakeCalcModularHouseholdDto(sim,
                                                                 mhh,
                                                                 sim.TemperatureProfiles[0],
                                                                 new HouseholdKey("hh1"),
                                                                 sim.GeographicLocations[0],
                                                                 out _,
                                                                 tds,
                                                                 trs,
                                                                 EnergyIntensityType.Random,
                                                                 css);
                fft.Dispose();
                db.Cleanup();
                wd.CleanUp();
                return(dtohh);
            }
        }
        public void RunMakeCalcTest()
        {
            //TODO: fix the container registering
            var builder = new ContainerBuilder();

            using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) {
                var path        = wd.WorkingDirectory;
                var inputlogger = wd.InputDataLogger;
                wd.InputDataLogger.AddSaver(new CalcPersonDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcAffordanceDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcVariableDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new BridgeDayEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new HouseholdDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcSiteDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcTransportationDeviceDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcTravelRouteDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new TransportationStatusLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new TransportationEventLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));

                using (var db = new DatabaseSetup(Utili.GetCurrentMethodAndClass())) {
                    var sim = new Simulator(db.ConnectionString);

                    var mhh       = sim.ModularHouseholds[0];
                    var r         = new Random(1);
                    var dcp       = new DeviceCategoryPicker(r, null);
                    var ltdtoDict = CalcLoadTypeDtoFactory.MakeLoadTypes(sim.LoadTypes.It, new TimeSpan(0, 1, 0),
                                                                         LoadTypePriority.RecommendedForHouseholds);
                    var ltdict = CalcLoadTypeFactory.MakeLoadTypes(ltdtoDict);
                    //var picker = new DeviceCategoryPicker(r,null);
                    var nr         = new NormalRandom(0, 0.1, r);
                    var parameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(2018, 1, 1)
                                     .SetEndDate(new DateTime(2018, 1, 1, 2, 0, 0)).SetSettlingDays(0).EnableShowSettlingPeriod();
                    builder.Register(x => parameters).As <CalcParameters>().SingleInstance();
                    builder.Register(x => new DateStampCreator(parameters)).As <DateStampCreator>().SingleInstance();
                    builder.Register(c => ltdict).As <CalcLoadTypeDictionary>().SingleInstance();
                    builder.Register(c => ltdtoDict).As <CalcLoadTypeDtoDictionary>().SingleInstance();
                    builder.Register(c => new NormalRandom(0, 1, r)).As <NormalRandom>().SingleInstance();
                    builder.Register(c => new FileFactoryAndTracker(path, mhh.Name, inputlogger))
                    .As <FileFactoryAndTracker>().SingleInstance();
                    builder.Register(c => new SqlResultLoggingService(path)).As <SqlResultLoggingService>()
                    .SingleInstance();
                    builder.Register(c => inputlogger).As <IInputDataLogger>().As <InputDataLogger>().SingleInstance();

                    builder.Register(c => new OnlineLoggingData(c.Resolve <DateStampCreator>(),
                                                                c.Resolve <IInputDataLogger>(), c.Resolve <CalcParameters>())).As <OnlineLoggingData>().As <IOnlineLoggingData>()
                    .SingleInstance();

                    builder.Register(c => new LogFile(parameters, c.Resolve <FileFactoryAndTracker>())).As <ILogFile>()
                    .SingleInstance();
                    builder.RegisterType <OnlineDeviceActivationProcessor>().As <IOnlineDeviceActivationProcessor>()
                    .SingleInstance();
                    builder.RegisterType <CalcModularHouseholdFactory>().As <CalcModularHouseholdFactory>()
                    .SingleInstance();
                    builder.Register(x => new DeviceCategoryPicker(r, null)).As <IDeviceCategoryPicker>()
                    .SingleInstance();

                    builder.Register(x => r).As <Random>().SingleInstance();
                    builder.RegisterType <CalcDeviceFactory>().As <CalcDeviceFactory>().SingleInstance();
                    builder.RegisterType <CalcDeviceDtoFactory>().As <CalcDeviceDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcLocationFactory>().As <CalcLocationFactory>().SingleInstance();
                    builder.RegisterType <CalcLocationDtoFactory>().As <CalcLocationDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcPersonFactory>().As <CalcPersonFactory>().SingleInstance();
                    builder.RegisterType <CalcPersonDtoFactory>().As <CalcPersonDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcAffordanceFactory>().As <CalcAffordanceFactory>().SingleInstance();
                    builder.RegisterType <CalcAffordanceDtoFactory>().As <CalcAffordanceDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcTransportationFactory>().As <CalcTransportationFactory>().SingleInstance();
                    builder.RegisterType <CalcModularHouseholdDtoFactory>().As <CalcModularHouseholdDtoFactory>()
                    .SingleInstance();
                    builder.RegisterType <CalcVariableDtoFactory>().As <CalcVariableDtoFactory>().SingleInstance();
                    builder.RegisterType <CalcTransportationDtoFactory>().As <CalcTransportationDtoFactory>()
                    .SingleInstance();
                    builder.RegisterType <VacationDtoFactory>().As <VacationDtoFactory>().SingleInstance();
                    builder.RegisterType <AvailabilityDtoRepository>().As <AvailabilityDtoRepository>().SingleInstance();
                    builder.RegisterType <CalcVariableRepository>().As <CalcVariableRepository>().SingleInstance();
                    builder.RegisterType <CalcRepo>().As <CalcRepo>().SingleInstance();
                    var container = builder.Build();
                    using (var scope = container.BeginLifetimeScope()) {
                        var hhdtofac = scope.Resolve <CalcModularHouseholdDtoFactory>();

                        var tds = sim.TransportationDeviceSets[0];
                        tds.SaveToDB();
                        var trs = sim.TravelRouteSets[0];
                        trs.SaveToDB();
                        var css = sim.ChargingStationSets[0];
                        css.SaveToDB();

                        /*Site home = sim.Sites.CreateNewItem(sim.ConnectionString);
                         * home.Name = "home";
                         * home.SaveToDB();
                         * Site outside = sim.Sites.CreateNewItem(sim.ConnectionString);
                         * outside.Name = "outside";
                         * outside.SaveToDB();
                         * home.AddLocation(sim.Locations.SafeFindByName("Living room", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Kitchen", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Bath", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Bedroom", FindMode.IgnoreCase));
                         * home.AddLocation(sim.Locations.SafeFindByName("Children's room", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Dance Studio", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Supermarket", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Garden", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Museum", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("School", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Food Market", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Sidewalk", FindMode.IgnoreCase));
                         * outside.AddLocation(sim.Locations.SafeFindByName("Office Workplace 1", FindMode.IgnoreCase));
                         * TravelRoute tr = new TravelRoute(null, db.ConnectionString, "tr1", "desc", home, outside);
                         * tr.SaveToDB();
                         * trs.AddRoute(tr);*/
                        var fft = scope.Resolve <FileFactoryAndTracker>();
                        fft.RegisterGeneralHouse();
                        var ctf = scope.Resolve <CalcTransportationFactory>();
                        //LogFile lf = new LogFile(wd.WorkingDirectory, "hh1", true);
                        //lf.RegisterKey("hh1", "hh1-prettyname");
                        var dtohh = hhdtofac.MakeCalcModularHouseholdDto(sim, mhh,
                                                                         sim.TemperatureProfiles[0], new HouseholdKey("hh1"), sim.GeographicLocations[0],
                                                                         out var dtolocs, tds, trs,
                                                                         EnergyIntensityType.Random, css);
                        var cvdto = scope.Resolve <CalcVariableDtoFactory>();
                        var cvr   = scope.Resolve <CalcVariableRepository>();
                        foreach (var v in cvdto.VariableDtos.Values)
                        {
                            cvr.RegisterVariable(new CalcVariable(v.Name, v.Guid, v.Value,
                                                                  v.LocationName, v.LocationGuid, v.HouseholdKey));
                        }

                        var cmhf = scope.Resolve <CalcModularHouseholdFactory>();
                        //CalcTransportationDtoFactory dtoFactory = new CalcTransportationDtoFactory(ltdtoDict);
                        //dtoFactory.MakeTransportationDtos(sim, sim.ModularHouseholds[0], tds, trs, out var sites,out var transportationDevices, out var routes, dtohh.LocationDtos, dtohh.HouseholdKey);
                        var calcRepo = scope.Resolve <CalcRepo>();
                        var chh      = cmhf.MakeCalcModularHousehold(dtohh, out var dtoCalcLocationDict, null, null,
                                                                     calcRepo);
                        //ctf.MakeTransportation(dtohh,dtoCalcLocationDict,chh);
                        if (chh.TransportationHandler == null)
                        {
                            throw new LPGException("no transportation handler");
                        }

                        var          src        = chh.TransportationHandler.CalcSites[0].Locations[0];
                        var          dst        = chh.TransportationHandler.CalcSites[1].Locations[0];
                        const string personname = "personname";
                        var          ts         = new TimeStep(1, parameters);
                        dst.Affordances[0].IsBusy(ts, src, personname, false);
                        dst.Affordances[0].Activate(ts, personname, src, out var personTimeProfile);
                        fft.Dispose();
                    }

                    db.Cleanup();
                }

                wd.CleanUp();
            }
        }