예제 #1
0
        public void RunPVProviderTestForAllEntries()
        {
            ServiceRepository services = new ServiceRepository(null, null, Logger, Config, new Random(1));
            var slice             = new ScenarioSliceParameters(Scenario.FromEnum(ScenarioEnum.Utopia), 2050, null);
            var dbHouse           = services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);
            var pventries         = dbHouse.Fetch <PvSystemEntry>();
            var has               = dbHouse.Fetch <Hausanschluss>();
            var dbdto             = new DBDto(null, has, null, null, null);
            PVProfileProvider pvp = new PVProfileProvider(services, slice, dbdto);

            foreach (var entry in pventries)
            {
                HouseComponentRo     hcro = new HouseComponentRo("myname", "PV", 1, 1, "status", "isns", "standort", 0);
                ProviderParameterDto pp   = new ProviderParameterDto(entry, null, hcro);
                pvp.PrepareLoadProfileIfNeeded(pp);
                var prosumer = pvp.ProvideProfile(pp);
                if (prosumer?.Profile == null)
                {
                    throw new FlaException("No profile");
                }

                if (Math.Abs(prosumer.Profile.EnergySum() - entry.EffectiveEnergyDemand) > 0.1)
                {
                    throw new FlaException("Invalid profile: missing energy: should be " + entry.EffectiveEnergyDemand + " but was " +
                                           prosumer.Profile.EnergySum());
                }

                Info("Profile generated correctly.");
            }
        }
        public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters)
        {
            Module.SetPrint(0);
            //var relevantPotentials = _pvPotentials.Where(x => x.HouseGuid == houseComponent.HouseGuid);
            PvSystemEntry entry = (PvSystemEntry)parameters.HouseComponent;
            int           idx   = 0;

            foreach (var area in entry.PVAreas)
            {
                var key    = MakeKeyFromPVArea(area);
                var keystr = key.GetKey();
                //key has been checked in this run
                if (_checkedKeys.Contains(keystr))
                {
                    continue;
                }

                _checkedKeys.Add(keystr);
                bool isInDb = _saveableEntries.CheckForName(keystr, MyLogger);
                if (isInDb)
                {
                    continue;
                }

                Info("Missing pv profile for " + keystr + ", generating...");
                PVSystemSettings pvs = new PVSystemSettings(key, 1, 1, MyLogger, idx++);
                var profile          = pvs.Run(Services.RunningConfig);
                _saveableEntries.AddRow(profile);
                _saveableEntries.SaveDictionaryToDatabase(MyLogger);
            }

            return(true);
        }
예제 #3
0
        public void RunPVProviderTest()
        {
            // ReSharper disable AssignNullToNotNullAttribute
            var dbdto = new DBDto(null, null, null, null, null);
            // ReSharper restore AssignNullToNotNullAttribute
            // ReSharper disable twice AssignNullToNotNullAttribute
            ServiceRepository services = new ServiceRepository(null, null, Logger, Config, new Random(1));
            PVProfileProvider pvp      = new PVProfileProvider(services, Constants.PresentSlice, dbdto);
            PvSystemEntry     pve      = new PvSystemEntry("houseguid", "pvguid", "haguid", "myname", "pv123", 2017);

            pve.PVAreas = new List <PVSystemArea> {
                new PVSystemArea(30, 30, 1000)
            };
            HouseComponentRo     hcro = new HouseComponentRo("myname", "PV", 1, 1, "status", "isns", "standort", 0);
            ProviderParameterDto pp   = new ProviderParameterDto(pve, null, hcro);

            pvp.PrepareLoadProfileIfNeeded(pp);
        }
