Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
5.2 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;


class Matrix {

    public:
        double resistivePart;
        double reactivePart;
};



int main() {

    int busBar;
    printf("Input the number of the bus bar: ");
    scanf("%d", &busBar);
    printf("\n\nConsider zero(0) if there is no value\n");


    Matrix impedanceValue[busBar][busBar];
    Matrix admittanceValue[busBar][busBar];
    Matrix admittanceMatrix[busBar][busBar];


    int tempIndex;

    for(int i=1; i<=busBar; i++) {
        for(int j=0; j<=busBar; j++) {

            tempIndex = j;

            if(i==j) {                                                 //A bus can't connect with its own
                continue;
            }
            if(j>i) {                                                  //For upper Diagonal

                printf("\nFor Z%d%d\n", i, tempIndex);
                printf("Resistive Part <space> Reactive Part: ");

                tempIndex--;
            }

            else {
                if(tempIndex!=0) {
                    impedanceValue[i-1][tempIndex].resistivePart = impedanceValue[tempIndex-1][i-1].resistivePart;
                    impedanceValue[i-1][tempIndex].reactivePart = impedanceValue[tempIndex-1][i-1].reactivePart;
                    continue;
                }
                printf("\nFor Z%d%d\n", i, tempIndex);
                printf("Resistive Part <space> Reactive Part: ");
            }

            scanf("%lf %lf", &impedanceValue[i-1][tempIndex].resistivePart, &impedanceValue[i-1][tempIndex].reactivePart);
        }
    }



    printf("\n\n");

    for(int i=0; i<busBar; i++) {
        for(int j=0; j<busBar; j++) {

            if(impedanceValue[i][j].resistivePart==0 && impedanceValue[i][j].reactivePart==0) {
                admittanceValue[i][j].resistivePart = 0;
                admittanceValue[i][j].reactivePart = 0;
                continue;
            }
            admittanceValue[i][j].resistivePart = impedanceValue[i][j].resistivePart / ((impedanceValue[i][j].resistivePart*impedanceValue[i][j].resistivePart)+(impedanceValue[i][j].reactivePart*impedanceValue[i][j].reactivePart));
            admittanceValue[i][j].reactivePart = (-1)*(impedanceValue[i][j].reactivePart / ((impedanceValue[i][j].resistivePart*impedanceValue[i][j].resistivePart)+(impedanceValue[i][j].reactivePart*impedanceValue[i][j].reactivePart)));
        }
    }









    printf("\n\n");

    for(int i=0; i<busBar; i++) {
        for(int j=0; j <busBar; j++) {

            tempIndex = j;

            if(j>i) {
                tempIndex++;

                printf("z%d%d = %0.2lf+j%0.2lf \t", i+1, tempIndex, impedanceValue[i][j].resistivePart, impedanceValue[i][j].reactivePart);
            }
            else {
                printf("z%d%d = %0.2lf+j%0.2lf \t", i+1, tempIndex, impedanceValue[i][j].resistivePart, impedanceValue[i][j].reactivePart);
            }
            if(j+1==busBar) {
                printf("\n");
            }
        }
    }





    printf("\n\n");

    for(int i=0; i<busBar; i++) {
        for(int j=0; j <busBar; j++) {

            if(j<i) {
                continue;
            }
            if(i==j) {                    // Negative Value

                admittanceMatrix[i][j].resistivePart = 0;
                admittanceMatrix[i][j].reactivePart = 0;

                for(int k=0; k<busBar; k++) {
                    admittanceMatrix[i][j].resistivePart = admittanceMatrix[i][j].resistivePart + admittanceValue[i][k].resistivePart;
                    admittanceMatrix[i][j].reactivePart = admittanceMatrix[i][j].reactivePart + admittanceValue[i][k].reactivePart;
                }
            }
            else {
                if (admittanceValue[i][j].resistivePart == 0) {
                    admittanceMatrix[i][j].resistivePart = admittanceMatrix[j][i].resistivePart = admittanceValue[i][j].resistivePart;
                }
                else admittanceMatrix[i][j].resistivePart = admittanceMatrix[j][i].resistivePart = (-1)*admittanceValue[i][j].resistivePart;


                if (admittanceValue[i][j].reactivePart == 0) {
                    admittanceMatrix[i][j].reactivePart = admittanceMatrix[j][i].reactivePart = admittanceValue[i][j].reactivePart;
                }
                else admittanceMatrix[i][j].reactivePart = admittanceMatrix[j][i].reactivePart = (-1)*admittanceValue[i][j].reactivePart;
            }
        }
    }




    printf("\n");

    for(int i=0; i<busBar; i++) {
        for(int j=0; j <busBar; j++) {


            if(i==j) {
                printf("Y%d%d = %0.2lf-j%0.2lf \t", i+1, j+1, admittanceMatrix[i][j].resistivePart, (-1)*admittanceMatrix[i][j].reactivePart);
            }
            else {
                printf("Y%d%d = %0.2lf+j%0.2lf \t", i+1, j+1, admittanceMatrix[i][j].resistivePart, admittanceMatrix[i][j].reactivePart);
            }

            if(j+1==busBar) {
                printf("\n");
            }
        }
    }
}