def test_cut_plate_eids(self): """recover element ids""" log = SimpleLogger(level='warning', encoding='utf-8', log_func=None) bdf_filename = os.path.join(MODEL_PATH, 'plate_py', 'plate_py.dat') model = read_bdf(bdf_filename, log=log) nnodes = len(model.nodes) nodal_result = np.ones(nnodes) coord = CORD2R(1, rid=0, origin=[0., 0., 0.], zaxis=[0., 0., 1], xzplane=[1., 0., 0.], comment='') model.coords[1] = coord ytol = 2. unique_geometry_array, unique_results_array, unused_rods = cut_face_model_by_coord( bdf_filename, coord, ytol, nodal_result, plane_atol=1e-5, skip_cleanup=True, csv_filename='cut_face.csv') #print(unique_geometry_array) #print(unique_results_array) unique_geometry_array = np.array(unique_geometry_array) unique_results_array = np.array(unique_results_array) assert unique_geometry_array.shape == (1, 40, 4), unique_geometry_array.shape assert unique_results_array.shape == (1, 40, 7), unique_results_array.shape unique_geometry_array = unique_geometry_array[0, :, :] unique_results_array = unique_results_array[0, :, :] assert unique_geometry_array.shape == (40, 4), unique_geometry_array.shape assert unique_results_array.shape == (40, 7), unique_results_array.shape
def test_cut_shell_model_1(self): """ tests: - cut_edge_model_by_coord - cut_face_model_by_coord """ model, nodal_result = _cut_shell_model_quads() coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') model.coords[1] = coord tol = 2. #------------------------------------------------------------------------- title = 'result' p1 = None p2 = None zaxis = None cut_and_plot_model(title, p1, p2, zaxis, model, coord, nodal_result, model.log, tol, plane_atol=1e-5, csv_filename=None, invert_yaxis=False, cut_type='edge', plot=IS_MATPLOTLIB, show=False) #========================================================================= out = cut_edge_model_by_coord(model, coord, tol, nodal_result, plane_atol=1e-5) unused_local_points_array, unused_global_points_array, result_array = out assert len(result_array) == 16, len(result_array) unused_geometry_array, result_array, unused_rods = cut_face_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5) result_array = np.array(result_array) assert result_array.shape == (1, 8, 7), result_array.shape os.remove('plane_edge.bdf') os.remove('plane_face.bdf')
def test_cut_shell_model_1(self): """ tests: - cut_edge_model_by_coord - cut_face_model_by_coord """ model, nodal_result = _cut_shell_model_quads() coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') model.coords[1] = coord tol = 2. #------------------------------------------------------------------------- title = 'result' p1 = None p2 = None zaxis = None cut_and_plot_model(title, p1, p2, zaxis, model, coord, nodal_result, model.log, tol, plane_atol=1e-5, csv_filename=None, invert_yaxis=False, cut_type='edge', show=False) #========================================================================= local_points_array, global_points_array, result_array = cut_edge_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5) assert len(result_array) == 16, len(result_array) geometry_array, result_array = cut_face_model_by_coord(model, coord, tol, nodal_result, plane_atol=1e-5) assert result_array is None, len(result_array) # no quad support os.remove('plane_edge.bdf') os.remove('plane_face.bdf')
def test_cut_shell_model_2(self): """ tests: - cut_edge_model_by_coord - cut_face_model_by_coord """ tol = 2. coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') model, nodal_result = _cut_shell_model_quads() #------------------------------------------------------------------------- # triangles split_to_trias(model) model.coords[1] = coord model.write_bdf('tris.bdf') #print('----------------------------') title = 'result' p1 = None p2 = None zaxis = None cut_and_plot_model(title, p1, p2, zaxis, model, coord, nodal_result, model.log, tol, plane_atol=1e-5, csv_filename=None, invert_yaxis=False, cut_type='edge', plot=IS_MATPLOTLIB, show=False) out = cut_edge_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5, csv_filename='cut_edge_2.csv') unused_local_points_array, unused_global_points_array, result_array = out assert len(result_array) == 20, len(result_array) unused_geometry_arrays, result_arrays, unused_rods = cut_face_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5, csv_filename='cut_face_2.csv') assert len(result_arrays[0]) == 8, len(result_arrays) os.remove('tris.bdf') os.remove('cut_edge_2.csv') os.remove('cut_face_2.csv') os.remove('plane_edge.bdf') os.remove('plane_face.bdf')
def test_cut_shell_model_2(self): """tests pierce_shell_model""" tol = 2. coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') model, nodal_result = _cut_shell_model_quads() #------------------------------------------------------------------------- # triangles elements2 = {} neids = len(model.elements) for eid, elem in iteritems(model.elements): elem_a, elem_b = elem.split_to_ctria3(eid, eid + neids) elements2[elem_a.eid] = elem_a elements2[elem_b.eid] = elem_b model.elements = elements2 print(elements2) model.coords[1] = coord model.write_bdf('tris.bdf') print('----------------------------') local_points_array, global_points_array, result_array = cut_edge_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5, csv_filename='cut_edge_2.csv') assert len(result_array) == 20, len(result_array) geometry_arrays, result_arrays = cut_face_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5, csv_filename='cut_face_2.csv') assert len(result_arrays[0]) == 8, len(result_arrays)
def test_cut_shell_model_1(self): """tests pierce_shell_model""" model, nodal_result = _cut_shell_model_quads() coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') model.coords[1] = coord tol = 2. #------------------------------------------------------------------------- local_points_array, global_points_array, result_array = cut_edge_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5) assert len(result_array) == 16, len(result_array) geometry_array, result_array = cut_face_model_by_coord(model, coord, tol, nodal_result, plane_atol=1e-5) assert result_array is None, len(result_array) # no quad support
def cut_and_plot_model(title, p1, p2, zaxis, model, coord, nodal_result, log, ytol, plane_atol=1e-5, csv_filename=None, invert_yaxis=False, cut_type='edge', show=True): """ Cuts a Nastran model with a cutting plane Parameters ---------- title : str the title for the plot p1 : ??? ??? p2 : ??? ??? zaxis : ??? ??? model : str / BDF str : the bdf filename model : a properly configurated BDF object coord : Coord the coordinate system to cut the model with nodal_result : (nelements, ) float np.ndarray the result to cut the model with log : logger a logging object ytol : float the tolerance to filter edges (using some large value) to prevent excessive computations plane_atol : float; default=1e-5 the tolerance for a line that's located on the y=0 local plane csv_filename : str; default=None None : don't write a csv str : write a csv """ assert cut_type.lower() in ['edge' ], 'cut_type=%r and must be edge' % cut_type cut_type = cut_type.lower() if cut_type == 'edge': # bar cutting version csv_filename_edge = None #if csv_filename: #csv_filename_edge = csv_filename + '_edge.csv' local_points_array, global_points_array, result_array = cut_edge_model_by_coord( model, coord, ytol, nodal_result, plane_atol=plane_atol, csv_filename=csv_filename_edge) if len(local_points_array) == 0: log.error('No elements were piereced. Check your cutting plane.') return plot_cutting_plane_edges(title, p1, p2, zaxis, local_points_array, global_points_array, result_array, csv_filename=csv_filename, invert_yaxis=invert_yaxis, show=show) elif cut_type == 'face': csv_filename_face = None if csv_filename: csv_filename_face = csv_filename + '_face.csv' geometry_arrays, results_arrays = cut_face_model_by_coord( model, coord, ytol, nodal_result, plane_atol=plane_atol, csv_filename=csv_filename_face) plot_cutting_plane_faces(title, p1, p2, zaxis, geometry_arrays, results_arrays, csv_filename=csv_filename, invert_yaxis=invert_yaxis, show=show) else: # pragma: no cover raise NotImplementedError('cut_type=%r' % cut_type)
def test_cut_bwb(self): """recover element ids""" log = SimpleLogger(level='warning', encoding='utf-8', log_func=None) is_bwb = True if is_bwb: bdf_filename = os.path.join(MODEL_PATH, 'bwb', 'bwb_saero.bdf') # ymax~=1262.0 else: # pragma: no cover bdf_filename = r'C:\NASA\asm\all_modes_mach_0.85\flutter.bdf' # ymax=1160.601 normal_plane = np.array([0., 1., 0.]) model = read_bdf(bdf_filename, log=log) model2 = read_bdf(bdf_filename, log=log) # initialize theta thetas = {} for eid in model.elements: # theta, Ex, Ey, Gxy thetas[eid] = (0., 0., 0., 0.) #p1 = np.array([466.78845, 735.9053, 0.0]) #p2 = np.array([624.91345, 639.68896, -0.99763656]) #dx = p2 - p1 ytol = 2. nodal_result = None plane_bdf_filenames = [] y = [] A = [] I = [] EI = [] avg_centroid = [] for i in range(2000): if is_bwb: dy = 100. * i + 1. # bwb coord = CORD2R(1, rid=0, origin=[0., dy, 0.], zaxis=[0., dy, 1], xzplane=[1., dy, 0.]) else: # pragma: no cover dy = 4. * i + 1. # CRM coord = CORD2R(1, rid=0, origin=[0., dy, 0.], zaxis=[0., dy, 1], xzplane=[1., dy, 0.]) #origin = np.array([0., dy, 0.]) #xzplane = origin + dx #xzplane = np.array([1., dy, 0.]) #coord = CORD2R.add_axes(cid, rid=0, origin=p1, xaxis=p2-p1, yaxis=None, zaxis=None, #xyplane=None, yzplane=None, xzplane=None, comment='') print(coord) model.coords[1] = coord plane_bdf_filename = 'plane_face_%i.bdf' % i cut_face_filename = 'cut_face_%i.csv' % i if os.path.exists(cut_face_filename): os.remove(cut_face_filename) try: out = cut_face_model_by_coord( model2, coord, ytol, nodal_result, plane_atol=1e-5, skip_cleanup=True, #csv_filename=cut_face_filename, csv_filename=None, #plane_bdf_filename=None) plane_bdf_filename=plane_bdf_filename, plane_bdf_offset=dy) except RuntimeError: # incorrect ivalues=[0, 1, 2]; dy=771. for CRM continue unused_unique_geometry_array, unused_unique_results_array, rods = out if not os.path.exists(plane_bdf_filename): break plane_bdf_filenames.append(plane_bdf_filename) # eid, nid, inid1, inid2 #print(unique_geometry_array) #moi_filename = 'amoi_%i.bdf' % i moi_filename = None out = calculate_area_moi(model, rods, normal_plane, thetas, moi_filename=moi_filename) #print(out) Ai, Ii, EIi, avg_centroidi = out y.append(dy) A.append(Ai) I.append(Ii) EI.append(EIi) avg_centroid.append(avg_centroidi) #break with open('thetas.csv', 'w') as csv_filename: csv_filename.write('# eid(%i),theta,Ex,Ey,Gxy\n') for eid, (theta, Ex, Ey, Gxy) in sorted(thetas.items()): csv_filename.write('%i,%f,%f,%f,%f\n' % (eid, theta, Ex, Ey, Gxy)) y = np.array(y, dtype='float64') A = np.array(A, dtype='float64') I = np.array(I, dtype='float64') EI = np.array(EI, dtype='float64') avg_centroid = np.array(avg_centroid, dtype='float64') inid = 1 beam_model = BDF(debug=False) avg_centroid[:, 1] = y # wrong mid = 1 E = 3.0e7 G = None nu = 0.3 model.add_mat1(mid, E, G, nu, rho=0.1) Ix = I[:, 0] Iy = I[:, 1] Ixy = I[:, 2] J = Ix + Iy #i1, i2, i12 = Ix, Iy, Ixy for inid, xyz in enumerate(avg_centroid): beam_model.add_grid(inid + 1, xyz) for eid in range(1, len(A)): pid = eid nids = [eid, eid + 1] x = [1., 0., 0.] g0 = None beam_model.add_cbeam(eid, pid, nids, x, g0, offt='GGG', bit=None, pa=0, pb=0, wa=None, wb=None, sa=0, sb=0, comment='') # j = i1 + i2 so = ['YES', 'YES'] xxb = [0., 1.] area = [A[eid - 1], A[eid]] i1 = [Ix[eid - 1], Ix[eid]] i2 = [Iy[eid - 1], Iy[eid]] i12 = [Ixy[eid - 1], Ixy[eid]] j = [J[eid - 1], J[eid]] beam_model.add_pbeam(pid, mid, xxb, so, area, i1, i2, i12, j, nsm=None, c1=None, c2=None, d1=None, d2=None, e1=None, e2=None, f1=None, f2=None, k1=1., k2=1., s1=0., s2=0., nsia=0., nsib=None, cwa=0., cwb=None, m1a=0., m2a=None, m1b=0., m2b=None, n1a=0., n2a=None, n1b=0., n2b=None, comment='') beam_model.write_bdf('equivalent_beam_model.bdf') X = np.vstack([y, A]).T Y = np.hstack([X, I, EI, avg_centroid]) header = 'y, A, Ix, Iz, Ixz, Ex*Ix, Ex*Iz, Ex*Ixz, xcentroid, ycentroid, zcentroid' np.savetxt('cut_data_vs_span.csv', Y, header=header, delimiter=',') show = True #show = False if IS_MATPLOTLIB: plot_inertia(y, A, I, EI, avg_centroid, show=show)
def test_cut_shell_model(self): """tests pierce_shell_model""" pid = 10 mid1 = 100 model = BDF(log=log) # intersects (min) model.add_grid(1, [0., 0., 0.]) model.add_grid(2, [1., 0., 0.]) model.add_grid(3, [1., 1., 0.]) model.add_grid(4, [0., 1., 0.]) model.add_cquad4(1, pid, [1, 2, 3, 4]) # intersects (max) model.add_grid(5, [0., 0., 1.]) model.add_grid(6, [1., 0., 1.]) model.add_grid(7, [1., 1., 1.]) model.add_grid(8, [0., 1., 1.]) model.add_cquad4(2, pid, [5, 6, 7, 8]) # intersects (mid) model.add_grid(9, [0., 0., 0.5]) model.add_grid(10, [1., 0., 0.5]) model.add_grid(11, [1., 1., 0.5]) model.add_grid(12, [0., 1., 0.5]) model.add_cquad4(3, pid, [9, 10, 11, 12]) # doesn't intersect model.add_grid(13, [10., 0., 0.]) model.add_grid(14, [11., 0., 0.]) model.add_grid(15, [11., 1., 0.]) model.add_grid(16, [10., 1., 0.]) model.add_cquad4(4, pid, [13, 14, 15, 16]) model.add_pshell(pid, mid1=mid1, t=2.) E = 1.0 G = None nu = 0.3 model.add_mat1(mid1, E, G, nu, rho=1.0) model.validate() model.cross_reference() xyz_points = [ [0.4, 0.6, 0.], [-1., -1, 0.], ] tol = 2. coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') nodal_result = np.linspace(0., 1., num=16) local_points_array, global_points_array, result_array = cut_edge_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5) assert len(result_array) == 16, len(result_array) local_points_array, global_points_array, result_array = cut_face_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5) assert len(result_array) == 0, len(result_array) # no quad support
def cut_and_plot_moi(bdf_filename: str, normal_plane: np.ndarray, log: SimpleLogger, dys: List[float], coords: List[CORD2R], ytol: float=2.0, dirname: str='', plot: bool=True, show: bool=False) -> Tuple[Any, Any, Any, Any, Any]: # y, A, I, EI, avg_centroid model = read_bdf(bdf_filename, log=log) model2 = read_bdf(bdf_filename, log=log) # initialize theta thetas = {} for eid in model.elements: # theta, Ex, Ey, Gxy thetas[eid] = (0., 0., 0., 0.) #p1 = np.array([466.78845, 735.9053, 0.0]) #p2 = np.array([624.91345, 639.68896, -0.99763656]) #dx = p2 - p1 nodal_result = None plane_bdf_filenames = [] y = [] A = [] I = [] J = [] EI = [] GJ = [] avg_centroid = [] for i, dy, coord in zip(count(), dys, coords): model.coords[1] = coord plane_bdf_filename = os.path.join(dirname, f'plane_face_{i:d}.bdf') cut_face_filename = os.path.join(dirname, f'cut_face_{i:d}.csv') if os.path.exists(cut_face_filename): os.remove(cut_face_filename) try: out = cut_face_model_by_coord( model2, coord, ytol, nodal_result, plane_atol=1e-5, skip_cleanup=True, #csv_filename=cut_face_filename, csv_filename=None, #plane_bdf_filename=None) plane_bdf_filename=plane_bdf_filename, plane_bdf_offset=dy) except RuntimeError: # incorrect ivalues=[0, 1, 2]; dy=771. for CRM continue unused_unique_geometry_array, unused_unique_results_array, rods = out if not os.path.exists(plane_bdf_filename): break plane_bdf_filenames.append(plane_bdf_filename) # eid, nid, inid1, inid2 #print(unique_geometry_array) #moi_filename = 'amoi_%i.bdf' % i moi_filename = None out = calculate_area_moi(model, rods, normal_plane, thetas, moi_filename=moi_filename) #print(out) Ai, Ii, EIi, avg_centroidi = out #Ai, Ii, Ji, EIi, GJi, avg_centroidi = out Ji = GJi = 1.0 y.append(dy) A.append(Ai) I.append(Ii) J.append(Ji) EI.append(EIi) GJ.append(GJi) avg_centroid.append(avg_centroidi) #break thetas_csv_filename = os.path.join(dirname, 'thetas.csv') with open(thetas_csv_filename, 'w') as csv_filename: csv_filename.write('# eid(%i),theta,Ex,Ey,Gxy\n') for eid, (theta, Ex, Ey, Gxy) in sorted(thetas.items()): csv_filename.write('%i,%f,%f,%f,%f\n' % (eid, theta, Ex, Ey, Gxy)) y = np.array(y, dtype='float64') A = np.array(A, dtype='float64') I = np.array(I, dtype='float64') J = np.array(J, dtype='float64') EI = np.array(EI, dtype='float64') GJ = np.array(GJ, dtype='float64') avg_centroid = np.array(avg_centroid, dtype='float64') inid = 1 beam_model = BDF(debug=False) avg_centroid[:, 1] = y # wrong mid = 1 E = 3.0e7 G = None nu = 0.3 model.add_mat1(mid, E, G, nu, rho=0.1) # 0 1 2 3 4 5 # [Ixx, Iyy, Izz, Ixy, Iyz, Ixz] Ix = I[:, 0] Iy = I[:, 1] Iz = I[:, 2] Ixz = I[:, 5] J = Ix + Iz #i1, i2, i12 = Ix, Iy, Ixy for inid, xyz in enumerate(avg_centroid): beam_model.add_grid(inid+1, xyz) for eid in range(1, len(A)): pid = eid nids = [eid, eid + 1] x = [1., 0., 0.] g0 = None beam_model.add_cbeam(eid, pid, nids, x, g0, offt='GGG', bit=None, pa=0, pb=0, wa=None, wb=None, sa=0, sb=0, comment='') # j = i1 + i2 so = ['YES', 'YES'] xxb = [0., 1.] area = [A[eid-1], A[eid]] i1 = [Ix[eid-1], Ix[eid]] i2 = [Iz[eid-1], Iz[eid]] i12 = [Ixz[eid-1], Ixz[eid]] j = [J[eid-1], J[eid]] beam_model.add_pbeam(pid, mid, xxb, so, area, i1, i2, i12, j, nsm=None, c1=None, c2=None, d1=None, d2=None, e1=None, e2=None, f1=None, f2=None, k1=1., k2=1., s1=0., s2=0., nsia=0., nsib=None, cwa=0., cwb=None, m1a=0., m2a=0., m1b=None, m2b=None, n1a=0., n2a=0., n1b=None, n2b=None, comment='') beam_model_bdf_filename = os.path.join(dirname, 'equivalent_beam_model.bdf') beam_model.write_bdf(beam_model_bdf_filename) X = np.vstack([y, A]).T Y = np.hstack([X, I, EI, avg_centroid]) header = 'y, A, Ix, Iz, Ixz, Ex*Ix, Ex*Iz, Ex*Ixz, xcentroid, ycentroid, zcentroid' cut_data_span_filename = os.path.join(dirname, 'cut_data_vs_span.csv') np.savetxt(cut_data_span_filename, Y, header=header, delimiter=',') if IS_MATPLOTLIB and (plot or show): plot_inertia(y, A, I, J, EI, GJ, avg_centroid, show=show, dirname=dirname) else: plane_bdf_filenames = [] return y, A, I, J, EI, GJ, avg_centroid, plane_bdf_filenames
def test_cut_shell_model_2(self): """ tests: - cut_edge_model_by_coord - cut_face_model_by_coord """ tol = 2. coord = CORD2R(1, rid=0, origin=[0.5, 0., 0.], zaxis=[0.5, 0., 1], xzplane=[1.5, 0., 0.], comment='') model, nodal_result = _cut_shell_model_quads() #------------------------------------------------------------------------- # triangles elements2 = {} neids = len(model.elements) for eid, elem in model.elements.items(): elem_a, elem_b = elem.split_to_ctria3(eid, eid + neids) elements2[elem_a.eid] = elem_a elements2[elem_b.eid] = elem_b model.elements = elements2 model.coords[1] = coord model.write_bdf('tris.bdf') #print('----------------------------') title = 'result' p1 = None p2 = None zaxis = None cut_and_plot_model(title, p1, p2, zaxis, model, coord, nodal_result, model.log, tol, plane_atol=1e-5, csv_filename=None, invert_yaxis=False, cut_type='edge', show=False) local_points_array, global_points_array, result_array = cut_edge_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5, csv_filename='cut_edge_2.csv') assert len(result_array) == 20, len(result_array) geometry_arrays, result_arrays = cut_face_model_by_coord( model, coord, tol, nodal_result, plane_atol=1e-5, csv_filename='cut_face_2.csv') assert len(result_arrays[0]) == 8, len(result_arrays) os.remove('tris.bdf') os.remove('cut_edge_2.csv') os.remove('cut_face_2.csv') os.remove('plane_edge.bdf') os.remove('plane_face.bdf')