// MoonSteel - 钢结构设计库
// 主模块:包含核心计算功能

// ==================== 轴心受力构件 ====================

///|
/// 轴心受拉构件强度验算
/// σ = N / A ≤ f
/// 参数: n - 轴力 (N), a - 净截面积 (m²), f - 强度设计值 (Pa)
pub fn axial_tension_strength(n : Double, a : Double, f : Double) -> Double {
  if a <= 0.0 {
    return 0.0
  }
  n / a
}

///|
/// 轴心受拉构件强度验算 (返回是否满足)
pub fn axial_tension_check(n : Double, a : Double, f : Double) -> Bool {
  let sigma = axial_tension_strength(n, a, f)
  sigma <= f
}

///|
/// 轴心受压构件强度验算
/// σ = N / A ≤ f
pub fn axial_compression_strength(n : Double, a : Double, f : Double) -> Double {
  if a <= 0.0 {
    return 0.0
  }
  n / a
}

///|
/// 轴心受压构件稳定验算
/// N / (φ * A) ≤ f
/// 参数: n - 轴力 (N), phi - 稳定系数, a - 毛截面积 (m²), f - 强度设计值 (Pa)
pub fn axial_compression_stability(
  n : Double,
  phi : Double,
  a : Double,
  f : Double,
) -> Double {
  if phi <= 0.0 || a <= 0.0 {
    return 0.0
  }
  n / (phi * a)
}

///|
/// 轴心受压构件稳定验算 (返回是否满足)
pub fn axial_compression_stability_check(
  n : Double,
  phi : Double,
  a : Double,
  f : Double,
) -> Bool {
  let sigma = axial_compression_stability(n, phi, a, f)
  sigma <= f
}

///|
/// 轴心受压构件承载力
/// N_u = φ * A * f
pub fn axial_compression_capacity(
  phi : Double,
  a : Double,
  f : Double,
) -> Double {
  phi * a * f
}

// ==================== 受弯构件 (梁) ====================

///|
/// 受弯构件正应力强度验算
/// σ = M / W ≤ f
/// 参数: m - 弯矩 (N·m), w - 截面模量 (m³), f - 强度设计值 (Pa)
pub fn bending_stress(m : Double, w : Double, f : Double) -> Double {
  if w <= 0.0 {
    return 0.0
  }
  m / w
}

///|
/// 受弯构件正应力强度验算 (返回是否满足)
pub fn bending_stress_check(m : Double, w : Double, f : Double) -> Bool {
  let sigma = bending_stress(m, w, f)
  sigma <= f
}

///|
/// 受弯构件剪应力强度验算
/// τ = V * S / (I * t_w) ≤ f_v
/// 参数: v - 剪力 (N), s - 面积矩 (m³), i - 惯性矩 (m⁴), tw - 腹板厚度 (m), fv - 抗剪强度设计值 (Pa)
pub fn shear_stress_beam(
  v : Double,
  s : Double,
  i : Double,
  tw : Double,
  fv : Double,
) -> Double {
  if i <= 0.0 || tw <= 0.0 {
    return 0.0
  }
  v * s / (i * tw)
}

///|
/// 受弯构件剪应力强度验算 (返回是否满足)
pub fn shear_stress_beam_check(
  v : Double,
  s : Double,
  i : Double,
  tw : Double,
  fv : Double,
) -> Bool {
  let tau = shear_stress_beam(v, s, i, tw, fv)
  tau <= fv
}

///|
/// 受弯构件整体稳定验算
/// M / (φ_b * W) ≤ f
/// 参数: m - 弯矩, phi_b - 整体稳定系数, w - 截面模量, f - 强度设计值
pub fn bending_overall_stability(
  m : Double,
  phi_b : Double,
  w : Double,
  f : Double,
) -> Double {
  if phi_b <= 0.0 || w <= 0.0 {
    return 0.0
  }
  m / (phi_b * w)
}

///|
/// 受弯构件整体稳定验算 (返回是否满足)
pub fn bending_overall_stability_check(
  m : Double,
  phi_b : Double,
  w : Double,
  f : Double,
) -> Bool {
  let sigma = bending_overall_stability(m, phi_b, w, f)
  sigma <= f
}

///|
/// 受弯构件局部稳定验算 (翼缘)
/// b/t ≤ [b/t]
pub fn local_stability_flange(b_t_ratio : Double, limit : Double) -> Bool {
  b_t_ratio <= limit
}

///|
/// 受弯构件局部稳定验算 (腹板)
/// h_w/t_w ≤ [h_w/t_w]
pub fn local_stability_web(h_t_ratio : Double, limit : Double) -> Bool {
  h_t_ratio <= limit
}

