ADD_USERFCN Appends a userfcn to the list to be called for a case. MPC = ADD_USERFCN(MPC, STAGE, FCN) MPC = ADD_USERFCN(MPC, STAGE, FCN, ARGS) MPC = ADD_USERFCN(MPC, STAGE, FCN, ARGS, ALLOW_MULTIPLE) A userfcn is a callback function that can be called automatically by MATPOWER at one of various stages in a simulation. MPC : the case struct STAGE : the name of the stage at which this function should be called: ext2int, formulation, int2ext, printpf, savecase FCN : the name of the userfcn ARGS : (optional) the value to be passed as an argument to the userfcn (typically a struct) ALLOW_MULTIPLE : (optional) if TRUE, allows the same function to be added more than once. Currently there are 5 different callback stages defined. Each stage has a name, and by convention, the name of a user-defined callback function ends with the name of the stage. The following is a description of each stage, when it is called and the input and output arguments which vary depending on the stage. The reserves example (see RUNOPF_W_RES) is used to illustrate how these callback userfcns might be used. 1. ext2int Called from EXT2INT immediately after the case is converted from external to internal indexing. Inputs are a MATPOWER case struct (MPC), freshly converted to internal indexing and any (optional) ARGS value supplied via ADD_USERFCN. Output is the (presumably updated) MPC. This is typically used to reorder any input arguments that may be needed in internal ordering by the formulation stage. E.g. mpc = userfcn_reserves_ext2int(mpc, mpopt, args) 2. formulation Called from OPF after the OPF Model (OM) object has been initialized with the standard OPF formulation, but before calling the solver. Inputs are the OM object and any (optional) ARGS supplied via ADD_USERFCN. Output is the OM object. This is the ideal place to add any additional vars, constraints or costs to the OPF formulation. E.g. om = userfcn_reserves_formulation(om, mpopt, args) 3. int2ext Called from INT2EXT immediately before the resulting case is converted from internal back to external indexing. Inputs are the RESULTS struct and any (optional) ARGS supplied via ADD_USERFCN. Output is the RESULTS struct. This is typically used to convert any results to external indexing and populate any corresponding fields in the RESULTS struct. E.g. results = userfcn_reserves_int2ext(results, mpopt, args) 4. printpf Called from PRINTPF after the pretty-printing of the standard OPF output. Inputs are the RESULTS struct, the file descriptor to write to, a MATPOWER options struct, and any (optional) ARGS supplied via ADD_USERFCN. Output is the RESULTS struct. This is typically used for any additional pretty-printing of results. E.g. results = userfcn_reserves_printpf(results, fd, mpopt, args) 5. savecase Called from SAVECASE when saving a case struct to an M-file after printing all of the other data to the file. Inputs are the case struct, the file descriptor to write to, the variable prefix (typically 'mpc.') and any (optional) ARGS supplied via ADD_USERFCN. Output is the case struct. This is typically used to write any non-standard case struct fields to the case file. E.g. mpc = userfcn_reserves_printpf(mpc, fd, prefix, args) See also RUN_USERFCN, REMOVE_USERFCN, TOGGLE_RESERVES, TOGGLE_IFLIMS, TOGGLE_DCLINE, TOGGLE_SOFTLIMS, and RUNOPF_W_RES.
0001 function mpc = add_userfcn(mpc, stage, fcn, args, allow_multiple) 0002 %ADD_USERFCN Appends a userfcn to the list to be called for a case. 0003 % 0004 % MPC = ADD_USERFCN(MPC, STAGE, FCN) 0005 % MPC = ADD_USERFCN(MPC, STAGE, FCN, ARGS) 0006 % MPC = ADD_USERFCN(MPC, STAGE, FCN, ARGS, ALLOW_MULTIPLE) 0007 % 0008 % A userfcn is a callback function that can be called automatically by 0009 % MATPOWER at one of various stages in a simulation. 0010 % 0011 % MPC : the case struct 0012 % STAGE : the name of the stage at which this function should be 0013 % called: ext2int, formulation, int2ext, printpf, savecase 0014 % FCN : the name of the userfcn 0015 % ARGS : (optional) the value to be passed as an argument to the 0016 % userfcn (typically a struct) 0017 % ALLOW_MULTIPLE : (optional) if TRUE, allows the same function to 0018 % be added more than once. 0019 % 0020 % Currently there are 5 different callback stages defined. Each stage has 0021 % a name, and by convention, the name of a user-defined callback function 0022 % ends with the name of the stage. The following is a description of each 0023 % stage, when it is called and the input and output arguments which vary 0024 % depending on the stage. The reserves example (see RUNOPF_W_RES) is used 0025 % to illustrate how these callback userfcns might be used. 0026 % 0027 % 1. ext2int 0028 % 0029 % Called from EXT2INT immediately after the case is converted from 0030 % external to internal indexing. Inputs are a MATPOWER case struct (MPC), 0031 % freshly converted to internal indexing and any (optional) ARGS value 0032 % supplied via ADD_USERFCN. Output is the (presumably updated) MPC. This is 0033 % typically used to reorder any input arguments that may be needed in 0034 % internal ordering by the formulation stage. 0035 % 0036 % E.g. mpc = userfcn_reserves_ext2int(mpc, mpopt, args) 0037 % 0038 % 2. formulation 0039 % 0040 % Called from OPF after the OPF Model (OM) object has been initialized 0041 % with the standard OPF formulation, but before calling the solver. Inputs 0042 % are the OM object and any (optional) ARGS supplied via ADD_USERFCN. 0043 % Output is the OM object. This is the ideal place to add any additional 0044 % vars, constraints or costs to the OPF formulation. 0045 % 0046 % E.g. om = userfcn_reserves_formulation(om, mpopt, args) 0047 % 0048 % 3. int2ext 0049 % 0050 % Called from INT2EXT immediately before the resulting case is converted 0051 % from internal back to external indexing. Inputs are the RESULTS struct 0052 % and any (optional) ARGS supplied via ADD_USERFCN. Output is the RESULTS 0053 % struct. This is typically used to convert any results to external 0054 % indexing and populate any corresponding fields in the RESULTS struct. 0055 % 0056 % E.g. results = userfcn_reserves_int2ext(results, mpopt, args) 0057 % 0058 % 4. printpf 0059 % 0060 % Called from PRINTPF after the pretty-printing of the standard OPF 0061 % output. Inputs are the RESULTS struct, the file descriptor to write to, 0062 % a MATPOWER options struct, and any (optional) ARGS supplied via 0063 % ADD_USERFCN. Output is the RESULTS struct. This is typically used for 0064 % any additional pretty-printing of results. 0065 % 0066 % E.g. results = userfcn_reserves_printpf(results, fd, mpopt, args) 0067 % 0068 % 5. savecase 0069 % 0070 % Called from SAVECASE when saving a case struct to an M-file after 0071 % printing all of the other data to the file. Inputs are the case struct, 0072 % the file descriptor to write to, the variable prefix (typically 'mpc.') 0073 % and any (optional) ARGS supplied via ADD_USERFCN. Output is the case 0074 % struct. This is typically used to write any non-standard case struct 0075 % fields to the case file. 0076 % 0077 % E.g. mpc = userfcn_reserves_printpf(mpc, fd, prefix, args) 0078 % 0079 % See also RUN_USERFCN, REMOVE_USERFCN, TOGGLE_RESERVES, TOGGLE_IFLIMS, 0080 % TOGGLE_DCLINE, TOGGLE_SOFTLIMS, and RUNOPF_W_RES. 0081 0082 % MATPOWER 0083 % Copyright (c) 2009-2018, Power Systems Engineering Research Center (PSERC) 0084 % by Ray Zimmerman, PSERC Cornell 0085 % 0086 % This file is part of MATPOWER. 0087 % Covered by the 3-clause BSD License (see LICENSE file for details). 0088 % See https://matpower.org for more info. 0089 0090 if nargin < 5 0091 allow_multiple = 0; 0092 if nargin < 4 0093 args = []; 0094 end 0095 end 0096 switch stage 0097 case {'ext2int', 'formulation', 'int2ext', 'printpf', 'savecase'} 0098 %% ok 0099 otherwise 0100 error('add_userfcn : ''%s'' is not the name of a valid callback stage\n', stage); 0101 end 0102 0103 n = 1; 0104 if isfield(mpc, 'userfcn') 0105 if isfield(mpc.userfcn, stage) 0106 n = length(mpc.userfcn.(stage)) + 1; 0107 if ~allow_multiple 0108 if have_feature('octave') 0109 fcn_info = functions(fcn); 0110 for k = 1:n-1 0111 cb_info = functions(mpc.userfcn.(stage)(k).fcn); 0112 if strcmp(cb_info.function, fcn_info.function) 0113 error('add_userfcn: the function ''%s'' has already been added', func2str(fcn)); 0114 end 0115 end 0116 else 0117 for k = 1:n-1 0118 if isequal(mpc.userfcn.(stage)(k).fcn, fcn) 0119 error('add_userfcn: the function ''%s'' has already been added', func2str(fcn)); 0120 end 0121 end 0122 end 0123 end 0124 end 0125 end 0126 0127 mpc.userfcn.(stage)(n).fcn = fcn; 0128 if ~isempty(args) 0129 mpc.userfcn.(stage)(n).args = args; 0130 end