Exemple #1
0
def _invert_(fName, mesh, lam=20, absoluteError=0.0005, relativeError=0.05, opt=False, chi2_lims=(0.9, 1.1), ref=False, ref_fName=None, i_max=10, blocky=False):
    """ args are passed to ERTManager, run optimize inversion, and saves to para_mesh """
    opening_string = '\n\n\nfName: {}\nOpt: {}\nlam: {}\n'.format(fName, opt, lam)
    print(opening_string)
    data = pg.load(fName)
    ertmgr = ert.ERTManager()
    data['err'] = ertmgr.estimateError(data, absoluteError=absoluteError, relativeError=relativeError)
    mesh = pg.load(mesh)
    ertmgr.fop.setMesh(mesh)
    ertmgr.inv.inv.setBlockyModel(blocky)

    if opt:
        chi_min = chi2_lims[0]
        chi_max = chi2_lims[1]
    else:
        chi_min = 0
        chi_max = np.inf

    print('ref is: ', ref)
    if ref:
        ref_model_vtk = pg.load(ref_fName)
        ref_model = ref_model_vtk.exportData('res')
        smr = True
    else:
        model = np.ones(len(mesh.cells()))
        smr = False

    print('preparing for inversion while loop')
    lam_record = []
    chi2_record = []
    model_record = []
    chi2 = -1

    while not chi_min < chi2 < chi_max:
        if len(chi2_record) > i_max:
            raise ValueError("optimization reached maximum number of runs, i_max: ", i_max)
        if chi2_record:
            lam = update_lam(chi2_record, lam_record)

        if ref:
            _ = ertmgr.invert(
                data=data, lam=lam,
                startModel=ref_model, startModelIsReference=smr,
                verbose=True,
            )
        else:
            _ = ertmgr.invert(
                data=data, lam=lam,
                verbose=True,
            )

        chi2 = ertmgr.inv.chi2()
        chi2_record.append(chi2)
        model = ertmgr.inv.model
        model_record.append(model)
        lam_record.append(lam)
        print('chi2 record: ', chi2_record)
        print('lambda record: ', lam_record)
    return(ertmgr, lam_record, chi2_record, model_record)
# Optional: you can filter all values and tokens in the data container.
# Its possible that there are some negative data values due to noise and
# huge geometric factors. So we need to remove them.
data.remove(data['rhoa'] < 0)
pg.info('Filtered rhoa (min/max)', min(data['rhoa']), max(data['rhoa']))

# You can save the data for further use
data.save('simple.dat')

# You can take a look at the data
ert.show(data)

###############################################################################
# Initialize the ERTManager, e.g. with a data container or a filename.
#
mgr = ert.ERTManager('simple.dat')
###############################################################################
# Run the inversion with the preset data. The Inversion mesh will be created
# with default settings.
inv = mgr.invert(lam=20, verbose=True)
# np.testing.assert_approx_equal(mgr.inv.chi2(), 1.049145, significant=3)

###############################################################################
# Let the ERTManger show you the model of the last successful run and how it
# fits the data. Shows data, model response, and model.
#
mgr.showResultAndFit()
meshPD = pg.Mesh(mgr.paraDomain)  # Save copy of para mesh for plotting later
# %%
# You can also provide your own mesh (e.g., a structured grid if you like them)
#
###############################################################################
# Get some example data with topography, typically by a call like
# data = ert.load("filename.dat")
# that supports various file formats
data = pg.getExampleFile('ert/slagdump.ohm', load=True, verbose=True)
print(data)

###############################################################################
# The data file does not contain geometric factors (token field 'k'),
# so we create them based on the given topography.
data['k'] = ert.createGeometricFactors(data, numerical=True)

###############################################################################
# We initialize the ERTManager for further steps and eventually inversion.
mgr = ert.ERTManager(sr=False)

###############################################################################
# It might be interesting to see the topography effect, i.e the ratio between
# the numerically computed geometry factor and the analytical formula
k0 = ert.createGeometricFactors(data)
ert.showData(data, vals=k0 / data['k'], label='Topography effect')

###############################################################################
# The data container has no apparent resistivities (token field 'rhoa') yet.
# We can let the Manager fix this later for us (as we now have the 'k' field),
# or we do it manually.
mgr.checkData(data)
print(data)

###############################################################################