DHU_A.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.wheel import WheelS3
  9. from ...components.mixed import Mixed
  10. from ..utils.fit_utils import (
  11. observe,record,reorder_posterior,get_fitted_result
  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. rw_FA_val : bool = False,
  69. plot_TVP : bool = True
  70. ):
  71. with pm.Model() as self.MODEL_PYMC:
  72. param_prior = {name:comp.prior() for name,comp in self.components.items()}
  73. param_prior['F_air'] = AirFlow.prior(rw_FA_val=rw_FA_val,N=len(input_data))
  74. res = DHU_A.model(
  75. **{k:input_data.loc[:,v].values for k,v in self.model_input_data_columns.items()},
  76. engine = 'pymc',
  77. components = self.components,
  78. param = param_prior
  79. )
  80. for std_name,name in self.model_observe_data_columns.items():
  81. if name not in observed_data.columns:
  82. continue
  83. observed_data = observed_data.rename(columns={name:std_name})
  84. observe('mixed_1_ToutA',res['mixed_1']['ToutA'],observed=observed_data)
  85. observe('mixed_1_DoutA',res['mixed_1']['DoutA'],observed=observed_data)
  86. observe('wheel_1_ToutC',res['wheel_1']['ToutC'],observed=observed_data)
  87. observe('coil_2_ToutA',res['coil_2']['ToutA'],observed=observed_data)
  88. observe('coil_2_DoutA',res['coil_2']['DoutA'],observed=observed_data)
  89. observe('wheel_2_ToutP',res['wheel_2']['ToutP'],observed=observed_data)
  90. observe('wheel_2_DoutP',res['wheel_2']['DoutP'],observed=observed_data)
  91. observe('wheel_2_ToutR',res['wheel_2']['ToutR'],observed=observed_data)
  92. observe('steamcoil_1_FP',res['steamcoil_1']['FP'],observed=observed_data,sigma=1000)
  93. observe('steamcoil_1_Fs',res['steamcoil_1']['Fs'],observed=observed_data,sigma=20)
  94. observe('steamcoil_2_Fs',res['steamcoil_2']['Fs'],observed=observed_data,sigma=20)
  95. # record('steamcoil_1_Fs',res['steamcoil_1']['Fs'])
  96. # record('steamcoil_2_Fs',res['steamcoil_2']['Fs'])
  97. record('wheel_2_ToutC',res['wheel_2']['ToutC'])
  98. record('mixed_2_ToutA',res['mixed_2']['ToutA'])
  99. record('wheel_1_FaP',res['wheel_1']['FP'])
  100. record('wheel_1_FaR',res['wheel_1']['FR'])
  101. record('wheel_1_FaC',res['wheel_1']['FC'])
  102. record('mixed_1_FaA',res['mixed_1']['FA'])
  103. record('mixed_1_FaM',res['mixed_1']['FM'])
  104. record('F_air_S',res['Fa']['Fa_S'])
  105. record('F_air_H',res['Fa']['Fa_H'])
  106. record('F_air_X',res['Fa']['Fa_X'])
  107. self.param_posterior = pm.find_MAP(maxeval=50000,include_transformed=False)
  108. self.record_model(
  109. model_name = 'DHU',
  110. model = reorder_posterior(param_prior,self.param_posterior),
  111. train_data = {'x':np.array([1])},
  112. train_metric = {'R2':1,'MAE':1,'MAPE':1}
  113. )
  114. self.TVP_data,self.TVP_metric = get_fitted_result(self.param_posterior,observed_data,plot_TVP)
  115. return self
  116. def predict(self,input_data:pd.DataFrame) -> dict:
  117. param_posterior = self.model_info['model_DHU']
  118. res = DHU_A.model(
  119. **{k:input_data.loc[:,v].values for k,v in self.model_input_data_columns.items()},
  120. engine = 'numpy',
  121. components = self.components,
  122. param = param_posterior
  123. )
  124. return res
  125. def predict_system(self,input_data:pd.DataFrame) -> pd.DataFrame:
  126. pred_res = self.predict(input_data)
  127. system_output = {}
  128. for equp_name,output_info in pred_res.items():
  129. for output_name,output_value in output_info.items():
  130. system_output[f'{equp_name}_{output_name}'] = output_value
  131. system_output = pd.DataFrame(system_output)
  132. system_output['Fs'] = system_output.steamcoil_1_Fs + system_output.steamcoil_2_Fs
  133. return system_output
  134. def optimize(
  135. self,
  136. cur_input_data : pd.DataFrame,
  137. wheel_1_TinR_ub: float = 120,
  138. wheel_1_TinR_lb: float = 70,
  139. wheel_2_TinR_ub: float = 120,
  140. wheel_2_TinR_lb: float = 70,
  141. constrains : list = None
  142. ) -> list:
  143. constrains = [] if constrains is None else constrains
  144. cur_input_data = cur_input_data.iloc[[0],:]
  145. opt_var_boundary = {
  146. 'wheel_1_TinR':{'lb':wheel_1_TinR_lb,'ub':wheel_1_TinR_ub},
  147. 'wheel_2_TinR':{'lb':wheel_2_TinR_lb,'ub':wheel_2_TinR_ub},
  148. }
  149. opt_var_value = cur_input_data.loc[:,list(opt_var_boundary.keys())]
  150. oth_var_value = (
  151. cur_input_data
  152. .loc[:,list(self.model_input_data_columns.values())]
  153. .drop(opt_var_value.columns,axis=1)
  154. )
  155. opt_res = optimizer(
  156. model = self,
  157. opt_var_boundary = opt_var_boundary,
  158. opt_var_value = opt_var_value,
  159. oth_var_value = oth_var_value,
  160. constrains = constrains
  161. )
  162. return opt_res
  163. @classmethod
  164. def model(
  165. cls,
  166. Tin_F, # 前表冷后温度
  167. Hin_F, # 前表冷后湿度
  168. fan_1_Hz, # 处理侧风机频率
  169. fan_2_Hz, # 再生侧风机频率
  170. coil_1_TinW, # 前表冷进水温度
  171. coil_2_TinW, # 中表冷进水温度
  172. coil_3_TinW, # 后表冷进水温度
  173. coil_1_Val, # 前表冷阀门开度
  174. coil_2_Val, # 中表冷阀门开度
  175. coil_3_Val, # 后表冷阀门开度
  176. wheel_1_TinR, # 前转轮再生侧温度
  177. wheel_2_TinR, # 后转轮再生侧温度
  178. mixed_1_TinM, # 回风温度(处理侧)
  179. mixed_1_HinM, # 回风湿度(处理侧)
  180. mixed_2_TinM, # 补风温度(再生侧)
  181. mixed_2_HinM, # 补风湿度(再生侧)
  182. engine : str,
  183. components: dict,
  184. param : dict,
  185. ) -> dict:
  186. # 水的质量流量
  187. coil_2_FW = coil_2_Val / 100
  188. coil_3_FW = coil_3_Val / 100
  189. # 空气的质量流量
  190. air_flow = AirFlow.model(fan_1_Hz=fan_1_Hz,fan_2_Hz=fan_2_Hz,param=param)
  191. # 前转轮
  192. wheel_1_res = components['wheel_1'].model(
  193. TinP = Tin_F,
  194. HinP = Hin_F,
  195. FP = air_flow['wheel_1_FaP'],
  196. TinR = wheel_1_TinR,
  197. HinR = 0,
  198. FR = air_flow['wheel_1_FaR'],
  199. TinC = Tin_F,
  200. HinC = Hin_F,
  201. FC = air_flow['wheel_1_FaC'],
  202. engine = engine,
  203. param = param['wheel_1']
  204. )
  205. # 处理侧混风(回风)
  206. mixed_1_res = components['mixed_1'].model(
  207. TinA = wheel_1_res['ToutP'],
  208. HinA = wheel_1_res['HoutP'],
  209. FA = air_flow['mixed_1_FaA'],
  210. TinM = mixed_1_TinM,
  211. HinM = mixed_1_HinM,
  212. FM = air_flow['mixed_1_FaM'],
  213. engine = engine
  214. )
  215. # 中表冷
  216. coil_2_res = components['coil_2'].model(
  217. TinA = mixed_1_res['ToutA'],
  218. HinA = mixed_1_res['HoutA'],
  219. FA = air_flow['coil_2_FaA'],
  220. TinW = coil_2_TinW,
  221. FW = coil_2_FW,
  222. engine = engine,
  223. param = param['coil_2']
  224. )
  225. # 后转轮
  226. wheel_2_res = components['wheel_2'].model(
  227. TinP = coil_2_res['ToutA'],
  228. HinP = coil_2_res['HoutA'],
  229. FP = air_flow['wheel_2_FaP'],
  230. TinC = wheel_1_res['ToutC'],
  231. HinC = wheel_1_res['HoutC'],
  232. FC = air_flow['wheel_2_FaC'],
  233. TinR = wheel_2_TinR,
  234. HinR = 0,
  235. FR = air_flow['wheel_2_FaR'],
  236. engine = engine,
  237. param = param['wheel_2'],
  238. )
  239. # 后表冷
  240. coil_3_res = components['coil_3'].model(
  241. TinA = wheel_2_res['ToutP'],
  242. HinA = wheel_2_res['HoutP'],
  243. FA = air_flow['coil_3_FaA'],
  244. TinW = coil_3_TinW,
  245. FW = coil_3_FW,
  246. engine = engine,
  247. param = param['coil_3']
  248. )
  249. # 后转轮湿度修正
  250. wheel_2_res_adj = components['wheel_2'].model(
  251. TinP = coil_2_res['ToutA'],
  252. HinP = coil_2_res['HoutA'],
  253. FP = air_flow['wheel_2_FaP'],
  254. TinC = wheel_1_res['ToutC'],
  255. HinC = wheel_1_res['HoutC'],
  256. FC = air_flow['wheel_2_FaC'],
  257. TinR = wheel_2_TinR,
  258. HinR = wheel_2_res['HoutC'],
  259. FR = air_flow['wheel_2_FaR'],
  260. engine = engine,
  261. param = param['wheel_2'],
  262. )
  263. # 再生侧混风(排风)
  264. mixed_2_res = components['mixed_2'].model(
  265. TinA = wheel_2_res_adj['ToutR'],
  266. HinA = wheel_2_res_adj['HoutR'],
  267. FA = air_flow['mixed_2_FaA'],
  268. TinM = mixed_2_TinM,
  269. HinM = mixed_2_HinM,
  270. FM = air_flow['mixed_2_FaM'],
  271. engine = engine
  272. )
  273. # 前转轮湿度修正
  274. wheel_1_res_adj = components['wheel_1'].model(
  275. TinP = Tin_F,
  276. HinP = Hin_F,
  277. FP = air_flow['wheel_1_FaP'],
  278. TinR = wheel_1_TinR,
  279. HinR = mixed_2_res['HoutA'],
  280. FR = air_flow['wheel_1_FaR'],
  281. TinC = Tin_F,
  282. HinC = Hin_F,
  283. FC = air_flow['wheel_1_FaC'],
  284. engine = engine,
  285. param = param['wheel_1']
  286. )
  287. # 前蒸气盘管
  288. steamcoil_1_res = components['steamcoil_1'].model(
  289. TinA = mixed_2_res['ToutA'],
  290. ToutA = wheel_1_TinR,
  291. FA = air_flow['steamcoil_1_Fa'],
  292. param = param['steamcoil_1'],
  293. engine = engine
  294. )
  295. # 后蒸气盘管
  296. steamcoil_2_res = components['steamcoil_2'].model(
  297. TinA = wheel_2_res_adj['ToutC'],
  298. ToutA = wheel_2_TinR,
  299. FA = air_flow['steamcoil_2_Fa'],
  300. param = param['steamcoil_2'],
  301. engine = engine
  302. )
  303. return {
  304. 'coil_2' : coil_2_res,
  305. 'coil_3' : coil_3_res,
  306. 'wheel_1' : wheel_1_res_adj,
  307. 'wheel_2' : wheel_2_res_adj,
  308. 'mixed_1' : mixed_1_res,
  309. 'mixed_2' : mixed_2_res,
  310. 'steamcoil_1': steamcoil_1_res,
  311. 'steamcoil_2': steamcoil_2_res,
  312. 'Fa' : air_flow,
  313. 'summary' : {}
  314. }
  315. class AirFlow:
  316. @classmethod
  317. def model(cls,fan_1_Hz,fan_2_Hz,param):
  318. # 空气的质量流量
  319. F_air_HzP_H = param['F_air']['HzP_H']
  320. F_air_HzP_X = param['F_air']['HzP_X']
  321. F_air_HzP_S = F_air_HzP_H + F_air_HzP_X
  322. F_air_HzR_B = param['F_air']['HzR_B']
  323. F_air_S_base = 1
  324. F_air_X_base = param['F_air']['X_base']
  325. F_air_H_base = param['F_air']['H_base']
  326. F_air_B_base = param['F_air']['B_base']
  327. F_air_val_rw = param['F_air'].get('val_rw',0)
  328. F_air_val_pct = param['F_air'].get('val_pct',0)
  329. F_air_X_base_adj = F_air_X_base + F_air_val_rw
  330. F_air_H_base_adj = F_air_H_base - F_air_val_rw * F_air_val_pct
  331. F_air_B_base_adj = F_air_B_base - F_air_val_rw * (1 - F_air_val_pct)
  332. Fa_S = F_air_S_base + F_air_HzP_S * (fan_1_Hz / 50)
  333. Fa_H = F_air_H_base_adj + F_air_HzP_H * (fan_1_Hz / 50)
  334. Fa_X = F_air_X_base_adj + F_air_HzP_X * (fan_1_Hz / 50)
  335. Fa_B = F_air_B_base_adj + F_air_HzR_B * (fan_2_Hz / 50)
  336. Fa_P = Fa_B + Fa_X + Fa_H - Fa_S
  337. wheel_1_FaP = Fa_S - Fa_H
  338. wheel_1_FaC = Fa_X - wheel_1_FaP
  339. wheel_1_FaR = Fa_P
  340. wheel_2_FaP = Fa_S
  341. wheel_2_FaC = wheel_1_FaC
  342. wheel_2_FaR = wheel_1_FaC
  343. mixed_1_FaM = Fa_H
  344. mixed_1_FaA = wheel_1_FaP
  345. mixed_2_FaM = Fa_B
  346. mixed_2_FaA = wheel_1_FaC
  347. coil_2_FaA = Fa_S
  348. coil_3_FaA = Fa_S
  349. steamcoil_1_Fa = Fa_P
  350. steamcoil_2_Fa = wheel_1_FaC
  351. return {
  352. 'Fa_S':Fa_S,'Fa_H':Fa_H,'Fa_X':Fa_X,'Fa_B':Fa_B,'Fa_P':Fa_P,
  353. 'wheel_1_FaP':wheel_1_FaP,'wheel_1_FaC':wheel_1_FaC,'wheel_1_FaR':wheel_1_FaR,
  354. 'wheel_2_FaP':wheel_2_FaP,'wheel_2_FaC':wheel_2_FaC,'wheel_2_FaR':wheel_2_FaR,
  355. 'mixed_1_FaM':mixed_1_FaM,'mixed_1_FaA':mixed_1_FaA,
  356. 'mixed_2_FaM':mixed_2_FaM,'mixed_2_FaA':mixed_2_FaA,
  357. 'coil_2_FaA':coil_2_FaA,'coil_3_FaA':coil_3_FaA,
  358. 'steamcoil_1_Fa':steamcoil_1_Fa,'steamcoil_2_Fa':steamcoil_2_Fa
  359. }
  360. @classmethod
  361. def prior(cls,rw_FA_val,N) -> dict:
  362. HzP_X = pm.HalfNormal('F_air_HzP_X',sigma=1,initval=1)
  363. HzP_H = pm.HalfNormal('F_air_HzP_H',sigma=1,initval=0.1)
  364. HzR_B = pm.HalfNormal('F_air_HzR_B',sigma=1,initval=0.5)
  365. X_base = pm.TruncatedNormal('F_air_X_base',mu=0.5,sigma=0.2,lower=0,initval=0.5)
  366. H_base = pm.TruncatedNormal('F_air_H_base',mu=0.6,sigma=0.2,lower=0,upper=0.999,initval=0.6)
  367. B_base = pm.TruncatedNormal('F_air_B_base',mu=0.2,sigma=0.1,lower=0,initval=0.1)
  368. if rw_FA_val:
  369. period = 30
  370. n_segments = int(np.ceil(N/period))
  371. remainder = N % period
  372. repeat = [period] * (n_segments - 1) + ([remainder] if remainder != 0 else [])
  373. rw = pm.GaussianRandomWalk(
  374. 'rw',sigma=0.1,init_dist=pm.Normal.dist(mu=0,sigma=0.3),shape=n_segments)
  375. val_rw = pm.Deterministic('F_air_val_rw',pt.repeat(rw,repeat))
  376. val_pct = pm.Beta('F_air_val_pct',alpha=8,beta=1,initval=0.9)
  377. else:
  378. val_rw = 0
  379. val_pct = 0
  380. return {
  381. 'HzP_X':HzP_X,'HzP_H':HzP_H,'HzR_B':HzR_B,
  382. 'X_base':X_base,'H_base':H_base,'B_base':B_base,
  383. 'val_rw':val_rw,'val_pct':val_pct
  384. }