CPF_TARGET_LAM_EVENT_CB Callback to handle TARGET_LAM events [NX, CX, DONE, ROLLBACK, EVNTS, CB_DATA, RESULTS] = CPF_TARGET_LAM_EVENT_CB(K, NX, CX, PX, DONE, ROLLBACK, EVNTS, ... CB_DATA, CB_ARGS, RESULTS) Callback to handle TARGET_LAM events, triggered by event function CPF_TARGET_LAM_EVENT to indicate that a target lambda value has been reached or that the full continuation curve has been traced. This function sets the msg field of the event when the target lambda has been found, raises the DONE.flag and sets the DONE.msg. If the current or predicted next step overshoot the target lambda, it adjusts the step size to be exactly what is needed to reach the target, and sets the parameterization for that step to be the natural parameterization. See CPF_DEFAULT_CALLBACK for details of the input and output arguments.
0001 function [nx, cx, done, rollback, evnts, cb_data, results] = cpf_target_lam_event_cb(... 0002 k, nx, cx, px, done, rollback, evnts, cb_data, cb_args, results) 0003 %CPF_TARGET_LAM_EVENT_CB Callback to handle TARGET_LAM events 0004 % [NX, CX, DONE, ROLLBACK, EVNTS, CB_DATA, RESULTS] = 0005 % CPF_TARGET_LAM_EVENT_CB(K, NX, CX, PX, DONE, ROLLBACK, EVNTS, ... 0006 % CB_DATA, CB_ARGS, RESULTS) 0007 % 0008 % Callback to handle TARGET_LAM events, triggered by event function 0009 % CPF_TARGET_LAM_EVENT to indicate that a target lambda value has been 0010 % reached or that the full continuation curve has been traced. 0011 % 0012 % This function sets the msg field of the event when the target lambda has 0013 % been found, raises the DONE.flag and sets the DONE.msg. If the current 0014 % or predicted next step overshoot the target lambda, it adjusts the step 0015 % size to be exactly what is needed to reach the target, and sets the 0016 % parameterization for that step to be the natural parameterization. 0017 % 0018 % See CPF_DEFAULT_CALLBACK for details of the input and output arguments. 0019 0020 % MATPOWER 0021 % Copyright (c) 2016, Power Systems Engineering Research Center (PSERC) 0022 % by Ray Zimmerman, PSERC Cornell 0023 % and Shrirang Abhyankar, Argonne National Laboratory 0024 % 0025 % This file is part of MATPOWER. 0026 % Covered by the 3-clause BSD License (see LICENSE file for details). 0027 % See https://matpower.org for more info. 0028 0029 %% skip if initialize, finalize or done 0030 if k <= 0 || done.flag 0031 return; 0032 end 0033 0034 %% make stop_at numerical, 0 = FULL, -Inf = NOSE 0035 stop_at = cb_data.mpopt.cpf.stop_at; 0036 verbose = cb_data.mpopt.verbose; 0037 if ischar(stop_at) 0038 if strcmp(stop_at, 'FULL') 0039 stop_at = 0; 0040 else %% NOSE 0041 stop_at = -Inf; 0042 end 0043 end 0044 0045 %% handle event 0046 event_detected = 0; 0047 for i = 1:length(evnts) 0048 if strcmp(evnts(i).name, 'TARGET_LAM') 0049 event_detected = 1; 0050 if evnts(i).zero %% prepare to terminate 0051 done.flag = 1; 0052 if stop_at == 0 %% FULL 0053 done.msg = sprintf('Traced full continuation curve in %d continuation steps', k); 0054 else %% target lambda value 0055 done.msg = sprintf('Reached desired lambda %g in %d continuation steps', ... 0056 stop_at, k); 0057 end 0058 else %% set step-size & parameterization to terminate next time 0059 if stop_at == 0 %% FULL 0060 evnts(i).msg = sprintf('%s\n step %d expected to overshoot full trace, reduce step size and set natural param', evnts(i).msg, k); 0061 else %% target lambda value 0062 evnts(i).msg = sprintf('%s\n step %d expected to overshoot target lambda, reduce step size and set natural param', evnts(i).msg, k); 0063 end 0064 evnts(i).log = 1; 0065 if stop_at == 0 %% FULL 0066 cx.this_step = cx.lam; 0067 else %% target lambda value 0068 cx.this_step = stop_at - cx.lam; 0069 end 0070 cx.this_parm = 1; %% change to natural parameterization 0071 end 0072 break; 0073 end 0074 end 0075 0076 %% otherwise, check if predicted lambda of next step will overshoot 0077 %% (by more than remaining distance, to play it safe) 0078 if ~event_detected && ~rollback 0079 if isempty(nx.this_step) 0080 step = nx.default_step; 0081 else 0082 step = nx.this_step; 0083 end 0084 [V_hat, lam_hat] = cpf_predictor(nx.V, nx.lam, nx.z, step, cb_data.pv, cb_data.pq); 0085 if stop_at == 0 %% FULL 0086 if lam_hat < -nx.lam 0087 nx.this_step = nx.lam; 0088 nx.this_parm = 1; %% change to natural parameterization 0089 if verbose > 2 0090 fprintf(' step %d expected to overshoot full trace, reduce step size and set natural param\n', k+1); 0091 end 0092 end 0093 elseif stop_at > 0 %% target lambda value 0094 if lam_hat > stop_at + (stop_at - nx.lam) 0095 nx.this_step = stop_at - nx.lam; 0096 nx.this_parm = 1; %% change to natural parameterization 0097 if verbose > 2 0098 fprintf(' step %d expected to overshoot target lambda, reduce step size and set natural param\n', k+1); 0099 end 0100 end 0101 end 0102 end