function Schematic = StarConnection(p,t,Subdomains,Circuit,l,nper,PowerSupplyType,AC_RMSSupplyValue)
% Construct electrical circuit with star connected stator winding
% Copyright 2008-2024, Vladimir Kuptsov

Npp=Circuit.Npp;      % number of parallel paths
Rs=Circuit.Rs;
Rs = Npp*Rs;          % parallel path phase resistance
Lsew=Circuit.Lsew;
Lsew = Npp*Lsew;      % parallel path end winding inductance
layout = Circuit.layout;
if exist('AC_RMSSupplyValue','var')
    Em=sqrt(2)*AC_RMSSupplyValue; Im=sqrt(2)*AC_RMSSupplyValue;
else
    Em=[]; Im=[];
end

[ok_layout ok_Npp] = ValidateLayout(layout,Npp);
if ~ok_layout
    disp('ValidateLayout: desequencing of parallel path numbering');
    error(' ');
end
if ~ok_Npp
    disp('ValidateLayout: number of parallel paths is not consistent with winding layout table');
    error(' ');
end

if isempty(PowerSupplyType)
    PowerSupplyType='3-phase voltage source';   % voltage source by default
end
Schematic = CircuitCreateSchematic();
if strcmp(PowerSupplyType,'3-phase voltage source')
    if Npp==1
        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddE(Branch,'Ea','CircuitControl.va',Em,0);
        Branch = CircuitAddCoil(Branch,'coil_a','a',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);

        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddE(Branch,'Eb','CircuitControl.vb');
        Branch = CircuitAddCoil(Branch,'coil_b','b',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);

        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddE(Branch,'Ec','CircuitControl.vc');
        Branch = CircuitAddCoil(Branch,'coil_c','c',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch); 
    elseif Npp>1
        Branch = CircuitCreateBranch(0,1);
        Branch = CircuitAddE(Branch,'Ea','CircuitControl.va');
        Schematic = CircuitAddBranch(Schematic,Branch);

        Branch = CircuitCreateBranch(0,2);
        Branch = CircuitAddE(Branch,'Eb','CircuitControl.vb');
        Schematic = CircuitAddBranch(Schematic,Branch);

        Branch = CircuitCreateBranch(0,3);
        Branch = CircuitAddE(Branch,'Ec','CircuitControl.vc');
        Schematic = CircuitAddBranch(Schematic,Branch);

        for i=1:Npp
            Branch = CircuitCreateBranch(1,3+i);
            Branch = CircuitAddCoil(Branch,['coil_a' num2str(i)],'a',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
            Schematic = CircuitAddBranch(Schematic,Branch);

            Branch = CircuitCreateBranch(2,3+i);
            Branch = CircuitAddCoil(Branch,['coil_b' num2str(i)],'b',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
            Schematic = CircuitAddBranch(Schematic,Branch);

            Branch = CircuitCreateBranch(3,3+i);
            Branch = CircuitAddCoil(Branch,['coil_c' num2str(i)],'c',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
            Schematic = CircuitAddBranch(Schematic,Branch);
        end
    end
elseif strcmp(PowerSupplyType,'3-phase current source')
    if Npp==1
        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddJ(Branch,'Ja','CircuitControl.ia');
        Branch = CircuitAddCoil(Branch,'coil_a','a',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddJ(Branch,'Jb','CircuitControl.ib');
        Branch = CircuitAddCoil(Branch,'coil_b','b',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddJ(Branch,'Jc','CircuitControl.ic');
        Branch = CircuitAddCoil(Branch,'coil_c','c',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(1,0);
        Branch = CircuitAddR(Branch,'Rn',10^7);
        Schematic = CircuitAddBranch(Schematic,Branch);
    elseif Npp>1
        Branch = CircuitCreateBranch(0,1);
        Branch = CircuitAddJ(Branch,'Ja','CircuitControl.ia');
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(0,2);
        Branch = CircuitAddJ(Branch,'Jb','CircuitControl.ib');
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(0,3);
        Branch = CircuitAddJ(Branch,'Jc','CircuitControl.ic');
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        for i=1:Npp
            Branch = CircuitCreateBranch(1,3+i);
            Branch = CircuitAddCoil(Branch,['coil_a' num2str(i)],'a',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
            Schematic = CircuitAddBranch(Schematic,Branch);
            
            Branch = CircuitCreateBranch(2,3+i);
            Branch = CircuitAddCoil(Branch,['coil_b' num2str(i)],'b',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
            Schematic = CircuitAddBranch(Schematic,Branch);
            
            Branch = CircuitCreateBranch(3,3+i);
            Branch = CircuitAddCoil(Branch,['coil_c' num2str(i)],'c',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
            Schematic = CircuitAddBranch(Schematic,Branch);
            
            Branch = CircuitCreateBranch(0,3+i);
            Branch = CircuitAddR(Branch,['Rn' num2str(i)],10^7);
            Schematic = CircuitAddBranch(Schematic,Branch);
        end
    end
else
    error(' ');
end


