| 1 | 
chuckv | 
1659 | 
import sys | 
| 2 | 
  | 
  | 
import math | 
| 3 | 
  | 
  | 
import logging | 
| 4 | 
  | 
  | 
import os | 
| 5 | 
  | 
  | 
import subprocess | 
| 6 | 
  | 
  | 
 | 
| 7 | 
  | 
  | 
fraw_list = []#List of all .md files found (even the includes). | 
| 8 | 
  | 
  | 
fmd_list = []#List of all config .md files that can be run (not the includes). | 
| 9 | 
  | 
  | 
fmd_base_list = []#List of the names of the .md files that can be run. | 
| 10 | 
  | 
  | 
 | 
| 11 | 
  | 
  | 
dir_cwd = ""#Current working directory. | 
| 12 | 
  | 
  | 
dir_openmd = ""#Absolute path for openmd | 
| 13 | 
  | 
  | 
dir_base = ""#Directory where the script is run from. | 
| 14 | 
  | 
  | 
 | 
| 15 | 
  | 
  | 
""" | 
| 16 | 
  | 
  | 
Function sets up the dir_base and dir_openmd. If an openmd executable is not | 
| 17 | 
  | 
  | 
found, script exits. Function looks for the openmd in the relative location | 
| 18 | 
  | 
  | 
../build/bin but stores the openmd location as an absolute path. | 
| 19 | 
  | 
  | 
@author Samuel Njoroge | 
| 20 | 
  | 
  | 
""" | 
| 21 | 
  | 
  | 
def setupDirectories(): | 
| 22 | 
  | 
  | 
        global dir_base, dir_openmd, dir_cwd | 
| 23 | 
  | 
  | 
        dir_base = os.getcwd() | 
| 24 | 
  | 
  | 
        if(os.path.isfile("../build/bin/openmd")): | 
| 25 | 
  | 
  | 
                os.chdir("../build/bin/") | 
| 26 | 
  | 
  | 
                dir_openmd = os.getcwd() | 
| 27 | 
  | 
  | 
                os.chdir(dir_base) | 
| 28 | 
  | 
  | 
        else: | 
| 29 | 
  | 
  | 
                print "OpenMD executable not found." | 
| 30 | 
  | 
  | 
                sys.exit() | 
| 31 | 
  | 
  | 
         | 
| 32 | 
  | 
  | 
         | 
| 33 | 
  | 
  | 
""" | 
| 34 | 
  | 
  | 
Function checks if the sample_file and validate_file (.md files) have the same | 
| 35 | 
  | 
  | 
statusTime = interval time for the stats file. | 
| 36 | 
  | 
  | 
@author Samuel Njoroge | 
| 37 | 
  | 
  | 
@param string sample_file - .md file that is being run. | 
| 38 | 
  | 
  | 
@param string validate_file - .md file the result is being compared to. | 
| 39 | 
  | 
  | 
@return boolean | 
| 40 | 
  | 
  | 
""" | 
| 41 | 
  | 
  | 
def validate_md_time(sample_file, validate_file): | 
| 42 | 
  | 
  | 
  sample_status_time = 0 | 
| 43 | 
  | 
  | 
  sample_sample_time = 0 | 
| 44 | 
  | 
  | 
  sample_run_time = 0 | 
| 45 | 
  | 
  | 
  validate_status_time = 0 | 
| 46 | 
  | 
  | 
  validate_sample_time = 0 | 
| 47 | 
  | 
  | 
  validate_run_time = 0 | 
| 48 | 
  | 
  | 
   | 
| 49 | 
  | 
  | 
  samplefh = open(sample_file, "r") | 
| 50 | 
  | 
  | 
  validatefh = open(validate_file, "r") | 
| 51 | 
  | 
  | 
   | 
| 52 | 
  | 
  | 
  line = samplefh.readline() | 
| 53 | 
  | 
  | 
  while line: | 
| 54 | 
  | 
  | 
    if "statusTime" in line: | 
| 55 | 
  | 
  | 
      arr = line.split('=') | 
| 56 | 
  | 
  | 
      temp = arr[1] | 
| 57 | 
  | 
  | 
      sample_status_time = float(temp.replace(';', '')) | 
| 58 | 
  | 
  | 
    elif "sampleTime" in line: | 
| 59 | 
  | 
  | 
      arr = line.split('=') | 
| 60 | 
  | 
  | 
      temp = arr[1] | 
| 61 | 
  | 
  | 
      sample_sample_time = float(temp.replace(';', '')) | 
| 62 | 
  | 
  | 
    elif "runTime" in line: | 
