Skip to content

Commit

Permalink
Fix CP2K restart file (#195)
Browse files Browse the repository at this point in the history
* Fixed frame in get_trajectory

Improved extraction of input file

* Parsing project_name instead of appending input_filename with -1.restart
  • Loading branch information
JosePizarro3 authored Feb 6, 2024
1 parent 52336ce commit ec71a8c
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions electronicparsers/cp2k/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def get_trajectory(self, frame):
filename = self._normalize_filename(filename)
traj_format = self.inp_parser.get('MOTION/PRINT/TRAJECTORY/FORMAT', 'XYZ').strip()
traj_format = self._file_extension_map.get(traj_format, 'xyz')
filename = '%s-pos-1.%s' % (filename, traj_format)
filename = f'{filename}-pos-1.{traj_format}'
frequency = 1

self.traj_parser.mainfile = os.path.join(self.maindir, filename)
Expand All @@ -944,7 +944,10 @@ def get_trajectory(self, frame):
try:
return self.traj_parser.get_trajectory(frame)
except Exception:
self.logger.error('Error reading trajectory.')
self.logger.error(
'Error reading trajectory for the specific frame.',
data={'frame': frame},
)

def get_lattice_vectors(self, frame):
lattice_vectors = None
Expand Down Expand Up @@ -1127,8 +1130,9 @@ def parse_input(self):
'''
Parses input file from the settings.
'''
input_filename = self.settings.get('cp2k', {}).get('input_filename', None)
if input_filename is None:
input_filename = self.settings.get('cp2k', {}).get('input_filename')
project_name = self.settings.get('global', {}).get('project_name')
if input_filename is None and project_name is None:
return

definitions = dict(m_env.all_definitions_by_name)
Expand Down Expand Up @@ -1169,8 +1173,17 @@ def parse(name, data, section):

input_files = get_files(input_filename, self.filepath, self.mainfile, deep=False)
if not input_files:
self.logger.warning(f'Input file not found.')
return
self.logger.warning(
'Input *.inp file not found. We will attempt finding the restart file from '
'the project_name appending a -1, <project_name>-1.restart.',
data={'project_name': project_name},
)
# Patch to check if the input is .restart and CP2K appended a -1 after the name
if project_name:
project_filename = f'{project_name}-1.restart'
input_files = get_files(project_filename, self.filepath, self.mainfile, deep=False)
else:
return
if len(input_files) > 1:
self.logger.warning(f'Multiple input files found. We will parse the first read file.')
self.inp_parser.mainfile = input_files[0]
Expand Down Expand Up @@ -1464,6 +1477,14 @@ def parse_calculations(calculations):
sec_system = self.parse_system(atomic_coord)
else:
sec_system = self.parse_system(frame)
# Patch when dealing with GeometryOptimizations which have missing iteration frames
if sec_system is None and n == len(calculations) - 1:
self.logger.warning(
'Could not parse system information for the last frame. '
'We will attempt to parse the system information from (frame + 1).',
data={'frame': frame},
)
sec_system = self.parse_system(frame + 1)
if sec_system:
sec_scc.system_ref = sec_system
if self.archive.run[-1].method:
Expand Down Expand Up @@ -1502,10 +1523,14 @@ def get_pdos_files(n_optimization_steps: int):
if optimization_steps is not None:
n_optimization_steps = optimization_steps[-1].step
if n_optimization_steps != len(optimization_steps):
self.logger.warning(f'The length of optimization steps sections in '
f'the *.out file, {len(optimization_steps)} does '
f'not coincide with the last parsed optimization step '
f'number, {n_optimization_steps}.')
self.logger.warning(
'The length of optimization steps sections in the *.out file, does '
'not coincide with the last parsed optimization step number.',
data = {
'length optimization_steps': len(optimization_steps),
'last optimization step': n_optimization_steps,
},
)
pdos_files = get_pdos_files(n_optimization_steps)
self.parse_dos(sec_run.calculation[-1], pdos_files)
elif (molecular_dynamics := quickstep.get('molecular_dynamics')) is not None:
Expand Down

0 comments on commit ec71a8c

Please sign in to comment.