ForceBalance API  1.3
Automated optimization of force fields and empirical potentials
engine.py
Go to the documentation of this file.
1 """ Engine base class from which all ForceBalance MD engines are derived. """
2 
3 import abc
4 import os
5 import subprocess
6 import shutil
7 import numpy as np
8 import time
9 from collections import OrderedDict
10 import tarfile
11 import forcebalance
12 from forcebalance.nifty import *
13 from forcebalance.finite_difference import fdwrap_G, fdwrap_H, f1d2p, f12d3p
14 from forcebalance.optimizer import Counter
15 from forcebalance.output import getLogger
16 logger = getLogger(__name__)
17 
18 class Engine(forcebalance.BaseClass):
19 
20  """
21  Base class for all engines.
22 
23  1. Introduction
24 
25  In ForceBalance an Engine represents a molecular dynamics code
26  and the calculations that may be carried out with that code.
27 
28  2. Purpose
29 
30  Previously system calls to MD software have been made by the
31  Target. Duplication of code was occurring, because different
32  Targets were carrying out the same type of calculation.
33 
34  3. Also
35 
36  Target objects should contain Engine objects, because OpenMM
37  Engine objects need to be initialized at the start of a
38  calculation.
39 
40  """
41 
42  def __init__(self, name="engine", **kwargs):
43  self.valkwd += ['mol', 'coords', 'name', 'target', 'pbc', 'FF', 'nonbonded_method', 'nonbonded_cutoff']
44  kwargs = {i:j for i,j in kwargs.items() if j is not None and i in self.valkwd}
45  super(Engine, self).__init__(kwargs)
46  self.name = name
47  if 'verbose' in kwargs:
48  self.verbose = verbose
49  else:
50  self.verbose = False
51 
52  if 'target' in kwargs:
53  self.target = kwargs['target']
54  self.root = self.target.root
55  self.srcdir = os.path.join(self.root, self.target.tgtdir)
56  self.tempdir = os.path.join(self.root, self.target.tempdir)
57  else:
58  warn_once("Running without a target, using current directory.")
59  self.root = os.getcwd()
60  self.srcdir = self.root
61  self.tempdir = self.root
62  if 'FF' in kwargs:
63  self.FF = kwargs['FF']
64  if hasattr(self,'target') and not hasattr(self,'FF'):
65  self.FF = self.target.FF
66  #============================================#
67  #| Initialization consists of three stages: |#
68  #| 1) Setting up options |#
69  #| 2) Reading the source directory |#
70  #| 3) Preparing the temp directory |#
71  #============================================#
72 
73  self.setopts(**kwargs)
74  cwd = os.getcwd()
75 
76  os.chdir(self.srcdir)
77  self.readsrc(**kwargs)
78 
79  os.chdir(self.tempdir)
80  self.prepare(**kwargs)
81  os.chdir(cwd)
82 
83  if self.verbose:
84  printcool_dictionary(OrderedDict([(i, self.__dict__[i]) for i in sorted(self.__dict__.keys())]), title="Attributes for engine %s" % self.__class__.__name__)
85  return
86 
87  def setopts(self, **kwargs):
88  return
89 
90  def readsrc(self, **kwargs):
91  return
92 
93  def prepare(self, **kwargs):
94  return
Nifty functions, intended to be imported by any module within ForceBalance.
def readsrc(self, kwargs)
Definition: engine.py:92
Optimization algorithms.
def __init__(self, name="engine", kwargs)
Definition: engine.py:44
def setopts(self, kwargs)
Definition: engine.py:89
def printcool_dictionary(Dict, title="Dictionary Keys : Values", bold=False, color=2, keywidth=25, topwidth=50, center=True, leftpad=0)
See documentation for printcool; this is a nice way to print out keys/values in a dictionary...
Definition: nifty.py:366
def prepare(self, kwargs)
Definition: engine.py:95
def warn_once(warning, warnhash=None)
Prints a warning but will only do so once in a given run.
Definition: nifty.py:1611
target
Engines can get properties from the Target that creates them.
Definition: engine.py:55
Base class for all engines.
Definition: engine.py:41