def calcERT(ertScheme, rhoa): ert = ERT(verbose=False) solutionName = createCacheName('ERT') + "-" + str(ertScheme.size()) + \ "-" + str(len(rhoa)) try: ertModels = pg.load(solutionName + '.bmat') ertMesh = pg.load(solutionName + '.bms') except Exception as e: print(e) print("Building .... ") ertModels = ert.invert(ertScheme, values=rhoa, maxiter=10, lambd=50, paraDX=0.5, paraDZ=0.5, nLayers=20, paraDepth=15, verbose=1) ertMesh = ert.fop.regionManager().paraDomain() ertModels.save(solutionName + '.bmat') ertMesh.save(solutionName) ertRatioModels = pg.RMatrix(ertModels) for i in range(len(ertModels)): ertRatioModels[i] /= ertModels[0] return ertMesh, ertModels, ertRatioModels
def createAnimation(out, stream=False): mesh = pg.load(out + '.bms') meshC = pg.load(out + 'C.bms') densMatrix = pg.load(out + 'density.bmat') vels = np.load(out + 'velo.bmat.npy') cMin = 9e99 cMax = -9e99 for d in densMatrix: cMin = min(cMin, pg.min(d)) cMax = max(cMax, pg.max(d)) if stream: pg.viewer.mpl.saveAnimation(mesh, densMatrix, out + '-stream', plc=None, label='', cMin=cMin, cMax=cMax, logScale=False, cmap=None, vData=vels, coarseMesh=meshC, color='white') else: pg.viewer.mpl.saveAnimation(mesh, densMatrix, out, plc=None, label='', cMin=cMin, cMax=cMax, logScale=False, cmap=None)
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)
def test_meshIO(self): # text bms version v3 which stores geometry flag mesh = pg.Mesh(2, isGeometry=True) mesh.save('_tmp') mesh2 = pg.load('_tmp.bms', verbose=True) self.assertEqual(mesh.isGeometry(), mesh2.isGeometry())
def setMesh(self, mesh, refine=True): """Set the internal mesh for this Manager. Inject the mesh in the internal fop und inv. Initialize RegionManager. For more than two regions the first is assumed to be background. Optional the forward mesh can be refined for higher numerical accuracy. Parameters ---------- DOCUMENTME!!! """ if isinstance(mesh, str): mesh = pg.load(mesh) if self.verbose: print(mesh) self.mesh = pg.Mesh(mesh) self.mesh.createNeighbourInfos() self.fop.setMesh(self.mesh) self.fop.regionManager().setConstraintType(1) if self.fop.regionManager().regionCount() > 1: self.fop.regionManager().region(1).setBackground(True) # self.fop.regionManager().regions().begin().second.setBackground(1) self.fop.createRefinedForwardMesh(refine) self.paraDomain = self.fop.regionManager().paraDomain() self.inv.setForwardOperator(self.fop) # necessary? CR: check this
def showModel(outPath): # geom = pg.load('synth/' + 'synthGeom.bms') # pd = pg.load(outPath + '/paraDomain.bms') paraMesh = pg.load(outPath + '/paraMesh.bms') model = pg.load(outPath + "/model.vector") fopMesh = pg.load(outPath + "fopMesh.bms") fopModel = pg.load(outPath + "fopModel.vector") allModel = np.zeros(len(model) + 2) allModel[2:] = model # allModel[0] = fopModel[fopMesh.findCellByMarker( # pg.MARKER_FIXEDVALUE_REGION - 0)[0].id()] allModel[1] = fopModel[fopMesh.findCellByMarker( pg.MARKER_FIXEDVALUE_REGION - 1)[0].id()] ax = savefig(paraMesh, None, allModel[paraMesh.cellMarkers()], 'Hydraulic conductivity $K$ in m$/$s', cMin=1e-5, cMax=1e-2, nLevs=4, cMap='viridis') pg.wait() pg.show(fopMesh, ax=ax, linewidth=0.2) paraMesh.createNeighborInfos() bs = [] for b in paraMesh.boundaries(): if b.leftCell() and b.rightCell(): if b.leftCell().marker() > 1 or b.rightCell().marker() > 1: bs.append(b) pg.viewer.mpl.drawSelectedMeshBoundaries(ax, bs, color=(0.0, 0.0, 0.0, 1.0), linewidth=1.0) pg.viewer.mpl.saveFigure(ax.figure, 'hydrInversionModel') print('Done!') pg.wait()
def test_meshBMS(self): # text bms version v3 which stores geometry flag mesh = pg.Mesh(2, isGeometry=True) import tempfile as tmp _, fn = tmp.mkstemp() mesh.save(fn) mesh2 = pg.load(fn + '.bms', verbose=True) self.assertEqual(mesh.isGeometry(), mesh2.isGeometry())
def exportSens(meshfile, sensMatrix, outFilename, dataID, tol=1e-5, save=False, saveTo='.'): mesh = g.Mesh(meshfile) mesh.showInfos() S = g.RMatrix(); g.load(S, sensMatrix) print(S.rows()) print(S.cols()) savePath = os.path.abspath(saveTo) try: os.makedirs(savePath) except OSError: if os.path.exists(savePath): # We are nearly safe pass else: # There was an error on creation, so make sure we know about it raise if dataID == -1: for i in range(0, S.rows()): d = b.prepExportSensitivityData(mesh , S[i], tol) name = os.path.join(savePath, outFilename + "-" + str(i) + ".vec") if save: print(name) g.save(d, name) mesh.addExportData(name, d) else: d = b.prepExportSensitivityData(mesh , S[dataID], tol) name = os.path.join(savePath, outFilename + "-" + str(dataID) + ".vec") if save: print(name) g.save(d, name) mesh.addExportData(name, d) mesh.exportVTK(outFilename)
def showModel(outPath): # geom = pg.load('synth/' + 'synthGeom.bms') # pd = pg.load(outPath + '/paraDomain.bms') paraMesh = pg.load(outPath + '/paraMesh.bms') model = pg.load(outPath + "/model.vector") fopMesh = pg.load(outPath + "fopMesh.bms") fopModel = pg.load(outPath + "fopModel.vector") allModel = np.zeros(len(model)+2) allModel[2:] = model # allModel[0] = fopModel[fopMesh.findCellByMarker( # pg.MARKER_FIXEDVALUE_REGION - 0)[0].id()] allModel[1] = fopModel[fopMesh.findCellByMarker( pg.MARKER_FIXEDVALUE_REGION - 1)[0].id()] ax = savefig(paraMesh, None, allModel[paraMesh.cellMarkers()], 'Hydraulic conductivity $K$ in m$/$s', cMin=1e-5, cMax=1e-2, nLevs=4, cMap='viridis') pg.wait() pg.show(fopMesh, ax=ax, linewidth=0.2) paraMesh.createNeighbourInfos() bs = [] for b in paraMesh.boundaries(): if b.leftCell() and b.rightCell(): if b.leftCell().marker() > 1 or b.rightCell().marker() > 1: bs.append(b) pg.mplviewer.drawSelectedMeshBoundaries( ax, bs, color=(0.0, 0.0, 0.0, 1.0), linewidth=1.0) pg.mplviewer.saveFigure(ax.figure, 'hydrInversionModel') print('Done!') pg.wait()
def generateDataPDF(data, filename="data.pdf"): """Generate a multi-page pdf showing all data properties.""" if isinstance(data, str): filename = data.replace('.txt', '-data.pdf') data = pg.load(data) with PdfPages(filename) as pdf: fig, ax = plt.subplots() for tok in data.tokenList().split(): if data.haveData(tok): pg.show(data, tok, ax=ax, label=tok) fig.savefig(pdf, format='pdf') ax.cla()
def createAnimation(out, stream=False): mesh = pg.load(out + '.bms') meshC = pg.load(out + 'C.bms') densMatrix = pg.load(out + 'density.bmat') vels = np.load(out+'velo.bmat.npy') cMin= 9e99 cMax= -9e99 for d in densMatrix: cMin = min(cMin, pg.min(d)) cMax = max(cMax, pg.max(d)) if stream: pg.mplviewer.saveAnimation(mesh, densMatrix, out + '-stream', plc=None, label='', cMin=cMin, cMax=cMax, logScale=False, cmap=None, vData=vels, coarseMesh=meshC, color='white' ) else: pg.mplviewer.saveAnimation(mesh, densMatrix, out, plc=None, label='', cMin=cMin, cMax=cMax, logScale=False, cmap=None)
def getExampleFile(path, load=False, verbose=False): """Download and return a filename to the example repository. TODO: checksum or hash test for the content. Parameters ---------- path: str Path to the remote repo load: bool [False] Try to load the file and return the relating object. Returns ------- filename: str Filename to the data content data: obj content of the path if load is True """ url = exampleDataRepository + path fileName = os.path.join(tempfile.gettempdir(), gimliExampleDataPath, path) if not os.path.exists(fileName): if verbose: pg.info("Getting:", fileName) os.makedirs(os.path.dirname(fileName), exist_ok=True) tmp = urlretrieve(url, fileName) else: if verbose: pg.info("File already exists:", fileName) if load: print(fileName) d = pg.load(fileName) return pg.load(fileName) return fileName
def calcERT(ertScheme, rhoa): ert = ERT(verbose=False) solutionName = createCacheName('ERT')+ "-" + str(ertScheme.size())+ "-" + str(len(rhoa)) try: ertModels = pg.load(solutionName + '.bmat') ertMesh = pg.load(solutionName + '.bms') except Exception as e: print(e) print("Building .... ") ertModels = ert.invert(ertScheme, values=rhoa, maxiter=10, lambd=50, paraDX=0.5, paraDZ= 0.5, nLayers=20, paraDepth=15, verbose=1) ertMesh=ert.fop.regionManager().paraDomain() ertModels.save(solutionName + '.bmat') ertMesh.save(solutionName) ertRatioModels = pg.RMatrix(ertModels) for i in range(len(ertModels)): ertRatioModels[i] /= ertModels[0] return ertMesh, ertModels, ertRatioModels
def test_VTK_DataRead(self): grid = pg.createGrid(np.arange(4), np.arange(3), np.arange(2)) cM = np.arange(grid.cellCount()) grid.setCellMarkers(cM) import tempfile as tmp _, fn = tmp.mkstemp(suffix='.vtk') grid.exportVTK(fn) mesh = pg.load(fn) np.testing.assert_array_equal(mesh.cellMarkers(), cM) np.testing.assert_array_equal(mesh['Marker'], cM) mesh = pg.meshtools.readMeshIO(fn) np.testing.assert_array_equal(mesh['Marker'], cM) fn = pg.getExampleFile('meshes/test_tetgen_dataCol.vtk') mesh = pg.load(fn) np.testing.assert_array_equal(mesh.cellMarkers(), cM) np.testing.assert_array_equal(mesh['Marker'], cM) mesh = pg.meshtools.readMeshIO(fn) np.testing.assert_array_equal(mesh['Marker'], cM)
def plot_inv(fvtk, fpng, cm, cM): mesh = pg.load(fvtk) # embed() rho = mesh.data('res') cov = mesh.data('cov') _ = plt.figure(figsize=(10, 5)) ax = plt.gca() ax, cbar = pg.show(mesh=mesh, data=rho, coverage=cov, ax=ax, hold=True, cMap='jet', cMin=cm, cMax=cM, colorBar=True, showelectrodes=True, label='resistivity [ohm m]') ax.set_xlabel('m') ax.set_ylabel('m') plt.tight_layout() plt.savefig(fpng, dpi=600)
def load(fileName, verbose=False, **kwargs): """Shortcut to load ERT data. Import Data and try to assume the file format. Additionally to unified data format we support the wide-spread res2dinv format as well as ASCII column files generated by the processing software of various instruments (ABEM LS, Syscal Pro, Resecs, ?) If this fails, install pybert and use its auto importer pybert.importData. Parameters ---------- fileName: str Returns ------- data: pg.DataContainer """ data = pg.load(fileName) if isinstance(data, pg.DataContainerERT): return data try: pg.info("could not read unified data format for ERT ... try res2dinv") data = importRes2dInv(fileName) return data except: pg.info("could not read res2dinv ... try Ascii columns") try: data = importAsciiColumns(fileName) return data except Exception as e: pg.info("Failed importing Ascii column file. Consider using pybert.") pg.info(e) if verbose: pg.info("Try to import using pybert .. if available") pb = pg.optImport('pybert') data = pb.loadData(fileName) if isinstance(data, pg.DataContainerERT): return data pg.critical("Can't import ERT data file.", fileName)
def simulateOld(mesh, scheme, res, sr=True, useBert=True, verbose=False, **kwargs): """ERT forward calculation. Convenience function to use the ERT modelling operator if you like static functions. See :py:mod:`pygimli.ert.ERTManager.simulate` for description of the arguments. Parameters ---------- mesh: :gimliapi:`GIMLI::Mesh` | str Modelling domain. Mesh can be a file name here. scheme: :gimliapi:`GIMLI::DataContainerERT` | str Data configuration. Scheme can be a file name here. res: see :py:mod:`pygimli.ert.ERTManager.simulate` Resistivity distribution. sr: bool [True] Use singularity removal technique. useBert: bool [True] Use Bert forward operator instead of the reference implementation. **kwargs: Forwarded to :py:mod:`pygimli.ert.ERTManager.simulate` """ from .ertManager import ERTManager ert = ERTManager(useBert=useBert, sr=sr, verbose=verbose) if isinstance(mesh, str): mesh = pg.load(mesh) if isinstance(scheme, str): scheme = pg.physics.ert.load(scheme) return ert.simulate(mesh=mesh, res=res, scheme=scheme, verbose=verbose, **kwargs)
def test_io_STL(self): try: import tempfile as tmp except ImportError: return str = r"""solid name facet normal 1.000000e+000 0.000000e+000 0.000000e+000 outer loop vertex 6.100000e+002 -1.046773e+002 -2.818708e+003 vertex 6.100000e+002 -1.249950e+002 -2.814064e+003 vertex 6.100000e+002 -2.507565e+000 -2.930000e+003 endloop endfacet endsolid solid name facet normal -2.568735e-007 1.396183e-002 -9.999025e-001 outer loop vertex 1.350000e+002 3.839764e+001 -2.929429e+003 vertex 6.100000e+002 7.930283e+001 -2.928858e+003 vertex 6.100000e+002 -2.507565e+000 -2.930000e+003 endloop endfacet endsolid""" _, fileName = tmp.mkstemp(suffix='.stl') fi = open(fileName, 'w') fi.write(str) fi.close() mesh = pg.load(fileName, verbose=True) np.testing.assert_equal(mesh.cellCount(), 0) np.testing.assert_equal(mesh.nodeCount(), 5) np.testing.assert_equal(mesh.boundaryCount(), 2) np.testing.assert_equal( np.array(pg.unique(pg.sort(mesh.boundaryMarkers()))), [0, 1]) try: os.remove(fileName) except: print("can't remove:", fileName)
def test_io_STL(self): try: import tempfile as tmp except ImportError: return str = r"""solid name facet normal 1.000000e+000 0.000000e+000 0.000000e+000 outer loop vertex 6.100000e+002 -1.046773e+002 -2.818708e+003 vertex 6.100000e+002 -1.249950e+002 -2.814064e+003 vertex 6.100000e+002 -2.507565e+000 -2.930000e+003 endloop endfacet endsolid solid name facet normal -2.568735e-007 1.396183e-002 -9.999025e-001 outer loop vertex 1.350000e+002 3.839764e+001 -2.929429e+003 vertex 6.100000e+002 7.930283e+001 -2.928858e+003 vertex 6.100000e+002 -2.507565e+000 -2.930000e+003 endloop endfacet endsolid""" _, fileName = tmp.mkstemp(suffix='.stl') fi = open(fileName, 'w') fi.write(str) fi.close() mesh = pg.load(fileName, verbose=True) np.testing.assert_equal(mesh.cellCount(), 0) np.testing.assert_equal(mesh.nodeCount(), 5) np.testing.assert_equal(mesh.boundaryCount(), 2) np.testing.assert_equal(np.array(pg.unique(pg.sort(mesh.boundaryMarkers()))), [0, 1]) try: os.remove(fileName) except: print("can't remove:", fileName)
def load(fileName, verbose=False, **kwargs): """Shortcut to load ERT data. Import Data and try to assume the file format. Use pybert importer if installed. Parameters ---------- fileName: str Returns ------- data: pg.DataContainer """ data = pg.load(fileName) if isinstance(data, pg.DataContainerERT): return data
""" Functions to call PyGimli from geotransdens authors: AAP, JHG 06-2020 """ import numpy as np import pygimli as pg import pybert as pb import time import csv from pygimli.utils import gmat2numpy global meshERT, meshTD meshERT = pg.load('ERTMESH.bms') start = time.time() meshTD = pg.load('HYDROMESH.vtk') end = time.time() #print("Time to load the VTK file and create TD mesh: "+str(end - start)+"seconds") def interpolant(): ''' Creates interpolant between ERT mesh and TD mesh''' start = time.time() interpMat = meshERT.interpolationMatrix(meshTD.positions()) end = time.time() print("Time to create interpolant " + str(end - start) + " seconds") return interpMat def interpolate_jacobian(inVector, interpolant=None):
def showSynthData(synthPath): geom = pg.load(synthPath + 'synthGeom.bms') mesh = pg.load(synthPath + 'synth.bms') k = np.load(synthPath + 'synthK.npy') vel = np.load(synthPath + 'synthVel.npy') sat = np.load(synthPath + 'synthSat.npy') scheme = pg.DataContainer(synthPath + 'synth.shm', 'a b m n') # rhoaR = np.load(synthPath + 'synthRhoaRatio.npy') # rhoa = np.load(synthPath + 'synthRhoa.npy') row = 3 col = 2 # START model perm + input savefig(mesh, geom, k, 'Hydraulic conductivity $K$ in m$/$s', out='hydrConductModel', cMin=1e-5, cMax=1e-2, nLevs=4, cmap='viridis') # START velocity axVel, _ = pg.show(mesh, np.sqrt(vel[0]**2 + vel[1]**2), logScale=0, colorBar=1, pad=0.55, label='Velocity $|v|$ in m$/$s', hold=1) meshC = pg.meshtools.createMesh(geom, quality=33, area=0.5, smooth=[1, 10]) pg.show(mesh, data=vel, ax=axVel, coarseMesh=meshC, color='black', linewidth=0.5, dropTol=1e-6) pg.show(geom, ax=axVel, fillRegion=False) saveAxes(axVel, 'hydrVelocity', adjust=True) # START Saturation axs = plt.subplots(row, col, sharex=True, sharey=True, figsize=(10. * 0.65, 7.25 * 0.65))[1].flatten() satScale = 0.001 for i, a in enumerate(axs): savefig( mesh, geom, sat[i * len(sat) / (len(axs)) + 1] * satScale, # /mesh.cellSizes(), label=None, out=None, cMin=0, cMax=2.5, ax=a, adjust=True) pg.viewer.mpl.drawSensors(a, scheme.sensorPositions(), diam=0.15, color='green') add_inner_title(a, "t = %d days" % days[i], 3, color="w") if i < (row - 1) * col: a.set_xlabel('') if i % col: a.set_ylabel('') a.set_ylim([-16, 0]) pg.viewer.mpl.saveFigure(axs[0].figure, "hydrSaturation") pg.viewer.mpl.createColorBarOnly(cMin=0, cMax=2.5, logScale=False, cMap='Spectral_r', nLevs=5, label=r'Concentration $c$ in g$/$l', orientation='horizontal', savefig='hydrSaturationCbar') # END Saturation pg.wait()
This tutorial shows ... """ import pygimli as pg import numpy as np ############################################################################### # data = pg.DataContainer() print(data) data.setSensorPosition(0, (0.0, 0.0, 0)) print(data) #define global datasize data.resize(4) data.set('a', [1.0, 2.0, 3.0, 4.0]) data.save("dummy.dat") data = pg.load("dummy.dat") print(data) filename = "dummy.dat" data = pg.load(filename) print(data) print(data('a'))
import matplotlib.pyplot as plt import numpy as np from matplotlib import ticker from mpl_toolkits.axes_grid1 import ImageGrid import pygimli as pg from fpinv import add_inner_title, logFormat, rst_cov, set_style from pygimli.viewer.mpl import drawModel from settings import plot_boreholes fs = 5.5 set_style(fs, style="seaborn-dark") # Load data mesh1 = pg.load("paraDomain_1.bms") mesh2 = pg.load("paraDomain_2.bms") est = np.load("conventional.npz") joint = np.load("joint_inversion_1.npz") joint2 = np.load("joint_inversion_2.npz") sensors = np.loadtxt("sensors.npy") def to_sat(fw, fi, fa, fr): phi = 1 - fr return fw / phi, fi / phi, fa / phi # Conventional inversion vel, rho, fa, fi, fw, cov = est["vel"], est["rho"], est["fa"], est["fi"], est[ "fw"], est["mask"]
def showVec(lastOnly, cmap): plt.close('all') meshInv = pg.load('mesh\meshParaDomain.bms') vectors = glob.glob("model_*.vector") # Get the final model vector(e.g. the highest number) v = np.zeros(len(vectors)) for s in enumerate(vectors): v[s[0]] = int(re.split(r'[_.]', s[1])[1]) i0 = vectors[np.where(v == 0)[0][0]] i1 = vectors[np.where(v == 1)[0][0]] iMax = vectors[np.where(v == v.max())[0][0]] if lastOnly == 1: vectors = iMax else: # Get the Initial model, first iteration, and last iteration only vectors = [i0, i1, iMax] print(meshInv.cellCount()) if type(vectors) == str: ax = plt.gca() datInv = pg.load(vectors) print(datInv.size()) dataplot, _ = pg.show(ax=ax, mesh=meshInv, data=datInv, cmap=cmap, showMesh=0, cMin=1, cMax=1000, colorBar=True) # Formatting ax.set_xlim(left=-10, right=375) ax.set_ylim(top=40, bottom=-50) ax.minorticks_on() ax.set_title(os.getcwd() + ' -- ' + vectors, loc='left') ax.set_ylabel('Elevation (mASL)') ax.set_xlabel('Distance (m)') dataplot.plot(dataplot.get_xlim(), [-30, -30], color='black') dataplot.plot(dataplot.get_xlim(), [0, 0], color='black') fig = plt.gcf() fig.set_size_inches(19, 5) fig.tight_layout() plt.show() if type(vectors) == list: fig, ax = plt.subplots(len(vectors), 1, sharex='all', sharey='all') for vecName in enumerate(vectors): print(vecName) # ax = plt.gca() datInv = pg.load(vecName[1]) print(datInv.size()) dataplot, cb = pg.show(ax=ax[vecName[0]], mesh=meshInv, data=datInv, cmap=cmap, showMesh=0, cMin=1, cMax=1000, colorBar=True) ax[vecName[0]].set_xlim(left=-10, right=450) ax[vecName[0]].set_ylim(top=40, bottom=-50) ax[vecName[0]].minorticks_on() ax[vecName[0]].set_title(os.getcwd() + ' -- ' + vecName[1], loc='left') ax[vecName[0]].set_ylabel('Elevation (mASL)') ax[vecName[0]].set_xlabel('Distance (m)') dataplot.plot(dataplot.get_xlim(), [-30, -30], color='black') dataplot.plot(dataplot.get_xlim(), [0, 0], color='black') fig = plt.gcf() fig.set_size_inches(15, 11) fig.tight_layout() meshInv.cellCount() == datInv.size() filename = os.path.basename(os.path.normpath(os.getcwd())) i = 0 while os.path.exists('{}{:d}.png'.format(filename, i)): i += 1 plt.savefig('{}_({:d}).png'.format(filename, i), dpi=300) plt.savefig('{}_({:d}).svg'.format(filename, i))
import pybert as pb import pygimli as pg # compute k, err, rhoa data = pb.DataContainerERT("ldp2.dat") data.set("k", pg.geometricFactors(data)) data.set( "err", pb.Resistivity.estimateError(data, absoluteUError=0.0001, relativeError=0.03)) data.set("rhoa", data("u") / data("i") * data("k")) # mesh mesh = pg.load("bukov.bms") # inversion res = pb.Resistivity(data) res.setMesh(mesh) #res.setMesh(mesh, refine=False) res.invert() #res.showResult() res.saveResult("in_python_res") # ve verzi 2.2.9 spadne, ale vysledky ulozi #pg.wait()
plt.savefig('{}_({:d}).svg'.format(filename, i)) # plt.savefig('{}{:d}.eps'.format(filename, i),format = 'eps',dpi=500) # manager.window.showMaximized() showVec(lastOnly=1, cmap='jet_r') #%% defMap = plt.get_cmap('jet_r') old_cdict = defMap._segmentdata new_cditc = { 'blue': [(0.0, 0.86, 0.86), (0.05, 0, 0), (0.35, 0, 0), (0.6599999999999999, 1, 1), (0.89, 1, 1), (1.0, 0.5, 0.5)], 'green': [(0.0, 0.62, 0.62), (0.05, 0, 0), (0.08999999999999997, 0, 0), (0.36, 1, 1), (0.625, 1, 1), (0.875, 0, 0), (1.0, 0, 0)], 'red': [(0.0, 0.86, 0.86), (0.05, 0.5, 0.5), (0.10999999999999999, 1, 1), (0.33999999999999997, 1, 1), (0.65, 0, 0), (1.0, 0, 0)] } my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', new_cditc, 256) meshInv = pg.load("mesh/meshParaDomain.bms") datInv = pg.load("model_15.vector") dataplot, _ = pg.show(mesh=meshInv, data=datInv, cmap=my_cmap, cMin=1, cMax=1000, colorBar=True)
import sys import numpy as np import pygimli as pg from fpinv import FourPhaseModel mesh = pg.load("mesh.bms") if len(sys.argv) > 1: pd = pg.load("paraDomain_2.bms") resinv = np.loadtxt("res_conventional_2.dat") vest = np.loadtxt("vel_conventional_2.dat") scenario = "Fig2" pg.boxprint(scenario) phi = 0.3 # Porosity assumed to calculate fi, fa, fw with 4PM else: pd = pg.load("paraDomain_1.bms") resinv = np.loadtxt("res_conventional_1.dat") vest = np.loadtxt("vel_conventional_1.dat") scenario = "Fig1" pg.boxprint(scenario) frtrue = np.load("true_model.npz")["fr"] phi = [] for cell in pd.cells(): idx = mesh.findCell(cell.center()).id() phi.append(1 - frtrue[idx]) phi = np.array(phi) # Save some stuff fpm = FourPhaseModel(phi=phi) fae, fie, fwe, maske = fpm.all(resinv, vest)
import numpy as np import pybert as pb import pygimli as pg import pygimli.meshtools as mt from pybert.manager import ERTManager from pygimli.physics import Refraction from pygimli.physics.traveltime import createRAData mesh = pg.load("mesh.bms") sensors = np.load("sensors.npy", allow_pickle=True) rhotrue = np.loadtxt("rhotrue.dat") veltrue = np.loadtxt("veltrue.dat") pg.boxprint("Simulate apparent resistivities") # Create more realistic data set ertScheme = pb.createData(sensors, "dd", spacings=[1, 2, 4]) k = pb.geometricFactors(ertScheme) ertScheme.markInvalid(pg.abs(k) > 5000) ertScheme.removeInvalid() ert = ERTManager() # Create suitable mesh for ert forward calculation # NOTE: In the published results paraMaxCellSize=1.0 was used, which is # increased here to allow testing on Continuous Integration services. meshERTFWD = mt.createParaMesh(ertScheme, quality=33.5, paraMaxCellSize=2.0,
dt = h * 0.5/max(velocities) times = np.arange(0.0, tmax, dt) print(mesh, "h:", h, "dt:", dt, "n_t", len(times)) uSource = ricker(f0, times, t0=1./f0) solutionName = 'uGridBig-'+ str(dt) + '-' + str(h) nx = len(x) ny = len(y) dt = times[1] - times[0] try: print("load:", solutionName) u = pg.load(solutionName + '.bmat') except Exception as e: print(e) u = solvePressureWave(mesh, velocities, times, sourcePos=(0.0, 0.0), uSource=uSource, verbose=10) u.save(solutionName + '.bmat') print(u) fig = plt.figure() ax = fig.add_subplot(1,1,1) Ustep = len(u)/2 field = drawWaveField(ax, u[Ustep, :]) plt.show()
pg.boxprint("%d / %d" % (i + 1, npar)) print(Fx.flat[i], end=" ") Fx1 = np.copy(Fx) Fx1.flat[i] += df dataERT1, dataSRT1 = forward4PM(meshERT, meshRST, schemeERT, schemeSRT, Fx1) jacERT[:, i] = (np.log(dataERT1('rhoa')) - np.log(dataERT('rhoa'))) / df / errorERT jacSRT[:, i] = (dataSRT1('t') - dataSRT('t')) / df / errorSRT print("ready.") return jacERT, jacSRT # load synthetic mesh mesh = pg.load("mesh.bms") meshRST = pg.load("paraDomain_2.bms") meshRST.createSecondaryNodes(3) meshERT = pg.load("meshERT_2.bms") for cell in meshRST.cells(): NN = mesh.findCell(cell.center()) cell.setMarker(mesh.cellMarkers()[NN.id()]) for cell in meshERT.cells(): NN = mesh.findCell(cell.center()) if NN: cell.setMarker(mesh.cellMarkers()[NN.id()]) else: cell.setMarker(len(np.unique(mesh.cellMarkers()))) # triangle boundary
dt = h * 0.5 / max(velocities) times = np.arange(0.0, tmax, dt) print(mesh, "h:", h, "dt:", dt, "n_t", len(times)) uSource = ricker(f0, times, t0=1. / f0) solutionName = 'uGridBig-' + str(dt) + '-' + str(h) nx = len(x) ny = len(y) dt = times[1] - times[0] try: print("load:", solutionName) u = pg.load(solutionName + '.bmat') except Exception as e: print(e) u = solvePressureWave(mesh, velocities, times, sourcePos=(0.0, 0.0), uSource=uSource, verbose=10) u.save(solutionName + '.bmat') print(u) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) Ustep = len(u) / 2
def calcSeismics(meshIn, vP): """Do seismic computations.""" meshSeis = meshIn.createH2() meshSeis = mt.appendTriangleBoundary( meshSeis, xbound=25, ybound=22.0, marker=1, quality=32.0, area=0.3, smooth=True, markerBoundary=1, isSubSurface=False, verbose=False) print(meshSeis) meshSeis = meshSeis.createH2() meshSeis = meshSeis.createH2() # meshSeis = meshSeis.createP2() meshSeis.smooth(1, 1, 1, 4) vP = pg.interpolate(meshIn, vP, meshSeis.cellCenters()) mesh = meshSeis vP = pg.solver.fillEmptyToCellArray(mesh, vP) print(mesh) # ax, cbar = pg.show(mesh, data=vP) # pg.show(mesh, axes=ax) geophPointsX = np.arange(-19, 19.1, 1) geophPoints = np.vstack((geophPointsX, np.zeros(len(geophPointsX)))).T sourcePos = geophPoints[4] c = mesh.findCell(sourcePos) h1 = pg.findBoundary(c.boundaryNodes(0)).size() h2 = pg.findBoundary(c.boundaryNodes(1)).size() h3 = pg.findBoundary(c.boundaryNodes(2)).size() print([h1, h2, h3]) h = pg.median([h1, h2, h3]) # h = pg.median(mesh.boundarySizes()) f0scale = 0.25 cfl = 0.5 dt = cfl * h / max(vP) print("Courant-Friedrich-Lewy number:", cfl) tmax = 40./min(vP) times = np.arange(0.0, tmax, dt) solutionName = createCacheName('seis', mesh, times) + "cfl-" + str(cfl) try: # u = pg.load(solutionName + '.bmat') uI = pg.load(solutionName + 'I.bmat') except Exception as e: print(e) f0 = f0scale * 1./dt print("h:", round(h, 2), "dt:", round(dt, 5), "1/dt:", round(1/dt, 1), "f0", round(f0, 2), "Wavelength: ", round(max(vP)/f0, 2), " m") uSource = ricker(times, f0, t0=1./f0) plt.figure() plt.plot(times, uSource, '-*') plt.show(block=0) plt.pause(0.01) u = solvePressureWave(mesh, vP, times, sourcePos=sourcePos, uSource=uSource, verbose=10) u.save(solutionName) uI = pg.RMatrix() print("interpolate node to cell data ... ") pg.interpolate(mesh, u, mesh.cellCenters(), uI) print("... done") uI.save(solutionName+'I') # nodes = [mesh.findNearestNode(p) for p in geophPoints] # fig = plt.figure() # axs = fig.add_subplot(1,1,1) # drawSeismogramm(axs, mesh, u, nodes, dt, i=None) # plt.show() dpi = 92 scale = 1 fig = plt.figure(facecolor='white', figsize=(scale*800/dpi, scale*490/dpi), dpi=dpi) ax = fig.add_subplot(1, 1, 1) gci = pg.mplviewer.drawModel(ax, mesh, data=uI[0], cMin=-1, cMax=1, cmap='bwr') pg.mplviewer.drawMeshBoundaries(ax, meshIn, hideMesh=1) ax.set_xlim((-20, 20)) ax.set_ylim((-15, 0)) ax.set_ylabel('Depth [m]') ax.set_xlabel('$x$ [m]') ticks = ax.yaxis.get_majorticklocs() tickLabels = [] for t in ticks: tickLabels.append(str(int(abs(t)))) ax.set_yticklabels(tickLabels) plt.tight_layout() # ax, cbar = pg.show(mesh, data=vP) # pg.showNow() # ax = fig.add_subplot(1,1,1) def animate(i): i = i*5 if i > len(uI)-1: return print("Frame:", i, "/", len(uI)) ui = uI[i] ui = ui / max(pg.abs(ui)) ui = pg.logDropTol(ui, 1e-2) cMax = max(pg.abs(ui)) pg.mplviewer.setMappableData(gci, ui, cMin=-cMax, cMax=cMax, logScale=False) # plt.pause(0.001) anim = animation.FuncAnimation(fig, animate, frames=int(len(uI)/5), interval=0.001, repeat=0) # , blit=True) out = 'seis' + str(f0scale) + "cfl-" + str(cfl) anim.save(out + ".mp4", writer=None, fps=20, dpi=dpi, codec=None, bitrate=24*1024, extra_args=None, metadata=None, extra_anim=None, savefig_kwargs=None) try: print("create frames ... ") os.system('mkdir -p anim-' + out) os.system('ffmpeg -i ' + out + '.mp4 anim-' + out + '/movie%d.jpg') except: pass
def calcSeismics(meshIn, vP): """Do seismic computations.""" meshSeis = meshIn.createH2() meshSeis = mt.appendTriangleBoundary(meshSeis, xbound=25, ybound=22.0, marker=1, quality=32.0, area=0.3, smooth=True, markerBoundary=1, isSubSurface=False, verbose=False) print(meshSeis) meshSeis = meshSeis.createH2() meshSeis = meshSeis.createH2() # meshSeis = meshSeis.createP2() meshSeis.smooth(1, 1, 1, 4) vP = pg.interpolate(meshIn, vP, meshSeis.cellCenters()) mesh = meshSeis vP = pg.solver.fillEmptyToCellArray(mesh, vP) print(mesh) # ax, cbar = pg.show(mesh, data=vP) # pg.show(mesh, axes=ax) geophPointsX = np.arange(-19, 19.1, 1) geophPoints = np.vstack((geophPointsX, np.zeros(len(geophPointsX)))).T sourcePos = geophPoints[4] c = mesh.findCell(sourcePos) h1 = pg.findBoundary(c.boundaryNodes(0)).size() h2 = pg.findBoundary(c.boundaryNodes(1)).size() h3 = pg.findBoundary(c.boundaryNodes(2)).size() print([h1, h2, h3]) h = pg.math.median([h1, h2, h3]) # h = pg.math.median(mesh.boundarySizes()) f0scale = 0.25 cfl = 0.5 dt = cfl * h / max(vP) print("Courant-Friedrich-Lewy number:", cfl) tmax = 40. / min(vP) times = np.arange(0.0, tmax, dt) solutionName = createCacheName('seis', mesh, times) + "cfl-" + str(cfl) try: # u = pg.load(solutionName + '.bmat') uI = pg.load(solutionName + 'I.bmat') except Exception as e: print(e) f0 = f0scale * 1. / dt print("h:", round(h, 2), "dt:", round(dt, 5), "1/dt:", round(1 / dt, 1), "f0", round(f0, 2), "Wavelength: ", round(max(vP) / f0, 2), " m") uSource = ricker(times, f0, t0=1. / f0) plt.figure() plt.plot(times, uSource, '-*') plt.show(block=0) plt.pause(0.01) u = solvePressureWave(mesh, vP, times, sourcePos=sourcePos, uSource=uSource, verbose=10) u.save(solutionName) uI = pg.Matrix() print("interpolate node to cell data ... ") pg.interpolate(mesh, u, mesh.cellCenters(), uI) print("... done") uI.save(solutionName + 'I') # nodes = [mesh.findNearestNode(p) for p in geophPoints] # fig = plt.figure() # axs = fig.add_subplot(1,1,1) # drawSeismogramm(axs, mesh, u, nodes, dt, i=None) # plt.show() dpi = 92 scale = 1 fig = plt.figure(facecolor='white', figsize=(scale * 800 / dpi, scale * 490 / dpi), dpi=dpi) ax = fig.add_subplot(1, 1, 1) gci = pg.viewer.mpl.drawModel(ax, mesh, data=uI[0], cMin=-1, cMax=1, cmap='bwr') pg.viewer.mpl.drawMeshBoundaries(ax, meshIn, hideMesh=1) ax.set_xlim((-20, 20)) ax.set_ylim((-15, 0)) ax.set_ylabel('Depth [m]') ax.set_xlabel('$x$ [m]') ticks = ax.yaxis.get_majorticklocs() tickLabels = [] for t in ticks: tickLabels.append(str(int(abs(t)))) ax.set_yticklabels(tickLabels) plt.tight_layout() # ax, cbar = pg.show(mesh, data=vP) # pg.showNow() # ax = fig.add_subplot(1,1,1) def animate(i): i = i * 5 if i > len(uI) - 1: return print("Frame:", i, "/", len(uI)) ui = uI[i] ui = ui / max(pg.abs(ui)) ui = pg.logDropTol(ui, 1e-2) cMax = max(pg.abs(ui)) pg.viewer.mpl.setMappableData(gci, ui, cMin=-cMax, cMax=cMax, logScale=False) # plt.pause(0.001) anim = animation.FuncAnimation(fig, animate, frames=int(len(uI) / 5), interval=0.001, repeat=0) # , blit=True) out = 'seis' + str(f0scale) + "cfl-" + str(cfl) anim.save(out + ".mp4", writer=None, fps=20, dpi=dpi, codec=None, bitrate=24 * 1024, extra_args=None, metadata=None, extra_anim=None, savefig_kwargs=None) try: print("create frames ... ") os.system('mkdir -p anim-' + out) os.system('ffmpeg -i ' + out + '.mp4 anim-' + out + '/movie%d.jpg') except: pass
import pygimli as pg import numpy as np from settings import fpm pd = pg.load("paraDomain_1.bms") resinv = np.loadtxt("res_conventional.dat") vest = np.loadtxt("vel_conventional.dat") fae, fie, fwe, maske = fpm.all(resinv, vest) print(np.min(fwe), np.max(fwe)) np.savez("conventional.npz", vel=np.array(vest), rho=np.array(resinv), fa=fae, fi=fie, fw=fwe, mask=maske)
def main(): # read config file conf_file = "inv.conf" with open(conf_file, "r") as fd: conf = json.load(fd) # res = pb.Resistivity("input.dat") # res.invert() # np.savetxt('resistivity.vector', res.resistivity) # return # load data file data = pg.DataContainerERT("input.dat") # remove invalid data oldsize = data.size() data.removeInvalid() newsize = data.size() if newsize < oldsize: print('Removed ' + str(oldsize - newsize) + ' values.') if not data.allNonZero('rhoa'): print("No or partial rhoa values.") return # check, compute error if data.allNonZero('err'): error = data('err') else: print("estimate data error") error = conf["relativeError"] + conf["absoluteError"] / data('rhoa') # create FOP fop = pg.DCSRMultiElectrodeModelling(verbose=conf["verbose"]) fop.setThreadCount(psutil.cpu_count(logical=False)) fop.setData(data) # create Inv inv = pg.RInversion(verbose=conf["verbose"], dosave=False) # variables tD, tM are needed to prevent destruct objects tD = pg.RTransLog() tM = pg.RTransLogLU() inv.setTransData(tD) inv.setTransModel(tM) inv.setForwardOperator(fop) # mesh if conf["meshFile"] == "": depth = conf["depth"] if depth is None: depth = pg.DCParaDepth(data) poly = pg.meshtools.createParaMeshPLC( data.sensorPositions(), paraDepth=depth, paraDX=conf["paraDX"], paraMaxCellSize=conf["maxCellArea"], paraBoundary=2, boundary=2) if conf["verbose"]: print("creating mesh...") mesh = pg.meshtools.createMesh(poly, quality=conf["quality"], smooth=(1, 10)) else: mesh = pg.Mesh(pg.load(conf["meshFile"])) mesh.createNeighbourInfos() if conf["verbose"]: print(mesh) sys.stdout.flush() # flush before multithreading fop.setMesh(mesh) fop.regionManager().setConstraintType(1) if not conf["omitBackground"]: if fop.regionManager().regionCount() > 1: fop.regionManager().region(1).setBackground(True) if conf["meshFile"] == "": fop.createRefinedForwardMesh(True, False) else: fop.createRefinedForwardMesh(conf["refineMesh"], conf["refineP2"]) paraDomain = fop.regionManager().paraDomain() inv.setForwardOperator(fop) # necessary? # inversion parameters inv.setData(data('rhoa')) inv.setRelativeError(error) fop.regionManager().setZWeight(conf['zWeight']) inv.setLambda(conf['lam']) inv.setMaxIter(conf['maxIter']) inv.setRobustData(conf['robustData']) inv.setBlockyModel(conf['blockyModel']) inv.setRecalcJacobian(conf['recalcJacobian']) pc = fop.regionManager().parameterCount() startModel = pg.RVector(pc, pg.median(data('rhoa'))) inv.setModel(startModel) # Run the inversion sys.stdout.flush() # flush before multithreading model = inv.run() resistivity = model(paraDomain.cellMarkers()) np.savetxt('resistivity.vector', resistivity) print("Done.")
import numpy as np import pygimli as pg import pygimli.meshtools as mt from fpinv import FourPhaseModel, NN_interpolate from pygimli.physics import Refraction, ERTManager from settings import * #need ertData, rstData, a mesh and phi to be given ertData = pg.DataContainerERT("ert_filtered.data") print(ertData) mesh = pg.load("mesh_1.bms") paraDomain = pg.load("paraDomain_1.bms") depth = mesh.ymax() - mesh.ymin() ert = ERTManager() resinv = ert.invert(ertData, mesh=mesh, lam=60, zWeight=zWeight, maxIter=maxIter) print("ERT chi:", ert.inv.chi2()) print("ERT rms:", ert.inv.relrms()) np.savetxt("res_conventional.dat", resinv) ############# ttData = pg.DataContainer("rst_filtered.data", "s g") rst = Refraction(ttData) # INVERSION
import pybert as pb import pygimli as pg # compute k, err, rhoa data = pb.DataContainerERT("lhp2.dat") data.set("k", pg.geometricFactors(data)) data.set("err", pb.Resistivity.estimateError(data, absoluteUError=0.0001, relativeError=0.03)) data.set("rhoa", data("u") / data("i") * data("k")) # mesh mesh = pg.load("oblast.bms") #mesh = pg.meshtools.createParaMesh2DGrid(data.sensorPositions(), paraDX=0.25, paraDZ=0.25, nLayers=20) # inversion res = pb.Resistivity(data) res.setMesh(mesh) #res.setMesh(mesh, refine=False) res.invert() res.showResult() #res.saveResult("in_python_res") pg.wait()
def showSynthData(synthPath): geom = pg.load(synthPath + 'synthGeom.bms') mesh = pg.load(synthPath + 'synth.bms') k = np.load(synthPath + 'synthK.npy') vel = np.load(synthPath + 'synthVel.npy') sat = np.load(synthPath + 'synthSat.npy') scheme = pg.DataContainer(synthPath + 'synth.shm', 'a b m n') # rhoaR = np.load(synthPath + 'synthRhoaRatio.npy') # rhoa = np.load(synthPath + 'synthRhoa.npy') row = 3 col = 2 # START model perm + input savefig(mesh, geom, k, 'Hydraulic conductivity $K$ in m$/$s', out='hydrConductModel', cMin=1e-5, cMax=1e-2, nLevs=4, cmap='viridis') # START velocity axVel, _ = pg.show(mesh, np.sqrt(vel[0]**2 + vel[1]**2), logScale=0, colorBar=1, pad=0.55, label='Velocity $|v|$ in m$/$s', hold=1) meshC = pg.meshtools.createMesh(geom, quality=33, area=0.5, smooth=[1, 10]) pg.show(mesh, data=vel, ax=axVel, coarseMesh=meshC, color='black', linewidth=0.5, dropTol=1e-6) pg.show(geom, ax=axVel, fillRegion=False) saveAxes(axVel, 'hydrVelocity', adjust=True) # START Saturation axs = plt.subplots(row, col, sharex=True, sharey=True, figsize=(10.*0.65, 7.25*0.65))[1].flatten() satScale = 0.001 for i, a in enumerate(axs): savefig(mesh, geom, sat[i*len(sat)/(len(axs))+1] * satScale, # /mesh.cellSizes(), label=None, out=None, cMin=0, cMax=2.5, ax=a, adjust=True) pg.mplviewer.drawSensors(a, scheme.sensorPositions(), diam=0.15, color='green') add_inner_title(a, "t = %d days" % days[i], 3, color="w") if i < (row-1)*col: a.set_xlabel('') if i % col: a.set_ylabel('') a.set_ylim([-16, 0]) pg.mplviewer.saveFigure(axs[0].figure, "hydrSaturation") pg.mplviewer.createColorBarOnly(cMin=0, cMax=2.5, logScale=False, cMap='Spectral_r', nLevs=5, label=r'Concentration $c$ in g$/$l', orientation='horizontal', savefig='hydrSaturationCbar') # END Saturation pg.wait()