forked from INSIGNEO/CT2S-Abaqus-PostProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageEMinforeachnode.py
More file actions
56 lines (46 loc) · 1.93 KB
/
AverageEMinforeachnode.py
File metadata and controls
56 lines (46 loc) · 1.93 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
def read_data(file_name):
with open(file_name, 'r') as file:
lines = file.readlines()
data = []
for line in lines:
split_line = line.split()
if len(split_line) < 2:
print(f"Skipping line due to insufficient data: '{line.strip()}'")
continue
try:
node_data = [int(split_line[0]), float(split_line[1])]
data.append(node_data)
except ValueError:
print(f"Skipping line due to a conversion error: '{line.strip()}'")
continue
return data
def calculate_average_strains(data):
strains = {}
for node_data in data:
node, strain = node_data
if node in strains:
strains[node].append(strain)
else:
strains[node] = [strain]
average_strains = {}
for node, strain_values in strains.items():
average_strains[node] = sum(strain_values) / len(strain_values)
return average_strains
def write_data(file_name, data):
with open(file_name, 'w') as file:
for node, average_strain in data.items():
line = "{:d}\t{:15.10E}\n".format(node, average_strain)
file.write(line)
import os
def process_files_average_emin(input_dir, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
for file in files:
input_file_path = os.path.join(input_dir, file)
output_file_name = f"{os.path.splitext(file)[0]}_averaged.txt"
output_file_path = os.path.join(output_dir, output_file_name)
data = read_data(input_file_path)
average_strains = calculate_average_strains(data)
write_data(output_file_path, average_strains)
print(f"Processed {len(files)} files, and saved them to '{output_dir}'.")