Exemple #1
0
    def testBackengine(self):
        """ Check that the backengine can be executed and output is generated. """

        # Setup parameters.
        xrts_parameters = self.xrts_parameters

        # Construct an instance.
        xrts_calculator = PlasmaXRTSCalculator(parameters=xrts_parameters,
                                               input_path=self.input_path,
                                               output_path='xrts_out')

        # Call the backengine.
        xrts_calculator.backengine()

        # Check for output.
        self.assertTrue(os.path.isdir(xrts_calculator.parameters._tmp_dir))
        self.assertTrue(
            'xrts_out.txt' in os.listdir(xrts_calculator.parameters._tmp_dir))
        self.assertTrue(
            'xrts.log' in os.listdir(xrts_calculator.parameters._tmp_dir))

        # Check log content.
        self.assertIn("SUMMARY TABLE",
                      xrts_calculator._PlasmaXRTSCalculator__run_log)
        # Check data shape.
        self.assertEqual(xrts_calculator._PlasmaXRTSCalculator__run_data.shape,
                         (201, 4))
Exemple #2
0
    def testReadH5(self):
        """ Test the readH5 function to read input from the photon propagator. """
        # Construct parameters.
        xrts_parameters = self.xrts_parameters

        xrts_calculator = PlasmaXRTSCalculator(
            parameters=xrts_parameters,
            input_path=TestUtilities.generateTestFilePath(
                'prop_out_0000001.h5'),
            output_path='xrts_out.h5')

        xrts_calculator._readH5()

        self.assertIn('source_spectrum', xrts_calculator._input_data.keys())

        e = xrts_calculator._input_data['source_spectrum'][:, 0]
        s = xrts_calculator._input_data['source_spectrum'][:, 1]
Exemple #3
0
    def testPhotonEnergyConsistency(self):
        """ Test that an exception is thrown if the given photon energy is not equal to the source photon energy. """
        # Construct parameters.
        xrts_parameters = self.xrts_parameters

        xrts_calculator = PlasmaXRTSCalculator(
            parameters=xrts_parameters,
            input_path=TestUtilities.generateTestFilePath(
                'prop_out_0000001.h5'),
            output_path='xrts_out.h5')
        # Should print a warning.
        xrts_calculator.parameters.photon_energy = 5.0e3
Exemple #4
0
    def notestBackengineWithWPGOut(self):
        """ Test that extracting the spectrum from a wpg output works. """
        """ notested because requires file not in TestFiles. """
        # Construct parameters.
        xrts_parameters = self.xrts_parameters

        xrts_calculator = PlasmaXRTSCalculator(parameters=xrts_parameters,
                                               input_path='prop_out.h5',
                                               output_path='xrts_out.h5')

        # Read in the data.
        xrts_calculator._readH5()

        # Specify that we want to use the measured source spectrum.
        xrts_parameters.source_spectrum = 'prop'
        xrts_parameters.energy_range = {
            'min': -300.0,
            'max': 300.0,
            'step': 3.,
        }

        # Run the backengine.
        xrts_calculator.backengine()

        self.assertTrue(
            'source_spectrum.txt' in os.listdir(xrts_parameters._tmp_dir))
Exemple #5
0
    def testConstruction(self):
        """ Testing the default construction of the class. """

        # Attempt to construct an instance of the class.
        xrts_calculator = PlasmaXRTSCalculator(parameters=self.xrts_parameters,
                                               input_path=self.input_path,
                                               output_path='xrts_out')

        # Check instance and inheritance.
        self.assertIsInstance(xrts_calculator, PlasmaXRTSCalculator)
        self.assertIsInstance(xrts_calculator, AbstractPhotonDiffractor)
        self.assertIsInstance(xrts_calculator, AbstractBaseCalculator)

        # Check attributes are initialized.
        self.assertEqual(xrts_calculator._input_data, {})
