-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequation.py
64 lines (53 loc) · 1.57 KB
/
equation.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
import tensorflow as tf
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
from torch.autograd import Variable
from torch.autograd import grad
import numpy as np
def u0(t, x):
"""
Input: t,x = time and space points for initial condition
Output: u_0(t,x) = solution on initial condition
"""
#return torch.sin(np.pi*x) + 0.5*torch.sin(4*np.pi*x)
n = x.shape[0]
return torch.zeros((n,1))
def v0(t, x, dimension):
"""
Input: t,x = time and space points for speed initial condition
dimension = space dimension for model
Output: v_0(t,x) = speed on initial condition
"""
n = x.shape[0]
res = torch.zeros((n, dimension))
return res
def u_bound(t, x, dimension):
"""
Input: t,x = time and space points for boundary condition
dimension = space dimension for model
Output: u_b(t,x) = solution on boundary condition
"""
n = x.shape[0]
res = torch.zeros((n, dimension))
return res
def residual(t, x, u_t, u_tt, u_xx, c):
"""
Input: t,x and derivatives of u
Ouput : residual of PDE
"""
#return u_tt - (c**2)*u_xx
return u_xx + np.pi**2 * torch.sin(np.pi * x) * torch.sin(np.pi * t)
def true_u(x, a=0.5, c=2):
"""
Input: x and hyperparameters
Ouput : true forward solution
"""
t = x[:, 0]
x = x[:, 1]
return np.sin(np.pi * x) * np.cos(c * np.pi * t) + a * np.sin(2 * c * np.pi * x) * np.cos(4 * c * np.pi * t)
# DTYPE = 'float32'
# tf.keras.backend.set_floatx(DTYPE)
# a = tf.constant([0, 1], dtype=DTYPE)
# print(a)