科研 Skill
工程单位换算
unit-converter
Converts flow rates, pressures, viscosities, and other engineering units with dimensional-consistency checking via the Pint library. Use for quick, checked unit conversions.
SKILL.md
Unit Converter Skill
A comprehensive unit conversion tool for engineering work using the Pint library. This skill handles common engineering unit conversions with dimensional analysis and consistency checking.
Overview of Pint Library
Pint is a Python library for working with physical quantities. It provides:
- Unit Registry: Central registry of units and their relationships
- Quantity Objects: Combines numerical values with units
- Automatic Conversion: Converts between compatible units
- Dimensional Analysis: Verifies dimensional consistency
- Custom Units: Allows definition of domain-specific units
- Context Management: Handles different unit systems (SI, Imperial, etc.)
Installation
pip install pint
Basic Usage
import pint
ureg = pint.UnitRegistry()
# Create a quantity
flow = 100 * ureg.liter / ureg.minute
# Convert to another unit
flow_gpm = flow.to('gallon/minute')
print(f"{flow_gpm:.2f}") # Output: 26.42 gallon / minute
Common Engineering Unit Conversions
Flow Rate Conversions
Flow rates are fundamental in fluid mechanics and process engineering.
Common Units:
- m³/s (cubic meters per second): SI standard
- m³/h (cubic meters per hour): Common in HVAC
- L/min (liters per minute): Small flow applications
- gpm (gallons per minute): US standard for liquid flow
- cfm (cubic feet per minute): US standard for gas flow
Conversion Examples:
# Water flow in a pipe
flow = 500 * ureg.liter / ureg.minute
print(f"Flow: {flow.to('m^3/s'):.4f}") # 0.0083 m³/s
print(f"Flow: {flow.to('m^3/hour'):.2f}") # 30.00 m³/h
print(f"Flow: {flow.to('gallon/minute'):.2f}") # 132.09 gpm
# Air flow in ventilation
air_flow = 1000 * ureg.cfm
print(f"Air flow: {air_flow.to('m^3/s'):.3f}") # 0.472 m³/s
print(f"Air flow: {air_flow.to('L/s'):.1f}") # 471.9 L/s
Pressure Conversions
Pressure measurements vary widely across industries and regions.
Common Units:
- Pa (Pascal): SI base unit (N/m²)
- kPa (kiloPascal): Common engineering unit
- bar: Metric unit (100 kPa)
- psi (pounds per square inch): US standard
- mmHg (millimeters of mercury): Medical, vacuum
- atm (atmosphere): Standard atmospheric pressure
Conversion Examples:
# Hydraulic system pressure
pressure = 150 * ureg.bar
print(f"Pressure: {pressure.to('Pa'):.0f}") # 15000000 Pa
print(f"Pressure: {pressure.to('kPa'):.0f}") # 15000 kPa
print(f"Pressure: {pressure.to('psi'):.1f}") # 2176.0 psi
print(f"Pressure: {pressure.to('atm'):.1f}") # 147.9 atm
# Vacuum measurement
vacuum = 500 * ureg.mmHg
print(f"Vacuum: {vacuum.to('Pa'):.0f}") # 66661 Pa
print(f"Vacuum: {vacuum.to('kPa'):.2f}") # 66.66 kPa
print(f"Vacuum: {vacuum.to('psi'):.2f}") # 9.67 psi
# Tire pressure
tire = 32 * ureg.psi
print(f"Tire: {tire.to('kPa'):.1f}") # 220.6 kPa
print(f"Tire: {tire.to('bar'):.2f}") # 2.21 bar
Viscosity Conversions
Viscosity can be expressed as dynamic or kinematic viscosity.
Dynamic Viscosity:
- Pa·s (Pascal-second): SI unit
- cP (centipoise): Common in industry (1 cP = 1 mPa·s)
- P (Poise): CGS unit (1 P = 0.1 Pa·s)
Kinematic Viscosity:
- m²/s: SI unit
- cSt (centistokes): Common in industry
- St (Stokes): CGS unit
Conversion Examples:
# Dynamic viscosity - Water at 20°C
viscosity_dynamic = 1.002 * ureg.centipoise
print(f"Dynamic: {viscosity_dynamic.to('Pa*s'):.6f}") # 0.001002 Pa·s
print(f"Dynamic: {viscosity_dynamic.to('poise'):.5f}") # 0.01002 P
# Dynamic viscosity - Motor oil
oil_dynamic = 250 * ureg.centipoise
print(f"Oil: {oil_dynamic.to('Pa*s'):.3f}") # 0.250 Pa·s
# Kinematic viscosity - Water at 20°C
viscosity_kinematic = 1.004 * ureg.centistokes
print(f"Kinematic: {viscosity_kinematic.to('m^2/s'):.9f}") # 0.000001004 m²/s
print(f"Kinematic: {viscosity_kinematic.to('stokes'):.5f}") # 0.01004 St
# Kinematic viscosity - Motor oil SAE 30
oil_kinematic = 100 * ureg.centistokes
print(f"Oil SAE 30: {oil_kinematic.to('m^2/s'):.7f}") # 0.0000100 m²/s
Note on Viscosity Conversion: To convert between dynamic and kinematic viscosity:
Kinematic Viscosity = Dynamic Viscosity / Density
ν (m²/s) = μ (Pa·s) / ρ (kg/m³)
Length Conversions
Common Units:
- m (meter): SI base unit
- mm (millimeter): Engineering drawings
- cm (centimeter): General measurements
- ft (foot): US standard
- in (inch): US manufacturing
Conversion Examples:
# Pipe diameter
diameter = 4 * ureg.inch
print(f"Diameter: {diameter.to('mm'):.1f}") # 101.6 mm
print(f"Diameter: {diameter.to('m'):.4f}") # 0.1016 m
print(f"Diameter: {diameter.to('ft'):.3f}") # 0.333 ft
# Building dimensions
length = 15.5 * ureg.meter
print(f"Length: {length.to('ft'):.2f}") # 50.85 ft
print(f"Length: {length.to('inch'):.1f}") # 610.2 in
# Sheet thickness
thickness = 0.5 * ureg.mm
print(f"Thickness: {thickness.to('inch'):.4f}") # 0.0197 in
print(f"Thickness: {thickness.to('m'):.6f}") # 0.000500 m
Power Conversions
Common Units:
- W (Watt): SI unit
- kW (kilowatt): Common engineering unit
- HP (horsepower): Mechanical power (1 HP = 745.7 W)
- MW (megawatt): Large power systems
Conversion Examples:
# Motor power
motor = 50 * ureg.horsepower
print(f"Motor: {motor.to('kW'):.2f}") # 37.28 kW
print(f"Motor: {motor.to('W'):.0f}") # 37285 W
# Pump power
pump = 15 * ureg.kW
print(f"Pump: {pump.to('hp'):.2f}") # 20.11 hp
print(f"Pump: {pump.to('W'):.0f}") # 15000 W
# Generator output
generator = 2.5 * ureg.MW
print(f"Generator: {generator.to('kW'):.0f}") # 2500 kW
print(f"Generator: {generator.to('hp'):.1f}") # 3352.0 hp
Temperature Conversions
Common Units:
- K (Kelvin): SI base unit, absolute scale
- °C (Celsius): Metric, water freezes at 0°C
- °F (Fahrenheit): US standard, water freezes at 32°F
Conversion Examples:
# Process temperature
temp_c = 85 * ureg.degC
print(f"Temperature: {temp_c.to('degF'):.1f}") # 185.0 °F
print(f"Temperature: {temp_c.to('kelvin'):.2f}") # 358.15 K
# Cryogenic temperature
temp_k = 77 * ureg.kelvin # Liquid nitrogen
print(f"LN2: {temp_k.to('degC'):.2f}") # -196.15 °C
print(f"LN2: {temp_k.to('degF'):.2f}") # -321.07 °F
# Ambient temperature
temp_f = 72 * ureg.degF
print(f"Room: {temp_f.to('degC'):.1f}") # 22.2 °C
print(f"Room: {temp_f.to('kelvin'):.2f}") # 295.37 K
Additional Engineering Units
Area:
area = 500 * ureg.meter**2
print(f"Area: {area.to('ft^2'):.1f}") # 5381.9 ft²
print(f"Area: {area.to('hectare'):.2f}") # 0.05 ha
# Pipe cross-section
pipe_area = 3.14 * ureg.inch**2
print(f"Pipe area: {pipe_area.to('cm^2'):.2f}") # 20.26 cm²
Volume:
tank = 5000 * ureg.liter
print(f"Tank: {tank.to('m^3'):.1f}") # 5.0 m³
print(f"Tank: {tank.to('gallon'):.1f}") # 1320.9 gal
print(f"Tank: {tank.to('ft^3'):.2f}") # 176.57 ft³
Velocity:
velocity = 2.5 * ureg.meter / ureg.second
print(f"Velocity: {velocity.to('ft/s'):.2f}") # 8.20 ft/s
print(f"Velocity: {velocity.to('km/h'):.1f}") # 9.0 km/h
print(f"Velocity: {velocity.to('mph'):.2f}") # 5.59 mph
Mass Flow:
mass_flow = 100 * ureg.kg / ureg.hour
print(f"Mass flow: {mass_flow.to('kg/s'):.5f}") # 0.02778 kg/s
print(f"Mass flow: {mass_flow.to('lb/min'):.3f}") # 3.667 lb/min
Density:
density = 1000 * ureg.kg / ureg.meter**3
print(f"Density: {density.to('g/cm^3'):.1f}") # 1.0 g/cm³
print(f"Density: {density.to('lb/ft^3'):.2f}") # 62.43 lb/ft³
Energy:
energy = 1000 * ureg.joule
print(f"Energy: {energy.to('kJ'):.1f}") # 1.0 kJ
print(f"Energy: {energy.to('BTU'):.3f}") # 0.948 BTU
print(f"Energy: {energy.to('kWh'):.6f}") # 0.000278 kWh
print(f"Energy: {energy.to('cal'):.1f}") # 239.0 cal
Dimensional Analysis
Dimensional analysis ensures that equations are dimensionally consistent.
Checking Dimensional Consistency
# Reynolds number (dimensionless)
rho = 1000 * ureg.kg / ureg.m**3 # Density
v = 2 * ureg.m / ureg.s # Velocity
D = 0.1 * ureg.m # Diameter
mu = 0.001 * ureg.Pa * ureg.s # Dynamic viscosity
Re = (rho * v * D / mu).to_base_units()
print(f"Reynolds number: {Re:.0f}") # Dimensionless: 200000
# Verify it's dimensionless
print(f"Dimensionality: {Re.dimensionality}") # dimensionless
Common Dimensional Relationships
# Verify force = mass × acceleration
mass = 10 * ureg.kg
accel = 9.81 * ureg.m / ureg.s**2
force = mass * accel
print(f"Force: {force.to('N'):.1f}") # 98.1 N
print(f"Dimensionality: {force.dimensionality}") # [length] * [mass] / [time]^2
# Verify pressure = force / area
force = 1000 * ureg.newton
area = 0.01 * ureg.m**2
pressure = force / area
print(f"Pressure: {pressure.to('kPa'):.0f}") # 100 kPa
print(f"Dimensionality: {pressure.dimensionality}") # [mass] / ([length] * [time]^2)
# Verify power = force × velocity
force = 500 * ureg.N
velocity = 10 * ureg.m / ureg.s
power = force * velocity
print(f"Power: {power.to('kW'):.1f}") # 5.0 kW
print(f"Dimensionality: {power.dimensionality}") # [length]^2 * [mass] / [time]^3
Unit Consistency Checking
Pint automatically checks unit consistency and raises errors for invalid operations.
Valid Operations
# Adding compatible units
length1 = 10 * ureg.meter
length2 = 5 * ureg.feet
total_length = length1 + length2.to('meter')
print(f"Total: {total_length:.3f}") # 11.524 m
# Multiplying different units
area = (5 * ureg.meter) * (3 * ureg.meter)
print(f"Area: {area:.0f}") # 15 m²
# Dividing units
velocity = (100 * ureg.meter) / (10 * ureg.second)
print(f"Velocity: {velocity:.0f}") # 10 m/s
Invalid Operations (Will Raise Errors)
# Cannot add incompatible units
try:
result = (10 * ureg.meter) + (5 * ureg.second)
except pint.errors.DimensionalityError as e:
print(f"Error: Cannot add length and time")
# Cannot compare incompatible units
try:
result = (10 * ureg.meter) > (5 * ureg.kg)
except pint.errors.DimensionalityError as e:
print(f"Error: Cannot compare length and mass")
Best Practices
-
Always specify units explicitly:
# Good pressure = 150 * ureg.psi # Bad - units unclear pressure = 150 -
Use base units for calculations:
# Convert to base units for complex calculations result = calculation().to_base_units() -
Check dimensionality:
# Verify result is what you expect if result.dimensionality == ureg.pascal.dimensionality: print("Result is a pressure") -
Handle unit strings carefully:
# Parse unit strings safely try: quantity = ureg.Quantity(value, unit_string) except pint.errors.UndefinedUnitError: print(f"Unknown unit: {unit_string}")
Using the converter.py Module
The included converter.py module provides convenient functions for common conversions:
from converter import (
convert_flow,
convert_pressure,
convert_viscosity_dynamic,
convert_length,
convert_power,
convert_temperature,
check_dimensional_consistency,
ureg
)
# Simple conversions
flow_gpm = convert_flow(100, 'L/min', 'gal/min')
print(f"Flow: {flow_gpm:.2f} gpm")
pressure_bar = convert_pressure(150, 'psi', 'bar')
print(f"Pressure: {pressure_bar:.2f} bar")
# Check if operation is dimensionally valid
is_valid = check_dimensional_consistency('m/s', 'ft/min')
print(f"Can convert velocity: {is_valid}") # True
is_valid = check_dimensional_consistency('m', 'kg')
print(f"Can convert length to mass: {is_valid}") # False
Quick Reference
See the included quick reference table for common conversion factors and formulas.
Error Handling
Always wrap conversions in try-except blocks for production code:
try:
result = convert_pressure(150, 'psi', 'bar')
print(f"Pressure: {result:.2f} bar")
except pint.errors.UndefinedUnitError:
print("Unknown unit specified")
except pint.errors.DimensionalityError:
print("Units are not compatible")
except Exception as e:
print(f"Conversion error: {e}")
Tips for Engineering Applications
- Always document units in comments and variable names
- Use consistent unit systems within calculations
- Verify dimensional analysis for derived quantities
- Store values with units throughout calculations
- Only convert to display units at the end
- Use Pint's formatting for clear output
Resources
示例提示词
帮我把这批流量数据从加仑每分钟换算成立方米每小时。
所属插件
安装该插件会一并获得同领域的 36 个 Skill / MCP。
同插件的其他科研 Skill
- ANSYS Fluent CFD仿真自动化Automates ANSYS Fluent CFD setup, meshing, and batch runs via Python scripting, journal files, and the PyAnsys ecosystem. Use when a user wants to script or automate an ANSYS Fluent simulation.
- Cantera燃烧仿真Runs Cantera combustion simulations: flame temperature, ignition delay, laminar flame speed, PSR. Use for mechanism-based combustion analysis tasks.
- 离心泵气蚀风险评估Calculates NPSHa and NPSHr and assesses cavitation risk for centrifugal pumps, with prevention and detection guidance. Use when sizing suction piping or diagnosing pump cavitation.
- 气蚀评估物性数据库Looks up vapor pressure, NPSHr data, and correlations for cavitation risk assessment in pumps. Use when cavitation-analysis needs reference property or NPSHr data.
- 离心泵设计计算Designs centrifugal pumps from first principles using Euler equations, velocity triangles, and specific speed. Use for impeller sizing or new centrifugal pump design.
- COMSOL多物理场耦合仿真Sets up coupled fluid-structure interaction simulations in COMSOL Multiphysics, focused on pump vibration analysis. Use when a user wants to script or automate a COMSOL FSI model.
- CoolProp流体物性查询Queries thermodynamic and transport properties for 100+ pure and pseudo-pure fluids via the CoolProp library. Use when a calculation needs fluid density, viscosity, or enthalpy data.
- 工程计算环境初始化Sets up a Python session with engineering constants, Pint units, and plotting defaults. Use at the start of a calculation session.
- 流体计算数值错误校验Handles common numerical errors in fluid calculations, such as division by zero and out-of-range physical values, with validation checks. Use when fluid code fails or returns unphysical results.
- 流体力学分析工作流Guides a systematic fluid dynamics analysis from problem setup and domain definition through model selection to validation. Use when planning a CFD or analytical fluid mechanics study end to end.
- 经验公式流体物性速算Computes quick fluid property estimates (density, viscosity, vapor pressure) from empirical correlations without needing a database. Use for fast order-of-magnitude property checks.
- 管道流动与泵选型计算Computes pipe flow, pump sizing, friction factor, and compressible flow results with the fluids Python package. Use for quick, code-based fluid mechanics calculations.
- 工程工具与数据库清点Enumerates installed Python packages, databases, and simulation tools available in the current environment. Use at the start of a session to avoid recommending unavailable tools.
- 管路元件损失系数查询Looks up loss coefficients (K-values) and equivalent lengths for pipes, valves, and fittings in pump and piping systems. Use when computing system head loss.
- 流体与材料物性查询Looks up temperature-dependent fluid viscosities, densities, and other material properties for pump and heat-transfer calculations. Use when a calculation needs property data at a given temperature.
- 泵性能曲线绘制Creates professional engineering plots such as pump performance curves, CFD velocity contours, and publication-quality figures with matplotlib. Use when a result needs a plotted figure.
- NASA大气与航空流体数据Accesses atmospheric property profiles and standard-atmosphere models from NASA Earthdata for high-altitude and aerospace fluid analysis. Use when a task needs altitude-dependent atmospheric data.
- 管网图论建模Models hydraulic piping networks and multi-pump systems as graphs using NetworkX flow algorithms. Use for analyzing flow distribution across a branched or looped piping network.
- REFPROP高精度物性查询Queries high-accuracy thermodynamic and transport properties from the commercial NIST REFPROP database. Use when a calculation needs reference-quality fluid property data and REFPROP is licensed.
- 数值数组计算Performs numerical array operations with NumPy: velocity field processing, pump curve interpolation, linear systems, and matrix methods. Use for general numerical computation in engineering code.
- OpenFOAM仿真与后处理Runs and post-processes OpenFOAM CFD simulations with PyFoam, pyvista, and vtk for mesh setup, solver control, and visualization. Use for scripted OpenFOAM workflows outside ParaView.
- 泵性能曲线与相似定律Generates and interprets pump H-Q/efficiency/power/NPSH curves, applies affinity laws, and predicts off-design performance. Use for pump curve analysis.
- 工程单位换算与量纲分析Handles engineering units with automatic conversion and dimensional-consistency checking using the Pint library. Use whenever a calculation mixes units or needs unit safety.
- 容积式泵设计分析Designs and analyzes gear, piston, and screw positive-displacement pumps, including volumetric efficiency and slip. Use when the pump is positive-displacement rather than centrifugal.
常见问题
工程单位换算能做什么?
Converts flow rates, pressures, viscosities, and other engineering units with dimensional-consistency checking via the Pint library. Use for quick, checked unit conversions。
如何安装工程单位换算?
在科搭 Codar 左侧点击「插件」,在「组件」页签搜索「工程单位换算」并安装;在 Claude Code 或 Codex 中可以发送本页的安装提示词,或下载 Skill 包后解压到 ~/.claude/skills 或 ~/.codex/skills。
工程单位换算需要哪些权限?
该科研 Skill没有声明额外权限;执行具体操作时仍由 Agent 按自身规则请求确认。
工程单位换算经过官方审核吗?
是。SpeedSkills 收录的每个版本都会记录内容摘要与权限声明,当前版本已通过安全审核。