def example_geometry2d(): mapdl = pyansys.Mapdl() geometry = Geometry2d(mapdl) # or with optional parameters: geometry = Geometry2d(mapdl, rotation_angle=0.5, destination=Point2D(0, 1)) # following methods do not change already created data inside ANSYS geometry.set_rotation(radians=1.0) geometry.set_rotation_in_degree(degrees=20.0) geometry.set_destination(point=Point2D(2, -3)) geometry.create() # this creates the keypoints, lines and areas in ANSYS. # Note: Geometry2d does not implement create(), but enforces this method to all subclasses # Warning: Changes to geometry won't be transfered to ANSYS after this call. # If you call create() a second time, a new geometry or an error is created! # Beware! Some APDL functions change keypoint numbers. # In current version, Geometry2D is not updated automatically. # Make sure to use below methods/attributes before such changes. geometry.keypoints # list of ANSYS keypoint numbers geometry.lines # lit of ANSYS line numbers geometry.areas # list of ANSYS area numbers geometry.select_lines( ) # Selects all lines belonging to the geometry (deselecting all other lines). geometry.select_areas( ) # Selects all areas belonging to the geometry (deselecting all other areas). geometry.set_material_number(mat=3) geometry.set_element_type(183)
def init_ansys(self, root_folder): path = pathlib.Path(root_folder) path.parent.mkdir(parents=True, exist_ok=True) os.mkdir(root_folder) return pyansys.Mapdl(run_location=root_folder, interactive_plotting=True, override=True, start_timeout=600)
def mapdl(): # os.environ['I_MPI_SHM_LMT'] = 'shm' mapdl = pyansys.Mapdl(MAPDLBIN[rver], override=True, jobname=rver, loglevel='ERROR', interactive_plotting=False, prefer_pexpect=True) # build the cyclic model mapdl.prep7() mapdl.shpp('off') mapdl.cdread('db', pyansys.examples.sector_archive_file) mapdl.prep7() mapdl.cyclic() # set material properties mapdl.mp('NUXY', 1, 0.31) mapdl.mp('DENS', 1, 4.1408E-04) mapdl.mp('EX', 1, 16900000) mapdl.emodif('ALL', 'MAT', 1) # setup and solve mapdl('/SOLU') mapdl.Antype(2, 'new') mapdl.Modopt('lanb', 1, 1, 100000) mapdl.Eqslv('SPARSE') mapdl.Lumpm(0) mapdl.Pstres(0) mapdl.Bcsoption('INCORE') mapdl.mxpand(elcalc='YES') mapdl.solve() mapdl.finish() # setup ansys for output without line breaks mapdl.post1() mapdl.header('OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF') nsigfig = 10 mapdl.format('', 'E', nsigfig + 9, nsigfig) mapdl.page(1E9, '', -1, 240) return mapdl
def example_geo2d_isogon_01(): mapdl = pyansys.Mapdl(override=True, interactive_plotting=True, jobname="example_geo2d_isogon_01") radius = 40 edges = 12 isogon = Isogon(mapdl, radius, edges) isogon.create() rectangles = [] for i, rotation in enumerate(range(0, 359, round(360 / edges))): rectangle = Rectangle(mapdl, width=30, height=10) rectangles.append(rectangle) rectangle.set_destination(isogon.points[i]) rectangle.set_rotation_in_degree(180 - rotation + (180 / edges)) rectangle.create() # create keypoints, lines and area in ANSYS mapdl.gplot() mapdl.exit()
def example_geo2d_rectangle_01(): mapdl = pyansys.Mapdl(override=True, interactive_plotting=True, jobname="example_geo2d_rectangle_01") rectangle = Rectangle(mapdl, width=10, height=30) rectangle.set_rotation_in_degree(45) rectangle.create() # create keypoints, lines and area in ANSYS rectangle.set_element_type(mapdl.et("", 183)) rectangle.mesh(10) # or: rectangle.mesh_custom(...) mapdl.pnum("KP", 1) mapdl.pnum("LINE", 1) mapdl.pnum("AREA", 1) mapdl.gplot() mapdl.exit()
import pytest import pyansys if pyansys.has_ansys: mapdl = pyansys.Mapdl(override=True) @pytest.fixture(scope='function') def cleared(): mapdl.clear() mapdl.prep7() yield @pytest.mark.skipif(not pyansys.has_ansys, reason="Requires ANSYS installed") def test_e(cleared): mapdl.et("", 183) n0 = mapdl.n("", 0, 0, 0) n1 = mapdl.n("", 1, 0, 0) n2 = mapdl.n("", 1, 1, 0) n3 = mapdl.n("", 0, 1, 1) n4 = mapdl.n("", 0, 1, -1) e0 = mapdl.e(n0, n1, n2, n3) assert e0 == 1 e1 = mapdl.e(n0, n1, n2, n4) assert e1 == 2 @pytest.mark.skipif(not pyansys.has_ansys, reason="Requires ANSYS installed") def test_et(cleared): n_plane183 = mapdl.et("", "PLANE183")
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.Mapdl(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
""" Script generated by pyansys version 0.39.10 """ import pyansys ansys = pyansys.Mapdl(loglevel="INFO") # 直接通过结果文件导出displacement ansys.run("FINISH") ansys.run("/CLEAR") ansys.run("/NOPR") ansys.run("*DIM,path_,STRING,128,2") ansys.run("path_(1,1)='D:/Alai/ansys_Alai/pump/pump_files/dp0/SYS/MECH/file'") ansys.run("path_(1,2)='D:\disp_pump\\'") ansys.run("/POST1") ansys.run("/MKDIR,path_(1,2)") ansys.run("/CWD,path_(1,2)") ansys.run("FILE,path_(1,1),'rst'") ansys.run("INRES,ALL") ansys.run("*DO,j,0.1,50,0.5") ansys.run("SET,0,0,,,j") ansys.run("PRITER") ansys.run("file_name=CHRVAL(j)") ansys.run("*CFOPEN,%file_name%,txt") ansys.run("ALLS,ALL") # *GET,nmax,NODE,,NUM,MAX ansys.run("*GET,n_count,NODE,0,COUNT") ansys.run("*GET,n_current,NODE,,NUM,MIN") ansys.run("*DO,i,1,n_count,1") ansys.run("n_disp_x = UX(n_current)") ansys.run("n_disp_y = UY(n_current)") ansys.run("n_disp_z = UZ(n_current)") ansys.run("*GET,n_disp_sum,NODE,i,U,SUM") # n_disp_sum= SQRT(n_disp_x**2+n_disp_y**2+n_disp_z**2)
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.Mapdl(MAPDL182BIN, override=True, jobname=rver, loglevel='DEBUG', interactive_plotting=False, prefer_pexpect=True) else: ansys = pyansys.Mapdl(MAPDL150BIN, 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()
def test_v194(): mapdl = pyansys.Mapdl(MAPDL194BIN, override=True) mapdl.prep7()
from ansys_model import ANSYS_MODEL from NAGA_2 import MyProblem, NSGA2_ANSYS from config import CONFIG import pyansys # 初始化配置文件 opt = CONFIG(root_path='/root/', Encoding='BG', NIND=20, MAXGEN=300, Drawing=1, plot_use=False) # ansys建模 ansys = pyansys.Mapdl(run_location=opt.root_path, override=True, interactive_plotting=True, loglevel="ERROR") model = ANSYS_MODEL(ansys=ansys, opt=opt) model.model() # 多目标优化 ansys_nsga2 = NSGA2_ANSYS(MyProblem(ansys), opt.Encoding, opt.NIND, opt.MAXGEN, opt.Drawing) ansys_nsga2.run_nsga()
def mapdl(): return pyansys.Mapdl(override=True)
""" Script generated by pyansys version 0.39.17 """ import pyansys import numpy as np import pandas as pd import random ansys = pyansys.Mapdl(loglevel="INFO") ITER_SIZE = 2000 FEATURES = 25 BLOCK_X = np.linspace(0.75, 2, 20) BLOCK_Y = np.linspace(0.75, 2, 20) BLOCK_THICK = np.linspace(0.01, 0.04, 20) CRACK_X1 = np.linspace(0, 0.9, 20) CRACK_Y1 = np.linspace(0, 0.9, 20) LAYERS = [0.01, 0.02] #CRACK_X2 = np.linspace(0, 0.9, 19) #CRACK_Y2 = np.linspace(0, 0.9, 19) DATA_ARRAY = np.zeros((ITER_SIZE, FEATURES)) FREQs = [ "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20" ] for i in range(ITER_SIZE):