private void MakeAbregelungWorksheet([NotNull] ScenarioSliceParameters slice, double maxDailyGen,
                                             [NotNull] Profile addedProfile)
        {
            RowCollection rc = new RowCollection("effect", "Effekt");

            for (double i = 0; i < 1; i += 0.01)
            {
                double storageSize = maxDailyGen * 2;
                var    minimzed    = ProfileSmoothing.FindBestPowerReductionRatio(addedProfile,
                                                                                  storageSize,
                                                                                  out var _,
                                                                                  out var reductionFactor, i);
                double friendlySize = storageSize / Constants.GWhFactor;
                Info("Size: " + i + " " + friendlySize + " gwh, Reduction factor: " + reductionFactor);
                RowBuilder rb = RowBuilder.Start("Size", friendlySize);
                rb.Add("storage", storageSize);
                rb.Add("ReductionFactor", reductionFactor);
                rb.Add("Capping", i);
                rb.Add("Max Power", minimzed.MaxPower() / 1000);
                rb.Add("Min Power", minimzed.MinPower() / 1000);
                rc.Add(rb);
            }

            var fnFactor = MakeAndRegisterFullFilename("CappingImpact.xlsx", slice);

            XlsxDumper.WriteToXlsx(fnFactor, rc);
            SaveToPublicationDirectory(fnFactor, slice, "4.5");
        }
