import tensorflow as tf
# In tectonics.js surface height is calculated at Lithosphere.surface_height()
# surface_height
#   - displacement
#     - thickness ✓
#     - density ✓
#     - material_density ✓
#   - sealevel (guess)

material_density = {
    'fine_sediment': 1500.,
    'voarse_sediment': 1500.,
    'sediment': 1500.,
    'sedimentary': 2600.,
    'metamorphic': 2800.,
    'felsic_plutonic': 2600.,
    'felsic_volcanic': 2600.,
    'mafic_volcanic_min': 2890.,  # Carlson & Raskin 1984
    'mafic_volcanic_max': 3300.,
    'mantle': 3075.,  # derived empirically using isostatic model
    'ocean': 1026.,
}


def get_thickness(saved):
  fraction_of_lifetime = saved.crust_layers['age'] / 7889537440886400.0
  mafic_density = material_density['mafic_volcanic_min'] + (
      material_density['mafic_volcanic_max'] -
      material_density['mafic_volcanic_min']) * fraction_of_lifetime
  total_thickness = (saved.crust_layers['mafic_volcanic'] +
                     saved.crust_layers['mafic_plutonic']) / mafic_density
  for rocktype, density in material_density.items():
    if rocktype in saved.crust_layers:
      total_thickness += saved.crust_layers[rocktype] / density
  return total_thickness


def isostatic_displacement(saved):
  thickness = get_thickness(saved)
  return thickness - saved.crust_layers['total'] / material_density['mantle']