///|
/// 受弯构件挠度验算
/// f ≤ [f]
/// 参数: f_actual - 实际挠度 (m), f_limit - 允许挠度 (m)
pub fn deflection_check(f_actual : Double, f_limit : Double) -> Bool {
  f_actual <= f_limit
}

// ==================== 压弯构件 (柱) ====================

///|
/// 压弯构件强度验算 (单向弯曲)
/// N/A + M/W ≤ f
pub fn beam_column_strength(
  n : Double,
  a : Double,
  m : Double,
  w : Double,
  f : Double,
) -> Double {
  if a <= 0.0 || w <= 0.0 {
    return 0.0
  }
  n / a + m / w
}

///|
/// 压弯构件强度验算 (返回是否满足)
pub fn beam_column_strength_check(
  n : Double,
  a : Double,
  m : Double,
  w : Double,
  f : Double,
) -> Bool {
  let sigma = beam_column_strength(n, a, m, w, f)
  sigma <= f
}

///|
/// 压弯构件平面内稳定验算
/// N/(φ_x*A) + M_x/(γ_x*W_x) ≤ f
/// 参数: n - 轴力, phi_x - 轴心受压稳定系数, a - 面积, mx - 弯矩, gamma_x - 塑性发展系数, wx - 截面模量, f - 强度设计值
pub fn beam_column_in_plane_stability(
  n : Double,
  phi_x : Double,
  a : Double,
  mx : Double,
  gamma_x : Double,
  wx : Double,
  f : Double,
) -> Double {
  if phi_x <= 0.0 || a <= 0.0 || gamma_x <= 0.0 || wx <= 0.0 {
    return 0.0
  }
  n / (phi_x * a) + mx / (gamma_x * wx)
}

///|
/// 压弯构件平面内稳定验算 (返回是否满足)
pub fn beam_column_in_plane_stability_check(
  n : Double,
  phi_x : Double,
  a : Double,
  mx : Double,
  gamma_x : Double,
  wx : Double,
  f : Double,
) -> Bool {
  let sigma = beam_column_in_plane_stability(n, phi_x, a, mx, gamma_x, wx, f)
  sigma <= f
}

///|
/// 压弯构件平面外稳定验算
/// N/(φ_y*A) + M_x/(φ_b*W_x) ≤ f
pub fn beam_column_out_plane_stability(
  n : Double,
  phi_y : Double,
  a : Double,
  mx : Double,
  phi_b : Double,
  wx : Double,
  f : Double,
) -> Double {
  if phi_y <= 0.0 || a <= 0.0 || phi_b <= 0.0 || wx <= 0.0 {
    return 0.0
  }
  n / (phi_y * a) + mx / (phi_b * wx)
}

///|
/// 压弯构件平面外稳定验算 (返回是否满足)
pub fn beam_column_out_plane_stability_check(
  n : Double,
  phi_y : Double,
  a : Double,
  mx : Double,
  phi_b : Double,
  wx : Double,
  f : Double,
) -> Bool {
  let sigma = beam_column_out_plane_stability(n, phi_y, a, mx, phi_b, wx, f)
  sigma <= f
}

// ==================== 连接设计 - 焊缝 ====================

///|
/// 对接焊缝抗拉强度验算
/// σ = N / (l_w * t) ≤ f_t^w
/// 参数: n - 轴力 (N), lw - 焊缝长度 (m), t - 板厚 (m), ftw - 抗拉强度设计值 (Pa)
pub fn butt_weld_tension(
  n : Double,
  lw : Double,
  t : Double,
  ftw : Double,
) -> Double {
  if lw <= 0.0 || t <= 0.0 {
    return 0.0
  }
  n / (lw * t)
}

///|
/// 对接焊缝抗拉强度验算 (返回是否满足)
pub fn butt_weld_tension_check(
  n : Double,
  lw : Double,
  t : Double,
  ftw : Double,
) -> Bool {
  let sigma = butt_weld_tension(n, lw, t, ftw)
  sigma <= ftw
}

///|
/// 对接焊缝抗剪强度验算
/// τ = V / (l_w * t) ≤ f_v^w
pub fn butt_weld_shear(
  v : Double,
  lw : Double,
  t : Double,
  fvw : Double,
) -> Double {
  if lw <= 0.0 || t <= 0.0 {
    return 0.0
  }
  v / (lw * t)
}

///|
/// 对接焊缝抗剪强度验算 (返回是否满足)
pub fn butt_weld_shear_check(
  v : Double,
  lw : Double,
  t : Double,
  fvw : Double,
) -> Bool {
  let tau = butt_weld_shear(v, lw, t, fvw)
  tau <= fvw
}

