-
Notifications
You must be signed in to change notification settings - Fork 37
CRL Vessel #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
CRL Vessel #202
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b6d5db3
Updated ignore
rjrios915 c6ac68d
Merge branch 'master' of https://github.com/rjrios915/svZeroDSolver
rjrios915 cf67a82
CRL Branch
rjrios915 df6a4f2
clang
rjrios915 a30d2dd
updated Doc and Clang
rjrios915 001cf29
Merge branch 'SimVascular:master' into CRLVessel
rjrios915 db50063
Merge remote-tracking branch 'origin/master' into pr-202
ncdorn ded953b
reformat jsons
ncdorn 9ccb2f5
clean up files
ncdorn d2330f3
update license header
ncdorn eb237f8
clean up changes
ncdorn 0ab5cfc
rebase CMakeLists.txt
ncdorn 42686bd
Merge remote-tracking branch 'origin/master' into pr-202
ncdorn ff61b12
clean up tests
ncdorn 956d507
update CRL test
ncdorn df1bf56
rename CRL test
ncdorn cee9758
exclude pulsatile_CRL from visualizer tests
ncdorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the | ||
| // University of California, and others. SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| #include "BloodVesselCRL.h" | ||
|
|
||
| void BloodVesselCRL::setup_dofs(DOFHandler& dofhandler) { | ||
| Block::setup_dofs_(dofhandler, 2, {}); | ||
| } | ||
|
|
||
| void BloodVesselCRL::update_constant(SparseSystem& system, | ||
| std::vector<double>& parameters) { | ||
| // Get parameters | ||
| double capacitance = parameters[global_param_ids[ParamId::CAPACITANCE]]; | ||
| double inductance = parameters[global_param_ids[ParamId::INDUCTANCE]]; | ||
| double resistance = parameters[global_param_ids[ParamId::RESISTANCE]]; | ||
|
|
||
| // Set element contributions | ||
| // coeffRef args are the indices (i,j) of the matrix | ||
| // global_eqn_ids: number of rows in the matrix, set in setup_dofs | ||
| // global_var_ids: number of columns, organized as pressure and flow of all | ||
| // inlets and then all outlets of the block | ||
| system.E.coeffRef(global_eqn_ids[0], global_var_ids[3]) = -inductance; | ||
| system.E.coeffRef(global_eqn_ids[1], global_var_ids[0]) = -capacitance; | ||
| system.F.coeffRef(global_eqn_ids[0], global_var_ids[0]) = 1.0; | ||
| system.F.coeffRef(global_eqn_ids[0], global_var_ids[3]) = -resistance; | ||
| system.F.coeffRef(global_eqn_ids[0], global_var_ids[2]) = -1.0; | ||
| system.F.coeffRef(global_eqn_ids[1], global_var_ids[1]) = 1.0; | ||
| system.F.coeffRef(global_eqn_ids[1], global_var_ids[3]) = -1.0; | ||
| } | ||
|
|
||
| void BloodVesselCRL::update_solution( | ||
| SparseSystem& system, std::vector<double>& parameters, | ||
| const Eigen::Matrix<double, Eigen::Dynamic, 1>& y, | ||
| const Eigen::Matrix<double, Eigen::Dynamic, 1>& dy) { | ||
| // Get parameters | ||
| double capacitance = parameters[global_param_ids[ParamId::CAPACITANCE]]; | ||
| double stenosis_coeff = | ||
| parameters[global_param_ids[ParamId::STENOSIS_COEFFICIENT]]; | ||
| double q_out = y[global_var_ids[3]]; | ||
| double dq_out = dy[global_var_ids[3]]; | ||
| double stenosis_resistance = stenosis_coeff * fabs(q_out); | ||
|
|
||
| // Set element contributions | ||
| system.C(global_eqn_ids[0]) = stenosis_resistance * -q_out; | ||
|
|
||
| double sgn_q_out = (0.0 < q_out) - (q_out < 0.0); | ||
| system.dC_dy.coeffRef(global_eqn_ids[0], global_var_ids[1]) = | ||
| stenosis_coeff * sgn_q_out * -2.0 * q_out; | ||
| } | ||
|
|
||
| void BloodVesselCRL::update_gradient( | ||
| Eigen::SparseMatrix<double>& jacobian, | ||
| Eigen::Matrix<double, Eigen::Dynamic, 1>& residual, | ||
| Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha, std::vector<double>& y, | ||
| std::vector<double>& dy) { | ||
| auto y0 = y[global_var_ids[0]]; | ||
| auto y1 = y[global_var_ids[1]]; | ||
| auto y2 = y[global_var_ids[2]]; | ||
| auto y3 = y[global_var_ids[3]]; | ||
|
|
||
| auto dy0 = dy[global_var_ids[0]]; | ||
| auto dy1 = dy[global_var_ids[1]]; | ||
| auto dy3 = dy[global_var_ids[3]]; | ||
|
|
||
| auto resistance = alpha[global_param_ids[ParamId::RESISTANCE]]; | ||
| auto capacitance = alpha[global_param_ids[ParamId::CAPACITANCE]]; | ||
| auto inductance = alpha[global_param_ids[ParamId::INDUCTANCE]]; | ||
| double stenosis_coeff = 0.0; | ||
|
|
||
| if (global_param_ids.size() > 3) { | ||
| stenosis_coeff = alpha[global_param_ids[ParamId::STENOSIS_COEFFICIENT]]; | ||
| } | ||
| auto stenosis_resistance = stenosis_coeff * fabs(y3); | ||
|
|
||
| jacobian.coeffRef(global_eqn_ids[0], global_param_ids[0]) = -y3; | ||
| jacobian.coeffRef(global_eqn_ids[0], global_param_ids[2]) = -dy3; | ||
|
|
||
| if (global_param_ids.size() > 3) { | ||
| jacobian.coeffRef(global_eqn_ids[0], global_param_ids[3]) = -fabs(y3) * y3; | ||
| } | ||
|
|
||
| jacobian.coeffRef(global_eqn_ids[1], global_param_ids[1]) = -dy0; | ||
|
|
||
| residual(global_eqn_ids[0]) = | ||
| y0 - (resistance + stenosis_resistance) * y3 - y2 - inductance * dy3; | ||
| residual(global_eqn_ids[1]) = y1 - y3 - capacitance * dy0; | ||
| } |
ncdorn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the | ||
| // University of California, and others. SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| /** | ||
| * @file BloodVesselCRL.h | ||
| * @brief model::BloodVesselCRL source file | ||
| */ | ||
| #ifndef SVZERODSOLVER_MODEL_BLOODVESSELCRL_HPP_ | ||
| #define SVZERODSOLVER_MODEL_BLOODVESSELCRL_HPP_ | ||
|
|
||
| #include <math.h> | ||
|
|
||
| #include "Block.h" | ||
| #include "SparseSystem.h" | ||
|
|
||
| /** | ||
| * @brief Capacitor-resistor-inductor blood vessel with optional stenosis | ||
| * | ||
| * Models the mechanical behavior of a bloodvesselCRL with optional stenosis. | ||
| * | ||
| * \f[ | ||
| * \begin{circuitikz} \draw | ||
| * node[left] {$Q_{in}$} [-latex] (0,0) -- (0.8,0); | ||
| * \draw (1,0) node[anchor=south]{$P_{in}$} | ||
| * to [R, l=$R$, *-] (3,0) | ||
| * to [R, l=$S$, -] (5,0) | ||
| * (5,0) to [L, l=$L$, -*] (7,0) | ||
| * node[anchor=south]{$P_{out}$} | ||
| * (1,0) to [C, l=$C$, -] (1,-1.5) | ||
| * node[ground]{}; | ||
| * \draw [-latex] (7.2,0) -- (8,0) node[right] {$Q_{out}$}; | ||
| * \end{circuitikz} | ||
| * \f] | ||
| * | ||
| * ### Governing equations | ||
| * | ||
| * \f[ | ||
| * P_\text{in}-P_\text{out} - (R + S|Q_\text{out}|) Q_\text{out}-L | ||
| * \dot{Q}_\text{out}=0 \f] | ||
| * | ||
| * \f[ | ||
| * Q_\text{in}-Q_\text{out} - C \dot{P}_\text{in}=0 \f] | ||
| * | ||
| * ### Local contributions | ||
| * | ||
| * \f[ | ||
| * \mathbf{y}^{e}=\left[\begin{array}{llll}P_{i n} & Q_{in} & | ||
| * P_{out} & Q_{out}\end{array}\right]^\text{T} \f] | ||
| * | ||
| * \f[ | ||
| * \mathbf{F}^{e}=\left[\begin{array}{cccc} | ||
| * 1 & 0 & -1 & -R \\ | ||
| * 0 & 1 & 0 & -1 | ||
| * \end{array}\right] | ||
| * \f] | ||
| * | ||
| * \f[ | ||
| * \mathbf{E}^{e}=\left[\begin{array}{cccc} | ||
| * 0 & 0 & 0 & -L \\ | ||
| * -C & 0 & 0 & 0 | ||
| * \end{array}\right] | ||
| * \f] | ||
| * | ||
| * \f[ | ||
| * \mathbf{c}^{e} = S|Q_\text{out}| | ||
| * \left[\begin{array}{c} | ||
| * -Q_\text{out} \\ | ||
| * 0 | ||
| * \end{array}\right] | ||
| * \f] | ||
| * | ||
| * \f[ | ||
| * \left(\frac{\partial\mathbf{c}}{\partial\mathbf{y}}\right)^{e} = | ||
| * S \text{sgn} (Q_\text{in}) | ||
| * \left[\begin{array}{cccc} | ||
| * 0 & -2Q_\text{out} & 0 & 0 \\ | ||
| * 0 & 0 & 0 & 0 | ||
| * \end{array}\right] | ||
| * \f] | ||
| * | ||
| * \f[ | ||
| * \left(\frac{\partial\mathbf{c}}{\partial\dot{\mathbf{y}}}\right)^{e} = | ||
| * S|Q_\text{out}| | ||
| * \left[\begin{array}{cccc} | ||
| * 0 & 0 & 0 & 0 \\ | ||
| * 0 & 0 & 0 & 0 | ||
| * \end{array}\right] | ||
| * \f] | ||
| * | ||
| * with the stenosis resistance \f$ S=K_{t} \frac{\rho}{2 | ||
| * A_{o}^{2}}\left(\frac{A_{o}}{A_{s}}-1\right)^{2} \f$. | ||
| * \f$R\f$, \f$C\f$, and \f$L\f$ refer to | ||
| * Poisieuille resistance, capacitance and inductance, respectively. | ||
| * | ||
| * ### Gradient | ||
| * | ||
| * Gradient of the equations with respect to the parameters: | ||
| * | ||
| * \f[ | ||
| * \mathbf{J}^{e} = \left[\begin{array}{cccc} | ||
| * -y_3 & 0 & -\dot{y}_3 & -|y_3|y_3 \\ | ||
| * 0 & 0 & -\dot{y}_0 & 0 \\ | ||
| * \end{array}\right] | ||
| * \f] | ||
| * | ||
| * ### Parameters | ||
| * | ||
| * Parameter sequence for constructing this block | ||
| * | ||
| * * `0` Poiseuille resistance | ||
| * * `1` Capacitance | ||
| * * `2` Inductance | ||
| * * `3` Stenosis coefficient | ||
| * | ||
| */ | ||
| class BloodVesselCRL : public Block { | ||
| public: | ||
| /** | ||
| * @brief Local IDs of the parameters | ||
| * | ||
| */ | ||
| enum ParamId { | ||
| RESISTANCE = 0, | ||
| CAPACITANCE = 1, | ||
| INDUCTANCE = 2, | ||
| STENOSIS_COEFFICIENT = 3, | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Construct a new BloodVesselCRL object | ||
| * | ||
| * @param id Global ID of the block | ||
| * @param model The model to which the block belongs | ||
| */ | ||
| BloodVesselCRL(int id, Model* model) | ||
| : Block(id, model, BlockType::blood_vessel_CRL, BlockClass::vessel, | ||
| {{"R_poiseuille", InputParameter()}, | ||
| {"C", InputParameter(true)}, | ||
| {"L", InputParameter(true)}, | ||
| {"stenosis_coefficient", InputParameter(true)}}) {} | ||
|
|
||
| /** | ||
| * @brief Set up the degrees of freedom (DOF) of the block | ||
| * | ||
| * Set \ref global_var_ids and \ref global_eqn_ids of the element based on the | ||
| * number of equations and the number of internal variables of the | ||
| * element. | ||
| * | ||
| * @param dofhandler Degree-of-freedom handler to register variables and | ||
| * equations at | ||
| */ | ||
| void setup_dofs(DOFHandler& dofhandler); | ||
|
|
||
| /** | ||
| * @brief Update the constant contributions of the element in a sparse | ||
| system | ||
| * | ||
| * @param system System to update contributions at | ||
| * @param parameters Parameters of the model | ||
| */ | ||
| void update_constant(SparseSystem& system, std::vector<double>& parameters); | ||
|
|
||
| /** | ||
| * @brief Update the solution-dependent contributions of the element in a | ||
| * sparse system | ||
| * | ||
| * @param system System to update contributions at | ||
| * @param parameters Parameters of the model | ||
| * @param y Current solution | ||
| * @param dy Current derivate of the solution | ||
| */ | ||
| void update_solution(SparseSystem& system, std::vector<double>& parameters, | ||
| const Eigen::Matrix<double, Eigen::Dynamic, 1>& y, | ||
| const Eigen::Matrix<double, Eigen::Dynamic, 1>& dy); | ||
|
|
||
| /** | ||
| * @brief Set the gradient of the block contributions with respect to the | ||
| * parameters | ||
| * | ||
| * @param jacobian Jacobian with respect to the parameters | ||
| * @param alpha Current parameter vector | ||
| * @param residual Residual with respect to the parameters | ||
| * @param y Current solution | ||
| * @param dy Time-derivative of the current solution | ||
| */ | ||
| void update_gradient(Eigen::SparseMatrix<double>& jacobian, | ||
| Eigen::Matrix<double, Eigen::Dynamic, 1>& residual, | ||
| Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha, | ||
| std::vector<double>& y, std::vector<double>& dy); | ||
|
|
||
| /** | ||
| * @brief Number of triplets of element | ||
| * | ||
| * Number of triplets that the element contributes to the global system | ||
| * (relevant for sparse memory reservation) | ||
| */ | ||
| TripletsContributions num_triplets{5, 3, 2}; | ||
| }; | ||
|
|
||
| #endif // SVZERODSOLVER_MODEL_BLOODVESSELCRL_HPP_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
|
|
||
| #include "Block.h" | ||
| #include "BloodVessel.h" | ||
| #include "BloodVesselCRL.h" | ||
| #include "SparseSystem.h" | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.