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 % Copyright (c) 2013-2015 by Power System Engineering Research Center (PSERC) 0057 % by Ray Zimmerman, PSERC Cornell 0058 % 0059 % $Id: cpf_default_callback.m 2644 2015-03-11 19:34:22Z ray $ 0060 % 0061 % This file is part of MATPOWER. 0062 % Covered by the 3-clause BSD License (see LICENSE file for details). 0063 % See http://www.pserc.cornell.edu/matpower/ for more info. 0064 0065 %% initialize plotting options 0066 plot_level = cb_data.mpopt.cpf.plot.level; 0067 plot_bus = cb_data.mpopt.cpf.plot.bus; 0068 if plot_level 0069 if isempty(plot_bus) %% no bus specified 0070 %% pick PQ bus with largest transfer 0071 [junk, idx] = max(cb_data.Sxfr(cb_data.pq)); 0072 if isempty(idx) %% or bus 1 if there are none 0073 idx = 1; 0074 else 0075 idx = cb_data.pq(idx(1)); 0076 end 0077 idx_e = cb_data.mpc_target.order.bus.i2e(idx); 0078 else 0079 idx_e = plot_bus; %% external bus number 0080 idx = full(cb_data.mpc_target.order.bus.e2i(idx_e)); 0081 if idx == 0 0082 error('cpf_default_callback: %d is not a valid bus number for MPOPT.cpf.plot.bus', idx_e); 0083 end 0084 end 0085 end 0086 0087 %%----- FINAL call ----- 0088 if nargout == 2 0089 %% assemble results struct 0090 results = cb_state; %% initialize results with final state 0091 results.max_lam = max(cb_state.lam_c); 0092 results.iterations = k; 0093 0094 %% finish final lambda-V nose curve plot 0095 if plot_level 0096 %% plot the final nose curve 0097 plot(cb_state.lam_c', ... 0098 abs(cb_state.V_c(idx,:))', ... 0099 '-', 'Color', [0.25 0.25 1]); 0100 axis([0 max([1;max(cb_state.lam_p);max(cb_state.lam_c)])*1.05 ... 0101 0 max([1;max(abs(cb_state.V_p(idx)));max(abs(cb_state.V_c(idx)))*1.05])]); 0102 hold off; 0103 end 0104 %%----- INITIAL call ----- 0105 elseif k == 0 0106 %% initialize state 0107 cb_state = struct( 'V_p', V_p, ... 0108 'lam_p', lam_p, ... 0109 'V_c', V_c, ... 0110 'lam_c', lam_c, ... 0111 'iterations', 0); 0112 0113 %% initialize lambda-V nose curve plot 0114 if plot_level 0115 plot(cb_state.lam_p(1), abs(cb_state.V_p(idx,1)), '-', 'Color', [0.25 0.25 1]); 0116 title(sprintf('Voltage at Bus %d', idx_e)); 0117 xlabel('\lambda'); 0118 ylabel('Voltage Magnitude'); 0119 axis([0 max([1;max(cb_state.lam_p);max(cb_state.lam_c)])*1.05 ... 0120 0 max([1;max(abs(cb_state.V_p(idx)));max(abs(cb_state.V_c(idx)))*1.05])]); 0121 hold on; 0122 end 0123 %%----- ITERATION call ----- 0124 else 0125 %% update state 0126 cb_state.V_p = [cb_state.V_p V_p]; 0127 cb_state.lam_p = [cb_state.lam_p lam_p]; 0128 cb_state.V_c = [cb_state.V_c V_c]; 0129 cb_state.lam_c = [cb_state.lam_c lam_c]; 0130 cb_state.iterations = k; 0131 0132 %% plot single step of the lambda-V nose curve 0133 if plot_level > 1 0134 plot([cb_state.lam_c(k); cb_state.lam_p(k+1)], ... 0135 [abs(cb_state.V_c(idx,k)); abs(cb_state.V_p(idx,k+1))], ... 0136 '-', 'Color', 0.85*[1 0.75 0.75]); 0137 plot([cb_state.lam_p(k+1); cb_state.lam_c(k+1)], ... 0138 [abs(cb_state.V_p(idx,k+1)); abs(cb_state.V_c(idx,k+1))], ... 0139 '-', 'Color', 0.85*[0.75 1 0.75]); 0140 plot(cb_state.lam_p(k+1), abs(cb_state.V_p(idx,k+1)), 'x', ... 0141 'Color', 0.85*[1 0.75 0.75]); 0142 plot(cb_state.lam_c(k+1)', ... 0143 abs(cb_state.V_c(idx,k+1))', ... 0144 '-o', 'Color', [0.25 0.25 1]); 0145 axis([0 max([1;max(cb_state.lam_p);max(cb_state.lam_c)])*1.05 ... 0146 0 max([1;max(abs(cb_state.V_p(idx)));max(abs(cb_state.V_c(idx)))*1.05])]); 0147 drawnow; 0148 if plot_level > 2 0149 pause; 0150 end 0151 end 0152 end