Exemple #6
0
    def testPhotonEnergyConsistency(self):
        """ Test that an exception is thrown if the given photon energy is not equal to the source photon energy. """
        # Construct parameters.
        xrts_parameters = self.xrts_parameters

        xrts_calculator = PlasmaXRTSCalculator(
            parameters=xrts_parameters,
            input_path=TestUtilities.generateTestFilePath(
                'prop_out_0000001.h5'),
            output_path='xrts_out.h5')
        # This should cause the exception since source photon energy is 4.96 keV.
        xrts_calculator.parameters.photon_energy = 5.0e3

        # Read in the data.
        self.assertRaises(RuntimeError, xrts_calculator._readH5)
Exemple #7
0
    def testConstructionParameters(self):
        """ Testing the input parameter checks pass for a sane parameter dict. """

        # Construct an instance.
        xrts_calculator = PlasmaXRTSCalculator(parameters=self.xrts_parameters,
                                               input_path=self.input_path,
                                               output_path='xrts_out')

        # Query the parameters.
        query = xrts_calculator.parameters

        # Check query is ok.
        self.assertIsInstance(query, PlasmaXRTSCalculatorParameters)

        # Check attributes are initialized.
        self.assertEqual(xrts_calculator._input_data, {})
Exemple #8
0
    def testSerializeSourceSpectrum(self):
        """ Test saving the source spectrum to file in the correct location. """
        # Construct parameters.
        xrts_parameters = self.xrts_parameters

        xrts_calculator = PlasmaXRTSCalculator(
            parameters=xrts_parameters,
            input_path=TestUtilities.generateTestFilePath(
                'prop_out_0000001.h5'),
            output_path='xrts_out.h5')

        xrts_calculator._readH5()

        xrts_parameters.source_spectrum = 'prop'

        xrts_parameters._serialize()

        xrts_calculator._serializeSourceSpectrum()

        self.assertTrue(
            'source_spectrum.txt' in os.listdir(xrts_parameters._tmp_dir))
Exemple #9
0
    def testBackengineWithGauss(self):
        """ Test saving the source spectrum to file in the correct location. """
        # Construct parameters.
        xrts_parameters = self.xrts_parameters

        xrts_calculator = PlasmaXRTSCalculator(
            parameters=xrts_parameters,
            input_path=TestUtilities.generateTestFilePath(
                'prop_out_0000001.h5'),
            output_path='xrts_out.h5')

        # Read in the data.
        xrts_calculator._readH5()

        # Specify that we want to use the measured source spectrum.
        xrts_parameters.source_spectrum_fwhm = 1.0
        xrts_parameters.energy_range = {
            'min': -200.0,
            'max': 200.0,
            'step': 2.,
        }

        # Run the backengine.
        xrts_calculator.backengine()
Exemple #10
0
    # Get number of zones.
    number_of_zones = pos.shape[0]

    # Get information from last zone.
    zone_index = number_of_zones - 1

    # Update plasma parameters.
    plasma_parameters.electron_temperature = temp[zone_index] * Kelvin_to_eV
    plasma_parameters.ion_temperature = temp[zone_index] * Kelvin_to_eV
    plasma_parameters.mass_density = rho[zone_index] * 1e-3

    # Set up the calculator.
    xrts_calculator = PlasmaXRTSCalculator(
        parameters=plasma_parameters,
        input_path=None,
        output_path=None,
    )

    # Run the calculation.
    print("Processing zone 0 of %d" % (number_of_zones))
    xrts_calculator.backengine()

    # Get the total spectrum.
    total_spectrum = xrts_calculator.data[:, 3] * abs(pos[zone_index - 1])

    # Loop over all but last zones.
    for zone in range(number_of_zones - 1):

        print("Processing zone %d of %d" % (zone, number_of_zones))
