tsounakis

Numerical Analysis 3

Dec 15th, 2021 (edited)
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.31 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. from sympy import Symbol
  4.  
  5. class fuel:
  6.     def __init__(self, name, pricePerTon, thermalForce):
  7.         self.name = name
  8.         self.pricePerTon = pricePerTon
  9.         self.thermalForce = thermalForce
  10.  
  11. class unit:
  12.     def __init__(self, name, powerOutput, hourConsumption, fuel):
  13.         self.name = name
  14.         self.powerOutput = powerOutput
  15.         self.hourConsumption = hourConsumption
  16.         self.error = 0
  17.         self.errorTemp = 0
  18.         self.fuel = fuel
  19.         self.specialFuncCost = None
  20.         self.thermalConsumption = None
  21.  
  22.     def leastSquares(self):
  23.         self.a = np.empty([4, 4])
  24.         self.b = np.empty([4, 1])
  25.         self.coeffs = np.empty([1, 4])
  26.         self.sigmax = [sum(power(self.powerOutput, j)) for j in range(7)]
  27.         self.sigmayx = np.zeros([4, 1])
  28.         for m in range(4):
  29.             for i in range(4):
  30.                 self.sigmayx[m] += self.hourConsumption[i] * pow(self.powerOutput[i], m)
  31.        
  32.  
  33.         self.a[0][0] = 4
  34.         for i in range(4):
  35.             for j in range(4):
  36.                 if i+j==0:
  37.                     self.a[i][j] = 4
  38.                 elif i+j==1:
  39.                     self.a[i][j] = self.sigmax[1]
  40.                 elif i+j==2:
  41.                     self.a[i][j] = self.sigmax[2]
  42.                 elif i+j==3:
  43.                     self.a[i][j] = self.sigmax[3]
  44.                 elif i+j==4:
  45.                     self.a[i][j] = self.sigmax[4]
  46.                 elif i+j==5:
  47.                     self.a[i][j] = self.sigmax[5]
  48.                 elif i+j==6:
  49.                     self.a[i][j] = self.sigmax[6]
  50.             self.b[i] = self.sigmayx[i]
  51.         self.coeffs = np.linalg.solve(self.a, self.b)
  52.    
  53.     def printError(self):
  54.        
  55.         for i in range(4):
  56.             print("i = {} : S_temp = {}".format(i, self.errorTemp))
  57.             for m in range(4):
  58.                 self.errorTemp -= (self.coeffs[m] * pow(self.powerOutput[i], m))
  59.                 print("\t\tm = {} : S_temp = {}".format(m, self.errorTemp))
  60.             self.errorTemp += self.hourConsumption[i]
  61.             self.errorTemp **= 2
  62.             self.error += self.errorTemp
  63.             self.errorTemp = 0
  64.        
  65.         print("Το σφάλμα ισούται με: {}".format(self.error))
  66.  
  67. def power(list, j):
  68.     for i in list:
  69.         yield pow(i, j)
  70.  
  71. def plotThermalConsumption(unit):
  72.     H = []
  73.     P = np.linspace(100,320)
  74.     H.append(np.poly1d([float(unit[0].coeffs[3]), float(unit[0].coeffs[2]), float(unit[0].coeffs[1]), float(unit[0].coeffs[0])], variable = "P"))
  75.     H.append(np.poly1d([float(unit[1].coeffs[3]), float(unit[1].coeffs[2]), float(unit[1].coeffs[1]), float(unit[1].coeffs[0])], variable = "P"))
  76.    
  77.     unit[0].thermalConsumption = H[0]
  78.     unit[1].thermalConsumption = H[1]
  79.  
  80.     plt.figure(1)
  81.     plt.title("Kατανάλωσης θερμότητας H(P) ως προς την ισχύ εξόδου Ρ")
  82.     plt.xlabel("Ηλεκτρική Ισχύς Εξόδου (MW)")
  83.     plt.ylabel("Ωριαία Κατανάλωση Θερμότητας (GCal/h)")
  84.     for h_theta in H:
  85.         plt.plot(P, h_theta(P))
  86.     for u in unit:
  87.         plt.plot(u.powerOutput, u.hourConsumption, 'o', color = "blue" if u.name == "Lavrio" else "orange", label = "Η/Σ Λαυρίου" if u.name == "Lavrio" else "Η/Σ Μεγαλόπολης")
  88.     plt.legend(loc="upper left")
  89.     plt.grid()
  90.  
  91. def plotHourlyConsumption(units):
  92.     P = np.linspace(100,320)
  93.     plt.figure(2)
  94.     plt.title("Ωριαίo κόστος λειτουργίας F ως προς την ισχύ εξόδου P")
  95.     plt.xlabel("Ηλεκτρική Ισχύς Εξόδου (MW)")
  96.     plt.ylabel("Ωριαία Κόστος Λειτουργίας (€/h)")
  97.     plt.grid()
  98.     for unit in units:
  99.         unit.hourlyConsumption = unit.thermalConsumption * unit.fuel.pricePerTon / unit.fuel.thermalForce
  100.         plt.plot(P, unit.hourlyConsumption(P), label = "Η/Σ Λαυρίου" if unit.name == "Lavrio" else "Η/Σ Μεγαλόπολης")
  101.     plt.legend(loc="upper left")
  102.  
  103. def plotFuncCost(units):
  104.     P = np.arange(100,320)
  105.     plt.figure(3)
  106.     plt.title("Ειδικό κόστος λειτουργίας F/ P ως προς την ισχύ εξόδου P")
  107.     plt.xlabel("Ηλεκτρική Ισχύς Εξόδου (MW)")
  108.     plt.ylabel("Ειδικό Κόστος Λειτουργίας (€/MWh)")
  109.     plt.grid()
  110.     for unit in units:
  111.         print(unit.hourlyConsumption)
  112.         div = np.polydiv(unit.hourlyConsumption, [1.0])
  113.         unit.specialFuncCost = div[0][0]*P**2 + div[0][1]*P + div[0][2] + div[0][3]*(1/P)
  114.         plt.plot(P, unit.specialFuncCost, label = "Η/Σ Λαυρίου" if unit.name == "Lavrio" else "Η/Σ Μεγαλόπολης")
  115.     plt.legend(loc="upper left")
  116.  
  117. def main():
  118.     Lignite = fuel("Lignite", 16.5, 1000)
  119.     FuelOil = fuel("Fuel Oil", 500, 9600)
  120.  
  121.     Lavrio = unit("Lavrio", [120, 180, 270, 300], [246, 351, 514, 572], FuelOil)
  122.     Megalopolis = unit("Megalopolis", [150, 215, 285, 300], [307, 427, 563, 594], Lignite)
  123.  
  124.  
  125.     Lavrio.leastSquares()
  126.     Megalopolis.leastSquares()
  127.    
  128.     plotThermalConsumption([Lavrio, Megalopolis])
  129.     plotHourlyConsumption([Lavrio, Megalopolis])
  130.     plotFuncCost([Lavrio, Megalopolis])
  131.     plt.show()
  132.  
  133. if __name__ == "__main__":
  134.     main()
Add Comment
Please, Sign In to add comment