Build a river temperature prediction with climate drivers and flow.
- User
Build a river temperature prediction with climate drivers and flow.
Assistant
Build a river temperature prediction with climate drivers and flow.
The user wants a system to predict river temperature using climate drivers and flow. This is a hydrological/environmental modeling task. Let me design a comprehensive scientific visualization and simulation tool.
Key components:
I'll create:
A physics-based thermal model that accounts for:
Interactive controls for climate parameters
Real-time visualization of temperature predictions
Time series charts
Heat flux breakdown visualization
River cross-section thermal profile
Design aesthetic:
Let me build this comprehensive prediction system.
A physics-based thermal model that predicts river temperature using climate drivers (air temperature, solar radiation, wind, humidity) and flow discharge. The model implements heat balance equations including atmospheric exchange, solar absorption, and thermal advection.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>River Temperature Prediction Model</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg: #0a1628;
--bg-secondary: #0f2140;
--fg: #e8f0f8;
--muted: #5a7a9a;
--accent: #00d4aa;
--accent-warm: #ff6b4a;
--card: rgba(15, 33, 64, 0.8);
--border: rgba(90, 122, 154, 0.3);
--water-cold: #0088cc;
--water-warm: #cc4400;
}
* { box-sizing: border-box; }
body {
font-family: 'Space Grotesk', sans-serif;
background: var(--bg);
color: var(--fg);
min-height: 100vh;
overflow-x: hidden;
}
.mono { font-family: 'IBM Plex Mono', monospace; }
.bg-pattern {
background-image:
radial-gradient(ellipse at 20% 30%, rgba(0, 212, 170, 0.08) 0%, transparent 50%),
radial-gradient(ellipse at 80% 70%, rgba(255, 107, 74, 0.06) 0%, transparent 50%),
linear-gradient(180deg, var(--bg) 0%, var(--bg-secondary) 100%);
}
.glass-card {
background: var(--card);
backdrop-filter: blur(20px);
border: 1px solid var(--border);
border-radius: 16px;
}
.input-range {
-webkit-appearance: none;
appearance: none;
width: 100%;
height: 6px;
border-radius: 3px;
background: rgba(90, 122, 154, 0.3);
outline: none;
}
.input-range::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
background: var(--accent);
cursor: pointer;
box-shadow: 0 0 10px rgba(0, 212, 170, 0.5);
transition: transform 0.2s, box-shadow 0.2s;
}
.input-range::-webkit-slider-thumb:hover {
transform: scale(1.2);
box-shadow: 0 0 20px rgba(0, 212, 170, 0.7);
}
.input-range::-moz-range-thumb {
width: 18px;
height: 18px;
border-radius: 50%;
background: var(--accent);
cursor: pointer;
border: none;
}
.water-viz {
position: relative;
height: 120px;
border-radius: 12px;
overflow: hidden;
background: linear-gradient(180deg, rgba(0, 136, 204, 0.2) 0%, rgba(0, 136, 204, 0.4) 100%);
}
.water-surface {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 40px;
background: linear-gradient(180deg,
rgba(255, 255, 255, 0.15) 0%,
rgba(255, 255, 255, 0.05) 50%,
transparent 100%);
}
.flow-particle {
position: absolute;
width: 4px;
height: 4px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.6);
animation: flowMove linear infinite;
}
@keyframes flowMove {
0% { transform: translateX(-100vw); }
100% { transform: translateX(100vw); }
}
@keyframes fadeSlideUp {
from { opacity: 0; transform: translateY(30px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.6; }
}
.animate-in {
animation: fadeSlideUp 0.6s ease-out forwards;
}
.stagger-1 { animation-delay: 0.1s; opacity: 0; }
.stagger-2 { animation-delay: 0.2s; opacity: 0; }
.stagger-3 { animation-delay: 0.3s; opacity: 0; }
.stagger-4 { animation-delay: 0.4s; opacity: 0; }
.heat-bar {
height: 8px;
border-radius: 4px;
transition: width 0.5s ease-out, background 0.5s ease-out;
}
.temp-indicator {
transition: all 0.5s ease-out;
}
.chart-line {
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: drawLine 2s ease-out forwards;
}
@keyframes drawLine {
to { stroke-dashoffset: 0; }
}
.metric-card {
transition: transform 0.3s, box-shadow 0.3s;
}
.metric-card:hover {
transform: translateY(-4px);
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}
.btn-run {
background: linear-gradient(135deg, var(--accent) 0%, #00b894 100%);
transition: all 0.3s ease;
}
.btn-run:hover {
transform: translateY(-2px);
box-shadow: 0 10px 30px rgba(0, 212, 170, 0.4);
}
.btn-run:active {
transform: translateY(0);
}
.tooltip {
position: relative;
}
.tooltip::after {
content: attr(data-tip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
padding: 6px 12px;
background: var(--bg);
border: 1px solid var(--border);
border-radius: 6px;
font-size: 12px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s;
}
.tooltip:hover::after {
opacity: 1;
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
.focus-visible:focus {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
</style>
</head>
<body class="bg-pattern">
<div class="max-w-7xl mx-auto px-4 py-8 md:py-12">
<!-- Header -->
<header class="mb-10 animate-in stagger-1">
<div class="flex items-center gap-3 mb-2">
<div class="w-10 h-10 rounded-lg bg-gradient-to-br from-cyan-500 to-teal-600 flex items-center justify-center">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2">
<path d="M12 2v6m0 0l3-3m-3 3l-3-3"/>
<path d="M3 12c0 4.97 4.03 9 9 9s9-4.03 9-9"/>
<path d="M7 12c0-2.76 2.24-5 5-5"/>
</svg>
</div>
<span class="mono text-sm text-[var(--muted)]">Hydrothermal Model v2.0</span>
</div>
<h1 class="text-3xl md:text-4xl font-semibold tracking-tight mb-2">
River Temperature Prediction
</h1>
<p class="text-[var(--muted)] max-w-2xl">
Physics-based thermal model incorporating climate drivers and hydraulic flow dynamics for accurate stream temperature forecasting.
</p>
</header>
<!-- Main Grid -->
<div class="grid lg:grid-cols-3 gap-6">
<!-- Left Panel: Climate Drivers -->
<div class="lg:col-span-1 space-y-6">
<!-- Climate Inputs -->
<div class="glass-card p-6 animate-in stagger-2">
<h2 class="text-lg font-medium mb-5 flex items-center gap-2">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--accent)" stroke-width="2">
<circle cx="12" cy="12" r="4"/>
<path d="M12 2v2m0 16v2M4.93 4.93l1.41 1.41m11.32 11.32l1.41 1.41M2 12h2m16 0h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"/>
</svg>
Climate Drivers
</h2>
<div class="space-y-5">
<!-- Air Temperature -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Air Temperature</label>
<span class="mono text-sm" id="airTempValue">20°C</span>
</div>
<input type="range" id="airTemp" class="input-range focus-visible" min="-10" max="40" value="20" aria-label="Air Temperature">
</div>
<!-- Solar Radiation -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Solar Radiation</label>
<span class="mono text-sm" id="solarValue">500 W/m²</span>
</div>
<input type="range" id="solar" class="input-range focus-visible" min="0" max="1000" value="500" aria-label="Solar Radiation">
</div>
<!-- Wind Speed -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Wind Speed</label>
<span class="mono text-sm" id="windValue">3.0 m/s</span>
</div>
<input type="range" id="wind" class="input-range focus-visible" min="0" max="15" value="3" step="0.5" aria-label="Wind Speed">
</div>
<!-- Relative Humidity -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Relative Humidity</label>
<span class="mono text-sm" id="humidityValue">60%</span>
</div>
<input type="range" id="humidity" class="input-range focus-visible" min="10" max="100" value="60" aria-label="Relative Humidity">
</div>
<!-- Cloud Cover -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Cloud Cover</label>
<span class="mono text-sm" id="cloudValue">30%</span>
</div>
<input type="range" id="cloud" class="input-range focus-visible" min="0" max="100" value="30" aria-label="Cloud Cover">
</div>
</div>
</div>
<!-- Flow Parameters -->
<div class="glass-card p-6 animate-in stagger-3">
<h2 class="text-lg font-medium mb-5 flex items-center gap-2">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--accent)" stroke-width="2">
<path d="M12 2c-5 5-8 8-8 12a8 8 0 1016 0c0-4-3-7-8-12z"/>
</svg>
Hydraulic Flow
</h2>
<div class="space-y-5">
<!-- Discharge -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Discharge</label>
<span class="mono text-sm" id="dischargeValue">50 m³/s</span>
</div>
<input type="range" id="discharge" class="input-range focus-visible" min="5" max="200" value="50" aria-label="Discharge">
</div>
<!-- Upstream Temperature -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Upstream Temperature</label>
<span class="mono text-sm" id="upstreamValue">15°C</span>
</div>
<input type="range" id="upstream" class="input-range focus-visible" min="0" max="30" value="15" aria-label="Upstream Temperature">
</div>
<!-- River Depth -->
<div>
<div class="flex justify-between mb-2">
<label class="text-sm text-[var(--muted)]">Mean Depth</label>
<span class="mono text-sm" id="depthValue">2.0 m</span>
</div>
<input type="range" id="depth" class="input-range focus-visible" min="0.5" max="10" value="2" step="0.1" aria-label="River Depth">
</div>
</div>
<button id="runModel" class="btn-run w-full mt-6 py-3 px-4 rounded-lg font-medium text-[var(--bg)] flex items-center justify-center gap-2 focus-visible">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polygon points="5 3 19 12 5 21 5 3"/>
</svg>
Run Prediction Model
</button>
</div>
</div>
<!-- Right Panel: Results -->
<div class="lg:col-span-2 space-y-6">
<!-- Current Prediction -->
<div class="glass-card p-6 animate-in stagger-2">
<div class="flex items-center justify-between mb-6">
<h2 class="text-lg font-medium">Predicted River Temperature</h2>
<span class="mono text-xs text-[var(--muted)]">7-day forecast</span>
</div>
<div class="flex items-center gap-8 mb-6">
<div class="relative">
<div id="tempGauge" class="w-32 h-32 rounded-full border-4 flex items-center justify-center" style="border-color: var(--water-cold)">
<div class="text-center">
<span id="mainTemp" class="text-4xl font-semibold">18.2</span>
<span class="text-xl text-[var(--muted)]">°C</span>
</div>
</div>
</div>
<div class="flex-1 space-y-3">
<div class="flex justify-between items-center">
<span class="text-sm text-[var(--muted)]">Thermal Classification</span>
<span id="thermalClass" class="mono text-sm px-3 py-1 rounded-full bg-cyan-500/20 text-cyan-400">Cool</span>
</div>
<div class="flex justify-between items-center">
<span class="text-sm text-[var(--muted)]">Confidence Interval</span>
<span id="confidence" class="mono text-sm">±1.2°C</span>
</div>
<div class="flex justify-between items-center">
<span class="text-sm text-[var(--muted)]">Model Uncertainty</span>
<span id="uncertainty" class="mono text-sm">6.5%</span>
</div>
</div>
</div>
<!-- Water Visualization -->
<div class="water-viz" id="waterViz">
<div class="water-surface"></div>
<div id="flowParticles"></div>
<div class="absolute bottom-0 left-0 right-0 h-8 bg-gradient-to-t from-black/30 to-transparent"></div>
<div class="absolute bottom-2 left-3 flex items-center gap-2">
<span class="text-xs text-white/60">Flow Direction</span>
<svg width="24" height="12" viewBox="0 0 24 12" fill="none" stroke="white" stroke-width="1.5" opacity="0.6">
<path d="M0 6h20m0 0l-4-4m4 4l-4 4"/>
</svg>
</div>
<div class="absolute top-2 right-3">
<span id="flowSpeed" class="mono text-xs text-white/60">1.2 m/s</span>
</div>
</div>
</div>
<!-- Heat Flux Components -->
<div class="glass-card p-6 animate-in stagger-3">
<h2 class="text-lg font-medium mb-5">Heat Flux Components</h2>
<div class="grid sm:grid-cols-2 gap-4">
<div class="metric-card p-4 rounded-xl bg-black/20 border border-[var(--border)]">
<div class="flex items-center gap-2 mb-2">
<div class="w-3 h-3 rounded-full bg-orange-400"></div>
<span class="text-sm text-[var(--muted)]">Solar Absorption</span>
</div>
<div class="flex items-end justify-between">
<span id="solarFlux" class="text-2xl font-semibold mono">+142</span>
<span class="text-sm text-[var(--muted)]">W/m²</span>
</div>
<div class="mt-2 h-2 rounded-full bg-black/30">
<div id="solarBar" class="heat-bar bg-gradient-to-r from-orange-500 to-yellow-400" style="width: 70%"></div>
</div>
</div>
<div class="metric-card p-4 rounded-xl bg-black/20 border border-[var(--border)]">
<div class="flex items-center gap-2 mb-2">
<div class="w-3 h-3 rounded-full bg-cyan-400"></div>
<span class="text-sm text-[var(--muted)]">Longwave Radiation</span>
</div>
<div class="flex items-end justify-between">
<span id="longwaveFlux" class="text-2xl font-semibold mono">-48</span>
<span class="text-sm text-[var(--muted)]">W/m²</span>
</div>
<div class="mt-2 h-2 rounded-full bg-black/30">
<div id="longwaveBar" class="heat-bar bg-gradient-to-r from-cyan-500 to-blue-400" style="width: 40%"></div>
</div>
</div>
<div class="metric-card p-4 rounded-xl bg-black/20 border border-[var(--border)]">
<div class="flex items-center gap-2 mb-2">
<div class="w-3 h-3 rounded-full bg-teal-400"></div>
<span class="text-sm text-[var(--muted)]">Evaporative Heat</span>
</div>
<div class="flex items-end justify-between">
<span id="evapFlux" class="text-2xl font-semibold mono">-86</span>
<span class="text-sm text-[var(--muted)]">W/m²</span>
</div>
<div class="mt-2 h-2 rounded-full bg-black/30">
<div id="evapBar" class="heat-bar bg-gradient-to-r from-teal-500 to-emerald-400" style="width: 55%"></div>
</div>
</div>
<div class="metric-card p-4 rounded-xl bg-black/20 border border-[var(--border)]">
<div class="flex items-center gap-2 mb-2">
<div class="w-3 h-3 rounded-full bg-purple-400"></div>
<span class="text-sm text-[var(--muted)]">Convective Heat</span>
</div>
<div class="flex items-end justify-between">
<span id="convFlux" class="text-2xl font-semibold mono">-24</span>
<span class="text-sm text-[var(--muted)]">W/m²</span>
</div>
<div class="mt-2 h-2 rounded-full bg-black/30">
<div id="convBar" class="heat-bar bg-gradient-to-r from-purple-500 to-pink-400" style="width: 30%"></div>
</div>
</div>
</div>
<div class="mt-4 p-4 rounded-xl bg-black/30 border border-[var(--border)]">
<div class="flex items-center justify-between">
<span class="text-sm text-[var(--muted)]">Net Heat Flux</span>
<span id="netFlux" class="text-xl font-semibold mono text-[var(--accent)]">-16 W/m²</span>
</div>
<p class="text-xs text-[var(--muted)] mt-1">Negative flux indicates river cooling; positive indicates warming</p>
</div>
</div>
<!-- 7-Day Forecast Chart -->
<div class="glass-card p-6 animate-in stagger-4">
<h2 class="text-lg font-medium mb-5">7-Day Temperature Forecast</h2>
<div class="relative h-48" id="chartContainer">
<svg id="forecastChart" width="100%" height="100%" preserveAspectRatio="none">
<!-- Grid lines -->
<g class="grid-lines" stroke="rgba(90, 122, 154, 0.2)" stroke-width="1">
<line x1="0" y1="0" x2="100%" y2="0"/>
<line x1="0" y1="25%" x2="100%" y2="25%"/>
<line x1="0" y1="50%" x2="100%" y2="50%"/>
<line x1="0" y1="75%" x2="100%" y2="75%"/>
<line x1="0" y1="100%" x2="100%" y2="100%"/>
</g>
<!-- Confidence band -->
<path id="confidenceBand" fill="rgba(0, 212, 170, 0.1)" stroke="none"/>
<!-- Main temperature line -->
<path id="tempLine" fill="none" stroke="var(--accent)" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="chart-line"/>
<!-- Data points -->
<g id="dataPoints"></g>
</svg>
<!-- Y-axis labels -->
<div class="absolute left-0 top-0 h-full flex flex-col justify-between text-xs mono text-[var(--muted)] pr-2">
<span>30°C</span>
<span>22°C</span>
<span>14°C</span>
<span>6°C</span>
<span>-2°C</span>
</div>
</div>
<!-- X-axis labels -->
<div class="flex justify-between mt-2 px-8 text-xs mono text-[var(--muted)]" id="dayLabels">
<span>Day 0</span>
<span>Day 1</span>
<span>Day 2</span>
<span>Day 3</span>
<span>Day 4</span>
<span>Day 5</span>
<span>Day 6</span>
</div>
</div>
<!-- Model Parameters -->
<div class="glass-card p-6 animate-in stagger-4">
<h2 class="text-lg font-medium mb-4">Model Physics</h2>
<div class="grid sm:grid-cols-3 gap-4 text-sm">
<div class="p-3 rounded-lg bg-black/20">
<div class="text-[var(--muted)] mb-1">Sensitivity to Air Temp</div>
<div id="sensitivity" class="mono font-medium">0.85</div>
</div>
<div class="p-3 rounded-lg bg-black/20">
<div class="text-[var(--muted)] mb-1">Thermal Response Time</div>
<div id="responseTime" class="mono font-medium">4.2 hrs</div>
</div>
<div class="p-3 rounded-lg bg-black/20">
<div class="text-[var(--muted)] mb-1">Advection Coefficient</div>
<div id="advection" class="mono font-medium">0.12</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
// Initialize state
const state = {
airTemp: 20,
solar: 500,
wind: 3,
humidity: 60,
cloud: 30,
discharge: 50,
upstream: 15,
depth: 2.0,
forecast: [],
animFrameId: null
};
// DOM Elements
const elements = {
airTemp: document.getElementById('airTemp'),
solar: document.getElementById('solar'),
wind: document.getElementById('wind'),
humidity: document.getElementById('humidity'),
cloud: document.getElementById('cloud'),
discharge: document.getElementById('discharge'),
upstream: document.getElementById('upstream'),
depth: document.getElementById('depth'),
runModel: document.getElementById('runModel'),
mainTemp: document.getElementById('mainTemp'),
tempGauge: document.getElementById('tempGauge'),
thermalClass: document.getElementById('thermalClass'),
confidence: document.getElementById('confidence'),
uncertainty: document.getElementById('uncertainty'),
flowSpeed: document.getElementById('flowSpeed'),
solarFlux: document.getElementById('solarFlux'),
longwaveFlux: document.getElementById('longwaveFlux'),
evapFlux: document.getElementById('evapFlux'),
convFlux: document.getElementById('convFlux'),
netFlux: document.getElementById('netFlux'),
solarBar: document.getElementById('solarBar'),
longwaveBar: document.getElementById('longwaveBar'),
evapBar: document.getElementById('evapBar'),
convBar: document.getElementById('convBar'),
tempLine: document.getElementById('tempLine'),
confidenceBand: document.getElementById('confidenceBand'),
dataPoints: document.getElementById('dataPoints'),
waterViz: document.getElementById('waterViz'),
flowParticles: document.getElementById('flowParticles'),
sensitivity: document.getElementById('sensitivity'),
responseTime: document.getElementById('responseTime'),
advection: document.getElementById('advection')
};
// Value display elements
const valueDisplays = {
airTemp: document.getElementById('airTempValue'),
solar: document.getElementById('solarValue'),
wind: document.getElementById('windValue'),
humidity: document.getElementById('humidityValue'),
cloud: document.getElementById('cloudValue'),
discharge: document.getElementById('dischargeValue'),
upstream: document.getElementById('upstreamValue'),
depth: document.getElementById('depthValue')
};
// Physics constants
const CONSTANTS = {
WATER_DENSITY: 1000,
WATER_SPECIFIC_HEAT: 4186,
STEFAN_BOLTZMANN: 5.67e-8,
LATENT_HEAT_VAP: 2.45e6,
PSYCHROMETRIC: 0.066,
ALBEDO_WATER: 0.06,
EMISSIVITY_WATER: 0.97
};
// Heat flux calculations
function calcSaturationVaporPressure(temp) {
return 0.6108 * Math.exp((17.27 * temp) / (temp + 237.3));
}
function calcSolarAbsorption(solar, cloud, albedo = CONSTANTS.ALBEDO_WATER) {
const cloudFactor = 1 - 0.65 * Math.pow(cloud / 100, 2);
return solar * cloudFactor * (1 - albedo);
}
function calcLongwaveRadiation(waterTemp, airTemp, humidity, cloud) {
const e_a = calcSaturationVaporPressure(airTemp) * (humidity / 100);
const clearSkyEmissivity = 0.642 * Math.pow(e_a / (airTemp + 273.15), 0.143);
const cloudFactor = 1 + 0.17 * Math.pow(cloud / 100, 2);
const atmospheric = CONSTANTS.STEFAN_BOLTZMANN * clearSkyEmissivity * cloudFactor * Math.pow(airTemp + 273.15, 4);
const water = CONSTANTS.STEFAN_BOLTZMANN * CONSTANTS.EMISSIVITY_WATER * Math.pow(waterTemp + 273.15, 4);
return atmospheric - water;
}
function calcEvaporativeHeat(waterTemp, airTemp, humidity, wind) {
const e_s = calcSaturationVaporPressure(waterTemp);
const e_a = calcSaturationVaporPressure(airTemp) * (humidity / 100);
const windFunc = 6.9 + 0.345 * wind;
return -CONSTANTS.LATENT_HEAT_VAP * windFunc * (e_s - e_a) / 1000;
}
function calcConvectiveHeat(waterTemp, airTemp, humidity, wind) {
const windFunc = 6.9 + 0.345 * wind;
return CONSTANTS.PSYCHROMETRIC * windFunc * (waterTemp - airTemp);
}
function calcAdvectionTemp(upstreamTemp, discharge, depth, currentTemp) {
const velocity = discharge / (depth * 50);
const residenceFactor = Math.exp(-0.05 / Math.max(0.1, velocity));
return upstreamTemp * (1 - residenceFactor) + currentTemp * residenceFactor;
}
function predictTemperature(inputs, days = 7) {
const predictions = [];
let currentTemp = inputs.upstream;
for (let day = 0; day <= days; day++) {
const solarFlux = calcSolarAbsorption(inputs.solar, inputs.cloud);
const longwaveFlux = calcLongwaveRadiation(currentTemp, inputs.airTemp, inputs.humidity, inputs.cloud);
const evapFlux = calcEvaporativeHeat(currentTemp, inputs.airTemp, inputs.humidity, inputs.wind);
const convFlux = calcConvectiveHeat(currentTemp, inputs.airTemp, inputs.humidity, inputs.wind);
const netFlux = solarFlux + longwaveFlux + evapFlux + convFlux;
const volumePerArea = inputs.depth * 1000;
const tempChange = (netFlux * 86400) / (CONSTANTS.WATER_DENSITY * CONSTANTS.WATER_SPECIFIC_HEAT * volumePerArea);
const advectionEffect = calcAdvectionTemp(inputs.upstream, inputs.discharge, inputs.depth, currentTemp);
currentTemp = currentTemp * 0.7 + advectionEffect * 0.3 + tempChange * 0.5;
currentTemp += (inputs.airTemp - currentTemp) * 0.1;
currentTemp = Math.max(-2, Math.min(35, currentTemp));
const uncertaintyBase = 0.8 + day * 0.15;
const uncertaintyFactor = uncertaintyBase * (1 + Math.abs(inputs.airTemp - currentTemp) / 50);
predictions.push({
day,
temp: currentTemp,
lower: currentTemp - uncertaintyFactor,
upper: currentTemp + uncertaintyFactor,
fluxes: { solar: solarFlux, longwave: longwaveFlux, evap: evapFlux, conv: convFlux, net: netFlux }
});
}
return predictions;
}
function getThermalClass(temp) {
if (temp < 10) return { label: 'Cold', color: 'bg-blue-500/20 text-blue-400' };
if (temp < 18) return { label: 'Cool', color: 'bg-cyan-500/20 text-cyan-400' };
if (temp < 24) return { label: 'Warm', color: 'bg-yellow-500/20 text-yellow-400' };
return { label: 'Hot', color: 'bg-red-500/20 text-red-400' };
}
function getTempColor(temp) {
if (temp < 10) return '#0088cc';
if (temp < 18) return '#00aaaa';
if (temp < 24) return '#cc8800';
return '#cc4400';
}
function updateUI() {
const predictions = predictTemperature(state);
state.forecast = predictions;
const current = predictions[0];
const temp = current.temp;
const fluxes = current.fluxes;
// Update main temperature
elements.mainTemp.textContent = temp.toFixed(1);
// Update gauge color
const tempColor = getTempColor(temp);
elements.tempGauge.style.borderColor = tempColor;
// Update thermal class
const thermal = getThermalClass(temp);
elements.thermalClass.textContent = thermal.label;
elements.thermalClass.className = `mono text-sm px-3 py-1 rounded-full ${thermal.color}`;
// Update confidence
const confInterval = (predictions[6].upper - predictions[6].lower) / 2;
elements.confidence.textContent = `±${confInterval.toFixed(1)}°C`;
// Update uncertainty
const uncertainty = ((predictions[6].upper - predictions[6].lower) / temp * 100);
elements.uncertainty.textContent = `${Math.min(99, uncertainty).toFixed(1)}%`;
// Update flow speed
const flowSpeed = state.discharge / (state.depth * 50);
elements.flowSpeed.textContent = `${flowSpeed.toFixed(1)} m/s`;
// Update heat fluxes
elements.solarFlux.textContent = `+${Math.round(fluxes.solar)}`;
elements.longwaveFlux.textContent = fluxes.longwave > 0 ? `+${Math.round(fluxes.longwave)}` : Math.round(fluxes.longwave);
elements.evapFlux.textContent = Math.round(fluxes.evap);
elements.convFlux.textContent = fluxes.conv > 0 ? `+${Math.round(fluxes.conv)}` : Math.round(fluxes.conv);
elements.netFlux.textContent = `${fluxes.net > 0 ? '+' : ''}${Math.round(fluxes.net)} W/m²`;
// Update bars
const maxFlux = Math.max(
Math.abs(fluxes.solar),
Math.abs(fluxes.longwave),
Math.abs(fluxes.evap),
Math.abs(fluxes.conv),
200
);
elements.solarBar.style.width = `${Math.min(100, (Math.abs(fluxes.solar) / maxFlux) * 100)}%`;
elements.longwaveBar.style.width = `${Math.min(100, (Math.abs(fluxes.longwave) / maxFlux) * 100)}%`;
elements.evapBar.style.width = `${Math.min(100, (Math.abs(fluxes.evap) / maxFlux) * 100)}%`;
elements.convBar.style.width = `${Math.min(100, (Math.abs(fluxes.conv) / maxFlux) * 100)}%`;
// Update water visualization color
const coldColor = { r: 0, g: 136, b: 204 };
const warmColor = { r: 204, g: 68, b: 0 };
const t = Math.max(0, Math.min(1, (temp + 5) / 35));
const r = Math.round(coldColor.r + (warmColor.r - coldColor.r) * t);
const g = Math.round(coldColor.g + (warmColor.g - coldColor.g) * t);
const b = Math.round(coldColor.b + (warmColor.b - coldColor.b) * t);
elements.waterViz.style.background = `linear-gradient(180deg, rgba(${r}, ${g}, ${b}, 0.3) 0%, rgba(${r}, ${g}, ${b}, 0.5) 100%)`;
// Update chart
updateChart(predictions);
// Update model parameters
const sensitivity = 0.7 + Math.abs(state.airTemp - temp) / 100;
elements.sensitivity.textContent = sensitivity.toFixed(2);
const responseTime = (state.depth * 2) / (1 + state.wind / 10);
elements.responseTime.textContent = `${responseTime.toFixed(1)} hrs`;
const advectionCoeff = state.discharge / (state.depth * 500);
elements.advection.textContent = advectionCoeff.toFixed(2);
}
function updateChart(predictions) {
const container = document.getElementById('chartContainer');
const width = container.offsetWidth;
const height = container.offsetHeight;
const padding = { left: 40, right: 20, top: 20, bottom: 20 };
const chartWidth = width - padding.left - padding.right;
const chartHeight = height - padding.top - padding.bottom;
const tempRange = { min: -2, max: 30 };
function tempToY(temp) {
return padding.top + chartHeight - ((temp - tempRange.min) / (tempRange.max - tempRange.min)) * chartHeight;
}
function dayToX(day) {
return padding.left + (day / 6) * chartWidth;
}
// Generate path
const linePath = predictions.map((p, i) => {
const x = dayToX(p.day);
const y = tempToY(p.temp);
return `${i === 0 ? 'M' : 'L'} ${x} ${y}`;
}).join(' ');
elements.tempLine.setAttribute('d', linePath);
// Confidence band
const bandPath = [
...predictions.map((p, i) => `${i === 0 ? 'M' : 'L'} ${dayToX(p.day)} ${tempToY(p.upper)}`),
...predictions.slice().reverse().map((p, i) => `L ${dayToX(p.day)} ${tempToY(p.lower)}`),
'Z'
].join(' ');
elements.confidenceBand.setAttribute('d', bandPath);
// Data points
elements.dataPoints.innerHTML = predictions.map((p, i) => `
<circle
cx="${dayToX(p.day)}"
cy="${tempToY(p.temp)}"
r="4"
fill="var(--bg)"
stroke="var(--accent)"
stroke-width="2"
class="temp-indicator"
/>
`).join('');
}
function createFlowParticles() {
elements.flowParticles.innerHTML = '';
const particleCount = 15;
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement('div');
particle.className = 'flow-particle';
particle.style.top = `${20 + Math.random() * 80}px`;
particle.style.animationDuration = `${3 + Math.random() * 4}s`;
particle.style.animationDelay = `${Math.random() * 5}s`;
elements.flowParticles.appendChild(particle);
}
}
function setupEventListeners() {
const inputs = ['airTemp', 'solar', 'wind', 'humidity', 'cloud', 'discharge', 'upstream', 'depth'];
inputs.forEach(name => {
elements[name].addEventListener('input', (e) => {
const value = parseFloat(e.target.value);
state[name] = value;
// Update display
const units = {
airTemp: '°C',
solar: ' W/m²',
wind: ' m/s',
humidity: '%',
cloud: '%',
discharge: ' m³/s',
upstream: '°C',
depth: ' m'
};
valueDisplays[name].textContent = `${value}${units[name]}`;
});
});
elements.runModel.addEventListener('click', () => {
elements.runModel.classList.add('scale-95');
setTimeout(() => {
elements.runModel.classList.remove('scale-95');
updateUI();
}, 150);
});
// Live updates on input
inputs.forEach(name => {
elements[name].addEventListener('input', updateUI);
});
// Handle resize
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
if (state.forecast.length > 0) {
updateChart(state.forecast);
}
}, 100);
});
}
// Initialize
function init() {
setupEventListeners();
createFlowParticles();
updateUI();
}
// Start when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
</script>
</body>
</html>
Physics-Based Model:
Interactive Controls:
Visualizations:
Technical Implementation: