# -*- coding: utf-8 -*- import pandas as pd import numpy as np import itertools, json, warnings, time warnings.filterwarnings('ignore', category=Warning) import itertools, json from config import load_common_config from utils.mysql import get_all_pipe_by_design_plan_id from utils.dust import group_pipes ,get_pipes_diameter import os def minKCal(flows, maxPipe, minSpeed, maxSpeed, minRate): flows.sort() minFlow = (np.pi * np.power(maxPipe / 1000.0, 2.0) / 4.0) * 3600.0 * minSpeed - np.sum(flows) * minRate maxFlow = (np.pi * np.power(maxPipe / 1000.0, 2.0) / 4.0) * 3600.0 * maxSpeed - np.sum(flows) * minRate flows = flows * (1 - minRate) minK, maxK = 0, 0 while minFlow > 0.0 and minK < len(flows): minFlow -= flows[len(flows) - 1 - minK] if minFlow > 0.0: minK += 1 while maxFlow > 0.0 and maxK < len(flows): maxFlow -= flows[maxK] if maxFlow > 0.0: maxK += 1 return min(minK + 1, len(flows)), min(maxK + 1, len(flows)) def maxKCal(flows, maxPipe, avgSpeed, minRate): flows.sort() mFlow = (np.pi * np.power(maxPipe / 1000.0, 2.0) / 4.0) * 3600.0 * avgSpeed - np.sum(flows) * minRate mflows = flows * (1 - minRate) maxK = 0 while mFlow > 0.0 and maxK < len(mflows): mFlow -= mflows[maxK] maxK += 1 return min(maxK, len(flows)) def CombCal(fileName, minSpeed, maxSpeed, avgSpeed, minRate,df=None): if df is None: df = pd.read_csv(fileName, encoding='utf-8') df['面积m2'] = (np.pi * np.power(df['管径D(mm)'] / 1000.0, 2.0) / 4.0) * 3600.0 valves = [minRate, 1.0] maxK = maxKCal(df['风量Q(m3/h)'].values.copy(), df['管径D(mm)'].values[-1], avgSpeed, minRate) mm_arr, qq_arr = [[minRate], [1.0]], [] m_arr, q_arr = [], [] for ar in mm_arr: if np.count_nonzero(np.array(ar) == 1.0) == 0: m_arr.append(ar) m_flow = np.dot(ar, df['风量Q(m3/h)'][:len(ar)].values) q_arr.append(m_flow) else: m_flow = np.dot(ar, df['风量Q(m3/h)'][:len(ar)].values) m_Speed = m_flow / df['面积m2'][len(ar)-1] if m_Speed > minSpeed and m_Speed < maxSpeed: m_arr.append(ar) q_arr.append(m_flow) for k in range(1, len(df)): mm_arr = m_arr qq_arr = q_arr m_arr = [] q_arr = [] m_count = 0 for valve in valves: for i in range(len(mm_arr)): ar = mm_arr[i].copy() ar.append(valve) m_nonzero = np.count_nonzero(np.array(ar) == 1.0) if m_nonzero == 0 and len(ar) < maxK: m_arr.append(ar) q_arr.append(qq_arr[i] + minRate * df['风量Q(m3/h)'][len(ar)-1]) else: if m_nonzero <= maxK: m_flow = qq_arr[i] + valve * df['风量Q(m3/h)'][len(ar)-1] m_Speed = m_flow / df['面积m2'][len(ar) - 1] if m_Speed > minSpeed and m_Speed < maxSpeed: m_arr.append(ar) q_arr.append(m_flow) if m_nonzero == maxK: m_count += 1 # print(f'{k}:{len(m_arr)}:{m_Speed}:{m_count}') res = {} for ar in m_arr: if np.count_nonzero(np.array(ar) == 1.0) > 0: df['开度'] = ar df['支管风量'] = df['开度'] * df['风量Q(m3/h)'] df['总管风量'] = df['支管风量'].cumsum() df['总管风速'] = df['总管风量'] / df['面积m2'] m_Flow = df['总管风量'][len(df) - 1] m_minSpeed = df['总管风速'][(df['开度'] == 1.0).idxmax():].min() m_maxSpeed = df['总管风速'][(df['开度'] == 1.0).idxmax():].max() m_name = df['编号'][df['开度'] == 1.0].values res[str(set(m_name))] = { "mainFlow": m_Flow, "minSpeed": np.round(m_minSpeed, 2), "maxSpeed": np.round(m_maxSpeed, 2), "setNames": str(list(m_name)) } with open('comb.json', 'w', encoding='utf-8') as json_file: json.dump(res, json_file, indent=2) def CombSelect(S_Comb,file_path='comb.json'): _Res = pd.read_json(file_path, encoding='utf-8') S_Col = [] S_Count = 0 for col in _Res.columns: if eval(col) & S_Comb == S_Comb: if S_Count == 0: S_Count = len(eval(col)) if S_Count + 1 < len(eval(col)): break S_Col.append(col) return _Res[S_Col].T.sort_values(by=['mainFlow', 'minSpeed', 'maxSpeed'], ascending=[True, True, True]) def recommend_v2(design_plan_id,prod_pipe_list,pipe_id,force_update): output_dir = './calculated/v2' if not os.path.exists(os.path.join(output_dir, f'{pipe_id}.json')) or force_update: common_config = load_common_config() all_pipes = get_all_pipe_by_design_plan_id(design_plan_id) groups = group_pipes(all_pipes) for parentId, pipes in groups.items(): pipes = get_pipes_diameter(pipes,design_plan_id) data = [] for pipe in pipes: data.append([float(pipe['flow']),float(pipe['diameter']), pipe['id']]) # flow(风量Q(m3/h)) diameter(管径D(mm)) id(编号) df = pd.DataFrame(data, columns=['风量Q(m3/h)', '管径D(mm)', '编号']) CombCal(fileName='',minSpeed=common_config['minSpeed'], maxSpeed=common_config['maxSpeed'], minRate=common_config['minRate'],avgSpeed=common_config['avgSpeed'],df=df) # 复制comb.json文件到./calculated/.json with open('comb.json', 'r', encoding='utf-8') as json_file: res = json.load(json_file) os.makedirs(output_dir, exist_ok=True) # 如果目录不存在则创建 with open(f'{output_dir}/{parentId}.json', 'w', encoding='utf-8') as json_file: json.dump(res, json_file, indent=2) # 读取./calculated/.json文件 with open(f'./{output_dir}/{pipe_id}.json', 'r', encoding='utf-8') as json_file: res = json.load(json_file) ids_set = {pipe['id'] for index, pipe in enumerate(prod_pipe_list) if pipe['valveOpening'] == 100} df = CombSelect(ids_set,file_path=f'./{output_dir}/{pipe_id}.json') # 把df转换为json格式 df = df.to_dict(orient='records') return df