class FComp(Component): infile = File(iotype='in', local_path='input') outfile = File(FileRef('output'), iotype='out') outval = Int(iotype='out') def execute(self): """ Runs code and sets `outfile`. """ with open(self.infile.abspath()) as f: s = f.read().strip() val = int(s.split()[1]) self.outval = val + 1 fname = self.outfile.abspath() print "about to open", fname sys.stdout.flush() with open(fname, 'w') as f: print "writing %s %d %d to %s" % (self.name, self.outval, MPI.COMM_WORLD.rank, fname) sys.stdout.flush() f.write("%s %d %d\n" % (self.name, self.outval, MPI.COMM_WORLD.rank)) print "wrote %s %d %d to %s" % (self.name, self.outval, MPI.COMM_WORLD.rank, fname) sys.stdout.flush()
class Passthrough(Component): """ Copies input files (implicitly via local_path) to output. """ text_in = File(iotype='in', local_path='tout') text_out = File(iotype='out') def execute(self): """ Just set name of output file. """ self.text_out = 'tout'
def __init__(self, iotype, client, rpath, component): ProxyMixin.__init__(self, client, rpath) self._component = component self._path = 'AS-%s.dat' % rpath # Local filename for remote data. desc = client.get(rpath+'.description') metadata = {} if iotype == 'out': metadata['path'] = self._path File.__init__(self, default_value=None, iotype=iotype, desc=desc, **metadata)
class Passthrough(Component): """ Copies input files (implicitly via local_path) to output. """ text_in = File(iotype='in', local_path='tout', legal_types=['xyzzy', 'txt']) binary_in = File(iotype='in', local_path='bout') text_out = File(FileRef('tout'), iotype='out') binary_out = File(FileRef('bout', binary=True), iotype='out') def execute(self): """ File copies are performed implicitly. """ # We have to manually propagate 'extra_stuff' because the output # FileRef isn't copied from the input FileRef. self.binary_out.extra_stuff = self.binary_in.extra_stuff
class Model(Assembly): """ Run multiple `Unique` component instances. """ infile = File(iotype='in', local_path='input') outfile = File(FileRef('output'), iotype='out') def configure(self): self.add('a', Unique()) self.add('b', Unique()) self.driver.workflow.add(['a', 'b']) self.connect('infile', 'a.infile') self.connect('a.outfile', 'b.infile') self.connect('b.outfile', 'outfile')
def test_bad_trait(self): logging.debug('') logging.debug('test_bad_trait') try: File(42) except Exception, exc: self.assertEqual(str(exc), 'File default value must be a FileRef.')
class Sink(Component): """ Consumes files. """ text_data = Str(iotype='out') text_file = File(iotype='in') def execute(self): """ Read test data from files. """ with self.text_file.open() as inp: self.text_data = inp.read()
class Source(Component): """ Produces files. """ text_data = Str('Hello world', iotype='in') text_file = File('source.txt', iotype='out') def execute(self): """ Write test data to files. """ with open(self.text_file.path, 'w') as out: out.write(self.text_data)
class Sink(Component): """ Consumes files. """ bogus_path = Str('', iotype='in') text_data = Str(iotype='out') binary_data = List([1.0], iotype='out') text_file = File(iotype='in') binary_file = File(iotype='in') def execute(self): """ Read test data from files. """ if self.bogus_path: self.text_file.path = self.bogus_path inp = self.text_file.open() self.text_data = inp.read() inp.close() inp = self.binary_file.open() self.binary_data = cPickle.load(inp) inp.close()
class Source(Component): """ Produces files. """ write_files = Bool(True, iotype='in') text_data = Str(iotype='in') binary_data = Array(dtype='d', iotype='in') text_file = File(path='source.txt', iotype='out', content_type='txt') binary_file = File(path='source.bin', iotype='out', binary=True, extra_stuff='Hello world!') def execute(self): """ Write test data to files. """ if self.write_files: out = open(self.text_file.path, 'w') out.write(self.text_data) out.close() out = open(self.binary_file.path, 'wb') cPickle.dump(self.binary_data, out, 2) out.close()
class Sleeper(ExternalCode): """ Used to test external code functionality. """ delay = Int(1, units='s', iotype='in') env_filename = Str(iotype='in') infile = File(iotype='in', local_path='input') outfile = File(FileRef('output'), iotype='out') def __init__(self): super(Sleeper, self).__init__() self.directory = DIRECTORY self.external_files = [ FileMetadata(path='sleep.py', input=True, constant=True), ] def execute(self): """ Runs code and sets `outfile`. """ self.command = ['python', 'sleep.py', str(self.delay)] if self.env_filename: self.command.append(self.env_filename) super(Sleeper, self).execute()
class Sink(Component): """ Consumes files. """ text_data = Str(iotype='out') binary_data = Array(dtype='d', iotype='out') text_file = File(iotype='in') binary_file = File(iotype='in') executions = Int(0, iotype='in', desc='Count of Oddball instance_method() calls.') def __init__(self, *args, **kwargs): super(Sink, self).__init__(*args, **kwargs) global SINK_INIT SINK_INIT = True def execute(self): """ Read test data from files. """ with self.text_file.open() as inp: self.text_data = inp.read() with self.binary_file.open() as inp: self.binary_data = cPickle.load(inp)
def test_deriv_ignore_nondiff_groups(self): # This test makes sure that we don't group comps in a nondiff block # if we have 1 differentiable connection and 1 non-differentiable one # with deriv_ignore. top = set_as_top(Assembly()) top.add('src', MyCompDerivs()) top.add('target', MyCompDerivs()) top.src.add('mesh_file', File(iotype='out', deriv_ignore=True)) top.target.add('mesh_file', File(iotype='in', deriv_ignore=True)) top.connect('src.mesh_file', 'target.mesh_file') top.connect('src.y', 'target.x1') top.driver.workflow.add(['src', 'target']) top.run() J = top.driver.calc_gradient(inputs=['src.x1'], outputs=['target.y']) assert_rel_error(self, J[0, 0], 64.0, .001) systems = top._system.dump(stream=None) self.assertTrue('FD_' not in systems)
def test_unsupported_traits(self): my_comp = VarComponent() my_comp.add('unsupported', File(iotype='in')) sb = Namelist(my_comp) sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("unsupported") try: sb.generate() except RuntimeError, err: self.assertEqual(str(err), "Error generating input file. Don't" + \ " know how to handle data in variable" + \ " unsupported in group Test.")
class FComp2(Component): inval = Int(iotype='in') outfile = File(FileRef('output'), iotype='out') outval = Int(iotype='out') def execute(self): self.outval = self.inval + 1 fname = self.outfile.abspath() print "about to open", fname sys.stdout.flush() with open(fname, 'w') as f: print "writing %s %d %d to %s" % (self.name, self.outval, MPI.COMM_WORLD.rank, fname) sys.stdout.flush() f.write("%s %d %d\n" % (self.name, self.outval, MPI.COMM_WORLD.rank)) print "wrote %s %d %d to %s" % (self.name, self.outval, MPI.COMM_WORLD.rank, fname) sys.stdout.flush()
def __init__(self): super(DumbVT3, self).__init__() self.add('a', Float(1., units='ft')) self.add('b', Float(12., units='inch')) self.add('data', File())
class DumbVT(VariableTree): v1 = Float(1., desc='vv1') v2 = Float(2., desc='vv2') data = File() vt2 = VarTree(DumbVT2())
class BadVT2(VariableTree): x = Float(-1.) y = Float(-2.) data = File() vt3 = DumbVT3()
class DumbVT2(VariableTree): x = Float(-1.) y = Float(-2.) data = File() vt3 = VarTree(DumbVT3())
def __init__(self): super(DumbVT, self).__init__() self.add('vt2', DumbVT2()) self.add('v1', Float(1.)) self.add('v2', Float(2.)) self.add('data', File())
class DumbVT3arr(VariableTree): a = Float(1., units='ft') b = Float(12., units='inch') arr = Array([1, 2, 3, 4, 5]) data = File()
else: self.fail('Expected ValueError') def test_bad_trait(self): logging.debug('') logging.debug('test_bad_trait') try: File(42) except Exception, exc: self.assertEqual(str(exc), 'File default value must be a FileRef.') else: self.fail('Expected Exception') try: File(iotype='out') except Exception, exc: self.assertEqual(str(exc), "Output File must have 'path' defined.") else: self.fail('Expected Exception') try: File(iotype='out', path='xyzzy', legal_types=42) except Exception, exc: self.assertEqual(str(exc), "'legal_types' invalid for output File.") else: self.fail('Expected Exception') try: File(iotype='out', path='xyzzy', local_path=42)
class TestComponent(Component): """ Just something to test with. """ x = Float(iotype='in', default_value=2, desc='X input') y = Float(iotype='in', default_value=3, desc='Y input', low=-10, high=10, units='ft') z = Float(iotype='out', desc='Z output', units='ft') exe_count = Int(iotype='out', desc='Execution count') in_file = File(iotype='in', local_path='inFile.dat', desc='Input file') out_file = File(iotype='out', path='outFile.dat', desc='Output file') def __init__(self): super(TestComponent, self).__init__() self.add('sub_group', SubGroup()) self.add('obj_input', TopObj(iotype='in')) self.add('obj_output', TopObj(iotype='out')) def execute(self): if self.x < 0: raise RuntimeError('x %s is < 0' % self.x) self.z = self.x * self.y self.exe_count += 1 with self.in_file.open() as inp: with open(self.out_file.path, 'w') as out: out.write(inp.read()) self._logger.info(' %s %s %s', self.x, self.y, self.z) sys.stdout.write('stdout: %s %s %s\n' % (self.x, self.y, self.z)) sys.stdout.flush() # sys.stderr.write('stderr: %s %s %s\n' % (self.x, self.y, self.z)) # sys.stderr.flush() # Copy input object to output object. self.obj_output.tob = self.obj_input.tob self.obj_output.tof = self.obj_input.tof self.obj_output.toi = self.obj_input.toi self.obj_output.tos = self.obj_input.tos self.obj_output.tofe = self.obj_input.tofe self.obj_output.toie = self.obj_input.toie self.obj_output.tose = self.obj_input.tose self.obj_output.tof1d = self.obj_input.tof1d self.obj_output.tof2d = self.obj_input.tof2d self.obj_output.tof3d = self.obj_input.tof3d self.obj_output.toi1d = self.obj_input.toi1d self.obj_output.tos1d = self.obj_input.tos1d self.obj_output.toflst = self.obj_input.toflst self.obj_output.toilst = self.obj_input.toilst self.obj_output.subobj.sob = self.obj_input.subobj.sob self.obj_output.subobj.sof = self.obj_input.subobj.sof self.obj_output.subobj.soi = self.obj_input.subobj.soi self.obj_output.subobj.sos = self.obj_input.subobj.sos @rbac(('owner', 'user')) def cause_exception(self): self.raise_exception("It's your own fault...", RuntimeError) @rbac(('owner', 'user')) def float_method(self): return self.x + self.y @rbac(('owner', 'user')) def int_method(self): return self.exe_count @rbac(('owner', 'user')) def null_method(self): return @rbac(('owner', 'user')) def str_method(self): msg = 'current state: x %r, y %r, z %r, exe_count %r' \ % (self.x, self.y, self.z, self.exe_count) return msg
class Source(Component): """ Produces files. A fair amount of stuff happens in Component.save_to_egg() in relation to handling external files and file variables. """ write_files = Bool(True, iotype='in') text_data = Str(iotype='in') text_file = File(iotype='out') sub = VarTree(Subcontainer(), iotype='out') def __init__(self, *args, **kwargs): super(Source, self).__init__(*args, **kwargs) global SOURCE_INIT SOURCE_INIT = True def configure(self): """ Called once we have a valid hierarchy above us. """ self.add('sub', Subcontainer()) # Some custom objects that must be restored. self.obj_list = [DataObj(i) for i in range(3)] # External file that doesn't exist at time of save. self.external_files.append(FileMetadata(path='does-not-exist')) self.directory = self.get_abs_directory() # Force absolute. # Absolute external file that exists at time of save. path = os.path.join(self.directory, EXTERNAL_FILES[0]) with open(path, 'w') as out: out.write('Twisty narrow passages.\n') self.external_files.append(FileMetadata(path=path, input=True, constant=True)) # Absolute external file that exists at time of save, in separate tree. path = os.path.join(self.directory, EXTERNAL_FILES[1]) leaf = os.path.dirname(path) if not os.path.exists(leaf): os.makedirs(leaf) with open(path, 'w') as out: out.write('Some external data.\n') self.external_files.append(FileMetadata(path=path)) # Relative external file that exists at time of save. with self.dir_context: path = EXTERNAL_FILES[2] with open(path, 'w') as out: out.write('Hello world!\n') self.external_files.append(FileMetadata(path=path)) # Relative external file that exists at time of save, in separate tree. with self.dir_context: path = EXTERNAL_FILES[3] leaf = os.path.dirname(path) if not os.path.exists(leaf): os.makedirs(leaf) with open(path, 'w') as out: out.write('Some more external data.\n') self.external_files.append(FileMetadata(path=path)) def execute(self): """ Write test data to files. """ if self.write_files: cwd = os.getcwd() path = 'source.txt' self._logger.debug("opening file '%s' in %s", path, cwd) with open(path, 'w') as out: out.write(self.text_data) self.text_file = FileRef(path) path = os.path.join('..', 'sub', 'source.bin') self._logger.debug("opening file '%s' in %s", path, cwd) with open(path, 'wb') as out: cPickle.dump(self.sub.binary_data, out, 2) self.sub.binary_file = FileRef(path, binary=True)
class DumbVT3(VariableTree): a = Float(1., units='ft') b = Float(12., units='inch') data = File()
class Subcontainer(VariableTree): """ Just a subcontainer for Source. """ binary_data = Array(dtype='d', iotype='in') binary_file = File(iotype='out')
def __init__(self): super(DumbVT2, self).__init__() self.add('x', Float(-1.)) self.add('y', Float(-2.)) self.add('data', File()) self.add('vt3', DumbVT3())