%% Bloomfield System - Z Matrix Calculations

%% Clear Workspace
clc
clear all
close all

%% Constants

% 1/0 ACSR parameters
GMR_1_0_ACSR = 0.004463;
r_1_0_ACSR = 1.12;

% 1/0 CU parameters
GMR_1_0_CU = 0.01113;
r_1_0_CU = 0.607;

% Distances of conductors on imaginary plane
d_a = j*29;
d_c = 2.5+j*29;
d_b = 7+j*29;
d_n = 4+j*33;

% Calculate distances between conductors
D_ab = abs(d_a-d_b);
D_bc = abs(d_b-d_c);
D_ca = abs(d_c-d_a);

D_an = abs(d_a-d_n);
D_bn = abs(d_b-d_n);
D_cn = abs(d_c-d_n);

D_a = [0 D_ab D_ca D_an];
D_b = [D_ab 0 D_bc D_bn];
D_c = [D_ca D_bc 0 D_cn];
D_n = [D_an D_bn D_cn 0];

D = [D_a; D_b; D_c; D_n];

%% Determine primitive impedance matrices

% For 3PH 1/0 ACSR
for i=1:4
   for k=1:4
      if i==k
          Z1(i,k) = r_1_0_ACSR+0.09530+j*0.12134*(log(1/GMR_1_0_ACSR)+7.93402);
      else
          Z1(i,k) = 0.09530+j*0.12134*(log(1/D(i,k))+7.93402);
      end
   end
end

% For 3PH 1/0 CU
for i=1:4
   for k=1:4
      if i==k
          Z2(i,k) = r_1_0_CU+0.09530+j*0.12134*(log(1/GMR_1_0_CU)+7.93402);
      else
          Z2(i,k) = 0.09530+j*0.12134*(log(1/D(i,k))+7.93402);
      end
   end
end

% For 1PH 1/0 CU
% Assumes B Phase
for i=1:2
   for k=1:2
      if i==k
          Z3(i,k) = r_1_0_CU+0.09530+j*0.12134*(log(1/GMR_1_0_CU)+7.93402);
      else
          Z3(i,k) = 0.09530+j*0.12134*(log(1/D_bn)+7.93402);
      end
   end
end

%% Partition Z

% 3PH 1/0 ACSR
Z1_ij = Z1(1:3,1:3);
Z1_in = Z1(4,1:3);
Z1_nn = Z1(4,4);
Z1_nj = Z1(1:3,4);

% 3PH 1/0 CU
Z2_ij = Z2(1:3,1:3);
Z2_in = Z2(4,1:3);
Z2_nn = Z2(4,4);
Z2_nj = Z2(1:3,4);

% 1PH 1/0 CU
Z3_ij = Z3(1,1);
Z3_in = Z3(2,1);
Z3_nn = Z3(2,2);
Z3_nj = Z3(1,2);

%% Determine Z_abc

% 3PH 1/0 ACSR
Z1_abc = Z1_ij-(Z1_in*inv(Z1_nn)*Z1_nj);

% 3PH 1/0 CU
Z2_abc = Z2_ij-(Z2_in*inv(Z2_nn)*Z2_nj);

% 1PH 1/0 CU
Z3_abc = Z3_ij-(Z3_in*inv(Z3_nn)*Z3_nj);

%% Determine Z_012

a = exp(j*2*pi/3);
A = [1 1 1; 1 a^2 a; 1 a a^2];

% 3PH 1/0 ACSR
Z1_012 = inv(A)*Z1_abc*A;

% 3PH 1/0 CU
Z2_012 = inv(A)*Z2_abc*A;

% 1PH 1/0 CU
%Z3_012 = inv(A)*Z3_abc*A;

%% Determine t_n

% 3PH 1/0 ACSR
t1_n = -(inv(Z1_nn)*Z1_nj);

% 3PH 1/0 CU
t2_n = -(inv(Z2_nn)*Z2_nj);

% 1PH 1/0 CU
%t3_n = -(inv(Z3_nn)*Z3_nj);

%% Determine R and X Matrices

% R,X Matrices for 3PH 1/0 ACSR
Z1_abcR = real(Z1_abc)
Z1_abcX = imag(Z1_abc)

Z1_012R = real(Z1_012)
Z1_012X = imag(Z1_012)

% R,X Matrices for 3PH 1/0 CU
Z2_abcR = real(Z2_abc)
Z2_abcX = imag(Z2_abc)

Z2_012R = real(Z2_012)
Z2_012X = imag(Z2_012)

% R,X Matrices for 1PH 1/0 CU
Z3_abcR = real(Z3_abc)
Z3_abcX = imag(Z3_abc)

%Z3_012R = real(Z3_012)
%Z3_012X = imag(Z3_012)



%%
% Created by Connie Maluwelmeng on 14 November 2015.