-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTEM_1D_Inversion.m
259 lines (205 loc) · 7.17 KB
/
TEM_1D_Inversion.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
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
function [m, obj_fn, lambda_history] = TEM_1D_Inversion(varargin)
%TEM_1D_Inversion inversion of synthetic TEM data
%
p = inputParser;
addParameter(p, 'data', [], @isnumeric);
addParameter(p, 'times', [], @isnumeric);
addParameter(p, 'obs', [], @isnumeric);
addParameter(p, 'rho', [], @isnumeric);
addParameter(p, 'thickness', [], @isnumeric);
addParameter(p, 'fix', [], @isnumeric);
addParameter(p, 'verbose', true, @islogical);
addParameter(p, 'maxit', 1e2, @isnumeric);
addParameter(p, 'lambda', 1e2, @isnumeric);
addParameter(p, 'lambda_min', 1e-12, @isnumeric);
addParameter(p, 'reduce_lambda', 1e-1, @isnumeric);
addParameter(p, 'lambda_threshold', 1e-1, @isnumeric);
addParameter(p, 'scale_data', @asinh, @(x) isa(x, 'function_handle'));
addParameter(p, 'scale_asinh', 1e-12, @isnumeric);
addParameter(p, 'goal_normgc', 1e-4, @isnumeric);
addParameter(p, 'goal_obj', 1e-8, @isnumeric);
validationFcn = @(x) assert(isnumeric(x) && isscalar(x) ...
&& (x > 0), 'Parameter must be > 0');
addParameter(p, 'pert', 1e-6, validationFcn);
addParameter(p, 'goal_obj_diff', 1e-5, @isnumeric);
addParameter(p, 'linesearch_threshold', 1e-6, @isnumeric);
addParameter(p, 'armijo_c1', 1e-4, @isnumeric);
parse(p, varargin{:});
dobs = p.Results.data;
assert(~isempty(dobs), 'Empty dataset.');
t = p.Results.times;
assert(~isempty(t), 'Empty time range.');
assert(length(t) == length(dobs), 'Data and times not equal.');
r = p.Results.obs;
assert(r > 0.0, 'r must be > 0.');
rho = p.Results.rho;
assert(~isempty(rho), 'Missing starting model layer resistivities.');
thk = p.Results.thickness;
assert(length(thk) == length(rho) - 1, 'Wrong layer thicknesses.');
fix = p.Results.fix;
assert(length(fix) == length(rho), 'Wrong number of fixed params.');
assert(~all(fix), 'At least one parameter must be free.');
if isempty(fix)
fix = zeros(size(rho));
end
update = ~fix;
P = eye(length(rho));
if ~all(update)
P(find(~update), :) = []; %#ok
end
WTW = eye(min(size(P')));
verbose = p.Results.verbose;
max_gn_iterations = p.Results.maxit;
lambda = p.Results.lambda;
lambda_min = p.Results.lambda_min;
lambda_scaling_factor = p.Results.reduce_lambda;
lambda_droptol = p.Results.lambda_threshold;
pert = p.Results.pert;
scale_data = p.Results.scale_data;
a = p.Results.scale_asinh;
scalefn = @(x, a) scale_data(x ./ a);
c1 = p.Results.armijo_c1;
step_size_min = p.Results.linesearch_threshold;
tol = p.Results.goal_normgc;
obj_fn_goal = p.Results.goal_obj;
goal_obj_diff = p.Results.goal_obj_diff;
% Some useful function handles
%
getData = @(rho) getVMDLayeredTransient([t(1) t(end)], r, rho, thk, 0.0, 1.0);
residual = @(dobs, d, a) scalefn(dobs, a) - scalefn(d, a);
data_norm = @(r) 0.5 * norm(r)^2;
objective_function = @(r) data_norm(r);
% Transform model parameters, here we take the natural log of rho
%
log_p = log(rho);
% Calculate response of starting model
%
d = getData(rho);
% Calculate data residual for starting model
%
data_residual = residual(dobs, d, a);
obj_fn_current = ...
data_norm(data_residual);
% Initialize GN monitoring
%
obj_fn = zeros(max_gn_iterations, 1);
lambda_history = zeros(max_gn_iterations, 1);
norm_gc = Inf();
obj_fn_diff = Inf();
gn_it_counter = 0;
obj_fn(1) = obj_fn_current;
% Gauss-Newton iterations start here.
% Iterations will be carried out as long as
%
% - the norm of the gradient J^T * r is larger than a predefined tolerance
% - maximum iteration is not reached
% - the norm of the objective function is larger than a predefined value
% - the difference between subsequent doesn't fall below a threshold.
%
while (norm_gc > tol) && ...
(gn_it_counter < max_gn_iterations) && ...
(obj_fn_current > obj_fn_goal) && ...
(obj_fn_diff > goal_obj_diff)
%
% We have data d, current model log_p, and have evaluated the objective
% function.
%
% Jacobian is defined as J = du/dm * dm / drho.
%
J = getJ('data', d, ...
'rho', exp(log_p), ...
'thickness', thk, ...
'times', t, ...
'obs', r, ...
'pert', pert, ...
'scale_asinh', a, ...
'protem', false);
% Restrict problem to "active" model parameters by only keeping associated columns of J
%
J = J * P';
% Gradient J^T * r = -\grad \Phi
%
gc = J' * data_residual;
norm_gc = norm(gc);
% GN search direction, restricted to active parameters
%
gn_dir = P' * ((J' * J + lambda * WTW) \ gc);
if verbose
fprintf('It. %3d: ', gn_it_counter);
fprintf('JTr: %1.6e ', norm_gc);
end
% Line Search:
% Take a fraction of the GN direction and evaluate the response
%
step_size = 1.0;
log_p_proposed = log_p + step_size * gn_dir;
d_proposed = getData(exp(log_p_proposed));
obj_fn_proposed = objective_function(...
residual(dobs, d_proposed, a));
obj_fn_diff = obj_fn_current - obj_fn_proposed;
% Directional derivative for Armijo rule
% must be negative for descent direction
dir_deriv = -gn_dir' * (P' * gc);
gn_it_counter = gn_it_counter + 1;
while (obj_fn_diff < -c1 * step_size * dir_deriv)
step_size = step_size / 2.0;
if step_size < step_size_min
error('Line search breakdown. Exit.');
end
log_p_proposed = log_p + step_size * gn_dir;
d_proposed = getData(exp(log_p_proposed));
obj_fn_proposed = objective_function(...
residual(dobs, d_proposed, a));
obj_fn_diff = obj_fn_current - obj_fn_proposed;
end
obj_rel_drop = obj_fn_diff / obj_fn_current;
% Update parameter after line search
%
rho = exp(log_p_proposed);
log_p = log_p_proposed;
% Update current model response, data residual, and objective function
%
d = d_proposed;
data_residual = residual(dobs, d, a);
obj_fn_current = obj_fn_proposed;
% Decrease regularization parameter lambda whenever the relative change
% of the objective function falls below a given drop tolerance.
% In the first Gauss-Newton iteration, the given value of lambda should
% always be accepted.
%
if obj_rel_drop < lambda_droptol && gn_it_counter > 1
lambda = lambda * lambda_scaling_factor;
end
% Reject further decrease of lambda when a given threshold is reached
%
if lambda < lambda_min
lambda = lambda_min;
end
% Book-keeping
%
lambda_history(gn_it_counter) = lambda;
obj_fn(gn_it_counter) = obj_fn_current;
if verbose
fprintf('LS: alpha=%1.3e lambda %1.3e obj_fn %1.3e obj_rel_drop %1.3e\n', ...
step_size, lambda, obj_fn_current, obj_rel_drop);
figure(1);
loglog(t, abs(dobs), t, abs(d));
plotTransient(t, dobs, [], 'r');
hold on
plotTransient(t, d, [], 'b');
legend('dobs', '', 'd', '');
hold off
set(gcf, 'position', [1 50 560 420]);
figure(2);
plot_model(rho, thk);
set(gcf, 'Position', [613 50 560 419]);
drawnow();
end
end
% Return values of model parameters, objective function convergence, and
% change of lambda during iterations
%
m = rho;
lambda_history = lambda_history(1:gn_it_counter);
obj_fn = obj_fn(1:gn_it_counter);
% EOF