def test_vector_modulo_cell(self):
     for test_tag, test_dict in self.vector_modulo_tests.items():
         test_file = self.test_input_template % test_tag
         test_path = os.path.join(self.test_input_folder, test_file)
         extb = ExcitonTB(test_path)
         for vector_str, vector_dict in test_dict.items():
             test_vector = extb.get_vector_modulo_cell(
                 vector=vector_dict['vector'], macro_cell=False)
             val_vector = vector_dict['modulo_vector']
             for test_elem, val_elem in zip(test_vector, val_vector):
                 self.assertAlmostEqual(test_elem, val_elem, self.dp)
    def test_energy_cutoff_band_indices(self):
        for tag, example in self.eigenvalue_cutoff_tests.items():

            for idx, cutoff in enumerate(example['energy_cutoff']):
                cb_max, vb_min = ExcitonTB.get_band_extrema(
                    eigenvalues=example['eigenvalues'],
                    energy_cutoff=cutoff,
                    edge_index=example['edge_index'])
                self.assertEqual(cb_max, example['cb_max'][idx])
                self.assertEqual(vb_min, example['vb_min'][idx])
 def test_fourier_matrix_creation(self):
     for test_tag, test_dict in self.vector_modulo_tests.items():
         test_file = self.test_input_template % test_tag
         test_path = os.path.join(self.test_input_folder, test_file)
         extb = ExcitonTB(test_path)
         exp_size = (extb.n_atoms, extb.n_atoms)
         for kpt in extb.k_grid:
             for idx, pos in enumerate(extb.motif_vectors):
                 fourier_matrix = extb.create_fourier_matrix(pos=pos,
                                                             pos_idx=idx,
                                                             kdiff=kpt)
                 fourier_matrix_c = extb.create_fourier_matrix(pos=pos,
                                                               pos_idx=idx,
                                                               kdiff=-kpt)
                 self.assertTupleEqual(fourier_matrix.shape, exp_size)
                 self.assertTupleEqual(fourier_matrix_c.shape, exp_size)
                 self.assertListAlmostEqual(
                     list(fourier_matrix_c.ravel()),
                     list(np.conj(fourier_matrix.ravel())),
                     places=self.dp)
    def test_matrix_dim_and_block_starts(self):
        for test_tag, test_dict in self.matrix_dim_block_starts_tests.items():
            test_file = self.test_input_template % test_tag
            test_path = os.path.join(self.test_input_folder, test_file)
            extb = ExcitonTB(test_path)
            for cutoff_str in self.energy_cutoffs:
                matrix_elem_name = self.element_name_template_2.format(
                    test_tag, cutoff_str)
                extb.create_matrix_element_hdf5(matrix_elem_name,
                                                float(cutoff_str))
                val_dict = test_dict[cutoff_str]

                full_path = os.path.join(self.element_dir, matrix_elem_name)
                with File(full_path, 'r') as f:
                    test_data = extb.get_matrix_dim_and_block_starts(f)
                os.remove(full_path)

                test_mat_size = test_data[0][0]
                val_mat_size = val_dict['matrix_dim']
                self.assertEqual(test_mat_size, val_mat_size)

                test_block_starts = test_data[1][0]
                val_block_starts = val_dict['block_start']
                self.assertListEqual(test_block_starts, val_block_starts)
