svZeroDSolver
Loading...
Searching...
No Matches
Solver.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
2// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
3/**
4 * @file Solver.h
5 * @brief Solver source file
6 */
7
8#include "Integrator.h"
9#include "Model.h"
11#include "State.h"
12#include "debug.h"
13
14#ifndef SVZERODSOLVER_SOLVE_SOLVER_HPP_
15#define SVZERODSOLVER_SOLVE_SOLVER_HPP_
16
17/**
18 * @brief Class for running 0D simulations.
19 *
20 * The solver solves for pressure and flow rate at the nodes of the
21 * lumped-parameter system \cite pfaller22. The lumped-parameter network is
22 * documented in SparseSystem. Refer to Integrator for the time discretization.
23 *
24 */
25class Solver {
26 public:
27 /**
28 * @brief Construct a new Solver object
29 *
30 * @param config Configuration handler
31 */
32 Solver(const nlohmann::json& config);
33
34 /**
35 * @brief Run the simulation
36 *
37 */
38 void run();
39
40 /**
41 * @brief Get the full result as a csv encoded string
42 *
43 * @return std::string Result
44 */
45 std::string get_full_result() const;
46
47 /**
48 * @brief Get the result of a single DOF over time
49 *
50 * @param dof_name Name of the degree-of-freedom
51 * @return Eigen::VectorXd Result
52 */
53 Eigen::VectorXd get_single_result(const std::string& dof_name) const;
54
55 /**
56 * @brief Get the result of a single DOF averaged over time
57 *
58 * @param dof_name Name of the degree-of-freedom
59 * @return double Result
60 */
61 double get_single_result_avg(const std::string& dof_name) const;
62
63 /**
64 * @brief Get the time steps of the result
65 *
66 * @return std::vector<double> Vector of times
67 */
68 std::vector<double> get_times() const;
69
70 /**
71 * @brief Update the parameters of a block
72 *
73 * @param block_name Name of the block
74 * @param new_params New parameters
75 */
76 void update_block_params(const std::string& block_name,
77 const std::vector<double>& new_params);
78
79 /**
80 * @brief Read the parameters of a block
81 *
82 * @param block_name Name of the block
83 *
84 * @return std::vector<double> Block parameters
85 */
86 std::vector<double> read_block_params(const std::string& block_name);
87
88 /**
89 * @brief Write the result to a csv file.
90 *
91 * @param filename
92 */
93 void write_result_to_csv(const std::string& filename) const;
94
95 private:
96 std::shared_ptr<Model> model;
97 SimulationParameters simparams;
98 std::vector<State> states;
99 std::vector<double> times;
100 State initial_state;
101
102 void sanity_checks();
103
104 /**
105 * @brief Get indices of flow and pressure degrees-of-freedom in solution
106 * vector for all vessel caps
107 *
108 * @return std::vector<std::pair<int, int>> Indices of flow and pressure
109 * degrees-of-freedom in solution vector for all vessel caps
110 */
111 std::vector<std::pair<int, int>> get_vessel_caps_dof_indices();
112
113 /**
114 * @brief Check if flows and pressures for all vessel caps have converged,
115 * based on cycle-to-cycle error for last two simulated cardiac cycles
116 *
117 * @param states_last_two_cycles Vector of solution states for last two
118 * simulated cardiac cycles
119 * @param vessel_caps_dof_indices Indices of flow and pressure
120 * degrees-of-freedom in solution vector for all vessel caps
121 *
122 * @return bool True if flows and pressures for all vessel caps have converged
123 */
124 bool check_vessel_cap_convergence(
125 const std::vector<State>& states_last_two_cycles,
126 const std::vector<std::pair<int, int>>& vessel_caps_dof_indices);
127
128 /**
129 * @brief Get cycle-to-cycle errors for flow and pressure for a single vessel
130 * cap
131 *
132 * @param states_last_two_cycles Vector of solution states for last two
133 * simulated cardiac cycles
134 * @param dof_indices Indices of flow and pressure degrees-of-freedom in
135 * solution vector for a single vessel cap
136 *
137 * @return std::pair<double, double> Cycle-to-cycle errors for flow and
138 * pressure
139 */
140 std::pair<double, double> get_cycle_to_cycle_errors_in_flow_and_pressure(
141 const std::vector<State>& states_last_two_cycles,
142 const std::pair<int, int>& dof_indices);
143};
144
145#endif
Integrator source file.
model::Model source file
Source file to read simulation configuration.
State source file.
std::vector< double > read_block_params(const std::string &block_name)
Read the parameters of a block.
Definition Solver.cpp:367
void run()
Run the simulation.
Definition Solver.cpp:43
Solver(const nlohmann::json &config)
Construct a new Solver object.
Definition Solver.cpp:8
void update_block_params(const std::string &block_name, const std::vector< double > &new_params)
Update the parameters of a block.
Definition Solver.cpp:343
double get_single_result_avg(const std::string &dof_name) const
Get the result of a single DOF averaged over time.
Definition Solver.cpp:331
std::vector< double > get_times() const
Get the time steps of the result.
Definition Solver.cpp:300
Eigen::VectorXd get_single_result(const std::string &dof_name) const
Get the result of a single DOF over time.
Definition Solver.cpp:319
void write_result_to_csv(const std::string &filename) const
Write the result to a csv file.
Definition Solver.cpp:386
std::string get_full_result() const
Get the full result as a csv encoded string.
Definition Solver.cpp:302
State of the system.
Definition State.h:19
DEBUG_MSG source file.
Simulation parameters.
Definition SimulationParameters.h:23