def runStructures(self, structures, path, calcStates=True): '''Run simulations for all structures in the given structure list with the base path "path". This method dispatches all processes and returns the user has to wait for processes to finish before accessing results. Stores started processes in self.processes ''' local = Local() for ss in structures: spath = Path.joinpath(path, str(ss.dirname)) su.mkdir(spath) if calcStates: self.initdir(ss, spath) #proc = su.dispatch(self.progWS, "",spath) proc = self.pltfm.submitjob(self.progWS, [], spath, 1, "00:10") self.processes.append(proc) if calcStates: dbg.debug("Starting calcWS program.....\n", dbg.verb_modes["verbose"], self) dbg.flush() # TODO do not wait for all processes-start each strucrure when ready! self.waitforproc(1) dbg.debug("Starting lqcl.....\n", dbg.verb_modes["verbose"], self) dbg.flush() #del processes #processes = [] for ss in structures: spath = Path.joinpath(path, str(ss.dirname)) proc = self.pltfm.submitjob(self.proglqcl, [], Path.joinpath(spath, self.datpath)) self.processes.append(proc) return self.processes
def dispatch(prog, args, dirpath=None, infile=None, outfile=None, errfile=None): '''Dispatch program prog with arguments args. Optional parameters: dirpath: Path to working directory. infile: File with inputs to the program. Use subprocess.PIPE for input stream. outfile: File for program output. Use subprocess.PIPE for output stream. errfile: File for program output. Use subprocess.PIPE for output stream. Returns the Popen() process. Note that if all outputs are piped, the system can cause the program to crash since the pipes might not be properly closed between calls. ''' if infile is None: infile = tmp() if outfile is None: outfile = tmp() if errfile is None: errfile = tmp() if args == []: progargs = prog else: progargs = [prog] for a in args: progargs.append(a) if dirpath is None: dirpath = "./" dbg.debug( "<<<< Dispatching program: " + str(progargs) + " from " + dirpath, dbg.verb_modes["chatty"]) process = Popen(progargs, cwd=dirpath, close_fds=True, stdout=outfile, stderr=errfile, stdin=infile, universal_newlines=True) dbg.debug(" with pid=" + str(process.pid) + " >>>>\n", dbg.verb_modes["chatty"]) dbg.flush() return process
def jobstatus(self, proc): for info in self.procinfo: if info[0] == proc.pid: jobname = info[1] p = su.dispatch("bjobs", ["-J", jobname], errfile=subprocess.PIPE) out = p.communicate() if ("not found" in out[1]) and (proc.poll() != None): dbg.debug("process ended!") return False else: return True
def run_sewself(self, structure, spath, inputs=None): '''Run sewself for given structure and structure path (spath) inputs (Optional): List of sting of commands to be executed (a-w) ''' commands = "" if inputs is None: inputs = self.commands for c in inputs: if c[0] == 'a': commands += "a\n" + c[1] + "\n" + c[2] + \ "\n" + c[3] + "\n" elif c[0] == 'b': commands += "b\n" commands += str(self.numpar.get('Tlattice')) + "\n" commands += str(self.numpar.get('Te')) + "\n" elif c[0] == 'c': commands += "c\n" elif c[0] == 'd': commands += "d\n" elif c[0] == 'e': commands += "e\n" # compute self-consitent potential commands += str(self.numpar.get('Tlattice')) + "\n" commands += str(self.numpar.get('Te')) + "\n" elif c[0] == 'f': commands += "f\n" elif c[0] == 'g': commands += "g\n" commands += str(self.numpar.get('Te')) + "\n" else: print("WARNING: Option " + c[0] + \ " not implemented in Isewself!") commands += "q\n^C\n" dbg.debug("Running sewself:", callclass=self) dbg.flush() proc = su.dispatch(self.sewself, [], spath, infile=subprocess.PIPE, outfile=subprocess.PIPE, errfile=subprocess.PIPE) dbg.debug("Communicating sewself: \n" + commands, callclass=self) dbg.flush() out, err = proc.communicate(commands) structure.output = out
def minimize(self, model, sgenerator, pathwd, pathresults=None, seq=None): if pathresults is None: pathresults = pathwd dbg.debug("Starting minimization\n", dbg.verb_modes["verbose"], self) niter = 0 while self.converged == 0: niter += 1 dbg.debug("Iteration " + str(niter) + "\n", dbg.verb_modes["verbose"], self) newx = self.nextstep() sgenerator.gen_struct_from_hilbert_curve(newx) if seq is not None: t = model.runStructSeq(sgenerator.structures[-len(newx):], pathwd, seq) for tt in t: tt.join() else: model.runStructures(sgenerator.structures[-len(newx):], pathwd) model.waitforproc(0.1) newy = [] xi = 0 model.gatherResults(sgenerator.structures[-len(newx):], pathwd, pathresults=pathresults) for ss in sgenerator.structures[-len(newx):]: try: val = -float(model.getMerit(ss, pathwd)) newy.append(val) except (ValueError): del newx[xi] xi -= 1 xi += 1 self.addpoints(newx, newy) self.writeresults(pathresults, "hilbert.log") dbg.debug( "Minimization finished with convergence: " + str(self.converged) + "\n", dbg.verb_modes["verbose"], self) dbg.flush() return self.converged
def minimize(self, model, sgenerator, pathwd, pathresults=None, seq=None): if pathresults is None: pathresults = pathwd dbg.debug("Starting minimization\n", dbg.verb_modes["verbose"], self) niter = 0 while self.converged == 0: niter += 1 dbg.debug("Iteration " + str(niter) + "\n", dbg.verb_modes["verbose"], self) newx = self.nextstep() sgenerator.generateStructures(newx) if seq is not None: t = model.runStructSeq(sgenerator.structures[-len(newx):], pathwd, seq) for tt in t: tt.join() else: model.runStructures(sgenerator.structures[-len(newx):], pathwd) model.waitforproc(60) newy = [] newx_success = [] xi = 0 model.gatherResults(sgenerator.structures[-len(newx):], pathwd, pathresults=pathresults) self.addEvaldPoints(model, sgenerator, pathwd, sgenerator.structures[-len(newx):]) #self.writeresults(pathresults, "mdgauss.log") dbg.debug( "Minimization finished with convergence: " + str(self.converged) + "\n", dbg.verb_modes["verbose"], self) dbg.flush() return self.converged
import aftershoq.utils.debug as dbg from aftershoq.interface import Isewself, Inegf from aftershoq.numerics import Paraopt, Gaussopt from matplotlib import pyplot as pl import aftershoq.utils.const as const if __name__ == '__main__': # the working directory: path = "/Users/martin/Projects/Optimization/ASQW/ASQWchi2/Mehrans/v3/" #path = os.getcwd()+"/"+path su.mkdir(path) # Setup debugger: dbg.open(dbg.verb_modes['verbose'], path + "/debug.log") dbg.debug("Debug file \n\n") dbg.flush() sep = '\n----------------------------------------------\n' print('--- Welcome to the "sewself" demonstration of ---\n') print(' "AFTERSHOQ" \n\n') print(' Written by Martin Franckie 2018.') print(' Please give credit where credit') print(' is due\n') print(sep) print('Creating semiconductor materials and alloys:\n') print(' ( All CBO relative to GaAs )\n') # create materials GaAs and two AlGaAs: