Chaste  Build::
AbstractStructuralAdaptationSolver.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 <fstream>
37 #include <algorithm>
38 #include <iostream>
39 #include "AbstractStructuralAdaptationSolver.hpp"
40 #include "BaseUnits.hpp"
41 
42 template<unsigned DIM>
44  : mTolerance(1.e-4),
45  mTimeIncrement(1.e-4 * unit::seconds),
46  mReferenceTimeScale(BaseUnits::Instance()->GetReferenceTimeScale()),
47  mWriteOutput(false),
48  mOutputFileName(),
49  mMaxIterations(1.e5),
50  mpVesselNetwork()
51 {
52 
53 }
54 
55 template<unsigned DIM>
57 {
58 
59 }
60 
61 template<unsigned DIM>
63 {
64  return mTolerance;
65 }
66 
67 template<unsigned DIM>
69 {
70  return mWriteOutput;
71 }
72 
73 template<unsigned DIM>
75 {
76  return mOutputFileName;
77 }
78 
79 template<unsigned DIM>
81 {
82  return mTimeIncrement;
83 }
84 
85 template<unsigned DIM>
87 {
88  mTolerance = tolerance;
89 }
90 
91 template<unsigned DIM>
92 void AbstractStructuralAdaptationSolver<DIM>::SetTimeIncrement(units::quantity<unit::time> timeIncrement)
93 {
94  mTimeIncrement = timeIncrement;
95 }
96 
97 template<unsigned DIM>
99 {
100  mMaxIterations = iterations;
101 }
102 
103 template<unsigned DIM>
105 {
106  mWriteOutput = writeFlag;
107 }
108 
109 template<unsigned DIM>
111 {
112  mOutputFileName = filename;
113 }
114 
115 template<unsigned DIM>
117 {
118  std::ofstream out;
119 
120  double max_radius_relative_change = 1.0;
121  unsigned iteration = 0;
122  units::quantity<unit::time> time = 0.0 * unit::seconds;
123 
124  if (mWriteOutput && !mOutputFileName.empty())
125  {
126  out.open(mOutputFileName.c_str());
127  out << "\n";
128  out << "#Iteration Maximum relative change in radius in network\n\n";
129  }
130 
131  std::vector<boost::shared_ptr<VesselSegment<DIM> > > segments = mpVesselNetwork->GetVesselSegments();
132  std::vector<units::quantity<unit::length> > previous_radii(segments.size());
133  for (unsigned segment_index = 0; segment_index < segments.size(); segment_index++)
134  {
135  previous_radii[segment_index] = segments[segment_index]->GetRadius();
136  }
137 
138  while (max_radius_relative_change > mTolerance && time < (SimulationTime::Instance()->GetTimeStep()*mReferenceTimeScale) && iteration < mMaxIterations)
139  {
140  time += mTimeIncrement;
141  iteration++;
142 
143  Iterate();
144 
145  std::vector<double> relative_change(segments.size());
146  for (unsigned segment_index = 0; segment_index < segments.size(); segment_index++)
147  {
148  units::quantity<unit::length> current_radius = segments[segment_index]->GetRadius();
149  relative_change[segment_index] = fabs(1.0 - current_radius / previous_radii[segment_index]);
150  previous_radii[segment_index] = current_radius;
151  }
152 
153  max_radius_relative_change = *(std::max_element(relative_change.begin(), relative_change.end()));
154  if (out.is_open())
155  {
156  out << std::setw(6) << iteration << std::setw(20) << max_radius_relative_change << "\n";
157  }
158  }
159  if (out.is_open())
160  {
161  out.close();
162  }
163 }
164 
165 template<unsigned DIM>
167 {
168  mpVesselNetwork = pNetwork;
169 }
170 
171 template<unsigned DIM>
173 {
174  if(!mOutputFileName.empty())
175  {
176  std::ofstream out;
177  out.open(mOutputFileName.c_str(), std::ios::app); // append to file
178  out << "\nModule: AbstractStructuralAdaptationSolver\n";
179  out << "\n---------------\n";
180  out.close();
181  }
182 }
183 
184 // Explicit instantiation
void SetOutputFileName(const std::string &rFilename)
void SetVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pNetwork)
void SetTimeIncrement(units::quantity< unit::time > timeIncrement)
units::quantity< unit::time > GetTimeIncrement() const
boost::shared_ptr< VesselNetwork< DIM > > mpVesselNetwork