OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
CvodeSurfaceSolver.hpp
Go to the documentation of this file.
1
27#ifndef OPENSWMM_ENGINE_2D_CVODE_SURFACE_SOLVER_HPP
28#define OPENSWMM_ENGINE_2D_CVODE_SURFACE_SOLVER_HPP
29
30#include "../data/MeshData.hpp"
31#include "../data/SurfaceStateData.hpp"
32#include "../data/SolverOptions2D.hpp"
33
34#ifdef OPENSWMM_HAS_2D
35
36#include <vector>
37
38// Forward declarations for SUNDIALS types (avoid pulling in full headers)
39struct SUNContext_;
40typedef struct SUNContext_* SUNContext;
41typedef struct _generic_N_Vector* N_Vector;
42typedef struct _generic_SUNLinearSolver* SUNLinearSolver;
43
44namespace openswmm::twoD {
45
46class CvodeSurfaceSolver;
47
59struct CvodeSolverContext {
60 MeshData* mesh = nullptr;
61 SurfaceStateData* state = nullptr;
62 SolverOptions2D* opts = nullptr;
63 CvodeSurfaceSolver* solver = nullptr;
64};
65
69class CvodeSurfaceSolver {
70public:
71 CvodeSurfaceSolver() = default;
72 ~CvodeSurfaceSolver();
73
74 // Non-copyable
75 CvodeSurfaceSolver(const CvodeSurfaceSolver&) = delete;
76 CvodeSurfaceSolver& operator=(const CvodeSurfaceSolver&) = delete;
77
78 // Movable
79 CvodeSurfaceSolver(CvodeSurfaceSolver&& o) noexcept;
80 CvodeSurfaceSolver& operator=(CvodeSurfaceSolver&& o) noexcept;
81
92 void initialize(MeshData& mesh, SurfaceStateData& state,
93 SolverOptions2D& opts);
94
106 double advance(double t_current, double t_target);
107
115 void reinitialize(double t0);
116
120 void finalize();
121
123 long last_num_steps() const noexcept { return last_nsteps_; }
124
126 double last_step_size() const noexcept { return last_h_; }
127
129 bool is_initialized() const noexcept { return cvode_mem_ != nullptr; }
130
131private:
132 void* cvode_mem_ = nullptr;
133 SUNLinearSolver ls_ = nullptr;
134 N_Vector y_ = nullptr;
135 SUNContext sun_ctx_ = nullptr;
136
137 CvodeSolverContext ctx_;
138
139 long last_nsteps_ = 0;
140 double last_h_ = 0.0;
141
146 std::vector<double> precond_diag_;
147
148 // ------------------------------------------------------------------
149 // SUNDIALS callbacks. All three have C linkage requirements imposed
150 // by SUNDIALS' function-pointer typedefs; we expose them as static
151 // member functions to keep them inside the class scope.
152 // ------------------------------------------------------------------
153
160 static int rhs_fn(double t, N_Vector y, N_Vector ydot, void* user_data);
161
173 static int psetup_fn(double t, N_Vector y, N_Vector fy,
174 int jok, int* jcurPtr, double gamma,
175 void* user_data);
176
183 static int psolve_fn(double t, N_Vector y, N_Vector fy,
184 N_Vector r, N_Vector z,
185 double gamma, double delta, int lr,
186 void* user_data);
187};
188
189} // namespace openswmm::twoD
190
191#endif // OPENSWMM_HAS_2D
192
193#endif // OPENSWMM_ENGINE_2D_CVODE_SURFACE_SOLVER_HPP
Definition NodeCoupling.cpp:15
double * y
Definition odesolve.c:28