def LoadKM(): """ Loads m and k matrices from a full file """ # Create file reader object fobj = pyansys.FullReader(fullfile) dofref, k, m = fobj.LoadKM() # print results ndim = k.shape[0] print('Loaded {:d} x {:d} mass and stiffness matrices'.format(ndim, ndim)) print('\t k has {:d} entries'.format(k.indices.size)) print('\t m has {:d} entries'.format(m.indices.size)) # compute natural frequencies if installed try: from scipy.sparse import linalg from scipy import sparse except ImportError: return 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=10000) # System natural frequencies f = np.real(w)**0.5 / (2 * np.pi) print('First four natural frequencies:') for i in range(4): print('{:.3f} Hz'.format(f[i]))
def LoadKM(): """ Loads m and k matrices from a full file """ import numpy as np # Create file reader object fobj = pyansys.FullReader(fullfile) dofref, k, m = fobj.LoadKM(utri=False) # print results ndim = k.shape[0] print('Loaded {:d} x {:d} mass and stiffness matrices'.format(ndim, ndim)) print('\t k has {:d} entries'.format(k.indices.size)) print('\t m has {:d} entries'.format(m.indices.size)) # compute natural frequencies if installed try: from scipy.sparse import linalg except BaseException: return # removing constrained nodes (this is not efficient) free = np.logical_not(fobj.const).nonzero()[0] k = k[free][:, free] m = m[free][:, free] import numpy as np # Solve w, v = linalg.eigsh(k, k=20, M=m, sigma=10000) # System natural frequencies f = np.real(w)**0.5 / (2 * np.pi) print('First four natural frequencies:') for i in range(4): print('{:.3f} Hz'.format(f[i]))
def SolveKM(): """ 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.FullReader(pyansys.examples.fullfile) dofref, k, m = full.LoadKM(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 = vtki.Plotter() # add two meshes to the plotting class plobj.add_mesh(grid.copy(), style='wireframe') plobj.add_mesh(grid, scalars=n, stitle='Normalized\nDisplacement', flipscalars=True) # Update the coordinates by adding the mode shape to the grid plobj.update_coordinates(grid.GetNumpyPoints() + disp / 80, render=False) plobj.add_text('Cantliver Beam 4th Mode Shape at {:.4f}'.format(f[3]), fontsize=30) plobj.plot()
def load_km(): """ Loads m and k matrices from a full file """ # Create file reader object fobj = pyansys.FullReader(fullfile) dofref, k, m = fobj.load_km() # print results ndim = k.shape[0] print('Loaded {:d} x {:d} mass and stiffness matrices'.format(ndim, ndim)) print('\t k has {:d} entries'.format(k.indices.size)) print('\t m has {:d} entries'.format(m.indices.size)) # compute natural frequencies if installed try: from scipy.sparse import linalg from scipy import sparse except ImportError: return 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=10000) # System natural frequencies f = np.real(w)**0.5 / (2 * np.pi) print('First four natural frequencies:') for i in range(4): print('{:.3f} Hz'.format(f[i])) # breakpoint() known_result = np.array([ 1283.20036921, 1283.20036921, 5781.97486169, 6919.39887714, 6919.39887714, 10172.61497694, 16497.85701889, 16497.85701889, 17343.9939669, 27457.18472747, 27457.18472747, 28908.52552073, 30326.16886062, 39175.76412419, 39175.76412419, 40503.70406456, 49819.91597612, 51043.03965541, 51043.03965541, 52193.86143879 ]) assert np.allclose(f, known_result)
# Plot the displacement of Mode 0 in the x direction result.PlotNodalResult(0, 'x', label='Displacement') #============================================================================== # Load KM #============================================================================== # Load the reader from pyansys import pyansys from pyansys import examples filename = examples.fullfile # Create result reader object and read in full file fobj = pyansys.FullReader(filename) fobj.LoadFullKM() import numpy as np from scipy.sparse import csc_matrix, linalg ndim = fobj.nref.size k = csc_matrix((fobj.kdata, (fobj.krows, fobj.kcols)), shape=(ndim, ndim)) m = csc_matrix((fobj.mdata, (fobj.mrows, fobj.mcols)), shape=(ndim, ndim)) # Solve w, v = linalg.eigsh(k, k=20, M=m, sigma=10000) # System natural frequencies f = (np.real(w))**0.5 / (2 * np.pi) print('First four natural frequencies') for i in range(4):
def test_fullreader(): fobj = pyansys.FullReader(fullfile) dofref, k, m = fobj.LoadKM() assert dofref.size assert k.size assert m.size
def SolveKM(): """ Loads and solves a mass and stiffness matrix from an ansys full file """ import numpy as np from scipy.sparse import linalg import pyansys # load the mass and stiffness matrices full = pyansys.FullReader(pyansys.examples.fullfile) dofref, k, m = full.LoadKM(utri=False) # removing constrained nodes (this is not efficient) free = np.logical_not(full.const).nonzero()[0] k = k[free][:, free] m = m[free][:, free] # Solve w, v = linalg.eigsh(k, k=20, M=m, sigma=10000) # System natural frequencies f = (np.real(w))**0.5 / (2 * np.pi) #%% Plot result import vtkInterface # Get the 4th mode shape mode_shape = v[:, 3] # x, y, z displacement for each node # create the full mode shape including the constrained nodes full_mode_shape = np.zeros(dofref.shape[0]) full_mode_shape[np.logical_not(full.const)] = mode_shape # 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.ReadArchive(pyansys.examples.hexarchivefile) grid = archive.ParseVTK() # plot the normalized displacement # grid.Plot(scalars=n) # Fancy plot the displacement plobj = vtkInterface.PlotClass() # add two meshes to the plotting class. Meshes are copied on load plobj.AddMesh(grid, style='wireframe') plobj.AddMesh(grid, scalars=n, stitle='Normalized\nDisplacement', flipscalars=True) # Update the coordinates by adding the mode shape to the grid plobj.UpdateCoordinates(grid.GetNumpyPoints() + disp / 80, render=False) plobj.AddText('Cantliver Beam 4th Mode Shape at {:.4f}'.format(f[3]), fontsize=30) plobj.Plot() del plobj