-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
294 lines (249 loc) · 8.86 KB
/
train.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
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
"""Main train script."""
import argparse
import math
from copy import copy
from typing import Dict, Any
import yaml
import torch
import matplotlib.pyplot as plt
from matplotlib import ticker
from models import HighBetaMLP, GradShafranovMLP, InverseGradShafranovMLP
from physics import (
HighBetaEquilibrium,
GradShafranovEquilibrium,
InverseGradShafranovEquilibrium,
)
from utils import mae, get_flux_surfaces_from_wout
torch.set_default_dtype(torch.float64)
_TRUE_COLOR = "#af8dc3"
_PREDICTED_COLOR = "#7fbf7b"
def get_equilibrium_and_model(**equi_kws):
_equi_kws = copy(equi_kws)
target = _equi_kws.pop("_target_")
model_kws = {}
if target == "high-beta":
equi = HighBetaEquilibrium(**_equi_kws)
if not equi.normalized:
model_kws = {"a": equi.a, "psi_0": equi.psi_0}
model = HighBetaMLP(**model_kws)
return equi, model
if target == "grad-shafranov":
equi = GradShafranovEquilibrium(**_equi_kws)
if not equi.normalized:
model_kws = {
"R0": equi.Rb[0],
"a": equi.Rb[1],
"b": equi.Zb[1],
"psi_0": equi.psi_0,
}
model = GradShafranovMLP(**model_kws)
return equi, model
if target == "inverse-grad-shafranov":
equi = InverseGradShafranovEquilibrium(**_equi_kws)
if not equi.normalized:
model_kws = {"Rb": equi.Rb, "Zb": equi.Zb}
model = InverseGradShafranovMLP(**model_kws)
return equi, model
raise RuntimeError("Equilibrium " + target + " is not supported")
def train(
seed: int,
max_epochs: int,
nbatches: int,
log_every_n_steps: int,
update_axis_every_n_epochs: int,
learning_rate: float,
equilibrium: Dict[str, Any],
):
"""
Train model to solve the given equilibrium and plot solution.
Args:
seed (int): random seed.
max_epochs (int): number of epochs to train.
nbatches (int): number of batches.
log_every_n_steps (int): frequency for training log.
update_axis_every_n_epochs: frequency for updating axis guess.
Valid only for GradShafranov equilibria.
learning_rate (float): model learning rate.
equilibrium (dict): dict of keyword arguments for equilibrium and model
definition. Dict must have a `_target_` key to define the equilibrium.
"""
# Set seed
torch.manual_seed(seed)
###############
# Instantiate #
###############
# Get equilibrium and model
equi, model = get_equilibrium_and_model(seed=seed, **equilibrium)
model.train()
# shorthand
target = equilibrium["_target_"]
optimizer = torch.optim.LBFGS(
model.parameters(),
lr=learning_rate,
tolerance_grad=0,
tolerance_change=0,
max_iter=50,
line_search_fn="strong_wolfe",
)
#########
# Train #
#########
for epoch in range(max_epochs):
for batch_idx, (x_domain, x_boundary, x_axis) in zip(range(nbatches), equi):
x_domain.requires_grad_()
def closure():
optimizer.zero_grad()
loss = equi.closure(
x_domain,
model(x_domain),
x_boundary,
model(x_boundary),
x_axis,
model(x_axis) if x_axis is not None else None,
)
loss.backward()
return loss
optimizer.step(closure)
# Print the current loss:
# this is the loss at the given step and not the
# loss aggregated over all batches.
global_step = epoch * nbatches + batch_idx
if global_step % log_every_n_steps == log_every_n_steps - 1:
optimizer.zero_grad()
loss = equi.closure(
x_domain,
model(x_domain),
x_boundary,
model(x_boundary),
x_axis,
model(x_axis) if x_axis is not None else None,
return_dict=True,
)
string = f"[{epoch:5d}/{max_epochs:5d}][{batch_idx:3d}/{nbatches:3d}]"
for k, v in loss.items():
string += f", {k}={v.item():.2e}"
print(string)
# Update running axis guess
if target == "grad-shafranov":
if epoch % update_axis_every_n_epochs == update_axis_every_n_epochs - 1:
if equi.psi_0 > 0:
psi = "min"
else:
psi = "max"
axis_guess = model.find_x_of_psi(psi, x_axis)
if equi.normalized:
axis_guess *= equi.Rb[0]
equi.update_axis(axis_guess[0])
string = (
f"[{epoch:5d}/{max_epochs:5d}][{batch_idx:3d}/{nbatches:3d}]"
)
string += f", update axis guess to [{axis_guess[0][0]:.2f}, {axis_guess[0][1]:.2f}]"
print(string)
#############
# Visualize #
#############
# Get solution on test collocation points on a regular grid
x = equi.grid()
if target == "inverse-grad-shafranov":
# Do not include axis to avoid `nan` in eps() computation
x = x[equi.ntheta :]
x.requires_grad_()
psi_hat = model(x)
# Compute normalized residual error
pde_mae = equi.mae_pde_loss(x, psi_hat)
print(f"pde mae={pde_mae:.2e}")
# Compute the normalized averaged force
if target == "grad-shafranov" or target == "inverse-grad-shafranov":
eps = equi.eps(x, psi_hat)
print(f"eps={eps:.2e}")
# Scale model solution
if equi.normalized:
psi_hat *= equi.psi_0
# Get grid points
grid = equi.grid(normalized=False)
has_analytical_solution = target == "high-beta" or (
target == "grad-shafranov" and equi.is_solovev
)
# Compute mae between model solution and analytical solution
if has_analytical_solution:
psi = equi.psi(grid)
psi_mae = mae(psi_hat.detach(), psi)
print(f"psi mae={psi_mae:.2e}")
# Plot magnetic flux
fig, ax = plt.subplots(1, 1, tight_layout=True)
if target == "inverse-grad-shafranov":
if equi.wout_path is not None:
# Plot VMEC flux surfaces
rz, psi = get_flux_surfaces_from_wout(equi.wout_path)
equi.fluxsurfacesplot(
rz,
ax,
phi=torch.linspace(0, 1, psi.shape[0]),
interpolation="linear",
color=_TRUE_COLOR,
label="VMEC",
linewidth=5,
)
RlZ_hat = model(grid)
equi.fluxsurfacesplot(
RlZ_hat[:, [0, 2]],
ax,
linestyle="dashed",
linewidth=5,
interpolation="linear",
color=_PREDICTED_COLOR,
label="NN",
)
ax.legend()
else:
equi.fluxplot(grid, psi_hat, ax, linestyles="dashed")
if has_analytical_solution:
# Plot analytical solution
equi.fluxplot(grid, psi, ax, linestyles="solid")
if target == "grad-shafranov" and equi.wout_path is not None:
# Plot VMEC flux surfaces
rz, psi = get_flux_surfaces_from_wout(equi.wout_path)
equi.fluxsurfacesplot(rz, ax, psi=psi, ns=psi.shape[0])
# Plot scatter plot
if target == "high-beta":
fig, ax = plt.subplots(1, 1, tight_layout=True)
_, _, _, im = ax.hist2d(psi_hat.tolist(), psi.tolist(), bins=50, cmin=1)
ax.plot([psi.min(), psi.max()], [psi.min(), psi.max()], "r--", linewidth=2)
fig.colorbar(im, ax=ax)
ax.set_xlabel(r"$\hat{\Psi}$")
ax.set_ylabel(r"$\Psi$")
# Plot eps over entire domain
if target == "grad-shafranov":
fig, ax = plt.subplots(1, 1, tight_layout=True)
equi.fluxplot(
x,
equi.eps(x, psi_hat, reduction=None),
ax,
filled=True,
locator=ticker.LogLocator(),
)
if target == "inverse-grad-shafranov":
fig, ax = plt.subplots(1, 1, tight_layout=True)
equi.fluxsurfacesplot(
psi_hat[:, [0, 2]],
ax,
scalar=equi.eps(x, psi_hat, reduction=None),
phi=x[:: equi.ntheta, 0] ** 2,
contourf_kwargs={"locator": ticker.LogLocator()},
color="k",
)
# Show figures
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
nargs="?",
type=str,
default="configs/solovev.yaml",
help="Configuration file to use",
)
args = parser.parse_args()
with open(args.config, "r") as f:
config = yaml.safe_load(f)
train(**config)