feat: Finishes filter pressure drop calculator

This commit is contained in:
2025-03-03 12:55:06 -05:00
parent eb6ec446dc
commit 8e0860af8e
10 changed files with 279 additions and 54 deletions

View File

@@ -1,8 +1,14 @@
# NOTE: Builds currently fail when building in release mode.
ARG SWIFT_MODE="debug"
# ================================
# Build image
# ================================
FROM swift:6.0-noble AS build
ARG SWIFT_MODE
# Install OS updates
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
&& apt-get -q update \
@@ -17,30 +23,35 @@ WORKDIR /build
# as long as your Package.swift/Package.resolved
# files do not change.
COPY ./Package.* ./
RUN swift package resolve \
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
RUN --mount=type=cache,target=/build/.build swift package resolve \
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
# Copy entire repo into container
COPY . .
# Build the application, with optimizations, with static linking, and using jemalloc
# N.B.: The static version of jemalloc is incompatible with the static Swift runtime.
RUN swift build -c release \
--product App \
--static-swift-stdlib \
-Xlinker -ljemalloc
RUN --mount=type=cache,target=/build/.build swift build \
-c ${SWIFT_MODE} \
--product App \
--static-swift-stdlib \
-Xswiftc -g \
-Xlinker -ljemalloc
# Switch to the staging area
WORKDIR /staging
# Copy main executable to staging area
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./
RUN --mount=type=cache,target=/build/.build cp \
"$(swift build --package-path /build -c ${SWIFT_MODE} --show-bin-path)/App" ./
# Copy static swift backtracer binary to staging area
RUN cp "/usr/libexec/swift/linux/swift-backtrace-static" ./
RUN --mount=type=cache,target=/build/.build \
cp "/usr/libexec/swift/linux/swift-backtrace-static" ./
# Copy resources bundled by SPM to staging area
RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
RUN --mount=type=cache,target=/build/.build \
find -L "$(swift build --package-path /build -c ${SWIFT_MODE} --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
# Copy any resources from the public directory and views directory if the directories exist
# Ensure that by default, neither the directory nor any of its contents are writable.

View File

