DHU_A.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. import numpy as np
  2. import pandas as pd
  3. import pymc as pm
  4. import pytensor.tensor as pt
  5. from .._base._base_device import BaseDevice
  6. from ...components.coil_water import CoolingCoil2
  7. from ...components.coil_steam import SteamCoilFs,SteamCoilFs2,SteamCoil
  8. from ...components.wheel3 import WheelS3,WheelS3V2,WheelS3V3
  9. from ...components.mixed import Mixed
  10. from ..utils.fit_utils import (
  11. observe,record,reorder_posterior
  12. )
  13. from ...tools.optimizer import optimizer
  14. class DHU_A(BaseDevice):
  15. model_input_data_columns = {
  16. 'Tin_F' : 'coil_1_ToutA',
  17. 'Hin_F' : 'coil_1_HoutA',
  18. 'fan_1_Hz' : 'fan_1_Hz',
  19. 'fan_2_Hz' : 'fan_2_Hz',
  20. 'coil_1_TinW' : 'coil_1_TinW',
  21. 'coil_2_TinW' : 'coil_2_TinW',
  22. 'coil_3_TinW' : 'coil_3_TinW',
  23. 'coil_1_Val' : 'coil_1_Val',
  24. 'coil_2_Val' : 'coil_2_Val',
  25. 'coil_3_Val' : 'coil_3_Val',
  26. 'wheel_1_TinR': 'wheel_1_TinR',
  27. 'wheel_2_TinR': 'wheel_2_TinR',
  28. 'mixed_1_TinM': 'mixed_1_TinM',
  29. 'mixed_2_TinM': 'mixed_2_TinM',
  30. 'mixed_1_HinM': 'mixed_1_HinM',
  31. 'mixed_2_HinM': 'mixed_2_HinM',
  32. }
  33. model_observe_data_columns = {
  34. 'mixed_1_ToutA' : 'mixed_1_ToutA',
  35. 'mixed_1_DoutA' : 'mixed_1_DoutA',
  36. 'wheel_1_ToutC' : 'wheel_1_ToutC',
  37. 'coil_2_ToutA' : 'coil_2_ToutA',
  38. 'coil_2_DoutA' : 'coil_2_DoutA',
  39. 'wheel_2_ToutP' : 'wheel_2_ToutP',
  40. 'wheel_2_DoutP' : 'wheel_2_DoutP',
  41. 'wheel_2_ToutR' : 'wheel_2_ToutR',
  42. # 'steamcoil_1_FP' : 'steamcoil_1_FP',
  43. # 'steamcoil_2_FP' : 'steamcoil_2_FP',
  44. # 'steamcoil_1_Fs' : 'steamcoil_1_Fs',
  45. # 'steamcoil_2_Fs' : 'steamcoil_2_Fs',
  46. # 'steamcoil_1_Val': 'steamcoil_1_Val',
  47. # 'steamcoil_2_Val': 'steamcoil_2_Val',
  48. }
  49. def __init__(self) -> None:
  50. super().__init__()
  51. self.components = [
  52. WheelS3('wheel_1'),
  53. WheelS3('wheel_2'),
  54. CoolingCoil2('coil_2'),
  55. CoolingCoil2('coil_3'),
  56. SteamCoil('steamcoil_1'),
  57. SteamCoil('steamcoil_2'),
  58. # SteamCoilFs2('steamcoil_1'),
  59. # SteamCoilFs('steamcoil_2'),
  60. Mixed('mixed_1'),
  61. Mixed('mixed_2'),
  62. ]
  63. self.components = {comp.name:comp for comp in self.components}
  64. def fit(
  65. self,
  66. input_data : pd.DataFrame,
  67. observed_data: pd.DataFrame,
  68. exist_Fa_H : bool,
  69. exist_Fa_B : bool,
  70. rw_FA_val : bool = False,
  71. plot_TVP : bool = True,
  72. ):
  73. with pm.Model() as self.MODEL_PYMC:
  74. param_prior = {name:comp.prior() for name,comp in self.components.items()}
  75. param_prior['F_air'] = AirFlow_DHU_AB.prior(
  76. rw_FA_val = rw_FA_val,
  77. N = len(input_data),
  78. exist_Fa_H = exist_Fa_H,
  79. exist_Fa_B = exist_Fa_B
  80. )
  81. res = self.model(
  82. **{k:input_data.loc[:,v].values for k,v in self.model_input_data_columns.items()},
  83. engine = 'pymc',
  84. components = self.components,
  85. param = param_prior
  86. )
  87. for std_name,name in self.model_observe_data_columns.items():
  88. if name not in observed_data.columns:
  89. continue
  90. observed_data = observed_data.rename(columns={name:std_name})
  91. observe('mixed_1_ToutA',res['mixed_1']['ToutA'],observed=observed_data)
  92. observe('mixed_1_DoutA',res['mixed_1']['DoutA'],observed=observed_data)
  93. observe('wheel_1_ToutC',res['wheel_1']['ToutC'],observed=observed_data)
  94. observe('coil_2_ToutA',res['coil_2']['ToutA'],observed=observed_data)
  95. observe('coil_2_DoutA',res['coil_2']['DoutA'],observed=observed_data)
  96. observe('wheel_2_ToutP',res['wheel_2']['ToutP'],observed=observed_data)
  97. observe('wheel_2_DoutP',res['wheel_2']['DoutP'],observed=observed_data,sigma=0.3)
  98. observe('wheel_2_ToutR',res['wheel_2']['ToutR'],observed=observed_data)
  99. #TODO observe wheel_2_ToutC
  100. # observe('steamcoil_1_FP',res['steamcoil_1']['FP'],observed=observed_data,sigma=10000)
  101. # observe('steamcoil_1_Fs',res['steamcoil_1']['Fs'],observed=observed_data,sigma=20)
  102. # observe('steamcoil_2_Fs',res['steamcoil_2']['Fs'],observed=observed_data,sigma=20)
  103. record('steamcoil_1_Fs',res['steamcoil_1']['Fs'])
  104. record('steamcoil_2_Fs',res['steamcoil_2']['Fs'])
  105. record('wheel_2_ToutC',res['wheel_2']['ToutC'])
  106. record('mixed_2_ToutA',res['mixed_2']['ToutA'])
  107. record('wheel_1_FaP',res['wheel_1']['FP'])
  108. record('wheel_1_FaR',res['wheel_1']['FR'])
  109. record('wheel_1_FaC',res['wheel_1']['FC'])
  110. record('mixed_1_FaA',res['mixed_1']['FA'])
  111. record('mixed_1_FaM',res['mixed_1']['FM'])
  112. record('F_air_S',res['Fa']['Fa_S'])
  113. record('F_air_H',res['Fa']['Fa_H'])
  114. record('F_air_X',res['Fa']['Fa_X'])
  115. self.param_posterior = pm.find_MAP(maxeval=50000,include_transformed=False)
  116. self.record_model(
  117. model_name = 'ATD',
  118. model = reorder_posterior(param_prior,self.param_posterior),
  119. train_data = {'x':np.array([1])},
  120. train_metric = {'R2':1,'MAE':1,'MAPE':1}
  121. )
  122. self.TVP_data = self.get_TVP(self.param_posterior,observed_data)
  123. self.TVP_metric = self.get_metric(self.TVP_data)
  124. if plot_TVP:
  125. self.plot_TVP(self.TVP_data).show()
  126. return self
  127. def optimize(
  128. self,
  129. cur_input_data : pd.DataFrame,
  130. wheel_1_TinR_ub: float = 120,
  131. wheel_1_TinR_lb: float = 70,
  132. wheel_2_TinR_ub: float = 120,
  133. wheel_2_TinR_lb: float = 70,
  134. constrains : list = None,
  135. logging : bool = True
  136. ) -> list:
  137. constrains = [] if constrains is None else constrains
  138. cur_input_data = cur_input_data.iloc[[0],:]
  139. opt_var_boundary = {
  140. 'wheel_1_TinR':{'lb':wheel_1_TinR_lb,'ub':wheel_1_TinR_ub},
  141. 'wheel_2_TinR':{'lb':wheel_2_TinR_lb,'ub':wheel_2_TinR_ub},
  142. }
  143. opt_var_value = cur_input_data.loc[:,list(opt_var_boundary.keys())]
  144. oth_var_value = (
  145. cur_input_data
  146. .loc[:,list(self.model_input_data_columns.values())]
  147. .drop(opt_var_value.columns,axis=1)
  148. )
  149. opt_res = optimizer(
  150. model = self,
  151. opt_var_boundary = opt_var_boundary,
  152. opt_var_value = opt_var_value,
  153. oth_var_value = oth_var_value,
  154. target = 'summary_Fs',
  155. target_min = True,
  156. constrains = constrains,
  157. logging = logging,
  158. other_kwargs = {'NIND':2000,'MAXGEN':50}
  159. )
  160. return opt_res
  161. @classmethod
  162. def model(
  163. cls,
  164. Tin_F, # 前表冷后温度
  165. Hin_F, # 前表冷后湿度
  166. fan_1_Hz, # 处理侧风机频率
  167. fan_2_Hz, # 再生侧风机频率
  168. coil_1_TinW, # 前表冷进水温度
  169. coil_2_TinW, # 中表冷进水温度
  170. coil_3_TinW, # 后表冷进水温度
  171. coil_1_Val, # 前表冷阀门开度
  172. coil_2_Val, # 中表冷阀门开度
  173. coil_3_Val, # 后表冷阀门开度
  174. wheel_1_TinR, # 前转轮再生侧温度
  175. wheel_2_TinR, # 后转轮再生侧温度
  176. mixed_1_TinM, # 回风温度(处理侧)
  177. mixed_1_HinM, # 回风湿度(处理侧)
  178. mixed_2_TinM, # 补风温度(再生侧)
  179. mixed_2_HinM, # 补风湿度(再生侧)
  180. engine : str,
  181. components: dict,
  182. param : dict,
  183. ) -> dict:
  184. # 水的质量流量
  185. coil_2_FW = coil_2_Val / 100
  186. coil_3_FW = coil_3_Val / 100
  187. # 空气的质量流量
  188. air_flow = AirFlow_DHU_AB.model(fan_1_Hz=fan_1_Hz,fan_2_Hz=fan_2_Hz,param=param,type='DHU_A')
  189. # 前转轮
  190. wheel_1_res = components['wheel_1'].model(
  191. TinP = Tin_F,
  192. HinP = Hin_F,
  193. FP = air_flow['wheel_1_FaP'],
  194. TinR = wheel_1_TinR,
  195. HinR = 0,
  196. FR = air_flow['wheel_1_FaR'],
  197. TinC = Tin_F,
  198. HinC = Hin_F,
  199. FC = air_flow['wheel_1_FaC'],
  200. engine = engine,
  201. param = param['wheel_1']
  202. )
  203. # 处理侧混风(回风)
  204. mixed_1_res = components['mixed_1'].model(
  205. TinA = wheel_1_res['ToutP'],
  206. HinA = wheel_1_res['HoutP'],
  207. FA = air_flow['mixed_1_FaA'],
  208. TinM = mixed_1_TinM,
  209. HinM = mixed_1_HinM,
  210. FM = air_flow['mixed_1_FaM'],
  211. engine = engine
  212. )
  213. # 中表冷
  214. coil_2_res = components['coil_2'].model(
  215. TinA = mixed_1_res['ToutA'],
  216. HinA = mixed_1_res['HoutA'],
  217. FA = air_flow['coil_2_FaA'],
  218. TinW = coil_2_TinW,
  219. FW = coil_2_FW,
  220. engine = engine,
  221. param = param['coil_2']
  222. )
  223. # 后转轮
  224. wheel_2_res = components['wheel_2'].model(
  225. TinP = coil_2_res['ToutA'],
  226. HinP = coil_2_res['HoutA'],
  227. FP = air_flow['wheel_2_FaP'],
  228. TinC = wheel_1_res['ToutC'],
  229. HinC = wheel_1_res['HoutC'],
  230. FC = air_flow['wheel_2_FaC'],
  231. TinR = wheel_2_TinR,
  232. HinR = 0,
  233. FR = air_flow['wheel_2_FaR'],
  234. engine = engine,
  235. param = param['wheel_2'],
  236. )
  237. # 后表冷
  238. coil_3_res = components['coil_3'].model(
  239. TinA = wheel_2_res['ToutP'],
  240. HinA = wheel_2_res['HoutP'],
  241. FA = air_flow['coil_3_FaA'],
  242. TinW = coil_3_TinW,
  243. FW = coil_3_FW,
  244. engine = engine,
  245. param = param['coil_3']
  246. )
  247. # 后转轮湿度修正
  248. wheel_2_res_adj = components['wheel_2'].model(
  249. TinP = coil_2_res['ToutA'],
  250. HinP = coil_2_res['HoutA'],
  251. FP = air_flow['wheel_2_FaP'],
  252. TinC = wheel_1_res['ToutC'],
  253. HinC = wheel_1_res['HoutC'],
  254. FC = air_flow['wheel_2_FaC'],
  255. TinR = wheel_2_TinR,
  256. HinR = wheel_2_res['HoutC'],
  257. FR = air_flow['wheel_2_FaR'],
  258. engine = engine,
  259. param = param['wheel_2'],
  260. )
  261. # 再生侧混风(排风)
  262. mixed_2_res = components['mixed_2'].model(
  263. TinA = wheel_2_res_adj['ToutR'],
  264. HinA = wheel_2_res_adj['HoutR'],
  265. FA = air_flow['mixed_2_FaA'],
  266. TinM = mixed_2_TinM,
  267. HinM = mixed_2_HinM,
  268. FM = air_flow['mixed_2_FaM'],
  269. engine = engine
  270. )
  271. # 前转轮湿度修正
  272. wheel_1_res_adj = components['wheel_1'].model(
  273. TinP = Tin_F,
  274. HinP = Hin_F,
  275. FP = air_flow['wheel_1_FaP'],
  276. TinR = wheel_1_TinR,
  277. HinR = mixed_2_res['HoutA'],
  278. FR = air_flow['wheel_1_FaR'],
  279. TinC = Tin_F,
  280. HinC = Hin_F,
  281. FC = air_flow['wheel_1_FaC'],
  282. engine = engine,
  283. param = param['wheel_1']
  284. )
  285. # 前蒸气盘管
  286. steamcoil_1_res = components['steamcoil_1'].model(
  287. TinA = mixed_2_res['ToutA'],
  288. ToutA = wheel_1_TinR,
  289. FA = air_flow['steamcoil_1_Fa'],
  290. param = param['steamcoil_1'],
  291. engine = engine
  292. )
  293. # 后蒸气盘管
  294. steamcoil_2_res = components['steamcoil_2'].model(
  295. TinA = wheel_2_res_adj['ToutC'],
  296. ToutA = wheel_2_TinR,
  297. FA = air_flow['steamcoil_2_Fa'],
  298. param = param['steamcoil_2'],
  299. engine = engine
  300. )
  301. return {
  302. 'coil_2' : coil_2_res,
  303. 'coil_3' : coil_3_res,
  304. 'wheel_1' : wheel_1_res_adj,
  305. 'wheel_2' : wheel_2_res_adj,
  306. 'mixed_1' : mixed_1_res,
  307. 'mixed_2' : mixed_2_res,
  308. 'steamcoil_1': steamcoil_1_res,
  309. 'steamcoil_2': steamcoil_2_res,
  310. 'Fa' : air_flow,
  311. 'summary' : {
  312. 'Fs':steamcoil_1_res['Fs'] + steamcoil_2_res['Fs'],
  313. }
  314. }
  315. class AirFlow_DHU_AB:
  316. @classmethod
  317. def model(cls,fan_1_Hz,fan_2_Hz,param,type):
  318. # 当定频风机固定的时候,各出入口处的基准的风量
  319. F_air_S_base = 1
  320. F_air_X_base = param['F_air']['X_base']
  321. F_air_H_base = param['F_air'].get('H_base',0)
  322. F_air_B_base = param['F_air'].get('B_base',0)
  323. F_air_val_rw = param['F_air'].get('val_rw',0)
  324. F_air_val_pct = param['F_air'].get('val_pct',0)
  325. # 新风阀的变化造成的基准风量变化
  326. F_air_S_base_adj = F_air_S_base
  327. F_air_X_base_adj = F_air_X_base + F_air_val_rw
  328. 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
  329. 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
  330. # F_air_X_base_adj = F_air_X_base + F_air_val_rw
  331. # F_air_B_base_adj = F_air_B_base - F_air_val_rw * F_air_val_pct[0] if 'B_base' in param['F_air'] else 0
  332. # F_air_S_base_adj = F_air_S_base + F_air_val_rw * F_air_val_pct[1] if 'H_base' in param['F_air'] else 0
  333. # F_air_H_base_adj = F_air_H_base - F_air_val_rw * F_air_val_pct[2] if 'H_base' in param['F_air'] else 0
  334. # 考虑风机频率变化对风量的影响,得到最终风量
  335. # 因为从数据上看,排风机的频率不会影响到新风量,所以在新风阀不动的情况下,新风的增加量可以认为全部进入送风
  336. F_air_HzP_X = param['F_air']['HzP_X']
  337. F_air_HzP_H = param['F_air'].get('HzP_H',0)
  338. F_air_HzP_S = F_air_HzP_X + F_air_HzP_H
  339. F_air_HzR_B = param['F_air'].get('HzR_B',0)
  340. Fa_S = F_air_S_base_adj + F_air_HzP_S * (fan_1_Hz / 50)
  341. Fa_H = F_air_H_base_adj + F_air_HzP_H * (fan_1_Hz / 50)
  342. Fa_X = F_air_X_base_adj + F_air_HzP_X * (fan_1_Hz / 50)
  343. Fa_B = F_air_B_base_adj + F_air_HzR_B * (fan_2_Hz / 50)
  344. Fa_P = Fa_B + Fa_X + Fa_H - Fa_S
  345. if type == 'DHU_A':
  346. wheel_1_FaP = Fa_S - Fa_H
  347. wheel_1_FaC = Fa_X - wheel_1_FaP
  348. wheel_1_FaR = Fa_P
  349. wheel_2_FaP = Fa_S
  350. wheel_2_FaC = wheel_1_FaC
  351. wheel_2_FaR = wheel_1_FaC
  352. mixed_1_FaM = Fa_H
  353. mixed_1_FaA = wheel_1_FaP
  354. mixed_2_FaM = Fa_B
  355. mixed_2_FaA = wheel_1_FaC
  356. coil_2_FaA = Fa_S
  357. coil_3_FaA = Fa_S
  358. steamcoil_1_Fa = Fa_P
  359. steamcoil_2_Fa = wheel_1_FaC
  360. elif type == 'DHU_B':
  361. wheel_1_FaP = Fa_X
  362. wheel_1_FaC = None
  363. wheel_1_FaR = Fa_P
  364. wheel_2_FaP = Fa_S
  365. wheel_2_FaC = Fa_X + Fa_H - Fa_S
  366. wheel_2_FaR = wheel_2_FaC
  367. mixed_1_FaM = Fa_H
  368. mixed_1_FaA = Fa_X
  369. mixed_2_FaM = Fa_B
  370. mixed_2_FaA = wheel_2_FaC
  371. coil_2_FaA = Fa_S
  372. coil_3_FaA = Fa_S
  373. steamcoil_1_Fa = Fa_P
  374. steamcoil_2_Fa = wheel_2_FaC
  375. else:
  376. raise Exception('type error')
  377. return {
  378. 'Fa_S':Fa_S,'Fa_H':Fa_H,'Fa_X':Fa_X,'Fa_B':Fa_B,'Fa_P':Fa_P,
  379. 'wheel_1_FaP':wheel_1_FaP,'wheel_1_FaC':wheel_1_FaC,'wheel_1_FaR':wheel_1_FaR,
  380. 'wheel_2_FaP':wheel_2_FaP,'wheel_2_FaC':wheel_2_FaC,'wheel_2_FaR':wheel_2_FaR,
  381. 'mixed_1_FaM':mixed_1_FaM,'mixed_1_FaA':mixed_1_FaA,
  382. 'mixed_2_FaM':mixed_2_FaM,'mixed_2_FaA':mixed_2_FaA,
  383. 'coil_2_FaA':coil_2_FaA,'coil_3_FaA':coil_3_FaA,
  384. 'steamcoil_1_Fa':steamcoil_1_Fa,'steamcoil_2_Fa':steamcoil_2_Fa
  385. }
  386. @classmethod
  387. def prior(
  388. cls,
  389. rw_FA_val : bool,
  390. N : int,
  391. exist_Fa_H: bool,
  392. exist_Fa_B: bool
  393. ) -> dict:
  394. param = {}
  395. # 新风参数
  396. param['HzP_X'] = pm.HalfNormal('F_air_HzP_X',sigma=1,initval=1)
  397. param['X_base'] = pm.TruncatedNormal('F_air_X_base',mu=0.5,sigma=0.2,lower=0,initval=0.5)
  398. if exist_Fa_H:
  399. param['HzP_H'] = pm.HalfNormal('F_air_HzP_H',sigma=1,initval=0.1)
  400. param['H_base'] = pm.TruncatedNormal('F_air_H_base',mu=0.6,sigma=0.2,lower=0,upper=0.999,initval=0.6)
  401. if exist_Fa_B:
  402. param['HzR_B'] = pm.HalfNormal('F_air_HzR_B',sigma=1,initval=0.5)
  403. param['B_base'] = pm.TruncatedNormal('F_air_B_base',mu=0.2,sigma=0.1,lower=0,initval=0.1)
  404. if rw_FA_val:
  405. period = 48
  406. n_segments = int(np.ceil(N/period))
  407. remainder = N % period
  408. repeat = [period] * (n_segments - 1) + ([remainder] if remainder != 0 else [])
  409. rw = pm.GaussianRandomWalk(
  410. 'rw',sigma=0.1,init_dist=pm.Normal.dist(mu=0,sigma=0.3),shape=n_segments)
  411. param['val_rw'] = pm.Deterministic('F_air_val_rw',pt.repeat(rw,repeat))
  412. param['val_pct'] = pm.Beta('F_air_val_pct',alpha=8,beta=1,initval=0.9)
  413. # 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]))
  414. else:
  415. param['val_rw'] = 0
  416. param['val_pct'] = 0
  417. return param