protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters) { var dbDstProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters); var dbSrcProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileImport, Constants.PresentSlice).Database; var vdewvals = dbSrcProfiles.Fetch <VDEWProfileValues>(); var dbHouses = Services.SqlConnection.GetDatabaseConnection(Stage.Houses, parameters).Database; var houses = dbHouses.Fetch <House>(); Prosumer.ClearProsumerTypeFromDB(Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters), ProsumerType.Household, TableType.HousePart); var slp = new SLPProvider(parameters.DstYear); //var houses = dbHouses.Fetch<House>(); var households = dbHouses.Fetch <Household>(); //var trafoKreise = dbDstProfiles.Database.Fetch<TrafoKreisResult>(); Log(MessageType.Info, "making " + households.Count + " households"); var sa = Prosumer.GetSaveableEntry(dbDstProfiles, TableType.HousePart); // && idx < 20 double totalHouseholdEnergy = 0; double totalProfileEnergy = 0; for (var idx = 0; idx < households.Count; idx++) { var household = households[idx]; House house = houses.Single(x => x.HouseGuid == household.HouseGuid); Hausanschluss ha = house.Hausanschluss.Single(x => x.HausanschlussGuid == household.HausAnschlussGuid); // ReSharper disable once UseObjectOrCollectionInitializer var pa = new Prosumer(household.HouseGuid, household.StandortIDsAsJson, ProsumerType.Household, household.HouseholdGuid, household.FinalIsn, household.HausAnschlussGuid, ha.ObjectID); pa.Profile = slp.Run(vdewvals, "H0", household.LowVoltageYearlyTotalElectricityUse); pa.SumElectricityPlanned = household.LowVoltageYearlyTotalElectricityUse; sa.RowEntries.Add(pa.GetRow()); totalHouseholdEnergy += pa.SumElectricityPlanned; totalProfileEnergy += pa.Profile?.EnergySum() ?? 0; if (sa.RowEntries.Count > 1000) { sa.SaveDictionaryToDatabase(); } } sa.SaveDictionaryToDatabase(); if (Math.Abs(totalHouseholdEnergy - totalProfileEnergy) > 1) { throw new FlaException("energy sums not equal between planned energy and energy in profiles"); } Info("Total Household energy: " + totalHouseholdEnergy); /* * var loadedProsumers = Prosumer.LoadProsumers(dbDstProfiles, TableType.HousePart); * foreach (Prosumer loadedProsumer in loadedProsumers) { * Log(MessageType.Info,loadedProsumer.Name + " - " + loadedProsumer.SumElectricityFromProfile + " - " + loadedProsumer.Profile?.Values.Sum() ); * }*/ //dbDstProfiles.Database.CompleteTransaction(); }
protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters) { var dbDstProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters); var fieldDefinition = Prosumer.GetSaveableEntry(dbDstProfiles, TableType.HousePart); fieldDefinition.MakeTableForListOfFields(); // Services.SqlConnection.RecreateTable<Prosumer>(Stage.ProfileGeneration, parameters.DstScenario, parameters.DstYear); dbDstProfiles.Database.Execute("VACUUM"); }
protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters) { var dbSrcProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileImport, Constants.PresentSlice).Database; var vdewvals = dbSrcProfiles.Fetch <VDEWProfileValues>(); var dbHouses = Services.SqlConnection.GetDatabaseConnection(Stage.Houses, parameters).Database; var dbDstProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters); Prosumer.ClearProsumerTypeFromDB(Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters), ProsumerType.Household, TableType.HousePart); var slp = new SLPProvider(parameters.DstYear); //var hausAnschlüsse = dbHouses.Fetch<Hausanschluss>(); //var houses = dbHouses.Fetch<House>(); var buisineses = dbHouses.Fetch <BusinessEntry>(); //double totalHouseholdEnergy = 0; //double totalProfileEnergy = 0; var sa = Prosumer.GetSaveableEntry(dbDstProfiles, TableType.HousePart); Log(MessageType.Info, "making " + buisineses.Count + " businesses"); foreach (var be in buisineses) { // Hausanschluss ha = hausAnschlüsse.Single(x => x.HausanschlussGuid == be.HausAnschlussGuid); var pa = new Prosumer(be.HouseGuid, be.StandortIDsAsJson, ProsumerType.BusinessNoLastgang, be.BusinessGuid, be.FinalIsn, be.HausAnschlussGuid, "") { Profile = slp.Run(vdewvals, "G0", be.LowVoltageYearlyTotalElectricityUse), SumElectricityPlanned = be.LowVoltageYearlyTotalElectricityUse }; //totalHouseholdEnergy += pa.SumElectricityPlanned; //totalProfileEnergy += (double)pa.Profile?.EnergySum(); sa.AddRow(pa); } /*var trafoKreise = dbDstProfiles.Database.Fetch<TrafoKreisResult>(); * double plannedHouseholdEnergy = trafoKreise.Sum(x => x.BusinessEnergy); * if (Math.Abs(plannedHouseholdEnergy - totalHouseholdEnergy) > 1) * { * throw new Exception("energy sums are not equal between planned energy and allocated household energy"); * } * * if (Math.Abs(plannedHouseholdEnergy - totalProfileEnergy) > 1) * { * throw new Exception("energy sums not equal between planned energy and energy in profiles"); * }*/ sa.SaveDictionaryToDatabase(); }
private MyDb PrepareProfileExport([NotNull] ScenarioSliceParameters parameters) { SqlConnection.RecreateTable <HausAnschlussExportEntry>(Stage.ProfileExport, parameters); var dbProfileExport = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileExport, parameters); var fieldDefinition = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseLoad); fieldDefinition.MakeTableForListOfFields(); var resultPath = dbProfileExport.GetResultFullPath(SequenceNumber, Name); if (!Directory.Exists(resultPath)) { Directory.CreateDirectory(resultPath); } var saLoad1 = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseLoad); saLoad1.MakeTableForListOfFields(); var saGeneration1 = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseGeneration); saGeneration1.MakeTableForListOfFields(); return(dbProfileExport); }
protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters) { var dbHouse = Services.SqlConnection.GetDatabaseConnection(Stage.Houses, parameters); var dbDstProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters); //var dbSrcProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileImport, Constants.PresentSlice).Database; var dbPVProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGenerationPV, Constants.PresentSlice); int profileCount = Profile.CountProfiles(dbPVProfiles, TableType.PVGeneration); if (profileCount < 100) { throw new FlaException("No profiles in the database. Not generated for this slice perhaps?"); } var pvsystems = dbHouse.Database.Fetch <PvSystemEntry>(); var dbHouses = Services.SqlConnection.GetDatabaseConnection(Stage.Houses, parameters).Database; var houses = dbHouses.Fetch <House>(); Prosumer.ClearProsumerTypeFromDB(Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters), ProsumerType.PV, TableType.HousePart); if (pvsystems.Count == 0) { throw new FlaException("No pv systems found"); } Log(MessageType.Info, "Making " + pvsystems.Count + " pvsystems"); var sa = Prosumer.GetSaveableEntry(dbDstProfiles, TableType.HousePart); //count total number of unique systems List <string> allKeys = new List <string>(); foreach (PvSystemEntry pvsystem in pvsystems) { foreach (PVSystemArea area in pvsystem.PVAreas) { int myazimut = (int)area.Azimut + 180; if (myazimut == 360) { myazimut = 0; } G_PVProfileGeneration.PVSystemKey pvk = new G_PVProfileGeneration.PVSystemKey(myazimut, (int)area.Tilt); allKeys.Add(pvk.GetKey()); } } var keys = allKeys.Distinct().OrderBy(x => x); Info("Total unique keys: " + keys.Count()); //dumping for debugging var fn = MakeAndRegisterFullFilename("MyKeys.csv", Name, "", parameters); File.WriteAllText(fn, JsonConvert.SerializeObject(keys, Formatting.Indented)); var fn2 = MakeAndRegisterFullFilename("MyKeysPredefined.csv", Name, "", parameters); var presetKeys = Profile.LoadAllKeys(dbPVProfiles, TableType.PVGeneration); File.WriteAllText(fn2, JsonConvert.SerializeObject(presetKeys, Formatting.Indented)); Info("Wrote keys to " + fn); double totalEnergyFromAllSystems = 0; double totalProfileEnergy = 0; for (var idx = 0; idx < pvsystems.Count; idx++) { var pvsystem = pvsystems[idx]; if (pvsystem.PVAreas.Count == 0) { throw new FlaException("No PV Areas were defined."); } House house = houses.Single(x => x.HouseGuid == pvsystem.HouseGuid); Hausanschluss ha = house.Hausanschluss.Single(x => x.HausanschlussGuid == pvsystem.HausAnschlussGuid); var pa = new Prosumer(pvsystem.HouseGuid, house.ComplexName, ProsumerType.PV, pvsystem.PvGuid, pvsystem.FinalIsn, pvsystem.HausAnschlussGuid, ha.ObjectID); Profile p = null; double totalEnergyForThisSystem = 0; foreach (PVSystemArea area in pvsystem.PVAreas) { G_PVProfileGeneration.PVSystemKey pvk = new G_PVProfileGeneration.PVSystemKey((int)area.Azimut + 180, (int)area.Tilt); totalEnergyForThisSystem += area.Energy; var singleProfile = Profile.LoadProfile(dbPVProfiles, TableType.PVGeneration, pvk.GetKey()); if (singleProfile.Count == 0) { throw new FlaException("No profile was found: " + pvk.GetKey()); } if (singleProfile.Count > 1) { throw new FlaException("too many profiles found: " + pvk.GetKey()); } singleProfile[0].ProfileType = ProfileType.Energy; singleProfile[0] = singleProfile[0].ScaleToTargetSum(area.Energy, singleProfile[0].Name); if (Math.Abs(singleProfile[0].EnergySum() - area.Energy) > 0.1) { throw new FlaException("invalid energy"); } if (p == null) { p = singleProfile[0]; } else { p = p.Add(singleProfile[0], p.Name); } } if (Math.Abs(totalEnergyForThisSystem) < 1) { throw new FlaException("PV profile with 0 energy found."); } if (p == null) { throw new FlaException("No profile for " + pvsystem.Name + (" idx:" + idx)); } pa.Profile = p; pa.SumElectricityPlanned = totalEnergyForThisSystem; totalEnergyFromAllSystems += totalEnergyForThisSystem; sa.RowEntries.Add(pa.GetRow()); if (pa.Profile == null) { throw new FlaException("No profile"); } totalProfileEnergy += pa.Profile.EnergySum(); if (Math.Abs(totalEnergyFromAllSystems - totalProfileEnergy) > 1) { throw new FlaException("energy sums not equal between planned energy and energy in profiles"); } if (sa.RowEntries.Count > 100) { sa.SaveDictionaryToDatabase(); } } sa.SaveDictionaryToDatabase(); if (Math.Abs(totalEnergyFromAllSystems - totalProfileEnergy) > 1) { throw new FlaException("energy sums not equal between planned energy and energy in profiles"); } Info("Total energy from all pv systems: " + totalEnergyFromAllSystems.ToString("N")); }
protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters) { Prosumer.ClearProsumerTypeFromDB(Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters), ProsumerType.LastgangGeneration, TableType.HousePart); Prosumer.ClearProsumerTypeFromDB(Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters), ProsumerType.BusinessWithLastgang, TableType.HousePart); var dbSrcProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileImport, Constants.PresentSlice).Database; var dbHouses = Services.SqlConnection.GetDatabaseConnection(Stage.Houses, parameters).Database; var dbDstProfiles = Services.SqlConnection.GetDatabaseConnection(Stage.ProfileGeneration, parameters); var assignments = dbSrcProfiles.Fetch <LastgangBusinessAssignment>(); var profiles = dbSrcProfiles.Fetch <RlmProfile>(); var houses = dbHouses.Fetch <House>(); var businesses = dbHouses.Fetch <BusinessEntry>(); //var hausAnschlussses = dbHouses.Fetch<Hausanschluss>(); Log(MessageType.Info, "making " + assignments.Count + " assignments"); var sa = Prosumer.GetSaveableEntry(dbDstProfiles, TableType.HousePart); if (skipAllRlm) { return; } foreach (var assignment in assignments) { if (assignment.BusinessName == "none") { continue; } //pv anlagen if (!string.IsNullOrWhiteSpace(assignment.ErzeugerID)) { if (MakeErzeugerLastgang(houses, assignment, profiles, sa)) { continue; } } if (!string.IsNullOrWhiteSpace(assignment.ComplexName) && !string.IsNullOrWhiteSpace(assignment.BusinessName)) { var selectedBusinesses = businesses.Where(x => x.BusinessName == assignment.BusinessName).ToList(); if (selectedBusinesses.Count == 0) { throw new Exception("Not a single business was found: " + assignment.BusinessName); } var rightbusiness = selectedBusinesses[0]; if (assignment.Standort != null && selectedBusinesses.Count > 1) { rightbusiness = selectedBusinesses.Single(x => x.Standorte.Contains(assignment.Standort)); } if (selectedBusinesses.Count > 1) { var rightHouse = houses.Single(x => x.ComplexName == assignment.ComplexName); selectedBusinesses = selectedBusinesses.Where(x => x.HouseGuid == rightHouse.HouseGuid).ToList(); rightbusiness = selectedBusinesses[0]; } var house = houses.Single(x => x.HouseGuid == rightbusiness.HouseGuid); int isnid = -1; if (house.GebäudeObjectIDs.Count > 0) { isnid = house.GebäudeObjectIDs[0]; } //if (house.GebäudeObjectIDs.Count > 1) { //throw new Exception("trying to export entry for house with more than one isn id"); //} var has = house.Hausanschluss.FirstOrDefault(x => x.Isn == isnid); if (has == null) { throw new FlaException("No hausanschluss"); } var pa = new Prosumer(house.HouseGuid, assignment.RlmFilename, ProsumerType.BusinessWithLastgang, rightbusiness.BusinessGuid, isnid, has.HausanschlussGuid, has.ObjectID); var rlmprofile = profiles.Single(x => x.Name == assignment.RlmFilename); pa.Profile = rlmprofile.Profile; sa.AddRow(pa); } } sa.SaveDictionaryToDatabase(); }
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"); }