예제 #4
0
        public void Run()
        {
            PrepareUnitTest();

            // ReSharper disable AssignNullToNotNullAttribute
            ServiceRepository sp = new ServiceRepository(null, null, Logger, Config, new Random());

            // ReSharper restore AssignNullToNotNullAttribute
            var             slice   = Constants.PresentSlice;
            var             dbHouse = sp.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);
            var             airconditioningEntries = dbHouse.Fetch <AirConditioningEntry>();
            var             hausanschlusses        = dbHouse.Fetch <Hausanschluss>();
            DBDto           dbDto = new DBDto(new List <House>(), hausanschlusses, new List <Car>(), new List <Household>(), new List <RlmProfile>());
            CoolingProvider hp    = new CoolingProvider(sp, slice, dbDto);

            Info("total hse: " + airconditioningEntries.Count);
            Profile sumProfile = Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour);

            foreach (var entry in airconditioningEntries)
            {
                (entry.HouseComponentType == HouseComponentType.Cooling).Should().BeTrue();
                hp.IsCorrectProvider(entry).Should().BeTrue();
                HouseComponentRo     hro = new HouseComponentRo(string.Empty, string.Empty, 1, 1, string.Empty, string.Empty, string.Empty, 0);
                ProviderParameterDto ppd = new ProviderParameterDto(entry, string.Empty, hro);
                hp.PrepareLoadProfileIfNeeded(ppd);
                var prof = hp.ProvideProfile(ppd);
                if (prof != null)
                {
                    if (prof.Profile == null)
                    {
                        throw new FlaException("Profile was null");
                    }

                    prof.Profile.EnergySum().Should().BeApproximately(entry.EffectiveEnergyDemand, entry.EffectiveEnergyDemand * 0.1);
                    sumProfile = sumProfile.Add(prof.Profile.Values);
                }
            }

            string fn = WorkingDirectory.Combine("Profiletest_cooling.xlsx");

            XlsxDumper.DumpProfilesToExcel(fn, 2050, 15, new ProfileWorksheetContent("Sum", "Last", 240, sumProfile));
            Info("Wrote to " + fn);
        }
        public void Run()
        {
            PrepareUnitTest();
            // ReSharper disable AssignNullToNotNullAttribute
            ServiceRepository sp = new ServiceRepository(null, null, Logger, Config, new Random());
            // ReSharper restore AssignNullToNotNullAttribute

            var             utopiaSlice          = new ScenarioSliceParameters(Scenario.FromEnum(ScenarioEnum.Utopia), 2050, null);
            var             dbHouse              = sp.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, utopiaSlice);
            var             heatingSystemEntries = dbHouse.Fetch <HeatingSystemEntry>();
            var             hausanschlusses      = dbHouse.Fetch <Hausanschluss>();
            DBDto           dbDto = new DBDto(new List <House>(), hausanschlusses, new List <Car>(), new List <Household>(), new List <RlmProfile>());
            HeatingProvider hp    = new HeatingProvider(sp, utopiaSlice, dbDto);

            Info("total hse: " + heatingSystemEntries.Count);
            Profile sumProfile = Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour);

            foreach (var entry in heatingSystemEntries)
            {
                (entry.HouseComponentType == HouseComponentType.Heating).Should().BeTrue();
                hp.IsCorrectProvider(entry).Should().BeTrue();
                HouseComponentRo     hro = new HouseComponentRo("", "", 1, 1, "", "", "", 0);
                ProviderParameterDto ppd = new ProviderParameterDto(entry, "", hro);
                hp.PrepareLoadProfileIfNeeded(ppd);
                var prof = hp.ProvideProfile(ppd);
                if (prof != null)
                {
                    if (prof.Profile == null)
                    {
                        throw new FlaException("Profile was null");
                    }

                    prof.Profile.EnergySum().Should().BeApproximately(entry.EffectiveEnergyDemand / 3, entry.EffectiveEnergyDemand * 0.1);
                    sumProfile = sumProfile.Add(prof.Profile.Values);
                }
            }

            var fn = Path.Combine(WorkingDirectory.Dir, "Profiletest.xlsx");

            XlsxDumper.DumpProfilesToExcel(fn, 2050, 15, new ProfileWorksheetContent("Sum", "Last", 240, sumProfile));
        }
        public void TestProfileCreation()
        {
            SqlConnectionPreparer ms = new SqlConnectionPreparer(Config);
            var dbHouses             = ms.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            var pvEntries = dbHouses.Fetch <PvSystemEntry>();

            if (pvEntries.Count == 0)
            {
                throw new FlaException("No PVEntries");
            }

            var houseGuids = pvEntries.Select(x => x.HouseGuid).Distinct().Take(10).ToList();
            // ReSharper disable AssignNullToNotNullAttribute
            ServiceRepository sp = new ServiceRepository(null, null, Logger, Config, new Random());
            // ReSharper restore AssignNullToNotNullAttribute

            // ReSharper disable AssignNullToNotNullAttribute
            var dbdto = new DBDto(null, dbHouses.Fetch <Hausanschluss>(), null, null, null);
            // ReSharper restore AssignNullToNotNullAttribute

            PVProfileProvider pvp = new PVProfileProvider(sp, Constants.PresentSlice, dbdto);
            var hcrc = new HouseComponentRo("name", "type", 1, 1, "status", "", "standort", 0);

            foreach (string houseGuid in houseGuids)
            {
                var pse = pvEntries.Single(x => x.HouseGuid == houseGuid);
                ProviderParameterDto parameters = new ProviderParameterDto(pse, "dummydir", hcrc);
                pvp.PrepareLoadProfileIfNeeded(parameters);
            }

            foreach (string houseGuid in houseGuids)
            {
                var pse = pvEntries.Single(x => x.HouseGuid == houseGuid);
                ProviderParameterDto parameters = new ProviderParameterDto(pse, "dummydir", hcrc);
                var result = pvp.ProvideProfile(parameters);
                Info("Got a profile with " + result?.Profile?.EnergySum());
            }
        }
