-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneractio.py
More file actions
154 lines (125 loc) · 5.12 KB
/
generactio.py
File metadata and controls
154 lines (125 loc) · 5.12 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
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
import math
def generate_semicircle_holes_mesh():
# 配置参数
R = 1.0 # 半圆半径
r = 0.03 # 小圆孔半径
n_arc = 30 # 进一步减少半圆弧的点数
n_bottom = 6 # 进一步减少底边的点数
n_hole = 20 # 每个圆孔的点数
# 圆孔中心位置
hole_centers = [
(-0.35, 0.5), (0, 0.5), (0.35, 0.5), # 上排三个圆孔
(-0.35, 0.25), (0, 0.25), (0.35, 0.25) # 下排三个圆孔
]
nodes = [] # 存储节点坐标
faces = [] # 存储面信息
# 1. 生成外部半圆弧节点 (从右到左)
for i in range(n_arc):
theta = math.pi * i / (n_arc - 1)
x = R * math.cos(theta)
y = R * math.sin(theta)
nodes.append((x, y))
# 2. 生成底边内部节点 (从左到右)
bottom_points = n_bottom - 2 # 不包括端点
for i in range(1, bottom_points + 1):
x = -1.0 + 2.0 * i / (n_bottom - 1)
nodes.append((x, 0.0))
# 记录外边界节点数量
boundary_nodes = len(nodes)
# 3. 生成圆孔节点
for center_x, center_y in hole_centers:
start_idx = len(nodes) + 1
for i in range(n_hole):
theta = 2 * math.pi * i / n_hole
x = center_x + r * math.cos(theta)
y = center_y + r * math.sin(theta)
nodes.append((x, y))
# 修改面的生成方式
faces = []
# 1. 外部半圆弧面
for i in range(n_arc - 1):
# 修改格式:(节点1, 节点2, 左单元, 右单元)
faces.append((i+1, i+2, -1, 0)) # 外部边界左单元为-1,右单元为0
# 2. 底边面
bottom_start = n_arc
faces.append((n_arc, n_arc + 1, -1, 0))
for i in range(bottom_points - 1):
faces.append((bottom_start + i + 1, bottom_start + i + 2, -1, 0))
faces.append((bottom_start + bottom_points, 1, -1, 0))
# 3. 圆孔面
hole_start = boundary_nodes + 1
for h in range(6):
start = hole_start + h * n_hole
for i in range(n_hole):
current = start + i
next_point = start + (i + 1) % n_hole
faces.append((current, next_point, 0, -1)) # 内部圆孔左单元为0,右单元为-1
# 记录节点总数并验证
total_nodes = len(nodes)
print(f"Total nodes generated: {total_nodes}")
# 验证节点编号的连续性
node_indices = set(range(1, total_nodes + 1))
face_nodes = set()
for n1, n2, _, _ in faces:
face_nodes.add(n1)
face_nodes.add(n2)
if node_indices != face_nodes:
print("Warning: Node indices are not continuous!")
# 写入文件
with open('datainput/semicircle_holes_new.cas', 'w') as f:
# 写入文件头
f.write('(1 "Generated Semicircle with Holes")\n')
f.write('(0 "Generated Format")\n\n')
# 写入维度信息
f.write('(0 "Dimension : 2")\n')
f.write('(2 2)\n\n')
total_nodes = len(nodes)
# 写入节点数量信息
f.write(f'(0 "Number of Nodes : {total_nodes}")\n')
f.write('(10 (0 1 234 0 2))\n\n')
total_faces = len(faces)
# 写入面信息
f.write(f'(0 "Total Number of Faces : {total_faces}")\n')
f.write(f'(0 " Boundary Faces : {total_faces}")\n')
f.write('(0 " Interior Faces : 0")\n')
f.write('(13 (0 1 444 0))\n\n')
# 写入单元信息
f.write('(0 "Total Number of Cells : 0")\n')
f.write('(0 " Tri cells : 0")\n')
f.write('(0 " Quad cells : 0")\n')
f.write('(12 (0 1 211 0))\n\n')
# 写入节点坐标
f.write(f'(0 "Zone 1 Number of Nodes : {total_nodes}")\n')
for x, y in nodes:
f.write(f"{x:.6f} {y:.6f}\n")
# 写入面连接信息
f.write(f'\n(0 "Zone 2 Number of Face : {total_faces}")\n')
for n1, n2, left, right in faces:
# 确保写入的格式与 two_front.py 期望的格式一致
f.write(f"{n1} {n2} {left} {right}\n")
# 在生成网格后调用验证
if not validate_mesh_data(nodes, faces):
raise ValueError("Generated mesh is invalid")
def validate_mesh_data(nodes, faces):
"""验证网格数据的正确性"""
try:
# 验证节点数量
total_nodes = len(nodes)
if total_nodes < 1:
raise ValueError("No nodes generated")
# 验证面的连接关系
for i, (n1, n2, left, right) in enumerate(faces):
if n1 < 1 or n1 > total_nodes or n2 < 1 or n2 > total_nodes:
raise ValueError(f"Invalid node index in face {i+1}: ({n1}, {n2})")
if n1 == n2:
raise ValueError(f"Self-loop detected in face {i+1}")
# 验证边界条件
for n1, n2, left, right in faces:
if not ((left == -1 and right == 0) or (left == 0 and right == -1)):
raise ValueError(f"Invalid boundary conditions: left={left}, right={right}")
return True
except Exception as e:
print(f"Mesh validation failed: {e}")
return False
# 运行生成程序
generate_semicircle_holes_mesh()