import Lean

/-!
# Al–Cu：守恒、物料平衡与两类相场动力学

这是可以独立运行的课堂证明，仅依赖 Lean 4.8.0 自带的库。
命令：lean AlCu.lean

模型使用 Int 作精确代数核验。各量先用约定单位换算，能量、通量和速率的
乘积使用相应的乘积单位。有理数关系可清除正分母后逐项代入。符号 μ、g 表示
已由热力学模型给定的势与驱动力。下列证明显式处理共切线、质量守恒和有限单元的瞬时能量收支。
空间连续方程、时间离散和 Al–Cu 实验参数均在课件中另行讨论。
-/

namespace AlCu

/-! ## 1. 共切线、组元化学势与混合物的能量下界

以 X 表示按共同尺度放大后的 Cu 原子分数，纯 Al、纯 Cu 分别在 X=0 和 X=S。
斜率按此坐标取值。曲线上组分 X 处的切线，与两端点相交时给出
μAl=g−mX、μCu=μAl+mS。在物理坐标 S=1 时，后者即 μCu=μAl+m。
斜率来自各相自由能对组分的导数；这里将相切斜率和接触处能量作为输入。
-/

theorem common_tangent_equal_mu
    (xA xB gA gB slopeA slopeB m intercept S : Int)
    (slopeLeft : slopeA = m) (slopeRight : slopeB = m)
    (contactLeft : gA = m * xA + intercept)
    (contactRight : gB = m * xB + intercept) :
    gA - slopeA * xA = gB - slopeB * xB ∧
    (gA - slopeA * xA) + slopeA * S = (gB - slopeB * xB) + slopeB * S := by
  rw [slopeLeft, slopeRight, contactLeft, contactRight]
  omega

-- 对同一条线按相的原子量求和，仅依赖 Cu 总量 wA*xA+wB*xB 与总原子量 wA+wB。
private theorem weighted_affine_identity (wA wB xA xB m intercept : Int) :
    wA * (m * xA + intercept) + wB * (m * xB + intercept) =
      m * (wA * xA + wB * xB) + (wA + wB) * intercept := by
  simp only [Int.mul_add, Int.add_mul, Int.mul_left_comm]
  omega

-- 对任何一对候选组成都可应用；若直线在所有候选点下方，每种两相混合物均受此界约束。
theorem affine_mixture_lower_bound (wA wB xA xB gA gB m intercept : Int)
    (nonnegA : 0 ≤ wA) (nonnegB : 0 ≤ wB)
    (supportA : m * xA + intercept ≤ gA)
    (supportB : m * xB + intercept ≤ gB) :
    m * (wA * xA + wB * xB) + (wA + wB) * intercept ≤ wA * gA + wB * gB := by
  rw [← weighted_affine_identity]
  exact Int.add_le_add (Int.mul_le_mul_of_nonneg_left supportA nonnegA)
    (Int.mul_le_mul_of_nonneg_left supportB nonnegB)

-- 在两个接触组分取值时恰达下界。与上一条合用，可比较同总量、同总组分的两相混合物。
theorem contact_mixture_attains_bound (wA wB xA xB gA gB m intercept : Int)
    (contactA : gA = m * xA + intercept)
    (contactB : gB = m * xB + intercept) :
    wA * gA + wB * gB = m * (wA * xA + wB * xB) + (wA + wB) * intercept := by
  rw [contactA, contactB]
  exact weighted_affine_identity wA wB xA xB m intercept


/-! ## 2. 从守恒方程解出相分数

W 是总量，B 是 β 相的量，a 和 b 是系线端点。若组分用 Cu 质量分数，W、B
必须用质量；若用原子分数，则按组成原子的物质的量计。物理使用范围 W>0、
0≤B≤W、a<b。结论 B(b−a)=W(c₀−a) 可在这些正分母条件下还原成杠杆定律。
-/

