Example #1
0
        protected override void RunActualProcess()
        {
            var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouses.RecreateTable <AirConditioningEntry>();
            var houses     = dbHouses.Fetch <House>();
            var businesses = dbHouses.Fetch <BusinessEntry>();

            dbHouses.BeginTransaction();
            foreach (var house in houses)
            {
                var housebusinesses = businesses.Where(x => x.HouseGuid == house.Guid);
                foreach (var business in housebusinesses)
                {
                    if (business.Name.Contains("ML & G AG, Migrol Service M. Leibundgut, Kirchbergstrasse 235, 3400 Burgdorf"))
                    {
                        Console.WriteLine("hi");
                    }

                    if (business.BusinessType == BusinessType.Shop && business.EffectiveEnergyDemand > 20000 &&
                        string.IsNullOrWhiteSpace(business.RlmProfileName))
                    {
                        var airconditioningEntry = new AirConditioningEntry(house.Guid,
                                                                            Guid.NewGuid().ToString(),
                                                                            business.EffectiveEnergyDemand * 0.1,
                                                                            3,
                                                                            AirConditioningType.Commercial,
                                                                            business.HausAnschlussGuid,
                                                                            house.ComplexName + " - Air Conditioning",
                                                                            business.Standort);
                        business.SetEnergyReduction("AC", airconditioningEntry.EffectiveEnergyDemand);
                        dbHouses.Save(airconditioningEntry);
                        dbHouses.Save(business);
                    }

                    if (business.BusinessType == BusinessType.Industrie && business.EffectiveEnergyDemand > 200000 &&
                        string.IsNullOrWhiteSpace(business.RlmProfileName))
                    {
                        var airconditioningEntry = new AirConditioningEntry(house.Guid,
                                                                            Guid.NewGuid().ToString(),
                                                                            business.EffectiveEnergyDemand * 0.1,
                                                                            3,
                                                                            AirConditioningType.Industrial,
                                                                            business.HausAnschlussGuid,
                                                                            house.ComplexName + " - Air conditioning",
                                                                            business.Standort);
                        business.SetEnergyReduction("AC", airconditioningEntry.EffectiveEnergyDemand);
                        dbHouses.Save(airconditioningEntry);
                        dbHouses.Save(business);
                    }
                }
            }

            dbHouses.CompleteTransaction();
        }
        protected override Prosumer ProvidePrivateProfile([NotNull] ProviderParameterDto ppdto)
        {
            AirConditioningEntry hse = (AirConditioningEntry)ppdto.HouseComponent;

            if (hse.HouseComponentType != HouseComponentType.Cooling)
            {
                throw new FlaException("Wrong type");
            }

            Hausanschluss ha = _dbDto.Hausanschlusse.Single(x => x.Guid == hse.HausAnschlussGuid);
            var           pa = new Prosumer(hse.HouseGuid,
                                            hse.Standort,
                                            hse.HouseComponentType,
                                            hse.SourceGuid,
                                            hse.FinalIsn,
                                            hse.HausAnschlussGuid,
                                            ha.ObjectID,
                                            GenerationOrLoad.Load,
                                            ha.Trafokreis,
                                            Name,
                                            "Cooling State Engine");

            // todo: randomize this with buckets and/or simulate a central control
            double targetRuntimePerDay       = 2 + Services.Rnd.NextDouble() * 2;
            double trigger                   = 1 - Services.Rnd.NextDouble() * 0.1;
            CoolingCalculationParameters hpc = new CoolingCalculationParameters(targetRuntimePerDay, trigger);
            var hpr = _coolingProfileGeneratorsByType[hse.AirConditioningType].Run(hpc, hse.EffectiveEnergyDemand, Services.Rnd);

            ppdto.HouseComponentResultObject.CoolingType = hse.AirConditioningType.ToString();
            pa.Profile = hpr.GetEnergyDemandProfile().ScaleToTargetSum(hse.EffectiveEnergyDemand, "Air Conditioning Profile", out var _);
            if (Math.Abs(pa.Profile.EnergySum() - hse.EffectiveEnergyDemand) > 1)
            {
                throw new FlaException("Energy sum from the cooling is all wrong. Should be " + hse.EffectiveEnergyDemand + " but was " +
                                       pa.Profile.EnergySum());
            }

            return(pa);
        }
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var dbSrcHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice.PreviousSliceNotNull);
            var dbDstHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);

            dbDstHouses.RecreateTable <AirConditioningEntry>();
            var srcAirconditioningEntries = dbSrcHouses.Fetch <AirConditioningEntry>();
            var srcHouses       = dbSrcHouses.Fetch <House>();
            var airConGuids     = srcAirconditioningEntries.Select(x => x.HouseGuid).ToList();
            var hausanschlusess = dbSrcHouses.Fetch <Hausanschluss>();
            var businesses      = dbDstHouses.Fetch <BusinessEntry>();
            var housesWithoutAirconditioning1 = srcHouses.Where(x => !airConGuids.Contains(x.Guid)).ToList();

            if (housesWithoutAirconditioning1.Count == 0)
            {
                throw new FlaException("no houses left without airconditioning");
            }

            foreach (var conditioningEntry in srcAirconditioningEntries)
            {
                conditioningEntry.HausAnschlussGuid.Should().NotBeNullOrWhiteSpace();
                var ha = hausanschlusess.First(x => x.Guid == conditioningEntry.HausAnschlussGuid);
                if (ha.ObjectID.ToLower().Contains("kleinanschluss"))
                {
                    throw new FlaException("No ac at kleinanschluss");
                }
            }

            // ReSharper disable once ArrangeLocalFunctionBody
            double CalculateYearlyAirConditioningEnergyConsumption(House h) =>
            h.EnergieBezugsFläche * slice.AirConditioningEnergyIntensityInKWhPerSquareMeter;

            double totalEbf = srcHouses.Sum(x => x.EnergieBezugsFläche);
            double totalTargetAirConditioning =
                totalEbf * slice.PercentageOfAreaWithAirConditioning * slice.AirConditioningEnergyIntensityInKWhPerSquareMeter;
            double currentAirConditioning = srcAirconditioningEntries.Sum(x => x.EffectiveEnergyDemand);
            double newlyInstealledEnergyForAirconditioning = totalTargetAirConditioning - currentAirConditioning;

            if (newlyInstealledEnergyForAirconditioning < 0)
            {
                throw new FlaException("Negative air conditioning target. Currently installed in " + slice + " are " +
                                       currentAirConditioning / (totalEbf * slice.AirConditioningEnergyIntensityInKWhPerSquareMeter));
            }

            bool failOnOver = slice.DstYear != 2050;
            WeightedRandomAllocator <House> allocator = new WeightedRandomAllocator <House>(Services.Rnd, Services.Logger);
            var housesForAirConditioning = allocator.PickObjectUntilLimit(housesWithoutAirconditioning1,
                                                                          x => x.EnergieBezugsFläche,
                                                                          CalculateYearlyAirConditioningEnergyConsumption,
                                                                          newlyInstealledEnergyForAirconditioning,
                                                                          failOnOver);
            var dstAirConditioningEntries = new List <AirConditioningEntry>();

            foreach (var house in housesForAirConditioning)
            {
                var businessesInHouse   = businesses.Where(x => x.HouseGuid == house.Guid).ToList();
                AirConditioningType act = AirConditioningType.Commercial;
                if (businessesInHouse.Count == 0)
                {
                    act = AirConditioningType.Residential;
                }

                foreach (var entry in businessesInHouse)
                {
                    if (entry.BusinessType == BusinessType.Industrie)
                    {
                        act = AirConditioningType.Industrial;
                    }
                }

                Hausanschluss ha = house.GetHausanschlussByIsn(new List <int>(), null, hausanschlusess, MyLogger, false);
                if (ha != null && ha.ObjectID.ToLower().Contains("kleinanschluss"))
                {
                    ha = null;
                }

                if (ha != null)
                {
                    var ace = new AirConditioningEntry(house.Guid,
                                                       Guid.NewGuid().ToString(),
                                                       CalculateYearlyAirConditioningEnergyConsumption(house),
                                                       1,
                                                       act,
                                                       ha.Guid,
                                                       house.ComplexName + " - Air Conditioning",
                                                       "");
                    dstAirConditioningEntries.Add(ace);
                }
            }

            foreach (var airconditioningEntry in srcAirconditioningEntries)
            {
                airconditioningEntry.ID = 0;
                dstAirConditioningEntries.Add(airconditioningEntry);
            }

            //final checks
            foreach (var dstAirConditioningEntry in dstAirConditioningEntries)
            {
                dstAirConditioningEntry.HausAnschlussGuid.Should().NotBeNullOrWhiteSpace();
                var ha = hausanschlusess.First(x => x.Guid == dstAirConditioningEntry.HausAnschlussGuid);
                if (ha.ObjectID.ToLower().Contains("kleinanschluss"))
                {
                    throw new FlaException("No ac at kleinanschluss");
                }
            }

            dbDstHouses.BeginTransaction();
            foreach (var airConditioningEntry in dstAirConditioningEntries)
            {
                dbDstHouses.Save(airConditioningEntry);
            }

            dbDstHouses.CompleteTransaction();
        }