File size: 43,844 Bytes
785c727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
import Mathlib
import OAI.Geometry.CubicTorus.Reflection.BVScaledFlux

namespace OAI

section

section

noncomputable section
open MeasureTheory Set Filter
open scoped ENNReal Topology RealInnerProductSpace
namespace CubicTorus

lemma trapped_characteristic {U : Set Torus} {c : Torus} {n x : Cover}
    {r ε : ℝ} (hr : 0 < r) (hrsmall : r < 1 / 2) (_hε : 0 ≤ ε)
    (htrap : normalCap c n r (ε * r) ⊆ (closure U)ᶜ ∧
      normalCap c (-n) r (ε * r) ⊆ U)
    (hx : ‖x‖ < 1) (hstrip : ε < |⟪n, x⟫|) :
    characteristic U (quotientMap (r • x) + c) = halfspaceIndicator n x := by
  have hnorm : ‖r • x‖ < r := by
    rw [norm_smul, Real.norm_of_nonneg hr.le]
    nlinarith
  have hsl : shortestLift ((quotientMap (r • x) + c) - c) = r • x := by
    rw [add_sub_cancel_right]
    exact shortestLift_quotientMap (hnorm.trans hrsmall)
  have hd : dist (quotientMap (r • x) + c) c < r := by
    rw [dist_eq_norm, ← norm_shortestLift, hsl]
    exact hnorm
  by_cases hneg : ⟪n, x⟫ < 0
  · have hm : quotientMap (r • x) + c ∈ U := by
      apply htrap.2
      refine ⟨hd, ?_⟩
      rw [hsl, inner_neg_left, inner_smul_right]
      rw [abs_of_neg hneg] at hstrip
      nlinarith [mul_lt_mul_of_pos_right hstrip hr]
    simp [characteristic, halfspaceIndicator, hm, hneg]
  · have hm : quotientMap (r • x) + c ∉ U := by
      have hh : quotientMap (r • x) + c ∈ (closure U)ᶜ := by
        apply htrap.1
        refine ⟨hd, ?_⟩
        rw [hsl, inner_smul_right]
        rw [abs_of_nonneg (le_of_not_gt hneg)] at hstrip
        nlinarith [mul_lt_mul_of_pos_right hstrip hr]
      exact fun h ↦ hh (subset_closure h)
    simp [characteristic, halfspaceIndicator, hm, hneg]

theorem normalMeasure_trapped_projection {E : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ)
    {a n : Cover} (hn : ‖n‖ = 1) {r ε : ℝ}
    (hr : 0 < r) (hrsmall : r < 1 / 2) (hε : 0 ≤ ε)
    (hc : Metric.closedBall a r ⊆ {x | ∀ i, 0 < x i ∧ x i < 1})
    (htrap : normalCap (quotientMap a) n r (ε * r) ⊆ (closure E)ᶜ ∧
      normalCap (quotientMap a) (-n) r (ε * r) ⊆ E)
    {ψ : Cover → ℝ} (hψ : ContDiff ℝ (⊤ : ℕ∞) ψ)
    (hs : tsupport ψ ⊆ Metric.ball (0 : Cover) 1)
    (hstrip : ∀ x, |⟪n, x⟫| ≤ ε → fderiv ℝ ψ x n = 0) :
    (∫ z, ψ (r⁻¹ • ((z.1 : Cover) - a)) * ⟪(z.2 : Cover), n⟫
      ∂(μ : Measure NormalBundle)) / r ^ 2 =
      ∫ y : Plane, ψ (normalPlaneEmbedding n y) := by
  have hF : ContDiff ℝ (⊤ : ℕ∞) (fun x ↦ ψ x • n) := hψ.smul_const n
  have hsF : tsupport (fun x ↦ ψ x • n) ⊆ Metric.ball (0 : Cover) 1 :=
    (tsupport_smul_subset_left (f := ψ) (g := fun _ ↦ n)).trans hs
  have hf := normalMeasure_scaled_flux hE hμ hF
    (hsF.trans Metric.ball_subset_closedBall) a hr hc
  simp only [inner_smul_right] at hf
  rw [hf, ← integral_plane_coordinates,
    ← integral_halfspace_flux hn hψ (hs.trans Metric.ball_subset_closedBall)]
  apply integral_congr_ae
  filter_upwards [] with x
  by_cases hx : x ∈ tsupport (fun x ↦ ψ x • n)
  · by_cases hm : |⟪n, x⟫| ≤ ε
    · rw [divergence_smul_const (hψ.differentiable (by simp)).differentiableAt, hstrip x hm]
      simp
    · have he := trapped_characteristic hr hrsmall hε htrap
        (show ‖x‖ < 1 by simpa using hsF hx) (lt_of_not_ge hm)
      rw [quotientMap_add]
      exact congrArg (fun t : ℝ ↦ t * divergence (fun x ↦ ψ x • n) x) he
  · rw [divergence_of_notMem_tsupport hx]
    simp

lemma unit_normal_sq_distance {ν n : Cover} (hν : ‖ν‖ = 1) (hn : ‖n‖ = 1) :
    ‖ν - n‖ ^ 2 = 2 * (1 - ⟪ν, n⟫) := by
  rw [norm_sub_sq_real, hν, hn]
  ring

theorem normalMeasure_trapped_excess {E : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ)
    {a n : Cover} (hn : ‖n‖ = 1) {r ε : ℝ}
    (hr : 0 < r) (hrsmall : r < 1 / 2) (hε : 0 ≤ ε)
    (hc : Metric.closedBall a r ⊆ {x | ∀ i, 0 < x i ∧ x i < 1})
    (htrap : normalCap (quotientMap a) n r (ε * r) ⊆ (closure E)ᶜ ∧
      normalCap (quotientMap a) (-n) r (ε * r) ⊆ E)
    {ψ : Cover → ℝ} (hψ : ContDiff ℝ (⊤ : ℕ∞) ψ)
    (hs : tsupport ψ ⊆ Metric.ball (0 : Cover) 1)
    (hstrip : ∀ x, |⟪n, x⟫| ≤ ε → fderiv ℝ ψ x n = 0) :
    (∫ z, ψ (r⁻¹ • ((z.1 : Cover) - a)) ∂(μ : Measure NormalBundle)) / r ^ 2 -
      (∫ y : Plane, ψ (normalPlaneEmbedding n y)) =
      (∫ z, ψ (r⁻¹ • ((z.1 : Cover) - a)) * ‖(z.2 : Cover) - n‖ ^ 2
        ∂(μ : Measure NormalBundle)) / (2 * r ^ 2) := by
  have hp := normalMeasure_trapped_projection hE hμ hn hr hrsmall hε hc htrap hψ hs hstrip
  let f : NormalBundle → ℝ := fun z ↦ ψ (r⁻¹ • ((z.1 : Cover) - a))
  have hf : Continuous f := hψ.continuous.comp
    ((continuous_subtype_val.comp continuous_fst).sub continuous_const |>.const_smul r⁻¹)
  have hfi : Integrable f (μ : Measure NormalBundle) :=
    hf.integrable_of_hasCompactSupport (HasCompactSupport.of_compactSpace _)
  have hν : Continuous (fun z : NormalBundle ↦ ⟪(z.2 : Cover), n⟫) :=
    (continuous_subtype_val.comp continuous_snd).inner continuous_const
  have hgi : Integrable (fun z : NormalBundle ↦ f z * ⟪(z.2 : Cover), n⟫)
      (μ : Measure NormalBundle) :=
    (hf.mul hν).integrable_of_hasCompactSupport (HasCompactSupport.of_compactSpace _)
  have he (z : NormalBundle) :
      f z * ‖(z.2 : Cover) - n‖ ^ 2 = 2 * (f z - f z * ⟪(z.2 : Cover), n⟫) := by
    rw [unit_normal_sq_distance (by simpa using z.2.property) hn]
    ring
  change _ - _ = (∫ z, f z * ‖(z.2 : Cover) - n‖ ^ 2 ∂(μ : Measure NormalBundle)) / _
  simp_rw [he]
  rw [integral_const_mul, integral_sub hfi hgi, ← hp]
  dsimp [f]
  ring

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter
open scoped ENNReal Topology RealInnerProductSpace
namespace CubicTorus

