broadfield-dev commited on
Commit
c463819
·
verified ·
1 Parent(s): c1f3e16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -24
app.py CHANGED
@@ -15,12 +15,10 @@ def gravitational_time_dilation(mass, radius, t0):
15
  Returns: Dilated time (seconds)
16
  """
17
  try:
18
- # Schwarzschild radius factor
19
  rs = (2 * G * mass) / (C ** 2)
20
- # Time dilation factor sqrt(1 - rs/r)
21
  factor = math.sqrt(1 - rs / radius)
22
  if factor <= 0 or math.isnan(factor):
23
- return None # Invalid due to event horizon or negative sqrt
24
  return t0 / factor
25
  except Exception:
26
  return None
@@ -35,8 +33,7 @@ def relativistic_time_dilation(velocity, t0):
35
  try:
36
  beta = velocity / C
37
  if beta >= 1:
38
- return None # Cannot exceed speed of light
39
- # Lorentz factor
40
  factor = math.sqrt(1 - beta ** 2)
41
  return t0 / factor
42
  except Exception:
@@ -51,11 +48,37 @@ def home():
51
  <style>
52
  body { font-family: Arial, sans-serif; margin: 20px; }
53
  .container { max-width: 800px; margin: auto; }
54
- .input-group { margin: 10px 0; }
55
- label { display: inline-block; width: 200px; }
56
- input { padding: 5px; width: 200px; }
57
- button { padding: 10px 20px; margin: 10px 0; }
58
- #result { margin-top: 20px; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  </style>
60
  </head>
61
  <body>
@@ -64,23 +87,84 @@ def home():
64
  <p>Calculate gravitational and relativistic time dilation for two observers.</p>
65
  <div class='input-group'>
66
  <h3>Observer 1</h3>
67
- <label>Mass (kg):</label><input type='number' id='mass1' value='5.972e24'><br>
68
- <label>Radius (m):</label><input type='number' id='radius1' value='6.371e6'><br>
69
- <label>Velocity (m/s):</label><input type='number' id='velocity1' value='0'><br>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  </div>
71
  <div class='input-group'>
72
  <h3>Observer 2</h3>
73
- <label>Mass (kg):</label><input type='number' id='mass2' value='5.972e26'><br>
74
- <label>Radius (m):</label><input type='number' id='radius2' value='6.371e6'><br>
75
- <label>Velocity (m/s):</label><input type='number' id='velocity2' value='0'><br>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  </div>
77
  <div class='input-group'>
78
- <label>Proper Time (s):</label><input type='number' id='proper_time' value='86400'><br>
 
 
 
 
 
 
 
 
79
  </div>
80
  <button onclick='calculate()'>Calculate</button>
81
  <div id='result'></div>
82
  </div>
83
  <script>
 
 
 
 
 
84
  async function calculate() {
85
  const data = {
86
  mass1: parseFloat(document.getElementById('mass1').value),
@@ -116,32 +200,26 @@ def calculate():
116
  velocity2 = data.get('velocity2', 0)
117
  t0 = data.get('proper_time')
118
 
119
- # Input validation
120
  if not all([mass1, radius1, mass2, radius2, t0]) or any(x <= 0 for x in [mass1, radius1, mass2, radius2, t0]):
121
  return jsonify({"message": "Invalid input: All values must be positive numbers."}), 400
122
 
123
- # Calculate gravitational time dilation
124
  t1_grav = gravitational_time_dilation(mass1, radius1, t0)
125
  t2_grav = gravitational_time_dilation(mass2, radius2, t0)
126
 
127
  if t1_grav is None or t2_grav is None:
128
  return jsonify({"message": "Error: Invalid gravitational parameters (e.g., within event horizon)."}), 400
129
 
130
- # Calculate relativistic time dilation
131
  t1_rel = relativistic_time_dilation(velocity1, t0) if velocity1 > 0 else t0
132
  t2_rel = relativistic_time_dilation(velocity2, t0) if velocity2 > 0 else t0
133
 
134
  if (velocity1 > 0 and t1_rel is None) or (velocity2 > 0 and t2_rel is None):
135
  return jsonify({"message": "Error: Velocity exceeds speed of light."}), 400
136
 
137
- # Combine effects (approximate by multiplying factors)
138
  t1_total = t1_grav if velocity1 == 0 else t1_grav * (t1_rel / t0)
139
  t2_total = t2_grav if velocity2 == 0 else t2_grav * (t2_rel / t0)
140
 
141
- # Calculate relative time difference
142
  time_diff = abs(t1_total - t2_total)
143
 
144
- # Prepare response
145
  message = (
146
  f"Results for proper time {t0:.2f} seconds:\n"
147
  f"Observer 1:\n"
@@ -159,6 +237,5 @@ def calculate():
159
  return jsonify({"message": f"Error: {str(e)}"}), 500
160
 
161
  if __name__ == '__main__':
162
- # For Hugging Face Spaces, use the PORT environment variable
163
  port = int(os.environ.get("PORT", 7860))
164
  app.run(host='0.0.0.0', port=port)
 
15
  Returns: Dilated time (seconds)
16
  """
