DHU_AB.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. from typing import Union
  2. import numpy as np
  3. import pandas as pd
  4. import pymc as pm
  5. import pytensor.tensor as pt
  6. from .._base._base_device import BaseDevice
  7. from ...components import (
  8. coil_water,coil_steam,wheel2,wheel3,mixed
  9. )
  10. from ..utils.fit_utils import (
  11. observe,reorder_posterior
  12. )
  13. from ...tools.optimizer import optimizer
  14. from ...tools.data_cleaner import DataCleaner
  15. class DHU_AB(BaseDevice):
  16. val_rw_adj_target = ('coil_2_DoutA','wheel_2_DoutP')
  17. def __init__(
  18. self,
  19. DHU_type = 'A',
  20. exist_Fa_H = True,
  21. exist_Fa_B = True,
  22. wheel_1 = None,
  23. wheel_2 = None,
  24. coolingcoil_2 = 'CoolingCoil2',
  25. coolingcoil_3 = 'CoolingCoil2',
  26. heatingcoil_1 = 'SteamCoil',
  27. heatingcoil_2 = 'SteamCoil',
  28. mixed_1 = 'Mixed',
  29. mixed_2 = 'Mixed',
  30. other_info = None
  31. ) -> None:
  32. super().__init__()
  33. self.DHU_type = DHU_type.replace('DHU_','')
  34. if self.DHU_type == 'A':
  35. wheel_1 = wheel_1 if wheel_1 is not None else 'WheelS3V3'
  36. wheel_2 = wheel_2 if wheel_2 is not None else 'WheelS3V2'
  37. elif self.DHU_type == 'B':
  38. wheel_1 = wheel_1 if wheel_1 is not None else 'WheelS2V2'
  39. wheel_2 = wheel_2 if wheel_2 is not None else 'WheelS3V3'
  40. else:
  41. raise Exception('DHU_type must be A or B')
  42. self.components_str = {
  43. 'wheel_1' : wheel_1,
  44. 'wheel_2' : wheel_2,
  45. 'coil_2' : coolingcoil_2,
  46. 'coil_3' : coolingcoil_3,
  47. 'heatingcoil_1': heatingcoil_1,
  48. 'heatingcoil_2': heatingcoil_2,
  49. 'mixed_1' : mixed_1,
  50. 'mixed_2' : mixed_2
  51. }
  52. self.exist_Fa_H = exist_Fa_H
  53. self.exist_Fa_B = exist_Fa_B
  54. self.other_info = other_info if other_info is not None else {}
  55. self.record_load_info(
  56. components_str = self.components_str,
  57. DHU_type = self.DHU_type,
  58. exist_Fa_H = self.exist_Fa_H,
  59. exist_Fa_B = self.exist_Fa_B,
  60. other_info = self.other_info
  61. )
  62. @property
  63. def components(self):
  64. comp_map = {
  65. 'WheelS2':wheel2,'WheelS3':wheel3,'CoolingCoil':coil_water,
  66. 'SteamCoil':coil_steam,'Mixed':mixed
  67. }
  68. output ={}
  69. for comp_name,comp_model in self.components_str.items():
  70. if comp_model == 'SteamCoilVal':
  71. output[comp_name] = coil_steam.SteamCoilVal(
  72. name = comp_name,
  73. Fs_rated = self.other_info[f'{comp_name}_Fs_rated']
  74. )
  75. continue
  76. for comp_map_k,comp_map_v in comp_map.items():
  77. if comp_model.startswith(comp_map_k):
  78. output[comp_name] = getattr(comp_map_v,comp_model)(name = comp_name)
  79. return output
  80. @property
  81. def model_input_data_columns(self):
  82. columns = {
  83. 'Tin_F' : 'coil_1_ToutA',
  84. 'Hin_F' : 'coil_1_HoutA',
  85. 'fan_1_Hz' : 'fan_1_Hz',
  86. 'fan_2_Hz' : 'fan_2_Hz',
  87. 'coil_1_TinW' : 'coil_1_TinW',
  88. 'coil_2_TinW' : 'coil_2_TinW',
  89. 'coil_3_TinW' : 'coil_3_TinW',
  90. 'coil_1_Val' : 'coil_1_Val',
  91. 'coil_2_Val' : 'coil_2_Val',
  92. 'coil_3_Val' : 'coil_3_Val',
  93. 'wheel_1_TinR': 'wheel_1_TinR',
  94. 'wheel_2_TinR': 'wheel_2_TinR',
  95. }
  96. if self.exist_Fa_H:
  97. columns['mixed_1_TinM'] = 'mixed_1_TinM'
  98. columns['mixed_1_HinM'] = 'mixed_1_HinM'
  99. if self.exist_Fa_B:
  100. columns['mixed_2_TinM'] = 'mixed_2_TinM'
  101. columns['mixed_2_HinM'] = 'mixed_2_HinM'
  102. return columns
  103. @property
  104. def model_observe_data_columns(self):
  105. columns = {
  106. 'mixed_1_ToutA': 'mixed_1_ToutA',
  107. 'mixed_1_DoutA': 'mixed_1_DoutA',
  108. 'coil_2_ToutA' : 'coil_2_ToutA',
  109. 'coil_2_DoutA' : 'coil_2_DoutA',
  110. 'wheel_2_ToutP': 'wheel_2_ToutP',
  111. 'wheel_2_DoutP': 'wheel_2_DoutP',
  112. 'mixed_2_ToutA': 'mixed_2_ToutA', # 涉及前再生加热盘管的热量
  113. 'wheel_2_ToutC': 'wheel_2_ToutC', # 涉及后再生加热盘管的热量
  114. }
  115. if self.DHU_type == 'A':
  116. columns['wheel_1_ToutC'] = 'wheel_1_ToutC' # A类除湿机前转轮是三分转轮
  117. for idx in [1,2]:
  118. heatingcoil_idx = f'heatingcoil_{idx}'
  119. if isinstance(self.components[heatingcoil_idx],coil_steam.SteamCoilFs2):
  120. columns[f'{heatingcoil_idx}_FP'] = f'{heatingcoil_idx}_FP'
  121. columns[f'{heatingcoil_idx}_Fs'] = f'{heatingcoil_idx}_Fs'
  122. elif isinstance(self.components[heatingcoil_idx],coil_steam.SteamCoilFs):
  123. columns[f'{heatingcoil_idx}_Fs'] = f'{heatingcoil_idx}_Fs'
  124. elif isinstance(self.components[heatingcoil_idx],coil_steam.SteamCoil):
  125. # columns['wheel_2_ToutC'] = 'wheel_2_ToutC'
  126. # columns['wheel_1_ToutR'] = 'wheel_1_ToutR'
  127. # columns['mixed_2_ToutA'] = 'mixed_2_ToutA'
  128. pass
  129. elif isinstance(self.components[heatingcoil_idx],coil_steam.SteamCoilVal):
  130. columns[f'{heatingcoil_idx}_Val'] = f'{heatingcoil_idx}_Val'
  131. else:
  132. raise Exception('WRONG')
  133. exclude_obs = self.other_info.get('exclude_obs',[])
  134. for col in exclude_obs:
  135. if col in columns:
  136. del columns[col]
  137. return columns
  138. def fit(
  139. self,
  140. input_data : pd.DataFrame,
  141. observed_data: pd.DataFrame,
  142. rw_FA_val : bool = False,
  143. plot_TVP : bool = True,
  144. ):
  145. if len(input_data) < 30:
  146. raise Exception('数据量过少')
  147. with pm.Model() as self.MODEL_PYMC:
  148. param_prior = {name:comp.prior() for name,comp in self.components.items()}
  149. param_prior['F_air'] = AirFlow_DHU_AB.prior(
  150. rw_FA_val = rw_FA_val,
  151. N = len(input_data),
  152. exist_Fa_H = self.exist_Fa_H,
  153. exist_Fa_B = self.exist_Fa_B
  154. )
  155. res = self.model(
  156. **{k:input_data.loc[:,v].values for k,v in self.model_input_data_columns.items()},
  157. engine = 'pymc',
  158. components = self.components,
  159. param = param_prior
  160. )
  161. for std_name,name in self.model_observe_data_columns.items():
  162. if name not in observed_data.columns:
  163. raise Exception(f'Missing column: {name}')
  164. observed_data = observed_data.rename(columns={name:std_name})
  165. std_name_equp,std_name_point = std_name.rsplit('_',1)
  166. sigma = {
  167. 'wheel_2_DoutP' : 0.3,
  168. 'heatingcoil_1_Fs': 20,
  169. 'heatingcoil_2_Fs': 20,
  170. 'heatingcoil_1_FP': 10000,
  171. 'heatingcoil_2_FP': 10000,
  172. }
  173. if std_name in ['heatingcoil_1_Val','heatingcoil_2_Val']:
  174. sigma = res[std_name_equp]['sigma']
  175. else:
  176. sigma = {
  177. 'wheel_2_DoutP' : 0.3,
  178. 'heatingcoil_1_Fs': 20,
  179. 'heatingcoil_2_Fs': 20,
  180. 'heatingcoil_1_FP': 10000,
  181. 'heatingcoil_2_FP': 10000,
  182. }.get(std_name,1)
  183. observe(
  184. name = std_name,
  185. var = res[std_name_equp][std_name_point],
  186. observed = observed_data,
  187. sigma = sigma
  188. )
  189. self.param_posterior = pm.find_MAP(maxeval=50000,include_transformed=False)
  190. self.record_load_info(
  191. param_posterior = self.param_posterior
  192. )
  193. self.record_model(
  194. model_name = 'ATD',
  195. model = reorder_posterior(param_prior,self.param_posterior),
  196. train_data = {
  197. 'wheel_1_TinR' : observed_data.loc[:,'wheel_1_TinR'].values,
  198. 'wheel_2_TinR' : observed_data.loc[:,'wheel_2_TinR'].values,
  199. 'wheel_2_DoutP': observed_data.loc[:,'wheel_2_DoutP'].values,
  200. },
  201. train_metric = {'R2':1,'MAE':1,'MAPE':1}
  202. )
  203. self.TVP_data = self.get_TVP(self.param_posterior,observed_data)
  204. self.TVP_metric = self.get_metric(self.TVP_data)
  205. if plot_TVP:
  206. self.plot_TVP(self.TVP_data).show()
  207. return self
  208. @property
  209. def F_air_val_rw(self):
  210. return self.model_info['model_ATD']['F_air']['val_rw']
  211. def set_F_air_val_rw(self,value:float):
  212. self.model_info['model_ATD']['F_air']['val_rw'] = value
  213. return self
  214. def clean_data(
  215. self,
  216. data : pd.DataFrame,
  217. data_type : list=['input','observed'],
  218. print_process: bool = True,
  219. fill_zero : bool = False,
  220. save_log : Union[str,None] = None
  221. ) -> pd.DataFrame:
  222. data = data.replace(-9999,np.nan)
  223. clean_data = DataCleaner(data,print_process=print_process)
  224. if 'input' in data_type:
  225. clean_data = (
  226. clean_data
  227. .rm_rolling_fluct(window=60,fun='ptp',thre=0.1,include_cols=['State'])
  228. .rm_rule('State != 1')
  229. .rm_outrange(method='raw',upper=140,lower=20,include_cols=['wheel_1_TinR','wheel_2_TinR'])
  230. )
  231. if self.DHU_type == 'A':
  232. clean_data = (
  233. clean_data
  234. .rm_rule('wheel_1_ToutC<=coil_1_ToutA')
  235. )
  236. if 'observed' in data_type:
  237. pass
  238. clean_data = clean_data.get_data(
  239. fill = 0 if fill_zero else None,
  240. save_log = save_log
  241. )
  242. return clean_data
  243. def optimize(
  244. self,
  245. cur_input_data: pd.DataFrame,
  246. wheel_1_TinR : tuple = (70,120),
  247. wheel_2_TinR : tuple = (70,120),
  248. fan_2_Hz : tuple = (30,50),
  249. constrains : list = None,
  250. logging : bool = True,
  251. target : str = 'summary_Fs',
  252. target_min : bool = True
  253. ) -> list:
  254. constrains = [] if constrains is None else constrains
  255. cur_input_data = cur_input_data.iloc[[0],:]
  256. opt_var_boundary = {}
  257. if wheel_1_TinR is not None:
  258. opt_var_boundary['wheel_1_TinR'] = {'lb':min(wheel_1_TinR),'ub':max(wheel_1_TinR)}
  259. if wheel_2_TinR is not None:
  260. opt_var_boundary['wheel_2_TinR'] = {'lb':min(wheel_2_TinR),'ub':max(wheel_2_TinR)}
  261. if fan_2_Hz is not None:
  262. opt_var_boundary['fan_2_Hz'] = {'lb':min(fan_2_Hz),'ub':max(fan_2_Hz)}
  263. opt_var_value = cur_input_data.loc[:,list(opt_var_boundary.keys())]
  264. oth_var_value = (
  265. cur_input_data
  266. .loc[:,list(self.model_input_data_columns.values())]
  267. .drop(opt_var_value.columns,axis=1)
  268. )
  269. opt_res = optimizer(
  270. model = self,
  271. opt_var_boundary = opt_var_boundary,
  272. opt_var_value = opt_var_value,
  273. oth_var_value = oth_var_value,
  274. target = target,
  275. target_min = target_min,
  276. constrains = constrains,
  277. logging = logging,
  278. other_kwargs = {'NIND':2000,'MAXGEN':50}
  279. )
  280. return opt_res
  281. def model(self,*args,**kwargs):
  282. if self.DHU_type == 'A':
  283. return model_A(*args,**kwargs)
  284. elif self.DHU_type == 'B':
  285. return model_B(*args,**kwargs)
  286. else:
  287. raise ValueError('DHU_type must be A or B')
  288. def plot_opt(
  289. self,
  290. cur_input_data: pd.DataFrame,
  291. target_min : str = 'summary_waste',
  292. coil_3_DoutA : tuple = None
  293. ):
  294. if coil_3_DoutA is None:
  295. coil_3_DoutA = (
  296. self.model_info['model_train_info_ATD']['wheel_2_DoutP_min'],
  297. self.model_info['model_train_info_ATD']['wheel_2_DoutP_max']
  298. )
  299. data_input = (
  300. pd.MultiIndex.from_product(
  301. [
  302. np.linspace(
  303. self.model_info['model_train_info_ATD']['wheel_1_TinR_min']-5,
  304. self.model_info['model_train_info_ATD']['wheel_1_TinR_max']+5,
  305. 1000
  306. ),
  307. np.linspace(
  308. self.model_info['model_train_info_ATD']['wheel_2_TinR_min']-5,
  309. self.model_info['model_train_info_ATD']['wheel_2_TinR_max']+5,
  310. 1000
  311. ),
  312. ],
  313. names=['wheel_1_TinR','wheel_2_TinR']
  314. )
  315. .to_frame(index=False)
  316. )
  317. for col in cur_input_data.columns:
  318. if col in data_input.columns:
  319. continue
  320. data_input[col] = cur_input_data.loc[:,col].iat[0]
  321. data_output = self.predict_system(data_input)
  322. data = (
  323. data_output
  324. .assign(
  325. wheel_1_TinR = data_input.loc[:,'wheel_1_TinR'],
  326. wheel_2_TinR = data_input.loc[:,'wheel_2_TinR'],
  327. )
  328. .assign(coil_3_DoutA=lambda dt:dt.coil_3_DoutA.round(1))
  329. .loc[lambda dt:dt.coil_3_DoutA.between(*(min(coil_3_DoutA),max(coil_3_DoutA)))]
  330. .loc[lambda dt:dt.groupby('coil_3_DoutA')[target_min].idxmin()]
  331. .loc[lambda dt:dt.coil_3_DoutA.mod(1)==0]
  332. )
  333. import plotnine as gg
  334. plot = (
  335. data
  336. .pipe(gg.ggplot)
  337. + gg.aes(x='wheel_1_TinR',y='wheel_2_TinR')
  338. + gg.geom_path(size=1)
  339. + gg.geom_point()
  340. + gg.geom_label(gg.aes(label='coil_3_DoutA'))
  341. + gg.geom_abline(slope=1,intercept=0,color='red',linetype='--')
  342. )
  343. return plot
  344. def plot_check(self,cur_input_data:pd.DataFrame) -> dict:
  345. pa1=self.curve(input_data=cur_input_data,x='wheel_1_TinR',y='wheel_1_DoutP')
  346. pa2=self.curve(input_data=cur_input_data,x='wheel_1_TinR',y='wheel_1_ToutP')
  347. pa3=self.curve(input_data=cur_input_data,x='wheel_1_TinR',y='wheel_1_EFF')
  348. pb1=self.curve(input_data=cur_input_data,x='wheel_2_TinR',y='wheel_2_DoutP')
  349. pb2=self.curve(input_data=cur_input_data,x='wheel_2_TinR',y='wheel_2_ToutP')
  350. pb3=self.curve(input_data=cur_input_data,x='wheel_2_TinR',y='wheel_2_EFF')
  351. plot_EFF = (pa1|pa2|pa3)/(pb1|pb2|pb3)
  352. p1=self.curve(x='wheel_1_TinR',y='summary_waste_cond1',input_data=cur_input_data)
  353. p2=self.curve(x='wheel_2_TinR',y='summary_waste_cond2',input_data=cur_input_data)
  354. p3=self.curve(x='wheel_1_TinR',y='summary_waste_Qsen1',input_data=cur_input_data)
  355. p4=self.curve(x='wheel_2_TinR',y='summary_waste_Qsen2',input_data=cur_input_data)
  356. p5=self.curve(x='wheel_1_TinR',y='summary_waste_out',input_data=cur_input_data)
  357. p6=self.curve(x='wheel_2_TinR',y='summary_waste_out',input_data=cur_input_data)
  358. plot_waste = (p1|p3|p5)/(p2|p4|p6)
  359. plot_opt = self.plot_opt(cur_input_data)
  360. return {'plot_EFF':plot_EFF,'plot_waste':plot_waste,'plot_opt':plot_opt}
  361. def model_A(
  362. Tin_F, # 前表冷后温度
  363. Hin_F, # 前表冷后湿度
  364. fan_1_Hz, # 处理侧风机频率
  365. fan_2_Hz, # 再生侧风机频率
  366. coil_1_TinW, # 前表冷进水温度
  367. coil_2_TinW, # 中表冷进水温度
  368. coil_3_TinW, # 后表冷进水温度
  369. coil_1_Val, # 前表冷阀门开度
  370. coil_2_Val, # 中表冷阀门开度
  371. coil_3_Val, # 后表冷阀门开度
  372. wheel_1_TinR, # 前转轮再生侧温度
  373. wheel_2_TinR, # 后转轮再生侧温度
  374. engine : str,
  375. components: dict,
  376. param : dict,
  377. mixed_1_TinM = 0, # 回风温度(处理侧)
  378. mixed_1_HinM = 0, # 回风湿度(处理侧)
  379. mixed_2_TinM = 0, # 补风温度(再生侧)
  380. mixed_2_HinM = 0, # 补风湿度(再生侧)
  381. ) -> dict:
  382. # 水的质量流量
  383. coil_2_FW = coil_2_Val / 100
  384. coil_3_FW = coil_3_Val / 100
  385. # 空气的质量流量
  386. air_flow = AirFlow_DHU_AB.model(fan_1_Hz=fan_1_Hz,fan_2_Hz=fan_2_Hz,param=param,type='DHU_A')
  387. # 前转轮
  388. wheel_1_res = components['wheel_1'].model(
  389. TinP = Tin_F,
  390. HinP = Hin_F,
  391. FP = air_flow['wheel_1_FaP'],
  392. TinR = wheel_1_TinR,
  393. HinR = 0,
  394. FR = air_flow['wheel_1_FaR'],
  395. TinC = Tin_F,
  396. HinC = Hin_F,
  397. FC = air_flow['wheel_1_FaC'],
  398. engine = engine,
  399. param = param['wheel_1']
  400. )
  401. # 处理侧混风(回风)
  402. mixed_1_res = components['mixed_1'].model(
  403. TinA = wheel_1_res['ToutP'],
  404. HinA = wheel_1_res['HoutP'],
  405. FA = air_flow['mixed_1_FaA'],
  406. TinM = mixed_1_TinM,
  407. HinM = mixed_1_HinM,
  408. FM = air_flow['mixed_1_FaM'],
  409. engine = engine
  410. )
  411. # 中表冷
  412. coil_2_res = components['coil_2'].model(
  413. TinA = mixed_1_res['ToutA'],
  414. HinA = mixed_1_res['HoutA'],
  415. FA = air_flow['coil_2_FaA'],
  416. TinW = coil_2_TinW,
  417. FW = coil_2_FW,
  418. engine = engine,
  419. param = param['coil_2']
  420. )
  421. # 后转轮
  422. wheel_2_res = components['wheel_2'].model(
  423. TinP = coil_2_res['ToutA'],
  424. HinP = coil_2_res['HoutA'],
  425. FP = air_flow['wheel_2_FaP'],
  426. TinC = wheel_1_res['ToutC'],
  427. HinC = wheel_1_res['HoutC'],
  428. FC = air_flow['wheel_2_FaC'],
  429. TinR = wheel_2_TinR,
  430. HinR = 0,
  431. FR = air_flow['wheel_2_FaR'],
  432. engine = engine,
  433. param = param['wheel_2'],
  434. )
  435. # 后表冷
  436. coil_3_res = components['coil_3'].model(
  437. TinA = wheel_2_res['ToutP'],
  438. HinA = wheel_2_res['HoutP'],
  439. FA = air_flow['coil_3_FaA'],
  440. TinW = coil_3_TinW,
  441. FW = coil_3_FW,
  442. engine = engine,
  443. param = param['coil_3']
  444. )
  445. # 后转轮湿度修正
  446. wheel_2_res_adj = components['wheel_2'].model(
  447. TinP = coil_2_res['ToutA'],
  448. HinP = coil_2_res['HoutA'],
  449. FP = air_flow['wheel_2_FaP'],
  450. TinC = wheel_1_res['ToutC'],
  451. HinC = wheel_1_res['HoutC'],
  452. FC = air_flow['wheel_2_FaC'],
  453. TinR = wheel_2_TinR,
  454. HinR = wheel_2_res['HoutC'],
  455. FR = air_flow['wheel_2_FaR'],
  456. engine = engine,
  457. param = param['wheel_2'],
  458. )
  459. # 再生侧混风(排风)
  460. mixed_2_res = components['mixed_2'].model(
  461. TinA = wheel_2_res_adj['ToutR'],
  462. HinA = wheel_2_res_adj['HoutR'],
  463. FA = air_flow['mixed_2_FaA'],
  464. TinM = mixed_2_TinM,
  465. HinM = mixed_2_HinM,
  466. FM = air_flow['mixed_2_FaM'],
  467. engine = engine
  468. )
  469. # 前转轮湿度修正
  470. wheel_1_res_adj = components['wheel_1'].model(
  471. TinP = Tin_F,
  472. HinP = Hin_F,
  473. FP = air_flow['wheel_1_FaP'],
  474. TinR = wheel_1_TinR,
  475. HinR = mixed_2_res['HoutA'],
  476. FR = air_flow['wheel_1_FaR'],
  477. TinC = Tin_F,
  478. HinC = Hin_F,
  479. FC = air_flow['wheel_1_FaC'],
  480. engine = engine,
  481. param = param['wheel_1']
  482. )
  483. # 前再生加热盘管
  484. heatingcoil_1_res = components['heatingcoil_1'].model(
  485. TinA = mixed_2_res['ToutA'],
  486. ToutA = wheel_1_TinR,
  487. FA = air_flow['heatingcoil_1_Fa'],
  488. param = param['heatingcoil_1'],
  489. engine = engine
  490. )
  491. # 后再生加热盘管
  492. heatingcoil_2_res = components['heatingcoil_2'].model(
  493. TinA = wheel_2_res_adj['ToutC'],
  494. ToutA = wheel_2_TinR,
  495. FA = air_flow['heatingcoil_2_Fa'],
  496. param = param['heatingcoil_2'],
  497. engine = engine
  498. )
  499. waste = cal_Q_waste(
  500. wheel_1_res = wheel_1_res_adj,
  501. wheel_2_res = wheel_2_res_adj,
  502. heatingcoil_1_res = heatingcoil_1_res,
  503. heatingcoil_2_res = heatingcoil_2_res,
  504. wheel_1_TinR = wheel_1_TinR,
  505. wheel_2_TinR = wheel_2_TinR
  506. )
  507. return {
  508. 'coil_2' : coil_2_res,
  509. 'coil_3' : coil_3_res,
  510. 'wheel_1' : wheel_1_res_adj,
  511. 'wheel_2' : wheel_2_res_adj,
  512. 'mixed_1' : mixed_1_res,
  513. 'mixed_2' : mixed_2_res,
  514. 'heatingcoil_1': heatingcoil_1_res,
  515. 'heatingcoil_2': heatingcoil_2_res,
  516. 'Fa' : air_flow,
  517. 'summary' : {
  518. 'Fs' : heatingcoil_1_res['Fs'] + heatingcoil_2_res['Fs'],
  519. **waste,
  520. }
  521. }
  522. def model_B(
  523. Tin_F, # 前表冷后温度
  524. Hin_F, # 前表冷后湿度
  525. fan_1_Hz, # 处理侧风机频率
  526. fan_2_Hz, # 再生侧风机频率
  527. coil_1_TinW, # 前表冷进水温度
  528. coil_2_TinW, # 中表冷进水温度
  529. coil_3_TinW, # 后表冷进水温度
  530. coil_1_Val, # 前表冷阀门开度
  531. coil_2_Val, # 中表冷阀门开度
  532. coil_3_Val, # 后表冷阀门开度
  533. wheel_1_TinR, # 前转轮再生侧温度
  534. wheel_2_TinR, # 后转轮再生侧温度
  535. engine : str,
  536. components: dict,
  537. param : dict,
  538. mixed_1_TinM = 0, # 回风温度(处理侧)
  539. mixed_1_HinM = 0, # 回风湿度(处理侧)
  540. mixed_2_TinM = 0, # 补风温度(再生侧)
  541. mixed_2_HinM = 0, # 补风湿度(再生侧)
  542. ) -> dict:
  543. # 水的质量流量
  544. coil_2_FW = coil_2_Val / 100
  545. coil_3_FW = coil_3_Val / 100
  546. # 空气的质量流量
  547. air_flow = AirFlow_DHU_AB.model(fan_1_Hz=fan_1_Hz,fan_2_Hz=fan_2_Hz,param=param,type='DHU_B')
  548. # 前转轮
  549. wheel_1_res = components['wheel_1'].model(
  550. TinP = Tin_F,
  551. HinP = Hin_F,
  552. FP = air_flow['wheel_1_FaP'],
  553. TinR = wheel_1_TinR,
  554. HinR = 0,
  555. FR = air_flow['wheel_1_FaR'],
  556. engine = engine,
  557. param = param['wheel_1'],
  558. )
  559. # 处理侧混风(回风)
  560. mixed_1_res = components['mixed_1'].model(
  561. TinA = wheel_1_res['ToutP'],
  562. HinA = wheel_1_res['HoutP'],
  563. FA = air_flow['mixed_1_FaA'],
  564. TinM = mixed_1_TinM,
  565. HinM = mixed_1_HinM,
  566. FM = air_flow['mixed_1_FaM'],
  567. engine = engine
  568. )
  569. # 中表冷
  570. coil_2_res = components['coil_2'].model(
  571. TinA = mixed_1_res['ToutA'],
  572. HinA = mixed_1_res['HoutA'],
  573. FA = air_flow['coil_2_FaA'],
  574. TinW = coil_2_TinW,
  575. FW = coil_2_FW,
  576. engine = engine,
  577. param = param['coil_2']
  578. )
  579. # 后转轮
  580. wheel_2_res = components['wheel_2'].model(
  581. TinP = coil_2_res['ToutA'],
  582. HinP = coil_2_res['HoutA'],
  583. FP = air_flow['wheel_2_FaP'],
  584. TinC = mixed_1_res['ToutA'],
  585. HinC = mixed_1_res['HoutA'],
  586. FC = air_flow['wheel_2_FaC'],
  587. TinR = wheel_2_TinR,
  588. HinR = 0,
  589. FR = air_flow['wheel_2_FaR'],
  590. engine = engine,
  591. param = param['wheel_2'],
  592. )
  593. # 后表冷
  594. coil_3_res = components['coil_3'].model(
  595. TinA = wheel_2_res['ToutP'],
  596. HinA = wheel_2_res['HoutP'],
  597. FA = air_flow['coil_3_FaA'],
  598. TinW = coil_3_TinW,
  599. FW = coil_3_FW,
  600. engine = engine,
  601. param = param['coil_3']
  602. )
  603. # 后转轮湿度修正
  604. wheel_2_res_adj = components['wheel_2'].model(
  605. TinP = coil_2_res['ToutA'],
  606. HinP = coil_2_res['HoutA'],
  607. FP = air_flow['wheel_2_FaP'],
  608. TinC = mixed_1_res['ToutA'],
  609. HinC = mixed_1_res['HoutA'],
  610. FC = air_flow['wheel_2_FaC'],
  611. TinR = wheel_2_TinR,
  612. HinR = wheel_2_res['HoutC'],
  613. FR = air_flow['wheel_2_FaR'],
  614. engine = engine,
  615. param = param['wheel_2'],
  616. )
  617. # 再生侧混风(排风)
  618. mixed_2_res = components['mixed_2'].model(
  619. TinA = wheel_2_res_adj['ToutR'],
  620. HinA = wheel_2_res_adj['HoutR'],
  621. FA = air_flow['mixed_2_FaA'],
  622. TinM = mixed_2_TinM,
  623. HinM = mixed_2_HinM,
  624. FM = air_flow['mixed_2_FaM'],
  625. engine = engine
  626. )
  627. # 前转轮湿度修正
  628. wheel_1_res_adj = components['wheel_1'].model(
  629. TinP = Tin_F,
  630. HinP = Hin_F,
  631. FP = air_flow['wheel_1_FaP'],
  632. TinR = wheel_1_TinR,
  633. HinR = mixed_2_res['HoutA'],
  634. FR = air_flow['wheel_1_FaR'],
  635. engine = engine,
  636. param = param['wheel_1'],
  637. )
  638. # 前蒸气盘管
  639. heatingcoil_1_res = components['heatingcoil_1'].model(
  640. TinA = mixed_2_res['ToutA'],
  641. ToutA = wheel_1_TinR,
  642. FA = air_flow['heatingcoil_1_Fa'],
  643. param = param['heatingcoil_1'],
  644. engine = engine
  645. )
  646. # 后蒸气盘管
  647. heatingcoil_2_res = components['heatingcoil_2'].model(
  648. TinA = wheel_2_res_adj['ToutC'],
  649. ToutA = wheel_2_TinR,
  650. FA = air_flow['heatingcoil_2_Fa'],
  651. param = param['heatingcoil_2'],
  652. engine = engine
  653. )
  654. waste = cal_Q_waste(
  655. wheel_1_res = wheel_1_res_adj,
  656. wheel_2_res = wheel_2_res_adj,
  657. heatingcoil_1_res = heatingcoil_1_res,
  658. heatingcoil_2_res = heatingcoil_2_res,
  659. wheel_1_TinR = wheel_1_TinR,
  660. wheel_2_TinR = wheel_2_TinR
  661. )
  662. return {
  663. 'coil_2' : coil_2_res,
  664. 'coil_3' : coil_3_res,
  665. 'wheel_1' : wheel_1_res_adj,
  666. 'wheel_2' : wheel_2_res_adj,
  667. 'mixed_1' : mixed_1_res,
  668. 'mixed_2' : mixed_2_res,
  669. 'heatingcoil_1': heatingcoil_1_res,
  670. 'heatingcoil_2': heatingcoil_2_res,
  671. 'Fa' : air_flow,
  672. 'summary' : {
  673. 'Fs' : heatingcoil_1_res['Fs'] + heatingcoil_2_res['Fs'],
  674. **waste
  675. }
  676. }
  677. class AirFlow_DHU_AB:
  678. @classmethod
  679. def model(cls,fan_1_Hz,fan_2_Hz,param,type):
  680. # 当定频风机固定的时候,各出入口处的基准的风量
  681. F_air_S_base = 1
  682. F_air_X_base = param['F_air']['X_base']
  683. F_air_H_base = param['F_air'].get('H_base',0)
  684. F_air_B_base = param['F_air'].get('B_base',0)
  685. F_air_val_rw = param['F_air'].get('val_rw',0)
  686. F_air_val_pct = param['F_air'].get('val_pct',0)
  687. # 新风阀的变化造成的基准风量变化
  688. F_air_S_base_adj = F_air_S_base
  689. F_air_X_base_adj = F_air_X_base + F_air_val_rw
  690. F_air_H_base_adj = F_air_H_base - F_air_val_rw * F_air_val_pct if 'H_base' in param['F_air'] else 0
  691. F_air_B_base_adj = F_air_B_base - F_air_val_rw * (1 - F_air_val_pct) if 'B_base' in param['F_air'] else 0
  692. # 考虑风机频率变化对风量的影响,得到最终风量
  693. F_air_HzP_X = param['F_air']['HzP_X']
  694. F_air_HzP_H = param['F_air'].get('HzP_H',0)
  695. F_air_HzP_S = F_air_HzP_X + F_air_HzP_H
  696. F_air_HzR_B = param['F_air'].get('HzR_B',0)
  697. Fa_S = F_air_S_base_adj + F_air_HzP_S * (fan_1_Hz / 50)
  698. Fa_H = F_air_H_base_adj + F_air_HzP_H * (fan_1_Hz / 50)
  699. Fa_X = F_air_X_base_adj + F_air_HzP_X * (fan_1_Hz / 50)
  700. Fa_B = F_air_B_base_adj + F_air_HzR_B * (fan_2_Hz / 50)
  701. Fa_P = Fa_B + Fa_X + Fa_H - Fa_S
  702. if type == 'DHU_A':
  703. wheel_1_FaP = Fa_S - Fa_H
  704. wheel_1_FaC = Fa_X - wheel_1_FaP
  705. wheel_1_FaR = Fa_P
  706. wheel_2_FaP = Fa_S
  707. wheel_2_FaC = wheel_1_FaC
  708. wheel_2_FaR = wheel_1_FaC
  709. mixed_1_FaM = Fa_H
  710. mixed_1_FaA = wheel_1_FaP
  711. mixed_2_FaM = Fa_B
  712. mixed_2_FaA = wheel_1_FaC
  713. coil_2_FaA = Fa_S
  714. coil_3_FaA = Fa_S
  715. heatingcoil_1_Fa = Fa_P
  716. heatingcoil_2_Fa = wheel_1_FaC
  717. elif type == 'DHU_B':
  718. wheel_1_FaP = Fa_X
  719. wheel_1_FaC = np.nan
  720. wheel_1_FaR = Fa_P
  721. wheel_2_FaP = Fa_S
  722. wheel_2_FaC = Fa_X + Fa_H - Fa_S
  723. wheel_2_FaR = wheel_2_FaC
  724. mixed_1_FaM = Fa_H
  725. mixed_1_FaA = Fa_X
  726. mixed_2_FaM = Fa_B
  727. mixed_2_FaA = wheel_2_FaC
  728. coil_2_FaA = Fa_S
  729. coil_3_FaA = Fa_S
  730. heatingcoil_1_Fa = Fa_P
  731. heatingcoil_2_Fa = wheel_2_FaC
  732. else:
  733. raise Exception('type error')
  734. return {
  735. 'Fa_S':Fa_S,'Fa_H':Fa_H,'Fa_X':Fa_X,'Fa_B':Fa_B,'Fa_P':Fa_P,
  736. 'wheel_1_FaP':wheel_1_FaP,'wheel_1_FaC':wheel_1_FaC,'wheel_1_FaR':wheel_1_FaR,
  737. 'wheel_2_FaP':wheel_2_FaP,'wheel_2_FaC':wheel_2_FaC,'wheel_2_FaR':wheel_2_FaR,
  738. 'mixed_1_FaM':mixed_1_FaM,'mixed_1_FaA':mixed_1_FaA,
  739. 'mixed_2_FaM':mixed_2_FaM,'mixed_2_FaA':mixed_2_FaA,
  740. 'coil_2_FaA':coil_2_FaA,'coil_3_FaA':coil_3_FaA,
  741. 'heatingcoil_1_Fa':heatingcoil_1_Fa,'heatingcoil_2_Fa':heatingcoil_2_Fa
  742. }
  743. @classmethod
  744. def prior(
  745. cls,
  746. rw_FA_val : bool,
  747. N : int,
  748. exist_Fa_H: bool,
  749. exist_Fa_B: bool
  750. ) -> dict:
  751. param = {}
  752. # 新风参数
  753. param['HzP_X'] = pm.HalfNormal('F_air_HzP_X',sigma=1,initval=1)
  754. X_base_initval = 0.5 if exist_Fa_H else 1.5
  755. param['X_base'] = pm.TruncatedNormal(
  756. 'F_air_X_base',mu=0.5,sigma=0.2,lower=0,initval=X_base_initval)
  757. if exist_Fa_H:
  758. param['HzP_H'] = pm.HalfNormal('F_air_HzP_H',sigma=1,initval=0.1)
  759. param['H_base'] = pm.TruncatedNormal('F_air_H_base',mu=0.6,sigma=0.2,lower=0,upper=0.999,initval=0.6)
  760. if exist_Fa_B:
  761. param['HzR_B'] = pm.HalfNormal('F_air_HzR_B',sigma=1,initval=0.5)
  762. param['B_base'] = pm.TruncatedNormal('F_air_B_base',mu=0.2,sigma=0.1,lower=0,initval=0.1)
  763. if rw_FA_val:
  764. period = 48
  765. n_segments = int(np.ceil(N/period))
  766. remainder = N % period
  767. repeat = [period] * (n_segments - 1) + ([remainder] if remainder != 0 else [])
  768. rw = pm.GaussianRandomWalk(
  769. 'rw',sigma=0.1,init_dist=pm.Normal.dist(mu=0,sigma=0.3),shape=n_segments)
  770. rw = pm.math.switch(rw<0,0,rw)
  771. param['val_rw'] = pm.Deterministic('F_air_val_rw',pt.repeat(rw,repeat))
  772. param['val_pct'] = pm.Beta('F_air_val_pct',alpha=8,beta=1,initval=0.9)
  773. # param['val_pct'] = pm.Dirichlet('F_air_val_pct',np.array([0.1,0.1,0.8]),initval=np.array([0.1,0.1,0.8]))
  774. else:
  775. param['val_rw'] = 0
  776. param['val_pct'] = 0
  777. return param
  778. def cal_Q_waste(
  779. wheel_1_res,
  780. wheel_2_res,
  781. heatingcoil_1_res,
  782. heatingcoil_2_res,
  783. wheel_1_TinR,
  784. wheel_2_TinR
  785. ) -> dict:
  786. def waste_cond_func1(TinR):
  787. waste = 0.15 + 0.0001 * (TinR-70)**3
  788. return np.where(waste>0,waste,0)
  789. def waste_cond_func2(TinR):
  790. waste = 0.25 * (1 - np.exp(-0.04 * (TinR - 70)))
  791. return np.where(waste>0,waste,0)
  792. waste_Qsen1 = wheel_1_res['Qsen']
  793. waste_Qsen2 = wheel_2_res['Qsen']
  794. waste_cond1 = heatingcoil_1_res['Q'] * waste_cond_func1(wheel_1_TinR)
  795. waste_cond2 = heatingcoil_2_res['Q'] * waste_cond_func1(wheel_2_TinR)
  796. waste_out = (
  797. heatingcoil_1_res['Q'] + heatingcoil_2_res['Q']
  798. - wheel_1_res['Qsen'] - wheel_1_res['Qlat']
  799. - wheel_2_res['Qsen'] - wheel_2_res['Qlat']
  800. )
  801. return {
  802. 'waste_Qsen1': waste_Qsen1,
  803. 'waste_Qsen2': waste_Qsen2,
  804. 'waste_Qout' : waste_out,
  805. 'waste_cond1': waste_cond1,
  806. 'waste_cond2': waste_cond2,
  807. 'waste_out' : waste_out,
  808. # 'waste' : waste_cond2+waste_cond1,
  809. 'waste' : waste_Qsen1+waste_Qsen2+waste_cond1+waste_cond2+waste_out,
  810. }