yydryydr 发表于 2016-3-30 10:53

Abaqus里应用Python的一些技巧

  例如:cell4 = mdb.models['block'].parts['crankcase'].cells,要把part模块中编号为4的体赋值给cell4,就需通过路径mdb→models→part→cells(4号体属性),其中'block'、'crankcase'、分别是model和part的名字。
  在草图Sketch中画线:
  s = mdb.models[' block '].ConstrainedSketch(name='grid',
  sheetSize=3.0)
  s.Line(point1=(-1.275, 0.0), point2=(-1.125, 0.0))
  s.Line(point1=(1.125, 0.0), point2=(1.275, 0.0))
  执行任何一条命令都必须按照结构树的格式进行操作。我们所看到的python脚本繁杂的语句就是这样形成的。这样大量的命令不能在短时间内掌握,我们只需要根据自己的需要边建立模型边学习就可以了。
  a = mdb.models['Model-1'].rootAssembly
  s = a.instances['Mount-1'].edges
  side1Edges = s.findAt(((0.0475, 0.0, 0.0), ))
  以上三行与下面的句子是等同的,即把findat找到的edges赋值给side1Edges。分开来写简单明了,大大缩短了语句的长度。
  side1Edges = mdb.models['Model-1'].rootAssembly. instances['Mount-1'].edges. findAt(((0.0475, 0.0, 0.0), ))
  a.Surface(side1Edges=side1Edges, name='Bottom'),这行语句设置side1Edges所对应的edge为名称'Bottom'的surface的set。
  #===========================================================
  3.模型参数分析技巧
  Python脚本建模的好处就是可以进行参数分析,即改变我们要分析模型的几何尺寸、材料属性等可变参数,对数值模型进行求解计算,从而对所分析的对象有更全面的了解。
  1.对自己要进行参数分析的参数赋值:如几何尺寸或材料属性等a1=20,b1=30,c1=40,命名要符合python规则。
  2.cae与Python混合建模,不会的命令就利用cae自动生成,用Python reader记录命令然后进行修改,可以弥补不熟悉Python的缺点;
  3.逐句修改Python脚本,可以去掉一些不必要的语句并在cae中逐句进行验证。
  #===========================================================
  4. 几个命令的体会
  4.1 Set ( )
  Set命令在python建模时要经常用到,对实体、surface、element等分组,方便加载、施加约束和单元生死等控制
  4.2 Findat ( )
  对cell、edge、face、vertice进行查找,括号中参数为实体坐标
  p = mdb.models['Model-1'].parts['Mount']
  f = p.faces
  faces = f.findAt(((0.042303, 0.006937, 0.0), ))
  pickedRegions =(faces, )
  p.setElementType(regions=pickedRegions,
  elemTypes=(elemType1, elemType2))
  4.3 Len ( )
  利用len命令可以实现对单元选取
  p = mdb.models['precast culvert'].parts['soil']
  e = p.elements
  len(e)
  n1=len(e)
  elements = e #单元数存放在e [ ]的一维数组里
  p.Set(elements=elements, name='Set-3')
  对单元进行编组set,可以进行生死单元的控制,我摸索了好久才想到这个办法,目前只在二维模型应用过,三维也应该没问题。Abaqus没有办法对单元编号进行编号控制,也没有像ansys那样有效的选择命令,怎样选择abaqus的单元就是很头疼的问题,我要做路堤的分层回填模拟,手动选取单元根本就没有可能。Abaqus的编号其实是有规则的,后划分的单元编号最小,先划分的单元编号最大;这样我们就可以每次划分单元后都采用len命令计算一次单元数量,并用参数记录下来,这样我们就能计算出每部分单元的数量以及他的起始和终止编号。根据elements = e、p.Set(elements=elements, name='Set-3')语句就可以把每部分单元设置成set,以后操作就很方便了。
  #===========================================================
  5. 一个Abaqus/Python例子
  下面是一个Getting Started with Abaqus: Interactive Edition中的一个橡胶避震垫例子:☺号后语句表示我的注释,注释上面的句子。我也不懂的就没有注释,先熟悉一下Python的样子。在学习的时候可以copy(Crtol + V)到cae下面的命令行中一句句的执行,并在cae视窗中查看命令执行情况,领会命令使用方法。
  # Script for rubber mount example
  ☺“#”开头表示这一行为注释行,同ansys的“!”号
  from abaqus import *
  from abaqusConstants import *
  ☺引入abaqus中的一些模块,这些模块是abaqus已事先存储在文件中,要引入才这些模块能运行相应的命令
  session.viewports['Viewport: 1'].makeCurrent()
  session.viewports['Viewport: 1'].maximize()
  session.journalOptions.setValues(replayGeometry=COORDINATE,
  recoverGeometry=COORDINATE)
  ☺对cae视窗的操作命令;maximize()的括号好像是默认为当前值
  from caeModules import *
  from driverUtils import executeOnCaeStartup
  executeOnCaeStartup()
  Mdb()
  #--------------------------------------------------------------------------------------------------
  ## Sketch profile of the mount
  ☺进入草图模块
  s = mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=0.3)
  ☺建立一个sketch草图,草图的尺寸为0.3个单位;这个句子算是一个标准的Python语句,具体后面解释
  g, v, d, c = s.geometry, s.vertices, s.dimensions, s.constraints
  s.sketchOptions.setValues(decimalPlaces=3, viewStyle=AXISYM)
  s.setPrimaryObject(option=STANDALONE)
  ☺设置草图为轴对称模式
  s.ConstructionLine(point1=(0.0, -100.0), point2=(0.0, 100.0))
  s.FixedConstraint(entity=g)
  ☺建立辅助线及约束
  mdb.models['Model-1'].sketches['__profile__'].sketchOptions.setValues(gridFrequency=4)
  ☺sketch参数修改
  s.rectangle(point1=(0.01, 0.0), point2=(0.025, 0.01))
  ☺画矩形
  s.DistanceDimension(entity1=g, entity2=v,textPoint=(0.00998260825872421, -0.00830297358334064), value=0.01)
  s.VerticalDimension(vertex1=v,vertex2=v,textPoint=(0.0,0.00851448811590672), value=0.03)
  s.ObliqueDimension(vertex1=v,vertex2=v,textPoint=(0.025699570775032, -0.00830297358334064), value=0.05)
  ☺标注图形尺寸,还可以修改图形尺寸,如拉伸、压缩等
  s.CircleByCenterPerimeter(center=(0.085,0.025),point1=(0.06, 0.00740899052470922))
  ☺画圆
  s.CoincidentConstraint(entity1=v, entity2=g)
  s.DistanceDimension(entity1=g, entity2=v,textPoint=(0.0811913833022118, -0.023865295574069), value=0.1)
  s.VerticalDimension(vertex1=v, vertex2=v,textPoint=(0.115524396300316, 0.0262394621968269), value=0.0)
  s.ObliqueDimension(vertex1=v, vertex2=v,textPoint=(0.0519323498010635, 0.0), value=0.005)
  ☺修改圆尺寸、移动位置―――没搞清楚修改尺寸命令有什么实际意义,直接定义好尺寸不就结了?
  s.autoTrimCurve(curve1=g,point1=(0.124150268733501,-0.00965208746492863))
  ☺裁剪命令,其中g是圆的线编号,g=s.geometry
  s.autoTrimCurve(curve1=g,point1=(0.0601795427501202,0.020298857241869))
  s.autoTrimCurve(curve1=g,point1=(0.0557677671313286,0.030869778245687))
  ☺裁剪命令
  s.RadialDimension(curve=g,textPoint=(0.0725325122475624,0.0207393132150173),radius=0.047169905660283)
  d.setValues(reference=ON)
  ☺标注命令,标注界面很漂亮
  session.viewports['Viewport: 1'].view.fitView()
  ☺cae图形缩放的合适大小
  p = mdb.models['Model-1'].Part(name='Mount', dimensionality=AXISYMMETRIC, type=DEFORMABLE_BODY)
  p = mdb.models['Model-1'].parts['Mount']
  ☺命名model
  p.BaseShell(sketch=s)
  s.unsetPrimaryObject()
  session.viewports['Viewport: 1'].setValues(displayedObject=p)
  del mdb.models['Model-1'].sketches['__profile__']
  ☺显示model
  #--------------------------------------------------------------------------------------------------
  ## Create material 'Rubber'
  ☺创建材料模型
  mdb.models['Model-1'].Material('Rubber')
  mdb.models['Model-1'].materials['Rubber'].Hyperelastic(type=POLYNOMIAL, table=())
  mdb.models['Model-1'].materials['Rubber'].hyperelastic.UniaxialTestData(table=(( 0.054E6, 0.0380), (0.152E6, 0.1338), (0.254E6, 0.2210), (0.362E6, 0.3450), (0.459E6, 0.4600), (0.583E6, 0.6242), (0.656E6, 0.8510), (0.730E6, 1.4268)))
  mdb.models['Model-1'].materials['Rubber'].hyperelastic.BiaxialTestData(table=((0.089E6, 0.0200), (0.255E6, 0.1400), (0.503E6, 0.4200), (0.958E6, 1.4900), (1.703E6, 2.7500), (2.413E6, 3.4500)))
  mdb.models['Model-1'].materials['Rubber'].hyperelastic.PlanarTestData(table=((0.055E6, 0.0690), (0.324E6, 0.2828), (0.758E6, 1.3862), (1.269E6, 3.0345), (1.779E6, 4.0621)))
  ##
  ## Create material 'Steel'
  ##
  mdb.models['Model-1'].Material('Steel')
  mdb.models['Model-1'].materials['Steel'].Elastic(table=((200.E9, 0.3), ))
  ##
  ## Create solid sections for the rubber and steel
  ##
  mdb.models['Model-1'].HomogeneousSolidSection(name='RubberSection',
  material='Rubber', thickness=1.0)
  mdb.models['Model-1'].HomogeneousSolidSection(name='SteelSection',
  material='Steel', thickness=1.0)
  #-------------------------------------------------------------------
  ## Partition the part into two regions (rubber and steel regions)
  ☺切割体形成两个部分,从而可以赋予不同材料属性
  f, e, d = p.faces, p.edges, p.datums
  t =p.MakeSketchTransform(sketchPlane=f.findAt(coordinates=(0.043333,
  0.001667,0.0),normal=(0.0,0.0,1.0)),sketchPlaneSide=SIDE1,
  origin=(0.033052,0.014514, 0.0))
  s = mdb.models['Model-1'].ConstrainedSketch(name='__profile__',
  sheetSize=0.134, gridSpacing=0.003, transform=t)
  g, v, d1, c = s.geometry, s.vertices, s.dimensions, s.constraints
  s.sketchOptions.setValues(decimalPlaces=3)
  s.setPrimaryObject(option=SUPERIMPOSE)
  p.projectReferencesOntoSketch(sketch=s, filter=COPLANAR_EDGES)
  ☺进入草图,并设置草图属性(图纸大小、网格间距等)
  s.Line(point1=(0.026948, -0.009514), point2=(-0.03, -0.009514))
  s.HorizontalConstraint(entity=g.findAt((-0.001526, -0.009514)))
  s.PerpendicularConstraint(entity1=g.findAt((0.026948, -0.012014)),
  entity2=g.findAt((-0.001526, -0.009514)))
  ☺作辅助线,findat(查找)命令很有用,可以用来选择实体
  pickedFaces = f.findAt(((0.043333, 0.001667, 0.0), ))
  p.PartitionFaceBySketch(faces=pickedFaces, sketch=s)
  ☺用辅助线分割体
  s.unsetPrimaryObject()
  ☺显示分割后体
  del mdb.models['Model-1'].sketches['__profile__']
  #--------------------------------------------------------------------------------------------------
  ## Assign rubber section
  ☺实体指定不同的材料属性
  p = mdb.models['Model-1'].parts['Mount']
  f = p.faces
  faces = f.findAt(((0.042303, 0.006937, 0.0), ))
  region = regionToolset.Region(faces=faces)
  p.SectionAssignment(region=region, sectionName='RubberSection', offset=0.0)
  ##
  ## Assign steel section
  ##
  faces = f.findAt(((0.043333, 0.003333, 0.0), ))
  region = regionToolset.Region(faces=faces)
  p.SectionAssignment(region=region, sectionName='SteelSection', offset=0.0)
  a = mdb.models['Model-1'].rootAssembly
  session.viewports['Viewport: 1'].setValues(displayedObject=a)
  ##
  ## Set coordinate system (done by default)
  ##
  a.DatumCsysByDefault(CARTESIAN)
  ##
  ## Instance the mount
  ##
  p = mdb.models['Model-1'].parts['Mount']
  a.Instance(name='Mount-1', part=p, dependent=ON)
  ##
  ## Create geometry set 'Middle'
  ##
  e = a.instances['Mount-1'].edges
  edges = e.findAt(((0.020708, 0.03, 0.0), ))
  a.Set(edges=edges, name='Middle')
  ☺通过findat命令定义了一个edges组“Middle”
  ## Create geometry set 'Out'
  ##
  v = a.instances['Mount-1'].vertices
  verts = v.findAt(((0.01, 0.0, 0.0), ))
  a.Set(vertices=verts, name='Out')
  ☺通过findat命令定义了一个vertices组“Out”
  ## Create surface 'Bottom'
  ##
  s = a.instances['Mount-1'].edges
  side1Edges = s.findAt(((0.0475, 0.0, 0.0), ))
  a.Surface(side1Edges=side1Edges, name='Bottom')
  ☺通过findat命令定义了一个edges组“Bottom”
  #--------------------------------------------------------------------------------------------------
  ## Create a static general step
  ☺进入step模块
  mdb.models['Model-1'].StaticStep(name='Compress mount', previous='Initial',
  description='Apply axial pressure load to mount', timePeriod=1,
  adiabatic=OFF, maxNumInc=100, stabilization=None,
  timeIncrementationMethod=AUTOMATIC,
  initialInc=0.01, minInc=1e-05, maxInc=1, matrixSolver=SOLVER_DEFAULT,
  amplitude=RAMP, extrapolation=LINEAR, fullyPlastic="", nlgeom=ON)
  ☺step中的一些设置,与cae操作框相对应
  session.viewports['Viewport: 1'].assemblyDisplay.setValues(
  step='Compress mount')
  ☺cae中显示step模块 Compress mount
  ##
  ## Modify output requests
  ##
  mdb.models['Model-1'].fieldOutputRequests['F-Output-1'].setValues(
  variables=('S', 'PE', 'PEEQ', 'PEMAG', 'NE', 'LE', 'U', 'RF',
  'CF', 'CSTRESS', 'CDISP'))
  ☺对结果数据输出的一些定义
  regionDef=a.sets['Out']
  mdb.models['Model-1'].HistoryOutputRequest(name='H-Output-1',
  createStepName='Compress mount', variables=('U1', 'U2', 'U3'),
  region=regionDef)
  session.viewports['Viewport: 1'].assemblyDisplay.setValues(loads=ON, bcs=ON,
  predefinedFields=ON)
  #--------------------------------------------------------------------------------------------------
  ## Apply pressure load
  ☺进入load模块
  region = a.surfaces['Bottom']
  mdb.models['Model-1'].Pressure(name='Pressure',
  createStepName='Compress mount', region=region, magnitude=500000.0)
  ☺通过bottom的set对底边进行加载
  ## Apply symmetry bc to set "Middle'
  ##
  region = a.sets['Middle']
  mdb.models['Model-1'].DisplacementBC(name='Symmetry',
  createStepName='Compress mount', region=region, u2=0.0)
  ☺对顶面进行约束
  ## Suppress visibility of datum geometry
  ##
  session.viewports['Viewport: 1'].assemblyDisplay.geometryOptions.setValues(
  geometryEdgesInShaded=OFF, datumPoints=OFF, datumAxes=OFF, datumPlanes=OFF,datumCoordSystems=OFF)
  session.viewports['Viewport: 1'].assemblyDisplay.setValues(mesh=ON, loads=OFF,
  bcs=OFF, predefinedFields=OFF)
  session.viewports['Viewport: 1'].assemblyDisplay.meshOptions.setValues(
  meshTechnique=ON)
  p = mdb.models['Model-1'].parts['Mount']
  session.viewports['Viewport: 1'].setValues(displayedObject=p)
  ☺mesh模块的一些显示设置
  #--------------------------------------------------------------------------------------------------
  ## Assign edge seeds
  ☺进入mesh模块
  p = mdb.models['Model-1'].parts['Mount']
  e = p.edges
  pickedEdges = e.findAt(((0.0225, 0.005, 0.0), ), ((0.0475, 0.0, 0.0), ),
  ((0.020708, 0.03, 0.0), ))
  p.seedEdgeByNumber(edges=pickedEdges, number=30)
  pickedEdges = e.findAt(((0.053289, 0.023434, 0.0), ), ((0.01, 0.01125, 0.0), ))
  p.seedEdgeByNumber(edges=pickedEdges, number=14)
  pickedEdges = e.findAt(((0.01, 0.00125, 0.0), ), ((0.06, 0.00375, 0.0), ))
  p.seedEdgeByNumber(edges=pickedEdges, number=1)
  ☺选择边,设置种子数
  ## Use structured meshing
  ##
  f = p.faces
  pickedRegions = f
  p.setMeshControls(regions=pickedRegions, technique=STRUCTURED)
  ☺ STRUCTURED划分网格
  ## Assign element type to the rubber
  ##
  elemType1 = mesh.ElemType(elemCode=CAX4H, elemLibrary=STANDARD)
  elemType2 = mesh.ElemType(elemCode=CAX3, elemLibrary=STANDARD)
  faces = f.findAt(((0.042303, 0.006937, 0.0), ))
  pickedRegions =(faces, )
  p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2))
  ## Assign element type to the steel
  ##
  elemType1 = mesh.ElemType(elemCode=CAX4I, elemLibrary=STANDARD)
  elemType2 = mesh.ElemType(elemCode=CAX3, elemLibrary=STANDARD)
  faces = f.findAt(((0.043333, 0.003333, 0.0), ))
  pickedRegions =(faces, )
  p.setElementType(regions=pickedRegions, elemTypes=(elemType1, elemType2))
  ☺ 单元类型设置及不同材料面指定
  ## Generate mesh
  ##
  p.generateMesh()
  ☺ 划分当前网格
  session.viewports['Viewport: 1'].assemblyDisplay.setValues(mesh=OFF)
  session.viewports['Viewport: 1'].assemblyDisplay.meshOptions.setValues(
  meshTechnique=OFF)
  #--------------------------------------------------------------------------------------------------
  ## Create job
  ☺ 创建job设置
  mdb.Job(name='Mount', model='Model-1',
  description='Axisymmetric mount analysis under axial loading',
  modelPrint=ON)
  a = mdb.models['Model-1'].rootAssembly
  a.regenerate()
  ##
  ## Save model database
  ##
  mdb.saveAs('Mount')
  #--------------------------------------------------------------------------------------------------
  小结:
  1.Abaqus的python看似繁琐,但有着其固定、严谨的结构并不难理解
  2.pthon的顺序与cae的模块操作顺序基本一致
  3.这个例子的python脚本基本与cae自动生成的python脚本基本相同,完全可以通过交互式修改cae生成的python.rpy文件来建立自己的python脚本,免去了许多学习abaqus python命令格式的痛苦。

页: [1]
查看完整版本: Abaqus里应用Python的一些技巧