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 /// Set up and initialize the simulation parameters and model
35 void setup_initial();
36
37 /// Set up integrator
38 void setup_integrator();
39
40 /// Run the integration
41 void run_integration();
42
43 /// Run the simulation
44 void run();
45
46 /**
47 * @brief Get the full result as a csv encoded string
48 *
49 * @return std::string Result
50 */
51 std::string get_full_result() const;
52
53 /**
54 * @brief Get the result of a single DOF over time
55 *
56 * @param dof_name Name of the degree-of-freedom
57 * @return Eigen::VectorXd Result
58 */
59 Eigen::VectorXd get_single_result(const std::string& dof_name) const;
60
61 /**
62 * @brief Get the result of a single DOF averaged over time
63 *
64 * @param dof_name Name of the degree-of-freedom
65 * @return double Result
66 */
67 double get_single_result_avg(const std::string& dof_name) const;
68
69 /**
70 * @brief Get the time steps of the result
71 *
72 * @return std::vector<double> Vector of times
73 */
74 std::vector<double> get_times() const;
75
76 /**
77 * @brief Update the parameters of a block
78 *
79 * @param block_name Name of the block
80 * @param new_params New parameters
81 */
82 void update_block_params(const std::string& block_name,
83 const std::vector<double>& new_params);
84
85 /**
86 * @brief Read the parameters of a block
87 *
88 * @param block_name Name of the block
89 *
90 * @return std::vector<double> Block parameters
91 */
92 std::vector<double> read_block_params(const std::string& block_name);
93
94 /**
95 * @brief Write the result to a csv file.
96 *
97 * @param filename
98 */
99 void write_result_to_csv(const std::string& filename) const;
100
101 private:
102 std::shared_ptr<Model> model;
103 SimulationParameters simparams;
104 std::vector<State> states;
105 State initial_state;
106 State state;
107 std::vector<double> times;
108 double time;
109 Integrator integrator;
110
111 void sanity_checks();
112
113 /**
114 * @brief Get indices of flow and pressure degrees-of-freedom in solution
115 * vector for all vessel caps
116 *
117 * @return std::vector<std::pair<int, int>> Indices of flow and pressure
118 * degrees-of-freedom in solution vector for all vessel caps
119 */
120 std::vector<std::pair<int, int>> get_vessel_caps_dof_indices();
121
122 /**
123 * @brief Check if flows and pressures for all vessel caps have converged,
124 * based on cycle-to-cycle error for last two simulated cardiac cycles
125 *
126 * @param states_last_two_cycles Vector of solution states for last two
127 * simulated cardiac cycles
128 * @param vessel_caps_dof_indices Indices of flow and pressure
129 * degrees-of-freedom in solution vector for all vessel caps
130 *
131 * @return bool True if flows and pressures for all vessel caps have converged
132 */
133 bool check_vessel_cap_convergence(
134 const std::vector<State>& states_last_two_cycles,
135 const std::vector<std::pair<int, int>>& vessel_caps_dof_indices);
136
137 /**
138 * @brief Get cycle-to-cycle errors for flow and pressure for a single vessel
139 * cap
140 *
141 * @param states_last_two_cycles Vector of solution states for last two
142 * simulated cardiac cycles
143 * @param dof_indices Indices of flow and pressure degrees-of-freedom in
144 * solution vector for a single vessel cap
145 *
146 * @return std::pair<double, double> Cycle-to-cycle errors for flow and
147 * pressure
148 */
149 std::pair<double, double> get_cycle_to_cycle_errors_in_flow_and_pressure(
150 const std::vector<State>& states_last_two_cycles,
151 const std::pair<int, int>& dof_indices);
152};
153
154#endif
Integrator source file.
model::Model source file
Source file to read simulation configuration.
State source file.
Generalized-alpha integrator.
Definition Integrator.h:26
std::vector< double > read_block_params(const std::string &block_name)
Read the parameters of a block.
Definition Solver.cpp:406
void run()
Run the simulation.
Definition Solver.cpp:245
void setup_integrator()
Set up integrator.
Definition Solver.cpp:85
void run_integration()
Run the integration.
Definition Solver.cpp:111
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:382
void setup_initial()
Set up and initialize the simulation parameters and model.
Definition Solver.cpp:59
double get_single_result_avg(const std::string &dof_name) const
Get the result of a single DOF averaged over time.
Definition Solver.cpp:370
std::vector< double > get_times() const
Get the time steps of the result.
Definition Solver.cpp:339
Eigen::VectorXd get_single_result(const std::string &dof_name) const
Get the result of a single DOF over time.
Definition Solver.cpp:358
void write_result_to_csv(const std::string &filename) const
Write the result to a csv file.
Definition Solver.cpp:425
std::string get_full_result() const
Get the full result as a csv encoded string.
Definition Solver.cpp:341
State of the system.
Definition State.h:19
DEBUG_MSG source file.
Simulation parameters.
Definition SimulationParameters.h:37