def assert_fields(card1, card2): return try: fields1 = wipe_empty_fields(card1.repr_fields()) fields2 = wipe_empty_fields(card2.repr_fields()) except: print("card1 = \n%s" % card1) print("card2 = \n%s" % card2) raise if len(fields1) != len(fields2): msg = ('len(fields1)=%s len(fields2)=%s\n%r\n%r\n%s\n%s' % (len(fields1), len(fields2), fields1, fields2, print_card(fields1), print_card(fields2))) raise RuntimeError(msg) for (i, field1, field2) in zip(count(), fields1, fields2): value1a = print_field_8(field1) value2a = print_field_8(field2) if value1a != value2a: value1 = print_field_8(interpret_value(value1a)) value2 = print_field_8(interpret_value(value2a)) if value1 != value2: msg = 'value1 != value2\n' msg += ('cardName=%s ID=%s i=%s field1=%r field2=%r value1=%r ' 'value2=%r\n%r\n%r' % (fields1[0], fields1[1], i, field1, field2, value1, value2, fields1, fields2)) raise RuntimeError(msg)
def __repr__(self): """ Prints a card in the simplest way possible (default values are left blank). """ comment = self.comment list_fields = self.repr_fields() try: return comment + print_card(list_fields, size=8) except: try: return comment + print_card(list_fields, size=16) except: print('problem printing %s card' % self.type) print("list_fields = ", list_fields) raise
def write_load(self, bdf, load_case_id, nid, Fx, Fy, Fz, comment=''): """ This function takes a: load case node ID Force and writes out a properly formatted card """ cid = 0 scale_factor = 1. card = ['FORCE', load_case_id, nid, cid, scale_factor, Fx, Fy, Fz] #comment += " card=%s" % (card) #out = printCard(card)[:-1]+ ' $ %s\n' % comment out = print_card(card, size=16) bdf.write(out)
def print_repr_card(self, size=8, is_double=False): # type: (int, bool) -> str """prints the card in 8/16/16-double format""" list_fields = self.repr_fields() return self.comment + print_card( list_fields, size=size, is_double=is_double)
def print_card(self, size=8): list_fields = self.repr_fields() return self.comment + print_card(list_fields, size=size)
def parse_op2(contact_bdf, main_op2, subcase_id, contact_surfaces, eid_groups, nodes_groups, stiffnesses, errors): """ assumes static analysis """ from pyNastran.op2.op2 import read_op2 op2 = read_op2(op2_filename=main_op2) spring_forces = op2.spring_forces[subcase_id] #force_name = 'forces.bdf' #contact_bdf = 'spring.bdf' with open(contact_bdf, 'w') as bdf_file: errorsi = 0 for contact_surface, eid_group, nodes_group in zip( contact_surfaces, eid_groups, nodes_groups): (_left_bdf, _right_bdf, _dof, stiffness, cid, _glue, _initial_gap, _max_deflection_error) = contact_surface #spring_eids_min, spring_eids_max = eid_groups #nodes_left, nodes_right = nodes_groups #nelements = spring_eids_max - spring_eids_min #forces = zeros((nelements), 'float64') c1 = c2 = cid i = 0 for eid, force in iteritems(spring_forces.forces): #forces[i] = force g1 = spring_forces.g1[eid] g2 = spring_forces.g2[eid] _k = stiffnesses[i] #deflection = force/k if force <= 0: # assume a really small stiffness (no contact) #force = 0.0 #if errors[i] == 1: # if old error new_stiffness = 0.01 stiffnesses[i] = new_stiffness new_flag = 0 else: # force > 0 # the contact is correct #force_card = ['FORCE'] #if deflection > new_stiffness = stiffness #flag = int(force/abs(force)) # 0, 1 new_flag = 1 celas = ['CELAS2', eid, new_stiffness, g1, c1, g2, c2] bdf_file.write(print_card(celas)) # check to see if new_flag != errors[i]: errorsi += 1 errors[i] = new_flag i += 1 ierrors = where(errors == 1)[0] nerrors = len(ierrors) return nerrors
def test_card_double(self): fields = ['GRID', 1, None, 120.322, -4.82872, 1.13362] card_expected = ( 'GRID* 1 1.2032200000D+02-4.828720000D+00\n' '* 1.1336200000D+00\n') card1 = print_card(fields, size=16, is_double=True) card2 = print_card_double(fields) self.assertEqual(card1, card_expected) self.assertEqual(card2, card_expected) #card = print_card_double(['CHEXA', 1, 2, 2, 3, 4, 1, 8, 5, #6, 7]) #card_expected = '' #card_expected += 'CHEXA* 1 2 2 3\n' #card_expected +='* 4 1 8 5\n' #card_expected +='* 6 7\n' #card_expected += '* 1.1336200000D+00\n' #card = print_card_double(['CTETRA',6437,1,14533,5598,1577,9976,42364,5599,42365,42363,30022,12904]) #card_expected = '' #card_expected += 'CTETRA* 6437 1 14533 5598\n' #card_expected += '* 1577 9976 42364 5599\n' #card_expected += '* 42365 42363 30022 12904\n' #card_expected += '* \n' #card_expected +='CTETRA* 6437 1 14533 5598\n' #card_expected +='* 1577 9976 42364 5599\n' #card_expected +='* 42365 42363 30022 12904\n' #card_expected +='* \n' #self.assertEqual(card, card_expected) #============================= # mid E1 E2 nu12 G12 G1z G2z rho fields = [ 'MAT8', 6, 1.7e+7, 1.7e+7, .98, 340000., 180000., 180000., 0.0001712, # a1 a2 tref None, 71.33 ] card_expected = ( 'MAT8 6 1.7+7 1.7+7 .98 340000. 180000. 180000..0001712\n' ' 71.33\n') card1 = print_card_8(fields) card2 = print_card(fields) self.assertEqual(card1, card_expected) self.assertEqual(card2, card_expected) fields = [ 'MAT8', 6, 1.7e+7, 1.7e+7, .98, 340000., 180000., 180000., 0.0001712, # a1 a2 tref None, 71.33 ] card1 = print_card_16(fields) card2 = print_card(fields, size=16) card_expected = ( 'MAT8* 6 17000000. 17000000. .98\n' '* 340000. 180000. 180000. .0001712\n' '* 71.33\n' '*\n') # bad self.assertEqual(card1, card_expected) self.assertEqual(card2, card_expected)
def print_repr_card(self, size: int = 8, is_double: bool = False) -> str: """prints the card in 8/16/16-double format""" list_fields = self.repr_fields() return self.comment + print_card( list_fields, size=size, is_double=is_double)
def write_card_16(self, is_double: bool=False) -> str: fields = self.repr_fields() return print_card(fields, size=16, is_double=False)
def write_card(self, size=8, is_double=False): # type: (int, bool) -> str """prints the card in 8/16/16-double format""" return print_card(self.card, size=size, is_double=is_double)
def write_card(self, size: int = 8, is_double: bool = False) -> str: """prints the card in 8/16/16-double format""" return print_card(self.card, size=size, is_double=is_double)
def create_plane(self, width, height, nx, ny, eid_start): """ Creates a quadrilateral plane made of CTRIA3s. This output can be intersected with another geometry. :param width: width of plane :param height: height of plane :param nx: number of elements along the width :param ny: number of elements along the height :param eid_start: the starting elementID :: 1-----2 | \ B | | A \ | 4-----3 """ assert nx > 0 assert ny > 0 dx = width / nx dy = height / ny nid_start = 200 n = nid_start #x=[]; y=[] ij_NMap = {} points = {} for j in range(ny + 1): yi = dy * j for i in range(nx + 1): xi = dx * i points[n] = array([xi, yi, 0.]) ij_NMap[(i, j)] = n n += 1 elements = [] for j in range(ny): for i in range(nx): element = [ij_NMap[(i, j)], ij_NMap[(i + 1, j)], ij_NMap[(i + 1, j + 1)], ] elements.append(element) element = [ij_NMap[(i, j)], ij_NMap[(i + 1, j + 1)], ij_NMap[(i, j + 1)], ] elements.append(element) #origin = [width/2,height/2,0.] origin = [0., 0., -1.] zaxis = array([0., 1., 0.]) xaxis = array([1., 0., 0.]) with open('plane.bdf', 'wb') as bdf_file: #f.write('SOL 101\n') #f.write('CEND\n') #f.write('BEGIN BULK\n') cid = 200 rid = 0 coord = ['CORD2R', cid, rid, origin[0], origin[1], origin[2], zaxis[0], zaxis[1], zaxis[2], xaxis[0], xaxis[1], xaxis[2]] f.write(print_card(coord)) for nid, point in sorted(iteritems(points)): (x, y, z) = point node = ['GRID', nid, cid, x, y, z] bdf_file.write(print_card(node)) pid = 123 mid = pid eidStart = 200 for eid, element in enumerate(elements): (n1, n2, n3) = element tri = ['CTRIA3', eid + eid_start, pid, n1, n2, n3] bdf_file.write(print_card(tri)) shell = ['PSHELL', pid, mid, 1.0] bdf_file.write(print_card(shell)) mat = ['MAT1', mid, 1e7, None, 0.3] bdf_file.write(print_card(mat)) #bdf_file.write('ENDDATA\n') return points, elements
def print_repr_card(self, size=8, is_double=False): list_fields = self.repr_fields() return self.comment + print_card(list_fields, size=size, is_double=is_double)
def print_raw_card(self, size=8, is_double=False): # type: (int, bool) -> str """A card's raw fields include all defaults for all fields""" list_fields = self.raw_fields() return self.comment + print_card( list_fields, size=size, is_double=is_double)
def print_raw_card(self, size=8, is_double=False): """A card's raw fields include all defaults for all fields""" list_fields = self.raw_fields() return self.comment + print_card(list_fields, size=size, is_double=is_double)
def write_card_16(self, is_double=False): # type: (int, bool) -> str fields = self.repr_fields() return print_card(fields, size=16, is_double=False)
def print_repr_card(self, size=8, is_double=False): """prints the card in 8/16/16-double format""" list_fields = self.repr_fields() return self.comment + print_card(list_fields, size=size, is_double=is_double)
def print_repr_card(self, size=8, is_double=False): """prints the card in 8/16/16-double format""" return print_card(self.card, size=size, is_double=is_double)
def setup_contact(main_bdf, contact_bdf, contact_surfaces): main_bdf2 = 'main2.bdf' model_main = BDF() model_main.read_bdf(main_bdf) # ------------------------------------------------------------------------ # update the case control deck cc = model_main.case_control_deck subcase_ids = cc.get_subcase_list() subcase_ids.pop(0) subcase0_id = subcase_ids[0] cc.add_parameter_to_local_subcase(subcase0_id, 'FORCE(SORT1, REAL) = ALL') print(cc) model_main.write_bdf('junk.bdf') #sys.exit() # ------------------------------------------------------------------------ # update the main bdf with the INCLUDE fiel contact_include = write_include(contact_bdf) model_main.rejects.append([contact_include]) # ------------------------------------------------------------------------ eid_start = get_max_eid(model_main) + 1 # elements + 1 -> starting ID eid = eid_start # ------------------------------------------------------------------------ # apply springs based on distance with open(contact_bdf, 'w') as f: eid_groups = [] nodes_groups = [] neids = [] i = 0 for contact_surface in contact_surfaces: nids_group1 = contact_surface['group1_nodes'] nids_group2 = contact_surface['group2_nodes'] stiffness = contact_surface['stiffness'] dof = contact_surface['dof'] cid = contact_surface['cid'] print("nids_group1 = %s" % nids_group1) #model_left = BDF(debug=False) #model_left.read_bdf(left_bdf) #model_right = BDF(debug=False) #model_right.read_bdf(right_bdf) # change nodes from cid=0 to cid=N nodes_left = update_nodes_cid(model_main, nids_group1, cid) nodes_right = update_nodes_cid(model_main, nids_group2, cid) # find 5 closest nodes; TODO: update this... spring_sets = { # left # right 1: [2, 4, 5], 2: [10, 3], } c1 = c2 = dof neids_start = eid #i = 0 if i > 0: f.write( '$-------------------------------------------------------\n' ) f.write('$ contact set %i\n' % i) for g1, spring_nodes in sorted(iteritems(spring_sets)): f.write('$ g1=%i, dof=%i\n' % (g1, dof)) for g2 in spring_nodes: celas = ['CELAS2', eid, stiffness, g1, c1, g2, c2] f.write(print_card(celas)) eid += 1 #i += 1 neid = eid - neids_start eid_group = (neids_start, neid) eid_groups.append(eid_group) nodes_group = (nodes_left, nodes_right) nodes_groups.append(nodes_group) neids.append(neid) i += 1 f.close() # ------------------------------------------------------------------------ # we put this at the bottom b/c we need to update the node coordinate systems model_main.write_bdf(main_bdf2) del model_main # ------------------------------------------------------------------------ # set the initial stiffnesses stiffnesses = zeros(sum(neids), 'float64') errors = ones(sum(neids), 'float64') ieid = 0 for neid, contact_surface in zip(neids, contact_surfaces): nids_group1 = contact_surface['group1_nodes'] #nids_group2 = contact_surface['group2_nodes'] stiffness = contact_surface['stiffness'] #dof = contact_surface['dof'] #cid = contact_surface['cid'] #(nids_group1, nids_group2, dof, stiffness, cid, glue, initial_gap, max_deflection_error) = #'group1_nodes' : group1_nodes, #'group2_nodes' : group2_nodes, #'dof' : 1, # dof in contact (1, 2, 3, 4, 5, 6) - nodes in group1/2 #'K' : 1.e8, # stiffness of contact interface #'cid' : 1, # direction of contact #'glue' : False, # #'initial_gap' : 0.0, #'max_deflection_error' : max_deflection_error, #left_bdf, right_bdf, dof, stiffness, cid, glue, initial_gap = contact_surface stiffnesses[ieid:ieid + neid] = stiffness ieid += neid return eid_groups, nodes_groups, stiffnesses, errors