attempt-005/Model.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue
/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)
## REVIEW PACKET
SUPPLIED BY THE USER: 1 L water at 20 C in a pot on a stove; salt will be added; goal = salted water reaches
its boiling point soonest; salt amount, pot, heater, room unspecified.
ACCEPTED EARLIER (kept): lumped Newtonian heating dH/dt = P - h (T - 20), constant stove power P, no evaporation,
salt (crystal at 20 C) dissolves instantly at the moment of addition (total enthalpy conserved).
MODEL CHOICES / ASSUMPTIONS (all appear as `Premises` fields or theorem hypotheses):
* NaCl; r = salt mass / water mass in [1/50, 1/20] (20-50 g in the 0.998 kg of water of 1 L); mass fraction
w = r/(1+r) (conversion proved in `w_range`), n = 1000*0.998*r/58.44 mol (`nSalt`, bounds in `n_range`).
* Pot+water enthalpy Hw(T) has heat capacity Cw(T) >= 4100 J/K on 10..101 C (0.998 kg water >= 4.17 kJ/K; pot adds).
* Apparent molar heat capacity of NaCl in the final solution = Laliberte (2009) correlation `PhiC`
(empirical coefficients a1..a6 from Laliberte, J. Chem. Eng. Data 54, 1725; fit range 1.5-120 C, w <= 0.26).
Ph(T) is the apparent molar enthalpy of NaCl in that solution: dPh/dT = PhiC (definition of Phi_C).
Solution enthalpy = (water at T) + n*Ph(T) (definition of apparent molar enthalpy; `solEnthalpy`).
* Crystal NaCl enthalpy Hcr with Cp_cr in [40,100] J/K/mol on 20..25 C (Archer 1992 Part I, Eqs 4-6: Cp(298.15)=50.16;
my evaluation of the spline gives 50.04..50.16 on 20..25 C).
* Parker NSRDS-NBS 2 (1965): Delta H_inf(25 C, NaCl(c) -> infinite dilution) = 928 +- 5 cal/mol (printed p.26);
Phi_L(25 C, m) (Table X, p.50) is RELATIVE to infinite dilution: Ph(25) - Hcr(25) = Hinf + Phi_L.
For the regime m = 17.1 r in [0.34, 0.86] mol/kg Table X gives Phi_L = 83, 49, 20, -41 cal/mol at m = .2775,
.5551, .7401, 1.1101 and dPhi_L/dm^(1/2) < 0 for m >= .185, so Phi_L in [-41, 88]; premise used: [-41, 100] cal. cal = 4.184 J.
* Boiling temperature Tb of the final solution: 20 <= Tb <= 101 C (1 atm, <= 4.8 wt%: ideal elevation <= ~0.9 K); stove
beats losses at Tb; Tw, Ts = temperature as function of total enthalpy (inverse of Hw, Hs; existence by IVT,
`inverse_exists`); tau_w, tau_s = elapsed time as function of enthalpy: d tau/dH = 1/(P - h (T(H) - 20)).
PROVED (not assumed): Phi_C < 0 and >= -105 J/(K mol) for all regime/temperatures (concavity + exp bounds, `cs_sign`);
trapezoid enclosure Ph(101) - Ph(20) >= -3363 J/mol from 11 Taylor-bounded exp evaluations (`ph_drop`);
dissolution at 20 C >= 3890 J/mol via Kirchhoff (`thermo_facts`); salt addition at any x in [20,Tb] cools
(Hs(x) - Hw(x) >= 0), solution enthalpy strictly increasing, mixing at 20 C stays above 10 C; and the timing
comparison (`timing_core`, `answer_theorem`).
CONCLUSIONS: `cs_sign`, `answer_theorem` (salting at the start is no slower than salting at any later water temperature).
-/
/-! ### calculus helpers -/
lemma dmono (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
(hf : ∀ T ∈ Set.Icc a b, 0 ≤ f T) : MonotoneOn F (Set.Icc a b) := by
apply monotoneOn_of_deriv_nonneg (convex_Icc a b)
· exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
· exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
· intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)
lemma danti (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
(hf : ∀ T ∈ Set.Icc a b, f T ≤ 0) : AntitoneOn F (Set.Icc a b) := by
apply antitoneOn_of_deriv_nonpos (convex_Icc a b)
· exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
· exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
· intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)
lemma dstrict (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
(hf : ∀ T ∈ Set.Icc a b, 0 < f T) : StrictMonoOn F (Set.Icc a b) := by
apply strictMonoOn_of_deriv_pos (convex_Icc a b)
· exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
· intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)
lemma lin_deriv (F : ℝ → ℝ) (d c T : ℝ) (h : HasDerivAt F d T) :
HasDerivAt (fun y => F y + c*y) (d + c) T :=
(h.add ((hasDerivAt_id T).const_mul c)).congr_deriv (by simp)
/-- Taylor-polynomial upper bound for exp on [0,4] (from `Real.exp_bound`, 7 terms, applied to x/4 and raised to the 4th power) -/
noncomputable def Q (x : ℝ) : ℝ := ((∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280))^4
lemma exp_ub (x : ℝ) (h0 : 0 ≤ x) (h4 : x ≤ 4) : Real.exp x ≤ Q x := by
have hy : |x/4| ≤ 1 := by rw [abs_le]; constructor <;> linarith
have hb := Real.exp_bound hy (n := 7) (by norm_num)
rw [abs_of_nonneg (by linarith : 0 ≤ x/4)] at hb
have h1 := (abs_le.mp hb).2
have e : ((Nat.succ 7 : ℕ) : ℝ) / ((Nat.factorial 7 : ℝ) * (7:ℕ)) = 8/35280 := by norm_num [Nat.factorial]
rw [e] at h1
have e2 : Real.exp x = Real.exp (x/4) ^ 4 := by
rw [← Real.exp_nat_mul]; congr 1; push_cast; ring
rw [e2]; exact pow_le_pow_left₀ (Real.exp_pos _).le (by linarith) 4
/-! ### Laliberté (2009) NaCl correlation: empirical inputs (exposed) and derived consequences -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def a1 : ℝ := -0.0693559668993322
noncomputable def a2 : ℝ := -0.0782134167486952
noncomputable def a3 : ℝ := 3.84798479408635
noncomputable def a4 : ℝ := -11.2762109247072
noncomputable def a5 : ℝ := 8.73187698542672
noncomputable def a6 : ℝ := 1.81245930472755
/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity
Cp_i in J/(g K)); w = salt mass fraction, T in C -/
noncomputable def PhiC (w T : ℝ) : ℝ :=
Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)
/-- Laliberté's mixing rule Cp_sol = w_w Cp_w + w_s Cp_i gives C_solution - C_water = ms*Cp_i = (ms/M)*(M*Cp_i):
per mole of salt the heat-capacity change is M*Cp_i = PhiC. -/
lemma apparent_capacity (ms mw M cpw cpi : ℝ) (hM : 0 < M) (hs : 0 < ms + mw) :
(ms+mw) * ((mw/(ms+mw)) * cpw + (ms/(ms+mw)) * cpi) - mw * cpw = (ms/M) * (M * cpi) := by
field_simp; ring
lemma phi_mono_w (w T : ℝ) (h : 1/51 ≤ w) : PhiC (1/51) T ≤ PhiC w T := by
unfold PhiC
have h1 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) ≤ Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) :=
Real.exp_le_exp.mpr (by unfold a4; linarith)
have h2 : (1/51:ℝ) ^ a6 ≤ w ^ a6 := Real.rpow_le_rpow (by norm_num) h (by unfold a6; norm_num)
have ha1 : a1 < 0 := by unfold a1; norm_num
have ha5 : 0 < a5 := by unfold a5; norm_num
have hM : 0 < Mnacl := by unfold Mnacl; norm_num
exact mul_le_mul_of_nonneg_left (by nlinarith) hM.le
lemma phi_neg (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T < 0 := by
unfold PhiC
have hw : 0 < w := by linarith
have e1 : w ^ a6 ≤ w ^ ((3:ℝ)/2) :=
Real.rpow_le_rpow_of_exponent_ge hw (by linarith) (by unfold a6; norm_num)
have e2 : w ^ ((3:ℝ)/2) = w * Real.sqrt w := by
rw [Real.sqrt_eq_rpow, show ((3:ℝ)/2) = 1 + 1/2 by norm_num, Real.rpow_add hw, Real.rpow_one]
have e3 : Real.sqrt w ≤ 1/4 := by rw [Real.sqrt_le_left (by norm_num)]; nlinarith
have e4 : w ^ a6 ≤ 1/84 := by have := Real.sqrt_nonneg w; nlinarith
have q := Real.quadratic_le_exp_of_nonneg (x := 0.01*T) (by linarith)
set E := Real.exp (0.01*T)
have hα : 1.2 ≤ a2*T + a3*E + a4*w := by
unfold a2 a3 a4
nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ T - 10)]
have hex : 2 ≤ Real.exp (a2*T + a3*E + a4*w) := by linarith [Real.add_one_le_exp (a2*T + a3*E + a4*w)]
have hM : 0 < Mnacl := by unfold Mnacl; norm_num
refine mul_neg_of_pos_of_neg hM ?_
unfold a1 a5 at *
nlinarith
/-- Φ_C is concave in T: its value at T lies above the chord between any a < b. -/
lemma phi_chord (w a b T : ℝ) (hab : a < b) (h1 : a ≤ T) (h2 : T ≤ b) :
(b-a) * PhiC w T ≥ (b-T) * PhiC w a + (T-a) * PhiC w b := by
set α : ℝ → ℝ := fun s => a2*s + a3*Real.exp (0.01*s) + a4*w with hαdef
have ha3 : 0 < a3 := by unfold a3; norm_num
have tang : ∀ s, Real.exp (α s) ≥ Real.exp (α T) * (1 + (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T)) := by
intro s
have e1 : Real.exp (0.01*s) ≥ Real.exp (0.01*T) * (1 + 0.01*(s-T)) := by
have : Real.exp (0.01*s) = Real.exp (0.01*T) * Real.exp (0.01*(s-T)) := by
rw [← Real.exp_add]; congr 1; ring
rw [this]
nlinarith [Real.add_one_le_exp (0.01*(s-T)), Real.exp_pos (0.01*T)]
have e2 : α s - α T ≥ (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T) := by
simp only [hαdef]; nlinarith
have e3 : Real.exp (α s) = Real.exp (α T) * Real.exp (α s - α T) := by
rw [← Real.exp_add]; congr 1; ring
rw [e3]
nlinarith [Real.add_one_le_exp (α s - α T), Real.exp_pos (α T)]
have ta := tang a
have tb := tang b
have hg : (b-a) * Real.exp (α T) ≤ (b-T) * Real.exp (α a) + (T-a) * Real.exp (α b) := by
nlinarith [mul_le_mul_of_nonneg_left ta (by linarith : (0:ℝ) ≤ b - T),
mul_le_mul_of_nonneg_left tb (by linarith : (0:ℝ) ≤ T - a)]
have ha1 : a1 < 0 := by unfold a1; norm_num
have hM : 0 < Mnacl := by unfold Mnacl; norm_num
unfold PhiC
have : (b-a) * (a1 * Real.exp (α T)) ≥ (b-T) * (a1 * Real.exp (α a)) + (T-a) * (a1 * Real.exp (α b)) := by
nlinarith
simp only [hαdef] at this
nlinarith [mul_pos hM (by linarith : (0:ℝ) < b - a)]
/-- trapezoid lower bound: if F' = f lies above its chord on [a,b] then F b - F a ≥ (b-a)(f a + f b)/2 -/
lemma trap (F f : ℝ → ℝ) (a b : ℝ) (hab : a < b)
(hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
(hc : ∀ T ∈ Set.Icc a b, (b-a) * f T ≥ (b-T) * f a + (T-a) * f b) :
(b-a)*(f a + f b)/2 ≤ F b - F a := by
have hba : 0 < b - a := by linarith
have hd : ∀ T ∈ Set.Icc a b, HasDerivAt
(fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (f T - ((b-T) * f a + (T-a) * f b)/(b-a)) T := by
intro T hT
have u : HasDerivAt (fun T : ℝ => T - a) 1 T := (hasDerivAt_id T).sub_const a
have v := (u.const_mul (f a)).add (((u.pow 2).mul_const (f b - f a)).div_const (2*(b-a)))
convert (hF T hT).sub_const (F a) |>.sub v using 1
field_simp; ring
have := dmono _ _ a b hd (fun T hT => by
have := hc T hT
rw [sub_nonneg, div_le_iff₀ hba]; linarith) ⟨le_rfl, hab.le⟩ ⟨hab.le, le_rfl⟩ hab.le
simp only at this
have e : (f a * (b-a) + (b-a)^2 * (f b - f a) / (2*(b-a))) = (b-a)*(f a + f b)/2 := by
field_simp; ring
simp at this
nlinarith [e]
lemma node (w T V β U L : ℝ) (h : 1/51 ≤ w)
(hh : 0 ≤ T ∧ T ≤ 400 ∧ Q (0.01*T) ≤ V ∧ a2*T + a3*V + a4*(1/51) ≤ β ∧ 0 ≤ β ∧ β ≤ 4 ∧ Q β ≤ U ∧
-L ≤ Mnacl*a1*U) : -L ≤ PhiC w T := by
obtain ⟨h0, h4, hV, hb, hb0, hb4, hU, hL⟩ := hh
refine le_trans ?_ (phi_mono_w w T h)
have e1 : Real.exp (0.01*T) ≤ V := (exp_ub _ (by linarith) (by linarith)).trans hV
have ha3 : 0 < a3 := by unfold a3; norm_num
have e2 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) ≤ U :=
(Real.exp_le_exp.mpr (by nlinarith [mul_le_mul_of_nonneg_left e1 ha3.le])).trans ((exp_ub β hb0 hb4).trans hU)
have h5 : 0 ≤ a5 * (1/51:ℝ)^a6 := mul_nonneg (by unfold a5; norm_num) (Real.rpow_nonneg (by norm_num) _)
have ha1 : a1 < 0 := by unfold a1; norm_num
have hM : 0 < Mnacl := by unfold Mnacl; norm_num
unfold PhiC
nlinarith [mul_le_mul_of_nonneg_left e2 (mul_pos hM (neg_pos.mpr ha1)).le, mul_nonneg hM.le h5]
lemma nodes (w : ℝ) (h : 1/51 ≤ w) : -(104.51:ℝ) ≤ PhiC w 10 ∧ -(74.77:ℝ) ≤ PhiC w 20 ∧ -(56.07:ℝ) ≤ PhiC w 30 ∧ -(44.29:ℝ) ≤ PhiC w 40 ∧ -(37.06:ℝ) ≤ PhiC w 50 ∧ -(33.04:ℝ) ≤ PhiC w 60 ∧ -(31.6:ℝ) ≤ PhiC w 70 ∧ -(32.65:ℝ) ≤ PhiC w 80 ∧ -(36.75:ℝ) ≤ PhiC w 90 ∧ -(45.49:ℝ) ≤ PhiC w 100 ∧ -(46.73:ℝ) ≤ PhiC w 101 :=
⟨node w 10 1.10519 3.2497 25.784 104.51 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 20 1.22142 2.9148 18.447 74.77 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 30 1.34987 2.6269 13.832 56.07 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 40 1.49184 2.3911 10.927 44.29 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 50 1.64874 2.2127 9.142 37.06 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 60 1.82213 2.0978 8.15 33.04 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 70 2.01377 2.0531 7.794 31.6 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 80 2.22556 2.0859 8.053 32.65 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 90 2.45962 2.2044 9.066 36.75 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 100 2.7183 2.4177 11.222 45.49 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
node w 101 2.74562 2.4446 11.527 46.73 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1])⟩
lemma piece (w : ℝ) (Ph : ℝ → ℝ) (a b La Lb : ℝ) (hab : a < b) (ha : 10 ≤ a) (hb : b ≤ 101)
(hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T)
(hA : -La ≤ PhiC w a) (hB : -Lb ≤ PhiC w b) : -((b-a)*(La+Lb)/2) ≤ Ph b - Ph a := by
have := trap Ph (PhiC w) a b hab (fun T hT => hPh T ⟨by linarith [hT.1], by linarith [hT.2]⟩)
(fun T hT => phi_chord w a b T hab hT.1 hT.2)
nlinarith
lemma phi_floor (w T : ℝ) (h1 : 1/51 ≤ w) (t1 : 10 ≤ T) (t2 : T ≤ 101) : -105 ≤ PhiC w T := by
have c := phi_chord w 10 101 T (by norm_num) t1 t2
obtain ⟨n10, -, -, -, -, -, -, -, -, -, n101⟩ := nodes w h1
nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ 105 - 104.51),
mul_nonneg (by linarith : (0:ℝ) ≤ T - 10) (by linarith : (0:ℝ) ≤ 46.73 - 0)]
/-- Total change of apparent molar enthalpy of NaCl from 20 C to 101 C is at least -3363 J/mol. -/
lemma ph_drop (w : ℝ) (Ph : ℝ → ℝ) (h1 : 1/51 ≤ w)
(hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T) : -3363 ≤ Ph 101 - Ph 20 := by
obtain ⟨n10, n20, n30, n40, n50, n60, n70, n80, n90, n100, n101⟩ := nodes w h1
have p0 := piece w Ph 20 30 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n20 n30
have p1 := piece w Ph 30 40 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n30 n40
have p2 := piece w Ph 40 50 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n40 n50
have p3 := piece w Ph 50 60 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n50 n60
have p4 := piece w Ph 60 70 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n60 n70
have p5 := piece w Ph 70 80 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n70 n80
have p6 := piece w Ph 80 90 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n80 n90
have p7 := piece w Ph 90 100 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n90 n100
have p8 := piece w Ph 100 101 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n100 n101
norm_num at *
linarith
/-! ## Heating-time model (accepted Newtonian lumped model; state variable = total enthalpy H) -/
/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to
total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.
τw, τs = elapsed time as a function of total enthalpy on the two branches. -/
def boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=
(τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))
theorem timing_core (P h Tr Tl Tb : ℝ) (Hw Hs Tw Ts τw τs : ℝ → ℝ)
(hh : 0 ≤ h) (hP : h*(Tb-Tr) < P) (hl : Tl ≤ Tr) (hr : Tr ≤ Tb)
(hHw : StrictMonoOn Hw (Set.Icc Tl Tb)) (hHs : StrictMonoOn Hs (Set.Icc Tl Tb))
(hTw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tl Tb ∧ Hw (Tw H) = H)
(hTs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), Ts H ∈ Set.Icc Tl Tb ∧ Hs (Ts H) = H)
(hτw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H)
(hτs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), HasDerivAt τs (1/(P - h*(Ts H - Tr))) H)
(hcool : ∀ x ∈ Set.Icc Tr Tb, Hw x ≤ Hs x) (hlow : Hs Tl ≤ Hw Tr) :
∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw Hs Tr Tb Tr ≤ boilTime τw τs Hw Hs Tr Tb Ta := by
intro Ta hTa
have hTrI : Tr ∈ Set.Icc Tl Tb := ⟨hl, hr⟩
have hTaI : Ta ∈ Set.Icc Tl Tb := ⟨hl.trans hTa.1, hTa.2⟩
have hTbI : Tb ∈ Set.Icc Tl Tb := ⟨hl.trans hr, le_rfl⟩
have w1 : Hw Tr ≤ Hw Ta := hHw.monotoneOn hTrI hTaI hTa.1
have w2 : Hw Ta ≤ Hw Tb := hHw.monotoneOn hTaI hTbI hTa.2
have s1 : Hw Tb ≤ Hs Tb := hcool Tb ⟨hr, le_rfl⟩
have hpos : ∀ x, x ≤ Tb → 0 < P - h*(x - Tr) := by
intro x hx
have : h*(x-Tr) ≤ h*(Tb-Tr) := mul_le_mul_of_nonneg_left (by linarith) hh
linarith
have key : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta), 1/(P - h*(Tw H - Tr)) - 1/(P - h*(Ts H - Tr)) ≥ 0 := by
intro H hH
obtain ⟨hx, hxH⟩ := hTw H ⟨hH.1, hH.2.trans w2⟩
obtain ⟨hy, hyH⟩ := hTs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩
have hxr : Tr ≤ Tw H := by
by_contra hc
have := hHw hx hTrI (not_le.mp hc)
rw [hxH] at this
linarith [hH.1]
have hc := hcool _ ⟨hxr, hx.2⟩
rw [hxH] at hc
have hyx : Ts H ≤ Tw H := by
by_contra hcon
have := hHs hx hy (not_le.mp hcon)
rw [hyH] at this
linarith
have := mul_le_mul_of_nonneg_left (by linarith : Ts H - Tr ≤ Tw H - Tr) hh
have := one_div_le_one_div_of_le (hpos _ hx.2) (by linarith : P - h*(Tw H - Tr) ≤ P - h*(Ts H - Tr))
linarith
have mono := dmono (fun H => τw H - τs H) _ (Hw Tr) (Hw Ta)
(fun H hH => (hτw H ⟨hH.1, hH.2.trans w2⟩).sub (hτs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩)) key
have := mono ⟨le_rfl, w1⟩ ⟨w1, le_rfl⟩ w1
simp only [boilTime] at this ⊢
linarith
/-- sanity: the inverse temperature functions Tw, Ts exist (intermediate value theorem). -/
lemma inverse_exists (H : ℝ → ℝ) (Tl Tb : ℝ) (hle : Tl ≤ Tb) (hc : ContinuousOn H (Set.Icc Tl Tb)) :
∀ y ∈ Set.Icc (H Tl) (H Tb), ∃ T ∈ Set.Icc Tl Tb, H T = y := fun y hy => by
obtain ⟨T, hT, e⟩ := intermediate_value_Icc hle hc hy
exact ⟨T, hT, e⟩
/-! ## Thermodynamics of dissolving NaCl(cr) (at 20 C) in the pot -/
/-- thermochemical calorie in J (Parker 1965 reports cal) -/
noncomputable def cal : ℝ := 4.184
/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl
/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.
Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);
the salt started as crystal with enthalpy Hcr 20. -/
def solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)
/-- Physical premises (empirical data and definitions), see the review packet. -/
structure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where
r_lo : 1/50 ≤ r
r_hi : r ≤ 1/20
hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T
hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T
ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T
cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T
cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100
hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal
phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal
diss25 : Ph 25 - Hcr 25 = hinf + phiL
lemma w_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 1/51 ≤ r/(1+r) ∧ r/(1+r) ≤ 1/21 := by
constructor
· rw [le_div_iff₀ (by linarith)]; linarith
· rw [div_le_iff₀ (by linarith)]; linarith
lemma n_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 0.34 ≤ nSalt r ∧ nSalt r ≤ 0.86 := by
unfold nSalt Mnacl
constructor
· rw [le_div_iff₀ (by norm_num)]; linarith
· rw [div_le_iff₀ (by norm_num)]; linarith
theorem thermo_facts (r Tb : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ)
(hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL) (hT2 : Tb ≤ 101) :
StrictMonoOn Hw (Set.Icc 10 Tb) ∧ StrictMonoOn (solEnthalpy (nSalt r) Hw Ph Hcr) (Set.Icc 10 Tb) ∧
(∀ x ∈ Set.Icc (20:ℝ) Tb, Hw x ≤ solEnthalpy (nSalt r) Hw Ph Hcr x) ∧
solEnthalpy (nSalt r) Hw Ph Hcr 10 ≤ Hw 20 := by
obtain ⟨hw1, hw2⟩ := w_range r hp.r_lo hp.r_hi
obtain ⟨hn1, hn2⟩ := n_range r hp.r_lo hp.r_hi
set w := r/(1+r)
set n := nSalt r
have hs1 : ∀ T ∈ Set.Icc (10:ℝ) Tb, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, hT.2.trans hT2⟩
have hs2 : ∀ T ∈ Set.Icc (20:ℝ) 25, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨by linarith [hT.1], by linarith [hT.2]⟩
have hs3 : ∀ T ∈ Set.Icc (10:ℝ) 20, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, by linarith [hT.2]⟩
have hfl : ∀ T ∈ Set.Icc (10:ℝ) 101, -105 ≤ PhiC w T := fun T hT => phi_floor w T hw1 hT.1 hT.2
have hng : ∀ T ∈ Set.Icc (10:ℝ) 101, PhiC w T ≤ 0 := fun T hT => (phi_neg w T hw1 hw2 hT.1 hT.2).le
have hWs := dstrict Hw Cw 10 Tb (fun T hT => hp.hw_deriv T (hs1 T hT))
(fun T hT => by linarith [hp.hw_cap T (hs1 T hT)])
have hHs : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T :=
fun T hT => (hp.hw_deriv T hT).add (((hp.ph_deriv T hT).sub_const (Hcr 20)).const_mul n)
have hSs := dstrict _ _ 10 Tb (fun T hT => hHs T (hs1 T hT))
(fun T hT => by nlinarith [hp.hw_cap T (hs1 T hT), hfl T (hs1 T hT)])
-- Kirchhoff: dissolution enthalpy Ph T - Hcr T between 20 and 25 C
have hd : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T :=
fun T hT => (hp.ph_deriv T (hs2 T hT)).sub (hp.cr_deriv T hT)
have hlow20 := danti _ _ 20 25 (fun T hT => lin_deriv _ _ 40 T (hd T hT))
(fun T hT => by linarith [hng T (hs2 T hT), (hp.cr_cap T hT).1]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
have hup20 := dmono _ _ 20 25 (fun T hT => lin_deriv _ _ 205 T (hd T hT))
(fun T hT => by linarith [hfl T (hs2 T hT), (hp.cr_cap T hT).2]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
have hup10 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ 105 T (hp.ph_deriv T (hs3 T hT)))
(fun T hT => by linarith [hfl T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
have hup4 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ (-4100) T (hp.hw_deriv T (hs3 T hT)))
(fun T hT => by linarith [hp.hw_cap T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
simp only at hlow20 hup20 hup10 hup4
-- Parker's numbers: dissolution enthalpy at 20 C
obtain ⟨hi1, hi2⟩ := hp.hinf_b
obtain ⟨hl1, hl2⟩ := hp.phiL_b
have hcal : cal = 4.184 := rfl
rw [hcal] at hi1 hi2 hl1 hl2
have hd25 := hp.diss25
refine ⟨hWs, hSs, fun x hx => ?_, ?_⟩
· have hanti2 := danti Ph _ 10 101 hp.ph_deriv hng
have h1 : Ph 101 ≤ Ph x := hanti2 ⟨by linarith [hx.1], by linarith [hx.2]⟩ ⟨by norm_num, le_rfl⟩ (by linarith [hx.2])
have h2 := ph_drop w Ph hw1 hp.ph_deriv
unfold solEnthalpy
nlinarith
· unfold solEnthalpy
nlinarith
/-! ## Conclusions (named constants of the regime; numerals live in definitions) -/
/-- starting / room temperature of the water (C) -/
noncomputable def Tr : ℝ := 20
/-- lowest temperature considered (C) -/
noncomputable def Tmin : ℝ := 10
/-- highest admissible boiling temperature of the solution (C) -/
noncomputable def Tcap : ℝ := 101
/-- regime of the salt/water mass ratio r: 20 to 50 g NaCl per kg water -/
noncomputable def rLo : ℝ := 1/50
noncomputable def rHi : ℝ := 1/20
/-- lower bound for Phi_C (J/K/mol) and for the heat-capacity change n*Phi_C (J/K) -/
noncomputable def PhiFloor : ℝ := 105
noncomputable def CsFloor : ℝ := 91
/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C the apparent molar heat capacity of NaCl
in solution is negative and >= -105 J/(K mol); dissolving the salt LOWERS the total heat capacity of the pot
contents by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/
theorem cs_sign : ∀ (r T : ℝ), rLo ≤ r → r ≤ rHi → Tmin ≤ T → T ≤ Tcap →
-PhiFloor ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧
-CsFloor < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by
intro r T h1 h2 t1 t2
unfold rLo rHi Tmin Tcap PhiFloor CsFloor at *
obtain ⟨w1, w2⟩ := w_range r h1 h2
obtain ⟨n1, n2⟩ := n_range r h1 h2
have f := phi_floor _ T w1 t1 t2
have g := phi_neg _ T w1 w2 t1 t2
refine ⟨f, g, ?_, ?_⟩ <;> nlinarith
/-- Tw is the temperature as a function of total enthalpy H on the branch with enthalpy-temperature relation Hf
(Hf (T H) = H, T H in [Tl,Tb]), for H in [a,b]. -/
def InverseOn (Hf T : ℝ → ℝ) (Tl Tb a b : ℝ) : Prop := ∀ H ∈ Set.Icc a b, T H ∈ Set.Icc Tl Tb ∧ Hf (T H) = H
/-- τ is the elapsed time as a function of total enthalpy on a branch with temperature profile T(H): since
dH/dt = P - h (T - Tr) > 0, dτ/dH = 1/(P - h (T(H) - Tr)). -/
def TimeProfile (P h Tr : ℝ) (τ T : ℝ → ℝ) (a b : ℝ) : Prop :=
∀ H ∈ Set.Icc a b, HasDerivAt τ (1/(P - h*(T H - Tr))) H
/-- CONCLUSION 2 (answer). Salt added at the very start (water at Tr = 20 C) gives a boiling time no longer than salt
added once the water has reached any later temperature Ta <= Tb. -/
theorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),
Premises r Cw Hw Ph Hcr Cpcr hinf phiL → Tr ≤ Tb → Tb ≤ Tcap → 0 ≤ h → h*(Tb-Tr) < P →
InverseOn Hw Tw Tmin Tb (Hw Tr) (Hw Tb) →
InverseOn (solEnthalpy (nSalt r) Hw Ph Hcr) Ts Tmin Tb
(solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) →
TimeProfile P h Tr τw Tw (Hw Tr) (Hw Tb) →
TimeProfile P h Tr τs Ts (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) →
∀ Ta ∈ Set.Icc Tr Tb,
boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Tr
≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Ta := by
intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs
unfold InverseOn at hTw hTs
unfold TimeProfile at hτw hτs
unfold Tr Tmin Tcap at *
obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2
exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1
m1 m2 hTw hTs hτw hτs c1 c2