| 63 | 
  | 
  | 
      arr = line.split('=') | 
| 64 | 
  | 
  | 
      temp = arr[1] | 
| 65 | 
  | 
  | 
      sample_run_time = float(temp.replace(';', '')) | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
    line = samplefh.readline() | 
| 68 | 
  | 
  | 
 | 
| 69 | 
  | 
  | 
  line = validatefh.readline() | 
| 70 | 
  | 
  | 
  while line: | 
| 71 | 
  | 
  | 
    if "statusTime" in line: | 
| 72 | 
  | 
  | 
      arr = line.split('=') | 
| 73 | 
  | 
  | 
      temp = arr[1] | 
| 74 | 
  | 
  | 
      validate_status_time = float(temp.replace(';', '')) | 
| 75 | 
  | 
  | 
    elif "sampleTime" in line: | 
| 76 | 
  | 
  | 
      arr = line.split('=') | 
| 77 | 
  | 
  | 
      temp = arr[1] | 
| 78 | 
  | 
  | 
      validate_sample_time = float(temp.replace(';', '')) | 
| 79 | 
  | 
  | 
    elif "runTime" in line: | 
| 80 | 
  | 
  | 
      arr = line.split('=') | 
| 81 | 
  | 
  | 
      temp = arr[1] | 
| 82 | 
  | 
  | 
      validate_run_time = float(temp.replace(';', '')) | 
| 83 | 
  | 
  | 
 | 
| 84 | 
  | 
  | 
    line = validatefh.readline() | 
| 85 | 
  | 
  | 
 | 
| 86 | 
  | 
  | 
  if (sample_status_time > 0) or (validate_status_time > 0): | 
| 87 | 
  | 
  | 
    if sample_status_time == validate_status_time: | 
| 88 | 
  | 
  | 
      return True | 
| 89 | 
  | 
  | 
    else: | 
| 90 | 
  | 
  | 
      return False | 
| 91 | 
  | 
  | 
 | 
| 92 | 
  | 
  | 
  if (sample_sample_time > 0) or (validate_sample_time > 0): | 
| 93 | 
  | 
  | 
    if sample_sample_time == validate_sample_time: | 
| 94 | 
  | 
  | 
      return True | 
| 95 | 
  | 
  | 
    else: | 
| 96 | 
  | 
  | 
      return False | 
| 97 | 
  | 
  | 
 | 
| 98 | 
  | 
  | 
  if (sample_run_time > 0) or (validate_run_time > 0): | 
| 99 | 
  | 
  | 
    if sample_run_time == validate_run_time: | 
| 100 | 
  | 
  | 
      return True | 
| 101 | 
  | 
  | 
    else: | 
| 102 | 
  | 
  | 
      return False | 
| 103 | 
  | 
  | 
 | 
| 104 | 
  | 
  | 
  return False | 
| 105 | 
  | 
  | 
   | 
| 106 | 
  | 
  | 
""" | 
| 107 | 
  | 
  | 
Function checks if an .md config file and not an include file. | 
| 108 | 
  | 
  | 
@author Samuel Njoroge | 
| 109 | 
  | 
  | 
@param string file - .md file name | 
| 110 | 
  | 
  | 
@return boolean | 
| 111 | 
  | 
  | 
""" | 
| 112 | 
  | 
  | 
def file_is_md(filename): | 
| 113 | 
  | 
  | 
        file_handle = open(filename) | 
| 114 | 
  | 
  | 
        line = file_handle.readline() | 
| 115 | 
  | 
  | 
         | 
| 116 | 
  | 
  | 
        while line: | 
| 117 | 
  | 
  | 
                if "<OpenMD version=" in line: | 
| 118 | 
  | 
  | 
                        return True | 
| 119 | 
  | 
  | 
                line = file_handle.readline() | 
| 120 | 
  | 
  | 
                         | 
| 121 | 
  | 
  | 
        return False | 
| 122 | 
  | 
  | 
         | 
| 123 | 
  | 
  | 
""" | 
| 124 | 
  | 
  | 
Function compares two numbers. | 
| 125 | 
  | 
  | 
@author Samuel Njoroge and (). | 
| 126 | 
  | 
  | 
@param float one - first number to compare. | 
| 127 | 
  | 
  | 
@param float two - second number to compare. | 
| 128 | 
  | 
  | 
@param boolean ignore_sign - if sign is a factor in the comparison. | 
| 129 | 
  | 
  | 
@return float diff - difference between the two numbers. | 
| 130 | 
  | 
  | 
""" | 
| 131 | 
  | 
  | 
