You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.8 KiB
87 lines
3.8 KiB
# -*- coding: utf-8 -*-
|
|
|
|
import pandas as pd
|
|
import numpy as np
|
|
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 CombCal(fileName, minSpeed, maxSpeed, minRate,df):
|
|
if df is None:
|
|
df = pd.read_csv(fileName, encoding='utf-8')
|
|
res = {}
|
|
for k in range(1, len(df)+1):
|
|
for combs in itertools.combinations(range(len(df)), k):
|
|
m_minSpeed, m_maxSpeed = maxSpeed, -1.0 * minSpeed
|
|
m_Flow = df['风量Q(m3/h)'][0:combs[0]].sum() * minRate
|
|
for i in range(combs[0], len(df)):
|
|
if i in combs:
|
|
m_Flow += df['风量Q(m3/h)'][i]
|
|
else:
|
|
m_Flow += df['风量Q(m3/h)'][i] * minRate
|
|
m_Speed = m_Flow / (np.pi * np.power(df['管径D(mm)'][i] / 1000.0, 2.0) / 4.0) / 3600.0
|
|
if m_Speed <= minSpeed or m_Speed >= maxSpeed:
|
|
break
|
|
else:
|
|
m_minSpeed = min(m_minSpeed, m_Speed)
|
|
m_maxSpeed = max(m_maxSpeed, m_Speed)
|
|
if m_Speed > minSpeed and m_Speed < maxSpeed:
|
|
res[str(set(combs))] = {
|
|
"mainFlow": m_Flow,
|
|
"minSpeed": np.round(m_minSpeed, 2),
|
|
"maxSpeed": np.round(m_maxSpeed, 2),
|
|
"setNames": str(list(df['编号'][list(combs)].values))
|
|
}
|
|
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 main():
|
|
CombCal(minSpeed=15, maxSpeed=1000, minRate=0.1)
|
|
|
|
def recommend_v1(design_plan_id,prod_pipe_list,pipe_id,force_update):
|
|
output_dir = './calculated/v1'
|
|
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'],df=df)
|
|
# 复制comb.json文件到./calculated/<parentId>.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/<pipe_id>.json文件
|
|
with open(f'{output_dir}/{pipe_id}.json', 'r', encoding='utf-8') as json_file:
|
|
res = json.load(json_file)
|
|
index_set = {index for index, pipe in enumerate(prod_pipe_list) if pipe['valveOpening'] == 100}
|
|
df = CombSelect(index_set,file_path=f'{output_dir}/{pipe_id}.json')
|
|
# 把df转换为json格式
|
|
df = df.to_dict(orient='records')
|
|
return df
|