def test_emarray(): out = utils.EMArray(3) assert out.amp == 3 assert out.pha == 0 assert out.real == 3 assert out.imag == 0 out = utils.EMArray(1, 1) assert out.amp == np.sqrt(2) assert out.pha == 45. assert out.real == 1 assert out.imag == 1 out = utils.EMArray([1, 0], [1, 1]) assert_allclose(out.amp, [np.sqrt(2), 1]) assert_allclose(out.pha, [45., 90.]) assert_allclose(out.real, [1, 0]) assert_allclose(out.imag, [1, 1])
def test_emarray(): out = utils.EMArray(3) assert out.amp() == 3 assert out.pha() == 0 assert out.real == 3 assert out.imag == 0 out = utils.EMArray(1 + 1j) assert out.amp() == np.sqrt(2) assert_allclose(out.pha(), np.pi / 4) assert out.real == 1 assert out.imag == 1 out = utils.EMArray([1 + 1j, 0 + 1j, -1 - 1j]) assert_allclose(out.amp(), [np.sqrt(2), 1, np.sqrt(2)]) assert_allclose(out.pha(unwrap=False), [np.pi / 4, np.pi / 2, -3 * np.pi / 4]) assert_allclose(out.pha(deg=True, unwrap=False), [45., 90., -135.]) assert_allclose(out.pha(deg=True, unwrap=False, lag=False), [-45., -90., 135.]) assert_allclose(out.pha(deg=True, lag=False), [-45., -90., -225.]) assert_allclose(out.real, [1, 0, -1]) assert_allclose(out.imag, [1, 1, -1])
def csem_layered_earth( srcloc, rxlocs, depth, res, aniso, frequency, nlayers=5, src_type="electric", rx_type="electric", src_direction="x", rx_direction="x", verb=0, ): """ Simulating CSEM response in a layered earth """ # Safety checks if len(depth) != nlayers - 1: raise Exception("Length of depth should be nlayers-1") if len(res) != nlayers: raise Exception("Length of res should be nlayers") if len(aniso) != nlayers: raise Exception("Length of aniso should be nlayers") if srcloc.size != 3: raise Exception("size of srcloc should be 3! (x, y, z) location") rxlocs = np.atleast_2d(rxlocs) if src_direction == "x": src = np.r_[srcloc.flatten(), 0.0, 0.0].tolist() elif src_direction == "y": src = np.r_[srcloc.flatten(), 90.0, 0.0].tolist() elif src_direction == "z": src = np.r_[srcloc.flatten(), 0.0, 90.0].tolist() else: raise Exception("src_direction should be x, y, or z") if rx_direction == "x": rx = [rxlocs[:, 0], rxlocs[:, 1], rxlocs[:, 2], 0.0, 0.0] elif rx_direction == "y": rx = [rxlocs[:, 0], rxlocs[:, 1], rxlocs[:, 2], 90.0, 0.0] elif rx_direction == "z": rx = [rxlocs[:, 0], rxlocs[:, 1], rxlocs[:, 2], 0.0, 90.0] else: raise Exception("rx_direction should be x, y, or z") if rx_type == "electric": rx_type = None if src_type == "electric": src_type = None if len(np.unique(np.array(res))) == 1: xdirect = True else: xdirect = False inpdat = { "res": res, "src": src, "rec": rx, "depth": depth, "freqtime": frequency, "aniso": aniso, "verb": verb, "xdirect": xdirect, "mrec": rx_type, "msrc": src_type, } out = bipole(**inpdat) return utils.EMArray(out)
def csem_data_app( frequency, zsrc, zrx, rho0, rho1, rho2, rho3, rho4, rv_rh, dz1, dz2, dz3, frequency_bg, zsrc_bg, zrx_bg, rho0_bg, rho1_bg, rho2_bg, rho3_bg, rho4_bg, rv_rh_bg, dz1_bg, dz2_bg, dz3_bg, Field, Plane, Component, Complex, scale, Fixed, vmin, vmax, ): rcParams["font.size"] = 16 # lamda0, lamda1, lamda2, lamda3, lamda4 = 1.0, 1.0, 1.0, 1.0, 1.0 d = np.r_[0.0, np.cumsum(np.r_[dz1, dz2, dz3])] depth = [d[0], d[1], d[2], d[3]] # Layer boundaries d_bg = np.r_[0.0, np.cumsum(np.r_[dz1_bg, dz2_bg, dz3_bg])] depth_bg = [d_bg[0], d_bg[1], d_bg[2], d_bg[3]] # Layer boundaries res = [rho0, rho1, rho2, rho3, rho4] # Anomaly resistivities res_bg = [rho0_bg, rho1_bg, rho2_bg, rho3_bg, rho4_bg] # Anomaly resistivities aniso = [ 1.0, 1.0, np.sqrt(rv_rh), 1.0, 1.0, ] # Layer anisotropies (same for anomaly and background) aniso_bg = [ 1.0, 1.0, np.sqrt(rv_rh_bg), 1.0, 1.0, ] # Layer anisotropies (same for anomaly and background) # Modelling parameters # verb = 1 # Spatial parameters dx = 100.0 dy = 100.0 nrx = 200 x = np.r_[np.arange(nrx) * dx + dx] # Offsets y = np.r_[np.arange(nrx) * dy + dy] # Offsets z = np.ones_like(x) * -zrx srcloc = np.r_[0.0, 0.0, -zsrc] srcloc = np.r_[0.0, 0.0, -zsrc] if Plane == "XZ": rxlocs = np.c_[x, np.zeros_like(x), z] xlabel = "X offset (m)" # x0 = y.copy() elif Plane == "YZ": rxlocs = np.c_[np.zeros_like(x), y, z] xlabel = "Y offset (m)" # x0 = x.copy() if Field == "E": label = "Electric field (V/m)" data = csem_layered_earth(srcloc, rxlocs, depth, res, aniso, frequency, rx_direction=Component) data_bg = csem_layered_earth( srcloc, rxlocs, depth_bg, res_bg, aniso_bg, frequency_bg, rx_direction=Component, ) elif Field == "H": label = "Magnetic field (A/m)" data = csem_layered_earth( srcloc, rxlocs, depth, res, aniso, frequency, rx_direction=Component, rx_type="magnetic", ) data_bg = csem_layered_earth( srcloc, rxlocs, depth_bg, res_bg, aniso_bg, frequency_bg, rx_direction=Component, rx_type="magnetic", ) elif Field == "Zxy": label = "Impedance (V/A)" data_ex = csem_layered_earth(srcloc, rxlocs, depth, res, aniso, frequency, rx_direction="x") data_ex_bg = csem_layered_earth(srcloc, rxlocs, depth_bg, res_bg, aniso_bg, frequency_bg, rx_direction="x") data_hy = csem_layered_earth( srcloc, rxlocs, depth_bg, res_bg, aniso_bg, frequency_bg, rx_direction="y", rx_type="magnetic", ) data_hy_bg = csem_layered_earth( srcloc, rxlocs, depth_bg, res_bg, aniso_bg, frequency_bg, rx_direction="y", rx_type="magnetic", ) data = utils.EMArray(data_ex / data_hy) data_bg = utils.EMArray(data_ex_bg / data_hy_bg) if Complex == "Real": val = data.real val_bg = data_bg.real elif Complex == "Imag": val = data.imag val_bg = data_bg.imag elif Complex == "Amp": if Field == "Zxy": label = "Apparent Resistivity ($\Omega$m)" val = abs(data)**2 / (2 * frequency * np.pi * mu_0) val_bg = abs(data_bg)**2 / (2 * frequency_bg * np.pi * mu_0) else: val = data.amp val_bg = data_bg.amp elif Complex == "Phase": label = "Phase (degree)" val = data.pha val_bg = data_bg.pha plt.figure(figsize=(8 * 1.4, 3 * 1.4)) ax = plt.subplot(111) if scale == "log": ax.plot(x, abs(val), "r") ax.plot(x, abs(val_bg), "k") elif scale == "linear": ax.plot(x, val, "r") ax.plot(x, val_bg, "k") ax.grid(True) ax.set_xlabel(xlabel) ax.set_ylabel(label) ax.set_yscale(scale) ax.legend(("Response", "Background")) if Complex == "Amp": if Field == "Zxy": title = "$\\rho_{xy}$" else: title = "|" + Field + Component + "|" else: if Field == "Zxy": title = Complex + "(" + Field + ")" else: title = Complex + "(" + Field + Component + ")" ax.set_title(title) if Fixed: ax.set_ylim(vmin, vmax) plt.show()
def csem_layered_earth(srcloc, rxlocs, depth, res, aniso, frequency, nlayers=5, src_type="electric", rx_type="electric", src_direction="x", rx_direction="x", verb=0): """ Simulating CSEM response in a layered earth """ # Safety checks if len(depth) != nlayers - 1: raise Exception("Length of depth should be nlayers-1") if len(res) != nlayers: raise Exception("Length of res should be nlayers") if len(aniso) != nlayers: raise Exception("Length of aniso should be nlayers") if srcloc.size != 3: raise Exception("size of srcloc should be 3! (x, y, z) location") rxlocs = np.atleast_2d(rxlocs) if src_direction == "x": src = np.r_[srcloc.flatten(), 0., 0.].tolist() elif src_direction == "y": src = np.r_[srcloc.flatten(), 90., 0.].tolist() elif src_direction == "z": src = np.r_[srcloc.flatten(), 0., 90.].tolist() else: raise Exception("src_direction should be x, y, or z") if rx_direction == "x": rx = [rxlocs[:, 0], rxlocs[:, 1], rxlocs[:, 2], 0., 0.] elif rx_direction == "y": rx = [rxlocs[:, 0], rxlocs[:, 1], rxlocs[:, 2], 90., 0.] elif rx_direction == "z": rx = [rxlocs[:, 0], rxlocs[:, 1], rxlocs[:, 2], 0., 90.] else: raise Exception("rx_direction should be x, y, or z") if rx_type is "electric": rx_type = None if src_type is "electric": src_type = None if len(np.unique(np.array(res))) == 1: xdirect = True else: xdirect = False inpdat = { 'res': res, 'src': src, 'rec': rx, 'depth': depth, 'freqtime': frequency, 'aniso': aniso, 'verb': verb, 'xdirect': xdirect, 'mrec': rx_type, 'msrc': src_type } out = bipole(**inpdat) return utils.EMArray(out)