OpenSWMM Engine  6.0.0-alpha.3
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.3)
Loading...
Searching...
No Matches
openswmm_api_common.hpp
Go to the documentation of this file.
1
12
13#ifndef OPENSWMM_API_COMMON_HPP
14#define OPENSWMM_API_COMMON_HPP
15
16#include "SWMMEngine.hpp"
17#include "UnitConversion.hpp"
19
20#include <cstring>
21#include <algorithm>
22
23static inline openswmm::SWMMEngine* to_engine(SWMM_Engine e) noexcept {
24 return static_cast<openswmm::SWMMEngine*>(e);
25}
26
27// ============================================================================
28// Unit conversion at the C API boundary
29// ============================================================================
30//
31// The engine stores all state in internal SWMM units (ft, cfs, ft³, ft/s, sec).
32// The public C API, like legacy swmm5.c, exposes values in the project's
33// configured *display* units (selected by [OPTIONS] FLOW_UNITS): getters return
34// display units, setters accept display units.
35//
36// getter: display = internal * UCF(q) -> to_display()
37// setter: internal = display * UCF_inv(q) -> to_internal()
38//
39// Setters multiply by the precomputed reciprocal (UCF_inv / Qcf_inv) rather than
40// dividing by UCF — no runtime division at any boundary site. `q` is a
41// openswmm::ucf::Quantity code (LENGTH, FLOW, VOLUME, LANDAREA, RAINFALL,
42// EVAPRATE, RAINDEPTH, ...). Templated on the context type so the include of the
43// full SimulationContext definition is deferred to the impl translation units.
44
46template <class Ctx>
47static inline double to_display(const Ctx& ctx, int q, double v) noexcept {
48 return v * openswmm::ucf::UCF(q, ctx.options);
49}
50
52template <class Ctx>
53static inline double to_internal(const Ctx& ctx, int q, double v) noexcept {
54 return v * openswmm::ucf::UCF_inv(q, ctx.options);
55}
56
57#define CHECK_HANDLE(e) do { if (!(e)) return SWMM_ERR_BADHANDLE; } while(0)
58#define CHECK_INDEX(cond) do { if (!(cond)) return SWMM_ERR_BADINDEX; } while(0)
59
60// ============================================================================
61// Lifecycle state guards
62// ============================================================================
63
65#define CHECK_GEOMETRY(ctx) \
66 do { \
67 if ((ctx).state != openswmm::EngineState::BUILDING && \
68 (ctx).state != openswmm::EngineState::OPENED) \
69 return SWMM_ERR_LIFECYCLE; \
70 } while(0)
71
77#define CHECK_EDITABLE(ctx) \
78 do { \
79 if ((ctx).state != openswmm::EngineState::BUILDING && \
80 (ctx).state != openswmm::EngineState::OPENED) \
81 return SWMM_ERR_LIFECYCLE; \
82 } while(0)
83
85#define CHECK_INITIAL_COND(ctx) \
86 do { \
87 if ((ctx).state != openswmm::EngineState::BUILDING && \
88 (ctx).state != openswmm::EngineState::OPENED && \
89 (ctx).state != openswmm::EngineState::INITIALIZED) \
90 return SWMM_ERR_LIFECYCLE; \
91 } while(0)
92
94#define CHECK_RUNNING(ctx) \
95 do { \
96 if ((ctx).state != openswmm::EngineState::RUNNING) \
97 return SWMM_ERR_LIFECYCLE; \
98 } while(0)
99
101#define CHECK_READABLE(ctx) \
102 do { \
103 if ((ctx).state == openswmm::EngineState::CREATED || \
104 (ctx).state == openswmm::EngineState::CLOSED || \
105 (ctx).state == openswmm::EngineState::ERROR_STATE) \
106 return SWMM_ERR_LIFECYCLE; \
107 } while(0)
108
109#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:23
double UCF_inv(int quantity, const SimulationOptions &opts)
Reciprocal of UCF — multiply display by this to get internal units.
Definition UnitConversion.cpp:37
void * SWMM_Engine
Opaque handle to an OpenSWMM Engine instance.
Definition openswmm_callbacks.h:35
OpenSWMM Engine — primary transparent C API (master header).