如何通过Python在for循环中保存数据

时间:2016-07-23 07:38:57

标签: python stack-overflow

我是Python编程的新手。我想保存字段输出请求

[mdb.models['Model-1'].fieldOutputRequests['F-Output-1'].setValues(variables=('S', 'U', 'SF'))]

通过改变边界位移作为每个循环j的文本文件。 任何人都可以帮我解决这个问题吗? 我附上了代码。

谢谢! D. Bhuiyan

from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *
### PART ###
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=2.0)
mdb.models['Model-1'].sketches['__profile__'].Spot(point=(0.0, 0.0))
mdb.models['Model-1'].sketches['__profile__'].Spot(point=(1.0, 0.0))
mdb.models['Model-1'].sketches['__profile__'].Line(point1=(0.0, 0.0), point2=(1.0, 0.0))
mdb.models['Model-1'].sketches['__profile__'].HorizontalConstraint(addUndoState=False, entity=mdb.models['Model-1'].sketches['__profile__'].geometry[2])
mdb.models['Model-1'].Part(dimensionality=TWO_D_PLANAR, name='Part-1', type=DEFORMABLE_BODY)
mdb.models['Model-1'].parts['Part-1'].BaseWire(sketch=mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']

### MATERIAL & SECTION ###
mdb.models['Model-1'].Material(name='Material-1')
mdb.models['Model-1'].materials['Material-1'].Density(table=((7800.0, ), ))
mdb.models['Model-1'].materials['Material-1'].Elastic(table=((206000000000.0, 0.3), ))

### SET & SURFACE ###   
mdb.models['Model-1'].CircularProfile(name='Circ_profile', r=0.015)
mdb.models['Model-1'].BeamSection(consistentMassMatrix=False, integration=
    DURING_ANALYSIS, material='Material-1', name='Beam_section', poissonRatio=0.0, profile='Circ_profile', temperatureVar=LINEAR)
mdb.models['Model-1'].parts['Part-1'].Set(edges=
    mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ), name='Beam')
mdb.models['Model-1'].parts['Part-1'].SectionAssignment(offset=0.0, offsetField='', offsetType=MIDDLE_SURFACE, region=
    mdb.models['Model-1'].parts['Part-1'].sets['Beam'], sectionName='Beam_section', thicknessAssignment=FROM_SECTION)
mdb.models['Model-1'].rootAssembly.DatumCsysByDefault(CARTESIAN)
mdb.models['Model-1'].rootAssembly.Instance(dependent=ON, name='Part-1-1', part=mdb.models['Model-1'].parts['Part-1'])

### STEP, BC & LOAD ###
mdb.models['Model-1'].StaticStep(description=
    'Apply Displacement in the DOFs at node', initialInc=1.0, name='Step-1', previous='Initial')
mdb.models['Model-1'].fieldOutputRequests['F-Output-1'].setValues(variables=('S', 'U', 'SF'))
del mdb.models['Model-1'].historyOutputRequests['H-Output-1']

mdb.models['Model-1'].parts['Part-1'].seedEdgeBySize(constraint=FINER, deviationFactor=0.1, edges=
    mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ), size=2.0)
mdb.models['Model-1'].parts['Part-1'].setElementType(elemTypes=(ElemType(
    elemCode=B23, elemLibrary=STANDARD), ), regions=(mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ), ))
mdb.models['Model-1'].parts['Part-1'].setElementType(elemTypes=(ElemType(
    elemCode=B23, elemLibrary=STANDARD), ), regions=(mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ), ))
mdb.models['Model-1'].parts['Part-1'].generateMesh()
mdb.models['Model-1'].rootAssembly.regenerate()
del mdb.models['Model-1'].rootAssembly.features['Part-1-1']

mdb.models['Model-1'].rootAssembly.Instance(dependent=ON, name='Part-1-1', part=mdb.models['Model-1'].parts['Part-1'])
mdb.models['Model-1'].parts['Part-1'].deleteMesh(regions=mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ))
mdb.models['Model-1'].parts['Part-1'].seedEdgeByNumber(constraint=FINER, edges=
    mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ), number=2)
