2d-predictor / test_api.py
alaka's picture
Fix
c42edd1
Raw
History Blame Contribute Delete
4.01 kB
#!/usr/bin/env python
"""Test the NeuralFoil API via HTTP requests"""
import requests
import json
# API endpoint
API_URL = "http://127.0.0.1:7861/api/predict"
print("Testing NeuralFoil API")
print("=" * 60)
print("Make sure the app is running: python app.py")
print("=" * 60)
# Test 1: Simple airfoil
print("\nTest 1: Simple airfoil with 9 points")
print("-" * 60)
coordinates = [
[1.0, 0.0],
[0.75, 0.03],
[0.5, 0.05],
[0.25, 0.04],
[0.0, 0.0],
[0.25, -0.02],
[0.5, -0.03],
[0.75, -0.02],
[1.0, 0.0]
]
payload = {
"data": [
coordinates, # airfoil coordinates
5.0, # angle of attack (degrees)
1000000, # Reynolds number
"small" # model size
]
}
try:
response = requests.post(API_URL, json=payload)
response.raise_for_status()
result = response.json()
if "data" in result and len(result["data"]) > 0:
data = result["data"][0]
print("βœ“ API call successful!")
print(f"\nAerodynamic coefficients:")
print(f" CL = {data.get('CL', 'N/A')}")
print(f" CD = {data.get('CD', 'N/A')}")
print(f" CM = {data.get('CM', 'N/A')}")
print(f" Confidence = {data.get('analysis_confidence', 'N/A')}")
if 'pressure_coefficients' in data:
pc = data['pressure_coefficients']
print(f"\nPressure coefficients:")
print(f" Upper surface: {len(pc.get('upper_surface_cp', []))} interpolated points")
print(f" Lower surface: {len(pc.get('lower_surface_cp', []))} interpolated points")
print(f" Upper (32 BL): {len(pc.get('upper_surface_cp_32', []))} points")
print(f" Lower (32 BL): {len(pc.get('lower_surface_cp_32', []))} points")
else:
print("βœ— Unexpected response format")
print(json.dumps(result, indent=2)[:200])
except requests.exceptions.ConnectionError:
print("βœ— Error: Cannot connect to API")
print(f" Make sure the app is running on {API_URL}")
print(" Run: python app.py")
except Exception as e:
print(f"βœ— Error: {e}")
# Test 2: RAE 2822 from file
print("\n\nTest 2: RAE 2822 airfoil from file")
print("-" * 60)
try:
with open("examples/rae2822.dat", "r") as f:
lines = f.readlines()[1:] # Skip header
coordinates = []
for line in lines:
parts = line.strip().split()
if len(parts) >= 2:
coordinates.append([float(parts[0]), float(parts[1])])
print(f"Loaded {len(coordinates)} coordinate points")
payload = {
"data": [
coordinates,
2.0, # alpha (deg)
6e6, # reynolds
"large" # model_size
]
}
response = requests.post(API_URL, json=payload)
response.raise_for_status()
result = response.json()
if "data" in result and len(result["data"]) > 0:
data = result["data"][0]
print("βœ“ API call successful!")
print(f"\nAerodynamic coefficients:")
print(f" CL = {data.get('CL', 'N/A')}")
print(f" CD = {data.get('CD', 'N/A')}")
print(f" CM = {data.get('CM', 'N/A')}")
cl = data.get('CL', 0)
cd = data.get('CD', 1)
ld = cl / cd if cd and cd > 0 else 0
print(f" L/D = {ld:.1f}")
if 'pressure_coefficients' in data:
pc = data['pressure_coefficients']
print(f"\nPressure coefficients:")
print(f" Upper surface: {len(pc.get('upper_surface_cp', []))} interpolated points")
print(f" Lower surface: {len(pc.get('lower_surface_cp', []))} interpolated points")
except FileNotFoundError:
print("βœ— Error: examples/rae2822.dat not found")
except requests.exceptions.ConnectionError:
print("βœ— Error: Cannot connect to API")
except Exception as e:
print(f"βœ— Error: {e}")
print("\n" + "=" * 60)
print("Testing complete!")
print("=" * 60)