Chaste  Build::
FlowSolver.cpp
1 /*
2 
3  Copyright (c) 2005-2015, University of Oxford.
4  All rights reserved.
5 
6  University of Oxford means the Chancellor, Masters and Scholars of the
7  University of Oxford, having an administrative office at Wellington
8  Square, Oxford OX1 2JD, UK.
9 
10  This file is part of Chaste.
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34  */
35 
36 #include <algorithm>
37 #include "Exception.hpp"
38 #include "ReplicatableVector.hpp"
39 #include "PetscTools.hpp"
40 #include "VesselSegment.hpp"
41 #include "FlowSolver.hpp"
42 #include "UnitCollection.hpp"
43 #include "VesselNetworkGraphCalculator.hpp"
44 
45 template<unsigned DIM>
47  : mNodes(),
48  mVessels(),
49  mpVesselNetwork(),
50  mNodeVesselConnectivity(),
51  mNodeNodeConnectivity(),
52  mBoundaryConditionNodeIndices(),
53  mUnconnectedNodeIndices(),
54  mpLinearSystem(),
55  mUseDirectSolver(true),
56  mIsSetUp(false)
57 {
58 
59 }
60 
61 template<unsigned DIM>
63 {
64 
65 }
66 
67 template <unsigned DIM>
68 boost::shared_ptr<FlowSolver<DIM> > FlowSolver<DIM>::Create()
69 {
70  MAKE_PTR(FlowSolver<DIM>, pSelf);
71  return pSelf;
72 }
73 
74 template<unsigned DIM>
76 {
77  if(!mpVesselNetwork)
78  {
79  EXCEPTION("A vessel network is required before calling SetUp");
80  }
81  mVessels = mpVesselNetwork->GetVessels();
82  mNodes = mpVesselNetwork->GetVesselEndNodes();
83 
84  unsigned num_nodes = mNodes.size();
85  unsigned max_branches = mpVesselNetwork->GetMaxBranchesOnNode();
86 
87  // Set up the system
88  mpLinearSystem = boost::shared_ptr<LinearSystem>(new LinearSystem(num_nodes, max_branches + 1));
89 
90  // Note: If the network is small the preconditioner is turned off in LinearSystem,
91  // so an iterative solver is used instead.
92  if (num_nodes >= 6 && mUseDirectSolver)
93  {
94  mpLinearSystem->SetPcType("lu");
95  mpLinearSystem->SetKspType("preonly");
96  }
97 
98  // Get the boundary condition nodes
99  std::vector<boost::shared_ptr<VesselNode<DIM> > > boundary_condition_nodes;
100  for (unsigned node_index = 0; node_index < num_nodes; node_index++)
101  {
102  if (mNodes[node_index]->GetFlowProperties()->IsInputNode()
103  || mNodes[node_index]->GetFlowProperties()->IsOutputNode())
104  {
105  boundary_condition_nodes.push_back(mNodes[node_index]);
106  mBoundaryConditionNodeIndices.push_back(node_index);
107  }
108  }
109 
110  // Get the nodes that correspond to segments that are not connected to the rest of the network
111  boost::shared_ptr<VesselNetworkGraphCalculator<DIM> > p_graph_calculator = VesselNetworkGraphCalculator<DIM>::Create();
112  p_graph_calculator->SetVesselNetwork(mpVesselNetwork);
113  mNodeVesselConnectivity = p_graph_calculator->GetNodeVesselConnectivity();
114  mNodeNodeConnectivity = p_graph_calculator->GetNodeNodeConnectivity();
115 
116  std::vector<bool> connected = p_graph_calculator->IsConnected(boundary_condition_nodes, mNodes);
117  mUnconnectedNodeIndices.clear();
118  for (unsigned node_index = 0; node_index < num_nodes; node_index++)
119  {
120  if (!connected[node_index])
121  {
122  mUnconnectedNodeIndices.push_back(node_index);
123  }
124  }
125 
126  std::cout << "num_connected" << mUnconnectedNodeIndices.size() << std::endl;
127  mIsSetUp = true;
128  Update(false);
129 }
130 
131 template<unsigned DIM>
132 void FlowSolver<DIM>::SetUseDirectSolver(bool useDirectSolver)
133 {
134  mUseDirectSolver = useDirectSolver;
135 }
136 
137 template<unsigned DIM>
138 void FlowSolver<DIM>::SetVesselNetwork(boost::shared_ptr<VesselNetwork<DIM> > pVesselNetwork)
139 {
140  mpVesselNetwork = pVesselNetwork;
141 }
142 
143 template<unsigned DIM>
144 void FlowSolver<DIM>::Update(bool runSetup)
145 {
146  if(!mIsSetUp or runSetup)
147  {
148  SetUp();
149  }
150 
151  mpLinearSystem->SwitchWriteModeLhsMatrix();
152  mpLinearSystem->ZeroLhsMatrix();
153  mpLinearSystem->ZeroRhsVector();
154 
155  // Get the impedances, scale them by the maximum impedance to remove small values from the system matrix
156  std::vector<units::quantity<unit::flow_impedance> > scaled_impedances;
157  units::quantity<unit::flow_impedance> max_impedance = 0.0 * unit::pascal_second_per_metre_cubed;
158  units::quantity<unit::flow_impedance> min_impedance = DBL_MAX * unit::pascal_second_per_metre_cubed;
159  for (unsigned vessel_index = 0; vessel_index < mVessels.size(); vessel_index++)
160  {
161  units::quantity<unit::flow_impedance> impedance = mVessels[vessel_index]->GetFlowProperties()->GetImpedance();
162  if (impedance <= 0.0 * unit::pascal_second_per_metre_cubed)
163  {
164  EXCEPTION("Impedance should be a positive number.");
165  }
166  if(impedance > max_impedance)
167  {
168  max_impedance = impedance;
169  }
170  if(impedance < min_impedance)
171  {
172  min_impedance = impedance;
173  }
174  scaled_impedances.push_back(impedance);
175  }
176  units::quantity<unit::flow_impedance> multiplier = (max_impedance + min_impedance) / 2.0; //scale impedances to avoid floating point problems in PETSC solvers.
177 
178  // Set up the system matrix
179  for (unsigned node_index = 0; node_index < mNodes.size(); node_index++)
180  {
181  bool is_bc_node = (std::find(mBoundaryConditionNodeIndices.begin(), mBoundaryConditionNodeIndices.end(),
182  node_index) != mBoundaryConditionNodeIndices.end());
183  bool is_unconnected_node = (std::find(mUnconnectedNodeIndices.begin(), mUnconnectedNodeIndices.end(),
184  node_index) != mUnconnectedNodeIndices.end());
185 
186  if (is_bc_node or is_unconnected_node)
187  {
188  mpLinearSystem->AddToMatrixElement(node_index, node_index, 1.0);
189  if(mNodes[node_index]->GetFlowProperties()->UseVelocityBoundaryCondition())
190  {
191  // Velocity BC: Assumes only single vessel at inlets
192  mpLinearSystem->AddToMatrixElement(node_index, mNodeNodeConnectivity[node_index][0], -1.0);
193  }
194 
195  }
196  else
197  {
198  for (unsigned vessel_index = 0; vessel_index < mNodeVesselConnectivity[node_index].size(); vessel_index++)
199  {
200  units::quantity<unit::flow_impedance> impedance = scaled_impedances[mNodeVesselConnectivity[node_index][vessel_index]];
201  // Add the inverse impedances to the linear system
202 
203  mpLinearSystem->AddToMatrixElement(node_index, node_index, -multiplier / impedance); // Aii
204  mpLinearSystem->AddToMatrixElement(node_index, mNodeNodeConnectivity[node_index][vessel_index], multiplier / impedance); // Aij
205  }
206  }
207  }
208 
209  mpLinearSystem->AssembleIntermediateLinearSystem();
210  // Update the RHS
211 
212  for (unsigned bc_index = 0; bc_index < mBoundaryConditionNodeIndices.size(); bc_index++)
213  {
214  if(mNodes[mBoundaryConditionNodeIndices[bc_index]]->GetFlowProperties()->UseVelocityBoundaryCondition())
215  {
216  boost::shared_ptr<Vessel<DIM> > p_vessel = mNodes[mBoundaryConditionNodeIndices[bc_index]]->GetSegment(0)->GetVessel();
217  units::quantity<unit::flow_rate> flow_rate = boost::units::fabs(p_vessel->GetFlowProperties()->GetFlowRate());
218  units::quantity<unit::flow_impedance> impedance = p_vessel->GetFlowProperties()->GetImpedance();
219  double pressure_drop = flow_rate * impedance/ unit::pascals;
220  mpLinearSystem->SetRhsVectorElement(mBoundaryConditionNodeIndices[bc_index], pressure_drop);
221  }
222  else
223  {
224  mpLinearSystem->SetRhsVectorElement(
226  mNodes[mBoundaryConditionNodeIndices[bc_index]]->GetFlowProperties()->GetPressure()/unit::pascals);
227  }
228  }
229 }
230 
231 template<unsigned DIM>
233 {
234  if (!mIsSetUp)
235  {
236  SetUp();
237  }
238 
239  // Assemble and solve the final system
240  mpLinearSystem->AssembleFinalLinearSystem();
241  Vec solution = PetscTools::CreateVec(mNodes.size());
242  solution = mpLinearSystem->Solve();
243 
244  // Recover the pressure of the vessel nodes
245  ReplicatableVector a(solution);
246 
247  std::cout << "****************" << std::endl;
248  for (unsigned node_index = 0; node_index < mNodes.size(); node_index++)
249  {
250  std::cout << "pressure" << a[node_index] << std::endl;
251  mNodes[node_index]->GetFlowProperties()->SetPressure(a[node_index] * unit::pascals);
252  }
253  std::cout << "****************" << std::endl;
254 
255  // Set the segment flow rates and nodal pressures
256  for (unsigned vessel_index = 0; vessel_index < mVessels.size(); vessel_index++)
257  {
258  units::quantity<unit::pressure> start_node_pressure = mVessels[vessel_index]->GetStartNode()->GetFlowProperties()->GetPressure();
259  units::quantity<unit::pressure> end_node_pressure = mVessels[vessel_index]->GetEndNode()->GetFlowProperties()->GetPressure();
260  units::quantity<unit::flow_rate> flow_rate = (start_node_pressure - end_node_pressure) / mVessels[vessel_index]->GetFlowProperties()->GetImpedance();
261 
262  // Clean up small values as some structural adaptation calculators are sensitive to them.
263  if (fabs(flow_rate) < pow(10, -20)*unit::metre_cubed_per_second)
264  {
265  flow_rate = 0.0 * unit::metre_cubed_per_second;
266  }
267 
268  std::vector<boost::shared_ptr<VesselSegment<DIM> > > segments = mVessels[vessel_index]->GetSegments();
269  units::quantity<unit::pressure> pressure = start_node_pressure;
270  for (unsigned segment_index = 0; segment_index < segments.size() - 1; segment_index++)
271  {
272  pressure -= segments[segment_index]->GetFlowProperties()->GetImpedance() * flow_rate;
273  segments[segment_index]->GetNode(1)->GetFlowProperties()->SetPressure(pressure);
274  segments[segment_index]->GetFlowProperties()->SetFlowRate(flow_rate);
275  }
276  segments[segments.size() - 1]->GetFlowProperties()->SetFlowRate(flow_rate);
277  }
278 
279  // Clean up
280  PetscTools::Destroy(solution);
281 }
282 
283 // Explicit instantiation
284 template class FlowSolver<2> ;
285 template class FlowSolver<3> ;
286 
std::vector< boost::shared_ptr< VesselNode< DIM > > > mNodes
Definition: FlowSolver.hpp:61
std::vector< unsigned > mBoundaryConditionNodeIndices
Definition: FlowSolver.hpp:87
void SetUp()
Definition: FlowSolver.cpp:75
void SetUseDirectSolver(bool useDirectSolver)
Definition: FlowSolver.cpp:132
boost::shared_ptr< LinearSystem > mpLinearSystem
Definition: FlowSolver.hpp:97
static boost::shared_ptr< FlowSolver< DIM > > Create()
Definition: FlowSolver.cpp:68
std::vector< std::vector< unsigned > > mNodeVesselConnectivity
Definition: FlowSolver.hpp:77
boost::shared_ptr< VesselNetwork< DIM > > mpVesselNetwork
Definition: FlowSolver.hpp:72
std::vector< unsigned > mUnconnectedNodeIndices
Definition: FlowSolver.hpp:92
static boost::shared_ptr< VesselNetworkGraphCalculator< DIM > > Create()
std::vector< boost::shared_ptr< Vessel< DIM > > > mVessels
Definition: FlowSolver.hpp:67
bool mUseDirectSolver
Definition: FlowSolver.hpp:102
void SetVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pVesselNetwork)
Definition: FlowSolver.cpp:138
void Solve()
Definition: FlowSolver.cpp:232
std::vector< std::vector< unsigned > > mNodeNodeConnectivity
Definition: FlowSolver.hpp:82
void Update(bool runSetup=false)
Definition: FlowSolver.cpp:144