def test_writesector(tmpdir): archive = pyansys.Archive(examples.sector_archive_file) filename = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(filename, archive.grid) archive_new = pyansys.Archive(filename) assert np.allclose(archive.grid.points, archive_new.grid.points) assert np.allclose(archive.grid.cells, archive_new.grid.cells)
def test_read_parm(): filename = os.path.join(testfiles_path, 'parm.cdb') archive = pyansys.Archive(filename) assert len(archive.raw['parameters']) == 0 archive = pyansys.Archive(filename, read_parameters=True) assert len(archive.raw['parameters']) == 2 for parm in archive.raw['parameters']: assert isinstance(archive.raw['parameters'][parm], np.ndarray)
def test_read_parm(): filename = os.path.join(testfiles_path, 'parm.cdb') archive = pyansys.Archive(filename) with pytest.raises(AttributeError): archive.parameters archive = pyansys.Archive(filename, read_parameters=True) assert len(archive.parameters) == 2 for parm in archive.parameters: assert isinstance(archive.parameters[parm], np.ndarray)
def test_writehex(tmpdir): archive = pyansys.Archive(examples.hexarchivefile) grid = archive.parse_vtk() temp_archive = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(temp_archive, grid) archive2 = pyansys.Archive(temp_archive) grid2 = archive2.parse_vtk() assert np.allclose(grid.points, grid2.points) assert np.allclose(grid.cells, grid2.cells)
def test_writehex_missing_node_num(tmpdir): archive = pyansys.Archive(examples.hexarchivefile) grid = archive.parse_vtk() grid.point_arrays['ansys_node_num'][:-1] = -1 temp_archive = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(temp_archive, grid) archive2 = pyansys.Archive(temp_archive) grid2 = archive2.parse_vtk() assert np.allclose(grid.points.shape, grid2.points.shape) assert np.allclose(grid.cells.size, grid2.cells.size)
def test_write_angle(tmpdir): archive = pyansys.Archive(examples.hexarchivefile) try: nblock_filename = str(tmpdir.mkdir("tmpdir").join('nblock.cdb')) except: nblock_filename = '/tmp/nblock.cdb' angles = archive.raw['nodes'][:, 3:] pyansys.write_nblock(nblock_filename, archive.raw['nnum'], archive.raw['nodes'][:, :3], angles) archive2 = pyansys.Archive(nblock_filename) assert np.allclose(archive2.raw['nodes'], archive.raw['nodes'])
def test_invalid_archive(tmpdir): archive = pyansys.Archive(examples.hexarchivefile) # grid = archive.parse_vtk() try: nblock_filename = str(tmpdir.mkdir("tmpdir").join('nblock.cdb')) except: nblock_filename = '/tmp/nblock.cdb' pyansys.write_nblock(nblock_filename, archive.raw['nnum'], archive.raw['nodes'][:, :3]) archive = pyansys.Archive(nblock_filename) with pytest.raises(Exception): archive.parse_vtk()
def test_writehex_missing_elem_num(tmpdir): archive = pyansys.Archive(examples.hexarchivefile) grid = archive.parse_vtk() grid.cell_arrays['ansys_elem_num'][:10] = -1 grid.cell_arrays['ansys_etype'] = np.ones(grid.number_of_cells)*-1 grid.cell_arrays['ansys_elem_type_num'] = np.ones(grid.number_of_cells)*-1 temp_archive = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(temp_archive, grid) archive2 = pyansys.Archive(temp_archive) grid2 = archive2.parse_vtk() assert np.allclose(grid.points.shape, grid2.points.shape) assert np.allclose(grid.cells.size, grid2.cells.size)
def show_hex_archive(off_screen=None): """Displays a hex beam mesh""" # Load an archive file archive = pyansys.Archive(hexarchivefile) archive.plot(off_screen=off_screen, color='w', show_edges=True) assert archive.grid.n_points assert archive.grid.n_cells
def test_invalid_archive(tmpdir, hex_archive): nblock_filename = str(tmpdir.mkdir("tmpdir").join('nblock.cdb')) pyansys.write_nblock(nblock_filename, hex_archive.nnum, hex_archive.nodes) archive = pyansys.Archive(nblock_filename) with pytest.raises(AttributeError): archive.grid
def test_missing_midside(): allowable_types = [45, 95, 185, 186, 92, 187] archive_file = os.path.join(testfiles_path, 'mixed_missing_midside.cdb') archive = pyansys.Archive(archive_file, allowable_types=allowable_types) assert (archive.quality > 0.0).all() assert not np.any(archive.grid.celltypes == VTK_TETRA)
def test_missing_midside(): archive_file = os.path.join(testfiles_path, 'mixed_missing_midside.cdb') archive = pyansys.Archive(archive_file) grid = archive.parse_vtk( allowable_types=['45', '95', '185', '186', '92', '187']) assert (grid.quality > 0.0).all() assert not np.any(grid.celltypes == VTK_TETRA)
def test_invalid_archive(tmpdir, hex_archive): nblock_filename = str(tmpdir.mkdir("tmpdir").join('nblock.cdb')) pyansys.write_nblock(nblock_filename, hex_archive.nnum, hex_archive.nodes) archive = pyansys.Archive(nblock_filename) assert archive.grid is None
def test_write_angle(tmpdir, hex_archive): nblock_filename = str(tmpdir.mkdir("tmpdir").join('nblock.cdb')) pyansys.write_nblock(nblock_filename, hex_archive.nnum, hex_archive.nodes, hex_archive.node_angles) archive = pyansys.Archive(nblock_filename, parse_vtk=False) assert np.allclose(archive.nodes, hex_archive.nodes)
def test_writehex(tmpdir, hex_archive): temp_archive = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(temp_archive, hex_archive.grid) archive_new = pyansys.Archive(temp_archive) assert np.allclose(hex_archive.grid.points, archive_new.grid.points) assert np.allclose(hex_archive.grid.cells, archive_new.grid.cells)
def test_write_component(tmpdir): items = np.array([1, 20, 50, 51, 52, 53]) temp_archive = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) comp_name = 'TEST' pyansys.write_cmblock(temp_archive, items, comp_name, 'node') archive = pyansys.Archive(temp_archive) assert np.allclose(archive.node_components[comp_name], items)
def test_read_wb_nblock(): expected = np.array([[9.89367578e-02, -8.07092192e-04, 8.53764953e+00], [9.65803244e-02, 2.00906704e-02, 8.53744951e+00], [9.19243555e-02, 3.98781615e-02, 8.53723652e+00]]) filename = os.path.join(testfiles_path, 'workbench_193.cdb') archive = pyansys.Archive(filename) assert np.allclose(archive.nodes, expected) assert np.allclose(archive.node_angles, 0)
def show_hex_archive(off_screen=None): """ Displays a hex beam mesh """ # Load an archive file archive = pyansys.Archive(hexarchivefile) grid = archive.parse_vtk() grid.plot(off_screen=off_screen) assert grid.n_points assert grid.n_cells
def test_writehex_missing_node_num(tmpdir, hex_archive): hex_archive.grid.point_arrays['ansys_node_num'][:-1] = -1 temp_archive = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(temp_archive, hex_archive.grid) archive_new = pyansys.Archive(temp_archive) assert np.allclose(hex_archive.grid.points.shape, archive_new.grid.points.shape) assert np.allclose(hex_archive.grid.cells.size, archive_new.grid.cells.size)
def test_write_lin_archive(tmpdir, celltype): archive_file = os.path.join(testfiles_path, 'all_solid_cells.cdb') archive = pyansys.Archive(archive_file) linear_grid = archive.parse_vtk(force_linear=True) mask = linear_grid.celltypes == celltype assert mask.any() linear_grid = linear_grid.extract_cells(mask) try: tmp_archive_file = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) except: tmp_archive_file = '/tmp/nblock.cdb' pyansys.save_as_archive(tmp_archive_file, linear_grid) new_archive = pyansys.Archive(tmp_archive_file) new_linear_grid = new_archive.parse_vtk() assert np.allclose(linear_grid.cells, new_linear_grid.cells) assert np.allclose(linear_grid.points, new_linear_grid.points)
def solve_km(): """ Loads and solves a mass and stiffness matrix from an ansys full file """ try: from scipy.sparse import linalg from scipy import sparse except ImportError: print('scipy not installed, aborting') return # load the mass and stiffness matrices full = pyansys.read_binary(pyansys.examples.fullfile) dofref, k, m = full.load_km(sort=True) # make symmetric k += sparse.triu(k, 1).T m += sparse.triu(m, 1).T k += sparse.diags(np.random.random(k.shape[0]) / 1E20, shape=k.shape) # Solve w, v = linalg.eigsh(k, k=20, M=m, sigma=1000) # System natural frequencies f = (np.real(w))**0.5 / (2 * np.pi) # %% Plot result # Get the 4th mode shape full_mode_shape = v[:, 3] # x, y, z displacement for each node # reshape and compute the normalized displacement disp = full_mode_shape.reshape((-1, 3)) n = (disp * disp).sum(1)**0.5 n /= n.max() # normalize # load an archive file and create a vtk unstructured grid archive = pyansys.Archive(pyansys.examples.hexarchivefile) grid = archive.parse_vtk() # Fancy plot the displacement plobj = pv.Plotter() # add two meshes to the plotting class plobj.add_mesh(grid.copy(), color='w', style='wireframe') plobj.add_mesh(grid, scalars=n, stitle='Normalized\nDisplacement', flip_scalars=True, cmap='jet') # Update the coordinates by adding the mode shape to the grid plobj.update_coordinates(grid.points + disp / 80, render=False) plobj.add_text('Cantliver Beam 4th\nMode Shape at\n{:.4f}'.format(f[3]), font_size=30) plobj.plot()
def test_read_hypermesh(): expected = np.array([[-6.01203, 2.98129, 2.38556], [-3.03231, 2.98067, 2.38309], [-0.03485, 2.98004, 2.3805], [2.98794, 2.97941, 2.37773], [5.98956, 2.97878, 2.37488], [5.98956, 5.97878, 2.37488]]) filename = os.path.join(testfiles_path, 'hypermesh.cdb') archive = pyansys.Archive(filename, verbose=True) assert np.allclose(archive.nodes[:6], expected)
def test_read_complex_archive(): archive_file = os.path.join(testfiles_path, 'all_solid_cells.cdb') archive = pyansys.Archive(archive_file) grid = archive.parse_vtk() assert grid.number_of_cells == 4 assert np.unique(grid.celltypes).size == 4 assert np.all(grid.celltypes > 20) assert np.all(grid.quality > 0.0) linear_grid = archive.parse_vtk(force_linear=True) assert np.all(linear_grid.celltypes < 20) assert np.all(linear_grid.quality > 0.0)
def test_write_quad_complex_archive(tmpdir, celltype): # celltype = 24 archive_file = os.path.join(testfiles_path, 'all_solid_cells.cdb') archive = pyansys.Archive(archive_file) grid = archive.parse_vtk() mask = grid.celltypes == celltype assert mask.any() grid = grid.extract_cells(mask) try: tmp_archive_file = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) except: tmp_archive_file = '/tmp/nblock.cdb' pyansys.save_as_archive(tmp_archive_file, grid) new_archive = pyansys.Archive(tmp_archive_file) new_grid = new_archive.parse_vtk() assert np.allclose(grid.cells, new_grid.cells) assert np.allclose(grid.points, new_grid.points) assert (new_grid.quality > 0.0).all()
def test_writehex_missing_elem_num(tmpdir, hex_archive): grid = hex_archive.grid grid.cell_arrays['ansys_elem_num'][:10] = -1 grid.cell_arrays['ansys_etype'] = np.ones(grid.number_of_cells)*-1 grid.cell_arrays['ansys_elem_type_num'] = np.ones(grid.number_of_cells)*-1 filename = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(filename, grid) archive_new = pyansys.Archive(filename) assert np.allclose(hex_archive.grid.points, archive_new.grid.points) assert np.allclose(hex_archive.grid.cells, archive_new.grid.cells)
def test_parse_vtk(): archive = pyansys.Archive(examples.hexarchivefile) grid = archive.parse_vtk() assert grid.points.size assert grid.cells.size assert 'ansys_node_num' in grid.point_arrays assert np.all(grid.quality > 0) with pytest.raises(Exception): archive.parse_vtk(allowable_types=186) with pytest.raises(Exception): archive.parse_vtk(allowable_types=[1, 2, 3])
def test_write_lin_archive(tmpdir, celltype, all_solid_cells_archive_linear): linear_grid = all_solid_cells_archive_linear.grid mask = linear_grid.celltypes == celltype assert mask.any() linear_grid = linear_grid.extract_cells(mask) tmp_archive_file = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) pyansys.save_as_archive(tmp_archive_file, linear_grid) new_archive = pyansys.Archive(tmp_archive_file) assert new_archive.quality > 0 assert np.allclose(linear_grid.celltypes, new_archive.grid.celltypes)
class TestLoadArchive182_183(): filename = os.path.join(testfiles_path, 'archive.cdb') archive = pyansys.Archive(filename) assert archive.raw['nnum'].size == 12484 assert archive.raw['elem'].shape[0] == 6000 def test_parse(self): nnode = self.archive.raw['nnum'].size nelem = self.archive.raw['elem'].shape[0] grid = self.archive.parse_vtk() assert grid.n_points == nnode assert grid.n_cells == nelem assert np.sum(grid.celltypes == 9) == 3000 assert np.sum(grid.celltypes == 23) == 3000 assert np.allclose(self.archive.raw['nodes'][:, :3], grid.points)
def test_write_quad_complex_archive(tmpdir, celltype, all_solid_cells_archive): grid = all_solid_cells_archive.grid mask = grid.celltypes == celltype assert mask.any() grid = grid.extract_cells(mask) try: tmp_archive_file = str(tmpdir.mkdir("tmpdir").join('tmp.cdb')) except: tmp_archive_file = '/tmp/nblock.cdb' pyansys.save_as_archive(tmp_archive_file, grid) new_archive = pyansys.Archive(tmp_archive_file) assert np.allclose(grid.cells, new_archive.grid.cells) assert np.allclose(grid.points, new_archive.grid.points) assert (new_archive.quality > 0.0).all()
class TestResultReader(object): result = pyansys.read_binary(examples.rstfile) archive = pyansys.Archive(examples.hexarchivefile) def test_geometry_elements(self): r_elem = self.result.geometry['elem'][self.result.sidx_elem] a_elem = self.archive.raw['elem'] assert np.allclose(r_elem, a_elem) def test_geometry_nodes(self): r_node = self.result.geometry['nodes'] a_node = self.archive.raw['nodes'] assert np.allclose(r_node, a_node) def test_geometry_nodenum(self): r_values = self.result.geometry['nnum'] a_values = self.archive.raw['nnum'] assert np.allclose(r_values, a_values) def test_results_displacement(self): textfile = os.path.join(testfiles_path, 'prnsol_u.txt') nnum, r_values = self.result.nodal_solution(0) a_values = np.loadtxt(textfile, skiprows=2)[:, 1:4] assert np.allclose(r_values, a_values) def test_results_stress(self): r_nnum, r_values = self.result.nodal_stress(0) textfile = os.path.join(testfiles_path, 'prnsol_s.txt') a_values = np.loadtxt(textfile, skiprows=2)[:, 1:] # ignore nan nanmask = ~np.isnan(r_values).any(1) assert np.allclose(r_values[nanmask], a_values, atol=1E-1) def test_results_pstress(self): r_nnum, r_values = self.result.principal_nodal_stress(0) textfile = os.path.join(testfiles_path, 'prnsol_s_prin.txt') a_values = np.loadtxt(textfile, skiprows=2)[:, 1:] # ignore nan nanmask = ~np.isnan(r_values).any(1) assert np.allclose(r_values[nanmask], a_values, atol=100)