///|
/// 角焊缝强度验算
/// √(σ_f² + τ_f²) ≤ f_f^w
/// 参数: sigma_f - 正应力, tau_f - 剪应力, ffw - 角焊缝强度设计值
pub fn fillet_weld_strength(
  sigma_f : Double,
  tau_f : Double,
  ffw : Double,
) -> Double {
  (square(sigma_f) + square(tau_f)).sqrt()
}

///|
/// 角焊缝强度验算 (返回是否满足)
pub fn fillet_weld_strength_check(
  sigma_f : Double,
  tau_f : Double,
  ffw : Double,
) -> Bool {
  let stress = fillet_weld_strength(sigma_f, tau_f, ffw)
  stress <= ffw
}

///|
/// 角焊缝有效厚度
/// h_e = 0.7 * h_f
/// 参数: hf - 焊脚尺寸 (m)
pub fn fillet_weld_effective_thickness(hf : Double) -> Double {
  0.7 * hf
}

///|
/// 角焊缝有效长度
/// l_w = l - 2*h_f (实际长度减去起弧落弧)
pub fn fillet_weld_effective_length(l : Double, hf : Double) -> Double {
  max(0.0, l - 2.0 * hf)
}

// ==================== 连接设计 - 螺栓 ====================

///|
/// 普通螺栓抗剪承载力
/// N_v^b = n_v * (π*d²/4) * f_v^b
/// 参数: nv - 受剪面数, d - 螺栓直径 (m), fvb - 抗剪强度设计值 (Pa)
pub fn bolt_shear_capacity(nv : Int, d : Double, fvb : Double) -> Double {
  let area = PI * square(d) / 4.0
  nv.to_double() * area * fvb
}

///|
/// 普通螺栓承压承载力
/// N_c^b = d * Σt * f_c^b
/// 参数: d - 螺栓直径 (m), sum_t - 同一方向承压构件总厚度 (m), fcb - 承压强度设计值 (Pa)
pub fn bolt_bearing_capacity(
  d : Double,
  sum_t : Double,
  fcb : Double,
) -> Double {
  d * sum_t * fcb
}

///|
/// 普通螺栓抗拉承载力
/// N_t^b = (π*d_e²/4) * f_t^b
/// 参数: de - 有效直径 (m), ftb - 抗拉强度设计值 (Pa)
pub fn bolt_tension_capacity(de : Double, ftb : Double) -> Double {
  let area = PI * square(de) / 4.0
  area * ftb
}

///|
/// 螺栓连接承载力 (取较小值)
/// N = min(N_v^b, N_c^b)
pub fn bolt_connection_capacity(nv : Double, nc : Double) -> Double {
  min(nv, nc)
}

///|
/// 高强度螺栓摩擦型抗剪承载力
/// N_v^b = 0.9 * n_f * μ * P
/// 参数: nf - 传力摩擦面数, mu - 摩擦系数, p - 预拉力 (N)
pub fn hsb_friction_shear_capacity(nf : Int, mu : Double, p : Double) -> Double {
  0.9 * nf.to_double() * mu * p
}

///|
/// 高强度螺栓预拉力
/// P = 0.75 * A_s * f_u
/// 参数: area_s - 螺纹有效面积 (m²), fu - 螺栓抗拉强度 (Pa)
pub fn hsb_preload(area_s : Double, fu : Double) -> Double {
  0.75 * area_s * fu
}

// ==================== 稳定计算 ====================

///|
/// 整体稳定系数 (受弯构件,简化)
/// φ_b = 1.0 (当满足构造要求时)
pub fn overall_stability_coefficient_beam(
  lambda_y : Double,
  eta_b : Double,
) -> Double {
  // 简化计算,实际应查表
  if lambda_y < 120.0 {
    1.0
  } else {
    0.95
  }
}

///|
/// 局部稳定 (板件宽厚比)
/// 判断是否满足局部稳定要求
pub fn local_stability_check(b_t_ratio : Double, limit : Double) -> Bool {
  b_t_ratio <= limit
}

///|
/// 局部稳定 (加劲肋设置)
/// 判断是否需要设置加劲肋
pub fn stiffener_required(h_t_ratio : Double, limit : Double) -> Bool {
  h_t_ratio > limit
}

// ==================== 疲劳计算 ====================

///|
/// 疲劳强度验算
/// Δσ ≤ [Δσ]
/// 参数: delta_sigma - 应力幅 (Pa), delta_sigma_limit - 容许应力幅 (Pa)
pub fn fatigue_strength_check(
  delta_sigma : Double,
  delta_sigma_limit : Double,
) -> Bool {
  delta_sigma <= delta_sigma_limit
}

