forked from ShengqiangSKR/ABAQUS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurfIO.py
More file actions
205 lines (168 loc) · 6.61 KB
/
Copy pathSurfIO.py
File metadata and controls
205 lines (168 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# !/usr/bin/python
# -*-coding: UTF-8-*-
# Du. Shengqiang
#
import os
import sys
import copy
# path definition
#thisPath = os.path.abspath(__file__)
#thisDir = os.path.dirname(thisPath)
#修改以下定义
#1.原始inp名称,将其放在工作目录下
filename='Job-single.inp'
#2.输出文件名称
outPutFileName='Job-single_modify.inp'
#以下无需修改
def FormatTrans(data=[],datatype='',to='',output=''):
# keyword to = HYPERMESH or ABAQUS
id_aba,id_hm=TitleMatch(data=data, symbol='*Surface, type=ELEMENT,', datatype = datatype)
if to=='HYPERMESH':
idall=id_aba
del id_hm
newdata= copy.deepcopy(data)
appenddata_dict={}
for idx in idall:
#获得组成该面的S类型
newdata,stype=FindDefBlow(data=newdata,st=idx)
#对所有S类型对应的单元进行查找
#indexdic format -> {'S2':45}
indexdict=FindDefUp(data=newdata,st=idx,ref=stype)
indexcp=sorted(indexdict.items(), key=lambda d: d[1], reverse=False)
#print(indexcp)
#对数据进行重写
newrow=[]#单个面的所有组成信息 -> 45, S2```
for i in range(len(indexcp)):
try:
#_a=indexcp[i][1]+1
#_b=indexcp[i+1][1]
#print(_a,_b)
_numberow=newdata[indexcp[i][1]+1:indexcp[i+1][1]]
except:
_numberow = newdata[indexcp[i][1] + 1:idx]
#abaqus keyword 支持用generate的方法生成更加简洁的序列,需要区分这种情况
if newdata[indexcp[i][1]][-8:]=='generate':
for txt in _numberow:
lbdata=txt.split(',')
if len(lbdata[-1])==0:
del lbdata[-1]
#按第三位递进
for n in range(int(lbdata[0]),int(lbdata[1])+1,int(lbdata[2])):
newlb = str(n) + ', ' + indexcp[i][0]
newrow.append(newlb)
#以下是常规情况
else:
for txt in _numberow:
labellist=txt.split(',')
if len(labellist[-1])==0:
del labellist[-1]
for lb in labellist:
newlb=lb+', '+indexcp[i][0]
newrow.append(newlb)
#print(newrow)
appenddata_dict[idx]= newrow
#需要替换掉原来的内容或者生成新的文件
#1.去除internal set定义行,将其转换为None,方便后续删除
newdata[indexcp[0][1]:idx]=[None]*(idx-indexcp[0][1])
#2.将整个list拆开
print('Start Main Process!~')
idall.insert(0,-1)
split_id=idall
block_all=[]
print('Step1: Seperate Source Data into Blocks')
for count_ in range(len(split_id)-1):
block=newdata[split_id[count_]+1:split_id[count_+1]+1]
block_all.append(block)
block_all.append(newdata[split_id[-1]+1:])
if len(appenddata_dict.values())==len(block_all)-1:
print('Len Match')
FinalData=[]
appenddata_cp=sorted(appenddata_dict.items(), key=lambda d: d[0], reverse=False)
#print(block_all[-2])
#print(appenddata_cp[-2][1])
print('Step2: Join Blocks together')
for i in range(len(appenddata_cp)):
FinalData += block_all[i]+appenddata_cp[i][1]
FinalData+=block_all[-1]
FinalData=[x+'\n' for x in FinalData if x!=None]
WriteData(FinalData,output)
#向上寻找各set中包含的单元
#暂用方法,暂时不做修改
def FindDefUp(data,st,ref):
facename=data[st].split('name=')[1]
indexdict={}
while ref!=[]:
if facename in data[st-1]:
_start=data[st-1].index(facename)+len(facename)
_stype=data[st-1][_start+1:_start+3]
print('Find Stype Definition Line: {} -> line {}'.format(_stype,st-1))
ref.remove(_stype)
indexdict[data[st-1][_start+1:_start+3]]=st-1
st-=1
print('--------------Over--------------')
print('\n'*2)
return indexdict
#寻找面定义下,该面的组成set
#通用方法,无需修改
def FindDefBlow(data,st):
facename=data[st].split('name=')[1]
print ('----------Surface Info----------')
print ('Surface Name -> {}'.format(facename))
stype=[]
print('Include Internal Sets: ')
while facename in data[st+1]:
print(data[st+1])
stype.append(data[st+1][-2:])
data[st+1]=None
st+=1
print ('Stype: {}'.format(stype))
print ('----------Detail----------')
return data, stype
def TitleMatch(data=[], symbol='', datatype = ''):
idx_hpmsh=[]
idx_abaqus=[]
# query *Surface definition
for i in range(len(data)):
if symbol in data[i] and datatype == 'HPMSH':
idx_hpmsh.append(i)
elif symbol in data[i] and datatype == 'ABAQUS':
idx_abaqus.append(i)
elif symbol in data[i] and datatype == '':
# we need judge the data type
if i!=len(data)-1:
if data[i].split('name=')[1] in data[i+1]:
idx_abaqus.append(i)
else:
idx_hpmsh.append(i)
else:
continue
print('Find All Surface Definition Lines')
if datatype=='ABAQUS':
print('Find {} surfaces named in ABAQUS format'.format(len(idx_abaqus)))
print(idx_abaqus)
else:
print('Find {} surfaces named in HYPERMESH format'.format(len(idx_hpmsh)))
print(idx_hpmsh)
return idx_abaqus, idx_hpmsh
def WriteData(data,file):
f=open(file,'w')
f.writelines(data)
print('Modification Process Succeeds !')
print('A New .inp file has been generated! -> {}'.format(file))
f.close()
# read .inp file
def ReadData(file):
with open(file) as f:
data=f.readlines()
f.close()
data=map(StripFuc,data)
return data
#
def StripFuc(x):
return x.strip('\n')
#main
file=filename
write_to_file=outPutFileName
thisDir=os.getcwd()
dataDir=os.path.join(thisDir,file)
FormatTrans(data=ReadData(dataDir),datatype='ABAQUS',to='HYPERMESH',output=write_to_file)