### EE393 Semester Project Preapared by Eytan Dilmen and Salih Emre Kaynar Electronics Engineering
import numpy as np
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QDialog
from PyQt5.uic import loadUi
import matplotlib.pyplot as plt


class guideneme(QDialog):
    def __init__(self):
        super(guideneme,self).__init__()
        loadUi('guideneme.ui', self)
        self.setWindowTitle('Solution for 3 Generator System')
        
    @pyqtSlot()
    def on_pushButton_clicked(self):
        totalDemand=float(self.lineEdit.text())
        inp=3
        def inputs(num):
            a=np.zeros((num+1,num+1))
            a[0,0]=2*float(self.lineEdit_2.text())
            a[1,1]=2*float(self.lineEdit_3.text())
            a[2,2]=2*float(self.lineEdit_4.text())
            return a
        matrix=inputs(inp)        
        for i in range(0,inp):
            matrix[inp,i]=-1
            matrix[i,inp]=-1
        def const(num):
            b=np.zeros((num+1,1))
            b[0,0]=(-1)*float(self.lineEdit_5.text())
            b[1,0]=(-1)*float(self.lineEdit_6.text())
            b[2,0]=(-1)*float(self.lineEdit_7.text())
            return b
        def genCost(x,y,p): #defining the function to plot 
            a=x*p**2+p*y
            return a
        constants=const(inp)
        constants[inp,0]= (-1)*totalDemand
        ans=np.linalg.solve(matrix,constants)#This line calculates the power values and incremental cost
        cost1=float(self.lineEdit_2.text())*ans[0,0]**2+float(self.lineEdit_5.text())*ans[0,0]
        cost2=float(self.lineEdit_3.text())*ans[1,0]**2+float(self.lineEdit_6.text())*ans[1,0]
        cost3=float(self.lineEdit_4.text())*ans[2,0]**2+float(self.lineEdit_7.text())*ans[2,0]
        self.textBrowser.setText("The power generated by Generator 1 is " +format(ans[0,0],'.2f')+" MW.")
        self.textBrowser.append("The power generated by Generator 2 is " +format(ans[1,0],'.2f')+ "MW.")
        self.textBrowser.append("The power generated by Generator 3 is " +format(ans[2,0],'.2f')+ "MW.")
        self.textBrowser.append("The incremental cost is " +format(ans[3,0],'.2f')+ "$/MWh.")
        self.textBrowser.append("Cost of " +format(ans[0,0],'.2f')+ "MW from Generator 1 is : " + format(cost1,'.2f')+'$')
        self.textBrowser.append("Cost of " +format(ans[1,0],'.2f')+ "MW from Generator 2 is : " + format(cost2,'.2f')+'$')
        self.textBrowser.append("Cost of " +format(ans[2,0],'.2f')+ "MW from Generator 3 is : " + format(cost3,'.2f')+'$')
        xs=range(0,int(self.lineEdit.text())*1000)
        xss=[]
        for i in xs:
            xss.append(i/1000)
        ys=[]
        for j in xss: #This loop finds the y values from the domain x 
            ys.append(genCost(float(self.lineEdit_2.text()),float(self.lineEdit_5.text()),j))
        plt.plot(xss, ys, color='blue')
        ys=[]
        for j in xss: 
            ys.append(genCost(float(self.lineEdit_3.text()),float(self.lineEdit_6.text()),j))
        plt.plot(xss, ys, color='black')
        ys=[]
        for j in xss:
            ys.append(genCost(float(self.lineEdit_4.text()),float(self.lineEdit_7.text()),j))
        plt.plot(xss, ys, color='red')
        plt.xlabel('MW', fontsize=18)
        plt.ylabel('$', fontsize=16)
        plt.legend(['GEN 1','GEN 2','GEN 3'])
        plt.title("FUEL COST CURVE")
app=QApplication(sys.argv)
widget=guideneme()
widget.show()
sys.exit(app.exec_())