1
0
Fork 0
mirror of https://github.com/Findus23/rebound-collisions.git synced 2024-09-19 15:53:48 +02:00

move utils into separate files

This commit is contained in:
Lukas Winkler 2021-04-05 17:57:45 +02:00
parent 8ce566edd1
commit 940df1bfe1
Signed by: lukas
GPG key ID: 54DE4D798D244853
6 changed files with 71 additions and 62 deletions

5
utils/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from constants import *
from math import *
from os_tools import *
from plotting import *
from simulation import *

5
utils/constants.py Normal file
View file

@ -0,0 +1,5 @@
solar_radius = 6.957e8
solar_mass = 1.9885e+30
earth_water_mass = 1.7e21 # Lécuyer et al. 1998
earth_mass = 5.9722e24

4
utils/math.py Normal file
View file

@ -0,0 +1,4 @@
def clamp(n: float, smallest: float, largest: float) -> float:
assert smallest < largest
return max(smallest, min(n, largest))

39
utils/os_tools.py Normal file
View file

@ -0,0 +1,39 @@
import os
import subprocess
from pathlib import Path
import socket
from sys import argv
from setproctitle import setproctitle
def filename_from_argv(argument: str = None) -> Path:
if len(argv) < 2:
raise ValueError("specify filename")
if argument:
fn = argument
else:
fn = argv[1]
fn = fn.replace(".bin", "").replace(".meta.json", "")
if fn.endswith("."):
fn = fn[:-1]
return Path(fn.replace(".bin", "").replace(".meta.json", ""))
def git_hash() -> str:
output = subprocess.run(["git", "rev-parse", "--verify", "HEAD"], capture_output=True)
return output.stdout.decode()
def check_heartbeat_needs_recompile() -> None:
library = Path("heartbeat/heartbeat.so")
code = library.with_suffix(".c")
if code.stat().st_mtime > library.stat().st_mtime:
raise RuntimeError("heartbeat.so is out of date. Please recompile it from source.")
def process_friendlyness(fn: Path) -> None:
if socket.gethostname() == "standpc":
# only handle other computers specially
return
setproctitle(f"[{fn.stem}] [rebound-watersim] read /home/winklerl23/sim-info.txt for more information")
os.nice(5)

14
utils/plotting.py Normal file
View file

@ -0,0 +1,14 @@
from typing import Tuple
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
def create_figure() -> Tuple[Figure, Axes]:
"""
helper function for matplotlib OOP interface with proper typing
"""
fig: Figure = plt.figure()
ax: Axes = fig.gca()
return fig, ax

View file

@ -1,24 +1,13 @@
import os
import socket
import subprocess
from ctypes import c_uint32
from pathlib import Path
from random import randint
from sys import argv
from typing import Tuple, Dict
from typing import Dict
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from numpy import linalg
from rebound import Simulation, Orbit, Particle, OrbitPlot
from rebound import Simulation, Orbit, OrbitPlot, Particle
from scipy.constants import pi, gravitational_constant
from setproctitle import setproctitle
from extradata import ExtraData
solar_radius = 6.957e8
solar_mass = 1.9885e+30
from utils import solar_mass
def random_hash() -> c_uint32:
@ -76,38 +65,11 @@ def show_orbits(sim: Simulation):
OrbitPlot(sim, slices=1, color=True)
plt.show()
def clamp(n: float, smallest: float, largest: float) -> float:
assert smallest < largest
return max(smallest, min(n, largest))
def filename_from_argv(argument: str = None) -> Path:
if len(argv) < 2:
raise ValueError("specify filename")
if argument:
fn = argument
else:
fn = argv[1]
fn = fn.replace(".bin", "").replace(".meta.json", "")
if fn.endswith("."):
fn = fn[:-1]
return Path(fn.replace(".bin", "").replace(".meta.json", ""))
def create_figure() -> Tuple[Figure, Axes]:
"""
helper function for matplotlib OOP interface with proper typing
"""
fig: Figure = plt.figure()
ax: Axes = fig.gca()
return fig, ax
def reorder_particles(sim: Simulation, ed: ExtraData) -> None:
"""
probably not needed anymore
"""
exit()
particles_by_hash: Dict[int, Particle] = {}
hashes = []
suns = []
@ -151,23 +113,3 @@ def reorder_particles(sim: Simulation, ed: ExtraData) -> None:
# TODO: double-check meaning
sim.testparticle_type = 1
assert sim.N == original_N
def git_hash() -> str:
output = subprocess.run(["git", "rev-parse", "--verify", "HEAD"], capture_output=True)
return output.stdout.decode()
def check_heartbeat_needs_recompile() -> None:
library = Path("heartbeat/heartbeat.so")
code = library.with_suffix(".c")
if code.stat().st_mtime > library.stat().st_mtime:
raise RuntimeError("heartbeat.so is out of date. Please recompile it from source.")
def process_friendlyness(fn: Path) -> None:
if socket.gethostname() == "standpc":
# only handle other computers specially
return
setproctitle(f"[{fn.stem}] [rebound-watersim] read /home/winklerl23/sim-info.txt for more information")
os.nice(5)