aboutsummaryrefslogtreecommitdiffstats
path: root/Helpers/physics_helpers.py
blob: 9a4c88552cd74dfaead61572be21ab5ed70fac04 (plain)
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
"""
Simple Physics Helper Functions.
"""

import numpy as np


def mass2radius(mass, rho=2.0):
    """
    Convert mass to radius. Assuming sphere.

    @param mass - mass - [kg]
    @param rho - density - [g/cc]
    @return radius - radius - [km]

    Mass in kg; density in g/cc
    Default density is 2 g/cc; earth has 5 g/cc
    """

    rho /= 1000.0 # kg/cc
    rho *= (100.0*1000.0)**3.0 # kg/km3
    V = mass/rho # km3
    r = (V/np.pi * 0.75)**(1./3.) # km
    return r


def radius2mass(radius, rho=2.0):
    """
    Convert radius to mass. Assuming sphere.

    @param radius - radius - [km]
    @param rho - density - [g/cc]
    @return mass - mass - [kg]

    Radius is km; density in g/cc
    Default density is 2 g/cc; earth has 5 g/cc
    """

    rho /= 1000.0 # kg/cc
    rho *= (100.0*1000.0)**3.0 # kg/km3
    mass = 4./3. * np.pi* radius**3.0 * rho # kg
    return mass


def rm2density(radius, mass):
    """
    Compute Density from Radius & Mass.

    @param radius - radius - [km]
    @param mass - mass - [kg]
    @return density - density - [g/cm3]
    """

    volume = 4.0/3.0 * np.pi * radius**3.0 # km3
    density = mass / volume  # kg/km3
    density *= 1000.0        # g/km3
    density /= (1000.0)**3.0 # g/m3
    density /= (100.0)**3.0  # g/cm3
    return density