-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_febio.py
258 lines (191 loc) · 8.8 KB
/
_febio.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'''
*
* _febio
* GEOVAR FEBio MODULE
*
* Module designed to delegate "febio-specific" functions or operations
* Most of these functions deal with reading/writing/operating/executing FEBio config files
*
*
* AUTHOR : Fluvio L. Lobo Fenoglietto
* DATE : May 31st, 2019
*
'''
import numpy as np
from lxml import etree
# ************************************************************************
# FUNCTIONS =============================================================*
# ************************************************************************
def read_febio_file( febio_filename ):
'''
READ FEBio FILE
'''
#print( '\n' )
#print( "READ FEBio INFO..." )
#file = self.input + febio_filename
file = febio_filename
#print(file)
febio_doc = etree.parse( file )
febio_root = febio_doc.getroot()
root_len = len(febio_root)
#print( root_len )
for i in range( 0, root_len ):
if febio_root[i].tag == 'Geometry':
geo_index = i
# extracting parts from the geometry section
geo = febio_root[geo_index]
geo_len = len( geo )
#return febio_doc, febio_root, geo, geo_index, geo_len
#self.febio_doc = febio_doc
#self.febio_root = febio_root
return geo, febio_doc, febio_root
# ------------------------------------------------------------------------------------------------------------ #
def write_febio_file( febio_filename ):
'''
WRITE FEBio FILE
'''
# ------------------------------------------------------------------------------------------------------------ #
def get_febio_data( geo, _iter ):
'''
EXTRACT FEBio FILE DATA
'''
#print( '\n' )
#print( "EXTRACT FEBio INFO..." )
# the inputs of this program should be the XML components of interest (e.g.: Geometry)
geo_len = len(geo)
fdata = {}
fdata[str(_iter)] = {}
fdata[str(_iter)]['nodes'] = []
nodeset = {}
fdata[str(_iter)]['nodeset'] = nodeset
for i in range( 0, geo_len ):
#print( i )
#print( geo[i].tag )
if geo[i].tag == 'Nodes': # -------------------------------------------------------------------------- #
#print( "> Gathering Nodes..." )
nodes_id, nodes = get_nodes( geo, i )
if geo[i].tag == 'Elements': # ----------------------------------------------------------------------- #
#print( "> Gathering Elements..." )
elements_id, elements = get_elements( geo, i)
if geo[i].tag == 'NodeSet': # ------------------------------------------------------------------------ #
#print( "> Gathering NodeSet(s)..." )
nodeset = get_nodeset( geo, i, nodes, nodeset )
# update structure
fdata[str(_iter)]['nodes'] = nodes
fdata[str(_iter)]['nodeset'] = nodeset
return fdata, nodes_id, nodes, nodeset
# ------------------------------------------------------------------------------------------------------------ #
def get_nodes( geo, index ):
'''
GET NODE DATA
'''
nodes_obj = geo[index]
nodes_len = len( nodes_obj )
# initializing numpy arrays
nodes_id = []
nodes = np.zeros(( nodes_len, 3 ), dtype=float)
for j in range( 0, nodes_len ):
# extracting raw data
## extracting nodes
#fdata['baseline']['geo']['nodes']['id'].append( nodes[j].attrib['id'] )
#fdata['baseline']['geo']['nodes']['text'].append( nodes[j].text )
# tranform into numeric values for proper manipulation
nodes_id.append(int(nodes_obj[j].attrib['id']))
nodes[j] = nodes_obj[j].text.split(',')
return nodes_id, nodes
# ------------------------------------------------------------------------------------------------------------ #
def get_elements( geo, index):
'''
GET ELEMENT DATA
'''
elements_obj = geo[index]
elements_len = len( elements_obj )
# using attributes to ensure proper extraction
# this will be implemented now and should be standard in the future
if elements_obj.attrib['type'] == 'tet4':
#print( ">> The mesh uses tets of 4 nodes" )
nodes_per_tet = 4
# initializing numpy arrays
elements_id = []
elements = np.zeros(( elements_len, nodes_per_tet ), dtype=int)
for j in range( 0, elements_len ):
# extracting raw data
## extracting elements
#fdata['baseline']['geo']['elements']['id'].append( elements[j].attrib['id'] )
#fdata['baseline']['geo']['elements']['text'].append( elements[j].text )
# tranform into numeric values for proper manipulation
elements_id.append(int(elements_obj[j].attrib['id']))
elements[j] = elements_obj[j].text.split(',')
return elements_id, elements
# ------------------------------------------------------------------------------------------------------------ #
def get_nodeset( geo, index, nodes, nodeset ):
'''
GET NODESET DATA
'''
nodeset_obj = geo[index]
nodeset_len = len(nodeset_obj)
# using the attributes
nodeset_type = nodeset_obj.attrib['name']
nodeset_len = len(nodeset_obj)
# nodeset index
nodeset_index = len(nodeset)
nodeset[str(nodeset_index)] = {}
if nodeset_type[:-2] == 'FixedDisplacement': # ----------------------------------------------------------- #
nodeset[str(nodeset_index)]['type'] = 'fixdisp{}'.format(nodeset_type[len(nodeset_type)-2:])
if nodeset_type[:-2] == 'PrescribedDisplacement': # ------------------------------------------------------ #
nodeset[str(nodeset_index)]['type'] = 'presdisp{}'.format(nodeset_type[len(nodeset_type)-2:])
elif nodeset_type[:-2] != 'FixedDisplacement' and nodeset_type[:-2] != 'PrescribedDisplacement':
print('The current version of GEOVAR does not support FEBios NoseSet:Type {}'.format( nodeset_type ))
nodeset[str(nodeset_index)]['id'] = []
nodeset[str(nodeset_index)]['nodes'] = []
for j in range( 0, nodeset_len ):
nodeset[str(nodeset_index)]['id'].append( nodeset_obj[j].attrib['id'] )
nodeset[str(nodeset_index)]['nodes'].append( nodes[ int(nodeset_obj[j].attrib['id'])-1] )
return nodeset
# ------------------------------------------------------------------------------------------------------------ #
def nodeset_match( nodeset ):
'''
MATCHES CORRESPONDING NODES ON THE BASIS OF COORDINATE EXACT SIMILARITIES
'''
# -------------------------------------------------------------------------------------------------------- # here we identify the relation between the nodes associated with each boundary condition
nodeset_len = len(nodeset)
match_type = []
match_axis = []
match_mean = []
match_sd = []
for i in range( 0, nodeset_len ):
# determine by average
nodeset_nodes = nodeset[str(i)]['nodes']
nodes_len = len(nodeset_nodes)
nodes_sum = np.zeros((3))
nodes_mean = np.zeros((3))
nodes_sd = np.zeros((3))
for j in range( 0, nodes_len ):
nodes_sum = nodes_sum + nodeset_nodes[j]
nodes_mean = nodes_sum / nodes_len
for j in range( 0, nodes_len ):
nodes_sd = nodes_sd + ( nodeset_nodes[j] - nodes_mean )**2
nodes_sd = np.sqrt( nodes_sd / nodes_len )
# determine smallest deviation
nodes_sd_min = np.min( nodes_sd )
#print( nodes_sd_min )
# determine if smallest deviation meets tolerance
tol = 1e-10 # --------------------------------------------------------------------> this value needs to be extracted, used as an input
if np.abs(nodes_sd_min) < tol:
# capturing index of smallest deviation
# capturing the average value of the smallest deviation
for k in range( 0, len(nodes_sd) ):
if nodes_sd[k] == nodes_sd_min:
min_index = k
break
nodes_mean_min = nodes_mean[min_index]
# results
match_type.append( nodeset[str(i)]['type'][:-2] )
match_axis.append( min_index )
match_mean.append( nodes_mean_min )
match_sd.append( nodes_sd_min )
print( match_type, match_axis, match_mean, match_sd )
# -------------------------------------------------------------------------------------------------------- # here we identify the nodes that have that relation on the next part
# import nodes from vtk mesh
# generate new nodeset structure
return nodeset