Files
global_warming_course/Time-Stepping Naked Planet Model/Grading/naked_planet_tosubmit.py
2025-09-16 13:35:18 +02:00

69 lines
1.9 KiB
Python

# %%
# %%
import numpy as np
import matplotlib.pyplot as plt
# Set up constants as given:
timeStep_years = 100 # years
waterDepth = 4000 # meters
L = 1350 # W/m²
albedo = 0.3
epsilon = 1
sigma = 5.67e-8 # W/m² K^4
# The incoming solar flux is constant:
incoming_flux = L * (1 - albedo) / 4 # W/m²
# Calculate the heat capacity (J/m² K)
# (water density = 1000 kg/m³, specific heat = 4184 J/(kg·K))
heat_capacity = waterDepth * 1000 * 4184
# Read the number of time steps from stdin
nSteps = int(input(""))
# Set the initial conditions.
# (Initial temperature is chosen in Kelvins; 0 K is allowed though not physical.)
initial_temperature = 0.0 # Kelvin
heatContent = initial_temperature * heat_capacity # in J/m²
# Prepare lists for time and temperature (for plotting, if desired)
time_list = [0]
temperature_list = [initial_temperature]
# Convert the time step to seconds.
dt = timeStep_years * 365 * 24 * 3600
# Integration loop: update heat content and temperature each step.
for i in range(nSteps):
# Current temperature in Kelvin:
T = heatContent / heat_capacity
# Differential equation: dHeat/dt = incoming_flux - ε·σ·(T)⁴
dH_dt = incoming_flux - epsilon * sigma * (T**4)
# Update the heat content over the time step:
heatContent = heatContent + dH_dt * dt
# Append new time and temperature to lists:
time_list.append((i+1) * timeStep_years)
temperature_list.append(heatContent / heat_capacity)
# Compute the final temperature (in Kelvin) and the corresponding outgoing heat flux.
final_temperature = heatContent / heat_capacity
final_heat_flux = epsilon * sigma * (final_temperature**4)
# Output only the final temperature and heat flux.
print(final_temperature, final_heat_flux)
# Plotting code
plt.plot(time_list, temperature_list)
plt.show()
# %%