-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHMC.SYN.FullMomentTensor.py
183 lines (146 loc) · 5.17 KB
/
HMC.SYN.FullMomentTensor.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
#!/usr/bin/env python
import os
from seishmc.DHMC.fmt import DHMC_FMT
from seishmc.visualization.viz_samples_fmt import pairplot_samples_FMT
from mtuq.graphics import plot_data_greens1, plot_beachball
from mtuq import read, open_db
from mtuq.event import Origin
from mtuq.misfit import Misfit
from mtuq.process_data import ProcessData
from mtuq.util.cap import parse_station_codes, Trapezoid
import numpy as np
# Set the random seed using our lab's room number.
np.random.seed(511)
if __name__=='__main__':
#
# Synthetic example of moment tensor inversion using Hamiltonian Monte Carlo (HMC) sampling.
#
path_data = '../data/examples/synthetic/data/*.[zrt]'
path_greens = '../data/examples/synthetic/greens'
path_weights= '../data/examples/synthetic/weights.dat'
event_id = 'syn_example'
model = 'socal3D'
taup_model = 'ak135'
# folder storing the cache, samples, and inversion results.
saving_dir = "../output/examples/synthetic/HMC_FMT/"
#
# Synthetic waveform
#
process_sw = ProcessData(
filter_type='Bandpass',
freq_min=0.033333,
freq_max=0.125,
taup_model=taup_model,
apply_scaling=False,
window_type='synthetic',
window_length=100.,
capuaf_file=path_weights,
)
#
# For our objective function, we will use zero time shift.
#
misfit_sw = Misfit(
norm='L2',
time_shift_min=-0.,
time_shift_max=+0.,
time_shift_groups=['ZR','T'],
)
#
# User-supplied weights control how much each station contributes to the
# objective function
#
station_id_list = parse_station_codes(path_weights)
#
# Next, we specify the source-time function
#
wavelet = Trapezoid(
magnitude=3.0)
#
# Origin time and location will be fixed.
#
origin = Origin({
'time': '2019-07-05T12:38:30.0000Z',
'latitude': 35.771667,
'longitude': -117.571,
'depth_in_m': 6820.0,
'id': '11057910'
})
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
#
# The main I/O work starts now
#
if comm.rank==0:
print('Reading data...\n')
data = read(path_data, format='sac',
event_id=event_id,
station_id_list=station_id_list,
tags=['units:cm', 'type:displacement'])
data.sort_by_distance()
stations = data.get_stations()
print('Processing data...\n')
data_sw = data.map(process_sw)
print('Load Greens functions...\n')
db = open_db(path_greens, format='SPECFEM3D_SGT', model='socal3D')
greens = db.get_greens_tensors(stations, origin)
print('Processing Greens functions...\n')
greens.convolve(wavelet)
greens_sw = greens.map(process_sw)
else:
stations = None
data_sw = None
greens_sw = None
stations = comm.bcast(stations, root=0)
data_sw = comm.bcast(data_sw, root=0)
greens_sw = comm.bcast(greens_sw, root=0)
#
# The main computational work starts now
#
rank = comm.Get_rank()
print('Sampling with HMC...\n')
solver_hmc = DHMC_FMT(None, None, None,
misfit_sw, data_sw, greens_sw,
saving_dir, b_save_cache=True,
n_step_cache=500, verbose=True)
# set the range of number of step
solver_hmc.set_n_step(min=3, max=10)
# set the range of step interval
solver_hmc.set_epsilon(min=0.05, max=1.0)
# set the number of sample to be accepted
n_sample = 2000
# set initial solution in degree and Mw
q0 = np.array([np.random.uniform(0, 360),
np.random.uniform(0, 90),
np.random.uniform(-90, 90),
np.random.uniform(2.8, 3.2),
np.random.uniform(0, 180),
np.random.uniform(-30, 30)])
solver_hmc.set_q(q0)
# sampling...
task_id = 'syn_FMT_hmc_%d' % rank
solver_hmc.sampling(n_sample=n_sample, task_id=task_id)
print('Generating figures...\n')
data_file = os.path.join(saving_dir, "%s_samples_N%d.pkl" % (task_id, n_sample))
fig_path = os.path.join(saving_dir, task_id)
# the reference solution is utilized for plotting only.
ref_sol = np.array([30., 45., 60., 2.95, 83., -7.])
pairplot_samples_FMT(file_path=data_file, fig_saving_path=fig_path, init_sol=q0, ref_sol=ref_sol)
# if no reference solution provided, plot with:
# pairplot_samples_FMT(file_path=data_file, fig_saving_path=fig_path, init_sol=q0, ref_sol=None)
# Get the solution
best_mt, lune_dict = solver_hmc.get_solution()
fig_path = os.path.join(saving_dir, '%s_waveforms.png' % task_id)
plot_data_greens1(fig_path,
data_sw,
greens_sw,
process_sw,
misfit_sw,
stations,
origin,
best_mt,
lune_dict)
fig_path = os.path.join(saving_dir, '%s_beachball.png' % task_id)
plot_beachball(fig_path, best_mt, stations, origin)
MPI.Finalize()
print('\nFinished\n')