-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleMassSpringDamper.py
More file actions
172 lines (110 loc) · 3.86 KB
/
SingleMassSpringDamper.py
File metadata and controls
172 lines (110 loc) · 3.86 KB
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
# Single Mass-Spring-Damper system Solved by PINN
# This software is distributed under the BSD 3-clause license.
# License file included in the script directory.
# This script solves the single mass-spring-damper system explained in the IntroductionToPINN.pdf file.
# Using both:
# - Runge-Kutta Numerical solver
# - Physics Informed Neural Network (PINN)
# Written by: MehdiYaghouti, 2023
# Contact Info.: MehdiYaghouti@gmail.com
import jax
import jax.numpy as jnp
import numpy as np
import matplotlib.pyplot as plt
import optax
import numba
m = 1.5
k = 4
b = 0.5
@numba.njit
def RK4(odefun,ics,h,span,degree):
N= int( (span[1]-span[0])/h )
tY = np.zeros((N+1,degree+1))
tY[0,1:] = ics
for i in range(N):
tY[i+1,0] = tY[i,0] + h
k1= odefun(tY[i,0] , tY[i,1:])
k2= odefun(tY[i,0] +(h/2), tY[i,1:] +(h*k1)/2 )
k3= odefun(tY[i,0] +(h/2), tY[i,1:] +(h*k2)/2)
k4= odefun(tY[i,0] +(h) , tY[i,1:] +(h*k3))
tY[i+1,1:] = tY[i,1:] + h*(1/6) * (k1+2*k2+2*k3+k4)
return tY[:,0],tY[:,1:]
@numba.njit
def system_of_ode(t,V):
dx, x = V[0],V[1]
return np.array([ (-k*x-b*dx)/m ,dx])
t,y=RK4(system_of_ode, ics=np.array([0,6]), h=1e-5, span=np.array([1e-4,10*np.pi]), degree =2)
plt.figure(figsize=(10,6))
plt.plot(t,y[:,1],'-r',label='Runge-Kutta x[1]')
plt.grid()
plt.legend()
N_b = 1000
N_c = 100000
tmin,tmax=0*jnp.pi , 10*jnp.pi
y1_t0 = jnp.zeros([N_b,1],dtype='float32')
y1_ic = jnp.ones_like(y1_t0)*0
Y1_IC = jnp.concatenate([y1_t0,y1_ic],axis=1)
y2_t0 = jnp.zeros([N_b,1],dtype='float32')
y2_ic = jnp.ones_like(y2_t0) * 6
Y2_IC = jnp.concatenate([y2_t0,y2_ic],axis=1)
cnds = [Y1_IC,Y2_IC ]
key=jax.random.PRNGKey(0)
t_c = jax.random.uniform(key,minval=tmin,maxval=tmax,shape=(N_c,1))
pnts_train = t_c
def ODE(t,y1,dy,d2y):
return m*d2y(t)-(-k*y1(t)-b*dy(t))
def init_params(layers):
keys = jax.random.split(jax.random.PRNGKey(0),len(layers)-1)
params = list()
for key,n_in,n_out in zip(keys,layers[:-1],layers[1:]):
lb, ub = -(1 / jnp.sqrt(n_in)), (1 / jnp.sqrt(n_in))
W = lb + (ub-lb) * jax.random.uniform(key,shape=(n_in,n_out))
B = jax.random.uniform(key,shape=(n_out,))
params.append({'W':W,'B':B})
return params
def fwd(params,t):
X = jnp.concatenate([t],axis=1)
*hidden,last = params
for layer in hidden :
X = jax.nn.tanh(X@layer['W']+layer['B'])
return X@last['W'] + last['B']
@jax.jit
def MSE(true,pred):
return jnp.mean((true-pred)**2)
def Loss(params,pnts_train,cnds):
t_c =pnts_train[:,[0]]
y1_func = lambda t : fwd(params,t)[:,[0]]
y1_func_t = lambda t:jax.grad(lambda t:jnp.sum(y1_func(t)))(t)
d2y1_func_t = lambda t:jax.grad(lambda t: jnp.sum(jax.grad(lambda t:jnp.sum(y1_func(t)))(t)) )(t)
loss_y1 = ODE(t_c,y1_func, y1_func_t, d2y1_func_t)
loss = jnp.mean( loss_y1 **2)
t_ic,y1_ic = cnds[0][:,[0]],cnds[0][:,[1]]
loss += MSE(y1_ic,y1_func_t(t_ic))
t_ic,y2_ic = cnds[1][:,[0]],cnds[1][:,[1]]
loss += MSE(y2_ic,y1_func(t_ic))
return loss
@jax.jit
def update(opt_state,params,pnts_train,cnds):
grads=jax.jit(jax.grad(Loss,0))(params,pnts_train,cnds)
updates, opt_state = optimizer.update(grads, opt_state)
params = optax.apply_updates(params, updates)
return opt_state,params
params = init_params([1] + [4]+[9]*3+[4] +[1])
optimizer = optax.adam(2e-2)
opt_state = optimizer.init(params)
epochs = 20000
loss=[]
for _ in range(epochs):
key=jax.random.PRNGKey(_)
t_c = jax.random.uniform(key,minval=tmin,maxval=tmax,shape=(N_c,1))
pnts_train = t_c
opt_state,params = update(opt_state,params,pnts_train,cnds)
if _ %(50) ==0:
loss.append(Loss(params,pnts_train,cnds))
print(f'Epoch={_}\tloss={loss[-1]:.3e}')
dT = 1e-3
Tf = 10*jnp.pi
T = np.arange(0,Tf+dT,dT)
plt.plot(T,fwd(params,T.reshape(-1,1))[:,0],'--k',label='NN[x1]',linewidth=2)
plt.legend()
plt.savefig('SingleMassSpringDamper.png')