mdb.models['Model-1'].parts['Part-1'].setElementType(elemTypes=(ElemType(
    elemCode=B23, elemLibrary=STANDARD), ), regions=(mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#1 ]', ), ), ))
mdb.models['Model-1'].parts['Part-1'].generateMesh()
mdb.models['Model-1'].rootAssembly.regenerate()
del mdb.models['Model-1'].rootAssembly.features['Part-1-1']

mdb.models['Model-1'].rootAssembly.Instance(dependent=ON, name='Part-1-1', 
    part=mdb.models['Model-1'].parts['Part-1'])
mdb.models['Model-1'].parts['Part-1'].Set(name='Sensor@node2', nodes=
    mdb.models['Model-1'].parts['Part-1'].nodes.getSequenceFromMask(mask=('[#2 ]', ), ))    
mdb.models['Model-1'].rootAssembly.regenerate()
mdb.models['Model-1'].parts['Part-1'].sets.changeKey(fromName='Sensor@node2', toName='node2')
mdb.models['Model-1'].parts['Part-1'].Set(name='node1', nodes=
    mdb.models['Model-1'].parts['Part-1'].nodes.getSequenceFromMask(mask=('[#1 ]', ), ))
mdb.models['Model-1'].parts['Part-1'].Set(name='node3', nodes=
    mdb.models['Model-1'].parts['Part-1'].nodes.getSequenceFromMask(mask=('[#4 ]', ), ))

mdb.models['Model-1'].rootAssembly.regenerate()

## for loop
A1=([0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0])
for j in range(len(A1)):
    f=A1[j]
mdb.models['Model-1'].DisplacementBC(amplitude=UNSET, createStepName='Step-1', 
    distributionType=UNIFORM, fieldName='', fixed=OFF, localCsys=None, name=
    'BC-1', region=mdb.models['Model-1'].rootAssembly.instances['Part-1-1'].sets['node1'], u1=f[0], u2=f[1], ur3=f[2])
mdb.models['Model-1'].DisplacementBC(amplitude=UNSET, createStepName='Step-1', 
    distributionType=UNIFORM, fieldName='', fixed=OFF, localCsys=None, name=
    'BC-2', region= mdb.models['Model-1'].rootAssembly.instances['Part-1-1'].sets['node2'], u1=f[3], u2=f[4], ur3=f[5])
mdb.models['Model-1'].DisplacementBC(amplitude=UNSET, createStepName='Step-1', 
    distributionType=UNIFORM, fieldName='', fixed=OFF, localCsys=None, name=
    'BC-3', region=mdb.models['Model-1'].rootAssembly.instances['Part-1-1'].sets['node3'], u1=f[6], u2=f[7], ur3=f[8])

mdb.models['Model-1'].parts['Part-1'].assignBeamSectionOrientation(method=
    N1_COSINES, n1=(0.0, 0.0, -1.0), region=mdb.models['Model-1'].parts['Part-1'].sets['Beam'])

## Job ##
mdb.Job(atTime=None, contactPrint=OFF, description='', echoPrint=OFF, 
    explicitPrecision=SINGLE, getMemoryFromAnalysis=True, historyPrint=OFF, 
    memory=90, memoryUnits=PERCENTAGE, model='Model-1', modelPrint=OFF, name='Job-1', nodalOutputPrecision=SINGLE, queue=None, resultsFormat=ODB, 
    scratch='', type=ANALYSIS, userSubroutine='', waitHours=0, aitMinutes=0)

1 个答案:

答案 0 :(得分:0)

创建文件的基础是:

file = open("newfile.txt", "w")
file.write("This is your first line")
file.write("This is your second line")
file.close()

在这种情况下:

file = open("newfile.txt", "w")
for j in range(len(A1)):
to_write = mdb.models['Model-1'].fieldOutputRequests['F-Output-1'].setValues(variables=('S', 'U', 'SF')) del mdb.models['Model-1'].historyOutputRequests['H-Output-1']
file.write(to_write)

你应该明确边界位移的意思吗?