@@ -1,4 +1,66 @@
FROM swift:6.0-noble
# NOTE: Builds currently fail when building in release mode.
ARG SWIFT_MODE="debug"
# ================================
# Build image
# ================================
FROM swift:6.0-noble AS build
ARG SWIFT_MODE
# Install OS updates
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
&& apt-get -q update \
&& apt-get -q dist-upgrade -y \
&& apt-get install -y libjemalloc-dev
# Set up a build area
WORKDIR /build
# First just resolve dependencies.
# This creates a cached layer that can be reused
# as long as your Package.swift/Package.resolved
# files do not change.
COPY ./Package.* ./
RUN --mount=type=cache,target=/build/.build swift package resolve \
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
# Copy entire repo into container
COPY . .
# Build the application, with optimizations, with static linking, and using jemalloc
# N.B.: The static version of jemalloc is incompatible with the static Swift runtime.
RUN --mount=type=cache,target=/build/.build swift build \
-c ${SWIFT_MODE} \
--product App \
--static-swift-stdlib \
-Xswiftc -g \
-Xlinker -ljemalloc
# Switch to the staging area
WORKDIR /staging
# Copy main executable to staging area
RUN --mount=type=cache,target=/build/.build cp \
"$(swift build --package-path /build -c ${SWIFT_MODE} --show-bin-path)/App" ./
# Copy static swift backtracer binary to staging area
RUN --mount=type=cache,target=/build/.build \
cp "/usr/libexec/swift/linux/swift-backtrace-static" ./
# Copy resources bundled by SPM to staging area
RUN --mount=type=cache,target=/build/.build \
find -L "$(swift build --package-path /build -c ${SWIFT_MODE} --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
# Copy any resources from the public directory and views directory if the directories exist
# Ensure that by default, neither the directory nor any of its contents are writable.
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
# ================================
# Run image
# ================================
FROM ubuntu:noble
# Make sure all system packages are up to date, and install only essential packages.
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
@@ -9,24 +71,30 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
ca-certificates \
tzdata \
# If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
libcurl4 \
libcurl4 \
curl \
# If your app or its dependencies import FoundationXML, also install `libxml2`.
# libxml2 \
sqlite3 \
&& rm -r /var/lib/apt/lists/*
# Set up a build area
# Create a vapor user and group with /app as its home directory
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
# Switch to the new home directory
WORKDIR /app
# First just resolve dependencies.
# This creates a cached layer that can be reused
# as long as your Package.swift/Package.resolved
# files do not change.
COPY ./Package.* ./
RUN swift package resolve \
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
# Copy built executable and any staged resources from builder
COPY --from=build --chown=vapor:vapor /staging /app
# Copy entire repo into container
COPY . .
# Provide configuration needed by the built-in crash reporter and some sensible default behaviors.
ENV SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=./swift-backtrace-static
CMD ["swift", "test"]
# Ensure all further commands run as the vapor user
USER vapor:vapor
# Let Docker bind to port 8080
EXPOSE 8080
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
ENTRYPOINT ["./App"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

View File

@@ -107,5 +107,6 @@ var swiftSettings: [SwiftSetting] { [
.enableExperimentalFeature("StrictConcurrency=complete"),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("ForwardTrailingClosures")
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("InferSendableFromCaptures")
] }

File diff suppressed because one or more lines are too long

View File

@@ -42,8 +42,7 @@ extension ApiController: DependencyKey {
case let .calculateFilterPressureDrop(request):
logger.debug("Calculating filter pressure drop: \(request)")
// FIX:
fatalError()
return try await request.respond(logger: logger)
case let .calculateHVACSystemPerformance(request):
logger.debug("Calculating hvac system performance: \(request)")

View File

@@ -0,0 +1,156 @@
import Foundation
import Logging
import Routes
public extension FilterPressureDrop.Request {
func respond(logger: Logger) async throws -> FilterPressureDrop.Response {
switch self {
case let .basic(request):
return try request.respond(logger: logger)
case let .fanLaw(request):
return try request.respond(logger: logger)
}
}
}
private extension FilterPressureDrop.Request.Basic {
func respond(logger: Logger) throws -> FilterPressureDrop.Response {
try validate()
let cfmPerTon = Double(climateZone.cfmPerTon)
let totalCFM = systemSize * cfmPerTon
// calculate filter area in sq. ft.
let filterArea = (filterWidth * filterHeight) / 144
let filterVelocity = totalCFM / filterArea
let initialPressureDrop = filterType.initialPressureDrop(velocity: filterVelocity)
let maxPressureDrop = 0.15
var warnings = [String]()
if filterVelocity > 350 {
warnings.append(
"""
Face velocity exceeds 350 FPM - consider using a larger filter to reduce pressure drop and
improve system efficiency.
"""
)
}
if initialPressureDrop > (maxPressureDrop / 2) {
warnings.append(
"""
Initial pressure drop is more than 50% of maximum allowable - consider using a larger filter or different
filter type with lower initial pressure drop.
"""
)
}
return .basic(.init(
filterArea: filterArea,
feetPerMinute: filterVelocity,
initialPressureDrop: initialPressureDrop,
maxPressureDrop: maxPressureDrop,
warnings: warnings
))
}
func validate() throws {
guard systemSize > 0 else {
throw ValidationError(message: "System size should be greater than 0.")
}
guard filterWidth > 0 else {
throw ValidationError(message: "Filter width should be greater than 0.")
}
guard filterHeight > 0 else {
throw ValidationError(message: "Filter height should be greater than 0.")
}
}
}
private extension FilterPressureDrop.Request.FanLaw {
func respond(logger: Logger) throws -> FilterPressureDrop.Response {
try validate()
// calculate filter area in square ft.
let filterArea = (filterWidth * filterHeight) / 144
let faceVelocity = designAirflow / filterArea
let velocityRatio = designAirflow / ratedAirflow
let pressureRatio = pow(velocityRatio, 2)
let predictedPressureDrop = ratedPressureDrop * pressureRatio
return .fanLaw(.init(
predictedPressureDrop: predictedPressureDrop,
velocityRatio: velocityRatio,
pressureRatio: pressureRatio,
faceVelocity: faceVelocity
))
}
func validate() throws {
guard filterWidth > 0 else {
throw ValidationError(message: "Filter width should be greater than 0.")
}
guard filterHeight > 0 else {
throw ValidationError(message: "Filter height should be greater than 0.")
}
guard filterDepth > 0 else {
throw ValidationError(message: "Filter depth should be greater than 0.")
}
guard ratedAirflow > 0 else {
throw ValidationError(message: "Rated airflow should be greater than 0.")
}
guard ratedPressureDrop > 0 else {
throw ValidationError(message: "Rated pressure drop should be greater than 0.")
}
guard designAirflow > 0 else {
throw ValidationError(message: "Design airflow should be greater than 0.")
}
}
}
extension FilterPressureDrop.FilterType {
// swiftlint:disable cyclomatic_complexity
func initialPressureDrop(velocity: Double) -> Double {
switch self {
case .fiberglass:
if velocity <= 300 {
return 0.05
} else if velocity <= 350 {
return 0.08
} else {
return 0.12
}
case .pleatedBasic:
if velocity <= 300 {
return 0.08
} else if velocity <= 350 {
return 0.12
} else {
return 0.15
}
case .pleatedBetter:
if velocity <= 300 {
return 0.15
} else if velocity <= 350 {
return 0.18
} else {
return 0.22
}
case .pleatedBest:
if velocity <= 300 {
return 0.20
} else if velocity <= 350 {
return 0.25
} else {
return 0.3
}
}
}
// swiftlint:enable cyclomatic_complexity
}

View File

@@ -15,11 +15,11 @@ public enum FilterPressureDrop {
public struct Basic: Codable, Equatable, Sendable {
let systemSize: Double
let climateZone: ClimateZone
let filterType: FilterType
let filterWidth: Double
let filterHeight: Double
public let systemSize: Double
public let climateZone: ClimateZone
public let filterType: FilterType
public let filterWidth: Double
public let filterHeight: Double
public init(
systemSize: Double,
@@ -38,12 +38,12 @@ public enum FilterPressureDrop {
public struct FanLaw: Codable, Equatable, Sendable {
let filterWidth: Double
let filterHeight: Double
let filterDepth: Double
let ratedAirflow: Double
let ratedPressureDrop: Double
let designAirflow: Double
public let filterWidth: Double
public let filterHeight: Double
public let filterDepth: Double
public let ratedAirflow: Double
public let ratedPressureDrop: Double
public let designAirflow: Double
public init(
filterWidth: Double,

View File

@@ -44,7 +44,7 @@ struct HomePage: HTML, Sendable {
route: .capacitor(.index)
)
group(
label: "Filter Pressure Drop",
label: "Filter Sizing",
description: FilterPressureDrop.description,
svg: .funnel,
route: .filterPressureDrop(.index)

View File

@@ -103,15 +103,10 @@ extension ViewController: DependencyKey {
case let .filterPressureDrop(route):
switch route {
case let .index(mode: mode):
// FIX: remove response.
#if DEBUG
return request.respond(FilterPressureDropForm(mode: mode, response: .mock(mode: mode ?? .basic)))
#else
return request.respond(FilterPressureDropForm(mode: mode, response: nil))
#endif
case .submit:
// FIX: implement.
fatalError()
return request.respond(FilterPressureDropForm(mode: mode, response: nil))
case let .submit(request):
let response = try await request.respond(logger: logger)
return FilterPressureDropResponse(response: response)
}
case let .hvacSystemPerformance(route):

View File

@@ -25,9 +25,6 @@ struct FilterPressureDropForm: HTML, Sendable {
.attributes(.class("mb-6"))
}
// FIX: Remove when done.
WarningBox("This is under construction and does not work currently.")
form(
.hx.post(route: .filterPressureDrop(.index)),
.hx.target("#result")
@@ -41,8 +38,6 @@ struct FilterPressureDropForm: HTML, Sendable {
}
div {
SubmitButton(label: "Calculate Pressure Drop")
// FIX: Remove when done.
.attributes(.disabled)
}
}
}
@@ -127,7 +122,7 @@ struct FilterPressureDropForm: HTML, Sendable {
.attributes(.type(.number), .step("0.1"), .min("0.1"), .required)
}
LabeledContent(label: "Filter Pressure Drop: (in. w.c.)") {
Input(id: "ratedPressure", placeholder: "Rated or measured pressure drop")
Input(id: "ratedPressureDrop", placeholder: "Rated or measured pressure drop")
.attributes(.type(.number), .step("0.1"), .min("0.1"), .required)
}
LabeledContent(label: "Design Airflow: (CFM)") {