Exemple #11
0
    def testSaveH5(self):
        """ Test hdf5 output generation. """
        # Make sure we clean up after ourselves.
        outfile = 'xrts_out.h5'
        self.__files_to_remove.append(outfile)

        # Setup parameters.
        xrts_parameters = self.xrts_parameters

        # Construct an instance.
        xrts_calculator = PlasmaXRTSCalculator(parameters=xrts_parameters,
                                               input_path=self.input_path,
                                               output_path=outfile)

        # Call the backengine.
        xrts_calculator.backengine()

        # Save to h5
        xrts_calculator.saveH5()

        # Check output was written.
        self.assertTrue(os.path.isfile(xrts_calculator.output_path))

        # Open the file for reading.
        h5 = h5py.File(xrts_calculator.output_path, 'r')

        # Check that the all data to be provided is present.
        for key in xrts_calculator.providedData():
            group = key.split('/')[1]
            self.assertTrue(group in list(h5.keys()))

        # Check dynamic datasets shapes and units.
        self.assertEqual(
            numpy.array(h5['data/dynamic/']['energy_shifts']).shape, (201, ))
        self.assertEqual(
            str(h5['data/dynamic/']['energy_shifts'].attrs['unit']), 'eV')

        self.assertEqual(
            numpy.array(h5['data/dynamic/']['Skw_free']).shape, (201, ))
        self.assertEqual(str(h5['data/dynamic/']['Skw_free'].attrs['unit']),
                         'eV**-1')

        self.assertEqual(
            numpy.array(h5['data/dynamic/']['Skw_bound']).shape, (201, ))
        self.assertEqual(str(h5['data/dynamic/']['Skw_bound'].attrs['unit']),
                         'eV**-1')

        self.assertEqual(
            numpy.array(h5['data/dynamic/']['Skw_total']).shape, (201, ))
        self.assertEqual(str(h5['data/dynamic/']['Skw_total'].attrs['unit']),
                         'eV**-1')

        # Check static data.
        self.assertAlmostEqual(h5['data/static']['k'].value, 3.555e10, 3)
        self.assertAlmostEqual(h5['data/static']['fk'].value, 1.532, 3)
        self.assertAlmostEqual(h5['data/static']['qk'].value, 0.4427, 4)
        self.assertAlmostEqual(h5['data/static']['Sk_ion'].value, 1.048, 3)
        self.assertAlmostEqual(h5['data/static']['Sk_free'].value, 0.8075, 4)
        self.assertAlmostEqual(h5['data/static']['Sk_core'].value, 0.05999, 4)
        self.assertAlmostEqual(h5['data/static']['Wk'].value, 4.084, 2)
        self.assertAlmostEqual(h5['data/static']['Sk_total'].value, 6.002, 3)
        self.assertAlmostEqual(h5['data/static']['ipl'].value, 38.353, 3)
        # IPL has a unit.
        self.assertEqual(h5['data/static']['ipl'].attrs['unit'], 'eV')
        self.assertAlmostEqual(h5['data/static']['lfc'].value, 0.000, 3)
        self.assertAlmostEqual(h5['data/static']['debye_waller'].value, 1.000,
                               3)
    vel = meshes["vel"] ### Use for applying Doppler shift (?)

    # Get number of zones.
    number_of_zones = pos.shape[0]

    # Get information from last zone.
    zone_index = number_of_zones - 1

    # Update plasma parameters.
    plasma_parameters.electron_temperature = temp[zone_index] * Kelvin_to_eV
    plasma_parameters.ion_temperature = temp[zone_index] * Kelvin_to_eV
    plasma_parameters.mass_density = rho[zone_index] * 1e-3

    # Set up the calculator.
    xrts_calculator = PlasmaXRTSCalculator(parameters=plasma_parameters,
        input_path=None,
        output_path=None,
        )

    # Run the calculation.
    print "Processing zone 0 of %d" % (number_of_zones)
    xrts_calculator.backengine()

    # Get the total spectrum.
    total_spectrum = xrts_calculator.data[:,3] * abs(pos[zone_index - 1])

    # Loop over all but last zones.
    for zone in range(number_of_zones - 1):

        print "Processing zone %d of %d" % (zone, number_of_zones)

        # Decrement zone index.