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