17
  try:
 
18
  rs = (2 * G * mass) / (C ** 2)
 
19
  factor = math.sqrt(1 - rs / radius)
20
  if factor <= 0 or math.isnan(factor):
21
+ return None
22
  return t0 / factor
23
  except Exception:
24
  return None
 
33
  try:
34
  beta = velocity / C
35
  if beta >= 1:
36
+ return None
 
37
  factor = math.sqrt(1 - beta ** 2)
38
  return t0 / factor
39
  except Exception:
 
48
  <style>
49
  body { font-family: Arial, sans-serif; margin: 20px; }
50
  .container { max-width: 800px; margin: auto; }
51
+ .input-group { margin: 20px 0; }
52
+ label {
53
+ display: inline-block;
54
+ width: 200px;
55
+ cursor: pointer;
56
+ color: #007bff;
57
+ text-decoration: underline;
58
+ }
59
+ label:hover { color: #0056b3; }
60
+ input { padding: 5px; width: 200px; margin-left: 10px; }
61
+ button {
62
+ padding: 10px 20px;
63
+ margin: 20px 0;
64
+ background-color: #007bff;
65
+ color: white;
66
+ border: none;
67
+ cursor: pointer;
68
+ }
69
+ button:hover { background-color: #0056b3; }
70
+ #result { margin-top: 20px; padding: 10px; border: 1px solid #ddd; }
71
+ .popdown {
72
+ display: none;
73
+ margin-top: 5px;
74
+ padding: 10px;
75
+ background-color: #f8f9fa;
76
+ border: 1px solid #ddd;
77
+ border-radius: 4px;
78
+ max-width: 400px;
79
+ font-size: 14px;
80
+ }
81
+ .popdown.show { display: block; }
82
  </style>
83
  </head>
84
  <body>
 
87
  <p>Calculate gravitational and relativistic time dilation for two observers.</p>
88
  <div class='input-group'>
89
  <h3>Observer 1</h3>
90
+ <div>
91
+ <label onclick="togglePopdown('mass1-info')">Mass (kg):</label>
92
+ <input type='number' id='mass1' value='5.972e24'>
93
+ <div id='mass1-info' class='popdown'>
94
+ The mass of the object (e.g., planet or star) affecting Observer 1, in kilograms.
95
+ This determines the strength of the gravitational field. For example, Earth's mass is approximately 5.972 × 10²⁴ kg.
96
+ Larger masses cause greater gravitational time dilation, slowing time for the observer.
97
+ </div>
98
+ </div>
99
+ <div>
100
+ <label onclick="togglePopdown('radius1-info')">Radius (m):</label>
101
+ <input type='number' id='radius1' value='6.371e6'>
102
+ <div id='radius1-info' class='popdown'>
103
+ The distance from the center of the massive object to Observer 1, in meters.
104
+ This is typically the radius of the planet or star. For example, Earth's radius is about 6.371 × 10⁶ m.
105
+ Smaller radii (closer to the mass) increase gravitational time dilation.
106
+ </div>
107
+ </div>
108
+ <div>
109
+ <label onclick="togglePopdown('velocity1-info')">Velocity (m/s):</label>
110
+ <input type='number' id='velocity1' value='0'>
111
+ <div id='velocity1-info' class='popdown'>
112
+ The speed of Observer 1 relative to a stationary reference frame, in meters per second.
113
+ This affects relativistic time dilation. For example, a velocity of 0 m/s means no relativistic effects,
114
+ while high speeds (e.g., 0.5 × speed of light) significantly slow time. Must be less than 299,792,458 m/s.
115
+ </div>
116
+ </div>
117
  </div>
118
  <div class='input-group'>
119
  <h3>Observer 2</h3>
120
+ <div>
121
+ <label onclick="togglePopdown('mass2-info')">Mass (kg):</label>
122
+ <input type='number' id='mass2' value='5.972e26'>
123
+ <div id='mass2-info' class='popdown'>
124
+ The mass of the object affecting Observer 2, in kilograms.
125
+ For example, a planet 100 times Earth's mass would be 5.972 × 10²⁶ kg.
126
+ This influences gravitational time dilation, with larger masses causing greater time dilation.
127
+ </div>
128
+ </div>
129
+ <div>
130
+ <label onclick="togglePopdown('radius2-info')">Radius (m):</label>
131
+ <input type='number' id='radius2' value='6.371e6'>
132
+ <div id='radius2-info' class='popdown'>
133
+ The distance from the center of the massive object to Observer 2, in meters.
134
+ For example, using Earth's radius (6.371 × 10⁶ m) assumes Observer 2 is on the surface.
135
+ Smaller distances amplify gravitational time dilation effects.
136
+ </div>
137
+ </div>
138
+ <div>
139
+ <label onclick="togglePopdown('velocity2-info')">Velocity (m/s):</label>
140
+ <input type='number' id='velocity2' value='0'>
141
+ <div id='velocity2-info' class='popdown'>
142
+ The speed of Observer 2 relative to a stationary reference frame, in meters per second.
143
+ High velocities cause relativistic time dilation, slowing time for the observer.
144
+ Set to 0 for no relativistic effects. Must be less than the speed of light (299,792,458 m/s).
145
+ </div>
146
+ </div>
147
  </div>
148
  <div class='input-group'>
149
+ <div>
150
+ <label onclick="togglePopdown('proper_time-info')">Proper Time (s):</label>
151
+ <input type='number' id='proper_time' value='86400'>
152
+ <div id='proper_time-info' class='popdown'>
153
+ The time experienced by a reference observer in a weak gravitational field and at rest, in seconds.
154
+ For example, 86,400 seconds equals 1 day. This is the baseline time used to calculate how much
155
+ time each observer experiences after dilation effects are applied.
156
+ </div>
157
+ </div>
158
  </div>
159
  <button onclick='calculate()'>Calculate</button>
160
  <div id='result'></div>
161
  </div>
162
  <script>
163
+ function togglePopdown(id) {
164
+ const popdown = document.getElementById(id);
165
+ popdown.classList.toggle('show');
166
+ }
167
+
168
  async function calculate() {
169
  const data = {
170
  mass1: parseFloat(document.getElementById('mass1').value),
 
200
  velocity2 = data.get('velocity2', 0)
201
  t0 = data.get('proper_time')
202
 
 
203
  if not all([mass1, radius1, mass2, radius2, t0]) or any(x <= 0 for x in [mass1, radius1, mass2, radius2, t0]):
204
  return jsonify({"message": "Invalid input: All values must be positive numbers."}), 400
205
 
 
206
  t1_grav = gravitational_time_dilation(mass1, radius1, t0)
207
  t2_grav = gravitational_time_dilation(mass2, radius2, t0)
208
 
209
  if t1_grav is None or t2_grav is None:
210
  return jsonify({"message": "Error: Invalid gravitational parameters (e.g., within event horizon)."}), 400
211
 
 
212
  t1_rel = relativistic_time_dilation(velocity1, t0) if velocity1 > 0 else t0
213
  t2_rel = relativistic_time_dilation(velocity2, t0) if velocity2 > 0 else t0
214
 
215
  if (velocity1 > 0 and t1_rel is None) or (velocity2 > 0 and t2_rel is None):
216
  return jsonify({"message": "Error: Velocity exceeds speed of light."}), 400
217
 
 
218
  t1_total = t1_grav if velocity1 == 0 else t1_grav * (t1_rel / t0)
219
  t2_total = t2_grav if velocity2 == 0 else t2_grav * (t2_rel / t0)
220
 
 
221
  time_diff = abs(t1_total - t2_total)
222
 
 
223
  message = (
224
  f"Results for proper time {t0:.2f} seconds:\n"
225
  f"Observer 1:\n"
 
237
  return jsonify({"message": f"Error: {str(e)}"}), 500
238
 
239
  if __name__ == '__main__':
 
240
  port = int(os.environ.get("PORT", 7860))
241
  app.run(host='0.0.0.0', port=port)