Skip to content

Commit a809bd4

Browse files
authored
feat: truss design case study
2 parents 2a630ba + 66b8665 commit a809bd4

16 files changed

+513
-64
lines changed

CvxLean/Command/Equivalence.lean

+11
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ def evalEquivalenceAux (probIdStx eqvIdStx : TSyntax `ident) (xs : Array (Syntax
111111
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.map_domain
112112
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.add_constraint
113113
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun
114+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_1
115+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_2
116+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_3
117+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_4
118+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_5
119+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_6
120+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_7
121+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_8
122+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_9
123+
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_objFun_10
114124
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_constraints
115125
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_constraint_1
116126
simpThms ← simpThms.addDeclToUnfold ``Minimization.Equivalence.rewrite_constraint_2
@@ -144,6 +154,7 @@ def evalEquivalenceAux (probIdStx eqvIdStx : TSyntax `ident) (xs : Array (Syntax
144154
let eqvNonPropArgs ← eqvArgs.filterM fun arg => do
145155
return !(← inferType (← inferType arg)).isProp
146156
let psi ← mkLambdaFVars eqvNonPropArgs res.expr
157+
trace[Meta.debug] "psi: {psi}"
147158

148159
try
149160
let psiF ← realToFloat psi

CvxLean/Examples/FittingSphere.lean

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ def fittingSphere :=
8080
subject to
8181
h₁ : 0 < r
8282

83-
instance : ChangeOfVariables fun (ct : (Fin n → ℝ) × ℝ) => (ct.1, sqrt (ct.2 + ‖ct.1‖ ^ 2)) :=
83+
instance : ChangeOfVariables fun ((c, t) : (Fin n → ℝ) × ℝ) => (c, sqrt (t + ‖c‖ ^ 2)) :=
8484
{ inv := fun (c, r) => (c, r ^ 2 - ‖c‖ ^ 2),
85-
condition := fun (_, t) => 0t,
86-
property := fun ⟨c, t⟩ h => by simp [sqrt_sq h] }
85+
condition := fun (_, r) => 0r,
86+
property := fun ⟨c, r⟩ h => by simp [sqrt_sq h] }
8787

8888
equivalence' eqv/fittingSphereT (n m : ℕ) (x : Fin m → Fin n → ℝ) : fittingSphere n m x := by
8989
-- Change of variables.

CvxLean/Examples/HypersonicShapeDesign.lean

+74-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CvxLean
2+
import CvxLean.Command.Util.TimeCmd
23

34
noncomputable section
45

@@ -16,50 +17,109 @@ def hypersonicShapeDesign :=
1617
optimization (Δx : ℝ)
1718
minimize sqrt ((1 / Δx ^ 2) - 1)
1819
subject to
19-
h1 : 10e-6 ≤ Δx
20-
h2 : Δx ≤ 1
21-
h3 : a * (1 / Δx) - (1 - b) * sqrt (1 - Δx ^ 2) ≤ 0
20+
h₁ : 10e-6 ≤ Δx
21+
h₂ : Δx ≤ 1
22+
h₃ : a * (1 / Δx) - (1 - b) * sqrt (1 - Δx ^ 2) ≤ 0
2223

23-
set_option trace.Meta.debug true
24-
25-
#check Lean.Expr
26-
27-
equivalence eqv/hypersonicShapeDesignConvex (a b : ℝ) (ha : 0 ≤ a) (hb : b < 1) :
24+
equivalence' eqv₁/hypersonicShapeDesignConvex (a b : ℝ) (ha : 0 ≤ a) (hb₁ : 0 ≤ b) (hb₂ : b < 1) :
2825
hypersonicShapeDesign a b := by
2926
pre_dcp
3027

28+
#print hypersonicShapeDesignConvex
29+
3130
@[optimization_param]
3231
def aₚ : ℝ := 0.05
3332

33+
@[simp high]
3434
lemma aₚ_nonneg : 0 ≤ aₚ := by
3535
unfold aₚ; norm_num
3636

3737
@[optimization_param]
3838
def bₚ : ℝ := 0.65
3939

40+
lemma bₚ_nonneg : 0 ≤ bₚ := by
41+
unfold bₚ; norm_num
42+
4043
lemma bₚ_lt_one : bₚ < 1 := by
4144
unfold bₚ; norm_num
4245

4346
@[simp high]
44-
lemma one_sub_bₚ_nonpos : 01 - bₚ := by
47+
lemma one_sub_bₚ_nonneg : 01 - bₚ := by
4548
unfold bₚ; norm_num
4649

47-
solve hypersonicShapeDesignConvex aₚ bₚ aₚ_nonneg bₚ_lt_one
50+
time_cmd solve hypersonicShapeDesignConvex aₚ bₚ aₚ_nonneg bₚ_nonneg bₚ_lt_one
51+
52+
#print hypersonicShapeDesignConvex.reduced
4853

4954
-- Final width of wedge.
50-
def width := hypersonicShapeDesignConvex.solution
55+
def width := eqv₁.backward_map aₚ.float bₚ.float hypersonicShapeDesignConvex.solution
5156

52-
#eval width
57+
#eval width -- 0.989524
58+
59+
#eval aₚ.float * (1 / width) - (1 - bₚ.float) * Float.sqrt (1 - width ^ 2) ≤ 0
60+
#eval aₚ.float * (1 / width) - (1 - bₚ.float) * Float.sqrt (1 - width ^ 2) ≤ 0.000001
5361

5462
-- Final height of wedge.
5563
def height := Float.sqrt (1 - width ^ 2)
5664

57-
#eval height
65+
#eval height -- 0.144368
5866

5967
-- Final L/D ratio.
6068
def ldRatio := 1 / (Float.sqrt ((1 / width ^ 2) - 1))
6169

62-
#eval ldRatio
70+
#eval ldRatio -- 6.854156
71+
72+
-- While the above is good enough, we simplify the problem further by performing a change of
73+
-- variables and simplifying appropriately.
74+
75+
equivalence' eqv₂/hypersonicShapeDesignSimpler (a b : ℝ) (ha : 0 ≤ a) (hb₁ : 0 ≤ b)
76+
(hb₂ : b < 1) : hypersonicShapeDesignConvex a b ha hb₁ hb₂ := by
77+
change_of_variables (z) (Δx ↦ sqrt z)
78+
conv_constr h₁ =>
79+
rewrite [le_sqrt' (by norm_num)]; norm_num
80+
conv_constr h₂ =>
81+
rewrite [sqrt_le_iff]; norm_num
82+
rw_constr h₃ =>
83+
have hz : 0 ≤ z := by arith
84+
have h_one_sub_z : 01 - z := by arith
85+
rewrite [rpow_two (sqrt a), sq_sqrt ha, rpow_two (sqrt z), sq_sqrt hz]
86+
rewrite [div_le_iff (by arith)]
87+
have hlhs : 0 ≤ a / sqrt z := div_nonneg ha (sqrt_nonneg _)
88+
have hrhs : 0 ≤ sqrt (1 - z) * (1 - b) := mul_nonneg (sqrt_nonneg _) (by arith)
89+
rewrite [← pow_two_le_pow_two hlhs hrhs]
90+
rewrite [div_rpow ha (sqrt_nonneg _), rpow_two (sqrt z), sq_sqrt hz]
91+
rewrite [mul_rpow (sqrt_nonneg _) (by arith), rpow_two (sqrt (1 - z)), sq_sqrt h_one_sub_z]
92+
rewrite [← mul_one_div, ← inv_eq_one_div, mul_comm (1 - z) _]
93+
rfl
94+
rename_constrs [h₁, h₂, h₃]
95+
rw_obj =>
96+
rewrite [rpow_neg (sqrt_nonneg _), rpow_two (sqrt z), sq_sqrt (by arith)]
97+
rfl
98+
99+
#print hypersonicShapeDesignSimpler
100+
101+
time_cmd solve hypersonicShapeDesignSimpler aₚ bₚ aₚ_nonneg bₚ_nonneg bₚ_lt_one
102+
103+
#print hypersonicShapeDesignSimpler.reduced
104+
105+
-- Final width of wedge.
106+
def width' :=
107+
eqv₁.backward_map aₚ.float bₚ.float <|
108+
eqv₂.backward_map aₚ.float bₚ.float hypersonicShapeDesignSimpler.solution
109+
110+
#eval width' -- 0.989524
111+
112+
#eval aₚ.float * (1 / width') - (1 - bₚ.float) * Float.sqrt (1 - width' ^ 2) ≤ 0
113+
114+
-- Final height of wedge.
115+
def height' := Float.sqrt (1 - width' ^ 2)
116+
117+
#eval height' -- 0.144371
118+
119+
-- Final L/D ratio.
120+
def ldRatio' := 1 / (Float.sqrt ((1 / width' ^ 2) - 1))
121+
122+
#eval ldRatio' -- 6.854031
63123

64124
end HypersonicShapeDesign
65125

0 commit comments

Comments
 (0)