def absDiff(one, two, ignore_sign = False): | 
| 132 | 
  | 
  | 
        if ignore_sign: | 
| 133 | 
  | 
  | 
                one, two = math.fabs(one), math.fabs(two) | 
| 134 | 
  | 
  | 
        return max(one, two) - min(one, two) | 
| 135 | 
  | 
  | 
         | 
| 136 | 
  | 
  | 
""" | 
| 137 | 
  | 
  | 
Function compares two files. | 
| 138 | 
  | 
  | 
@author Samuel Njoroge and (). | 
| 139 | 
  | 
  | 
@param string fExpected - name of the expected file. | 
| 140 | 
  | 
  | 
@param string fNew - name of the new test file. | 
| 141 | 
  | 
  | 
@param float epsilon - Precision of the comparison of the files. | 
| 142 | 
  | 
  | 
@param boolean ignore_sign - if sign will be a factor in comparing the digits. | 
| 143 | 
  | 
  | 
@return boolean | 
| 144 | 
  | 
  | 
""" | 
| 145 | 
  | 
  | 
def compare(fExpected, fNew, epsilon = 0.00001, ignore_sign=False): | 
| 146 | 
  | 
  | 
        fone = open(fExpected, 'r') | 
| 147 | 
  | 
  | 
        ftwo = open(fNew, 'r') | 
| 148 | 
  | 
  | 
 | 
| 149 | 
  | 
  | 
        diffs = 0 | 
| 150 | 
  | 
  | 
         | 
| 151 | 
  | 
  | 
        i = 0 | 
| 152 | 
  | 
  | 
        for lineone in fone: | 
| 153 | 
  | 
  | 
                linetwo = ftwo.readline() | 
| 154 | 
  | 
  | 
 | 
| 155 | 
  | 
  | 
                elementsone = lineone.split() | 
| 156 | 
  | 
  | 
                elementstwo = linetwo.split() | 
| 157 | 
  | 
  | 
 | 
| 158 | 
  | 
  | 
                i = i + 1 | 
| 159 | 
  | 
  | 
 | 
| 160 | 
  | 
  | 
                lenone = len(elementsone) | 
| 161 | 
  | 
  | 
                lentwo = len(elementstwo) | 
| 162 | 
  | 
  | 
 | 
| 163 | 
  | 
  | 
                if lenone != lentwo: | 
| 164 | 
  | 
  | 
                        diffs = diffs + 1 | 
| 165 | 
  | 
  | 
                        print "Line " + str(i) + " do not match in the files." | 
| 166 | 
  | 
  | 
                        return True | 
| 167 | 
  | 
  | 
                else: | 
| 168 | 
  | 
  | 
                        for j in range(lenone): | 
| 169 | 
  | 
  | 
                                # used to ignore XYZ meta data such as # of frames and # of atoms | 
| 170 | 
  | 
  | 
                                try: | 
| 171 | 
  | 
  | 
                                        feone = int(elementsone[j]) | 
| 172 | 
  | 
  | 
                                        fetwo = int(elementstwo[j]) | 
| 173 | 
  | 
  | 
                                        # these are ints -- skip this pair | 
| 174 | 
  | 
  | 
                                        continue | 
| 175 | 
  | 
  | 
                                except ValueError: | 
| 176 | 
  | 
  | 
                                        pass | 
| 177 | 
  | 
  | 
                                try: | 
| 178 | 
  | 
  | 
                                        feone = float(elementsone[j]) | 
| 179 | 
  | 
  | 
                                        fetwo = float(elementstwo[j]) | 
| 180 | 
  | 
  | 
 | 
| 181 | 
  | 
  | 
                                        fediff = absDiff(feone, fetwo, ignore_sign) | 
| 182 | 
  | 
  | 
 | 
| 183 | 
  | 
  | 
                                        if fediff > epsilon: | 
| 184 | 
  | 
  | 
                                                diffs = diffs + 1 | 
| 185 | 
  | 
  | 
                                                print "Line " + str(i) + " do not match in the files." | 
| 186 | 
  | 
  | 
                                                return True | 
| 187 | 
  | 
  | 
                                except ValueError: | 
| 188 | 
  | 
  | 
                                        pass | 
| 189 | 
  | 
  | 
        return False#diffs == 0 | 
| 190 | 
  | 
  | 
         | 
| 191 | 
  | 
  | 
""" | 
| 192 | 
  | 
  | 
Function scans the directory for .md config files which will be run for | 
| 193 | 
  | 
  | 
testing. | 
| 194 | 
  | 
  | 
@author Samuel Njoroge | 
| 195 | 
  | 
  | 
""" | 
| 196 | 
  | 
  | 
