forked from neuraloperator/physics_informed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile-solver-legacy.py
65 lines (49 loc) · 1.57 KB
/
profile-solver-legacy.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
import math
import torch
from solver.legacy_solver import navier_stokes_2d, GaussianRF
import scipy.io
from timeit import default_timer
if __name__ == '__main__':
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Resolution
s = 2048
sub = 1
# Number of solutions to generate
N = 1
# Set up 2d GRF with covariance parameters
GRF = GaussianRF(2, s, alpha=2.5, tau=7, device=device)
# Forcing function: 0.1*(sin(2pi(x+y)) + cos(2pi(x+y)))
t = torch.linspace(0, 1, s + 1, device=device)
t = t[0:-1]
X, Y = torch.meshgrid(t, t)
f = 0.1 * (torch.sin(2 * math.pi * (X + Y)) + torch.cos(2 * math.pi * (X + Y)))
# Number of snapshots from solution
record_steps = 200
# Inputs
a = torch.zeros(N, s, s)
# Solutions
u = torch.zeros(N, s, s, record_steps)
# Solve equations in batches (order of magnitude speed-up)
# Batch size
bsize = 1
c = 0
t0 = default_timer()
for j in range(N // bsize):
# Sample random feilds
w0 = GRF.sample(bsize)
# Solve NS
sol, sol_t = navier_stokes_2d(w0, f, 1e-3, 50.0, 1e-4, record_steps)
a[c:(c + bsize), ...] = w0
u[c:(c + bsize), ...] = sol
c += bsize
t1 = default_timer()
print(f'Time cost {t1 - t0} s')
torch.save(
{
'a': a.cpu(),
'u': u.cpu(),
't': sol_t.cpu()
},
'data/ns_data.pt'
)
# scipy.io.savemat('data/ns_data.mat', mdict={'a': a.cpu().numpy(), 'u': u.cpu().numpy(), 't': sol_t.cpu().numpy()})