-
Notifications
You must be signed in to change notification settings - Fork 9
/
Problem.py
176 lines (135 loc) · 5.76 KB
/
Problem.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
import torch
import abc
from math import pi
class Problem(metaclass=abc.ABCMeta):
@abc.abstractmethod
def __init__(self):
super().__init__()
self.ground_truth = None
@abc.abstractmethod
def __str__(self):
pass
@abc.abstractmethod
def pde(self, xx, yy):
pass
@abc.abstractmethod
def bound_condition(self, xx, yy):
pass
@abc.abstractmethod
def init_condition(self, xx, yy):
pass
def set_groud_truth(self):
pass
class Problem_1d_bodong(Problem):
def __init__(self):
super().__init__()
self.groud_truth = None
self.set_groud_truth()
def __str__(self):
return "dy_xx = -0.49 * sin(0.7 * x) - 2.25 * cos(1.5 * x)"
def pde(self, xx, yy):
dy_x = torch.autograd.grad(sum(yy[:, :]), xx, retain_graph=True, create_graph=True)[0]
dy_xx = torch.autograd.grad(sum(dy_x[:, :]), xx, retain_graph=True, create_graph=True)[0]
return -0.49 * torch.sin(0.7 * xx) - 2.25 * torch.cos(1.5 * xx), dy_xx
def bound_condition(self, xx, yy):
return self.ground_truth(xx), yy
def init_condition(self, xx, yy):
return torch.tensor(0.), torch.tensor(0.)
def set_groud_truth(self):
def fun(x):
return torch.sin(0.7 * x) + torch.cos(1.5 * x) - 0.1 * x
self.ground_truth = fun
return fun
class Problem_1d_diffussion(Problem):
def __init__(self):
super().__init__()
self.groud_truth = None
self.set_groud_truth()
def __str__(self):
return "dy_xx = -0.49 * sin(0.7 * x) - 2.25 * cos(1.5 * x)"
def pde(self, xx, yy):
dy_x = torch.autograd.grad(sum(yy[:, :]), xx, retain_graph=True, create_graph=True)[0]
dy_x, dy_t = dy_x[:, 0:1], dy_x[:, 1:]
dy_xx = torch.autograd.grad(sum(dy_x[:, :]), xx, retain_graph=True, create_graph=True)[0][:, 0:1]
return (
-dy_t + dy_xx,
xx[:, 1:]
* (0.49 * torch.sin(0.7 * xx[:, 0:1]) + 2.25 * torch.cos(1.5 * xx[:, 0:1])) - torch.sin(
0.7 * xx[:, 0:1]) - 3 * torch.cos(1.5 * xx[:, 0:1]) + 0.1 * xx[:, 0:1]
)
def bound_condition(self, xx, yy):
return self.ground_truth(xx), yy
def init_condition(self, xx, yy):
return self.groud_truth(xx), yy
def set_groud_truth(self):
def fun(x):
return (torch.sin(0.7 * x[:, 0:1]) + torch.cos(1.5 * x[:, 0:1]) - 0.1 * x[:, 0:1]) * x[:, 1:]
self.ground_truth = fun
return fun
class Problem_2d_Poisson(Problem):
def __init__(self):
super().__init__()
self.groud_truth = None
self.set_groud_truth()
def __str__(self):
return "dy_xx+d_y_yy = -0.25 * torch.sin(0.5 * xx[:, 0:1]) - 0.49 * torch.sin(0.7 * xx[:, 1:])"
def pde(self, xx, yy):
dy_x = torch.autograd.grad(sum(yy[:, :]), xx, retain_graph=True, create_graph=True)[0]
dy_x, dy_y = dy_x[:, 0:1], dy_x[:, 1:]
dy_xx = torch.autograd.grad(sum(dy_x[:, :]), xx, retain_graph=True, create_graph=True)[0][:, 0:1]
dy_yy = torch.autograd.grad(sum(dy_x[:, :]), xx, retain_graph=True, create_graph=True)[0][:, 1:]
return (
dy_yy + dy_xx,
# -0.01*pi**2 * torch.sin(0.1 * xx[:, 0:1]*pi) - 0.04*pi**2 * torch.sin(0.2 * xx[:, 1:]*pi)
# torch.zeros_like(dy_xx)
-torch.sin((xx[:, 1:] + 10) / 20 * pi)
* (0.49 * torch.sin(0.7 * xx[:, 0:1]) + 2.25 * torch.cos(1.5 * xx[:, 0:1]))
- (torch.sin(0.7 * xx[:, 0:1]) + torch.cos(1.5 * xx[:, 0:1]) - 0.1 * xx[:, 0:1]) * torch.sin(
(xx[:, 1:] + 10) / 20 * pi) * pi ** 2 / 400
)
def bound_condition(self, xx, yy):
return self.ground_truth(xx), yy
def init_condition(self, xx, yy):
return torch.tensor([0.]), torch.tensor([0.])
def set_groud_truth(self):
def fun(x):
return (torch.sin(0.7 * x[:, 0:1]) + torch.cos(1.5 * x[:, 0:1]) - 0.1 * x[:, 0:1]) * torch.sin(
(x[:, 1:] + 10) / 20 * pi)
self.ground_truth = fun
return fun
class Problem_Sphere_Poisson(Problem):
def __init__(self):
super().__init__()
self.groud_truth = None
self.m = 7
self.set_groud_truth()
def __str__(self):
return "dy_xx+d_y_yy = -0.25 * torch.sin(0.5 * xx[:, 0:1]) - 0.49 * torch.sin(0.7 * xx[:, 1:])"
def pde(self, x, y):
m = self.m
dy_x = torch.autograd.grad(sum(y[:, :]), x, retain_graph=True, create_graph=True)[0]
dy_x, dy_t = dy_x[:, 0:1], dy_x[:, 1:]
sinx = torch.sin(x[:, 0:1])
dy_xx = torch.autograd.grad(sum(dy_x[:, :] * sinx), x, retain_graph=True, create_graph=True)[0][:, 0:1]
dy_tt = torch.autograd.grad(sum(dy_t[:, :]), x, retain_graph=True, create_graph=True)[0][:, 1:]
return (
dy_xx / sinx
+ dy_tt / sinx ** 2,
(-(m + 1) * (m + 2) * torch.cos(x[:, :1]) * (torch.sin(x[:, :1]) ** (m)) * torch.cos(
m * x[:, 1:] - 0.0 * m) - 2 * torch.cos(x[:, :1]))
- (-(m) * (m + 1) * torch.cos(x[:, :1]) * (torch.sin(x[:, :1]) ** (m - 1)) * torch.cos(
(m - 1) * x[:, 1:] - 0.0 * m) - 2 * torch.cos(x[:, :1]))
)
def bound_condition(self, xx, yy):
return self.ground_truth(xx), yy
def init_condition(self, xx, yy):
return torch.tensor([0.]), torch.tensor([0.])
def set_groud_truth(self):
m = self.m
def fun(x):
return (torch.cos(x[:, :1]) * (torch.sin(x[:, :1]) ** m) * torch.cos(
m * x[:, 1:]) + torch.cos(x[:, :1])) \
- (torch.cos(x[:, :1]) * (torch.sin(x[:, :1]) ** (m - 1)) * torch.cos(
(m - 1) * x[:, 1:]) + torch.cos(x[:, :1]))
self.ground_truth = fun
return fun