broadfield-dev commited on
Commit
2386211
·
verified ·
1 Parent(s): 9cb0542

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -12
app.py CHANGED
@@ -1,31 +1,35 @@
1
  # app.py
2
  from flask import Flask, send_from_directory, jsonify, request
3
  import os
 
4
 
5
  app = Flask(__name__)
6
 
7
- # Updated simulation parameters with real-world masses (scaled down for simulation)
8
- # Sun: 1.989 x 10^30 kg, Earth: 5.972 x 10^24 kg, Mars: 6.417 x 10^23 kg
9
- # Scale factor: 1e-26 for manageable numbers
 
 
 
10
  simulation_params = {
11
  "sun": {
12
- "mass": 1.989e4, # Scaled mass (1.989 x 10^30 kg * 1e-26)
13
  "position": [0, 0, 0],
14
  "orbital_velocity": 0, # Sun is stationary
15
  },
16
  "earth": {
17
- "mass": 5.972e-2, # Scaled mass (5.972 x 10^24 kg * 1e-26)
18
- "position": [10, 0, 0], # Approx 1 AU scaled down (149.6 million km -> 10 units)
19
- "orbital_velocity": 0.0298, # 29.8 km/s scaled down
20
  },
21
  "mars": {
22
- "mass": 6.417e-3, # Scaled mass (6.417 x 10^23 kg * 1e-26)
23
- "position": [15, 0, 0], # Approx 1.52 AU scaled down (227.9 million km -> 15 units)
24
- "orbital_velocity": 0.0241, # 24.1 km/s scaled down
25
  },
26
  "fluid_speed": 0.1,
27
- "fluid_friction": 0.9, # Default friction factor
28
- "fluid_deflection": 0.1, # Default deflection strength
29
  }
30
 
31
  # Serve the frontend
@@ -51,6 +55,32 @@ def update_params():
51
  simulation_params.update(data)
52
  return jsonify({"status": "success", "params": simulation_params})
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  if __name__ == '__main__':
55
  port = int(os.environ.get('PORT', 7860)) # Default port for Hugging Face Spaces
56
  app.run(host='0.0.0.0', port=port)
 
1
  # app.py
2
  from flask import Flask, send_from_directory, jsonify, request
3
  import os
4
+ import json
5
 
6
  app = Flask(__name__)
7
 
8
+ # Directory to store settings
9
+ SETTINGS_DIR = "settings"
10
+ if not os.path.exists(SETTINGS_DIR):
11
+ os.makedirs(SETTINGS_DIR)
12
+
13
+ # Real-world simulation parameters (unscaled)
14
  simulation_params = {
15
  "sun": {
16
+ "mass": 1.989e30, # Real mass in kg
17
  "position": [0, 0, 0],
18
  "orbital_velocity": 0, # Sun is stationary
19
  },
20
  "earth": {
21
+ "mass": 5.972e24, # Real mass in kg
22
+ "position": [149.6e6, 0, 0], # 1 AU in km
23
+ "orbital_velocity": 29.8, # Real orbital velocity in km/s
24
  },
25
  "mars": {
26
+ "mass": 6.417e23, # Real mass in kg
27
+ "position": [227.9e6, 0, 0], # 1.52 AU in km
28
+ "orbital_velocity": 24.1, # Real orbital velocity in km/s
29
  },
30
  "fluid_speed": 0.1,
31
+ "fluid_friction": 0.9,
32
+ "fluid_deflection": 0.1,
33
  }
34
 
35
  # Serve the frontend
 
55
  simulation_params.update(data)
56
  return jsonify({"status": "success", "params": simulation_params})
57
 
58
+ # API to save settings to a JSON file
59
+ @app.route('/api/save', methods=['POST'])
60
+ def save_settings():
61
+ try:
62
+ filename = os.path.join(SETTINGS_DIR, "settings.json")
63
+ with open(filename, 'w') as f:
64
+ json.dump(simulation_params, f, indent=4)
65
+ return jsonify({"status": "success", "message": "Settings saved successfully"})
66
+ except Exception as e:
67
+ return jsonify({"status": "error", "message": str(e)}), 500
68
+
69
+ # API to load settings from a JSON file
70
+ @app.route('/api/load', methods=['GET'])
71
+ def load_settings():
72
+ global simulation_params
73
+ try:
74
+ filename = os.path.join(SETTINGS_DIR, "settings.json")
75
+ if os.path.exists(filename):
76
+ with open(filename, 'r') as f:
77
+ simulation_params = json.load(f)
78
+ return jsonify({"status": "success", "params": simulation_params})
79
+ else:
80
+ return jsonify({"status": "error", "message": "No saved settings found"}), 404
81
+ except Exception as e:
82
+ return jsonify({"status": "error", "message": str(e)}), 500
83
+
84
  if __name__ == '__main__':
85
  port = int(os.environ.get('PORT', 7860)) # Default port for Hugging Face Spaces
86
  app.run(host='0.0.0.0', port=port)