-
Notifications
You must be signed in to change notification settings - Fork 46
/
beso_separate.py
173 lines (161 loc) · 6.92 KB
/
beso_separate.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
# The aim of this script is to read .inp file and separate adjacend elements, so that every element has its own nodes,
# thus nodal results from CalculiX are not averaged between neighbouring elements.
def separating(file_name, nodes={}):
# reading node position if it is not defined
if not nodes:
if file_name[-4:] == ".inp":
file_nameR = file_name
else:
file_nameR = file_name + ".inp"
fR = open(file_nameR, "r")
nodes = {} # dict with nodes position
model_definition = True
read_node = False
line = "\n"
include = ""
while line != "":
if include:
line = f_include.readline()
if line == "":
f_include.close()
include = ""
line = fR.readline()
else:
line = fR.readline()
if line.strip() == '':
continue
elif line[0] == '*': # start/end of a reading set
if line[0:2] == '**': # comments
continue
if line[:8].upper() == "*INCLUDE":
start = 1 + line.index("=")
include = line[start:].strip().strip('"')
f_include = open(include, "r")
continue
read_node = False
# reading nodes
if (line[:5].upper() == "*NODE") and (model_definition is True):
read_node = True
elif read_node is True:
line_list = line.split(',')
number = int(line_list[0])
x = float(line_list[1])
y = float(line_list[2])
z = float(line_list[3])
nodes[number] = [x, y, z]
elif line[:5].upper() == "*STEP":
model_definition = False
fR.close()
# creating new file from old one with separated elements
if file_name[-4:] == ".inp":
file_nameR = file_name
file_nameW = file_name[:-4] + "_separated.inp"
else:
file_nameR = file_name + ".inp"
file_nameW = file_name + "_separated.inp"
fR = open(file_nameR, "r")
fW = open(file_nameW, "w")
nn_added = max(nodes.keys())
number_of_nodes = 0
rewriting_nodes = False
left_original = set()
for line in fR:
if line[0:2] == '**': # comments
fW.write(line)
continue
# define nodes and equations after *ELEMENT block
elif line[0] == "*" and rewriting_nodes is True:
number_of_nodes = 0
rewriting_nodes = False
fW.write("** Nodes added by optimization to separate nodal stresses\n")
fW.write("*NODE\n")
for [nn, nn_added] in coincident_nodes:
fW.write("{}, {}, {}, {}\n" .format(nn_added, nodes[nn][0], nodes[nn][1], nodes[nn][2]))
fW.write(" \n")
fW.write("**Equations added by optimization to separate nodal stresses\n")
fW.write("*EQUATION\n")
if only_translations is True:
dofs = [1, 2, 3]
else:
dofs = [1, 2, 3, 4, 5, 6]
for [nn, nn_added] in coincident_nodes:
for dof in dofs:
fW.write("2\n")
fW.write("{}, {}, {}, {}, {}, {} \n" .format(nn_added, dof, 1, nn, dof, -1))
fW.write(" \n")
# rewriting associated nodes under *ELEMENT card
elif rewriting_nodes is True:
line_list = line.split(",")
if len(line_list) > 1:
if line_list[-1].strip() == "":
line_list.pop()
if second_line is False:
line_new = line_list[0] + ", "
start_position = 1
if len(line_list) < number_of_nodes + 1:
second_line = True
else:
line_new = ""
start_position = 0
second_line = False
for nn in line_list[start_position:]:
nn = int(nn)
if (only_translations is False) and (nn not in left_original): # write 1 original nn for shell/beam
left_original.add(nn)
line_new += str(nn) + ", "
else:
nn_added += 1
line_new += str(nn_added) + ", "
coincident_nodes.append((nn, nn_added))
fW.write(line_new[:-2] + "\n")
continue
# find *ELEMENT card
if line[:8].upper() == "*ELEMENT":
line_list = line[8:].upper().split(',')
for line_part in line_list:
if line_part.split('=')[0].strip() == "TYPE":
elm_type = line_part.split('=')[1].strip()
if elm_type in ["S3", "CPS3", "CPE3", "CAX3"]:
number_of_nodes = 3
only_translations = False
elif elm_type in ["S6", "CPS6", "CPE6", "CAX6"]:
number_of_nodes = 6
only_translations = False
elif elm_type in ["S4", "S4R", "CPS4", "CPS4R", "CPE4", "CPE4R", "CAX4", "CAX4R"]:
number_of_nodes = 4
only_translations = False
elif elm_type in ["S8", "S8R", "CPS8", "CPS8R", "CPE8", "CPE8R", "CAX8", "CAX8R"]:
number_of_nodes = 8
only_translations = False
elif elm_type == "C3D4":
number_of_nodes = 4
only_translations = True
elif elm_type == "C3D10":
number_of_nodes = 10
only_translations = True
elif elm_type in ["C3D8", "C3D8R", "C3D8I"]:
number_of_nodes = 8
only_translations = True
elif elm_type in ["C3D20", "C3D20R", "C3D20RI"]:
number_of_nodes = 20
only_translations = True
elif elm_type == "C3D6":
number_of_nodes = 6
only_translations = True
elif elm_type == "C3D15":
number_of_nodes = 15
only_translations = True
elif elm_type == ["B31", "B31R", "T3D2"]:
number_of_nodes = 2
only_translations = False
elif elm_type == ["B32", "B32R", "T3D3"]:
number_of_nodes = 3
only_translations = False
coincident_nodes = []
break
if number_of_nodes:
rewriting_nodes = True
second_line = False
fW.write(line)
fR.close()
fW.close()