import YM.AnalyticNext.CONNECTED_ACTIVITY_JETS_E6.Annulus
import YM.AnalyticNext.CONNECTED_ACTIVITY_JETS_E6.Shutter
import YM.Next.SCHEDULE.GridCost
/-!
# Γ charts and masks
Insert on intact data; guards on real a. Larger patches allow optional
Mayer letters when gamma off. Uses both ends/symmetry neighborhoods.
Meas/costs incl log J (not assuming ≤1 globally). Signs.
-/
set_option autoImplicit false
noncomputable section
namespace YM.ConnectedActivityE6.Coat
open Filter Function Lattice RG Pressure Pressure.Stores Pressure.Tube Tree Wilson Finset MeasureTheory Matrix
open Push Push.AbsoluteSums Precision Exterior Shell Channel Next.Fluctuations SectorMeasure Oriented Work.E03
open Bloch Laurent Work.F03 Progress.StationaryScenes Bid SliceRows Control Window Census Green Mask
open scoped Topology InnerProductSpace Matrix.Norms.Frobenius ContDiff BigOperators Classical
variable {G : Gauge} {c f n : Side} {P : Teller G} {S : StepData c f} {T : Tube G S} {F : Forest S.ratio}
variable (X : Coat T F)
/-- For Hot sites only. Hard Y not here. -/
def cone (a : ℝ) (u : Directions G f) (i : Bond f) := X.χ.innerShare (a*X.t) ‖u i‖
def plaza (O : Support c) := univ.filter fun i : Bond f => X.Hot i ∧ ¬Disjoint O (ends S i)
def booth (X : Coat T F) (i : Site c) := X.plaza (Animals.neighbors X.B i)
def enforce (a : ℝ) (U : Directions G f) (Y : Finset (Bond f)) := ∏ i∈Y,X.cone a U i
def gentry (Ñ : ChartRatio G) (a : ℝ) (U : Directions G f) (i : Bond f) := X.cone (2*a) U i * Real.log (Ñ.J (U i))
lemma booked (a : ℝ) (U : Directions G f) (i : Bond f) : X.cone a U i ∈ Set.Icc 0 1 := X.χ.range _
lemma ages (i : Bond f) (o : Site c) (h : X.Hot i) (hp : distAt S i o ≤ X.B) : i∈ X.booth o :=
mem_filter.mpr ⟨mem_univ _,h, not_disjoint_iff.mpr ⟨_,(Animals.neighbors_mem ..).mpr hp, center_end i⟩⟩
lemma conserve (Y : Finset (Bond f)) (a : ℝ) (U : Directions G f) : X.enforce a U Y ∈ Set.Icc 0 1 :=
⟨Finset.prod_nonneg fun i _ => (booked X a U i).1, prod_le_one (fun i _ => (booked X a U i).1)
(fun i _ => (booked X a U i).2)⟩
lemma premiere {a : ℝ} (hp : 0 X.serves ha U i (h i hi))
lemma protection (O : Support c) {a : ℝ} (ha : 0 X.amount ha U i fun he => h <| prod_eq_zero hi he
lemma protection_pack {a : ℝ} (ha : 0
X.protection _ ha U h i (mem_filter.mpr ⟨mem_univ _,hg, Finset.not_disjoint_iff.mpr ⟨_,hi,center_end _⟩⟩)
/-- Broad gamma can accompany the full chart substitution. -/
lemma buys_empty (X : Coat T F) {a γ W : ℝ} (hF : ShellAccess S G X.x W X.width γ)
(hm : γ*X.width ≤ a) {Ñ : ChartRatio G} (_h : LocalRatio Ñ) (hy : a*X.t < Ñ.radius)
(U : Config G f) (hp : Seeded X.seeds U X.k X.V X.x W) (hf : Alive S U X.k)
(hs : SliceSet S F U) :
∀ i : Bond f, X.Hot i → ∃ v ∈ Metric.ball (0:G.Color) Ñ.radius, ‖v‖ ≤ a*X.t ∧
U i=G.lieExp v*replica S.ratio X.V i := by
intro i he
let z := U i*(replica S.ratio X.V i)⁻¹
have hd : Interior S X.x W (atEdge S i) (X.B/4) U X.k X.V := Interior.of_actual U X.V X.k (atEdge S i) hp hf
fun h => active_far X.seeds i he (Corona.halo_mono (by omega) (Finset.Subset.refl _) h)
have hc : G.chord z ≤ X.width*X.t := hF.exacts F U X.V i X.k hs hd
have hv := hF.log
have hz : G.chord z ≤ 2*(X.width*X.t) := hc.trans (by linarith [hv.down])
let v := G.plog (G.mat z)
have hh : ‖v‖ ≤ a*X.t := calc
_ ≤ γ*G.chord z := hv.cap _ hz
_ ≤ γ*X.width*X.t := by rw [mul_assoc]; gcongr; exact hv.price.le
_ ≤ _ := mul_le_mul_of_nonneg_right hm (temp_nonneg _)
exact ⟨v,Metric.mem_ball.mpr (by rw [dist_zero_right]; exact hh.trans_lt hy),hh,by rw [show G.lieExp v=_ from hv.right z hz]; unfold z; group⟩
/-- at all (even chart-outside) arguments. -/
theorem germ_charge {Ñ : ChartRatio G} (h : LocalRatio Ñ) (L : Side) {a : ℝ} (ha : 0 ?_⟩
rw [gentry,abs_mul]
by_cases hk : X.cone (2*a) U i=0
· rw [hk,abs_zero,zero_mul]; positivity
have ht : ‖U i‖ ≤ 4*a*X.t := (X.amount (show 0 < 2*a*X.t by positivity) U i hk).trans_eq (by ring)
have hb := H (U i) (show ‖U i-0‖ ≤ r by rw [sub_zero]; exact ht.trans hg)
simp only [sub_zero,f,h.anchored,Real.log_one] at hb
rw [abs_of_nonneg (X.booked (2*a) U i).1]
apply (mul_le_of_le_one_left (abs_nonneg _) (X.booked (2*a) U i).2).trans
exact (hb.trans (mul_le_mul_of_nonneg_left ht hC.le)).trans_eq (by ring)
end YM.ConnectedActivityE6.Coat