From ca27ba6f3c10e89d0a9bdf30f7c8685907397745 Mon Sep 17 00:00:00 2001 From: Benjamin Morgan Date: Sun, 4 Jul 2021 18:14:26 +0000 Subject: [PATCH] Tidying set_up_calculation code. --- pyscses/set_up_calculation.py | 45 ++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/pyscses/set_up_calculation.py b/pyscses/set_up_calculation.py index 274537e..6836e1b 100644 --- a/pyscses/set_up_calculation.py +++ b/pyscses/set_up_calculation.py @@ -6,10 +6,14 @@ from bisect import bisect_left, bisect_right from sklearn.cluster import AgglomerativeClustering # type: ignore -def site_from_input_file( site, defect_species, site_charge, core, temperature ): +def site_from_input_file(site, + defect_species, + site_charge, + core, + temperature): """ Takes the data from the input file and converts it into a site. - The input data file is a .txt file where each line in the file corresponds to a site. The values in each line are formatted and separated into the corresponding properties before creating a Site object for each site. + The input data file is a .txt file where each line in the file corresponds to a site. The values in each line are formatted and separated into the corresponding properties before creating a Site object for each site. Args: site (str): A line in the input file. @@ -40,16 +44,18 @@ def site_from_input_file( site, defect_species, site_charge, core, temperature ) #defect_energies = [ 0.0 for e in site[4::2] ] return Site( label, x, [ defect_species[l] for l in defect_labels ], defect_energies, valence=valence ) -def format_line( line, site_charge, offset = 0.0 ): +def format_line(line, + site_charge, + offset = 0.0): """ - Each line in the input file is formatted into separate site properties. + Each line in the input file is formatted into separate site properties. Args: line (str): A line in the input file. site_charge (bool): The site charge refers to the contribution to the overall charge of a site given by the original, non-defective species present at that site. True if the site charge contribution is to be included in the calculation, False if it is not to be included. - Returns: - list: line from the input file, split into individual values. + Returns: + list: line from the input file, split into individual values. """ # x coordinate @@ -69,16 +75,20 @@ def format_line( line, site_charge, offset = 0.0 ): # line[i] = 0.0 return line -def load_site_data( filename, x_min, x_max, site_charge, offset = 0.0 ): +def load_site_data(filename, + x_min, + x_max, + site_charge, + offset = 0.0): """ Reads in the input data and formats the input data if the x coordinate values are within the calculation region. Args: filename(string): Filename for importing the input data. x_min(float): Minimum x coordinate value defining the calculation region. - x_max(float): Maximum x coordinate value defining the calculation region. + x_max(float): Maximum x coordinate value defining the calculation region. site_charge (bool): True if site charges are to be included in the calculation, false if they are not to be included. - + Return: list: formatted data for calculation. @@ -93,15 +103,15 @@ def load_site_data( filename, x_min, x_max, site_charge, offset = 0.0 ): return input_data -def cluster_similar_sites( input_data ): +def cluster_similar_sites(input_data): """Clusters data points the input data to be projected onto a site. Clustering criterion is set as 0.01 nm. - + Args: input_data (str): The input file. - + Returns: str: The input file, formatted with x coordinate values with correct clustering coordinates. - + """ coordinates = np.array([[ion[2]] for ion in input_data]) # format data into correct format # Use sklearn's clustering functionality to assign site clusters @@ -121,14 +131,17 @@ def cluster_similar_sites( input_data ): sites_format += cluster return sites_format -def calculate_grid_offsets( filename, x_min, x_max, system ): +def calculate_grid_offsets(filename, + x_min, + x_max, + system): """Reads in the input data calculates the distance to the next site outside of the defined calculation region. Allows calculation of the delta_x and volume values for the endmost grid points. Args: filename(string): Filename for importing the input data. x_min(float): Minimum x coordinate value defining the calculation region. - x_max(float): Maximum x coordinate value defining the calculation region. - system(str): 'single' for single grain boundary systems, 'double' for systems where the input data has been mirrored to give two grain boundaries. + x_max(float): Maximum x coordinate value defining the calculation region. + system(str): 'single' for single grain boundary systems, 'double' for systems where the input data has been mirrored to give two grain boundaries. Returns: list, list: distance between the midpoint of the endmost sites and the midpoint of the next site outside of the calculation region for the first and last sites respectively. Distance between the endmost sites and the next site outside of the calculation region for the first and last sites respectively.