lemma gradient_of_notMem_tsupport {ψ : Cover → ℝ} {x : Cover}
    (hx : x ∉ tsupport ψ) : gradient ψ x = 0 := by
  simp [gradient, fderiv_of_notMem_tsupport ℝ hx]

lemma gradient_tsupport_subset (ψ : Cover → ℝ) :
    tsupport (gradient ψ) ⊆ tsupport ψ := by
  apply closure_minimal _ isClosed_closure
  intro x hx
  by_contra hh
  exact hx (gradient_of_notMem_tsupport hh)

theorem normalMeasure_local_laplacian {E : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ)
    {ψ : Cover → ℝ} (hψ : ContDiff ℝ (⊤ : ℕ∞) ψ)
    (hc : HasCompactSupport ψ)
    (hs : tsupport ψ ⊆ {x | ∀ i, 0 < x i ∧ x i < 1}) :
    (∫ x : Cover, characteristic E (quotientMap x) * divergence (gradient ψ) x) =
      ∫ z, fderiv ℝ ψ z.1 (z.2 : Cover) ∂(μ : Measure NormalBundle) := by
  have hf := normalMeasure_local_flux hE hμ (gradient_contDiff hψ)
    (hc.of_isClosed_subset isClosed_closure (gradient_tsupport_subset ψ))
    ((gradient_tsupport_subset ψ).trans hs)
  simpa [inner_gradient_right] using hf.symm

theorem weakCMC_scalar_residual_identity {E : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) {H : ℝ}
    (hH : ∀ D : WallVariation, normalFirstVariation μ D =
      H * directionalFlux E D.coordinate D.function)
    {n : Cover} (hn : ‖n‖ = 1)
    {ψ : Cover → ℝ} (hψ : ContDiff ℝ (⊤ : ℕ∞) ψ)
    (hc : HasCompactSupport ψ)
    (hs : tsupport ψ ⊆ {x | ∀ i, 0 < x i ∧ x i < 1}) :
    (∫ x : Cover, characteristic E (quotientMap x) * divergence (gradient ψ) x) -
      (∫ z, fderiv ℝ ψ z.1 n ∂(μ : Measure NormalBundle)) +
      H * (∫ z, ψ z.1 * ⟪(z.2 : Cover), n⟫ ∂(μ : Measure NormalBundle)) =
      (1 / 2 : ℝ) * ∫ z, ‖(z.2 : Cover) - n‖ ^ 2 * fderiv ℝ ψ z.1 (z.2 : Cover)
        ∂(μ : Measure NormalBundle) := by
  have hx : Continuous (fun z : NormalBundle ↦ (z.1 : Cover)) :=
    continuous_subtype_val.comp continuous_fst
  have hν : Continuous (fun z : NormalBundle ↦ (z.2 : Cover)) :=
    continuous_subtype_val.comp continuous_snd
  have hd := (hψ.continuous_fderiv (by simp)).comp hx
  have hB : Integrable (fun z : NormalBundle ↦ fderiv ℝ ψ z.1 (z.2 : Cover)) (μ : Measure NormalBundle) :=
    (hd.clm_apply hν).integrable_of_hasCompactSupport (HasCompactSupport.of_compactSpace _)
  have hD : Integrable (fun z : NormalBundle ↦ fderiv ℝ ψ z.1 n) (μ : Measure NormalBundle) :=
    (hd.clm_apply continuous_const).integrable_of_hasCompactSupport (HasCompactSupport.of_compactSpace _)
  have hA : Integrable (fun z : NormalBundle ↦ ⟪(z.2 : Cover), n⟫ * fderiv ℝ ψ z.1 (z.2 : Cover))
      (μ : Measure NormalBundle) :=
    ((hν.inner continuous_const).mul (hd.clm_apply hν)).integrable_of_hasCompactSupport
      (HasCompactSupport.of_compactSpace _)
  have hF := hψ.smul_const n
  have hFs := tsupport_smul_subset_left (f := ψ) (g := fun _ ↦ n)
  have hvar := weakCMC_local_identity hμ hH hF
    (hc.of_isClosed_subset isClosed_closure hFs) (hFs.trans hs)
  have he (z : NormalBundle) : tangentialDivergence (fun x ↦ ψ x • n) z.1 z.2 =
      fderiv ℝ ψ z.1 n - ⟪(z.2 : Cover), n⟫ * fderiv ℝ ψ z.1 (z.2 : Cover) := by
    rw [tangentialDivergence_smul_const ((hψ.differentiable (by simp)) _)]
    simp only [tangentPart, map_sub, map_smul, smul_eq_mul]
  simp_rw [he, inner_smul_right] at hvar
  rw [integral_sub hD hA] at hvar
  have herr (z : NormalBundle) :
      ‖(z.2 : Cover) - n‖ ^ 2 * fderiv ℝ ψ z.1 (z.2 : Cover) =
      2 * (fderiv ℝ ψ z.1 (z.2 : Cover) -
        ⟪(z.2 : Cover), n⟫ * fderiv ℝ ψ z.1 (z.2 : Cover)) := by
    rw [unit_normal_sq_distance z.2.property hn]
    ring
  simp_rw [herr]
  rw [integral_const_mul, integral_sub hB hA, normalMeasure_local_laplacian hE hμ hψ hc hs]
  linarith

theorem weakCMC_scalar_residual_bound {E : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) {H : ℝ}
    (hH : ∀ D : WallVariation, normalFirstVariation μ D =
      H * directionalFlux E D.coordinate D.function)
    {n : Cover} (hn : ‖n‖ = 1)
    {ψ : Cover → ℝ} (hψ : ContDiff ℝ (⊤ : ℕ∞) ψ)
    (hc : HasCompactSupport ψ)
    (hs : tsupport ψ ⊆ {x | ∀ i, 0 < x i ∧ x i < 1})
    {w : NormalBundle → ℝ} (hw : Integrable w (μ : Measure NormalBundle))
    (hb : ∀ᵐ z ∂(μ : Measure NormalBundle), ‖fderiv ℝ ψ z.1‖ * ‖(z.2 : Cover) - n‖ ^ 2 ≤ w z) :
    |(∫ x : Cover, characteristic E (quotientMap x) * divergence (gradient ψ) x) -
      (∫ z, fderiv ℝ ψ z.1 n ∂(μ : Measure NormalBundle)) +
      H * (∫ z, ψ z.1 * ⟪(z.2 : Cover), n⟫ ∂(μ : Measure NormalBundle))| ≤
      (1 / 2 : ℝ) * ∫ z, w z ∂(μ : Measure NormalBundle) := by
  rw [weakCMC_scalar_residual_identity hE hμ hH hn hψ hc hs, abs_mul]
  norm_num only [abs_of_nonneg (by norm_num : (0 : ℝ) ≤ 1 / 2)]
  apply mul_le_mul_of_nonneg_left _ (by norm_num)
  rw [← Real.norm_eq_abs]
  apply norm_integral_le_of_norm_le hw
  filter_upwards [hb] with z hz
  rw [Real.norm_eq_abs, abs_mul, abs_of_nonneg (sq_nonneg _)]
  have hd : |fderiv ℝ ψ z.1 (z.2 : Cover)| ≤ ‖fderiv ℝ ψ z.1‖ := by
    simpa only [Real.norm_eq_abs, z.2.property, mul_one] using
      (fderiv ℝ ψ z.1).le_opNorm (z.2 : Cover)
  exact (mul_le_mul_of_nonneg_left hd (sq_nonneg _)).trans (by simpa only [mul_comm] using hz)

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter
open scoped ENNReal Topology RealInnerProductSpace
namespace CubicTorus

