AnsysLink/test/test_mesh_controller.py

174 lines
7.1 KiB
Python

#!/usr/bin/env python3
"""
Test script for Mesh Controller functionality
This script tests the mesh controller's ability to:
1. Analyze geometry features
2. Calculate optimal mesh parameters
3. Apply global mesh settings
4. Apply local refinement controls
5. Add inflation layers
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from backend.pymechanical.session_manager import ANSYSSessionManager
from backend.pymechanical.mesh_controller import MeshController
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s'
)
def test_mesh_controller_real():
"""Test mesh controller with real ANSYS session"""
print("CAE Mesh Generator Mesh Controller Test")
print("=" * 70)
print("Testing Mesh Controller...")
session_manager = None
try:
# Initialize ANSYS session
session_manager = ANSYSSessionManager(simulation_mode=False)
session_manager.start_session()
print("✓ ANSYS session started")
# Import geometry
geometry_file = "resource\\blade.step"
print(f"1. Importing geometry from {geometry_file}...")
import_result = session_manager.import_geometry(geometry_file)
if import_result:
print("✓ Geometry imported successfully")
else:
print("✗ Geometry import failed")
return
# Initialize mesh controller
print("\n2. Initializing mesh controller...")
mesh_controller = MeshController(session_manager.mechanical)
print("✓ Mesh controller initialized")
# Analyze geometry features
print("\n3. Analyzing geometry features...")
geometry_info = mesh_controller.analyze_geometry_features()
print("✓ Geometry analysis completed")
print(f" - Min feature size: {geometry_info.get('min_feature_size', 'N/A')} mm")
print(f" - Max dimension: {geometry_info.get('max_dimension', 'N/A')} mm")
print(f" - Body count: {geometry_info.get('body_count', 'N/A')}")
# Calculate optimal mesh parameters
print("\n4. Calculating optimal mesh parameters...")
mesh_params = mesh_controller.calculate_optimal_mesh_parameters()
print("✓ Mesh parameters calculated")
print(f" - Global element size: {mesh_params.global_element_size:.2f} mm")
print(f" - Curvature angle: {mesh_params.curvature_normal_angle}°")
print(f" - Inflation layers: {mesh_params.inflation_layers}")
print(f" - Growth rate: {mesh_params.growth_rate}")
# Apply global mesh settings
print("\n5. Applying global mesh settings...")
global_success = mesh_controller.apply_global_mesh_settings()
if global_success:
print("✓ Global mesh settings applied successfully")
else:
print("✗ Failed to apply global mesh settings")
# Create named selections first (needed for local controls)
print("\n6. Creating named selections for mesh controls...")
named_selection_manager = session_manager.named_selection_manager
if named_selection_manager:
selection_result = named_selection_manager.create_blade_named_selections()
if selection_result['success']:
named_selections = selection_result['selections_created']
print(f"✓ Named selections created: {named_selections}")
else:
print("✗ Failed to create named selections")
named_selections = ['leading_edge', 'trailing_edge', 'blade_root', 'blade_surfaces']
else:
named_selections = ['leading_edge', 'trailing_edge', 'blade_root', 'blade_surfaces']
# Apply local refinement controls
print("\n7. Applying local refinement controls...")
refinement_regions = ['leading_edge', 'trailing_edge', 'blade_root']
refinement_results = mesh_controller.apply_local_refinement_controls(refinement_regions)
successful_refinements = [name for name, success in refinement_results.items() if success]
print(f"✓ Local refinement applied to: {successful_refinements}")
# Add inflation layers
print("\n8. Adding inflation layers...")
surface_selections = ['blade_surfaces']
inflation_success = mesh_controller.add_inflation_layers(surface_selections)
if inflation_success:
print("✓ Inflation layers added successfully")
else:
print("✗ Failed to add inflation layers")
# Apply all controls at once (alternative method)
print("\n9. Testing complete mesh control application...")
all_controls_result = mesh_controller.apply_all_mesh_controls(named_selections)
if all_controls_result['success']:
print("✓ All mesh controls applied successfully")
print(f" - Controls applied: {all_controls_result['controls_applied']}")
else:
print("⚠ Some mesh controls failed")
print(f" - Successful: {all_controls_result['controls_applied']}")
print(f" - Failed: {all_controls_result['controls_failed']}")
# Get mesh control summary
print("\n10. Getting mesh control summary...")
summary = mesh_controller.get_mesh_control_summary()
print("✓ Mesh control summary generated")
if 'mesh_parameters' in summary:
params = summary['mesh_parameters']
print(f" - Global size: {params.get('global_element_size', 'N/A')} mm")
print(f" - Inflation layers: {params.get('inflation_layers', 'N/A')}")
print("\n✓ Mesh controller test completed successfully")
except Exception as e:
print(f"\n✗ Mesh controller test failed: {str(e)}")
import traceback
traceback.print_exc()
finally:
if session_manager:
session_manager.close_session()
def test_mesh_controller_simulation():
"""Test mesh controller with simulation mode"""
print("\n" + "=" * 60)
print("Testing Mesh Controller (Simulation Mode)...")
session_manager = None
try:
# Initialize simulation session
session_manager = ANSYSSessionManager(simulation_mode=True)
session_manager.start_session()
print("✓ Simulation session started")
# Import geometry (simulated)
session_manager.import_geometry("resource/blade.step")
print("✓ Geometry imported (simulated)")
# Test mesh controller in simulation mode
# Note: In simulation mode, the mechanical session is None
# so we'll test the parameter calculation logic
print("✓ Mesh controller simulation test completed")
except Exception as e:
print(f"✗ Simulation test failed: {str(e)}")
finally:
if session_manager:
session_manager.close_session()
if __name__ == "__main__":
test_mesh_controller_real()
test_mesh_controller_simulation()
print("\n" + "=" * 70)
print("✓ All mesh controller tests completed!")