-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInversion_IP.py
298 lines (226 loc) · 9.14 KB
/
Inversion_IP.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
#!/usr/bin/env python
# coding: utf-8
# Author: David Caterina - ULiège (2022)
# # Inversion of IP data
# In[63]:
# Common imports
import numpy as np
from matplotlib import pyplot as plt
import pygimli as pg
from pygimli.physics.ert import createGeometricFactors, ERTManager
from pygimli.meshtools import createMesh
import pybert as pb
import os.path
from copy import deepcopy
plt.rcParams['figure.dpi'] = 300
plt.rcParams['savefig.dpi'] = 300
# ### Defining some usefull functions
# In[64]:
def plotCompareResults(data, invResults):
relError = ((abs(data['rhoa']-invResults))/data['rhoa'] * 100) # Relative error in % for each data point
figComp = plt.figure(figsize=(10,5))
ax2 = figComp.add_subplot(122)
ax2.plot(data['rhoa'], invResults, 'bo', label='Full Data')
ax2.axline((0,0), slope=1, color='k', label='Unity line')
ax2.set_xlabel('Measured Apparent resistivities [Ohm.m]')
ax2.set_ylabel('Computed Apparent resistivities [Ohm.m]')
ax1 = figComp.add_subplot(121)
ax1.hist(relError, color='b')
ax1.set_xlabel('Relative error [%]')
ax1.set_ylabel('Number of data points')
plt.plot()
return
# In[65]:
def plotCompareResultsIP(data, invResults):
ma=data['ip']*0.001
relError = ((abs(ma-invResults))/ma * 100) # Relative error in % for each data point
figComp = plt.figure(figsize=(10,5))
ax2 = figComp.add_subplot(122)
ax2.plot(ma, invResults, 'bo', label='Full Data')
ax2.axline((0,0), slope=1, color='k', label='Unity line')
ax2.set_xlabel('Measured Apparent chargeability [mV/V]')
ax2.set_ylabel('Computed Apparent resistivities [mV/V]')
ax1 = figComp.add_subplot(121)
ax1.hist(relError, color='b')
ax1.set_xlabel('Relative error [%]')
ax1.set_ylabel('Number of data points')
plt.plot()
return
# ### File system and names
# In[66]:
plt.close('all')
directory = './P1/Pygimli/Orozco-slater/'
dataFile = 'P1.ohm'
saveDir = 'Results/'
if os.path.exists(directory+"TrimmedDataset.ohm"): # check if TrimmedDataset.ohm exists
os.remove(directory+"TrimmedDataset.ohm")
if not os.path.isdir(directory+saveDir):
os.mkdir(directory+saveDir)
xlabel="Distance (m)"
ylabel="Elevation (m)"
# ### Base inversion and parameters
# In[68]:
# Loading the dataset
tdip = pb.TDIPdata(directory+dataFile)
data = tdip.data
# meshInit = createMesh(data.sensors())
if not data.allNonZero('k'):
data['k'] = createGeometricFactors(data, numerical=True)
if not data.allNonZero('rhoa'):
data['rhoa'] = data['r']*data['k']
# Remove negative apparent resistivity
dataLength0=len(data["rhoa"])
data.remove(data["rhoa"]<=0)
dataLength1=len(data["rhoa"])
print(f'{dataLength0-dataLength1} data with negative resistivity, removed from the dataset')
if not data.allNonZero('err'):
absoluteError = 0.001
relativeError = 0.03
data['err'] = relativeError + absoluteError / data['rhoa'] # In % of the resistance
dataErr=deepcopy(np.asarray((data['err'])))
mgr = pb.ERTManager(data)
# Mesh creation
paraDepth = 20 # Depth to obtain at any points on the profile
quality = 33.6
paraMaxCellSize = 0.5
paraDX = 0.3
# Changes to the paraDepth
positions = data.sensors()
meshInit = mgr.createMesh(data, paraDepth=paraDepth, quality=quality-2, paraMaxCellSize=paraMaxCellSize*4, paraDX=paraDX*3)
mgr.setMesh(meshInit)
pg.show(mgr.paraDomain)
# ### Inversion of the data (round 1)
# In[69]:
# # Invert resistivity
lam = 20
verbose = True
robustData = True
chi1Opt = False # Optimize lambda for chi2 = 1
zWeight = 1.0
blockyModel = True
tdip.invertRhoa(mesh=meshInit,lam=lam,robustData=robustData,verbose=verbose,maxIter=1)
ertMgr = tdip.ERT
inv = ertMgr.fw.inv
inv.setModel(pg.Vector(len(tdip.res),np.abs(np.median(tdip.data["rhoa"]))))
inv.setReferenceModel(pg.Vector(len(tdip.res),np.abs(np.median(tdip.data["rhoa"]))))
inv.setRobustData(robustData)
inv.setVerbose(verbose)
inv.setLambda(lam)
inv.setBlockyModel(blockyModel)
inv.setDeltaPhiAbortPercent(1)
inv.setRelativeError(pg.Vector(dataErr))
#inv.setAbsoluteError(dataErr)
tdip.res = inv.run()
tdip.showResistivity(cMap='jet')
data["err"]=deepcopy(np.asarray((dataErr))) # apparently invertRhoa modifies the error column. We reinitialize to the original values here (useful if a trimmed dataset is saved, see after)
# ### Using this inversion, we filter the data and re-run the inversion on a finer mesh (round 2)
# In[70]:
simData=ertMgr.inv.response
obsData=data["rhoa"]
relError = ((abs(obsData-simData))/obsData * 100) # Relative error in % for each data point
cutOffError=5 # Maximum accepted relative error on the reconstruction (%)
plotCompareResults(data, simData)
if inv.relrms() > cutOffError:
print(f'The inversion run leads to a relative rms of {inv.relrms()} % (chi² = {inv.chi2()}) in {inv.iter()} iterations. \nThis is too high, the dataset will be filtered')
idxRemove = relError > cutOffError
idxKeep = [not(i) for i in idxRemove]
print(f'Out of the initial {len(idxRemove)} data points, {sum(idxKeep)} are kept for inversion.')
data.remove(idxRemove)
data.save(directory + 'TrimmedDataset.ohm')
idxKeep = [not(i) for i in idxRemove]
relErrorBis = ((abs(data['rhoa']-np.asarray(inv.response())[idxKeep]))/data['rhoa'] * 100)
plotCompareResults(data, np.asarray(inv.response())[idxKeep])
else:
print(f'The inversion run leads to a relative rms of {inv.relrms()} % (chi² = {inv.chi2()}) in {inv.iter()} iterations.')
# Re-run the inversion on a finner mesh:
#mgr.setMesh(meshFine)
#pg.show(mgr.paraDomain)
if os.path.exists(directory+"TrimmedDataset.ohm"): # check if TrimmedDataset.ohm exists
tdip = pb.TDIPdata(directory+"TrimmedDataset.ohm")
data = tdip.data
dataErr=deepcopy(np.asarray(data["err"])) # define the error
else:
tdip = pb.TDIPdata(directory+dataFile)
data=tdip.data
if not data.allNonZero('k'):
data['k'] = createGeometricFactors(data, numerical=True)
if not data.allNonZero('rhoa'):
data['rhoa'] = data['r']*data['k']
if not data.allNonZero('err'):
absoluteError = 0.001
relativeError = 0.03
data['err'] = relativeError + absoluteError / data['rhoa'] # In % of the resistance
dataErr=deepcopy(np.asarray(data["err"])) # define the error
else:
print(data["err"])
dataErr=deepcopy(np.asarray(data["err"])) # define the error
mgr = pb.ERTManager(data)
meshFine = mgr.createMesh(data, paraDepth=paraDepth, quality=quality, paraMaxCellSize=paraMaxCellSize, paraDX=paraDX)
mgr.setMesh(meshFine)
tdip.invertRhoa(mesh=meshFine,lam=lam,robustData=robustData,verbose=verbose,maxIter=1)
ertMgr = tdip.ERT
inv = ertMgr.fw.inv
inv.setRobustData(robustData)
inv.setVerbose(verbose)
inv.setModel(pg.Vector(len(tdip.res),np.abs(np.median(tdip.data["rhoa"]))))
inv.setReferenceModel(pg.Vector(len(tdip.res),np.abs(np.median(tdip.data["rhoa"]))))
inv.setLambda(lam)
inv.setRelativeError(pg.Vector(dataErr))
inv.setBlockyModel(blockyModel)
inv.setDeltaPhiAbortPercent(1)
tdip.res = inv.run()
# ### Display final resistivity model and fit
# In[71]:
RMSERT=f'The inversion run of ERT data leads to a relative rms of {inv.relrms()} % (chi² = {inv.chi2()}) in {inv.iter()} iterations.'
print(RMSERT)
tdip.coverage = ertMgr.coverage()
tdip.pd = ertMgr.paraDomain
tdip.showResistivity(cMap='jet',cMin=10,cMax=5000,xlabel=xlabel, ylabel=ylabel)
plt.savefig(directory+saveDir+'Resistivity_model.png',transparent=True)
simData=ertMgr.inv.response
obsData=data["rhoa"]
plotCompareResults(data, simData)
plt.savefig(directory+saveDir+'Resistivity_fit.png',transparent=True)
# ### Inversion chargeability with controlled parameters
# In[73]:
ma = tdip.data['ip']*0.001 # chargeability expressed in V/V
fop = tdip.ERT.fop
mesh = tdip.ERT.mesh
res = tdip.res
fIp = pb.tdip.mipmodelling.DCIPMModelling(fop, mesh, res)
if fop.regionManager().regionCount() > 1:
fIp.region(1).setBackground(True)
fIp.region(2).setConstraintType(1)
fIp.regionManager().setZWeight(zWeight)
INV = pg.core.RInversion(ma, fIp, True, False)
tD, tM = pg.core.RTrans(), pg.core.RTransLogLU(0.0001, 0.999)
INV.setTransData(tD)
INV.setTransModel(tM)
mstart = pg.Vector(len(res), np.abs(pg.median(ma)))
INV.setModel(mstart)
if not data.allNonZero('ipErr'):
INV.setAbsoluteError(np.ones_like(res)*0.002)
print('No IP error available, default value of 0.002 V/V used')
else:
print('IP error available')
INV.setAbsoluteError(tdip.data['ipErr']*0.001)
INV.setLambda(lam)
INV.setRobustData(robustData)
INV.setBlockyModel(blockyModel)
INV.setDeltaPhiAbortPercent(1)
tdip.m = INV.run()
tdip.mafwd = INV.response()
tdip.showChargeability(cMap='jet',cMin = 0, cMax = 25,xlabel=xlabel, ylabel=ylabel)
plt.savefig(directory+saveDir+'Chargeability_model.png',transparent=True)
RMSIP=f'The inversion run of IP data leads to a relative rms of {INV.relrms()} % (chi² = {INV.chi2()}) in {INV.iter()} iterations.'
print(RMSIP)
RMS=[RMSERT,RMSIP]
with open(directory+saveDir+'RMSResults.txt',"w") as f:
for s in RMS:
f.write(str(s)+"\n")
# ### Export of results in vtk
# In[74]:
tdip.pd.addData("Resistivity/Ohmm", tdip.res)
tdip.pd.addData("coverage(log10)", tdip.coverage)
tdip.pd.addData("Phase/mrad", tdip.m*1000)
tdip.pd.exportVTK(directory+saveDir+'dcinv.result')