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 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(); }
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(); }