#!/usr/freeware/bin/python # """ Spring Back Wrapper Copyright 2000 - The Budd Company Technical Center Charles Stuart - stuart@budd.thyssenkrupp.com ******************************************************************************* BETA TEST VERSION - JULY 20, 2000 CONTACT AUTHOR VIA E-MAIL OR AT (248) 391 - 9197 FOR NEW VERSION ******************************************************************************* Uses ideas expressed by Nathan Wallace, Hans Nowack, Hrvoje Niksic, and Jared Stein This script implements a wrapper function for a PAMstamp spring back analysis run. It will start the analysis and monitor its output to verify that the run is converging. If a predetermined limit in the number of iterations is exceeded without convergence, the program will stop the run, modify the input data deck, and restart. All user-modifiable variables are at the beginning of the script. """ import os,sys,time,string,math # File Variables - modify to match your setup # #PAM_PATH = "./Dummy.py" #PAM = 'Dummy.py' PAM_PATH = "/scratch/stein/esi/Pam2000/v2kdp_sgio2k.x" PAM = 'v2kdp_sgi' # Global Control Variables - add salt to taste # # Iteration_threshold = number of iterations to allow before restart # PCG_Tolerance = not used currently # interval = time interval for monitoring analysis output # DEBUG = verbosity of Run_Status and terminal messages, 0 = normal, # 1 = verbose # Iteration_threshold = 200000 PCG_Tolerance = 0.0001 interval = 300 DEBUG = 1 # Function: findminPCG(Output) # Search output file for minimum PCG Tolerance # # Input: File name to search # Output: Minimum PCG found # If minimum PCG is 0, output is next smallest value def findminPCG(Output): PCG_list = [ ] data_file = open(Output, 'r') Start_position = 0 while (1): posit0 = data_file.tell() string0 = data_file.readline() line_list = string.split(string0) try: if (line_list[0] == "TOTAL-PCG-ITR" ): Start_position = data_file.tell() break except: continue data_file.seek(Start_position) while (1): string0 = data_file.readline() if (string0 == ""): break line_list = string.split(string0) try: PCG_list.append(string.atof(line_list[6])) except: break data_file.close() PCG_list.sort() if (PCG_list[0] == 0): return PCG_list[1] else: return PCG_list[0] # Function: updateInput(Input, new_PCG) # Modify input file with a new PCG tolerance # # Input: Input file name, PCG tolernace # Output: None def updateInput(Input, new_PCG): input_file = open(Input, 'r+') Start_position = 0 while (1): posit0 = input_file.tell() string0 = input_file.readline() line_list = string.split(string0) try: if (line_list[0] == "ELAST"): break except: continue input_file.seek(posit0) elast_card = input_file.readline() input_file.seek(posit0) elast_line = string.split(elast_card) String0 = string.ljust(elast_line[0],6) + string.ljust(elast_line[1],9) String1 = string.rjust(elast_line[2],5) + string.rjust(elast_line[3],5) String2 = string.rjust(elast_line[4],10) + string.rjust(elast_line[5],10) String3 = string.rjust(elast_line[6],10) + string.rjust(`new_PCG`,10) String4 = string.rjust("1",15) input_file.write(String0 + String1 + String2 + String3 + String4) input_file.close() if (DEBUG): print(String0 + String1 + String2 + String3 + String4) return # Function monitorPAM(Input, Output, pid_child, pid_analysis) # Monitor PAMstamp process output, allowing the analysis program to # be halted and restarted with new values if no sign of convergence # is seen # # Input: Input file, Output file, and process ID's of controlling programs # Output: None def monitorPAM(Input, Output, pid_child, pid_analysis): Newton_Iterations_start = 1 Start_time = time.time() printTime = time.asctime(time.localtime(Start_time)) Start_position = 0 Repeat_Flag = 0 if (DEBUG): print("Input File: " + Input) print("Output File: " + Output) status_file = open('Run_Status','w') status_file.write(`os.getpid()` + ": Spring Back Wrapper, v." + SBW_VERSION + "\n") status_file.write(`os.getpid()` + ": Monitoring - Start " + printTime + "\n") status_file.flush() while (1): data_file = open(Output, 'r') data_file.seek(Start_position) posit0 = data_file.tell() string0 = data_file.readline() while (1): posit1 = data_file.tell() string1 = data_file.readline() if ( string1 == "" ): line_list = string.split(string0) try: if ( line_list[0] == 'ERROR' ): print(" *** Error in output file ***") status_file.write("\n " + `os.getpid()` + ": *** Error Found *** \n") status_file.flush() status_file.close() return except: print("Continue - no ERROR") try: if ( line_list[0] == 'NORMAL' ): print(" Normal Termination ") printTime = time.asctime(time.localtime(time.time())) status_file.write(`os.getpid()` + printTime + ": Normal Termination \n") status_file.flush() status_file.close() return except: print("Continue - Analysis still running") Start_position = posit1 break else: string0 = string1 posit0 = posit1 try: Newton_Iterations = string.atoi(line_list[2]) Iteration_Number = string.atoi(line_list[0]) except: if (DEBUG): print("Malformed line" + string0) else: print("Working...") try: if ( Newton_Iterations == Newton_Iterations_start ): if ( Iteration_Number > Iteration_threshold ): try: print(`os.getpid()` + ": Killing Analysis ...") if (DEBUG): status_file.write(`os.getpid()` + ": Stopping analysis PID: " + `pid_analysis` + "\n") status_file.flush() print(`os.getpid()` + ": Analysis PID: " + `pid_analysis`) print(`os.getpid()` + ": Child PID: " + `pid_child`) os.kill(pid_child,1) os.kill(pid_analysis,1) except: print(`os.getpid()` + ": Trouble killing child or analysis") print(`os.getpid()` + ": Finding min PCG...") try: min_PCG = findminPCG(Output) new_PCG = (math.ceil((min_PCG * 1.10) * 1000000)) / 1000000 print(`os.getpid()` + ": New PCG: " + `new_PCG` + " - Updating...") except: print(`os.getpid()` + ": Trouble finding new PCG") try: updateInput(Input, new_PCG) printTime = time.asctime(time.localtime(time.time())) status_file.write(`os.getpid()` + ": Changing PCG to " + `new_PCG` + " at " + printTime + "\n") status_file.flush() except: print(`os.getpid()` + ": Trouble updating PCG in data file") print(`os.getpid()` + ": Forking new analysis process") Repeat_Flag = Repeat_Flag + 1 Iteration_Number = 0 pid_child = os.fork() if ( Repeat_Flag != 0 ): Output = `Repeat_Flag` + Output if pid_child: time.sleep(30) cmd = 'ps | grep ' + PAM running_PAM = string.splitfields(os.popen(cmd).readline()) pid_analysis = string.atoi(running_PAM[0]) status_file.write(`os.getpid()` + ": New Analysis PID: " + `pid_analysis` + "\n") status_file.write(`os.getpid()` + ": New Child PID: " + `pid_child` + "\n") status_file.flush() posit0 = 0 else: Cmd = PAM_PATH + " <" + Input + " >" + Output os.system(Cmd) sys.exit() else: print(`os.getpid()` + ": Iteration threshold not reached") else: printTime = time.asctime(time.localtime(time.time())) status_file.write(`os.getpid()` + ": Newton Iterations incremented at " + printTime + "\n") status_file.write(`os.getpid()` + ": Monitoring Ends ...") status_file.close() return except: printTime = time.asctime(time.localtime(time.time())) print("Newton Iteration or Threshold comparison error " + printTime) try: printTime = time.asctime(time.localtime(time.time())) status_file.write(`os.getpid()` + ": Monitoring Time " + printTime + "\n") status_file.flush() except: print(`os.getpid()` + ": Still Working...") data_file.close() Start_position = posit0 time.sleep(interval) print("Major Error - should never get here!") return # # Start of execution # SBW_VERSION = "BETA1" if (len(sys.argv) == 3): Input_File_Name = sys.argv[1] Output_File_Name = sys.argv[2] else: print(" ") Input_File_Name = raw_input("Input file name ? ") Output_File_Name = raw_input("Output file name ? ") print(" ") pid = os.fork() # Kick off Child Process if pid: # The Parent process does the monitoring by calling a function which # scans the output file to check the number of iterations run. time.sleep(30) try: cmd = 'ps | grep ' + PAM running_PAM = string.splitfields(os.popen(cmd).readline()) pid_PAM = string.atoi(running_PAM[0]) if (DEBUG): print(`os.getpid()` + ": Analysis PID: " + `pid_PAM`) except: print(`os.getpid()` + ": Couldn't get analysis PID") pid_PAM = pid monitorPAM(Input_File_Name, Output_File_Name, pid, pid_PAM) print(`os.getpid()` + ": Spring Back Wrapper exiting") sys.exit() else: Cmd = PAM_PATH + " <" + Input_File_Name + " >" + Output_File_Name os.system(Cmd) sys.exit()