class TestLoad181(): filename = os.path.join(testfiles_path, 'shell181.rst') result = pyansys.ResultReader(filename) def test_load(self): assert np.any(self.result.grid.cells) assert np.any(self.result.grid.points) def test_elementstress(self): element_stress, elemnum, enode = self.result.ElementStress(0) element0 = element_stress[0] # ansys prints both postiive and negative component values if np.sign(element0[0][0]) != np.sign(ANSYS_ELEM[0][0]): element0 *= -1 # wide atol limits considering the 5 sigfig from ASCII tables assert np.allclose(element0, np.array(ANSYS_ELEM), atol=1E-6) def test_nodalstress(self): nnum, stress = self.result.NodalStress(0) # element0 = element_stress[0] if np.sign(stress[0][0]) != np.sign(ANSYS_NODE[0][0]): stress *= -1 # wide atol limits considering the 5 sigfig from ASCII tables assert np.allclose(stress, np.array(ANSYS_NODE), atol=1E-6)
def results(self): """ Returns a binary interface to the result file """ resultfile = os.path.join(self.path, '%s.rst' % self.jobname) if not os.path.isfile(resultfile): raise Exception('No results found at %s' % resultfile) return pyansys.ResultReader(resultfile)
def DisplayStress(): """ Load and plot 1st bend of a hexahedral beam """ # get location of this file result = pyansys.ResultReader(rstfile) print('Displaying node averaged stress in x direction for Mode 6') result.PlotNodalStress(5, 'Sx')
def DisplayDisplacement(): """ Load and plot 1st bend of a hexahedral beam """ # get location of this file fobj = pyansys.ResultReader(rstfile) print('Displaying ANSYS Mode 1') fobj.PlotNodalSolution(0, label='Displacement')
def test_animate_nodal_solution(tmpdir): result = pyansys.ResultReader(rstfile) temp_movie = str(tmpdir.mkdir("tmpdir").join('tmp.mp4')) result.AnimateNodalSolution(0, nangles=20, movie_filename=temp_movie, interactive=False) assert np.any(result.grid.points) assert os.path.isfile(temp_movie)
class TestResultReader(object): result = pyansys.ResultReader(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.NodalSolution(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.NodalStress(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.PrincipalNodalStress(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)
def test_loadresult(): result = pyansys.ResultReader(rstfile) # check result is loaded assert result.nsets assert result.nnum.size # check geometry is genreated grid = result.grid assert grid.points.size assert grid.cells.size assert 'ansys_node_num' in grid.point_arrays # check results can be loaded nnum, disp = result.NodalSolution(0) assert nnum.size assert disp.size nnum, disp = result.NodalSolution(0) assert nnum.size assert disp.size nnum, disp = result.PrincipalNodalStress(0) assert nnum.size assert disp.size nnum, disp = result.NodalStress(0) assert nnum.size assert disp.size element_stress, enum, enode = result.ElementStress(0) assert element_stress[0].size assert enum.size assert enode[0].size element_stress, enum, enode = result.ElementStress(0, principal=True) assert element_stress[0].size assert enum.size assert enode[0].size
def LoadResult(): """ Loads a result file and prints out the displacement of all the nodes from a modal analysis. """ # Load result file result = pyansys.ResultReader(rstfile) print('Loaded result file with {:d} result sets'.format(result.nsets)) print('Contains {:d} nodes'.format(len(result.nnum))) # display result nnum, disp = result.NodalSolution(0) print('Nodal displacement for nodes 30 to 40 is:') for i in range(29, 40): node = result.nnum[i] x = disp[i, 0] y = disp[i, 1] z = disp[i, 2] print('{:2d} {:10.6f} {:10.6f} {:10.6f}'.format(node, x, y, z))
grid = vtkInterface.LoadGrid('hex.vtk') grid.Plot() #============================================================================== # load beam results #============================================================================== # Load the reader from pyansys import pyansys from pyansys import examples # Sample result file and associated archive file rstfile = examples.rstfile hexarchivefile = examples.hexarchivefile # Create result reader object by loading the result file result = pyansys.ResultReader(rstfile) # Get beam natural frequencies freqs = result.GetTimeValues() # Get the node numbers in this result file nnum = result.nnum # Get the 1st bending mode shape. Nodes are ordered according to nnum. disp = result.GetResult(0, True) # uses 0 based indexing # Load CDB (necessary for display) result.LoadArchive(hexarchivefile) # Plot the displacement of Mode 0 in the x direction result.PlotNodalResult(0, 'x', label='Displacement')
def test_loadbeam(): linkresult = os.path.join(testfiles_path, 'link1.rst') result = pyansys.ResultReader(linkresult) assert np.any(result.grid.cells)
import sys import pyansys if len(sys.argv) < 2: print("Enter rst file name with the command: python3 rst2VTK.py file.rst") sys.exit() infile = sys.argv[1] outfile = infile + '.vtk' result = pyansys.ResultReader(infile) grid = result.grid.copy() # check binary_reader.py at source code of pyansys for i in range(result.nsets): _, val = result.NodalSolution(i) grid.point_arrays['NodalSolution{:03d}'.format(i)] = val nodenum = result.grid.point_arrays['ansys_node_num'] _, stress = result.NodalStress(i) grid.point_arrays['NodalStress{:03d}'.format(i)] = stress grid.save(outfile) #pyansys fromPyPI is required. #If pyansys is built from source code, cython module is necessary
ANSYS_ELEM = [ [0.17662E-07, 79.410, -11.979, -0.11843E-02, 4.8423, -0.72216E-04], [0.20287E-07, 91.212, 27.364, -0.13603E-02, 4.8423, -0.72216E-04], [0.20287E-07, 91.212, 27.364, -0.13603E-02, -4.8423, 0.72216E-04], [0.17662E-07, 79.410, -11.979, -0.11843E-02, -4.8423, 0.72216E-04] ] ANSYS_NODE = [ [0.20287E-07, 91.212, 27.364, -0.13603E-02, 4.8423, -0.72216E-04], [0.17662E-07, 79.410, -11.979, -0.11843E-02, 4.8423, -0.72216E-04], [0.17662E-07, 79.410, -11.979, -0.11843E-02, -4.8423, 0.72216E-04], [0.20287E-07, 91.212, 27.364, -0.13603E-02, -4.8423, 0.72216E-04] ] result_file = os.path.join(testfiles_path, 'shell281.rst') test_result = pyansys.ResultReader(result_file, valid_element_types=['281']) # estress, elem, enode = test_result.ElementStress(0, in_element_coord_sys=False) estress, elem, enode = test_result.ElementStress(0, in_element_coord_sys=True) print(estress[23][:4]) # debug np.any(np.isclose(-50.863, estress[23])) np.isclose(-50.863, estress[23]).any(1).nonzero() # np.isclose(-50.863, table).any(1).nonzero() f.seek(400284 - 8) table = ReadTable(f, 'f') # f.seek(400284) ncomp = 6 nodstr = 4
def CylinderANSYS(plot_vtk=True, plot_ansys=True): # cylinder parameters # torque = 100 radius = 2 h_tip = 2 height = 20 elemsize = 0.5 # pi = np.arccos(-1) force = 100 / radius pressure = force / (h_tip * 2 * np.pi * radius) # start ANSYS # pyansys.OpenLogger() ansys = pyansys.ANSYS(override=True) # Define higher-order SOLID186 # Define surface effect elements SURF154 to apply torque # as a tangential pressure ansys.Prep7() ansys.Et(1, 186) ansys.Et(2, 154) ansys.R(1) ansys.R(2) # Aluminum properties (or something) ansys.Mp('ex', 1, 10e6) ansys.Mp('nuxy', 1, 0.3) ansys.Mp('dens', 1, 0.1 / 386.1) ansys.Mp('dens', 2, 0) # Simple cylinder for i in range(4): ansys.Cylind(radius, '', '', height, 90 * (i - 1), 90 * i) ansys.Nummrg('kp') # non-interactive volume plot if plot_ansys: ansys.Show() ansys.Menu('grph') ansys.View(1, 1, 1, 1) ansys.Vplot() ansys.Wait(1) # mesh cylinder ansys.Lsel('s', 'loc', 'x', 0) ansys.Lsel('r', 'loc', 'y', 0) ansys.Lsel('r', 'loc', 'z', 0, height - h_tip) ansys.Lesize('all', elemsize * 2) ansys.Mshape(0) ansys.Mshkey(1) ansys.Esize(elemsize) ansys.Allsel('all') ansys.Vsweep('ALL') ansys.Csys(1) ansys.Asel('s', 'loc', 'z', '', height - h_tip + 0.0001) ansys.Asel('r', 'loc', 'x', radius) ansys.Local(11, 1) ansys.Csys(0) ansys.Aatt(2, 2, 2, 11) ansys.Amesh('all') ansys.Finish() if plot_ansys: ansys.Eplot() ansys.Wait(1) # new solution ansys.Slashsolu() ansys.Antype('static', 'new') ansys.Eqslv('pcg', 1e-8) # Apply tangential pressure ansys.Esel('s', 'type', '', 2) ansys.Sfe('all', 2, 'pres', '', pressure) # Constrain bottom of cylinder/rod ansys.Asel('s', 'loc', 'z', 0) ansys.Nsla('s', 1) ansys.D('all', 'all') ansys.Allsel() ansys.Psf('pres', '', 2) ansys.Pbc('u', 1) ansys.Solve() ansys.Finish() # ansys.Save() ansys.Exit() # open the result file path = ansys.path resultfile = os.path.join(path, 'file.rst') result = pyansys.ResultReader(resultfile) element_stress, elemnum, enode = result.ElementStress(0) print(element_stress) nodenum, stress = result.NodalStress(0) print(stress) if plot_vtk: # # plot interactively result.PlotNodalSolution(0, colormap='bwr') result.PlotNodalStress(0, 'Sx', colormap='bwr') result.PlotPrincipalNodalStress(0, 'SEQV', colormap='bwr') # plot and save non-interactively cpos = [(20.992831318277517, 9.78629316586435, 31.905115108541928), (0.35955395443745797, -1.4198191001571547, 10.346158032932495), (-0.10547549888485548, 0.9200673323892437, -0.377294345312956)] result.PlotNodalSolution(0, interactive=False, cpos=cpos, screenshot=os.path.join( path, 'cylinder_disp.png')) result.PlotNodalStress(0, 'Sx', colormap='bwr', interactive=False, cpos=cpos, screenshot=os.path.join(path, 'cylinder_sx.png')) result.PlotPrincipalNodalStress(0, 'SEQV', colormap='bwr', interactive=False, cpos=cpos, screenshot=os.path.join( path, 'cylinder_vonmises.png'))
import os import pytest import pyansys from pyansys.examples import sector_result_file, rstfile from vtki.plotting import running_xserver def test_non_cyclic(): with pytest.raises(Exception): pyansys.CyclicResult(rstfile) result = pyansys.ResultReader(sector_result_file) def test_is_cyclic(): assert hasattr(result, 'rotor') @pytest.mark.skipif(not running_xserver(), reason="Requires active X Server") def test_animate_nodal_solution(tmpdir): temp_movie = str(tmpdir.mkdir("tmpdir").join('tmp.mp4')) result.AnimateNodalSolution(0, nangles=20, movie_filename=temp_movie, interactive=False) assert os.path.isfile(temp_movie)