Skip to content

Commit

Permalink
Improvements for check with hydrostatic integration fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nichollsh committed Sep 11, 2024
1 parent d473120 commit 0e20718
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/janus/utils/atmosphere_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import netCDF4 as nc
from janus.utils import phys
from janus.utils.height import AtmosphericHeight
from janus.utils.height import integrate_heights
import os, copy, platform, shutil
import pwd
from datetime import datetime
Expand Down Expand Up @@ -172,7 +172,7 @@ def __init__(self, T_surf: float, P_surf: float, P_top: float, pl_radius: float,
self.rho = np.zeros(self.nlev) # Density of atmosphere at a given level
self.ifatm = np.zeros(self.nlev) # Defines nth level to which atmosphere is calculated
self.cp = np.zeros(self.nlev) # Mean heat capacity
self.height_error = True # error when doing hydrostatic integration?
self.height_error = False # error when doing hydrostatic integration?

# Define T and P arrays from surface up
self.tmp[0] = self.ts # K
Expand Down Expand Up @@ -389,7 +389,8 @@ def write_ncdf(self, fpath:str):

# ----------------------
# Calculate gravity and height (in case it hasn't been done already)
self.z = AtmosphericHeight(self, self.planet_mass, self.planet_radius)
self.z, height_err = integrate_heights(self, self.planet_mass, self.planet_radius)
self.height_error = height_err

self.zl = np.zeros(len(self.z)+1)
for i in range(1,len(self.z)):
Expand Down
24 changes: 12 additions & 12 deletions src/janus/utils/height.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import numpy as np
import janus.utils.phys as phys

import logging
import logging
log = logging.getLogger("fwl."+__name__)


def gravity( m, r ):
g = phys.G*m/r**2
return g

def AtmosphericHeight(atm, m_planet, r_planet):
def integrate_heights(atm, m_planet, r_planet):

z_profile = np.zeros(len(atm.p))
grav_s = gravity( m_planet, r_planet )
Expand All @@ -20,7 +20,7 @@ def AtmosphericHeight(atm, m_planet, r_planet):
for vol in atm.vol_list.keys():
atm.x_gas[vol] = atm.x_gas[vol][::-1]

atm.height_error = False
ok = True
for n in range(0, len(z_profile)-1):

# Gravity with height
Expand All @@ -32,27 +32,27 @@ def AtmosphericHeight(atm, m_planet, r_planet):
mean_molar_mass += phys.molar_mass[vol]*atm.x_gas[vol][n]

# Use hydrostatic equation to get height difference
dz = phys.R_gas * atm.tmp[n] / (mean_molar_mass * grav_z * atm.p[n]) * (atm.p[n] - atm.p[n+1])
dz = phys.R_gas * atm.tmp[n] / (mean_molar_mass * grav_z * atm.p[n]) * (atm.p[n] - atm.p[n+1])

# Next height
z_profile[n+1] = z_profile[n] + dz

# Check if heights are very large.
# This implies that the hydrostatic/gravity integration failed.
if z_profile[n+1] > 1.0e8:
atm.height_error = True
log.warning("Hydrostatic integration blew up. Setting dummy values for height")
if (z_profile[n+1] > 1.0e8) or (dz > 1e8):
ok = False
log.error("Hydrostatic integration blew up. Setting dummy values for height")
break

# Set dummy values
if atm.height_error:
# Set dummy values
if not ok:
z_profile = np.linspace(0.0, 1000.0, len(atm.p))

# Reverse arrays again back to normal
atm.p = atm.p[::-1]
atm.tmp = atm.tmp[::-1]
for vol in atm.vol_list.keys():
atm.x_gas[vol] = atm.x_gas[vol][::-1]
z_profile = z_profile[::-1]

return z_profile
return z_profile, ok

0 comments on commit 0e20718

Please sign in to comment.