def test_remote(self): logging.debug('') logging.debug('test_remote') init_cluster(allow_shell=True) dummy = 'dummy_output' if os.path.exists(dummy): os.remove(dummy) extcode = set_as_top(ExternalCode()) extcode.timeout = 5 extcode.command = ['python', 'sleep.py', '1', dummy] extcode.env_vars = {'SLEEP_DATA': 'Hello world!'} extcode.external_files.extend( (FileMetadata(path='sleep.py', input=True), FileMetadata(path=dummy, output=True))) extcode.resources = {'n_cpus': 1} extcode.run() self.assertEqual(extcode.return_code, 0) self.assertEqual(extcode.timed_out, False) self.assertEqual(os.path.exists(dummy), True) try: with open(dummy, 'r') as inp: data = inp.readline().rstrip() self.assertEqual(data, extcode.env_vars['SLEEP_DATA']) finally: os.remove(dummy)
def __init__(self, *args, **kwargs): # ---------------------------------------------- # --- Constructor for the Geometry Component --- # ---------------------------------------------- super(GeometryComp, self).__init__(*args, **kwargs) # -------------------------------------- # --- External Code Public Variables --- # -------------------------------------- self.stdout = Path('OpenMDAO') + r"\inj_loc.txt" self.stderr = Path('OpenMDAO') + r"\stderr.log" self.external_files = [ FileMetadata(path=self.stdout), FileMetadata(path=self.stderr) ] try: os.remove(Path('STEP') + '\Config' + self.config + '.step') except OSError: pass self.force_execute = True venturi_throat_dia = 0.55 # --- inch sector_angle = 12.0 # --- deg inner_dia = 15.5 # --- inch outer_dia = 22.0 # --- inch self.sector_area = (pi * (outer_dia / 2)**2 - pi * (inner_dia / 2)**2) * sector_angle / 360.0 self.venturi_exit_area = pi * (0.85 / 2)**2 * 4 + pi * (1.15 / 2)**2 self.venturi_throat_area = pi * (venturi_throat_dia / 2)**2 * 5 self.injector_dia = 0.85
def test_normal(self): logging.debug('') logging.debug('test_normal') dummy = 'dummy_output' if os.path.exists(dummy): os.remove(dummy) extcode = set_as_top(ExternalCode()) extcode.timeout = 5 extcode.command = ['python', 'sleep.py', '1', dummy] extcode.env_vars = {'SLEEP_DATA': 'Hello world!'} extcode.external_files.extend( (FileMetadata(path='sleep.py', input=True), FileMetadata(path=dummy, output=True))) extcode.run() self.assertEqual(extcode.return_code, 0) self.assertEqual(extcode.timed_out, False) self.assertEqual(os.path.exists(dummy), True) with open(dummy, 'r') as inp: data = inp.readline().rstrip() self.assertEqual(data, extcode.env_vars['SLEEP_DATA']) # Now show that existing outputs are removed before execution. extcode.command = ['python', 'sleep.py', '1'] extcode.run() msg = "[Errno 2] No such file or directory: 'dummy_output'" assert_raises(self, "open(dummy, 'r')", globals(), locals(), IOError, msg)
def execute(self): ''' Executes file-wrapped component --- ''' print 'Updating Local Directory Structure ...' shutil.rmtree(Path('Patran') + '\Config' + self.config, True) os.mkdir(Path('Patran') + '\Config' + self.config) shutil.copy2( Path('STEP') + '\Config' + self.config + '.step', Path('OpenMDAO')) shutil.copy2(Path('Scripts') + '/run_cubit.py', Path('OpenMDAO')) print 'Generating Mesh Remotely for Configuration ' + self.config + ' ...' self.command = [ 'python', 'run_cubit.py', self.config, str(self.pilot_recession), str(self.venturi_angle), str(self.vane_height) ] # ------------------------------ # --- Job Submission Command --- # ------------------------------ print 'Starting Job on NAS ...' print Path('STEP') + '\Config' + self.config + '.step' self.external_files = [ FileMetadata('run_cubit.py', input=True), FileMetadata('Config' + self.config + '.step', input=True), FileMetadata('patran.out', output=True, binary=True), FileMetadata('Config' + self.config + '.cub', output=True, binary=True) ] self.resources = {'localhost': False} print 'Job Complete ...' # ----------------------------- # --- Execute the component --- # ----------------------------- super(MeshComp, self).execute() # --- Copy files to local directories shutil.copy2( Path('OpenMDAO') + '/patran.out', Path('Patran') + '\Config' + self.config) shutil.copy2( Path('OpenMDAO') + '/Config' + self.config + '.cub', Path('Cubit'))
def __init__(self): super(WrapAbaqus_stent, self).__init__() self.input_file = 'ark_stent.py' self.output_file = 'output.txt' self.command = [ 'C:/Users/tbrosse/.openmdao/gui/projects/Stent/launcher.bat' ] self.external_files = [ FileMetadata(path=self.input_file, input=True), FileMetadata(path=self.output_file), ]
def __init__(self): super(Unique, self).__init__(directory=DIRECTORY) self.create_instance_dir = True self.external_files = [ FileMetadata(path='sleep.py', input=True, constant=True), ] self.command = ['python', 'sleep.py', '1']
def configure(self): """ If specified, read XML file. """ super(VSP, self).configure() self.external_files = [] if self.xml_filename: self.read_input(self.xml_filename) self.external_files.append( FileMetadata(path=self.xml_filename, input=True, desc='VSP XML file.'))
def __init__(self): super(MyCode, self).__init__() self.appname = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'genoutfile.py') self.external_files = [ FileMetadata(path="myfile.txt", input=True, binary=False, desc='a test file') ] self.command = ["%s" % self.appname]
def __init__(self, owExe, scriptFile=None, debug=False): """ Constructor for the OWwrapped component """ super(OWwrapped, self).__init__() # External Code public variables self.input_file = 'myinput.txt' self.output_file = 'myoutput.txt' self.stderr = 'myerror.log' self.debug = debug self.script_file = 'script_file.xml' # replace with actual file name if scriptFile is not None: self.script_file = scriptFile # external_files : member of Component class = list of FileMetadata objects self.external_files = [ #FileMetadata(path=self.input_file, input=True), FileMetadata(path=self.output_file), FileMetadata(path=self.stderr), ] self.command = [owExe, self.script_file]
def test_save_load(self): logging.debug('') logging.debug('test_save_load') extcode = set_as_top(ExternalCode()) extcode.name = 'ExternalCode' extcode.timeout = 5 extcode.command = ['python', 'sleep.py', '1'] extcode.external_files = [ FileMetadata(path='sleep.py', input=True, constant=True), ] # Exercise check_save_load(). retcode = check_save_load(extcode) self.assertEqual(retcode, 0)
def add_external_files(self): self.external_files.extend([ FileMetadata(path=os.path.join(self.basedir, self.hawc2bin), input=True, binary=True, desc='DLL files.'), FileMetadata(path=os.path.join(self.basedir, self.wp_datafile), input=True, binary=True, desc='wpdata file.'), FileMetadata(path=os.path.join(self.basedir, 'resourse.dat'), input=True, binary=True, desc='resourse.dat file.'), FileMetadata(path=os.path.join(self.basedir, '*.DLL'), input=True, binary=True, desc='DLL files.'), FileMetadata(path=os.path.join(self.basedir, '*.dll'), input=True, binary=True, desc='dll files.'), FileMetadata(path=os.path.join(self.data_directory, '*'), input=True, desc='data files.'), FileMetadata(path=os.path.join(self.control_directory, '*'), input=True, binary=True, desc='controller DLLs.'), FileMetadata(path=os.path.join(self.turb_directory, '*'), input=True, binary=True, desc='turbulence files.')])
def test_remote(self): logging.debug('') logging.debug('test_remote') init_cluster(allow_shell=True) sleeper = set_as_top(Sleeper()) sleeper.env_filename = ENV_FILE sleeper.env_vars = {'SLEEP_DATA': 'Hello world!'} sleeper.external_files.append( FileMetadata(path=ENV_FILE, output=True)) sleeper.infile = FileRef(INP_FILE, sleeper, input=True) sleeper.timeout = 5 sleeper.resources = {'min_cpus': 1} sleeper.run() self.assertEqual(sleeper.return_code, 0) self.assertEqual(sleeper.timed_out, False) self.assertEqual(os.path.exists(ENV_FILE), True) with open(ENV_FILE, 'r') as inp: data = inp.readline().rstrip() self.assertEqual(data, sleeper.env_vars['SLEEP_DATA']) with sleeper.outfile.open() as inp: result = inp.read() self.assertEqual(result, INP_DATA) # Null input file. sleeper.stdin = '' assert_raises(self, 'sleeper.run()', globals(), locals(), ValueError, ": Remote execution requires stdin of DEV_NULL or" " filename, got ''") # Specified stdin, stdout, and join stderr. with open('sleep.in', 'w') as out: out.write('froboz is a pig!\n') sleeper.stdin = 'sleep.in' sleeper.stdout = 'sleep.out' sleeper.stderr = ExternalCode.STDOUT sleeper.run() # Null stderr. sleeper.stderr = None sleeper.run()
def __init__(self): super(DREA,self).__init__() self.command = ['drea'] self.ist = None self.ifab = None self.external_files = [ FileMetadata(path='control.in', input=True), FileMetadata(path='flocond.in', input=True), FileMetadata(path='expnd.in', input=True), FileMetadata(path='zrdmix.in', input=True), FileMetadata(path='hwall.in', input=True), FileMetadata(path='ejectd.out'), FileMetadata(path=self.stderr), ]
def __init__(self, geom, atm): super(runFASText, self).__init__() self.rawfast = runFAST(geom, atm) # self.rawfast.setFastFile("MyFastInputTemplate.fst") # still needs to live in "InputFilesToWrite/" self.rawfast.model_path = 'ModelFiles/' self.rawfast.template_path = "InputFilesToWrite/" self.rawfast.ptfm_file = "NREL5MW_Platform.ptfm" self.rawfast.wamit_path = "ModelFiles/WAMIT/spar" self.rawfast.setFastFile( "NREL5MW_Monopile_Floating.fst" ) # still needs to live in "InputFilesToWrite/" # self.rawfast.setFastFile("NREL5MW_Monopile_Floating.v7.01.fst") # still needs to live in "InputFilesToWrite/" self.rawfast.setOutputs(self.fast_outputs) # self.stderr = "error.out" # self.stdout = "mystdout" self.basedir = os.path.join(os.getcwd(), "all_runs") try: os.mkdir(self.basedir) except: print "failed to make base dir all_runs; or it exists" self.copyback_files = True self.appname = self.rawfast.getBin() # template_dir = self.rawfast.getTemplateDir() # noiset = os.path.join(template_dir, self.rawfast.noise_template) # fastt = os.path.join(template_dir, self.rawfast.template_file) noiset = os.path.join("InputFilesToWrite", "Noise.v7.02.ipt") adt = os.path.join("InputFilesToWrite", "NREL5MW.ad") bladet = os.path.join("InputFilesToWrite", "NREL5MW_Blade.dat") ptfmt = os.path.join("InputFilesToWrite", "NREL5MW_Platform.ptfm") foundationt = os.path.join("ModelFiles", "NREL5MW_Monopile_Tower_RigFnd.dat") spar1 = os.path.join("ModelFiles", os.path.join("WAMIT", "spar.1")) spar3 = os.path.join("ModelFiles", os.path.join("WAMIT", "spar.3")) sparhst = os.path.join("ModelFiles", os.path.join("WAMIT", "spar.hst")) # fastt = os.path.join("InputFilesToWrite", "NREL5MW_Monopile_Rigid.v7.02.fst") fastt = os.path.join("InputFilesToWrite", self.rawfast.fast_file) tst = os.path.join("InputFilesToWrite", "turbsim_template.inp") self.command = [self.appname, "test.fst"] self.external_files = [ FileMetadata(path=noiset, binary=False), FileMetadata(path=adt, binary=False), FileMetadata(path=bladet, binary=False), FileMetadata(path=ptfmt, binary=False), FileMetadata(path=spar1, binary=False), FileMetadata(path=spar3, binary=False), FileMetadata(path=sparhst, binary=False), FileMetadata(path=foundationt, binary=False), FileMetadata(path=tst, binary=False), FileMetadata(path=fastt, binary=False) ] for nm in self.rawfast.getafNames(): self.external_files.append( FileMetadata(path="%s" % nm, binary=False))
def __init__(self): super(Sleeper, self).__init__() self.directory = DIRECTORY self.external_files = [ FileMetadata(path='sleep.py', input=True, constant=True), ]
def test_normal(self): logging.debug('') logging.debug('test_normal') sleeper = set_as_top(Sleeper()) sleeper.env_filename = ENV_FILE sleeper.env_vars = {'SLEEP_DATA': 'Hello world!'} sleeper.external_files.append( FileMetadata(path=ENV_FILE, output=True)) sleeper.infile = FileRef(INP_FILE, sleeper, input=True) sleeper.stderr = None sleeper.run() self.assertEqual(sleeper.return_code, 0) self.assertEqual(sleeper.timed_out, False) self.assertEqual(os.path.exists(ENV_FILE), True) with open(ENV_FILE, 'rU') as inp: data = inp.readline().rstrip() self.assertEqual(data, sleeper.env_vars['SLEEP_DATA']) with sleeper.outfile.open() as inp: result = inp.read() self.assertEqual(result, INP_DATA) # Force an error. sleeper.stderr = 'sleep.err' sleeper.delay = -1 assert_raises(self, 'sleeper.run()', globals(), locals(), RuntimeError, ': return_code = 1') sleeper.delay = 1 # Redirect stdout & stderr. sleeper.env_vars = {'SLEEP_ECHO': '1'} sleeper.stdin = 'sleep.in' sleeper.stdout = 'sleep.out' sleeper.stderr = 'sleep.err' with open('sleep.in', 'w') as out: out.write('Hello World!\n') sleeper.run() with open('sleep.out', 'r') as inp: self.assertEqual(inp.read(), 'stdin echo to stdout\n' 'Hello World!\n') with open('sleep.err', 'r') as inp: self.assertEqual(inp.read(), 'stdin echo to stderr\n' 'Hello World!\n') # Exercise check_files() errors. os.remove('input') assert_raises(self, 'sleeper.check_files(inputs=True)', globals(), locals(), RuntimeError, ": missing 'in' file 'input'") os.remove('output') assert_raises(self, 'sleeper.check_files(inputs=False)', globals(), locals(), RuntimeError, ": missing 'out' file 'output'") os.remove('sleep.in') assert_raises(self, 'sleeper.check_files(inputs=True)', globals(), locals(), RuntimeError, ": missing stdin file 'sleep.in'") os.remove('sleep.err') assert_raises(self, 'sleeper.check_files(inputs=False)', globals(), locals(), RuntimeError, ": missing stderr file 'sleep.err'") os.remove('sleep.out') assert_raises(self, 'sleeper.check_files(inputs=False)', globals(), locals(), RuntimeError, ": missing stdout file 'sleep.out'") # Show that non-existent expected files are detected. sleeper.external_files.append( FileMetadata(path='missing-input', input=True)) assert_raises(self, 'sleeper.run()', globals(), locals(), RuntimeError, ": missing input file 'missing-input'")
def __init__(self, owExe, scriptFile=None, extOpt=False, debug=False, stopOW=True, start_once=False, opt_log=False): """ Constructor for the OWwrapped component """ self.debug = debug if self.debug: sys.stderr.write('\nIn {:}.__init__()\n'.format(self.__class__)) super(OWcomp, self).__init__() # public variables self.input_file = 'myinput.txt' self.output_file = 'myoutput.txt' self.stderr = 'myerror.log' # external_files : member of Component class = list of FileMetadata objects self.external_files = [ FileMetadata(path=self.output_file), FileMetadata(path=self.stderr), ] self.stopOW = stopOW self.start_once = start_once self.replace_turbine = False self.opt_log = opt_log self.extOpt = extOpt self.resname = '' # start with empty string self.script_file = scriptFile self.scriptOK = False if scriptFile is not None: # Check script file for validity and extract some path information self.scriptOK = self.parse_scriptFile() if not self.scriptOK: raise ValueError return self.scriptDict = rwScriptXML.rdScript(self.script_file, self.debug) if self.debug: sys.stderr.write('Script File Contents:\n') for k in self.scriptDict.keys(): sys.stderr.write(' {:12s} {:}\n'.format( k, self.scriptDict[k])) # Log all optimization settings? if self.opt_log: self.olname = 'owOptLog.txt' self.olfh = open(self.olname, 'w') if self.debug: sys.stderr.write('Logging optimization params to {:}\n'.format( self.olname)) # Set the version of OpenWind that we want to use self.command = [owExe, self.script_file] # Keep the initial value of rotor diam so we can # see if it (or other turb param) has changed self.rtr_diam_init = self.rotor_diameter # ... other params .... # Try starting OpenWind here (if self.start_once is True) if self.start_once: self.proc = subprocess.Popen(self.command) self.pid = self.proc.pid if self.debug: sys.stderr.write('Started OpenWind with pid {:}\n'.format( self.pid)) sys.stderr.write(' OWComp: dummyVbl {:}\n'.format( self.dummyVbl)) if self.debug: sys.stderr.write('\nLeaving {:}.__init__()\n\n'.format( self.__class__))
def __init__(self, directory=''): """Constructor for the PdcylComp component""" super(PdcylComp, self).__init__(directory) # External Code public variables self.stdin = 'PDCYL.in' self.stdout = 'PDCYL.out' self.stderr = 'PDCYL.err' self.command = ['PDCYL'] self.external_files = [ FileMetadata(path=self.stdin, input=True), FileMetadata(path=self.stdout), FileMetadata(path=self.stderr), ] # Dictionary contains location of every numeric scalar variable fields = {} fields[8] = 'wsweep' fields[9] = 'war' fields[10] = 'wtaper' fields[11] = 'wtcroot' fields[12] = 'wtctip' fields[13] = 'warea' fields[15] = 'ps' fields[16] = 'tmgw' fields[17] = 'effw' fields[18] = 'effc' fields[19] = 'esw' fields[20] = 'fcsw' fields[21] = 'dsw' fields[22] = 'kdew' fields[23] = 'kdfw' fields[25] = 'istama' fields[27] = 'cs1' fields[28] = 'cs2' fields[29] = 'uwwg' fields[30] = 'xwloc1' fields[32] = 'claqr' fields[33] = 'ifuel' fields[35] = 'cwman' fields[36] = 'cf' fields[40] = 'itail' fields[42] = 'uwt' fields[43] = 'clrt' fields[44] = 'harea' fields[49] = 'frn' fields[50] = 'frab' fields[51] = 'bodl' fields[52] = 'bdmax' fields[54] = 'ckf' fields[55] = 'ec' fields[56] = 'kgc' fields[57] = 'kgw' fields[58] = 'kcont' fields[59] = 'kconb' fields[67] = 'ftst' fields[68] = 'ftsb' fields[69] = 'fcst' fields[70] = 'fcsb' fields[71] = 'est' fields[72] = 'esb' fields[73] = 'eft' fields[74] = 'efb' fields[75] = 'dst' fields[76] = 'dsb' fields[77] = 'dft' fields[78] = 'dfb' fields[79] = 'tmgt' fields[80] = 'tmgb' fields[81] = 'kde' fields[82] = 'kdf' fields[84] = 'clbr1' fields[85] = 'icyl' fields[90] = 'neng' fields[91] = 'nengwing' fields[92] = 'wfp' fields[93] = 'clrw1' fields[95] = 'clrw2' fields[96] = 'clrw3' fields[100] = 'deslf' fields[101] = 'ultlf' fields[102] = 'axac' fields[103] = 'cman' fields[104] = 'iload' fields[107] = 'pgt' fields[108] = 'pgb' fields[109] = 'wfbump' fields[110] = 'wfland' fields[114] = 'vsink' fields[115] = 'stroke' fields[116] = 'clrg1' fields[117] = 'clrg2' fields[118] = 'wfgr1' fields[119] = 'wfgr2' fields[120] = 'igear' fields[122] = 'gfrl' fields[123] = 'clrgw1' fields[124] = 'clrgw2' fields[129] = 'wgto' fields[130] = 'wtff' fields[131] = 'cbum' fields[132] = 'clan' fields[136] = 'ischrenk' fields[138] = 'icomnd' fields[140] = 'wgno' fields[141] = 'slfmb' fields[142] = 'wmis' fields[143] = 'wsur' fields[144] = 'wcw' fields[145] = 'wca' fields[146] = 'nwing' self._fields = fields
def read_input(self, casename=None): """ Read from ``<casename>.input`` and ``<casename>.boundata`` files. """ casename = casename or self.input.casename self.external_files = [] with self.dir_context: self.input.read(casename) self.boundata.read(casename, self.input) self.external_files.extend(( FileMetadata(path='%s.input' % casename, input=True, desc='General setup information.'), FileMetadata(path='%s.boundata' % casename, input=True, desc='Boundary condition information.'), FileMetadata(path='%s.mesh' % casename, input=True, binary=True, desc='Mesh information.'), FileMetadata(path='%s.restart.old' % casename, input=True, binary=True, desc='Current flowfield.'), FileMetadata(path='%s.converge' % casename, output=True, desc='Convergence data.'), FileMetadata(path='%s.forces' % casename, output=True, desc='Resultant forces.'), FileMetadata(path='%s.output' % casename, output=True, desc='Command output.'), FileMetadata(path='%s.log' % casename, output=True, desc='Parallel execution log.'), FileMetadata(path='%s.p3dabs' % casename, output=True, binary=True, desc='Plot3D absolute flowfield.'), FileMetadata(path='%s.p3drel' % casename, output=True, binary=True, desc='Plot3D relative flowfield.'), FileMetadata(path='%s.restart.new' % casename, output=True, binary=True, desc='Current flowfield.'), FileMetadata(path='fort.60', output=True, desc=''), ))