CPF_DEFAULT_CALLBACK Default callback function for CPF [CB_STATE, RESULTS] = ... CPF_DEFAULT_CALLBACK(K, V_C, LAM_C, V_P, LAM_P, ... CB_DATA, CB_STATE, CB_ARGS, RESULTS) Default callback function used by RUNCPF. Takes input from current iteration, returns a user defined state struct and on the final call a results struct. Inputs: K - continuation step iteration count V_C - vector of complex bus voltages after K-th corrector step LAM_C - value of LAMBDA after K-th corrector step V_P - vector of complex bus voltages after K-th predictor step LAM_P - value of LAMBDA after K-th predictor step CB_DATA - struct containing potentially useful static data, with the following fields (all based on internal indexing): mpc_base - MATPOWER case struct of base state mpc_target - MATPOWER case struct of target state Sxfr - nb x 1 vector of scheduled transfers in p.u. Ybus - bus admittance matrix Yf - branch admittance matrix, "from" end of branches Yt - branch admittance matrix, "to" end of branches pv - vector of indices of PV buses pq - vector of indices of PQ buses ref - vector of indices of REF buses mpopt - MATPOWER options struct CB_STATE - struct to which the user may add fields containing any information the callback function would like to pass from one invokation to the next (avoiding the following 5 field names which are already used by the default callback: V_p, lam_p, V_c, lam_c, iterations) CB_ARGS - struct specified in MPOPT.cpf.user_callback_args RESULTS - initial value of output struct to be assigned to CPF field of results struct returned by RUNCPF Outputs: CB_STATE - updated version of CB_STATE input arg RESULTS - updated version of RESULTS input arg This function is called in three different contexts, distinguished as follows: (1) initial - called without RESULTS output arg, with K = 0, after base power flow, before 1st CPF step. (2) iterations - called without RESULTS output arg, with K > 0 at each iteration, after predictor-corrector step (3) final - called with RESULTS output arg, after exiting predictor-corrector loop, inputs identical to last iteration call See also RUNCPF.
0001 function [cb_state, results] = ... 0002 cpf_default_callback(k, V_c, lam_c, V_p, lam_p, cb_data, cb_state, cb_args, results) 0003 %CPF_DEFAULT_CALLBACK Default callback function for CPF 0004 % [CB_STATE, RESULTS] = ... 0005 % CPF_DEFAULT_CALLBACK(K, V_C, LAM_C, V_P, LAM_P, ... 0006 % CB_DATA, CB_STATE, CB_ARGS, RESULTS) 0007 % 0008 % Default callback function used by RUNCPF. Takes input from current 0009 % iteration, returns a user defined state struct and on the final call 0010 % a results struct. 0011 % 0012 % Inputs: 0013 % K - continuation step iteration count 0014 % V_C - vector of complex bus voltages after K-th corrector step 0015 % LAM_C - value of LAMBDA after K-th corrector step 0016 % V_P - vector of complex bus voltages after K-th predictor step 0017 % LAM_P - value of LAMBDA after K-th predictor step 0018 % CB_DATA - struct containing potentially useful static data, 0019 % with the following fields (all based on internal indexing): 0020 % mpc_base - MATPOWER case struct of base state 0021 % mpc_target - MATPOWER case struct of target state 0022 % Sxfr - nb x 1 vector of scheduled transfers in p.u. 0023 % Ybus - bus admittance matrix 0024 % Yf - branch admittance matrix, "from" end of branches 0025 % Yt - branch admittance matrix, "to" end of branches 0026 % pv - vector of indices of PV buses 0027 % pq - vector of indices of PQ buses 0028 % ref - vector of indices of REF buses 0029 % mpopt - MATPOWER options struct 0030 % CB_STATE - struct to which the user may add fields containing 0031 % any information the callback function would like to 0032 % pass from one invokation to the next (avoiding the 0033 % following 5 field names which are already used by 0034 % the default callback: V_p, lam_p, V_c, lam_c, iterations) 0035 % CB_ARGS - struct specified in MPOPT.cpf.user_callback_args 0036 % RESULTS - initial value of output struct to be assigned to 0037 % CPF field of results struct returned by RUNCPF 0038 % 0039 % Outputs: 0040 % CB_STATE - updated version of CB_STATE input arg 0041 % RESULTS - updated version of RESULTS input arg 0042 % 0043 % This function is called in three different contexts, distinguished 0044 % as follows: 0045 % (1) initial - called without RESULTS output arg, with K = 0, 0046 % after base power flow, before 1st CPF step. 0047 % (2) iterations - called without RESULTS output arg, with K > 0 0048 % at each iteration, after predictor-corrector step 0049 % (3) final - called with RESULTS output arg, after exiting 0050 % predictor-corrector loop, inputs identical to 0051 % last iteration call 0052 % 0053 % See also RUNCPF. 0054 0055 % MATPOWER 0056 % $Id: cpf_default_callback.m 2235 2013-12-11 13:44:13Z ray $ 0057 % by Ray Zimmerman, PSERC Cornell 0058 % Copyright (c) 2013 by Power System Engineering Research Center (PSERC) 0059 % 0060 % This file is part of MATPOWER. 0061 % See http://www.pserc.cornell.edu/matpower/ for more info. 0062 % 0063 % MATPOWER is free software: you can redistribute it and/or modify 0064 % it under the terms of the GNU General Public License as published 0065 % by the Free Software Foundation, either version 3 of the License, 0066 % or (at your option) any later version. 0067 % 0068 % MATPOWER is distributed in the hope that it will be useful, 0069 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0070 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0071 % GNU General Public License for more details. 0072 % 0073 % You should have received a copy of the GNU General Public License 0074 % along with MATPOWER. If not, see <http://www.gnu.org/licenses/>. 0075 % 0076 % Additional permission under GNU GPL version 3 section 7 0077 % 0078 % If you modify MATPOWER, or any covered work, to interface with 0079 % other modules (such as MATLAB code and MEX-files) available in a 0080 % MATLAB(R) or comparable environment containing parts covered 0081 % under other licensing terms, the licensors of MATPOWER grant 0082 % you additional permission to convey the resulting work. 0083 0084 %% initialize plotting options 0085 plot_level = cb_data.mpopt.cpf.plot.level; 0086 plot_bus = cb_data.mpopt.cpf.plot.bus; 0087 if plot_level 0088 if isempty(plot_bus) %% no bus specified 0089 %% pick PQ bus with largest transfer 0090 [junk, idx] = max(cb_data.Sxfr(cb_data.pq)); 0091 if isempty(idx) %% or bus 1 if there are none 0092 idx = 1; 0093 else 0094 idx = cb_data.pq(idx(1)); 0095 end 0096 idx_e = cb_data.mpc_target.order.bus.i2e(idx); 0097 else 0098 idx_e = plot_bus; %% external bus number 0099 idx = full(cb_data.mpc_target.order.bus.e2i(idx_e)); 0100 if idx == 0 0101 error('cpf_default_callback: %d is not a valid bus number for MPOPT.cpf.plot.bus', idx_e); 0102 end 0103 end 0104 end 0105 0106 %%----- INITIAL call ----- 0107 if k == 0 0108 %% initialize state 0109 cb_state = struct( 'V_p', V_p, ... 0110 'lam_p', lam_p, ... 0111 'V_c', V_c, ... 0112 'lam_c', lam_c, ... 0113 'iterations', 0); 0114 0115 %% initialize lambda-V nose curve plot 0116 if plot_level 0117 plot(cb_state.lam_p(1), abs(cb_state.V_p(idx,1)), '-'); 0118 title(sprintf('Voltage at Bus %d', idx_e)); 0119 xlabel('\lambda'); 0120 ylabel('Voltage Magnitude'); 0121 axis([0 max([1;max(cb_state.lam_p);max(cb_state.lam_c)])*1.05 ... 0122 0 max([1;max(abs(cb_state.V_p(idx)));max(abs(cb_state.V_c(idx)))*1.05])]); 0123 hold on; 0124 end 0125 %%----- FINAL call ----- 0126 elseif nargout == 2 0127 %% assemble results struct 0128 results = cb_state; %% initialize results with final state 0129 results.max_lam = max(cb_state.lam_c); 0130 results.iterations = k; 0131 0132 %% finish final lambda-V nose curve plot 0133 if plot_level 0134 %% plot the final nose curve 0135 plot(cb_state.lam_c', ... 0136 abs(cb_state.V_c(idx,:))', ... 0137 '-'); 0138 axis([0 max([1;max(cb_state.lam_p);max(cb_state.lam_c)])*1.05 ... 0139 0 max([1;max(abs(cb_state.V_p(idx)));max(abs(cb_state.V_c(idx)))*1.05])]); 0140 hold off; 0141 end 0142 %%----- ITERATION call ----- 0143 else 0144 %% update state 0145 cb_state.V_p = [cb_state.V_p V_p]; 0146 cb_state.lam_p = [cb_state.lam_p lam_p]; 0147 cb_state.V_c = [cb_state.V_c V_c]; 0148 cb_state.lam_c = [cb_state.lam_c lam_c]; 0149 cb_state.iterations = k; 0150 0151 %% plot single step of the lambda-V nose curve 0152 if plot_level > 1 0153 plot([cb_state.lam_c(k); cb_state.lam_p(k+1)], ... 0154 [abs(cb_state.V_c(idx,k)); abs(cb_state.V_p(idx,k+1))], ... 0155 '-', 'Color', 0.85*[1 0.75 0.75]); 0156 plot([cb_state.lam_p(k+1); cb_state.lam_c(k+1)], ... 0157 [abs(cb_state.V_p(idx,k+1)); abs(cb_state.V_c(idx,k+1))], ... 0158 '-', 'Color', 0.85*[0.75 1 0.75]); 0159 plot(cb_state.lam_p(k+1), abs(cb_state.V_p(idx,k+1)), 'x', ... 0160 'Color', 0.85*[1 0.75 0.75]); 0161 plot(cb_state.lam_c(k+1)', ... 0162 abs(cb_state.V_c(idx,k+1))', ... 0163 '-o'); 0164 axis([0 max([1;max(cb_state.lam_p);max(cb_state.lam_c)])*1.05 ... 0165 0 max([1;max(abs(cb_state.V_p(idx)));max(abs(cb_state.V_c(idx)))*1.05])]); 0166 drawnow; 0167 if plot_level > 2 0168 pause; 0169 end 0170 end 0171 end