This repository has been archived on 2026-02-12. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
swift-duct-calc/Sources/ManualDClient/Live.swift
Michael Housh 4f3cc2c7ea
Some checks failed
CI / Linux Tests (push) Failing after 7m3s
WIP: Cleans up ManualDClient and adds some more document strings.
2026-01-29 22:56:39 -05:00

51 lines
1.9 KiB
Swift

import Dependencies
import Foundation
import ManualDCore
extension ManualDClient: DependencyKey {
public static let liveValue: Self = .init(
ductSize: { cfm, frictionRate in
guard cfm > 0 else {
throw ManualDError(message: "Design CFM should be greater than 0.")
}
let fr = pow(frictionRate.rawValue, 0.5)
let ductulatorSize = pow(Double(cfm.rawValue) / (3.12 * fr), 0.38)
let finalSize = try roundSize(ductulatorSize)
let flexSize = try flexSize(cfm, frictionRate.rawValue)
return .init(
calculatedSize: ductulatorSize,
finalSize: finalSize,
flexSize: flexSize,
velocity: velocity(cfm: cfm, roundSize: finalSize)
)
},
frictionRate: { request in
// Ensure the total effective length is greater than 0.
guard request.totalEquivalentLength > 0 else {
throw ManualDError(message: "Total Effective Length should be greater than 0.")
}
let totalComponentLosses = request.componentPressureLosses.total
let availableStaticPressure = request.externalStaticPressure - totalComponentLosses
let frictionRate = availableStaticPressure * 100.0 / Double(request.totalEquivalentLength)
return .init(
availableStaticPressure: availableStaticPressure,
value: frictionRate
)
},
// totalEquivalentLength: { request in
// let trunkLengths = request.trunkLengths.reduce(0) { $0 + $1 }
// let runoutLengths = request.runoutLengths.reduce(0) { $0 + $1 }
// let groupLengths = request.effectiveLengthGroups.totalEffectiveLength
// return trunkLengths + runoutLengths + groupLengths
// },
rectangularSize: { round, height in
let width = (Double.pi * (pow(Double(round.rawValue) / 2.0, 2.0))) / Double(height.rawValue)
return .init(
height: height.rawValue,
width: Int(width.rounded(.toNearestOrEven))
)
}
)
}