-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.py
344 lines (266 loc) · 7.7 KB
/
examples.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
Examples include:
1) Simple forward model of a velocity-step
2) Forward model of a slide-hold-slide experiment
3) Inversion on synthetic data
4) Bayesian inference
5) Simulating regular stick-slips with radiation damping
"""
from __future__ import print_function
import matplotlib
matplotlib.use("qt5agg")
import matplotlib.pyplot as plt
import numpy as np
import seaborn
seaborn.set(font_scale=1.2)
from pyrsf.inversion import rsf_inversion
# Initialise inversion API
rsf = rsf_inversion()
# A simple velocity-step forward model
def simple_forward_model():
# Dictionary of input parameters
params = {
"a": 0.001,
"b": 0.0015,
"Dc": 1e-4,
"k": 50.0,
"mu0": 0.6,
"V0": 1e-6,
"V1": 1e-5,
"eta": 0,
}
t = np.linspace(0, 100, int(1e3))
# Set model parameters
rsf.set_params(params)
# Select ageing law
rsf.set_state_evolution("ageing")
# Set initial values (V0, theta0), taken at steady-state
rsf.set_initial_values(np.hstack([params["V0"], params["Dc"] / params["V0"]]))
# Perform forward model
result = rsf.forward(t, mode="dense")
# Time-series of friction and velocity
t = result["t"]
mu = result["mu"]
V = result["V"]
# Plot results
plt.figure()
plt.subplot(211)
plt.plot(t, mu, "-")
plt.ylabel("friction [-]")
plt.subplot(212)
plt.axhline(params["V1"], ls="--", c="k")
plt.plot(t, V)
plt.yscale("log")
plt.xlabel("time [s]")
plt.ylabel("velocity [m/s]")
plt.tight_layout()
plt.show()
# Forward model of slide-hold-slide sequence
def forward_SHS():
# Dictionary of input parameters
params = {
"a": 0.001,
"b": 0.0015,
"Dc": 1e-4,
"k": 50.0,
"mu0": 0.6,
"V0": 1e-6,
"V1": 1e-6,
"eta": 0,
}
t = np.linspace(0, 100, int(1e1))
# Set model parameters
rsf.set_params(params)
# Select ageing law
rsf.set_state_evolution("ageing")
# Set initial values (V0, theta0), taken at steady-state
rsf.set_initial_values(np.hstack([params["V0"], params["Dc"] / params["V0"]]))
# Perform forward model
result = rsf.forward(t)
# Time-series of friction and velocity
mu = result["mu"]
V = result["V"]
plt.figure()
plt.subplot(211)
plt.plot(t, mu)
plt.subplot(212)
plt.plot(t, V)
t_prev = t[-1]
t = np.linspace(t_prev, 1000+t_prev, int(1e3))
params["V1"] = 0.0
rsf.set_params(params)
rsf.set_initial_values(np.hstack([result["V"][-1], result["theta"][-1]]))
# Perform forward model
result = rsf.forward(t)
# Time-series of friction and velocity
mu = result["mu"]
V = result["V"]
plt.subplot(211)
plt.plot(t, mu)
plt.subplot(212)
plt.plot(t, V)
t_prev = t[-1]
t = np.linspace(t_prev, 1000 + t_prev, int(1e4))
params["V1"] = 1e-6
rsf.set_params(params)
rsf.set_initial_values(np.hstack([result["V"][-1], result["theta"][-1]]))
result = rsf.forward(t)
mu = result["mu"]
V = result["V"]
plt.subplot(211)
plt.plot(t, mu)
plt.subplot(212)
plt.plot(t, V)
# Plot results
plt.subplot(211)
plt.ylabel("friction [-]")
plt.subplot(212)
plt.yscale("log")
plt.xlabel("time [s]")
plt.ylabel("velocity [m/s]")
plt.tight_layout()
plt.show()
# Perform inversion on synthetic data
def simple_inversion():
# Dictionary of input parameters
params = {
"a": 0.001,
"b": 0.0015,
"Dc": 1e-4,
"k": 50.0,
"mu0": 0.6,
"V0": 3e-6,
"V1": 1e-5,
"eta": 0,
}
t = np.linspace(0, 100, int(1e3))
# Set model parameters
rsf.set_params(params)
# Select ageing law
rsf.set_state_evolution("ageing")
# Set initial values (V0, theta0), taken at steady-state
y0 = [params["V0"], params["Dc"] / params["V0"]]
rsf.set_initial_values(y0)
# Perform forward model
result = rsf.forward(t)
# Generate noisy signal
np.random.seed(0)
noise = 1e-4*(np.random.rand(len(result["mu"])) - 0.5)
mu_noisy = result["mu"] + noise
# Change initial parameters to make the inversion
# scheme sweat a little bit
params["a"] = 0.0008
params["b"] = 0.0011
params["Dc"] = 0.9e-4
# params["k"] = 40.0
y0 = [params["V0"], params["Dc"] / params["V0"]]
rsf.set_params(params)
rsf.set_initial_values(y0)
# Construct our data dictionary
data_dict = {"mu": mu_noisy, "t": t}
# The parameters to invert for
inversion_params = ("a", "b", "Dc", "k")
# Perform the inversion. The results are given as a dictionary
# in pairs of (value, uncertainty)
inv_result = rsf.inversion(data_dict, inversion_params, plot=True, mode="step")
print(inv_result)
# Perform Bayesian inference
def bayesian_inference():
# Dictionary of input parameters
params = {
"a": 0.001,
"b": 0.0015,
"Dc": 1e-4,
"k": 50.0,
"mu0": 0.6,
"V0": 3e-6,
"V1": 1e-5,
"eta": 0,
}
t = np.linspace(0, 100, int(1e3))
# Set model parameters
rsf.set_params(params)
# Select ageing law
rsf.set_state_evolution("ageing")
# Set initial values (V0, theta0), taken at steady-state
y0 = [params["V0"], params["Dc"] / params["V0"]]
rsf.set_initial_values(y0)
# Perform forward model
result = rsf.forward(t)
# Generate noisy signal
np.random.seed(0)
noise = 1e-4*(np.random.rand(len(result["mu"])) - 0.5)
mu_noisy = result["mu"] + noise
# Change initial parameters to make the inversion
# scheme sweat a little bit
params["a"] = 0.0008
params["b"] = 0.0011
params["Dc"] = 0.9e-4
# params["k"] = 40.0
y0 = [params["V0"], params["Dc"] / params["V0"]]
rsf.set_params(params)
rsf.set_initial_values(y0)
# Construct our data dictionary
data_dict = {"mu": mu_noisy, "t": t}
# The parameters to invert for
inversion_params = ("a", "b", "Dc", "k")
# Perform the inversion. The results are given as a dictionary
# in pairs of (value, uncertainty)
inv_result = rsf.inversion(
data_dict, inversion_params, plot=False,
bayes=True, load_pickle=False, mode="step"
)
rsf.plot_mcmc_chain()
rsf.corner_plot()
# Forward modelling of stick-slips with stable limit cycles
def regular_stickslips():
# Define parameters for radiation damping. See e.g. Thomas et al. (2014)
G = 30e9
Vs = 3e3
sigma = 1e7
eta = 0.5*G/(Vs*sigma)
# Dictionary of input parameters
params = {
"a": 0.001,
"b": 0.0015,
"Dc": 3e-5,
"k": 10.0,
"mu0": 0.6,
"V0": 1e-6,
"V1": 1e-5,
"eta": eta,
}
kc = (params["b"]-params["a"])/params["Dc"]
print("k/kc = %.3f" % (params["k"]/kc))
t = np.linspace(0, 500, int(1e4))
# Set model parameters
rsf.set_params(params)
# Select ageing law
rsf.set_state_evolution("ageing")
# Set initial values (V0, theta0), taken at steady-state
rsf.set_initial_values(np.hstack([params["V0"], params["Dc"] / params["V0"]]))
# Perform forward model
result = rsf.forward(t, mode="step")
# Time-series of friction and velocity
t = result["t"]
mu = result["mu"]
V = result["V"]
# Plot results
plt.figure()
plt.subplot(211)
plt.plot(t, mu, "-")
plt.ylabel("friction [-]")
plt.subplot(212)
plt.axhline(params["V1"], ls="--", c="k")
plt.plot(t, V)
plt.yscale("log")
plt.xlabel("time [s]")
plt.ylabel("velocity [m/s]")
plt.tight_layout()
plt.show()
if __name__ == "__main__":
simple_forward_model()
forward_SHS()
simple_inversion()
bayesian_inference()
regular_stickslips()