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