Skip to content

Commit

Permalink
Fixed command line usage of xtb
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelRobidas committed Mar 20, 2022
1 parent f7e8be7 commit 0c1b73c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 35 deletions.
6 changes: 6 additions & 0 deletions ccinput/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,12 @@ class CalcType(Enum):
"pm7": "PM7",
"hf": "HF",
},
"xtb": {
"gfn2-xtb": "gfn2-xtb",
"gfn1-xtb": "gfn1-xtb",
"gfn0-xtb": "gfn0-xtb",
"gfn-ff": "gfn-ff",
},
}

SOFTWARE_BASIS_SETS = {
Expand Down
4 changes: 4 additions & 0 deletions ccinput/packages/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,7 @@ def create_input_file(self):
self.input_file = "\n".join([i.strip() for i in raw.split("\n")]).replace(
"\n\n\n", "\n\n"
)

@property
def output(self):
return self.input_file
4 changes: 4 additions & 0 deletions ccinput/packages/orca.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,7 @@ def create_input_file(self):
self.block_lines,
)
self.input_file = "\n".join([i.strip() for i in raw.split("\n")])

@property
def output(self):
return self.input_file
35 changes: 21 additions & 14 deletions ccinput/packages/xtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, calc):
self.calc = calc
self.program = ""
self.cmd_arguments = ""
self.option_file = ""
self.input_file = ""
self.specifications = ""
self.force_constant = 1.0

Expand Down Expand Up @@ -68,20 +68,20 @@ def handle_constraints_scan(self):
if len(self.calc.constraints) == 0:
return

self.option_file += "$constrain\n"
self.option_file += f"force constant={self.force_constant}\n"
self.input_file += "$constrain\n"
self.input_file += f"force constant={self.force_constant}\n"
self.has_scan = False

for cmd in self.calc.constraints:
self.option_file += cmd.to_xtb()
self.input_file += cmd.to_xtb()
if cmd.scan:
self.has_scan = True

