private Dictionary <int, OnlineEnergyFileRow> MakeSumsPerMonth([NotNull] CalcLoadTypeDto dstLoadType, [ItemNotNull][NotNull] List <OnlineEnergyFileRow> energyFileRows, DateTime curDate, [NotNull] OnlineEnergyFileRow sum, int rowlength) { var calcParameters = Repository.CalcParameters; var sumsPerMonth = new Dictionary <int, OnlineEnergyFileRow>(); double runningtotal = 0; foreach (var efr in energyFileRows) { curDate += calcParameters.InternalStepsize; sum.AddValues(efr); if (Config.IsInUnitTesting && Config.ExtraUnitTestChecking) { runningtotal += efr.SumFresh(); } if (Config.IsInUnitTesting && Config.ExtraUnitTestChecking && Math.Abs(runningtotal - sum.SumFresh()) > 0.000001) { throw new LPGException("Unknown bug while generating the device totals. Sums don't match."); } if (!sumsPerMonth.ContainsKey(curDate.Month)) { var ts = new TimeStep(0, 0, true); sumsPerMonth.Add(curDate.Month, new OnlineEnergyFileRow(ts, new List <double>(new double[rowlength]), dstLoadType)); } sumsPerMonth[curDate.Month].AddValues(efr); } return(sumsPerMonth); }
public List <OnlineEnergyFileRow> ProcessOneTimestep(TimeStep timeStep) { CleanExpiredStateMachines(timeStep, _stateMachinesByLoadtype); var fileRows = new List <OnlineEnergyFileRow>(); //var procesedMachines = new Dictionary<CalcLoadTypeDto, List< OnlineDeviceStateMachine>>(); //foreach (OnlineDeviceStateMachine stateMachine in _statemachines) { // if (!_loadTypeDict.ContainsKey(stateMachine.LoadType)) { // throw new LPGException("Found a state machine for a load type that does not exist: " + stateMachine.OefcKey + ": " + stateMachine.LoadType); // } //} //foreach (var loadType in _loadTypeDict.Keys) { // var columnEntriesDeviceKey = Oefc.ColumnEntriesByLoadTypeByDeviceKey[loadType]; // foreach (var machine in _statemachines) { // if (columnEntriesDeviceKey.ContainsKey(machine.OefcKey)) { // procesedMachines[loadType].Add( machine); // } // } //} foreach (var pair in _stateMachinesByLoadtype) { //if (procesedMachines[pair.Key].Count != pair.Value) { //} var energyvalues = new List <double>(new double[Oefc.ColumnCountByLoadType[pair.Key]]); foreach (var machine in pair.Value) { energyvalues[machine.ColumnNumber] += machine.GetEnergyValueForTimeStep(timeStep, _zeroEntries); } var fileRow = new OnlineEnergyFileRow(timeStep, energyvalues, pair.Key); fileRows.Add(fileRow); } /*if (Config.ExtraUnitTestChecking) { * if (procesedMachines.Count != _statemachines.Count) { * var nonprocessed = * _statemachines.Where(x => !procesedMachines.Contains(x)).ToList(); * throw new LPGException("Not all machines were processed! Processed:" + procesedMachines.Count + * " Total:" + _statemachines.Count + Environment.NewLine + nonprocessed); * } * }*/ CleanZeroValueEntries(timeStep); return(fileRows); }
private List <OnlineEnergyFileRow> ReadOnlineEnergyFileRowsIntoMemory([NotNull] CalcLoadTypeDto calcLoadType, out double total) { var path = _fft.GetResultFileEntry(ResultFileID.OnlineDeviceActivationFiles, calcLoadType.Name, Constants.GeneralHouseholdKey, null, null) .FullFileName; var energyFileRows = new List <OnlineEnergyFileRow>(); total = 0; if (path == null) { throw new LPGException("path was null"); } using (Stream fs = new FileStream(path, FileMode.Open)) { long currentPosition = 0; #pragma warning disable S2930 // "IDisposables" should be disposed using (var br = new BinaryReader(fs)) { #pragma warning restore S2930 // "IDisposables" should be disposed while (currentPosition < fs.Length) { var efr = OnlineEnergyFileRow.Read(br, calcLoadType, _repository.CalcParameters); energyFileRows.Add(efr); if (Config.IsInUnitTesting && Config.ExtraUnitTestChecking) { total += efr.SumFresh(); } currentPosition += efr.EntryLengthInByte; } } } return(energyFileRows); }
private void Run([NotNull] CalcLoadTypeDto dstLoadType, [NotNull][ItemNotNull] List <OnlineEnergyFileRow> energyFileRows, [NotNull] IFileFactoryAndTracker fft, [NotNull] EnergyFileColumns efc, [NotNull] Dictionary <CalcLoadTypeDto, Dictionary <StrGuid, double> > loadTypeTodeviceIDToAverageLookup, [ItemNotNull][NotNull] List <DeviceTaggingSetInformation> deviceTaggingSets, [NotNull] Dictionary <string, string> deviceNameToCategory, [NotNull] Dictionary <string, double> deviceEnergyDict, [NotNull] HouseholdKey key) { if (!efc.ColumnEntriesByColumn.ContainsKey(dstLoadType)) { //for this load type for this house there are no column, so nothing to do return; } var calcParameters = Repository.CalcParameters; var rowlength = energyFileRows[0].EnergyEntries.Count; var ts = new TimeStep(0, 0, true); var sum = new OnlineEnergyFileRow(ts, new List <double>(new double[rowlength]), dstLoadType); var curDate = calcParameters.OfficialStartTime; var sumsPerMonth = MakeSumsPerMonth(dstLoadType, energyFileRows, curDate, sum, rowlength); /* * if (Config.IsInUnitTesting && Config.ExtraUnitTestChecking) { * if (!double.IsNaN(previousTotal) && Math.Abs(sum.SumFresh - previousTotal) > 0.000001) { * throw new LPGException("Unknown bug while generating the device totals. Sums don't match."); * } * }*/ var sumPerMonthPerDeviceID = MakeSumPerMonthPerDeviceID(dstLoadType, efc, sumsPerMonth, out var columns); var sumsPerDeviceID = new Dictionary <StrGuid, double>(); var deviceNamesPerID = new Dictionary <StrGuid, string>(); var sumPerDeviceName = new Dictionary <string, double>(); foreach (var pair in columns) { var ce = pair.Value; if (!sumsPerDeviceID.ContainsKey(ce.DeviceGuid)) { sumsPerDeviceID.Add(ce.DeviceGuid, 0); deviceNamesPerID.Add(ce.DeviceGuid, ce.Name); } if (!sumPerDeviceName.ContainsKey(ce.Name)) { sumPerDeviceName.Add(ce.Name, 0); } sumPerDeviceName[ce.Name] += sum.EnergyEntries[pair.Key]; sumsPerDeviceID[ce.DeviceGuid] += sum.EnergyEntries[pair.Key]; } MakeTotalsPerDeviceTaggingSet(fft, dstLoadType, deviceTaggingSets, deviceEnergyDict, key); var builder = new StringBuilder(); foreach (var calcDeviceTaggingSet in deviceTaggingSets) { if (calcDeviceTaggingSet.LoadTypesForThisSet.Any(x => x.Name == dstLoadType.Name)) { builder.Append(calcDeviceTaggingSet.Name).Append(calcParameters.CSVCharacter); } } var taggingsetHeader = builder.ToString(); var devicesums = fft.MakeFile <StreamWriter>("DeviceSums." + dstLoadType.Name + "." + key.Key + ".csv", "Summed up " + dstLoadType.Name + " use per device and comparison with statistical values", true, ResultFileID.DeviceSums, key, TargetDirectory.Reports, calcParameters.InternalStepsize, CalcOption.TotalsPerDevice, dstLoadType.ConvertToLoadTypeInformation()); var calcDuration = calcParameters.OfficialEndTime - calcParameters.OfficialStartTime; var amountofYears = calcDuration.TotalDays / 365.0; var sb = new StringBuilder(); sb.Append("Device name"); sb.Append(calcParameters.CSVCharacter); sb.Append("Usage sum in this simulation [").Append(dstLoadType.UnitOfSum).Append("]"); sb.Append(calcParameters.CSVCharacter); sb.Append("Usage sum in this simulation linear extrapolated to 1 year [").Append(dstLoadType.UnitOfSum).Append("]"); sb.Append(calcParameters.CSVCharacter); sb.Append("Comparison Value from the device entry [").Append(dstLoadType.UnitOfSum).Append("]"); sb.Append(calcParameters.CSVCharacter); sb.Append("Percentage of the comparison value [1 = 100%]"); sb.Append(calcParameters.CSVCharacter); sb.Append("Device Category"); sb.Append(calcParameters.CSVCharacter); sb.Append(taggingsetHeader); devicesums.WriteLine(sb); double devicesum = 0; double extrapolatedSum = 0; double comparsionvaluessum = 0; foreach (var keyValuePair in sumsPerDeviceID) { var s = string.Empty; s += deviceNamesPerID[keyValuePair.Key]; s += calcParameters.CSVCharacter; s += keyValuePair.Value * dstLoadType.ConversionFactor; devicesum += keyValuePair.Value; //deviceSums.AddDeviceSum(deviceNamesPerID[keyValuePair.Key],devicesum,dstLoadType); s += calcParameters.CSVCharacter; var extrapolatedValue = keyValuePair.Value * dstLoadType.ConversionFactor / amountofYears; s += extrapolatedValue; extrapolatedSum += keyValuePair.Value / amountofYears; s += calcParameters.CSVCharacter; double defaultvalue = 0; if (loadTypeTodeviceIDToAverageLookup.ContainsKey(dstLoadType)) { if (loadTypeTodeviceIDToAverageLookup[dstLoadType].ContainsKey(keyValuePair.Key)) { defaultvalue = loadTypeTodeviceIDToAverageLookup[dstLoadType][keyValuePair.Key]; } } s += defaultvalue; comparsionvaluessum += defaultvalue; s += calcParameters.CSVCharacter; if (Math.Abs(defaultvalue) > Constants.Ebsilon) { s += extrapolatedValue / defaultvalue; } else { s += 0; } s += calcParameters.CSVCharacter; var devicename = deviceNamesPerID[keyValuePair.Key]; var deviceCategory = "(no category)"; if (deviceNameToCategory.ContainsKey(devicename)) { deviceCategory = deviceNameToCategory[devicename]; } s += deviceCategory; s += calcParameters.CSVCharacter; var tags = string.Empty; foreach (var calcDeviceTaggingSet in deviceTaggingSets) { if (calcDeviceTaggingSet.LoadTypesForThisSet.Any(x => x.Name == dstLoadType.Name)) { var deviceName = deviceNamesPerID[keyValuePair.Key]; if (calcDeviceTaggingSet.TagByDeviceName.ContainsKey(deviceName)) { tags += calcDeviceTaggingSet.TagByDeviceName[deviceName] + calcParameters.CSVCharacter; } else { tags += Constants.UnknownTag + calcParameters.CSVCharacter; } } } devicesums.WriteLine(s + tags); } var sumstr = "Sums"; sumstr += calcParameters.CSVCharacter; sumstr += devicesum * dstLoadType.ConversionFactor; sumstr += calcParameters.CSVCharacter; sumstr += extrapolatedSum * dstLoadType.ConversionFactor; sumstr += calcParameters.CSVCharacter; sumstr += comparsionvaluessum; devicesums.WriteLine(sumstr); devicesums.Flush(); WriteMonthlyDeviceSums(fft, dstLoadType, sumPerMonthPerDeviceID, deviceNamesPerID, key); }
public void Run([NotNull] CalcLoadTypeDto dstLoadType, [NotNull][ItemNotNull] List <OnlineEnergyFileRow> energyFileRows, [NotNull] EnergyFileColumns efc) { var calcParameters = Repository.CalcParameters; var dsc = new DateStampCreator(calcParameters); var externalfactor = (int) (calcParameters.ExternalStepsize.TotalSeconds / calcParameters.InternalStepsize.TotalSeconds); if (externalfactor == 1) { return; } var externalFileName = calcParameters.ExternalStepsize.TotalSeconds.ToString(CultureInfo.InvariantCulture); StreamWriter sumfile = null; if (calcParameters.IsSet(CalcOption.SumProfileExternalEntireHouse)) { sumfile = _fft.MakeFile <StreamWriter>("SumProfiles_" + externalFileName + "s." + dstLoadType.Name + ".csv", "Sum energy profiles for " + externalFileName + "s " + dstLoadType.Name, true, ResultFileID.CSVSumProfileExternal, Constants.GeneralHouseholdKey, TargetDirectory.Results, calcParameters.InternalStepsize, CalcOption.SumProfileExternalEntireHouse, dstLoadType.ConvertToLoadTypeInformation()); sumfile.WriteLine(dstLoadType.Name + "." + dsc.GenerateDateStampHeader() + "Sum [" + dstLoadType.UnitOfSum + "]"); } StreamWriter normalfile = null; if (calcParameters.IsSet(CalcOption.DeviceProfileExternalEntireHouse)) { normalfile = _fft.MakeFile <StreamWriter>("DeviceProfiles_" + externalFileName + "s." + dstLoadType.Name + ".csv", "Device energy profiles for " + externalFileName + "s " + dstLoadType.Name, true, ResultFileID.DeviceProfileCSVExternal, Constants.GeneralHouseholdKey, TargetDirectory.Results, calcParameters.InternalStepsize, CalcOption.DeviceProfileExternalEntireHouse, dstLoadType.ConvertToLoadTypeInformation()); normalfile.WriteLine(dstLoadType.Name + "." + dsc.GenerateDateStampHeader() + efc.GetTotalHeaderString(dstLoadType, null)); } if (calcParameters.IsSet(CalcOption.DeviceProfileExternalEntireHouse) || calcParameters.IsSet(CalcOption.SumProfileExternalEntireHouse)) { for (var outerIndex = 0; outerIndex < energyFileRows.Count; outerIndex += externalfactor) { var efr = new OnlineEnergyFileRow(energyFileRows[outerIndex]); if (!efr.Timestep.DisplayThisStep) { continue; } for (var innerIndex = outerIndex + 1; innerIndex < externalfactor + outerIndex && innerIndex < energyFileRows.Count; innerIndex++) { var efr2 = energyFileRows[innerIndex]; efr.AddValues(efr2); } var sb = new StringBuilder(); dsc.GenerateDateStampForTimestep(efr.Timestep, sb); if (calcParameters.IsSet(CalcOption.DeviceProfileExternalEntireHouse)) { var normalstr = sb + efr.GetEnergyEntriesAsString(true, dstLoadType, null, calcParameters.CSVCharacter) .ToString(); if (normalfile == null) { throw new LPGException("File is null. Please report."); } normalfile.WriteLine(normalstr); } if (calcParameters.IsSet(CalcOption.SumProfileExternalEntireHouse)) { if (sumfile == null) { throw new LPGException("File is null. Please report."); } sumfile.WriteLine(sb + (efr.SumCached * dstLoadType.ConversionFactor).ToString(Config.CultureInfo)); } } } }
public void RunIndividualHouseholds([NotNull] CalcLoadTypeDto dstLoadType, [NotNull][ItemNotNull] List <OnlineEnergyFileRow> energyFileRows, [NotNull] EnergyFileColumns efc, [NotNull] HouseholdKey hhnum) { var calcParameters = Repository.CalcParameters; var dsc = new DateStampCreator(calcParameters); var externalfactor = (int) (calcParameters.ExternalStepsize.TotalSeconds / calcParameters.InternalStepsize.TotalSeconds); if (externalfactor == 1) { return; } var externalFileName = calcParameters.ExternalStepsize.TotalSeconds.ToString(CultureInfo.InvariantCulture); var columns = (from entry in efc.ColumnEntriesByColumn[dstLoadType].Values where entry.HouseholdKey == hhnum select entry.Column).ToList(); var hhname = "." + hhnum + "."; StreamWriter sumfile = null; if (calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholds)) { sumfile = _fft.MakeFile <StreamWriter>( "SumProfiles_" + externalFileName + "s" + hhname + dstLoadType.Name + ".csv", "Summed up energy profile for all devices for " + dstLoadType.Name + " for " + hhname + " for " + externalFileName + "s", true, ResultFileID.ExternalSumsForHouseholds, hhnum, TargetDirectory.Results, calcParameters.ExternalStepsize, CalcOption.SumProfileExternalIndividualHouseholds, dstLoadType.ConvertToLoadTypeInformation()); sumfile.WriteLine(dstLoadType.Name + "." + dsc.GenerateDateStampHeader() + "Sum [" + dstLoadType.UnitOfSum + "]"); } StreamWriter normalfile = null; if (calcParameters.IsSet(CalcOption.DeviceProfileExternalIndividualHouseholds)) { normalfile = _fft.MakeFile <StreamWriter>( "DeviceProfiles_" + externalFileName + "s" + hhname + dstLoadType.Name + ".csv", "Energy use by each device in each Timestep for " + dstLoadType.Name + " for " + hhname, true, ResultFileID.DeviceProfileCSVExternalForHouseholds, hhnum, TargetDirectory.Results, calcParameters.ExternalStepsize, CalcOption.DeviceProfileExternalIndividualHouseholds, dstLoadType.ConvertToLoadTypeInformation()); normalfile.WriteLine(dstLoadType.Name + "." + dsc.GenerateDateStampHeader() + efc.GetTotalHeaderString(dstLoadType, columns)); } StreamWriter jsonfile = null; List <double> valuesForJsonExport = new List <double>(); if (calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholdsAsJson)) { jsonfile = _fft.MakeFile <StreamWriter>( "SumProfiles_" + externalFileName + "s" + hhname + dstLoadType.Name + ".json", "Summed up energy profile for all devices for " + dstLoadType.Name + " for " + hhname + " for " + externalFileName + "s as json", true, ResultFileID.ExternalSumsForHouseholdsJson, hhnum, TargetDirectory.Results, calcParameters.ExternalStepsize, CalcOption.SumProfileExternalIndividualHouseholdsAsJson, dstLoadType.ConvertToLoadTypeInformation()); } if (calcParameters.IsSet(CalcOption.DeviceProfileExternalIndividualHouseholds) || calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholds) || calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholdsAsJson)) { for (var outerIndex = 0; outerIndex < energyFileRows.Count; outerIndex += externalfactor) { var efr = new OnlineEnergyFileRow(energyFileRows[outerIndex]); if (!efr.Timestep.DisplayThisStep) { continue; } for (var innerIndex = outerIndex + 1; innerIndex < externalfactor + outerIndex && innerIndex < energyFileRows.Count; innerIndex++) { var efr2 = energyFileRows[innerIndex]; efr.AddValues(efr2); } var sb = new StringBuilder(); dsc.GenerateDateStampForTimestep(efr.Timestep, sb); if (calcParameters.IsSet(CalcOption.DeviceProfileExternalIndividualHouseholds)) { var normalstr = sb.ToString() + efr.GetEnergyEntriesAsString(true, dstLoadType, columns, calcParameters.CSVCharacter); if (normalfile == null) { throw new LPGException("File was null. Please report."); } normalfile.WriteLine(normalstr); } if (calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholds) || calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholdsAsJson)) { double sum = efr.GetSumForCertainCols(columns) * dstLoadType.ConversionFactor; if (calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholds)) { var sumstring = sb.ToString() + sum; if (sumfile == null) { throw new LPGException("File was null. Please report."); } sumfile.WriteLine(sumstring); } if (calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholdsAsJson)) { valuesForJsonExport.Add(sum); } } } if (calcParameters.IsSet(CalcOption.SumProfileExternalIndividualHouseholdsAsJson)) { if (jsonfile == null) { throw new LPGException("Jsonfile was null"); } jsonfile.WriteLine(JsonConvert.SerializeObject(valuesForJsonExport, Formatting.Indented)); } } }