// Reverse osmosis lab: UI + a 3D cut-away of a membrane channel driven by model.js. import * as THREE from '../../vendor/three/three.module.min.js'; import { roModel, PRESETS } from './model.js'; const $ = id => document.getElementById(id); const inputs = { pressure: $('pressure'), salinity: $('salinity'), recovery: $('recovery'), membrane: $('membrane'), erd: $('erd') }; const fmt = (n, d = 0) => n.toLocaleString('en-US', { minimumFractionDigits: d, maximumFractionDigits: d }); let result = null; function read() { return { pressure: +inputs.pressure.value, salinity: +inputs.salinity.value, recovery: +inputs.recovery.value, membrane: inputs.membrane.value, erd: inputs.erd.checked }; } function update() { const s = read(); result = roModel(s); $('pressure-out').textContent = fmt(s.pressure, 1) + ' bar'; $('salinity-out').textContent = fmt(s.salinity) + ' mg/L'; $('recovery-out').textContent = fmt(s.recovery) + ' %'; $('r-flux').textContent = fmt(result.flux, 1); $('r-perm').textContent = result.running ? fmt(result.permeate) : '–'; $('r-rej').textContent = result.running ? fmt(result.rejection * 100, 1) : '–'; $('r-energy').textContent = result.running ? fmt(result.energy, 2) : '–'; $('r-ndp').textContent = fmt(result.ndp, 1); $('r-conc').textContent = fmt(result.concentrate); $('meter-p').style.width = Math.min(100, s.pressure / 80 * 100) + '%'; $('meter-osm').style.width = Math.min(100, result.osmoticWall / 80 * 100) + '%'; const status = $('status'); status.textContent = result.notes.join(' '); status.classList.toggle('is-stop', !result.running); } function applyPreset(key) { const p = PRESETS[key]; inputs.pressure.value = p.pressure; inputs.salinity.value = p.salinity; inputs.recovery.value = p.recovery; inputs.membrane.value = p.membrane; inputs.erd.checked = p.erd; document.querySelectorAll('[data-preset]').forEach(b => b.setAttribute('aria-pressed', String(b.dataset.preset === key))); update(); } document.querySelectorAll('[data-preset]').forEach(b => b.addEventListener('click', () => applyPreset(b.dataset.preset))); Object.values(inputs).forEach(el => el.addEventListener('input', () => { document.querySelectorAll('[data-preset]').forEach(b => b.setAttribute('aria-pressed', 'false')); update(); })); applyPreset('brackish'); // ---------- 3D channel ---------- function scene3d() { const host = $('stage'); const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75)); host.appendChild(renderer.domElement); const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 100); const rig = new THREE.Group(); scene.add(rig); scene.add(new THREE.HemisphereLight(0xcfe6ff, 0x0a1430, 1.5)); const sun = new THREE.DirectionalLight(0xffffff, 1.6); sun.position.set(3, 6, 5); scene.add(sun); const LX = 4, LZ = 1.2, TOP = 1.7, BOT = -1.5; // Membrane sheet with a pore-like grid const c = document.createElement('canvas'); c.width = 256; c.height = 128; const g = c.getContext('2d'); g.fillStyle = '#b98a2c'; g.fillRect(0, 0, 256, 128); g.fillStyle = 'rgba(40,24,0,.55)'; for (let x = 4; x < 256; x += 8) for (let y = 4; y < 128; y += 8) { g.beginPath(); g.arc(x, y, 1.6, 0, 7); g.fill(); } const tex = new THREE.CanvasTexture(c); tex.colorSpace = THREE.SRGBColorSpace; tex.wrapS = tex.wrapT = THREE.RepeatWrapping; tex.repeat.set(3, 2); const membrane = new THREE.Mesh(new THREE.BoxGeometry(LX * 2, 0.12, LZ * 2), new THREE.MeshStandardMaterial({ map: tex, transparent: true, opacity: 0.86, roughness: 0.6, emissive: 0x3a2600, emissiveIntensity: 0.6 })); rig.add(membrane); // Channel outline const outline = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.BoxGeometry(LX * 2, TOP - BOT, LZ * 2)), new THREE.LineBasicMaterial({ color: 0x38d6ff, transparent: true, opacity: 0.35 })); outline.position.y = (TOP + BOT) / 2; rig.add(outline); const feedTint = new THREE.Mesh(new THREE.BoxGeometry(LX * 2, TOP, LZ * 2), new THREE.MeshBasicMaterial({ color: 0xff9d5c, transparent: true, opacity: 0.045, depthWrite: false })); feedTint.position.y = TOP / 2; rig.add(feedTint); const permTint = new THREE.Mesh(new THREE.BoxGeometry(LX * 2, -BOT, LZ * 2), new THREE.MeshBasicMaterial({ color: 0x38d6ff, transparent: true, opacity: 0.06, depthWrite: false })); permTint.position.y = BOT / 2; rig.add(permTint); const NW = 340, NS = 150; const water = new THREE.InstancedMesh(new THREE.SphereGeometry(0.055, 10, 8), new THREE.MeshStandardMaterial({ color: 0x58c8ff, emissive: 0x0b3a66, roughness: 0.35 }), NW); const salt = new THREE.InstancedMesh(new THREE.SphereGeometry(0.1, 12, 10), new THREE.MeshStandardMaterial({ color: 0xff9d5c, emissive: 0x5a2400, roughness: 0.5 }), NS); rig.add(water, salt); const rnd = (a, b) => a + Math.random() * (b - a); const mk = n => Array.from({ length: n }, () => ({ x: rnd(-LX, LX), y: rnd(0.15, TOP - 0.1), z: rnd(-LZ + 0.1, LZ - 0.1), vy: 0, side: 0, sp: rnd(0.7, 1.3) })); const W = mk(NW), S = mk(NS); const mat = new THREE.Matrix4(); const hide = new THREE.Matrix4().makeScale(0, 0, 0); let yaw = -0.5, pitch = 0.32, dragging = null, idle = 0; host.addEventListener('pointerdown', e => { dragging = { x: e.clientX, y: e.clientY, yaw, pitch }; host.setPointerCapture(e.pointerId); }); host.addEventListener('pointermove', e => { if (!dragging) return; yaw = dragging.yaw + (e.clientX - dragging.x) * 0.008; pitch = Math.max(-0.1, Math.min(0.9, dragging.pitch + (e.clientY - dragging.y) * 0.005)); idle = 0; }); const end = () => { dragging = null; }; host.addEventListener('pointerup', end); host.addEventListener('pointercancel', end); function resize() { const w = host.clientWidth || 300, h = host.clientHeight || 300; renderer.setSize(w, h, false); camera.aspect = w / h; const dist = 11.5 * Math.max(1, 1.25 / camera.aspect); camera.position.set(0, 0.6, dist); camera.lookAt(0, 0.1, 0); camera.updateProjectionMatrix(); } new ResizeObserver(resize).observe(host); resize(); const still = matchMedia('(prefers-reduced-motion: reduce)').matches; let last = performance.now(); function frame(now) { requestAnimationFrame(frame); const dt = Math.min(0.05, (now - last) / 1000) * (still ? 0.35 : 1); last = now; idle += dt; const r = result, s = read(); const cross = r.running ? Math.min(0.95, r.flux / 32) : 0; // chance a water molecule crosses per wall hit const push = 0.25 + Math.min(1.6, s.pressure / 30); // pressure drives molecules to the wall const pass = r.running ? Math.min(0.2, (1 - r.rejection) * 1.5) : 0; const flow = 1.15; for (let i = 0; i < NW; i++) { const p = W[i]; if (p.side === 0) { p.x += flow * p.sp * dt; p.vy += (-push * 0.9 + rnd(-2.2, 2.2)) * dt; p.vy *= 0.96; p.y += p.vy * dt; if (p.y > TOP - 0.08) { p.y = TOP - 0.08; p.vy = -Math.abs(p.vy); } if (p.y < 0.13) { if (Math.random() < cross) { p.side = 1; p.y = -0.13; p.vy = -0.5; } else { p.y = 0.13; p.vy = Math.abs(p.vy) * 0.6 + 0.25; } } if (p.x > LX) { p.x = -LX; p.y = rnd(0.2, TOP - 0.1); p.vy = 0; } } else { p.x += flow * 0.55 * dt; p.vy += (-0.25 + rnd(-1, 1)) * dt; p.vy *= 0.95; p.y += p.vy * dt; if (p.y < BOT + 0.08) { p.y = BOT + 0.08; p.vy = Math.abs(p.vy) * 0.3; } if (p.y > -0.13) { p.y = -0.13; p.vy = -0.2; } if (p.x > LX) { p.side = 0; p.x = -LX; p.y = rnd(0.2, TOP - 0.1); p.vy = 0; } } mat.makeTranslation(p.x, p.y, p.z); water.setMatrixAt(i, mat); } const shown = Math.round(10 + Math.sqrt(s.salinity / 45000) * (NS - 10)); for (let i = 0; i < NS; i++) { const p = S[i]; if (i >= shown) { salt.setMatrixAt(i, hide); continue; } if (p.side === 0) { p.x += flow * p.sp * 0.9 * dt; p.vy += (-push * 0.8 + rnd(-1.8, 1.8)) * dt; p.vy *= 0.96; p.y += p.vy * dt; if (p.y > TOP - 0.12) { p.y = TOP - 0.12; p.vy = -Math.abs(p.vy); } if (p.y < 0.17) { if (Math.random() < pass * dt * 6) { p.side = 1; p.y = -0.17; p.vy = -0.4; } else { p.y = 0.17; p.vy = Math.abs(p.vy) * 0.25 + 0.05; } } if (p.x > LX) { p.x = -LX; p.y = rnd(0.25, TOP - 0.15); p.vy = 0; } } else { p.x += flow * 0.55 * dt; p.vy += (-0.2 + rnd(-0.8, 0.8)) * dt; p.vy *= 0.95; p.y += p.vy * dt; if (p.y < BOT + 0.12) { p.y = BOT + 0.12; p.vy = 0.1; } if (p.y > -0.17) { p.y = -0.17; p.vy = -0.2; } if (p.x > LX) { p.side = 0; p.x = -LX; p.y = rnd(0.25, TOP - 0.15); p.vy = 0; } } mat.makeTranslation(p.x, p.y, p.z); salt.setMatrixAt(i, mat); } water.instanceMatrix.needsUpdate = true; salt.instanceMatrix.needsUpdate = true; membrane.material.emissiveIntensity = r.running ? 0.6 + Math.min(0.8, r.flux / 40) : 0.25; rig.rotation.set(pitch, yaw + (dragging || still ? 0 : Math.sin(idle * 0.25) * 0.12), 0); renderer.render(scene, camera); } requestAnimationFrame(frame); } try { scene3d(); } catch (err) { console.error('3D view unavailable; the calculator still works.', err); $('stage').classList.add('is-off'); }