Exemplo n.º 1
0
        public static void MakeChartsAndPDF(CalculationProfiler calculationProfiler, string resultPath)
        {
            Exception innerException = null;
            var       t = new Thread(() => {
                try
                {
                    SqlResultLoggingService srls = new SqlResultLoggingService(resultPath);
                    CalcParameterLogger cpl      = new CalcParameterLogger(srls);
                    InputDataLogger idl          = new InputDataLogger(Array.Empty <IDataSaverBase>());
                    var calcParameters           = cpl.Load();
                    Logger.Info("Checking for charting parameters");
                    if (!calcParameters.IsSet(CalcOption.MakePDF) && !calcParameters.IsSet(CalcOption.MakeGraphics))
                    {
                        Logger.Info("No charts wanted");
                        return;
                    }
                    calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass() + " - Charting");

                    using (FileFactoryAndTracker fileFactoryAndTracker =
                               new FileFactoryAndTracker(resultPath, "Name", idl)) {
                        calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass() + " - Chart Generator RunAll");
                        ChartCreationParameters ccp = new ChartCreationParameters(144, 1600, 1000,
                                                                                  false, calcParameters.CSVCharacter, new DirectoryInfo(resultPath));
                        var cg = new ChartGeneratorManager(calculationProfiler, fileFactoryAndTracker, ccp);
                        cg.Run(resultPath);
                        calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass() + " - Chart Generator RunAll");
                        if (calcParameters.IsSet(CalcOption.MakePDF))
                        {
                            calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass() + " - PDF Creation");
                            Logger.ImportantInfo(
                                "Creating the PDF. This will take a really long time without any progress report...");

                            MigraPDFCreator mpc = new MigraPDFCreator(calculationProfiler);
                            mpc.MakeDocument(resultPath, "", false, false,
                                             calcParameters.CSVCharacter, fileFactoryAndTracker);
                            calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass() + " - PDF Creation");
                        }
                    }
                    Logger.Info("Finished making the charts");
                    calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass() + " - Charting");
                    CalculationDurationFlameChart cdfc = new CalculationDurationFlameChart();
                    cdfc.Run(calculationProfiler, resultPath, "CalcManager");
                }
                catch (Exception ex)
                {
                    innerException = ex;
                    Logger.Exception(ex);
                }
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
            if (innerException != null)
            {
                Logger.Error("Exception during the PDF creation!");
                Logger.Exception(innerException);
                throw innerException;
            }
        }
Exemplo n.º 2
0
        public static void LaunchParallel([NotNull] ParallelJsonLauncherOptions options)
        {
            Logger.LogFileIndex = "JsonParallelCalculationStarter";
            Logger.LogToFile    = true;
            CalculationProfiler calculationProfiler = new CalculationProfiler();

            calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            var pl = new ParallelJsonLauncher();

            pl.LaunchParallelInternal(options);
            calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
        }
        public bool Export([NotNull] JsonDatabaseExportOptions calcDirectoryOptions)
        {
            _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            string jsonFileName = calcDirectoryOptions.Output;

            if (jsonFileName == null)
            {
                Logger.Error("No file was set.");
                return(false);
            }

            Logger.Info("Loading...");
            var sim = new Simulator(_connectionString);

            Logger.Info("Loading finished.");
            switch (calcDirectoryOptions.ProcessingType)
            {
            case TypesToProcess.HouseholdTemplates:
                ExportStuff <HouseholdTemplate.JsonDto, HouseholdTemplate>(jsonFileName,
                                                                           sim.HouseholdTemplates.It.ToList());
                break;

            case TypesToProcess.ModularHouseholds:
                ExportStuff <ModularHousehold.JsonModularHousehold, ModularHousehold>(jsonFileName,
                                                                                      sim.ModularHouseholds.It.ToList());
                break;

            case TypesToProcess.None:
                throw new LPGException("You need to set a type that you want to process");

            case TypesToProcess.HouseholdTraits:
                ExportStuff <HouseholdTrait.JsonDto, HouseholdTrait>(jsonFileName,
                                                                     sim.HouseholdTraits.It.ToList());
                break;

            case TypesToProcess.HouseholdTraitsWithDeviceCategories:
                ExportHHTsWithDeviceCategories(jsonFileName, sim);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(TypesToProcess));
            }

            _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
            return(true);
        }
