Skip to content

Commit bfee8f3

Browse files
authored
Merge pull request #54 from bio-phys/fix-filename
Fix filename bug
2 parents f88f074 + 9ecfd9f commit bfee8f3

File tree

7 files changed

+47
-23
lines changed

7 files changed

+47
-23
lines changed

changelog/54.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed edge-case in input filename parsing.

mdbenchmark/generate.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ def generate(name, gpu, module, host, min_nodes, max_nodes, time, list_hosts):
113113
directory += '_gpu'
114114
gpu_string = ' with GPUs'
115115

116-
# Check and append the correct extensions for a given module.
117-
# If not files exist as expected, we stop here.
118-
engine_input = engine.check_file_extension(name)
116+
# Check if all needed files exist. Throw an error if they do not.
117+
engine.check_input_file_exists(name)
119118

120119
console.info('Creating benchmark system for {}.', m + gpu_string)
121120
number_of_benchmarks = (len(module) * (max_nodes + 1 - min_nodes))
@@ -126,8 +125,15 @@ def generate(name, gpu, module, host, min_nodes, max_nodes, time, list_hosts):
126125

127126
top = dtr.Tree(directory)
128127
for n in range(min_nodes, max_nodes + 1):
129-
engine.write_bench(top, tmpl, n, gpu, m, engine_input, name, host,
130-
time)
128+
engine.write_bench(
129+
top=top,
130+
tmpl=tmpl,
131+
nodes=n,
132+
gpu=gpu,
133+
module=m,
134+
name=name,
135+
host=host,
136+
time=time)
131137

132138
# Provide some output for the user
133139
console.info('Finished generating all benchmarks.\n'

mdbenchmark/mdengines/gromacs.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def analyze_run(sim):
105105
sim.categories['gpu'], sim.categories['host'], ncores)
106106

107107

108-
def write_bench(top, tmpl, nodes, gpu, module, tpr, name, host, time):
109-
""" Writes a single gromacs benchmark file and the respective sim object
108+
def write_bench(top, tmpl, nodes, gpu, module, name, host, time):
109+
"""Generates a single job file for GROMACS and the respective Sim object.
110110
"""
111111
sim = mds.Sim(
112112
top['{}/'.format(nodes)],
@@ -120,7 +120,12 @@ def write_bench(top, tmpl, nodes, gpu, module, tpr, name, host, time):
120120
'started': False
121121
})
122122

123-
copyfile(tpr, sim[tpr].relpath)
123+
full_filename = name + '.tpr'
124+
if name.endswith('.tpr'):
125+
full_filename = name
126+
name = name[:-4]
127+
128+
copyfile(full_filename, sim[full_filename].relpath)
124129
# Add some time buffer to the requested time. Otherwise the queuing system
125130
# kills the jobs before GROMACS can finish
126131
formatted_time = '{:02d}:{:02d}:00'.format(*divmod(time + 5, 60))
@@ -140,22 +145,20 @@ def write_bench(top, tmpl, nodes, gpu, module, tpr, name, host, time):
140145
fh.write(script)
141146

142147

143-
def check_file_extension(name):
144-
""" check and append the correct file extensions for the given module.
148+
def check_input_file_exists(name):
149+
"""Check if the TPR file exists.
145150
"""
146-
# Check that the .tpr file exists.
147-
fn, ext = os.path.splitext(name)
148-
149-
if ext == '':
150-
ext = '.tpr'
151+
fn = name
152+
if fn.endswith('.tpr'):
153+
fn = name[:-4]
151154

152-
tpr = fn + ext
155+
tpr = fn + '.tpr'
153156
if not os.path.exists(tpr):
154157
console.error(
155158
"File {} does not exist, but is needed for GROMACS benchmarks.",
156159
tpr)
157160

158-
return tpr
161+
return
159162

160163

161164
def cleanup_before_restart(sim):

mdbenchmark/mdengines/namd.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ def analyze_run(sim):
9494
sim.categories['host'], ncores)
9595

9696

97-
def write_bench(top, tmpl, nodes, gpu, module, tpr, name, host, time):
97+
def write_bench(top, tmpl, nodes, gpu, module, name, host, time):
9898
""" Writes a single namd benchmark file and the respective sim object
9999
"""
100+
# Strip the file extension, if we were given one.
101+
# This makes the usage of `mdbenchmark generate` equivalent between NAMD and GROMACS.
102+
if name.endswith('.namd'):
103+
name = name[:-5]
100104
sim = mds.Sim(
101105
top['{}/'.format(nodes)],
102106
categories={
@@ -162,18 +166,21 @@ def analyze_namd_file(fh):
162166
console.error('No absolute path detected in NAMD file!')
163167

164168

165-
def check_file_extension(name):
169+
def check_input_file_exists(name):
166170
"""Check and append the correct file extensions for the NAMD module.
167171
"""
168172
# Check whether the needed files are there.
169173
for extension in ['namd', 'psf', 'pdb']:
174+
if name.endswith('.{}'.format(extension)):
175+
name = name[:-1 + len(extension)]
176+
170177
fn = '{}.{}'.format(name, extension)
171178
if not os.path.exists(fn):
172179
console.error(
173180
"File {} does not exist, but is needed for NAMD benchmarks.",
174181
fn)
175182

176-
return name
183+
return
177184

178185

179186
def cleanup_before_restart(sim):

mdbenchmark/tests/mdengines/test_gromacs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_cli():
118118

119119
@test_cli.command()
120120
def test():
121-
gromacs.check_file_extension('md')
121+
gromacs.check_input_file_exists('md')
122122

123123
output = 'ERROR File md.tpr does not exist, but is needed for GROMACS benchmarks.\n'
124124
result = cli_runner.invoke(test_cli, ['test'])

mdbenchmark/tests/mdengines/test_namd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_cli():
7979

8080
@test_cli.command()
8181
def test():
82-
namd.check_file_extension('md')
82+
namd.check_input_file_exists('md')
8383

8484
output = 'ERROR File md.namd does not exist, but is needed for NAMD benchmarks.\n'
8585
result = cli_runner.invoke(test_cli, ['test'])

mdbenchmark/tests/test_generate.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import os
2121

2222
import pytest
23-
2423
from mdbenchmark import cli
2524
from mdbenchmark.ext.click_test import cli_runner
2625

@@ -72,6 +71,14 @@ def test_generate(cli_runner, tmpdir, tpr_file):
7271
assert os.path.exists(
7372
'draco_gromacs/2016_gpu/{}/bench.job'.format(i))
7473

74+
# Make sure we pass the correct file name to the job template. It
75+
# should not contain any file extensions for GROMACS.
76+
with open('draco_gromacs/2016_gpu/6/bench.job') as f:
77+
for line in f:
78+
if not line.startswith('srun'):
79+
continue
80+
assert line.endswith('-deffnm protein')
81+
7582

7683
def test_generate_console_messages(cli_runner, tmpdir):
7784
"""Test that the CLI for generate prints all error messages as expected."""

0 commit comments

Comments
 (0)