lemma normalMeasure_frontier_ae {E : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) :
    ∀ᵐ z ∂(μ : Measure NormalBundle), quotientMap z.1 ∈ frontier E := by
  have hh : ∀ᵐ c ∂(spatialNormalMeasure μ : Measure Torus), c ∈ frontier E := by
    filter_upwards [Measure.support_mem_ae (μ := (spatialNormalMeasure μ : Measure Torus))] with c hc
    have hc' := spatialNormalMeasure_support_subset_boundary hE hμ hc
    rw [frontier_eq_closure_inter_closure]
    exact ⟨Measure.support_restrict_subset.trans inter_subset_left hc'.1,
      Measure.support_restrict_subset.trans inter_subset_left hc'.2⟩
  change ∀ᵐ c ∂Measure.map normalProjection (μ : Measure NormalBundle), c ∈ frontier E at hh
  exact (ae_map_iff normalProjection_continuous.measurable.aemeasurable
    (show MeasurableSet {c : Torus | c ∈ frontier E} from isClosed_frontier.measurableSet)).mp hh

lemma trapped_frontier_height {E : Set Torus} (hE : IsOpen E)
    {a n x : Cover} {r ε : ℝ} (hrsmall : r < 1 / 2)
    (htrap : normalCap (quotientMap a) n r (ε * r) ⊆ (closure E)ᶜ ∧
      normalCap (quotientMap a) (-n) r (ε * r) ⊆ E)
    (hx : x ∈ Metric.ball a r) (hb : quotientMap x ∈ frontier E) :
    |⟪n, x - a⟫| ≤ ε * r := by
  have hl : shortestLift (quotientMap x - quotientMap a) = x - a := by
    rw [← quotientMap_sub]
    exact shortestLift_quotientMap ((show ‖x - a‖ < r by simpa only [Metric.mem_ball, dist_eq_norm] using hx).trans hrsmall)
  have hd : dist (quotientMap x) (quotientMap a) < r := by
    rw [dist_eq_norm, ← norm_shortestLift, hl]
    simpa only [Metric.mem_ball, dist_eq_norm] using hx
  apply abs_le.mpr
  constructor
  · by_contra! hh
    have hmem := htrap.2 (show quotientMap x ∈ normalCap (quotientMap a) (-n) r (ε * r) from
      ⟨hd, by rw [hl, inner_neg_left]; linarith⟩)
    rw [frontier, hE.interior_eq] at hb
    exact hb.2 hmem
  · by_contra! hh
    have hnot := htrap.1 (show quotientMap x ∈ normalCap (quotientMap a) n r (ε * r) from
      ⟨hd, by simpa only [hl] using hh⟩)
    exact hnot (frontier_subset_closure hb)

theorem normalMeasure_trapped_test_derivative_zero {E : Set Torus} (hE : IsOpen E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ)
    {a n : Cover} {r ε : ℝ} (hrsmall : r < 1 / 2)
    (htrap : normalCap (quotientMap a) n r (ε * r) ⊆ (closure E)ᶜ ∧
      normalCap (quotientMap a) (-n) r (ε * r) ⊆ E)
    {ψ : Cover → ℝ} (hs : tsupport ψ ⊆ Metric.ball a r)
    (hstrip : ∀ x, |⟪n, x - a⟫| ≤ ε * r → fderiv ℝ ψ x n = 0) :
    ∀ᵐ z ∂(μ : Measure NormalBundle), fderiv ℝ ψ z.1 n = 0 := by
  filter_upwards [normalMeasure_frontier_ae hE.measurableSet hμ] with z hz
  by_cases hsupp : (z.1 : Cover) ∈ tsupport ψ
  · exact hstrip _ (trapped_frontier_height hE hrsmall htrap (hs hsupp) hz)
  · rw [fderiv_of_notMem_tsupport ℝ hsupp]
    rfl

theorem weakCMC_trapped_scalar_identity {E : Set Torus} (hE : IsOpen E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) {H : ℝ}
    (hH : ∀ D : WallVariation, normalFirstVariation μ D =
      H * directionalFlux E D.coordinate D.function)
    {a n : Cover} (hn : ‖n‖ = 1) {r ε : ℝ} (hrsmall : r < 1 / 2)
    (htrap : normalCap (quotientMap a) n r (ε * r) ⊆ (closure E)ᶜ ∧
      normalCap (quotientMap a) (-n) r (ε * r) ⊆ E)
    {ψ : Cover → ℝ} (hψ : ContDiff ℝ (⊤ : ℕ∞) ψ)
    (hs : tsupport ψ ⊆ Metric.ball a r)
    (hcell : Metric.closedBall a r ⊆ {x | ∀ i, 0 < x i ∧ x i < 1})
    (hstrip : ∀ x, |⟪n, x - a⟫| ≤ ε * r → fderiv ℝ ψ x n = 0) :
    (∫ x : Cover, characteristic E (quotientMap x) * divergence (gradient ψ) x) +
      H * (∫ z, ψ z.1 * ⟪(z.2 : Cover), n⟫ ∂(μ : Measure NormalBundle)) =
      (1 / 2 : ℝ) * ∫ z, ‖(z.2 : Cover) - n‖ ^ 2 * fderiv ℝ ψ z.1 (z.2 : Cover)
        ∂(μ : Measure NormalBundle) := by
  have hc : HasCompactSupport ψ := (isCompact_closedBall a r).of_isClosed_subset isClosed_closure
    (hs.trans Metric.ball_subset_closedBall)
  have hi := weakCMC_scalar_residual_identity hE.measurableSet hμ hH hn hψ hc
    (hs.trans (Metric.ball_subset_closedBall.trans hcell))
  have hz : (∫ z, fderiv ℝ ψ z.1 n ∂(μ : Measure NormalBundle)) = 0 :=
    integral_eq_zero_of_ae (normalMeasure_trapped_test_derivative_zero hE hμ hrsmall htrap hs hstrip)
  simpa only [hz, sub_zero] using hi

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter Metric
open scoped ENNReal Topology RealInnerProductSpace
namespace CubicTorus

