Расчёт абсолютных погрешностей вычислений темератур

This commit is contained in:
2025-05-16 12:47:30 +03:00
parent 409b3ecd97
commit e06d350fd3
3 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
import re
def parse_temp_file(temp_file):
node_temps = {}
with open(temp_file, 'r') as f:
for line in f:
match = re.match(r'^\s*(\d+)\s+([\d.]+)\s*$', line.strip())
if match:
node = int(match.group(1))
temp = float(match.group(2))
node_temps[node] = temp
return node_temps
def parse_node_file(node_file):
node_coords = {}
with open(node_file, 'r') as f:
for line in f:
match = re.match(r'^\s*(\d+)\s+([-\d.Ee]+)\s+([-\d.Ee]+)\s+([-\d.Ee]+)\s+[-\d.]+', line.strip())
if match:
node = int(match.group(1))
x = float(match.group(2))
y = float(match.group(3))
node_coords[node] = (x, y)
return node_coords
def process_files(temp_file, node_file, output_file):
node_temps = parse_temp_file(temp_file)
node_coords = parse_node_file(node_file)
with open(output_file, 'w') as f:
for node in node_temps:
if node in node_coords:
x, y = node_coords[node]
temp = node_temps[node]
f.write(f"100 {int(x)} {int(y)} {temp}\n")
else:
print(f"Warning: Node {node} not found in coordinate file")
temp_file = "temp_data.txt"
node_file = "node_data.txt"
output_file = "ansys.txt"
process_files(temp_file, node_file, output_file)
print(f"Output written to {output_file}")

View File

@@ -0,0 +1,60 @@
import math
def read_points(filename):
points = []
with open(filename, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) == 4:
try:
x = float(parts[1])
y = float(parts[2])
temp = float(parts[3])
points.append((x, y, temp))
except ValueError:
print(f"Warning: Skipping invalid line in {filename}: {line.strip()}")
return points
def euclidean_distance(p1, p2):
return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
def find_closest_points(myprog_points, ansys_points):
differences = []
for myprog_point in myprog_points:
myprog_x, myprog_y, myprog_temp = myprog_point
min_distance = float('inf')
temp_diff = None
closest_point = None
# Find closest point in ansys.txt
for ansys_point in ansys_points:
ansys_x, ansys_y, ansys_temp = ansys_point
distance = euclidean_distance((myprog_x, myprog_y), (ansys_x, ansys_y))
if distance < min_distance:
min_distance = distance
temp_diff = abs(myprog_temp - ansys_temp)
closest_point = ansys_point
if temp_diff is not None:
differences.append(temp_diff)
print(f"Myprog point ({myprog_x}, {myprog_y}, {myprog_temp}) -> Closest Ansys point ({closest_point[0]}, {closest_point[1]}, {closest_point[2]}) -> Temp diff: {temp_diff}")
return differences
def process_files(myprog_file, ansys_file):
myprog_points = read_points(myprog_file)
ansys_points = read_points(ansys_file)
if not myprog_points or not ansys_points:
print("Error: One or both files are empty or contain no valid data.")
return
differences = find_closest_points(myprog_points, ansys_points)
if differences:
avg_difference = sum(differences) / len(differences)
print(f"\nAverage temperature difference: {avg_difference}")
else:
print("No valid matches found between the files.")
myprog_file = "myprog.txt"
ansys_file = "ansys.txt"
process_files(myprog_file, ansys_file)

View File

@@ -0,0 +1,4 @@
file = open('../implicit10.txt').readlines()
lines = [line for line in file if line.startswith('100')]
open('myprog.txt', 'w').write(''.join(lines))