theorem lever_from_mass_balance (W B a b c₀ : Int)
    (balance : (W - B) * a + B * b = W * c₀) :
    B * (b - a) = W * (c₀ - a) := by
  simp only [Int.sub_mul, Int.mul_sub] at balance ⊢
  omega

/-! ## 3. 离散自由能的梯度项怎样进入化学势？

两个等体积相邻单元，网格距离为 1，Δc=cR−cL。若每个单元的体自由能导数为
fL、fR，键能为 κ(Δc)²/2，其方向导数为 κΔc(vR−vL)。下面整理总方向导数，
得到 μL=fL−κΔc、μR=fR+κΔc。本命题核验方向导数展开后的系数收集。
-/

theorem gradient_energy_coefficients (κ Δc fL fR vL vR : Int) :
    (fL - κ * Δc) * vL + (fR + κ * Δc) * vR =
      fL * vL + fR * vR + (κ * Δc) * (vR - vL) := by
  simp only [Int.sub_mul, Int.add_mul, Int.mul_sub]
  omega

/-! ## 4. Cahn–Hilliard：无外部通量的两单元交换

J=M(μL−μR)，方向从左到右。左单元变化率 −J，右单元变化率 J。
此处 CH 有限体积模型中只保留两个单元之间的一条键。定义的是瞬时速率。
-/

def chFlux (M μL μR : Int) : Int := M * (μL - μR)

theorem ch_mass_conservation (J : Int) : -J + J = 0 := by
  omega

theorem ch_energy_identity (M μL μR : Int) :
    μL * (-chFlux M μL μR) + μR * chFlux M μL μR =
      -(M * ((μL - μR) * (μL - μR))) := by
  calc
    _ = -((μL - μR) * chFlux M μL μR) := by
      rw [Int.sub_mul, Int.mul_neg]
      omega
    _ = _ := by simp only [chFlux, Int.mul_left_comm]

private theorem square_nonnegative (z : Int) : 0 ≤ z * z := by
  by_cases hz : 0 ≤ z
  · exact Int.mul_nonneg hz hz
  · have hn : z ≤ 0 := by omega
    exact Int.mul_nonneg_of_nonpos_of_nonpos hn hn

theorem ch_energy_nonincrease (M μL μR : Int) (hM : 0 ≤ M) :
    μL * (-chFlux M μL μR) + μR * chFlux M μL μR ≤ 0 := by
  rw [ch_energy_identity]
  exact Int.neg_nonpos_of_nonneg (Int.mul_nonneg hM (square_nonnegative _))

/-! ## 5. Allen–Cahn：局部非守恒序参量

取 g=∂F/∂η，L≥0，η 的变化率为 −Lg。两单元也可逐项求和。
g 可包含结构有序化、界面或弹性能模型给出的贡献。
-/

def acRate (L g : Int) : Int := -(L * g)

theorem ac_energy_identity (L g : Int) :
    g * acRate L g = -(L * (g * g)) := by
  simp only [acRate, Int.mul_neg, Int.mul_left_comm]

theorem ac_energy_nonincrease (L g : Int) (hL : 0 ≤ L) :
    g * acRate L g ≤ 0 := by
  rw [ac_energy_identity]
  exact Int.neg_nonpos_of_nonneg (Int.mul_nonneg hL (square_nonnegative _))

-- 一个非零速率的精确反例：AC 对序参量本身没有总量守恒要求。
theorem ac_order_can_change : acRate 1 1 ≠ 0 := by decide

-- 耦合模型示例：c 随 CH 交换，两个单元的 η 分别随 AC 弛豫。
theorem coupled_energy_nonincrease (M L μL μR gL gR : Int)
    (hM : 0 ≤ M) (hL : 0 ≤ L) :
    (μL * (-chFlux M μL μR) + μR * chFlux M μL μR) +
      gL * acRate L gL + gR * acRate L gR ≤ 0 := by
  have hCH := ch_energy_nonincrease M μL μR hM
  have hLeft := ac_energy_nonincrease L gL hL
  have hRight := ac_energy_nonincrease L gR hL
  omega

end AlCu