lemma good_scales_boundary_cone {E B : Set Torus} (hE : MeasurableSet E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) {N : Torus → Cover} (hN : Measurable N)
    (hpolar : ∀ᵐ z ∂(μ : Measure NormalBundle), N (quotientMap z.1) = (z.2 : Cover))
    (hn : ∀ᵐ x ∂(spatialNormalMeasure μ : Measure Torus), ‖N x‖ = 1)
    {D δ : ℝ} (hδ : 0 < δ)
    (hd : ∀ a ∈ B, ∀ t : ℝ, 0 < t → t ≤ D →
      δ * t ^ 3 ≤ torusVolume.real (E ∩ Metric.ball a t) ∧
      δ * t ^ 3 ≤ torusVolume.real (Eᶜ ∩ Metric.ball a t))
    {c : Torus} (hc : c ∈ B) {n : Cover} (hnorm : ‖n‖ = 1)
    {R ε : ℝ} (hRsmall : R ≤ 1 / 32) (hRD : 2 * R ≤ D) (hε : 0 < ε) (hε1 : ε ≤ 1)
    (hgood : ∀ s : ℝ, 0 < s → s ≤ 16 * R →
      131072 * normalOscillation (spatialNormalMeasure μ) N c n s < δ ^ 2 * (ε / 8) ^ 6) :
    ∀ y ∈ B, dist y c ≤ R → |⟪n, shortestLift (y - c)⟫| ≤ ε * dist y c := by
  intro y hy hyr
  by_cases hyc : y = c
  · subst y
    have hz : shortestLift (0 : Torus) = 0 := shortestLift_eq_of_lift rfl (by norm_num)
    simp only [sub_self, hz, inner_zero_right, abs_zero, dist_self, mul_zero, le_refl]
  have hdpos : 0 < dist y c := dist_pos.mpr hyc
  let r := 2 * dist y c
  let t := ε * dist y c / 4
  have hr : 0 < r := by dsimp [r]; positivity
  have hrsmall : r ≤ 1 / 16 := by dsimp [r]; linarith
  have ht : 0 < t := by dsimp [t]; positivity
  have htr : t ≤ r := by dsimp [t, r]; nlinarith
  have htD : t ≤ D := htr.trans (by dsimp [r]; linarith)
  have har : dist c c < r := by simpa only [dist_self] using hr
  have hbr : dist y c < r := by dsimp [r]; linarith
  have hquot : t / r = ε / 8 := by dsimp [t, r]; field_simp; norm_num
  have hos := hgood (8 * r) (by positivity) (by dsimp [r]; linarith)
  have hforbid : ∀ a ∈ B, dist a c < r → ∀ b ∈ B, dist b c < r →
      ¬ 2 * t ≤ ⟪n, shortestLift (b - a)⟫ := by
    intro a ha har b hb hbr hh
    have hp := ordered_density_normal_oscillation hE hμ hN hpolar hn hnorm hr hrsmall ht htr hδ
      har hbr (hd a ha t ht htD).2 (hd b hb t ht htD).1 hh
    rw [hquot] at hp
    exact (not_lt_of_ge hp) hos
  have hupper := lt_of_not_ge (hforbid c hc har y hy hbr)
  have hlower := lt_of_not_ge (hforbid y hy hbr c hc har)
  have he : shortestLift (c - y) = -shortestLift (y - c) := by
    apply shortestLift_eq_of_lift
    · rw [quotientMap_neg, quotientMap_shortestLift]; abel
    · rw [norm_neg, norm_shortestLift, ← dist_eq_norm]; linarith
  rw [he, inner_neg_right] at hlower
  have habs : |⟪n, shortestLift (y - c)⟫| < 2 * t := abs_lt.mpr ⟨by linarith, hupper⟩
  exact habs.le.trans (by dsimp [t]; nlinarith)

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter Metric
open scoped ENNReal NNReal Topology RealInnerProductSpace
namespace CubicTorus

def normalPlaneProjection (n v : Cover) : Cover := v - ⟪n, v⟫ • n

lemma normalPlaneProjection_sub (n v w : Cover) :
    normalPlaneProjection n (v - w) = normalPlaneProjection n v - normalPlaneProjection n w := by
  simp only [normalPlaneProjection, inner_sub_right, sub_smul]
  abel

lemma normalPlaneProjection_inner {n : Cover} (hn : ‖n‖ = 1) (v : Cover) :
    ⟪n, normalPlaneProjection n v⟫ = 0 := by
  simp only [normalPlaneProjection, inner_sub_right, inner_smul_right, real_inner_self_eq_norm_sq, hn]
  ring

lemma norm_le_two_normalPlaneProjection {n v : Cover} (hn : ‖n‖ = 1)
    {ε : ℝ} (hε : ε ≤ 1 / 2) (hc : |⟪n, v⟫| ≤ ε * ‖v‖) :
    ‖v‖ ≤ 2 * ‖normalPlaneProjection n v‖ := by
  have hh := norm_add_le (normalPlaneProjection n v) (⟪n, v⟫ • n)
  rw [show normalPlaneProjection n v + ⟪n, v⟫ • n = v by simp [normalPlaneProjection],
    norm_smul, hn, mul_one, Real.norm_eq_abs] at hh
  nlinarith [norm_nonneg v]

lemma exists_lipschitz_height_of_cone (S : Set Cover) {n : Cover} (hn : ‖n‖ = 1)
    {ε : ℝ} (hε0 : 0 ≤ ε) (hε : ε ≤ 1 / 2)
    (hc : ∀ x ∈ S, ∀ y ∈ S, |⟪n, x - y⟫| ≤ ε * ‖x - y‖) :
    ∃ g : Cover → ℝ, LipschitzWith ⟨2 * ε, by positivity⟩ g ∧
      ∀ x ∈ S, g (normalPlaneProjection n x) = ⟪n, x⟫ := by
  classical
  by_cases hnon : S.Nonempty
  swap
  · refine ⟨fun _ ↦ 0, ?_, ?_⟩
    · apply LipschitzWith.of_dist_le_mul
      intro x y
      simp only [dist_self]
      change (0 : ℝ) ≤ 2 * ε * dist x y
      positivity
    simp only [not_nonempty_iff_eq_empty.mp hnon, mem_empty_iff_false, false_implies, implies_true]
  let : Nonempty S := hnon.to_subtype
  let F : S → Cover := fun x ↦ normalPlaneProjection n x.1
  have hF : Function.Injective F := by
    intro x y hxy
    apply Subtype.ext
    apply sub_eq_zero.mp
    apply norm_eq_zero.mp
    apply le_antisymm _ (norm_nonneg _)
    have hh := norm_le_two_normalPlaneProjection hn hε (hc x x.property y y.property)
    rw [normalPlaneProjection_sub, show normalPlaneProjection n x.1 = normalPlaneProjection n y.1 from hxy,
      sub_self, norm_zero, mul_zero] at hh
    exact hh
  let height : Cover → ℝ := fun z ↦ ⟪n, ((Function.invFun F z).1 : Cover)⟫
  have hheight : LipschitzOnWith ⟨2 * ε, by positivity⟩ height (range F) := by
    apply LipschitzOnWith.of_dist_le_mul
    rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩
    have hx := Function.leftInverse_invFun hF x
    have hy := Function.leftInverse_invFun hF y
    dsimp only [height]
    rw [hx, hy]
    change |⟪n, (x : Cover)⟫ - ⟪n, (y : Cover)⟫| ≤ (2 * ε) * ‖normalPlaneProjection n x - normalPlaneProjection n y‖
    rw [← inner_sub_right]
    have hnorm := norm_le_two_normalPlaneProjection hn hε (hc x x.property y y.property)
    have hm := mul_le_mul_of_nonneg_left hnorm hε0
    rw [normalPlaneProjection_sub] at hm
    exact (hc x x.property y y.property).trans (by nlinarith [hm])
  obtain ⟨g, hg, hgeq⟩ := hheight.extend_real
  refine ⟨g, hg, ?_⟩
  intro x hx
  have hh := hgeq (show F ⟨x, hx⟩ ∈ range F from mem_range_self _)
  simpa only [F, height, Function.leftInverse_invFun hF ⟨x, hx⟩] using hh.symm

