private static void SaveRun(bool forceRandom,
                                    [NotNull] Simulator sim,
                                    [NotNull] CalcStartParameterSet csps)
        {
            bool allgood = true;

#pragma warning disable 162
            bool success = RunOneCalcEntry(csps, sim, forceRandom);
            if (!success)
            {
                allgood = false;
            }
            //}

            if (!CalcStarter.ContinueRunning)
            {
                csps.ReportCancelFunc?.Invoke();
                return;
            }
#pragma warning restore 162
            if (allgood)
            {
                var cpf            = new CalcParametersFactory();
                var calcParameters = cpf.MakeCalculationParametersFromConfig(csps, forceRandom);
                var cpp            = new DatFileDeletor(calcParameters, csps.ResultPath, csps.CalcTarget.Name);
                cpp.ProcessResults();
                SaveCallFunction(csps.ReportFinishFuncForHousehold, csps);
            }
            else
            {
                throw new LPGException("No Results in CalcQueRunner! Please report this bug.");
            }
        }
        private static void SaveCallFunction([CanBeNull] Func <bool, string, string, bool> reportFinishFuncForHousehold,
                                             [NotNull] CalcStartParameterSet csps)
        {
            if (reportFinishFuncForHousehold == null)
            {
                return;
            }

            Logger.Get()
            .SafeExecuteWithWait(
                () => reportFinishFuncForHousehold(true, csps.CalcTarget.Name, csps.ResultPath));
        }
Пример #3
0
 public void Start([NotNull] CalcStartParameterSet csps)
 {
     if (!csps.ResumeSettlement || csps.CalcTarget.CalcObjectType != CalcObjectType.Settlement)
     {
         if (!Config.IsInHeadless)
         {
             if (!CheckAndClearDirectory(csps.ResultPath, csps.PreserveLogfileWhileClearingFolder))
             {
                 csps.ReportFinishFuncForHousehold?.Invoke(false, string.Empty, csps.ResultPath);
                 return;
             }
         }
     }
     try {
         var cqr = new CalcQueueRunner();
         cqr.Start(csps, _sim);
     }
     catch (Exception e) {
         Logger.Exception(e);
         CalcQueueRunner.CloseLogfilesAfterError();
         if (!Config.IsInUnitTesting && !Config.IsInHeadless)
         {
             csps.ReportCancelFunc?.Invoke();
             if (e is DataIntegrityException exception)
             {
                 MessageWindowHandler.Mw.ShowDataIntegrityMessage(exception);
             }
             else
             {
                 MessageWindowHandler.Mw.ShowDebugMessage(e);
             }
         }
         else
         {
             throw;
         }
     }
 }
 public void Start([NotNull] CalcStartParameterSet csps, [NotNull] Simulator sim)
 {
     try {
         var forceRandom = false;
         if (csps.CalcTarget.GetType() == typeof(Settlement))
         {
             forceRandom = true;
         }
         SaveRun(forceRandom, sim, csps);
     }
     catch (DataIntegrityException e) {
         Logger.Error("DataIntegrityException:" + Environment.NewLine + e.Message);
         CloseLogfilesAfterError();
         if (!Config.IsInUnitTesting && !Config.IsInHeadless)
         {
             MessageWindowHandler.Mw.ShowDataIntegrityMessage(e);
             csps.ReportCancelFunc?.Invoke();
         }
         else
         {
             throw;
         }
     }
 }
        private static bool RunOneCalcEntry([NotNull] CalcStartParameterSet csps, [NotNull] Simulator sim, bool forceRandom)
        {
            CalcManager.StartRunning();
            Logger.Info("Running the simulation for " + csps.CalcTarget.Name + " from " +
                        sim.MyGeneralConfig.StartDateDateTime.ToShortDateString() + " to " +
                        sim.MyGeneralConfig.EndDateDateTime.ToShortDateString());
            CalcManager calcManager = null;

            try {
                var cmf = new CalcManagerFactory();
                calcManager = cmf.GetCalcManager(sim, csps, forceRandom);
                //, forceRandom,
                //temperatureProfile,  geographicLocation, calcEntry.EnergyIntensity,
                //fileVersion, loadTypePriority, deviceSelection, transportationDeviceSet, travelRouteSet);
                _calcManagers.Add(calcManager);

                /*CalcObjectType cot;
                 * if (calcEntry.CalcObject.GetType() == typeof(ModularHousehold)) {
                 *  cot = CalcObjectType.ModularHousehold;
                 * }
                 * else if (calcEntry.CalcObject.GetType() == typeof(House)) {
                 *  cot = CalcObjectType.House;
                 * }
                 * else if (calcEntry.CalcObject.GetType() == typeof(Settlement)) {
                 *  cot = CalcObjectType.Settlement;
                 * }
                 * else {
                 *  throw new LPGException("Forgotten Calc Object Type. Please report!");
                 * }*/
                calcManager.Run(csps.ReportCancelFunc);

                Logger.Info("Finished the simulation...");
                _calcManagers.Remove(calcManager);
            }
            catch (DataIntegrityException die) {
                calcManager?.Dispose();
                if (die.Element != null)
                {
                    if (csps.OpenTabFunc == null)
                    {
                        throw new LPGException("OpenTabFunc was null");
                    }
                    if (csps.Dispatcher != null && !csps.Dispatcher.IsCorrectThread())
                    {
                        csps.Dispatcher.BeginInvoke(csps.OpenTabFunc, die.Element);
                    }
                    else
                    {
                        csps.OpenTabFunc(die.Element);
                    }
                }
                csps.ReportCancelFunc?.Invoke();

                throw;
            }
            finally {
                if (calcManager != null)
                {
                    calcManager.Dispose();
                    // ReSharper disable once RedundantAssignment
#pragma warning disable S1854   // Dead stores should be removed
#pragma warning disable IDE0059 // Value assigned to symbol is never used
                    calcManager = null;
#pragma warning restore IDE0059 // Value assigned to symbol is never used
#pragma warning restore S1854   // Dead stores should be removed
#pragma warning disable S1215   // "GC.Collect" should not be called
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
#pragma warning restore S1215 // "GC.Collect" should not be called
                    //if (Logger.Get().Errors.Count == 0) {
                    //    string finishedFile = Path.Combine(csps.ResultPath, Constants.FinishedFileFlag);
                    //    TimeSpan duration = DateTime.Now - csps.CalculationStartTime;
                    //    using (var sw = new StreamWriter(finishedFile)) {
                    //        sw.WriteLine("Finished at " + DateTime.Now);
                    //        sw.WriteLine("Duration in seconds:");
                    //        sw.WriteLine(duration.TotalSeconds);
                    //    }
                    //}
                }
            }

            return(true);
        }