-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcats-cradle-flash.txt
152 lines (136 loc) · 4.76 KB
/
cats-cradle-flash.txt
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
=== File: app/models/string_theory.py ===
from dataclasses import dataclass, asdict
import numpy as np
from typing import List, Dict
import logging
logger = logging.getLogger(__name__)
@dataclass
class StringTheorySystem:
dimensions: int = 10
tension: float = 1.0
coupling: float = 0.1
alpha_prime: float = 1.0
compactification: Dict = None
def __post_init__(self):
if self.compactification is None:
self.compactification = {
'radius': [1.0] * 6, # Compactification radii for 6 extra dimensions
'topology': 'Calabi-Yau'
}
def calculate_mass_spectrum(self) -> List[float]:
try:
n = np.arange(10)
return np.sqrt(n / self.alpha_prime).tolist()
except Exception as e:
logger.error(f"Error calculating mass spectrum: {str(e)}")
return []
def update_parameters(self, params: Dict) -> None:
try:
for key, value in params.items():
if hasattr(self, key) and key != 'compactification':
setattr(self, key, float(value))
elif key == 'compactification_radius':
self.compactification['radius'] = [float(value)] * 6
logger.info(f"Parameters updated: {params}")
except Exception as e:
logger.error(f"Error updating parameters: {str(e)}")
raise ValueError(f"Invalid parameters: {str(e)}")
def to_dict(self) -> Dict:
return asdict(self)
=== File: app/api/v1/endpoints/string_theory.py ===
from fastapi import APIRouter, HTTPException, Depends
from typing import Dict
from app.schemas.string_theory import StringParameters, SystemState, SystemResponse
from app.models.string_theory import StringTheorySystem
from datetime import datetime
import logging
router = APIRouter()
system = StringTheorySystem()
logger = logging.getLogger(__name__)
@router.get("/", response_model=SystemResponse)
async def get_system_state():
"""
Get current state of the string theory system.
"""
try:
state = system.to_dict()
state.update({
'mass_spectrum': system.calculate_mass_spectrum(),
'timestamp': datetime.utcnow().isoformat()
})
return {
"status": "success",
"data": state
}
except Exception as e:
logger.error(f"Error getting system state: {str(e)}")
raise HTTPException(
status_code=500,
detail="Internal server error"
)
@router.post("/update", response_model=SystemResponse)
async def update_parameters(params: StringParameters):
"""
Update string theory system parameters.
"""
try:
system.update_parameters(params.dict())
state = system.to_dict()
state.update({
'mass_spectrum': system.calculate_mass_spectrum(),
'timestamp': datetime.utcnow().isoformat()
})
return {
"status": "success",
"data": state
}
except ValueError as e:
raise HTTPException(
status_code=400,
detail=str(e)
)
except Exception as e:
logger.error(f"Error updating parameters: {str(e)}")
raise HTTPException(
status_code=500,
detail="Internal server error"
)
=== File: tests/test_api.py ===
import pytest
from httpx import AsyncClient
from app.main import app
@pytest.mark.asyncio
async def test_get_system_state():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/api/v1/string-theory/")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert data["status"] == "success"
assert "data" in data
@pytest.mark.asyncio
async def test_update_parameters():
async with AsyncClient(app=app, base_url="http://test") as client:
params = {
"dimensions": 11,
"tension": 1.5,
"coupling": 0.2,
"alpha_prime": 1.2,
"compactification_radius": 1.1
}
response = await client.post("/api/v1/string-theory/update", json=params)
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert data["data"]["dimensions"] == 11
=== File: app/api/deps.py ===
from typing import Generator
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import jwt, JWTError
from app.core.config import get_settings
from datetime import datetime
settings = get_settings()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/login")
async def get_current_time() -> datetime:
return datetime.utcnow()