예제 #7
0
        protected override Prosumer ProvidePrivateProfile([NotNull] ProviderParameterDto ppdto)
        {
            DHWHeaterEntry dhw = (DHWHeaterEntry)ppdto.HouseComponent;

            if (dhw.HouseComponentType != HouseComponentType.Dhw)
            {
                throw new FlaException("Wrong type");
            }

            ppdto.HouseComponentResultObject.DhwSystemType = dhw.DhwHeatingSystemType.ToString();
            if (dhw.DhwHeatingSystemType != DhwHeatingSystem.Electricity && dhw.DhwHeatingSystemType != DhwHeatingSystem.Heatpump)
            {
                ppdto.HouseComponentResultObject.HeatingSystemMessage = "Not electric heating";
                return(null);
            }

            Hausanschluss ha = _dbDto.Hausanschlusse.Single(x => x.Guid == dhw.HausAnschlussGuid);
            var           pa = new Prosumer(dhw.HouseGuid,
                                            dhw.Name,
                                            dhw.HouseComponentType,
                                            dhw.SourceGuid,
                                            dhw.FinalIsn,
                                            dhw.HausAnschlussGuid,
                                            ha.ObjectID,
                                            GenerationOrLoad.Load,
                                            ha.Trafokreis,
                                            Name,
                                            "DHW Profile Generator");
            //todo: randomize this with buckets and/or simulate a central control
            int startTime = 2 * 4 + Services.Rnd.Next(12);
            int stopTime  = startTime + 3 * 4 + Services.Rnd.Next(12);
            //double targetRuntimePerDay = 3*4 + Services.Rnd.NextDouble() * 4;
            double trigger = 1 - 0.05 * Services.Rnd.NextDouble();
            DhwCalculationParameters dhwCalculationParameters = new DhwCalculationParameters(startTime, stopTime, trigger);
            var dhwResult = _dhw.Run(dhwCalculationParameters, dhw.EffectiveEnergyDemand, Services.Rnd);

            pa.Profile = dhwResult.GetEnergyDemandProfile();
            return(pa);
        }
        protected override Prosumer ProvidePrivateProfile([NotNull] ProviderParameterDto ppdto)
        {
            AirConditioningEntry hse = (AirConditioningEntry)ppdto.HouseComponent;

            if (hse.HouseComponentType != HouseComponentType.Cooling)
            {
                throw new FlaException("Wrong type");
            }

            Hausanschluss ha = _dbDto.Hausanschlusse.Single(x => x.Guid == hse.HausAnschlussGuid);
            var           pa = new Prosumer(hse.HouseGuid,
                                            hse.Standort,
                                            hse.HouseComponentType,
                                            hse.SourceGuid,
                                            hse.FinalIsn,
                                            hse.HausAnschlussGuid,
                                            ha.ObjectID,
                                            GenerationOrLoad.Load,
                                            ha.Trafokreis,
                                            Name,
                                            "Cooling State Engine");

            // todo: randomize this with buckets and/or simulate a central control
            double targetRuntimePerDay       = 2 + Services.Rnd.NextDouble() * 2;
            double trigger                   = 1 - Services.Rnd.NextDouble() * 0.1;
            CoolingCalculationParameters hpc = new CoolingCalculationParameters(targetRuntimePerDay, trigger);
            var hpr = _coolingProfileGeneratorsByType[hse.AirConditioningType].Run(hpc, hse.EffectiveEnergyDemand, Services.Rnd);

            ppdto.HouseComponentResultObject.CoolingType = hse.AirConditioningType.ToString();
            pa.Profile = hpr.GetEnergyDemandProfile().ScaleToTargetSum(hse.EffectiveEnergyDemand, "Air Conditioning Profile", out var _);
            if (Math.Abs(pa.Profile.EnergySum() - hse.EffectiveEnergyDemand) > 1)
            {
                throw new FlaException("Energy sum from the cooling is all wrong. Should be " + hse.EffectiveEnergyDemand + " but was " +
                                       pa.Profile.EnergySum());
            }

            return(pa);
        }
        protected override Prosumer ProvidePrivateProfile([NotNull] ProviderParameterDto ppdto)
        {
            Module.SetPrint(0);
            //var relevantPotentials = _pvPotentials.Where(x => x.HouseGuid == houseComponent.HouseGuid);
            PvSystemEntry entry = (PvSystemEntry)ppdto.HouseComponent;

            if (_dbDto.Hausanschlusse == null)
            {
                throw new FlaException("hausanschlüsse were not initalized");
            }
            if (_dbDto.Hausanschlusse.Count == 0)
            {
                throw new FlaException("not a single hausanschluss");
            }

            Hausanschluss hausanschluss = _dbDto.Hausanschlusse.FirstOrDefault(x => x.Guid == entry.HausAnschlussGuid);

            if (hausanschluss == null)
            {
                throw new FlaException("No hausanschluss found for guid: " + entry.HausAnschlussGuid);
            }

            if (hausanschluss.ObjectID.ToLower().Contains("leuchte"))
            {
                throw new FlaException("PV anlage an einer leuchte! " + hausanschluss.ObjectID + " - " + entry.Name);
            }
            //TODO: change this to use pv system areas from the pvsystem entry
            Profile sumProf = Profile.MakeConstantProfile(0, ppdto.HouseComponent.Name, Profile.ProfileResolution.QuarterHour);

            if (Math.Abs(entry.PVAreas.Sum(x => x.Energy) - entry.EffectiveEnergyDemand) > 0.1)
            {
                throw new FlaException("Sum of the pv areas did not match pv entry sum");
            }
            foreach (var area in entry.PVAreas)
            {
                var key          = MakeKeyFromPVArea(area);
                var keystr       = key.GetKey();
                var areaProfiles = _saveableEntries.LoadAllOrMatching("Name", keystr);
                if (areaProfiles.Count != 1)
                {
                    throw new FlaException("Invalid count");
                }

                var areaProfile = areaProfiles[0];
                areaProfile.EnergyOrPower = EnergyOrPower.Energy;
                areaProfile = areaProfile.ScaleToTargetSum(area.Energy, entry.Name, out var _);
                sumProf     = sumProf.Add(areaProfile, entry.Name);
            }

            if (Math.Abs(Slice.PVCurtailToXPercent) < 0.00001)
            {
                throw new FlaException("Found curtailment to 0");
            }

            if (sumProf.EnergySum() < 0)
            {
                throw new FlaException("Negative PV Power");
            }

            if (Slice.PVCurtailToXPercent < 1)
            {
                sumProf = sumProf.LimitPositiveToPercentageOfMax(Slice.PVCurtailToXPercent);
            }

            var prosumer = new Prosumer(entry.HouseGuid, entry.Name,
                                        HouseComponentType.Photovoltaik, entry.SourceGuid, entry.FinalIsn, entry.HausAnschlussGuid, hausanschluss.ObjectID,
                                        GenerationOrLoad.Generation,
                                        hausanschluss.Trafokreis, Name, "PV Profile")
            {
                Profile = sumProf
            };

            if (Math.Abs(Slice.PVCurtailToXPercent - 1) < 0.01 && Math.Abs(sumProf.EnergySum() - entry.EffectiveEnergyDemand) > 0.1)
            {
                throw new FlaException("PV Energy result is wrong");
            }
            return(prosumer);
        }
 public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters) => true;
        protected override Prosumer ProvidePrivateProfile([NotNull] ProviderParameterDto ppdto)
        {
            HeatingSystemEntry hse = (HeatingSystemEntry)ppdto.HouseComponent;

            if (hse.HouseComponentType != HouseComponentType.Heating)
            {
                throw new FlaException("Wrong type");
            }

            ppdto.HouseComponentResultObject.HeatingSystemType = hse.SynthesizedHeatingSystemType;
            if (hse.SynthesizedHeatingSystemType != HeatingSystemType.Heatpump && hse.SynthesizedHeatingSystemType != HeatingSystemType.Electricity)
            {
                ppdto.HouseComponentResultObject.HeatingSystemMessage = "Not electric heating";
                return(null);
            }

            if (!hse.ProvideProfile)
            {
                return(null);
            }

            if (hse.HausAnschlussGuid == null)
            {
                return(null);
            }

            Hausanschluss ha = _dbDto.Hausanschlusse.Single(x => x.Guid == hse.HausAnschlussGuid);

            if (ha.ObjectID.ToLower().Contains("kleinanschluss"))
            {
                throw new FlaException("Heizung am kleinanschluss?");
            }

            var pa = new Prosumer(hse.HouseGuid,
                                  hse.Standort,
                                  hse.HouseComponentType,
                                  hse.SourceGuid,
                                  hse.FinalIsn,
                                  hse.HausAnschlussGuid,
                                  ha.ObjectID,
                                  GenerationOrLoad.Load,
                                  ha.Trafokreis,
                                  Name,
                                  "Heatpump Profile Generator");
            //todo: randomize this with buckets and/or simulate a central control
            int    morningTime                = 4 * 4 + Services.Rnd.Next(8);
            int    eveningTime                = 20 * 4 + Services.Rnd.Next(8);
            double targetRuntimePerDay        = 16 + Services.Rnd.NextDouble() * 4;
            double trigger                    = 1 - Services.Rnd.NextDouble() * 0.1;
            HeatpumpCalculationParameters hpc = new HeatpumpCalculationParameters(HeatPumpTimingMode.OverTheEntireDay,
                                                                                  morningTime,
                                                                                  eveningTime,
                                                                                  targetRuntimePerDay,
                                                                                  trigger,
                                                                                  1);

            hpc.StartLevelPercent = 1 - Services.Rnd.NextDouble() * .5;
            var hpr = _hpg.Run(hpc, hse.EffectiveEnergyDemand, Services.Rnd);

            pa.Profile = hpr.GetEnergyDemandProfile();
            return(pa);
        }
        public void ProcessAllHouses([NotNull] ScenarioSliceParameters parameters,
                                     [NotNull] Func <string, ScenarioSliceParameters, bool, string> makeAndRegisterFullFilename,
                                     ProcessingMode processingMode,
                                     [NotNull][ItemNotNull] List <string> developmentStatus)
        {
            if (!Directory.Exists(_processingResultPathForProfiles))
            {
                Directory.CreateDirectory(_processingResultPathForProfiles);
                Thread.Sleep(500);
            }
            else
            {
                var dstDi = new DirectoryInfo(_processingResultPathForProfiles);
                var files = dstDi.GetFiles();
                Info("Cleaning " + files.Length + " files from result directory " + _processingResultPathForProfiles);
                foreach (var file in files)
                {
                    file.Delete();
                }
            }

            var dbHouses = _services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, parameters);

            Info("using house db in  " + dbHouses.ConnectionString);
            var houses = dbHouses.Fetch <House>();
            HouseComponentRepository hcr = new HouseComponentRepository(dbHouses);
            var   households             = dbHouses.Fetch <Household>();
            var   hausanschlusses        = dbHouses.Fetch <Hausanschluss>();
            var   cars  = dbHouses.Fetch <Car>();
            var   dbRaw = _services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var   measuredRlmProfiles = dbRaw.Fetch <RlmProfile>();
            DBDto dbdto = new DBDto(houses, hausanschlusses, cars, households, measuredRlmProfiles);
            ProsumerComponentResultArchiver pra = new ProsumerComponentResultArchiver(Stage.ProfileGeneration, parameters, processingMode, _services);
            var trafokreiseToProcess            = PrepareListOfTrafokreiseToProcess(hausanschlusses);

            List <HouseCreationAndCalculationJob> districts = new List <HouseCreationAndCalculationJob>();
            var vdewValues           = dbRaw.Fetch <VDEWProfileValue>();
            var feiertage            = dbRaw.Fetch <FeiertagImport>();
            var slpProvider          = new SLPProvider(parameters.DstYear, vdewValues, feiertage);
            var loadProfileProviders = MakeDiContrainer(parameters, developmentStatus, hausanschlusses, houses, districts, slpProvider, dbdto);

            if (processingMode == ProcessingMode.Collecting)
            {
                ClearAllExistingExportProfiles();
            }

            var lpgDirectoryInfo          = GetLPGCalcDirectoryInfo(parameters);
            ProfileGenerationRo pgRo      = new ProfileGenerationRo();
            DateTime            startTime = DateTime.Now;
            DateTime            lastLog   = DateTime.Now;

            Info("Processing " + houses.Count + " houses.");
            Info("Processing mode is " + processingMode);
            List <string> brokenLpgDirectories = new List <string>();
            List <string> brokenLpgJsons       = new List <string>();
            Dictionary <string, List <HouseComponentEntry> > houseComponentsByObjectID = BuildDictionaryByObjektID(houses,
                                                                                                                   hcr,
                                                                                                                   hausanschlusses,
                                                                                                                   pgRo,
                                                                                                                   out var numberOfcomponents);
            int       processedComponents = 0;
            Stopwatch swCollecting        = new Stopwatch();
            Stopwatch swWriting           = new Stopwatch();
            Dictionary <string, int> numberOfEmptyProsumers = new Dictionary <string, int>();
            HashSet <string>         validHouseGuids        = houses.Select(x => x.Guid).ToHashSet();

            foreach (KeyValuePair <string, List <HouseComponentEntry> > pair in houseComponentsByObjectID)
            {
                var houseProsumers = new List <Prosumer>();
                //erst alle profile einsammeln / vorbereiten
                swCollecting.Start();
                foreach (var component in pair.Value)
                {
                    processedComponents++;
                    if (processedComponents % 1000 == 0)
                    {
                        Info(processingMode + " processed Components " + processedComponents + " / " + numberOfcomponents);
                    }

                    ProviderParameterDto ppdto = new ProviderParameterDto(component.Component, lpgDirectoryInfo.FullName, pgRo[component.Component]);
                    var provider = loadProfileProviders.GetCorrectProvider(component.Component);
                    pgRo[component.Component].UsedProvider = provider.Name;
                    if (processingMode == ProcessingMode.Preparing)
                    {
                        provider.PrepareLoadProfileIfNeeded(ppdto);
                    }

                    if (processingMode == ProcessingMode.Collecting)
                    {
                        if (trafokreiseToProcess.Contains(component.Hausanschluss.Trafokreis))
                        {
                            Prosumer p = provider.ProvideProfile(ppdto);
                            //Todo: add up profile per trafokreis, profile per provider etc right here
                            if (p != null)
                            {
                                // some providers that are not ready will return null
                                if (p.Profile?.EnergyOrPower != EnergyOrPower.Energy)
                                {
                                    throw new FlaException("Got a power profile from " + provider.Name);
                                }

                                CheckProfileIntegrity(p, provider, component.Component, validHouseGuids);
                                // ReSharper disable once AssignNullToNotNullAttribute
                                pgRo[component.Component].AddProsumerInformation(p);
                                houseProsumers.Add(p);
                                pra.Archive(p);
                            }
                            else
                            {
                                if (!numberOfEmptyProsumers.ContainsKey(provider.Name))
                                {
                                    numberOfEmptyProsumers.Add(provider.Name, 0);
                                }

                                numberOfEmptyProsumers[provider.Name]++;
                            }
                        }
                    }
                }

                swCollecting.Stop();
                //dann alles rausschreiben
                swWriting.Start();
                if (processingMode == ProcessingMode.Collecting)
                {
                    var generationProsumers = houseProsumers.Where(x => x.GenerationOrLoad == GenerationOrLoad.Generation).ToList();
                    var loadProsumers       = houseProsumers.Where(x => x.GenerationOrLoad == GenerationOrLoad.Load).ToList();
                    var component           = pair.Value[0];
                    if (loadProsumers.Count > 0)
                    {
                        Prosumer summedProsumer = GetSummedProsumer(loadProsumers, component.Hausanschluss.Trafokreis);
                        var      haros          = pgRo.HausanschlussByObjectId(pair.Key);
                        double   maxPower       = summedProsumer.Profile?.MaxPower() ?? 0;
                        foreach (var haro in haros)
                        {
                            haro.MaximumPower = maxPower;
                        }

                        WriteSumLineToCsv(summedProsumer, component.Hausanschluss.Trafokreis, GenerationOrLoad.Load);
                    }

                    if (generationProsumers.Count > 0)
                    {
                        var sumProfile = GetSummedProsumer(generationProsumers, component.Hausanschluss.Trafokreis);
                        WriteSumLineToCsv(sumProfile, component.Hausanschluss.Trafokreis, GenerationOrLoad.Generation);
                        // ReSharper disable once PossibleNullReferenceException
                    }
                }

                swWriting.Stop();
                ReportProgress(startTime, processedComponents, numberOfcomponents, parameters, ref lastLog, swCollecting, swWriting);
            }

            Info("Finished processing all components, finishing up now. Duration: " + (DateTime.Now - startTime).TotalMinutes.ToString("F2") +
                 " minutes");
            Info("collecting took " + swCollecting.Elapsed + " and writing took " + swWriting.Elapsed);
            foreach (var pair in numberOfEmptyProsumers)
            {
                Info("NumberOfEmptyProsumers for " + pair.Key + " was " + pair.Value);
            }

            if (processingMode == ProcessingMode.Preparing)
            {
                DateTime endtime           = new DateTime(parameters.DstYear, 12, 31);
                string   houseJobDirectory = Path.Combine(_services.RunningConfig.Directories.HouseJobsDirectory,
                                                          parameters.DstScenario.ToString(),
                                                          parameters.DstYear.ToString());
                DirectoryInfo houseJobDi = new DirectoryInfo(houseJobDirectory);
                WriteDistrictsForLPG(districts, houseJobDi, _services.Logger, parameters, endtime, pgRo);
            }
            else
            {
                DateTime startSavingToDB = DateTime.Now;
                pra.FinishSavingEverything();
                Info("Finished writing prosumers to db. Duration: " + (DateTime.Now - startSavingToDB).TotalMinutes.ToString("F2") + " minutes");
            }

            pra.Dispose();
            var excelFiles = WriteExcelResultFiles(parameters, makeAndRegisterFullFilename, processingMode, pgRo);

            if (_services.RunningConfig.CollectFilesForArchive)
            {
                SaveToArchiveDirectory(parameters, excelFiles, _services.StartingTime, _services.RunningConfig);
            }

            WriteBrokenLpgCalcCleanupBatch(parameters, makeAndRegisterFullFilename, processingMode, brokenLpgDirectories, brokenLpgJsons);
            if (processingMode == ProcessingMode.Collecting)
            {
                foreach (var provider in loadProfileProviders.Providers)
                {
                    provider.DoFinishCheck();
                }
            }
        }