def test_lammpstraj_get_step_struct(db_test_app, data_regression): path = os.path.join(TEST_DIR, "input_files", "trajectory.lammpstrj") data = LammpsTrajectory(path) data_regression.check( recursive_round(data.get_step_structure(-1).attributes, 2, apply_lists=True))
def test_lammpstraj_get_step_struct(db_test_app, data_regression): # pylint: disable=unused-argument """Get the structure data for a given trajectory step""" path = os.path.join(TEST_DIR, 'input_files', 'trajectory.lammpstrj') data = LammpsTrajectory(path) data_regression.check( recursive_round(data.get_step_structure(-1).attributes, 2, apply_lists=True))
def parse(self, **kwargs): """Parses the datafolder, stores results.""" resources = self.get_parsing_resources(kwargs, traj_in_temp=True) if resources.exit_code is not None: return resources.exit_code log_data, exit_code = self.parse_log_file() if exit_code is not None: return exit_code traj_error = None if not resources.traj_paths: traj_error = self.exit_codes.ERROR_TRAJ_FILE_MISSING else: try: trajectory_data = LammpsTrajectory( resources.traj_paths[0], aliases={ 'stresses': [f'c_stpa[{i+1}]' for i in range(6)], 'forces': ['fx', 'fy', 'fz'], }, ) self.out('trajectory_data', trajectory_data) self.out( 'structure', trajectory_data.get_step_structure( -1, original_structure=self.node.inputs.structure), ) except Exception as err: # pylint: disable=broad-except traceback.print_exc() self.logger.error(str(err)) traj_error = self.exit_codes.ERROR_TRAJ_PARSING # save results into node output_data = log_data['data'] if 'units_style' in output_data: output_data.update( get_units_dict( output_data['units_style'], ['energy', 'force', 'distance', 'pressure'], )) output_data['stress_units'] = output_data.pop('pressure_units') else: self.logger.warning('units missing in log') self.add_warnings_and_errors(output_data) self.add_standard_info(output_data) parameters_data = Dict(dict=output_data) self.out('results', parameters_data) if output_data['errors']: return self.exit_codes.ERROR_LAMMPS_RUN if traj_error: return traj_error if not log_data.get('found_end', False): return self.exit_codes.ERROR_RUN_INCOMPLETE return None
def parse(self, **kwargs): """Parses the datafolder, stores results.""" resources = self.get_parsing_resources(kwargs, traj_in_temp=True) if resources.exit_code is not None: return resources.exit_code log_data, exit_code = self.parse_log_file() if exit_code is not None: return exit_code traj_error = None if not resources.traj_paths: traj_error = self.exit_codes.ERROR_TRAJ_FILE_MISSING else: try: trajectory_data = LammpsTrajectory( resources.traj_paths[0], aliases={ "stresses": ["c_stpa[{}]".format(i + 1) for i in range(6)], "forces": ["fx", "fy", "fz"], }, ) self.out("trajectory_data", trajectory_data) self.out( "structure", trajectory_data.get_step_structure( -1, original_structure=self.node.inputs.structure), ) except Exception as err: traceback.print_exc() self.logger.error(str(err)) traj_error = self.exit_codes.ERROR_TRAJ_PARSING # save results into node output_data = log_data["data"] if "units_style" in output_data: output_data.update( get_units_dict( output_data["units_style"], ["energy", "force", "distance", "pressure"], )) output_data["stress_units"] = output_data.pop("pressure_units") else: self.logger.warning("units missing in log") self.add_warnings_and_errors(output_data) self.add_standard_info(output_data) parameters_data = Dict(dict=output_data) self.out("results", parameters_data) if output_data["errors"]: return self.exit_codes.ERROR_LAMMPS_RUN if traj_error: return traj_error if not log_data.get("found_end", False): return self.exit_codes.ERROR_RUN_INCOMPLETE
def test_lammpstraj_timesteps(db_test_app): # pylint: disable=unused-argument """Get the list of timesteps for the trajectory data""" path = os.path.join(TEST_DIR, 'input_files', 'trajectory.lammpstrj') data = LammpsTrajectory(path) assert data.time_steps == [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, ]
def test_lammpstraj_timesteps(db_test_app): path = os.path.join(TEST_DIR, "input_files", "trajectory.lammpstrj") data = LammpsTrajectory(path) assert data.time_steps == [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, ]
def parse(self, **kwargs): """Parse the retrieved folder and store results.""" # pylint: disable=too-many-locals, too-many-branches, too-many-return-statements # retrieve resources resources = self.get_parsing_resources(kwargs, traj_in_temp=True) if resources.exit_code is not None: return resources.exit_code # parse log file log_data, exit_code = self.parse_log_file() if exit_code is not None: return exit_code traj_error = None if not resources.traj_paths: traj_error = self.exit_codes.ERROR_TRAJ_FILE_MISSING else: try: trajectory_data = LammpsTrajectory(resources.traj_paths[0]) self.out('trajectory_data', trajectory_data) except Exception as err: # pylint: disable=broad-except traceback.print_exc() self.logger.error(str(err)) traj_error = self.exit_codes.ERROR_TRAJ_PARSING # save results into node output_data = log_data['data'] if 'units_style' in output_data: output_data.update( get_units_dict(output_data['units_style'], ['distance', 'time', 'energy'])) else: self.logger.warning('units missing in log') self.add_warnings_and_errors(output_data) self.add_standard_info(output_data) if 'parameters' in self.node.get_incoming().all_link_labels(): output_data['timestep_picoseconds'] = convert_units( self.node.inputs.parameters.dict.timestep, output_data['units_style'], 'time', 'picoseconds', ) parameters_data = Dict(dict=output_data) self.out('results', parameters_data) # parse the system data file sys_data_error = None if resources.sys_paths: sys_data = ArrayData() try: with open(resources.sys_paths[0]) as handle: names = handle.readline().strip().split() for i, col in enumerate( np.loadtxt(resources.sys_paths[0], skiprows=1, unpack=True, ndmin=2)): sys_data.set_array(names[i], col) except Exception: # pylint: disable=broad-except traceback.print_exc() sys_data_error = self.exit_codes.ERROR_INFO_PARSING sys_data.set_attribute('units_style', output_data.get('units_style', None)) self.out('system_data', sys_data) if output_data['errors']: return self.exit_codes.ERROR_LAMMPS_RUN if traj_error: return traj_error if sys_data_error: return sys_data_error if not log_data.get('found_end', False): return self.exit_codes.ERROR_RUN_INCOMPLETE return None
def parse(self, **kwargs): """Parse the retrieved folder and store results.""" # retrieve resources resources = self.get_parsing_resources(kwargs, traj_in_temp=True) if resources.exit_code is not None: return resources.exit_code # parse log file log_data, exit_code = self.parse_log_file() if exit_code is not None: return exit_code traj_error = None if not resources.traj_paths: traj_error = self.exit_codes.ERROR_TRAJ_FILE_MISSING else: try: trajectories = { os.path.basename(traj_path).split("-")[0]: LammpsTrajectory(traj_path) for traj_path in resources.traj_paths } self.out("trajectory", trajectories) except Exception as err: traceback.print_exc() self.logger.error(str(err)) traj_error = self.exit_codes.ERROR_TRAJ_PARSING # save results into node output_data = log_data["data"] if "units_style" in output_data: output_data.update( get_units_dict(output_data["units_style"], ["distance", "time", "energy"])) else: self.logger.warning("units missing in log") self.add_warnings_and_errors(output_data) self.add_standard_info(output_data) if "parameters" in self.node.get_incoming().all_link_labels(): output_data["timestep_picoseconds"] = convert_units( self.node.inputs.parameters.dict.timestep, output_data["units_style"], "time", "picoseconds", ) output_data["stage_names"] = [ s["name"] for s in self.node.inputs.parameters.dict.stages ] parameters_data = Dict(dict=output_data) self.out("results", parameters_data) # parse the system data file sys_data_error = None arrays = {} for sys_path in resources.sys_paths: stage_name = os.path.basename(sys_path).split("-")[0] sys_data = ArrayData() sys_data.set_attribute("units_style", output_data.get("units_style", None)) try: with open(sys_path) as handle: names = handle.readline().strip().split() for i, col in enumerate( np.loadtxt(sys_path, skiprows=1, unpack=True, ndmin=2)): sys_data.set_array(names[i], col) arrays[stage_name] = sys_data except Exception: traceback.print_exc() sys_data_error = self.exit_codes.ERROR_INFO_PARSING if arrays: self.out("system", arrays) # retrieve the last restart file, per stage restart_map = {} for rpath in resources.restart_paths: rpath_base = os.path.basename(rpath) match = re.match(r"([^\-]*)\-.*\.([\d]+)", rpath_base) if match: stage, step = match.groups() if int(step) > restart_map.get(stage, (-1, None))[0]: restart_map[stage] = (int(step), rpath) for stage, (step, rpath) in restart_map.items(): with io.open(rpath, "rb") as handle: self.retrieved.put_object_from_filelike( handle, os.path.basename(rpath), "wb", force=True) if output_data["errors"]: return self.exit_codes.ERROR_LAMMPS_RUN if traj_error: return traj_error if sys_data_error: return sys_data_error if not log_data.get("found_end", False): return self.exit_codes.ERROR_RUN_INCOMPLETE
def test_lammps_trajectory_data(db_test_app, data_regression): path = os.path.join(TEST_DIR, "input_files", "trajectory.lammpstrj") data = LammpsTrajectory(path) data_regression.check(data.attributes)
def test_lammpstraj_get_step_string(db_test_app, file_regression): path = os.path.join(TEST_DIR, "input_files", "trajectory.lammpstrj") data = LammpsTrajectory(path) file_regression.check(data.get_step_string(-1))
def parse(self, **kwargs): """Parse the retrieved folder and store results.""" # retrieve resources resources = self.get_parsing_resources(kwargs, traj_in_temp=True) if resources.exit_code is not None: return resources.exit_code # parse log file log_data, exit_code = self.parse_log_file() if exit_code is not None: return exit_code traj_error = None if not resources.traj_paths: traj_error = self.exit_codes.ERROR_TRAJ_FILE_MISSING else: try: trajectory_data = LammpsTrajectory(resources.traj_paths[0]) self.out("trajectory_data", trajectory_data) except Exception as err: traceback.print_exc() self.logger.error(str(err)) traj_error = self.exit_codes.ERROR_TRAJ_PARSING # save results into node output_data = log_data["data"] if "units_style" in output_data: output_data.update( get_units_dict(output_data["units_style"], ["distance", "time", "energy"])) else: self.logger.warning("units missing in log") self.add_warnings_and_errors(output_data) self.add_standard_info(output_data) if "parameters" in self.node.get_incoming().all_link_labels(): output_data["timestep_picoseconds"] = convert_units( self.node.inputs.parameters.dict.timestep, output_data["units_style"], "time", "picoseconds", ) parameters_data = Dict(dict=output_data) self.out("results", parameters_data) # parse the system data file sys_data_error = None if resources.sys_paths: sys_data = ArrayData() try: with open(resources.sys_paths[0]) as handle: names = handle.readline().strip().split() for i, col in enumerate( np.loadtxt(resources.sys_paths[0], skiprows=1, unpack=True, ndmin=2)): sys_data.set_array(names[i], col) except Exception: traceback.print_exc() sys_data_error = self.exit_codes.ERROR_INFO_PARSING sys_data.set_attribute("units_style", output_data.get("units_style", None)) self.out("system_data", sys_data) if output_data["errors"]: return self.exit_codes.ERROR_LAMMPS_RUN if traj_error: return traj_error if sys_data_error: return sys_data_error if not log_data.get("found_end", False): return self.exit_codes.ERROR_RUN_INCOMPLETE
def test_write_as_lammps(db_test_app, tmp_path): path = os.path.join(TEST_DIR, "input_files", "trajectory.lammpstrj") data = LammpsTrajectory(path) with tmp_path.joinpath("trajectory.lammpstrj").open(mode="wb") as handle: data.write_as_lammps(handle)
def test_write_as_lammps(db_test_app, tmp_path): # pylint: disable=unused-argument """Test that one can write the trajectory in a lammps compatible format""" path = os.path.join(TEST_DIR, 'input_files', 'trajectory.lammpstrj') data = LammpsTrajectory(path) with tmp_path.joinpath('trajectory.lammpstrj').open(mode='wb') as handle: data.write_as_lammps(handle)
def parse(self, **kwargs): """ Parse the files produced by lammps. It takes care of parsing the log.lammps file, the trajectory file and the yaml file with the final value of the variables printed in the ``thermo_style``. """ # pylint: disable=too-many-return-statements try: out_folder = self.retrieved except exceptions.NotExistent: return self.exit_codes.ERROR_NO_RETRIEVED_FOLDER list_of_files = out_folder.list_object_names() # check log file if self.node.get_option('logfile_filename') not in list_of_files: return self.exit_codes.ERROR_LOG_FILE_MISSING filename = self.node.get_option('logfile_filename') parsed_data = parse_logfile(file_contents=self.node.outputs.retrieved. get_object_content(filename)) if parsed_data is None: return self.exit_codes.ERROR_PARSING_LOGFILE global_data = parsed_data['global'] arrays = parsed_data['time_dependent'] # check final variable file if self.node.get_option('variables_filename') not in list_of_files: return self.exit_codes.ERROR_FINAL_VARIABLE_FILE_MISSING filename = self.node.get_option('variables_filename') final_variables = parse_final_data( file_contents=self.node.outputs.retrieved.get_object_content( filename)) if final_variables is None: return self.exit_codes.ERROR_PARSING_FINAL_VARIABLES results = orm.Dict(dict={ **final_variables, 'compute_variables': global_data }) # Expose the results from the log.lammps outputs self.out('results', results) # Get the time-dependent outputs exposed as a dictionary time_dependent_computes = orm.Dict(dict=arrays) self.out('time_dependent_computes', time_dependent_computes) # check trajectory file if self.node.get_option('trajectory_filename') not in list_of_files: return self.exit_codes.ERROR_TRAJECTORY_FILE_MISSING # Gather the lammps trajectory data filename = self.node.get_option('trajectory_filename') with self.node.outputs.retrieved.open(filename) as handle: lammps_trajectory = LammpsTrajectory(handle) self.out('trajectories', lammps_trajectory) self.out('structure', lammps_trajectory.get_step_structure(-1)) # check stdout if self.node.get_option('scheduler_stdout') not in list_of_files: return self.exit_codes.ERROR_STDOUT_FILE_MISSING # check stderr if self.node.get_option('scheduler_stderr') not in list_of_files: return self.exit_codes.ERROR_STDERR_FILE_MISSING return None
def parse(self, **kwargs): """Parse the retrieved folder and store results.""" # pylint: disable= too-many-locals, too-many-branches, too-many-statements, too-many-return-statements # retrieve resources resources = self.get_parsing_resources(kwargs, traj_in_temp=True) if resources.exit_code is not None: return resources.exit_code # parse log file log_data, exit_code = self.parse_log_file() if exit_code is not None: return exit_code traj_error = None if not resources.traj_paths: traj_error = self.exit_codes.ERROR_TRAJ_FILE_MISSING else: try: trajectories = { os.path.basename(traj_path).split('-')[0]: LammpsTrajectory(traj_path) for traj_path in resources.traj_paths } self.out('trajectory', trajectories) except Exception as err: # pylint: disable=broad-except traceback.print_exc() self.logger.error(str(err)) traj_error = self.exit_codes.ERROR_TRAJ_PARSING # save results into node output_data = log_data['data'] if 'units_style' in output_data: output_data.update( get_units_dict(output_data['units_style'], ['distance', 'time', 'energy'])) else: self.logger.warning('units missing in log') self.add_warnings_and_errors(output_data) self.add_standard_info(output_data) if 'parameters' in self.node.get_incoming().all_link_labels(): output_data['timestep_picoseconds'] = convert_units( self.node.inputs.parameters.dict.timestep, output_data['units_style'], 'time', 'picoseconds', ) output_data['stage_names'] = [ s['name'] for s in self.node.inputs.parameters.dict.stages ] parameters_data = Dict(dict=output_data) self.out('results', parameters_data) # parse the system data file sys_data_error = None arrays = {} for sys_path in resources.sys_paths: stage_name = os.path.basename(sys_path).split('-')[0] sys_data = ArrayData() sys_data.set_attribute('units_style', output_data.get('units_style', None)) try: with open(sys_path) as handle: names = handle.readline().strip().split() for i, col in enumerate( np.loadtxt(sys_path, skiprows=1, unpack=True, ndmin=2)): sys_data.set_array(names[i], col) arrays[stage_name] = sys_data except Exception: # pylint: disable=broad-except traceback.print_exc() sys_data_error = self.exit_codes.ERROR_INFO_PARSING if arrays: self.out('system', arrays) # retrieve the last restart file, per stage restart_map = {} for rpath in resources.restart_paths: rpath_base = os.path.basename(rpath) match = re.match(r'([^\-]*)\-.*\.([\d]+)', rpath_base) if match: stage, step = match.groups() if int(step) > restart_map.get(stage, (-1, None))[0]: restart_map[stage] = (int(step), rpath) for stage, (step, rpath) in restart_map.items(): with io.open(rpath, 'rb') as handle: self.retrieved.put_object_from_filelike( handle, os.path.basename(rpath), 'wb', force=True) if output_data['errors']: return self.exit_codes.ERROR_LAMMPS_RUN if traj_error: return traj_error if sys_data_error: return sys_data_error if not log_data.get('found_end', False): return self.exit_codes.ERROR_RUN_INCOMPLETE return None
def test_lammpstraj_get_step_string(db_test_app, file_regression): # pylint: disable=unused-argument """Get the step information from a trajectory data file""" path = os.path.join(TEST_DIR, 'input_files', 'trajectory.lammpstrj') data = LammpsTrajectory(path) file_regression.check(data.get_step_string(-1))
def test_lammps_trajectory_data(db_test_app, data_regression): # pylint: disable=unused-argument """Test that one can generate a trajectory from a file""" path = os.path.join(TEST_DIR, 'input_files', 'trajectory.lammpstrj') data = LammpsTrajectory(path) data_regression.check(data.attributes)