|
| 1 | +/** |
| 2 | + * @copyright Copyright (c) 2024-2025 Ronan LE MEILLAT |
| 3 | + * @license AGPL-3.0-or-later |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, either version 3 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +import type React from "react"; |
| 19 | + |
| 20 | +import { |
| 21 | + AGA8wasm, |
| 22 | + PropertiesGERGResult, |
| 23 | + R, |
| 24 | + type GasMixtureExt, |
| 25 | +} from "@sctg/aga8-js"; |
| 26 | +import { useEffect } from "react"; |
| 27 | + |
| 28 | +import { OrificeSelector } from "./OrificeSelector"; |
| 29 | +import { PressureSlider } from "./PressureSlider"; |
| 30 | +import { ConcentrationSelector } from "./ConcentrationSelector"; |
| 31 | +import { Cd } from "@/config/site"; |
| 32 | +import { logSonicNozzleFlowCalculation } from "@/utilities"; |
| 33 | + |
| 34 | +export type FlowData = { |
| 35 | + massFlow: number; // kg/s |
| 36 | + p_crit: number; // kPa |
| 37 | + A: number; // area of the orifice in m² |
| 38 | + properties: PropertiesGERGResult; // Gas properties |
| 39 | + molarMass: number; // g/mol |
| 40 | + Rs: number; // J/(kg·K) |
| 41 | + rho: number; // kg/m³ |
| 42 | + rho_out: number; // kg/m³ |
| 43 | +}; |
| 44 | +interface CalibrationInletProps { |
| 45 | + label: string; |
| 46 | + pressure: number; |
| 47 | + selectedGas: GasMixtureExt; |
| 48 | + selectedCalibrationConcentration: number; |
| 49 | + selectedOrifice: number; |
| 50 | + temperature: number; |
| 51 | + onPressureChange: (pressure: number) => void; |
| 52 | + onCalibrationConcentrationChange: (concentration: number) => void; |
| 53 | + onOrificeChange: (orifice: number) => void; |
| 54 | + onFlowDataChange: (flowData: FlowData) => void; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Compute the gas flow function of pressure |
| 59 | + * @param temperature - Temperature in K |
| 60 | + * @param pressure - Inlet pressure in kPa |
| 61 | + * @param outletPressure - Outlet pressure in kPa |
| 62 | + * @param gas - Gas mixture |
| 63 | + * @param orifice - Orifice diameter in mm |
| 64 | + * @returns the mass flow rate in kg/s and the critical pressure in kPa |
| 65 | + */ |
| 66 | +async function computeGasFlowFunctionOfPressure( |
| 67 | + temperature: number, |
| 68 | + pressure: number, |
| 69 | + outletPressure: number, |
| 70 | + gas: GasMixtureExt, |
| 71 | + orifice: number, |
| 72 | +): Promise<FlowData> { |
| 73 | + // Initialize GERG-2008 module |
| 74 | + const AGA8 = await AGA8wasm(); |
| 75 | + |
| 76 | + AGA8.SetupGERG(); |
| 77 | + |
| 78 | + const A = Math.PI * Math.pow(orifice / 2000, 2); // A - Area of the orifice |
| 79 | + |
| 80 | + // Calculate gas properties |
| 81 | + const molarMass = AGA8.MolarMassGERG(gas.gasMixture); // g/mol |
| 82 | + const { D } = AGA8.DensityGERG(0, temperature, pressure, gas.gasMixture); // mol/L |
| 83 | + const { D: D_out } = AGA8.DensityGERG( |
| 84 | + 0, |
| 85 | + temperature, |
| 86 | + outletPressure, |
| 87 | + gas.gasMixture, |
| 88 | + ); // mol/L |
| 89 | + const properties = AGA8.PropertiesGERG(temperature, D, gas.gasMixture); |
| 90 | + const molarMassSI = molarMass / 1000; // kg/mol (SI units) |
| 91 | + const densitySI = D * 1000; // mol/m³ (SI units) |
| 92 | + // Extract critical flow factor (Cf) |
| 93 | + const Cf = properties.Cf; |
| 94 | + |
| 95 | + /** Specific gas constant */ |
| 96 | + const Rs = R / molarMassSI; // J/(kg·K) |
| 97 | + |
| 98 | + // Maximal outlet pressure (critical flow) |
| 99 | + const p_crit = pressure * Cf; // kPa |
| 100 | + // Calculate mass flow rate |
| 101 | + // Q = Cd * Cf * A * P / sqrt(Rs * T) |
| 102 | + const massFlow = |
| 103 | + (Cd * Cf * A * (pressure * 1000)) / Math.sqrt(Rs * temperature); // kg/s |
| 104 | + |
| 105 | + const rho = densitySI * molarMassSI; // kg/m³ |
| 106 | + const rho_out = D_out * 1000 * molarMassSI; // kg/m³ |
| 107 | + |
| 108 | + const _flowData = { |
| 109 | + massFlow: massFlow, |
| 110 | + p_crit: p_crit, |
| 111 | + A: A, |
| 112 | + properties: properties, |
| 113 | + molarMass: molarMass, |
| 114 | + Rs: Rs, |
| 115 | + rho: rho, |
| 116 | + rho_out: rho_out, |
| 117 | + }; |
| 118 | + |
| 119 | + // Output results |
| 120 | + logSonicNozzleFlowCalculation( |
| 121 | + gas, |
| 122 | + temperature, |
| 123 | + pressure, |
| 124 | + outletPressure, |
| 125 | + orifice, |
| 126 | + _flowData, |
| 127 | + ); |
| 128 | + |
| 129 | + return _flowData; |
| 130 | +} |
| 131 | + |
| 132 | +export const CalibrationInlet: React.FC<CalibrationInletProps> = ({ |
| 133 | + label, |
| 134 | + pressure, |
| 135 | + selectedGas, |
| 136 | + selectedCalibrationConcentration, |
| 137 | + selectedOrifice, |
| 138 | + temperature, |
| 139 | + onPressureChange, |
| 140 | + onCalibrationConcentrationChange, |
| 141 | + onOrificeChange, |
| 142 | + onFlowDataChange, |
| 143 | +}) => { |
| 144 | + useEffect(() => { |
| 145 | + const updateFlow = async () => { |
| 146 | + const newFlowData = await computeGasFlowFunctionOfPressure( |
| 147 | + temperature, |
| 148 | + pressure, |
| 149 | + 101.325, |
| 150 | + selectedGas, |
| 151 | + selectedOrifice, |
| 152 | + ); |
| 153 | + |
| 154 | + onFlowDataChange(newFlowData); |
| 155 | + }; |
| 156 | + |
| 157 | + updateFlow(); |
| 158 | + }, [temperature, pressure, selectedGas, selectedOrifice, onFlowDataChange]); |
| 159 | + |
| 160 | + return ( |
| 161 | + <div> |
| 162 | + <PressureSlider |
| 163 | + label={`${label} Pressure`} |
| 164 | + value={pressure} |
| 165 | + onChange={onPressureChange} |
| 166 | + /> |
| 167 | + <div className="my-4"> |
| 168 | + <ConcentrationSelector |
| 169 | + label={`${label} concentration`} |
| 170 | + selectedConcentration={selectedCalibrationConcentration} |
| 171 | + onConcentrationChange={onCalibrationConcentrationChange} |
| 172 | + /> |
| 173 | + </div> |
| 174 | + <OrificeSelector |
| 175 | + label={`Orifice ${label}`} |
| 176 | + selectedOrifice={selectedOrifice} |
| 177 | + onOrificeChange={onOrificeChange} |
| 178 | + /> |
| 179 | + </div> |
| 180 | + ); |
| 181 | +}; |
| 182 | + |
0 commit comments