126 lines
6.6 KiB
JavaScript
126 lines
6.6 KiB
JavaScript
/* pond.js — ปริมาตรสระน้ำ (เรขาคณิต) + น้ำฝนที่รับได้
|
||
pure module: ไม่แตะ DOM
|
||
|
||
สูตรที่ใช้ (ตรวจย้อนกลับได้):
|
||
- ปริมาตรทรงสี่เหลี่ยมคางหมู (frustum): V = h/3 × (Aล่าง + Aบน + √(Aล่าง×Aบน))
|
||
- ความชันข้าง s (แนวนอน ต่อ 1 แนวตั้ง): ด้านล่าง = ด้านบน − 2×s×h
|
||
- น้ำฝนลงสระ = พื้นที่รับน้ำฝน(ตร.ม.) × ฝน(ม.) × สัมประสิทธิ์น้ำไหลลงสระ
|
||
*/
|
||
|
||
export const RAI_IN_SQUARE_METRE = 1600;
|
||
|
||
function requirePositive(value, label) {
|
||
const number = Number(value);
|
||
if (!Number.isFinite(number) || number <= 0) {
|
||
throw new RangeError(label + " ต้องเป็นตัวเลขมากกว่า 0");
|
||
}
|
||
return number;
|
||
}
|
||
|
||
function requirePercent(value, label) {
|
||
const number = Number(value);
|
||
if (!Number.isFinite(number) || number <= 0 || number > 100) {
|
||
throw new RangeError(label + " ต้องอยู่ระหว่าง 0–100");
|
||
}
|
||
return number;
|
||
}
|
||
|
||
function frustumVolume(bottomArea, topArea, height) {
|
||
return (height / 3) * (bottomArea + topArea + Math.sqrt(bottomArea * topArea));
|
||
}
|
||
|
||
/**
|
||
* @param {object} input
|
||
* @param {"box"|"trapezoid"} input.shape box = ผนังตั้งฉาก, trapezoid = ผนังลาด
|
||
* @param {number} input.top_length_m ความยาวปากบ่อ (ม.)
|
||
* @param {number} input.top_width_m ความกว้างปากบ่อ (ม.)
|
||
* @param {number} input.depth_m ความลึกบ่อ (ม.)
|
||
* @param {number} [input.side_slope_horizontal_per_vertical] ความชันผนัง (แนวนอนต่อแนวตั้ง)
|
||
* @param {number} input.usable_depth_percent ระดับน้ำใช้การ (% ของความลึก)
|
||
* @param {number} [input.catchment_rai] พื้นที่รับน้ำฝน (ไร่)
|
||
* @param {number} [input.rainfall_mm] ปริมาณฝนที่ตกในครั้ง/ช่วงที่คิด (มม.)
|
||
* @param {number} [input.runoff_coefficient] สัมประสิทธิ์น้ำไหลลงสระ (0–1)
|
||
*/
|
||
export function calculatePondVolume(input) {
|
||
const shape = input.shape === "box" ? "box" : "trapezoid";
|
||
const topLength = requirePositive(input.top_length_m, "ความยาวปากบ่อ");
|
||
const topWidth = requirePositive(input.top_width_m, "ความกว้างปากบ่อ");
|
||
const depth = requirePositive(input.depth_m, "ความลึกบ่อ");
|
||
const slope = shape === "box" ? 0 : Math.max(0, Number(input.side_slope_horizontal_per_vertical) || 0);
|
||
const usablePercent = requirePercent(input.usable_depth_percent, "ระดับน้ำใช้การ");
|
||
|
||
const catchmentRai = Math.max(0, Number(input.catchment_rai) || 0);
|
||
const rainfallMm = Math.max(0, Number(input.rainfall_mm) || 0);
|
||
const runoff = Math.min(1, Math.max(0, Number(input.runoff_coefficient ?? 0.8)));
|
||
|
||
const bottomLength = topLength - 2 * slope * depth;
|
||
const bottomWidth = topWidth - 2 * slope * depth;
|
||
|
||
if (bottomLength <= 0 || bottomWidth <= 0) {
|
||
throw new RangeError(
|
||
"ผนังลาดชันเกินขนาดปากบ่อ: ด้านล่างเหลือ " +
|
||
bottomLength.toFixed(2) + " × " + bottomWidth.toFixed(2) +
|
||
" ม. — เพิ่มขนาดปากบ่อ ลดความลึก หรือลดความชันผนัง"
|
||
);
|
||
}
|
||
|
||
const topArea = topLength * topWidth;
|
||
const bottomArea = bottomLength * bottomWidth;
|
||
const excavatedVolume = shape === "box" ? topArea * depth : frustumVolume(bottomArea, topArea, depth);
|
||
|
||
/* ปริมาตรน้ำใช้การ = ปริมาตรระหว่างก้นบ่อถึงระดับน้ำใช้การ */
|
||
const usableDepth = (depth * usablePercent) / 100;
|
||
const levelLength = bottomLength + 2 * slope * usableDepth;
|
||
const levelWidth = bottomWidth + 2 * slope * usableDepth;
|
||
const levelArea = levelLength * levelWidth;
|
||
const usableVolume = shape === "box"
|
||
? bottomArea * usableDepth
|
||
: frustumVolume(bottomArea, levelArea, usableDepth);
|
||
|
||
const rainHarvestM3 = catchmentRai * RAI_IN_SQUARE_METRE * (rainfallMm / 1000) * runoff;
|
||
const totalAvailable = usableVolume + rainHarvestM3;
|
||
|
||
const warnings = [];
|
||
if (usablePercent < 50) {
|
||
warnings.push("ระดับน้ำใช้การเหลือ " + usablePercent + "% — น้ำส่วนล่างจะระเหย/รั่วซึมง่าย");
|
||
}
|
||
if (shape === "box" && depth > 4) {
|
||
warnings.push("ผนังตั้งฉากลึกเกิน 4 ม. เสี่ยงดินถล่ม — ควรลาดผนัง (trapezoid)");
|
||
}
|
||
if (rainfallMm > 0 && catchmentRai === 0) {
|
||
warnings.push("ใส่พื้นที่รับน้ำฝนเป็น 0 — น้ำฝนที่คำนวณได้จึงเป็น 0");
|
||
}
|
||
|
||
const steps = [
|
||
{ label: "พื้นที่ปากบ่อ", value: Number(topArea.toFixed(2)), unit: "ตร.ม." },
|
||
{ label: "พื้นที่ก้นบ่อ", value: Number(bottomArea.toFixed(2)), unit: "ตร.ม." },
|
||
{ label: "ปริมาตรขุดทั้งหมด", value: Number(excavatedVolume.toFixed(2)), unit: "ลบ.ม." },
|
||
{
|
||
label: "ระดับน้ำใช้การ " + usablePercent + "% = " + usableDepth.toFixed(2) + " ม.",
|
||
value: Number(usableVolume.toFixed(2)),
|
||
unit: "ลบ.ม."
|
||
},
|
||
{
|
||
label: "น้ำฝนลงสระ = " + catchmentRai + " ไร่ × " + rainfallMm + " มม. × " + runoff,
|
||
value: Number(rainHarvestM3.toFixed(2)),
|
||
unit: "ลบ.ม."
|
||
}
|
||
];
|
||
|
||
return {
|
||
value: Number(totalAvailable.toFixed(2)),
|
||
unit: "cubic_metre",
|
||
steps,
|
||
warnings,
|
||
details: {
|
||
excavated_volume_m3: Number(excavatedVolume.toFixed(2)),
|
||
usable_volume_m3: Number(usableVolume.toFixed(2)),
|
||
rain_harvest_m3: Number(rainHarvestM3.toFixed(2)),
|
||
total_available_m3: Number(totalAvailable.toFixed(2)),
|
||
bottom_length_m: Number(bottomLength.toFixed(2)),
|
||
bottom_width_m: Number(bottomWidth.toFixed(2)),
|
||
usable_depth_m: Number(usableDepth.toFixed(2))
|
||
}
|
||
};
|
||
}
|