Skip to content

Commit

Permalink
tweak optimize to permit logging
Browse files Browse the repository at this point in the history
  • Loading branch information
corinwagen committed Sep 5, 2020
1 parent 075a39e commit 8105735
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cctk/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,14 +1648,16 @@ def optimize(self, inplace=True, nprocs=1):
else:
return optimized

def csearch(self, nprocs=1, constraints=[]):
def csearch(self, nprocs=1, constraints=[], logfile=None, noncovalent=False):
"""
Optimize molecule at the GFN2-xtb level of theory.
Args:
nprocs (int): number of processors to use
constraints (list): atoms numbers to freeze
noncovalent (Bool): whether or not to use non-covalent settings
logfile (str): file to write ongoing ``crest`` output to
"""
import cctk.optimize as opt
assert isinstance(nprocs, int), "nprocs must be int!"
return opt.csearch(self, nprocs=nprocs, constraints=constraints)
return opt.csearch(self, nprocs=nprocs, constraints=constraints, noncovalent=noncovalent, logfile=logfile)
15 changes: 11 additions & 4 deletions cctk/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import numpy as np
import os, tempfile, shutil, re
import os, tempfile, shutil, re, logging
import cctk
import subprocess as sp

Expand Down Expand Up @@ -78,7 +78,7 @@ def run_xtb(molecule, nprocs=1, return_energy=False):
else:
return output_mol

def csearch(molecule, constraints=None, rotamers=True, nprocs=1, noncovalent=False):
def csearch(molecule, constraints=None, rotamers=True, nprocs=1, noncovalent=False, logfile=None):
"""
Run a conformational search on a molecule using ``crest``.
Expand All @@ -88,13 +88,14 @@ def csearch(molecule, constraints=None, rotamers=True, nprocs=1, noncovalent=Fal
rotamers (bool): return all rotamers or group into distinct conformers
nprocs (int): number of processors to use
noncovalent (Bool): whether or not to use non-covalent settings
logfile (str): file to write ongoing ``crest`` output to
Returns:
cctk.ConformationalEnsemble
"""

assert isinstance(molecule, cctk.Molecule), "need a valid molecule!"
assert isinstance(nprocs, int)
assert isinstance(logfile, str)

assert installed("crest"), "crest must be installed!"

Expand All @@ -115,7 +116,13 @@ def csearch(molecule, constraints=None, rotamers=True, nprocs=1, noncovalent=Fal
result.check_returncode()

command = f"crest xtb-in.xyz --chrg {molecule.charge} --uhf {molecule.multiplicity - 1} -T {nprocs} {nci}"
result = sp.run(command, stdout=sp.PIPE, stderr=sp.PIPE, cwd=tmpdir, shell=True)

if logfile:
with open(logfile, "w") as f:
result = sp.run(command, stdout=f, stderr=f, cwd=tmpdir, shell=True)
else:
result = sp.run(command, stdout=sp.PIPE, stderr=sp.PIPE, cwd=tmpdir, shell=True)

result.check_returncode()

if rotamers:
Expand Down

0 comments on commit 8105735

Please sign in to comment.