def vertical(thickness, velocity, zp): """ Calculates the first-arrival travel-times for given a layered model. Simulates a vertical seismic profile. The source is assumed to be at z = 0. The z-axis is positive downward. Parameters: * thickness : list The thickness of each layer in order of increasing depth * velocity : list The velocity of each layer in order of increasing depth * zp : list The depths of the measurement stations (seismometers) Returns: * travel_times : array The first-arrival travel-times calculated at the measurement stations. """ if len(thickness) != len(velocity): raise ValueError, "thickness and velocity must have same length" nlayers = len(thickness) zmax = sum(thickness) z = [sum(thickness[:i]) for i in xrange(nlayers + 1)] layers = [ Square((0, zmax, z[i], z[i + 1]), props={'vp': velocity[i]}) for i in xrange(nlayers) ] srcs = [(0, 0)] * len(zp) recs = [(0, z) for z in zp] return ttime2d.straight(layers, 'vp', srcs, recs)
def vertical(thickness, velocity, zp): """ Calculates the first-arrival travel-times for given a layered model. Simulates a vertical seismic profile. The source is assumed to be at z = 0. The z-axis is positive downward. Parameters: * thickness : list The thickness of each layer in order of increasing depth * velocity : list The velocity of each layer in order of increasing depth * zp : list The depths of the measurement stations (seismometers) Returns: * travel_times : array The first-arrival travel-times calculated at the measurement stations. """ if len(thickness) != len(velocity): raise ValueError, "thickness and velocity must have same length" nlayers = len(thickness) zmax = sum(thickness) z = [sum(thickness[:i]) for i in xrange(nlayers + 1)] layers = [Square((0, zmax, z[i], z[i + 1]), props={"vp": velocity[i]}) for i in xrange(nlayers)] srcs = [(0, 0)] * len(zp) recs = [(0, z) for z in zp] return ttime2d.straight(layers, "vp", srcs, recs)
def _get_jacobian(self): nlayers = len(self.thickness) zmax = sum(self.thickness) z = [sum(self.thickness[:i]) for i in xrange(nlayers + 1)] layers = [Square((0, zmax, z[i], z[i + 1]), props={"vp": 1.0}) for i in xrange(nlayers)] srcs = [(0, 0)] * len(self.zp) recs = [(0, z) for z in self.zp] jac_T = numpy.array([ttime2d.straight([l], "vp", srcs, recs) for l in layers]) return jac_T
def test_general(): """ General test of the class SRTomo, as it is in the docs of this class. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) assert_array_almost_equal(tomo.fit().estimate_, np.array([2., 5.]), 9)
def _get_jacobian(self): nlayers = len(self.thickness) zmax = sum(self.thickness) z = [sum(self.thickness[:i]) for i in xrange(nlayers + 1)] layers = [ Square((0, zmax, z[i], z[i + 1]), props={'vp': 1.}) for i in xrange(nlayers) ] srcs = [(0, 0)] * len(self.zp) recs = [(0, z) for z in self.zp] jac_T = numpy.array( [ttime2d.straight([l], 'vp', srcs, recs) for l in layers]) return jac_T
def test_jacobian(): """ srtomo.SRTomo.jacobian return the jacobian of the model provided. In this simple model, the jacobian can be easily calculated. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) assert_array_almost_equal(tomo.jacobian().todense(), np.array([[5., 0.], [5., 5.]]), 9)
def test_predicted(): """ Test to verify srtomo.SRTomo.predicted function. Given the correct parameters, this function must return the result of the forward data. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) # The parameter used inside the class is slowness, so 1/vp. tomo.p_ = np.array([1./2., 1./5.]) assert_array_almost_equal(tomo.predicted(), ttimes, 9)
def test_predicted(): """ Test to verify srtomo.SRTomo.predicted function. Given the correct parameters, this function must return the result of the forward data. """ model = SquareMesh((0, 10, 0, 10), shape=(2, 1), props={'vp': [2., 5.]}) src = (5, 0) srcs = [src, src] recs = [(0, 0), (5, 10)] ttimes = ttime2d.straight(model, 'vp', srcs, recs) mesh = SquareMesh((0, 10, 0, 10), shape=(2, 1)) tomo = srtomo.SRTomo(ttimes, srcs, recs, mesh) # The parameter used inside the class is slowness, so 1/vp. tomo.p_ = np.array([1. / 2., 1. / 5.]) assert_array_almost_equal(tomo.predicted(), ttimes, 9)
def _get_jacobian(self): """ Build the Jacobian (sensitivity) matrix using the travel-time data stored. """ log.info(" calculating Jacobian (sensitivity matrix):") start = time.time() srcs, recs = self.srcs, self.recs if not self.sparse: jac = numpy.array([ttime2d.straight([cell], "", srcs, recs, velocity=1.0) for cell in self.mesh]).T else: shoot = ttime2d.straight nonzero = [] extend = nonzero.extend for j, c in enumerate(self.mesh): extend((i, j, tt) for i, tt in enumerate(shoot([c], "", srcs, recs, velocity=1.0)) if tt != 0) row, col, val = numpy.array(nonzero).T shape = (self.ndata, self.nparams) jac = scipy.sparse.csr_matrix((val, (row, col)), shape) log.info(" time: %s" % (utils.sec2hms(time.time() - start))) return jac
def _get_jacobian(self): """ Build the Jacobian (sensitivity) matrix using the travel-time data stored. """ srcs, recs = self.srcs, self.recs if not self.sparse: jac = numpy.array( [ttime2d.straight([cell], '', srcs, recs, velocity=1.) for cell in self.mesh]).T else: shoot = ttime2d.straight nonzero = [] extend = nonzero.extend for j, c in enumerate(self.mesh): extend((i, j, tt) for i, tt in enumerate(shoot([c], '', srcs, recs, velocity=1.)) if tt != 0) row, col, val = numpy.array(nonzero).T shape = (self.ndata, self.nparams) jac = scipy.sparse.csr_matrix((val, (row, col)), shape) return jac
from fatiando.vis import mpl from fatiando import utils area = (0, 500000, 0, 500000) shape = (30, 30) model = SquareMesh(area, shape) vel = 4000 * np.ones(shape) vel[5:25, 5:25] = 10000 model.addprop('vp', vel.ravel()) # Make some travel time data and add noise seed = 0 # Set the random seed so that points are the same every time src_loc = utils.random_points(area, 80, seed=seed) rec_loc = utils.circular_points(area, 30, random=True, seed=seed) srcs, recs = utils.connect_points(src_loc, rec_loc) tts = ttime2d.straight(model, 'vp', srcs, recs) tts, error = utils.contaminate(tts, 0.02, percent=True, return_stddev=True, seed=seed) # Make the mesh mesh = SquareMesh(area, shape) # and run the inversion misfit = srtomo.SRTomo(tts, srcs, recs, mesh) regularization = Smoothness2D(mesh.shape) # Will use the l-curve criterion to find the best regularization parameter tomo = LCurve(misfit, regularization, [10**i for i in np.arange(0, 10, 1)], jobs=8).fit() mesh.addprop('vp', tomo.estimate_)
model = SquareMesh(area, shape) vel = 4000 * np.ones(shape) vel[5:25, 5:25] = 10000 model.addprop('vp', vel.ravel()) # Make some noisy travel time data using straight-rays # Set the random seed so that points are the same every time we run this script seed = 0 src_loc_x, src_loc_y = gridder.scatter(area, 80, seed=seed) src_loc = np.transpose([src_loc_x, src_loc_y]) rec_loc_x, rec_loc_y = gridder.circular_scatter(area, 30, random=True, seed=seed) rec_loc = np.transpose([rec_loc_x, rec_loc_y]) srcs = [src for src in src_loc for _ in rec_loc] recs = [rec for _ in src_loc for rec in rec_loc] tts = ttime2d.straight(model, 'vp', srcs, recs) # Use 2% random noise to corrupt the data tts = utils.contaminate(tts, 0.02, percent=True, seed=seed) # Make a mesh for the inversion. The inversion will estimate the velocity in # each square of the mesh. To make things simpler, we'll use a mesh that is the # same as our original model. mesh = SquareMesh(area, shape) # Create solvers for each type of regularization and fit the synthetic data to # obtain an estimated velocity model solver = srtomo.SRTomo(tts, srcs, recs, mesh) smooth = solver + 1e8*Smoothness2D(mesh.shape) smooth.fit()
mpl.figure() mpl.axis('scaled') mpl.suptitle("Choose the location of the receivers") rec_points = mpl.pick_points(area, mpl.gca(), marker='^', color='r') # and the source mpl.figure() mpl.axis('scaled') mpl.suptitle("Choose the location of the source") mpl.points(rec_points, '^r') src = mpl.pick_points(area, mpl.gca(), marker='*', color='y') if len(src) > 1: print "Don't be greedy! Pick only one point as the source" sys.exit() # Calculate the P and S wave traveltimes srcs, recs = utils.connect_points(src, rec_points) ptime = ttime2d.straight(model, 'vp', srcs, recs) stime = ttime2d.straight(model, 'vs', srcs, recs) # Calculate the residual time (S - P) with added noise traveltime, error = utils.contaminate(stime - ptime, 0.05, percent=True, return_stddev=True) solver = Homogeneous(traveltime, recs, vp, vs) # Pick the initial estimate and fit mpl.figure() mpl.axis('scaled') mpl.suptitle("Choose the initial estimate") mpl.points(rec_points, '^r') mpl.points(src, '*y') initial = mpl.pick_points(area, mpl.gca(), marker='*', color='b') if len(initial) > 1: print "Don't be greedy! Pick only one point" sys.exit()
mpl.figure() mpl.axis('scaled') mpl.suptitle("Choose the location of the receivers") rec_points = mpl.pick_points(area, mpl.gca(), marker='^', color='r') # and the source mpl.figure() mpl.axis('scaled') mpl.suptitle("Choose the location of the source") mpl.points(rec_points, '^r') src = mpl.pick_points(area, mpl.gca(), marker='*', color='y') if len(src) > 1: print "Don't be greedy! Pick only one point as the source" sys.exit() # Calculate the P and S wave traveltimes srcs, recs = utils.connect_points(src, rec_points) ptime = ttime2d.straight(model, 'vp', srcs, recs) stime = ttime2d.straight(model, 'vs', srcs, recs) # Calculate the residual time (S - P) with added noise traveltime, error = utils.contaminate(stime - ptime, 0.05, percent=True, return_stddev=True) solver = Homogeneous(traveltime, recs, vp, vs) # Pick the initial estimate and fit mpl.figure() mpl.axis('scaled') mpl.suptitle("Choose the initial estimate") mpl.points(rec_points, '^r') mpl.points(src, '*y') initial = mpl.pick_points(area, mpl.gca(), marker='*', color='b') if len(initial) > 1: