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 = ""
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 = ""
Qdal = 6862
Qnorm = 6797
M3gas = 3336/2
Qvw = 8574.5 # kWh
Qgas = M3gas*9.769 # COP = 2; heatpump!
print Qgas+Qdal+Qnorm
with open("{}KNMI_20180101.txt".format(FileDir)) as File:
Input = File.read().split("\n")
Input = Input[14:]
Input = Input[:-2]
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]
PVpanels = 60
h = 99.5
b = 165
eta = 0.23
PVEnergy = PVpanels*h*b*(2.77777778*10**-7)*eta*TotalEnergy*0.62
print PVEnergy
## 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
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)]
##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
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