示例#1
0
class TestCyclicResultReader(object):

    # avoids errors in collection
    result_file = os.path.join(data_path, 'cyclic_%s.rst' % rver)
    try:
        # test if raw results are being read properly by using the normal result reader
        result = ResultFile(result_file, ignore_cyclic=True)

        if rver == 'v182':
            ansys = pyansys.ANSYS('/usr/ansys_inc/v182/ansys/bin/ansys182',
                                  override=True, jobname=rver,
                                  loglevel='DEBUG',
                                  interactive_plotting=False,
                                  prefer_pexpect=True)
        else:
            ansys = pyansys.ANSYS('/usr/ansys_inc/v150/ansys/bin/ansys150',
                                  override=True, jobname=rver,
                                  loglevel='DEBUG',
                                  interactive_plotting=False,
                                  prefer_pexpect=True)


        # copy result file to ansys's temporary path
        copyfile(result_file, os.path.join(ansys.path, '%s.rst' % rver))

        # setup ansys for output without line breaks
        ansys.Post1()
        ansys.Set(1, 1)
        ansys.Header('OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF')
        nsigfig = 10
        ansys.Format('', 'E', nsigfig + 9, nsigfig)  
        ansys.Page(1E9, '', -1, 240)

    except:  # for travis and appveyor
        pass

    def test_prnsol_u(self):
        # verify cyclic displacements
        table = self.ansys.Prnsol('u').splitlines()
        if self.ansys.using_corba:
            array = np.genfromtxt(table[7:])
        else:
            array = np.genfromtxt(table[9:])
        ansys_nnum = array[:, 0].astype(np.int)
        ansys_disp = array[:, 1:-1]

        nnum, disp = self.result.nodal_solution(0)

        # cyclic model will only output the master sector
        ansys_nnum = ansys_nnum[:nnum.size]
        ansys_disp = ansys_disp[:nnum.size]

        assert np.allclose(ansys_nnum, nnum)
        assert np.allclose(ansys_disp, disp)

    def test_presol_s(self):
        # verify element stress
        element_stress, _, enode = self.result.element_stress(0)
        element_stress = np.vstack(element_stress)
        enode = np.hstack(enode)

        # parse ansys result
        table = self.ansys.Presol('S').splitlines()
        ansys_element_stress = []
        line_length = len(table[15])
        for line in table:
            if len(line) == line_length:
                ansys_element_stress.append(line)

        ansys_element_stress = np.genfromtxt(ansys_element_stress)
        ansys_enode = ansys_element_stress[:, 0].astype(np.int)
        ansys_element_stress = ansys_element_stress[:, 1:]

        assert np.allclose(element_stress, ansys_element_stress)
        assert np.allclose(enode, ansys_enode)

    def test_prnsol_s(self):
        # verify cyclic displacements
        table = self.ansys.Prnsol('s').splitlines()
        if self.ansys.using_corba:
            array = np.genfromtxt(table[7:])
        else:
            array = np.genfromtxt(table[10:])
        ansys_nnum = array[:, 0].astype(np.int)
        ansys_stress = array[:, 1:]

        nnum, stress = self.result.nodal_stress(0)

        # v150 includes nodes in the geometry that aren't in the result
        mask = np.in1d(nnum, ansys_nnum)
        nnum = nnum[mask]
        stress = stress[mask]

        assert np.allclose(ansys_nnum, nnum)
        assert np.allclose(ansys_stress, stress)

    def test_prnsol_prin(self):
        # verify principal stress
        table = self.ansys.Prnsol('prin').splitlines()
        if self.ansys.using_corba:
            array = np.genfromtxt(table[7:])
        else:
            array = np.genfromtxt(table[10:])
        ansys_nnum = array[:, 0].astype(np.int)
        ansys_stress = array[:, 1:]

        nnum, stress = self.result.principal_nodal_stress(0)

        # v150 includes nodes in the geometry that aren't in the result
        mask = np.in1d(nnum, ansys_nnum)
        nnum = nnum[mask]
        stress = stress[mask]

        assert np.allclose(ansys_nnum, nnum)
        assert np.allclose(ansys_stress, stress, atol=1E-2)

    @pytest.mark.skipif(not system_supports_plotting(), reason="Requires active X Server")
    def test_plot(self):
        filename = '/tmp/temp.png'
        self.result.plot_nodal_solution(0, screenshot=filename,
                                      off_screen=True)

        # self.result.plot_nodal_stress(0, 'Sx', screenshot=filename,
        #                             off_screen=True)

        self.result.plot_principal_nodal_stress(0, 'EQV', screenshot=filename,
                                                off_screen=True)

    def test_exit(self):
        self.ansys.Exit()
示例#2
0
"""
This script was used to generate the sector modal analysis result file.

Result file is sector.rst
"""
import pyansys
from pyansys import examples

ansys = pyansys.ANSYS()

ansys.Cdread('db', examples.sector_archive_file)

# make cyclic
ansys('/PREP7')
ansys('CYCLIC')

# Steel
ansys('MP,    NUXY,   1,  0.3')
ansys('MP,    DENS,   1,  0.0005')
ansys('MP,      EX,   1,  17000000')
ansys('EMODIF,ALL,MAT,1')

# Static solution
ansys('/SOLU')
ansys('ANTYPE, 2, new')
ansys('MODOPT, LANB, 6, 1')
ansys.Cycopt('hindex', 'all')
ansys.Bcsoption('', 'INCORE')
ansys.Mxpand('', '', '', 'Yes')
ansys.Solve()
ansys.Finish()
示例#3
0
def ansys_cylinder_demo(exec_file=None, plot_vtk=True,
                        plot_ansys=True, as_test=False):
    """
    Cylinder demo for ansys
    """
    # cylinder parameters
    # torque = 100
    radius = 2
    h_tip = 2
    height = 20
    elemsize = 0.5
    force = 100/radius
    pressure = force/(h_tip*2*np.pi*radius)

    # start ANSYS
    if as_test:
        loglevel = 'ERROR'
    else:
        loglevel = 'INFO'
    ansys = pyansys.ANSYS(exec_file=exec_file, override=True, loglevel=loglevel)

    # 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 and not as_test:
        ansys.View(1, 1, 1, 1)
        ansys.Vplot()

    # 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 and not as_test:
        ansys.Eplot()

    # 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.Exit()

    # open the result file
    result = ansys.result
    element_stress, elemnum, enode = result.element_stress(0)
    if as_test:
        assert len(element_stress)
    else:
        print(element_stress[:10])
    nodenum, stress = result.nodal_stress(0)
    if as_test:
        assert np.any(stress)
    else:
        print(stress[:10])

    cpos = [(20.992831318277517, 9.78629316586435, 31.905115108541928),
            (0.35955395443745797, -1.4198191001571547, 10.346158032932495),
            (-0.10547549888485548, 0.9200673323892437, -0.377294345312956)]

    if plot_vtk:
        result.plot_nodal_solution(0, cpos=cpos, cmap='bwr',
                                   off_screen=as_test, screenshot=as_test)
        result.plot_nodal_stress(0, 'x', cpos=cpos, cmap='bwr',
                                 off_screen=as_test, screenshot=as_test)
        result.plot_principal_nodal_stress(0, 'EQV', cpos=cpos,
                                           cmap='bwr', off_screen=as_test, screenshot=as_test)

    return True
示例#4
0
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'))