This forum is specifically for questions about using MATPOWER. This question does not appear to be about MATPOWER (let alone problems with MATPOWER and MATLAB code generation ;-).
I suggest you try a different forum for general help with your own MATLAB code. Best regards, Ray On Apr 14, 2020, at 8:33 AM, Teshome Hambissa <[email protected]<mailto:[email protected]>> wrote: what my problem in the following matlab function.can you correct me please? % Start of file (run_LF.m) clear; format short; tole=1e-9; deg=180/pi; rad=1/deg ; A=1.5; % Base values Sbase=100; Ubase=220; Zb=Ubase^2/Sbase; % Bus data % Number of buses nbus=6; %Bus 1, slack bus U1=220/Ubase; theta1=-1.5*rad; PL1=50/Sbase; QL1=5/Sbase; %Bus 2, PQ-bus PG2=100/Sbase; QG2=10/Sbase; PL2=(200+0.1*A)/Sbase; QL2=20/Sbase; %Bus 3, PQ-bus PG3=150/Sbase; QG3=15/Sbase; PL3=300/Sbase; QL3=30/Sbase; %Bus 4, PQ-bus PG4=0/Sbase; QG4=0/Sbase; PL4=0/Sbase; QL4=0/Sbase; Qc=(25-0.2*A)/Sbase; %Bus 5, PQ-bus PG5=200/Sbase; QG5=20/Sbase; PL5=350/Sbase; QL5=(40-0.1*A)/Sbase; %Bus 6, PQ-bus PG6=200/Sbase; QG6=10/Sbase; PL6=100/Sbase; QL6=20/Sbase; % Line data r=0.0529;x=0.529;b=3.3081*10^-6; l12=150;l13=170;l23=180;l34=100;l45=200;l56=100; r12=r*l12;r13=r*l13;r23=r*l23;r34=r*l34;r45=r*l45;r56=r*l56; x12=x*l12;x13=x*l13;x23=x*l23;x34=x*l34;x45=x*l45;x56=x*l56; z12=(r12+x12*i)/Zb;b12=b*l12*Zb*j/2; z13=(r13+x13*i)/Zb;b13=b*l13*Zb*j/2; z23=(r23+x23*i)/Zb;b23=b*l23*Zb*j/2; z34=(r34+x34*i)/Zb;b34=b*l34*Zb*j/2; z45=(r45+x45*i)/Zb;b45=b*l45*Zb*j/2; z56=(r56+x56*i)/Zb;b56=b*l56*Zb*j/2; b21=b12;b31=b13;b32=b23;b43=b34;b54=b45;b65=b56; % Shunt Qshunt=Qc/Sbase; Zshunt=Ubase^2/(Qshunt*j); Zbshunt=Zshunt/Zb; yshunt=i*Qshunt/1^2; % YBUS matrix y11=1/z12+1/z13+b12+b13; y12=-1/z12; y13=-1/z13; y14=0; y15=0; y16=0; y21=-1/z12; y22=1/z12+1/z23+b12+b23; y23=-1/z23; y24=0; y25=0; y26=0; y31=-1/z13; y32=-1/z23; y33=1/z13+1/z23+1/z34+b13+b23+b34; y34=-1/z34; y35=0; y36=0; y41=0; y42=0; y43=-1/z34; y44=1/z34+yshunt+1/z45+b34+b45; y45=-1/z45; y46=0; y51=0; y52=0; y53=0; y54=-1/z45; y55=1/z45+1/z56+b45+b56; y56=-1/z56; y61=0; y62=0; y63=0; y64=0; y65=-1/z56; y66=1/z56+b56; YBUS=[ y11 y12 y13 y14 y15 y16; y21 y22 y23 y24 y25 y26; y31 y32 y33 y34 y35 y36; y41 y42 y43 y44 y45 y46; y51 y52 y53 y54 y55 y56; y61 y62 y63 y64 y65 y66];% Define YBUS G=real(YBUS); B=imag(YBUS); " OutmailID: 124547456, List: 'matpower-l', MemberID: 82861091 SCRIPT: "% PGD and QGD for PU- and PQ-buses" TCL MERGE ERROR ( 04/14/2020 13:15:31 ): "invalid command name "%" PGD2=PG2-PL2; % for bus 2 (PQ-bus) PGD3=PG3-PL3; % for bus 3 (PQ-bus) PGD4=PG4-PL4; % for bus 4 (PQ-bus) PGD5=PG5-PL5; PGD6=PG6-PL6; PGD=[PGD2 ; PGD3 ; PGD4 ; PGD5 ; PGD6]; QGD2=QG2-QL2; QGD3=QG3-QL3; QGD4=QG4-QL4; QGD5=QG5-QL5; QGD6=QG6-QL6; QGD=[QGD2 ; QGD3 ; QGD4 ; QGD5 ; QGD6]; % % Use fsolve function in MATLAB to run load flow % % Unknown variables [theta2 theta3 theta4 U2 U3 U4]’; % Define the initial values of the unknown variables X0=[0 0 0 0 0 1 1 1 1 1]'; % Flat initial values s_z=size(X0); nx=s_z(1,1); % number of unknown variables % The function below is used for fsolve (type "help fsolve" in MATLAB) options_solve = optimset('Display','off','TolX',tole,'TolFun',tole); % Parameters used for fsolve PAR=[nx ; nbus ; U1 ; theta1];% U1 and theta1 are known (slack bus). [X_X,FVAL,EXITFLAG,OUTPUT]=fsolve('EG2100_solve_LF',X0,options_solve,G,B,PGD,QGD,PAR); if EXITFLAG~=1, disp('No solution'), EXITFLAG=EXITFLAG, return end, % Solved variables X_X=[theta2 theta3 theta4 U2 U3 U4]’; ANG=[theta1 X_X(1) X_X(2) X_X(3) X_X(4) X_X(5)];% Voltage phase angles VOLT=[U1 X_X(6) X_X(7) X_X(8) X_X(9) X_X(10)]; % Voltage magnitudes ANG_deg=ANG*deg;% in degrees VOLT_kV=VOLT*Ubase; % in kV " OutmailID: 124547456, List: 'matpower-l', MemberID: 82861091 SCRIPT: "% The generated active and reactive power at slack bus and PU-buses" TCL MERGE ERROR ( 04/14/2020 13:15:31 ): "invalid command name "%"" OutmailID: 124547456, List: 'matpower-l', MemberID: 82861091 SCRIPT: "% g=-G;b=-B; % At slack bus, bus 1 P12=g(1,2)*VOLT(1)^2-... VOLT(1)*VOLT(2)*(g(1,2)*cos(ANG(1)-ANG(2))+b(1,2)*sin(ANG(1)-ANG(2))); P13=g(1,3)*VOLT(1)^2-... VOLT(1)*VOLT(3)*(g(1,3)*cos(ANG(1)-ANG(3))+b(1,3)*sin(ANG(1)-ANG(3))); Q12=(-b12-b(1,2))*VOLT(1)^2-... VOLT(1)*VOLT(2)*(g(1,2)*sin(ANG(1)-ANG(2))-b(1,2)*cos(ANG(1)-ANG(2))); Q13=(-b13-b(1,3))*VOLT(1)^2-... VOLT(1)*VOLT(3)*(g(1,3)*sin(ANG(1)-ANG(3))-b(1,3)*cos(ANG(1)-ANG(3))); PG1=P12+P13+PL1; QG1=Q12+Q13+QL1; PG1_MW=PG1*Sbase; QG1_MW=QG1*Sbase;" TCL MERGE ERROR ( 04/14/2020 13:15:31 ): "invalid command name "%"" OutmailID: 124547456, List: 'matpower-l', MemberID: 82861091 SCRIPT: "% % Losses" TCL MERGE ERROR ( 04/14/2020 13:15:31 ): "invalid command name "%"" OutmailID: 124547456, List: 'matpower-l', MemberID: 82861091 SCRIPT: "% P34=g(3,4)*VOLT(3)^2-... VOLT(3)*VOLT(4)*(g(3,4)*cos(ANG(3)-ANG(4))+b(3,4)*sin(ANG(3)-ANG(4))); PLoss_tot=(PG1+PG2+PG3+PG4+PG5+PG6)-(PL1+PL2+PL3+PL4+PL5+PL6); PLoss_tot_MW=PLoss_tot*Sbase PLoss_Sys1=(PG1+PG2+PG3)-(PL1+PL2+PL3+P34);% Find the power losses in System 1 (pu) PLoss_Sys1_MW=PLoss_Sys1*Sbase PLoss_Sys2=(PG4+PG5+PG6+P34)-(PL4+PL5+PL6);% Find the power losses in System 2 (MW) PLoss_Sys2_MW=PLoss_Sys2*Sbase % End of file (run_LF.m) The function is in the second file named EG2100_solve_LF % Start of file (solve_lf.m) % This function solves g(x)=0 for x. function [g_x]=EG2100_solve_lf(X,G,B,PGD,QGD,PAR); nx=PAR(1); nbus=PAR(2); U1=PAR(3); theta1=PAR(4); PGD2=PGD(1); PGD3=PGD(2); PGD4=PGD(3); PGD5=PGD(4); PGD6=PGD(5); QGD2=QGD(1); QGD3=QGD(2); QGD4=QGD(3); QGD5=QGD(4); QGD6=QGD(5); theta2=X(1); theta3=X(2); theta4=X(3); theta5=X(4); theta6=X(5); U2=X(6); U3=X(7); U4=X(8); U5=X(9); U6=X(10); ANG=[theta1 theta2 theta3 theta4 theta5 theta6]' ; VOLT=[U1 U2 U3 U4 U5 U6]'; % We have nx unknown variables, therefore the % size of g(x) is nx by 1. g_x=zeros(nx,1); %Based on equation (7.43), find Pk and Qk P2=VOLT(2)*(VOLT(1)*(G(2,1)*cos(ANG(2)-ANG(1))+B(2,1)*sin(ANG(2)-ANG(1)))... +VOLT(3)*(G(2,3)*cos(ANG(2)-ANG(3))+B(2,3)*sin(ANG(2)-ANG(3)))... +VOLT(2)*(G(2,2)*cos(ANG(2)-ANG(2))+B(2,2)*sin(ANG(2)-ANG(2)))); % +U2*U4*(G(2,4)*cos(theta2-theta4)+B(2,4)*sin(theta2-theta4))... % +U2*U5*(G(2,5)*cos(theta2-theta5)+B(2,5)*sin(theta2-theta5))... % +U2*U6*(G(2,6)*cos(theta2-theta6)+B(2,6)*sin(theta2-theta6)); P3=VOLT(3)*(VOLT(2)*(G(3,2)*cos(ANG(3)-ANG(2))+B(3,2)*sin(ANG(3)-ANG(2)))... +VOLT(4)*(G(3,4)*cos(ANG(3)-ANG(4))+B(3,4)*sin(ANG(3)-ANG(4)))... +VOLT(3)*(G(3,3)*cos(ANG(3)-ANG(3))+B(3,3)*sin(ANG(3)-ANG(3)))... +VOLT(1)*(G(3,1)*cos(ANG(3)-ANG(1))+B(3,1)*sin(ANG(3)-ANG(1)))); % +U3*U6*(G(3,6)*cos(theta3-theta6)+B(3,6)*sin(theta3-theta6))...; P4=VOLT(4)*(VOLT(3)*(G(4,3)*cos(ANG(4)-ANG(3))+B(4,3)*sin(ANG(4)-ANG(3)))... +VOLT(5)*(G(4,5)*cos(ANG(4)-ANG(5))+B(4,5)*sin(ANG(4)-ANG(5)))... +VOLT(4)*(G(4,4)*cos(ANG(4)-ANG(4))+B(4,4)*sin(ANG(4)-ANG(4)))); P5=VOLT(5)*(VOLT(4)*(G(5,4)*cos(ANG(5)-ANG(4))+B(5,4)*sin(ANG(5)-ANG(4)))... +VOLT(6)*(G(5,6)*cos(ANG(5)-ANG(6))+B(5,6)*sin(ANG(5)-ANG(6)))... +VOLT(5)*(G(5,5)*cos(ANG(5)-ANG(3))+B(5,5)*sin(ANG(5)-ANG(5)))); P6=VOLT(6)*(VOLT(5)*(G(6,5)*cos(ANG(6)-ANG(5))+B(6,5)*sin(ANG(6)-ANG(5)))... +VOLT(6)*(G(6,6)*cos(ANG(2)-ANG(3))+B(2,3)*sin(ANG(2)-ANG(3)))); %Qk Q2=VOLT(2)*(VOLT(1)*(G(2,1)*sin(ANG(2)-ANG(1))-B(2,1)*cos(ANG(2)-ANG(1)))... +VOLT(3)*(G(2,3)*sin(ANG(2)-ANG(3))-B(2,3)*cos(ANG(2)-ANG(3)))... +VOLT(2)*(G(2,2)*sin(ANG(2)-ANG(2))-B(2,2)*cos(ANG(2)-ANG(2)))); Q3=VOLT(3)*(VOLT(2)*(G(3,2)*sin(ANG(3)-ANG(2))-B(3,2)*cos(ANG(3)-ANG(2)))... +VOLT(4)*(G(3,4)*sin(ANG(3)-ANG(4))-B(3,4)*cos(ANG(3)-ANG(4)))... +VOLT(3)*(G(3,3)*sin(ANG(3)-ANG(3))-B(3,3)*cos(ANG(3)-ANG(3)))); Q4=VOLT(4)*(VOLT(3)*(G(4,3)*sin(ANG(4)-ANG(3))-B(4,3)*cos(ANG(4)-ANG(3)))... +VOLT(5)*(G(4,5)*sin(ANG(4)-ANG(5))-B(4,5)*cos(ANG(4)-ANG(5)))... +VOLT(4)*(G(4,4)*sin(ANG(4)-ANG(4))-B(4,4)*cos(ANG(4)-ANG(4)))); Q5=VOLT(5)*(VOLT(4)*(G(5,4)*sin(ANG(5)-ANG(4))-B(5,4)*cos(ANG(5)-ANG(4)))... +VOLT(6)*(G(5,6)*sin(ANG(5)-ANG(6))-B(5,6)*cos(ANG(5)-ANG(6)))... +VOLT(5)*(G(5,5)*sin(ANG(5)-ANG(5))-B(5,5)*cos(ANG(5)-ANG(5)))); Q6=VOLT(6)*(VOLT(5)*(G(6,5)*sin(ANG(6)-ANG(5))-B(6,5)*cos(ANG(6)-ANG(5)))... +VOLT(6)*(G(2,3)*sin(ANG(6)-ANG(6))-B(6,6)*cos(ANG(6)-ANG(6)))); % Active power mismatch (PU- and PQ-buses) % Bus 2 g_x(1)=P2-PGD2; % Bus 3 g_x(2)=P3-PGD3; % Bus 4 g_x(3)=P4-PGD4; % Bus 5 g_x(4)=P5-PGD5; % Bus 6 g_x(5)=P6-PGD6; % Reactive power mismatch (PQ-buses) % Bus 2 g_x(6)=Q2-QGD2; % Bus 3 g_x(7)=Q3-QGD3; % Bus 4 g_x(8)=Q4-QGD4; % Bus 5 g_x(9)=Q5-QGD5; % Bus 6 g_x(10)=Q6-QGD6; % End of file (solve_lf.m) Teshome Hambissa Lecturer Addis Ababa Universiy, AAiT, SECE Addis Ababa, Ethiopia On Tue, Apr 14, 2020 at 4:31 PM Teshome Hambissa <[email protected]<mailto:[email protected]>> wrote: Dear member, I need run power flow calculation for three generator and four bus system.can you give me the way i understand the matlab code for this problem kind regards, Teshome Hambissa Lecturer Addis Ababa Universiy, AAiT, SECE Addis Ababa, Ethiopia On Mon, Apr 13, 2020 at 9:58 PM Ray Daniel Zimmerman <[email protected]<mailto:[email protected]>> wrote: If I understand correctly, persistent should work if it appears at the top-level within the function (as opposed to inside if-then blocks like this particular case). So I think you should be able to fix this one by moving that line to the first line in the function. I don’t see any other persistent calls that should require changing. That is, I believe they are all already at the top level of their function. As far as num2cell(), my first idea is to try replacing all calls with a call to a new function, say mp_num2cell() that simply creates a cell array of the same dimensions as the input and copies the contents element by element, something like … function c = mp_num2cell(num) c = cell(size(num)); for k = 1:length(num(:)) c{k} = num(k); end Let me know if that works and if anything else pops up. Ray On Apr 13, 2020, at 9:58 AM, Lode De Herdt <[email protected]<mailto:[email protected]>> wrote: Hi Ray, Thank you for the reply. I've spent a little time trying to remove the things that are unsupported from the MATPOWER code but it looks like I'm opening a Pandora's box here; each error I solve just leads to the next errors so it's difficult to solve without good knowledge of MATLAB and the inner workings of MATPOWER. The first error was: PERSISTENT declarations may only appear at the top-level. Function 'moption.m' (#57.41253.41263), line 697, column 9: "persistent" So I replaced persistent nsc_opt with nsc_opt = []; and did the same for other places where 'persistent' was used as was suggested in message 05192<https://www.mail-archive.com/[email protected]/msg05192.html> of this forum, but that then leads to this: This assignment writes a 'struct' value into a 'double' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches. Function 'mpoption.m' (#34.71098.71101), line 1531, column 5: "opt" One of the other errors indicates that the num2cell function is not supported for code generation, but who knows what else will appear once that one is solved. Let me know if you or anyone else has any other ideas, or experience with MATPOWER and code generation. Best regards, Lode On Mon, Apr 13, 2020 at 4:36 PM Ray Daniel Zimmerman <[email protected]<mailto:[email protected]>> wrote: I can’t think of another option. I’ve not personally worked with the C code generation in MATLAB, so I’m not familiar with the requirements. Do you know what specifically is used by MATPOWER that is not supported? If there are easy ways to avoid those things, it would certainly make sense to do so. Ray On Apr 13, 2020, at 7:53 AM, Lode De Herdt <[email protected]<mailto:[email protected]>> wrote: Dear Matpower users, I am trying to run a simulation of a distribution grid on an OPAL-RT real-time digital simulator, using Matpower. The simulator requires a Simulink model, which it converts to C code in order to run the model on its hardware. Using the MATLAB Function block in Simulink, I can integrate a few lines of Matlab code into the model that run a power flow with Matpower. Unfortunately it seems that not all of the code used for Matpower is supported for C code generation, so the Simulink model cannot run. Other than manually re-writing the Matpower code to avoid everything that's unsupported for code generation, is there any easier way of running Matpower in Simulink? It must be compatible with code generation so using an extrinsic coder or something similar is unfortunately not an option. Thank you for your help! Best regards, Lode --_000_B48DD3D8FDE84BF7ACC8030D323619FDcornelledu_ Content-Type: text/html; charset="utf-8" Content-ID: <[email protected]> Content-Transfer-Encoding: base64 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""> <div class="">This forum is specifically for questions about using MATPOWER. This question does not appear to be about MATPOWER (let alone problems with MATPOWER and MATLAB code generation ;-).</div> <div class=""><br class=""> </div> <div class="">I suggest you try a different forum for general help with your own MATLAB code.</div> <div class=""><br class=""> </div> <div class="">Best regards,</div> <div class=""><br class=""> </div> <div class=""> Ray</div> <div class=""><br class=""> </div> <div><br class=""> <blockquote type="cite" class=""> <div class="">On Apr 14, 2020, at 8:33 AM, Teshome Hambissa <<a href="mailto:[email protected]" class="">[email protected]</a>> wrote:</div> <br class="Apple-interchange-newline"> <div class=""> <div dir="ltr" class="">what my problem in the following matlab function.can you correct me please? <div class=""><br class=""> </div> <div class="">% Start of file (run_LF.m)<br class=""> clear;<br class=""> format short;<br class=""> tole=1e-9; deg=180/pi; rad=1/deg ;<br class=""> A=1.5;<br class=""> % Base values<br class=""> Sbase=100; Ubase=220; Zb=Ubase^2/Sbase;<br class=""> % Bus data<br class=""> % Number of buses<br class=""> nbus=6;<br class=""> %Bus 1, slack bus<br class=""> U1=220/Ubase; theta1=-1.5*rad; PL1=50/Sbase; QL1=5/Sbase;<br class=""> %Bus 2, PQ-bus<br class=""> PG2=100/Sbase; QG2=10/Sbase; PL2=(200+0.1*A)/Sbase; QL2=20/Sbase;<br class=""> %Bus 3, PQ-bus<br class=""> PG3=150/Sbase; QG3=15/Sbase; PL3=300/Sbase; QL3=30/Sbase;<br class=""> %Bus 4, PQ-bus<br class=""> PG4=0/Sbase; QG4=0/Sbase; PL4=0/Sbase; QL4=0/Sbase; Qc=(25-0.2*A)/Sbase;<br class=""> %Bus 5, PQ-bus<br class=""> PG5=200/Sbase; QG5=20/Sbase; PL5=350/Sbase; QL5=(40-0.1*A)/Sbase;<br class=""> %Bus 6, PQ-bus<br class=""> PG6=200/Sbase; QG6=10/Sbase; PL6=100/Sbase; QL6=20/Sbase;<br class=""> % Line data<br class=""> r=0.0529;x=0.529;b=3.3081*10^-6;<br class=""> l12=150;l13=170;l23=180;l34=100;l45=200;l56=100;<br class=""> r12=r*l12;r13=r*l13;r23=r*l23;r34=r*l34;r45=r*l45;r56=r*l56;<br class=""> x12=x*l12;x13=x*l13;x23=x*l23;x34=x*l34;x45=x*l45;x56=x*l56;<br class=""> z12=(r12+x12*i)/Zb;b12=b*l12*Zb*j/2;<br class=""> z13=(r13+x13*i)/Zb;b13=b*l13*Zb*j/2;<br class=""> z23=(r23+x23*i)/Zb;b23=b*l23*Zb*j/2;<br class=""> z34=(r34+x34*i)/Zb;b34=b*l34*Zb*j/2;<br class=""> z45=(r45+x45*i)/Zb;b45=b*l45*Zb*j/2;<br class=""> z56=(r56+x56*i)/Zb;b56=b*l56*Zb*j/2;<br class=""> b21=b12;b31=b13;b32=b23;b43=b34;b54=b45;b65=b56;<br class=""> % Shunt <br class=""> Qshunt=Qc/Sbase;<br class=""> Zshunt=Ubase^2/(Qshunt*j);<br class=""> Zbshunt=Zshunt/Zb;<br class=""> yshunt=i*Qshunt/1^2;<br class=""> % YBUS matrix<br class=""> y11=1/z12+1/z13+b12+b13; y12=-1/z12; y13=-1/z13; y14=0; y15=0; y16=0;<br class=""> y21=-1/z12; y22=1/z12+1/z23+b12+b23; y23=-1/z23; y24=0; y25=0; y26=0;<br class=""> y31=-1/z13; y32=-1/z23; y33=1/z13+1/z23+1/z34+b13+b23+b34; y34=-1/z34; y35=0; y36=0;<br class=""> y41=0; y42=0; y43=-1/z34; y44=1/z34+yshunt+1/z45+b34+b45; y45=-1/z45; y46=0;<br class=""> y51=0; y52=0; y53=0; y54=-1/z45; y55=1/z45+1/z56+b45+b56; y56=-1/z56;<br class=""> y61=0; y62=0; y63=0; y64=0; y65=-1/z56; y66=1/z56+b56;<br class=""> YBUS=[ y11 y12 y13 y14 y15 y16;<br class=""> y21 y22 y23 y24 y25 y26;<br class=""> y31 y32 y33 y34 y35 y36;<br class=""> y41 y42 y43 y44 y45 y46;<br class=""> y51 y52 y53 y54 y55 y56;<br class=""> y61 y62 y63 y64 y65 y66];% Define YBUS<br class=""> G=real(YBUS);<br class=""> B=imag(YBUS);<br class="">" TCL MERGE ERROR ( 04/14/2020 13:15:31 ): "wrong # args: should be "for start test next command"<br class=""> % PGD and QGD for PU- and PQ-buses<br class=""> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br class=""> PGD2=PG2-PL2; % for bus 2 (PQ-bus)<br class=""> PGD3=PG3-PL3; % for bus 3 (PQ-bus)<br class=""> PGD4=PG4-PL4; % for bus 4 (PQ-bus)<br class=""> PGD5=PG5-PL5;<br class=""> PGD6=PG6-PL6;<br class=""> PGD=[PGD2 ; PGD3 ; PGD4 ; PGD5 ; PGD6];<br class=""> QGD2=QG2-QL2;<br class=""> QGD3=QG3-QL3;<br class=""> QGD4=QG4-QL4;<br class=""> QGD5=QG5-QL5;<br class=""> QGD6=QG6-QL6; <br class=""> QGD=[QGD2 ; QGD3 ; QGD4 ; QGD5 ; QGD6];<br class=""> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br class=""> % Use fsolve function in MATLAB to run load flow<br class=""> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br class=""> % Unknown variables [theta2 theta3 theta4 U2 U3 U4]’;<br class=""> % Define the initial values of the unknown variables<br class=""> X0=[0 0 0 0 0 1 1 1 1 1]'; % Flat initial values<br class=""> s_z=size(X0);<br class=""> nx=s_z(1,1); % number of unknown variables<br class=""> % The function below is used for fsolve (type "help fsolve" in MATLAB)<br class=""> options_solve = optimset('Display','off','TolX',tole,'TolFun',tole);<br class=""> % Parameters used for fsolve<br class=""> PAR=[nx ; nbus ; U1 ; theta1];% U1 and theta1 are known (slack bus).<br class=""> [X_X,FVAL,EXITFLAG,OUTPUT]=fsolve('EG2100_solve_LF',X0,options_solve,G,B,PGD,QGD,PAR);<br class=""> if EXITFLAG~=1,<br class=""> disp('No solution'),<br class=""> EXITFLAG=EXITFLAG,<br class=""> return<br class=""> end,<br class=""> % Solved variables X_X=[theta2 theta3 theta4 U2 U3 U4]’;<br class=""> ANG=[theta1 X_X(1) X_X(2) X_X(3) X_X(4) X_X(5)];% Voltage phase angles<br class=""> VOLT=[U1 X_X(6) X_X(7) X_X(8) X_X(9) X_X(10)]; % Voltage magnitudes<br class=""> ANG_deg=ANG*deg;% in degrees<br class=""> VOLT_kV=VOLT*Ubase; % in kV<br class=""> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br class=""> % The generated active and reactive power at slack bus and PU-buses<br class=""> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br class=""> g=-G;b=-B;<br class=""> % At slack bus, bus 1<br class=""> P12=g(1,2)*VOLT(1)^2-...<br class=""> VOLT(1)*VOLT(2)*(g(1,2)*cos(ANG(1)-ANG(2))+b(1,2)*sin(ANG(1)-ANG(2)));<br class=""> P13=g(1,3)*VOLT(1)^2-...<br class=""> VOLT(1)*VOLT(3)*(g(1,3)*cos(ANG(1)-ANG(3))+b(1,3)*sin(ANG(1)-ANG(3)));<br class=""> Q12=(-b12-b(1,2))*VOLT(1)^2-...<br class=""> VOLT(1)*VOLT(2)*(g(1,2)*sin(ANG(1)-ANG(2))-b(1,2)*cos(ANG(1)-ANG(2)));<br class=""> Q13=(-b13-b(1,3))*VOLT(1)^2-...<br class=""> VOLT(1)*VOLT(3)*(g(1,3)*sin(ANG(1)-ANG(3))-b(1,3)*cos(ANG(1)-ANG(3)));<br class=""> PG1=P12+P13+PL1;<br class=""> QG1=Q12+Q13+QL1;<br class=""> PG1_MW=PG1*Sbase;<br class=""> QG1_MW=QG1*Sbase;<br class=""> %%%%%%%%%<br class=""> % Losses<br class=""> %%%%%%%%%<br class=""> P34=g(3,4)*VOLT(3)^2-...<br class=""> VOLT(3)*VOLT(4)*(g(3,4)*cos(ANG(3)-ANG(4))+b(3,4)*sin(ANG(3)-ANG(4)));<br class=""> PLoss_tot=(PG1+PG2+PG3+PG4+PG5+PG6)-(PL1+PL2+PL3+PL4+PL5+PL6);<br class=""> PLoss_tot_MW=PLoss_tot*Sbase<br class=""> PLoss_Sys1=(PG1+PG2+PG3)-(PL1+PL2+PL3+P34);% Find the power losses in System 1 (pu)<br class=""> PLoss_Sys1_MW=PLoss_Sys1*Sbase<br class=""> PLoss_Sys2=(PG4+PG5+PG6+P34)-(PL4+PL5+PL6);% Find the power losses in System 2 (MW)<br class=""> PLoss_Sys2_MW=PLoss_Sys2*Sbase <br class=""> % End of file (run_LF.m)<br class=""> The function is in the second file named EG2100_solve_LF<br class=""> % Start of file (solve_lf.m)<br class=""> % This function solves g(x)=0 for x.<br class=""> function [g_x]=EG2100_solve_lf(X,G,B,PGD,QGD,PAR);<br class=""> nx=PAR(1); nbus=PAR(2); U1=PAR(3); theta1=PAR(4);<br class=""> PGD2=PGD(1); PGD3=PGD(2); PGD4=PGD(3); PGD5=PGD(4); PGD6=PGD(5); <br class=""> QGD2=QGD(1); QGD3=QGD(2); QGD4=QGD(3); QGD5=QGD(4); QGD6=QGD(5);<br class=""> theta2=X(1); theta3=X(2); theta4=X(3); theta5=X(4); theta6=X(5);<br class=""> U2=X(6); U3=X(7); U4=X(8); U5=X(9); U6=X(10);<br class=""> ANG=[theta1 theta2 theta3 theta4 theta5 theta6]' ; VOLT=[U1 U2 U3 U4 U5 U6]';<br class=""> % We have nx unknown variables, therefore the<br class=""> % size of g(x) is nx by 1.<br class=""> g_x=zeros(nx,1);<br class=""> %Based on equation (7.43), find Pk and Qk<br class=""> P2=VOLT(2)*(VOLT(1)*(G(2,1)*cos(ANG(2)-ANG(1))+B(2,1)*sin(ANG(2)-ANG(1)))...<br class=""> +VOLT(3)*(G(2,3)*cos(ANG(2)-ANG(3))+B(2,3)*sin(ANG(2)-ANG(3)))...<br class=""> +VOLT(2)*(G(2,2)*cos(ANG(2)-ANG(2))+B(2,2)*sin(ANG(2)-ANG(2))));<br class=""> % +U2*U4*(G(2,4)*cos(theta2-theta4)+B(2,4)*sin(theta2-theta4))...<br class=""> % +U2*U5*(G(2,5)*cos(theta2-theta5)+B(2,5)*sin(theta2-theta5))...<br class=""> % +U2*U6*(G(2,6)*cos(theta2-theta6)+B(2,6)*sin(theta2-theta6));<br class=""> P3=VOLT(3)*(VOLT(2)*(G(3,2)*cos(ANG(3)-ANG(2))+B(3,2)*sin(ANG(3)-ANG(2)))...<br class=""> +VOLT(4)*(G(3,4)*cos(ANG(3)-ANG(4))+B(3,4)*sin(ANG(3)-ANG(4)))...<br class=""> +VOLT(3)*(G(3,3)*cos(ANG(3)-ANG(3))+B(3,3)*sin(ANG(3)-ANG(3)))...<br class=""> +VOLT(1)*(G(3,1)*cos(ANG(3)-ANG(1))+B(3,1)*sin(ANG(3)-ANG(1))));<br class=""> % +U3*U6*(G(3,6)*cos(theta3-theta6)+B(3,6)*sin(theta3-theta6))...;<br class=""> P4=VOLT(4)*(VOLT(3)*(G(4,3)*cos(ANG(4)-ANG(3))+B(4,3)*sin(ANG(4)-ANG(3)))...<br class=""> +VOLT(5)*(G(4,5)*cos(ANG(4)-ANG(5))+B(4,5)*sin(ANG(4)-ANG(5)))...<br class=""> +VOLT(4)*(G(4,4)*cos(ANG(4)-ANG(4))+B(4,4)*sin(ANG(4)-ANG(4))));<br class=""> P5=VOLT(5)*(VOLT(4)*(G(5,4)*cos(ANG(5)-ANG(4))+B(5,4)*sin(ANG(5)-ANG(4)))...<br class=""> +VOLT(6)*(G(5,6)*cos(ANG(5)-ANG(6))+B(5,6)*sin(ANG(5)-ANG(6)))...<br class=""> +VOLT(5)*(G(5,5)*cos(ANG(5)-ANG(3))+B(5,5)*sin(ANG(5)-ANG(5))));<br class=""> P6=VOLT(6)*(VOLT(5)*(G(6,5)*cos(ANG(6)-ANG(5))+B(6,5)*sin(ANG(6)-ANG(5)))...<br class=""> +VOLT(6)*(G(6,6)*cos(ANG(2)-ANG(3))+B(2,3)*sin(ANG(2)-ANG(3))));<br class=""> %Qk<br class=""> Q2=VOLT(2)*(VOLT(1)*(G(2,1)*sin(ANG(2)-ANG(1))-B(2,1)*cos(ANG(2)-ANG(1)))...<br class=""> +VOLT(3)*(G(2,3)*sin(ANG(2)-ANG(3))-B(2,3)*cos(ANG(2)-ANG(3)))...<br class=""> +VOLT(2)*(G(2,2)*sin(ANG(2)-ANG(2))-B(2,2)*cos(ANG(2)-ANG(2))));<br class=""> Q3=VOLT(3)*(VOLT(2)*(G(3,2)*sin(ANG(3)-ANG(2))-B(3,2)*cos(ANG(3)-ANG(2)))...<br class=""> +VOLT(4)*(G(3,4)*sin(ANG(3)-ANG(4))-B(3,4)*cos(ANG(3)-ANG(4)))...<br class=""> +VOLT(3)*(G(3,3)*sin(ANG(3)-ANG(3))-B(3,3)*cos(ANG(3)-ANG(3)))); <br class=""> Q4=VOLT(4)*(VOLT(3)*(G(4,3)*sin(ANG(4)-ANG(3))-B(4,3)*cos(ANG(4)-ANG(3)))...<br class=""> +VOLT(5)*(G(4,5)*sin(ANG(4)-ANG(5))-B(4,5)*cos(ANG(4)-ANG(5)))...<br class=""> +VOLT(4)*(G(4,4)*sin(ANG(4)-ANG(4))-B(4,4)*cos(ANG(4)-ANG(4)))); <br class=""> Q5=VOLT(5)*(VOLT(4)*(G(5,4)*sin(ANG(5)-ANG(4))-B(5,4)*cos(ANG(5)-ANG(4)))...<br class=""> +VOLT(6)*(G(5,6)*sin(ANG(5)-ANG(6))-B(5,6)*cos(ANG(5)-ANG(6)))...<br class=""> +VOLT(5)*(G(5,5)*sin(ANG(5)-ANG(5))-B(5,5)*cos(ANG(5)-ANG(5)))); <br class=""> Q6=VOLT(6)*(VOLT(5)*(G(6,5)*sin(ANG(6)-ANG(5))-B(6,5)*cos(ANG(6)-ANG(5)))...<br class=""> +VOLT(6)*(G(2,3)*sin(ANG(6)-ANG(6))-B(6,6)*cos(ANG(6)-ANG(6)))); <br class=""> % Active power mismatch (PU- and PQ-buses)<br class=""> % Bus 2<br class=""> g_x(1)=P2-PGD2;<br class=""> % Bus 3<br class=""> g_x(2)=P3-PGD3;<br class=""> % Bus 4<br class=""> g_x(3)=P4-PGD4;<br class=""> % Bus 5<br class=""> g_x(4)=P5-PGD5;<br class=""> % Bus 6<br class=""> g_x(5)=P6-PGD6;<br class=""> % Reactive power mismatch (PQ-buses)<br class=""> % Bus 2<br class=""> g_x(6)=Q2-QGD2;<br class=""> % Bus 3<br class=""> g_x(7)=Q3-QGD3;<br class=""> % Bus 4<br class=""> g_x(8)=Q4-QGD4;<br class=""> % Bus 5<br class=""> g_x(9)=Q5-QGD5;<br class=""> % Bus 6<br class=""> g_x(10)=Q6-QGD6;<br class=""> % End of file (solve_lf.m)<br clear="all" class=""> <div class=""> <div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"> <div dir="ltr" class=""> <div class=""> <div dir="ltr" class=""> <div dir="ltr" class=""> <div class=""> <div dir="ltr" class=""> <blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><font size="4" class=""><font color="#6fa8dc" style="background-color:rgb(243,243,243)" face="garamond, serif" class=""><font class="">Teshome Hambissa</font><br class=""> Lecturer<br class=""> Addis Ababa Universiy, AAiT, SECE<br class=""> Addis Ababa,</font><font color="#6fa8dc" style="background-color:rgb(243,243,243)" face="garamond, serif" class=""> Ethiopia</font></font></blockquote> </div> </div> </div> </div> </div> </div> </div> </div> <br class=""> </div> </div> <br class=""> <div class="gmail_quote"> <div dir="ltr" class="gmail_attr">On Tue, Apr 14, 2020 at 4:31 PM Teshome Hambissa <<a href="mailto:[email protected]" class="">[email protected]</a>> wrote:<br class=""> </div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr" class="">Dear member, <div class="">I need run power flow calculation for three generator and four bus system.can you give me the way i understand the matlab code for this problem</div> <div class=""><br class=""> </div> <div class="">kind regards,<br clear="all" class=""> <div class=""> <div dir="ltr" class=""> <div dir="ltr" class=""> <div class=""> <div dir="ltr" class=""> <div dir="ltr" class=""> <div class=""> <div dir="ltr" class=""> <blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><font size="4" class=""><font color="#6fa8dc" style="background-color:rgb(243,243,243)" face="garamond, serif" class=""><font class="">Teshome Hambissa</font><br class=""> Lecturer<br class=""> Addis Ababa Universiy, AAiT, SECE<br class=""> Addis Ababa,</font><font color="#6fa8dc" style="background-color:rgb(243,243,243)" face="garamond, serif" class=""> Ethiopia</font></font></blockquote> </div> </div> </div> </div> </div> </div> </div> </div> <br class=""> </div> </div> <br class=""> <div class="gmail_quote"> <div dir="ltr" class="gmail_attr">On Mon, Apr 13, 2020 at 9:58 PM Ray Daniel Zimmerman <<a href="mailto:[email protected]" target="_blank" class="">[email protected]</a>> wrote:<br class=""> </div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div class="">If I understand correctly, <font face="Courier" class="">persistent</font> should work if it appears at the top-level within the function (as opposed to inside if-then blocks like this particular case). So I think you should be able to fix this one by moving that line to the first line in the function. I don’t see any other <span style="font-family:Courier" class="">persistent</span> calls that should require changing. That is, I believe they are all already at the top level of their function. <div class=""><br class=""> </div> <div class="">As far as <font face="Courier" class="">num2cell()</font>, my first idea is to try replacing all calls with a call to a new function, say <font face="Courier" class="">mp_num2cell()</font> that simply creates a cell array of the same dimensions as the input and copies the contents element by element, something like …</div> <div class=""><br class=""> </div> <div class=""> <div class=""><font face="Courier" class="">function c = mp_num2cell(num)</font></div> <div class=""><font face="Courier" class="">c = cell(size(num));</font></div> <div class=""><font face="Courier" class="">for k = 1:length(num(:))</font></div> <div class=""><font face="Courier" class=""> c{k} = num(k);</font></div> <div class=""><font face="Courier" class="">end</font></div> </div> <div class=""><br class=""> </div> <div class=""> <div class="">Let me know if that works and if anything else pops up.</div> <div class=""><br class=""> </div> <div class=""> Ray</div> <div class=""><br class=""> </div> <div class=""><br class=""> <div class=""><br class=""> <blockquote type="cite" class=""> <div class="">On Apr 13, 2020, at 9:58 AM, Lode De Herdt <<a href="mailto:[email protected]" target="_blank" class="">[email protected]</a>> wrote:</div> <br class=""> <div class=""> <div dir="ltr" class="">Hi Ray, <div class=""><br class=""> </div> <div class="">Thank you for the reply. I've spent a little time trying to remove the things that are unsupported from the MATPOWER code but it looks like I'm opening a Pandora's box here; each error I solve just leads to the next errors so it's difficult to solve without good knowledge of MATLAB and the inner workings of MATPOWER.</div> <div class=""><br class=""> </div> <div class="">The first error was:</div> <div class=""><i class="">PERSISTENT declarations may only appear at the top-level.</i></div> <div class=""><i class="">Function 'moption.m' (#57.41253.41263), line 697, column 9:</i></div> <div class=""><i class="">"persistent"</i></div> <div class=""><br class=""> </div> <div class="">So I replaced <i class="">persistent nsc_opt</i> with <i class="">nsc_opt = [];</i> and did the same for other places where 'persistent' was used as was suggested in <a href="https://www.mail-archive.com/[email protected]/msg05192.html" target="_blank" class=""> message 05192</a> of this forum, but that then leads to this:</div> <div class=""><i class="">This assignment writes a 'struct' value into a 'double' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches.<br class=""> Function 'mpoption.m' (#34.71098.71101), line 1531, column 5:<br class=""> "opt"</i><br class=""> </div> <div class=""><br class=""> </div> <div class="">One of the other errors indicates that the <i class="">num2cell </i> function is not supported for code generation, but who knows what else will appear once that one is solved.</div> <div class=""><br class=""> </div> <div class="">Let me know if you or anyone else has any other ideas, or experience with MATPOWER and code generation.</div> <div class=""><br class=""> </div> <div class="">Best regards,</div> <div class="">Lode</div> <div class=""><i class=""><br class=""> </i></div> <div class=""><i class=""><br class=""> </i></div> </div> <br class=""> <div class="gmail_quote"> <div dir="ltr" class="gmail_attr">On Mon, Apr 13, 2020 at 4:36 PM Ray Daniel Zimmerman <<a href="mailto:[email protected]" target="_blank" class="">[email protected]</a>> wrote:<br class=""> </div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div class="">I can’t think of another option. I’ve not personally worked with the C code generation in MATLAB, so I’m not familiar with the requirements. Do you know what specifically is used by MATPOWER that is not supported? If there are easy ways to avoid those things, it would certainly make sense to do so. <div class=""><br class=""> </div> <div class=""> Ray</div> <div class=""><br class=""> <div class=""><br class=""> <blockquote type="cite" class=""> <div class="">On Apr 13, 2020, at 7:53 AM, Lode De Herdt <<a href="mailto:[email protected]" target="_blank" class="">[email protected]</a>> wrote:</div> <br class=""> <div class=""> <div dir="ltr" class="">Dear Matpower users, <div class=""><br class=""> </div> <div class="">I am trying to run a simulation of a distribution grid on an OPAL-RT real-time digital simulator, using Matpower. The simulator requires a Simulink model, which it converts to C code in order to run the model on its hardware. Using the <i class="">MATLAB Function</i> block in Simulink, I can integrate a few lines of Matlab code into the model that run a power flow with Matpower.</div> <div class=""><br class=""> </div> <div class="">Unfortunately it seems that not all of the code used for Matpower is supported for C code generation, so the Simulink model cannot run.</div> <div class=""><br class=""> </div> <div class="">Other than manually re-writing the Matpower code to avoid everything that's unsupported for code generation, is there any easier way of running Matpower in Simulink? It must be compatible with code generation so using an extrinsic coder or something similar is unfortunately not an option.</div> <div class=""><br class=""> </div> <div class="">Thank you for your help!</div> <div class=""><br class=""> </div> <div class="">Best regards,</div> <div class="">Lode</div> </div> </div> </blockquote> </div> <br class=""> </div> </div> </blockquote> </div> </div> </blockquote> </div> <br class=""> </div> </div> </div> </blockquote> </div> </blockquote> </div> </div> </blockquote> </div> <br class=""> </body> </html> --
