public static void MakeFlameChart([NotNull] DirectoryInfo di, [NotNull] CalculationProfiler calculationProfiler) { string targetfile = Path.Combine(di.FullName, Constants.CalculationProfilerJson); using (StreamWriter sw = new StreamWriter(targetfile)) { calculationProfiler.WriteJson(sw); CalculationDurationFlameChart cdfc = new CalculationDurationFlameChart(); Thread t = new Thread(() => { try { cdfc.Run(calculationProfiler, di.FullName, "CommandlineCalc"); } catch (Exception ex) { Logger.Exception(ex); } }); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); } }
public void StartHousehold([NotNull] Simulator sim, [NotNull] JsonCalcSpecification jcs, [NotNull] JsonReference calcObjectReference, [CanBeNull] Action <CalculationProfiler, string> makeAllCharts) { if (!CheckSimulator(jcs, sim)) { throw new LPGPBadParameterException("Invalid simulation state"); } var calcObject = GetCalcObject(sim, calcObjectReference); if (jcs.OutputDirectory == null) { jcs.OutputDirectory = AutomationUtili.CleanFileName(calcObject.Name) + " - " + calcObject; } _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass()); if (calcObjectReference == null) { throw new LPGException("No calculation object was selected."); } var calculationStartTime = DateTime.Now; if (calcObject == null) { throw new LPGException("Could not find the Calc Object with the guid " + calcObjectReference.Guid); } var generalResultsDirectory = new DirectoryInfo(jcs.OutputDirectory ?? throw new LPGException("Output directory was null.")); var finishedFile = Path.Combine(generalResultsDirectory.FullName, Constants.FinishedFileFlag); if (Directory.Exists(generalResultsDirectory.FullName)) { if (jcs.SkipExisting) { if (File.Exists(finishedFile)) { Logger.Error("Directory already exists and calculation is finished. Exiting."); _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass()); return; } } Logger.Warning("Directory already exists, but calculation is not finished or skip existing is not specified. Deleting folder."); var files = generalResultsDirectory.GetFiles(); foreach (FileInfo file in files) { if (file.Name.StartsWith("Log.", StringComparison.OrdinalIgnoreCase)) { continue; } if (file.Name.EndsWith(".db3", StringComparison.OrdinalIgnoreCase)) { continue; } file.Delete(); } var directories = generalResultsDirectory.GetDirectories(); foreach (DirectoryInfo info in directories) { info.Delete(true); } Thread.Sleep(1000); } generalResultsDirectory.Create(); Thread.Sleep(500); Logger.SetLogFilePath(Path.Combine(generalResultsDirectory.FullName, "Log.CommandlineCalculation.txt")); Logger.LogToFile = true; Logger.Get().FlushExistingMessages(); Logger.Info("---------------------------"); Logger.Info("Used calculation specification:"); Logger.Info(JsonConvert.SerializeObject(jcs, Formatting.Indented), true); Logger.Info("---------------------------"); Logger.Info("Directory: " + generalResultsDirectory.FullName); sim.MyGeneralConfig.StartDateUIString = jcs.StartDate.ToString(); sim.MyGeneralConfig.EndDateUIString = jcs.EndDate.ToString(); sim.MyGeneralConfig.InternalTimeResolution = "00:01:00"; sim.MyGeneralConfig.DestinationPath = generalResultsDirectory.FullName; sim.MyGeneralConfig.ApplyOptionDefault(jcs.DefaultForOutputFiles); if (jcs.CalcOptions != null) { foreach (var option in jcs.CalcOptions) { //var option = option; /*if (option == null) { * throw new LPGException("Could not identify Calc Option " + option + ". Stopping."); * }*/ Logger.Info("Enabling option " + option); sim.MyGeneralConfig.Enable(option); } } if (jcs.DeleteDAT) { sim.MyGeneralConfig.DeleteDatFiles = "TRUE"; } else { sim.MyGeneralConfig.DeleteDatFiles = "FALSE"; } if (jcs.ExternalTimeResolution == null) { sim.MyGeneralConfig.ExternalTimeResolution = sim.MyGeneralConfig.InternalTimeResolution; } else { sim.MyGeneralConfig.ExternalTimeResolution = jcs.ExternalTimeResolution; } sim.MyGeneralConfig.RandomSeed = jcs.RandomSeed; var eit = jcs.EnergyIntensityType; if (eit == EnergyIntensityType.AsOriginal) { eit = calcObject.EnergyIntensityType; } var cs = new CalcStarter(sim); var temperatureProfile = sim.TemperatureProfiles.FindByJsonReference(jcs.TemperatureProfile); if (temperatureProfile == null) { throw new LPGException("Temperature Profile not found."); } if (jcs.GeographicLocation == null) { throw new LPGPBadParameterException("No geographic location was set in the calculation request"); } var geographicLocation = sim.GeographicLocations.FindByJsonReference(jcs.GeographicLocation); if (geographicLocation == null) { throw new LPGException("Geographic location not found."); } DeviceSelection deviceSelection = null; if (jcs.DeviceSelection != null) { deviceSelection = sim.DeviceSelections.FindByJsonReference(jcs.DeviceSelection); if (deviceSelection == null) { throw new LPGException("Unknown device selection \"" + jcs.DeviceSelection.Guid + "\""); } } if (jcs.EnableTransportation) { } if (jcs.LoadTypePriority == LoadTypePriority.Undefined) { if (calcObject.CalcObjectType == CalcObjectType.ModularHousehold) { jcs.LoadTypePriority = LoadTypePriority.RecommendedForHouseholds; } else { jcs.LoadTypePriority = LoadTypePriority.RecommendedForHouses; } } var options = sim.MyGeneralConfig.AllEnabledOptions(); //options.Add(CalcOption.OverallDats); var calcStartParameterSet = new CalcStartParameterSet(ReportFinishFuncForHouseAndSettlement, ReportFinishFuncForHousehold, OpenTabFunc, null, geographicLocation, temperatureProfile, calcObject, eit, ReportCancelFunc, false, deviceSelection, jcs.LoadTypePriority, null, null, options, sim.MyGeneralConfig.StartDateDateTime, sim.MyGeneralConfig.EndDateDateTime, sim.MyGeneralConfig.InternalStepSize, sim.MyGeneralConfig.CSVCharacter, jcs.RandomSeed, sim.MyGeneralConfig.ExternalStepSize, sim.MyGeneralConfig.DeleteDatFilesBool, sim.MyGeneralConfig.WriteExcelColumnBool, sim.MyGeneralConfig.ShowSettlingPeriodBool, 3, sim.MyGeneralConfig.RepetitionCount, _calculationProfiler, null, jcs.LoadtypesForPostprocessing, sim.MyGeneralConfig.DeviceProfileHeaderMode, jcs.IgnorePreviousActivitiesWhenNeeded, jcs.OutputDirectory, jcs.EnableTransportation); calcStartParameterSet.PreserveLogfileWhileClearingFolder = true; cs.Start(calcStartParameterSet); if (jcs.CalcOptions != null && jcs.CalcOptions.Contains(CalcOption.CalculationFlameChart)) { string targetfile = Path.Combine(generalResultsDirectory.FullName, Constants.CalculationProfilerJson); using (StreamWriter sw = new StreamWriter(targetfile)) { _calculationProfiler.WriteJson(sw); } } _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass()); if (makeAllCharts != null) { makeAllCharts(_calculationProfiler, calcStartParameterSet.ResultPath); } var duration = DateTime.Now - calculationStartTime; if (jcs.DeleteAllButPDF) { var allFileInfos = generalResultsDirectory.GetFiles("*.*", SearchOption.AllDirectories); foreach (var fi in allFileInfos) { if (fi.Name.ToUpperInvariant().EndsWith(".PDF", StringComparison.Ordinal)) { continue; } if (fi.Name.ToUpperInvariant().StartsWith("SUMPROFILES.", StringComparison.Ordinal)) { continue; } if (fi.Name.ToUpperInvariant().StartsWith("HOUSEHOLDNAME.", StringComparison.Ordinal)) { continue; } fi.Delete(); } } if (jcs.DeleteSqlite) { var allFileInfos = generalResultsDirectory.GetFiles("*.sqlite", SearchOption.AllDirectories); GC.Collect(); GC.WaitForPendingFinalizers(); foreach (var fi in allFileInfos) { try { fi.Delete(); } catch (Exception ex) { Logger.Exception(ex); } } } Logger.ImportantInfo("Calculation duration:" + duration); //cleanup empty directories var subdirs = generalResultsDirectory.GetDirectories(); foreach (var subdir in subdirs) { var files = subdir.GetFiles(); var subsubdirs = subdir.GetDirectories(); if (files.Length == 0 && subsubdirs.Length == 0) { subdir.Delete(); } } using (var sw = new StreamWriter(finishedFile)) { sw.WriteLine("Finished at " + DateTime.Now); sw.WriteLine("Duration in seconds:"); sw.WriteLine(duration.TotalSeconds); } }