| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| ╔══════════════════════════════════════════════════════════════════════════════╗ | |
| ║ WATER-BASED COMPUTER SIMULATION (Ω Hydro-Compute Engine) ║ | |
| ║ คอมพิวเตอร์พลังงานน้ำ — ใช้ความร้อนขับเคลื่อนคณิตศาสตร์ ║ | |
| ║ ║ | |
| ║ หลักการ: ║ | |
| ║ - พลังงานความร้อน (Watts) → กระตุ้นการไหลของน้ำ ║ | |
| ║ - การไหลของน้ำ → สร้างกิริยาคณิตศาสตร์ (bit flips, logic gates) ║ | |
| ║ - สมดุลความร้อน-ความเย็น → ควบคุมประสิทธิภาพ ║ | |
| ║ - น้ำ 1 ลิตร = 2,260,000 จูล (Latent Heat of Vaporization) ║ | |
| ║ ║ | |
| ║ Version: 1.0.0 ║ | |
| ║ Author: Chaiyphop Nilpat (The Architect) ║ | |
| ║ License: Architect's Final Constant License ║ | |
| ╚══════════════════════════════════════════════════════════════════════════════╝ | |
| """ | |
| import numpy as np | |
| import math | |
| import json | |
| import time | |
| from dataclasses import dataclass, field | |
| from typing import List, Dict, Tuple, Optional | |
| from datetime import datetime | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| # PHYSICAL CONSTANTS | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| class WaterComputeConstants: | |
| """ค่าคงที่ทางฟิสิกส์สำหรับคอมพิวเตอร์น้ำ""" | |
| # น้ำ | |
| LATENT_HEAT_VAPORIZATION = 2_260_000 # J/kg (พลังงานแฝงการระเหย) | |
| SPECIFIC_HEAT_WATER = 4186 # J/(kg·K) | |
| WATER_DENSITY = 1000 # kg/m³ | |
| WATER_MASS_PER_LITER = 1.0 # kg | |
| # ความร้อน | |
| BOLTZMANN = 1.380649e-23 # J/K | |
| ROOM_TEMP = 25.0 # °C | |
| OPTIMAL_TEMP = 50.0 # °C (อุณหภูมิที่เหมาะสมที่สุด) | |
| # การระบายความร้อน (Newton's Law) | |
| HEAT_TRANSFER_COEFF = 10.0 # W/(m²·K) | |
| SURFACE_AREA = 0.5 # m² | |
| THERMAL_CAPACITY = 500.0 # J/K | |
| # พลังงาน | |
| BASE_POWER = 0.01 # Watts (พลังงานเริ่มต้น) | |
| POWER_INCREMENT = 1e-8 # Watts ต่อรอบ | |
| MAX_POWER = 100.0 # Watts | |
| # ประสิทธิภาพ | |
| ALPHA_TEMP = 0.01 # ค่าคงที่ความไวต่ออุณหภูมิ | |
| EFFICIENCY_HEAT = 0.85 # η = 85% แปลงเป็นความร้อน | |
| # การไหล | |
| FLOW_COEFFICIENT = 10.0 # สัมประสิทธิ์การไหล | |
| FLOW_RESISTANCE = 0.5 # ความต้านทานการไหล | |
| # คณิตศาสตร์ | |
| BITS_PER_FLOW_UNIT = 1000 # บิตต่อการไหล 1 หน่วย | |
| MATH_ACTION_COEFF = 1.0 # สัมประสิทธิ์กิริยาคณิตศาสตร์ | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| # CORE PHYSICS ENGINE | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| class ThermalBalanceEngine: | |
| """ | |
| สมการสมดุลความร้อน-ความเย็น: | |
| dT/dt = (Q_in - Q_out) / C_th | |
| Q_in = η × P | |
| Q_out = h × A × (T - T_ambient) | |
| """ | |
| def __init__(self): | |
| self.temperature = WaterComputeConstants.ROOM_TEMP | |
| self.ambient_temp = WaterComputeConstants.ROOM_TEMP | |
| self.thermal_capacity = WaterComputeConstants.THERMAL_CAPACITY | |
| self.heat_transfer = WaterComputeConstants.HEAT_TRANSFER_COEFF | |
| self.surface_area = WaterComputeConstants.SURFACE_AREA | |
| self.efficiency = WaterComputeConstants.EFFICIENCY_HEAT | |
| def step(self, power: float, dt: float = 1.0) -> Dict: | |
| """คำนวณอุณหภูมิในรอบถัดไป""" | |
| # พลังงานความร้อนที่เข้าสู่ระบบ | |
| Q_in = self.efficiency * power | |
| # พลังงานความร้อนที่ระบายออก (Newton's Law of Cooling) | |
| Q_out = self.heat_transfer * self.surface_area * (self.temperature - self.ambient_temp) | |
| # เปลี่ยนอุณหภูมิ | |
| dT = (Q_in - Q_out) / self.thermal_capacity * dt | |
| self.temperature += dT | |
| # ไม่ต่ำกว่าอุณหภูมิแวดล้อม | |
| self.temperature = max(self.temperature, self.ambient_temp) | |
| return { | |
| 'temperature': self.temperature, | |
| 'Q_in': Q_in, | |
| 'Q_out': Q_out, | |
| 'dT': dT | |
| } | |
| class WaterFlowEngine: | |
| """ | |
| โมเดลการไหลของน้ำ: | |
| Flow = coeff × P / (1 + resistance × T) | |
| น้ำไหลเร็วขึ้นเมื่อพลังงานเพิ่ม แต่ช้าลงเมื่อร้อนเกิน | |
| """ | |
| def __init__(self): | |
| self.coefficient = WaterComputeConstants.FLOW_COEFFICIENT | |
| self.resistance = WaterComputeConstants.FLOW_RESISTANCE | |
| self.flow_rate = 0.0 | |
| self.total_volume = 0.0 # ลิตร | |
| def step(self, power: float, temperature: float, dt: float = 1.0) -> Dict: | |
| """คำนวณการไหลของน้ำ""" | |
| # การไหลขึ้นกับพลังงานและอุณหภูมิ | |
| temp_factor = 1.0 / (1.0 + self.resistance * (temperature - WaterComputeConstants.ROOM_TEMP) / 100.0) | |
| self.flow_rate = self.coefficient * power * temp_factor | |
| # ปริมาตรน้ำที่ไหล | |
| volume = self.flow_rate * dt | |
| self.total_volume += volume | |
| return { | |
| 'flow_rate': self.flow_rate, | |
| 'total_volume': self.total_volume, | |
| 'temp_factor': temp_factor | |
| } | |
| class MathActionEngine: | |
| """ | |
| กิริยาคณิตศาสตร์จากน้ำไหล: | |
| M(t) = M₀ × e^(-α(T - T_opt)²) × flow × bits_per_unit | |
| น้ำที่ไหลผ่าน "ประตูตรรกะ" สร้าง bit flips = การคำนวณ | |
| """ | |
| def __init__(self): | |
| self.max_efficiency = 1.0 | |
| self.alpha = WaterComputeConstants.ALPHA_TEMP | |
| self.optimal_temp = WaterComputeConstants.OPTIMAL_TEMP | |
| self.bits_per_unit = WaterComputeConstants.BITS_PER_FLOW_UNIT | |
| self.total_operations = 0 | |
| self.total_bits_processed = 0 | |
| def efficiency(self, temperature: float) -> float: | |
| """ประสิทธิภาพคณิตศาสตร์ตามอุณหภูมิ (Gaussian)""" | |
| return self.max_efficiency * math.exp( | |
| -self.alpha * (temperature - self.optimal_temp) ** 2 | |
| ) | |
| def step(self, flow_rate: float, temperature: float, dt: float = 1.0) -> Dict: | |
| """คำนวณกิริยาคณิตศาสตร์ในรอบนี้""" | |
| eff = self.efficiency(temperature) | |
| # จำนวนการดำเนินการคณิตศาสตร์ | |
| operations = flow_rate * eff * WaterComputeConstants.MATH_ACTION_COEFF * dt | |
| # จำนวนบิตที่ประมวลผล | |
| bits = operations * self.bits_per_unit | |
| self.total_operations += operations | |
| self.total_bits_processed += bits | |
| return { | |
| 'efficiency': eff, | |
| 'operations': operations, | |
| 'bits_processed': bits, | |
| 'total_operations': self.total_operations, | |
| 'total_bits': self.total_bits_processed | |
| } | |
| class EnergyCodeEngine: | |
| """ | |
| ถอดรหัสพลังงานเป็นรหัสตัวเลข: | |
| - 1 จูล = 1 บิต (สมมุติฐาน) | |
| - Normalized code = Q / Q_max | |
| - สถานะน้ำ: < 0.226 = ของเหลว, >= 0.226 = ไอ | |
| """ | |
| def __init__(self): | |
| self.latent_heat = WaterComputeConstants.LATENT_HEAT_VAPORIZATION | |
| self.max_energy = 10_000_000 # J (ค่าสูงสุดสำหรับ normalize) | |
| self.vaporization_threshold = 0.226 # normalized | |
| def decode(self, energy_joules: float) -> Dict: | |
| """ถอดรหัสพลังงานเป็นค่ารหัส""" | |
| # Binary mapping: 1 J = 1 bit | |
| binary_bits = int(energy_joules) | |
| # Normalized code | |
| normalized = energy_joules / self.max_energy | |
| normalized = min(normalized, 1.0) | |
| # สถานะน้ำ | |
| state = "vapor" if normalized >= self.vaporization_threshold else "liquid" | |
| return { | |
| 'binary_bits': binary_bits, | |
| 'normalized_code': normalized, | |
| 'water_state': state, | |
| 'vaporization_equiv_kg': energy_joules / self.latent_heat | |
| } | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| # WATER COMPUTER SYSTEM | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| class SimulationSnapshot: | |
| """ข้อมูล snapshot ของแต่ละรอบ""" | |
| round: int | |
| power: float | |
| temperature: float | |
| flow_rate: float | |
| efficiency: float | |
| operations: float | |
| bits_processed: float | |
| total_ops: float | |
| total_bits: float | |
| Q_in: float | |
| Q_out: float | |
| water_state: str | |
| normalized_code: float | |
| binary_bits: int | |
| class WaterComputer: | |
| """ | |
| คอมพิวเตอร์พลังงานน้ำ — ระบบจำลองเต็มรูปแบบ | |
| Architecture: | |
| ┌─────────────────────────────────────────────┐ | |
| │ POWER INPUT (Watts) │ | |
| │ ↓ │ | |
| │ ┌─────────────────────────────────────┐ │ | |
| │ │ Thermal Balance Engine │ │ | |
| │ │ dT/dt = (ηP - hA(T-Tₐ)) / C_th │ │ | |
| │ └──────────────┬──────────────────────┘ │ | |
| │ ↓ Temperature │ | |
| │ ┌─────────────────────────────────────┐ │ | |
| │ │ Water Flow Engine │ │ | |
| │ │ Flow = coeff × P × temp_factor │ │ | |
| │ └──────────────┬──────────────────────┘ │ | |
| │ ↓ Flow Rate │ | |
| │ ┌─────────────────────────────────────┐ │ | |
| │ │ Math Action Engine │ │ | |
| │ │ M = M₀ × e^(-α(T-T_opt)²) × Flow │ │ | |
| │ └──────────────┬──────────────────────┘ │ | |
| │ ↓ Operations │ | |
| │ ┌─────────────────────────────────────┐ │ | |
| │ │ Energy Code Engine │ │ | |
| │ │ Decode: J → bits → state │ │ | |
| │ └─────────────────────────────────────┘ │ | |
| └─────────────────────────────────────────────┘ | |
| """ | |
| def __init__(self): | |
| self.thermal = ThermalBalanceEngine() | |
| self.water_flow = WaterFlowEngine() | |
| self.math_action = MathActionEngine() | |
| self.energy_code = EnergyCodeEngine() | |
| self.history: List[SimulationSnapshot] = [] | |
| self.current_round = 0 | |
| self.current_power = WaterComputeConstants.BASE_POWER | |
| # สถิติ | |
| self.peak_efficiency = 0.0 | |
| self.peak_ops_per_second = 0.0 | |
| self.rounds_at_optimal = 0 | |
| self.vaporization_events = 0 | |
| def run_round(self, dt: float = 1.0) -> SimulationSnapshot: | |
| """รัน 1 รอบการจำลอง""" | |
| self.current_round += 1 | |
| # 1) Thermal Balance → อุณหภูมิ | |
| thermal_result = self.thermal.step(self.current_power, dt) | |
| temp = thermal_result['temperature'] | |
| # 2) Water Flow → การไหล | |
| flow_result = self.water_flow.step(self.current_power, temp, dt) | |
| flow = flow_result['flow_rate'] | |
| # 3) Math Action → กิริยาคณิตศาสตร์ | |
| math_result = self.math_action.step(flow, temp, dt) | |
| eff = math_result['efficiency'] | |
| # 4) Energy Code → ถอดรหัส | |
| energy_joules = self.current_power * dt | |
| code_result = self.energy_code.decode(energy_joules) | |
| # สร้าง snapshot | |
| snapshot = SimulationSnapshot( | |
| round=self.current_round, | |
| power=self.current_power, | |
| temperature=temp, | |
| flow_rate=flow, | |
| efficiency=eff, | |
| operations=math_result['operations'], | |
| bits_processed=math_result['bits_processed'], | |
| total_ops=math_result['total_operations'], | |
| total_bits=math_result['total_bits'], | |
| Q_in=thermal_result['Q_in'], | |
| Q_out=thermal_result['Q_out'], | |
| water_state=code_result['water_state'], | |
| normalized_code=code_result['normalized_code'], | |
| binary_bits=code_result['binary_bits'] | |
| ) | |
| self.history.append(snapshot) | |
| # อัปเดตสถิติ | |
| if eff > self.peak_efficiency: | |
| self.peak_efficiency = eff | |
| if math_result['operations'] > self.peak_ops_per_second: | |
| self.peak_ops_per_second = math_result['operations'] | |
| # นับรอบที่อุณหภูมิเหมาะสม (±5°C จาก optimal) | |
| if abs(temp - WaterComputeConstants.OPTIMAL_TEMP) <= 5.0: | |
| self.rounds_at_optimal += 1 | |
| # นับเหตุการณ์ระเหย | |
| if code_result['water_state'] == 'vapor': | |
| self.vaporization_events += 1 | |
| return snapshot | |
| def run_simulation(self, rounds: int, power_increment: float = 1e-8, | |
| sample_every: int = 1000) -> Dict: | |
| """ | |
| รันจำลองหลายรอบ | |
| Args: | |
| rounds: จำนวนรอบทั้งหมด | |
| power_increment: พลังงานที่เพิ่มต่อรอบ (Watts) | |
| sample_every: บันทึกข้อมูลทุกกี่รอบ (เพื่อประหยัด memory) | |
| """ | |
| print(f"\n{'='*70}") | |
| print(f" Ω HYDRO-COMPUTE ENGINE — Water-Based Computer Simulation") | |
| print(f"{'='*70}") | |
| print(f" Rounds: {rounds:,}") | |
| print(f" Base Power: {WaterComputeConstants.BASE_POWER} W") | |
| print(f" Power Increment: {power_increment} W/round") | |
| print(f" Optimal Temp: {WaterComputeConstants.OPTIMAL_TEMP}°C") | |
| print(f" Latent Heat: {WaterComputeConstants.LATENT_HEAT_VAPORIZATION:,} J/kg") | |
| print(f"{'='*70}\n") | |
| start_time = time.time() | |
| sampled_data = [] | |
| for i in range(rounds): | |
| # รัน 1 รอบ | |
| snapshot = self.run_round() | |
| # เพิ่มพลังงานทีละน้อย | |
| self.current_power += power_increment | |
| self.current_power = min(self.current_power, WaterComputeConstants.MAX_POWER) | |
| # บันทึกตัวอย่าง | |
| if i % sample_every == 0 or i == rounds - 1: | |
| sampled_data.append({ | |
| 'round': snapshot.round, | |
| 'power': snapshot.power, | |
| 'temperature': snapshot.temperature, | |
| 'flow_rate': snapshot.flow_rate, | |
| 'efficiency': snapshot.efficiency, | |
| 'operations': snapshot.operations, | |
| 'total_ops': snapshot.total_ops, | |
| 'total_bits': snapshot.total_bits, | |
| 'water_state': snapshot.water_state, | |
| 'normalized_code': snapshot.normalized_code | |
| }) | |
| # แสดงความคืบหน้า | |
| if (i + 1) % (rounds // 10) == 0: | |
| progress = (i + 1) / rounds * 100 | |
| elapsed = time.time() - start_time | |
| print(f" Progress: {progress:.0f}% | Round {i+1:,}/{rounds:,} | " | |
| f"Temp: {snapshot.temperature:.1f}°C | " | |
| f"Eff: {snapshot.efficiency:.4f} | " | |
| f"Elapsed: {elapsed:.1f}s") | |
| elapsed = time.time() - start_time | |
| # สรุปผล | |
| results = self._generate_summary(sampled_data, elapsed, rounds) | |
| return results | |
| def _generate_summary(self, sampled_data: List[Dict], elapsed: float, | |
| total_rounds: int) -> Dict: | |
| """สร้างสรุปผลการจำลอง""" | |
| final = self.history[-1] if self.history else None | |
| summary = { | |
| 'simulation_info': { | |
| 'total_rounds': total_rounds, | |
| 'elapsed_seconds': elapsed, | |
| 'rounds_per_second': total_rounds / elapsed if elapsed > 0 else 0, | |
| 'timestamp': datetime.now().isoformat() | |
| }, | |
| 'final_state': { | |
| 'round': final.round if final else 0, | |
| 'power': final.power if final else 0, | |
| 'temperature': final.temperature if final else 0, | |
| 'efficiency': final.efficiency if final else 0, | |
| 'flow_rate': final.flow_rate if final else 0, | |
| 'total_operations': final.total_ops if final else 0, | |
| 'total_bits': final.total_bits if final else 0, | |
| 'water_state': final.water_state if final else 'unknown' | |
| }, | |
| 'statistics': { | |
| 'peak_efficiency': self.peak_efficiency, | |
| 'peak_ops_per_round': self.peak_ops_per_second, | |
| 'rounds_at_optimal_temp': self.rounds_at_optimal, | |
| 'vaporization_events': self.vaporization_events, | |
| 'optimal_temp_percentage': self.rounds_at_optimal / total_rounds * 100 if total_rounds > 0 else 0 | |
| }, | |
| 'physics_summary': { | |
| 'latent_heat_vaporization': f"{WaterComputeConstants.LATENT_HEAT_VAPORIZATION:,} J/kg", | |
| 'water_equiv_kg': final.total_ops / WaterComputeConstants.LATENT_HEAT_VAPORIZATION if final else 0, | |
| 'energy_to_vaporize_1kg': f"ต้องใช้ {WaterComputeConstants.LATENT_HEAT_VAPORIZATION:,} J = " | |
| f"{WaterComputeConstants.LATENT_HEAT_VAPORIZATION/3600:.1f} Wh", | |
| 'bottles_for_100W_1hr': f"100W × 1hr = 360,000 J = " | |
| f"{360000/WaterComputeConstants.LATENT_HEAT_VAPORIZATION:.3f} ขวดน้ำ" | |
| }, | |
| 'sampled_data': sampled_data | |
| } | |
| return summary | |
| def print_report(self, summary: Dict): | |
| """พิมพ์รายงานสรุป""" | |
| print(f"\n{'='*70}") | |
| print(f" 📊 SIMULATION REPORT") | |
| print(f"{'='*70}") | |
| info = summary['simulation_info'] | |
| print(f"\n ⏱️ Performance:") | |
| print(f" Rounds: {info['total_rounds']:,}") | |
| print(f" Time: {info['elapsed_seconds']:.2f}s") | |
| print(f" Speed: {info['rounds_per_second']:,.0f} rounds/sec") | |
| final = summary['final_state'] | |
| print(f"\n 🔚 Final State (Round {final['round']:,}):") | |
| print(f" Power: {final['power']:.6f} W") | |
| print(f" Temperature: {final['temperature']:.2f}°C") | |
| print(f" Efficiency: {final['efficiency']:.6f}") | |
| print(f" Flow Rate: {final['flow_rate']:.6f} units/s") | |
| print(f" Total Operations: {final['total_operations']:,.0f}") | |
| print(f" Total Bits: {final['total_bits']:,.0f}") | |
| print(f" Water State: {final['water_state']}") | |
| stats = summary['statistics'] | |
| print(f"\n 📈 Statistics:") | |
| print(f" Peak Efficiency: {stats['peak_efficiency']:.6f}") | |
| print(f" Peak Ops/Round: {stats['peak_ops_per_round']:,.2f}") | |
| print(f" Rounds at Optimal Temp: {stats['rounds_at_optimal_temp']:,} " | |
| f"({stats['optimal_temp_percentage']:.1f}%)") | |
| print(f" Vaporization Events: {stats['vaporization_events']:,}") | |
| physics = summary['physics_summary'] | |
| print(f"\n 💧 Water Physics:") | |
| print(f" Latent Heat: {physics['latent_heat_vaporization']}") | |
| print(f" Water Equivalent: {physics['water_equiv_kg']:.6f} kg") | |
| print(f" Energy to Vaporize 1kg: {physics['energy_to_vaporize_1kg']}") | |
| print(f" 100W × 1hr = {physics['bottles_for_100W_1hr']}") | |
| print(f"\n{'='*70}") | |
| print(f" Ω HYDRO-COMPUTE SIMULATION COMPLETE") | |
| print(f"{'='*70}\n") | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| # WATER LOGIC GATE SIMULATOR | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| class WaterLogicGate: | |
| """ | |
| ประตูตรรกะที่ทำงานด้วยน้ำ | |
| AND Gate: น้ำต้องไหลเข้าทั้งสองช่อง → ออก | |
| OR Gate: น้ำไหลเข้าช่องใดช่องหนึ่ง → ออก | |
| NOT Gate: น้ำเข้า → ปิดทางออก (valve) | |
| """ | |
| def AND_gate(flow_a: float, flow_b: float, threshold: float = 0.1) -> float: | |
| """AND: ทั้งสองช่องต้องมีน้ำไหลเกิน threshold""" | |
| if flow_a > threshold and flow_b > threshold: | |
| return min(flow_a, flow_b) | |
| return 0.0 | |
| def OR_gate(flow_a: float, flow_b: float, threshold: float = 0.1) -> float: | |
| """OR: ช่องใดช่องหนึ่งมีน้ำไหล""" | |
| return max(flow_a, flow_b) if (flow_a > threshold or flow_b > threshold) else 0.0 | |
| def NOT_gate(flow_in: float, threshold: float = 0.1) -> float: | |
| """NOT: มีน้ำเข้า → ปิด (output = 0), ไม่มีน้ำเข้า → เปิด (output = 1)""" | |
| return 1.0 if flow_in <= threshold else 0.0 | |
| def XOR_gate(flow_a: float, flow_b: float, threshold: float = 0.1) -> float: | |
| """XOR: น้ำไหลเข้าเพียงช่องเดียว""" | |
| a_on = flow_a > threshold | |
| b_on = flow_b > threshold | |
| if a_on != b_on: | |
| return max(flow_a, flow_b) | |
| return 0.0 | |
| class WaterCircuit: | |
| """ | |
| วงจรน้ำ — ประกอบประตูตรรกะเป็นวงจรคำนวณ | |
| ตัวอย่าง: Half Adder = XOR (sum) + AND (carry) | |
| """ | |
| def __init__(self): | |
| self.gate = WaterLogicGate() | |
| self.operations_count = 0 | |
| def half_adder(self, flow_a: float, flow_b: float) -> Dict: | |
| """Half Adder: บวกบิต 2 ตัวด้วยน้ำ""" | |
| sum_flow = self.gate.XOR_gate(flow_a, flow_b) | |
| carry_flow = self.gate.AND_gate(flow_a, flow_b) | |
| self.operations_count += 2 | |
| return {'sum': sum_flow, 'carry': carry_flow} | |
| def full_adder(self, flow_a: float, flow_b: float, flow_cin: float) -> Dict: | |
| """Full Adder: บวกบิต 3 ตัวด้วยน้ำ""" | |
| # Stage 1 | |
| s1 = self.gate.XOR_gate(flow_a, flow_b) | |
| c1 = self.gate.AND_gate(flow_a, flow_b) | |
| # Stage 2 | |
| sum_flow = self.gate.XOR_gate(s1, flow_cin) | |
| c2 = self.gate.AND_gate(s1, flow_cin) | |
| # Final carry | |
| carry_flow = self.gate.OR_gate(c1, c2) | |
| self.operations_count += 5 | |
| return {'sum': sum_flow, 'carry': carry_flow} | |
| def compute_math_expression(self, flows: List[float]) -> Dict: | |
| """ | |
| คำนวณนิพจน์คณิตศาสตร์ด้วยวงจรน้ำ | |
| ตัวอย่าง: (a AND b) OR (c XOR d) | |
| """ | |
| if len(flows) < 4: | |
| return {'error': 'Need at least 4 flow inputs'} | |
| a, b, c, d = flows[:4] | |
| # (a AND b) | |
| term1 = self.gate.AND_gate(a, b) | |
| # (c XOR d) | |
| term2 = self.gate.XOR_gate(c, d) | |
| # term1 OR term2 | |
| result = self.gate.OR_gate(term1, term2) | |
| self.operations_count += 3 | |
| return { | |
| 'term1_AND': term1, | |
| 'term2_XOR': term2, | |
| 'result_OR': result, | |
| 'expression': '(a AND b) OR (c XOR d)' | |
| } | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| # MAIN EXECUTION | |
| # ═══════════════════════════════════════════════════════════════════════════════ | |
| def main(): | |
| """จุดเริ่มต้น — รันจำลอง 1,000,000 รอบ""" | |
| # สร้างคอมพิวเตอร์น้ำ | |
| computer = WaterComputer() | |
| # รันจำลอง 1,000,000 รอบ | |
| summary = computer.run_simulation( | |
| rounds=1_000_000, | |
| power_increment=1e-8, | |
| sample_every=1000 # บันทึกทุก 1,000 รอบ → 1,000 data points | |
| ) | |
| # พิมพ์รายงาน | |
| computer.print_report(summary) | |
| # ทดสอบวงจรน้ำ | |
| print(f"\n{'='*70}") | |
| print(f" 💧 WATER LOGIC GATE TEST") | |
| print(f"{'='*70}") | |
| circuit = WaterCircuit() | |
| # Test Half Adder | |
| print(f"\n Half Adder Test:") | |
| result = circuit.half_adder(0.5, 0.3) | |
| print(f" Input: flow_a=0.5, flow_b=0.3") | |
| print(f" Sum: {result['sum']:.4f}, Carry: {result['carry']:.4f}") | |
| # Test Full Adder | |
| print(f"\n Full Adder Test:") | |
| result = circuit.full_adder(0.8, 0.6, 0.2) | |
| print(f" Input: flow_a=0.8, flow_b=0.6, cin=0.2") | |
| print(f" Sum: {result['sum']:.4f}, Carry: {result['carry']:.4f}") | |
| # Test Math Expression | |
| print(f"\n Math Expression: (a AND b) OR (c XOR d)") | |
| result = circuit.compute_math_expression([0.7, 0.5, 0.3, 0.9]) | |
| print(f" Input: a=0.7, b=0.5, c=0.3, d=0.9") | |
| print(f" (a AND b) = {result['term1_AND']:.4f}") | |
| print(f" (c XOR d) = {result['term2_XOR']:.4f}") | |
| print(f" Result = {result['result_OR']:.4f}") | |
| print(f"\n Total Logic Operations: {circuit.operations_count}") | |
| # บันทึกข้อมูลเป็น JSON | |
| output_file = "water_computer_results.json" | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| json.dump(summary, f, indent=2, ensure_ascii=False) | |
| print(f"\n 📁 Results saved to: {output_file}") | |
| print(f"\n{'='*70}") | |
| print(f" Ω WATER-BASED COMPUTER — SIMULATION COMPLETE") | |
| print(f"{'='*70}\n") | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 30.9 kB
- Xet hash:
- b91f1476067a93844f4ee92ba3a797084bf79ecf2f73525202372b600db2e9f7
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.