///|
/// 疲劳容许应力幅 (简化)
/// [Δσ] = C / n^(1/β)
/// 参数: c - 常数, n - 循环次数, beta - 指数
/// 注意: 简化计算,当 beta=3 时使用近似公式
pub fn fatigue_allowable_stress_amplitude(
  c : Double,
  n : Double,
  beta : Double,
) -> Double {
  if n <= 0.0 || beta <= 0.0 {
    return 0.0
  }
  // 简化: 当 beta=3 时,[Δσ] ≈ C / n^(1/3)
  // 使用近似: n^(1/3) ≈ n^0.33
  // 更简单的方法: 使用对数,但这里简化处理
  if approx_equal(beta, 3.0, 0.1) {
    // n^(1/3) 的近似
    let mut x = n
    let mut i = 0
    while i < 20 {
      let x3 = x * x * x
      let x2 = x * x
      if abs(x2) < EPSILON {
        break
      }
      let x_new = x - (x3 - n) / (3.0 * x2)
      if abs(x_new - x) < EPSILON {
        return c / x_new
      }
      x = x_new
      i = i + 1
    }
    c / x
  } else {
    // 其他 beta 值,使用简化: 假设 beta=3
    let mut x = n
    let mut i = 0
    while i < 20 {
      let x3 = x * x * x
      let x2 = x * x
      if abs(x2) < EPSILON {
        break
      }
      let x_new = x - (x3 - n) / (3.0 * x2)
      if abs(x_new - x) < EPSILON {
        return c / x_new
      }
      x = x_new
      i = i + 1
    }
    c / x
  }
}

///|
/// 应力幅
/// Δσ = σ_max - σ_min
pub fn stress_amplitude(sigma_max : Double, sigma_min : Double) -> Double {
  sigma_max - sigma_min
}

// ==================== 防火设计 ====================

///|
/// 钢材温度计算 (简化)
/// T = T_0 + (Q / (ρ * c * V))
/// 参数: t0 - 初始温度 (℃), q - 热量 (J), rho - 密度 (kg/m³), c - 比热容 (J/(kg·K)), v - 体积 (m³)
pub fn steel_temperature(
  t0 : Double,
  q : Double,
  rho : Double,
  c : Double,
  v : Double,
) -> Double {
  if rho <= 0.0 || c <= 0.0 || v <= 0.0 {
    return t0
  }
  t0 + q / (rho * c * v)
}

///|
/// 防火保护层厚度 (简化)
/// 根据耐火极限要求确定
pub fn fire_protection_thickness(
  fire_resistance_time : Double,
  steel_section_factor : Double,
) -> Double {
  // 简化公式,实际应查表
  fire_resistance_time * steel_section_factor * 0.01
}

///|
/// 防火验算
/// T ≤ T_critical
pub fn fire_resistance_check(t : Double, t_critical : Double) -> Bool {
  t <= t_critical
}

// ==================== 构造要求 ====================

///|
/// 最小板厚验算
pub fn min_thickness_check(t : Double, t_min : Double) -> Bool {
  t >= t_min
}

///|
/// 螺栓间距验算
/// 3d ≤ s ≤ min(15t, 200mm)
pub fn bolt_spacing_check(s : Double, d : Double, t : Double) -> Bool {
  let min_spacing = 3.0 * d
  let max_spacing = min(15.0 * t, 0.2)
  s >= min_spacing && s <= max_spacing
}

///|
/// 螺栓边距验算
/// 1.5d ≤ e ≤ min(4t, 8d)
pub fn bolt_edge_distance_check(e : Double, d : Double, t : Double) -> Bool {
  let min_edge = 1.5 * d
  let max_edge = min(4.0 * t, 8.0 * d)
  e >= min_edge && e <= max_edge
}

// ==================== 辅助函数 ====================

///|
/// 荷载组合 (基本组合)
/// S = γ_G * G + γ_Q * Q
pub fn load_combination_basic(
  g : Double,
  q : Double,
  gamma_g : Double,
  gamma_q : Double,
) -> Double {
  gamma_g * g + gamma_q * q
}

///|
/// 荷载组合 (标准组合)
/// S = G + Q
pub fn load_combination_standard(g : Double, q : Double) -> Double {
  g + q
}

///|
/// 截面分类判断 (根据宽厚比)
pub fn section_classification(
  b_t_ratio : Double,
  limit_s3 : Double,
  limit_s4 : Double,
) -> String {
  if b_t_ratio <= limit_s3 {
    "S3"
  } else if b_t_ratio <= limit_s4 {
    "S4"
  } else {
    "S5"
  }
}

///|
/// 塑性发展系数
/// γ_x = 1.0 (弹性), 1.05 (部分塑性)
pub fn plastic_development_coefficient(section_class : String) -> Double {
  if section_class == "S1" || section_class == "S2" {
    1.05
  } else {
    1.0
  }
}