Battery size calculator

Imports

In [1]:
import csv
import random
import matplotlib.pyplot as plt
%matplotlib inline
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 12# Set Figure size
fig_size[1] = 7

FileDir = ""

Pre-processing input files

In [2]:
with open("{}Accugrootte_LOD100.csv".format(FileDir)) as File:
    Data = File.read().split("\n")

    #Postprocessing file
dat = []
Battery = []
Charges = []
NewBat = []
NetResult = []
for i in Data: 
    try:
        NetResult.append(0)
        Battery.append(0)
        NewBat.append(0)
        Charges.append(0)
        dat.append(float(i))
    except ValueError:
        print i
Data = dat
dat = ""

In [3]:
Qdal = 6862
Qnorm = 6797
M3gas = 3336/2
Qvw = 8574.5 # kWh

Qgas = M3gas*9.769 # COP = 2; heatpump!
print Qgas+Qdal+Qnorm
29953.692

Open KNMI Weather input (Tmax, Tmin, Qsolar)

In [4]:
with open("{}KNMI_20180101.txt".format(FileDir)) as File:
    Input = File.read().split("\n")
Input = Input[14:]
Input = Input[:-2]
In [5]:
DegreeDays = [18-((float(i.split(",")[2])+int(i.split(",")[3]))/20) for i in Input]
DegreeDays = [x if x >= 0 else 0 for x in DegreeDays] #if < 0; make 0. Else degreedays
TotalEnergy = sum(float(i.split(",")[4]) for i in Input)
DegreeSum = sum(DegreeDays)
print DegreeSum
print TotalEnergy
RadiationFactor = [float(x.split(",")[4])/TotalEnergy for x in Input]
DegreeFactor = [x/DegreeSum for x in DegreeDays]
2899.2
359676.0

Assumption for amount of PV-panels

In [6]:
PVpanels = 60
h = 99.5
b = 165
eta = 0.23
PVEnergy = PVpanels*h*b*(2.77777778*10**-7)*eta*TotalEnergy*0.62
print PVEnergy
14034.1708795

Total demand of the district has been divided between different sinks, where heating will be aproached dynamically

In [7]:
## This cell contains Yearly energy usage for the following posts:
# Everything in kWh
# Also add energy production!
#PVEnergy = 565000 
EDomestic = 210600
#EHeating = 81000
EHeating = Qgas
EDHW = 81000
EHelp = 40500
EGeneral = 52650
Total = Qdal+Qnorm#EDomestic + EHeating + EDHW + EHelp + EGeneral
Margin = 0.2*Total
In [8]:
EPV = [x*(PVEnergy) for x in RadiationFactor]
EHeat = [x*(EHeating) for x in DegreeFactor]
Eetc = [(Total + Margin/2)/365 +random.randint(-1,1) for x in DegreeFactor]
Data = [EHeat[i]+Eetc[i]-x for i,x in enumerate(EPV)]
In [9]:
##Optimalization settings
Iterations = 5000
Mutation = 10
BatSize = 2000
ChargeSpeed = 1#%
ChargeSpeedMax = ChargeSpeed*24*BatSize
ChargeSpeedMin = -ChargeSpeed*24*BatSize
ResultArray = []
Error = 10000000000000
NewError = 10000000000000
SizeIterations = 11
#TotalEnergy = 513570.7123
TotalEnergy2 = Qdal+Qnorm+Qgas
Charge = 100

Initital conditions for simulation

The simulation below will increment the battery size and show results

