-
Notifications
You must be signed in to change notification settings - Fork 2
/
3d.py
178 lines (140 loc) · 6.19 KB
/
3d.py
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
from abaqus import *
from abaqusConstants import *
import os
import numpy as np
import time
import sys
analysisType = sys.argv[-1]
partName, instanceName = 'femur_cartilage', 'femur_cartilage-1'
inputPath = 'input_3d.csv'
if analysisType == 'hf':
outputPath = 'output_3d_highfidelity.csv'
jobName, modelName = 'Job-1', 'OpenKnee_model'
else:
outputPath = 'output_3d_lowfidelity.csv'
jobName, modelName = 'Job-1_lowfidelity', 'OpenKnee_model_lowfidelity'
class main_tools(object):
def __init__(self, jobName, partName, instanceName, modelName, mdb, session, viewportObj1 = 'Viewport: 1'):
self.jobName = jobName
self.odbName = jobName + '.odb'
self.partName = partName
self.modelName = modelName
self.instanceName = instanceName
self.mdb = mdb
self.session = session
self.viewportObj1 = self.session.viewports[viewportObj1]
def job_submit(self, jobName):
FirstJob = self.mdb.jobs[jobName]
FirstJob.submit(consistencyChecking=OFF)
FirstJob.waitForCompletion()
def open_odb(self, odbName, readOnly = True):
try:
return self.session.openOdb(name = odbName, readOnly = readOnly)
except:
time.sleep(5)
print 'open_odb() did not work.'
return self.session.openOdb(name = odbName, readOnly = readOnly)
def close_odb(self, odbName = ''):
if odbName == '': odbName = self.odbName
try:
self.session.odbs[odbName].close()
except:
print 'close_odb did not work.'
def save_and_close_odb(self, odbObj):
odbObj.save()
odbObj.close()
def output_values(self, odb, stepName = 'Main', frameNum = -1, parameterName = 'SDV3'):
return odb.steps[stepName].frames[frameNum].fieldOutputs[parameterName].values
def edit_node_by_offset(self ,offsetPars):
self.mdb.meshEditOptions.setValues(enableUndo=True, maxUndoCacheElements=0.5)
partObj = self.mdb.models[self.modelName].parts[self.partName]
nodeObj = partObj.nodes
num = 0
for i in offsetPars:
num += 1
if num % 3 == 1:
nodes = i - 1
elif num % 3 == 2:
u1 = i
elif num % 3 == 0:
u2 = i
partObj.editNode(nodes=nodeObj[nodes], offset1=-u1, offset2=-u2, projectToGeometry=OFF)
def extract_coords_values(self, frameNum = -1, stepName = 'Main', odb = ''):
if odb == '': odb = self.open_odb(self.odbName)
temp = []
dispVal = self.output_values(odb, stepName, frameNum, 'COORD')
for s in dispVal:
temp.append(s.nodeLabel)
temp.append(s.dataDouble[0])
temp.append(s.dataDouble[1])
return temp
def extract_nodal_values(self, frameNum = -1, stepName = 'Main', odb = ''):
if odb == '': odb = self.open_odb(self.odbName)
temp = []
dispVal = self.output_values(odb, stepName, frameNum, 'COORD')
for s in dispVal:
temp.append(s.nodeLabel)
temp.append(s.dataDouble[0])
temp.append(s.dataDouble[1])
return temp
def integration_points_values(self, parameters = ['LE22'], frameNum = -1, stepName = 'UC', odb = ''):
if odb == '': odb = self.open_odb(self.odbName)
temp = []
for sdv in parameters:
temp.append([])
outputValues = self.output_values(odb, stepName, frameNum, sdv)
for outputVal in outputValues:
temp[-1].append([outputVal.data, outputVal.elementLabel, outputVal.integrationPoint])
return temp
os.chdir(r"C:\temp\HybridML")
FileName = 'Open_knee_native'
openMdb(pathName = FileName + '.cae')
mdb.saveAs(pathName = FileName + '-Backup.cae')
openMdb(pathName=FileName + '.cae')
modelObj = mdb.models[modelName]
mt = main_tools(jobName, partName, instanceName, modelName, mdb, session) # main tools of Abaqus
iterationNumber = 100
for i in [inputPath, outputPath]: open(i, 'w').close()
np.random.seed(14)
cf2List = np.random.uniform(-900.0,-400.0,iterationNumber)
timeSpan = 10
t = 0
i = 0
for cf2 in (cf2List):
i += 1
modelObj.loads['Static_Load'].setValues(cf2=cf2)
modelObj.steps['LOAD'].setValues(timePeriod=timeSpan,
initialInc=timeSpan/2,
maxInc=timeSpan)
t0 = time.time()
mt.job_submit(jobName)
t1 = time.time()
with open('time_' + outputPath, 'a') as outputFile: np.savetxt(outputFile, [t1 - t0], delimiter=",")
odb = mt.open_odb(mt.odbName)
frameObjT = odb.steps['LOAD'].frames[-1]
if analysisType == 'hf':
temp = np.array([[cf2]], dtype="float32")
with open(inputPath, 'a') as inputFile: np.savetxt(inputFile, temp, delimiter=",")
region = odb.rootAssembly.instances['PART-1-1'].elementSets['TIBIA_CARTILAGE_MED-1__PICKEDSET26']
Sarrays = frameObjT.fieldOutputs['S'].getSubset(region = region, position = ELEMENT_NODAL).values
LEarrays = frameObjT.fieldOutputs['LE'].getSubset(region = region, position = ELEMENT_NODAL).values
temp = []
for (S, LE) in zip(Sarrays, LEarrays):
temp.append(np.array([S.data[0],
S.data[1],
S.data[2],
LE.data[0],
LE.data[1],
LE.data[2]], dtype="float32"))
temp = np.array(temp, dtype="float32")
with open(outputPath, 'a') as outputFile: np.savetxt(outputFile, temp, delimiter=",")
else:
region = odb.rootAssembly.instances['PART-1-1'].nodeSets['TIBIA_CARTILAGE_MED-1__PICKEDSET26']
Uarrays = frameObjT.fieldOutputs['U'].getSubset(region = region, position = NODAL).values
temp = []
for U in Uarrays:
temp.append(np.array(U.data[0:3], dtype="float32"))
temp = np.array(temp, dtype="float32")
with open(outputPath, 'a') as inputFile: np.savetxt(inputFile, temp, delimiter=",")
print i, temp.shape
mt.close_odb()