Spaces:
Sleeping
Sleeping
File size: 11,365 Bytes
e0ef700 |
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
#!/usr/bin/env python3
"""
Extract combinatorics and optimal angles from maximal volume configuration.
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
import numpy as np
import json
from scipy.spatial import Delaunay
from ideal_poly_volume_toolkit.rivin_delaunay import check_delaunay_realizability, build_edge_adjacency
def extract_combinatorics_and_angles(json_file):
"""Extract triangulation and optimal angles from optimization result."""
# Load configuration
with open(json_file, 'r') as f:
data = json.load(f)
# Get vertex positions
real = np.array(data['best']['vertices_real'])
imag = np.array(data['best']['vertices_imag'])
points = np.column_stack([real, imag])
n_vertices = len(points)
volume = data['best']['volume']
print(f"βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
print(f"MAXIMAL VOLUME IDEAL POLYHEDRON - COMPLETE ANALYSIS")
print(f"βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
print(f"\nVolume: {volume:.12f}")
print(f"Vertices: {n_vertices}")
# Compute Delaunay triangulation
tri = Delaunay(points)
triangulation = [tuple(sorted(simplex)) for simplex in tri.simplices]
triangulation = sorted(set(triangulation)) # Remove duplicates, sort
print(f"Triangles: {len(triangulation)}")
# Display triangulation
print(f"\n{'β'*63}")
print(f"TRIANGULATION (Combinatorial Structure)")
print(f"{'β'*63}")
print("\nTriangles (vertex indices):")
for i, tri in enumerate(triangulation, 1):
print(f" {i:2d}. {tri}")
# Get optimal angles from Rivin LP
print(f"\n{'β'*63}")
print(f"OPTIMAL ANGLES FROM RIVIN LP")
print(f"{'β'*63}")
result = check_delaunay_realizability(triangulation, verbose=False, strict=False)
if not result['realizable']:
print("ERROR: Configuration not realizable!")
return
# Extract angles (convert from scaled units to radians)
angles_scaled = result['angles']
angles_radians = angles_scaled * np.pi
n_triangles = len(triangulation)
angles_array = angles_radians.reshape((n_triangles, 3))
print("\nFace angles (interior angles of each triangle):")
print(f"{'Tri#':>5} {'Vertices':>20} {'Vtx':>5} {'Angle (rad)':>14} {'Angle (deg)':>12} {'Angle/Ο':>10}")
print(f"{'-'*70}")
for i, tri in enumerate(triangulation):
for j, vertex in enumerate(tri):
angle_rad = angles_array[i, j]
angle_deg = np.degrees(angle_rad)
angle_normalized = angle_rad / np.pi
tri_str = f"({tri[0]},{tri[1]},{tri[2]})"
print(f" {i+1:3d}. {tri_str:>18s} v{vertex:2d} {angle_rad:13.10f} {angle_deg:11.6f}Β° {angle_normalized:9.6f}Ο")
# Compute ALL dihedral angles
print(f"\n{'='*78}")
print(f"ALL DIHEDRAL ANGLES (Complete Ideal Polyhedron)")
print(f"{'='*78}")
from scipy.spatial import ConvexHull
from fractions import Fraction
# Find boundary (convex hull)
hull = ConvexHull(points)
boundary_edges = set()
boundary_vertices = set()
for simplex in hull.simplices:
edge = tuple(sorted([simplex[0], simplex[1]]))
boundary_edges.add(edge)
boundary_vertices.add(simplex[0])
boundary_vertices.add(simplex[1])
edge_adjacency = build_edge_adjacency(triangulation)
interior_edges = [e for e, ops in edge_adjacency.items() if len(ops) == 2]
print(f"\nTopology:")
print(f" Total vertices: {n_vertices}")
print(f" Finite triangles: {len(triangulation)}")
print(f" Boundary vertices: {len(boundary_vertices)}")
print(f" Interior edges: {len(interior_edges)}")
print(f" Boundary edges: {len(boundary_edges)}")
print(f" Total edges: {len(interior_edges)} + {len(boundary_edges)} + {len(boundary_vertices)} = {len(interior_edges) + len(boundary_edges) + len(boundary_vertices)}")
print(f" Expected (3V-6): {3*n_vertices - 6}")
# TYPE 1: Interior edge dihedrals
print(f"\n{'β'*78}")
print(f"TYPE 1: Interior Edge Dihedrals ({len(interior_edges)} edges)")
print(f"(Edges shared by two finite triangles)")
print(f"{'β'*78}")
all_dihedrals = []
for edge, opposite_corners in sorted(edge_adjacency.items()):
if len(opposite_corners) == 2:
angle1 = angles_array[opposite_corners[0][0], opposite_corners[0][1]]
angle2 = angles_array[opposite_corners[1][0], opposite_corners[1][1]]
dihedral = angle1 + angle2
normalized = dihedral / np.pi
frac = Fraction(normalized).limit_denominator(20)
rational = f"{frac.numerator}Ο/{frac.denominator}" if frac.denominator > 1 else f"{frac.numerator}Ο"
all_dihedrals.append({
'type': 'interior',
'edge': edge,
'dihedral_rad': dihedral,
'dihedral_deg': np.degrees(dihedral),
'normalized': normalized,
'rational': rational,
'p': frac.numerator,
'q': frac.denominator,
})
# Print first 10
for d in all_dihedrals[:10]:
print(f" {str(d['edge']):>12} {d['dihedral_deg']:7.3f}Β° = {d['normalized']:8.6f}Ο β {d['rational']:>8}")
if len(all_dihedrals) > 10:
print(f" ... ({len(all_dihedrals) - 10} more)")
# TYPE 2: Boundary edge dihedrals
print(f"\n{'β'*78}")
print(f"TYPE 2: Boundary Edge Dihedrals ({len(boundary_edges)} edges)")
print(f"(Angle opposite each boundary edge in the triangle containing it)")
print(f"{'β'*78}")
boundary_dihedrals = []
for edge in sorted(boundary_edges):
# Find the triangle containing this edge
v1, v2 = edge
for i, tri in enumerate(triangulation):
if v1 in tri and v2 in tri:
# Find the third vertex (opposite to the edge)
v3 = [v for v in tri if v != v1 and v != v2][0]
v3_idx = tri.index(v3)
dihedral = angles_array[i, v3_idx]
normalized = dihedral / np.pi
frac = Fraction(normalized).limit_denominator(20)
rational = f"{frac.numerator}Ο/{frac.denominator}" if frac.denominator > 1 else f"{frac.numerator}Ο"
boundary_dihedrals.append({
'type': 'boundary',
'edge': edge,
'dihedral_rad': dihedral,
'dihedral_deg': np.degrees(dihedral),
'normalized': normalized,
'rational': rational,
'p': frac.numerator,
'q': frac.denominator,
})
print(f" {str(edge):>12} {np.degrees(dihedral):7.3f}Β° = {normalized:8.6f}Ο β {rational:>8}")
break
all_dihedrals.extend(boundary_dihedrals)
# TYPE 3: Vertex-to-β dihedrals
print(f"\n{'β'*78}")
print(f"TYPE 3: Vertex-to-β Dihedrals ({len(boundary_vertices)} edges)")
print(f"(Sum of all angles at each boundary vertex)")
print(f"{'β'*78}")
vertex_dihedrals = []
for v in sorted(boundary_vertices):
# Sum all angles at this vertex
total_angle = 0
for i, tri in enumerate(triangulation):
if v in tri:
v_idx = tri.index(v)
total_angle += angles_array[i, v_idx]
normalized = total_angle / np.pi
frac = Fraction(normalized).limit_denominator(20)
rational = f"{frac.numerator}Ο/{frac.denominator}" if frac.denominator > 1 else f"{frac.numerator}Ο"
vertex_dihedrals.append({
'type': 'to_infinity',
'edge': (v, 'inf'),
'dihedral_rad': total_angle,
'dihedral_deg': np.degrees(total_angle),
'normalized': normalized,
'rational': rational,
'p': frac.numerator,
'q': frac.denominator,
})
print(f" v{v}ββ {np.degrees(total_angle):11.3f}Β° = {normalized:8.6f}Ο β {rational:>8} {'< Ο β' if total_angle < np.pi else 'β₯ Ο β'}")
all_dihedrals.extend(vertex_dihedrals)
dihedrals = all_dihedrals # For later use
# Summary of rational patterns
print(f"\n{'β'*63}")
print(f"RATIONAL ANGLE SUMMARY")
print(f"{'β'*63}")
from collections import Counter
pattern_counts = Counter(d['rational'] for d in dihedrals)
print(f"\n{'Pattern':>10} {'Count':>8} {'Degrees':>12}")
print(f"{'-'*32}")
for pattern, count in pattern_counts.most_common():
# Get representative angle
angle_deg = next(d['dihedral_deg'] for d in dihedrals if d['rational'] == pattern)
print(f" {pattern:>8} {count:7d} {angle_deg:11.3f}Β°")
# Determine the dominant denominator
denominator_counts = Counter(d['q'] for d in dihedrals)
dominant_q = denominator_counts.most_common(1)[0][0]
print(f"\n{'β'*63}")
if len(denominator_counts) == 1 and dominant_q > 1:
print(f"VERIFICATION: All angles are exact multiples of Ο/{dominant_q}!")
else:
print(f"VERIFICATION: All angles are exact rational multiples of Ο!")
print(f"{'β'*63}")
# Export to JSON
output_data = {
'metadata': {
'source_file': str(json_file),
'volume': float(volume),
'n_vertices': int(n_vertices),
'n_triangles': len(triangulation),
},
'combinatorics': {
'triangles': [[int(v) for v in tri] for tri in triangulation],
},
'optimal_angles': {
'face_angles_radians': angles_array.tolist(),
'face_angles_degrees': np.degrees(angles_array).tolist(),
'dihedral_angles': [
{
'type': d['type'],
'edge': [int(d['edge'][0]), str(d['edge'][1])], # Handle 'inf' for vertex-to-β
'angle_radians': float(d['dihedral_rad']),
'angle_degrees': float(d['dihedral_deg']),
'rational_form': d['rational'],
'p': int(d['p']),
'q': int(d['q']),
}
for d in dihedrals
],
},
'vertex_positions': {
'real': real.tolist(),
'imag': imag.tolist(),
},
}
# Generate output filename based on input file
input_name = Path(json_file).stem
output_file = Path(f'results/data/{input_name}_combinatorics.json')
output_file.parent.mkdir(parents=True, exist_ok=True)
with open(output_file, 'w') as f:
json.dump(output_data, f, indent=2)
print(f"\nβ Exported to: {output_file}")
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Extract combinatorics and optimal angles from maximal volume config')
parser.add_argument('json_file', help='Path to optimization result JSON file')
args = parser.parse_args()
extract_combinatorics_and_angles(args.json_file)
|