svMultiPhysics
Loading...
Searching...
No Matches
OneDSolverInterface.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef ONEDSOLVER_INTERFACE_H
5#define ONEDSOLVER_INTERFACE_H
6
7#include <dlfcn.h>
8#include <string>
9#include <stdexcept>
10
11/// @brief Wrapper class for dynamically loading and calling the 1D solver shared library.
12///
13/// This class loads the svOneDSolver shared library and
14/// provides a C++ wrapper around its exported C interface.
15///
16/// The interface is used to couple 3D Navier-Stokes simulations in
17/// svMultiPhysics with reduced-order 1D blood flow models in
18/// svOneDSolver.
19///
20/// Supported coupling modes:
21/// - NEU coupling: 3D flow rate -> 1D model, 1D pressure -> 3D solver
22/// - DIR coupling: 3D pressure -> 1D model, 1D flow rate -> 3D solver
23///
24/// Each initialized 1D model is identified by a problem_id assigned
25/// by svOneDSolver and used in subsequent library calls.
26///
27/// Shared library functions:
28/// - initialize_1d(input_file, problem_id, system_size, coupling_type)
29/// - set_external_step_size_1d(problem_id, dt)
30/// - return_1d_solution(problem_id, solution, size)
31/// - update_1d_solution(problem_id, solution, size)
32/// - run_1d_simulation_step_1d(problem_id, time, save_flag, coupling_type,
33/// params, solution, cpl_value, error_code)
34/// - extract_coupled_dof(problem_id, coupled_dof, coupling_type)
35//
37 public:
38 OneDSolverInterface() = default;
40
41 /// @brief Load the 1D solver shared library from the given path.
42 void load_library(const std::string& interface_lib);
43
44 /// @brief Initialize the 1D solver from an input file.
45 ///
46 /// @param[in] input_file Path to the 1D solver .in file.
47 /// @param[out] problem_id Problem identifier assigned by the solver.
48 /// @param[out] system_size Total number of DOFs (nodes * 2: flow + area).
49 /// @param[in] coupling_type "NEU" or "DIR" coupling direction.
50 void initialize(const std::string& input_file, int& problem_id,
51 int& system_size, const std::string& coupling_type);
52
53 /// @brief Synchronize the 1D solver's internal time step with the 3D solver.
54 void set_external_step_size(int problem_id, double dt);
55
56 /// @brief Copy the current 1D solution into the caller-provided buffer.
57 void return_solution(int problem_id, double* solution, int size);
58
59 /// @brief Push a solution vector into the 1D solver as the current state.
60 void update_solution(int problem_id, double* solution, int size);
61
62 /// @brief Advance the 1D solver by one time step.
63 ///
64 /// @param[in] problem_id Problem identifier.
65 /// @param[in] save_incr VTK output interval (Increment_in_saving_VTK_files from solver.xml);
66 /// the 1D library decides internally whether to write output.
67 /// @param[in] coupling_type "NEU" or "DIR".
68 /// @param[in] params Array [N, t1, t2, ..., val1, val2, ...] where N=2.
69 /// @param[in,out] solution Solution vector updated after the step.
70 /// @param[out] cpl_value The BC value returned by the 1D solver
71 /// (pressure for NEU, flow for DIR).
72 /// @param[in] last_flag 'L' for the final (committed) iteration, 'D' for
73 /// derivative / predictor steps.
74 /// @param[out] error_code Non-zero on failure.
75 /// 0 indicates success. Any non-zero value is
76 /// interpreted by this wrapper as a 1D solver failure.
77 void run_simulation(int problem_id, double current_time, int save_incr,
78 const std::string& coupling_type, double* params,
79 double* solution, double& cpl_value, char last_flag,
80 int& error_code);
81
82 /// @brief Retrieve the index within the solution vector that corresponds to
83 /// the coupled boundary DOF.
84 void extract_coupled_dof(int problem_id, int& coupled_dof,
85 const std::string& coupling_type);
86
87
88 private:
89 /// @brief Problem identifier assigned by the 1D solver during initialization.
90 int problem_id_ = 0;
91 /// @brief Total number of DOFs (nodes * 2: flow + area) assigned by the 1D solver during initialization.
92 int system_size_ = 0;
93
94 void* library_handle_ = nullptr;
95
96 // Function pointers to shared-library symbols.
97
98 /// @brief Initialize the 1D solver
99 void (*initialize_1d_)(const char*, int&, int&, const char*) = nullptr;
100
101 /// @brief Set the 1D solver's internal time step size
102 void (*set_external_step_size_1d_)(int, double) = nullptr;
103
104 /// @brief Return the current 1D solution vector
105 void (*return_1d_solution_)(int, double*, int) = nullptr;
106
107 /// @brief Update the 1D solver's current solution vector
108 void (*update_1d_solution_)(int, double*, int) = nullptr;
109
110 /// @brief Advance the 1D solver by one 3D time step
111 void (*run_1d_simulation_step_1d_)(int, double, int, const char*, double*,
112 double*, double&, char*, int&) = nullptr;
113
114 /// @brief Retrieve the index within the solution vector that corresponds to
115 /// the coupled boundary DOF.
116 void (*extract_coupled_dof_)(int, int&, char*) = nullptr;
117};
118
119#endif // ONEDSOLVER_INTERFACE_H
Wrapper class for dynamically loading and calling the 1D solver shared library.
Definition OneDSolverInterface.h:36
void load_library(const std::string &interface_lib)
Load the 1D solver shared library from the given path.
Definition OneDSolverInterface.cpp:20
void update_solution(int problem_id, double *solution, int size)
Push a solution vector into the 1D solver as the current state.
Definition OneDSolverInterface.cpp:107
void run_simulation(int problem_id, double current_time, int save_incr, const std::string &coupling_type, double *params, double *solution, double &cpl_value, char last_flag, int &error_code)
Advance the 1D solver by one time step.
Definition OneDSolverInterface.cpp:120
void extract_coupled_dof(int problem_id, int &coupled_dof, const std::string &coupling_type)
Retrieve the index within the solution vector that corresponds to the coupled boundary DOF.
Definition OneDSolverInterface.cpp:160
void return_solution(int problem_id, double *solution, int size)
Copy the current 1D solution into the caller-provided buffer.
Definition OneDSolverInterface.cpp:94
void initialize(const std::string &input_file, int &problem_id, int &system_size, const std::string &coupling_type)
Initialize the 1D solver from an input file.
Definition OneDSolverInterface.cpp:59
void set_external_step_size(int problem_id, double dt)
Synchronize the 1D solver's internal time step with the 3D solver.
Definition OneDSolverInterface.cpp:81