def scanForMdFiles(): | 
| 197 | 
  | 
  | 
        paths = [] | 
| 198 | 
  | 
  | 
        variable = "sam is here" | 
| 199 | 
  | 
  | 
        for p in os.listdir("../samples/"): | 
| 200 | 
  | 
  | 
                paths.append(os.path.abspath("../samples/" + p)) | 
| 201 | 
  | 
  | 
         | 
| 202 | 
  | 
  | 
        while len(paths): | 
| 203 | 
  | 
  | 
                p = paths.pop() | 
| 204 | 
  | 
  | 
                if os.path.isfile(p): | 
| 205 | 
  | 
  | 
                        fraw_list.append(p) | 
| 206 | 
  | 
  | 
                else: | 
| 207 | 
  | 
  | 
                        for np in os.listdir(p): | 
| 208 | 
  | 
  | 
                                paths.append(os.path.abspath(p + "/" + np)) | 
| 209 | 
  | 
  | 
                                 | 
| 210 | 
  | 
  | 
        for f in fraw_list: | 
| 211 | 
  | 
  | 
                if (".md" in f) and (not ".svn" in f) and file_is_md(f): | 
| 212 | 
  | 
  | 
                        fmd_list.append(f) | 
| 213 | 
  | 
  | 
                        temp = os.path.basename(f) | 
| 214 | 
  | 
  | 
                        temp = temp.replace(".md",'') | 
| 215 | 
  | 
  | 
                        fmd_base_list.append(temp) | 
| 216 | 
  | 
  | 
                 | 
| 217 | 
  | 
  | 
""" | 
| 218 | 
  | 
  | 
Function runs OpenMD on the files the compares them with the already run files (*_v.stat) | 
| 219 | 
  | 
  | 
@author Samuel Njoroge | 
| 220 | 
  | 
  | 
""" | 
| 221 | 
  | 
  | 
def runMdFiles(): | 
| 222 | 
  | 
  | 
        global dir_base, dir_openmd, dir_cwd | 
| 223 | 
  | 
  | 
        output = [] | 
| 224 | 
  | 
  | 
        for x in range(0, len(fmd_list)): | 
| 225 | 
  | 
  | 
                #subprocess.call(["export FORCE_PARAM_PATH=/Users/snjoroge/Documents/openmd/development/forceFields"]) | 
| 226 | 
  | 
  | 
                if "argon" in fmd_list[x]: | 
| 227 | 
  | 
  | 
                        print "Switching to Directory: " + os.path.dirname(fmd_list[x]) | 
| 228 | 
  | 
  | 
                        os.chdir(os.path.dirname(fmd_list[x])) | 
| 229 | 
  | 
  | 
                        print "Running file: " + fmd_list[x] | 
| 230 | 
  | 
  | 
                        output = subprocess.call([dir_openmd + "/openmd", fmd_list[x]]) | 
| 231 | 
  | 
  | 
                        if(os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat")): | 
| 232 | 
  | 
  | 
                                #print "Renaming File: " + fmd_base_list[x] + ".stat - " + fmd_base_list[x] + "_v.stat" | 
| 233 | 
  | 
  | 
                                #subprocess.call(["cp", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.stat"]) | 
| 234 | 
  | 
  | 
                                print "Comparing: " + fmd_base_list[x] + ".stat <=> " + fmd_base_list[x] + "_v.stat" | 
| 235 | 
  | 
  | 
                                if(compare(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.stat")): | 
| 236 | 
  | 
  | 
                                        print "Files Do not match." | 
| 237 | 
  | 
  | 
                                else: | 
| 238 | 
  | 
  | 
                                        print "Files match." | 
| 239 | 
  | 
  | 
                os.chdir(dir_base) | 
| 240 | 
  | 
  | 
 | 
| 241 | 
  | 
  | 
def cleanUp(): | 
| 242 | 
  | 
  | 
        print "delete all files generated so not to commit" | 
| 243 | 
  | 
  | 
        for x in range(0, len(fmd_list)): | 
| 244 | 
  | 
  | 
                print "DELETE:" + os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor" | 
| 245 | 
  | 
  | 
                os.remove(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor") | 
| 246 | 
  | 
  | 
                print "DELETE:" + os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat" | 
| 247 | 
  | 
  | 
                os.remove(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat") | 
| 248 | 
  | 
  | 
                print "DELETE:" + os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".dump" | 
| 249 | 
  | 
  | 
                os.remove(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".dump") | 
| 250 | 
  | 
  | 
         |