DHU_B.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_A import AirFlow_DHU_AB
  16. class DHU_B(BaseDevice):
  17. model_input_data_columns = {}
  18. model_observe_data_columns = {}
  19. def __init__(self):
  20. super().__init__()
  21. self.components = [
  22. WheelS2V3('wheel_1'),
  23. WheelS3('wheel_2'),
  24. CoolingCoil2('coil_2'),
  25. CoolingCoil2('coil_3'),
  26. SteamCoil('steamcoil_1'),
  27. SteamCoil('steamcoil_2'),
  28. Mixed('mixed_1'),
  29. Mixed('mixed_2'),
  30. ]
  31. self.components = {comp.name:comp for comp in self.components}
  32. def fit(
  33. self,
  34. input_data : pd.DataFrame,
  35. observed_data: pd.DataFrame,
  36. exist_Fa_H : bool,
  37. exist_Fa_B : bool,
  38. rw_FA_val : bool = False,
  39. plot_TVP : bool = True
  40. ):
  41. with pm.Model() as self.MODEL_PYMC:
  42. param_prior = {name:comp.prior() for name,comp in self.components.items()}
  43. param_prior['F_air'] = AirFlow_DHU_AB.prior(
  44. rw_FA_val = rw_FA_val,
  45. N = len(input_data),
  46. exist_Fa_H = exist_Fa_H,
  47. exist_Fa_B = exist_Fa_B
  48. )
  49. res = self.model(
  50. **{k:input_data.loc[:,v].values for k,v in self.model_input_data_columns.items()},
  51. engine = 'pymc',
  52. components = self.components,
  53. param = param_prior
  54. )
  55. for std_name,name in self.model_observe_data_columns.items():
  56. if name not in observed_data.columns:
  57. continue
  58. observed_data = observed_data.rename(columns={name:std_name})
  59. observe('mixed_1_ToutA',res['mixed_1']['ToutA'],observed=observed_data)
  60. observe('mixed_1_DoutA',res['mixed_1']['DoutA'],observed=observed_data)
  61. observe('coil_2_ToutA',res['coil_2']['ToutA'],observed=observed_data)
  62. observe('coil_2_DoutA',res['coil_2']['DoutA'],observed=observed_data)
  63. observe('wheel_2_ToutP',res['wheel_2']['ToutP'],observed=observed_data)
  64. observe('wheel_2_DoutP',res['wheel_2']['DoutP'],observed=observed_data)
  65. observe('wheel_2_ToutC',res['wheel_2']['ToutC'],observed=observed_data)
  66. observe('wheel_2_ToutR',res['wheel_2']['ToutR'],observed=observed_data)
  67. observe('wheel_1_ToutR',res['wheel_1']['ToutR'],observed=observed_data,sigma=5)
  68. record('steamcoil_1_Fs',res['steamcoil_1']['Fs'])
  69. record('steamcoil_2_Fs',res['steamcoil_2']['Fs'])
  70. record('mixed_2_ToutA',res['mixed_2']['ToutA'])
  71. record('wheel_1_FaP',res['wheel_1']['FP'])
  72. record('wheel_1_FaR',res['wheel_1']['FR'])
  73. record('wheel_1_FaC',res['wheel_1']['FC'])
  74. record('mixed_1_FaA',res['mixed_1']['FA'])
  75. record('mixed_1_FaM',res['mixed_1']['FM'])
  76. record('F_air_S',res['Fa']['Fa_S'])
  77. record('F_air_H',res['Fa']['Fa_H'])
  78. record('F_air_X',res['Fa']['Fa_X'])
  79. self.param_posterior = pm.find_MAP(maxeval=50000,include_transformed=False)
  80. self.record_model(
  81. model_name = 'ATD',
  82. model = reorder_posterior(param_prior,self.param_posterior),
  83. train_data = {'x':np.array([1])},
  84. train_metric = {'R2':1,'MAE':1,'MAPE':1}
  85. )
  86. self.TVP_data,self.TVP_metric = get_fitted_result(self.param_posterior,observed_data,plot_TVP)
  87. return self
  88. def optimize(
  89. self,
  90. cur_input_data : pd.DataFrame,
  91. wheel_1_TinR_ub: float = 120,
  92. wheel_1_TinR_lb: float = 70,
  93. wheel_2_TinR_ub: float = 120,
  94. wheel_2_TinR_lb: float = 70,
  95. constrains : list = None,
  96. logging : bool = True
  97. ) -> list:
  98. constrains = [] if constrains is None else constrains
  99. cur_input_data = cur_input_data.iloc[[0],:]
  100. opt_var_boundary = {
  101. 'wheel_1_TinR':{'lb':wheel_1_TinR_lb,'ub':wheel_1_TinR_ub},
  102. 'wheel_2_TinR':{'lb':wheel_2_TinR_lb,'ub':wheel_2_TinR_ub},
  103. }
  104. opt_var_value = cur_input_data.loc[:,list(opt_var_boundary.keys())]
  105. oth_var_value = (
  106. cur_input_data
  107. .loc[:,list(self.model_input_data_columns.values())]
  108. .drop(opt_var_value.columns,axis=1)
  109. )
  110. opt_res = optimizer(
  111. model = self,
  112. opt_var_boundary = opt_var_boundary,
  113. opt_var_value = opt_var_value,
  114. oth_var_value = oth_var_value,
  115. target = 'Fs',
  116. target_min = True,
  117. constrains = constrains,
  118. logging = logging,
  119. other_kwargs = {'NIND':2000,'MAXGEN':50}
  120. )
  121. return opt_res
  122. @classmethod
  123. def model(
  124. cls,
  125. Tin_F, # 前表冷后温度
  126. Hin_F, # 前表冷后湿度
  127. fan_1_Hz, # 处理侧风机频率
  128. fan_2_Hz, # 再生侧风机频率
  129. coil_1_TinW, # 前表冷进水温度
  130. coil_2_TinW, # 中表冷进水温度
  131. coil_3_TinW, # 后表冷进水温度
  132. coil_1_Val, # 前表冷阀门开度
  133. coil_2_Val, # 中表冷阀门开度
  134. coil_3_Val, # 后表冷阀门开度
  135. wheel_1_TinR, # 前转轮再生侧温度
  136. wheel_2_TinR, # 后转轮再生侧温度
  137. mixed_1_TinM, # 回风温度(处理侧)
  138. mixed_1_HinM, # 回风湿度(处理侧)
  139. mixed_2_TinM, # 补风温度(再生侧)
  140. mixed_2_HinM, # 补风湿度(再生侧)
  141. engine : str,
  142. components: dict,
  143. param : dict,
  144. ) -> dict:
  145. # 水的质量流量
  146. coil_2_FW = coil_2_Val / 100
  147. coil_3_FW = coil_3_Val / 100
  148. # 空气的质量流量
  149. air_flow = AirFlow_DHU_AB.model(fan_1_Hz=fan_1_Hz,fan_2_Hz=fan_2_Hz,param=param,type='DHU_B')
  150. # 前转轮
  151. wheel_1_res = components['wheel_1'].model(
  152. TinP = Tin_F,
  153. HinP = Hin_F,
  154. FP = air_flow['wheel_1_FaP'],
  155. TinR = wheel_1_TinR,
  156. HinR = 0,
  157. FR = air_flow['wheel_1_FaR'],
  158. engine = engine,
  159. param = param,
  160. )
  161. # 处理侧混风(回风)
  162. mixed_1_res = components['mixed_1'].model(
  163. TinA = wheel_1_res['ToutP'],
  164. HinA = wheel_1_res['HoutP'],
  165. FA = air_flow['mixed_1_FaA'],
  166. TinM = mixed_1_TinM,
  167. HinM = mixed_1_HinM,
  168. FM = air_flow['mixed_1_FaM'],
  169. engine = engine
  170. )
  171. # 中表冷
  172. coil_2_res = components['coil_2'].model(
  173. TinA = mixed_1_res['ToutA'],
  174. HinA = mixed_1_res['HoutA'],
  175. FA = air_flow['coil_2_FaA'],
  176. TinW = coil_2_TinW,
  177. FW = coil_2_FW,
  178. engine = engine,
  179. param = param['coil_2']
  180. )
  181. # 后转轮
  182. wheel_2_res = components['wheel_2'].model(
  183. TinP = coil_2_res['ToutA'],
  184. HinP = coil_2_res['HoutA'],
  185. FP = air_flow['wheel_2_FaP'],
  186. TinC = mixed_1_res['ToutA'],
  187. HinC = mixed_1_res['HoutA'],
  188. FC = air_flow['wheel_2_FaC'],
  189. TinR = wheel_2_TinR,
  190. HinR = 0,
  191. FR = air_flow['wheel_2_FaR'],
  192. engine = engine,
  193. param = param['wheel_2'],
  194. )
  195. # 后表冷
  196. coil_3_res = components['coil_3'].model(
  197. TinA = wheel_2_res['ToutP'],
  198. HinA = wheel_2_res['HoutP'],
  199. FA = air_flow['coil_3_FaA'],
  200. TinW = coil_3_TinW,
  201. FW = coil_3_FW,
  202. engine = engine,
  203. param = param['coil_3']
  204. )
  205. # 后转轮湿度修正
  206. wheel_2_res_adj = components['wheel_2'].model(
  207. TinP = coil_2_res['ToutA'],
  208. HinP = coil_2_res['HoutA'],
  209. FP = air_flow['wheel_2_FaP'],
  210. TinC = mixed_1_res['ToutA'],
  211. HinC = mixed_1_res['HoutA'],
  212. FC = air_flow['wheel_2_FaC'],
  213. TinR = wheel_2_TinR,
  214. HinR = wheel_2_res['HoutC'],
  215. FR = air_flow['wheel_2_FaR'],
  216. engine = engine,
  217. param = param['wheel_2'],
  218. )
  219. # 再生侧混风(排风)
  220. mixed_2_res = components['mixed_2'].model(
  221. TinA = wheel_2_res_adj['ToutR'],
  222. HinA = wheel_2_res_adj['HoutR'],
  223. FA = air_flow['mixed_2_FaA'],
  224. TinM = mixed_2_TinM,
  225. HinM = mixed_2_HinM,
  226. FM = air_flow['mixed_2_FaM'],
  227. engine = engine
  228. )
  229. # 前转轮湿度修正
  230. wheel_1_res_adj = components['wheel_1'].model(
  231. TinP = Tin_F,
  232. HinP = Hin_F,
  233. FP = air_flow['wheel_1_FaP'],
  234. TinR = wheel_1_TinR,
  235. HinR = mixed_2_res['HoutA'],
  236. FR = air_flow['wheel_1_FaR'],
  237. engine = engine,
  238. param = param,
  239. )
  240. # 前蒸气盘管
  241. steamcoil_1_res = components['steamcoil_1'].model(
  242. TinA = mixed_2_res['ToutA'],
  243. ToutA = wheel_1_TinR,
  244. FA = air_flow['steamcoil_1_Fa'],
  245. param = param['steamcoil_1'],
  246. engine = engine
  247. )
  248. # 后蒸气盘管
  249. steamcoil_2_res = components['steamcoil_2'].model(
  250. TinA = wheel_2_res_adj['ToutC'],
  251. ToutA = wheel_2_TinR,
  252. FA = air_flow['steamcoil_2_Fa'],
  253. param = param['steamcoil_2'],
  254. engine = engine
  255. )
  256. return {
  257. 'coil_2' : coil_2_res,
  258. 'coil_3' : coil_3_res,
  259. 'wheel_1' : wheel_1_res_adj,
  260. 'wheel_2' : wheel_2_res_adj,
  261. 'mixed_1' : mixed_1_res,
  262. 'mixed_2' : mixed_2_res,
  263. 'steamcoil_1': steamcoil_1_res,
  264. 'steamcoil_2': steamcoil_2_res,
  265. 'Fa' : air_flow,
  266. 'summary' : {
  267. 'Fs':steamcoil_1_res['Fs'] + steamcoil_2_res['Fs'],
  268. }
  269. }