Exemplo n.º 2
0
        protected override void RunActualProcess([NotNull][ItemNotNull] List <ScenarioSliceParameters> allSlices, [NotNull] AnalysisRepository analysisRepo)
        {
            Info("starting to make house results");
            var sheets = new List <RowCollection>();

            foreach (var slice in allSlices)
            {
                RowCollection rc          = new RowCollection(slice.DstScenario.ShortName + "_" + slice.DstYear, slice.DstScenario.Name + " - " + slice.DstYear);
                var           pventries   = analysisRepo.GetSlice(slice).Fetch <PvSystemEntry>();
                var           houses      = analysisRepo.GetSlice(slice).Fetch <House>();
                var           pvPotential = analysisRepo.GetSlice(slice).Fetch <PVPotential>();
                foreach (var house in houses)
                {
                    var pventriesInHouse = pventries.GetByReferenceGuidWithEmptyReturns(house.HouseGuid, "HouseGuid", y => y.HouseGuid);
                    var rb = RowBuilder.Start("House", house.ComplexName);
                    var pvPotentialsInHouse = pvPotential.GetByReferenceGuidWithEmptyReturns(house.HouseGuid, "HouseGuid", y => y.HouseGuid);
                    rb.Add("PV entries", pventriesInHouse.Count);
                    if (pventriesInHouse.Count > 0)
                    {
                        rb.Add("PV Year", pventriesInHouse.Max(x => x.BuildYear));
                    }

                    rb.Add("EnergySum", pventriesInHouse.Sum(x => x.EffectiveEnergyDemand));
                    rb.Add("Potential Count", pvPotentialsInHouse.Count);
                    rb.Add("Potential Energy Sum", pvPotentialsInHouse.Sum(x => x.SonnendachStromErtrag));
                    rc.Add(rb);
                }
                sheets.Add(rc);
            }

            var fn = MakeAndRegisterFullFilename("PVAnalysis.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, sheets);
        }
        protected override void RunActualProcess()
        {
            var           dbRaw   = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var           dbHouse = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var           houses  = dbHouse.Fetch <House>();
            var           ebbe    = dbRaw.Fetch <EnergiebedarfsdatenBern>();
            RowCollection rc      = new RowCollection("Analysis", "Analysis");

            foreach (var house in houses)
            {
                RowBuilder rb       = RowBuilder.Start("House", house.ComplexName);
                var        ebbedata = ebbe.Where(x => house.EGIDs.Contains((int)x.egid)).ToList();
                foreach (var ebbeSet in ebbedata)
                {
                    ebbe.Remove(ebbeSet);
                }

                rb.Add("Ebbe", JsonConvert.SerializeObject(ebbedata));
                rc.Add(rb);
            }

            foreach (var ebbeset in ebbe)
            {
                RowBuilder rb = RowBuilder.Start("Ebbe eGid", ebbeset.egid);
                rb.Add("Ebbe", JsonConvert.SerializeObject(ebbeset));
                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("HeatingSystemAnalysis.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
Exemplo n.º 4
0
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters)
        {
            //profile export
            var dbProfileExport =
                Services.SqlConnection.GetDatabaseConnection(Stage.ProfileExport, parameters);
            var prosumers = Prosumer.LoadProsumers(dbProfileExport, TableType.HouseGeneration);

            var resultPathGeneration = Path.Combine(dbProfileExport.GetResultFullPath(SequenceNumber, Name), "Export");

            if (Directory.Exists(resultPathGeneration))
            {
                Directory.Delete(resultPathGeneration, true);
                Thread.Sleep(500);
            }

            if (!Directory.Exists(resultPathGeneration))
            {
                Directory.CreateDirectory(resultPathGeneration);
                Thread.Sleep(500);
            }

            HashSet <string> usedKeys = new HashSet <string>();
            var           trafokreise = prosumers.Select(x => x.TrafoKreis).Distinct().ToList();
            RowCollection rc          = new RowCollection();

            foreach (var trafokreis in trafokreise)
            {
                if (string.IsNullOrWhiteSpace(trafokreis))
                {
                    continue;
                }

                var    filteredProsumers     = prosumers.Where(x => x.TrafoKreis == trafokreis).ToList();
                string tkFileName            = trafokreis.Replace("ä", "ae").Replace("ö", "oe").Replace("ü", "ue");
                var    csvFileNameGeneration = Path.Combine(resultPathGeneration, tkFileName + ".csv");
                var    sw2 = new StreamWriter(csvFileNameGeneration);

                int lines = 0;
                foreach (var prosumer in filteredProsumers)
                {
                    var row = RowBuilder.Start("Trafokreis", trafokreis).Add("Name", prosumer.Name).Add("Energy", prosumer.SumElectricityFromProfile);
                    if (usedKeys.Contains(prosumer.HausanschlussKey))
                    {
                        throw new FlaException("This key was already exported");
                    }
                    usedKeys.Add(prosumer.HausanschlussKey);
                    sw2.WriteLine(prosumer.GetCSVLine());
                    lines++;
                    rc.Add(row);
                }
                Log(MessageType.Info, "Wrote " + lines + " lines to  " + csvFileNameGeneration);
                sw2.Close();
            }

            var fn = MakeAndRegisterFullFilename("Generation.xlsx", parameters);

            XlsxDumper.WriteToXlsx(rc, fn, "Generators");
        }
        protected override void RunChartMaking()
        {
            AnalysisRepository rp    = new AnalysisRepository(Services.RunningConfig);
            var           slice      = rp.GetSlice(Constants.PresentSlice);
            var           potentials = slice.Fetch <PVPotential>();
            RowCollection rc         = new RowCollection("pv", "pv");

            foreach (var potential in potentials)
            {
                rc.Add(RowBuilder.Start("Neigung", potential.Neigung)
                       .Add("Ausrichtung", potential.Ausrichtung)
                       .Add("Fläche", potential.SonnendachStromErtrag));
            }
            var fn = MakeAndRegisterFullFilename("PVpotentials.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
        private void ExportAsExcel([NotNull][ItemNotNull] List <Hausanschluss> hausanschlusses, [NotNull][ItemNotNull] List <House> houses)
        {
            {
                //houses direct
                RowCollection rc = new RowCollection("Houses", "Häuser");
                foreach (House house in houses)
                {
                    rc.Rows.Add(MakeHouseRow(house));
                    foreach (var ha in house.Hausanschluss)
                    {
                        rc.Rows.Add(MakeHausanschlussRow(ha));
                    }
                }

                var fn = MakeAndRegisterFullFilename("HousesWithHausanschluss.xlsx", Constants.PresentSlice);
                XlsxDumper.WriteToXlsx(fn, rc);
            }
            {
                //houses by Trafokreis
                RowCollection rc          = new RowCollection("Houses", "Häuser");
                var           trafokreise = hausanschlusses.Select(x => x.Trafokreis).Distinct().ToList();
                foreach (var trafokreis in trafokreise)
                {
                    var rbTk           = RowBuilder.Start("Trafokreis Name", trafokreis);
                    var selectedHouses = houses.Where(x => x.Hausanschluss.Any(y => y.Trafokreis == trafokreis)).ToList();
                    rbTk.Add("House Count", selectedHouses.Count);
                    rc.Rows.Add(rbTk.GetRow());
                    foreach (House house in selectedHouses)
                    {
                        rc.Rows.Add(MakeHouseRow(house));
                        foreach (var ha in house.Hausanschluss)
                        {
                            rc.Rows.Add(MakeHausanschlussRow(ha));
                        }
                    }
                }

                var fn = MakeAndRegisterFullFilename("HousesWithTrafokreisTree.xlsx", Constants.PresentSlice);
                XlsxDumper.WriteToXlsx(fn, rc);
            }
        }
        private void DumpHeatingSystemsToXlsx([NotNull] ScenarioSliceParameters slice,
                                              [NotNull][ItemNotNull] List <HeatingSystemEntry> srcHeatingSystems,
                                              [NotNull] Dictionary <string, House> dstHousesByGuid)
        {
            RowCollection rc1 = new RowCollection("HeatingSystems", "HeatingSystems");

            foreach (var heatingSystemEntry in srcHeatingSystems)
            {
                var house = dstHousesByGuid[heatingSystemEntry.HouseGuid];
                var rb    = RowBuilder.Start("House", house.ComplexName);
                rb.Add("Effective EnergyDemand", heatingSystemEntry.EffectiveEnergyDemand);
                rb.Add("Effective HeatDemand", heatingSystemEntry.HeatDemand);
                rb.Add("Heating system type", heatingSystemEntry.SynthesizedHeatingSystemType);
                rb.Add("Age", heatingSystemEntry.Age);
                rc1.Add(rb);
            }

            var fn1 = MakeAndRegisterFullFilename("heatingsystemdump.xlsx", slice);

            XlsxDumper.WriteToXlsx(fn1, rc1);
        }
Exemplo n.º 8
0
        private void WriteSumLine([NotNull][ItemNotNull] List <CSVFile> csvs1, [NotNull] ScenarioSliceParameters slice)
        {
            if (slice.DstYear != 2050 && slice.SmartGridEnabled)
            {
                return;
            }

            string        suffix = csvs1[0].GenerationOrLoad.ToString().ToLower();
            var           fn     = MakeAndRegisterFullFilename("SummedProfilePerTrafokreis." + suffix + ".csv", slice);
            RowCollection rc     = new RowCollection("Sheet1", "Sheet1");

            using (StreamWriter sumfile = new StreamWriter(fn)) {
                Info("writing " + csvs1.Count + " sum lines for " + suffix);
                foreach (var csv in csvs1)
                {
                    double[] arr = new double[35040];
                    foreach (var line in csv.Lines)
                    {
                        for (int i = 0; i < 35040; i++)
                        {
                            arr[i] += line.Values[i];
                        }
                    }

                    Profile    p  = new Profile(csv.FileName ?? throw new InvalidOperationException(), arr.ToList().AsReadOnly(), EnergyOrPower.Energy);
                    RowBuilder rb = RowBuilder.Start("Name", csv.FileName);
                    rb.Add("Value", p.EnergySum());
                    rc.Add(rb);
                    sumfile.WriteLine(csv.FileName + ";" + p.GetCSVLine());
                }

                sumfile.Close();
            }

            var xlsfn = MakeAndRegisterFullFilename("SumsPerTrafokreis." + suffix + ".xlsx", slice);

            XlsxDumper.WriteToXlsx(xlsfn, rc);
            SaveToArchiveDirectory(fn, RelativeDirectory.Trafokreise, slice);
            SaveToArchiveDirectory(xlsfn, RelativeDirectory.Report, slice);
        }
        private void MakeStorageSizeSheet([NotNull] ScenarioSliceParameters slice, double maxDailyGen,
                                          [NotNull] Profile addedProfile)
        {
            RowCollection rc = new RowCollection("effect", "Effekt");

            for (double i = 0; i < 10; i += 0.1)
            {
                double storageSize = maxDailyGen * i;
                var    minimzed    = ProfileSmoothing.FindBestPowerReductionRatio(addedProfile,
                                                                                  storageSize,
                                                                                  out var _,
                                                                                  out var reductionFactor, 1);
                var minimzed2 = ProfileSmoothing.FindBestPowerReductionRatio(addedProfile,
                                                                             storageSize,
                                                                             out var _,
                                                                             out var reductionFactor2, 0.5);
                double friendlySize = storageSize / Constants.GWhFactor;
                Info("Size: " + i + " " + friendlySize + " gwh, Reduction factor: " + reductionFactor);
                RowBuilder rb = RowBuilder.Start("Size", friendlySize);
                rb.Add("DaySize", i);
                rb.Add("ReductionFactor", reductionFactor);
                rb.Add("Max Power", minimzed.MaxPower() / 1000);
                rb.Add("Min Power", minimzed.MinPower() / 1000);
                rb.Add("Energy", minimzed.EnergySum() / 1000000);
                rb.Add("EnergyFromGrid", minimzed.GetOnlyPositive("pos").EnergySum() / 1000000);
                rb.Add("EnergyToGrid", minimzed.GetOnlyNegative("neg").EnergySum() / 1000000);
                rb.Add("ReductionFactor Curtailed", reductionFactor2);
                rb.Add("Max Power Curtailed", minimzed2.MaxPower() / 1000);
                rb.Add("Min Power Curtailed", minimzed2.MinPower() / 1000);
                rb.Add("Energy Curtailed", minimzed2.EnergySum() / 1000000);
                rb.Add("EnergyFromGrid Curtailed", minimzed2.GetOnlyPositive("pos").EnergySum() / 1000000);
                rb.Add("EnergyToGrid Curtailed", minimzed2.GetOnlyNegative("neg").EnergySum() / 1000000);
                rc.Add(rb);
            }

            var fnFactor = MakeAndRegisterFullFilename("BatterySizeImpact.xlsx", slice);

            XlsxDumper.WriteToXlsx(fnFactor, rc);
            SaveToPublicationDirectory(fnFactor, slice, "4.5");
        }
        private void WriteProfilesToExcel([NotNull] Profile temperaturProfile,
                                          [NotNull] Profile houseEnergy,
                                          [NotNull] Profile heatPumpEnergysupply,
                                          [NotNull] string file)
        {
            //dump to csv
            RowCollection rc = new RowCollection("sheet1", "Sheet1");
            DateTime      dt = new DateTime(2017, 1, 1);

            for (int i = 0; i < 35040; i++)
            {
                RowBuilder rb = RowBuilder.Start("Idx", i);
                rb.Add("Time", dt.ToShortDateString() + " " + dt.ToShortTimeString());
                dt = dt.AddMinutes(15);
                rb.Add(temperaturProfile.Name, temperaturProfile.Values[i]);
                rb.Add(houseEnergy.Name, houseEnergy.Values[i]);
                rb.Add(heatPumpEnergysupply.Name, heatPumpEnergysupply.Values[i]);
                rc.Add(rb);
            }

            var           fn = Path.Combine(Config.Directories.UnitTestingDirectory, nameof(HeatPumpTest), file);
            FileInfo      fi = new FileInfo(fn);
            DirectoryInfo di = fi.Directory;

            if (di == null)
            {
                throw new FlaException("No path");
            }

            if (!di.Exists)
            {
                di.Create();
            }

            XlsxDumper.WriteToXlsx(fn, rc);
            Info("Wrote results to " + fn);
            Process.Start(di.FullName);
        }
Exemplo n.º 11
0
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            if (!Services.RunningConfig.MakeHouseSums)
            {
                return;
            }

            var presentSums      = GetEnergySumPerHouseForThePresent();
            var energySums       = GetPlannedEnergySumPerHouseForScenario(slice);
            var energyInProfiles = GetEnergyInProfiles(slice);

            RowCollection rc = new RowCollection("Comparison", "Comparison");

            rc.ColumnsToSum.Add("Summe Gegenwart");
            rc.ColumnsToSum.Add("Summe Haus Collection");
            rc.ColumnsToSum.Add("Summe Profile");
            rc.SumDivisionFactor = 1_000_000;
            presentSums.Sort((x, y) => y.Energy.CompareTo(x.Energy));
            foreach (var presentSum in presentSums)
            {
                RowBuilder rb = RowBuilder.Start("Hausname", presentSum.HouseName);
                rb.Add("Summe Gegenwart", presentSum.Energy);
                rb.Add("Summe Haus Collection", energySums.Single(x => x.HouseName == presentSum.HouseName).Energy);
                var profile = energyInProfiles.FirstOrDefault(x => x.HouseName == presentSum.HouseName);
                if (profile != null)
                {
                    rb.Add("Summe Profile", profile.Energy);
                }

                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("EnergyComparison.xlsx", slice);

            XlsxDumper.WriteToXlsx(fn, rc);
            SaveToArchiveDirectory(fn, RelativeDirectory.Report, slice);
        }
Exemplo n.º 12
0
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters)
        {
            Services.SqlConnection.RecreateTable <HausAnschlussExportEntry>(Stage.ProfileExport, parameters);
            MyDb dbProfileExport = PrepareProfileExport(parameters);

            //prepare profile export

            //load previous data
            var dbProfileGeneration = LoadPreviousData(parameters,
                                                       out var houses, out var households,
                                                       out var businesses);
            //trafokreise
            var trafokreise = houses.SelectMany(x => x.Hausanschluss).Select(x => x.Trafokreis).Distinct().ToList();

            //int trafokreiscount = 1;

            dbProfileGeneration.Database.BeginTransaction();
            RowCollection rc = new RowCollection();

            foreach (var trafokreis in trafokreise)
            {
                //prepare
                if (string.IsNullOrWhiteSpace(trafokreis))
                {
                    continue;
                }
                //  if (trafokreiscount > 3) {
                //    continue;
                //}
                //  trafokreiscount++;

                //merge
                var houseExportEntries = MergeAllEntriesForTrafokreis(houses, dbProfileGeneration, trafokreis,
                                                                      households, businesses);

                //validate
                CheckHouseExportEntryIfAllHouseholdsAreCovered(households, houseExportEntries);

                //save
                var saLoad       = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseLoad);
                var saGeneration = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseGeneration);
                foreach (var houseExportEntry in houseExportEntries)
                {
                    var rb = RowBuilder.Start("Trafokreis", trafokreis);
                    //load
                    var ps = new Prosumer(houseExportEntry.HouseGuid,
                                          houseExportEntry.HouseName, ProsumerType.HouseLoad,
                                          houseExportEntry.HouseGuid, houseExportEntry.Isn, houseExportEntry.Trafokreis,
                                          houseExportEntry.GetSumProfile(HausAnschlussExportEntry.TypeOfSum.Load), houseExportEntry.HausanschlussGuid, houseExportEntry.HausAnschlussKeyString);
                    double profileEnergy = ps.Profile?.EnergySum() ?? throw new Exception("Invalid profile");
                    rb.Add("Housename", houseExportEntry.HouseName)
                    .Add("Load in Profile", profileEnergy).Add("Total Energy Load Planned", houseExportEntry.TotalEnergyLoad);
                    if (Math.Abs(profileEnergy - houseExportEntry.TotalEnergyLoad) > 1)
                    {
                        throw new Exception("Wrong energy");
                    }
                    saLoad.AddRow(ps);
                    //generation
                    if (houseExportEntry.TotalEnergyGeneration > 1)
                    {
                        var psGen = new Prosumer(houseExportEntry.HouseGuid, houseExportEntry.HouseName, ProsumerType.HouseLoad, houseExportEntry.HouseGuid, houseExportEntry.Isn,
                                                 houseExportEntry.Trafokreis, houseExportEntry.GetSumProfile(HausAnschlussExportEntry.TypeOfSum.Generation), houseExportEntry.HausanschlussGuid,
                                                 houseExportEntry.HausAnschlussKeyString);
                        double profileEnergyGen = psGen.Profile?.EnergySum() ?? throw new Exception("Invalid profile");
                        if (Math.Abs(profileEnergyGen - houseExportEntry.TotalEnergyGeneration) > 1)
                        {
                            throw new Exception("Wrong energy");
                        }
                        rb.Add("Generation Profile Energy", profileEnergyGen)
                        .Add("Total Energy Generation Planned", houseExportEntry.TotalEnergyGeneration);
                        saGeneration.AddRow(psGen);
                    }

                    dbProfileExport.Database.Save(houseExportEntry);
                    rc.Add(rb);
                }
                saLoad.SaveDictionaryToDatabase();
                if (saGeneration.RowEntries.Count > 0)
                {
                    saGeneration.SaveDictionaryToDatabase();
                }
            }
            var fnLoad = MakeAndRegisterFullFilename("MergedStuff.xlsx", Name, "", parameters);

            XlsxDumper.WriteToXlsx(rc, fnLoad, "ProsumersMerged");
        }
        protected override void RunActualProcess()
        {
            var dbHouses  = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var dbComplex = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Complexes, Constants.PresentSlice);

            dbHouses.RecreateTable <HouseHeating>();
            var           buildingcomplexes       = dbComplex.Fetch <BuildingComplex>();
            var           dbRaw                   = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var           energieBedarfsDatenBern = dbRaw.Fetch <EnergiebedarfsdatenBern>();
            var           houses                  = dbHouses.Fetch <House>();
            var           potentialHeatingSystems = dbHouses.Fetch <PotentialHeatingSystemEntry>();
            RowCollection rc = new RowCollection("Heating", "Heizung");

            dbHouses.BeginTransaction();
            foreach (var h in houses)
            {
                RowBuilder rb = RowBuilder.Start("House", h.ComplexName);
                rc.Add(rb);
                var c            = buildingcomplexes.First(x => x.ComplexID == h.ComplexID);
                var hs           = potentialHeatingSystems.Where(x => x.HouseGuid == h.Guid).ToList();
                var houseHeating = new HouseHeating {
                    LocalnetFernwärmeEnergyUse = hs.Sum(x => x.YearlyFernwärmeDemand),
                    LocalnetGasEnergyUse       = hs.Sum(x => x.YearlyGasDemand),
                    HouseGuid = h.Guid,
                    LocalnetHeatingSystemEntryCount = hs.Count
                };
                rb.Add("Fernwärme", houseHeating.LocalnetFernwärmeEnergyUse).Add("LocalnetEntries", hs.Count);
                rb.Add("Standort", string.Join(",", hs.Select(x => x.Standort).Distinct()));
                rb.Add("Gas", houseHeating.LocalnetGasEnergyUse);
                rb.Add("EBF", h.EnergieBezugsFläche);
                rb.Add("Abrechnung", JsonConvert.SerializeObject(hs, Formatting.Indented));
                //collect ebbe daten
                foreach (var eGid in c.EGids)
                {
                    var ebdbs = energieBedarfsDatenBern.Where(x => x.egid == eGid).ToList();
                    if (ebdbs.Count > 1)
                    {
                        throw new Exception("too many ebdb");
                    }

                    if (ebdbs.Count == 1)
                    {
                        var eb = ebdbs[0];
                        houseHeating.KantonHeatingMethods.Add(GetHeatingMethodString((int)eb.upd_genhz));
                        houseHeating.KantonTotalEnergyDemand      += eb.calc_ehzww;
                        houseHeating.KantonHeizungEnergyDemand    += eb.calc_ehz;
                        houseHeating.KantonWarmwasserEnergyDemand += eb.calc_eww;
                        houseHeating.KantonDhwMethods.Add(GetHeatingMethodString(eb.upd_genww));
                    }
                }

                houseHeating.LocalnetCombinedEnergyDemand = houseHeating.LocalnetFernwärmeEnergyUse + houseHeating.LocalnetGasEnergyUse;
                houseHeating.LocalnetHeatingEnergyDensity = houseHeating.LocalnetAdjustedHeatingDemand / h.EnergieBezugsFläche;
                if (houseHeating.LocalnetHeatingEnergyDensity > 500)
                {
                    houseHeating.LocalnetHeatingEnergyDensity       = 250;
                    houseHeating.EnergyDensityIndustrialApplication = houseHeating.LocalnetHeatingEnergyDensity - 250;
                }

                houseHeating.KantonHeatingEnergyDensity = houseHeating.KantonTotalEnergyDemand / h.EnergieBezugsFläche;
                if (Math.Abs(houseHeating.LocalnetHeatingEnergyDensity) > 0.001)
                {
                    houseHeating.HeatingEnergyDifference = houseHeating.LocalnetHeatingEnergyDensity - houseHeating.KantonHeatingEnergyDensity;
                    if (double.IsNaN(houseHeating.HeatingEnergyDifference) || double.IsInfinity(houseHeating.HeatingEnergyDifference))
                    {
                        houseHeating.HeatingEnergyDifference = 0;
                    }
                }

                if (houseHeating.LocalnetHeatingEnergyDensity > 0)
                {
                    houseHeating.MergedHeatingEnergyDensity = houseHeating.LocalnetHeatingEnergyDensity;
                }
                else
                {
                    houseHeating.MergedHeatingEnergyDensity = houseHeating.KantonHeatingEnergyDensity;
                }

                if (houseHeating.MergedHeatingEnergyDensity < 0)
                {
                    throw new Exception("Negative heating intensity");
                }

                houseHeating.MergedHeatingDemand = 0;
                if (houseHeating.LocalnetCombinedEnergyDemand > 1)
                {
                    houseHeating.MergedHeatingDemand = houseHeating.LocalnetCombinedEnergyDemand;
                }
                else
                {
                    houseHeating.MergedHeatingDemand = houseHeating.KantonTotalEnergyDemand;
                }

                dbHouses.Save(h);
                dbHouses.Save(houseHeating);
            }

            dbHouses.CompleteTransaction();
            var fn = MakeAndRegisterFullFilename("heatingCalcsDump.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
        protected override void RunActualProcess()
        {
            var dbHouse = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouse.RecreateTable <BusinessEntry>();
            //load data
            var                  potentialBusinesses = dbHouse.Fetch <PotentialBusinessEntry>();
            var                  hausanschlusses     = dbHouse.Fetch <Hausanschluss>();
            var                  houses           = dbHouse.Fetch <House>();
            var                  validIsns        = houses.SelectMany(x => x.Hausanschluss.Select(y => y.Isn)).ToHashSet();
            List <int>           invalidIsns      = new List <int>();
            const int            randomlyChosenHa = 0;
            const int            reassignedHAs    = 0;
            List <BusinessEntry> businesses       = new List <BusinessEntry>();

            dbHouse.BeginTransaction();
            foreach (var pb in potentialBusinesses)
            {
                if ((pb.MyCategory == "Immobilien" || pb.MyCategory == "WEG") &&
                    pb.Standort != "Einschlagweg 59, Geschoss unbekannt, 3400 Burgdorf" &&
                    pb.Standort != "Fabrikweg 6, Geschoss unbekannt, 3400 Burgdorf")
                {
                    if (pb.HighVoltageYearlyElectricityUse > 0)
                    {
                        throw new FlaException("Building infrastructure with MS?");
                    }

                    var pbi = new PotentialBuildingInfrastructure(pb.HouseGuid,
                                                                  pb.BusinessName,
                                                                  pb.LowVoltageYearlyElectricityUse,
                                                                  pb.HighVoltageYearlyElectricityUse,
                                                                  pb.LowVoltageLocalnetEntries,
                                                                  pb.HighVoltageLocalnetEntries,
                                                                  pb.Standort, Guid.NewGuid().ToString());
                    dbHouse.Save(pbi);
                }
                else
                {
                    BusinessType bt = GetTypeFromDescription(pb.MyCategory);
                    var          be = new BusinessEntry(pb, bt);
                    be.HouseComponentType = HouseComponentType.BusinessNoLastgangLowVoltage;
                    //var hasIndustry = false;

                    /*if (pb.LowVoltageLocalnetEntries.Any(x => x.Rechnungsart == "Industrie") ||
                     *  pb.HighVoltageLocalnetEntries.Any(x => x.Rechnungsart == "Industrie")) {
                     *  hasIndustry = true;
                     * }*/

/*                    if (hasIndustry) {
 *                      be.BusinessType = BusinessType.Industrie;
 *                  }
 *                  else {
 *                      ;
 *                  }*/

                    //isn kontrolle
                    int validisn = 0;
                    foreach (int hhIsn in be.OriginalISNs)
                    {
                        if (!validIsns.Contains(hhIsn))
                        {
                            invalidIsns.Add(hhIsn);
                        }
                        else
                        {
                            validisn = hhIsn;
                        }
                    }

                    var house = houses.Single(x => x.Guid == be.HouseGuid);
                    if (validisn == 0)
                    {
                        be.FinalIsn = house.Hausanschluss[0].Isn;
                    }
                    else
                    {
                        be.FinalIsn = validisn;
                    }


                    var ha = hausanschlusses.Single(x => x.Guid == pb.HausAnschlussGuid);
                    if (ha == null)
                    {
                        throw new FlaException("ha was null");
                    }

                    /*     hausanschlusses.Where(x => x.HouseGuid == be.HouseGuid && x.Isn == be.FinalIsn).ToList();
                     * if (ha.Count == 0)
                     * {
                     *     //throw new FlaException("Kein Hausanschluss gefunden.");
                     *     reassignedHAs++;
                     *     be.HausAnschlussGuid = house.Hausanschluss[0].HausanschlussGuid;
                     * }
                     *
                     * if (ha.Count == 1)
                     * {
                     *     be.HausAnschlussGuid = ha[0].HausanschlussGuid;
                     * }
                     *
                     * if (ha.Count > 1)
                     * {
                     *     randomlyChosenHa++;
                     *     be.HausAnschlussGuid = ha[Services.Rnd.Next(ha.Count)].HausanschlussGuid;
                     *     //throw new FlaException("zu viele Hausanschlüsse gefunden.: " + ha.Count);
                     * }*/
                    be.HausAnschlussGuid = pb.HausAnschlussGuid;
                    businesses.Add(be);
                }
            }
            dbHouse.CompleteTransaction();
            AssignRlmProfiles(businesses, houses);
            foreach (var businessEntry in businesses)
            {
                if (businessEntry.LocalnetHighVoltageYearlyTotalElectricityUse > 0)
                {
                    if (businessEntry.HouseComponentType == HouseComponentType.BusinessNoLastgangLowVoltage)
                    {
                        businessEntry.HouseComponentType = HouseComponentType.BusinessNoLastgangHighVoltage;
                    }
                    else if (businessEntry.HouseComponentType == HouseComponentType.BusinessWithLastgangLowVoltage)
                    {
                        businessEntry.HouseComponentType = HouseComponentType.BusinessWithLastgangHighVoltage;
                    }
                }
            }

            Debug("Invalid Isns: " + invalidIsns.Distinct().Count());
            Debug("Zufällig ausgewählte Hausanschlüsse bei Häusern mit mehr als einem HA: " + randomlyChosenHa);
            Debug("Wohnungen mit neuem Hausanschluss wegen nicht gefundener ISN: " + reassignedHAs);
            int normalBusinesses = businesses.Count(x => x.HouseComponentType == HouseComponentType.BusinessNoLastgangLowVoltage);
            int rlmBusinesses    = businesses.Count(x => x.HouseComponentType == HouseComponentType.BusinessWithLastgangLowVoltage);

            Debug("Businesses without rlm:" + normalBusinesses + " with: " + rlmBusinesses);
            dbHouse.BeginTransaction();
            foreach (BusinessEntry entry in businesses)
            {
                dbHouse.Save(entry);
            }

            dbHouse.CompleteTransaction();
            RowCollection rc = new RowCollection("Businesses", "");

            foreach (var entry in businesses)
            {
                RowBuilder rb = RowBuilder.Start("Name", entry.BusinessName);
                rb.Add("Verbrauchstyp", entry.EnergyType);
                rb.Add("Verbrauch", entry.EffectiveEnergyDemand);
                rb.Add("Businesstype", entry.BusinessType.ToString());
                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("Businesses.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
        protected override void RunActualProcess()
        {
            var dbComplex = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Complexes, Constants.PresentSlice);
            //var dbRaw = SqlConnection.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var buildingcomplexes = dbComplex.Fetch <BuildingComplex>();

            var dbComplexEnergy = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ComplexEnergyData, Constants.PresentSlice);
            var complexdata     = dbComplexEnergy.Fetch <ComplexBuildingData>();
            var dbHouse         = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouse.RecreateTable <House>();
            dbHouse.RecreateTable <PotentialHousehold>();
            //var sonnendach = dbRaw.Fetch<SonnenDach>();
            dbHouse.BeginTransaction();

            List <string> complexesToIgnore = new List <string> {
                "EGID191357110", "EGID1306724", "EGID1305755", "Fischermätteliweg nn"
            };
            //complexesToIgnore.Add("Bernstrasse 113a");
            //complexesToIgnore.Add("Finkhubelweg 8");
            //complexesToIgnore.Add("Friedhof 4");
            var manualCoordinates = ReadManualCoordinatesList();

            foreach (var complex in buildingcomplexes)
            {
                if (complexesToIgnore.Contains(complex.ComplexName))
                {
                    continue;
                }
                //haus anlegen
                var h = new House(complex.ComplexName,
                                  complex.ComplexGuid,
                                  Guid.NewGuid().ToString()
                                  )
                {
                    ComplexID = complex.ComplexID,
                };
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                // ReSharper disable once HeuristicUnreachableCode
                if (h.Guid == null)
                {
                    // ReSharper disable once HeuristicUnreachableCode
                    throw new Exception("House guid was null");
                }

                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                // ReSharper disable once HeuristicUnreachableCode
                if (h.ComplexGuid == null)
                {
                    // ReSharper disable once HeuristicUnreachableCode
                    throw new Exception("Complex guid was null");
                }

                //gebäudeobjektids zuordnen

                foreach (var complexGebäudeObjectID in complex.GebäudeObjectIDs)
                {
                    h.GebäudeObjectIDs.Add(complexGebäudeObjectID);
                }

                // egids zuordnen
                foreach (var eGid in complex.EGids)
                {
                    h.EGIDs.Add((int)eGid);
                }

                //geo coords zuordnen
                foreach (var coord in complex.Coords)
                {
                    h.WgsGwrCoords.Add(WgsPoint.ConvertKoordsToLonLat(coord.X, coord.Y));
                }

                var manualCoord = manualCoordinates.Where(x => x.Name == h.ComplexName).ToList();
                if (manualCoord.Any())
                {
                    foreach (var manualComplexCoordinatese in manualCoord)
                    {
                        h.WgsGwrCoords.Add(new WgsPoint(manualComplexCoordinatese.Lon, manualComplexCoordinatese.Lat));
                    }
                }

                foreach (var coord in complex.LocalnetCoords)
                {
                    h.LocalWgsPoints.Add(WgsPoint.ConvertKoordsToLonLat(coord.X, coord.Y));
                }

                //addressen zuordnen
                if (complex.Adresses.Count == 0)
                {
                    h.Adress = "Unknown";
                }
                else
                {
                    h.Adress = complex.Adresses[0];
                }

                if (complex.TrafoKreise.Count > 0)
                {
                    foreach (var erzId in complex.ErzeugerIDs)
                    {
                        h.ErzeugerIDs.Add(erzId);
                    }
                }


                //assign household data
                var thiscomplexdata = complexdata.FirstOrDefault(x => x.ComplexName == complex.ComplexName);
                if (thiscomplexdata == null)
                {
                    h.Area = 0;
                    h.Appartments.Add(new AppartmentEntry(Guid.NewGuid().ToString(), 0, false, Constants.PresentSlice.DstYear));
                }
                else
                {
                    if (thiscomplexdata.AnzahlWohnungenBern > 0)
                    {
                        double avgArea = thiscomplexdata.TotalEnergieBezugsfläche / thiscomplexdata.AnzahlWohnungenBern;
                        for (int i = 0; i < thiscomplexdata.AnzahlWohnungenBern; i++)
                        {
                            h.Appartments.Add(new AppartmentEntry(Guid.NewGuid().ToString(), avgArea, true, Constants.PresentSlice.DstYear));
                        }
                    }
                    else
                    {
                        h.Appartments.Add(new AppartmentEntry(Guid.NewGuid().ToString(), thiscomplexdata.TotalEnergieBezugsfläche, false, Constants.PresentSlice.DstYear));
                    }
                    h.Area += thiscomplexdata.TotalArea;
                    h.AverageBuildingAge = thiscomplexdata.BuildingAges.Average();
                }

                dbHouse.Save(h);
            }

            dbHouse.CompleteTransaction();
            var           allHouses = dbHouse.FetchAsRepo <House>();
            RowCollection rc        = new RowCollection("houses", "houses");

            foreach (var house in allHouses)
            {
                RowBuilder rb = RowBuilder.Start("age", house.AverageBuildingAge);
                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("HouseAges.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
Exemplo n.º 16
0
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var dbSrcHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice.PreviousSliceNotNull);
            var dbDstHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);

            dbDstHouses.RecreateTable <PvSystemEntry>();
            dbDstHouses.RecreateTable <PVPotential>();
            var srcHouses          = dbSrcHouses.Fetch <House>();
            var srcPVPotentials    = dbSrcHouses.Fetch <PVPotential>();
            var srcPvSystemEntries = dbSrcHouses.Fetch <PvSystemEntry>();
            var hausanschlusses    = dbDstHouses.Fetch <Hausanschluss>();

            dbDstHouses.BeginTransaction();
            foreach (var potential in srcPVPotentials)
            {
                potential.ID = 0;
                dbDstHouses.Save(potential);
            }

            //copy pv systems from previous slice
            HashSet <string>            houseGuidsForSystemsWithPV = new HashSet <string>();
            Dictionary <string, double> pvPotentialByHouseGuid     = new Dictionary <string, double>();

            foreach (var pvpot in srcPVPotentials)
            {
                if (!houseGuidsForSystemsWithPV.Contains(pvpot.HouseGuid))
                {
                    houseGuidsForSystemsWithPV.Add(pvpot.HouseGuid);
                    pvPotentialByHouseGuid.Add(pvpot.HouseGuid, 0);
                }

                pvPotentialByHouseGuid[pvpot.HouseGuid] += pvpot.SonnendachStromErtrag;
            }

            var potentialhousesForPvSystems = srcHouses.Where(x => houseGuidsForSystemsWithPV.Contains(x.Guid)).ToList();

            foreach (var entry in srcPvSystemEntries)
            {
                var toremove = potentialhousesForPvSystems.FirstOrDefault(x => x.Guid == entry.HouseGuid);
                if (toremove != null)
                {
                    potentialhousesForPvSystems.Remove(toremove);
                }

                if (entry.PVAreas.Count == 0)
                {
                    throw new FlaException("No PV System areas defined.");
                }

                entry.ID = 0;
                dbDstHouses.Save(entry);
            }

            var  pvToInstallInkWh   = slice.PvPowerToInstallInGwh * 1_000_000;
            bool continueAllocation = true;
            int  pvSystemCount      = 0;

            while (pvToInstallInkWh > 0 && continueAllocation)
            {
                //make ranges
                var rangeEntries = SetRanges(potentialhousesForPvSystems, pvPotentialByHouseGuid);
                if (rangeEntries.Count == 0)
                {
                    continueAllocation = false;
                    continue;
                }

                //randomly pick
                var max        = rangeEntries.Max(x => x.EndRange);
                var pick       = Services.Rnd.NextDouble() * max;
                var rangeEntry = rangeEntries.Single(x => pick >= x.StartRange && pick <= x.EndRange);
                //remove house
                potentialhousesForPvSystems.Remove(rangeEntry.House);
                //save pvsystementry
                var pvPotenial = pvPotentialByHouseGuid[rangeEntry.House.Guid];
                pvSystemCount++;
                string erzeugerid    = "PV-" + slice.DstYear + "-" + pvSystemCount;
                var    hausanschlsus = rangeEntry.House.GetHausanschlussByIsn(new List <int>(), erzeugerid, hausanschlusses, MyLogger) ??
                                       throw new FlaException("no hausanschluss");
                if (hausanschlsus.ObjectID.ToLower().Contains("leuchte"))
                {
                    throw new FlaException("pv an leuchte in " + slice.DstYear + " " + hausanschlsus.ObjectID);
                }

                var pvSystemEntry = new PvSystemEntry(rangeEntry.House.Guid,
                                                      Guid.NewGuid().ToString(),
                                                      hausanschlsus.Guid,
                                                      rangeEntry.House.ComplexName,
                                                      erzeugerid,
                                                      slice.DstYear)
                {
                    EffectiveEnergyDemand = pvPotenial
                };
                var areas = srcPVPotentials.Where(x => x.HouseGuid == rangeEntry.House.Guid).ToList();
                foreach (var area in areas)
                {
                    pvSystemEntry.PVAreas.Add(new PVSystemArea(area.Ausrichtung, area.Neigung, area.SonnendachStromErtrag));
                }

                if (pvSystemEntry.PVAreas.Count == 0)
                {
                    throw new FlaException("No PV System areas defined.");
                }

                pvToInstallInkWh       -= pvSystemEntry.EffectiveEnergyDemand;
                pvSystemEntry.BuildYear = slice.DstYear;
                dbDstHouses.Save(pvSystemEntry);
                //deduct from pvtoinstall
            }

            dbDstHouses.CompleteTransaction();
            var           newPVs = dbDstHouses.FetchAsRepo <PvSystemEntry>();
            RowCollection rc     = new RowCollection("pv", "pv");

            foreach (var pv in newPVs)
            {
                foreach (var area in pv.PVAreas)
                {
                    RowBuilder rb = RowBuilder.Start("HA", pv.HausAnschlussGuid);
                    rc.Add(rb);
                    rb.Add("Azimut", area.Azimut);
                    rb.Add("Tilt", area.Tilt);
                    rb.Add("Energy", area.Energy);
                }
            }

            var fn = MakeAndRegisterFullFilename("PVExport.xlsx", slice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var dbSrcHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice.PreviousSliceNotNull);
            var dbDstHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);

            dbDstHouses.RecreateTable <HeatingSystemEntry>();
            var srcHeatingSystems = dbSrcHouses.Fetch <HeatingSystemEntry>();
            var srcHouses         = dbSrcHouses.Fetch <House>();
            var dstHouses         = dbDstHouses.FetchAsRepo <House>();

            if (srcHeatingSystems.Count == 0)
            {
                throw new FlaException("No heating systems were found");
            }

            double totalHeatingDemand = srcHeatingSystems.Sum(x => x.HeatDemand);
            double totalEbf           = srcHouses.Sum(x => x.EnergieBezugsFläche);
            double averageEnergyIntensityForNewAppartments = totalHeatingDemand / totalEbf * 0.5;

            //add heating demand from new appartments
            foreach (var hse in srcHeatingSystems)
            {
                var house = dstHouses.GetByGuid(hse.HouseGuid);
                foreach (var expansion in house.Appartments)
                {
                    var hseExpansion = hse.HeatDemands.FirstOrDefault(x => x.HouseExpansionGuid == expansion.Guid);
                    if (hseExpansion == null)
                    {
                        AppartmentHeatingDemand hsex = new AppartmentHeatingDemand(expansion.Guid,
                                                                                   expansion.EnergieBezugsFläche,
                                                                                   averageEnergyIntensityForNewAppartments * expansion.EnergieBezugsFläche,
                                                                                   slice.DstYear);
                        hse.HeatDemands.Add(hsex);
                    }
                }
            }

            var dstHousesByGuid = dstHouses.ToDictionary(x => x.Guid, x => x);

            DumpHeatingSystemsToXlsx(slice, srcHeatingSystems, dstHousesByGuid);

            //building renovations
            WeightedRandomAllocator <HeatingSystemEntry> wra = new WeightedRandomAllocator <HeatingSystemEntry>(Services.Rnd, MyLogger);

            var potentialRenovationHeatingSystems = srcHeatingSystems
                                                    .Where(x => x.Age > 10 && x.HeatDemand > 0 && Math.Abs(x.HeatDemand - x.OriginalHeatDemand2017) < 0.1).ToList();

            if (potentialRenovationHeatingSystems.Count == 0)
            {
                throw new FlaException("Not a single heating system found");
            }

            double averageRenovationFactor = slice.AverageHouseRenovationFactor;

            if (Math.Abs(averageRenovationFactor) < 0.000001)
            {
                throw new FlaException("Renovation factor was 0 for scenario " + slice);
            }

            int numberOfBuildingRenovations = (int)(slice.RenovationRatePercentage * srcHouses.Count);

            Info("renovating houses, target number " + numberOfBuildingRenovations + " GWh, renovation factor " + averageRenovationFactor);
            bool failOnOversubscribe = slice.DstYear != 2050;
            var  systemsToRenovate   = wra.PickNumberOfObjects(potentialRenovationHeatingSystems,
                                                               WeighingFunctionForRenovation,
                                                               numberOfBuildingRenovations,
                                                               failOnOversubscribe);

            Info("Renovating " + systemsToRenovate.Count + " houses with a total heat demand of " + systemsToRenovate.Sum(x => x.HeatDemand));
            foreach (var entry in systemsToRenovate)
            {
                entry.RenovateAllHeatDemand(averageRenovationFactor);
            }


            //change heating systems
            var changeableHeatingSystems = srcHeatingSystems.ToList();
            int elapsedTime = slice.DstYear - slice.PreviousSliceNotNull.DstYear;

            foreach (var heatingSystemEntry in srcHeatingSystems)
            {
                heatingSystemEntry.Age += elapsedTime;
            }


            int           yearsToAge             = slice.DstYear - slice.PreviousSliceNotNull.DstYear;
            RowCollection rc                     = new RowCollection("Changes", "Changes");
            double        totalOilHeatDemand2017 = changeableHeatingSystems.Where(x => x.HeatingSystemType2017 == HeatingSystemType.Öl)
                                                   .Sum(x => x.OriginalHeatDemand2017);
            double oilenergyAmountToChange = totalOilHeatDemand2017 * slice.Energy2017PercentageFromOilToHeatpump;

            ChangeHeatingSystemsForOneType(slice, changeableHeatingSystems, HeatingSystemType.Öl, oilenergyAmountToChange, rc, dstHousesByGuid);
            double totalGasHeatDemand2017 = changeableHeatingSystems.Where(x => x.HeatingSystemType2017 == HeatingSystemType.Gas)
                                            .Sum(x => x.OriginalHeatDemand2017);
            double gasEnergyAmountToChange = totalGasHeatDemand2017 * slice.Energy2017PercentageFromGasToHeatpump;

            ChangeHeatingSystemsForOneType(slice, changeableHeatingSystems, HeatingSystemType.Gas, gasEnergyAmountToChange, rc, dstHousesByGuid);
            double totalOtherHeatDemand = changeableHeatingSystems.Where(x => x.HeatingSystemType2017 == HeatingSystemType.Other)
                                          .Sum(x => x.OriginalHeatDemand2017);
            double otherDemandToChange = slice.Energy2017PercentageFromOtherToHeatpump * totalOtherHeatDemand;

            ChangeHeatingSystemsForOneType(slice, changeableHeatingSystems, HeatingSystemType.Other, otherDemandToChange, rc, dstHousesByGuid);
            var fn = MakeAndRegisterFullFilename("HeatingChangeLog.xlsx", slice);

            if (rc.Rows.Count > 0)
            {
                XlsxDumper.WriteToXlsx(fn, rc);
            }

            dbDstHouses.BeginTransaction();
            foreach (HeatingSystemEntry heatingSystemEntry in srcHeatingSystems)
            {
                heatingSystemEntry.Age += yearsToAge;
                heatingSystemEntry.ID   = 0;

                dbDstHouses.Save(heatingSystemEntry);
            }

            dbDstHouses.CompleteTransaction();
            var srcHouseGuids = srcHouses.Select(x => x.Guid).ToHashSet();
            var dstHouseGuids = dstHouses.Select(x => x.Guid).ToHashSet();

            foreach (var heatingSystem in srcHeatingSystems)
            {
                srcHouseGuids.Should().Contain(heatingSystem.HouseGuid);
                dstHouseGuids.Should().Contain(heatingSystem.HouseGuid);
            }
        }
        public void DumpToExcel([NotNull] string dstPath, XlsResultOutputMode mode)
        {
            RowCollection rc = new RowCollection("GeneratedLoadProfiles", "GeneratedLoadProfiles");

            if (mode == XlsResultOutputMode.ByTrafoStationTree)
            {
                var trafostationen = Houses.SelectMany(x => x.HausAnschlussList).Select(y => y.Trafokreis).Distinct().ToList();
                foreach (var trafostation in trafostationen)
                {
                    rc.Add(RowBuilder.Start("Trafostation", trafostation));
                    foreach (var houseRo in Houses)
                    {
                        if (houseRo.HausAnschlussList.Any(x => x.Trafokreis == trafostation))
                        {
                            rc.Add(houseRo.ToRowBuilder());
                            foreach (HausAnschlussRo hausAnschlussRo in houseRo.HausAnschlussList)
                            {
                                if (hausAnschlussRo.Trafokreis == trafostation)
                                {
                                    rc.Add(hausAnschlussRo.ToRowBuilder(houseRo, mode));
                                    foreach (HouseComponentRo component in hausAnschlussRo.HouseComponents)
                                    {
                                        rc.Add(component.ToRowBuilder(houseRo, hausAnschlussRo, mode));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (mode == XlsResultOutputMode.ByTrafoStationHausanschlussTree)
            {
                var trafostationen = Houses.SelectMany(x => x.HausAnschlussList).Select(y => y.Trafokreis).Distinct().ToList();
                var haros          = Houses.SelectMany(x => x.HausAnschlussList).Distinct().ToList();
                haros.Sort((x, y) => String.Compare(x.ObjektID, y.ObjektID, StringComparison.Ordinal));
                foreach (var trafostation in trafostationen)
                {
                    rc.Add(RowBuilder.Start("Trafostation", trafostation));
                    var filteredHaros = haros.Where(x => x.Trafokreis == trafostation);
                    foreach (var anschlussRo in filteredHaros)
                    {
                        rc.Add(anschlussRo.ToRowBuilder(null, XlsResultOutputMode.ByTrafoStationHausanschlussTree));
                        var housesForAnschluss = Houses.Where(x => x.HausAnschlussList.Contains(anschlussRo)).ToList();
                        foreach (var houseRo in housesForAnschluss)
                        {
                            rc.Add(houseRo.ToRowBuilder());
                            foreach (HouseComponentRo component in anschlussRo.HouseComponents)
                            {
                                rc.Add(component.ToRowBuilder(houseRo, anschlussRo, mode));
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (HouseRo house in Houses)
                {
                    if (mode == XlsResultOutputMode.Tree)
                    {
                        rc.Add(house.ToRowBuilder());
                    }

                    foreach (var anschlussRo in house.HausAnschlussList)
                    {
                        if (mode == XlsResultOutputMode.Tree)
                        {
                            rc.Add(anschlussRo.ToRowBuilder(house, mode));
                        }

                        foreach (var component in anschlussRo.HouseComponents)
                        {
                            rc.Add(component.ToRowBuilder(house, anschlussRo, mode));
                        }
                    }
                }
            }

            XlsxDumper.WriteToXlsx(dstPath, rc);
        }
Exemplo n.º 19
0
        protected override void RunChartMaking()
        {
            var slice = Constants.PresentSlice;
            //var dbHouse = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var ap               = new AnalysisRepository(Services.RunningConfig);
            var dbHouse          = ap.GetSlice(Constants.PresentSlice);
            var houses           = dbHouse.Fetch <House>();
            var houseTypeEntries = dbHouse.Fetch <HouseTypeEntry>();

            MakeHouseTypeEntries();
            MakeHouseTypeMap();
            MakeEnergyUseXls();
            void MakeEnergyUseXls()
            {
                var        households   = dbHouse.Fetch <Household>();
                var        businesses   = dbHouse.Fetch <BusinessEntry>();
                var        infra        = dbHouse.Fetch <BuildingInfrastructure>();
                var        light        = dbHouse.Fetch <StreetLightingEntry>();
                var        dhw          = dbHouse.Fetch <DHWHeaterEntry>();
                var        rc           = new RowCollection("energy", "energy");
                RowBuilder rb           = RowBuilder.Start("Haushalte", households.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                var        bigcustomers = businesses.Where(x => x.EffectiveEnergyDemand > 20000);
                var        small        = businesses.Where(x => x.EffectiveEnergyDemand <= 20000);

                rb.Add("Geschäftskunden > 20 MWh", bigcustomers.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                rb.Add("Geschäftskunden < 20 MWh", small.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                rb.Add("Gebäudeinfrastruktur", infra.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                rb.Add("Strassenbeleuchtung", light.Sum(x => x.YearlyElectricityUse) / Constants.GWhFactor);
                rb.Add("Elektroboiler", dhw.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);

                rc.Add(rb);
                var fn = MakeAndRegisterFullFilename("EnergyTreeMap.xlsx", Constants.PresentSlice);

                XlsxDumper.WriteToXlsx(fn, rc);
                SaveToPublicationDirectory(fn, Constants.PresentSlice, "4.2");
            }

            void MakeHouseTypeEntries()
            {
                var ssa = new SingleSankeyArrow("HouseTypeEntries", 1500, MyStage,
                                                SequenceNumber, Name, slice, Services);

                ssa.AddEntry(new SankeyEntry("Houses", houses.Count, 5000, Orientation.Straight));
                var countsPerType = new Dictionary <HouseType, int>();

                foreach (var entry in houseTypeEntries)
                {
                    if (!countsPerType.ContainsKey(entry.HouseType))
                    {
                        countsPerType.Add(entry.HouseType, 0);
                    }

                    countsPerType[entry.HouseType]++;
                }

                foreach (var pair in countsPerType)
                {
                    ssa.AddEntry(new SankeyEntry(pair.Key.ToString(), pair.Value * -1, 2000, Orientation.Up));
                }

                Services.PlotMaker.MakeSankeyChart(ssa);
            }

            void MakeHouseTypeMap()
            {
                var rgbs = new Dictionary <HouseType, RGB>();
                var hs   = houseTypeEntries.Select(x => x.HouseType).Distinct().ToList();
                var idx  = 0;

                foreach (var type in hs)
                {
                    var rgb = ColorGenerator.GetRGB(idx++);
                    if (rgb.R == 255 && rgb.B == 255 && rgb.G == 255)
                    {
                        rgb = new RGB(200, 200, 200);
                    }

                    if (rgb.R == 255 && rgb.B == 0 && rgb.G == 0)
                    {
                        rgb = Constants.Orange;
                    }

                    rgbs.Add(type, rgb);
                }

                RGB GetColor(House h)
                {
                    var hse = houseTypeEntries.Single(x => x.HouseGuid == h.Guid);

                    return(rgbs[hse.HouseType]);
                }

                var mapPoints = houses.Select(x => x.GetMapColorForHouse(GetColor)).ToList();

                var filename      = MakeAndRegisterFullFilename("HouseTypeMap.png", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in rgbs)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeOsmMap(Name, filename, mapPoints, new List <WgsPoint>(), legendEntries, new List <LineEntry>());
            }
        }
Exemplo n.º 20
0
        protected override void MakeVisualization([NotNull] ScenarioSliceParameters slice, bool isPresent)
        {
            var          dbHouse         = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);
            List <House> houses1         = dbHouse.Fetch <House>();
            var          heatingsystems1 = dbHouse.Fetch <HeatingSystemEntry>();
            Dictionary <string, HeatingSystemEntry> heatingsystemsByGuid = new Dictionary <string, HeatingSystemEntry>();

            foreach (var hse in heatingsystems1)
            {
                heatingsystemsByGuid.Add(hse.HouseGuid, hse);
            }

            MakeHeatingSystemAnalysis();
            EnergyIntensityHistogram();
            MakeHeatingTypeIntensityMap();
            MakeHeatingTypeMap();
            MakeHeatingSystemSankey();
            HeatingSystemCountHistogram();
            MakeHeatingSystemMapError();
            MakeHeatingSystemMap();
            MakeFernwärmeTypeMap();
            if (isPresent)
            {
                PresentOnlyVisualisations(heatingsystemsByGuid, slice, houses1, dbHouse);
            }

            void MakeHeatingSystemAnalysis()
            {
                var filename = MakeAndRegisterFullFilename("analysis.csv", slice);
                var sw       = new StreamWriter(filename);

                sw.WriteLine("Original Heating system - Anzahl");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.OriginalHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Count);
                }

                sw.WriteLine("");
                sw.WriteLine("");
                sw.WriteLine("Original Heating system - Summe");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.OriginalHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Sum(x => x.EffectiveEnergyDemand));
                }

                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("Target Anzahl");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.SynthesizedHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Count);
                }

                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("Target Summe");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.SynthesizedHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Sum(x => x.EffectiveEnergyDemand));
                }

                sw.Close();
            }

            void EnergyIntensityHistogram()
            {
                var filename  = MakeAndRegisterFullFilename("EnergyIntensityHistogram.png", slice);
                var ages      = heatingsystems1.Select(x => x.CalculatedAverageHeatingEnergyDemandDensity).Where(y => y > 0).ToList();
                var barSeries = new List <BarSeriesEntry>();
                var h         = new Histogram(ages, 100);

                barSeries.Add(BarSeriesEntry.MakeBarSeriesEntry(h, out var colnames, "F0"));
                Services.PlotMaker.MakeBarChart(filename, "EnergyIntensityHistogram", barSeries, colnames);
                var xlsfilename    = MakeAndRegisterFullFilename("EnergyIntensity.xlsx", slice);
                var heatingSystems = heatingsystems1.Where(y => y.CalculatedAverageHeatingEnergyDemandDensityWithNulls != null).ToList();
                var rbDict         = new Dictionary <string, RowBuilder>();

                foreach (var hs in heatingSystems)
                {
                    int        bucket = (int)(Math.Floor(hs.CalculatedAverageHeatingEnergyDemandDensityWithNulls / 50 ?? 0) * 50);
                    string     key    = bucket.ToString();
                    string     et     = hs.EnergyType.ToString();
                    RowBuilder rb;
                    if (rbDict.ContainsKey(key))
                    {
                        rb = rbDict[key];
                    }
                    else
                    {
                        rb = RowBuilder.Start("Energieträger", key);
                        rbDict.Add(key, rb);
                    }

                    rb.AddToPossiblyExisting(et, 1);
                }
                RowCollection rc = new RowCollection("energy", "energy");

                foreach (var builder in rbDict)
                {
                    rc.Add(builder.Value);
                }
                RowCollection rc2 = new RowCollection("raw", "raw");

                foreach (var heatingSystemEntry in heatingsystems1)
                {
                    RowBuilder rb = RowBuilder.Start("Träger", heatingSystemEntry.EnergyType)
                                    .Add("Effektiv", heatingSystemEntry.EffectiveEnergyDemand)
                                    .Add("EBF", heatingSystemEntry.Ebf);
                    rc2.Add(rb);
                }
                XlsxDumper.WriteToXlsx(xlsfilename, rc, rc2);
            }

            void MakeHeatingSystemSankey()
            {
                var ssa1 = new SingleSankeyArrow("HouseHeatingSystems", 1500, MyStage, SequenceNumber, Name, slice, Services);

                ssa1.AddEntry(new SankeyEntry("Houses", houses1.Count, 5000, Orientation.Straight));
                var ssa2 = new SingleSankeyArrow("EnergyBySystems", 1500, MyStage, SequenceNumber, Name, slice, Services);

                ssa2.AddEntry(new SankeyEntry("Houses", heatingsystems1.Sum(x => x.EffectiveEnergyDemand) / 1000000, 5000, Orientation.Straight));
                var counts = new Dictionary <HeatingSystemType, int>();
                var energy = new Dictionary <HeatingSystemType, double>();

                foreach (var entry in heatingsystems1)
                {
                    if (!counts.ContainsKey(entry.SynthesizedHeatingSystemType))
                    {
                        counts.Add(entry.SynthesizedHeatingSystemType, 0);
                        energy.Add(entry.SynthesizedHeatingSystemType, 0);
                    }

                    counts[entry.SynthesizedHeatingSystemType]++;
                    energy[entry.SynthesizedHeatingSystemType] += entry.EffectiveEnergyDemand;
                }

                var i = 1;

                foreach (var pair in counts)
                {
                    ssa1.AddEntry(new SankeyEntry(pair.Key.ToString(), pair.Value * -1, 2000 * i, Orientation.Up));
                    i++;
                }

                i = 1;
                foreach (var pair in energy)
                {
                    ssa2.AddEntry(new SankeyEntry(pair.Key.ToString(), pair.Value * -1 / 1000000, 2000 * i, Orientation.Up));
                    i++;
                }

                Services.PlotMaker.MakeSankeyChart(ssa1);
                Services.PlotMaker.MakeSankeyChart(ssa2);
            }

            void HeatingSystemCountHistogram()
            {
                var counts = new Dictionary <HeatingSystemType, int>();

                foreach (var entry in heatingsystems1)
                {
                    if (!counts.ContainsKey(entry.SynthesizedHeatingSystemType))
                    {
                        counts.Add(entry.SynthesizedHeatingSystemType, 0);
                    }

                    counts[entry.SynthesizedHeatingSystemType]++;
                }

                var filename  = MakeAndRegisterFullFilename("HeatingSystemHistogram.png", slice);
                var names     = new List <string>();
                var barSeries = new List <BarSeriesEntry>();
                var column    = 0;

                foreach (var pair in counts)
                {
                    names.Add(pair.Value.ToString());
                    var count = pair.Value;
                    barSeries.Add(BarSeriesEntry.MakeBarSeriesEntry(pair.Key.ToString(), count, column));
                    column++;
                }

                Services.PlotMaker.MakeBarChart(filename, "HeatingSystemHistogram", barSeries, names);
            }

            void MakeHeatingSystemMapError()
            {
                RGB GetColor(House h)
                {
                    var hse = heatingsystemsByGuid[h.Guid];

                    if (hse.OriginalHeatingSystemType == HeatingSystemType.Fernwärme)
                    {
                        return(Constants.Red);
                    }

                    if (hse.OriginalHeatingSystemType == HeatingSystemType.Gas)
                    {
                        return(Constants.Orange);
                    }

                    if (hse.OriginalHeatingSystemType == HeatingSystemType.FeuerungsstättenGas)
                    {
                        return(Constants.Orange);
                    }

                    return(Constants.Black);
                }

                var mapPoints = houses1.Select(x => x.GetMapPoint(GetColor)).ToList();

                var filename      = MakeAndRegisterFullFilename("KantonHeatingSystemErrors.svg", slice);
                var legendEntries = new List <MapLegendEntry> {
                    new MapLegendEntry("Kanton Fernwärme", Constants.Red),
                    new MapLegendEntry("Kanton Gas", Constants.Orange)
                };

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
            }

            void MakeHeatingSystemMap()
            {
                var rgbs = new Dictionary <HeatingSystemType, RGB>();
                var hs   = heatingsystems1.Select(x => x.SynthesizedHeatingSystemType).Distinct().ToList();
                var idx  = 0;

                foreach (var type in hs)
                {
                    rgbs.Add(type, ColorGenerator.GetRGB(idx++));
                }

                RGB GetColor(House h)
                {
                    var hse = heatingsystemsByGuid[h.Guid];

                    return(rgbs[hse.SynthesizedHeatingSystemType]);
                }

                var mapPoints = houses1.Select(x => x.GetMapPoint(GetColor)).ToList();

                var filename      = MakeAndRegisterFullFilename("HeatingSystemMap.svg", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in rgbs)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
            }

            void MakeHeatingTypeMap()
            {
                var maxEnergy             = heatingsystems1.Max(x => x.EffectiveEnergyDemand);
                var colorsByHeatingSystem = new Dictionary <HeatingSystemType, RGB> {
                    {
                        HeatingSystemType.Gas, Constants.Orange
                    }, {
                        HeatingSystemType.Öl, Constants.Black
                    }, {
                        HeatingSystemType.Electricity, Constants.Green
                    }, {
                        HeatingSystemType.Heatpump, Constants.Green
                    }, {
                        HeatingSystemType.Fernwärme, Constants.Blue
                    }, {
                        HeatingSystemType.Other, Constants.Türkis
                    }, {
                        HeatingSystemType.None, Constants.Yellow
                    }
                };

                RGBWithSize GetColorWithSize(House h)
                {
                    var s      = heatingsystemsByGuid[h.Guid];
                    var energy = Math.Log(s.EffectiveEnergyDemand / maxEnergy * 100) * 50;

                    if (energy < 10)
                    {
                        energy = 10;
                    }

                    if (!colorsByHeatingSystem.ContainsKey(s.SynthesizedHeatingSystemType))
                    {
                        throw new Exception("undefined color for " + s.SynthesizedHeatingSystemType);
                    }

                    var rgb = colorsByHeatingSystem[s.SynthesizedHeatingSystemType];

                    return(new RGBWithSize(rgb.R, rgb.G, rgb.B, (int)energy));
                }

                RGB GetColor(House h)
                {
                    var s = heatingsystemsByGuid[h.Guid];

                    if (!colorsByHeatingSystem.ContainsKey(s.SynthesizedHeatingSystemType))
                    {
                        throw new Exception("undefined color for " + s.SynthesizedHeatingSystemType);
                    }

                    var rgb = colorsByHeatingSystem[s.SynthesizedHeatingSystemType];

                    return(new RGB(rgb.R, rgb.G, rgb.B));
                }

                var mapPoints     = houses1.Select(x => x.GetMapPointWithSize(GetColorWithSize)).ToList();
                var filename      = MakeAndRegisterFullFilename("MapHeatingTypeAndSystemPerHousehold.svg", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in colorsByHeatingSystem)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
                var filenameOsm = MakeAndRegisterFullFilename("MapHeatingTypeAndSystemPerHouseholdOsm.png", slice);

                legendEntries.Add(new MapLegendEntry("Nicht Stadtgebiet", Constants.Red));
                var mceh = houses1.Select(x => x.GetMapColorForHouse(GetColor)).ToList();

                Services.PlotMaker.MakeOsmMap("HeatingTypeMap", filenameOsm, mceh, new List <WgsPoint>(), legendEntries, new List <LineEntry>());
            }

            void MakeFernwärmeTypeMap()
            {
                RGBWithLabel GetColor(House h)
                {
                    var s = heatingsystemsByGuid[h.Guid];

                    if (s.SynthesizedHeatingSystemType == HeatingSystemType.Fernwärme)
                    {
                        return(new RGBWithLabel(Constants.Green, ""));
                    }

                    return(new RGBWithLabel(Constants.Blue, "")); //h.ComplexName
                }

                var legendEntries = new List <MapLegendEntry>();
                var filenameOsm   = MakeAndRegisterFullFilename("FernwärmeOSM.png", slice);

                legendEntries.Add(new MapLegendEntry("Nicht Stadtgebiet", Constants.Red));
                legendEntries.Add(new MapLegendEntry("Nicht Fernwärme", Constants.Blue));
                legendEntries.Add(new MapLegendEntry("Fernwärme", Constants.Green));
                var mceh = houses1.Select(x => x.GetMapColorForHouse(GetColor)).ToList();

                Services.PlotMaker.MakeOsmMap("HeatingTypeMap", filenameOsm, mceh, new List <WgsPoint>(), legendEntries, new List <LineEntry>());
            }

            void MakeHeatingTypeIntensityMap()
            {
                var colorsByHeatingSystem = new Dictionary <HeatingSystemType, RGB> {
                    {
                        HeatingSystemType.Gas, Constants.Orange
                    }, {
                        HeatingSystemType.Öl, Constants.Black
                    }, {
                        HeatingSystemType.Electricity, Constants.Green
                    }, {
                        HeatingSystemType.Heatpump, Constants.Green
                    }, {
                        HeatingSystemType.Fernwärme, Constants.Blue
                    }, {
                        HeatingSystemType.Other, Constants.Türkis
                    }, {
                        HeatingSystemType.None, Constants.Türkis
                    }
                };

                RGBWithSize GetColor(House h)
                {
                    var s      = heatingsystemsByGuid[h.Guid];
                    var energy = s.CalculatedAverageHeatingEnergyDemandDensity / 10;

                    if (energy < 10)
                    {
                        energy = 10;
                    }

                    if (!colorsByHeatingSystem.ContainsKey(s.SynthesizedHeatingSystemType))
                    {
                        throw new Exception("undefined color for " + s.SynthesizedHeatingSystemType);
                    }

                    var rgb = colorsByHeatingSystem[s.SynthesizedHeatingSystemType];

                    return(new RGBWithSize(rgb.R, rgb.G, rgb.B, (int)energy));
                }

                var mapPoints     = houses1.Select(x => x.GetMapPointWithSize(GetColor)).ToList();
                var filename      = MakeAndRegisterFullFilename("MapHeatingTypeAndIntensityPerHouse.svg", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in colorsByHeatingSystem)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
            }
        }
        protected override void RunActualProcess()
        {
            var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouses.RecreateTable <DHWHeaterEntry>();
            var houses          = dbHouses.Fetch <House>();
            var households      = dbHouses.Fetch <Household>();
            var houseHeatings   = dbHouses.Fetch <HouseHeating>();
            var hausanschlusses = dbHouses.Fetch <Hausanschluss>();

            if (houseHeatings.All(x => x.KantonDhwMethods.Count == 0))
            {
                throw new Exception("not a single  dhw heating method was set");
            }

            if (houseHeatings.All(x => x.KantonHeatingMethods.Count == 0))
            {
                throw new Exception("not a single  space heating method was set");
            }

            RowCollection rc = new RowCollection("Sheet1", "Sheet1");

            dbHouses.BeginTransaction();
            foreach (var house in houses)
            {
                var householdInHouse = households.Where(x => x.HouseGuid == house.Guid).ToList();
                var occupantsInHouse = householdInHouse.SelectMany(x => x.Occupants).ToList();
                var dhwHeaterEntry   = new DHWHeaterEntry(house.Guid, Guid.NewGuid().ToString(), "DHW@" + house.ComplexName);
                var houseHeating     = houseHeatings.Single(x => x.HouseGuid == house.Guid);
                var heatingMethod    = HeatingSystemType.Unbekannt;
                if (houseHeating.KantonDhwMethods.Count > 0)
                {
                    heatingMethod = houseHeating.GetDominantDhwHeatingMethod();
                }

                var        peopleInHouse = occupantsInHouse.Count;
                RowBuilder rb            = RowBuilder.Start("House", house.ComplexName);
                rc.Add(rb);
                rb.Add("Households", households.Count);
                rb.Add("Persons", occupantsInHouse.Count);

                rb.Add("Ebbe DHW Estimate", houseHeating.KantonWarmwasserEnergyDemand);
                switch (heatingMethod)
                {
                case HeatingSystemType.Electricity:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    // electricity at night
                    break;

                case HeatingSystemType.SolarThermal:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.Gas:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Gasheating;
                    break;

                case HeatingSystemType.Heatpump:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.Fernwärme:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.DistrictHeating;
                    break;

                case HeatingSystemType.Other:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.None:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.None;
                    break;

                case HeatingSystemType.Öl:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.OilHeating;
                    break;

                case HeatingSystemType.Holz:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.Unbekannt:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.GasheatingLocalnet:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Gasheating;
                    break;

                case HeatingSystemType.FernwärmeLocalnet:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                case HeatingSystemType.FeuerungsstättenOil:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                case HeatingSystemType.FeuerungsstättenGas:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                case HeatingSystemType.Kohle:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                default: throw new Exception("Unknown heating method: " + heatingMethod);
                }

                rb.Add("Heating Method", dhwHeaterEntry.DhwHeatingSystemType);

                double        totalEnergy = 0;
                double        dhwEnergy   = 0;
                Hausanschluss hausanschluss;
                string        hausanschlussGuid = null;
                string        standort          = null;
                foreach (var hh in householdInHouse)
                {
                    double personEnergy = hh.EffectiveEnergyDemand / hh.Occupants.Count;
                    if (personEnergy > 1800)
                    {
                        dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    }
                }

                foreach (var hh in householdInHouse)
                {
                    double hhdhwEnergy  = 0;
                    double personEnergy = hh.EffectiveEnergyDemand / hh.Occupants.Count;

                    if (dhwHeaterEntry.DhwHeatingSystemType == DhwHeatingSystem.Electricity)
                    {
                        hhdhwEnergy = CalculateDHWEnergy(personEnergy) * hh.Occupants.Count;
                    }

                    var rbhh = RowBuilder.Start("household", hh.Name);
                    rbhh.Add("Persons", hh.Occupants.Count);
                    rbhh.Add("Energy Per Person in Household [kWh]", hhdhwEnergy);
                    rbhh.Add("Household DHW heating method", dhwHeaterEntry.DhwHeatingSystemType);
                    rbhh.Add("Household Energy", hh.EffectiveEnergyDemand);
                    rc.Add(rbhh);
                    if (Math.Abs(hh.EffectiveEnergyDemand - hh.LocalnetLowVoltageYearlyTotalElectricityUse) > 0.1)
                    {
                        if (Math.Abs(hh.EffectiveEnergyDemand - (hh.LocalnetLowVoltageYearlyTotalElectricityUse + dhwEnergy)) > 0.1)
                        {
                            throw new FlaException("Energy use does not fit");
                        }
                    }

                    hh.SetEnergyReduction("DHW", hhdhwEnergy);
                    if (hh.EffectiveEnergyDemand < 0)
                    {
                        throw new FlaException("Effective Energy demand was null");
                    }

                    totalEnergy      += hh.EffectiveEnergyDemand;
                    hausanschlussGuid = hh.HausAnschlussGuid;
                    standort          = hh.Standort;
                    dbHouses.Save(hh);
                    dhwEnergy += hhdhwEnergy;
                }

                if (hausanschlussGuid != null)
                {
                    hausanschluss = hausanschlusses.Single(x => x.Guid == hausanschlussGuid);
                }
                else
                {
                    hausanschluss = house.GetHausanschlussByIsn(new List <int>(), null, hausanschlusses, MyLogger) ??
                                    throw new FlaException("no hausanschluss");
                }

                dhwHeaterEntry.Standort              = standort;
                dhwHeaterEntry.HausAnschlussGuid     = hausanschluss.Guid;
                dhwHeaterEntry.EffectiveEnergyDemand = dhwEnergy;
                if (totalEnergy < 0)
                {
                    throw new FlaException("Negative total energy");
                }

                rb.Add("Total Energy Originally [kWh]", totalEnergy);
                rb.Add("Total Energy DHW [kWh]", dhwEnergy);
                rb.Add("DHW Power [kWh]", dhwEnergy / 365 / 2);
                rb.Add("Total Energy After Dhw [kWh]", totalEnergy - dhwEnergy);
                rb.Add("Energy Per Person [kWh]", totalEnergy / peopleInHouse);

                dbHouses.Save(dhwHeaterEntry);
            }

            dbHouses.CompleteTransaction();
            var hhdump = MakeAndRegisterFullFilename("Households.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(hhdump, rc);
        }
        protected override void RunActualProcess(ScenarioSliceParameters slice)
        {
            if (!slice.Equals(Constants.PresentSlice))
            {
                return;
            }

            var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);

            Info("using house db in  " + dbHouses.ConnectionString);
            var houses = dbHouses.Fetch <House>();
            HouseComponentRepository hcr = new HouseComponentRepository(dbHouses);
            var hausanschlusses          = dbHouses.Fetch <Hausanschluss>();
            var dbRaw                   = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var suppIsn                 = dbRaw.Fetch <HausanschlussImportSupplement>();
            var standortDict            = new Dictionary <string, string>();
            var rlms                    = dbRaw.Fetch <RlmProfile>();
            List <AssignmentEntry> ases = new List <AssignmentEntry>();

            foreach (var supplement in suppIsn)
            {
                if (!string.IsNullOrWhiteSpace(supplement.TargetStandort))
                {
                    if (standortDict.ContainsKey(supplement.TargetStandort))
                    {
                        throw new FlaException("Already contains standort " + supplement.TargetStandort);
                    }

                    standortDict.Add(supplement.TargetStandort, supplement.HaObjectid);
                }

                AssignmentEntry ase = ases.FirstOrDefault(x => x.ObjectId == supplement.HaObjectid);
                if (ase == null)
                {
                    ase = new AssignmentEntry(supplement.HaObjectid);
                    ases.Add(ase);
                }

                ase.Targets.Add(supplement.TargetStandort);
                ase.TargetCount++;
            }

            List <string>        checkedStandorte       = new List <string>();
            var                  lgzs                   = ReadZuordnungLastgänge();
            var                  successfullAssignments = new List <LastgangZuordnung>();
            RowCollection        rc = new RowCollection("sheet", "Sheet1");
            List <PvSystemEntry> assignedPVsystems = new List <PvSystemEntry>();
            List <PvSystemEntry> otherPVsystems    = new List <PvSystemEntry>();

            foreach (House house in houses)
            {
                var houseComponents = house.CollectHouseComponents(hcr);
                foreach (var component in houseComponents)
                {
                    if (component.Standort == null)
                    {
                        continue;
                    }

                    if (standortDict.ContainsKey(component.Standort))
                    {
                        Hausanschluss ha             = hausanschlusses.Single(x => x.Guid == component.HausAnschlussGuid);
                        string        targetObjectID = standortDict[component.Standort];
                        if (ha.ObjectID != targetObjectID)
                        {
                            throw new FlaException("Incorrect hausanschluss for " + component.Name + ": was supposed to be " + targetObjectID +
                                                   " but instead was " + ha.ObjectID + " (standort: " + component.Standort);
                        }

                        var ase = ases.Single(x => x.ObjectId == ha.ObjectID);
                        ase.AssignedCount++;
                        ase.Assigned.Add(component.Standort);
                        checkedStandorte.Add(component.Standort);
                    }

                    if (component.HouseComponentType == HouseComponentType.BusinessWithLastgangHighVoltage)
                    {
                        var businessEntry = (BusinessEntry)component;
                        var rlmfn         = businessEntry.RlmProfileName ?? throw new FlaException("No file");
                        var ha            = hausanschlusses.First(x => x.Guid == businessEntry.HausAnschlussGuid);
                        var rlm           = rlms.Single(x => x.Name == businessEntry.RlmProfileName);
                        var energysum     = new Profile(rlm.Profile).EnergySum();
                        LogAssignment(lgzs,
                                      rlmfn,
                                      ha,
                                      successfullAssignments,
                                      rc,
                                      businessEntry.Name,
                                      businessEntry.Standort,
                                      suppIsn,
                                      "HS",
                                      businessEntry.EffectiveEnergyDemand,
                                      energysum,
                                      house.ComplexName,
                                      businessEntry.FinalIsn);
                    }

                    if (component.HouseComponentType == HouseComponentType.BusinessWithLastgangLowVoltage)
                    {
                        var businessEntry = (BusinessEntry)component;
                        var rlmfn         = businessEntry.RlmProfileName ?? throw new FlaException("no file?");
                        var ha            = hausanschlusses.First(x => x.Guid == businessEntry.HausAnschlussGuid);
                        var rlm           = rlms.Single(x => x.Name == businessEntry.RlmProfileName);
                        var energysum     = new Profile(rlm.Profile).EnergySum();
                        LogAssignment(lgzs,
                                      rlmfn,
                                      ha,
                                      successfullAssignments,
                                      rc,
                                      businessEntry.Name,
                                      businessEntry.Standort,
                                      suppIsn,
                                      "NS",
                                      businessEntry.EffectiveEnergyDemand,
                                      energysum,
                                      house.ComplexName,
                                      businessEntry.FinalIsn);
                    }

                    if (component.HouseComponentType == HouseComponentType.Kwkw)
                    {
                        var kwkw      = (KleinWasserkraft)component;
                        var rlmfn     = kwkw.RlmProfileName;
                        var ha        = hausanschlusses.First(x => x.Guid == kwkw.HausAnschlussGuid);
                        var rlm       = rlms.Single(x => x.Name == kwkw.RlmProfileName);
                        var energysum = new Profile(rlm.Profile).EnergySum();
                        LogAssignment(lgzs,
                                      rlmfn,
                                      ha,
                                      successfullAssignments,
                                      rc,
                                      kwkw.Name,
                                      kwkw.Standort,
                                      suppIsn,
                                      "WKW",
                                      kwkw.EffectiveEnergyDemand,
                                      energysum,
                                      house.ComplexName,
                                      kwkw.FinalIsn);
                    }

                    if (component.HouseComponentType == HouseComponentType.Photovoltaik)
                    {
                        var ha = hausanschlusses.First(x => x.Guid == component.HausAnschlussGuid);
                        if (lgzs.Any(x => string.Equals(x.Knoten.ToLower(), ha.ObjectID.ToLower(), StringComparison.InvariantCultureIgnoreCase)))
                        {
                            assignedPVsystems.Add((PvSystemEntry)component);
                        }
                        else
                        {
                            otherPVsystems.Add((PvSystemEntry)component);
                        }
                    }
                }
            }

            foreach (var zuordnung in lgzs)
            {
                if (!successfullAssignments.Contains(zuordnung))
                {
                    RowBuilder rb         = RowBuilder.Start("Lastgang", zuordnung.FileName).Add("Diren Profilename", zuordnung.Knoten);
                    var        has        = hausanschlusses.Where(x => x.ObjectID == zuordnung.Knoten).ToList();
                    var        haguids    = has.Select(x => x.Guid).Distinct().ToList();
                    var        pvs        = assignedPVsystems.Where(x => haguids.Contains(x.HausAnschlussGuid)).ToList();
                    var        houseGuids = has.Select(x => x.HouseGuid).Distinct().ToList();
                    var        otherPV    = otherPVsystems.Where(x => houseGuids.Contains(x.HouseGuid));
                    rb.Add("PV Systems", string.Join(";", pvs.Select(x => x.Name)));
                    rb.Add("Other PV Systems@house", string.Join(";", otherPV.Select(x => x.Name)));
                    var rlm = rlms.Where(x => x.Name.Contains(zuordnung.FileName)).ToList();
                    for (int i = 0; i < rlm.Count; i++)
                    {
                        var energysum = new Profile(rlm[i].Profile).EnergySum();
                        rb.Add("Profilesumme " + i, energysum);
                    }

                    rc.Add(rb);
                    if (has.Count > 0)
                    {
                        List <string> housenames = new List <string>();
                        foreach (var ha in has)
                        {
                            var house = houses.First(x => x.Guid == ha.HouseGuid);
                            housenames.Add(house.ComplexName);
                        }

                        rb.Add("Hausname", string.Join(",", housenames.Distinct()));
                    }
                }
            }

            foreach (var pVsystem in otherPVsystems)
            {
                House house = houses.First(x => x.Guid == pVsystem.HouseGuid);
                var   rb    = RowBuilder.Start("Hausname", house.ComplexName);
                rb.Add("Effective Energy", pVsystem.EffectiveEnergyDemand);
                rb.Add("Planned Energy", pVsystem.EffectiveEnergyDemand);
                var ha = hausanschlusses.First(x => x.Guid == pVsystem.HausAnschlussGuid);
                rb.Add("Zugeordnete ObjektID", ha.ObjectID);
                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("WrongAssignments.xlsx", slice);

            XlsxDumper.WriteToXlsx(fn, rc);

            var missingAssigments = lgzs.Where(x => !successfullAssignments.Contains(x)).ToList();

            if (missingAssigments.Count > 0)
            {
                Info("Missing assignments: " + string.Join("\n", missingAssigments.Select(x => x.FileName)));
            }

            foreach (var pair in standortDict)
            {
                if (!checkedStandorte.Contains(pair.Key))
                {
                    //throw new FlaException("Didn't find supplemental standort " + pair.Value + " in the list of components. Typo?");
                }
            }

            foreach (var ase in ases)
            {
                foreach (var target in ase.Targets)
                {
                    if (!ase.Assigned.Contains(target))
                    {
                        //  throw new FlaException("Missing " + target + " from the list of assigned standorts");
                    }
                }

                foreach (var assigned in ase.Assigned)
                {
                    if (!ase.Targets.Contains(assigned))
                    {
                        //throw new FlaException("Missing " + assigned + " from the list of target standorts");
                    }
                }
            }
        }
Exemplo n.º 23
0
        protected override void RunActualProcess()
        {
            var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouses.RecreateTable <HeatingSystemEntry>();
            var dbRaw               = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var dbComplexes         = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Complexes, Constants.PresentSlice);
            var rnd                 = new Random();
            var houses              = dbHouses.Fetch <House>();
            var hausanschlusses     = dbHouses.Fetch <Hausanschluss>();
            var houseHeatingMethods = dbHouses.Fetch <HouseHeating>();
            var feuerungsstättenRaw = dbRaw.Fetch <FeuerungsStaette>();
            var complexes           = dbComplexes.Fetch <BuildingComplex>();
            Dictionary <string, BuildingComplex> buildingComplexesByName = new Dictionary <string, BuildingComplex>();

            foreach (BuildingComplex complex in complexes)
            {
                buildingComplexesByName.Add(complex.ComplexName, complex);
            }

            var potentialHeatingSystems = dbHouses.Fetch <PotentialHeatingSystemEntry>();

            dbHouses.BeginTransaction();
            OverrideRepository overrideRepository = new OverrideRepository();
            double             totalFernwärme     = potentialHeatingSystems.Sum(x => x.YearlyFernwärmeDemand);
            var overrideEntries = overrideRepository.ReadEntries(Services);
            var houseNames      = houses.Select(x => x.ComplexName).ToHashSet();

            foreach (OverrideEntry entry in overrideEntries)
            {
                if (!houseNames.Contains(entry.ComplexName))
                {
                    throw new FlaException("Kein Haus für Override Entry " + entry.ComplexName + ". Vermutlich vertippt?");
                }
            }

            List <HeatingSystemEntry> hses = new List <HeatingSystemEntry>();

            foreach (var house in houses)
            {
                var complex = buildingComplexesByName[house.ComplexName];
                List <FeuerungsStaette> feuerungsStättenForHouse = feuerungsstättenRaw.Where(x => {
                    if (x.EGID == null)
                    {
                        throw new FlaException("x.EGID != null");
                    }

                    return(complex.EGids.Contains((long)x.EGID));
                }).ToList();

                //set the age of the heating system, randomly up to 30 years old
                var feuerungsstättenType = string.Join(",", feuerungsStättenForHouse.Select(x => x.Brennstoff?.ToString()).Distinct());
                var hausanschluss        = house.GetHausanschlussByIsn(new List <int>(), null, hausanschlusses, MyLogger, false);
                if (hausanschluss != null && hausanschluss.ObjectID.ToLower().Contains("kleinanschluss"))
                {
                    hausanschluss = null;
                }

                var hse = new HeatingSystemEntry(house.Guid,
                                                 Guid.NewGuid().ToString(),
                                                 feuerungsstättenType,
                                                 hausanschluss?.Guid,
                                                 house.ComplexName,
                                                 house.ComplexName + " - Heating System  - Unknown");
                hses.Add(hse);
                var oldestYear = feuerungsStättenForHouse.Min(x => x.KesselBaujahr);
                if (oldestYear == null || oldestYear.Value < 1800)
                {
                    hse.Age = rnd.Next(30);
                }
                else
                {
                    hse.Age = 2019 - oldestYear.Value;
                }

                hse.FeuerungsstättenPower = feuerungsStättenForHouse.Sum(x => {
                    if (x.KesselLeistung == null)
                    {
                        throw new FlaException("Kesselleistung was null");
                    }

                    return((double)x.KesselLeistung);
                });
                hse.EstimatedMinimumEnergyFromFeuerungsStätten = hse.FeuerungsstättenPower * 1500;
                hse.EstimatedMaximumEnergyFromFeuerungsStätten = hse.FeuerungsstättenPower * 2200;
                //this is a localnet heating system, so use the localnet information
                var           potentialHouseHeatingSystems = potentialHeatingSystems.Where(x => x.HouseGuid == house.Guid).ToList();
                var           houseHeatingMethod           = houseHeatingMethods.Single(x => x.HouseGuid == house.Guid);
                double        totalHeatDemand = 0;
                OverrideEntry ore             = overrideEntries.FirstOrDefault(x => x.ComplexName == house.ComplexName);
                if (ore != null)
                {
                    Debug("Override Entry for " + house.ComplexName);
                }

                if (potentialHouseHeatingSystems.Count > 0)
                {
                    var totalGas   = potentialHouseHeatingSystems.Sum(x => x.YearlyGasDemand);
                    var totalWärme = potentialHouseHeatingSystems.Sum(x => x.YearlyFernwärmeDemand);
                    if (totalGas > 0)
                    {
                        //localnet heizung
                        totalHeatDemand = totalGas;
                        hse.OriginalHeatingSystemType    = HeatingSystemType.GasheatingLocalnet;
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.Gas;
                        if (totalWärme > 0)
                        {
                            throw new Exception("Both wärme and gas");
                        }

                        if (ore != null)
                        {
                            throw new FlaException("Overrride Entry für Localnet Gas Gebäude: " + house.ComplexName +
                                                   ", but that doesn't make sense");
                        }
                    }

                    if (totalWärme > 0)
                    {
                        totalHeatDemand = totalWärme;
                        hse.OriginalHeatingSystemType    = HeatingSystemType.FernwärmeLocalnet;
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.Fernwärme;
                    }
                } // no data, so use feuerungsstätten
                else if (ore != null)
                {
                    //kanton heizung
                    hse.OriginalHeatingSystemType = HeatingSystemType.None;
                    if (ore.HeatingSystemType == HeatingSystemType.Heatpump && hse.HausAnschlussGuid == null)
                    {
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.None;
                    }

                    hse.SynthesizedHeatingSystemType = ore.HeatingSystemType;
                    totalHeatDemand = ore.Amount;
                }
                else if (feuerungsStättenForHouse.Count > 0)
                {
                    var fs = feuerungsStättenForHouse[0];
                    if (fs.Brennstoff == "Oel")
                    {
                        hse.OriginalHeatingSystemType    = HeatingSystemType.FeuerungsstättenOil;
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.Öl;
                        totalHeatDemand = houseHeatingMethod.KantonTotalEnergyDemand;
                    }
                    else if (fs.Brennstoff == "Gas")
                    {
                        hse.OriginalHeatingSystemType = HeatingSystemType.FeuerungsstättenGas;
                        //beco says gas, but can't be, because localnet says no
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.Öl;
                        totalHeatDemand = houseHeatingMethod.KantonTotalEnergyDemand;
                    }
                    else
                    {
                        throw new Exception("invalid heating system");
                    }
                } // no beco, so use ebbe daten
                else if (houseHeatingMethod.KantonHeatingMethods.Count > 0)
                {
                    //kanton heizung
                    var kantonHeatingMethod = houseHeatingMethod.KantonHeatingMethods[0];
                    GetKantonHeatingSystem(kantonHeatingMethod, hse, houseHeatingMethod, ref totalHeatDemand);
                }
                else if (feuerungsStättenForHouse.Count > 0)
                {
                    var fs = feuerungsStättenForHouse[0];
                    if (fs.Brennstoff == "Oel")
                    {
                        hse.OriginalHeatingSystemType    = HeatingSystemType.FeuerungsstättenOil;
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.Öl;
                        totalHeatDemand = houseHeatingMethod.KantonTotalEnergyDemand;
                    }
                    else if (fs.Brennstoff == "Gas")
                    {
                        hse.OriginalHeatingSystemType = HeatingSystemType.FeuerungsstättenGas;
                        //beco says gas, but can't be, because localnet says no
                        hse.SynthesizedHeatingSystemType = HeatingSystemType.Öl;
                        totalHeatDemand = houseHeatingMethod.KantonTotalEnergyDemand;
                    }
                    else
                    {
                        throw new Exception("invalid heating system");
                    }
                } // no beco, so use ebbe daten
                else if (houseHeatingMethod.KantonHeatingMethods.Count > 0)
                {
                    //kanton heizung
                    var kantonHeatingMethod = houseHeatingMethod.KantonHeatingMethods[0];

                    GetKantonHeatingSystem(kantonHeatingMethod, hse, houseHeatingMethod, ref totalHeatDemand);
                }
                else
                {
                    hse.OriginalHeatingSystemType    = HeatingSystemType.None;
                    hse.SynthesizedHeatingSystemType = HeatingSystemType.None;
                    totalHeatDemand = 0;
                }

                if (hse.SynthesizedHeatingSystemType == HeatingSystemType.Heatpump && hse.HausAnschlussGuid == null)
                {
                    hse.SynthesizedHeatingSystemType = HeatingSystemType.None;
                }

                if (hse.SynthesizedHeatingSystemType == HeatingSystemType.Electricity && hse.HausAnschlussGuid == null)
                {
                    throw new FlaException("electric heating without anschluss");
                }

                if (hse.SynthesizedHeatingSystemType == HeatingSystemType.Heatpump && hse.HausAnschlussGuid == null)
                {
                    throw new FlaException("hp heating without anschluss");
                }

                hse.Standort              = house.ComplexName + " - " + hse.SynthesizedHeatingSystemType;
                hse.ProvideProfile        = false;
                hse.HeatingSystemType2017 = hse.SynthesizedHeatingSystemType;
                if (house.Appartments.Count == 0)
                {
                    throw new FlaException("Not a single area in the house " + house.ComplexName);
                }

                double avgHeatDemand = totalHeatDemand / house.Appartments.Count;
                foreach (var appartment in house.Appartments)
                {
                    hse.HeatDemands.Add(new AppartmentHeatingDemand(appartment.Guid,
                                                                    appartment.EnergieBezugsFläche,
                                                                    avgHeatDemand,
                                                                    Constants.PresentSlice.DstYear));
                }

                if (Math.Abs(hse.HeatDemand - totalHeatDemand) > 0.01)
                {
                    throw new FlaException("Invalid heat demand");
                }

                hse.OriginalHeatDemand2017 = hse.HeatDemand;
                dbHouses.Save(hse);
            }

            dbHouses.CompleteTransaction();
            double finalFernwärme = hses.Where(x => x.SynthesizedHeatingSystemType == HeatingSystemType.Fernwärme).Sum(x => x.EffectiveEnergyDemand);

            if (Math.Abs(finalFernwärme - totalFernwärme) > 1)
            {
                throw new FlaException("Fernwärme changed: Nach allem:" + finalFernwärme + " davor: " + totalFernwärme);
            }

            RowCollection rc = new RowCollection("Validation", "Validierung");

            foreach (var feuerungsStaette in feuerungsstättenRaw)
            {
                string     adress = feuerungsStaette.Strasse + " " + feuerungsStaette.Hausnummer;
                RowBuilder rb     = RowBuilder.Start("FeuerungsAdresse", adress);
                rc.Add(rb);
                rb.Add("Brennstoff", feuerungsStaette.Brennstoff);
                rb.Add("Energienutzung", feuerungsStaette.Energienutzung);
                rb.Add("Leistung", feuerungsStaette.KesselLeistung);
                if (feuerungsStaette.EGID != null)
                {
                    int egid  = (int)feuerungsStaette.EGID;
                    var house = houses.FirstOrDefault(x => x.EGIDs.Contains(egid));
                    if (house != null)
                    {
                        rb.Add("Haus", house.ComplexName);
                        rb.Add("HausAdresse", house.Adress);
                        HeatingSystemEntry hse = hses.Single(x => x.HouseGuid == house.Guid);
                        rb.Add("Gewählter Heizungstyp 1", hse.OriginalHeatingSystemType.ToString());
                        rb.Add("Gewählter Heizungstyp 2", hse.SynthesizedHeatingSystemType.ToString());
                        rb.Add("EBF", house.EnergieBezugsFläche);
                    }
                    else
                    {
                        var h2 = houses.FirstOrDefault(x => x.Adress?.Contains(adress) == true);
                        if (h2 != null)
                        {
                            rb.Add("Findbar über Adresse", h2.ComplexName);

                            HeatingSystemEntry hse = hses.Single(x => x.HouseGuid == h2.Guid);
                            rb.Add("Gewählter Heizungstyp 1", hse.OriginalHeatingSystemType.ToString());
                            rb.Add("Gewählter Heizungstyp 2", hse.SynthesizedHeatingSystemType.ToString());
                            rb.Add("EBF", h2.EnergieBezugsFläche);
                        }
                    }
                }
                else
                {
                    rb.Add("Egid fehlt", "True");
                }
            }

            var fn = MakeAndRegisterFullFilename("Feuerungsstätten-Validation.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
        protected override void RunActualProcess()
        {
            var dbHouses            = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var dbHousesPersistence =
                Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice, DatabaseCode.Persistence);

            dbHousesPersistence.CreateTableIfNotExists <PersistentHouseholdResidents>();
            var dbRaw      = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var households = dbHouses.Fetch <Household>();
            var jahrgänge  = dbRaw.Fetch <Jahrgang>();

            if (households.Count == 0)
            {
                throw new Exception("no households founds");
            }

            if (households.Count != A05_HouseholdMaker.HouseholdAccordingToStadtverwaltung)
            {
                throw new FlaException("Missing households!?");
            }

            foreach (var household in households)
            {
                household.Occupants.Clear();
            }
            var persistentResidents  = dbHousesPersistence.Fetch <PersistentHouseholdResidents>();
            int householdCountBefore = households.Count;

            dbHouses.BeginTransaction();
            Info("found " + persistentResidents.Count + " persistent residents");
            foreach (var persistentHouseholdResidentse in persistentResidents)
            {
                var household = households.FirstOrDefault(x => x.HouseholdKey == persistentHouseholdResidentse.HouseholdKey);
                if (household == null)
                {
                    Info("Invalid persistence for " + persistentHouseholdResidentse.HouseholdKey);
                    dbHousesPersistence.Delete(persistentHouseholdResidentse);
                    continue;
                }

                foreach (var persistentOccupant in persistentHouseholdResidentse.Occupants)
                {
                    var occupant = new Occupant(household.Guid,
                                                Guid.NewGuid().ToString(),
                                                persistentOccupant.Age,
                                                persistentOccupant.Gender,
                                                household.HouseGuid,
                                                household.HouseholdKey);
                    household.Occupants.Add(occupant);
                    var jahrgang = 2018 - persistentOccupant.Age;
                    var einträge = jahrgänge.Single(x => x.Jahr == jahrgang);
                    einträge.Count--;
                    if (einträge.Count < 0)
                    {
                        throw new FlaException("Negative population");
                    }
                }

                dbHouses.Save(household);
                households.Remove(household);
            }

            Info("Covered " + (householdCountBefore - households.Count) + " households from persistence, households left: " + households.Count);

            var hmfc             = new HouseMemberFuzzyCalc(Services.MyLogger, MyStage);
            var potentialPersons = new List <PotentialPerson>();

            foreach (var jahrgang in jahrgänge)
            {
                var age = 2018 - jahrgang.Jahr;
                var g   = Gender.Male;
                for (var i = 0; i < jahrgang.Count; i++)
                {
                    var pp = new PotentialPerson(g, age);
                    potentialPersons.Add(pp);
                    g = g == Gender.Male ? Gender.Female : Gender.Male;
                }
            }

            var r = new Random(1);

            foreach (var household in households)
            {
                household.HeuristicFamiliySize = hmfc.GetPeopleCountForEnergy(household.EffectiveEnergyDemand);
                household.Occupants.Clear();
            }

            //put at least one person into every household
            foreach (var household in households)
            {
                var eligiablePersons = potentialPersons.Where(x => x.Age >= 18).ToList();
                var occ = MakeOccupant(eligiablePersons, r, potentialPersons, household);
                household.Occupants.Add(occ);
                dbHouses.Save(occ);
            }

            //put a second person into the households that might have a second one
            foreach (var household in households)
            {
                if (household.HeuristicFamiliySize < 2)
                {
                    continue;
                }

                var g                = household.Occupants[0].Gender;
                var otherGender      = g == Gender.Male ? Gender.Female : Gender.Male;
                var eligiablePersons = potentialPersons.Where(x => x.Age >= 18 && x.Gender == otherGender).ToList();
                if (eligiablePersons.Count == 0)
                {
                    eligiablePersons = potentialPersons;
                }

                var occ2 = MakeOccupant(eligiablePersons, r, potentialPersons, household);
                household.Occupants.Add(occ2);
            }

            var count = 0;

            while (potentialPersons.Count > 0)
            {
                count++;
                if (count > 100000)
                {
                    throw new Exception("Couldnt allocate everything after " + count + " iterations," + potentialPersons.Count + " left.");
                }

                var allocatedCount = 0;
                foreach (var household in households)
                {
                    if (household.Occupants.Count >= household.HeuristicFamiliySize)
                    {
                        continue;
                    }

                    if (potentialPersons.Count == 0)
                    {
                        break;
                    }

                    var eligiablePersonsKids = potentialPersons.Where(x => x.Age < 18).ToList();
                    if (eligiablePersonsKids.Count == 0)
                    {
                        eligiablePersonsKids = potentialPersons;
                    }

                    var occ3 = MakeOccupant(eligiablePersonsKids, r, potentialPersons, household);
                    household.Occupants.Add(occ3);
                    allocatedCount++;
                }

                if (allocatedCount == 0 && potentialPersons.Count > 0)
                {
                    var hhs = households.Where(x => x.HeuristicFamiliySize > 2).ToList();
                    if (hhs.Count == 0)
                    {
                        hhs = households;
                    }

                    var idx = Services.Rnd.Next(hhs.Count);
                    hhs[idx].HeuristicFamiliySize++;
                }
            }

            List <PersistentHouseholdResidents> newPersistentResidents = new List <PersistentHouseholdResidents>();
            int peopleCount = 0;

            foreach (var hh in households)
            {
                dbHouses.Save(hh);
                PersistentHouseholdResidents phhr = new PersistentHouseholdResidents(hh.HouseholdKey);

                foreach (var occupant in hh.Occupants)
                {
                    phhr.Occupants.Add(new PersistentOccupant(occupant.Age, occupant.Gender));
                    peopleCount++;
                }

                newPersistentResidents.Add(phhr);
            }

            dbHouses.CompleteTransaction();
            dbHousesPersistence.BeginTransaction();
            foreach (var phhr in newPersistentResidents)
            {
                dbHousesPersistence.Save(phhr);
            }

            Info("Saved " + newPersistentResidents.Count + " persistence records with a total of " + peopleCount + " people");
            dbHousesPersistence.CompleteTransaction();
            var allhouseholds = dbHouses.FetchAsRepo <Household>();
            var rc            = new RowCollection("occupants", "occupants");

            foreach (var hh in allhouseholds)
            {
                foreach (var occupant in hh.Occupants)
                {
                    var rb = RowBuilder.Start("age", occupant.Age).Add("Gender", occupant.Gender);
                    rc.Add(rb);
                }
            }

            var fn = MakeAndRegisterFullFilename("OccupantList.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }