DHU_B.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 SteamCoil
  8. from ...components.wheel2 import WheelS2V3
  9. from ...components.wheel3 import WheelS3
  10. from ...components.mixed import Mixed
  11. from ..utils.fit_utils import (
  12. observe,record,reorder_posterior
  13. )
  14. from ...tools.optimizer import optimizer
  15. from .DHU_AB import AirFlow_DHU_AB
  16. class DHU_B(BaseDevice):
  17. model_input_data_columns = {
  18. 'Tin_F' : 'coil_1_ToutA',
  19. 'Hin_F' : 'coil_1_HoutA',
  20. 'fan_1_Hz' : 'fan_1_Hz',
  21. 'fan_2_Hz' : 'fan_2_Hz',
  22. 'coil_1_TinW' : 'coil_1_TinW',
  23. 'coil_2_TinW' : 'coil_2_TinW',
  24. 'coil_3_TinW' : 'coil_3_TinW',
  25. 'coil_1_Val' : 'coil_1_Val',
  26. 'coil_2_Val' : 'coil_2_Val',
  27. 'coil_3_Val' : 'coil_3_Val',
  28. 'wheel_1_TinR': 'wheel_1_TinR',
  29. 'wheel_2_TinR': 'wheel_2_TinR',
  30. 'mixed_1_TinM': 'mixed_1_TinM',
  31. 'mixed_2_TinM': 'mixed_2_TinM',
  32. 'mixed_1_HinM': 'mixed_1_HinM',
  33. 'mixed_2_HinM': 'mixed_2_HinM',
  34. }
  35. model_observe_data_columns = {
  36. 'mixed_1_ToutA' : 'mixed_1_ToutA',
  37. 'mixed_1_DoutA' : 'mixed_1_DoutA',
  38. 'coil_2_ToutA' : 'coil_2_ToutA',
  39. 'coil_2_DoutA' : 'coil_2_DoutA',
  40. 'wheel_2_ToutP' : 'wheel_2_ToutP',
  41. 'wheel_2_DoutP' : 'wheel_2_DoutP',
  42. 'wheel_2_ToutR' : 'wheel_2_ToutR',
  43. 'wheel_2_ToutC' : 'wheel_2_ToutC',
  44. }
  45. val_rw_adj_target = ('coil_2_DoutA','coil_3_DoutA')
  46. def __init__(
  47. self,
  48. wheel_1 = 'WheelS3',
  49. wheel_2 = 'WheelS3',
  50. coolingcoil_2 = 'CoolingCoil2',
  51. coolingcoil_3 = 'CoolingCoil2',
  52. heatingcoil_1 = 'SteamCoil',
  53. heatingcoil_2 = 'SteamCoil',
  54. mixed_1 = 'Mixed',
  55. mixed_2 = 'Mixed',
  56. **kwargs
  57. ) -> None:
  58. super().__init__()
  59. if 'components' in kwargs:
  60. self.components = kwargs['components']
  61. else:
  62. self.components = {
  63. 'wheel_1' : wheel_1,
  64. 'wheel_2' : wheel_2,
  65. 'coil_2' : coolingcoil_2,
  66. 'coil_3' : coolingcoil_3,
  67. 'heatingcoil_1': heatingcoil_1,
  68. 'heatingcoil_2': heatingcoil_2,
  69. 'mixed_1' : mixed_1,
  70. 'mixed_2' : mixed_2
  71. }
  72. self.record_load_info(components=self.components)
  73. self.components = {k:eval(v)(k) for k,v in self.components.items()}
  74. def fit(
  75. self,
  76. input_data : pd.DataFrame,
  77. observed_data: pd.DataFrame,
  78. exist_Fa_H : bool,
  79. exist_Fa_B : bool,
  80. rw_FA_val : bool = False,
  81. plot_TVP : bool = True
  82. ):
  83. with pm.Model() as self.MODEL_PYMC:
  84. param_prior = {name:comp.prior() for name,comp in self.components.items()}
  85. param_prior['F_air'] = AirFlow_DHU_AB.prior(
  86. rw_FA_val = rw_FA_val,
  87. N = len(input_data),
  88. exist_Fa_H = exist_Fa_H,
  89. exist_Fa_B = exist_Fa_B
  90. )
  91. res = self.model(
  92. **{k:input_data.loc[:,v].values for k,v in self.model_input_data_columns.items()},
  93. engine = 'pymc',
  94. components = self.components,
  95. param = param_prior
  96. )
  97. for std_name,name in self.model_observe_data_columns.items():
  98. if name not in observed_data.columns:
  99. continue
  100. observed_data = observed_data.rename(columns={name:std_name})
  101. observe('mixed_1_ToutA',res['mixed_1']['ToutA'],observed=observed_data)
  102. observe('mixed_1_DoutA',res['mixed_1']['DoutA'],observed=observed_data)
  103. observe('coil_2_ToutA',res['coil_2']['ToutA'],observed=observed_data)
  104. observe('coil_2_DoutA',res['coil_2']['DoutA'],observed=observed_data)
  105. observe('wheel_2_ToutP',res['wheel_2']['ToutP'],observed=observed_data)
  106. observe('wheel_2_DoutP',res['wheel_2']['DoutP'],observed=observed_data)
  107. observe('wheel_2_ToutC',res['wheel_2']['ToutC'],observed=observed_data)
  108. observe('wheel_2_ToutR',res['wheel_2']['ToutR'],observed=observed_data)
  109. observe('wheel_1_ToutR',res['wheel_1']['ToutR'],observed=observed_data,sigma=5)
  110. record('steamcoil_1_Fs',res['steamcoil_1']['Fs'])
  111. record('steamcoil_2_Fs',res['steamcoil_2']['Fs'])
  112. record('mixed_2_ToutA',res['mixed_2']['ToutA'])
  113. record('wheel_1_FaP',res['wheel_1']['FP'])
  114. record('wheel_1_FaR',res['wheel_1']['FR'])
  115. record('wheel_1_FaC',res['wheel_1']['FC'])
  116. record('mixed_1_FaA',res['mixed_1']['FA'])
  117. record('mixed_1_FaM',res['mixed_1']['FM'])
  118. record('F_air_S',res['Fa']['Fa_S'])
  119. record('F_air_H',res['Fa']['Fa_H'])
  120. record('F_air_X',res['Fa']['Fa_X'])
  121. self.param_posterior = pm.find_MAP(maxeval=50000,include_transformed=False)
  122. self.record_model(
  123. model_name = 'ATD',
  124. model = reorder_posterior(param_prior,self.param_posterior),
  125. train_data = {'x':np.array([1])},
  126. train_metric = {'R2':1,'MAE':1,'MAPE':1}
  127. )
  128. self.TVP_data,self.TVP_metric = get_fitted_result(self.param_posterior,observed_data,plot_TVP)
  129. return self
  130. def optimize(
  131. self,
  132. cur_input_data : pd.DataFrame,
  133. wheel_1_TinR_ub: float = 120,
  134. wheel_1_TinR_lb: float = 70,
  135. wheel_2_TinR_ub: float = 120,
  136. wheel_2_TinR_lb: float = 70,
  137. constrains : list = None,
  138. logging : bool = True
  139. ) -> list:
  140. constrains = [] if constrains is None else constrains
  141. cur_input_data = cur_input_data.iloc[[0],:]
  142. opt_var_boundary = {
  143. 'wheel_1_TinR':{'lb':wheel_1_TinR_lb,'ub':wheel_1_TinR_ub},
  144. 'wheel_2_TinR':{'lb':wheel_2_TinR_lb,'ub':wheel_2_TinR_ub},
  145. }
  146. opt_var_value = cur_input_data.loc[:,list(opt_var_boundary.keys())]
  147. oth_var_value = (
  148. cur_input_data
  149. .loc[:,list(self.model_input_data_columns.values())]
  150. .drop(opt_var_value.columns,axis=1)
  151. )
  152. opt_res = optimizer(
  153. model = self,
  154. opt_var_boundary = opt_var_boundary,
  155. opt_var_value = opt_var_value,
  156. oth_var_value = oth_var_value,
  157. target = 'Fs',
  158. target_min = True,
  159. constrains = constrains,
  160. logging = logging,
  161. other_kwargs = {'NIND':2000,'MAXGEN':50}
  162. )
  163. return opt_res
  164. @classmethod
  165. def model(
  166. cls,
  167. Tin_F, # 前表冷后温度
  168. Hin_F, # 前表冷后湿度
  169. fan_1_Hz, # 处理侧风机频率
  170. fan_2_Hz, # 再生侧风机频率
  171. coil_1_TinW, # 前表冷进水温度
  172. coil_2_TinW, # 中表冷进水温度
  173. coil_3_TinW, # 后表冷进水温度
  174. coil_1_Val, # 前表冷阀门开度
  175. coil_2_Val, # 中表冷阀门开度
  176. coil_3_Val, # 后表冷阀门开度
  177. wheel_1_TinR, # 前转轮再生侧温度
  178. wheel_2_TinR, # 后转轮再生侧温度
  179. mixed_1_TinM, # 回风温度(处理侧)
  180. mixed_1_HinM, # 回风湿度(处理侧)
  181. mixed_2_TinM, # 补风温度(再生侧)
  182. mixed_2_HinM, # 补风湿度(再生侧)
  183. engine : str,
  184. components: dict,
  185. param : dict,
  186. ) -> dict:
  187. # 水的质量流量
  188. coil_2_FW = coil_2_Val / 100
  189. coil_3_FW = coil_3_Val / 100
  190. # 空气的质量流量
  191. air_flow = AirFlow_DHU_AB.model(fan_1_Hz=fan_1_Hz,fan_2_Hz=fan_2_Hz,param=param,type='DHU_B')
  192. # 前转轮
  193. wheel_1_res = components['wheel_1'].model(
  194. TinP = Tin_F,
  195. HinP = Hin_F,
  196. FP = air_flow['wheel_1_FaP'],
  197. TinR = wheel_1_TinR,
  198. HinR = 0,
  199. FR = air_flow['wheel_1_FaR'],
  200. engine = engine,
  201. param = param,
  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 = mixed_1_res['ToutA'],
  229. HinC = mixed_1_res['HoutA'],
  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 = mixed_1_res['ToutA'],
  253. HinC = mixed_1_res['HoutA'],
  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. engine = engine,
  280. param = param,
  281. )
  282. # 前蒸气盘管
  283. steamcoil_1_res = components['steamcoil_1'].model(
  284. TinA = mixed_2_res['ToutA'],
  285. ToutA = wheel_1_TinR,
  286. FA = air_flow['steamcoil_1_Fa'],
  287. param = param['steamcoil_1'],
  288. engine = engine
  289. )
  290. # 后蒸气盘管
  291. steamcoil_2_res = components['steamcoil_2'].model(
  292. TinA = wheel_2_res_adj['ToutC'],
  293. ToutA = wheel_2_TinR,
  294. FA = air_flow['steamcoil_2_Fa'],
  295. param = param['steamcoil_2'],
  296. engine = engine
  297. )
  298. return {
  299. 'coil_2' : coil_2_res,
  300. 'coil_3' : coil_3_res,
  301. 'wheel_1' : wheel_1_res_adj,
  302. 'wheel_2' : wheel_2_res_adj,
  303. 'mixed_1' : mixed_1_res,
  304. 'mixed_2' : mixed_2_res,
  305. 'steamcoil_1': steamcoil_1_res,
  306. 'steamcoil_2': steamcoil_2_res,
  307. 'Fa' : air_flow,
  308. 'summary' : {
  309. 'Fs':steamcoil_1_res['Fs'] + steamcoil_2_res['Fs'],
  310. }
  311. }