-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_inversion_sila_example.m
234 lines (192 loc) · 8.03 KB
/
run_inversion_sila_example.m
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
% clear workpace
clear; clc
% make sure new matlab session use different random numbers
rng shuffle
%% add paths needed to exicute code
addpath([cd,'\river_models']); %add the path to the river models
addpath([cd,'\cosmo_models']); %add the path to the cosmo models
addpath([cd,'\thermo_models']); %add the path to the thermo models
addpath([cd,'\NA_inversion']); %add the path to the neighborhood algorithum
addpath([cd,'\forward_model_functions']) % add path to function to exicute forward models
addpath([cd,'\Calabria_data']); % add path to the folder with the pre-processed data
% name added to files produced at the end of the inversion
ftag = 'sila_example';
%% load the data
% cosmo data
load cosmo_sila.mat
% thermo data
load thermo_sila.mat
% marine terrace data
mt = load('sila_MT.txt');
% load stream data (Note this was prepped using TopoToolbox)
load stream_param_sila.mat
%% add marine terrace data to parameters
% define marine terrace parameters
param.mt_age = mt(:,1);
param.mt_u = mt(:,2);
param.mt_uc = mt(:,3);
%% User defined parameters
% define time variables
param.start_t = 30; % model run time in Myr
param.dt = 1e3; % model time step in years
% river variables
param.ee = 15; % error on river network elevations
% cosmo variable
param.muon=1; % model muon production according to [1: Braucher et al. 2013, 2: Heisinger et al. 2002a,b]
param.cosmo.thickness=0; % sample thickness in cm
param.cosmo.topocorr=1; % topographic shielding
param.cosmo.aa='std';
param.dx_cosmo=1000; % distance between sample locations for which tcn are calculated in m
param.t_record = 2; % when to start cosmo in Myr
% thermo variables
param.T0=20; % surface temperature at sea level in C
param.lr=5; % atmospheric lapse rate in C/km
param.TD=1E-6; % thermal diffusivity in m2/s
param.rho_c=2700; % crustal density in kg/m3
param.hpc=9.6E-10*param.rho_c;% crustal heat production in W/m3
param.cp_c=800; % specific heat capacity of granite in J/kg*K
param.gg = 25; % initial geothermal gradient
% other variables
param.kflag = 0;
% define overall parameters
param.nu=4; % number of uplift steps
param.nux=1; % 1: lateral constant uplift,
%% additional parameters needed for forward model
% additional cosmo parameters
param.cosmo_meas = cosmo_meas;
param.nr_cosmo = length(cosmo_meas.x);
[lon,lat] = utm2deg(param.Sx,param.Sy,'33 N');
param.cosmo.lat_river = lat;
param.cosmo.long_river = lon;
% additional thermo parameters
param.thermo_meas = thermo_meas;
param.nr_thermo = length(thermo_meas.x);
% sample = [Time1, Time2, Time3, U1, U2, U3, U4, K, n, m/n]
sample = [22.13,10.16,1.60,1.22,0.41,0.036,0.50,5.25e-6,1.11,0.40];
corr_sample_size(sample,param.nu,param.nux,param.kflag)
% Here 'sample' is roughly the MAP solution from pervious model runs
MAP = sample;
%% inversion of data with NA (Neighbourhood Algorithm)
ndin=10;
% define priors
rangein=zeros(2,ndin);
% t1 t2 t3 u1 u2 u3 u4 k n m/n
rangein(1,:)=[13 5 0.25 0.35 0 0 0 1E-8 1 0.3];
rangein(2,:)=[25 13 5 2 0.5 1.0 2 1E-5 1.5 0.7];
resolution=[0.1 0.1 0.05 0.01 0.002 0.005 0.005 5E-8 0.01 0.01]; % if the range of resampled cells < resolution, than the inverison is starting from a new initial ensemble
inv_method=1; % inversion method: 1 (only resampling of existing ensemble), 0 (resampling and additional random samples are generted)
% load boundaries of input parameters
load na_param_inc.mat
% Info and Logical unit common blocks used by NA routines
global inv_method verbose debug summary nxsave ndsave ndc nerr ncald nupd cells torder taxis tup tcd tdev tna tres sobol sob_seq
% User specific setup for forward modelling
nd=ndin;
range(:,1:nd)=rangein(:,1:nd);
% Read in options for Neighbourhood algorithm.
[monte,istype,nsleep,noforward,nclean,nsample,nsamplei,itmax,ncells]=NA_options(nsample_max, nit_max, nmod_max,...
nsleep_max, nd);
% set other info or debug options
check = 0;
scales(1:nd)=-1; % 0: No transform (All a priori model co-variances are equal to unity); 1: Use parameter range as a priori model co-variances
% Initialize NA routines.
[restartNA,ranget,xcur]=NA_initialize(nd,nd_max,range,scales,nsample,ncells);
% Generate or read in starting models
[new_models,new_misfit]=NA_initial_sample(istype,monte,nsample,nd,range,scales);
new_models(:,1) = sample';
% initialize variables
misfit_all=nan(1,nsample*(itmax+1));
models_all=nan(ndin,nsample*(itmax+1));
logL_topo_all=nan(1,nsample*(itmax+1));
logL_tcn_all=nan(1,nsample*(itmax+1));
logL_thermo_all=nan(1,nsample*(itmax+1));
logL_MT_all=nan(1,nsample*(itmax+1));
new_logL_topo=nan(1,nsample);
new_logL_tcn=nan(1,nsample);
new_logL_thermo=nan(1,nsample);
new_logL_MT = nan(1,nsample);
n = 0;
nn = 1;
for it = 1:itmax+1
% skip forward modelling for starting models if they have been read in from a NAD file
if (it>1 || noforward~=1)
hh = waitbar(0,['Running iteration ',num2str(it),' of ', num2str(itmax+1),'...']);
for i=1:nsample
misfits=forward_sila(new_models(:,i),param);
new_misfit(i)= misfits.norm;
new_logL_topo(i)=misfits.logL_topo;
new_logL_tcn(i)=misfits.logL_TCN;
new_logL_thermo(i)=misfits.logL_thermo;
new_logL_MT(i) = misfits.logL_MT;
waitbar(i/nsample,hh)
end
close(hh)
end
inds = n+1:n+nsample;
n = n+nsample;
misfit_all(inds) = new_misfit;
models_all(:,inds) = new_models;
logL_topo_all(inds) = new_logL_topo;
logL_tcn_all(inds) = new_logL_tcn;
logL_thermo_all(inds) = new_logL_thermo;
logL_MT_all(inds) = new_logL_MT;
misfit = misfit_all(nn:n);
models = models_all(:,nn:n);
% Calculate properties of current misfit distribution.
% (Mean,min,best model etc.)
[mfitmean,mfitminc,mfitmin,mfitord,mopt]=NA_misfits(misfit);
ntot=length(misfit);
calcmovement=0;
% tranform to scale
models_sca = nan(size(models));
for i=1:ntot
models_sca(:,i)=transform2sca(models(:,i),nd,range,scales);
end
if (it<itmax+1)
% Call main NA routines
if (monte)
% Perform Monte Carlo search for comparison to NA.
[new_models_sca]=NA_random(nd,range,nsample);
else
% generate a new sample using Neighbourhood algorithm (resample version)
[new_models_sca,xcur,restartNA,work_NA1]=NA_sample(models_sca,ntot,nsample,nd,nsleep,ncells,misfit,mfitord,ranget,check,xcur,calcmovement,nclean);
end
end
% transform to raw
for i=1:nsample
new_models(:,i)=transform2raw(nd,range,scales,new_models_sca(:,i));
end
% calculate difference between mest ncells models and either continue
% inversion or start from a new initial set of samples
max_d=max(models(:,mfitord(1:ncells))');
min_d=min(models(:,mfitord(1:ncells))');
d=max_d-min_d;
if sum(d<resolution)>0
misfit=[];
models=[];
% Generate or read in starting models
[new_models,new_misfit]=NA_initial_sample(istype,monte,nsample,nd,range,scales);
new_models(:,1) = MAP';
nn = n + 1;
end
end
% save relevent outputs
save(['misfit_',ftag,'.mat'],'misfit_all');
save(['models_',ftag,'.mat'],'models_all');
save(['logL_topo_',ftag,'.mat'],'logL_topo_all');
save(['logL_thermo_',ftag,'.mat'],'logL_thermo_all');
save(['logL_MT_',ftag,'.mat'],'logL_MT_all');
% sort the results and pull out the "best" model for plotting
tab = [misfit_all; models_all]';
tab = sortrows(tab);
inds = find(~isnan(tab(:,2)));
tab = tab(inds,:);
param.sample=tab(1,2:end)';
% plot the "best-fit" results
forward_sila_plot(param.sample,param);
% plot the marginal posteriors with the MAP solution as a red dashed line
param_names = {'Time 1 (Ma)','Time 2 (Ma)','Time 3 (Ma)','Uplift rate 1 (mm/yr)',...
'Uplift rate 2 (mm/yr)','Uplift rate 3 (mm/yr)','Uplift rate 4 (mm/yr)'....
'K (m^{(1-2m)}/yr)','n','m/n'};
param_dat = tab(:,2:end);
map = param_dat(1,:);
plot_marginals(param_dat,param_names,rangein,map);