-
Notifications
You must be signed in to change notification settings - Fork 2
/
ode_demo_petsc.py
295 lines (247 loc) · 8.96 KB
/
ode_demo_petsc.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
295
import os
import argparse
import time
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import sys
# uncomment the following to make the run deterministic
# torch.manual_seed(0)
# np.random.seed(0)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
#
# Exmaple usage:
# python3 ode_demo_petsc.py --double_prec -ts_adapt_type none -ts_type rk -ts_rk_type 4 -ts_trajectory_type memory -ts_trajectory_solution_only 0
#
# Note:
# - PETSc4py must be installed. It can be installed with PETSc using the configuration option --with-petsc4py
# - Must add -ts_adapt_type none to disable adaptive time integration for this example
# - Add --double_prec if PETSc is configured with double precision. It is not needed if PETSc is configured with --with-precision=single
# - The time integration methods can be switched at runtime
# Explicit RK: -ts_type rk -ts_rk_type <1fe,2a,3,3bs,4,5f,5dp,5bs>
# Backward Euler: -ts_type beuler
# Crank-Nicolson: -ts_type cn
# - By default, disk is used for checkpointing. To use DRAM, add -ts_trajectory_type memory. -ts_trajectory_solution_only 0 can be used to further reduce recomputation at the cost of more memory usage
parser = argparse.ArgumentParser("ODE demo")
parser.add_argument(
"--method", type=str, choices=["dopri5", "adams", "dopri5_fixed"], default="dopri5"
)
parser.add_argument("--data_size", type=int, default=1001)
parser.add_argument("--batch_time", type=int, default=10)
parser.add_argument("--batch_size", type=int, default=20)
parser.add_argument("--niters", type=int, default=2000)
parser.add_argument("--test_freq", type=int, default=20)
parser.add_argument("--viz", action="store_true")
parser.add_argument("--gpu", type=int, default=1)
parser.add_argument("--step_size", type=float, default=0.025)
parser.add_argument("--implicit_form", action="store_true")
parser.add_argument("--double_prec", action="store_true")
parser.add_argument("--use_dlpack", action="store_true")
args, unknown = parser.parse_known_args()
gpu = args.gpu
niters = args.niters
test_freq = args.test_freq
data_size = args.data_size
batch_time = args.batch_time
batch_size = args.batch_size
step_size = args.step_size
implicit_form = args.implicit_form
double_prec = args.double_prec
use_dlpack = args.use_dlpack
petsc4py_path = os.path.join(os.environ["PETSC_DIR"], os.environ["PETSC_ARCH"], "lib")
sys.path.append(petsc4py_path)
import petsc4py
sys.argv = [sys.argv[0]] + unknown
petsc4py.init(sys.argv)
from petsc4py import PETSc
# OptDB = PETSc.Options()
# print("first init: ",OptDB.getAll())
sys.path.append("../") # for quick debugging
from pnode import petsc_adjoint
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# device = torch.device('cpu')
if double_prec:
print("Using float64")
true_y0 = torch.tensor([[2.0, 0.0]], dtype=torch.float64).to(device)
t = torch.linspace(0.0, 25.0, data_size, dtype=torch.float64)
true_A = torch.tensor([[-0.1, 2.0], [-2.0, -0.1]], dtype=torch.float64).to(device)
else:
print("Using float32 (PyTorch default)")
true_y0 = torch.tensor([[2.0, 0.0]]).to(device)
t = torch.linspace(0.0, 25.0, data_size)
true_A = torch.tensor([[-0.1, 2.0], [-2.0, -0.1]]).to(device)
class Lambda(nn.Module):
def forward(self, t, y):
return torch.mm(y**3, true_A)
# data_size-1 should not exceed the number of time steps
if step_size > 25.0 / (data_size - 1):
print(
"Error: step_size={} too large (number of steps should not be smaller than data_size={} too large".format(
step_size, data_size
)
)
# sys.exit()
ode0 = petsc_adjoint.ODEPetsc()
ode0.setupTS(
true_y0,
Lambda(),
step_size=step_size,
enable_adjoint=False,
implicit_form=implicit_form,
use_dlpack=use_dlpack,
)
with torch.no_grad():
true_y = ode0.odeint(true_y0, t)
# print(true_y)
# sys.exit()
def get_batch():
s = torch.from_numpy(
np.random.choice(
np.arange(data_size - batch_time, dtype=np.int64), batch_size, replace=False
)
)
batch_y0 = true_y[s] # (M, D)
batch_t = t[:batch_time] # (T)
batch_y = torch.stack(
[true_y[s + i] for i in range(batch_time)], dim=0
) # (T, M, D)
return batch_y0, batch_t, batch_y
def makedirs(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
if args.viz:
makedirs("png")
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 4), facecolor="white")
ax_traj = fig.add_subplot(131, frameon=False)
ax_phase = fig.add_subplot(132, frameon=False)
ax_vecfield = fig.add_subplot(133, frameon=False)
plt.show(block=False)
def visualize(true_y, pred_y, odefunc, itr):
if args.viz:
ax_traj.cla()
ax_traj.set_title("Trajectories")
ax_traj.set_xlabel("t")
ax_traj.set_ylabel("x,y")
ax_traj.plot(
t.numpy(), true_y.numpy()[:, 0, 0], t.numpy(), true_y.numpy()[:, 0, 1], "g-"
)
ax_traj.plot(
t.numpy(),
pred_y.numpy()[:, 0, 0],
"--",
t.numpy(),
pred_y.numpy()[:, 0, 1],
"b--",
)
ax_traj.set_xlim(t.min(), t.max())
ax_traj.set_ylim(-2, 2)
ax_traj.legend()
ax_phase.cla()
ax_phase.set_title("Phase Portrait")
ax_phase.set_xlabel("x")
ax_phase.set_ylabel("y")
ax_phase.plot(true_y.numpy()[:, 0, 0], true_y.numpy()[:, 0, 1], "g-")
ax_phase.plot(pred_y.numpy()[:, 0, 0], pred_y.numpy()[:, 0, 1], "b--")
ax_phase.set_xlim(-2, 2)
ax_phase.set_ylim(-2, 2)
ax_vecfield.cla()
ax_vecfield.set_title("Learned Vector Field")
ax_vecfield.set_xlabel("x")
ax_vecfield.set_ylabel("y")
y, x = np.mgrid[-2:2:21j, -2:2:21j]
dydt = (
odefunc(0, torch.Tensor(np.stack([x, y], -1).reshape(21 * 21, 2)))
.cpu()
.detach()
.numpy()
)
mag = np.sqrt(dydt[:, 0] ** 2 + dydt[:, 1] ** 2).reshape(-1, 1)
dydt = dydt / mag
dydt = dydt.reshape(21, 21, 2)
ax_vecfield.streamplot(x, y, dydt[:, :, 0], dydt[:, :, 1], color="black")
ax_vecfield.set_xlim(-2, 2)
ax_vecfield.set_ylim(-2, 2)
fig.tight_layout()
plt.savefig("png/{:03d}".format(itr))
plt.draw()
plt.pause(0.001)
class ODEFunc(nn.Module):
def __init__(self):
super(ODEFunc, self).__init__()
if double_prec:
self.net = nn.Sequential(
nn.Linear(2, 50).double(),
nn.Tanh().double(),
nn.Linear(50, 2).double(),
).to(device)
else:
self.net = nn.Sequential(
nn.Linear(2, 50),
nn.Tanh(),
nn.Linear(50, 2),
).to(device)
for m in self.net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=0.1)
nn.init.constant_(m.bias, val=0)
def forward(self, t, y):
return self.net(y**3)
class RunningAverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, momentum=0.99):
self.momentum = momentum
self.reset()
def reset(self):
self.val = None
self.avg = 0
def update(self, val):
if self.val is None:
self.avg = val
else:
self.avg = self.avg * self.momentum + val * (1 - self.momentum)
self.val = val
if __name__ == "__main__":
ii = 0
batch_y0, _, _ = get_batch()
func = ODEFunc()
optimizer = optim.RMSprop(func.parameters(), lr=1e-3)
end = time.time()
time_meter = RunningAverageMeter(0.97)
loss_meter = RunningAverageMeter(0.97)
ode = petsc_adjoint.ODEPetsc()
ode.setupTS(
batch_y0,
func,
step_size=step_size,
implicit_form=implicit_form,
use_dlpack=use_dlpack,
)
for itr in range(1, niters + 1):
optimizer.zero_grad()
batch_y0, batch_t, batch_y = get_batch()
pred_y = ode.odeint_adjoint(batch_y0, batch_t)
loss = torch.mean(torch.abs(pred_y - batch_y))
loss.backward()
optimizer.step()
time_meter.update(time.time() - end)
loss_meter.update(loss.item())
if itr % test_freq == 0:
with torch.no_grad():
ode0.setupTS(
true_y0,
func,
step_size=step_size,
enable_adjoint=False,
implicit_form=implicit_form,
use_dlpack=use_dlpack,
)
pred_y = ode0.odeint_adjoint(true_y0, t)
loss = torch.mean(torch.abs(pred_y - true_y))
print("Iter {:04d} | Total Loss {:.6f}".format(itr, loss.item()))
visualize(true_y, pred_y, func, ii)
ii += 1
end = time.time()