├── .gitignore ├── LICENSE ├── PyPSASP ├── PSASPClasses │ ├── Executors.py │ ├── LoadFlow.py │ ├── Manipulators.py │ ├── PSASP.py │ └── __init__.py ├── __init__.py ├── constants │ ├── __init__.py │ └── const.py ├── tests │ ├── __init__.py │ ├── read_data_temp.py │ └── temp_alter_lf_output.py └── utils │ ├── __init__.py │ ├── utils_PSASP.py │ ├── utils_gadgets.py │ └── utils_sqlite.py ├── README.md ├── setup.py └── temp_CCT.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | __pycache__ 3 | tests 4 | PyPSASP.egg-info 5 | dist 6 | build -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 The Python Packaging Authority 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /PyPSASP/PSASPClasses/Executors.py: -------------------------------------------------------------------------------- 1 | import os 2 | from threading import Thread, Lock 3 | import time 4 | from warnings import warn 5 | from PyPSASP.constants import const 6 | from PyPSASP.utils.utils_gadgets import get_all_process,delete_files_pattern,generate_new_files_save_yield 7 | 8 | # TempBat = 'temp.bat' 9 | Lock_GetProcess = Lock() 10 | Lock_WriteBat = Lock() 11 | MAX_TRY = 60 12 | WAIT_TIME = 0.2 13 | MAX_TRY_GETINC = 10 14 | 15 | 16 | class executor_PSASP(object): 17 | def __init__(self, path_exe, path_env=None, path_flagfile=None, patterns_del=None, window_hide=None): 18 | if isinstance(path_flagfile,str): 19 | path_flagfiles = [path_flagfile] 20 | elif isinstance(path_flagfile,tuple) or isinstance(path_flagfile,list): 21 | path_flagfiles = list(path_flagfile).copy() 22 | else: 23 | path_flagfiles = None 24 | 25 | if isinstance(path_flagfiles,list): 26 | for hh in range(len(path_flagfiles)): 27 | pft = path_flagfiles[hh] 28 | if isinstance(pft, str): 29 | path_t, flag_t = os.path.split(pft) 30 | if not path_t: 31 | path_flagfiles[hh] = os.path.join(path_env, pft) 32 | if not os.path.isfile(path_exe): 33 | path_t, exe_t = os.path.split(path_exe) 34 | path_exe = os.path.join(path_env, exe_t) 35 | if not os.path.isfile(path_exe): 36 | # raise ValueError('%s not exist' % path_exe) 37 | warn('%s not exist' % path_exe) 38 | self.__path_exe = path_exe 39 | self.__path_env = path_env 40 | self.__path_flagfiles = path_flagfiles 41 | self.__current_process = [] 42 | self.__process_inc_matched = [] 43 | self.__flagfile_last_update_times = {} 44 | self.__patterns_del = patterns_del 45 | self.__window_hide = window_hide 46 | p,self.__exe_name = os.path.split(self.__path_exe) 47 | 48 | def __update_process(self): 49 | self.__current_process = get_all_process() 50 | 51 | def __get_process_inc(self): 52 | process_new = get_all_process() 53 | pid_old = [x['pid'] for x in self.__current_process] 54 | process_inc = [x for x in process_new if x['pid'] not in pid_old] 55 | return process_inc 56 | 57 | def __get_process_inc_matched(self, wait_time=0.5, max_try=MAX_TRY_GETINC): 58 | process_inc_matched = [] 59 | count = 0 60 | flag_found = False 61 | while count <= max_try: 62 | process_inc = self.__get_process_inc() 63 | process_inc_matched = [x for x in process_inc if x['exe'] == self.__path_exe] 64 | if not process_inc_matched: 65 | time.sleep(wait_time) 66 | else: 67 | flag_found = True 68 | break 69 | count += 1 70 | if flag_found: 71 | pid = process_inc_matched[0]['pid'] 72 | self.__hide_window(pid) 73 | return process_inc_matched 74 | 75 | def __update_mtime_flagfile(self): 76 | if isinstance(self.__path_flagfiles,list): 77 | for pft in self.__path_flagfiles: 78 | if isinstance(pft,str): 79 | if os.path.isfile(pft): 80 | self.__flagfile_last_update_times[pft] = get_updated_time(pft) 81 | else: 82 | self.__flagfile_last_update_times[pft] = None 83 | 84 | def __kill_process_while_flag(self, flag_kill_by_name=True, wait_time=WAIT_TIME, max_try=MAX_TRY): 85 | count = 0 86 | while count <= max_try: 87 | flag_kill = False 88 | if isinstance(self.__path_flagfiles, list): 89 | if self.__flagfile_last_update_times: 90 | for pft in self.__path_flagfiles: 91 | if isinstance(pft,str) and os.path.isfile(pft): 92 | if pft in self.__flagfile_last_update_times.keys(): 93 | tn = get_updated_time(pft) 94 | if tn is not None: 95 | told = self.__flagfile_last_update_times[pft] 96 | if (told is None) or (tn > told): 97 | flag_kill = True 98 | break 99 | else: 100 | break 101 | if flag_kill: 102 | break 103 | time.sleep(wait_time) 104 | count += 1 105 | if count >= max_try: 106 | print('maximum try reached') 107 | if flag_kill_by_name: 108 | os.system(r'taskkill /f /im %s -t' % self.__exe_name) 109 | else: 110 | for process_t in self.__process_inc_matched: 111 | os.system(r'taskkill /pid %d -t -f' % process_t['pid']) 112 | # print('process killed') 113 | 114 | def __delete_files_with_pattern(self): 115 | if self.__path_env: 116 | for pattern_t in self.__patterns_del: 117 | delete_files_pattern(self.__path_env, pattern_t) 118 | 119 | def __hide_window(self, pid): 120 | if isinstance(self.__window_hide, str): 121 | pass 122 | # TODO: how to hide window more elegantly? 123 | #hide_window_by_name(self.__hide_window) 124 | 125 | def execute_exe(self,flag_kill_by_name=True): 126 | temp_bat = None 127 | if self.__path_env: 128 | self.__delete_files_with_pattern() 129 | idx_drive_t = str.find(self.__path_env, ':') 130 | if idx_drive_t == -1: 131 | raise ValueError('Drive not found') 132 | drive_t = self.__path_env[:idx_drive_t + 1] 133 | temp_bat = next(generate_new_files_save_yield('tempBats', 'temp', '.bat')) 134 | if not os.path.isdir('tempBats'): 135 | os.makedirs('tempBats') 136 | with open(temp_bat, 'w') as f: 137 | f.write('%s\n' % drive_t) 138 | f.write('cd "%s"\n' % self.__path_env) 139 | f.write('"%s"' % self.__path_exe) 140 | exe_t = temp_bat 141 | else: 142 | exe_t = self.__path_exe 143 | if not self.__path_flagfiles: 144 | r = os.system(exe_t) 145 | else: 146 | r = None 147 | thread_exe = Thread(target=os.system, args=(exe_t,)) 148 | thread_kill = Thread(target=self.__kill_process_while_flag, args={'flag_kill_by_name':flag_kill_by_name}) 149 | if Lock_GetProcess.acquire(): 150 | if not flag_kill_by_name: 151 | self.__update_process() 152 | self.__update_mtime_flagfile() 153 | thread_exe.start() 154 | if not flag_kill_by_name: 155 | self.__process_inc_matched = self.__get_process_inc_matched() 156 | Lock_GetProcess.release() 157 | thread_kill.start() 158 | thread_kill.join() 159 | thread_exe.join() 160 | if temp_bat: 161 | try: 162 | os.remove(temp_bat) 163 | except: 164 | print('error while removing %s' % temp_bat) 165 | return r 166 | 167 | 168 | class executor_PSASP_lf(executor_PSASP): 169 | def __init__(self, path_exe, path_env): 170 | flag_file_lf = const.dict_mapping_files[const.LABEL_LF][const.LABEL_RESULTS][const.LABEL_CONF] 171 | executor_PSASP.__init__(self, path_exe, path_env, patterns_del=(flag_file_lf,)) 172 | 173 | 174 | class executor_PSASP_st(executor_PSASP): 175 | def __init__(self, path_exe, path_env): 176 | flag_file_st = const.dict_mapping_files[const.LABEL_ST][const.LABEL_RESULTS][const.LABEL_CONF] 177 | patterns_del = (const.PATTERN_OUTPUT_ST, flag_file_st) 178 | executor_PSASP.__init__(self, path_exe, path_env, flag_file_st, patterns_del, 179 | window_hide=const.WINDOW_NAME_ST) 180 | 181 | 182 | class executor_PSASP_sstlin(executor_PSASP): 183 | def __init__(self, path_exe, path_env): 184 | flag_file_sst_lin = const.dict_mapping_files[const.LABEL_SST_LIN][const.LABEL_RESULTS][const.LABEL_CONF] 185 | patterns_del = (flag_file_sst_lin,) 186 | executor_PSASP.__init__(self, path_exe, path_env, path_flagfile=None, patterns_del=patterns_del) 187 | 188 | 189 | class executor_PSASP_ssteig(executor_PSASP): 190 | def __init__(self, path_exe, path_env): 191 | flag_file_sst_eig = const.dict_mapping_files[const.LABEL_SST_EIG][const.LABEL_RESULTS][const.LABEL_CONF] 192 | patterns_del = (flag_file_sst_eig,) 193 | executor_PSASP.__init__(self, path_exe, path_env, flag_file_sst_eig, patterns_del) 194 | 195 | 196 | def get_updated_time(file_path_t): 197 | if os.path.isfile(file_path_t): 198 | return os.path.getmtime(file_path_t) 199 | else: 200 | return None 201 | 202 | 203 | if __name__ == '__main__': 204 | tn = get_updated_time(r'G:\CCT_10m39b\IEEE10m39b\PSASP\Temp_AVR_GOV_PSS_topoChange\STERR.LIS') 205 | print(tn) 206 | # path_exe = r'E:\05_Resources\Softwares\PSASP\CriticalFiles_60000\WMLFRTMsg.exe' 207 | # path_env = r'E:\01_Research\98_Data\SmallSystem_PSASP\Temp_20190422_MinInputs' 208 | path_exe_st = r'G:\CCT_10m39b\IEEE10m39b\PSASP\Temp_AVR_GOV_PSS_topoChange\wmudrt.exe' 209 | path_exe_lf = r'G:\CCT_10m39b\IEEE10m39b\PSASP\Temp_AVR_GOV_PSS_topoChange\WMLFRTMsg.exe' 210 | path_env = r'G:\CCT_10m39b\IEEE10m39b\PSASP\Temp_AVR_GOV_PSS_topoChange' 211 | et0 = executor_PSASP_st(path_exe_st, path_env) 212 | et1 = executor_PSASP_lf(path_exe_lf,path_env) 213 | et1.execute_exe() 214 | et0.execute_exe() 215 | pass 216 | # path_exe = r'E:\05_Resources\Softwares\PSASP\CriticalFiles\Wsstlin.exe' 217 | # path_exe_sstlin = r'E:\CNN\PSASP_SST\Temp2\Wsstlin.exe' 218 | # path_exe_ssteig = r'E:\CNN\PSASP_SST\Temp2\Wssteig.exe' 219 | # path_env = r'E:\CNN\PSASP_SST\Temp2' 220 | # 221 | # et1 = executor_PSASP_sstlin(path_exe_sstlin, path_env) 222 | # et2 = executor_PSASP_ssteig(path_exe_ssteig, path_env) 223 | # et1.execute_exe() 224 | # et2.execute_exe() 225 | -------------------------------------------------------------------------------- /PyPSASP/PSASPClasses/LoadFlow.py: -------------------------------------------------------------------------------- 1 | from PyPSASP.constants import const 2 | import math 3 | import numpy as np 4 | 5 | 6 | def get_c_voltage(v_amp, v_deg): 7 | v_rad = math.radians(v_deg) 8 | v_real = v_amp * math.cos(v_rad) 9 | v_image = v_amp * math.sin(v_rad) 10 | return v_real, v_image 11 | 12 | 13 | def convert_bus_voltage_single(dict_bus): 14 | v_real, v_image = get_c_voltage(dict_bus[const.VoltageKey], dict_bus[const.AngleKey]) 15 | dict_bus[const.VoltageRealKey] = v_real 16 | dict_bus[const.VoltageImageKey] = v_image 17 | return dict_bus 18 | 19 | 20 | def expand_lf(lf): 21 | lf_e = {(k, tuple([lf[k][hh][no] for no in dict_map_type2no[k]]), a): lf[k][hh][a] 22 | for k in dict_map_type2no.keys() for hh in range((len(lf[k]) if lf[k] else 0)) 23 | for a in dict_map_type2attr[k]} 24 | return lf_e 25 | 26 | 27 | dict_map_type2no = {const.LABEL_BUS: (const.BusNoKey,), 28 | const.LABEL_ACLINE: (const.INoKey, const.JNoKey, const.IDNoKey), 29 | const.LABEL_TRANSFORMER: (const.INoKey, const.JNoKey, const.IDNoKey), 30 | const.LABEL_DCLINE: (const.INoKey, const.JNoKey, const.IDNoKey), 31 | const.LABEL_GENERATOR: (const.BusNoKey,), 32 | const.LABEL_LOAD: (const.BusNoKey, const.LoadNoKey), 33 | } 34 | 35 | dict_map_type2attr = { 36 | const.LABEL_BUS: (const.VoltageRealKey, const.VoltageImageKey), 37 | const.LABEL_ACLINE: (const.PiKey, const.QiKey, const.PjKey, const.QjKey, const.ACLineQciKey, const.ACLineQcjKey), 38 | const.LABEL_TRANSFORMER: (const.PiKey, const.QiKey, const.PjKey, const.QjKey), 39 | const.LABEL_DCLINE: (const.PiKey, const.QiKey), 40 | const.LABEL_GENERATOR: (const.GenPgKey, const.GenQgKey), 41 | const.LABEL_LOAD: (const.LoadPlKey, const.LoadQlKey), 42 | } 43 | 44 | 45 | class LoadFlow(object): 46 | def __init__(self, dict_lf, bus_voltage_c_appended=False): 47 | self.__bus_volatage_c_appended = bus_voltage_c_appended 48 | self.__dict_lf = dict_lf 49 | self.__dict_lf_expanded = None 50 | 51 | @property 52 | def dict_lf(self): 53 | return self.__dict_lf 54 | 55 | @property 56 | def dict_lf_expanded(self): 57 | if not self.__dict_lf_expanded: 58 | self.expand_dict_lf() 59 | return self.__dict_lf_expanded 60 | 61 | @dict_lf.setter 62 | def dict_lf(self, value): 63 | self.__dict_lf = value 64 | 65 | def append_bus_voltage_c(self): 66 | data_buses = self.__dict_lf[const.LABEL_BUS] 67 | data_buses_a = [convert_bus_voltage_single(x) for x in data_buses] 68 | self.__dict_lf[const.LABEL_BUS] = data_buses_a 69 | 70 | def expand_dict_lf(self): 71 | if not self.__bus_volatage_c_appended: 72 | self.append_bus_voltage_c() 73 | self.__dict_lf_expanded = expand_lf(self.__dict_lf) 74 | return self.dict_lf_expanded 75 | 76 | 77 | class GridGraph: 78 | def __init__(self, lfs=None, lfr=None): 79 | self._lfs = lfs 80 | self._lfr = lfr 81 | self._bus_names = None 82 | self._bus_names_r = None 83 | self._Y = None 84 | self._flow = None 85 | self._injections = None 86 | 87 | @property 88 | def lfs(self): 89 | return self._lfs 90 | 91 | @property 92 | def lfr(self): 93 | return self._lfr 94 | 95 | @lfs.setter 96 | def lfs(self, lfs): 97 | self._lfs = lfs 98 | 99 | @lfr.setter 100 | def lfr(self, lfr): 101 | self._lfr = lfr 102 | 103 | def get_bus_names(self, reget=False): 104 | if (self._bus_names is None) or (self._bus_names_r is None) or reget: 105 | buses = self._lfs[const.LABEL_BUS] 106 | buses_r_no = [b[const.BusNoKey] for b in self._lfr[const.LABEL_BUS]] 107 | self._bus_names = [b[const.BusNameKey] for b in buses] 108 | self._bus_names_r = [self._bus_names[hh] for hh in buses_r_no] 109 | return self._bus_names, self._bus_names_r 110 | 111 | def get_Y(self, reget=False): 112 | if (self._Y is None) or reget: 113 | buses = self._lfs[const.LABEL_BUS] 114 | NB = len(buses) 115 | Y = np.zeros([NB, NB], dtype=np.complex128) 116 | for al in self._lfs[const.LABEL_ACLINE]: 117 | mark = al[const.MarkKey] 118 | if mark != 0: 119 | i_idx = al[const.INoKey] - 1 120 | j_idx = al[const.JNoKey] - 1 121 | xt = al[const.R1Key] + al[const.X1Key] * 1j 122 | jhalfb = al[const.ACLineHalfB1Key] * 1j 123 | if mark == 1: 124 | yt = 1 / xt 125 | Y[[i_idx, j_idx, i_idx, j_idx], [j_idx, i_idx, i_idx, j_idx]] += [-yt, -yt, yt + jhalfb, 126 | yt + jhalfb] 127 | else: 128 | if mark == 2: 129 | idx_t = j_idx 130 | elif mark == 3: 131 | idx_t = i_idx 132 | else: 133 | continue 134 | yt = 1 / (xt + 1 / jhalfb) 135 | Y[idx_t, idx_t] += yt + jhalfb 136 | for tr in self._lfs[const.LABEL_TRANSFORMER]: 137 | mark = tr[const.MarkKey] 138 | if mark != 0: 139 | i_idx = abs(tr[const.INoKey]) - 1 140 | j_idx = abs(tr[const.JNoKey]) - 1 141 | xt = tr[const.R1Key] + tr[const.X1Key] * 1j 142 | tkc = tr[const.TransTkKey] * np.exp(np.radians(tr[const.TransShiftAngKey]) * 1j) 143 | zm = tr[const.TransRmKey] + tr[const.TransXmKey] * 1j 144 | yt = 1 / xt 145 | if zm == 0: 146 | ym = zm 147 | else: 148 | ym = 1 / zm 149 | sij = -1 / tkc 150 | sji = np.conj(sij) 151 | sjj = sij * sji 152 | Y[[i_idx, j_idx, i_idx, j_idx], [i_idx, j_idx, j_idx, i_idx]] += np.array([1, sjj, sij, sji]) * yt 153 | Y[i_idx, i_idx] += ym 154 | self._Y = Y 155 | return self._Y 156 | 157 | def get_flow(self, reget=False): 158 | if (self._flow is None) or reget: 159 | Y = self.get_Y(reget) 160 | bus_r = self._lfr[const.LABEL_BUS] 161 | bus_r_idx = np.array([b[const.BusNoKey] for b in bus_r]) - 1 162 | idx_valid = np.argwhere(bus_r_idx>=0).flatten() 163 | bus_r_idx_valid = bus_r_idx[idx_valid] 164 | bus_r_valid = [bus_r[i] for i in idx_valid] 165 | bus_r_v_amp = np.array([b[const.VoltageKey] for b in bus_r_valid]) 166 | bus_r_v_ang = np.array([b[const.AngleKey] for b in bus_r_valid]) 167 | bus_r_v = bus_r_v_amp * np.exp(np.radians(bus_r_v_ang) * 1j) 168 | Y_r = Y[bus_r_idx_valid, :][:, bus_r_idx_valid] 169 | in_currents_r = np.matmul(Y_r, bus_r_v) 170 | flow_r = bus_r_v * np.conj(in_currents_r) 171 | self._flow = np.zeros(len(self._lfs[const.LABEL_BUS]), dtype=np.complex128) 172 | self._flow[bus_r_idx_valid] = flow_r 173 | return self._flow 174 | 175 | def get_injections(self, reget=False): 176 | if (self._injections is None) or reget: 177 | self._injections = np.zeros(len(self._lfs[const.LABEL_BUS]), dtype=np.complex128) 178 | for g in self._lfr[const.LABEL_GENERATOR]: 179 | self._injections[g[const.BusNoKey]-1] += g[const.GenPgKey] + g[const.GenQgKey] * 1j 180 | for l in self._lfr[const.LABEL_LOAD]: 181 | self._injections[l[const.BusNoKey]-1] -= l[const.LoadPlKey] + l[const.LoadQlKey] * 1j 182 | if self._lfr[const.LABEL_DCLINE]: 183 | for dl in self._lfr[const.LABEL_DCLINE]: 184 | i_idx = dl[const.INoKey] - 1 185 | j_idx = dl[const.JNoKey] - 1 186 | Sdi = (dl[const.DCLinePd1iKey] + dl[const.DCLinePd2iKey]) + \ 187 | 1j * (dl[const.DCLineQd1iKey] + dl[const.DCLineQd2iKey] + dl[const.DCLineQcipKey]) 188 | Sdj = (dl[const.DCLinePd1jKey] + dl[const.DCLinePd2jKey]) + \ 189 | 1j * (dl[const.DCLineQd1jKey] + dl[const.DCLineQd2jKey] + dl[const.DCLineQcjpKey]) 190 | self._injections[i_idx] += Sdi 191 | self._injections[j_idx] += Sdj 192 | return self._injections 193 | -------------------------------------------------------------------------------- /PyPSASP/PSASPClasses/Manipulators.py: -------------------------------------------------------------------------------- 1 | import re 2 | import os 3 | from PyPSASP.utils import utils_gadgets 4 | from PyPSASP.constants import const 5 | from PyPSASP.utils.utils_PSASP import reshape_pos_keys 6 | 7 | 8 | # import numpy as np 9 | 10 | class PSASP_Converter(object): 11 | def __init__(self): 12 | pass 13 | 14 | def convert_get2list(self, dict_get): 15 | list_get = [] 16 | for ele_type, list_ele in dict_get.items(): 17 | if list_ele: 18 | for hh in range(len(list_ele)): 19 | ele = list_ele[hh] 20 | list_get_t = [{const.EleTypeKey: ele_type, const.EleIdKey: hh, const.EleAttrNameKey: k, 21 | const.EleAttrValueKey: v} for k, v in ele.items()] 22 | list_get.extend(list_get_t) 23 | return list_get 24 | 25 | def convert_get2dict(self, list_get): 26 | eleTypes = set([x[const.EleTypeKey] for x in list_get]) 27 | idmax = {k: max([x[const.EleIdKey] for x in list_get if x[const.EleTypeKey] == k]) for k in eleTypes} 28 | dict_get = {k: [dict()] * (v + 1) for k, v in idmax.items()} 29 | for get_t in list_get: 30 | dict_get[get_t[const.EleTypeKey]][get_t[const.EleIdKey]][get_t[const.EleAttrNameKey]] = get_t[ 31 | const.EleAttrValueKey] 32 | return dict_get 33 | 34 | 35 | 36 | class PSASP_Parser(object): 37 | def __init__(self, path_temp=''): 38 | self.__path_temp = path_temp 39 | 40 | def parse_lines_PSASP(self, lines, pos_keys, pattern_parse=const.Pattern_read, key_busno=None): 41 | lines_t = lines.copy() 42 | if str.find(lines_t[0], const.CreatedOnPattern) != -1: 43 | list.pop(lines_t, 0) 44 | 45 | pos_keys_multiline = reshape_pos_keys(pos_keys) 46 | list_dict_parsed = [] 47 | append_no = isinstance(key_busno, str) 48 | count_t = 0 49 | count_num = 0 50 | dict_t = {} 51 | for h in range(len(lines_t)): 52 | line_t = lines_t[h] 53 | if isinstance(line_t,str): 54 | line_t = line_t.strip() 55 | pos_keys_t = pos_keys_multiline[count_t] 56 | if isinstance(line_t, str): 57 | contents = re.findall(pattern_parse, line_t) 58 | if not contents and len(pos_keys_t) == 1: 59 | contents = [line_t] 60 | if contents: 61 | dict_t_part = {pos_keys_t[hh]: utils_gadgets.convert_s(contents[hh]) for hh in 62 | range(min([len(contents), len(pos_keys_t)]))} 63 | dict_t.update(dict_t_part) 64 | if append_no: 65 | dict_t[key_busno] = count_num + 1 66 | count_t += 1 67 | count_num += 1 68 | if count_t >= len(pos_keys_multiline): 69 | list_dict_parsed.append(dict_t) 70 | dict_t = dict() 71 | count_t = 0 72 | return list_dict_parsed 73 | 74 | def parse_single_s(self, label_calType, label_getType, label_eleType): 75 | fnt = const.dict_mapping_files[label_calType][label_getType][label_eleType] 76 | fpt = os.path.join(self.__path_temp, fnt) 77 | if os.path.isfile(fpt): 78 | with open(fpt, 'r') as f: 79 | lines_raw = f.readlines() 80 | lines = [x.strip() for x in lines_raw] 81 | if lines: 82 | dmpk = const.dict_mapping_pos_keys 83 | if label_calType in dmpk.keys(): 84 | dmpk_sub_1 = dmpk[label_calType] 85 | if label_getType in dmpk_sub_1.keys(): 86 | dmpk_sub_2 = dmpk_sub_1[label_getType] 87 | if label_eleType in dmpk_sub_2.keys(): 88 | pos_keys = dmpk_sub_2[label_eleType] 89 | if fnt in const.files_lf_append_no: 90 | key_busno = const.BusNoKey 91 | else: 92 | key_busno = None 93 | list_dict_parsed = self.parse_lines_PSASP(lines, pos_keys, key_busno=key_busno) 94 | return list_dict_parsed 95 | 96 | def parse_single_s_lfs(self, label_eleType): 97 | """Parse single file of load flow settings (LF.L*)""" 98 | return self.parse_single_s(const.LABEL_LF, const.LABEL_SETTINGS, label_eleType) 99 | 100 | def parse_single_s_lfr(self, label_eleType): 101 | """Parse single file of load flow results (LF.LP*)""" 102 | return self.parse_single_s(const.LABEL_LF, const.LABEL_RESULTS, label_eleType) 103 | 104 | def parse_single_s_sts(self, label_eleType): 105 | """Parse single file of transient stability settings (ST.S*)""" 106 | return self.parse_single_s(const.LABEL_ST, const.LABEL_SETTINGS, label_eleType) 107 | 108 | def parse_single_s_str(self, label_eleType): 109 | """Parse single file of transient stability results (ST.CAL, STANA.DAT)""" 110 | return self.parse_single_s(const.LABEL_ST, const.LABEL_RESULTS, label_eleType) 111 | 112 | def parse_all_files_s(self, label_calType, label_getType, label_eles_do=None): 113 | """Parse all files of a given calculation-type and get-type""" 114 | dict_files = const.dict_mapping_files[label_calType][label_getType] 115 | # dict_pos_keys = const.dict_mapping_pos_keys[label_calType][label_getType] 116 | labels_do_ori = list(dict_files.keys()) 117 | flag_single = False 118 | if label_eles_do is not None: 119 | if isinstance(label_eles_do, str): 120 | flag_single = True 121 | label_eles_do = [label_eles_do] 122 | label_eles_do = set(labels_do_ori).intersection(set(label_eles_do)) 123 | else: 124 | label_eles_do = labels_do_ori 125 | dict_r = {k: self.parse_single_s(label_calType, label_getType, k) for k in label_eles_do} 126 | if flag_single and len(dict_r) == 1: 127 | dict_r = list(dict_r.values())[0] 128 | return dict_r 129 | 130 | def parse_all_calType(self,label_calType,labels_getType=None,labels_do=None): 131 | """Parse all files of a given calculation-type""" 132 | if labels_getType is None: 133 | lgt = tuple(const.dict_mapping_pos_keys.keys()) 134 | else: 135 | lgt = tuple(set(labels_getType).intersection(const.dict_mapping_pos_keys[label_calType].keys())) 136 | dict_t = {k:self.parse_all_files_s(label_calType,k,labels_do) for k in lgt} 137 | return dict_t 138 | 139 | def parse_all_lf_sr(self,labels_getType=None,labels_do=None): 140 | """Parse all load flow files of given get-types""" 141 | return self.parse_all_calType(const.LABEL_LF,labels_getType,labels_do) 142 | 143 | def parse_all_lf(self, label_getType, labels_do=None): 144 | """Parse all load flow files of a given get-types""" 145 | return self.parse_all_files_s(const.LABEL_LF, label_getType, labels_do) 146 | 147 | def parse_all_settings_lf(self, labels_do=None): 148 | """Parse all setting files of load flow""" 149 | return self.parse_all_lf(const.LABEL_SETTINGS, labels_do) 150 | 151 | def parse_all_results_lf(self, labels_do=None): 152 | """Parse all result files of load flow""" 153 | return self.parse_all_lf(const.LABEL_RESULTS, labels_do) 154 | 155 | def parse_all_settings_st(self, labels_do=None): 156 | """arse all setting files of transient stability""" 157 | return self.parse_all_files_s(const.LABEL_ST, const.LABEL_SETTINGS, labels_do) 158 | 159 | def parse_output_st_varinfs(self, path_STOUT=None): 160 | """Parse output meanings of transient stability""" 161 | if path_STOUT is None: 162 | path_STOUT = self.__path_temp 163 | list_desc_outputs = [] 164 | 165 | data_STOUT = self.import_STOUT(path_STOUT) 166 | type_t = 0 167 | for hh in range(len(data_STOUT)): 168 | data_piece = data_STOUT[hh] 169 | num_var_t = None 170 | vec_subtype_t = None 171 | no_var_t = None 172 | if data_piece[0] == 0: 173 | continue 174 | typett = data_piece[1] 175 | if typett in [8, 11]: 176 | continue 177 | if typett != 99: 178 | type_t = typett 179 | if type_t in [1, 2, 12]: 180 | if type_t in [1, 12]: 181 | num_pu = 2 182 | else: 183 | num_pu = 1 184 | vec_no_t = [x for x in data_piece[2:] if x != 0] 185 | num_var_t = round(len(vec_no_t) / num_pu) 186 | no_var_t = [[vec_no_t[ii * num_pu + jj] for jj in range(num_pu)] for ii in range(num_var_t)] 187 | vec_subtype_t = [0] * num_var_t 188 | elif type_t in [3, 4, 5, 6, 7, 13, 14]: 189 | if type_t in [3, 5]: 190 | point_start_t = 3 191 | elif type_t in [6, 7, 13]: 192 | point_start_t = 5 193 | else: 194 | point_start_t = 4 195 | # num_no_t = point_start_t-2 196 | vec_subtype_t = [x for x in data_piece[point_start_t:] if x != 0] 197 | num_var_t = len(vec_subtype_t) 198 | vec_no_t = data_piece[2:point_start_t] 199 | no_var_t = [vec_no_t] * num_var_t 200 | if all([x is not None for x in [num_var_t, vec_subtype_t, no_var_t]]): 201 | for hh in range(num_var_t): 202 | dict_t = {} 203 | dict_t[const.OutputKeyType] = type_t 204 | dict_t[const.OutputKeySubType] = vec_subtype_t[hh] 205 | dict_t[const.OutputKeyNoDesc] = no_var_t[hh] 206 | list_desc_outputs.append(dict_t) 207 | return list_desc_outputs 208 | 209 | def get_output_data_raw(self): 210 | count_t = 1 211 | data_FN = [] 212 | while True: 213 | FNt = os.path.join(self.__path_temp, const.FILE_TEMPLATE_OUTPUT_ST.format(count_t)) 214 | if os.path.isfile(FNt): 215 | with open(FNt, 'r') as f: 216 | data_raw = f.readlines() 217 | data_FN_rowwise_t = [[float(xx) for xx in x.strip().split(',') if xx] for x in data_raw] 218 | data_FN_colwise_t = [[k[hh] for k in data_FN_rowwise_t] for hh in range(len(data_FN_rowwise_t[0]))] 219 | data_FN.extend(data_FN_colwise_t) 220 | count_t += 1 221 | else: 222 | break 223 | return data_FN 224 | 225 | # TODO: consider the situation where we enable Lstop (stop simulation instantly at the unstable moment) ? 226 | def get_sim_time(self): 227 | dict_conf_ST = self.parse_single_s(const.LABEL_ST, const.LABEL_SETTINGS, const.LABEL_CONF)[0] 228 | Ttotal = dict_conf_ST[const.STTTotalKey] 229 | DT = dict_conf_ST[const.STDTKey] 230 | NT = round(Ttotal / DT) + 1 231 | list_t = [x * DT for x in range(NT)] 232 | return list_t 233 | 234 | def parse_output_st(self): 235 | data_raw = self.get_output_data_raw() 236 | # data_raw = import_STOUT(path_temp) 237 | list_desc_outputs = self.parse_output_st_varinfs(self.__path_temp) 238 | list_t = self.get_sim_time() 239 | list_heads = [{const.StOutVarNameKey: const.TimeKey}, 240 | *[dict({const.StOutVarNameKey: const.VarKeyPrefix + str(hh)}, **list_desc_outputs[hh]) for hh in 241 | range(len(list_desc_outputs))]] 242 | list_data_raw_col = [list_t, *data_raw] 243 | LT = len(list_data_raw_col[0]) 244 | list_data_raw_row = [[x[hh] for x in list_data_raw_col] for hh in range(LT)] 245 | # list_values = [dict({const.TimeKey:list_t[hh]},**{const.VarKeyPrefix+str(ll):data_raw[ll][hh] for ll in range(len(data_raw))}) for hh in range(len(list_t))] 246 | 247 | return list_heads, list_data_raw_row 248 | 249 | 250 | def get_gen_angles_st(self): 251 | list_heads, list_data_raw_row = self.parse_output_st() 252 | idx_gen_angle = [hh for hh in range(len(list_heads)) if (const.OutputKeyType in list_heads[hh].keys()) and (list_heads[hh][const.OutputKeyType] == 1)] 253 | gen_angles = [list_data_raw_row[hhh] for hhh in idx_gen_angle] 254 | return gen_angles 255 | 256 | def parse_all_parsable(self): 257 | dict_parsable_all = {} 258 | for label_calType, dict_files_sub_1 in const.dict_mapping_files.items(): 259 | dict_parsable_all[label_calType] = {} 260 | for label_getType, dict_files_sub_2 in dict_files_sub_1.items(): 261 | dict_parsable_all[label_calType][label_getType] = {} 262 | for label_eleType, dict_files_sub_3 in dict_files_sub_2.items(): 263 | dt = self.parse_single_s(label_calType, label_getType, label_eleType) 264 | if dt: 265 | dict_parsable_all[label_calType][label_getType][label_eleType] = dt 266 | return dict_parsable_all 267 | 268 | 269 | def import_STOUT(self, path_STOUT): 270 | data_STOUT = [] 271 | if os.path.isdir(path_STOUT): 272 | path_STOUT = os.path.join(path_STOUT, const.FILE_STOUT) 273 | if os.path.isfile(path_STOUT): 274 | with open(path_STOUT, 'r') as f: 275 | data_raw = f.readlines() 276 | data_STOUT = [[int(xx) for xx in x.strip().split(',') if xx] for x in data_raw] 277 | return data_STOUT 278 | 279 | 280 | 281 | class PSASP_Writer(object): 282 | def __init__(self, path_temp=''): 283 | self.__path_temp = path_temp 284 | 285 | def write_to_file(self, file_path, list_dict_values, pos_keys): 286 | 287 | suppath,fnt = os.path.split(file_path) 288 | if not os.path.isdir(suppath): 289 | os.makedirs(suppath) 290 | 291 | if list_dict_values: 292 | if isinstance(list_dict_values, dict): 293 | list_dict_values = [list_dict_values] 294 | pos_keys_multiline = reshape_pos_keys(pos_keys) 295 | lines_write = [(','.join( 296 | [str(x[pos_keys_t[hh]]) for hh in range(len(pos_keys_t)) if pos_keys_t[hh] in x.keys()]) + ',\n') 297 | for x in list_dict_values if x for pos_keys_t in pos_keys_multiline] 298 | with open(file_path, 'w') as f: 299 | f.writelines(lines_write) 300 | 301 | def write_to_file_s(self, label_calType, label_getType, label_eleType, list_dict_values): 302 | fnt = const.dict_mapping_files[label_calType][label_getType][label_eleType] 303 | fpt = os.path.join(self.__path_temp, fnt) 304 | pos_keys_t = const.dict_mapping_pos_keys[label_calType][label_getType][label_eleType] 305 | self.write_to_file(fpt, list_dict_values, pos_keys_t) 306 | return fpt 307 | 308 | def write_to_file_s_lfs(self, label_eleType, list_dict_values): 309 | return self.write_to_file_s(const.LABEL_LF, const.LABEL_SETTINGS, label_eleType, list_dict_values) 310 | 311 | def write_to_file_s_lfs_autofit(self, list_dict_values): 312 | if list_dict_values: 313 | # TODO: get all possible keys? 314 | keys_t = set(list_dict_values[0].keys()) 315 | dt = const.dict_pos_keys_lf_settings 316 | K_overlap = {k: len(keys_t.intersection(set(utils_gadgets.cat_lists(v) if isinstance(v[0],list) else v))) for k, v in dt.items()} 317 | MK = max(list(K_overlap.values())) 318 | labels_posible = [k for k, v in K_overlap.items() if v == MK] 319 | if len(labels_posible) > 1: 320 | dL = {k: abs(len(keys_t) - len(v)) for k, v in dt.items() if k in labels_posible} 321 | mDL = min(list(dL.values())) 322 | label_ele = [k for k, v in dL.items() if v == mDL][0] 323 | else: 324 | label_ele = labels_posible[0] 325 | return self.write_to_file_s(const.LABEL_LF, const.LABEL_SETTINGS, label_ele, list_dict_values) 326 | 327 | def write_all_writable(self,dict_all): 328 | for label_calType,dict_files_sub_1 in dict_all.items(): 329 | for label_getType,dict_files_sub_2 in dict_files_sub_1.items(): 330 | for label_eleType,dict_files_sub_3 in dict_files_sub_2.items(): 331 | lt = dict_all[label_calType][label_getType][label_eleType] 332 | self.write_to_file_s(label_calType,label_getType,label_eleType,lt) 333 | 334 | 335 | if __name__ == '__main__': 336 | path_temp = r'E:\01_Research\98_Data\SmallSystem_PSASP\SMIB\SMIB_0' 337 | Parser_t = PSASP_Parser(path_temp) 338 | list_heads,list_output = Parser_t.parse_output_st() 339 | pass -------------------------------------------------------------------------------- /PyPSASP/PSASPClasses/PSASP.py: -------------------------------------------------------------------------------- 1 | import os 2 | from PyPSASP.utils.utils_PSASP import copyfiles_st, copyfiles_lf, copyfiles_lfs 3 | from PyPSASP.utils.utils_gadgets import generate_new_files_save_yield, gen_token, formulate_list_of_dicts 4 | from PyPSASP.utils.utils_sqlite import insert_from_list_to_db 5 | from PyPSASP.constants import const 6 | from PyPSASP.PSASPClasses.Executors import executor_PSASP_lf, executor_PSASP_st 7 | from PyPSASP.PSASPClasses.Executors import executor_PSASP_sstlin, executor_PSASP_ssteig 8 | from PyPSASP.PSASPClasses.Manipulators import PSASP_Parser, PSASP_Converter, PSASP_Writer 9 | import random 10 | import time 11 | 12 | PATH_TEMP = r'E:\01_Research\98_Data\SmallSystem_PSASP\Temp_20190419' 13 | PATH_RESOURCES = r'E:\05_Resources\Softwares\PSASP\CriticalFiles_60000' 14 | PATH_OUTPUT = r'F:\Data\Research\PyPSASP\CCT\3m' 15 | 16 | PATH_OUTPUT_DEFAULT = 'CCT_RESULTS' 17 | 18 | OUTPUT_LF_KEY = 'output_lf' 19 | OUTPUT_ST_LEFT_KEY = 'output_st_left' 20 | OUTPUT_ST_RIGHT_KEY = 'output_st_right' 21 | OUTPUT_STANA_LEFT_DB = const.LABEL_ANA + '_left.db' 22 | OUTPUT_STANA_RIGHT_DB = const.LABEL_ANA + '_right.db' 23 | SUCCESS_LF_KEY = 'success_lf' 24 | CCT_KEY = 'CCT' 25 | TMAX_STEP_KEY = 'Tmax' 26 | 27 | T_SIM_KEY = 'Tsim' 28 | EPS_KEY = 'eps' 29 | T_LEFT_KEY = 'tleft' 30 | T_RIGHT_KEY = 'tright' 31 | F_LEFT_KEY = 'fleft' 32 | F_RIGHT_KEY = 'fright' 33 | COUNT_ITER_KEY = 'count' 34 | FLAG_LIMIT_TOUCHED_KEY = 'flag_limit_touched' 35 | ELAPSED_KEY = 'elapsed' 36 | 37 | Tstep_max_default = 0.2 38 | Tsim_default = 5 39 | eps_default = 0.005 40 | 41 | lf_output_prefix = 'lf_' 42 | st_output_prefix = 'st_' 43 | st_output_subfolder_left = 'left' 44 | st_output_subfolder_right = 'right' 45 | 46 | 47 | def func_change_lf_temp(P): 48 | if isinstance(P, PSASP): 49 | gen_ori = P.parser.parse_single_s_lfs(const.LABEL_GENERATOR) 50 | load_ori = P.parser.parse_single_s_lfs(const.LABEL_LOAD) 51 | gen_new = gen_ori.copy() 52 | load_new = load_ori.copy() 53 | Psum = 0 54 | for hh in range(len(gen_new)): 55 | gen_new[hh][const.GenPgKey] = gen_new[hh][const.PmaxKey] * (random.random() * 0.5 + 0.5) 56 | gen_new[hh][const.V0Key] = (random.random() * 0.2 + 0.95) 57 | Psum = Psum + gen_new[hh][const.GenPgKey] 58 | ''' 59 | for key_t in [const.GenPgKey,const.GenQgKey,const.V0Key,const.AngleKey]: 60 | gen_new[hh][key_t] = gen_new[hh][key_t]*(random.random()*0.5+0.5) 61 | ''' 62 | rands_t = [random.random() for hh in range(len(load_new))] 63 | Ap = random.random() * 0.4 + 0.8 64 | Pls_t = [x / sum(rands_t) * Ap * Psum for x in rands_t] 65 | for hh in range(len(load_new)): 66 | load_new[hh][const.LoadPlKey] = Pls_t[hh] 67 | load_new[hh][const.LoadQlKey] = 6 * random.random() 68 | ''' 69 | for key_t in [const.LoadPlKey,const.LoadQlKey,const.V0Key,const.AngleKey]: 70 | load_new[hh][key_t] = load_new[hh][key_t]*(random.random()*0.5+0.5) 71 | ''' 72 | 73 | P.writer.write_to_file_s_lfs_autofit(gen_new) 74 | P.writer.write_to_file_s_lfs_autofit(load_new) 75 | 76 | 77 | def func_change_t_regular(P, t): 78 | if isinstance(P, PSASP): 79 | STS11_ori = P.parser.parse_single_s(const.LABEL_ST, const.LABEL_SETTINGS, const.LABEL_FAULT) 80 | STS11_new = STS11_ori.copy() 81 | STS11_new[0][const.FaultTstartKey] = 0 82 | STS11_new[1][const.FaultTstartKey] = t 83 | STS11_new[2][const.FaultTstartKey] = t + 0.01 84 | P.writer.write_to_file_s(const.LABEL_ST, const.LABEL_SETTINGS, const.LABEL_FAULT, STS11_new) 85 | 86 | 87 | def func_judge_stable_regular(P): 88 | F = None 89 | if isinstance(P, PSASP): 90 | STCAL = P.parser.parse_single_s(const.LABEL_ST, const.LABEL_RESULTS, const.LABEL_CONF) 91 | if STCAL: 92 | STCAL = STCAL[0] 93 | F = STCAL[const.STIsStableKey] == 1 94 | return F 95 | 96 | 97 | class PSASP(object): 98 | def __init__(self, path_temp, path_resources=None): 99 | self.path_temp = path_temp 100 | if path_resources is None: 101 | self.path_resources = path_temp 102 | else: 103 | self.path_resources = path_resources 104 | self.__lfs = None 105 | 106 | @property 107 | def path_temp(self): 108 | return self.__path_temp 109 | 110 | @property 111 | def path_resources(self): 112 | return self.__path_resources 113 | 114 | @path_temp.setter 115 | def path_temp(self, value): 116 | if not isinstance(value, str): 117 | raise ValueError('path_temp must be a string!') 118 | elif not os.path.isdir(value): 119 | os.makedirs(value) 120 | self.__path_temp = value 121 | self.parser = PSASP_Parser(value) 122 | self.writer = PSASP_Writer(value) 123 | self.converter = PSASP_Converter() 124 | 125 | @path_resources.setter 126 | def path_resources(self, value): 127 | if not isinstance(value, str): 128 | raise ValueError('path_resources must be a string!') 129 | elif not os.path.isdir(value): 130 | raise ValueError('path_resources should be an existing folder!') 131 | self.__path_resources = value 132 | self.__path_exe_wmlfmsg = os.path.join(self.__path_resources, const.EXE_LF) 133 | self.__path_exe_wmudrt = os.path.join(self.__path_resources, const.EXE_ST) 134 | self.__path_exe_wsstlin = os.path.join(self.__path_resources, const.EXE_SST_LIN) 135 | self.__path_exe_wssteig = os.path.join(self.__path_resources, const.EXE_SST_EIG) 136 | self.__executor_lf = executor_PSASP_lf(self.__path_exe_wmlfmsg, self.path_temp) 137 | self.__executor_st = executor_PSASP_st(self.__path_exe_wmudrt, self.path_temp) 138 | self.__executor_sstlin = executor_PSASP_sstlin(self.__path_exe_wsstlin, self.path_temp) 139 | self.__executor_ssteig = executor_PSASP_ssteig(self.__path_exe_wssteig, self.path_temp) 140 | 141 | def calculate_LF(self): 142 | success_lf = False 143 | self.__executor_lf.execute_exe() 144 | LFCAL = self.parser.parse_single_s(const.LABEL_LF, const.LABEL_RESULTS, const.LABEL_CONF) 145 | if LFCAL: 146 | LFCAL = LFCAL[0] 147 | if const.MCalKey in LFCAL.keys(): 148 | success_lf = LFCAL[const.MCalKey] == 1 149 | return success_lf 150 | 151 | def calculate_ST(self): 152 | success_st = False 153 | self.__executor_st.execute_exe() 154 | STCAL = self.parser.parse_single_s(const.LABEL_ST, const.LABEL_RESULTS, const.LABEL_CONF) 155 | if STCAL: 156 | STCAL = STCAL[0] 157 | if const.MCalKey in STCAL.keys(): 158 | success_st = STCAL[const.MCalKey] > 0 159 | return success_st 160 | 161 | # TODO: read CAL file? 162 | def calculate_SST_LIN(self): 163 | success_sst_lin = True 164 | self.__executor_sstlin.execute_exe() 165 | return success_sst_lin 166 | 167 | # TODO: read CAL file? 168 | def calculate_SST_EIG(self): 169 | success_sst_eig = True 170 | self.__executor_ssteig.execute_exe() 171 | return success_sst_eig 172 | 173 | def calculate_CCT(self, path_save_left=None, path_save_right=None, 174 | func_change_t=func_change_t_regular, 175 | func_judge_stable=func_judge_stable_regular, label=None, 176 | Tstep_max=Tstep_max_default, Tsim=Tsim_default, eps=eps_default, 177 | copy_output=False): 178 | 179 | if label is None: 180 | label = '-------AFFAIR-------' 181 | rec = { 182 | TMAX_STEP_KEY: Tstep_max, 183 | T_SIM_KEY: Tsim, 184 | EPS_KEY: eps, 185 | T_LEFT_KEY: 0, 186 | T_RIGHT_KEY: Tstep_max, 187 | CCT_KEY: float('nan'), 188 | F_LEFT_KEY: False, 189 | F_RIGHT_KEY: True, 190 | COUNT_ITER_KEY: 0, 191 | FLAG_LIMIT_TOUCHED_KEY: False, 192 | OUTPUT_ST_LEFT_KEY: path_save_left, 193 | OUTPUT_ST_RIGHT_KEY: path_save_right 194 | } 195 | 196 | start_time = time.clock() 197 | while abs(rec[T_LEFT_KEY] - rec[T_RIGHT_KEY]) > rec[EPS_KEY]: 198 | if rec[COUNT_ITER_KEY] == 0 or (not rec[FLAG_LIMIT_TOUCHED_KEY]): 199 | CT_t = rec[T_RIGHT_KEY] 200 | else: 201 | CT_t = (rec[T_LEFT_KEY] + rec[T_RIGHT_KEY]) / 2 202 | func_change_t(self, CT_t) 203 | self.__executor_st.execute_exe() 204 | stable = func_judge_stable(self) 205 | if stable: 206 | rec[T_LEFT_KEY] = CT_t 207 | rec[F_LEFT_KEY] = stable 208 | rec[CCT_KEY] = CT_t 209 | if copy_output: 210 | if rec[OUTPUT_ST_LEFT_KEY]: 211 | copyfiles_st(self.path_temp, rec[OUTPUT_ST_LEFT_KEY]) 212 | if not rec[FLAG_LIMIT_TOUCHED_KEY]: 213 | rec[T_RIGHT_KEY] = CT_t + Tstep_max 214 | 215 | else: 216 | rec[T_RIGHT_KEY] = CT_t 217 | rec[F_RIGHT_KEY] = stable 218 | if copy_output: 219 | if rec[OUTPUT_ST_RIGHT_KEY]: 220 | copyfiles_st(self.path_temp, rec[OUTPUT_ST_RIGHT_KEY]) 221 | rec[FLAG_LIMIT_TOUCHED_KEY] = True 222 | 223 | rec[COUNT_ITER_KEY] += 1 224 | print( 225 | '%s%d (%s,%.4f): %.4f, %.4f' % (label, rec[COUNT_ITER_KEY], str(stable), 226 | rec[T_RIGHT_KEY] - rec[T_LEFT_KEY], 227 | rec[T_LEFT_KEY], rec[T_RIGHT_KEY])) 228 | if rec[T_RIGHT_KEY] > Tsim: 229 | break 230 | elapsed = time.clock() - start_time 231 | rec[ELAPSED_KEY] = elapsed 232 | print('%sCCT = %.4f' % (label, rec[CCT_KEY])) 233 | if not rec[OUTPUT_ST_LEFT_KEY]: 234 | rec[OUTPUT_ST_LEFT_KEY] = None 235 | rec[OUTPUT_ST_RIGHT_KEY] = None 236 | 237 | return rec 238 | 239 | 240 | class CCT_generator(object): 241 | @property 242 | def path_output(self): 243 | return self.__path_output 244 | 245 | @path_output.setter 246 | def path_output(self, value): 247 | if not isinstance(value, str): 248 | raise ValueError('path_output should be string!') 249 | else: 250 | if not os.path.isdir(value): 251 | os.makedirs(value) 252 | self.__path_output = value 253 | self.__path_record_master = os.path.join(value, const.RecordMasterDb) 254 | self.__path_record_lf = os.path.join(value, const.RecordLFDb) 255 | self.__path_output_st = os.path.join(value, const.LABEL_ST) 256 | self.__path_output_st_left = os.path.join(self.__path_output_st, st_output_subfolder_left) 257 | self.__path_output_st_right = os.path.join(self.__path_output_st, st_output_subfolder_right) 258 | 259 | def __init__(self, path_temp, path_resources=None, path_output=PATH_OUTPUT_DEFAULT, 260 | func_change_lfs=func_change_lf_temp): 261 | self.__path_temp = path_temp 262 | self.__PSASP = PSASP(path_temp, path_resources) 263 | self.path_output = path_output 264 | self.__func_change_lfs = func_change_lfs 265 | 266 | def insert_lf_into_db(self, token, table_name, lf_t): 267 | Converter_t = self.__PSASP.converter 268 | list_lf_t = Converter_t.convert_get2list(lf_t) 269 | heads, values = formulate_list_of_dicts(list_lf_t) 270 | insert_from_list_to_db(self.__path_record_lf, table_name, heads, values) 271 | insert_from_list_to_db(self.__path_record_lf, const.CompletedLFTable, 272 | [const.TokenKey, OUTPUT_LF_KEY], [[token, table_name]], 273 | primary_key=const.TokenKey) 274 | 275 | def run_sim_CCT_once(self, dump_lf=True, copy_output=False): 276 | self.__func_change_lfs(self.__PSASP) 277 | success_lf = self.__PSASP.calculate_LF() 278 | # success_lf = True 279 | rec_t = {SUCCESS_LF_KEY: success_lf} 280 | token_t = gen_token() 281 | rec_t[const.TokenKey] = token_t 282 | flft = lf_output_prefix + token_t 283 | stft = token_t 284 | if success_lf: 285 | fstleftt = os.path.join(self.__path_output_st_left, stft) 286 | fstrightt = os.path.join(self.__path_output_st_right, stft) 287 | rec_t_st = self.__PSASP.calculate_CCT(fstleftt, fstrightt, copy_output=copy_output) 288 | rec_t.update(rec_t_st) 289 | labels_t = [const.LABEL_RESULTS,const.LABEL_SETTINGS] 290 | else: 291 | labels_t = [const.LABEL_SETTINGS] 292 | 293 | Parser_t = self.__PSASP.parser 294 | lf_t = Parser_t.parse_all_lf_sr(labels_t) 295 | if dump_lf: 296 | flft = None 297 | lf_save_t = lf_t 298 | else: 299 | lf_save_t = None 300 | self.insert_lf_into_db(token_t, flft, lf_t) 301 | rec_t[OUTPUT_LF_KEY] = flft 302 | rec_t[const.LABEL_LF] = lf_save_t 303 | keys_t = list(rec_t.keys()) 304 | values_t = list(rec_t.values()) 305 | insert_from_list_to_db(self.__path_record_master, const.RecordMasterTable, keys_t, [values_t], 306 | primary_key=const.TokenKey) 307 | return rec_t 308 | 309 | 310 | if __name__ == '__main__': 311 | 312 | #os.system('@echo off') 313 | Pt = PSASP(r'E:\01_Research\98_Data\2016_06_01\2016_06_01T00_00_24') 314 | lf = Pt.parser.parse_all_results_lf() 315 | d = Pt.parser.parse_single_s(const.LABEL_LF,const.LABEL_SETTINGS,const.LABEL_GENERATOR) 316 | d[1][const.V0Key] = 1.0 317 | 318 | Pt.writer.write_to_file_s_lfs_autofit(d) 319 | 320 | Pt.calculate_LF() 321 | 322 | Cc = CCT_generator(PATH_TEMP, PATH_RESOURCES, PATH_OUTPUT, func_change_lf_temp) 323 | count_t = 0 324 | max_count = 10000 325 | while count_t <= max_count: 326 | Cc.run_sim_CCT_once() 327 | count_t += 1 328 | 329 | -------------------------------------------------------------------------------- /PyPSASP/PSASPClasses/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liu2bao/PyPSASP/ad7362e98f9f8490fafef70c1d47a357709da961/PyPSASP/PSASPClasses/__init__.py -------------------------------------------------------------------------------- /PyPSASP/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liu2bao/PyPSASP/ad7362e98f9f8490fafef70c1d47a357709da961/PyPSASP/__init__.py -------------------------------------------------------------------------------- /PyPSASP/constants/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liu2bao/PyPSASP/ad7362e98f9f8490fafef70c1d47a357709da961/PyPSASP/constants/__init__.py -------------------------------------------------------------------------------- /PyPSASP/constants/const.py: -------------------------------------------------------------------------------- 1 | contributor = ('LXZ', 'FYW',) 2 | # TODO: ORM? 3 | 4 | CalTypeKey = 'calculate_type' # LF, ST, SST_LIN, SST_EIG 5 | GetTypeKey = 'get_type' # settings, results 6 | EleTypeKey = 'element_type' # Bus, Tranformer, etc. 7 | EleAttrNameKey = 'attribute_name' # Pg, Qg, etc. 8 | EleIdKey = 'element_id' 9 | EleAttrValueKey = 'attribute_value' 10 | 11 | KeyInsertTimeStamp = 'insert_time' 12 | EXE_LF = 'WMLFRTMsg.exe' 13 | EXE_ST = 'wmudrt.exe' 14 | EXE_SST_LIN = 'Wsstlin.exe' 15 | EXE_SST_EIG = 'Wssteig.exe' 16 | WINDOW_NAME_ST = 'Transient Stability Calculation' 17 | FILE_STOUT = 'STOUT.INF' 18 | FILE_TEMPLATE_OUTPUT_ST = 'FN{}.DAT' 19 | PATTERN_OUTPUT_ST = FILE_TEMPLATE_OUTPUT_ST.format(r'\d+') 20 | FILE_PREFIX_LF = 'LF.' 21 | PATTERN_SETTINGS_LF = FILE_PREFIX_LF + '.N{0,1}L\d+' 22 | PATTERN_RESULTS_LF = FILE_PREFIX_LF + '.[N|L]P\d+' 23 | 24 | # calType 25 | LABEL_LF = 'load_flow' 26 | LABEL_ST = 'transient_stability' 27 | LABEL_SST_LIN = 'small-signal_stability_linearity' 28 | LABEL_SST_EIG = 'small-signal_stability_eigenvalue' 29 | 30 | # getType 31 | LABEL_SETTINGS = 'settings' 32 | LABEL_RESULTS = 'results' 33 | 34 | # eleType 35 | LABEL_CONF = 'configuration' 36 | LABEL_ERR = 'errors' 37 | LABEL_BUS = 'bus' 38 | LABEL_ACLINE = 'acline' 39 | LABEL_TRANSFORMER = 'transformer' 40 | LABEL_DCLINE = 'dcline' 41 | LABEL_GENERATOR = 'generator' 42 | LABEL_LOAD = 'load' 43 | LABEL_INTERCHANGE = 'interchange' 44 | LABEL_SVC = 'SVC' 45 | LABEL_FAULT = 'fault' 46 | LABEL_ANA = 'auto_analysis' 47 | 48 | LABEL_EIGVAL = 'eigenvalue' 49 | LABEL_EIGVEC = 'eigenvector' 50 | 51 | ########## 52 | PREFIX_FILE_LF = 'LF' 53 | PREFIX_FILE_ST = 'ST' 54 | POSTFIX_FILE_LF_SETTING = '.L{}' 55 | POSTFIX_FILE_ST_SETTING = '.L{}' 56 | POSTFIX_FILE_LF_RESULT = '.LP{}' 57 | CreatedOnPattern = 'Created on' 58 | 59 | Pattern_read = '([^\']*?|\'.*?\')[ ]*,' 60 | 61 | StOutVarNameKey = 'var_name' 62 | TimeKey = 'time' 63 | TokenKey = 'token' 64 | RecordMasterDb = 'record_master.db' 65 | RecordLFDb = 'record_lf.db' 66 | RecordMasterTable = 'records' 67 | CompletedLFTable = 'completed_lf' 68 | VarKeyPrefix = 'var_' 69 | VarDefinitionTable = 'VarDef' 70 | VarValueTable = 'VarValue' 71 | ################################################# 72 | # ---COMMON---# 73 | MarkKey = 'Mark' 74 | ParNoKey = 'Par_No' 75 | CtrlModeKey = 'Mode_Ctrl' 76 | CtrlBusKey = 'Bus_Ctrl' 77 | CtrlLineKey = 'Line_Ctrl' 78 | CtrlValueKey = 'Value_Ctrl' 79 | INoKey = 'I_No' 80 | JNoKey = 'J_No' 81 | IDNoKey = 'ID_No' 82 | IDKey = 'ID' 83 | R1Key = 'R1' 84 | X1Key = 'X1' 85 | OwnerKey = 'Owner' 86 | UpLimitKey = 'Up_Limit' 87 | LineNameKey = 'Line_Name' 88 | 89 | V0Key = 'V0' 90 | AngleKey = 'Angle' 91 | QmaxKey = 'Qmax' 92 | QminKey = 'Qmin' 93 | PmaxKey = 'Pmax' 94 | PminKey = 'Pmin' 95 | ParGroupKey = 'Par_Group' 96 | 97 | UnknownDesc = 'Unknown_desc' 98 | UnknownInt = 'Unknown_int' 99 | UnknownFloat_1 = 'Unknown_float' 100 | UnknownFloat_2 = 'Unknown_float' 101 | # ----END----# 102 | 103 | 104 | # ---BUS---# 105 | BusNoKey = 'Bus_No' 106 | BusNameKey = 'Bus_Name' 107 | BaseKVKey = 'Base_kV' 108 | AreaNoKey = 'Area_No' 109 | VmaxKVKey = 'Vmax_kV' 110 | VminKVKey = 'Vmin_kV' 111 | SC1MVAKey = 'SC1_MVA' 112 | SC3MVAKey = 'SC3_MVA' 113 | # ---END---# 114 | 115 | # ---ACLINE---# 116 | ACLineHalfB1Key = 'B1_Half' 117 | ACLineRateKAKey = 'Rate_kA' 118 | # ----END----# 119 | 120 | # ---TRANSFORMER---# 121 | TransTkKey = 'Tk' 122 | TransRmKey = 'Rm' 123 | TransXmKey = 'Xm' 124 | Trans2WKey = '2W' 125 | TransTPKey = 'TP' 126 | TransShiftAngKey = 'Ang_Shift' 127 | TransRateMVAKey = 'Rate_MVA' 128 | TransJKey = 'J*' 129 | TransTrsTypeKey = 'TrsType' 130 | TransNameKey = 'Transformer_Name' 131 | TransVi0KVKey = 'Vi0_kV' 132 | TransVj0KVKey = 'Vj0_kV' 133 | TransMaxTapPos2Key = 'Max_Tap2' # "最高档位2" 134 | TransMinTapPos2Key = 'Min_Tap2' # "最低档位2" 135 | TransMainTapPos2Key = 'Main_Tap2' # "主抽头档位2" 136 | TransVjstepPrcKey = 'Vjstep_Prc' 137 | TransVjPosKey = 'Vjpos' 138 | # -------END-------# 139 | 140 | # ---DCLINE--# 141 | DCLineRpiKey = 'Rpi_Ohm' 142 | DCLineLpiKey = 'Lpi_mH' 143 | DCLineRpjKey = 'Rpj_Ohm' 144 | DCLineLpjKey = 'Lpj_mH' 145 | DCLineRlKey = 'Rl_Ohm' 146 | DCLineLlKey = 'Ll_mH' 147 | DCLineReiKey = 'Rei_Ohm' 148 | DCLineRejKey = 'Rej_Ohm' 149 | DCLineLsiKey = 'Lsi_mH' 150 | DCLineLsjKey = 'Lsj_mH' 151 | DCLineVdnKey = 'Vdrate_kV' 152 | DCLineVhiKey = 'Vhi_kV' 153 | DCLineVliKey = 'Vli_kV' 154 | DCLineBiKey = 'I_Bridge' 155 | DCLineStiKey = 'Sti_MVA' 156 | DCLineRtiKey = 'Rti_Ohm' 157 | DCLineXtiKey = 'Xti_Perc' 158 | DCLineVtimaxKey = 'Vtimax_kV' 159 | DCLineVtiminKey = 'Vtimin_kV' 160 | DCLineNtapiKey = 'I_Tap' 161 | DCLineVhjKey = 'Vhj_kV' 162 | DCLineVljKey = 'Vlj_Kv' 163 | DCLineBjKey = 'J_Bridge' 164 | DCLineStjKey = 'Stj_MVA' 165 | DCLineRtjKey = 'Rtj_Ohm' 166 | DCLineXtjKey = 'Xtj_Perc' 167 | DCLineVtjmaxKey = 'Vtjmax_kV' 168 | DCLineVtjminKey = 'Vtjmin_kV' 169 | DCLineNtapjKey = 'J_Tap' 170 | DCLineOPKey = 'OP_Mode' 171 | DCLineQciKey = 'Qci_Mvar' 172 | DCLineQcjKey = 'Qcj_Mvar' 173 | DCLinePd1Key = 'Pd1_MW' 174 | DCLineVd1Key = 'Vd1_kV' 175 | DCLineA1minKey = 'A1min_D' 176 | DCLineA10Key = 'A10_D' 177 | DCLineGama1minKey = 'Gam1min_D' 178 | DCLineGama10Key = 'Gam10_D' 179 | DCLinePd2Key = 'Pd2_MW' 180 | DCLineVd2Key = 'Vd2_kV' 181 | DCLineA2minKey = 'A2min_D' 182 | DCLineA20Key = 'A20_D' 183 | DCLineGama2minKey = 'Gam2min_D' 184 | DCLineGama20Key = 'Gam20_D' 185 | # ----END---# 186 | 187 | # ---GENERATOR--# 188 | GenPgKey = 'Pg' 189 | GenQgKey = 'Qg' 190 | GenKPrcKey = 'K%' 191 | GenNameKey = 'Generator_Name' 192 | # ------END-----# 193 | 194 | # ---LOAD--# 195 | LoadNoKey = 'Load_No' 196 | LoadPlKey = 'Pl' 197 | LoadQlKey = 'Ql' 198 | LoadNameKey = 'Load_Name' 199 | # ---END--# 200 | 201 | 202 | # ---INTERCHANGE--# 203 | InterchangeAreaNoKey = 'Area_No' 204 | InterchangeAreaNameKey = 'Area_Name' 205 | InterchangeAdjGenKey = 'Gen_Adj' 206 | InterchangeSchedulePKey = 'P_Schedule' 207 | InterchangeToleranceKey = 'Tolerance' 208 | InterchangePmaxKey = 'Pmax' 209 | # -------END------# 210 | ################################################# 211 | # ---COMMON---# 212 | VoltageKey = 'V' 213 | VoltageRealKey = 'Vreal' 214 | VoltageImageKey = 'Vimg' 215 | PiKey = 'Pi' 216 | QiKey = 'Qi' 217 | PjKey = 'Pj' 218 | QjKey = 'Qj' 219 | # ----END----# 220 | 221 | # ---BUS---# 222 | # ---END---# 223 | 224 | # ---ACLINE---# 225 | ACLineQciKey = 'Qci' 226 | ACLineQcjKey = 'Qcj' 227 | # ----END----# 228 | 229 | # ---TRANSFORMER---# 230 | # -------END-------# 231 | 232 | # ---DCLINE--# 233 | DCLineId10Key = 'Id10' 234 | DCLineId20Key = 'Id20' 235 | DCLineVaciKey = 'Vaci' 236 | DCLineVacjKey = 'Vacj' 237 | DCLinePd1iKey = 'Pd1i' 238 | DCLineQd1iKey = 'Qd1i' 239 | DCLineVd10iKey = 'Vd10i' 240 | DCLineTk1iKey = 'Tk1i' 241 | DCLineA10rKey = 'A10r' 242 | DCLineTk1iPercKey = 'Tk1iPerc' 243 | DCLinePd1jKey = 'Pd1j' 244 | DCLineQd1jKey = 'Qd1j' 245 | DCLineVd10jKey = 'Vd10j' 246 | DCLineTk1jKey = 'Tk1j' 247 | DCLineTk1jPercKey = 'Tk1jPerc' 248 | DCLinePd2iKey = 'Pd2i' 249 | DCLineQd2iKey = 'Qd2i' 250 | DCLineVd20iKey = 'Vd20i' 251 | DCLineTk2iKey = 'Tk2i' 252 | DCLineA20rKey = 'A20r' 253 | DCLineTk2iPercKey = 'Tk2iPerc' 254 | DCLinePd2jKey = 'Pd2j' 255 | DCLineQd2jKey = 'Qd2j' 256 | DCLineVd20jKey = 'Vd20j' 257 | DCLineTk2jKey = 'Tk2j' 258 | DCLineTk2jPercKey = 'Tk2jPerc' 259 | DCLineVbiKey = 'Vbi' 260 | DCLineVbjKey = 'Vbj' 261 | DCLineVdbKey = 'Vdb' 262 | DCLineIdbKey = 'Idb' 263 | DCLineZdbKey = 'Zdb' 264 | DCLineLdbKey = 'Ldb' 265 | DCLineTkbiKey = 'Tkbi' 266 | DCLineTkbjKey = 'Tkbj' 267 | 268 | DCLineXciKey = 'Xci' 269 | DCLineXcjKey = 'Xcj' 270 | DCLineTkimaxKey = 'Tkimax' 271 | DCLineTkiminKey = 'Tkimin' 272 | DCLineTkjmaxKey = 'Tkjmax' 273 | DCLineTkjminKey = 'Tkjmin' 274 | DCLineQcipKey = 'Qcip' 275 | DCLineQcjpKey = 'Qcjp' 276 | # ----END---# 277 | 278 | # ---GENERATOR--# 279 | # ------END-----# 280 | 281 | # ---LOAD--# 282 | # ---END--# 283 | 284 | # ---INTERCHANGE--# 285 | InterchangePsumKey = 'P_sum' 286 | InterchangeAdjPgKey = 'Pg_adj' 287 | # -------END------# 288 | ################################################# 289 | # ---COMMON---# 290 | R0Key = 'R0' 291 | X0Key = 'X0' 292 | # ----END----# 293 | 294 | # ---BUS---# 295 | # ---END---# 296 | 297 | # ---ACLINE---# 298 | ACLineHalfB0Key = 'B0_Half' 299 | # ----END----# 300 | 301 | # ---TRANSFORMER---# 302 | TransTk0Key = 'Tk0' 303 | TransGm0Key = 'Gm0' 304 | TransBm0Key = 'Bm0' 305 | # -------END-------# 306 | 307 | # ---DCLINE--# 308 | DCLineModelKey = 'dc_model' # "直流模型" 309 | DCLineRegTypeKey = 'reg_type' 310 | DCLineRegFonKey = 'reg_fon' 311 | DCLineRegParKey = 'reg_par' 312 | DCLineRegSetKey = 'reg_set' 313 | DCLineAmaxKey = 'Amaxi_D' 314 | DCLineAminKey = 'Amini_D' 315 | DCLineIregFonKey = 'Ireg_fon' 316 | DCLineIregParKey = 'Ireg_par' 317 | DCLineIregSetKey = 'Ireg_set' 318 | DCLineVregFonKey = 'Vreg_fon' 319 | DCLineVregParKey = 'Vreg_par' 320 | DCLineVregSetKey = 'Vreg_set' 321 | DCLineGregFonKey = 'Greg_fon' 322 | DCLineGregParKey = 'Greg_par' 323 | DCLineGregSetKey = 'Greg_set' 324 | DCLineBmaxKey = 'Bmax_D' 325 | DCLineBminKey = 'Bmin_D' 326 | DCLineImKey = 'Im_Percent' 327 | DCLineVLowKey = 'Vlow_Perc' 328 | DCLineIdmaxKey = 'Idmax_kA' 329 | DCLineIdminKey = 'Idmin_ kA' 330 | DCLineDKiKey = 'dKi_Perc' 331 | DCLineDKjKey = 'dKj_Perc' 332 | DCLineDTiKey = 'dTi_s' 333 | DCLineDTjKey = 'dTj_s' 334 | DCLineVRelayKey = 'V_relay_Perc' 335 | DCLineTRelayKey = 'T_relay' 336 | DCLineTypeFdcKey = 'Type_fadc' 337 | DCLineKFdcKey = 'K_fadc' 338 | DCLineTsFdcKey = 'Ts_fadc' 339 | DCLineTeFdcKey = 'Te_fadc' 340 | DCLineTdoKey = 'Tdo' 341 | DCLineTdaKey = 'Tda' 342 | DCLineTdbKey = 'Tdb' 343 | DCLineTdcKey = 'Tdc' 344 | DCLineNRsKey = 'N_rs' 345 | DCLineVRsKey = 'V_rs_Perc' 346 | # ----END---# 347 | 348 | # ---GENERATOR--# 349 | GenTgKey = 'GEN_MODEL' 350 | GenLgKey = 'GEN_PAR' 351 | GenTvrKey = 'AVR_MODEL' 352 | GenLvrKey = 'AVR_PAR' 353 | GenTgoKey = 'GOV_MODEL' 354 | GenLgoKey = 'GOV_PAR' 355 | GenTpssKey = 'PSS_MODEL' 356 | GenLpssKey = 'PSS_PAR' 357 | GenXdpKey = 'XDP' 358 | GenXdppKey = 'XDPP' 359 | GenX2Key = 'X2' 360 | GenTjKey = 'TJ' 361 | GenShKey = 'RATE_MVA' 362 | GenPhKey = 'RATE_MW' 363 | # ------END-----# 364 | 365 | # ---LOAD--# 366 | LoadModelKey = 'LOAD_MODEL' 367 | LoadParKey = 'LOAD_PAR' 368 | LoadZPercentKey = 'Z_PERCENT' 369 | # ---END--# 370 | 371 | # ---SVC--# 372 | SVCSetBusKey = 'Set_Bus' 373 | SVCModelKey = 'SVC_MODEL' 374 | SVCXshKey = 'x_fixed' 375 | SVCAssBus1No = 'ass_bus_1' # "ass_bus1" 376 | SVCAssBus2No = 'ass_bus_2' # "ass_bus2" 377 | SVCAssLineNo = 'ass_line' # "ass_line" 378 | SVCAuxiliarySignal1ValidKey = 'valid_auxiliary_signal_1' # "辅助信号1有效" 379 | SVCAuxiliarySignal1TypeKey = 'type_auxiliary_signal_1' # "辅助信号1类型" 380 | SVCAuxiliarySignal2ValidKey = 'valid_auxiliary_signal_2' # "辅助信号2有效" 381 | SVCAuxiliarySignal2TypeKey = 'type_auxiliary_signal_2' # "辅助信号2类型" 382 | SVCNameKey = 'SVC_NAME' 383 | # --END--# 384 | 385 | ################################################# 386 | 387 | 388 | ################################################# 389 | NBusKey = 'NBus' 390 | NACKey = 'Nac' 391 | NTransKey = 'Ntrans' 392 | NDCKey = 'NDC' 393 | NGenKey = 'NGenerator' 394 | NLoadKey = 'NLoad' 395 | NAreaKey = 'Narea' 396 | NUPKey = 'NUP' 397 | NUDKey = 'UD_Time' 398 | NVRKey = 'NVR' # Number of voltage regulator 399 | NGovKey = 'NGov' 400 | NPssKey = 'NPss' 401 | NStaticLoadKey = 'NStaticLoad' 402 | NInductionMotorKey = 'NInductionMotor' 403 | NOtherUDKey = 'NOtherUD' 404 | NStateVariableKey = 'NStateVariable' 405 | NAugmentedStateVariableKey = 'NAugmentedStateVariable' 406 | CtrlUDKey = 'Ctrl_UD' 407 | MatlabIntKey = 'Ctrl_Matitf' 408 | CalDateKey = 'CAL_Date' 409 | CalTimeKey = 'CAL_Time' 410 | MCalKey = 'CALCULATE' 411 | 412 | LFNEQKey = 'NEQ' 413 | LFNSSKey = 'NSS' 414 | LFCtrlFactKey = 'Ctrl_Fact' 415 | LFBasicCapacityKey = 'BasicCapacity' 416 | LFVmaxKey = 'Vmax' 417 | LFVminKey = 'Vmin' 418 | LFEpsKey = 'Eps' 419 | LFMethodKey = 'Method' 420 | LFIterationKey = 'Iteration' 421 | LFCtrlAreaKey = 'Ctrl_Area' 422 | LFEQMethodKey = 'EQ_Method' 423 | LFCtrlsubKey = 'Ctrl_sub' 424 | LFUPCALLKey = 'UP_CALL' 425 | LFCtrlRmXmKey = 'Ctrl_RmXm' 426 | 427 | LFML23Key = 'L23chged' 428 | 429 | STNBPKey = 'NBP' 430 | STNL0Key = 'NL0' 431 | STNT0Key = 'NT0' 432 | STNSVCKey = 'NSVC' 433 | STNFaultKey = 'NFault' 434 | STNDistKey = 'Ndist' 435 | STNM0Key = 'NM0' 436 | STFSKey = 'ST_SC' 437 | STTTotalKey = 'T_Total' 438 | STDTKey = 'DT_Step' 439 | STToutKey = 'Tout' 440 | STMeqKey = 'EQ_Method' 441 | STCeqKey = 'EQ_Continu' 442 | STF60Key = 'F60' 443 | STMutKey = 'Mut' 444 | STF1Key = 'F1' 445 | STCmKey = 'Cm' 446 | STAmaxKey = 'Amax' 447 | STAreaKey = 'Area' 448 | STNUPKey = 'up_cal' 449 | STDErrorKey = 'DError' 450 | 451 | STIsStableKey = 'IsStable' 452 | STTimeLoseStableKey = 'Time_lose_stable' 453 | STGroupLoseStableKey = 'Ngroup_lose_stable' 454 | 455 | ################################################# 456 | 457 | 458 | ################################################# 459 | FaultLocateKey = 'LOCATE' 460 | FaultAddedBusNameKey = 'Add_Name' 461 | FaultPhaseAKey = 'PhaseA' 462 | FaultPhaseBKey = 'PhaseB' 463 | FaultPhaseCKey = 'PhaseC' 464 | FaultGroundKey = 'Ground' 465 | FaultShortKey = 'Short' 466 | FaultOpenKey = 'Open' 467 | FaultTstartKey = 'FaultTstart' 468 | FaultTendKey = 'FaultTend' 469 | FaultRKey = 'FaultR' 470 | FaultXKey = 'FaultX' 471 | 472 | ################################################# 473 | 474 | 475 | ################################################# 476 | ANALstopKey = 'Lstop' 477 | ANAMaxDAngKey = 'MaxDAng' 478 | ANAMinVolKey = 'MinVol' 479 | ANATDVolKey = 'TDVol' 480 | ANAMinFreqKey = 'MinFreq' 481 | ANATDFreqKey = 'TDFreq' 482 | ANANanaGrpKey = 'NanaGrp' 483 | ANATSDAngKey = 'TSDAng' 484 | ################################################# 485 | 486 | 487 | ################################################# 488 | ANATKey = 'ANA_T' 489 | ANAGrpNoKey = 'ANA_GrpNo' 490 | ANAGenAMaxKey = 'ANA_GenAMax' 491 | ANAGenAMinKey = 'ANA_GenAMin' 492 | ANAAngleKey = 'ANA_Angle' 493 | ANABusVMinKey = 'ANA_BusVMin' 494 | ANAVminKey = 'ANA_Vmin' 495 | ANAGenWMinKey = 'ANA_GenWMin' 496 | ANAWMinKey = 'ANA_WMin' 497 | ################################################# 498 | 499 | ################################################# 500 | OutputKeyType = 'var_type' 501 | OutputKeySubType = 'var_subtype' 502 | OutputKeyNoDesc = 'no_desc' 503 | OutputKeyValues = 'values' 504 | 505 | ################################################# 506 | 507 | 508 | ################################################# 509 | EIGVALNoKey = 'eigen_value_no' 510 | EIGVALRealKey = 'eigen_value_real_part' 511 | EIGVALImgKey = 'eigen_value_image_part' 512 | EIGVALEmprKey = 'elcetro-mechanic_participation_ratio' # "Elcetro-mechanic participation ratio" "机电回路相关比" 513 | ################################################# 514 | EIGVECNoKey = 'eigen_value_no' 515 | EIGVECRelatedKey = 'eigenvector_related_ratio' 516 | EIGVECRealKey = 'eigenvector_real_part' 517 | EIGVECImgKey = 'eigenvector_image_part' 518 | 519 | # --------------------------------------------------------------------------------------------------------# 520 | 521 | ################################################# 522 | 523 | pos_keys_lf_settings_bus = [BusNameKey, BaseKVKey, AreaNoKey, VmaxKVKey, VminKVKey, SC1MVAKey, SC3MVAKey] 524 | 525 | pos_keys_lf_settings_acline = [MarkKey, INoKey, JNoKey, IDNoKey, R1Key, X1Key, ACLineHalfB1Key, 526 | OwnerKey, CtrlModeKey, ParNoKey, CtrlBusKey, CtrlLineKey, 527 | CtrlValueKey, ACLineRateKAKey, UpLimitKey, UnknownDesc, UnknownInt, LineNameKey] 528 | 529 | pos_keys_lf_settings_transformer = [MarkKey, INoKey, JNoKey, IDNoKey, R1Key, X1Key, 530 | TransTkKey, TransRmKey, TransXmKey, Trans2WKey, 531 | CtrlModeKey, ParNoKey, TransTPKey, CtrlBusKey, CtrlLineKey, CtrlValueKey, 532 | TransShiftAngKey, TransRateMVAKey, UpLimitKey, IDKey, 533 | TransJKey, TransTrsTypeKey, UnknownDesc, UnknownInt, TransNameKey, 534 | TransVi0KVKey, TransVj0KVKey, TransMaxTapPos2Key, TransMinTapPos2Key, 535 | TransMainTapPos2Key, TransVjstepPrcKey, TransVjPosKey] 536 | 537 | pos_keys_lf_settings_dcline = [ 538 | [MarkKey, INoKey, JNoKey, IDNoKey, OwnerKey, LineNameKey], 539 | [DCLineRpiKey, DCLineLpiKey, DCLineRpjKey, DCLineLpjKey, DCLineRlKey, DCLineLlKey, 540 | DCLineReiKey, DCLineRejKey, DCLineLsiKey, DCLineLsjKey], 541 | [DCLineVdnKey], 542 | [DCLineVhiKey, DCLineVliKey, DCLineBiKey, DCLineStiKey, DCLineRtiKey, DCLineXtiKey, 543 | DCLineVtimaxKey, DCLineVtiminKey, DCLineNtapiKey], 544 | [DCLineVhjKey, DCLineVljKey, DCLineBjKey, DCLineStjKey, DCLineRtjKey, DCLineXtjKey, 545 | DCLineVtjmaxKey, DCLineVtjminKey, DCLineNtapjKey], 546 | [DCLineOPKey, DCLineQciKey, DCLineQcjKey], 547 | [DCLinePd1Key, DCLineVd1Key, DCLineA1minKey, DCLineA10Key, DCLineGama1minKey, DCLineGama10Key], 548 | [DCLinePd2Key, DCLineVd2Key, DCLineA2minKey, DCLineA20Key, DCLineGama2minKey, DCLineGama20Key] 549 | ] 550 | 551 | pos_keys_lf_settings_generator = [MarkKey, BusNoKey, CtrlModeKey, GenPgKey, GenQgKey, V0Key, AngleKey, 552 | QmaxKey, QminKey, PmaxKey, PminKey, ParGroupKey, 553 | CtrlBusKey, CtrlLineKey, CtrlValueKey, GenKPrcKey, 554 | UnknownDesc, UnknownInt, GenNameKey] 555 | 556 | pos_keys_lf_settings_load = [MarkKey, BusNoKey, LoadNoKey, CtrlModeKey, LoadPlKey, LoadQlKey, V0Key, AngleKey, 557 | QmaxKey, QminKey, PmaxKey, PminKey, ParGroupKey, CtrlBusKey, CtrlLineKey, CtrlValueKey, 558 | UnknownDesc, UnknownInt, LoadNameKey] 559 | 560 | pos_keys_lf_settings_interchange = [MarkKey, InterchangeAreaNoKey, InterchangeAreaNameKey, InterchangeAdjGenKey, 561 | InterchangeSchedulePKey, InterchangeToleranceKey, InterchangePmaxKey] 562 | 563 | pos_keys_lf_settings_conf = [ 564 | [NBusKey, NACKey, NTransKey, NDCKey, NGenKey, NLoadKey, NAreaKey, NUDKey, LFNEQKey, LFNSSKey, LFCtrlFactKey], 565 | [LFBasicCapacityKey, LFVmaxKey, LFVminKey, LFEpsKey, LFMethodKey, LFIterationKey, LFCtrlAreaKey, CtrlUDKey, 566 | LFEQMethodKey, LFCtrlsubKey, LFUPCALLKey, LFCtrlRmXmKey, MatlabIntKey] 567 | ] 568 | ################################################# 569 | 570 | ################################################# 571 | pos_keys_lf_results_bus = [BusNoKey, VoltageKey, AngleKey] 572 | pos_keys_lf_results_acline = [INoKey, JNoKey, IDNoKey, PiKey, QiKey, PjKey, QjKey, ACLineQciKey, ACLineQcjKey] 573 | pos_keys_lf_results_transformer = [INoKey, JNoKey, IDNoKey, PiKey, QiKey, PjKey, QjKey] 574 | 575 | pos_keys_lf_results_dcline = [ 576 | [INoKey, JNoKey, IDNoKey, OwnerKey, LineNameKey], 577 | [DCLineOPKey], 578 | [DCLineId10Key, DCLineId20Key, DCLineVaciKey, DCLineVacjKey], 579 | [DCLinePd1iKey, DCLineQd1iKey, DCLineVd10iKey, DCLineTk1iKey, DCLineA10rKey, DCLineTk1iPercKey], 580 | [DCLinePd1jKey, DCLineQd1jKey, DCLineVd10jKey, DCLineTk1jKey, DCLineTk1jPercKey], 581 | [DCLinePd2iKey, DCLineQd2iKey, DCLineVd20iKey, DCLineTk2iKey, DCLineA20rKey, DCLineTk2iPercKey], 582 | [DCLinePd2jKey, DCLineQd2jKey, DCLineVd20jKey, DCLineTk2jKey, DCLineTk2jPercKey], 583 | [DCLineVbiKey, DCLineVbjKey, DCLineVdbKey, DCLineIdbKey, DCLineZdbKey, DCLineLdbKey, DCLineTkbiKey, DCLineTkbjKey], 584 | [DCLineRpiKey, DCLineLpiKey, DCLineRpjKey, DCLineLpjKey, DCLineRlKey, DCLineLlKey, DCLineReiKey, DCLineRejKey, 585 | DCLineLsiKey, DCLineLsjKey], 586 | [DCLineXciKey, DCLineXcjKey, DCLineTkimaxKey, DCLineTkiminKey, DCLineTkjmaxKey, DCLineTkjminKey, DCLineQcipKey, 587 | DCLineQcjpKey] 588 | ] 589 | pos_keys_lf_results_generator = [BusNoKey, GenPgKey, GenQgKey] 590 | pos_keys_lf_results_load = [BusNoKey, LoadNoKey, LoadPlKey, LoadQlKey] 591 | pos_keys_lf_results_interchange = [AreaNoKey, InterchangePsumKey, InterchangeAdjPgKey] 592 | 593 | pos_keys_lf_results_conf = [[MCalKey, LFML23Key], 594 | [CalDateKey, CalTimeKey], 595 | [NUDKey, NUPKey], 596 | [NBusKey, NGenKey, NLoadKey, NACKey, NDCKey, NTransKey]] 597 | 598 | ################################################# 599 | pos_keys_st_settings_bus = [BusNameKey] 600 | pos_keys_st_settings_acline = [MarkKey, INoKey, JNoKey, IDNoKey, R0Key, X0Key, ACLineHalfB0Key, LineNameKey] 601 | pos_keys_st_settings_transformer = [MarkKey, INoKey, JNoKey, IDNoKey, R0Key, X0Key, TransTk0Key, 602 | TransGm0Key, TransBm0Key, TransNameKey] 603 | 604 | pos_keys_st_settings_dcline = [ 605 | [MarkKey, INoKey, JNoKey, IDNoKey, DCLineModelKey, LineNameKey], 606 | [DCLineRegTypeKey, DCLineRegFonKey, DCLineRegParKey, DCLineRegSetKey, DCLineAmaxKey, DCLineAminKey], 607 | [DCLineIregFonKey, DCLineIregParKey, DCLineIregSetKey, DCLineVregFonKey, DCLineVregParKey, DCLineVregSetKey, 608 | DCLineGregFonKey, DCLineGregParKey, DCLineGregSetKey], 609 | [DCLineBmaxKey, DCLineBminKey, DCLineImKey, DCLineVLowKey, DCLineIdmaxKey, DCLineIdminKey, 610 | DCLineDKiKey, DCLineDKjKey, DCLineDTiKey, DCLineDTjKey, DCLineVRelayKey, DCLineTRelayKey], 611 | [DCLineTypeFdcKey, DCLineKFdcKey, DCLineTsFdcKey, DCLineTeFdcKey, DCLineTdoKey, DCLineTdaKey, 612 | DCLineTdbKey, DCLineTdcKey, DCLineNRsKey, DCLineVRsKey] 613 | ] 614 | pos_keys_st_settings_generator = [MarkKey, BusNoKey, GenTgKey, GenLgKey, GenTvrKey, GenLvrKey, GenTgoKey, GenLgoKey, 615 | GenTpssKey, GenLpssKey, GenXdpKey, GenXdppKey, GenX2Key, GenTjKey, GenShKey, 616 | GenPhKey, GenNameKey] 617 | pos_keys_st_settings_load = [MarkKey, BusNoKey, LoadNoKey, LoadModelKey, LoadParKey, LoadZPercentKey, LoadNameKey] 618 | pos_keys_st_settings_SVC = [MarkKey, SVCSetBusKey, CtrlBusKey, SVCModelKey, ParNoKey, SVCXshKey, 619 | SVCAuxiliarySignal1ValidKey, SVCAuxiliarySignal1TypeKey, SVCAssBus1No, SVCAssBus2No, 620 | SVCAssLineNo, SVCAuxiliarySignal2ValidKey, SVCAuxiliarySignal2TypeKey, SVCNameKey] 621 | 622 | pos_keys_st_settings_conf = [ 623 | [STNBPKey, STNL0Key, STNT0Key, NDCKey, NGenKey, NLoadKey, STNSVCKey, STNFaultKey, STNDistKey, CtrlUDKey, 624 | STNM0Key, MatlabIntKey], 625 | [STFSKey, STTTotalKey, STDTKey, STToutKey, STMeqKey, STCeqKey, STF60Key, STMutKey, 626 | STF1Key, STCmKey, STAmaxKey, STAreaKey,STNUPKey, STDErrorKey] 627 | ] 628 | pos_keys_st_settings_fault = [MarkKey, INoKey, JNoKey, IDNoKey, FaultLocateKey, FaultAddedBusNameKey, 629 | FaultPhaseAKey, FaultPhaseBKey, FaultPhaseCKey, FaultGroundKey, 630 | FaultShortKey, FaultOpenKey, FaultTstartKey, FaultTendKey, FaultRKey, FaultXKey] 631 | 632 | pos_keys_st_settings_ana = [ANALstopKey, ANAMaxDAngKey, ANAMinVolKey, ANATDVolKey, ANAMinFreqKey, ANATDFreqKey, 633 | ANANanaGrpKey, ANATSDAngKey] 634 | ################################################# 635 | 636 | pos_keys_st_results_conf = [ 637 | [MCalKey], 638 | [CalDateKey, CalTimeKey], 639 | [CtrlUDKey, STNUPKey], 640 | [NBusKey, NGenKey, NLoadKey, NDCKey, STNSVCKey, STNFaultKey, STNDistKey], 641 | [STIsStableKey, STTimeLoseStableKey, STGroupLoseStableKey] 642 | ] 643 | 644 | pos_keys_st_results_ana = [ANATKey, ANAGrpNoKey, ANAGenAMaxKey, ANAGenAMinKey, ANAAngleKey, ANABusVMinKey, ANAVminKey, 645 | ANAGenWMinKey, ANAWMinKey] 646 | 647 | ################################################# 648 | pos_keys_sst_eig_results_eigval = [EIGVALNoKey, EIGVALRealKey, EIGVALImgKey, EIGVALEmprKey] 649 | pos_keys_sst_eig_results_eigenvec = [EIGVECNoKey, BusNoKey, EIGVECRelatedKey, EIGVECRealKey, EIGVECImgKey, BusNameKey] 650 | 651 | pos_keys_sst_eig_results_conf = [ 652 | [MCalKey], 653 | [CalDateKey, CalTimeKey], 654 | [NBusKey, NGenKey, NVRKey, NGovKey, NPssKey, NDCKey, NStaticLoadKey, NInductionMotorKey, NOtherUDKey], 655 | [NStateVariableKey, NAugmentedStateVariableKey] 656 | ] 657 | 658 | ################################################# 659 | 660 | 661 | dict_files_lf_settings = {LABEL_BUS: 'LF.L1', LABEL_ACLINE: 'LF.L2', LABEL_TRANSFORMER: 'LF.L3', 662 | LABEL_DCLINE: 'LF.NL4', LABEL_GENERATOR: 'LF.L5', LABEL_LOAD: 'LF.L6', 663 | LABEL_INTERCHANGE: 'LF.L7', LABEL_CONF: 'LF.L0'} 664 | dict_files_lf_results = {LABEL_BUS: 'LF.LP1', LABEL_ACLINE: 'LF.LP2', LABEL_TRANSFORMER: 'LF.LP3', 665 | LABEL_DCLINE: 'LF.NP4', LABEL_GENERATOR: 'LF.LP5', LABEL_LOAD: 'LF.LP6', 666 | LABEL_INTERCHANGE: 'LF.LP7', LABEL_CONF: 'LF.CAL'} 667 | dict_files_st_settings = {LABEL_BUS: 'ST.S1', LABEL_ACLINE: 'ST.S2', LABEL_TRANSFORMER: 'ST.S3', 668 | LABEL_DCLINE: 'ST.NS4', LABEL_GENERATOR: 'ST.S5', LABEL_LOAD: 'ST.S6', 669 | LABEL_SVC: 'ST.S7', LABEL_CONF: 'ST.S0', LABEL_FAULT: 'ST.S11', 670 | LABEL_ANA: 'STCRIT.DAT'} 671 | dict_files_st_results = {LABEL_CONF: 'ST.CAL', LABEL_ANA: 'STANA.DAT'} 672 | file_st_err = 'STERR.LIS' 673 | dict_files_sst_eig_results = {LABEL_CONF: 'SST.CAL', LABEL_EIGVAL: 'SST.EG1', LABEL_EIGVEC: 'SST.EG2'} 674 | dict_files_sst_lin_results = {LABEL_CONF: 'SSTLIN.CAL'} 675 | 676 | dict_pos_keys_lf_settings = {LABEL_BUS: pos_keys_lf_settings_bus, 677 | LABEL_ACLINE: pos_keys_lf_settings_acline, 678 | LABEL_TRANSFORMER: pos_keys_lf_settings_transformer, 679 | LABEL_DCLINE: pos_keys_lf_settings_dcline, 680 | LABEL_GENERATOR: pos_keys_lf_settings_generator, 681 | LABEL_LOAD: pos_keys_lf_settings_load, 682 | LABEL_INTERCHANGE: pos_keys_lf_settings_interchange, 683 | LABEL_CONF: pos_keys_lf_settings_conf} 684 | 685 | dict_pos_keys_lf_results = {LABEL_BUS: pos_keys_lf_results_bus, 686 | LABEL_ACLINE: pos_keys_lf_results_acline, 687 | LABEL_TRANSFORMER: pos_keys_lf_results_transformer, 688 | LABEL_DCLINE: pos_keys_lf_results_dcline, 689 | LABEL_GENERATOR: pos_keys_lf_results_generator, 690 | LABEL_LOAD: pos_keys_lf_results_load, 691 | LABEL_INTERCHANGE: pos_keys_lf_results_interchange, 692 | LABEL_CONF: pos_keys_lf_results_conf} 693 | 694 | dict_pos_keys_st_settings = {LABEL_BUS: pos_keys_st_settings_bus, 695 | LABEL_ACLINE: pos_keys_st_settings_acline, 696 | LABEL_TRANSFORMER: pos_keys_st_settings_transformer, 697 | LABEL_DCLINE: pos_keys_st_settings_dcline, 698 | LABEL_GENERATOR: pos_keys_st_settings_generator, 699 | LABEL_LOAD: pos_keys_st_settings_load, 700 | LABEL_SVC: pos_keys_st_settings_SVC, 701 | LABEL_CONF: pos_keys_st_settings_conf, 702 | LABEL_ANA: pos_keys_st_settings_ana, 703 | LABEL_FAULT: pos_keys_st_settings_fault} 704 | 705 | dict_pos_keys_st_results = {LABEL_CONF: pos_keys_st_results_conf, 706 | LABEL_ANA: pos_keys_st_results_ana} 707 | 708 | dict_pos_keys_sst_eig_results = {LABEL_EIGVAL: pos_keys_sst_eig_results_eigval, 709 | LABEL_EIGVEC: pos_keys_sst_eig_results_eigenvec, 710 | LABEL_CONF: pos_keys_sst_eig_results_conf} 711 | 712 | files_lf_append_no = [dict_files_lf_settings[LABEL_BUS], 713 | dict_files_st_settings[LABEL_BUS]] 714 | 715 | dict_mapping_files = {LABEL_LF: {LABEL_SETTINGS: dict_files_lf_settings, LABEL_RESULTS: dict_files_lf_results}, 716 | LABEL_ST: {LABEL_SETTINGS: dict_files_st_settings, LABEL_RESULTS: dict_files_st_results}, 717 | LABEL_SST_EIG: {LABEL_RESULTS: dict_files_sst_eig_results}, 718 | LABEL_SST_LIN: {LABEL_RESULTS: dict_files_sst_lin_results}} 719 | dict_mapping_pos_keys = {LABEL_LF: {LABEL_SETTINGS: dict_pos_keys_lf_settings, LABEL_RESULTS: dict_pos_keys_lf_results}, 720 | LABEL_ST: {LABEL_SETTINGS: dict_pos_keys_st_settings, LABEL_RESULTS: dict_pos_keys_st_results}, 721 | LABEL_SST_EIG: {LABEL_RESULTS: dict_pos_keys_sst_eig_results}} 722 | -------------------------------------------------------------------------------- /PyPSASP/tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liu2bao/PyPSASP/ad7362e98f9f8490fafef70c1d47a357709da961/PyPSASP/tests/__init__.py -------------------------------------------------------------------------------- /PyPSASP/tests/read_data_temp.py: -------------------------------------------------------------------------------- 1 | from PyPSASP.utils import utils_sqlite, utils_gadgets 2 | from PyPSASP.PSASPClasses.Manipulators import PSASP_Parser, PSASP_Converter 3 | from PyPSASP.PSASPClasses.PSASP import OUTPUT_LF_KEY, SUCCESS_LF_KEY, CCT_KEY 4 | from PyPSASP.constants import const 5 | import os 6 | import pickle 7 | 8 | path_save = r'F:\Data\Research\PyPSASP\CCT\3m' 9 | path_record_master = os.path.join(path_save, 'record.db') 10 | path_record_master_new = os.path.join(path_save, 'record_master.db') 11 | path_record_master_new_new = os.path.join(path_save, 'record_master_overall.db') 12 | path_record_lf = os.path.join(path_save, 'record_lf.db') 13 | 14 | 15 | 16 | Converter_t = PSASP_Converter() 17 | 18 | lfs_brief = utils_sqlite.read_db(path_record_lf,const.CompletedLFTable,return_dict_form=True) 19 | records = utils_sqlite.read_db(path_record_master_new,const.RecordMasterTable,return_dict_form=True) 20 | records_dict = {x[const.TokenKey]:x for x in records} 21 | results = {} 22 | for hh in range(len(lfs_brief)): 23 | #for hh in range(10): 24 | token_t = lfs_brief[hh][const.TokenKey] 25 | results[token_t] = {} 26 | lf_table_t = lfs_brief[hh][OUTPUT_LF_KEY] 27 | lf_t_list = utils_sqlite.read_db(path_record_lf,lf_table_t,return_dict_form=True) 28 | lf_t_dict = Converter_t.convert_get2dict(lf_t_list) 29 | load_flow_t = pickle.dumps(lf_t_dict) 30 | results[token_t][const.LABEL_LF] = lf_t_dict 31 | # results[token_t][const.LABEL_LF] = load_flow_t 32 | if token_t in records_dict.keys(): 33 | results[token_t].update(records_dict[token_t]) 34 | print(hh) 35 | 36 | results_list = list(results.values()) 37 | utils_sqlite.insert_from_list_to_db(path_record_master_new_new,const.RecordMasterTable,None,results_list,primary_key=const.TokenKey) 38 | 39 | 40 | S = utils_sqlite.read_db(path_record_master_new_new,const.RecordMasterTable,return_dict_form=True) -------------------------------------------------------------------------------- /PyPSASP/tests/temp_alter_lf_output.py: -------------------------------------------------------------------------------- 1 | from PyPSASP.utils import utils_sqlite, utils_gadgets 2 | from PyPSASP.PSASPClasses.Manipulators import PSASP_Parser, PSASP_Converter 3 | from PyPSASP.PSASPClasses.PSASP import OUTPUT_LF_KEY, SUCCESS_LF_KEY 4 | from PyPSASP.constants import const 5 | import os 6 | 7 | path_save = r'F:\Data\Research\PyPSASP\CCT\3m' 8 | path_record_master = os.path.join(path_save, 'record.db') 9 | path_record_master_new = os.path.join(path_save, 'record_master.db') 10 | path_record_lf = os.path.join(path_save, 'record_lf.db') 11 | Converter_t = PSASP_Converter() 12 | 13 | 14 | TT_e = utils_sqlite.read_db(path_record_lf, const.CompletedLFTable, return_dict_form=True) 15 | paths_lf_e = [t[OUTPUT_LF_KEY] for t in TT_e] 16 | T = utils_sqlite.read_db(path_record_master, const.RecordMasterTable, return_dict_form=True) 17 | TT = [] 18 | for hh in range(len(T)): 19 | tt = T[hh].copy() 20 | path_lf_t = T[hh][OUTPUT_LF_KEY] 21 | if path_lf_t in paths_lf_e: 22 | continue 23 | token_t = utils_gadgets.gen_token() 24 | tt[const.TokenKey] = token_t 25 | success_lf_t = T[hh][SUCCESS_LF_KEY] 26 | Parser_t = PSASP_Parser(path_lf_t) 27 | if success_lf_t: 28 | label_t = const.LABEL_RESULTS 29 | else: 30 | label_t = const.LABEL_SETTINGS 31 | lft = Parser_t.parse_all_lf(label_t) 32 | list_lft = Converter_t.convert_get2list(lft) 33 | heads, values = utils_gadgets.formulate_list_of_dicts(list_lft) 34 | lf_table_t = 'lf_' + token_t 35 | tt[OUTPUT_LF_KEY] = lf_table_t 36 | utils_sqlite.insert_from_list_to_db(path_record_lf, lf_table_t, heads, values) 37 | utils_sqlite.insert_from_list_to_db(path_record_lf, const.CompletedLFTable, 38 | [const.TokenKey, OUTPUT_LF_KEY, const.GetTypeKey], 39 | [[token_t, lf_table_t, label_t]], 40 | primary_key=const.TokenKey) 41 | TT.append(tt) 42 | 43 | keys_t, values_t = utils_gadgets.formulate_list_of_dicts(TT) 44 | utils_sqlite.insert_from_list_to_db(path_record_master_new, const.RecordMasterTable, keys_t, values_t, 45 | primary_key=const.TokenKey) 46 | -------------------------------------------------------------------------------- /PyPSASP/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liu2bao/PyPSASP/ad7362e98f9f8490fafef70c1d47a357709da961/PyPSASP/utils/__init__.py -------------------------------------------------------------------------------- /PyPSASP/utils/utils_PSASP.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | from PyPSASP.utils.utils_gadgets import copyfiles,copyfiles_pattern 4 | from PyPSASP.constants.const import FILE_STOUT, dict_files_st_settings, LABEL_CONF, dict_files_st_results, LABEL_ANA 5 | from PyPSASP.constants.const import FILE_PREFIX_LF,PATTERN_OUTPUT_ST,PATTERN_SETTINGS_LF,PATTERN_RESULTS_LF 6 | 7 | 8 | def copyfiles_st(path_src,path_dst): 9 | if os.path.isdir(path_src): 10 | ST_conf = {FILE_STOUT, 11 | dict_files_st_settings[LABEL_CONF], 12 | dict_files_st_results[LABEL_CONF], 13 | dict_files_st_results[LABEL_ANA]} 14 | files_t = os.listdir(path_src) 15 | files_part_1 = list(set(files_t).intersection(ST_conf)) 16 | files_part_2 = [x for x in files_t if re.match(PATTERN_OUTPUT_ST,x)] 17 | files_st = files_part_1+files_part_2 18 | copyfiles(path_src,path_dst,files_st) 19 | 20 | 21 | def copyfiles_lf(path_src,path_dst): 22 | if os.path.isdir(path_src): 23 | files_lf = [x for x in os.listdir(path_src) if x.startswith(FILE_PREFIX_LF)] 24 | copyfiles(path_src,path_dst,files_lf) 25 | 26 | 27 | def copyfiles_lfs(path_src,path_dst): 28 | copyfiles_pattern(path_src,path_dst,PATTERN_SETTINGS_LF) 29 | 30 | 31 | def copyfiles_lfr(path_src,path_dst): 32 | copyfiles_pattern(path_src,path_dst,PATTERN_RESULTS_LF) 33 | 34 | 35 | 36 | def reshape_pos_keys(pos_keys): 37 | if isinstance(pos_keys[0], list) or isinstance(pos_keys[0], tuple): 38 | pos_keys_multiline = pos_keys.copy() 39 | else: 40 | pos_keys_multiline = [pos_keys] 41 | 42 | return pos_keys_multiline 43 | 44 | 45 | 46 | 47 | if __name__=='__main__': 48 | path_src_t = r'E:\01_Research\98_Data\SmallSystem_PSASP\Temp_20190422_MinInputs' 49 | path_dst_t = r'E:\01_Research\98_Data\SmallSystem_PSASP\Temp_20190422_MinInputs\temp' 50 | copyfiles_st(path_src_t, path_dst_t) -------------------------------------------------------------------------------- /PyPSASP/utils/utils_gadgets.py: -------------------------------------------------------------------------------- 1 | import traceback 2 | from itertools import chain 3 | import shutil, os, re 4 | from hashlib import sha1 5 | import psutil 6 | import ctypes 7 | 8 | ASTERISKS = r'**********' 9 | user32 = ctypes.WinDLL('user32') 10 | SW_MAXIMISE = 3 11 | window_titles = [] 12 | 13 | GetWindowText = ctypes.windll.user32.GetWindowTextW 14 | GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW 15 | IsWindowVisible = ctypes.windll.user32.IsWindowVisible 16 | EnumWindows = ctypes.windll.user32.EnumWindows 17 | EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) 18 | 19 | 20 | JoinSep = '_' 21 | 22 | def cat_lists(L): 23 | lst = list(chain(*L)) 24 | return lst 25 | 26 | 27 | def convert2float_s(str_t): 28 | try: 29 | dig_t = float(str_t) 30 | except: 31 | dig_t = float('nan') 32 | return dig_t 33 | 34 | 35 | def convert_s(obj): 36 | ret = obj 37 | if isinstance(obj,str): 38 | if obj==ASTERISKS: 39 | ret = float('nan') 40 | else: 41 | for func in [int,float]: 42 | try: 43 | ret = func(obj) 44 | break 45 | except: 46 | pass 47 | return ret 48 | 49 | def copyfiles(path_src, path_dst, files): 50 | if not os.path.isdir(path_dst): 51 | os.makedirs(path_dst) 52 | 53 | for file in files: 54 | file_src = os.path.join(path_src, file) 55 | file_dst = os.path.join(path_dst, file) 56 | shutil.copy(file_src, file_dst) 57 | 58 | 59 | 60 | def copyfiles_pattern(path_src, path_dst, pattern, files=None): 61 | if files is None: 62 | if os.path.isdir(path_src): 63 | files = os.listdir(path_src) 64 | else: 65 | files = [] 66 | if not os.path.isdir(path_dst): 67 | os.makedirs(path_dst) 68 | 69 | for file in files: 70 | if re.match(file,pattern): 71 | file_src = os.path.join(path_src, file) 72 | file_dst = os.path.join(path_dst, file) 73 | shutil.copy(file_src, file_dst) 74 | 75 | 76 | def delete_files_pattern(path_t, pattern): 77 | if isinstance(path_t, str) and isinstance(pattern, str): 78 | if os.path.isdir(path_t): 79 | files = os.listdir(path_t) 80 | files_d = [x for x in files if re.match(pattern, x)] 81 | for fd in files_d: 82 | try: 83 | pfd = os.path.join(path_t, fd) 84 | os.remove(pfd) 85 | except: 86 | print('error in deleting %s' % fd) 87 | 88 | 89 | def get_all_process_legacy(): 90 | attrs_as_dict = ['pid', 'name', 'username', 'exe', 'create_time'] 91 | pid_list = psutil.pids() 92 | list_process = [] 93 | for pid in pid_list: 94 | try: 95 | dict_t = psutil.Process(pid).as_dict(attrs=attrs_as_dict) 96 | list_process.append(dict_t) 97 | except: 98 | traceback.print_exc() 99 | return list_process 100 | 101 | 102 | def get_all_process(name=None): 103 | attrs_as_dict = ['pid', 'name', 'username', 'exe', 'create_time'] 104 | list_process = [] 105 | for r in psutil.process_iter(): 106 | try: 107 | if (name is None) or (isinstance(name, str) and r.name() == name): 108 | dict_t = r.as_dict(attrs=attrs_as_dict) 109 | list_process.append(dict_t) 110 | except: 111 | traceback.print_exc() 112 | return list_process 113 | 114 | 115 | def get_all_process_tasklist(name=None): 116 | keys = ['name', 'exe', 'type', ''] 117 | r = os.popen('tasklist') 118 | A = r.read() 119 | B = [x.split() for x in A.split('\n')] 120 | # TODO: how to split?? 121 | 122 | 123 | def generate_new_files_save_yield(path_save, prefix_save, postfix_save='', try_ori=False, flag_dir = False, 124 | return_path = False, join_underline=True): 125 | if try_ori: 126 | count = -1 127 | else: 128 | count = 0 129 | max_count = 100000 130 | if isinstance(prefix_save, str) and isinstance(postfix_save, str): 131 | if join_underline and prefix_save[-1]!=JoinSep: 132 | prefix_save = prefix_save+JoinSep 133 | while count <= max_count: 134 | if count == -1: 135 | file_name = prefix_save.rstrip(JoinSep) + postfix_save 136 | else: 137 | file_name = prefix_save + str(count) + postfix_save 138 | file_path_t = os.path.join(path_save, file_name) 139 | if flag_dir: 140 | flag_t = os.path.isdir(file_path_t) 141 | else: 142 | flag_t = os.path.isfile(file_path_t) 143 | if not flag_t: 144 | if return_path: 145 | rt = os.path.join(path_save,file_name) 146 | else: 147 | rt = file_name 148 | yield rt 149 | count += 1 150 | 151 | 152 | def formulate_list_of_dicts(list_dicts): 153 | list_keys = union_all_keys(list_dicts) 154 | list_result = arrange_list_dict_by_keys(list_dicts, list_keys) 155 | return list_keys, list_result 156 | 157 | def union_all_keys(list_dicts): 158 | list_keys_raw = [] 159 | for dict_t in list_dicts: 160 | if isinstance(dict_t, dict): 161 | list_keys_raw.extend(dict_t.keys()) 162 | 163 | list_keys = list(set(list_keys_raw)) 164 | 165 | return list_keys 166 | 167 | def arrange_list_dict_by_keys(list_dicts, list_keys, dict_translate=None): 168 | num_keys = len(list_keys) 169 | list_result = [] 170 | if isinstance(dict_translate, dict): 171 | dict_translate_reverse = {v: k for k, v in dict_translate.items()} 172 | keys_o_r = dict_translate_reverse.keys() 173 | keys_o = dict_translate.keys() 174 | else: 175 | dict_translate_reverse = {} 176 | keys_o_r = [] 177 | keys_o = [] 178 | for dict_t in list_dicts: 179 | if isinstance(dict_t, dict): 180 | keys_dict_t = dict_t.keys() 181 | result_t = [] 182 | for key_t in list_keys: 183 | appended = False 184 | if key_t in keys_dict_t: 185 | result_t.append(dict_t[key_t]) 186 | appended = True 187 | elif key_t in keys_o: 188 | key_t_n = dict_translate[key_t] 189 | if key_t_n in keys_dict_t: 190 | result_t.append(dict_t[key_t_n]) 191 | appended = True 192 | elif key_t in keys_o_r: 193 | key_t_n = dict_translate_reverse[key_t] 194 | if key_t_n in keys_dict_t: 195 | result_t.append(dict_t[key_t_n]) 196 | appended = True 197 | if not appended: 198 | result_t.append(None) 199 | else: 200 | result_t = [None] * num_keys 201 | list_result.append(result_t) 202 | return list_result 203 | 204 | 205 | def gen_token(): 206 | return sha1(os.urandom(24)).hexdigest() 207 | 208 | 209 | def hide_window(): 210 | hWnd = user32.GetForegroundWindow() 211 | if hWnd: 212 | user32.ShowWindow(hWnd, 2) 213 | ctypes.windll.kernel32.CloseHandle(hWnd) 214 | 215 | 216 | def foreach_window(hwnd, lParam): 217 | global window_titles 218 | if IsWindowVisible(hwnd): 219 | length = GetWindowTextLength(hwnd) 220 | buff = ctypes.create_unicode_buffer(length + 1) 221 | GetWindowText(hwnd, buff, length + 1) 222 | window_titles.append(buff.value) 223 | return True 224 | 225 | 226 | def foreach_window_hide(hwnd,window_name, lParam): 227 | if IsWindowVisible(hwnd): 228 | length = GetWindowTextLength(hwnd) 229 | buff = ctypes.create_unicode_buffer(length + 1) 230 | GetWindowText(hwnd, buff, length + 1) 231 | if isinstance(buff.value,str): 232 | if str.find(buff.value,window_name)!=-1: 233 | user32.ShowWindow(hwnd, 2) 234 | return True 235 | return False 236 | 237 | 238 | def hide_window_by_name(window_name): 239 | def foreach_window_t(x,y): 240 | return foreach_window_hide(x,window_name,y) 241 | EnumWindows(EnumWindowsProc(foreach_window_t), 0) 242 | 243 | 244 | def get_window_titles(): 245 | global window_titles 246 | window_titles = [] 247 | EnumWindows(EnumWindowsProc(foreach_window), 0) 248 | return window_titles 249 | 250 | 251 | 252 | if __name__=='__main__': 253 | for i in range(100): 254 | a = gen_token() 255 | print(a) 256 | -------------------------------------------------------------------------------- /PyPSASP/utils/utils_sqlite.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sqlite3 3 | import traceback 4 | import datetime 5 | import contextlib 6 | import pickle 7 | from threading import Lock 8 | from PyPSASP.utils.utils_gadgets import formulate_list_of_dicts 9 | 10 | from PyPSASP.constants import const 11 | 12 | ListType = 'LIST' 13 | TupleType = 'TUPLE' 14 | DictType = 'DICT' 15 | DatetimeType = 'DATETIME' 16 | DictLocks = {} 17 | sqlite3.register_converter(DatetimeType, lambda x: datetime.datetime.strptime(x.decode(), '%Y-%m-%d %H:%M:%S')) 18 | sqlite3.register_converter(ListType, pickle.loads) 19 | sqlite3.register_converter(TupleType, pickle.loads) 20 | sqlite3.register_converter(DictType, pickle.loads) 21 | sqlite3.register_adapter(list, pickle.dumps) 22 | sqlite3.register_adapter(dict, pickle.dumps) 23 | sqlite3.register_adapter(tuple, pickle.dumps) 24 | 25 | 26 | @contextlib.contextmanager 27 | def my_sqlite3(db_path): 28 | global DictLocks 29 | if db_path not in DictLocks.keys(): 30 | DictLocks[db_path] = Lock() 31 | if DictLocks[db_path].acquire(): 32 | conn_temp = sqlite3.connect(db_path, detect_types=sqlite3.PARSE_COLNAMES | sqlite3.PARSE_DECLTYPES) 33 | cursor_temp = conn_temp.cursor() 34 | try: 35 | yield cursor_temp 36 | finally: 37 | conn_temp.commit() 38 | cursor_temp.close() 39 | conn_temp.close() 40 | DictLocks[db_path].release() 41 | 42 | 43 | def execute_sql(db_path, X): 44 | with my_sqlite3(db_path) as cursor_temp: 45 | data = None 46 | try: 47 | cursor_temp.execute(X) 48 | data = cursor_temp.fetchall() 49 | except: 50 | traceback.print_exc() 51 | return data 52 | 53 | 54 | def get_keys(db_path, table_name, return_type=False): 55 | with my_sqlite3(db_path) as cursor_temp: 56 | cursor_temp.execute("PRAGMA table_info(""%s"")" % table_name) 57 | keys_info = cursor_temp.fetchall() 58 | keys = [x[1] for x in keys_info] 59 | key_types = [x[2] for x in keys_info] 60 | if return_type: 61 | return keys, key_types 62 | else: 63 | return keys 64 | 65 | 66 | def read_db(db_path, table_name, list_keys=None, str_where=r'', return_dict_form=False): 67 | keys_sel = [] 68 | data = [] 69 | if os.path.isfile(db_path): 70 | flag_not_list = False 71 | if isinstance(list_keys, str): 72 | list_keys = [list_keys] 73 | flag_not_list = True 74 | try: 75 | keys, types = get_keys(db_path, table_name, return_type=True) 76 | dict_key_types = {keys[h]: types[h] for h in range(len(keys))} 77 | if list_keys is None: 78 | dict_key_types_sel = dict_key_types.copy() 79 | else: 80 | dict_key_types_sel = {k: v for k, v in dict_key_types.items() if k in list_keys} 81 | if dict_key_types_sel: 82 | keys_sel = list(dict_key_types_sel.keys()) 83 | key_f_t = ','.join([r'%s as "%s [%s]"' % (k, k, dict_key_types[k]) for k in keys_sel]) 84 | sql = r'SELECT %s FROM %s' % (key_f_t, table_name) 85 | # X = X.replace('[DATETIME]','[TIMESTAMP]') 86 | 87 | sql = sql + ' ' + str_where 88 | with my_sqlite3(db_path) as cursor_temp: 89 | cursor_temp.execute(sql) 90 | data = cursor_temp.fetchall() 91 | if flag_not_list and data and not return_dict_form: 92 | data = [x[0] for x in data] 93 | except: 94 | traceback.print_exc() 95 | print('error while reading %s.%s' % (db_path, table_name)) 96 | 97 | if return_dict_form: 98 | if data: 99 | data_dict_form = [{keys_sel[h]: d[h] for h in range(len(keys_sel))} for d in data] 100 | else: 101 | data_dict_form = [] 102 | return data_dict_form 103 | else: 104 | return keys_sel, data 105 | 106 | 107 | def get_table_names(db_path): 108 | with my_sqlite3(db_path) as cursor_temp: 109 | cursor_temp.execute(r'SELECT name FROM sqlite_master;') 110 | d = cursor_temp.fetchall() 111 | table_names = [x[0] for x in d] 112 | return table_names 113 | 114 | 115 | def create_table(db_path, table_name, dict_keys_type, primary_key=None, 116 | with_time_stamp=True, key_time_stamp=const.KeyInsertTimeStamp): 117 | sup_path_record_file = os.path.abspath(os.path.dirname(db_path)) 118 | if not os.path.exists(sup_path_record_file): 119 | os.makedirs(sup_path_record_file) 120 | 121 | table_names = get_table_names(db_path) 122 | if table_name not in table_names: 123 | 124 | dict_sql_keys = {k: "%s %s" % (k, v) for k, v in dict_keys_type.items()} 125 | if primary_key in dict_sql_keys.keys(): 126 | dict_sql_keys[primary_key] = dict_sql_keys[primary_key] + r' PRIMARY KEY NOT NULL' 127 | 128 | if with_time_stamp: 129 | sql_time_stamp = '%s TimeStamp NOT NULL DEFAULT (datetime(\'now\',\'localtime\'))' % key_time_stamp 130 | dict_sql_keys[key_time_stamp] = sql_time_stamp 131 | 132 | sql_main = "CREATE TABLE %s(%s);" % (table_name, ','.join(dict_sql_keys.values())) 133 | with my_sqlite3(db_path) as cursor_temp: 134 | cursor_temp.execute(sql_main) 135 | 136 | 137 | def add_keys(db_path, table_name, dict_keys_type): 138 | table_names = get_table_names(db_path) 139 | if table_name in table_names: 140 | with my_sqlite3(db_path) as cursor_temp: 141 | cursor_temp.execute("PRAGMA table_info(""%s"")" % table_name) 142 | keys_info = cursor_temp.fetchall() 143 | keys_existed = [x[1] for x in keys_info] 144 | keys_new = list(set(dict_keys_type.keys()).difference(keys_existed)) 145 | for key_new in keys_new: 146 | cursor_temp.execute("ALTER TABLE %s add %s %s" % (table_name, key_new, dict_keys_type[key_new])) 147 | 148 | 149 | def delete_list(db_path, table_name, ref_key, list_value_ref): 150 | table_names = get_table_names(db_path) 151 | if table_name in table_names: 152 | value_ref_f_t = '?' 153 | list_list_value_ref = [[x] for x in list_value_ref] 154 | sql = r'DELETE FROM {} WHERE {}={}'.format(table_name, ref_key, value_ref_f_t) 155 | with my_sqlite3(db_path) as cursor_temp: 156 | cursor_temp.executemany(sql, list_list_value_ref) 157 | 158 | 159 | def insert_from_list_to_db(db_path, table_name, list_keys, list_data, primary_key=None, 160 | with_time_stamp=True, key_time_stamp=const.KeyInsertTimeStamp, 161 | dict_keys_type_set=None): 162 | if list_keys is None: 163 | try: 164 | list_keys, list_data = formulate_list_of_dicts(list_data) 165 | except: 166 | traceback.print_exc() 167 | return 168 | if list_keys and list_data: 169 | if not isinstance(dict_keys_type_set, dict): 170 | dict_keys_type_set = {} 171 | if isinstance(list_keys, str): 172 | list_keys = [list_keys] 173 | list_data = [[x] for x in list_data] 174 | dict_keys_type = {list_keys[h]: dict_keys_type_set[list_keys[h]] if list_keys[ 175 | h] in dict_keys_type_set.keys() else get_type_str_list( 176 | [x[h] for x in list_data]) for h in range(len(list_keys))} 177 | create_table(db_path, table_name, dict_keys_type, primary_key, with_time_stamp, key_time_stamp) 178 | add_keys(db_path, table_name, dict_keys_type) 179 | key_f_t = ('(' + ','.join(['"{}"'] * len(list_keys)) + ')').format(*list_keys) 180 | data_f_t = ('(' + ','.join(['?'] * len(list_keys)) + ')') 181 | sql = r'INSERT OR IGNORE INTO "{}" {} VALUES{}'.format(table_name, key_f_t, data_f_t) 182 | with my_sqlite3(db_path) as cursor_temp: 183 | cursor_temp.executemany(sql, list_data) 184 | 185 | 186 | def update_list_to_db_multiref(db_path, table_name, list_keys_update, list_data_update, keys_ref, list_values_ref, 187 | str_where=r'', dict_keys_type_set=None): 188 | try: 189 | if list_keys_update and list_data_update: 190 | table_names = get_table_names(db_path) 191 | if table_name in table_names: 192 | if not isinstance(dict_keys_type_set, dict): 193 | dict_keys_type_set = {} 194 | if isinstance(keys_ref, str): 195 | keys_ref_t = [keys_ref] 196 | list_values_ref_t = [[x] for x in list_values_ref] 197 | else: 198 | keys_ref_t = keys_ref 199 | list_values_ref_t = list_values_ref 200 | 201 | if isinstance(list_keys_update, str): 202 | list_keys_update_t = [list_keys_update] 203 | list_data_update_t = [[x] for x in list_data_update] 204 | else: 205 | list_keys_update_t = list_keys_update 206 | list_data_update_t = list_data_update 207 | 208 | dict_keys_type = { 209 | list_keys_update_t[h]: dict_keys_type_set[list_keys_update_t[h]] if list_keys_update_t[ 210 | h] in dict_keys_type_set.keys() else get_type_str_list( 211 | [x[h] for x in list_data_update_t]) for h in range(len(list_keys_update_t))} 212 | 213 | add_keys(db_path, table_name, dict_keys_type) 214 | key_and_data_f_t = ','.join([x + '=?' for x in list_keys_update_t]) 215 | key_and_data_ref_t = ' AND '.join([k + '=?' for k in keys_ref_t]) 216 | str_where_t = str_where.strip().lstrip(r'WHERE').lstrip('where') 217 | sql = r'UPDATE {} SET {} WHERE {}{}'.format(table_name, key_and_data_f_t, key_and_data_ref_t, str_where_t) 218 | # cursor_temp.executemany(X,list_result) 219 | list_data = list_data_update_t.copy() 220 | for i in range(len(list_data)): 221 | list_data[i] = list(list_data[i]) 222 | list_data[i].extend(list_values_ref_t[i]) 223 | 224 | with my_sqlite3(db_path) as cursor_temp: 225 | cursor_temp.executemany(sql, list_data) 226 | except: 227 | traceback.print_exc() 228 | 229 | 230 | def get_type_str(obj): 231 | # type_str = 'CHAR' 232 | if isinstance(obj, str): 233 | type_str = 'CHAR' 234 | elif isinstance(obj, bool): 235 | type_str = 'INTEGER' 236 | elif isinstance(obj, int): 237 | type_str = 'INTEGER' 238 | elif isinstance(obj, float): 239 | type_str = 'DOUBLE' 240 | elif isinstance(obj, datetime.datetime): 241 | type_str = DatetimeType 242 | elif isinstance(obj, list): 243 | type_str = ListType 244 | elif isinstance(obj, tuple): 245 | type_str = TupleType 246 | elif isinstance(obj, dict): 247 | type_str = DictType 248 | else: 249 | type_str = 'CHAR' 250 | return type_str 251 | 252 | 253 | def get_type_str_list(list_t): 254 | for obj in list_t: 255 | if obj is not None: 256 | type_str_t = get_type_str(obj) 257 | return type_str_t 258 | return 'CHAR' 259 | 260 | 261 | if __name__ == '__main__': 262 | db_t = 'temp.db' 263 | table_t = 'rec' 264 | keys_t = ['name', 'age', 'experiences'] 265 | data_t = [['Tom', 18, ['primary', 'middle']], ['Jerry', 20, ['CattySchool', 'Others']]] 266 | insert_from_list_to_db(db_t, table_t, keys_t, data_t) 267 | D = read_db(db_t, table_t, return_dict_form=True) 268 | print(D) 269 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyPSASP 2 | use python to manipuate PSASP 3 | 4 | install by pip install --index-url https://test.pypi.org/simple/ --no-deps PyPSASP 5 | 6 | see wiki for instructions. 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md", "r") as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name="PyPSASP", 8 | version="0.2.0", 9 | author="liu2bao", 10 | author_email="liu2bao@yeah.net", 11 | description="Use python to manipulate PSASP", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/liu2bao/PyPSASP", 15 | packages=setuptools.find_packages(), 16 | classifiers=[ 17 | "Programming Language :: Python :: 3", 18 | "License :: OSI Approved :: MIT License", 19 | "Operating System :: Microsoft :: Windows", 20 | ], 21 | ) -------------------------------------------------------------------------------- /temp_CCT.py: -------------------------------------------------------------------------------- 1 | from PyPSASP.PSASPClasses.PSASP import CCT_generator,func_change_lf_temp 2 | from PyPSASP.constants import const 3 | import os, traceback 4 | 5 | ''' 6 | if __name__=='__main__': 7 | from PyPSASP.PSASPClasses.Manipulators import PSASP_Parser,PSASP_Writer 8 | 9 | path_temp = r'E:\01_Research\98_Data\SmallSystem_PSASP\SMIB\2016_06_01T00_00_24' 10 | path_temp_2 = r'E:\01_Research\98_Data\SmallSystem_PSASP\SMIB\2016_06_01T00_00_24_copy' 11 | Parser_t = PSASP_Parser(path_temp) 12 | Writer_t = PSASP_Writer(path_temp_2) 13 | T = Parser_t.parse_all_parsable() 14 | Writer_t.write_all_writable(T) 15 | ''' 16 | 17 | PATH_TEMP = r'E:\LXZ\PythonWorks\PowerSystem\Temp_20190419' 18 | PATH_RESOURCES = PATH_TEMP 19 | PATH_OUTPUT = r'E:\LXZ\Data\Research\PyPSASP\CCT\3m' 20 | 21 | os.system('@echo off') 22 | Cc = CCT_generator(PATH_TEMP, PATH_RESOURCES, PATH_OUTPUT, func_change_lf_temp) 23 | count_t = 0 24 | max_count = 10000 25 | while count_t <= max_count: 26 | try: 27 | rec_t = Cc.run_sim_CCT_once() 28 | count_t += 1 29 | except: 30 | traceback.print_exc() 31 | 32 | --------------------------------------------------------------------------------