OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
openswmm_api_common.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
28
29#ifndef OPENSWMM_API_COMMON_HPP
30#define OPENSWMM_API_COMMON_HPP
31
32#include "SWMMEngine.hpp"
33#include "UnitConversion.hpp"
35
36#include <cstring>
37#include <algorithm>
38
39static inline openswmm::SWMMEngine* to_engine(SWMM_Engine e) noexcept {
40 return static_cast<openswmm::SWMMEngine*>(e);
41}
42
43// ============================================================================
44// Unit conversion at the C API boundary
45// ============================================================================
46//
47// The engine stores all state in internal SWMM units (ft, cfs, ft³, ft/s, sec).
48// The public C API, like legacy swmm5.c, exposes values in the project's
49// configured *display* units (selected by [OPTIONS] FLOW_UNITS): getters return
50// display units, setters accept display units.
51//
52// getter: display = internal * UCF(q) -> to_display()
53// setter: internal = display * UCF_inv(q) -> to_internal()
54//
55// Setters multiply by the precomputed reciprocal (UCF_inv / Qcf_inv) rather than
56// dividing by UCF — no runtime division at any boundary site. `q` is a
57// openswmm::ucf::Quantity code (LENGTH, FLOW, VOLUME, LANDAREA, RAINFALL,
58// EVAPRATE, RAINDEPTH, ...). Templated on the context type so the include of the
59// full SimulationContext definition is deferred to the impl translation units.
60
62template <class Ctx>
63static inline double to_display(const Ctx& ctx, int q, double v) noexcept {
64 return v * openswmm::ucf::UCF(q, ctx.options);
65}
66
68template <class Ctx>
69static inline double to_internal(const Ctx& ctx, int q, double v) noexcept {
70 return v * openswmm::ucf::UCF_inv(q, ctx.options);
71}
72
73#define CHECK_HANDLE(e) do { if (!(e)) return SWMM_ERR_BADHANDLE; } while(0)
74#define CHECK_INDEX(cond) do { if (!(cond)) return SWMM_ERR_BADINDEX; } while(0)
75
76// ============================================================================
77// Lifecycle state guards
78// ============================================================================
79
81#define CHECK_GEOMETRY(ctx) \
82 do { \
83 if ((ctx).state != openswmm::EngineState::BUILDING && \
84 (ctx).state != openswmm::EngineState::OPENED) \
85 return SWMM_ERR_LIFECYCLE; \
86 } while(0)
87
93#define CHECK_EDITABLE(ctx) \
94 do { \
95 if ((ctx).state != openswmm::EngineState::BUILDING && \
96 (ctx).state != openswmm::EngineState::OPENED) \
97 return SWMM_ERR_LIFECYCLE; \
98 } while(0)
99
101#define CHECK_INITIAL_COND(ctx) \
102 do { \
103 if ((ctx).state != openswmm::EngineState::BUILDING && \
104 (ctx).state != openswmm::EngineState::OPENED && \
105 (ctx).state != openswmm::EngineState::INITIALIZED) \
106 return SWMM_ERR_LIFECYCLE; \
107 } while(0)
108
110#define CHECK_RUNNING(ctx) \
111 do { \
112 if ((ctx).state != openswmm::EngineState::RUNNING) \
113 return SWMM_ERR_LIFECYCLE; \
114 } while(0)
115
117#define CHECK_READABLE(ctx) \
118 do { \
119 if ((ctx).state == openswmm::EngineState::CREATED || \
120 (ctx).state == openswmm::EngineState::CLOSED || \
121 (ctx).state == openswmm::EngineState::ERROR_STATE) \
122 return SWMM_ERR_LIFECYCLE; \
123 } while(0)
124
125#endif /* OPENSWMM_API_COMMON_HPP */
Global unit conversion factors — matching legacy SWMM Ucf[]/Qcf[].
double UCF(int quantity, const SimulationOptions &opts)
Get unit conversion factor for a quantity.
Definition UnitConversion.cpp:39
double UCF_inv(int quantity, const SimulationOptions &opts)
Reciprocal of UCF — multiply display by this to get internal units.
Definition UnitConversion.cpp:53
void * SWMM_Engine
Opaque handle to an OpenSWMM Engine instance.
Definition openswmm_callbacks.h:51
OpenSWMM Engine — primary transparent C API (master header).