def test_runs_warnings(self): # The interpolation happens in maps.interp_spline_3d. # Here we just check the 'other' things: warning and errors, and that # it is composed correctly. # Check cubic spline runs fine (NOT CHECKING ACTUAL VALUES!. grid = emg3d.TensorMesh( [np.ones(4), np.array([1, 2, 3, 1]), np.array([2, 1, 1, 1])], [0, 0, 0]) field = fields.Field(grid) field.field = np.ones( field.field.size) + 1j * np.ones(field.field.size) grid = emg3d.TensorMesh( [np.ones(6), np.array([1, 1, 2, 3, 1]), np.array([1, 2, 1, 1, 1])], [-1, -1, -1]) efield = fields.Field(grid, frequency=1) n = efield.field.size efield.field = np.ones(n) + 1j * np.ones(n) # Provide wrong rec_loc input: with pytest.raises(ValueError, match='`receiver` needs to be in the'): fields.get_receiver(efield, (1, 1, 1))
def test_basic(self): ee = fields.Field(self.grid, self.field) assert_allclose(ee.field, self.field) assert_allclose(ee.fx, self.ex) assert_allclose(ee.fy, self.ey) assert_allclose(ee.fz, self.ez) assert ee.smu0 is None assert ee.sval is None assert ee.frequency is None assert ee.electric assert ee.field.dtype == self.field.dtype assert ee == ee.interpolate_to_grid(ee.grid) # Check representation of Field. assert f"Field: electric; {ee.grid.shape_cells[0]} x" in ee.__repr__() # Test amplitude and phase. assert_allclose(ee.fx.amp(), np.abs(ee.fx)) assert_allclose(ee.fy.pha(unwrap=False), np.angle(ee.fy)) # Test the other possibilities to initiate a Field-instance. frequency = 1.0 ee3 = fields.Field(self.grid, frequency=frequency, dtype=self.field.dtype) assert ee.field.size == ee3.field.size assert ee.field.dtype == np.complex128 assert ee3.frequency == frequency # Try setting values ee3.field = ee.field assert ee3.smu0 / ee3.sval == constants.mu_0 assert ee != ee3 # First has no frequency ee3.fx = ee.fx ee3.fy = ee.fy ee3.fz = ee.fz # Negative ee4 = fields.Field(self.grid, frequency=-frequency) assert ee.field.size == ee4.field.size assert ee4.field.dtype == np.float64 assert ee4.frequency == frequency assert ee4._frequency == -frequency assert ee4.smu0 / ee4.sval == constants.mu_0
def test_get_receiver(self): # We only check here that it gives the same as calling the function # itself; the rest should be tested in get_receiver(). grid1 = emg3d.TensorMesh( [np.ones(8), np.ones(8), np.ones(8)], (0, 0, 0)) ee = fields.Field(grid1) ee.field = np.arange(ee.field.size) + 2j * np.arange(ee.field.size) resp = ee.get_receiver((4, 4, 4, 0, 0)) assert_allclose(resp, 323.5 + 647.0j)
def test_interpolate_to_grid(self): # We only check here that it gives the same as calling the function # itself; the rest should be tested in interpolate(). grid1 = emg3d.TensorMesh( [np.ones(8), np.ones(8), np.ones(8)], (0, 0, 0)) grid2 = emg3d.TensorMesh([[2, 2, 2, 2], [3, 3], [4, 4]], (0, 0, 0)) ee = fields.Field(grid1) ee.field = np.ones(ee.field.size) + 2j * np.ones(ee.field.size) e2 = ee.interpolate_to_grid(grid2) assert_allclose(e2.field, 1 + 2j)
def test_arbitrarily_shaped_source(self): h = np.ones(4) grid = emg3d.TensorMesh([h * 200, h * 400, h * 800], [-400, -800, -1600]) freq = 1.11 strength = np.pi src = (0, 0, 0, 0, 90) # Manually sman = fields.Field(grid, frequency=freq) src4xxyyzz = [ np.r_[src[0] - 0.5, src[0] + 0.5, src[1] - 0.5, src[1] - 0.5, src[2], src[2]], np.r_[src[0] + 0.5, src[0] + 0.5, src[1] - 0.5, src[1] + 0.5, src[2], src[2]], np.r_[src[0] + 0.5, src[0] - 0.5, src[1] + 0.5, src[1] + 0.5, src[2], src[2]], ] for srcl in src4xxyyzz: sman.field += fields.get_source_field(grid, srcl, freq, strength=strength).field # Computed src5xyz = np.array( [[src[0] - 0.5, src[0] + 0.5, src[0] + 0.5, src[0] - 0.5], [src[1] - 0.5, src[1] - 0.5, src[1] + 0.5, src[1] + 0.5], [src[2], src[2], src[2], src[2]]]).T scomp = fields.get_source_field(grid, src5xyz, freq, strength=strength) assert_allclose(sman.field, scomp.field) # Normalized sman = fields.Field(grid, frequency=freq) for srcl in src4xxyyzz: sman.field += fields.get_source_field(grid, srcl, freq, strength=1 / 3).field scomp = fields.get_source_field(grid, src5xyz, freq, strength=1 / 3) assert_allclose(sman.field, scomp.field)
def test_copy_dict(self, tmpdir): ee = fields.Field(self.grid, self.field) # Test copy e2 = ee.copy() assert ee == e2 assert_allclose(ee.fx, e2.fx) assert_allclose(ee.fy, e2.fy) assert_allclose(ee.fz, e2.fz) assert not np.may_share_memory(ee.field, e2.field) edict = ee.to_dict() del edict['grid'] with pytest.raises(KeyError, match="'grid'"): fields.Field.from_dict(edict)
def test_get_magnetic_field(): # Check it does still the same (pure regression). dat = REGRES['reg_2'] model = dat['model'] efield = dat['result'] hfield = dat['hresult'] hout = fields.get_magnetic_field(model, efield) assert_allclose(hfield.field, hout.field) # Add some mu_r - Just 1, to trigger, and compare. dat = REGRES['res'] efield = dat['Fresult'] model1 = emg3d.Model(**dat['input_model']) model2 = emg3d.Model(**dat['input_model'], mu_r=1.) hout1 = fields.get_magnetic_field(model1, efield) hout2 = fields.get_magnetic_field(model2, efield) assert_allclose(hout1.field, hout2.field) # Test division by mu_r. model3 = emg3d.Model(**dat['input_model'], mu_r=2.) hout3 = fields.get_magnetic_field(model3, efield) assert_allclose(hout1.field, hout3.field * 2) # Comparison to alternative. # Using very unrealistic value, unrealistic stretching, to test. grid = emg3d.TensorMesh(h=[[1, 100, 25, 33], [1, 1, 33.3, 0.3, 1, 1], [2, 4, 8, 16]], origin=(88, 20, 9)) model = emg3d.Model(grid, mu_r=np.arange(1, grid.n_cells + 1) / 10) new = 10**np.arange(grid.n_edges) - grid.n_edges / 2 efield = fields.Field(grid, data=new, frequency=np.pi) hfield_nb = fields.get_magnetic_field(model, efield) hfield_np = alternatives.alt_get_magnetic_field(model, efield) assert_allclose(hfield_nb.fx, hfield_np.fx) assert_allclose(hfield_nb.fy, hfield_np.fy) assert_allclose(hfield_nb.fz, hfield_np.fz) # Test using discretize if discretize: h = np.ones(4) grid = emg3d.TensorMesh([h * 200, h * 300, h * 400], (0, 0, 0)) model = emg3d.Model(grid, property_x=3.24) sfield = fields.get_source_field(grid, (350, 550, 750, 30, 30), frequency=10) efield = emg3d.solve(model, sfield, plain=True, verb=0) mfield = fields.get_magnetic_field(model, efield).field dfield = grid.edge_curl * efield.field / sfield.smu0 assert_allclose(mfield, dfield)
def test_magnetic(self): ee = fields.Field(self.grid, electric=False) assert not ee.electric assert ee.fx.shape == self.grid.shape_faces_x assert ee.fy.shape == self.grid.shape_faces_y assert ee.fz.shape == self.grid.shape_faces_z assert ee.smu0 is None assert ee.sval is None assert ee.frequency is None # Check representation of Field. assert f"Field: magnetic; {ee.grid.shape_cells[0]} x" in ee.__repr__() # Try setting values ee.field = np.arange(self.grid.n_faces) ee.fx = np.ones(self.grid.shape_faces_x) ee.fy = np.ones(self.grid.shape_faces_y) ee.fz = np.ones(self.grid.shape_faces_z)
def test_dtype(self): with pytest.raises(ValueError, match="must be f>0"): _ = fields.Field(self.grid, frequency=0.0) with pytest.warns(np.ComplexWarning, match="Casting complex values"): lp = fields.Field(self.grid, self.field, frequency=-1) assert lp.field.dtype == np.float64 ignore = fields.Field(self.grid, frequency=-1, dtype=np.int64) assert ignore.field.dtype == np.float64 ignore = fields.Field(self.grid, self.field, dtype=np.int64) assert ignore.field.dtype == np.complex128 respected = fields.Field(self.grid, dtype=np.int64) assert respected.field.dtype == np.int64 default = fields.Field(self.grid) assert default.field.dtype == np.complex128