lemma lifted_cone_of_torus_cone {S : Set Torus} {a : Torus} {n : Cover} {ε : ℝ}
    (hS : S ⊆ Metric.ball a (1 / 4))
    (hc : ∀ x ∈ S, ∀ y ∈ S, |⟪n, shortestLift (x - y)⟫| ≤ ε * dist x y) :
    ∀ x ∈ (fun z ↦ shortestLift (z - a)) '' S,
      ∀ y ∈ (fun z ↦ shortestLift (z - a)) '' S, |⟪n, x - y⟫| ≤ ε * ‖x - y‖ := by
  rintro _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩
  have hr : dist x a + dist y a < 1 / 2 := by
    have hxl : dist x a < 1 / 4 := hS hx
    have hyl : dist y a < 1 / 4 := hS hy
    linarith
  rw [← shortestLift_sub_local hr, norm_shortestLift, ← dist_eq_norm]
  exact hc x hx y hy

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter Metric
open scoped ENNReal NNReal Topology RealInnerProductSpace

namespace CubicTorus

lemma ediam_metricBall_le (a : Torus) {r : ℝ} (_hr : 0 ≤ r) :
    ediam (Metric.ball a r) ≤ ENNReal.ofReal (2 * r) := by
  apply Metric.ediam_le_of_forall_dist_le
  intro x hx y hy
  have hh := dist_triangle_right x y a
  exact hh.trans (by linarith [Metric.mem_ball.mp hx, Metric.mem_ball.mp hy])