In [10]:
for k in xrange(SizeIterations):
    BatSize = (2**k)*20
    Iterations = 5000
    Mutation = 1
    ChargeSpeed = 1
    ChargeSpeedMax = ChargeSpeed*BatSize
    ChargeSpeedMin = -ChargeSpeed*BatSize
    ResultArray = []
    Error = 10000000000000
    NewError = 10000000000000
    Charge = 1000
    for i in xrange(Iterations):   
        if NewError < Error:
            #print "Yay, better set"
            Error = NewError
            Battery = NewBat
        GreyEnergy = 0
        NewError = 0
        if i == 0 and Charges[-3] == 0:
            Charge = 5
        else:
            Charge = Charges[-3]
        for x,Days in enumerate(Data):
            Bat = Battery[x]
            if Charge > 0:
                Charge = 0.99975*Charge #1% leakage
            #WTD = random.randint(0,2)
    #         if WTD == 0:
    #             continue
    #         elif WTD == 1:
    #             Bat -= random.randint(0,Mutation) 
    #         else:
    #             Bat += random.randint(0,Mutation)

            if Days < Bat:# and (Charge <= 0):
                if Charge >= 0:
                    Bat -= random.randint(0,Mutation)
                elif Charge >= BatSize:
                    Bat -= random.randint(0,Mutation)
            elif Days >= Bat:# and (Charge <= BatSize):
                if Charge <= BatSize:
                    Bat += random.randint(0,Mutation)
                elif Charge <= 0:
                    Bat += random.randint(0,Mutation)

            if Charge < 0 and Bat > 0:
                Bat = 0
            if Charge > BatSize and Bat < 0:
                Bat = 0

            if Bat <= ChargeSpeedMin:
                Bat = ChargeSpeedMin
            if Bat >= ChargeSpeedMax:
                Bat = ChargeSpeedMax

            NewError += (Days - Bat)**2
            if Charge > BatSize:
                NewError += (Charge-BatSize)**3
            elif Charge < 0:
                NewError += (0-Charge)**3
            if Charge - Bat < 0:
                Bat = 0
            if Charge < 0:
                Charge = 0
            Charge -= Bat
            NetResult[x] = (Days - Bat)
            NewBat[x] = Bat
            Charges[x] = Charge
            if NetResult[x] > 0:
                GreyEnergy += NetResult[x]

        #print "Residuals, Best:{}, New:{}".format(Error,NewError)
    
    
    plt.rcParams["figure.figsize"] = fig_size
    plt.plot(Data,label="Vraag - PV-opwekking [Positief = vraag, negatief = aanbod]",color='b', linewidth=2)
    plt.plot(NetResult,label="Vraag - PV-opwekking - laden/ontladen Accu",color='r', linewidth=2)
    plt.plot(Battery,label="Laden [positief], ontladen[negatief]",color='g', linewidth=2)
    plt.plot(Charges,label="Accu-lading",color='m',linestyle ="-.", linewidth=2)
    plt.legend()
    plt.title("Battery size: {} kWh".format(BatSize))
    
    plt.show()
    print "AccuGrootte: {} kWh".format(BatSize)
    print "Residuals, Best:{}, Nieuw:{}".format(Error,NewError)
    print "Totale Energie:{} kWh \nGrijze Energy:{} kWh".format(TotalEnergy2,GreyEnergy)
    print "zelf-voorzienend: {} %".format((1-(GreyEnergy/TotalEnergy2))*100)
    #print Battery
AccuGrootte: 20 kWh
Residuals, Best:2002352.56891, Nieuw:2005158.65812
Totale Energie:29953.692 kWh 
Grijze Energy:19643.9318288 kWh
zelf-voorzienend: 34.4189964002 %
AccuGrootte: 40 kWh
Residuals, Best:1998272.01192, Nieuw:2090870.35093
Totale Energie:29953.692 kWh 
Grijze Energy:19499.1175583 kWh
zelf-voorzienend: 34.9024569048 %
AccuGrootte: 80 kWh
Residuals, Best:1994848.29933, Nieuw:2605452.93676
Totale Energie:29953.692 kWh 
Grijze Energy:19363.0737322 kWh
zelf-voorzienend: 35.3566373981 %
AccuGrootte: 160 kWh
Residuals, Best:1984952.70424, Nieuw:2155788.40094
Totale Energie:29953.692 kWh 
Grijze Energy:19294.9993705 kWh
zelf-voorzienend: 35.583902744 %
AccuGrootte: 320 kWh
Residuals, Best:1961988.62318, Nieuw:2079589.39755
Totale Energie:29953.692 kWh 
Grijze Energy:19142.2509274 kWh
zelf-voorzienend: 36.0938513778 %
AccuGrootte: 640 kWh
Residuals, Best:1915043.47132, Nieuw:2277296.08003
Totale Energie:29953.692 kWh 
Grijze Energy:18823.7590229 kWh
zelf-voorzienend: 37.1571323399 %
AccuGrootte: 1280 kWh
Residuals, Best:1851321.01091, Nieuw:2086228.20887
Totale Energie:29953.692 kWh 
Grijze Energy:18198.8639378 kWh
zelf-voorzienend: 39.243336221 %