private static ICalcAbleObject MakeCalcHouseholdObject([NotNull] Simulator sim, [NotNull] CalcStartParameterSet csps, [NotNull] ICalcObject hh,
                                                               [NotNull] ILifetimeScope scope,
                                                               [NotNull] CalcVariableDtoFactory cvrdto,
                                                               [NotNull] CalcVariableRepository variableRepository,
                                                               out CalcObjectType cot,
                                                               [NotNull] CalcRepo calcRepo)
        {
            var              cmhdf        = scope.Resolve <CalcModularHouseholdDtoFactory>();
            HouseholdKey     householdKey = new HouseholdKey("HH1");
            CalcHouseholdDto dto          = cmhdf.MakeCalcModularHouseholdDto(sim, (ModularHousehold)hh,
                                                                              csps.TemperatureProfile, householdKey, csps.GeographicLocation,
                                                                              out _, csps.TransportationDeviceSet, csps.TravelRouteSet,
                                                                              csps.EnergyIntensity, csps.ChargingStationSet);
            var cmhf = scope.Resolve <CalcModularHouseholdFactory>();

            /*foreach (var v in dto.CalcVariables)
             *              {
             *                  variableRepository.RegisterVariable(new CalcVariable(v.Name, v.Guid, v.Value, v.LocationName, v.LocationGuid, v.HouseholdKey));
             *              }*/
            foreach (HouseholdKeyEntry entry in dto.GetHouseholdKeyEntries())
            {
                calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, entry);
            }

            calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, dto);
            RegisterAllDtoVariables(cvrdto, variableRepository);
            ICalcAbleObject ch = cmhf.MakeCalcModularHousehold(dto, out _, null, null, calcRepo);

            cot = CalcObjectType.ModularHousehold;
            return(ch);
        }
        private static ICalcAbleObject MakeCalcHouseObject([NotNull] Simulator sim,
                                                           [NotNull] CalcStartParameterSet csps, [NotNull] ICalcObject hh,
                                                           [NotNull] ILifetimeScope scope,
                                                           [NotNull] CalcVariableDtoFactory cvrdto, [NotNull] CalcVariableRepository variableRepository,
                                                           out CalcObjectType cot, [NotNull] CalcRepo calcRepo)
        {
            var house = (House)hh;

            calcRepo.FileFactoryAndTracker.RegisterHousehold(Constants.HouseKey, "House Infrastructure",
                                                             HouseholdKeyType.House, "House Infrastructure", house.Name, house.Description);
            var housedtoFac = scope.Resolve <CalcHouseDtoFactory>();
            var housedto    = housedtoFac.MakeHouseDto(sim, house, csps.TemperatureProfile,
                                                       csps.GeographicLocation, csps.EnergyIntensity);

            foreach (HouseholdKeyEntry entry in housedto.GetHouseholdKeyEntries())
            {
                calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, entry);
            }

            var convertedAutoDevList = housedto.AutoDevs.ConvertAll(x => (IHouseholdKey)x).ToList();

            if (calcRepo.CalcParameters.Options.Contains(CalcOption.HouseholdContents))
            {
                calcRepo.InputDataLogger.SaveList(convertedAutoDevList);
                calcRepo.InputDataLogger.Save(Constants.GeneralHouseholdKey, housedto);
            }

            var chf = scope.Resolve <CalcHouseFactory>();

            RegisterAllDtoVariables(cvrdto, variableRepository);
            ICalcAbleObject ch = chf.MakeCalcHouse(housedto, calcRepo);

            cot = CalcObjectType.House;
            return(ch);
        }
 private static void RegisterAllDtoVariables([NotNull] CalcVariableDtoFactory cvrdto, [NotNull] CalcVariableRepository variableRepository)
 {
     foreach (var v in cvrdto.GetAllVariableDtos())
     {
         variableRepository.RegisterVariable(new CalcVariable(v.Name, v.Guid, v.Value, v.LocationName,
                                                              v.LocationGuid, v.HouseholdKey));
     }
 }
 public CalcHouseDtoFactory([NotNull] CalcLoadTypeDtoDictionary ltDict,
                            [NotNull] Random random,
                            [NotNull] IDeviceCategoryPicker picker,
                            [NotNull] CalcParameters calcParameters,
                            [NotNull] CalcModularHouseholdDtoFactory hhDtoFactory,
                            [NotNull] AvailabilityDtoRepository availabilityDtoRepository,
                            [NotNull] CalcVariableDtoFactory calcVariableDtoFactory)
 {
     _ltDict                    = ltDict;
     _random                    = random;
     _picker                    = picker;
     _calcParameters            = calcParameters;
     _hhDtoFactory              = hhDtoFactory;
     _availabilityDtoRepository = availabilityDtoRepository;
     _calcVariableDtoFactory    = calcVariableDtoFactory;
 }
        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 List <CalcEnergyStorageDto> CreateEnergyStorageDtos([NotNull][ItemNotNull] List <EnergyStorage> energyStorages,
                                                                           [NotNull] CalcLoadTypeDtoDictionary ltdict,
                                                                           [NotNull] HouseholdKey householdKey, CalcVariableDtoFactory calcVariableDtoFactory)
        {
            var calcEnergyStorages = new List <CalcEnergyStorageDto>();

            foreach (var es in energyStorages)
            {
                if (es.LoadType == null)
                {
                    throw new LPGException("Energy storage load type was null");
                }

                if (ltdict.SimulateLoadtype(es.LoadType))
                {
                    List <CalcEnergyStorageSignalDto> signals = new List <CalcEnergyStorageSignalDto>();
                    foreach (var signal in es.Signals)
                    {
                        if (signal.Variable == null)
                        {
                            throw new DataIntegrityException("Signal variable was null");
                        }

                        var cvdto = calcVariableDtoFactory.RegisterVariableIfNotRegistered(signal.Variable, "House",
                                                                                           Constants.HouseLocationGuid, Constants.HouseKey);
                        var cessig = new CalcEnergyStorageSignalDto(signal.Name,
                                                                    signal.IntID,
                                                                    signal.TriggerLevelOff,
                                                                    signal.TriggerLevelOn,
                                                                    signal.Value,
                                                                    cvdto,
                                                                    Guid.NewGuid().ToStrGuid());
                        signals.Add(cessig);
                    }

                    //foreach (DeviceTaggingSet set in deviceTaggingSets) {
                    //set.AddTag(es.Name,"House Device");
                    //}
                    var lt  = ltdict.GetLoadtypeDtoByLoadType(es.LoadType);
                    var ces = new CalcEnergyStorageDto(es.Name,
                                                       es.IntID,
                                                       lt,
                                                       es.MaximumStorageRate,
                                                       es.MaximumWithdrawRate,
                                                       es.MinimumStorageRate,
                                                       es.MinimumWithdrawRate,
                                                       es.InitialFill,
                                                       es.StorageCapacity,
                                                       householdKey,
                                                       Guid.NewGuid().ToStrGuid(),
                                                       signals);
                    calcEnergyStorages.Add(ces);
                    if (es.Signals.Count != ces.Signals.Count)
                    {
                        throw new LPGException("Signals for energy storage were not correctly initialized");
                    }
                }
                else
                {
                    throw new DataIntegrityException("You are trying to run a calculation with a house type that uses an energy storage, " +
                                                     " that references an load type you excluded. This will not work. Please enable the load type " +
                                                     es.LoadType + " by for example chosing All Loadtypes");
                }
            }

            return(calcEnergyStorages);
        }