-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvxpy_utils.py
executable file
·211 lines (192 loc) · 7.09 KB
/
cvxpy_utils.py
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
import cvxpy
import utils
import numpy as np
from objectives.f_b_and_k_operators_jax import K_theta
from objectives.operator_helper import A_b_c_to_block_operator
def cvxpy_create_X(n_dof,):
# Define and solve the CVXPY problem.
# Create a symmetric matrix variable.
# One additional row and col each are added
# they have blank elements except for
# the diagonal element.
# X contains:
# Phi , Phi y,
# Phi y, y,
X = cvxpy.Variable((n_dof+1,n_dof+1), symmetric=True)
# The operator >> denotes matrix inequality.
constraints = [X >> 0]
# This constraint sets the last diagonal item of
# x\otimes x to 1. When a rank-1 solution is feasible,
# the SDP will produce a rank-1 solution. This allows
# us to exactly control y despite having no way
# to constrain the Phi_i y terms.
constraints += [
cvxpy.trace(utils.sdp_helper_get_elem(-1, -1, n_dof+1) @ X) == 1
]
return(X, constraints)
def cvxpy_no_windowpane(cp, current_scale, X):
(A_K_theta, b_K_theta, c_K_theta) = K_theta(
cp.net_poloidal_current_amperes,
cp.quadpoints_phi,
cp.quadpoints_theta,
cp.nfp, cp.m, cp.n,
cp.stellsym,
)
A_K_theta = A_K_theta.reshape((-1, A_K_theta.shape[-2], A_K_theta.shape[-1]))
b_K_theta = b_K_theta.reshape((-1, b_K_theta.shape[-1]))
c_K_theta = c_K_theta.flatten()
K_theta_operator, K_theta_scale = A_b_c_to_block_operator(
A=A_K_theta, b=b_K_theta, c=c_K_theta,
current_scale=current_scale,
normalize=True
)
constraints = []
if cp.stellsym:
loop_size = K_theta_operator.shape[0]//2
else:
loop_size = K_theta_operator.shape[0]
# This if statement distinguishes the sign of
# tot. pol. current. The total current should never
# change sign.
print('Testing net current sign')
if cp.net_poloidal_current_amperes > 0:
for i in range(loop_size):
constraints.append(
cvxpy.trace(
K_theta_operator[i, :, :] @ X
)>=0
)
else:
print('Net current is negative')
for i in range(loop_size):
constraints.append(
cvxpy.trace(
K_theta_operator[i, :, :] @ X
)<=0
)
return(constraints, K_theta_operator, K_theta_scale)
def cvxpy_create_integrated_L1_from_array(cpst, grid_3d_operator, X, stellsym):
'''
Constructing cvxpy constraints and variables necessary for an
L1 norm term.
-- Inputs:
grid_3d_operator: Array, has shape (n_grid, 3, ndof+1, ndof+1)
X: cvxpy Variable
stellsym: Whether the grid the operator lives on has stellarator
symmetry.
-- Outputs:
constraints: A list of cvxpy constraints.
L1_comps_to_sum: Adding a lam*cvxpy.sum(L1_comps_to_sum) term in the
objective adds an L1 norm term.
'''
normN_prime = np.linalg.norm(cpst.winding_surface.normal(), axis=-1)
normN_prime = normN_prime.flatten()
if stellsym:
loop_size = grid_3d_operator.shape[0]//2
jacobian_prime = normN_prime[:normN_prime.shape[0]//cpst.winding_surface.nfp//2]
else:
loop_size = grid_3d_operator.shape[0]
jacobian_prime = normN_prime[:normN_prime.shape[0]//cpst.winding_surface.nfp]
# q is used for L1 norm.
L1_comps_to_sum = cvxpy.Variable(loop_size*3, nonneg=True)
constraints = []
for i in range(loop_size):
for j in range(3):
# K dot nabla K L1
if np.all(grid_3d_operator[i, j, :, :]==0):
continue
constraints.append(
cvxpy.trace(
jacobian_prime[i] * grid_3d_operator[i, j, :, :] @ X
)<=L1_comps_to_sum[3*i+j]
)
constraints.append(
cvxpy.trace(
jacobian_prime[i] * grid_3d_operator[i, j, :, :] @ X
)>=-L1_comps_to_sum[3*i+j]
)
# The L1 norm is given by cvxpy.sum(L1_comps_to_sum)
return(constraints, L1_comps_to_sum)
def cvxpy_create_Linf_from_array(grid_3d_operator, X, stellsym):
'''
Constructing cvxpy constraints and variables necessary for an
L-inf norm term.
-- Inputs:
grid_3d_operator: Array, has shape (n_grid, 3, ndof+1, ndof+1)
X: cvxpy Variable
stellsym: Whether the grid the operator lives on has stellarator
symmetry.
-- Outputs:
constraints: A list of cvxpy constraints.
Linf: Adding a lam*Linf term in the
objective adds an L1 norm term.
'''
if stellsym:
loop_size = grid_3d_operator.shape[0]//2
else:
loop_size = grid_3d_operator.shape[0]
Linf = cvxpy.Variable(nonneg=True)
constraints = []
for i in range(loop_size):
for j in range(3):
# K dot nabla K L1
if np.all(grid_3d_operator[i, j, :, :]==0):
continue
constraints.append(
cvxpy.trace(
grid_3d_operator[i, j, :, :] @ X
)<=Linf
)
constraints.append(
cvxpy.trace(
grid_3d_operator[i, j, :, :] @ X
)>=-Linf
)
return(constraints, Linf)
def cvxpy_create_Linf_leq_from_array(grid_3d_operator, grid_3d_operator_scale, grid_1d_operator, grid_1d_operator_scale, k_param, X, stellsym):
'''
Constructing cvxpy constraints and variables necessary for the following constraint:
-kg(x) <= ||f(x)||_\infty <= kg(x)
-- Inputs:
grid_3d_operator: Array, has shape (n_grid, 3, ndof+1, ndof+1)
X: cvxpy Variable
stellsym: Whether the grid the operator lives on has stellarator
symmetry.
-- Outputs:
constraints: A list of cvxpy constraints.
Linf: Adding a lam*Linf term in the
objective adds an L1 norm term.
'''
if stellsym:
loop_size = grid_3d_operator.shape[0]//2
else:
loop_size = grid_3d_operator.shape[0]
#k_param = cvxpy.Variable()
k_param_eff = k_param * grid_1d_operator_scale / grid_3d_operator_scale
constraints = []
for i in range(loop_size):
for j in range(3):
# K dot nabla K L1
if np.all(grid_3d_operator[i, j, :, :]==0):
continue
# constraints.append(
# cvxpy.trace(grid_1d_operator[i, :, :] @ X)
# >=0
# )
constraints.append(
cvxpy.trace(
(
grid_3d_operator[i, j, :, :]
- k_param_eff * grid_1d_operator[i, :, :]
) @ X
)<=0
)
constraints.append(
cvxpy.trace(
(
-grid_3d_operator[i, j, :, :]
- k_param_eff * grid_1d_operator[i, :, :]
) @ X
)<=0
)
return(constraints)