| 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 | dir_openmd = os.path.abspath("../build/bin/openmd") | 
| 31 | elif(os.path.isfile("../bin/openmd")): | 
| 32 | dir_openmd = os.path.abspath("../bin/openmd") | 
| 33 | else: | 
| 34 | logger.error("OpenMD : %s", "openmd executable not found at the expected location. Script Will Quit...") | 
| 35 | sys.exit() | 
| 36 | forcefld_path = os.path.abspath("../forceFields") | 
| 37 | os.environ["FORCE_PARAM_PATH"] = forcefld_path | 
| 38 |  | 
| 39 | """ | 
| 40 | Function checks if the sample_file and validate_file (.md files) have the same | 
| 41 | statusTime = interval time for the stats file. | 
| 42 | @author Samuel Njoroge | 
| 43 | @param string sample_file - .md file that is being run. | 
| 44 | @param string validate_file - .md file the result is being compared to. | 
| 45 | @return boolean | 
| 46 | """ | 
| 47 | def validate_md_time(sample_file, validate_file): | 
| 48 | sample_status_time = 0 | 
| 49 | sample_sample_time = 0 | 
| 50 | sample_run_time = 0 | 
| 51 | validate_status_time = 0 | 
| 52 | validate_sample_time = 0 | 
| 53 | validate_run_time = 0 | 
| 54 | logger = logging.getLogger("tcpserver") | 
| 55 |  | 
| 56 | samplefh = open(sample_file, "r") | 
| 57 | validatefh = open(validate_file, "r") | 
| 58 |  | 
| 59 | line = samplefh.readline() | 
| 60 | while line: | 
| 61 | if "statusTime" in line: | 
| 62 | arr = line.split('=') | 
| 63 | temp = arr[1] | 
| 64 | sample_status_time = float(temp.replace(';', '')) | 
| 65 | elif "sampleTime" in line: | 
| 66 | arr = line.split('=') | 
| 67 | temp = arr[1] | 
| 68 | sample_sample_time = float(temp.replace(';', '')) | 
| 69 | elif "runTime" in line: | 
| 70 | arr = line.split('=') | 
| 71 | temp = arr[1] | 
| 72 | sample_run_time = float(temp.replace(';', '')) | 
| 73 |  | 
| 74 | line = samplefh.readline() | 
| 75 |  | 
| 76 | line = validatefh.readline() | 
| 77 | while line: | 
| 78 | if "statusTime" in line: | 
| 79 | arr = line.split('=') | 
| 80 | temp = arr[1] | 
| 81 | validate_status_time = float(temp.replace(';', '')) | 
| 82 | elif "sampleTime" in line: | 
| 83 | arr = line.split('=') | 
| 84 | temp = arr[1] | 
| 85 | validate_sample_time = float(temp.replace(';', '')) | 
| 86 | elif "runTime" in line: | 
| 87 | arr = line.split('=') | 
| 88 | temp = arr[1] | 
| 89 | validate_run_time = float(temp.replace(';', '')) | 
| 90 |  | 
| 91 | line = validatefh.readline() | 
| 92 |  | 
| 93 | if (sample_status_time > 0) or (validate_status_time > 0): | 
| 94 | if sample_status_time == validate_status_time: | 
| 95 | return True | 
| 96 |  | 
| 97 | if (sample_sample_time > 0) or (validate_sample_time > 0): | 
| 98 | if sample_sample_time == validate_sample_time: | 
| 99 | return True | 
| 100 |  | 
| 101 | if (sample_run_time > 0) or (validate_run_time > 0): | 
| 102 | if sample_run_time == validate_run_time: | 
| 103 | return True | 
| 104 |  | 
| 105 | logger.warning("MD File: %s", "Sample/Validation times do not match.") | 
| 106 | return False | 
| 107 |  | 
| 108 | """ | 
| 109 | Function checks if an .md config file and not an include file. | 
| 110 | @author Samuel Njoroge | 
| 111 | @param string file - .md file name | 
| 112 | @return boolean | 
| 113 | """ | 
| 114 | def file_is_md(filename): | 
| 115 | file_handle = open(filename) | 
| 116 | line = file_handle.readline() | 
| 117 |  | 
| 118 | while line: | 
| 119 | if "<OpenMD version=" in line: | 
| 120 | return True | 
| 121 | line = file_handle.readline() | 
| 122 |  | 
| 123 | return False | 
| 124 |  | 
| 125 | """ | 
| 126 | Function compares two numbers. | 
| 127 | @author Samuel Njoroge and (). | 
| 128 | @param float one - first number to compare. | 
| 129 | @param float two - second number to compare. | 
| 130 | @param boolean ignore_sign - if sign is a factor in the comparison. | 
| 131 | @return float diff - difference between the two numbers. | 
| 132 | """ | 
| 133 | def absDiff(one, two, ignore_sign = False): | 
| 134 | if ignore_sign: | 
| 135 | one, two = math.fabs(one), math.fabs(two) | 
| 136 | return max(one, two) - min(one, two) | 
| 137 |  | 
| 138 | """ | 
| 139 | Function compares two files. | 
| 140 | @author Samuel Njoroge and (). | 
| 141 | @param string fExpected - name of the validation file. | 
| 142 | @param string fNew - name of the file to validate. | 
| 143 | @param float epsilon - Precision of the comparison of the files. | 
| 144 | @param boolean ignore_sign - if sign will be a factor in comparing the digits. | 
| 145 | @return boolean | 
| 146 | """ | 
| 147 | def compare(fExpected, fNew, epsilon = 0.00001, ignore_sign=False): | 
| 148 | logger = logging.getLogger("tcpserver") | 
| 149 | fone = open(fExpected, 'r') | 
| 150 | ftwo = open(fNew, 'r') | 
| 151 |  | 
| 152 | diffs = 0 | 
| 153 |  | 
| 154 | i = 0 | 
| 155 | for lineone in fone: | 
| 156 | linetwo = ftwo.readline() | 
| 157 |  | 
| 158 | elementsone = lineone.split() | 
| 159 | elementstwo = linetwo.split() | 
| 160 |  | 
| 161 | i = i + 1 | 
| 162 |  | 
| 163 | lenone = len(elementsone) | 
| 164 | lentwo = len(elementstwo) | 
| 165 |  | 
| 166 | if lenone != lentwo: | 
| 167 | diffs = diffs + 1 | 
| 168 | logger.warning("Line: %d - %s", i, "no mach") | 
| 169 | return True | 
| 170 | else: | 
| 171 | for j in range(lenone): | 
| 172 | # used to ignore XYZ meta data such as # of frames and # of atoms | 
| 173 | try: | 
| 174 | feone = int(elementsone[j]) | 
| 175 | fetwo = int(elementstwo[j]) | 
| 176 | # these are ints -- skip this pair | 
| 177 | continue | 
| 178 | except ValueError: | 
| 179 | pass | 
| 180 | try: | 
| 181 | feone = float(elementsone[j]) | 
| 182 | fetwo = float(elementstwo[j]) | 
| 183 |  | 
| 184 | fediff = absDiff(feone, fetwo, ignore_sign) | 
| 185 |  | 
| 186 | if fediff > epsilon: | 
| 187 | diffs = diffs + 1 | 
| 188 | print "Line " + str(i) + " do not match in the files." | 
| 189 | return True | 
| 190 | except ValueError: | 
| 191 | pass | 
| 192 | return False#diffs == 0 | 
| 193 |  | 
| 194 | """ | 
| 195 | Function scans the directory for .md config files which will be run for | 
| 196 | testing. | 
| 197 | @author Samuel Njoroge | 
| 198 | """ | 
| 199 | def scanForMdFiles(): | 
| 200 | paths = [] | 
| 201 | variable = "sam is here" | 
| 202 | for p in os.listdir("../samples/"): | 
| 203 | paths.append(os.path.abspath("../samples/" + p)) | 
| 204 |  | 
| 205 | while len(paths): | 
| 206 | p = paths.pop() | 
| 207 | if os.path.isfile(p): | 
| 208 | fraw_list.append(p) | 
| 209 | else: | 
| 210 | for np in os.listdir(p): | 
| 211 | paths.append(os.path.abspath(p + "/" + np)) | 
| 212 |  | 
| 213 | for f in fraw_list: | 
| 214 | if (".md" in f) and (not ".svn" in f) and file_is_md(f): | 
| 215 | fmd_list.append(f) | 
| 216 | temp = os.path.basename(f) | 
| 217 | temp = temp.replace(".md",'') | 
| 218 | fmd_base_list.append(temp) | 
| 219 |  | 
| 220 | """ | 
| 221 | Function runs OpenMD on the files the compares them with the already run files (*_v.stat) | 
| 222 | @author Samuel Njoroge | 
| 223 | """ | 
| 224 | def runMdFiles(): | 
| 225 | logger = logging.getLogger("tcpserver") | 
| 226 | global dir_base, dir_openmd, dir_cwd | 
| 227 | output = [] | 
| 228 | for x in range(0, len(fmd_list)): | 
| 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") and os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.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: Files do not match.") | 
| 240 | else: | 
| 241 | logger.debug("Files Match") | 
| 242 | else: | 
| 243 | logger.warning("Stat Files: one of the files was not found: %s \n %s", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.stat") | 
| 244 |  | 
| 245 | if(os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor") and os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.eor")): | 
| 246 | #print "Renaming File: " + fmd_base_list[x] + ".stat - " + fmd_base_list[x] + "_v.stat" | 
| 247 | #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"]) | 
| 248 | logger.debug("Comparing: %s", "Comparing: " + fmd_base_list[x] + ".eor <=> " + fmd_base_list[x] + "_v.eor") | 
| 249 | if(compare(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.eor")): | 
| 250 | logger.warning("Files: Files do not match.") | 
| 251 | else: | 
| 252 | logger.debug("Files Match") | 
| 253 | else: | 
| 254 | logger.warning("Eor Files: one of the files was not found: %s \n %s", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor", os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + "_v.eor") | 
| 255 | os.chdir(dir_base) | 
| 256 |  | 
| 257 | def cleanUp(): | 
| 258 | print "Delete all files generated." | 
| 259 | for x in range(0, len(fmd_list)): | 
| 260 | if(os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor")): | 
| 261 | print "DELETE:" + os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor" | 
| 262 | os.remove(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".eor") | 
| 263 | if(os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat")): | 
| 264 | print "DELETE:" + os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat" | 
| 265 | os.remove(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".stat") | 
| 266 | if(os.path.exists(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".dump")): | 
| 267 | print "DELETE:" + os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".dump" | 
| 268 | os.remove(os.path.dirname(fmd_list[x]) + "/" + fmd_base_list[x] + ".dump") | 
| 269 |  | 
| 270 | """ | 
| 271 | Function compares .eor files. It compares sections <StuntDoubles> for position and section <FrameData> for time. | 
| 272 | @author Samuel Njoroge and Dr. Charles Vardeman | 
| 273 | @param string file_validate - name of the validation file. | 
| 274 | @param string file_validate - name of the file to validate. | 
| 275 | @param float epsilon - Precision of the comparison of the files. | 
| 276 | @param boolean ignore_sign - if sign will be a factor in comparing the digits. | 
| 277 | @return boolean | 
| 278 | """ | 
| 279 | def compareEor(file_validate, file_new, epsilon = 0.00001, ignore_sign=False): | 
| 280 | logger = logging.getLogger("tcpserver") | 
| 281 | handlerv = open(file_validate, 'r')#Validate file handler. | 
| 282 | handlern = open(file_new, 'r')#New file handler. | 
| 283 |  | 
| 284 | #Variables. | 
| 285 | indexv = indexn = 0 | 
| 286 | xv = xn = 0.0 | 
| 287 | yv = yn = 0.0 | 
| 288 | zv = zn = 0.0 | 
| 289 |  | 
| 290 | #Read first line. | 
| 291 | linev = handlerv.readline() | 
| 292 | linen = handlern.readline() | 
| 293 |  | 
| 294 | while linev: | 
| 295 | if '<StuntDoubles>' in linev: | 
| 296 | linev = handlerv.readline() | 
| 297 | linen = handlern.readline() | 
| 298 | while 2: | 
| 299 | Lv = linev.split() | 
| 300 | Ln = linen.split() | 
| 301 |  | 
| 302 | #If any of these fail, then the files do not match line by line. | 
| 303 | try: | 
| 304 | indexv = int(Lv[0]) | 
| 305 | indexn = int(Ln[0]) | 
| 306 | xv = float(Lv[2]) | 
| 307 | yv = float(Lv[3]) | 
| 308 | zv = float(Lv[4]) | 
| 309 | xn = float(Ln[2]) | 
| 310 | yn = float(Ln[3]) | 
| 311 | zn = float(Ln[4]) | 
| 312 | except: | 
| 313 | logger.warning("Format: files do not follow the same format \n '%s' \n '%s'", linev.strip(), linen.strip()) | 
| 314 | return True | 
| 315 |  | 
| 316 | if indexv != indexn: | 
| 317 | logger.warning("Indexes do not match: %d | %d", indexv, indexn) | 
| 318 |  | 
| 319 | fediff = absDiff(xv, xn, ignore_sign) | 
| 320 | if fediff > epsilon: | 
| 321 | logger.warning("Line: position x on index %d do not match", indexv) | 
| 322 | return True | 
| 323 |  | 
| 324 | fediff = absDiff(yv, yn, ignore_sign) | 
| 325 | if fediff > epsilon: | 
| 326 | logger.warning("Line: position y on index %d do not match", indexv) | 
| 327 | return True | 
| 328 |  | 
| 329 | fediff = absDiff(zv, zn, ignore_sign) | 
| 330 | if fediff > epsilon: | 
| 331 | logger.warning("Line: position z on index %d do not match", indexv) | 
| 332 | return True | 
| 333 |  | 
| 334 | linev = handlerv.readline() | 
| 335 | linen = handlern.readline() | 
| 336 |  | 
| 337 | if '</StuntDoubles>' in linev: | 
| 338 | break | 
| 339 | elif '<FrameData>' in linev: | 
| 340 |  | 
| 341 | linev = handlerv.readline() | 
| 342 | linen = handlern.readline() | 
| 343 |  | 
| 344 | while 1: | 
| 345 | if 'Time' in linev: | 
| 346 | Ltv = linev.split(':') | 
| 347 | Ltn = linen.split(':') | 
| 348 |  | 
| 349 | if int(Ltv[1]) != int(Ltn[1]): | 
| 350 | logger.warning("Time: FrameData time does not match.") | 
| 351 | return True | 
| 352 | elif '</FrameData>' in linev: | 
| 353 | break | 
| 354 | linev = handlerv.readline() | 
| 355 | linen = handlern.readline() | 
| 356 |  | 
| 357 | linev = handlerv.readline() | 
| 358 | linen = handlern.readline() | 
| 359 | return False | 
| 360 |  |