def calculation_required(self, atoms=None, quantities=None): """Check if a calculation is required. Available quantitites: forces, energy This method is used to check if a quantity is available without further calculations. For unknown/unsupported quantities this returns true, indicating that the quantity is not available. Because it is very hard to know what parameters affect the calculation of different quantities (energy, forces, etc.), this function simply checks that has the input file changed since last evaluation. args: quantitities: list of strings The names of the required quantitities. """ available_quantities = ["energy", "forces"] for quantity in quantities: if quantity not in available_quantities: print_warning("Quantity '{}' not available.".format(quantity)) return True if self.old_input is None: return True else: new_input = self.CP2K_INPUT._print_input(-1) return new_input != self.old_input
def vmd_view_trajectory(self): """View the MD trajectory with VMD. Opens a blocking vmd window and loads the .xyz file created from MD simulation. """ # Run in the output directory by default, can be changed with working_directory if self.working_directory is None: working_directory = os.path.dirname(self.output_path) else: working_directory = self.working_directory # Determine the file format file_format = None for print_section in self.CP2K_INPUT.MOTION.PRINT_list: file_format = print_section.TRAJECTORY.Format if file_format is not None: break if file_format is None or file_format == "XYZ" or file_format == "XMOL": file_suffix = ".xyz" elif file_format == "PDB": file_suffix = ".pdb" elif file_format == "DCD": file_suffix = ".dcd" else: print_warning("Cannot open the file format {} directly with VMD.".format(file_format)) return filename = working_directory + "/" + self.CP2K_INPUT.GLOBAL.Project_name + "-pos-1" + file_suffix command = "vmd " + filename # Call the subprocess. shell=True is used to access srun and # environment variable expansions. call(command, shell=True, cwd=working_directory)
def calculation_required(self, atoms=None, quantities=None): """Check if a calculation is required. Available quantitites: forces, energy This method is used to check if a quantity is available without further calculations. For unknown/unsupported quantities this returns true, indicating that the quantity is not available. Because it is very hard to know what parameters affect the calculation of different quantities (energy, forces, etc.), this function simply checks that has the input file changed since last evaluation. args: quantitities: list of strings The names of the required quantitities. """ available_quantities = ["energy", "forces"] for quantity in quantities: if quantity not in available_quantities: print_warning("Quantity '{}' not available.".format(quantity)) return True if self.old_input is None: return True else: new_input = self.CP2K_INPUT._print_input(-1) return new_input != self.old_input
def vmd_view_trajectory(self): """View the MD trajectory with VMD. Opens a blocking vmd window and loads the .xyz file created from MD simulation. """ # Run in the output directory by default, can be changed with working_directory if self.working_directory is None: working_directory = os.path.dirname(self.output_path) else: working_directory = self.working_directory # Determine the file format file_format = None for print_section in self.CP2K_INPUT.MOTION.PRINT_list: file_format = print_section.TRAJECTORY.Format if file_format is not None: break if file_format is None or file_format == "XYZ" or file_format == "XMOL": file_suffix = ".xyz" elif file_format == "PDB": file_suffix = ".pdb" elif file_format == "DCD": file_suffix = ".dcd" else: print_warning( "Cannot open the file format {} directly with VMD.".format( file_format)) return filename = working_directory + "/" + self.CP2K_INPUT.GLOBAL.Project_name + "-pos-1" + file_suffix command = "vmd " + filename # Call the subprocess. shell=True is used to access srun and # environment variable expansions. call(command, shell=True, cwd=working_directory)
def run(self, print_input=False): """Runs the input script.""" print_title("PYCP2K RUN STARTED") # Run in the output directory by default, can be changed with working_directory if self.working_directory is None: working_directory = os.path.dirname(self.output_path) else: working_directory = self.working_directory # Check that the cp2k version is correct print_text(">> CP2K version check...") command_for_version_check = " ".join([self.cp2k_command, "-v "]) version_result = check_output(command_for_version_check, shell=True, cwd=working_directory) result_lines = version_result.splitlines() run_version = result_lines[0].split()[2].decode() run_revision = result_lines[1].split()[-1].decode() build_version = pycp2k.config.build_version build_revision = pycp2k.config.build_revision if run_version != build_version: print_warning( "The CP2K version does not match the version for which PYCP2K was configured ({}). This migh affect the availability of keywords and sections in the input tree!" .format(build_version)) elif run_revision != build_revision: print_warning( "The CP2K revision does not match the version for which PYCP2K was configured ({}). This might affect the availability of keywords and sections in the input tree!" .format(build_revision)) # Write input file and possibly show it print_text(">> Creating CP2K input file...") self.write_input_file() if print_input: print_text(">> CP2K input file:") print(self.input) command_list = [] # MPI command and flags if self.mpi_on: command_list.append(self.mpi_command) if self.mpi_n_processes is not None: command_list.append("-n " + str(self.mpi_n_processes)) for flag in self.mpi_flags: command_list.append(str(flag)) # CP2K command and flags command_list.append(self.cp2k_command) command_list.append("-o " + str(self.output_path)) command_list.append("-i " + str(self.input_path)) for flag in self.cp2k_flags: command_list.append(str(flag)) # When shell=True the command is passed as string rather than a # sequence as instructed in subprocess documentation. command_string = " ".join(command_list) # Perform syntax check print_text(">> Performing syntax check on input file...") command_for_syntax_check = " ".join( [self.cp2k_command, "-i " + str(self.input_path), "--check"]) syntax_result = (check_output(command_for_syntax_check, shell=True, cwd=working_directory)).decode("utf-8") success_regex = re.compile("^SUCCESS", re.MULTILINE) match = success_regex.search(syntax_result) if match is None: print_error( "Syntax error in the input file. See the following output for further details." ) print(syntax_result) raise Exception # Call the subprocess. shell=True is used to access srun and # environment variable expansions. print_text(">> Running CP2K:") print_text(" -CP2K version: {}".format(run_version)) print_text(" -CP2K revision: {}".format(run_revision)) print_text(" -CP2K command: {}".format( os.path.basename(self.cp2k_command))) if self.mpi_on: print_text(" -MPI command: {}".format(self.mpi_command)) print_text(" -Processes: {}".format(self.mpi_n_processes)) start = time.time() try: check_output(command_string, shell=True, cwd=working_directory) except CalledProcessError: error_output_path = self.output_path print_error( "Error occured during CP2K calculation. See the output from {} for further details." .format(error_output_path)) raise end = time.time() elapsed = end - start m, s = divmod(elapsed, 60) h, m = divmod(m, 60) print_text(">> CP2K calculation finished succesfully!") print_text(">> Elapsed time: {:.0f}h:{:.0f}m:{:.0f}s".format(h, m, s)) # Save this input to old_input self.old_input = self.CP2K_INPUT._print_input(-1) print_title("PYCP2K RUN FINISHED")
def run(self, print_input=False): """Runs the input script.""" print_title("PYCP2K RUN STARTED") # Run in the output directory by default, can be changed with working_directory if self.working_directory is None: working_directory = os.path.dirname(self.output_path) else: working_directory = self.working_directory # Check that the cp2k version is correct print_text(">> CP2K version check...") command_for_version_check = " ".join([self.cp2k_command, "-v "]) version_result = check_output(command_for_version_check, shell=True, cwd=working_directory) result_lines = version_result.splitlines() run_version = result_lines[0].split()[2].decode() run_revision = result_lines[1].split()[-1].decode() build_version = pycp2k.config.build_version build_revision = pycp2k.config.build_revision if run_version != build_version: print_warning("The CP2K version does not match the version for which PYCP2K was configured ({}). This migh affect the availability of keywords and sections in the input tree!".format(build_version)) elif run_revision != build_revision: print_warning("The CP2K revision does not match the version for which PYCP2K was configured ({}). This might affect the availability of keywords and sections in the input tree!".format(build_revision)) # Write input file and possibly show it print_text(">> Creating CP2K input file...") self.write_input_file() if print_input: print_text(">> CP2K input file:") print(self.input) command_list = [] # MPI command and flags if self.mpi_on: command_list.append(self.mpi_command) if self.mpi_n_processes is not None: command_list.append("-n " + str(self.mpi_n_processes)) for flag in self.mpi_flags: command_list.append(str(flag)) # CP2K command and flags command_list.append(self.cp2k_command) command_list.append("-o " + str(self.output_path)) command_list.append("-i " + str(self.input_path)) for flag in self.cp2k_flags: command_list.append(str(flag)) # When shell=True the command is passed as string rather than a # sequence as instructed in subprocess documentation. command_string = " ".join(command_list) # Perform syntax check print_text(">> Performing syntax check on input file...") command_for_syntax_check = " ".join([self.cp2k_command, "-i " + str(self.input_path), "--check"]) syntax_result = (check_output(command_for_syntax_check, shell=True, cwd=working_directory)).decode("utf-8") success_regex = re.compile("^SUCCESS", re.MULTILINE) match = success_regex.search(syntax_result) if match is None: print_error("Syntax error in the input file. See the following output for further details.") print(syntax_result) raise Exception # Call the subprocess. shell=True is used to access srun and # environment variable expansions. print_text(">> Running CP2K:") print_text(" -CP2K version: {}".format(run_version)) print_text(" -CP2K revision: {}".format(run_revision)) print_text(" -CP2K command: {}".format(os.path.basename(self.cp2k_command))) if self.mpi_on: print_text(" -MPI command: {}".format(self.mpi_command)) print_text(" -Processes: {}".format(self.mpi_n_processes)) start = time.time() try: check_output(command_string, shell=True, cwd=working_directory) except CalledProcessError: error_output_path = self.output_path print_error("Error occured during CP2K calculation. See the output from {} for further details.".format(error_output_path)) raise end = time.time() elapsed = end - start m, s = divmod(elapsed, 60) h, m = divmod(m, 60) print_text(">> CP2K calculation finished succesfully!") print_text(">> Elapsed time: {:.0f}h:{:.0f}m:{:.0f}s".format(h, m, s)) # Save this input to old_input self.old_input = self.CP2K_INPUT._print_input(-1) print_title("PYCP2K RUN FINISHED")