Exemplo n.º 4
0
        protected override FileProcessingResult MakeOnePlot(HouseholdKeyEntry hhkey)
        {
            CalculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            var entries       = CalcDataRepository.LoadAffordanceEnergyUses(hhkey.HHKey);
            var usedLoadtypes = entries.Select(x => x.LoadTypeGuid).Distinct().ToList();
            var loadTypeInfos = CalcDataRepository.LoadTypes;

            foreach (StrGuid loadtypeGuid in usedLoadtypes)
            {
                var lti = loadTypeInfos.Single(x => x.Guid == loadtypeGuid);

                List <AffordanceEnergyUseEntry> filteredEntries = entries.Where(x => x.LoadTypeGuid == loadtypeGuid).ToList();
                var persons = filteredEntries.Select(x => x.PersonName).Distinct().ToList();
                PrepareData(filteredEntries, out var energyUsesPerPersonByAffordance, persons);
                DrawChart(hhkey, energyUsesPerPersonByAffordance, persons, lti);
            }

            CalculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
            return(FileProcessingResult.ShouldCreateFiles);
        }
Exemplo n.º 5
0
        public bool Import([NotNull] CsvImportOptions calcDirectoryOptions, [CanBeNull] out DateBasedProfile newProfile)
        {
            _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            string csvFilename = calcDirectoryOptions.Input;

            newProfile = null;
            if (csvFilename == null)
            {
                Logger.Error("No file was set.");
                return(false);
            }

            if (!File.Exists(csvFilename))
            {
                Logger.Error("File does not exist");
                return(false);
            }

            if (calcDirectoryOptions.Delimiter == null)
            {
                Logger.Error("No delimiter set.");
                return(false);
            }
            Logger.Info("Loading...");
            CSVImporter ci = new CSVImporter(true)
            {
                FileName        = csvFilename,
                TimeColumn      = 1,
                HeaderLineCount = 1,
                Separator       = calcDirectoryOptions.Delimiter[0],
                Column          = 2
            };

            ci.RefreshEntries();
            if (ci.Entries.Count == 0)
            {
                throw new LPGException("Not a single entry was found");
            }
            Logger.Info("PreviewText from the import:");
            Logger.Info(ci.PreviewText, true);
            //asd

            var sim = new Simulator(_connectionString);

            Logger.Info("Loading finished.");

            Logger.Info("Importing " + ci.Entries.Count + " datapoints...");
            var dbp = sim.DateBasedProfiles.CreateNewItem(sim.ConnectionString);

            for (var i = 0; i < ci.Entries.Count; i++)
            {
                var ce   = ci.Entries[i];
                var date = ce.Time;
                dbp.AddNewDatePoint(date, ce.Value, false);
            }
            dbp.Datapoints.Sort();
            dbp.SaveToDB();
            newProfile = dbp;
            Logger.Info("Imported all data points.");

            _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
            return(true);
        }
        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);
            }
        }
        public bool Import([NotNull] JsonDatabaseImportOptions calcDirectoryOptions)
        {
            _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            string jsonFileName = calcDirectoryOptions.Input;

            if (jsonFileName == null)
            {
                Logger.Error("No file was set.");
                return(false);
            }

            if (!File.Exists(jsonFileName))
            {
                Logger.Error("File does not exist");
                return(false);
            }
            Logger.Info("Loading...");
            var sim = new Simulator(_connectionString);

            Logger.Info("Loading finished.");
            switch (calcDirectoryOptions.Type)
            {
            case TypesToProcess.HouseholdTemplates: {
                string json = File.ReadAllText(jsonFileName);
                var    hhts = JsonConvert.DeserializeObject <List <HouseholdTemplate.JsonDto> >(json);
                Logger.Info("Started loading " + hhts.Count + " household templates");
                HouseholdTemplate.ImportObjectFromJson(sim, hhts);
                Logger.Info("Finished loading " + hhts.Count + " household templates");
                break;
            }

            case TypesToProcess.ModularHouseholds: {
                string json = File.ReadAllText(jsonFileName);
                var    hhts = JsonConvert.DeserializeObject <List <ModularHousehold.JsonModularHousehold> >(json);
                Logger.Info("Started loading " + hhts.Count + " households");
                ModularHousehold.ImportObjectFromJson(sim, hhts);
                Logger.Info("Finished loading " + hhts.Count + " households");
                break;
            }

            case TypesToProcess.None:
                throw new LPGException("You need to set a type that you want to process");

            case TypesToProcess.HouseholdTraits: {
                string json = File.ReadAllText(jsonFileName);
                var    hhts = JsonConvert.DeserializeObject <List <HouseholdTrait.JsonDto> >(json);
                Logger.Info("Started loading " + hhts.Count + " traits");
                HouseholdTrait.ImportObjectFromJson(sim, hhts);
                Logger.Info("Finished loading " + hhts.Count + " traits");
            }
            break;

            case TypesToProcess.HouseholdTraitsWithDeviceCategories:
                throw new LPGException("You need to set a type that you want to process");

            default:
                throw new ArgumentOutOfRangeException(nameof(JsonDatabaseImportOptions));
            }

            _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
            return(true);
        }