DHU_AB.py 32 KB

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