def test_errors(self): mesh = emg3d.TensorMesh([[2, 2], [2, 2], [2, 2]], origin=(-1, -1, -1)) survey = emg3d.Survey( sources=emg3d.TxElectricDipole((-1.5, 0, 0, 0, 0)), receivers=emg3d.RxElectricPoint((1.5, 0, 0, 0, 0)), frequencies=1.0, relative_error=0.01, ) sim_inp = { 'survey': survey, 'gridding': 'same', 'receiver_interpolation': 'linear' } # Anisotropic models. simulation = simulations.Simulation(model=emg3d.Model(mesh, 1, 2, 3), **sim_inp) with pytest.raises(NotImplementedError, match='for isotropic models'): simulation.gradient # Model with electric permittivity. simulation = simulations.Simulation(model=emg3d.Model(mesh, epsilon_r=3), **sim_inp) with pytest.raises(NotImplementedError, match='for el. permittivity'): simulation.gradient # Model with magnetic permeability. simulation = simulations.Simulation(model=emg3d.Model( mesh, mu_r=np.ones(mesh.shape_cells) * np.pi), **sim_inp) with pytest.raises(NotImplementedError, match='for magn. permeabili'): simulation.gradient
def test_rel_abs_rec(self): # Sources sources = emg3d.surveys.txrx_coordinates_to_dict( emg3d.TxElectricDipole, ([0, 100, 200], 0, 0, 0, 0)) # Abs and rel Receivers a_e_rec = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, (1000 + np.arange(3) * 100, 0, -100, 0, 0)) r_e_rec = emg3d.surveys.txrx_coordinates_to_dict(emg3d.RxElectricPoint, (1000, 0, -100, 0, 0), relative=True) a_h_rec = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxMagneticPoint, (1000 + np.arange(3) * 100, 0, -100, 0, 0)) r_h_rec = emg3d.surveys.txrx_coordinates_to_dict(emg3d.RxMagneticPoint, (1000, 0, -100, 0, 0), relative=True) receivers = emg3d.surveys.txrx_lists_to_dict( [a_e_rec, r_e_rec, a_h_rec, r_h_rec]) # Frequencies frequencies = (1.0) survey = emg3d.Survey(sources, receivers, frequencies, name='TestSurv', noise_floor=1e-15, relative_error=0.05) # Create a simple grid and model grid = emg3d.TensorMesh( [np.ones(32) * 250, np.ones(16) * 500, np.ones(16) * 500], np.array([-1250, -1250, -2250])) model = emg3d.Model(grid, 1) # Create a simulation, compute all fields. simulation = simulations.Simulation(survey, model, name='TestSim', max_workers=1, solver_opts={ 'maxit': 1, 'verb': 0, 'plain': True }, gridding='same') simulation.compute() # Relative receivers must be same as corresponding absolute receivers assert_allclose( [simulation.data.synthetic[i, i, 0].data for i in range(3)], simulation.data.synthetic[:, 3, 0].data) assert_allclose( [simulation.data.synthetic[i, i + 4, 0].data for i in range(3)], simulation.data.synthetic[:, 7, 0].data)
def test_factor(self): sources = emg3d.TxElectricDipole((0, 3000, -950, 0, 0)) receivers = emg3d.RxElectricPoint((0, 3000, -1000, 0, 0)) # Adjusted x-domain. survey = emg3d.Survey( self.sources, receivers, self.frequencies, noise_floor=1e-15, relative_error=0.05) gdict = meshes.estimate_gridding_opts({}, self.model, survey) assert_allclose(gdict['domain']['x'], (-800, 800)) # Adjusted x-domain. survey = emg3d.Survey( sources, self.receivers, self.frequencies, noise_floor=1e-15, relative_error=0.05) gdict = meshes.estimate_gridding_opts({}, self.model, survey) assert_allclose(gdict['domain']['y'], (1500, 3500))
def test_misfit(): data = 1 syn = 5 rel_err = 0.05 sources = emg3d.TxElectricDipole((0, 0, 0, 0, 0)) receivers = emg3d.RxElectricPoint((5, 0, 0, 0, 0)) survey = emg3d.Survey( sources=sources, receivers=receivers, frequencies=100, data=np.zeros((1, 1, 1)) + data, relative_error=0.05, ) grid = emg3d.TensorMesh([np.ones(10) * 2, [2, 2], [2, 2]], (-10, -2, -2)) model = emg3d.Model(grid, 1) simulation = simulations.Simulation(survey=survey, model=model) field = emg3d.Field(grid, dtype=np.float64) field.field += syn simulation._dict_efield['TxED-1']['f-1'] = field simulation.data['synthetic'] = simulation.data['observed'] * 0 + syn misfit = 0.5 * ((syn - data) / (rel_err * data))**2 def dummy(): pass simulation.compute = dummy # => switch of compute() assert_allclose(simulation.misfit, misfit) # Missing noise_floor / std. survey = emg3d.Survey(sources, receivers, 100) simulation = simulations.Simulation(survey=survey, model=model) with pytest.raises(ValueError, match="Either `noise_floor` or"): simulation.misfit
def test_dict_serialize_deserialize(): frequency = 1.0 grid = emg3d.TensorMesh([[2, 2], [3, 4], [0.5, 2]], (0, 0, 0)) field = emg3d.Field(grid) model = emg3d.Model(grid, 1) tx_e_d = emg3d.TxElectricDipole((0, 1000, 0, 0, -900, -950)) tx_m_d = emg3d.TxMagneticDipole([[0, 0, -900], [1000, 0, -950]]) tx_e_w = emg3d.TxElectricWire(([[0, 0, 0], [1, 1, 1], [1, 0, 1]])) rx_e_p = emg3d.RxElectricPoint((0, 1000, -950, 0, 20)) rx_m_p = emg3d.RxMagneticPoint((0, 1000, -950, 20, 0)) data = { 'Grid': grid, 'Model': model, 'Field': field, } if xarray: survey = emg3d.Survey( emg3d.surveys.txrx_lists_to_dict([tx_e_d, tx_m_d, tx_e_w]), emg3d.surveys.txrx_lists_to_dict([rx_e_p, rx_m_p]), frequency ) simulation = emg3d.Simulation(survey, model, gridding='same') data['Survey'] = survey data['Simulation'] = simulation # Get everything into a serialized dict. out = io._dict_serialize(data) # Get everything back. io._nonetype_to_none(out) keep = deepcopy(out) io._dict_deserialize(out) assert data.keys() == out.keys() assert out['Field'] == field assert out['Grid'] == grid assert out['Model'] == model if xarray: assert out['Survey'].sources == survey.sources assert (out['Simulation'].survey.receivers == simulation.survey.receivers) del keep['Grid']['hx'] with pytest.warns(UserWarning, match="Could not de-serialize"): io._dict_deserialize(keep)
class TestRun: # Default values for run-tests args_dict = { 'config': '.', 'nproc': 1, 'forward': False, 'misfit': False, 'gradient': True, 'path': None, 'survey': 'survey.npz', 'model': 'model.npz', 'output': 'output.npz', 'save': None, 'load': None, 'verbosity': 0, 'dry_run': True, } if xarray is not None: # Create a tiny dummy survey. data = np.ones((1, 17, 1)) data[0, 8:11, 0] = np.nan sources = emg3d.TxElectricDipole((4125, 4000, 4000, 0, 0)) receivers = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, (np.arange(17) * 250 + 2000, 4000, 3950, 0, 0)) survey = emg3d.Survey( name='CLI Survey', sources=sources, receivers=receivers, frequencies=1, noise_floor=1e-15, relative_error=0.05, data=data, ) # Create a dummy grid and model. xx = np.ones(16) * 500 grid = emg3d.TensorMesh([xx, xx, xx], origin=np.array([0, 0, 0])) model = emg3d.Model(grid, 1.) def test_basic(self, tmpdir, capsys): # Store survey and model. self.survey.to_file(os.path.join(tmpdir, 'survey.npz'), verb=0) emg3d.save(os.path.join(tmpdir, 'model.npz'), model=self.model, mesh=self.grid, verb=0) args_dict = self.args_dict.copy() args_dict['path'] = tmpdir args_dict['verbosity'] = -1 cli.run.simulation(args_dict) args_dict = self.args_dict.copy() args_dict['path'] = tmpdir args_dict['config'] = 'bla' args_dict['verbosity'] = 2 _, _ = capsys.readouterr() with pytest.raises(SystemExit) as e: cli.run.simulation(args_dict) assert e.type == SystemExit assert "* ERROR :: Config file not found: " in e.value.code # Missing survey. args_dict = self.args_dict.copy() args_dict['path'] = tmpdir args_dict['survey'] = 'wrong' with pytest.raises(SystemExit) as e: cli.run.simulation(args_dict) assert e.type == SystemExit assert "* ERROR :: Survey file not found: " in e.value.code assert "wrong" in e.value.code # Missing model. args_dict = self.args_dict.copy() args_dict['path'] = tmpdir args_dict['model'] = 'phantommodel' with pytest.raises(SystemExit) as e: cli.run.simulation(args_dict) assert e.type == SystemExit assert "* ERROR :: Model file not found: " in e.value.code assert "phantommodel" in e.value.code # Missing output directory. args_dict = self.args_dict.copy() args_dict['path'] = tmpdir args_dict['output'] = join('phantom', 'output', 'dir.npz') args_dict['save'] = join('phantom', 'simulation', 'save.npz') with pytest.raises(SystemExit) as e: cli.run.simulation(args_dict) assert e.type == SystemExit assert "* ERROR :: Output directory does not exist: " in e.value.code assert join("phantom", "output") in e.value.code assert join("phantom", "simulation") in e.value.code def test_run(self, tmpdir, capsys): # Write a config file. config = os.path.join(tmpdir, 'emg3d.cfg') with open(config, 'w') as f: f.write("[files]\n") f.write("save=mysim.npz\n") f.write("[solver_opts]\n") f.write("sslsolver=False\n") f.write("semicoarsening=False\n") f.write("linerelaxation=False\n") f.write("maxit=1\n") # Store survey and model. self.survey.to_file(os.path.join(tmpdir, 'survey.npz'), verb=1) emg3d.save(os.path.join(tmpdir, 'model.npz'), model=self.model, mesh=self.grid, verb=1) # Run a dry run (to output.npz). args_dict = self.args_dict.copy() args_dict['config'] = os.path.join(tmpdir, 'emg3d.cfg') args_dict['path'] = tmpdir cli.run.simulation(args_dict) # Actually run one iteration (to output2.npz). args_dict = self.args_dict.copy() args_dict['config'] = os.path.join(tmpdir, 'emg3d.cfg') args_dict['path'] = tmpdir args_dict['dry_run'] = False args_dict['output'] = 'output2.npz' cli.run.simulation(args_dict) # Ensure dry_run returns same shaped data as the real thing. res1 = emg3d.load(os.path.join(tmpdir, 'output.npz')) res2 = emg3d.load(os.path.join(tmpdir, 'output2.npz')) assert_allclose(res1['data'].shape, res2['data'].shape) assert_allclose(res1['misfit'].shape, res2['misfit'].shape) assert_allclose(res1['gradient'].shape, res2['gradient'].shape) # Assert we can load the simulation emg3d.Simulation.from_file(os.path.join(tmpdir, 'mysim.npz')) assert res1['n_observations'] == np.isfinite(self.data).sum() assert res2['n_observations'] == np.isfinite(self.data).sum() # Actually run one iteration (to output2.npz). args_dict = self.args_dict.copy() args_dict['config'] = os.path.join(tmpdir, 'emg3d.cfg') args_dict['path'] = tmpdir args_dict['forward'] = True args_dict['gradient'] = False args_dict['dry_run'] = False args_dict['output'] = 'output3.npz' cli.run.simulation(args_dict) res3 = emg3d.load(os.path.join(tmpdir, 'output3.npz')) assert 'misfit' not in res3 assert 'gradient' not in res3 # Redo for misfit, loading existing simulation. args_dict = self.args_dict.copy() args_dict['config'] = os.path.join(tmpdir, 'emg3d.cfg') args_dict['path'] = tmpdir args_dict['forward'] = False args_dict['misfit'] = True args_dict['gradient'] = False args_dict['dry_run'] = False args_dict['load'] = 'mysim.npz' args_dict['output'] = 'output3.npz' cli.run.simulation(args_dict) res3 = emg3d.load(os.path.join(tmpdir, 'output3.npz')) assert 'misfit' in res3 assert 'gradient' not in res3 def test_data(self, tmpdir, capsys): # Write a config file; remove_empty=False (default) config = os.path.join(tmpdir, 'emg3d.cfg') with open(config, 'w') as f: f.write("[data]\n") f.write("sources=TxED-1\n") f.write("receivers=RxEP-05, RxEP-10, RxEP-02, RxEP-12, RxEP-06\n") f.write("frequencies=f-1") # Store survey and model. self.survey.to_file(os.path.join(tmpdir, 'survey.npz'), verb=1) emg3d.save(os.path.join(tmpdir, 'model.npz'), model=self.model, mesh=self.grid, verb=1) # Run a dry run (to output.npz). args_dict = self.args_dict.copy() args_dict['config'] = os.path.join(tmpdir, 'emg3d.cfg') args_dict['path'] = tmpdir cli.run.simulation(args_dict.copy()) # Ensure dry_run returns same shaped data as the real thing. res = emg3d.load(os.path.join(tmpdir, 'output.npz')) assert_allclose(res['data'].shape, (1, 5, 1)) assert res['n_observations'] == 4 # Append config file with: remove_empty=True with open(config, 'a') as f: f.write("\nremove_empty=True") # Run a dry run (to output.npz). cli.run.simulation(args_dict) # Ensure dry_run returns same shaped data as the real thing. res = emg3d.load(os.path.join(tmpdir, 'output.npz')) assert_allclose(res['data'].shape, (1, 4, 1)) assert res['n_observations'] == 4
class TestGradient: if xarray is not None: # Create a simple mesh. hx = np.ones(64) * 100 mesh = emg3d.TensorMesh([hx, hx, hx], origin=[0, 0, 0]) # Define a simple survey, including 1 el. & 1 magn. receiver survey = emg3d.Survey( sources=emg3d.TxElectricDipole((1680, 3220, 3210, 20, 5)), receivers=[ emg3d.RxElectricPoint((4751, 3280, 3220, 33, 14)), emg3d.RxMagneticPoint((4758, 3230, 3250, 15, 70)), ], frequencies=1.0, relative_error=0.01, ) # Background Model con_init = np.ones(mesh.shape_cells) # Target Model 1: One Block con_true = np.ones(mesh.shape_cells) con_true[27:37, 27:37, 15:25] = 0.001 model_init = emg3d.Model(mesh, con_init, mapping='Conductivity') model_true = emg3d.Model(mesh, con_true, mapping='Conductivity') # mesh.plot_3d_slicer(con_true) # For debug / QC, needs discretize sim_inp = { 'survey': survey, 'solver_opts': { 'plain': True, 'tol': 5e-5 }, # Red. tol 4 speed 'max_workers': 1, 'gridding': 'same', 'verb': 0, 'receiver_interpolation': 'linear', } # Compute data (pre-computed and passed to Survey above) sim_data = simulations.Simulation(model=model_true, **sim_inp) sim_data.compute(observed=True) def test_errors(self): mesh = emg3d.TensorMesh([[2, 2], [2, 2], [2, 2]], origin=(-1, -1, -1)) survey = emg3d.Survey( sources=emg3d.TxElectricDipole((-1.5, 0, 0, 0, 0)), receivers=emg3d.RxElectricPoint((1.5, 0, 0, 0, 0)), frequencies=1.0, relative_error=0.01, ) sim_inp = { 'survey': survey, 'gridding': 'same', 'receiver_interpolation': 'linear' } # Anisotropic models. simulation = simulations.Simulation(model=emg3d.Model(mesh, 1, 2, 3), **sim_inp) with pytest.raises(NotImplementedError, match='for isotropic models'): simulation.gradient # Model with electric permittivity. simulation = simulations.Simulation(model=emg3d.Model(mesh, epsilon_r=3), **sim_inp) with pytest.raises(NotImplementedError, match='for el. permittivity'): simulation.gradient # Model with magnetic permeability. simulation = simulations.Simulation(model=emg3d.Model( mesh, mu_r=np.ones(mesh.shape_cells) * np.pi), **sim_inp) with pytest.raises(NotImplementedError, match='for magn. permeabili'): simulation.gradient def test_as_vs_fd_gradient(self, capsys): # Compute adjoint state misfit and gradient sim = simulations.Simulation(model=self.model_init, **self.sim_inp) data_misfit = sim.misfit grad = sim.gradient # For Debug / QC, needs discretize # from matplotlib.colors import LogNorm, SymLogNorm # mesh.plot_3d_slicer( # grad.ravel('F'), # pcolor_opts={ # 'cmap': 'RdBu_r', # 'norm': SymLogNorm(linthresh=1e-2, base=10, # vmin=-1e1, vmax=1e1)} # ) # We test a random cell from the inline xz slice, where the grad > 3. # # The NRMSD is (should) be below 1 %. However, (a) close to the # boundary, (b) in regions where the gradient is almost zero, and (c) # in regions where the gradient changes sign the NRMSD can become # large. This is mainly due to numerics, our coarse mesh, and the # reduced tolerance (which we reduced for speed). iy = 32 indices = np.argwhere(abs(grad[:, iy, :]) > 3) ix, iz = indices[np.random.randint(indices.shape[0])] nrmsd = alternatives.fd_vs_as_gradient((ix, iy, iz), self.model_init, grad, data_misfit, self.sim_inp) assert nrmsd < 0.3 @pytest.mark.skipif(discretize is None, reason="discretize not installed.") @pytest.mark.skipif(h5py is None, reason="h5py not installed.") def test_adjoint(self, tmpdir): sim = simulations.Simulation(model=self.model_init, file_dir=str(tmpdir), **self.sim_inp) v = np.random.rand(self.mesh.n_cells).reshape(self.mesh.shape_cells) w = np.random.rand(self.survey.size).reshape(self.survey.shape) wtJv = np.vdot(w, sim.jvec(v)).real vtJtw = np.vdot(v, sim.jtvec(w).ravel('F')) assert abs(wtJv - vtJtw) < 1e-10 @pytest.mark.skipif(discretize is None, reason="discretize not installed.") @pytest.mark.skipif(h5py is None, reason="h5py not installed.") def test_misfit(self, tmpdir): sim = simulations.Simulation(model=self.model_init, file_dir=str(tmpdir), **self.sim_inp) m0 = 2 * sim.model.property_x def func2(x): sim.model.property_x[...] = m0 return sim.jvec(x) def func1(x): sim.model.property_x[...] = x.reshape(sim.model.grid.shape_cells) sim.clean('computed') # Quick test that clean() removes the files assert len(os.listdir(tmpdir)) == 0 sim.compute() return sim.data.synthetic.data, func2 assert discretize.tests.check_derivative( func1, m0, plotIt=False, num=3, )
def test_print_solver(self, capsys): grid = emg3d.TensorMesh(h=[[(25, 10, -1.04), (25, 28), (25, 10, 1.04)], [(50, 8, -1.03), (50, 16), (50, 8, 1.03)], [(30, 8, -1.05), (30, 16), (30, 8, 1.05)]], origin='CCC') model = emg3d.Model(grid, property_x=1.5, property_y=1.8, property_z=3.3, mapping='Resistivity') sources = emg3d.TxElectricDipole((0, 0, 0, 0, 0)) receivers = [ emg3d.RxElectricPoint((x, 0, 0, 0, 0)) for x in [-10000, 10000] ] survey = emg3d.Survey( name='Test', sources=sources, receivers=receivers, frequencies=1.0, noise_floor=1e-15, relative_error=0.05, ) inp = { 'name': 'Test', 'survey': survey, 'model': model, 'gridding': 'same' } sol = { 'sslsolver': False, 'semicoarsening': False, 'linerelaxation': False, 'maxit': 1 } # Errors but verb=-1. _, _ = capsys.readouterr() # empty simulation = simulations.Simulation(verb=-1, **inp, solver_opts=sol) simulation.compute() out, _ = capsys.readouterr() assert out == "" # Errors with verb=0. out = simulation.print_solver_info('efield', verb=0, return_info=True) assert "= Source TxED-1; Frequency 1.0 Hz = MAX. ITERATION REAC" in out # Errors with verb=1. _, _ = capsys.readouterr() # empty simulation.print_solver_info('efield', verb=1) out, _ = capsys.readouterr() assert "= Source TxED-1; Frequency 1.0 Hz = 2.6e-02; 1; 0:00:" in out # No errors; solver-verb 3. simulation = simulations.Simulation(verb=1, **inp, solver_opts={'verb': 3}) simulation.compute() out, _ = capsys.readouterr() assert 'MG-cycle' in out assert 'CONVERGED' in out # No errors; solver-verb 0. simulation = simulations.Simulation(verb=1, **inp, solver_opts={'verb': 0}) simulation.compute() out, _ = capsys.readouterr() assert "= Source TxED-1; Frequency 1.0 Hz = CONVERGED" in out # Two sources, only compute 1, assure printing works. sources = [emg3d.TxElectricDipole((x, 0, 0, 0, 0)) for x in [0, 10]] survey = emg3d.Survey( name='Test', sources=sources, receivers=receivers, frequencies=1.0, noise_floor=1e-15, relative_error=0.05, ) inp = { 'name': 'Test', 'survey': survey, 'model': model, 'gridding': 'same' } simulation = simulations.Simulation(**inp, solver_opts={'verb': 0}) _ = simulation.get_efield('TxED-2', 'f-1') simulation.print_solver_info(verb=1) out, _ = capsys.readouterr() assert "= Source TxED-2; Frequency 1.0 Hz = CONVERGED" in out
def test_simulation_automatic(self, capsys): # Create a simple survey sources = emg3d.surveys.txrx_coordinates_to_dict( emg3d.TxElectricDipole, (0, [1000, 3000, 5000], -950, 0, 0)) receivers = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, ([-3000, 0, 3000], [0, 3000, 6000], -1000, 0, 0)) frequencies = (0.1, 1.0, 10.0) survey = emg3d.Survey(sources, receivers, frequencies, name='Test', noise_floor=1e-15, relative_error=0.05) # Create a simple grid and model grid = emg3d.TensorMesh( [np.ones(32) * 250, np.ones(16) * 500, np.ones(4) * 500], np.array([-1250, -1250, -2250])) model = emg3d.Model(grid, 1) # Create a simulation, compute all fields. inp = { 'survey': survey, 'model': model, 'gridding_opts': { 'expand': [1, 0.5], 'seasurface': 0, 'verb': 1 } } b_sim = simulations.Simulation(name='both', gridding='both', **inp) f_sim = simulations.Simulation(name='freq', gridding='frequency', **inp) t_sim = simulations.Simulation(name='src', gridding='source', **inp) s_sim = simulations.Simulation(name='single', gridding='single', **inp) # Quick repr test. assert " 24 x 24 (13,824) - 160 x 160 x 96 (2,457," in b_sim.__repr__() assert " 24 x 24 (13,824) - 160 x 160 x 96 (2,457," in f_sim.__repr__() assert "Source-dependent grids; 64 x 64 x 40 (163," in t_sim.__repr__() assert "ources and frequencies; 64 x 64 x 40 (163," in s_sim.__repr__() # Quick print_grid test: _, _ = capsys.readouterr() # empty b_sim.print_grid_info() out, _ = capsys.readouterr() assert "= Source: TxED-1; Frequency: 10.0 Hz =" in out assert "== GRIDDING IN X ==" in out assert b_sim.get_grid('TxED-1', 1.0).__repr__() in out _, _ = capsys.readouterr() # empty out = f_sim.print_grid_info(return_info=True) out2, _ = capsys.readouterr() assert out2 == "" assert "= Source: all" in out assert "== GRIDDING IN X ==" in out assert f_sim.get_grid('TxED-3', 1.0).__repr__() in out t_sim.print_grid_info() out, _ = capsys.readouterr() assert "; Frequency: all" in out assert "== GRIDDING IN X ==" in out assert t_sim.get_grid('TxED-1', 10.0).__repr__() in out _, _ = capsys.readouterr() # empty out = s_sim.print_grid_info(return_info=True) out2, _ = capsys.readouterr() assert out2 == "" assert "= Source: all; Frequency: all =" in out assert "== GRIDDING IN X ==" in out assert s_sim.get_grid('TxED-3', 0.1).__repr__() in out assert s_sim.print_grid_info(verb=-1) is None # Grids: Middle source / middle frequency should be the same in all. assert f_sim.get_grid('TxED-2', 1.0) == t_sim.get_grid('TxED-2', 1.0) assert f_sim.get_grid('TxED-2', 1.0) == s_sim.get_grid('TxED-2', 1.0) assert f_sim.get_grid('TxED-2', 1.0) == b_sim.get_grid('TxED-2', 1.0) assert f_sim.get_model('TxED-2', 1.0) == t_sim.get_model('TxED-2', 1.0) assert f_sim.get_model('TxED-2', 1.0) == s_sim.get_model('TxED-2', 1.0) assert f_sim.get_model('TxED-2', 1.0) == b_sim.get_model('TxED-2', 1.0) # Copy: f_sim_copy = f_sim.copy() assert (f_sim.get_grid('TxED-1', 1.0) == f_sim_copy.get_grid('TxED-1', 1.0)) assert 'expand' not in f_sim.gridding_opts.keys() assert 'expand' not in f_sim_copy.gridding_opts.keys() s_sim_copy = s_sim.copy() assert (s_sim.get_grid('TxED-1', 1.0) == s_sim_copy.get_grid('TxED-1', 1.0)) assert 'expand' not in s_sim.gridding_opts.keys() assert 'expand' not in s_sim_copy.gridding_opts.keys() t_sim_copy = t_sim.copy() assert (t_sim.get_grid('TxED-1', 1.0) == t_sim_copy.get_grid('TxED-1', 1.0)) assert 'expand' not in t_sim.gridding_opts.keys() assert 'expand' not in t_sim_copy.gridding_opts.keys()
class TestSimulation(): if xarray is not None: # Create a simple survey # Sources: 1 Electric Dipole, 1 Magnetic Dipole, 1 Electric Wire. s1 = emg3d.TxElectricDipole((0, 1000, -950, 0, 0)) s2 = emg3d.TxMagneticDipole((0, 3000, -950, 90, 0)) s3 = emg3d.TxElectricWire(([0, 4900, -950], [-20, 5000, -950], [20, 5100, -950])) sources = emg3d.surveys.txrx_lists_to_dict([s1, s2, s3]) # Receivers: 1 Electric Point, 1 Magnetic Point e_rec = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, (np.arange(6) * 1000, 0, -1000, 0, 0)) m_rec = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxMagneticPoint, (np.arange(6) * 1000, 0, -1000, 90, 0)) receivers = emg3d.surveys.txrx_lists_to_dict([e_rec, m_rec]) # Frequencies frequencies = (1.0, 2.0) survey = emg3d.Survey(sources, receivers, frequencies, name='TestSurv', noise_floor=1e-15, relative_error=0.05) # Create a simple grid and model grid = emg3d.TensorMesh( [np.ones(32) * 250, np.ones(16) * 500, np.ones(16) * 500], np.array([-1250, -1250, -2250])) model = emg3d.Model(grid, 1) # Create a simulation, compute all fields. simulation = simulations.Simulation(survey, model, name='TestSim', max_workers=1, solver_opts={ 'maxit': 1, 'verb': 0, 'sslsolver': False, 'linerelaxation': False, 'semicoarsening': False }, gridding='same') # Do first one single and then all together. simulation.get_efield('TxED-1', 'f-1') simulation.compute(observed=True, min_offset=1100) def test_same(self): assert self.simulation.get_model('TxMD-2', 'f-1') == self.model assert self.simulation.get_grid('TxEW-3', 1.0) == self.grid def test_fields(self, capsys): # Check sfield sfield = emg3d.get_source_field(self.grid, self.survey.sources['TxEW-3'], frequency=1.0) # Check efield efield, info = emg3d.solve(self.model, sfield, **self.simulation.solver_opts) assert self.simulation.get_efield('TxEW-3', 'f-1') == efield # See a single one self.simulation._dict_efield['TxEW-3'][1.0] = None _, _ = capsys.readouterr() self.simulation.get_efield('TxEW-3', 1.0) info = self.simulation.get_efield_info('TxEW-3', 1.0) assert 'MAX. ITERATION REACHED, NOT CONVERGED' in info['exit_message'] # Check hfield hfield = emg3d.get_magnetic_field(self.model, efield) assert self.simulation.get_hfield('TxEW-3', 1.0) == hfield s_hfield = self.simulation.get_hfield('TxEW-3', 1.0) assert s_hfield == hfield assert_allclose( self.simulation._dict_efield_info['TxEW-3']['f-1']['abs_error'], info['abs_error']) assert_allclose( self.simulation._dict_efield_info['TxEW-3']['f-1']['rel_error'], info['rel_error']) exit = self.simulation._dict_efield_info['TxEW-3']['f-1']['exit'] assert exit == info['exit'] == 1 # First hfield, ensure efield/hfield get computed. sim = self.simulation.copy(what='all') sim._dict_efield['TxEW-3']['f-1'] = None sim.get_hfield('TxEW-3', 'f-1') assert sim._dict_efield['TxEW-3']['f-1'] is not None def test_responses(self): # Check min_offset were switched-off assert_allclose(self.simulation.data.observed[0, 0, 0].data, np.nan) assert_allclose(self.simulation.data.observed[0, 6, 0].data, np.nan) # Get efield responses e_resp = self.simulation.get_efield('TxMD-2', 1.0).get_receiver( self.survey.receivers.values()) assert_allclose(self.simulation.data.synthetic[1, :6, 0].data, e_resp[:6], atol=1e-16) # Get hfield responses m_resp = self.simulation.get_hfield('TxMD-2', 1.0).get_receiver( self.survey.receivers.values()) assert_allclose(self.simulation.data.synthetic[1, 6:, 0].data, m_resp[6:], atol=1e-16) def test_errors(self): with pytest.raises(TypeError, match='Unexpected '): simulations.Simulation(self.survey, self.model, unknown=True, name='Test2') # gridding='same' with gridding_opts. with pytest.raises(TypeError, match="`gridding_opts` is not permitt"): simulations.Simulation(self.survey, self.model, name='Test', gridding='same', gridding_opts={'bummer': True}) # expand without seasurface with pytest.raises(KeyError, match="is required if"): simulations.Simulation(self.survey, self.model, name='Test', gridding='single', gridding_opts={'expand': [1, 2]}) def test_reprs(self): test = self.simulation.__repr__() assert "Simulation «TestSim»" in test assert "Survey «TestSurv»: 3 sources; 12 receivers; 2 frequenc" in test assert "Model: resistivity; isotropic; 32 x 16 x 16 (8,192)" in test assert "Gridding: Same grid as for model" in test test = self.simulation._repr_html_() assert "Simulation «TestSim»" in test assert "Survey «TestSurv»: 3 sources; 12 receivers; 2" in test assert "Model: resistivity; isotropic; 32 x 16 x 16 (8,192)" in test assert "Gridding: Same grid as for model" in test def test_copy(self, tmpdir): with pytest.raises(TypeError, match="Unrecognized `what`: nothing"): self.simulation.copy('nothing') sim2 = self.simulation.copy() assert self.simulation.name == sim2.name assert self.simulation.survey.sources == sim2.survey.sources assert_allclose( self.simulation.get_efield('TxED-1', 1.0).field, sim2.get_efield('TxED-1', 1.0).field) # Also check to_file()/from_file(). sim_dict = self.simulation.to_dict('all') sim2 = simulations.Simulation.from_dict(sim_dict.copy()) assert self.simulation.name == sim2.name assert self.simulation.survey.name == sim2.survey.name assert self.simulation.max_workers == sim2.max_workers assert self.simulation.gridding == sim2.gridding assert self.simulation.model == sim2.model del sim_dict['survey'] with pytest.raises(KeyError, match="'survey'"): simulations.Simulation.from_dict(sim_dict) # Also check to_file()/from_file(). self.simulation.to_file(tmpdir + '/test.npz', what='all') sim2 = simulations.Simulation.from_file(tmpdir + '/test.npz') assert self.simulation.name == sim2.name assert self.simulation.survey.name == sim2.survey.name assert self.simulation.max_workers == sim2.max_workers assert self.simulation.gridding == sim2.gridding assert self.simulation.model == sim2.model sim9 = simulations.Simulation.from_file(tmpdir + '/test.npz', verb=-1) assert sim2.name == sim9[0].name assert 'Data loaded from' in sim9[1] # Clean and ensure it is empty sim3 = self.simulation.copy() sim3.clean('all') assert sim3._dict_efield['TxMD-2']['f-1'] is None assert sim3._dict_efield_info['TxMD-2']['f-1'] is None with pytest.raises(TypeError, match="Unrecognized `what`: nothing"): sim3.clean('nothing') def test_dicts_provided(self): grids = self.simulation._dict_grid.copy() # dict sim1 = simulations.Simulation(self.survey, self.model, gridding='dict', gridding_opts=grids, name='Test2') m1 = sim1.get_model('TxEW-3', 1.0) g1 = sim1.get_grid('TxEW-3', 1.0) # provide sim2 = simulations.Simulation(self.survey, self.model, name='Test2', gridding='input', gridding_opts=grids['TxEW-3']['f-1']) m2 = sim2.get_model('TxEW-3', 1.0) g2 = sim2.get_grid('TxEW-3', 1.0) assert m1 == m2 assert g1 == g2 # to/from_dict sim1copy = sim1.copy() sim2copy = sim2.copy() m1c = sim1copy.get_model('TxEW-3', 1.0) g1c = sim1copy.get_grid('TxEW-3', 1.0) m2c = sim2copy.get_model('TxEW-3', 1.0) g2c = sim2copy.get_grid('TxEW-3', 1.0) assert m1 == m1c assert g1 == g1c assert m2 == m2c assert g2 == g2c def test_grid_provided(self): # Check bad grid hx = np.ones(17) * 20 grid = emg3d.TensorMesh([hx, hx, hx], (0, 0, 0)) with pytest.warns(UserWarning, match='optimal for MG solver. Good n'): simulations.Simulation(self.survey, self.model, gridding='input', gridding_opts=grid) def test_synthetic(self): sim = self.simulation.copy() sim._dict_efield = sim._dict_initiate # Reset sim.compute(observed=True, add_noise=False) assert_allclose(sim.data.synthetic, sim.data.observed) assert sim.survey.size == sim.data.observed.size def test_input_gradient(self): # Create another mesh, so there will be a difference. newgrid = emg3d.TensorMesh( [np.ones(16) * 500, np.ones(8) * 1000, np.ones(8) * 1000], np.array([-1250, -1250, -2250])) simulation = simulations.Simulation(self.survey, self.model, max_workers=1, solver_opts={ 'maxit': 1, 'verb': 0, 'sslsolver': False, 'linerelaxation': False, 'semicoarsening': False }, gridding='input', gridding_opts=newgrid, name='TestX') with pytest.warns(UserWarning, match='Receiver responses were obtain'): grad = simulation.gradient with pytest.warns(UserWarning, match='Receiver responses were obtain'): vec = simulation.data.residual.data.copy() vec *= simulation.data.weights.data jtvec = simulation.jtvec(vec) assert_allclose(grad, jtvec) jvec = simulation.jvec(np.ones(newgrid.n_cells)) assert jvec.shape == simulation.data.observed.data.shape # Ensure the gradient has the shape of the model, not of the input. assert grad.shape == self.model.shape sim2 = simulation.to_dict(what='all', copy=True) sim3 = simulation.to_dict(what='plain', copy=True) assert 'residual' in sim2['survey']['data'].keys() assert 'residual' not in sim3['survey']['data'].keys() simulation.clean('all') # Should remove 'residual', 'bfield-dicts' sim5 = simulation.to_dict('all') assert 'residual' not in sim5['survey']['data'].keys() assert '_dict_bfield' not in sim5.keys() def test_simulation_automatic(self, capsys): # Create a simple survey sources = emg3d.surveys.txrx_coordinates_to_dict( emg3d.TxElectricDipole, (0, [1000, 3000, 5000], -950, 0, 0)) receivers = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, ([-3000, 0, 3000], [0, 3000, 6000], -1000, 0, 0)) frequencies = (0.1, 1.0, 10.0) survey = emg3d.Survey(sources, receivers, frequencies, name='Test', noise_floor=1e-15, relative_error=0.05) # Create a simple grid and model grid = emg3d.TensorMesh( [np.ones(32) * 250, np.ones(16) * 500, np.ones(4) * 500], np.array([-1250, -1250, -2250])) model = emg3d.Model(grid, 1) # Create a simulation, compute all fields. inp = { 'survey': survey, 'model': model, 'gridding_opts': { 'expand': [1, 0.5], 'seasurface': 0, 'verb': 1 } } b_sim = simulations.Simulation(name='both', gridding='both', **inp) f_sim = simulations.Simulation(name='freq', gridding='frequency', **inp) t_sim = simulations.Simulation(name='src', gridding='source', **inp) s_sim = simulations.Simulation(name='single', gridding='single', **inp) # Quick repr test. assert " 24 x 24 (13,824) - 160 x 160 x 96 (2,457," in b_sim.__repr__() assert " 24 x 24 (13,824) - 160 x 160 x 96 (2,457," in f_sim.__repr__() assert "Source-dependent grids; 64 x 64 x 40 (163," in t_sim.__repr__() assert "ources and frequencies; 64 x 64 x 40 (163," in s_sim.__repr__() # Quick print_grid test: _, _ = capsys.readouterr() # empty b_sim.print_grid_info() out, _ = capsys.readouterr() assert "= Source: TxED-1; Frequency: 10.0 Hz =" in out assert "== GRIDDING IN X ==" in out assert b_sim.get_grid('TxED-1', 1.0).__repr__() in out _, _ = capsys.readouterr() # empty out = f_sim.print_grid_info(return_info=True) out2, _ = capsys.readouterr() assert out2 == "" assert "= Source: all" in out assert "== GRIDDING IN X ==" in out assert f_sim.get_grid('TxED-3', 1.0).__repr__() in out t_sim.print_grid_info() out, _ = capsys.readouterr() assert "; Frequency: all" in out assert "== GRIDDING IN X ==" in out assert t_sim.get_grid('TxED-1', 10.0).__repr__() in out _, _ = capsys.readouterr() # empty out = s_sim.print_grid_info(return_info=True) out2, _ = capsys.readouterr() assert out2 == "" assert "= Source: all; Frequency: all =" in out assert "== GRIDDING IN X ==" in out assert s_sim.get_grid('TxED-3', 0.1).__repr__() in out assert s_sim.print_grid_info(verb=-1) is None # Grids: Middle source / middle frequency should be the same in all. assert f_sim.get_grid('TxED-2', 1.0) == t_sim.get_grid('TxED-2', 1.0) assert f_sim.get_grid('TxED-2', 1.0) == s_sim.get_grid('TxED-2', 1.0) assert f_sim.get_grid('TxED-2', 1.0) == b_sim.get_grid('TxED-2', 1.0) assert f_sim.get_model('TxED-2', 1.0) == t_sim.get_model('TxED-2', 1.0) assert f_sim.get_model('TxED-2', 1.0) == s_sim.get_model('TxED-2', 1.0) assert f_sim.get_model('TxED-2', 1.0) == b_sim.get_model('TxED-2', 1.0) # Copy: f_sim_copy = f_sim.copy() assert (f_sim.get_grid('TxED-1', 1.0) == f_sim_copy.get_grid('TxED-1', 1.0)) assert 'expand' not in f_sim.gridding_opts.keys() assert 'expand' not in f_sim_copy.gridding_opts.keys() s_sim_copy = s_sim.copy() assert (s_sim.get_grid('TxED-1', 1.0) == s_sim_copy.get_grid('TxED-1', 1.0)) assert 'expand' not in s_sim.gridding_opts.keys() assert 'expand' not in s_sim_copy.gridding_opts.keys() t_sim_copy = t_sim.copy() assert (t_sim.get_grid('TxED-1', 1.0) == t_sim_copy.get_grid('TxED-1', 1.0)) assert 'expand' not in t_sim.gridding_opts.keys() assert 'expand' not in t_sim_copy.gridding_opts.keys() def test_print_solver(self, capsys): grid = emg3d.TensorMesh(h=[[(25, 10, -1.04), (25, 28), (25, 10, 1.04)], [(50, 8, -1.03), (50, 16), (50, 8, 1.03)], [(30, 8, -1.05), (30, 16), (30, 8, 1.05)]], origin='CCC') model = emg3d.Model(grid, property_x=1.5, property_y=1.8, property_z=3.3, mapping='Resistivity') sources = emg3d.TxElectricDipole((0, 0, 0, 0, 0)) receivers = [ emg3d.RxElectricPoint((x, 0, 0, 0, 0)) for x in [-10000, 10000] ] survey = emg3d.Survey( name='Test', sources=sources, receivers=receivers, frequencies=1.0, noise_floor=1e-15, relative_error=0.05, ) inp = { 'name': 'Test', 'survey': survey, 'model': model, 'gridding': 'same' } sol = { 'sslsolver': False, 'semicoarsening': False, 'linerelaxation': False, 'maxit': 1 } # Errors but verb=-1. _, _ = capsys.readouterr() # empty simulation = simulations.Simulation(verb=-1, **inp, solver_opts=sol) simulation.compute() out, _ = capsys.readouterr() assert out == "" # Errors with verb=0. out = simulation.print_solver_info('efield', verb=0, return_info=True) assert "= Source TxED-1; Frequency 1.0 Hz = MAX. ITERATION REAC" in out # Errors with verb=1. _, _ = capsys.readouterr() # empty simulation.print_solver_info('efield', verb=1) out, _ = capsys.readouterr() assert "= Source TxED-1; Frequency 1.0 Hz = 2.6e-02; 1; 0:00:" in out # No errors; solver-verb 3. simulation = simulations.Simulation(verb=1, **inp, solver_opts={'verb': 3}) simulation.compute() out, _ = capsys.readouterr() assert 'MG-cycle' in out assert 'CONVERGED' in out # No errors; solver-verb 0. simulation = simulations.Simulation(verb=1, **inp, solver_opts={'verb': 0}) simulation.compute() out, _ = capsys.readouterr() assert "= Source TxED-1; Frequency 1.0 Hz = CONVERGED" in out # Two sources, only compute 1, assure printing works. sources = [emg3d.TxElectricDipole((x, 0, 0, 0, 0)) for x in [0, 10]] survey = emg3d.Survey( name='Test', sources=sources, receivers=receivers, frequencies=1.0, noise_floor=1e-15, relative_error=0.05, ) inp = { 'name': 'Test', 'survey': survey, 'model': model, 'gridding': 'same' } simulation = simulations.Simulation(**inp, solver_opts={'verb': 0}) _ = simulation.get_efield('TxED-2', 'f-1') simulation.print_solver_info(verb=1) out, _ = capsys.readouterr() assert "= Source TxED-2; Frequency 1.0 Hz = CONVERGED" in out def test_rel_abs_rec(self): # Sources sources = emg3d.surveys.txrx_coordinates_to_dict( emg3d.TxElectricDipole, ([0, 100, 200], 0, 0, 0, 0)) # Abs and rel Receivers a_e_rec = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, (1000 + np.arange(3) * 100, 0, -100, 0, 0)) r_e_rec = emg3d.surveys.txrx_coordinates_to_dict(emg3d.RxElectricPoint, (1000, 0, -100, 0, 0), relative=True) a_h_rec = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxMagneticPoint, (1000 + np.arange(3) * 100, 0, -100, 0, 0)) r_h_rec = emg3d.surveys.txrx_coordinates_to_dict(emg3d.RxMagneticPoint, (1000, 0, -100, 0, 0), relative=True) receivers = emg3d.surveys.txrx_lists_to_dict( [a_e_rec, r_e_rec, a_h_rec, r_h_rec]) # Frequencies frequencies = (1.0) survey = emg3d.Survey(sources, receivers, frequencies, name='TestSurv', noise_floor=1e-15, relative_error=0.05) # Create a simple grid and model grid = emg3d.TensorMesh( [np.ones(32) * 250, np.ones(16) * 500, np.ones(16) * 500], np.array([-1250, -1250, -2250])) model = emg3d.Model(grid, 1) # Create a simulation, compute all fields. simulation = simulations.Simulation(survey, model, name='TestSim', max_workers=1, solver_opts={ 'maxit': 1, 'verb': 0, 'plain': True }, gridding='same') simulation.compute() # Relative receivers must be same as corresponding absolute receivers assert_allclose( [simulation.data.synthetic[i, i, 0].data for i in range(3)], simulation.data.synthetic[:, 3, 0].data) assert_allclose( [simulation.data.synthetic[i, i + 4, 0].data for i in range(3)], simulation.data.synthetic[:, 7, 0].data)
class TestEstimateGriddingOpts(): if xarray is not None: # Create a simple survey sources = emg3d.surveys.txrx_coordinates_to_dict( emg3d.TxElectricDipole, (0, [1000, 3000, 5000], -950, 0, 0)) receivers = emg3d.surveys.txrx_coordinates_to_dict( emg3d.RxElectricPoint, (np.arange(11)*500, 2000, -1000, 0, 0)) frequencies = (0.1, 10.0) survey = emg3d.Survey( sources, receivers, frequencies, noise_floor=1e-15, relative_error=0.05) # Create a simple grid and model grid = meshes.TensorMesh( [np.ones(32)*250, np.ones(16)*500, np.ones(16)*500], np.array([-1250, -1250, -2250])) model = emg3d.Model(grid, 0.1, np.ones(grid.shape_cells)*10) model.property_y[5, 8, 3] = 100000 # Cell at source center def test_empty_dict(self): gdict = meshes.estimate_gridding_opts({}, self.model, self.survey) assert gdict['frequency'] == 1.0 assert gdict['mapping'] == self.model.map.name assert_allclose(gdict['center'], (0, 3000, -950)) assert_allclose(gdict['domain']['x'], (-500, 5500)) assert_allclose(gdict['domain']['y'], (600, 5400)) assert_allclose(gdict['domain']['z'], (-3651, -651)) assert_allclose(gdict['properties'], [100000, 10, 10, 10, 10, 10, 10]) def test_mapping_vector(self): gridding_opts = { 'mapping': "LgConductivity", 'vector': 'xZ', } gdict = meshes.estimate_gridding_opts( gridding_opts, self.model, self.survey) assert_allclose( gdict['properties'], np.log10(1/np.array([100000, 10, 10, 10, 10, 10, 10])), atol=1e-15) assert_allclose(gdict['vector']['x'], self.grid.nodes_x) assert gdict['vector']['y'] is None assert_allclose(gdict['vector']['z'], self.grid.nodes_z) def test_vector_domain_distance(self): gridding_opts = { 'vector': 'Z', 'domain': (None, [-1000, 1000], None), 'distance': [[5, 10], None, None], } gdict = meshes.estimate_gridding_opts( gridding_opts, self.model, self.survey) assert gdict['vector']['x'] == gdict['vector']['y'] is None assert_allclose(gdict['vector']['z'], self.model.grid.nodes_z) assert gdict['domain']['x'] is None assert gdict['domain']['y'] == [-1000, 1000] assert gdict['domain']['z'] == [self.model.grid.nodes_z[0], self.model.grid.nodes_z[-1]] assert gdict['distance']['x'] == [5, 10] assert gdict['distance']['y'] == gdict['distance']['z'] is None # As dict gridding_opts = { 'vector': 'Z', 'domain': {'x': None, 'y': [-1000, 1000], 'z': None}, 'distance': {'x': [5, 10], 'y': None, 'z': None}, } gdict = meshes.estimate_gridding_opts( gridding_opts, self.model, self.survey) assert gdict['vector']['x'] == gdict['vector']['y'] is None assert_allclose(gdict['vector']['z'], self.model.grid.nodes_z) assert gdict['domain']['x'] is None assert gdict['domain']['y'] == [-1000, 1000] assert gdict['domain']['z'] == [self.model.grid.nodes_z[0], self.model.grid.nodes_z[-1]] assert gdict['distance']['x'] == [5, 10] assert gdict['distance']['y'] == gdict['distance']['z'] is None def test_pass_along(self): gridding_opts = { 'vector': {'x': None, 'y': 1, 'z': None}, 'stretching': [1.2, 1.3], 'seasurface': -500, 'cell_numbers': [10, 20, 30], 'lambda_factor': 0.8, 'max_buffer': 10000, 'min_width_limits': ([20, 40], [20, 40], [20, 40]), 'min_width_pps': 4, 'verb': -1, } gdict = meshes.estimate_gridding_opts( gridding_opts.copy(), self.model, self.survey) # Check that all parameters passed unchanged. gdict2 = {k: gdict[k] for k, _ in gridding_opts.items()} # Except the tuple, which should be a dict now gridding_opts['min_width_limits'] = { 'x': gridding_opts['min_width_limits'][0], 'y': gridding_opts['min_width_limits'][1], 'z': gridding_opts['min_width_limits'][2] } assert helpers.compare_dicts(gdict2, gridding_opts) def test_factor(self): sources = emg3d.TxElectricDipole((0, 3000, -950, 0, 0)) receivers = emg3d.RxElectricPoint((0, 3000, -1000, 0, 0)) # Adjusted x-domain. survey = emg3d.Survey( self.sources, receivers, self.frequencies, noise_floor=1e-15, relative_error=0.05) gdict = meshes.estimate_gridding_opts({}, self.model, survey) assert_allclose(gdict['domain']['x'], (-800, 800)) # Adjusted x-domain. survey = emg3d.Survey( sources, self.receivers, self.frequencies, noise_floor=1e-15, relative_error=0.05) gdict = meshes.estimate_gridding_opts({}, self.model, survey) assert_allclose(gdict['domain']['y'], (1500, 3500)) def test_error(self): with pytest.raises(TypeError, match='Unexpected gridding_opts'): _ = meshes.estimate_gridding_opts( {'what': True}, self.model, self.survey)
class TestSaveLoad: frequency = 1.0 grid = emg3d.TensorMesh([[2, 2], [3, 4], [0.5, 2]], (0, 0, 0)) field = emg3d.Field(grid) model = emg3d.Model(grid, 1) tx_e_d = emg3d.TxElectricDipole((0, 1000, 0, 0, -900, -950)) tx_m_d = emg3d.TxMagneticDipole([[0, 0, -900], [1000, 0, -950]]) tx_e_w = emg3d.TxElectricWire(([[0, 0, 0], [1, 1, 1], [1, 0, 1]])) rx_e_p = emg3d.RxElectricPoint((0, 1000, -950, 0, 20)) rx_m_p = emg3d.RxMagneticPoint((0, 1000, -950, 20, 0)) a = np.arange(10.) b = 1+5j data = { 'Grid': grid, 'Model': model, 'Field': field, 'a': a, 'b': b, } if xarray: survey = emg3d.Survey( emg3d.surveys.txrx_lists_to_dict([tx_e_d, tx_m_d, tx_e_w]), emg3d.surveys.txrx_lists_to_dict([rx_e_p, rx_m_p]), frequency ) simulation = emg3d.Simulation(survey, model, gridding='same') data['Survey'] = survey data['Simulation'] = simulation def test_npz(self, tmpdir, capsys): io.save(tmpdir+'/test.npz', **self.data) outstr, _ = capsys.readouterr() assert 'Data saved to «' in outstr assert emg3d.__version__ in outstr # Save it with other verbosity. _, _ = capsys.readouterr() io.save(tmpdir+'/test.npz', **self.data, verb=0) outstr, _ = capsys.readouterr() assert outstr == "" out = io.save(tmpdir+'/test.npz', **self.data, verb=-1) assert 'Data saved to «' in out # Load it. out_npz = io.load(str(tmpdir+'/test.npz'), allow_pickle=True) outstr, _ = capsys.readouterr() assert 'Data loaded from «' in outstr assert 'test.npz' in outstr assert emg3d.__version__ in outstr assert out_npz['Model'] == self.model assert_allclose(out_npz['a'], self.a) assert out_npz['b'] == self.b assert_allclose(self.field.fx, out_npz['Field'].fx) assert_allclose(self.grid.cell_volumes, out_npz['Grid'].cell_volumes) # Load it with other verbosity. _, _ = capsys.readouterr() out = io.load(tmpdir+'/test.npz', verb=0) outstr, _ = capsys.readouterr() assert outstr == "" out, out_str = io.load(tmpdir+'/test.npz', verb=-1) assert 'Data loaded from «' in out_str def test_h5(self, tmpdir): if h5py: io.save(tmpdir+'/test.h5', **self.data) out_h5 = io.load(str(tmpdir+'/test.h5')) assert out_h5['Model'] == self.model assert_allclose(out_h5['a'], self.a) assert out_h5['b'] == self.b assert_allclose(self.field.fx, out_h5['Field'].fx) assert_allclose(self.grid.cell_volumes, out_h5['Grid'].cell_volumes) else: with pytest.warns(UserWarning, match='emg3d: This feature requir'): io.save(tmpdir+'/test.h5', grid=self.grid) def test_json(self, tmpdir): io.save(tmpdir+'/test.json', **self.data) out_json = io.load(str(tmpdir+'/test.json')) assert out_json['Model'] == self.model assert_allclose(out_json['a'], self.a) assert out_json['b'] == self.b assert_allclose(self.field.fx, out_json['Field'].fx) assert_allclose(self.grid.cell_volumes, out_json['Grid'].cell_volumes) def test_warnings(self, tmpdir, capsys): # Check message from loading another file data = io._dict_serialize({'meshes': self.grid}) fdata = io._dict_flatten(data) np.savez_compressed(tmpdir+'/test2.npz', **fdata) _ = io.load(str(tmpdir+'/test2.npz'), allow_pickle=True) outstr, _ = capsys.readouterr() assert "[version/format/date unknown; not created by emg" in outstr # Unknown keyword. with pytest.raises(TypeError, match="Unexpected "): io.load('ttt.npz', stupidkeyword='a') # Unknown extension. with pytest.raises(ValueError, match="Unknown extension '.abc'"): io.save(tmpdir+'/testwrongextension.abc', something=1) with pytest.raises(ValueError, match="Unknown extension '.abc'"): io.load(tmpdir+'/testwrongextension.abc')