def get_ratios(self, choice=0): """ Saves the dependence of transition energies ratio on parameter x to file. """ peak_data = 'energies' if choice == 0 else 'intensities' levels_number = 7 parameters = { 'w': self.llw_parameters['w'], } ratio_file_name = self.get_file_name(data_name=f'ratios_{peak_data}', parameters=parameters) peak_file_name = self.get_file_name(data_name=peak_data, parameters=parameters) PathProcessor(ratio_file_name).remove_if_exists() with OpenedFile(ratio_file_name, mode='a') as ratio_file: with OpenedFile(peak_file_name) as peak_file: for line in peak_file: line = line.rstrip('\n') peak_row = [float(energy) for energy in line.split('\t')] for _ in range(len(peak_row), levels_number): peak_row.append(0) ratios = [peak_row[0]] for low in range(1, levels_number): for high in range(low + 1, levels_number): if peak_row[low] == 0: ratios.append(0) else: ratios.append(peak_row[high] / peak_row[low]) write_row(ratio_file, ratios)
def save_spectra_with_one_temperature( self, gamma: float, temperature: float, ): """ Saves inelastic neutron scattering spectra at specified temperature to file. """ energies = linspace(-5, 30, 10001) spectrum = self.get_spectrum( energies=energies, width_dict={'gamma': gamma}, temperature=temperature, ) file_name = self.get_file_name(data_name='spectra', parameters={ **self.llw_parameters, 'gamma': gamma, 'T': temperature, }) PathProcessor(file_name).remove_if_exists() with OpenedFile(file_name, mode='a') as file: for index, energy in enumerate(energies): write_row(file, (energy, spectrum[index]))
def save_spectra_with_many_temperatures( self, gamma: float, temperatures, ): """ Saves inelastic neutron scattering spectra at several specified temperatures to file. """ lines = {} parameters = { **self.llw_parameters, 'gamma': gamma, } data = { 'energies': [], } for t_number, temperature in enumerate(temperatures): data[temperature] = [] parameters['T'] = temperature file_name = self.get_file_name( data_name='spectra', parameters=parameters, ) with OpenedFile(file_name) as file: lines[temperature] = list(file) for index, line in enumerate(lines[temperature]): row = line.split('\t') if t_number == 0: data['energies'].append(float(row[0])) data[temperature].append(float(row[1])) del parameters['T'] file_name = self.get_file_name( data_name='spectra', parameters=parameters, ) PathProcessor(file_name).remove_if_exists() with OpenedFile(file_name, mode='a') as file: for index, _ in enumerate(data['energies']): write_row(file, row=[val[index] for key, val in data.items()]) return data
def save_to_file(self): """Saves parameters of the current object to file.""" saved_object = { 'crystal': self.material.crystal, 'rare_earth': self.material.rare_earth.name, 'parameters': self.parameters, 'magnet_field': self.magnet_field, } with OpenedFile(self.file_name, mode='w') as file: dump(saved_object, file, indent=4, sort_keys=True)
def save_intensities(self): """ Saves temperature dependence of magnetic susceptibilities to file. """ temperatures = linspace(0, 200, 1001) file_name = self.get_file_name( data_name='intensities_on_temperature', ) PathProcessor(file_name).remove_if_exists() with OpenedFile(file_name, mode='a') as file: for _, temperature in enumerate(temperatures): peaks = self.get_peaks(temperature=temperature) intensities = [peak[1] for peak in peaks if peak[0] >= 0] row = [temperature] + intensities write_row(file, row)
def save_peak_dat(self, number_of_intervals: int, choice=0): """ Saves the dependence of transition energies or intensities on parameter x to file. """ file_name = self.get_file_name( data_name='energies' if choice == 0 else 'intensities') PathProcessor(file_name).remove_if_exists() print(f'Saving of {"energies" if choice == 0 else "intensities"} ' f'datafiles will take some time...') with OpenedFile(file_name, mode='a') as file: for x_parameter in linspace(-1, 1, number_of_intervals + 1): self.llw_parameters['x'] = x_parameter row = (self.get_energies() if choice == 0 else self.get_intensities()) write_row(file, (x_parameter, *row))
def find_cross( self, experimental_value: float, experimental_energy: float, accuracy=0.005, ): """Returns points of cross experimental and calculated curves, recalculated with correct value of W.""" points = [] for w_parameter in ( abs(self.llw_parameters['w']), -abs(self.llw_parameters['w']), ): self.llw_parameters['w'] = w_parameter ratio_file_name = self.get_file_name( data_name='ratios_energies', parameters={'w': w_parameter}, ) with OpenedFile(ratio_file_name) as ratio_file: for line in ratio_file: line = line.rstrip('\n') numbers = [float(number) for number in line.split('\t')] if any( abs(experimental_value - value) < accuracy for value in numbers[1:]): points = self.check_ratios( numbers=numbers, points=points, experimental_value=experimental_value, accuracy=accuracy) for index, point in enumerate(points): self.llw_parameters['x'] = point.x self.llw_parameters['w'] = point.w level = int(point.ratio_name[-2]) - 1 old_energy = self.get_energies()[level] new_w = experimental_energy / old_energy new_w = -new_w if self.llw_parameters['w'] < 0 else new_w points[index] = CrossPoint( w=new_w, rare_earth=point.rare_earth, x=point.x, difference=point.difference, ratio_name=point.ratio_name, ) self.llw_parameters = {'w': points[index].w, 'x': points[index].x} return points
def save_susceptibility(self): """ Saves temperature dependence of magnetic susceptibilities to file. """ temperatures = linspace(0.1, 100.0, 300) common_file_name = self.get_file_name(data_name='susceptibilities', ) chi_curie, chi_van_vleck, chi = self.get_chi_dependence(temperatures) for axis in ('z', 'x', 'total'): file_name = common_file_name.replace('.dat', f'_chi_{axis}.dat') PathProcessor(file_name).remove_if_exists() with OpenedFile(file_name, mode='a') as file: row = ['T(Kelvin)'] if axis in ('z', 'x'): row += [ f'chi_curie_{axis}', f'chi_van_vleck_{axis}', f'chi_{axis}', ] else: row += [ 'chi_total', 'inverse_chi', ] write_row(file, row) for i, temperature in enumerate(temperatures): row = [temperature] if axis in ('z', 'x'): row += [ chi_curie[axis][i], chi_van_vleck[axis][i], chi[axis][i], ] else: row += [ chi['total'][i], chi['inverse'][i], ] write_row(file, row)
def load_data(self): """Loads CEF object from file""" with OpenedFile(self.file_name) as file: self.__dict__.update(load(file))