if self.has_scan:
self.option_file += "$scan\n"
self.input_file += "$scan\n"
for counter, cmd in enumerate(self.calc.constraints):
if cmd.scan:
self.option_file += (
self.input_file += (
f"{counter+1}: {cmd.start_d}, {cmd.end_d}, {cmd.num_steps}\n"
)

Expand Down Expand Up @@ -116,23 +116,23 @@ def handle_constraints_crest(self):
num_atoms = len(self.calc.xyz.split("\n"))
input_file_name = os.path.basename(self.calc.file)

self.option_file += "$constrain\n"
self.option_file += f"force constant={self.force_constant}\n"
self.option_file += f"reference={input_file_name}\n"
self.input_file += "$constrain\n"
self.input_file += f"force constant={self.force_constant}\n"
self.input_file += f"reference={input_file_name}\n"
constr_atoms = []
for cmd in self.calc.constraints:
self.option_file += cmd.to_xtb()
self.input_file += cmd.to_xtb()
constr_atoms += cmd.ids

self.option_file += f"atoms: {self.compress_indices(constr_atoms)}\n"
self.input_file += f"atoms: {self.compress_indices(constr_atoms)}\n"

mtd_atoms = list(range(1, num_atoms))
for a in constr_atoms:
if int(a) in mtd_atoms:
mtd_atoms.remove(int(a))

self.option_file += "$metadyn\n"
self.option_file += f"atoms: {self.compress_indices(mtd_atoms)}\n"
self.input_file += "$metadyn\n"
self.input_file += f"atoms: {self.compress_indices(mtd_atoms)}\n"

def handle_specifications(self):
SPECIFICATIONS = {
Expand Down Expand Up @@ -308,4 +308,11 @@ def handle_command(self):

def create_command(self):
input_file_name = os.path.basename(self.calc.file)
self.command = f"{self.program} {input_file_name} {self.cmd_arguments}"
self.command = f"{self.program} {input_file_name} {self.cmd_arguments}".strip()

@property
def output(self):
if self.input_file == "":
return self.command
else:
return f"{self.command}\n\n---input:\n{self.input_file}"
37 changes: 37 additions & 0 deletions ccinput/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,43 @@ def test_single_file_output_change_name(self):

self.assertEqual(outputs[0], "calc_dir/sp_HF.inp")

def test_xtb_commandline(self):
cmd_line = f"xtb sp gfn2-xtb -f {self.struct('ethanol')}"

parser = get_parser()
args = parser.parse_args(shlex.split(cmd_line))

objs, outputs = get_input_from_args(args)
self.assertEqual(len(objs), 1)
self.assertEqual(len(outputs), 0)

self.assertEqual(objs[0].input_file, "")
self.assertEqual(objs[0].command, "xtb ethanol.xyz")

def test_crest_commandline(self):
cmd_line = f"xtb 'constr conf search' gfn2-xtb -f {self.struct('ethanol')} --freeze 1 3"

parser = get_parser()
args = parser.parse_args(shlex.split(cmd_line))

objs, outputs = get_input_from_args(args)
self.assertEqual(len(objs), 1)
self.assertEqual(len(outputs), 0)

INPUT_REF = """
$constrain
force constant=1.0
reference=ethanol.xyz
distance: 1, 3, auto
atoms: 1,3
$metadyn
atoms: 2,4-9"""

self.assertTrue(self.is_equivalent(INPUT_REF, objs[0].input_file))
self.assertEqual(
objs[0].command, "crest ethanol.xyz -cinp input -rthr 0.6 -ewin 6"
)

@patch("ccinput.utilities.warn")
def test_warn_unknown(self, warn_fn):
warn_fn.side_effect = self.get_warn
Expand Down
39 changes: 23 additions & 16 deletions ccinput/tests/test_xtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_sp_basic(self):
REF = "xtb ethanol.xyz"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_sp_charge(self):
params = {
Expand All @@ -30,6 +31,7 @@ def test_sp_charge(self):
REF = "xtb Cl.xyz --chrg -1"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_sp_multiplicity(self):
params = {
Expand All @@ -44,6 +46,7 @@ def test_sp_multiplicity(self):
REF = "xtb Cl.xyz --uhf 2"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_sp_charge_multiplicity(self):
params = {
Expand All @@ -59,6 +62,7 @@ def test_sp_charge_multiplicity(self):
REF = "xtb Cl.xyz --chrg -1 --uhf 3"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_opt_charge(self):
params = {
Expand All @@ -73,6 +77,7 @@ def test_opt_charge(self):
REF = "xtb Cl.xyz --opt tight --chrg -1"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_freq_charge(self):
params = {
Expand All @@ -87,6 +92,7 @@ def test_freq_charge(self):
REF = "xtb Cl.xyz --hess --chrg -1"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_solvent(self):
params = {
Expand All @@ -103,6 +109,7 @@ def test_solvent(self):
REF = "xtb Cl.xyz --hess -g chcl3 --chrg -1"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_solvent_ALPB(self):
params = {
Expand All @@ -119,7 +126,7 @@ def test_solvent_ALPB(self):
REF = "xtb Cl.xyz --hess --alpb chcl3 --chrg -1"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.option_file)) ###
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_solvent_synonym(self):
params = {
Expand All @@ -136,7 +143,7 @@ def test_solvent_synonym(self):
REF = "xtb Cl.xyz --hess -g chcl3 --chrg -1"

self.assertTrue(self.is_equivalent(REF, xtb.command))
self.assertTrue(self.is_equivalent("", xtb.option_file))
self.assertTrue(self.is_equivalent("", xtb.input_file))

def test_solvent_invalid(self):
params = {
Expand Down Expand Up @@ -171,7 +178,7 @@ def test_scan(self):
$scan
1: 9.0, 1.4, 10
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_freeze(self):
params = {
Expand All @@ -191,7 +198,7 @@ def test_freeze(self):
force constant=1.0
distance: 1, 2, auto
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_constraint_overlap(self):
params = {
Expand All @@ -212,7 +219,7 @@ def test_constraint_overlap(self):
distance: 1, 2, auto
distance: 2, 3, auto
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_freeze_soft(self):
params = {
Expand All @@ -233,7 +240,7 @@ def test_freeze_soft(self):
force constant=0.1
distance: 1, 2, auto
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_duplicate_specifications(self):
params = {
Expand All @@ -254,7 +261,7 @@ def test_duplicate_specifications(self):
force constant=0.2
distance: 1, 2, auto
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_conformational_search(self):
params = {
Expand All @@ -269,7 +276,7 @@ def test_conformational_search(self):

self.assertTrue(self.is_equivalent(REF, xtb.command))

self.assertEqual("", xtb.option_file)
self.assertEqual("", xtb.input_file)

def test_conformational_search_specs(self):
params = {
Expand All @@ -285,7 +292,7 @@ def test_conformational_search_specs(self):

self.assertTrue(self.is_equivalent(REF, xtb.command))

self.assertEqual("", xtb.option_file)
self.assertEqual("", xtb.input_file)

def test_conformational_search_nci(self):
params = {
Expand All @@ -301,7 +308,7 @@ def test_conformational_search_nci(self):

self.assertTrue(self.is_equivalent(REF, xtb.command))

self.assertEqual("", xtb.option_file)
self.assertEqual("", xtb.input_file)

def test_constrained_conformational_search1(self):
params = {
Expand All @@ -324,7 +331,7 @@ def test_constrained_conformational_search1(self):
$metadyn
atoms: 3-9
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_constrained_conformational_search2(self):
params = {
Expand All @@ -348,7 +355,7 @@ def test_constrained_conformational_search2(self):
$metadyn
atoms: 2-3,5,7,9
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_constrained_conformational_search3(self):
params = {
Expand All @@ -371,7 +378,7 @@ def test_constrained_conformational_search3(self):
$metadyn
atoms: 1,4-9
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_constrained_conformational_search4(self):
params = {
Expand All @@ -395,7 +402,7 @@ def test_constrained_conformational_search4(self):
$metadyn
atoms: 1,4-9
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_constrained_conformational_search5(self):
params = {
Expand All @@ -420,7 +427,7 @@ def test_constrained_conformational_search5(self):
$metadyn
atoms: 1,5-9
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_constrained_conformational_search_equals(self):
params = {
Expand All @@ -444,7 +451,7 @@ def test_constrained_conformational_search_equals(self):
$metadyn
atoms: 1,4-9
"""
self.assertTrue(self.is_equivalent(INPUT, xtb.option_file))
self.assertTrue(self.is_equivalent(INPUT, xtb.input_file))

def test_invalid_specification(self):
params = {
Expand Down

0 comments on commit 0c1b73c

Please sign in to comment.