ForceBalance API  1.3
Automated optimization of force fields and empirical potentials
ForceBalance.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 """ @package ForceBalance
4 
5 Executable script for starting ForceBalance. """
6 from __future__ import print_function
7 
8 from builtins import range
9 import os, sys, re
10 import argparse
11 from forcebalance.parser import parse_inputs
12 from forcebalance.forcefield import FF
13 from forcebalance.objective import Objective
14 from forcebalance.optimizer import Optimizer
15 from forcebalance.nifty import printcool
16 import numpy
17 numpy.seterr(all='raise')
18 
19 def Run_ForceBalance(input_file, debug=False, continue_=False):
20  """ Create instances of ForceBalance components and run the optimizer.
21 
22  The triumvirate, trifecta, or trinity of components are:
23  - The force field
24  - The objective function
25  - The optimizer
26  Cipher: "All I gotta do here is pull this plug... and there you have to watch Apoc die"
27  Apoc: "TRINITY" *chunk*
28 
29  The force field is a class defined in forcefield.py.
30  The objective function is a combination of target classes and a penalty function class.
31  The optimizer is a class defined in this file.
32  """
33  try:
34 
35  options, tgt_opts = parse_inputs(input_file)
36 
37  if continue_: options['continue'] = True
38 
39  forcefield = FF(options)
40 
41  objective = Objective(options, tgt_opts, forcefield)
42 
43  optimizer = Optimizer(options, objective, forcefield)
44 
45  optimizer.Run()
46  except:
47  import traceback
48  traceback.print_exc()
49  if debug:
50  import pdb
51  pdb.post_mortem()
52 
53 def process(word, color):
54  if color == 'black':
55  Answer = word
56  elif color == 'blue':
57  Answer = "\x1b[44m" + " ".join(['' for i in range(len(word)+1)]) + "\x1b[0m"
58  elif color == 'gold':
59  Answer = "\x1b[43m" + " ".join(['' for i in range(len(word)+1)]) + "\x1b[0m"
60  return Answer
61 
62 def main():
63  printcool("Welcome to ForceBalance version 1.7.4! =D\nForce Field Optimization System\n\nAuthors:\nLee-Ping Wang\nYudong Qiu, Keri A. McKiernan\nJeffrey R. Wagner, Hyesu Jang, Simon Boothroyd\nArthur Vigil, Erik G. Brandt, Johnny Israeli", ansi="1", bold=True, minwidth=64)
64  logostr = """
65  ,'+++
66  ,++++++. .:,,.
67  :+++++'` `; `,:::::.`
68  '+++++' `'++++ `,::::,,`
69  `;+++++: ,+++++'. `.:::::,`
70  `++++++, :+++++'` .,::::,`
71  .+++++'. ;+++++' `,,:::,`
72  :+++++'` `;+++++: `,::::,.`
73  ++++;` `++++++: `.:+@@###@' `,,::::.`
74  : ,++++++. ,;'+###############. .,::,
75  :++++++` +################':` .
76  +++;` `.,,...####+.
77  ,##### +##. +++ +++
78  ,,,, ##### ###### +++ +++
79  ,::, ###' ####' :#####' +++ +++
80  ,::, :####@ ####. ,####' +++ +++
81  ,::, ###### #### +###+ +++ +++
82  ,::, ##### #### ###; +++ +++
83  ,::, :## #### ++` +++ +++
84  ,::, ####``..:;+##############+` +++ +++
85  ,::, .,:;;'++##################################` +++ +++
86  ,::, `############################++++''';;;;;;;;;;;''; +++ +++
87  ,::, ,########':,.`` #### +++ +++
88  ,::, #### +++ +++
89  ,::, #### +++ +++
90  ,::, #### +++ +++
91  ,::, #### +++ +++
92  ,::, #### +++ +++
93  ,::, #### +++ +++
94  ,,,, #### +++ +++
95  ;++, ####
96  `'+++++: ####
97  ` '+++++; #### `.,:.
98  ++++, :+++++'` #### `,:::::.
99  .'+++++, :++++++. ### `,::::,.`
100  `'+++++; .++++++, +` .,::::,`
101  ;+++++'` `++++++: .,:::,,`
102  :+++++'. `;+++++;` `.:::::,`
103  ,++++++` '++++++ `,::::,,`
104  .'+++++: ,+; `,:::::.
105  `'+++++: ,::,,.
106  ;++++. ,`
107  ,`
108 """
109  b = 'blue'
110  g = 'gold'
111  k = 'black'
112  colorlist = [[],[b],[b,g],[b,b,g],[b,b,g],[b,b,g],[b,b,g],[b,b,g],
113  [b,b,g],[b,b,k,g],[b,b,k,g],[b,k,g],[b,k],[k,k,b,b],
114  [g,k,k,b,b],[g,k,k,k,b,b],[g,k,k,k,b,b],[g,k,k,k,b,b],
115  [g,k,k,k,b,b],[g,k,k,k,b,b],[g,k,b,b],[g,k,b,b],[g,k,b,b],
116  [g,k,k,b,b],[g,k,b,b],[g,k,b,b],[g,k,b,b],[g,k,b,b],[g,k,b,b],
117  [g,k,b,b],[g,k,b,b],[b,k],[b,k],[b,b,k,g],[b,b,k,g],[b,b,k,g],
118  [b,b,k,g],[b,b,g],[b,b,g],[b,b,g],[b,b,g],[b,g],[b,g],[b],[]]
119 
120  words = [l.split() for l in logostr.split('\n')]
121  for ln, line in enumerate(logostr.split('\n')):
122  # Reconstruct the line.
123  words = line.split()
124  whites = re.findall('[ ]+',line)
125  newline = ''
126  i = 0
127 
128  if len(line) > 0 and line[0] == ' ':
129  while i < max(len(words), len(whites)):
130  try:
131  newline += whites[i]
132  except: pass
133  try:
134  newline += process(words[i], colorlist[ln][i])
135  except: pass
136  i += 1
137  elif len(line) > 0:
138  while i < max(len(words), len(whites)):
139  try:
140  newline += process(words[i], colorlist[ln][i])
141  except: pass
142  try:
143  newline += whites[i]
144  except: pass
145  i += 1
146  print(newline)
147 
148  parser = argparse.ArgumentParser(description="Force Field Optimization System")
149  parser.add_argument("-c", "--continue", action="store_true", help="Continue from a previous run")
150  parser.add_argument("-d", "--debug", action="store_true", help="Run interactive debugger on program crash")
151  parser.add_argument("input", help="Forcebalance input file")
152 
153  args = parser.parse_args()
154  continue_ = getattr(args, 'continue')
155 
156  Run_ForceBalance(args.input, debug=args.debug, continue_=continue_)
157 
158 if __name__ == "__main__":
159  main()
Nifty functions, intended to be imported by any module within ForceBalance.
Input file parser for ForceBalance jobs.
Optimization algorithms.
ForceBalance objective function.
def Run_ForceBalance(input_file, debug=False, continue_=False)
Create instances of ForceBalance components and run the optimizer.
Definition: ForceBalance.py:34
Force field module.
def parse_inputs(input_file=None)
Parse through the input file and read all user-supplied options.
Definition: parser.py:471
def printcool(text, sym="#", bold=False, color=2, ansi=None, bottom='-', minwidth=50, center=True, sym2="=")
Cool-looking printout for slick formatting of output.
Definition: nifty.py:321
def process(word, color)
Definition: ForceBalance.py:55