function Schematic = InverterCircuit(p,t,Subdomains,Circuit,l,nper,~,~)
% 3-phase bridge inverter circuit
% 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;

Roff=10^8;
Ron=10^-6;

[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

Schematic = CircuitCreateSchematic();
Branch = CircuitCreateBranch(0,1);
Branch = CircuitAddE(Branch,'Vdc','CircuitControl.vdc');
Schematic = CircuitAddBranch(Schematic,Branch);

Branch = CircuitCreateBranch(2,1);
Branch = CircuitAddSwitch(Branch,'switch_a1','CircuitControl.switch_a1',Roff,Ron);
Schematic = CircuitAddBranch(Schematic,Branch);
Branch = CircuitCreateBranch(2,1);
Branch = CircuitAddDiode(Branch,'diode_a1',Roff,Ron,1);
Schematic = CircuitAddBranch(Schematic,Branch);

Branch = CircuitCreateBranch(3,1);
Branch = CircuitAddSwitch(Branch,'switch_b1','CircuitControl.switch_b1',Roff,Ron);
Schematic = CircuitAddBranch(Schematic,Branch);
Branch = CircuitCreateBranch(3,1);
Branch = CircuitAddDiode(Branch,'diode_b1',Roff,Ron,1);
Schematic = CircuitAddBranch(Schematic,Branch);

Branch = CircuitCreateBranch(4,1);
Branch = CircuitAddSwitch(Branch,'switch_c1','CircuitControl.switch_c1',Roff,Ron);
Schematic = CircuitAddBranch(Schematic,Branch);
Branch = CircuitCreateBranch(4,1);
Branch = CircuitAddDiode(Branch,'diode_c1',Roff,Ron,1);
Schematic = CircuitAddBranch(Schematic,Branch);

Branch = CircuitCreateBranch(0,2);
Branch = CircuitAddSwitch(Branch,'switch_a2','CircuitControl.switch_a2',Roff,Ron);
Schematic = CircuitAddBranch(Schematic,Branch);
Branch = CircuitCreateBranch(0,2);
Branch = CircuitAddDiode(Branch,'diode_a2',Roff,Ron,1);
Schematic = CircuitAddBranch(Schematic,Branch);

Branch = CircuitCreateBranch(0,3);
Branch = CircuitAddSwitch(Branch,'switch_b2','CircuitControl.switch_b2',Roff,Ron);
Schematic = CircuitAddBranch(Schematic,Branch);
Branch = CircuitCreateBranch(0,3);
Branch = CircuitAddDiode(Branch,'diode_b2',Roff,Ron,1);
Schematic = CircuitAddBranch(Schematic,Branch);

Branch = CircuitCreateBranch(0,4);
Branch = CircuitAddSwitch(Branch,'switch_c2','CircuitControl.switch_c2',Roff,Ron);
Schematic = CircuitAddBranch(Schematic,Branch);
Branch = CircuitCreateBranch(0,4);
Branch = CircuitAddDiode(Branch,'diode_c2',Roff,Ron,1);
Schematic = CircuitAddBranch(Schematic,Branch);

if strcmp(Circuit.statorconnection,'StarConnection')
    if Npp==1
        Branch = CircuitCreateBranch(2,5);
        Branch = CircuitAddCoil(Branch,'coil_a','a',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(3,5);
        Branch = CircuitAddCoil(Branch,'coil_b','b',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
        
        Branch = CircuitCreateBranch(4,5);
        Branch = CircuitAddCoil(Branch,'coil_c','c',Subdomains,Rs,Lsew,1,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
    elseif Npp>1
        for i=1:Npp
            Branch = CircuitCreateBranch(2,4+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(3,4+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(4,4+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(Circuit.statorconnection,'DeltaConnection')
    for i=1:Npp
        Branch = CircuitCreateBranch(2,3);
        Branch = CircuitAddCoil(Branch,['coil_a' num2str(i)],'a',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);

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

        Branch = CircuitCreateBranch(4,2);
        Branch = CircuitAddCoil(Branch,['coil_c' num2str(i)],'c',Subdomains,Rs,Lsew,i,Npp,1,p,t,l,nper);
        Schematic = CircuitAddBranch(Schematic,Branch);
    end
else
    error('Undefined stator winding connection');
end




