Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
ImmersedBoundarySvgWriter.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "ImmersedBoundarySvgWriter.hpp"
37#include "ImmersedBoundaryMesh.hpp"
38
39template <unsigned DIM>
42 mSamplingMultiple(100u),
43 mSvgSize(1600.0),
44 mOutputDirectory(""),
45 mSvgHeader(""),
46 mSvgFooter("")
47{
48}
49
50template <unsigned DIM>
54
55template <unsigned DIM>
57 AbstractCellPopulation<DIM, DIM>& rCellPopulation)
58{
59 if (SimulationTime::Instance()->GetTimeStepsElapsed() % mSamplingMultiple == 0)
60 {
61 ImmersedBoundaryMesh<DIM, DIM> *p_mesh = static_cast<ImmersedBoundaryMesh<DIM, DIM> *>(&(rCellPopulation.rGetMesh()));
62
63 // Get the number of time steps elapsed to use in the file name, and zero-pad it
64 std::stringstream time;
65 time << std::setfill('0') << std::setw(6) << SimulationTime::Instance()->GetTimeStepsElapsed();
66
67 // Open the svg file for writing
68 std::string full_file_name = "results_" + time.str() + ".svg";
69 OutputFileHandler results_handler(mOutputDirectory, false);
70 out_stream svg_file = results_handler.OpenOutputFile(full_file_name);
71
72 (*svg_file) << mSvgHeader;
73
74 // Add all nodes to the svg file
75 double node_rad = p_mesh->GetAverageNodeSpacingOfElement(0, false) * 0.35 * mSvgSize;
76 for (auto it = p_mesh->GetNodeIteratorBegin();
77 it != p_mesh->GetNodeIteratorEnd();
78 ++it)
79 {
80 AddPointToSvgFile(svg_file, it->rGetLocation(), it->GetRegion(), node_rad);
81 }
82
83 (*svg_file) << mSvgFooter;
84
85 svg_file->close();
86 }
87}
88
89template <unsigned DIM>
91 AbstractCellPopulation<DIM, DIM>& rCellPopulation,
92 std::string outputDirectory)
93{
94 mOutputDirectory = outputDirectory;
95
96 // Define colours
97 std::string bg_col = "darkgray";
98 std::string region0_col = "#990000"; // dark red
99 std::string region1_col = "#cc0000"; // light red
100 std::string region2_col = "#e68a00"; // dark orange
101 std::string region3_col = "#ff9900"; // light orange
102 std::string region4_col = "#006666"; // dark teal
103 std::string region5_col = "#009999"; // light teal
104 std::string region6_col = "#000099"; // dark blue
105 std::string region7_col = "#0000cc"; // light blue
106 std::string region8_col = "#FFFFFF"; // white
107 std::string glyph0_col = "DarkRed"; // white
108 std::string glyph1_col = "DarkBlue"; // white
109 std::string glyph2_col = "DarkGreen"; // white
110
111 std::stringstream header;
112
113 // Set precision so as not to write out too many decimal places of uncompressed text
114 header << std::setprecision(5);
115
116 // Output svg header to file
117 header << "<svg version=\"1.1\" baseProfile=\"full\" width=\""
118 << mSvgSize <<"px\" height=\"" << mSvgSize << "px\" "
119 << "viewBox=\"0 0 " << mSvgSize << " " << mSvgSize << "\" "
120 << "xmlns=\"http://www.w3.org/2000/svg\">" << "\n";
121
122 // Add text/css style for elements
123 header << "<style type=\"text/css\">" << "\n"
124 << "\t.bg_rect{fill:" << bg_col << ";}" << "\n"
125 << "\t.node_0{fill:" << region0_col << ";}" << "\n"
126 << "\t.node_1{fill:" << region1_col << ";}" << "\n"
127 << "\t.node_2{fill:" << region2_col << ";}" << "\n"
128 << "\t.node_3{fill:" << region3_col << ";}" << "\n"
129 << "\t.node_4{fill:" << region4_col << ";}" << "\n"
130 << "\t.node_5{fill:" << region5_col << ";}" << "\n"
131 << "\t.node_6{fill:" << region6_col << ";}" << "\n"
132 << "\t.node_7{fill:" << region7_col << ";}" << "\n"
133 << "\t.node_8{fill:" << region8_col << ";}" << "\n"
134 << "\t.glyph_0{fill:" << glyph0_col << ";}" << "\n"
135 << "\t.glyph_1{fill:" << glyph1_col << ";}" << "\n"
136 << "\t.glyph_2{fill:" << glyph2_col << ";}" << "\n"
137 << "</style>" << "\n";
138
139 // Add background rectangle
140 header << "<rect class=\"bg_rect\" width=\"" << mSvgSize << "\" height=\"" << mSvgSize << "\"/>" << "\n";
141
142 mSvgHeader = header.str();
143 mSvgFooter = "</svg>\n";
144}
145
146template <unsigned DIM>
148 out_stream& rSvgFile,
149 c_vector<double, DIM> location,
150 unsigned region,
151 double rad)
152{
153 double scaled_x = location[0] * mSvgSize;
154 double scaled_y = (1.0 - location[1]) * mSvgSize;
155
156 (*rSvgFile) << "<circle class=\"node_" << region << "\" "
157 << "cx=\"" << scaled_x << "\" "
158 << "cy=\"" << scaled_y << "\" "
159 << "r=\"" << rad << "\"/>" << "\n";
160
161 // Account for possible wrap-around of glyph in x
162 if (scaled_x < rad)
163 {
164 (*rSvgFile) << "<circle class=\"node_" << region << "\" "
165 << "cx=\"" << scaled_x + mSvgSize << "\" "
166 << "cy=\"" << scaled_y << "\" "
167 << "r=\"" << rad << "\"/>" << "\n";
168 }
169 else if (scaled_x > mSvgSize - rad)
170 {
171 (*rSvgFile) << "<circle class=\"node_" << region << "\" "
172 << "cx=\"" << scaled_x - mSvgSize << "\" "
173 << "cy=\"" << scaled_y << "\" "
174 << "r=\"" << rad << "\"/>" << "\n";
175 }
176
177 // Account for possible wrap-around of glyph in y
178 if (scaled_y < rad)
179 {
180 (*rSvgFile) << "<circle class=\"node_" << region << "\" "
181 << "cx=\"" << scaled_x << "\" "
182 << "cy=\"" << scaled_y + mSvgSize << "\" "
183 << "r=\"" << rad << "\"/>" << "\n";
184 }
185 else if (scaled_y > mSvgSize - rad)
186 {
187 (*rSvgFile) << "<circle class=\"node_" << region << "\" "
188 << "cx=\"" << scaled_x << "\" "
189 << "cy=\"" << scaled_y - mSvgSize << "\" "
190 << "r=\"" << rad << "\"/>" << "\n";
191 }
192}
193
194template <unsigned DIM>
196 out_stream& rParamsFile)
197{
198 // Call method on direct parent class
200}
201
202template <unsigned DIM>
204{
205 return mSamplingMultiple;
206}
207
208template <unsigned DIM>
210 unsigned samplingMultiple)
211{
212 mSamplingMultiple = samplingMultiple;
213}
214
215template <unsigned DIM>
217{
218 return mSvgSize;
219}
220
221template <unsigned DIM>
223{
224 mSvgSize = svgSize;
225}
226
227// Explicit instantiation
228template class ImmersedBoundarySvgWriter<1>;
229template class ImmersedBoundarySvgWriter<2>;
230template class ImmersedBoundarySvgWriter<3>;
231
232// Serialization for Boost >= 1.36
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
virtual void OutputSimulationModifierParameters(out_stream &rParamsFile)=0
AbstractMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
NodeIterator GetNodeIteratorEnd()
NodeIterator GetNodeIteratorBegin(bool skipDeletedNodes=true)
double GetAverageNodeSpacingOfElement(unsigned index, bool recalculate=true)
void AddPointToSvgFile(out_stream &rSvgFile, c_vector< double, DIM > location, unsigned region, double rad)
virtual void SetupSolve(AbstractCellPopulation< DIM, DIM > &rCellPopulation, std::string outputDirectory)
void SetSamplingMultiple(unsigned samplingMultiple)
void OutputSimulationModifierParameters(out_stream &rParamsFile)
virtual void UpdateAtEndOfTimeStep(AbstractCellPopulation< DIM, DIM > &rCellPopulation)
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
static SimulationTime * Instance()
unsigned GetTimeStepsElapsed() const