from flask import Flask, request, jsonify import math import numpy as np app = Flask(__name__) # 定义计算解冻时间的函数 def calculate_thaw_time(mass, specific_heat, thermal_conductivity, length, width, height, T_thaw, T_env, T_initial, alpha=1.2): """ mass: 煤矿质量 (kg) specific_heat: 比热容 (kJ/(kg·°C)) thermal_conductivity: 热传导系数 (W/m·°C) length: 煤矿堆积的长度 (m) width: 煤矿堆积的宽度 (m) height: 煤矿堆积的高度 (m) T_thaw: 需要解冻的温度 (°C) T_env: 解冻库库温 (°C) T_initial: 煤矿初始温度 (°C) alpha: 修正系数 (默认 1.2) """ # 1. 计算煤矿堆的外部表面积 (A) external_area = 2 * (length * width + length * height + width * height) # 2. 修正表面积 (A_effective) effective_area = alpha * external_area # 3. 避免计算中 log 的负值和分母为零的情况 print(f"T_initial: {T_initial}") if T_initial == T_env or (T_thaw - T_env) / (T_initial - T_env) <= 0: return None # 4. 计算解冻时间 thaw_time = - (mass * specific_heat) / (thermal_conductivity * effective_area) * \ math.log((T_thaw - T_env) / (T_initial - T_env)) return thaw_time # 定义 REST API 路由 @app.route('/calculate-thaw-time', methods=['POST']) def calculate(): try: # 获取 JSON 参数 data = request.json # 打印接收到的 JSON 数据 print("Received data:", data) print("Request Headers:", request.headers) # 必填字段校验 required_fields = ['mass', 'specific_heat', 'thermal_conductivity', 'length', 'width', 'height', 'T_thaw', 'T_env', 'T_initial'] for field in required_fields: if field not in data: return jsonify({"error": f"Missing field: {field}"}), 400 # 提取参数 mass = data['mass'] specific_heat = data['specific_heat'] thermal_conductivity = data['thermal_conductivity'] length = data['length'] width = data['width'] height = data['height'] T_thaw = data['T_thaw'] T_env = data['T_env'] T_initial = data['T_initial'] alpha = data.get('alpha', 1.2) # 默认修正系数 # 计算解冻时间 thaw_time = calculate_thaw_time(mass, specific_heat, thermal_conductivity, length, width, height, T_thaw, T_env, T_initial, alpha) # 返回结果 if thaw_time is None: return jsonify({"error": "Invalid input values for calculation"}), 400 # 返回的解冻时间是小时 return jsonify({"thaw_time": thaw_time}) except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(port=9996, debug=True)