public CalcManager GetCalcManager([NotNull] Simulator sim, [NotNull] CalcStartParameterSet csps, bool forceRandom) //, ICalcObject hh, //bool forceRandom, TemperatureProfile temperatureProfile, //GeographicLocation geographicLocation, EnergyIntensityType energyIntensity, //string fileVersion, LoadTypePriority loadTypePriority, [CanBeNull] DeviceSelection deviceSelection, //TransportationDeviceSet transportationDeviceSet, TravelRouteSet travelRouteSet, //) { csps.CalculationProfiler.StartPart(Utili.GetCurrentMethodAndClass() + " Initializing"); if (sim == null) { throw new LPGException("Simulation was null"); } if (csps.CalcOptions.Contains(CalcOption.LogAllMessages)) { Logger.Get().StartCollectingAllMessages(); } CalcManager cm = null; Logger.Info("Starting the calculation of " + csps.CalcTarget.Name); try { if (DoIntegrityRun) { SimIntegrityChecker.Run(sim); } if (csps.CalcTarget.CalcObjectType == CalcObjectType.House && (csps.LoadTypePriority == LoadTypePriority.RecommendedForHouseholds || csps.LoadTypePriority == LoadTypePriority.Mandatory)) { throw new DataIntegrityException( "You are trying to calculate a house with only the load types for a household. This would mess up the warm water calculations. Please fix the load type selection."); } var chh = csps.CalcTarget as ModularHousehold; var ds = GetDeviceSelection(csps, csps.CalcTarget, chh); var cpf = new CalcParametersFactory(); var calcParameters = cpf.MakeCalculationParametersFromConfig(csps, forceRandom); var sqlFileName = Path.Combine(csps.ResultPath, "Results.sqlite"); var builder = new ContainerBuilder(); RegisterEverything(sim, csps.ResultPath, csps, csps.CalcTarget, builder, sqlFileName, calcParameters, ds); csps.CalculationProfiler.StopPart(Utili.GetCurrentMethodAndClass() + " Initializing"); csps.CalculationProfiler.StartPart(Utili.GetCurrentMethodAndClass() + " Generating Model"); var container = builder.Build(); using (var scope = container.BeginLifetimeScope()) { var calcRepo = PrepareCalculation(sim, csps, scope, out var dtoltdict, out var dls, out var variableRepository); cm = new CalcManager(csps.ResultPath, //hh.Name, //householdPlans, //csps.LPGVersion, calcParameters.ActualRandomSeed, dls, variableRepository, calcRepo //scope.Resolve<SqlResultLoggingService>() ); //_calcParameters.Logfile = cm.Logfile; //_calcParameters.NormalDistributedRandom = normalDistributedRandom; //_calcParameters.RandomGenerator = randomGenerator; //_calcParameters.Odap = cm.Odap; //_calcParameters.EnergyIntensity = csps.EnergyIntensity; // no vacation times needed for the light array CalcObjectType cot; ICalcAbleObject ch; CalcVariableDtoFactory cvrdto = scope.Resolve <CalcVariableDtoFactory>(); CalcDeviceTaggingSets devicetaggingSets = scope.Resolve <CalcDeviceTaggingSets>(); if (csps.CalcTarget.GetType() == typeof(House)) { ch = MakeCalcHouseObject(sim, csps, csps.CalcTarget, scope, cvrdto, variableRepository, out cot, calcRepo); CalcHouse chd = (CalcHouse)ch; if (chd.EnergyStorages != null) { foreach (var calcEnergyStorage in chd.EnergyStorages) { foreach (var taggingSet in devicetaggingSets.AllCalcDeviceTaggingSets) { taggingSet.AddTag(calcEnergyStorage.Name, "Energy Storage"); } } } } else if (csps.CalcTarget.GetType() == typeof(ModularHousehold)) { ch = MakeCalcHouseholdObject(sim, csps, csps.CalcTarget, scope, cvrdto, variableRepository, out cot, calcRepo); } else { throw new LPGException("The type " + csps.CalcTarget.GetType() + " is missing!"); } if (calcRepo.CalcParameters.Options.Contains(CalcOption.HouseholdContents)) { calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, devicetaggingSets.AllCalcDeviceTaggingSets); } CalcObjectInformation coi = new CalcObjectInformation(cot, ch.Name, csps.ResultPath); if (calcRepo.CalcParameters.Options.Contains(CalcOption.HouseholdContents)) { calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, coi); } //this logger doesnt save json, but strings! calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, csps); calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, dtoltdict.GetLoadTypeDtos()); cm.SetCalcObject(ch); CalcManager.ExitCalcFunction = false; //LogSeed(calcParameters.ActualRandomSeed, lf.FileFactoryAndTracker, calcParameters); csps.CalculationProfiler.StopPart(Utili.GetCurrentMethodAndClass() + " Generating Model"); return(cm); } } catch { cm?.Dispose(); 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); }