def main():
    parser = get_argparser()
    args = parser.parse_args()

    if args.output_name is None and (not args.terminal_output):
        raise Exception("Need to either specify output file (-o <output name>)\
                         or use terminal output (-t)!")

    freq_range = (args.frequency_min, args.frequency_max, args.frequency_num)

    exc_tb = ExcitonTB(args.input_hdf5)
    exc_tb.create_matrix_element_hdf5(storage_name=args.matrix_element,
                                      energy_cutoff=args.energy_cutoff)
    output = exc_tb.get_bse_eigensystem_direct(solve=True)
    n_spins = exc_tb.n_spins
    cutoff_bands_info = exc_tb.get_cutoff_bands_info(args.energy_cutoff)
    exc_tb.terminate_storage_usage()

    split = bool(exc_tb.n_spins == 2)
    if args.exciton_dos:
        output = extract_exciton_dos(excitons=output,
                                     frequencies=freq_range,
                                     broadening_function=args.broadening,
                                     sigma=args.broadening_width,
                                     spin_split=split)
    if args.terminal_output:
        write_obj = sys.stdout
        if args.exciton_dos:
            write_exciton_dos(output, freq_range, write_obj=write_obj)
        else:
            write_excitons(output,
                           write_obj=write_obj,
                           n_spins=n_spins,
                           cutoff_info=cutoff_bands_info)
    else:
        with open(args.output_name, 'w') as write_obj:
            if args.exciton_dos:
                write_exciton_dos(output, freq_range, write_obj=write_obj)
            else:
                write_excitons(output,
                               write_obj=write_obj,
                               n_spins=n_spins,
                               cutoff_info=cutoff_bands_info)
    def test_exciton_tb_init(self):

        for test_tag, test_dict in self.init_properties.items():
            test_file = self.test_input_template % test_tag
            test_path = os.path.join(self.test_input_folder, test_file)

            extb = ExcitonTB(test_path)

            # Integer/bool equality assertions
            self.assertEqual(test_dict['crystal']['n_atoms'], extb.n_atoms)
            self.assertEqual(test_dict['crystal']['n_orbs'], extb.n_orbs)
            self.assertEqual(test_dict['eigensystem']['n_k'], extb.n_k)
            self.assertEqual(test_dict['eigensystem']['n_con'], extb.n_con)
            self.assertEqual(test_dict['eigensystem']['n_val'], extb.n_val)
            self.assertEqual(test_dict['eigensystem']['n_spins'], extb.n_spins)
            self.assertEqual(test_dict['eigensystem']['convention'],
                             extb.convention)
            self.assertEqual(test_dict['eigensystem']['is_complex'],
                             extb.is_complex)

            # Float equality assertions
            self.assertAlmostEqual(test_dict['crystal']['alat'],
                                   extb.alat,
                                   places=self.dp)
            # List properties to check equality with.
            self.assertListAlmostEqual(test_dict['crystal']['motif'].ravel(),
                                       extb.motif_vectors.ravel(),
                                       places=self.dp)
            self.assertListAlmostEqual(
                test_dict['eigensystem']['k_grid'].ravel(),
                extb.k_grid.ravel(),
                places=self.dp)
            self.assertListAlmostEqual(test_dict['crystal']['avecs_a1'],
                                       extb.a1,
                                       places=self.dp)
            self.assertListAlmostEqual(test_dict['crystal']['avecs_a2'],
                                       extb.a2,
                                       places=self.dp)
def main():
    parser = get_argparser()
    args = parser.parse_args()

    if args.output_name is None and (not args.terminal_output):
        raise Exception("Need to either specify output file (-o <output name>)\
                         or use terminal output (-t)!")

    freq_range = (args.frequency_min, args.frequency_max, args.frequency_num)

    exc_tb = ExcitonTB(args.input_hdf5)

    if args.use_rpa:
        cond_tb = ConductivityTB(exciton_obj=exc_tb)
        frequencies, conductivity = cond_tb.non_interacting_conductivity(
            sigma=args.broadening_width,
            freq_range=freq_range,
            imag_dielectric=args.dielectric,
            broadening=args.broadening)
    else:
        exc_tb.create_matrix_element_hdf5(args.matrix_element,
                                          energy_cutoff=args.energy_cutoff)
        cond_tb = ConductivityTB(exciton_obj=exc_tb)
        frequencies, conductivity = cond_tb.interacting_conductivity(
            sigma=args.broadening_width,
            freq_range=freq_range,
            imag_dielectric=args.dielectric,
            broadening=args.broadening)
    exc_tb.terminate_storage_usage()

    if args.terminal_output:
        write_obj = sys.stdout
        write_conductivity(frequencies=frequencies,
                           conductivity=conductivity,
                           write_obj=write_obj,
                           is_interacting=bool(not args.use_rpa))
    else:
        with open(args.output_name, 'w') as write_obj:
            write_conductivity(frequencies=frequencies,
                               conductivity=conductivity,
                               write_obj=write_obj,
                               is_interacting=bool(not args.use_rpa))
    def test_creation_matrix_element_storage(self):
        for test_tag, test_dict in self.matrix_element_properties.items():
            el_shape_dict = test_dict[self.element_key]
            test_file = self.test_input_template % test_tag
            test_path = os.path.join(self.test_input_folder, test_file)
            extb = ExcitonTB(test_path)
            for cutoff_str in self.energy_cutoffs:
                matrix_elem_name = self.element_name_template_1.format(
                    test_tag, cutoff_str)
                extb.create_matrix_element_hdf5(matrix_elem_name,
                                                float(cutoff_str))
                full_path = os.path.join(self.element_dir, matrix_elem_name)
                with File(full_path, 'r') as f:
                    for k_str in self.two_point_k_strings[test_tag]:
                        el_shape = np.array(f['s0'][k_str]['mat_elems']).shape
                        k_str_in_val = bool(k_str in el_shape_dict[cutoff_str])
                        has_non_zero_shape = bool(np.product(el_shape) > 0)
                        self.assertEqual(k_str_in_val, has_non_zero_shape)

                        if has_non_zero_shape:
                            val_shape = el_shape_dict[cutoff_str][k_str]
                            self.assertTupleEqual(el_shape, val_shape)
                os.remove(full_path)
            extb.terminate_storage_usage()