def convert (self): value = self.conv_temp(self.temperature, self.metric, self.target) u.print_output(self.temperature, self.metric, value, self.target) return value;
def simple_or_restart(): """ Select the type of run. If the all backup files are present the run will be restarted. Otherwise, the directory will be cleaned and a new run will be started.""" for_restart = [ "backup_population.dat", "backup_mol.dat", "backup_min_energy.dat", "backup_iteration.dat", "backup_blacklist.dat", ] opt = "restart" for filename in for_restart: if glob.glob(filename): pass else: opt = "simple" if opt == "simple": print_output("Cleaning up the directory") for d in glob.glob("initial_*"): remover_dir(d) for d in glob.glob("generation_*_child*"): remover_dir(d) remover_dir("blacklist") for f in ["mol.sdf", "control.in", "geometry.in", "output.txt", "result.out", "kill.dat"]: remover_file(f) for f in for_restart: remover_file(f) if opt == "restart": remover_file("kill.dat") return opt
def __repr__(self): """Create an unambiguous object representation. The resulting string is an one-liner with the newline parameter replacing the original '\n' sign in the sdf_string and initial_sdf_string attribute.""" repr_list = [] for att_name in self.__dict__.keys(): if att_name in ["sdf_string", "initial_sdf_string"]: repr_list.append("%s='%s'" % (att_name, getattr( self, att_name).replace("\n", Structure.newline))) else: if type(self.__dict__[att_name]) in [str]: repr_list.append('%s=%s' % (att_name, repr(getattr(self, att_name)))) elif type(self.__dict__[att_name]) in [int, float, bool]: repr_list.append('%s=%s' % (att_name, repr(getattr(self, att_name)))) elif att_name == 'dof': for dof in self.dof: repr_list.append( '%s_%s=%s' % (dof.type, "values", repr(dof.values))) try: repr_list.append('%s_%s=%s' % (dof.type, "initial_values", repr(dof.initial_values))) except: pass elif att_name == 'mol_info': pass else: print_output("Unknown type of attribute " + str(att_name)) return "%s(mol, %s)" % (self.__class__.__name__, ', '.join(repr_list))
def __repr__(self): """Create an unambiguous object representation. The resulting string is an one-liner with the newline parameter replacing the original '\n' sign in the template sdf_string attribute.""" repr_list = [] for att_name in self.__dict__.keys(): if type(self.__dict__[att_name]) in [str] and \ att_name != "template_sdf_string": repr_list.append('%s="%s"' % (att_name, getattr(self, att_name))) elif type(self.__dict__[att_name]) in [int, float, bool, list]: repr_list.append('%s=%s' % (att_name, repr(getattr(self, att_name)))) elif att_name in ["template_sdf_string"]: repr_list.append("%s='%s'" % (att_name, getattr(self, att_name).replace( "\n", MoleculeDescription.newline, ))) else: print_output("Unknown type of attribute " + str(att_name)) return "%s(%s)" % (self.__class__.__name__, ', '.join(repr_list))
def __repr__(self): """Create an unambiguous object representation. The resulting string is an one-liner with the newline parameter replacing the original '\n' sign in the template sdf_string attribute.""" repr_list = [] for att_name in self.__dict__.keys(): if type(self.__dict__[att_name]) in [str] and \ att_name != "template_sdf_string": repr_list.append('%s="%s"' % (att_name, getattr(self, att_name))) elif type(self.__dict__[att_name]) in [int, float, bool, list]: repr_list.append('%s=%s' % (att_name, repr(getattr(self, att_name)))) elif att_name in ["template_sdf_string"]: repr_list.append("%s='%s'" % ( att_name, getattr( self, att_name).replace("\n", MoleculeDescription.newline,))) else: print_output("Unknown type of attribute "+str(att_name)) return "%s(%s)" % (self.__class__.__name__, ', '.join(repr_list))
def simple_or_restart(): """ Select the type of run. If the all backup files are present the run will be restarted. Otherwise, the directory will be cleaned and a new run will be started.""" for_restart = [ "backup_population.dat", "backup_mol.dat", "backup_min_energy.dat", "backup_iteration.dat", "backup_blacklist.dat" ] opt = "restart" for filename in for_restart: if glob.glob(filename): pass else: opt = "simple" if opt == "simple": print_output("Cleaning up the directory") for d in glob.glob("initial_*"): remover_dir(d) for d in glob.glob("generation_*_child*"): remover_dir(d) remover_dir("blacklist") for f in [ "mol.sdf", "control.in", "geometry.in", "output.txt", "result.out", "kill.dat" ]: remover_file(f) for f in for_restart: remover_file(f) if opt == "restart": remover_file("kill.dat") return opt
def check_for_kill(): """ Check if the kill.dat file is present in the directory or in the subdirectories. If discoveed the run will be aborted.""" if len(glob.glob("*/kill.dat")) == 0 and len(glob.glob("kill.dat")) == 0: pass else: print_output("Kill.dat file discovered. The code terminates") sys.exit(0)
def relax_info(struct): """ Prints the information about the structure to the output file after the local optimization.""" print_output(struct) for dof in struct.dof: print_output("Values of " + str(dof.type) + " before: " + str(dof.initial_values) + " and after local opt: " + str(dof.values))
def relax_info(struct): """ Prints the information about the structure to the output file after the local optimization.""" print_output(struct) for dof in struct.dof: print_output("Values of "+str(dof.type)+" before: " + str(dof.initial_values)+" and after local opt: " + str(dof.values))
def check_for_kill(): """ Check if the kill.dat file is present in the directory or in the subdirectories. If discoveed the run will be aborted.""" if len(glob.glob("*/kill.dat")) == 0 and len(glob.glob("kill.dat")) == 0: pass else: print_output("Kill.dat file discovered. The code terminates") sys.exit(0)
def selection(pop_list, selection_type, energy_range, fitness_sum_limit): """Select two objects from a list. Args: pop_list (list): sorted list of population objects selection_type (string): method of selection energy_range (float): criterion for distinctive energy values fitness_sum_limit (float): criterion for distinctive fitness values Returns: selected objects and a list of all fitness values Raises: ValueError: if the input parameters are outside the range If selection_type is not properly defined, it will be set to random. """ if len(pop_list) < 2: raise ValueError("The list needs to have at least two members.") if selection_type not in ["roulette_wheel", "roulette_wheel_reverse", "random"]: print_output("Method not found. Selection method set to random") selection_type = "random" if energy_range < 0: raise ValueError("The energy range cannot be negative") if fitness_sum_limit < 1: raise ValueError("The fitness_sum_limit cannot be smaller than 1") best_e = float(pop_list[0]) worst_e = float(pop_list[-1]) fitness = np.zeros(len(pop_list)) for j in range(len(pop_list)): if worst_e-best_e > energy_range: # only when energy differs fitness[j] = (worst_e-float(pop_list[j]))/(worst_e-best_e) else: fitness[j] = 1.0 # equal fitness if diversity low fitness_sum = fitness.sum() if selection_type == "roulette_wheel": if fitness_sum > fitness_sum_limit: x, y = find_two_in_list(fitness_sum, fitness) parent1 = pop_list[x] parent2 = pop_list[y] else: # if the sum is below the limit, best and a random are selected parent1 = pop_list[0] parent2 = pop_list[int(random.choice(range(1, len(pop_list))))] if selection_type == "roulette_wheel_reverse": if fitness_sum > fitness_sum_limit: # in order to prevent num problems fitness_rev = np.zeros(len(fitness)) for i in range(len(fitness)): fitness_rev[-(i+1)] = fitness[i] # swapping of fitness values x, y = find_two_in_list(fitness_sum, fitness_rev) parent1 = pop_list[x] parent2 = pop_list[y] else: parent1 = pop_list[-1] parent2 = pop_list[int(random.choice(range(1, len(pop_list)))-1)] if selection_type == "random": parents_ind = random.sample(xrange(len(pop_list)), 2) parent1 = pop_list[parents_ind[0]] parent2 = pop_list[parents_ind[1]] return parent1, parent2, fitness
def main(argv): start_time = time.time() inputFile = check_ags(argv) comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank == 0: hashtags, languages = master_worker(comm, inputFile) else: slave_worker(comm, inputFile) total = time.time() - start_time print_output(hashtags, languages) print("\n\n") print(f'Total Time for task is {round(total,4)} seconds.')
def call_mut(dof, max_mutations=None, weights=None): print_output("Performing mutation for: "+str(dof.type)) if max_mutations is not None: if hasattr(self.mol_info, "weights_"+str(dof.type)): weights = getattr(self.mol_info, "weights_"+str(dof.type)) dof.mutate_values(max_mutations, weights) else: dof.mutate_values(max_mutations=max_mutations) else: if hasattr(self.mol_info, "weights_"+str(dof.type)): weights = getattr(self.mol_info, "weights_"+str(dof.type)) dof.mutate_values(weights=weights) else: dof.mutate_values()
def call_mut(dof, max_mutations=None, weights=None): print_output("Performing mutation for: " + str(dof.type)) if max_mutations is not None: if hasattr(self.mol_info, "weights_" + str(dof.type)): weights = getattr(self.mol_info, "weights_" + str(dof.type)) dof.mutate_values(max_mutations, weights) else: dof.mutate_values(max_mutations=max_mutations) else: if hasattr(self.mol_info, "weights_" + str(dof.type)): weights = getattr(self.mol_info, "weights_" + str(dof.type)) dof.mutate_values(weights=weights) else: dof.mutate_values()
def detect_energy_function(params): """ Detect the energy function that will be used for local optimization.""" if "energy_function" not in params: print_output("The energy function hasn't been defined." " The code terminates") sys.exit(0) else: if params["energy_function"] in ["aims", "FHI-aims", "FHIaims"]: print_output("Local optimization will be performed with FHI-aims.") energy_function = "aims" elif params["energy_function"] in ["nwchem", "NWChem"]: print_output("Local optimization will be performed with NWChem.") energy_function = "nwchem" elif params["energy_function"] in ["ff", "force_field", "RDKit", "rdkit"]: print_output("Local optimization will be performed with RDKit.") energy_function = "ff" else: print_output("Unknown type of energy function." " The code terminates.") sys.exit(0) return energy_function
def perform_aims(self, sourcedir, execution_string, dirname): """Generate the FHI-aims input, run FHI-aims, store the output, assign new attributes and update attribute values.""" aims_object = AimsObject(sourcedir) aims_object.generate_input(self.sdf_string) aims_object.build_storage(dirname) success = aims_object.run_aims(execution_string) if success: aims_object.clean_and_store() self.energy = aims_object.get_energy() self.initial_sdf_string = self.sdf_string self.sdf_string = aims2sdf(aims_object.get_aims_string_opt(), self.mol_info.template_sdf_string) for dof in self.dof: setattr(dof, "initial_values", dof.values) dof.update_values(self.sdf_string) else: print_output("The FHI-aims relaxation failed")
def perform_aims(self, sourcedir, execution_string, dirname): """Generate the FHI-aims input, run FHI-aims, store the output, assign new attributes and update attribute values.""" aims_object = AimsObject(sourcedir) aims_object.generate_input(self.sdf_string) aims_object.build_storage(dirname) success = aims_object.run_aims(execution_string) if success: aims_object.clean_and_store() self.energy = aims_object.get_energy() self.initial_sdf_string = self.sdf_string self.sdf_string = aims2sdf(aims_object.get_aims_string_opt(), self.mol_info.template_sdf_string) for dof in self.dof: setattr(dof, "initial_values", dof.values) dof.update_values(self.sdf_string) else: print_output("The FHI-aims relaxation failed")
def get_parameters(self): """Assign permanent attributes (number of atoms, number of bonds and degrees of freedom related attributes) to the object.""" self.atoms, self.bonds = get_atoms_and_bonds(self.smiles) self_copy = deepcopy(self) dof_names = [] for attr, value in self_copy.__dict__.iteritems(): if str(attr).split('_')[0] == "optimize" and value: type_of_dof = str(attr).split('_')[1] linked_attr = {} for attr, value in self_copy.__dict__.iteritems(): if type_of_dof in str(attr).split('_'): linked_attr[attr] = value pos = get_positions(type_of_dof, self.smiles, **linked_attr) if len(pos) != 0: setattr(self, type_of_dof, pos) dof_names.append(type_of_dof) else: print_output("The degree to optimize: " + str(type_of_dof) + " hasn't been found.") setattr(self, "dof_names", dof_names)
def get_parameters(self): """Assign permanent attributes (number of atoms, number of bonds and degrees of freedom related attributes) to the object.""" self.atoms, self.bonds = get_atoms_and_bonds(self.smiles) self_copy = deepcopy(self) dof_names = [] for attr, value in self_copy.__dict__.iteritems(): if str(attr).split('_')[0] == "optimize" and value: type_of_dof = str(attr).split('_')[1] linked_attr = {} for attr, value in self_copy.__dict__.iteritems(): if type_of_dof in str(attr).split('_'): linked_attr[attr] = value pos = get_positions(type_of_dof, self.smiles, **linked_attr) if len(pos) != 0: setattr(self, type_of_dof, pos) dof_names.append(type_of_dof) else: print_output("The degree to optimize: "+str(type_of_dof) + " hasn't been found.") setattr(self, "dof_names", dof_names)
def __repr__(self): """Create an unambiguous object representation. The resulting string is an one-liner with the newline parameter replacing the original '\n' sign in the sdf_string and initial_sdf_string attribute.""" repr_list = [] for att_name in self.__dict__.keys(): if att_name in ["sdf_string", "initial_sdf_string"]: repr_list.append("%s='%s'" % ( att_name, getattr( self, att_name).replace("\n", Structure.newline))) else: if type(self.__dict__[att_name]) in [str]: repr_list.append('%s=%s' % ( att_name, repr(getattr(self, att_name)))) elif type(self.__dict__[att_name]) in [int, float, bool]: repr_list.append('%s=%s' % ( att_name, repr(getattr(self, att_name)))) elif att_name == 'dof': for dof in self.dof: repr_list.append('%s_%s=%s' % ( dof.type, "values", repr(dof.values))) try: repr_list.append('%s_%s=%s' % ( dof.type, "initial_values", repr(dof.initial_values))) except: pass elif att_name == 'mol_info': pass else: print_output("Unknown type of attribute "+str(att_name)) return "%s(mol, %s)" % (self.__class__.__name__, ', '.join(repr_list))
def __init__(self, arg=None, **kwargs): """Initialize the 3D structure: (1) from MoleculeDescription class object or from (2) from previously created object of the Structure class. Any present and valid keyword args overwrite the old values. Warning: there may be more attributes in the (2) case.""" if isinstance(arg, MoleculeDescription): self.mol_info = arg Structure.index += 1 self.index = Structure.index dof = [] for i in self.mol_info.dof_names: new_obj = create_dof_object(str(i), getattr(self.mol_info, i)) dof.append(new_obj) setattr(self, "dof", dof) elif isinstance(arg, Structure): self.mol_info = arg.mol_info Structure.index += 1 self.index = Structure.index for att_name in arg.__dict__.keys(): if att_name not in ["mol_info", "index"]: setattr(self, str(att_name), deepcopy(getattr(arg, str(att_name)))) else: print_output("Initialization can't be performed. Check the input") for key in kwargs.keys(): if key != "index": if hasattr(self, str(key)): print_output("Overwriting the value for keyword " + str(key)) print_output("Old value: " + str(getattr(self, str(key))) + ", new value: " + str(kwargs[key])) if key in ["sdf_string", "initial_sdf_string"]: setattr(self, str(key), kwargs[key].replace(Structure.newline, "\n")) elif key.split('_')[0] in self.mol_info.dof_names: for dof in self.dof: if key.split('_')[0] == dof.type: if key.split('_')[1] == 'initial': setattr(dof, 'initial_values', kwargs[key]) if key.split('_')[1] == 'values': setattr(dof, 'values', kwargs[key]) else: setattr(self, str(key), kwargs[key])
def __init__(self, arg=None, **kwargs): """Initialize the 3D structure: (1) from MoleculeDescription class object or from (2) from previously created object of the Structure class. Any present and valid keyword args overwrite the old values. Warning: there may be more attributes in the (2) case.""" if isinstance(arg, MoleculeDescription): self.mol_info = arg Structure.index += 1 self.index = Structure.index dof = [] for i in self.mol_info.dof_names: new_obj = create_dof_object(str(i), getattr(self.mol_info, i)) dof.append(new_obj) setattr(self, "dof", dof) elif isinstance(arg, Structure): self.mol_info = arg.mol_info Structure.index += 1 self.index = Structure.index for att_name in arg.__dict__.keys(): if att_name not in ["mol_info", "index"]: setattr(self, str(att_name), deepcopy(getattr(arg, str(att_name)))) else: print_output("Initialization can't be performed. Check the input") for key in kwargs.keys(): if key != "index": if hasattr(self, str(key)): print_output("Overwriting the value for keyword "+str(key)) print_output("Old value: "+str(getattr(self, str(key))) + ", new value: "+str(kwargs[key])) if key in ["sdf_string", "initial_sdf_string"]: setattr(self, str(key), kwargs[key].replace(Structure.newline, "\n")) elif key.split('_')[0] in self.mol_info.dof_names: for dof in self.dof: if key.split('_')[0] == dof.type: if key.split('_')[1] == 'initial': setattr(dof, 'initial_values', kwargs[key]) if key.split('_')[1] == 'values': setattr(dof, 'values', kwargs[key]) else: setattr(self, str(key), kwargs[key])
def convert(self): value = self.conv_weight(self.weight, self.metric, self.target) u.print_output(self.weight, self.metric, value, self.target) return value
def str_info(struct): """ Prints the information about the structure to the output file""" print_output(struct) for dof in struct.dof: print_output("Values of " + str(dof.type) + ": " + str(dof.values))
def str_info(struct): """ Prints the information about the structure to the output file""" print_output(struct) for dof in struct.dof: print_output("Values of " + str(dof.type) + ": " + str(dof.values))
def convert(self): value = self.conv_mem(self.size, self.metric, self.target) u.print_output(self.size, self.metric, value, self.target) return value;
def convert (self): value = self.conv_weight(self.weight, self.metric, self.target) u.print_output(self.weight, self.metric, value, self.target) return value;
def check_for_convergence(iteration, params, min_energy): """Check the run for convergence""" if iteration >= params["iter_limit_conv"] - 1: print_output("Checking for convergence") d = abs(min_energy[iteration + 1] - min_energy[iteration + 1 - params["iter_limit_conv"]]) if "energy_wanted" in params: if min_energy[-1] < params["energy_wanted"] or d < params["energy_diff_conv"]: print_output("Converged") killfile = open("kill.dat", "w") killfile.close() sys.exit(0) else: print_output("Not converged yet") else: if d < params["energy_diff_conv"]: print_output("Converged") killfile = open("kill.dat", "w") killfile.close() sys.exit(0) else: print_output("Not converged yet") if iteration == params["max_iter"] - 1: print_output("Max. number of iterations reached. The code terminates") killfile = open("kill.dat", "w") killfile.close() sys.exit(0) else: print_output("Next iteration will be perfomed")
def detect_energy_function(params): """ Detect the energy function that will be used for local optimization.""" if 'energy_function' not in params: print_output("The energy function hasn't been defined." " The code terminates") sys.exit(0) else: if params['energy_function'] in ['aims', 'FHI-aims', 'FHIaims']: print_output("Local optimization will be performed with FHI-aims.") energy_function = "aims" elif params['energy_function'] in ['nwchem', 'NWChem']: print_output("Local optimization will be performed with NWChem.") energy_function = "nwchem" elif params['energy_function'] in ['ORCA', 'Orca', 'orca']: print_output("Local optimization will be performed with ORCA.") energy_function = "orca" elif params['energy_function'] in [ 'ff', 'force_field', 'RDKit', 'rdkit' ]: print_output("Local optimization will be performed with RDKit.") energy_function = "ff" else: print_output("Unknown type of energy function." " The code terminates.") sys.exit(0) return energy_function
def convert(self): value = self.conv_dist(self.distance, self.metric, self.target) u.print_output(self.distance, self.metric, value, self.target) return value
def detect_energy_function(params): """ Detect the energy function that will be used for local optimization.""" if 'energy_function' not in params: print_output("The energy function hasn't been defined." " The code terminates") sys.exit(0) else: if params['energy_function'] in ['aims', 'FHI-aims', 'FHIaims']: print_output("Local optimization will be performed with FHI-aims.") energy_function = "aims" elif params['energy_function'] in ['nwchem', 'NWChem']: print_output("Local optimization will be performed with NWChem.") energy_function = "nwchem" elif params['energy_function'] in ['ORCA', 'Orca', 'orca']: print_output("Local optimization will be performed with ORCA.") energy_function = "orca" elif params['energy_function'] in ['ff', 'force_field', 'RDKit', 'rdkit']: print_output("Local optimization will be performed with RDKit.") energy_function = "ff" else: print_output("Unknown type of energy function." " The code terminates.") sys.exit(0) return energy_function
def convert (self): value = self.conv_dist(self.distance, self.metric, self.target) u.print_output(self.distance, self.metric, value, self.target) return value;
def check_for_convergence(iteration, params, min_energy): """Check the run for convergence""" if iteration >= params['iter_limit_conv'] - 1: print_output("Checking for convergence") d = abs(min_energy[iteration + 1] - min_energy[iteration + 1 - params['iter_limit_conv']]) if 'energy_wanted' in params: if min_energy[-1] < params['energy_wanted'] or \ d < params['energy_diff_conv']: print_output("Converged") killfile = open("kill.dat", "w") killfile.close() sys.exit(0) else: print_output("Not converged yet") else: if d < params['energy_diff_conv']: print_output("Converged") killfile = open("kill.dat", "w") killfile.close() sys.exit(0) else: print_output("Not converged yet") if iteration == params['max_iter'] - 1: print_output("Max. number of iterations reached. The code terminates") killfile = open("kill.dat", "w") killfile.close() sys.exit(0) else: print_output("Next iteration will be perfomed")