def test_blank(self): """ value = integer(card, n, fieldname) """ # integer with self.assertRaises(SyntaxError): blank(BDFCard([1] ), 0, 'field') with self.assertRaises(SyntaxError): blank(BDFCard(['1']), 0, 'field') # float with self.assertRaises(SyntaxError): blank(BDFCard([1.] ), 0, 'field') with self.assertRaises(SyntaxError): blank(BDFCard(['1.']), 0, 'field') # string with self.assertRaises(SyntaxError): blank(BDFCard(['a'] ), 0, 'field') with self.assertRaises(SyntaxError): blank(BDFCard(['1b']), 0, 'field') # blank val = blank(BDFCard([''] ), 0, 'field') self.assertEquals(val, None) val = blank(BDFCard([None] ), 0, 'field') self.assertEquals(val, None) val = blank(BDFCard([None] ), 0, 'field', 'default') self.assertEquals(val, 'default')
def test_double_or_blank(self): """ value = double_or_blank(card, n, fieldname, default=None) """ # integer card = BDFCard([1]) with self.assertRaises(SyntaxError): val = double_or_blank(card, 0, 'field') card = BDFCard(['2']) with self.assertRaises(SyntaxError): val = double_or_blank(card, 0, 'field') # float val = double_or_blank(BDFCard([1.]), 0, 'field') self.assertEquals(1., val) val = double_or_blank(BDFCard(['1.']), 0, 'field') self.assertEquals(1., val) val = double_or_blank(BDFCard(['1-3']), 0, 'field') self.assertEquals(1.e-3, val) # string with self.assertRaises(SyntaxError): double_or_blank(BDFCard(['a']), 0, 'field') with self.assertRaises(SyntaxError): double_or_blank(BDFCard(['1b']), 0, 'field') # blank double_or_blank(BDFCard([' ']), 0, 'field') double_or_blank(BDFCard([None]), 0, 'field')
def test_integer_double_or_string(self): # out of range with self.assertRaises(SyntaxError): integer_or_double(BDFCard([1.] ), 1, 'field') # integer self.check_integer(integer_double_or_string) # float self.check_double(integer_double_or_string) # string self.assertEqual('LOAD', integer_double_or_string(BDFCard(['load'] ), 0, 'field'))
def test_double(self): """ value = double(card, n, fieldname) """ # integer with self.assertRaises(SyntaxError): double(BDFCard([1]), 0, 'field') with self.assertRaises(SyntaxError): double(BDFCard(['1']), 0, 'field') # float double(BDFCard([1.]), 0, 'field') double(BDFCard(['1.']), 0, 'field') # string with self.assertRaises(SyntaxError): double(BDFCard(['a']), 0, 'field') with self.assertRaises(SyntaxError): double(BDFCard(['1b']), 0, 'field') # blank with self.assertRaises(SyntaxError): double(BDFCard(['']), 0, 'field') with self.assertRaises(SyntaxError): double(BDFCard([None]), 0, 'field') card = [1.0, '2.0', '3.', 'C', None, ''] exact = [1.0, 2.0, 3.0, SyntaxError, SyntaxError, SyntaxError] self.run_function(double, card, exact)
def test_integer(self): """ value = integer(card, n, fieldname) """ # integer integer(BDFCard([1]), 0, 'field') integer(BDFCard(['1']), 0, 'field') # float with self.assertRaises(SyntaxError): integer(BDFCard([1.]), 0, 'field') with self.assertRaises(SyntaxError): integer(BDFCard(['1.']), 0, 'field') # string with self.assertRaises(SyntaxError): integer(BDFCard(['a']), 0, 'field') with self.assertRaises(SyntaxError): integer(BDFCard(['1b']), 0, 'field') # blank with self.assertRaises(SyntaxError): integer(BDFCard(['']), 0, 'field') with self.assertRaises(SyntaxError): integer(BDFCard([None]), 0, 'field') card = [1, '2', '3.', 'C', None, ''] exact = [1, 2, SyntaxError, SyntaxError, SyntaxError, SyntaxError] self.run_function(integer, card, exact)
def test_string(self): """ value = string(card, n, fieldname) """ # integer with self.assertRaises(SyntaxError): string(BDFCard([1]), 0, 'field') with self.assertRaises(SyntaxError): string(BDFCard(['1']), 0, 'field') # float with self.assertRaises(SyntaxError): string(BDFCard([1.]), 0, 'field') with self.assertRaises(SyntaxError): string(BDFCard(['1.']), 0, 'field') # string string(BDFCard(['a']), 0, 'field') string(BDFCard(['1b']), 0, 'field') # blank with self.assertRaises(SyntaxError): string(BDFCard(['']), 0, 'field') with self.assertRaises(SyntaxError): string(BDFCard([None]), 0, 'field') card = ['a', 'b1', '3.', 'C', None, '', 'frog'] exact = ['a', 'b1', SyntaxError, 'C', SyntaxError, SyntaxError, 'frog'] self.run_function(string, card, exact)
def test_integer_or_blank(self): """ value = integer_or_blank(card, n, fieldname) """ # integer self.assertEqual(1, integer_or_blank(BDFCard([1]), 0, 'field')) self.assertEqual(2, integer_or_blank(BDFCard(['2']), 0, 'field')) # float with self.assertRaises(SyntaxError): integer_or_blank(BDFCard([1.]), 0, 'field') with self.assertRaises(SyntaxError): integer_or_blank(BDFCard(['1.']), 0, 'field') # string with self.assertRaises(SyntaxError): integer_or_blank(BDFCard(['a']), 0, 'field') with self.assertRaises(SyntaxError): integer_or_blank(BDFCard(['1b']), 0, 'field') # blank self.assertEqual('a', integer_or_blank(BDFCard(['']), 0, 'field', 'a')) self.assertEqual('b', integer_or_blank(BDFCard([None]), 0, 'field', 'b')) card = [1, '2', '3.', 'C', None, ''] exact = [1, 2, SyntaxError, SyntaxError, None, None] default = [None, None, None, None, None, None] self.run_function_default(integer_or_blank, card, exact, default)
def run_function(self, f, card, exact): assert len(card) == len(exact), 'len(card)=%s len(exact)=%s' % (len(card), len(exact)) i = 0 fieldname = 'f' card = BDFCard(card) for i, exacti in enumerate(exact): if exacti == SyntaxError: with self.assertRaises(SyntaxError): msg = 'field=%r exact=%r' % (card.field(i), exacti) #print msg f(card, i, fieldname) else: value = f(card, i, fieldname) self.assertEqual(value, exacti) i += 1
def __init__(self, card=[0, 0, 0, 0, 0, 0, 0], data=None, comment=''): """ Intilizes the CORD3G :param self: the CORD3G coordinate system object :param card: a list version of the fields """ if comment: self._comment = comment if isinstance(card, list): assert len(card) == 8 card = BDFCard(card) Coord.__init__(self, card, data) self.cid = integer(card, 1, 'cid') method = string_or_blank(card, 2, 'E313') self.methodES = method[0] self.methodInt = int(method[1:]) assert self.methodES in ['E', 'S'] assert 0 < self.methodInt < 1000 self.form = string_or_blank(card, 3, 'form', 'EQN') self.thetas = [integer(card, 4, 'theta1'), integer(card, 5, 'theta2'), integer(card, 6, 'theta3')] assert len(self.thetas) == 3, 'thetas=%s' % (self.thetas) self.cidRef = integer_or_blank(card, 7, 'cidRef') assert len(card) <= 8, 'len(CORD3G card) = %i' % len(card) # EQN for DEQATN, TABLE for TABLE3D assert self.form in ['EQN', 'TABLE']
def run_function_default(self, f, card, exact, default): fieldname = 'f' assert len(card) == len(exact), 'len(card)=%s len(exact)=%s' % (len(card), len(exact)) assert len(card) == len(default), 'len(card)=%s len(default)=%s' % (len(card), len(default)) i = 0 card = BDFCard(card) for i, exacti in enumerate(exact): defaulti = default[i] if exacti == SyntaxError: with self.assertRaises(exacti): msg = 'field=%r exact=%r default=%r' % (card.field(i), exacti, defaulti) #print msg f(card, i, fieldname, defaulti) else: value = f(card, i, fieldname, defaulti) self.assertEqual(value, exacti) i += 1
def check_double(self, method): # float method(BDFCard([3.0] ), 0, 'field') method(BDFCard(['4.0']), 0, 'field') method(BDFCard(['5.'] ), 0, 'field') self.assertEqual(1.0, double(BDFCard([1.] ), 0, 'field')) self.assertEqual(1.0, double(BDFCard(['1.']), 0, 'field')) self.assertEqual(-9.31e-4, double(BDFCard(['-9.31-4']), 0, 'field')) # float val = method(BDFCard([1.] ), 0, 'field') self.assertEquals(1., val) val = method(BDFCard(['1.']), 0, 'field') self.assertEquals(1., val) val = method(BDFCard(['1-3']), 0, 'field') self.assertEquals(1.e-3, val)
def test_integer_double_or_blank(self): """ value = double_or_blank(card, n, fieldname, default=None) """ # integer self.check_integer(integer_double_or_blank) # float self.check_double(integer_double_or_blank) # error - string with self.assertRaises(SyntaxError): integer_double_or_blank(BDFCard(['C'] ), 0, 'field') with self.assertRaises(SyntaxError): integer_double_or_blank(BDFCard(['1C'] ), 0, 'field') with self.assertRaises(SyntaxError): integer_double_or_blank(BDFCard(['C1'] ), 0, 'field') # blank double_or_blank(BDFCard([' ']), 0, 'field') double_or_blank(BDFCard([None]), 0, 'field')
def test_integer_double_or_blank(self): """ value = double_or_blank(card, n, fieldname, default=None) """ # integer integer_double_or_blank(BDFCard([1]), 0, 'field') integer_double_or_blank(BDFCard(['2']), 0, 'field') # float integer_double_or_blank(BDFCard([3.0]), 0, 'field') integer_double_or_blank(BDFCard(['4.0']), 0, 'field') integer_double_or_blank(BDFCard(['5.']), 0, 'field') # error - string with self.assertRaises(SyntaxError): integer_double_or_blank(BDFCard(['C']), 0, 'field') with self.assertRaises(SyntaxError): integer_double_or_blank(BDFCard(['1C']), 0, 'field') with self.assertRaises(SyntaxError): integer_double_or_blank(BDFCard(['C1']), 0, 'field')
def test_integer_string_or_blank(self): # integer self.check_integer(integer_string_or_blank) # float #print type(integer_string_or_blank(BDFCard(['4.0']), 0, 'field')) with self.assertRaises(SyntaxError): integer_string_or_blank(BDFCard([3.0] ), 0, 'field') with self.assertRaises(SyntaxError): integer_string_or_blank(BDFCard(['4.0']), 0, 'field') with self.assertRaises(SyntaxError): integer_string_or_blank(BDFCard(['5.'] ), 0, 'field') # string self.assertEqual('LOAD', integer_string_or_blank(BDFCard(['load'] ), 0, 'field')) # blank integer_string_or_blank(BDFCard([' ']), 0, 'field') integer_string_or_blank(BDFCard([None]), 0, 'field')
def test_double_or_blank(self): """ value = double_or_blank(card, n, fieldname, default=None) """ # integer card = BDFCard([1]) with self.assertRaises(SyntaxError): val = double_or_blank(card, 0, 'field') card = BDFCard(['2'] ) with self.assertRaises(SyntaxError): val = double_or_blank(card, 0, 'field') self.check_double(double_or_blank) # string with self.assertRaises(SyntaxError): double_or_blank(BDFCard(['a'] ), 0, 'field') with self.assertRaises(SyntaxError): double_or_blank(BDFCard(['1b']), 0, 'field') # blank double_or_blank(BDFCard([' ']), 0, 'field') double_or_blank(BDFCard([None]), 0, 'field')
def test_components(self): # single ints val = components(BDFCard([0]), 0, 'field') self.assertEquals(val, '0') val = components(BDFCard([1]), 0, 'field') self.assertEquals(val, '1') # single strings val = components(BDFCard(['0']), 0, 'field') self.assertEquals(val, '0') val = components(BDFCard(['1']), 0, 'field') self.assertEquals(val, '1') # double ints val = components(BDFCard(['123']), 0, 'field') self.assertEquals(val, '123') val = components(BDFCard([123]), 0, 'field') self.assertEquals(val, '123') val = components(BDFCard([321]), 0, 'field') self.assertEquals(val, '123') # embedded whiteshape with self.assertRaises(SyntaxError): val = components(BDFCard(['12 3']), 0, 'field') # all numbers val = components(BDFCard(['123456']), 0, 'field') self.assertEquals(val, '123456') # invalid 0's defined with numbers with self.assertRaises(SyntaxError): val = components(BDFCard(['0123456']), 0, 'field') with self.assertRaises(SyntaxError): val = components(BDFCard(['01']), 0, 'field') # doubles with self.assertRaises(SyntaxError): val = components(BDFCard(['4524']), 0, 'field') # only 0 to 6 with self.assertRaises(SyntaxError): val = components(BDFCard(['7']), 0, 'field') with self.assertRaises(SyntaxError): val = components(BDFCard([7]), 0, 'field') # dumb input with self.assertRaises(SyntaxError): val = components(BDFCard(['4.0']), 0, 'field') with self.assertRaises(SyntaxError): val = components(BDFCard(['-4.0']), 0, 'field') with self.assertRaises(SyntaxError): val = components(BDFCard(['asdf']), 0, 'field') with self.assertRaises(SyntaxError): val = components(BDFCard(['-1']), 0, 'field')
def check_integer(self, method): # integer self.assertEqual(1, method(BDFCard([1] ), 0, 'field')) self.assertEqual(2, method(BDFCard(['2'] ), 0, 'field'))
def test_integer_or_string(self): # out of range with self.assertRaises(SyntaxError): integer_or_string(BDFCard([1.] ), 1, 'field')
def add_card(self, card_lines, card_name, comment='', is_list=True): """ Adds a card object to the BDF object using a streaming approach. :param self: the BDF object :param card_lines: the list of the card fields >>> ['GRID,1,2',] # (is_list = False) >>> ['GRID',1,2,] # (is_list = True; default) :param card_name: the card_name -> 'GRID' :param comment: an optional the comment for the card :param is_list: changes card_lines from a list of lines to a list of fields :returns card_object: the card object representation of card .. note:: this is a very useful method for interfacing with the code .. note:: the cardObject is not a card-type object...so not a GRID card or CQUAD4 object. It's a BDFCard Object. However, you know the type (assuming a GRID), so just call the *mesh.Node(nid)* to get the Node object that was just created. .. warning:: cardObject is not returned """ if comment: self.bdf_out_file.write(comment) if card_name in ['DEQATN']: card_obj = card_lines card = card_lines #print("card =", '\n'.join(card_lines)) self.bdf_out_file.write('\n'.join(card_lines)) else: if is_list: fields = card_lines else: fields = to_fields(card_lines, card_name) # apply OPENMDAO syntax if self._is_dynamic_syntax: fields = [ self._parse_dynamic_syntax(field) if '%' in field[0:1] else field for field in fields ] card = wipe_empty_fields([ interpret_value(field, fields) if field is not None else None for field in fields ]) #print(card) card_obj = BDFCard(card) self.bdf_out_file.write(print_card_8(card_obj)) #for reject_card in self.reject_cards: #try: #print('comment =', comment) #except RuntimeError: #for field in reject_card: #if field is not None and '=' in field: #raise SyntaxError('cannot reject equal signed ' # 'cards\ncard=%s\n' % reject_card) #raise #self.reject_cards.append(card) #print('rejecting processed auto=rejected %s' % card) return card_obj
def __init__(self, card, data=None, comment=''): """ Creates a PARAM card. :param self: the object :param card: BDFCard object :param data: list of PARAM entries not including 'PARAM'; intended to be used by OP2 Reader (default=None) :param comment: optional string (default='') """ if comment: self._comment = comment if data: card = BDFCard(['PARAM'] + data) self.key = string(card, 1, 'key') n = 1 value = None if self.key == 'ACOUT': value = string_or_blank(card, 2, 'value', 'PEAK') elif self.key == 'ACOWEAK': value = string_or_blank(card, 2, 'value', 'NO') elif self.key == 'ACSYM': value = string_or_blank(card, 2, 'value', 'YES') elif self.key == 'ADJMETH': value = integer_or_blank(card, 2, 'value', 0) elif self.key == 'ADPCON': value = float_or_blank(card, 2, 'value', 1.0) #elif self.key == 'ADMPOST': #value = string_or_blank(card, 2, 'value', 0) ## TODO: 0 is not a string elif self.key == 'ADSDISC': value = float_or_blank(card, 2, 'value', 1e-8) elif self.key == 'AESMAXIT': value = integer_or_blank(card, 2, 'value', 15) elif self.key == 'AESMETH': value = string_or_blank(card, 2, 'value', 'SELECT') assert value in ['SELECT', 'AUTO', 'DIRECT', 'RITZ', 'ITER'], 'value=%s' % value elif self.key == 'AESTOL': value = ifloat_or_blank(card, 2, 'value', 1e-10) elif self.key == 'ADSTAT': value = string_or_blank(card, 2, 'value', 'YES') elif self.key in ['ALPHA1', 'ALPHA2', 'ALPHA1FL', 'ALPHA2FL']: # check alpha1/alpha1FL value1 = double_or_blank(card, 2, 'value1', 0.0) value2 = double_or_blank(card, 3, 'value2', 0.0) n = 2 elif self.key in [ 'CB1', 'CB2', 'CK1', 'CK2', 'CK3', 'CM1', 'CM2', 'CP1', 'CP2' ]: value1 = double_or_blank(card, 2, 'value1', 1.0) value2 = double_or_blank(card, 3, 'value2', 0.0) n = 2 elif self.key == 'POST': value = integer_or_blank(card, 2, 'value', 1) else: value = integer_double_or_string(card, 2, 'value') if value is None: self.values = [value1, value2] else: self.values = [value] if n == 1: if len(card) != 3: raise RuntimeError('len(PARAM card)=%i card=%r' % (len(card), card)) else: if len(card) != 4: raise RuntimeError('len(PARAM card)=%i card=%r' % (len(card), card))