lemma hausdorff_two_le_of_lower_density (σ : FiniteMeasure Torus) (S : Set Torus)
    {R c : ℝ} (hR : 0 < R) (hc : 0 < c)
    (hl : ∀ a ∈ S, ∀ r : ℝ, 0 < r → r ≤ R →
      c * r ^ 2 ≤ (σ : Measure Torus).real (Metric.ball a r)) :
    Measure.hausdorffMeasure 2 S ≤ ENNReal.ofReal (64 / c) * (σ : Measure Torus) univ := by
  let r : ℕ → ℝ := fun n ↦ R / ((n : ℝ) + 1)
  have hr (n) : 0 < r n := by dsimp [r]; positivity
  have hrR (n) : r n ≤ R := by
    dsimp [r]
    exact div_le_self hR.le (by linarith [Nat.cast_nonneg (α := ℝ) n])
  have hr0 : Tendsto r atTop (𝓝 0) := by
    have hh := (tendsto_const_nhds (x := R)).mul tendsto_one_div_add_atTop_nhds_zero_nat
    simpa only [mul_zero, mul_one_div] using hh
  have hex (n : ℕ) := Vitali.exists_disjoint_subfamily_covering_enlargement_ball S id
    (fun _ ↦ r n) (r n) (fun _ _ ↦ le_rfl) 4 (by norm_num)
  choose u hu hdis hcov using hex
  have hcount (n) : (u n).Countable :=
    (hdis n).countable_of_isOpen (fun _ _ ↦ Metric.isOpen_ball)
      (fun a _ ↦ ⟨a, Metric.mem_ball_self (hr n)⟩)
  let : ∀ n, Countable (u n) := fun n ↦ (hcount n).to_subtype
  let t (n : ℕ) (a : u n) : Set Torus := Metric.ball (a : Torus) (4 * r n)
  have hdiam (n : ℕ) (a : u n) : ediam (t n a) ≤ ENNReal.ofReal (8 * r n) := by
    simpa only [t, show (2 : ℝ) * (4 * r n) = 8 * r n by ring] using
      ediam_metricBall_le (a : Torus) (show 0 ≤ 4 * r n by positivity)
  have hcover (n) : S ⊆ ⋃ a : u n, t n a := by
    intro a ha
    obtain ⟨b, hb, hsub⟩ := hcov n a ha
    exact mem_iUnion.mpr ⟨⟨b, hb⟩, hsub (Metric.mem_ball_self (hr n))⟩
  have hstep (n : ℕ) (a : u n) : ediam (t n a) ^ (2 : ℝ) ≤
      ENNReal.ofReal (64 / c) * (σ : Measure Torus) (Metric.ball (a : Torus) (r n)) := by
    have hlow := hl a (hu n a.property) (r n) (hr n) (hrR n)
    have hb : ENNReal.ofReal (c * r n ^ 2) ≤
        (σ : Measure Torus) (Metric.ball (a : Torus) (r n)) :=
      (ENNReal.ofReal_le_iff_le_toReal (measure_ne_top _ _)).2 hlow
    calc
      _ = ediam (t n a) ^ (2 : ℕ) := ENNReal.rpow_two _
      _ ≤ (ENNReal.ofReal (8 * r n)) ^ (2 : ℕ) := pow_le_pow_left' (hdiam n a) _
      _ = ENNReal.ofReal (64 / c) * ENNReal.ofReal (c * r n ^ 2) := by
        rw [← ENNReal.ofReal_pow (by positivity), ← ENNReal.ofReal_mul (by positivity)]
        congr 1
        field_simp
        ring
      _ ≤ _ := mul_le_mul_right hb _
  have hsum (n : ℕ) : (∑' a : u n, ediam (t n a) ^ (2 : ℝ)) ≤
      ENNReal.ofReal (64 / c) * (σ : Measure Torus) univ := by
    calc
      _ ≤ ∑' a : u n, ENNReal.ofReal (64 / c) *
          (σ : Measure Torus) (Metric.ball (a : Torus) (r n)) := ENNReal.tsum_le_tsum (hstep n)
      _ = ENNReal.ofReal (64 / c) *
          (∑' a : u n, (σ : Measure Torus) (Metric.ball (a : Torus) (r n))) := by
        rw [ENNReal.tsum_mul_left]
      _ = ENNReal.ofReal (64 / c) *
          (σ : Measure Torus) (⋃ a ∈ u n, Metric.ball a (r n)) := by
        congr 1
        exact (measure_biUnion (hcount n) (hdis n)
          (fun _ _ ↦ Metric.isOpen_ball.measurableSet)).symm
      _ ≤ _ := mul_le_mul_right (measure_mono (subset_univ _)) _
  have hlim : Tendsto (fun n ↦ ENNReal.ofReal (8 * r n)) atTop (𝓝 0) := by
    simpa only [Function.comp_def, mul_zero, ENNReal.ofReal_zero] using
      ENNReal.continuous_ofReal.continuousAt.tendsto.comp (hr0.const_mul 8)
  have hh := Measure.hausdorffMeasure_le_liminf_tsum 2 S
    (fun n ↦ ENNReal.ofReal (8 * r n)) hlim t
    (Eventually.of_forall hdiam) (Eventually.of_forall hcover)
  exact hh.trans (liminf_le_of_frequently_le' (Eventually.of_forall hsum).frequently)

theorem isMinimizerAt_finite_boundary_area {V : ℝ} (hV : V ∈ Ioo 0 1)
    {E : Set Torus} (hE : MeasurableSet E) (hmin : IsMinimizerAt V E) :
    Measure.hausdorffMeasure 2 (frontier (openRepresentative E)) < ⊤ := by
  obtain ⟨μ, hmass, hflux⟩ := exists_normalMeasure hE hmin.1.2.ne
  have hμ : IsNormalMeasure E μ := ⟨hmass, hflux⟩
  obtain ⟨R, c, hR, hc, hl⟩ := isMinimizerAt_perimeter_lower_density hV hE hmin
  have hh := hausdorff_two_le_of_lower_density (spatialNormalMeasure μ)
    (volumeSupport E ∩ volumeSupport Eᶜ) hR hc (hl μ hμ)
  rw [isMinimizerAt_frontier_openRepresentative hV hE hmin]
  exact hh.trans_lt (by finiteness)

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter Metric
open scoped ENNReal NNReal Topology RealInnerProductSpace

namespace CubicTorus

lemma hausdorff_two_le_open_of_lower_density (σ : FiniteMeasure Torus) (S U : Set Torus)
    (hU : IsOpen U) (hSU : S ⊆ U) {R c : ℝ} (hR : 0 < R) (hc : 0 < c)
    (hl : ∀ a ∈ S, ∀ r : ℝ, 0 < r → r ≤ R →
      c * r ^ 2 ≤ (σ : Measure Torus).real (Metric.ball a r)) :
    Measure.hausdorffMeasure 2 S ≤ ENNReal.ofReal (64 / c) * (σ : Measure Torus) U := by
  classical
  let r : ℕ → ℝ := fun n ↦ R / ((n : ℝ) + 1)
  have hr (n) : 0 < r n := by dsimp [r]; positivity
  have hrR (n) : r n ≤ R := by
    dsimp [r]
    exact div_le_self hR.le (by linarith [Nat.cast_nonneg (α := ℝ) n])
  have hr0 : Tendsto r atTop (𝓝 0) := by
    have hh := (tendsto_const_nhds (x := R)).mul tendsto_one_div_add_atTop_nhds_zero_nat
    simpa only [mul_zero, mul_one_div] using hh
  have heps : ∀ a ∈ S, ∃ e : ℝ, 0 < e ∧ Metric.ball a e ⊆ U := by
    intro a ha
    exact Metric.isOpen_iff.mp hU a (hSU ha)
  choose e he heU using heps
  let rad (n : ℕ) (a : Torus) : ℝ := if ha : a ∈ S then min (r n) (e a ha) else r n
  have hrad (n : ℕ) (a : Torus) (ha : a ∈ S) : 0 < rad n a := by
    simp only [rad, dite_eq_left ha]
    exact lt_min (hr n) (he a ha)
  have hradle (n : ℕ) (a : Torus) : rad n a ≤ r n := by
    by_cases ha : a ∈ S
    · simp only [rad, dite_eq_left ha]; exact min_le_left _ _
    · simp only [rad, dite_eq_right ha]; exact le_rfl
  have hradU (n : ℕ) (a : Torus) (ha : a ∈ S) : Metric.ball a (rad n a) ⊆ U := by
    apply (Metric.ball_subset_ball ?_).trans (heU a ha)
    simp only [rad, dite_eq_left ha]
    exact min_le_right _ _
  have hex (n : ℕ) := Vitali.exists_disjoint_subfamily_covering_enlargement_ball S id
    (rad n) (r n) (fun a _ ↦ hradle n a) 4 (by norm_num)
  choose u hu hdis hcov using hex
  have hcount (n) : (u n).Countable :=
    (hdis n).countable_of_isOpen (fun _ _ ↦ Metric.isOpen_ball)
      (fun a ha ↦ ⟨a, Metric.mem_ball_self (hrad n a (hu n ha))⟩)
  let : ∀ n, Countable (u n) := fun n ↦ (hcount n).to_subtype
  let t (n : ℕ) (a : u n) : Set Torus := Metric.ball (a : Torus) (4 * rad n a)
  have hdiam' (n : ℕ) (a : u n) : ediam (t n a) ≤ ENNReal.ofReal (8 * rad n a) := by
    simpa only [t, show (2 : ℝ) * (4 * rad n a) = 8 * rad n a by ring] using
      ediam_metricBall_le (a : Torus) (show 0 ≤ 4 * rad n a from by
        have hh := hrad n a (hu n a.property); positivity)
  have hdiam (n : ℕ) (a : u n) : ediam (t n a) ≤ ENNReal.ofReal (8 * r n) :=
    (hdiam' n a).trans (ENNReal.ofReal_le_ofReal (by nlinarith [hradle n a]))
  have hcover (n) : S ⊆ ⋃ a : u n, t n a := by
    intro a ha
    obtain ⟨b, hb, hsub⟩ := hcov n a ha
    exact mem_iUnion.mpr ⟨⟨b, hb⟩, hsub (Metric.mem_ball_self (hrad n a ha))⟩
  have hstep (n : ℕ) (a : u n) : ediam (t n a) ^ (2 : ℝ) ≤
      ENNReal.ofReal (64 / c) * (σ : Measure Torus) (Metric.ball (a : Torus) (rad n a)) := by
    have hrpos := hrad n a (hu n a.property)
    have hlow := hl a (hu n a.property) (rad n a) hrpos ((hradle n a).trans (hrR n))
    have hb : ENNReal.ofReal (c * rad n a ^ 2) ≤
        (σ : Measure Torus) (Metric.ball (a : Torus) (rad n a)) :=
      (ENNReal.ofReal_le_iff_le_toReal (measure_ne_top _ _)).2 hlow
    calc
      _ = ediam (t n a) ^ (2 : ℕ) := ENNReal.rpow_two _
      _ ≤ (ENNReal.ofReal (8 * rad n a)) ^ (2 : ℕ) := pow_le_pow_left' (hdiam' n a) _
      _ = ENNReal.ofReal (64 / c) * ENNReal.ofReal (c * rad n a ^ 2) := by
        rw [← ENNReal.ofReal_pow (by positivity), ← ENNReal.ofReal_mul (by positivity)]
        congr 1
        field_simp
        ring
      _ ≤ _ := mul_le_mul_right hb _
  have hsum (n : ℕ) : (∑' a : u n, ediam (t n a) ^ (2 : ℝ)) ≤
      ENNReal.ofReal (64 / c) * (σ : Measure Torus) U := by
    calc
      _ ≤ ∑' a : u n, ENNReal.ofReal (64 / c) *
          (σ : Measure Torus) (Metric.ball (a : Torus) (rad n a)) := ENNReal.tsum_le_tsum (hstep n)
      _ = ENNReal.ofReal (64 / c) *
          (∑' a : u n, (σ : Measure Torus) (Metric.ball (a : Torus) (rad n a))) := by
        rw [ENNReal.tsum_mul_left]
      _ = ENNReal.ofReal (64 / c) *
          (σ : Measure Torus) (⋃ a ∈ u n, Metric.ball a (rad n a)) := by
        congr 1
        exact (measure_biUnion (hcount n) (hdis n)
          (fun _ _ ↦ Metric.isOpen_ball.measurableSet)).symm
      _ ≤ _ := mul_le_mul_right (measure_mono (by
        intro x hx
        obtain ⟨a, ha, hx⟩ := mem_iUnion₂.mp hx
        exact hradU n a (hu n ha) hx)) _
  have hlim : Tendsto (fun n ↦ ENNReal.ofReal (8 * r n)) atTop (𝓝 0) := by
    simpa only [Function.comp_def, mul_zero, ENNReal.ofReal_zero] using
      ENNReal.continuous_ofReal.continuousAt.tendsto.comp (hr0.const_mul 8)
  have hh := Measure.hausdorffMeasure_le_liminf_tsum 2 S
    (fun n ↦ ENNReal.ofReal (8 * r n)) hlim t
    (Eventually.of_forall hdiam) (Eventually.of_forall hcover)
  exact hh.trans (liminf_le_of_frequently_le' (Eventually.of_forall hsum).frequently)

lemma hausdorff_two_le_measure_of_lower_density (σ : FiniteMeasure Torus) (S : Set Torus)
    {R c : ℝ} (hR : 0 < R) (hc : 0 < c)
    (hl : ∀ a ∈ S, ∀ r : ℝ, 0 < r → r ≤ R →
      c * r ^ 2 ≤ (σ : Measure Torus).real (Metric.ball a r)) :
    Measure.hausdorffMeasure 2 S ≤ ENNReal.ofReal (64 / c) * (σ : Measure Torus) S := by
  have h0 : ENNReal.ofReal (64 / c) ≠ 0 := by positivity
  rw [S.measure_eq_iInf_isOpen (σ : Measure Torus),
    ENNReal.mul_iInf_of_ne h0 ENNReal.ofReal_ne_top]
  apply le_iInf
  intro U
  rw [ENNReal.mul_iInf_of_ne h0 ENNReal.ofReal_ne_top]
  apply le_iInf
  intro hSU
  rw [ENNReal.mul_iInf_of_ne h0 ENNReal.ofReal_ne_top]
  exact le_iInf fun hU ↦ hausdorff_two_le_open_of_lower_density σ S U hU hSU hR hc hl

lemma hausdorff_two_null_of_lower_density (σ : FiniteMeasure Torus) (B : Set Torus)
    {R c : ℝ} (hR : 0 < R) (hc : 0 < c)
    (hl : ∀ a ∈ B, ∀ r : ℝ, 0 < r → r ≤ R →
      c * r ^ 2 ≤ (σ : Measure Torus).real (Metric.ball a r))
    {S : Set Torus} (hSB : S ⊆ B) (hS : (σ : Measure Torus) S = 0) :
    Measure.hausdorffMeasure 2 S = 0 := by
  have hh := hausdorff_two_le_measure_of_lower_density σ S hR hc
    (fun a ha ↦ hl a (hSB ha))
  rw [hS, mul_zero] at hh
  exact le_zero_iff.mp hh

theorem isMinimizerAt_boundary_area_absolutelyContinuous {V : ℝ} (hV : V ∈ Ioo 0 1)
    {E : Set Torus} (hE : MeasurableSet E) (hmin : IsMinimizerAt V E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) :
    (Measure.hausdorffMeasure 2).restrict (frontier (openRepresentative E)) ≪
      (spatialNormalMeasure μ : Measure Torus) := by
  obtain ⟨R, c, hR, hc, hl⟩ := isMinimizerAt_perimeter_lower_density hV hE hmin
  intro S hS
  rw [Measure.restrict_apply₀' isClosed_frontier.measurableSet.nullMeasurableSet]
  apply hausdorff_two_null_of_lower_density (spatialNormalMeasure μ)
    (volumeSupport E ∩ volumeSupport Eᶜ) hR hc (hl μ hμ)
  · rw [← isMinimizerAt_frontier_openRepresentative hV hE hmin]
    exact inter_subset_right
  · exact measure_mono_null inter_subset_left hS

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter Metric
open scoped ENNReal Topology RealInnerProductSpace
namespace CubicTorus

lemma singleton_null_of_quadratic_upper_density (σ : FiniteMeasure Torus)
    {R A : ℝ} (hR : 0 < R)
    (hu : ∀ a : Torus, ∀ r : ℝ, 0 < r → r ≤ R →
      (σ : Measure Torus).real (Metric.ball a r) ≤ A * r ^ 2) (a : Torus) :
    (σ : Measure Torus) {a} = 0 := by
  have hh : ∀ᶠ r : ℝ in 𝓝[>] 0,
      (σ : Measure Torus).real {a} ≤ A * r ^ 2 := by
    filter_upwards [self_mem_nhdsWithin, mem_nhdsWithin_of_mem_nhds (Iio_mem_nhds hR)] with r hr hrR
    exact (measureReal_mono (singleton_subset_iff.mpr (Metric.mem_ball_self hr))).trans
      (hu a r hr hrR.le)
  have hl : Tendsto (fun r : ℝ ↦ A * r ^ 2) (𝓝[>] 0) (𝓝 0) := by
    simpa using (((tendsto_id : Tendsto (id : ℝ → ℝ) (𝓝 0) (𝓝 0)).pow 2).const_mul A).mono_left nhdsWithin_le_nhds
  have hzero := ge_of_tendsto hl hh
  exact (ENNReal.toReal_eq_zero_iff _).mp (le_antisymm hzero ENNReal.toReal_nonneg) |>.resolve_right
    (measure_ne_top _ _)

lemma measure_le_hausdorff_two_of_upper_density (σ : FiniteMeasure Torus)
    {R A : ℝ} (hR : 0 < R) (hA : 0 < A)
    (hu : ∀ a : Torus, ∀ r : ℝ, 0 < r → r ≤ R →
      (σ : Measure Torus).real (Metric.ball a r) ≤ A * r ^ 2) :
    (σ : Measure Torus) ≤ ENNReal.ofReal (4 * A) • Measure.hausdorffMeasure 2 := by
  have hk : ENNReal.ofReal (4 * A) ≠ 0 := by positivity
  have hsmall : (ENNReal.ofReal (4 * A))⁻¹ • (σ : Measure Torus) ≤ Measure.hausdorffMeasure 2 := by
    apply Measure.le_hausdorffMeasure 2 _ (ENNReal.ofReal (R / 2)) (by positivity)
    intro s hs
    rw [Measure.smul_apply, smul_eq_mul]
    apply (ENNReal.inv_mul_le_iff hk ENNReal.ofReal_ne_top).2
    by_cases he : s.Nonempty
    swap
    · simp [not_nonempty_iff_eq_empty.mp he]
    obtain ⟨a, ha⟩ := he
    have hfin : ediam s ≠ ⊤ := ne_top_of_le_ne_top ENNReal.ofReal_ne_top hs
    by_cases hz : ediam s = 0
    · have hsub : s ⊆ {a} := fun x hx ↦ (ediam_eq_zero_iff.mp hz hx ha)
      rw [measure_mono_null hsub (singleton_null_of_quadratic_upper_density σ hR hu a)]
      exact zero_le
    have hd : 0 < diam s := ENNReal.toReal_pos hz hfin
    have hdR : 2 * diam s ≤ R := by
      have hh := ENNReal.toReal_mono ENNReal.ofReal_ne_top hs
      rw [ENNReal.toReal_ofReal (by positivity : 0 ≤ R / 2)] at hh
      change diam s ≤ R / 2 at hh
      linarith
    have hsub : s ⊆ Metric.ball a (2 * diam s) := by
      intro x hx
      exact (Metric.dist_le_diam_of_mem' hfin hx ha).trans_lt (by linarith)
    have hb := (measureReal_mono hsub).trans (hu a (2 * diam s) (by positivity) hdR)
    have hh : (σ : Measure Torus) s ≤ ENNReal.ofReal (A * (2 * diam s) ^ 2) :=
      (ENNReal.le_ofReal_iff_toReal_le (measure_ne_top _ _) (by positivity)).2 hb
    calc
      _ ≤ ENNReal.ofReal (A * (2 * diam s) ^ 2) := hh
      _ = ENNReal.ofReal (4 * A) * ediam s ^ (2 : ℝ) := by
        rw [ENNReal.rpow_two, ← ENNReal.ofReal_toReal hfin]
        change ENNReal.ofReal (A * (2 * diam s) ^ 2) =
          ENNReal.ofReal (4 * A) * ENNReal.ofReal (diam s) ^ (2 : ℕ)
        rw [← ENNReal.ofReal_pow hd.le, ← ENNReal.ofReal_mul (by positivity)]
        congr 1
        ring
  intro s
  have hh := hsmall s
  rw [Measure.smul_apply, smul_eq_mul] at hh ⊢
  exact (ENNReal.inv_mul_le_iff hk ENNReal.ofReal_ne_top).1 hh

theorem isMinimizerAt_perimeter_absolutelyContinuous_area {V : ℝ} (hV : V ∈ Ioo 0 1)
    {E : Set Torus} (hE : MeasurableSet E) (hmin : IsMinimizerAt V E)
    {μ : FiniteMeasure NormalBundle} (hμ : IsNormalMeasure E μ) :
    (spatialNormalMeasure μ : Measure Torus) ≪
      (Measure.hausdorffMeasure 2).restrict (frontier (openRepresentative E)) := by
  obtain ⟨R, A, hR, hA, hu⟩ := isMinimizerAt_perimeter_upper_density hV hE hmin
  have hbound := measure_le_hausdorff_two_of_upper_density (spatialNormalMeasure μ) hR hA (hu μ hμ)
  have habs : (spatialNormalMeasure μ : Measure Torus) ≪ Measure.hausdorffMeasure 2 := by
    intro s hs
    have hh := hbound s
    simpa only [Measure.smul_apply, smul_eq_mul, hs, mul_zero, nonpos_iff_eq_zero] using hh
  have hsupport : ∀ᵐ x ∂(spatialNormalMeasure μ : Measure Torus),
      x ∈ frontier (openRepresentative E) := by
    filter_upwards [Measure.support_mem_ae (μ := (spatialNormalMeasure μ : Measure Torus))] with x hx
    rw [isMinimizerAt_frontier_openRepresentative hV hE hmin]
    exact spatialNormalMeasure_support_subset_boundary hE hμ hx
  intro s hs
  have hh : Measure.hausdorffMeasure 2 (s ∩ frontier (openRepresentative E)) = 0 := by
    simpa only [Measure.restrict_apply₀' isClosed_frontier.measurableSet.nullMeasurableSet] using hs
  have hnull := habs hh
  have hae : s =ᵐ[(spatialNormalMeasure μ : Measure Torus)] (s ∩ frontier (openRepresentative E) : Set Torus) := by
    filter_upwards [hsupport] with x hx
    exact propext ⟨fun hs ↦ ⟨hs, hx⟩, And.left⟩
  rw [measure_congr hae]
  exact hnull

end CubicTorus

end
end

section

noncomputable section
open MeasureTheory Set Filter Metric
open scoped ENNReal Topology RealInnerProductSpace
namespace CubicTorus

lemma measure_bad_scales_le (σ : FiniteMeasure Torus) (τ : Measure Torus)
    (B : Set Torus) {R A δ : ℝ} (hA : 0 < A) (hδ : 0 < δ)
    (hu : ∀ a : Torus, ∀ r : ℝ, 0 < r → r ≤ R →
      (σ : Measure Torus).real (Metric.ball a r) ≤ A * r ^ 2)
    (hbad : ∀ a ∈ B, ∃ r : ℝ, 0 < r ∧ 5 * r ≤ R ∧
      ENNReal.ofReal (δ * r ^ 2) ≤ τ (Metric.closedBall a r)) :
    (σ : Measure Torus) B ≤ ENNReal.ofReal (25 * A / δ) * τ univ := by
  classical
  choose r hr hrR he using hbad
  let rad (a : Torus) : ℝ := if ha : a ∈ B then r a ha else 0
  have hrad (a : Torus) (ha : a ∈ B) : 0 < rad a := by simpa only [rad, dite_eq_left ha] using hr a ha
  have hradR (a : Torus) (ha : a ∈ B) : 5 * rad a ≤ R := by
    simpa only [rad, dite_eq_left ha] using hrR a ha
  have henergy (a : Torus) (ha : a ∈ B) :
      ENNReal.ofReal (δ * rad a ^ 2) ≤ τ (Metric.closedBall a (rad a)) := by
    simpa only [rad, dite_eq_left ha] using he a ha
  obtain ⟨u, huB, hdis, hcov⟩ := Vitali.exists_disjoint_subfamily_covering_enlargement_closedBall
    B id rad (R / 5) (fun a ha ↦ by linarith [hradR a ha]) 4 (by norm_num)
  have hdisopen : u.PairwiseDisjoint (fun a ↦ Metric.ball a (rad a)) :=
    hdis.mono (fun _ ↦ Metric.ball_subset_closedBall)
  have hcount : u.Countable := hdisopen.countable_of_isOpen (fun _ _ ↦ Metric.isOpen_ball)
    (fun a ha ↦ ⟨a, Metric.mem_ball_self (hrad a (huB ha))⟩)
  let : Countable u := hcount.to_subtype
  have hcover : B ⊆ ⋃ a : u, Metric.closedBall (a : Torus) (4 * rad a) := by
    intro a ha
    obtain ⟨b, hb, hsub⟩ := hcov a ha
    exact mem_iUnion.mpr ⟨⟨b, hb⟩, hsub (Metric.mem_closedBall_self (hrad a ha).le)⟩
  have hstep (a : u) : (σ : Measure Torus) (Metric.closedBall (a : Torus) (4 * rad a)) ≤
      ENNReal.ofReal (25 * A / δ) * τ (Metric.closedBall (a : Torus) (rad a)) := by
    have hrpos := hrad a (huB a.property)
    have hh := (measureReal_mono (Metric.closedBall_subset_ball (by linarith : 4 * rad a < 5 * rad a))).trans
      (hu a (5 * rad a) (by positivity) (hradR a (huB a.property)))
    calc
      _ ≤ ENNReal.ofReal (A * (5 * rad a) ^ 2) :=
        (ENNReal.le_ofReal_iff_toReal_le (measure_ne_top _ _) (by positivity)).2 hh
      _ = ENNReal.ofReal (25 * A / δ) * ENNReal.ofReal (δ * rad a ^ 2) := by
        rw [← ENNReal.ofReal_mul (by positivity)]
        congr 1
        field_simp
        ring
      _ ≤ _ := mul_le_mul_right (henergy a (huB a.property)) _
  calc
    _ ≤ (σ : Measure Torus) (⋃ a : u, Metric.closedBall (a : Torus) (4 * rad a)) := measure_mono hcover
    _ ≤ ∑' a : u, (σ : Measure Torus) (Metric.closedBall (a : Torus) (4 * rad a)) := measure_iUnion_le _
    _ ≤ ∑' a : u, ENNReal.ofReal (25 * A / δ) * τ (Metric.closedBall (a : Torus) (rad a)) :=
      ENNReal.tsum_le_tsum hstep
    _ = ENNReal.ofReal (25 * A / δ) *
        (∑' a : u, τ (Metric.closedBall (a : Torus) (rad a))) := by rw [ENNReal.tsum_mul_left]
    _ = ENNReal.ofReal (25 * A / δ) * τ (⋃ a ∈ u, Metric.closedBall a (rad a)) := by
      congr 1
      exact (measure_biUnion hcount hdis (fun _ _ ↦ Metric.isClosed_closedBall.measurableSet)).symm
    _ ≤ _ := mul_le_mul_right (measure_mono (subset_univ _)) _

end CubicTorus

end
end

end

end OAI