-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
458 lines (393 loc) · 14.9 KB
/
__init__.py
File metadata and controls
458 lines (393 loc) · 14.9 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# quantum_gravity/__init__.py
"""
Efficient Quantum Gravity Framework
=================================
A framework for numerical simulations of quantum gravity with efficient
implementation of core algorithms and parallel computing capabilities.
"""
import numpy as np
import logging
from pathlib import Path
from typing import Dict
import json
from physics.observables import (
AreaObservable,
ADMMassObservable,
BlackHoleTemperatureObservable,
HawkingFluxObservable,
RadiationEntropyObservable,
ScaleFactorObservable,
EnergyDensityObservable,
QuantumCorrectionsObservable,
PerturbationSpectrumObservable,
StellarTemperatureObservable,
PressureObservable,
RingdownObservable
)
# Make key components available at package level
from core.grid import AdaptiveGrid
from core.state import QuantumState
from core.operators import QuantumOperator
from core.evolution import TimeEvolution
#from core.evolution import TimeEvolution
from numerics.errors import ErrorTracker
from physics.conservation import ConservationLawTracker
from physics.quantum_geometry import QuantumGeometry
from utils.io import QuantumGravityIO
# Package metadata
__version__ = '0.1.0'
__author__ = 'Christian Nygaard'
__license__ = 'MIT'
# Physical constants (in natural units)
CONSTANTS = {
'hbar': 1.0, # ℏ = 1
'c': 1.0, # c = 1
'G': 1.0, # G = 1
'l_p': 1.0, # Planck length
't_p': 1.0, # Planck time
'm_p': 1.0, # Planck mass
'lambda': 1e-52 # Cosmological constant
}
# Default configuration
DEFAULT_CONFIG = {
'grid': {
'points_min': 1000,
'points_max': 10000,
'adaptive_threshold': 1e-6,
'refinement_factor': 2.0
},
'evolution': {
'dt_min': 1e-6,
'dt_max': 1e-2,
'rtol': 1e-6,
'atol': 1e-8,
'method': 'adaptive'
},
'numerics': {
'eps_cut': 1e-10,
'max_iterations': 1000,
'convergence_threshold': 1e-8
},
'parallel': {
'load_balance': 'dynamic',
'comm_pattern': 'neighbor',
'chunk_size': 1000
},
'io': {
'output_dir': './output',
'checkpoint_interval': 100
}
}
class QuantumGravityConfig:
"""Configuration manager for quantum gravity framework."""
def __init__(self, config_path: str = None):
self.config = {
'grid': {
'points_min': 1000,
'points_max': 10000,
'adaptive_threshold': 1e-6,
'refinement_factor': 2.0
},
'numerics': {
'eps_cut': 1e-10,
'max_iterations': 1000,
'convergence_threshold': 1e-8
},
'io': {
'output_dir': './output',
'checkpoint_interval': 100
}
}
self.io = QuantumGravityIO(self.config['io']['output_dir'])
if config_path:
self.load_config(config_path)
# Setup logging
#self._setup_logging()
def load_config(self, config_path: str) -> None:
"""Load configuration from JSON file."""
try:
with open(config_path, 'r') as f:
user_config = json.load(f)
self._update_recursive(self.config, user_config)
logging.info(f"Loaded configuration from {config_path}")
except Exception as e:
logging.error(f"Error loading configuration: {str(e)}")
raise
def save_config(self, config_path: str) -> None:
"""Save current configuration to JSON file."""
try:
with open(config_path, 'w') as f:
json.dump(self.config, f, indent=2)
logging.info(f"Saved configuration to {config_path}")
except Exception as e:
logging.error(f"Error saving configuration: {str(e)}")
raise
def _update_recursive(self, d: Dict, u: Dict) -> None:
"""Recursively update nested dictionary."""
for k, v in u.items():
if isinstance(v, dict):
d[k] = self._update_recursive(d.get(k, {}), v)
else:
d[k] = v
return d
def _setup_logging(self) -> None:
"""Configure logging system."""
log_dir = Path(self.config['io']['output_dir'])
log_dir.mkdir(exist_ok=True)
logging.basicConfig(
filename=str(log_dir / 'quantum_gravity.log'),
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Also log to console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
def configure_logging(mass: float = None, simulation_type: str = 'black_hole', log_file: str = None):
"""Configure unified logging for quantum gravity framework.
Args:
mass: Mass parameter for black hole simulations
simulation_type: Type of simulation ('black_hole' or 'cosmology')
log_file: Optional custom log file name
"""
# Get the root logger
root = logging.getLogger()
# Remember if we already had a StreamHandler
had_stream_handler = False
stream_handler = None
# Clear existing handlers, but remember if we had a StreamHandler
if root.handlers:
for handler in root.handlers:
if isinstance(handler, logging.StreamHandler) and not isinstance(handler, logging.FileHandler):
had_stream_handler = True
stream_handler = handler
root.removeHandler(handler)
# Create output directory based on simulation type
output_dir = Path(f"results/{simulation_type}")
output_dir.mkdir(parents=True, exist_ok=True)
# Configure handlers
handlers = []
# Restore or create StreamHandler for console output
if had_stream_handler and stream_handler:
handlers.append(stream_handler)
else:
handlers.append(logging.StreamHandler())
# Add appropriate file handler based on simulation type
if simulation_type == 'black_hole':
log_file = f"simulation_M{mass:.0f}.txt"
elif simulation_type == 'stellar':
log_file = f"{log_file}.txt"
else:
log_file = "simulation.txt"
handlers.append(logging.FileHandler(str(output_dir / log_file), mode='w'))
# Apply configuration
logging.basicConfig(
level=logging.INFO,
format='%(message)s',
handlers=handlers
)
class QuantumGravity:
"""Main interface for quantum gravity framework."""
def __init__(self, config_path: str = None):
"""Initialize quantum gravity framework."""
#configure_logging() # Set up logging first
# Initialize quantum geometry first
self.quantum_geometry = QuantumGeometry()
# Get fundamental quantum geometric scales
self.l_universal = self.quantum_geometry.universal_quantum_length()
self.cosmic_factor = self.quantum_geometry.cosmic_scale_factor()
self.phase = self.quantum_geometry.quantum_geometric_phase()
# Load default configuration
self.config = QuantumGravityConfig(config_path)
# Override output directory to use results/
self.config.config['io']['output_dir'] = ''
# Initialize IO handler with correct path
from utils.io import QuantumGravityIO
self.io = QuantumGravityIO(self.config.config['io']['output_dir'])
# Initialize grid first
self.grid = AdaptiveGrid(
eps_threshold=self.config.config['grid']['adaptive_threshold'],
l_p=1.0
)
# Set initial points before creating state
initial_points = np.array([[0.0, 0.0, 0.0]], dtype=np.float32)
self.grid.set_points(initial_points)
# Initialize state with populated grid
self.state = QuantumState(
grid=self.grid,
initial_mass=1.0,
eps_cut=self.config.config['numerics']['eps_cut']
)
# Initialize quantum operators
self.operators = {
'hamiltonian': self._create_hamiltonian(),
'momentum': self._create_momentum_operator(),
'angular_momentum': self._create_angular_momentum(),
'constraints': self._create_constraints()
}
# Add physics namespace
self.physics = self._init_physics()
logging.info("Quantum gravity framework initialized")
def _create_hamiltonian(self):
"""Create Hamiltonian operator for energy evolution."""
return QuantumOperator(
self.grid,
operator_type='hamiltonian',
coupling_constant=CONSTANTS['G']
)
def _create_momentum_operator(self):
"""Create momentum operator for translations."""
return QuantumOperator(
self.grid,
operator_type='momentum',
dimensions=3
)
def _create_angular_momentum(self):
"""Create angular momentum operator for rotations."""
return QuantumOperator(
self.grid,
operator_type='angular_momentum',
dimensions=3
)
def _create_constraints(self):
"""Create constraint operators for gauge invariance."""
return [
QuantumOperator(
self.grid,
operator_type='constraint',
constraint_index=i
)
for i in range(4) # 4 constraints for diffeomorphism invariance
]
def _init_physics(self):
"""Initialize physics components."""
class Physics:
pass
physics = Physics()
physics.AreaObservable = AreaObservable
physics.ADMMassObservable = ADMMassObservable
physics.BlackHoleTemperatureObservable = BlackHoleTemperatureObservable
physics.HawkingFluxObservable = HawkingFluxObservable
physics.RadiationEntropyObservable = RadiationEntropyObservable
physics.ScaleFactorObservable = ScaleFactorObservable
physics.EnergyDensityObservable = EnergyDensityObservable
physics.QuantumCorrectionsObservable = QuantumCorrectionsObservable
physics.PerturbationSpectrumObservable = PerturbationSpectrumObservable
physics.StellarTemperatureObservable = StellarTemperatureObservable
physics.PressureObservable = PressureObservable
physics.RingdownObservable = RingdownObservable
return physics
def _load_config(self, config_path: str = None) -> Dict:
"""Load configuration with defaults."""
default_config = {
'grid': {
'points_min': 1000,
'points_max': 10000,
'adaptive_threshold': 1e-6,
'refinement_factor': 2.0
},
'numerics': {
'eps_cut': 1e-10,
'max_iterations': 1000,
'convergence_threshold': 1e-8
}
}
if config_path:
try:
with open(config_path, 'r') as f:
user_config = json.load(f)
# Update defaults with user config
self._update_recursive(default_config, user_config)
except Exception as e:
logging.warning(
f"Failed to load config from {config_path}: {e}"
)
logging.info(
"Using default configuration"
)
return default_config
def _update_recursive(self, d: Dict, u: Dict) -> Dict:
"""Recursively update nested dictionary."""
for k, v in u.items():
if isinstance(v, dict):
d[k] = self._update_recursive(d.get(k, {}), v)
else:
d[k] = v
return d
def _setup_grid(self):
logging.debug(f"R_star = {self.R_star:.3e}")
# Generate and validate points
points = self._generate_grid_points()
logging.debug(f"Points shape: {points.shape}")
logging.debug(f"Points stats: min={np.min(points):.3e}, max={np.max(points):.3e}")
# Check for invalid values
if not np.all(np.isfinite(points)):
invalid_mask = ~np.isfinite(points)
invalid_points = points[invalid_mask]
invalid_indices = np.where(invalid_mask)
logging.error(f"Invalid points at indices {invalid_indices}")
logging.error(f"Invalid values: {invalid_points}")
raise ValueError("NaN/Inf found in grid points")
# Clamp to safe range if needed
max_allowed = 1e308 # Max float64 ~1e308
if np.any(np.abs(points) > max_allowed):
points = np.clip(points, -max_allowed, max_allowed)
logging.warning("Points clipped to safe range")
self.qg.grid.set_points(points)
def _generate_grid_points(self):
num_points = 1000
# Use smaller scale factor to avoid overflow
scale = min(self.R_star, 1e100)
points = np.random.rand(num_points, 3) * scale
return points
def run_simulation(self, t_final: float, callback=None) -> None:
"""Run simulation to specified time."""
# Initialize evolution if not already done
if not hasattr(self, 'evolution'):
# Create error tracker
error_tracker = ErrorTracker(
grid=self.grid,
base_tolerances=self.config.config['numerics']
)
# Create conservation tracker
conservation_tracker = ConservationLawTracker(self.grid)
self.evolution = TimeEvolution(
grid=self.grid,
config={
'dt': 0.01,
'error_tolerance': 1e-6
},
error_tracker=error_tracker,
conservation_tracker=conservation_tracker,
state=self.qg.state # Add state parameter
)
# Add progress tracking
current_t = 0.0
step = 0
progress_interval = t_final / 100
next_report = progress_interval
while current_t < t_final:
self.state = self.evolution.step(self.state)
current_t += self.evolution.dt
# Progress reporting
if current_t >= next_report:
progress = (current_t / t_final) * 100
logging.info(
f"Simulation progress: {progress:.1f}% "
f"(t={current_t:.2f}/{t_final})"
)
next_report += progress_interval
if callback:
callback(self.state, current_t, step)
step += 1
logging.info(f"Simulation completed to time {t_final}")
__all__ = [
'QuantumGravity',
'QuantumGravityConfig',
'CONSTANTS',
'AdaptiveGrid',
'QuantumState',
'QuantumOperator',
'TimeEvolution',
'QuantumGravityIO'
]