解冻库python代码接收java算法数据.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from flask import Flask, request, jsonify
  2. import math
  3. import numpy as np
  4. app = Flask(__name__)
  5. # 定义计算解冻时间的函数
  6. def calculate_thaw_time(mass, specific_heat, thermal_conductivity, length, width, height, T_thaw, T_env, T_initial, alpha=1.2):
  7. """
  8. mass: 煤矿质量 (kg)
  9. specific_heat: 比热容 (kJ/(kg·°C))
  10. thermal_conductivity: 热传导系数 (W/m·°C)
  11. length: 煤矿堆积的长度 (m)
  12. width: 煤矿堆积的宽度 (m)
  13. height: 煤矿堆积的高度 (m)
  14. T_thaw: 需要解冻的温度 (°C)
  15. T_env: 解冻库库温 (°C)
  16. T_initial: 煤矿初始温度 (°C)
  17. alpha: 修正系数 (默认 1.2)
  18. """
  19. # 1. 计算煤矿堆的外部表面积 (A)
  20. external_area = 2 * (length * width + length * height + width * height)
  21. # 2. 修正表面积 (A_effective)
  22. effective_area = alpha * external_area
  23. # 3. 避免计算中 log 的负值和分母为零的情况
  24. print(f"T_initial: {T_initial}")
  25. if T_initial == T_env or (T_thaw - T_env) / (T_initial - T_env) <= 0:
  26. return None
  27. # 4. 计算解冻时间
  28. thaw_time = - (mass * specific_heat) / (thermal_conductivity * effective_area) * \
  29. math.log((T_thaw - T_env) / (T_initial - T_env))
  30. return thaw_time
  31. # 定义 REST API 路由
  32. @app.route('/calculate-thaw-time', methods=['POST'])
  33. def calculate():
  34. try:
  35. # 获取 JSON 参数
  36. data = request.json
  37. # 打印接收到的 JSON 数据
  38. print("Received data:", data)
  39. print("Request Headers:", request.headers)
  40. # 必填字段校验
  41. required_fields = ['mass', 'specific_heat', 'thermal_conductivity', 'length', 'width', 'height', 'T_thaw', 'T_env', 'T_initial']
  42. for field in required_fields:
  43. if field not in data:
  44. return jsonify({"error": f"Missing field: {field}"}), 400
  45. # 提取参数
  46. mass = data['mass']
  47. specific_heat = data['specific_heat']
  48. thermal_conductivity = data['thermal_conductivity']
  49. length = data['length']
  50. width = data['width']
  51. height = data['height']
  52. T_thaw = data['T_thaw']
  53. T_env = data['T_env']
  54. T_initial = data['T_initial']
  55. alpha = data.get('alpha', 1.2) # 默认修正系数
  56. # 计算解冻时间
  57. thaw_time = calculate_thaw_time(mass, specific_heat, thermal_conductivity, length, width, height, T_thaw, T_env, T_initial, alpha)
  58. # 返回结果
  59. if thaw_time is None:
  60. return jsonify({"error": "Invalid input values for calculation"}), 400
  61. # 返回的解冻时间是小时
  62. return jsonify({"thaw_time": thaw_time})
  63. except Exception as e:
  64. return jsonify({"error": str(e)}), 500
  65. if __name__ == '__main__':
  66. app.run(port=9996, debug=True)