def test_localadas(path): adas_class = "adf11" adas_file = Path("scd89") / "scd12_h.dat" with TemporaryDirectory() as path: reader = ADASReader(path, MagicMock()) filepath = Path(path) / adas_class / adas_file filepath.parent.mkdir(parents=True) filepath.touch() filestream = reader._get_file(adas_class, adas_file) assert filestream.name == str(filepath)
def fractional_abundance_setup( element: str, t: np.ndarray, input_Te: DataArray, input_Ne: DataArray, ) -> DataArray: """Calculate and output Fractional abundance at t=infinity for calculating the mean charge in test_impurity_concentration() Parameters ---------- element String of the symbol of the element per ADAS notation e.g be for Beryllium t Time np.ndarray (used for expanding the dimensions of the output of the function to ensure that time is a dimension of the output.) input_Te xarray.DataArray of electron temperature. Dimensions (rho, t) input_Ne xarray.DataArray of electron density. Dimensions (rho, t) Returns ------- F_z_tinf Fractional abundance of the ionisation stages of the element at t=infinity. xarray.DataArray with dimensions (ion_charges, rho, t) """ ADAS_file = ADASReader() SCD = ADAS_file.get_adf11("scd", element, "89") ACD = ADAS_file.get_adf11("acd", element, "89") example_frac_abundance = FractionalAbundance( SCD, ACD, ) example_frac_abundance.interpolate_rates(Ne=input_Ne, Te=input_Te) example_frac_abundance.calc_ionisation_balance_matrix(Ne=input_Ne) F_z_tinf = example_frac_abundance.calc_F_z_tinf() # ignore with mypy since this is testing and inputs are known F_z_tinf = F_z_tinf.expand_dims({"t": t.size}, axis=-1) # type: ignore return F_z_tinf
def test_read_invalid_adf11(): reader = ADASReader("", MagicMock()) quantity = "scd" # The following check is not strictly necessary but is included in case # ADF11_GENERAL_DATATYPES is changed in the future to exclude "scd" for some reason. try: _ = ADF11_GENERAL_DATATYPES[quantity] except Exception as e: raise e with assert_raises(AssertionError): invalid_file_name = Path("tests/unit/readers/invalid_adf11_file.dat") with open(invalid_file_name, "r") as invalid_file: reader._get_file = MagicMock(return_value=invalid_file) _ = reader.get_adf11(quantity, "he", "89")
class Testadf15_transport(Testadf15): """Provides unit tests for the ADF15 reader""" reader = ADASReader() element = "ar" charge = "16" file_type = "llu" year = "transport"
class Testadf15_ar10(Testadf15): """Provides unit tests for the ADF15 reader""" reader = ADASReader() element = "ar" charge = "10" file_type = "ic" year = "40"
class Testadf15_o5(Testadf15): """Provides unit tests for the ADF15 reader""" reader = ADASReader() element = "o" charge = "5" file_type = "pjr" year = "96"
class Testadf15_b2(Testadf15): """Provides unit tests for the ADF15 reader""" reader = ADASReader() element = "b" charge = "2" file_type = "llu" year = "93"
class Testadf15_he0(Testadf15): """Provides unit tests for the ADF15 reader""" reader = ADASReader() element = "he" charge = "0" file_type = "pju" year = "93"
def power_loss_setup( element: str, t: np.ndarray, input_Te: DataArray, input_Ne: DataArray, ) -> DataArray: """Calculate and output PowerLoss for calculating the extrapolated impurity density. Parameters ---------- element String of the symbol of the element per ADAS notation e.g be for Beryllium t Time np.ndarray (used for expanding the dimensions of the output of the function to ensure that time is a dimension of the output.) input_Te xarray.DataArray of electron temperature. Dimensions (rho, t) input_Ne xarray.DataArray of electron density. Dimensions (rho, t) Returns ------- power_loss Power loss of the element at t=infinity. xarray.DataArray with dimensions (ion_charges, rho, t). """ ADAS_file = ADASReader() PLT = ADAS_file.get_adf11("plt", element, "89") PRB = ADAS_file.get_adf11("prc", element, "89") power_loss_obj = PowerLoss(PLT, PRB) frac_abund_inf = fractional_abundance_setup(element, t, input_Te.isel(t=0), input_Ne.isel(t=0)) power_loss = power_loss_obj(input_Ne.isel(t=0), input_Te.isel(t=0), frac_abund_inf) return power_loss
def test_cache_openadas(): # TODO: Compile different options so can test with larger range of inputs adas_class = "adf11" adas_file = Path("scd89") / "scd12_h.dat" with cachedir() as cache, patch( "indica.readers.adas.urlretrieve") as urlretrieve: reader = ADASReader("", MagicMock()) cachepath = Path().home() / cache cache_file = cachepath / "adas" / "adf11" / adas_file urlretrieve.side_effect = lambda x, y: y.touch() filestream = reader._get_file(adas_class, adas_file) assert filestream.name == str(cache_file) urlretrieve.assert_called_once_with( f"https://open.adas.ac.uk/download/{adas_class}/{adas_file}", cache_file, ) urlretrieve.reset_mock() filestream2 = reader._get_file(adas_class, adas_file) assert filestream2.name == str(cache_file) urlretrieve.assert_not_called()
def test_reader_provenance(path, creation_time): with patch("datetime.datetime", MagicMock()): datetime.datetime.now.return_value = creation_time reader = ADASReader(path, MagicMock()) path = Path(path) openadas = path == Path("") if openadas: path = Path.home() / adas.CACHE_DIR / "adas" prov_id = hash_vals(path=path) reader.session.prov.agent.assert_called_once_with(prov_id) assert reader.agent is reader.session.prov.agent.return_value reader.session.prov.entity.assert_called_once_with( prov_id, {"path": str(path.resolve())}) assert reader.entity is reader.session.prov.entity.return_value reader.session.prov.delegation.assert_called_once_with( reader.session.agent, reader.agent) reader.session.prov.generation.assert_called_once_with( reader.entity, reader.session.session, time=creation_time) reader.session.prov.attribution.assert_called_once_with( reader.entity, reader.session.agent)
def test_context_manager(path): with ADASReader(path, MagicMock()) as reader: print(reader.requires_authentication)
def adas_readers(draw): return ADASReader(draw(paths), MagicMock())
class Testadf15: """Provides unit tests for the ADF15 reader""" reader = ADASReader() element = "ne" charge = "9" file_type = "pju" year = "96" def test_read(self): """Checks reading does not give exceptions""" try: _ = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) except Exception as e: raise e def test_false_input(self): """Checks reading with wrong inputs raises errors""" element = self.element + ";" try: _ = self.reader.get_adf15( element, self.charge, self.file_type, year=self.year ) except Exception as e: assert e def test_index(self): """Checks index is float and values withing specified bounds""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert all([type(index) is np.float64 for index in data.index.values]) assert all(data.index >= 0) assert all(data.index <= np.max(data.shape)) def test_electron_temperature(self): """Checks electron temperature is float and values withing specified bounds""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert all( [ type(electron_temperature) is np.float64 for electron_temperature in data.electron_temperature.values ] ) assert all(data.electron_temperature >= 0) assert all(data.electron_temperature < 1.0e6) def test_electron_density(self): """Checks electron density is float and values withing specified bounds""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert all( [ type(electron_density) is np.float64 for electron_density in data.electron_density.values ] ) assert all(data.electron_density >= 0) assert all(data.electron_density < 1.0e30) def test_wavelength(self): """Checks wavelengths is a float and values withing specified bounds""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert all( [type(wavelength) is np.float64 for wavelength in data.wavelength.values] ) assert all(data.wavelength >= 0) assert all(data.wavelength <= 1.0e6) def test_transition(self): """Checks transition is a string and of lenght > 0""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert all( [type(transition) is np.str_ for transition in data.transition.values] ) assert all([len(transition) > 0 for transition in data.transition.values]) def test_transition_type(self): """Checks transition type is a string and of lenght > 0""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert all( [type(transition_type) is np.str_ for transition_type in data.type.values] ) assert all([len(transition_type) > 0 for transition_type in data.type.values]) def test_datatype(self): """Checks datatype is string and as expected""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert type(data.attrs["datatype"]) is tuple assert len(data.attrs["datatype"]) == 2 assert all([type(datatype) is str for datatype in data.attrs["datatype"]]) assert data.attrs["datatype"] == ( f"photon_emissivity_coefficient_{self.file_type}", self.element, ) def test_provenance(self): """Checks datatype is string and as expected""" data = self.reader.get_adf15( self.element, self.charge, self.file_type, year=self.year ) assert type(data.attrs["provenance"]) is ProvEntity