-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourier2d.py
141 lines (122 loc) · 4.87 KB
/
fourier2d.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from .lowrank2d import LowRank2d
from .basics import SpectralConv2d
class FNN2d(nn.Module):
def __init__(self, modes1, modes2,
width=64, fc_dim=128,
layers=None,
in_dim=3, out_dim=1,
activation='tanh'):
super(FNN2d, self).__init__()
"""
The overall network. It contains 4 layers of the Fourier layer.
1. Lift the input to the desire channel dimension by self.fc0 .
2. 4 layers of the integral operators u' = (W + K)(u).
W defined by self.w; K defined by self.conv .
3. Project from the channel space to the output space by self.fc1 and self.fc2 .
input: the solution of the coefficient function and locations (a(x, y), x, y)
input shape: (batchsize, x=s, y=s, c=3)
output: the solution
output shape: (batchsize, x=s, y=s, c=1)
"""
self.modes1 = modes1
self.modes2 = modes2
self.width = width
# input channel is 3: (a(x, y), x, y)
if layers is None:
self.layers = [width] * 4
else:
self.layers = layers
self.fc0 = nn.Linear(in_dim, layers[0])
self.sp_convs = nn.ModuleList([SpectralConv2d(
in_size, out_size, mode1_num, mode2_num)
for in_size, out_size, mode1_num, mode2_num
in zip(self.layers, self.layers[1:], self.modes1, self.modes2)])
self.ws = nn.ModuleList([nn.Conv1d(in_size, out_size, 1)
for in_size, out_size in zip(self.layers, self.layers[1:])])
self.fc1 = nn.Linear(layers[-1], fc_dim)
self.fc2 = nn.Linear(fc_dim, out_dim)
if activation =='tanh':
self.activation = F.tanh
elif activation == 'gelu':
self.activation = F.gelu
elif activation == 'relu':
self.activation == F.relu
else:
raise ValueError(f'{activation} is not supported')
def forward(self, x):
'''
Args:
- x : (batch size, x_grid, y_grid, 2)
Returns:
- x: (batch size, x_grid, y_grid, 1)
'''
length = len(self.ws)
batchsize = x.shape[0]
size_x, size_y = x.shape[1], x.shape[2]
x = self.fc0(x)
x = x.permute(0, 3, 1, 2)
for i, (speconv, w) in enumerate(zip(self.sp_convs, self.ws)):
x1 = speconv(x)
x2 = w(x.view(batchsize, self.layers[i], -1)).view(batchsize, self.layers[i+1], size_x, size_y)
x = x1 + x2
if i != length - 1:
x = self.activation(x)
x = x.permute(0, 2, 3, 1)
x = self.fc1(x)
x = self.activation(x)
x = self.fc2(x)
return x
class PINO2d(nn.Module):
def __init__(self, modes1, modes2, width, layers=None, in_dim=3, out_dim=1):
'''
Args:
modes1: number of modes to keep
modes2: number of modes to keep
width: width of features
layers: list of integers
in_dim: input dimensionality, default: a(x), x, t
out_dim: output dimensionality, default: u(x,t)
'''
super(PINO2d, self).__init__()
self.modes1 = modes1
self.modes2 = modes2
self.width = width
if layers is None:
self.layers = [width] * 4
else:
self.layers = layers
self.fc0 = nn.Linear(in_dim, layers[0])
self.sp_convs = nn.ModuleList([SpectralConv2d(
in_size, out_size, mode1_num, mode2_num)
for in_size, out_size, mode1_num, mode2_num
in zip(self.layers, self.layers[1:], self.modes1, self.modes2)])
self.ws = nn.ModuleList([nn.Conv1d(in_size, out_size, 1)
for in_size, out_size in zip(self.layers[:-1], self.layers[1:-1])])
self.ws.append(LowRank2d(self.layers[-2], self.layers[-1]))
self.fc1 = nn.Linear(layers[-1], layers[-1] * 4)
self.fc2 = nn.Linear(layers[-1] * 4, out_dim)
def forward(self, x, y=None):
batchsize = x.shape[0]
size_x, size_y = x.shape[1], x.shape[2]
length = len(self.ws)
x = self.fc0(x)
x = x.permute(0, 3, 1, 2)
for i, (speconv, w) in enumerate(zip(self.sp_convs, self.ws)):
if i != length - 1:
x1 = speconv(x)
x2 = w(x.view(batchsize, self.layers[i], -1))\
.view(batchsize, self.layers[i+1], size_x, size_y)
x = x1 + x2
x = F.selu(x)
else:
x1 = speconv(x, y).reshape(batchsize, self.layers[-1], -1)
x2 = w(x, y).reshape(batchsize, self.layers[-1], -1)
x = x1 + x2
x = x.permute(0, 2, 1)
x = self.fc1(x)
x = F.selu(x)
x = self.fc2(x)
return x