def test_2Darray_write(self): my_comp = VarComponent() sb = Namelist(my_comp) my_comp.arrayvar = zeros([3, 2], dtype=numpy_float32) my_comp.arrayvar[0, 1] = 3.7 my_comp.arrayvar[2, 0] = 7.88 sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "\n" + \ "&Test\n" + \ " arrayvar(1,1) = 0.0, 3.700000047683716, \n" + \ "arrayvar(1,2) = 0.0, 0.0, \n" + \ "arrayvar(1,3) = 7.880000114440918, 0.0, \n" + \ "/\n" self.assertEqual(contents, compare)
def test_forgot_to_read(self): my_comp = VarComponent() sb = Namelist(my_comp) try: sb.load_model() except RuntimeError, err: msg = "Input file must be read with parse_file before " \ "load_model can be executed." self.assertEqual(str(err), msg)
def test_2Darray_write(self): top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) top['my_comp.arrayvar'] = zeros([3, 2], dtype=numpy_float32) top['my_comp.arrayvar'][0, 1] = 3.7 top['my_comp.arrayvar'][2, 0] = 7.88 sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "\n" + \ "&Test\n" + \ " arrayvar(1,1) = 0.0, 3.700000047683716, \n" + \ "arrayvar(1,2) = 0.0, 0.0, \n" + \ "arrayvar(1,3) = 7.880000114440918, 0.0, \n" + \ "/\n" self.assertEqual(contents, compare)
def test_unsupported_array(self): top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) top['my_comp.arrayvar'] = zeros([2, 2, 2], dtype=numpy_float32) sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") try: sb.generate() except RuntimeError as err: self.assertEqual(str(err), "Don't know how to handle array of" + \ " 3 dimensions") else: self.fail('RuntimeError expected')
def test_read3(self): # Parse a single group in a deck with non-unique group names namelist1 = "Testing\n" + \ "$GROUP\n" + \ " intvar = 99\n" + \ "$END\n" + \ "$GROUP\n" + \ " floatvar = 3.5e-23\n" + \ "$END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model(single_group=1) # Unchanged self.assertEqual(my_comp.intvar, 333) # Changed self.assertEqual(my_comp.floatvar, 3.5e-23)
def test_unsupported_array(self): my_comp = VarComponent() sb = Namelist(my_comp) my_comp.arrayvar = zeros([2, 2, 2], dtype=numpy_float32) sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") try: sb.generate() except RuntimeError, err: self.assertEqual(str(err), "Don't know how to handle array of" + \ " 3 dimensions")
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.")
def test_2Darray_read(self): namelist1 = "Testing\n" + \ "$GROUP\n" + \ " arrayvartwod(1,1) = 12, 24, 36\n" + \ " arrayvartwod(1,2) = 33, 66, 99\n" + \ "$END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() # Unchanged self.assertEqual(top['my_comp.arrayvartwod'][0][0], 12) self.assertEqual(top['my_comp.arrayvartwod'][1][2], 99)
def test_read3(self): # Parse a single group in a deck with non-unique group names namelist1 = "Testing\n" + \ "$GROUP\n" + \ " intvar = 99\n" + \ "$END\n" + \ "$GROUP\n" + \ " floatvar = 3.5e-23\n" + \ "$END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model(single_group=1) # Unchanged self.assertEqual(top['my_comp.intvar'], 333) # Changed self.assertEqual(top['my_comp.floatvar'], 3.5e-23)
def test_container_write(self): my_comp = VarComponent() my_comp.varcontainer.listenumvar = [1,2,1,3] sb = Namelist(my_comp) sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer") sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = T" in contents, True) self.assertEqual("textvar = 'Hey'" in contents, True) self.assertEqual("floatvar = 2.14543" in contents, True) self.assertEqual("intvar = 7777" in contents, True) self.assertEqual("listenumvar = 1, 2, 1, 3" in contents, True)
def test_read2(self): # Put variables in container, using rules_dict namelist1 = "Testing\n" + \ " \n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " intvar = 777\n" + \ " boolvar = .FALSE.\n" + \ " floatvar = -3.14\n" + \ " extravar = 555\n" + \ " TEXTVAR = 'That'\n" + \ " ! This is a comment too\n" + \ " listenumvar = 3,3,2,2\n" + \ "/\n" + \ "&NODICT\n" + \ " noval = 0\n" + \ "/\n" + \ "&DUPE\n" + \ " some = 0\n" + \ "/\n" + \ "&DUPE\n" + \ " some = 0\n" + \ "/\n" + \ "FREEFORM\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() rules_dict = {"OPTION": ["varcontainer"]} n1, n2, n3 = sb.load_model(rules_dict) self.assertEqual(n1[4], 'FREEFORM') self.assertEqual(n2[1], 'NODICT') self.assertEqual(n2[2], 'DUPE') self.assertEqual(n2[3], 'DUPE') self.assertEqual(n3, ['extravar']) self.assertEqual(my_comp.varcontainer.intvar, 777) self.assertEqual(my_comp.varcontainer.boolvar, False) self.assertEqual(my_comp.varcontainer.floatvar, -3.14) self.assertEqual(my_comp.varcontainer.textvar, 'That') self.assertEqual(my_comp.varcontainer.listenumvar, [3, 3, 2, 2])
def test_read2(self): # Put variables in container, using rules_dict namelist1 = "Testing\n" + \ " \n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " intvar = 777\n" + \ " boolvar = .FALSE.\n" + \ " floatvar = -3.14\n" + \ " extravar = 555\n" + \ " TEXTVAR = 'That'\n" + \ " ! This is a comment too\n" + \ "/\n" + \ "&NODICT\n" + \ " noval = 0\n" + \ "/\n" + \ "&DUPE\n" + \ " some = 0\n" + \ "/\n" + \ "&DUPE\n" + \ " some = 0\n" + \ "/\n" + \ "FREEFORM\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() rules_dict = { "OPTION" : ["varcontainer"] } n1, n2, n3 = sb.load_model(rules_dict) self.assertEqual(n1[4], 'FREEFORM') self.assertEqual(n2[1], 'NODICT') self.assertEqual(n2[2], 'DUPE') self.assertEqual(n2[3], 'DUPE') self.assertEqual(n3, ['extravar']) self.assertEqual(top['my_comp.varcontainer:intvar'], 777) self.assertEqual(top['my_comp.varcontainer:boolvar'], False) self.assertEqual(top['my_comp.varcontainer:floatvar'], -3.14) self.assertEqual(top['my_comp.varcontainer:textvar'], 'That')
def test_read2(self): # Put variables in container, using rules_dict namelist1 = "Testing\n" + \ " \n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " intvar = 777\n" + \ " boolvar = .FALSE.\n" + \ " floatvar = -3.14\n" + \ " extravar = 555\n" + \ " TEXTVAR = 'That'\n" + \ " ! This is a comment too\n" + \ " listenumvar = 3,3,2,2\n" + \ "/\n" + \ "&NODICT\n" + \ " noval = 0\n" + \ "/\n" + \ "&DUPE\n" + \ " some = 0\n" + \ "/\n" + \ "&DUPE\n" + \ " some = 0\n" + \ "/\n" + \ "FREEFORM\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() rules_dict = { "OPTION" : ["varcontainer"] } n1, n2, n3 = sb.load_model(rules_dict) self.assertEqual(n1[4], 'FREEFORM') self.assertEqual(n2[1], 'NODICT') self.assertEqual(n2[2], 'DUPE') self.assertEqual(n2[3], 'DUPE') self.assertEqual(n3, ['extravar']) self.assertEqual(my_comp.varcontainer.intvar, 777) self.assertEqual(my_comp.varcontainer.boolvar, False) self.assertEqual(my_comp.varcontainer.floatvar, -3.14) self.assertEqual(my_comp.varcontainer.textvar, 'That') self.assertEqual(my_comp.varcontainer.listenumvar, [3, 3, 2, 2])
def test_2Darray_read(self): namelist1 = "Testing\n" + \ "$GROUP\n" + \ " arrayvartwod(1,1) = 12, 24, 36\n" + \ " arrayvartwod(1,2) = 33, 66, 99\n" + \ "$END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() # Unchanged self.assertEqual(my_comp.arrayvartwod[0][0], 12) self.assertEqual(my_comp.arrayvartwod[1][2], 99)
def test_read4(self): # Variables on same line as header namelist1 = "Testing\n" + \ " \n" + \ "&OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23\n" + \ "/\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(my_comp.boolvar, True) self.assertEqual(my_comp.arrayvar[0], 3.5)
def test_read1(self): # Put variables in top container, so no rules_dict namelist1 = "Testing\n" + \ " \n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " INTVAR = 777, single(1) = 15.0, floatvar = -3.14\n" + \ " singleint(2) = 3,4,5\n" + \ " stringarray(3) = 'xyz'\n" + \ " boolvar = T\n" + \ " textvar = 'That'\n" + \ " ! This is a comment too\n" + \ " listenumvar = 3,3,2,2\n" + \ " listenumvar2 = 1.5\n" + \ " arrayvar = 3.5, 7.76, 1.23\n" + \ " arrayvarsplit = 3.5, 7.76\n" + \ " 5.45, 22.0\n" + \ " 1.23\n" + \ " arrayvarsplit2 = 1\n" + \ " 2\n" + \ " 3\n" + \ " arraysmall = 1.75\n" + \ " arrayshorthand = 3.456*8\n" + \ " expvar1 = 1.5e-12\n" + \ " expvar2 = -1.5D12\n" + \ "/\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(sb.title, 'Testing') self.assertEqual(my_comp.intvar, 777) self.assertEqual(my_comp.boolvar, True) self.assertEqual(my_comp.floatvar, -3.14) self.assertEqual(my_comp.expvar1, 1.5e-12) self.assertEqual(my_comp.expvar2, -1.5e12) self.assertEqual(my_comp.textvar, 'That') self.assertEqual(my_comp.listenumvar, [3, 3, 2, 2]) self.assertEqual(my_comp.listenumvar2, [1.5]) self.assertEqual(my_comp.arrayvar[0], 3.5) self.assertEqual(my_comp.arrayvar[1], 7.76) self.assertEqual(my_comp.arrayvar[2], 1.23) self.assertEqual(my_comp.arrayvarsplit[0], 3.5) self.assertEqual(my_comp.arrayvarsplit[1], 7.76) self.assertEqual(my_comp.arrayvarsplit[2], 5.45) self.assertEqual(my_comp.arrayvarsplit[3], 22.0) self.assertEqual(my_comp.arrayvarsplit[4], 1.23) self.assertEqual(my_comp.arrayvarsplit2[0], 1) self.assertEqual(my_comp.arrayvarsplit2[1], 2) self.assertEqual(my_comp.arrayvarsplit2[2], 3) self.assertEqual(my_comp.arraysmall[0], 1.75) self.assertEqual(len(my_comp.arraysmall), 1) self.assertEqual(my_comp.arrayshorthand[4], 3.456) self.assertEqual(len(my_comp.arrayshorthand), 8) self.assertEqual(my_comp.single[0], 15.0) self.assertEqual(my_comp.singleint[3], 5) self.assertEqual(my_comp.stringarray[2], 'xyz') self.assertEqual(type(my_comp.singleint[2]), numpy_int32) # Test out reading a single card by name self.assertEqual(sb.find_card('OPTION', 'floatvar'), -3.14)
def test_writes(self): top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.set_title("Testing") sb.add_group('FREEFORM') sb.add_group('OPTION') sb.add_comment("This is a comment") sb.add_var("boolvar") sb.add_var("intvar") sb.add_var("floatvar") sb.add_var("textvar") sb.add_newvar("newcard", "new value") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "Testing\n" + \ "FREEFORM\n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " boolvar = F\n" + \ " intvar = 333\n" + \ " floatvar = -16.54\n" + \ " textvar = 'This'\n" + \ " newcard = 'new value'\n" + \ "/\n" self.assertEqual(contents, compare)
def test_read_pre_setup(self): # Put variables in top container, so no rules_dict namelist1 = "Testing\n" + \ " \n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " INTVAR = 777, single(1) = 15.0, floatvar = -3.14\n" + \ " singleint(2) = 3,4,5\n" + \ " stringarray(3) = 'xyz'\n" + \ " boolvar = T\n" + \ " textvar = 'That'\n" + \ " ! This is a comment too\n" + \ " arrayvar = 3.5, 7.76, 1.23\n" + \ " arrayvarsplit = 3.5, 7.76\n" + \ " 5.45, 22.0\n" + \ " 1.23\n" + \ " arrayvarsplit2 = 1\n" + \ " 2\n" + \ " 3\n" + \ " arraysmall = 1.75\n" + \ " arrayshorthand = 3.456*8\n" + \ " expvar1 = 1.5e-12\n" + \ " expvar2 = -1.5D12\n" + \ "/\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() my_comp = top.root.add('my_comp', VarComponent()) sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(sb.title, 'Testing') self.assertEqual(my_comp._init_params_dict['intvar']['val'], 777) self.assertEqual(my_comp._init_params_dict['boolvar']['val'], True) self.assertEqual(my_comp._init_params_dict['floatvar']['val'], -3.14) self.assertEqual(my_comp._init_params_dict['expvar1']['val'], 1.5e-12) self.assertEqual(my_comp._init_params_dict['expvar2']['val'], -1.5e12) self.assertEqual(my_comp._init_params_dict['textvar']['val'], 'That') self.assertEqual(my_comp._init_params_dict['arrayvar']['val'][0], 3.5) self.assertEqual(my_comp._init_params_dict['arrayvar']['val'][1], 7.76) self.assertEqual(my_comp._init_params_dict['arrayvar']['val'][2], 1.23) self.assertEqual(my_comp._init_params_dict['arrayvarsplit']['val'][0], 3.5) self.assertEqual(my_comp._init_params_dict['arrayvarsplit']['val'][1], 7.76) self.assertEqual(my_comp._init_params_dict['arrayvarsplit']['val'][2], 5.45) self.assertEqual(my_comp._init_params_dict['arrayvarsplit']['val'][3], 22.0) self.assertEqual(my_comp._init_params_dict['arrayvarsplit']['val'][4], 1.23) self.assertEqual(my_comp._init_params_dict['arrayvarsplit2']['val'][0], 1) self.assertEqual(my_comp._init_params_dict['arrayvarsplit2']['val'][1], 2) self.assertEqual(my_comp._init_params_dict['arrayvarsplit2']['val'][2], 3) self.assertEqual(my_comp._init_params_dict['arraysmall']['val'], 1.75) self.assertEqual(my_comp._init_params_dict['arrayshorthand']['val'][4], 3.456) self.assertEqual(len(my_comp._init_params_dict['arrayshorthand']['val']), 8) self.assertEqual(my_comp._init_params_dict['single']['val'][0], 15.0) self.assertEqual(my_comp._init_params_dict['singleint']['val'][3], 5) self.assertEqual(my_comp._init_params_dict['stringarray']['val'][2], 'xyz')
def load_model(self, control_input='control.in', flocond_input='flocond.in', expnd_input='expnd.in', zrdmix_input='zrdmix.in', hwall_input='hwall.in'): '''Reads in an existing set of input files and populates the model.''' ignore = ['ist', 'ifab'] ct = Namelist(self) ct.set_filename(control_input) ct.parse_file() ct.load_model(ignore=ignore) ignore = ['p01d', 'p02d', 't01d', 't02d', 'rm1', 'rm2', 'gam', 'pinf', 'a1d', 'a2d', 'a3d'] fc = Namelist(self) fc.set_filename(flocond_input) fc.parse_file() fc.load_model(ignore=ignore) self.flow_in.pri.Pt = fc.find_card('floc', 'p01d') self.flow_in.sec.Pt = fc.find_card('floc', 'p02d') self.flow_in.pri.Tt = fc.find_card('floc', 't01d') self.flow_in.sec.Tt = fc.find_card('floc', 't02d') self.flow_in.pri.Mach = fc.find_card('floc', 'rm1') self.flow_in.sec.Mach = fc.find_card('floc', 'rm2') self.flow_in.gamma = fc.find_card('floc', 'gam') self.flow_in.Pstatic = fc.find_card('floc', 'pinf') a1d = fc.find_card('floc', 'a1d') a2d = fc.find_card('floc', 'a2d') a3d = fc.find_card('floc', 'a3d') ep = Namelist(self) ep.set_filename(expnd_input) ep.parse_file() ep.load_model() ignore = ['RLD', 'H0LM', 'H0HY', 'ALP1', 'ALP2', 'BWID'] zr = Namelist(self) zr.set_filename(zrdmix_input) zr.parse_file() zr.load_model(ignore=ignore) self.geo_in.length = zr.find_card('zrd', 'RLD') self.geo_in.LhWave = zr.find_card('zrd', 'H0LM') self.geo_in.LhMh = zr.find_card('zrd', 'H0HY') self.geo_in.ChuteAngles = zr.find_card('zrd', 'ALP1') #self.geo_in.ChuteAngles = zr.find_card('zrd', 'ALP2') BWID = zr.find_card('zrd', 'BWID') parser = FileParser() parser.set_file(hwall_input) parser.set_delimiters(", ") nrow = parser.transfer_var(0, 1) geom = zeros((nrow, 2), 'd') for index in range(nrow): geom[index, :] = parser.transfer_array(1+index, 1, 1+index, 2) self.geom = geom # Reverse area calculations for AsAp, AeAt and AR # Note that DREA only uses half the area as it assumes a plane of symmetry self.geo_in.Apri = 2.0*a1d self.geo_in.AsAp = 2.0*a2d/self.geo_in.Apri self.geo_in.Asec = self.geo_in.AsAp*self.geo_in.Apri self.geo_in.AeAt = 2.0*a3d/(self.geo_in.Apri+self.geo_in.Asec) self.geo_in.AR = BWID*BWID/(self.geo_in.Apri+self.geo_in.Asec) #self.geo_in.length = self.geom[-1, 0] self.geo_in.width = 0.5*(self.geo_in.Apri + self.geo_in.Asec)/self.geom[0,1] self.geo_in.Aexit = 2.0*self.geom[-1,1]*self.geo_in.width
def test_writes(self): my_comp = VarComponent() my_comp.listenumvar = [1, 2, 1, 3] my_comp.listenumvar2 = [1.5, 1.5] my_comp.listenumvar3 = ['b'] my_comp.listenumvar4 = [False, False, False] sb = Namelist(my_comp) sb.set_filename(self.filename) sb.set_title("Testing") sb.add_group('FREEFORM') sb.add_group('OPTION') sb.add_comment("This is a comment") sb.add_var("boolvar") sb.add_var("intvar") sb.add_var("floatvar") sb.add_var("textvar") sb.add_var("listenumvar") sb.add_var("listenumvar2") sb.add_var("listenumvar3") sb.add_var("listenumvar4") sb.add_newvar("newcard", "new value") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "Testing\n" + \ "FREEFORM\n" + \ "&OPTION\n" + \ " This is a comment\n" + \ " boolvar = F\n" + \ " intvar = 333\n" + \ " floatvar = -16.54\n" + \ " textvar = 'This'\n" + \ " listenumvar = 1, 2, 1, 3\n" + \ " listenumvar2 = 1.5, 1.5\n" + \ " listenumvar3 = 'b'\n" + \ " listenumvar4 = F, F, F\n" + \ " newcard = 'new value'\n" + \ "/\n" self.assertEqual(contents, compare)
def test_1Darray_write(self): my_comp = VarComponent() sb = Namelist(my_comp) my_comp.arrayvar = zeros(3, dtype=numpy_float32) my_comp.arrayvar[2] = 3.7 my_comp.single = array(['a', 'b', 'c']) my_comp.singleint = array([1, 2, 3]) my_comp.singlebool = array([False, True, False]) sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") # This should be ignored because it is zero-D sb.add_var("arrayvarzerod") sb.add_var("single") sb.add_var("singleint") sb.add_var("singlebool") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "\n" + \ "&Test\n" + \ " arrayvar = 0.0, 0.0, 3.700000047683716\n" + \ " single = 'a', 'b', 'c'\n" + \ " singleint = 1, 2, 3\n" + \ " singlebool = F, T, F\n" + \ "/\n" self.assertEqual(contents, compare)
def test_vartree_write(self): top = Problem() top.root = Group() top.root.add('my_comp',VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer") sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = T" in contents, True) self.assertEqual("textvar = 'Hey'" in contents, True) self.assertEqual("floatvar = 2.14543" in contents, True) self.assertEqual("intvar = 7777" in contents, True) # now test skipping sb = Namelist(top.root.my_comp) top['my_comp.varcontainer:boolvar'] = True top['my_comp.varcontainer:textvar'] = "Skipme" sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer", skip='textvar') sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = T" in contents, True) self.assertEqual("textvar = 'Skipme'" in contents, False) self.assertEqual("intvar = 7777" in contents, True)
def load_model(self): ct = Namelist(self) ct.set_filename('control.in') ct.parse_file() ct.load_model() fc = Namelist(self) fc.set_filename('flocond.in') fc.parse_file() fc.load_model() ep = Namelist(self) ep.set_filename('expnd.in') ep.parse_file() ep.load_model() zr = Namelist(self) zr.set_filename('zrdmix.in') zr.parse_file() zr.load_model()
def test_read4(self): # Variables on same line as header namelist1 = "Testing\n" + \ " \n" + \ "&OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23\n" + \ "/\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(top['my_comp.boolvar'], True) self.assertEqual(top['my_comp.arrayvar'][0], 3.5) namelist1 = "Testing\n" + \ " \n" + \ "$OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23, $END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(top['my_comp.boolvar'], True) self.assertEqual(top['my_comp.arrayvar'][0], 3.5)
def generate_input(self, FabriOrSub): """Creates the DREA input files.""" # Determine ist, ifab and geometry parameters # ------------------------- if FabriOrSub == 'Fabri': self.ist = 1 self.ifab = 1 elif FabriOrSub == 'Subsonic': self.ist = 0 self.ifab = 0 else: msg = 'FabriOrSub should be Fabri or Subsonic' raise RuntimeError(msg) self.geom[-1,0] = self.geo_in.length self.geom[0,1] = (self.geo_in.Apri/2+self.geo_in.Asec/2)/self.geo_in.width self.geom[-1,1] = self.geo_in.Aexit/2/self.geo_in.width # Create control.in # ------------------------- crt = Namelist(self) crt.set_filename("control.in") crt.add_group('cntrl') crt.add_var("icnvl") crt.add_var("ieject") crt.add_newvar("ist", self.ist) crt.add_newvar("ifab", self.ifab) crt.add_var("ispm") crt.add_var("iprnt") crt.add_var("ipw") crt.add_var("nmax") crt.generate() # Create flocond.in # ------------------------- flw = Namelist(self) flw.set_filename("flocond.in") flw.add_group('floc') flw.add_newvar("p01d", self.flow_in.pri.Pt) flw.add_newvar("p02d", self.flow_in.sec.Pt) flw.add_newvar("t01d", self.flow_in.pri.Tt) flw.add_newvar("t02d", self.flow_in.sec.Tt) flw.add_newvar("rm1", self.flow_in.pri.Mach) flw.add_newvar("rm2", self.flow_in.sec.Mach) flw.add_var("a1d") flw.add_var("a2d") flw.add_var("a3d") flw.add_var("rg") flw.add_newvar("gam", self.flow_in.gamma) flw.add_newvar("pinf", self.flow_in.Pstatic) flw.add_var("rec1") flw.add_var("rec2") flw.generate() # Create expnd.in # ------------------------- exd = Namelist(self) exd.set_filename("expnd.in") exd.add_group('exd') exd.add_var("rm1s") exd.add_var("rm2s") exd.add_var("dxe") exd.add_var("relx"), exd.add_var("errm") exd.add_var("nmx") exd.add_var("intt") exd.generate() # Create zrdmix.in # ------------------------- zrd = Namelist(self) zrd.set_filename("zrdmix.in") zrd.add_group('zrd') zrd.add_var("BWID") zrd.add_newvar("RLD", self.geo_in.length) zrd.add_var("RLPRNT") zrd.add_var("PR") zrd.add_var("CGR") zrd.add_var("REVRT") zrd.add_newvar("H0LM", self.geo_in.LhWave) zrd.add_newvar("H0HY", self.geo_in.LhMh) zrd.add_newvar("ALP1", self.geo_in.ChuteAngles) zrd.add_newvar("ALP2", self.geo_in.ChuteAngles) zrd.add_var("IMAX") zrd.add_var("JMAX") zrd.generate() # Create hwall.in # ------------------------- geom_data = [] form = "%.15f, %.15f, \n" geom_data.append("%d\n" % self.geom.shape[0]) for element in self.geom: geom_data.append(form % (element[0], element[1])) outfile = open("hwall.in", 'w') outfile.writelines(geom_data) outfile.close()
def test_container_write(self): my_comp = VarComponent() my_comp.varcontainer.listenumvar = [1, 2, 1, 3] sb = Namelist(my_comp) sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer") sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = T" in contents, True) self.assertEqual("textvar = 'Hey'" in contents, True) self.assertEqual("floatvar = 2.14543" in contents, True) self.assertEqual("intvar = 7777" in contents, True) self.assertEqual("listenumvar = 1, 2, 1, 3" in contents, True) # now test skipping sb = Namelist(my_comp) my_comp.varcontainer.boolvar = True my_comp.varcontainer.textvar = "Skipme" sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer", skip='textvar') sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = T" in contents, True) self.assertEqual("textvar = 'Skipme'" in contents, False)
def generate_input(self, FabriOrSub): """Creates the DREA input files.""" # Determine ist, ifab and geometry parameters # ------------------------- if FabriOrSub == 'Fabri': self.ist = 1 self.ifab = 1 if FabriOrSub == 'Subsonic': self.ist = 0 self.ifab = 0 self.geom[-1, 0] = self.geo_in.length self.geom[0, 1] = (self.geo_in.Apri / 2 + self.geo_in.Asec / 2) / self.geo_in.width self.geom[-1, 1] = self.geo_in.Aexit / 2 / self.geo_in.width # Create control.in # ------------------------- crt = Namelist(self) crt.set_filename("control.in") crt.add_group('cntrl') crt.add_var("icnvl") crt.add_var("ieject") crt.add_var("ist") crt.add_var("ifab") crt.add_var("ispm") crt.add_var("iprnt") crt.add_var("ipw") crt.add_var("nmax") crt.generate() # Create flocond.in # ------------------------- flw = Namelist(self) flw.set_filename("flocond.in") flw.add_group('floc') flw.add_newvar("p01d", self.flow_in.pri.Pt) flw.add_newvar("p02d", self.flow_in.sec.Pt) flw.add_newvar("t01d", self.flow_in.pri.Tt) flw.add_newvar("t02d", self.flow_in.sec.Tt) flw.add_newvar("rm1", self.flow_in.pri.Mach) flw.add_newvar("rm2", self.flow_in.sec.Mach) flw.add_var("a1d") flw.add_var("a2d") flw.add_var("a3d") flw.add_var("rg") flw.add_newvar("gam", self.flow_in.gamma) flw.add_newvar("pinf", self.flow_in.Pstatic) flw.add_var("rec1") flw.add_var("rec2") flw.generate() # Create expnd.in # ------------------------- exd = Namelist(self) exd.set_filename("expnd.in") exd.add_group('exd') exd.add_var("rm1s") exd.add_var("rm2s") exd.add_var("dxe") exd.add_var("relx"), exd.add_var("errm") exd.add_var("nmx") exd.add_var("intt") exd.generate() # Create zrdmix.in # ------------------------- zrd = Namelist(self) zrd.set_filename("zrdmix.in") zrd.add_group('zrd') zrd.add_var("BWID") zrd.add_newvar("RLD", self.geo_in.length) zrd.add_var("RLPRNT") zrd.add_var("PR") zrd.add_var("CGR") zrd.add_var("REVRT") zrd.add_newvar("H0LM", self.geo_in.LhWave) zrd.add_newvar("H0HY", self.geo_in.LhMh) zrd.add_newvar("ALP1", self.geo_in.ChuteAngles) zrd.add_newvar("ALP2", self.geo_in.ChuteAngles) zrd.add_var("IMAX") zrd.add_var("JMAX") zrd.generate() # Create hwall.in # ------------------------- geom_data = [] form = "%.15f, %.15f, \n" geom_data.append("%d\n" % self.geom.shape[0]) for element in self.geom: geom_data.append(form % (element[0], element[1])) outfile = open("hwall.in", 'w') outfile.writelines(geom_data) outfile.close()
def test_vartree_write2(self): #testing namelist_util before setup() top = Problem() top.root = Group() myvars = VarComponent() top.root.add('my_comp', myvars) sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer") sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = T" in contents, True) self.assertEqual("textvar = 'Hey'" in contents, True) self.assertEqual("floatvar = 2.14543" in contents, True) self.assertEqual("intvar = 7777" in contents, True) self.assertEqual("varcontainer" in contents,False) #ensure that containers with similar names are not confused # varcontainer vs. varcontainer_1 self.assertEqual("boolvar_1" in contents, False) self.assertEqual("intvar_1" in contents, False) # now test skipping sb = Namelist(top.root.my_comp) myvars._init_params_dict['varcontainer:boolvar']['val'] = False myvars._init_params_dict['varcontainer:textvar']['val'] = "Skipme" myvars._init_params_dict['varcontainer:intvar']['val'] = 8888 myvars._init_params_dict['varcontainer:floatvar']['val'] = 3.14 sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer",skip='textvar') sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar = F" in contents, True) self.assertEqual("intvar = 8888" in contents, True) self.assertEqual("floatvar = 3.14" in contents, True) self.assertEqual("textvar = 'Skipme'" in contents, False) self.assertEqual("varcontainer" not in contents,True) sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.add_group('Test') sb.add_container("varcontainer_1") sb.generate() f = open(self.filename, 'r') contents = f.read() self.assertEqual("boolvar_1 = T" in contents, True) self.assertEqual("intvar_1 = 4444" in contents, True) top.setup(check=False) top.run()
def test_read4(self): # Variables on same line as header namelist1 = "Testing\n" + \ " \n" + \ "&OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23\n" + \ "/\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(my_comp.boolvar, True) self.assertEqual(my_comp.arrayvar[0], 3.5) namelist1 = "Testing\n" + \ " \n" + \ "$OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23, $END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() my_comp = VarComponent() sb = Namelist(my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(my_comp.boolvar, True) self.assertEqual(my_comp.arrayvar[0], 3.5)
def test_1Darray_write(self): top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) top['my_comp.arrayvar'] = zeros(3, dtype=numpy_float32) top['my_comp.arrayvar'][2] = 3.7 top['my_comp.single'] = array(['a', 'b', 'c']) top['my_comp.singleint'] = array([1, 2, 3]) top['my_comp.singlebool'] = array([False, True, False]) sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") # This should be ignored because it is zero-D sb.add_var("arrayvarzerod") sb.add_var("single") sb.add_var("singleint") sb.add_var("singlebool") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "\n" + \ "&Test\n" + \ " arrayvar = 0.0, 0.0, 3.700000047683716\n" + \ " single = 'a', 'b', 'c'\n" + \ " singleint = 1, 2, 3\n" + \ " singlebool = F, T, F\n" + \ "/\n" self.assertEqual(contents, compare)