Skip to content

Commit 4cc4e5b

Browse files
authored
Set fixed precision instead of buffer size (#175)
1 parent 064c07d commit 4cc4e5b

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

src/solve/csv_writer.cpp

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
33
#include "csv_writer.h"
44

5+
#include <iomanip>
6+
57
/**
68
* @brief Write results vessel based.
79
*
@@ -19,9 +21,8 @@ std::string to_vessel_csv(const std::vector<double> &times,
1921
// Create string stream to buffer output
2022
std::stringstream out;
2123

22-
// Create short and long buffer for lines
23-
char sbuff[140];
24-
char lbuff[236];
24+
// Set floating point format for the entire stream
25+
out << std::scientific << std::setprecision(16);
2526

2627
// Write column labels
2728
if (derivative) {
@@ -82,21 +83,20 @@ std::string to_vessel_csv(const std::vector<double> &times,
8283
d_outflow_mean /= num_steps;
8384
d_inpres_mean /= num_steps;
8485
d_outpres_mean /= num_steps;
85-
snprintf(
86-
lbuff, 236, "%s,,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e\n",
87-
name.c_str(), inflow_mean, outflow_mean, inpres_mean, outpres_mean,
88-
d_inflow_mean, d_outflow_mean, d_inpres_mean, d_outpres_mean);
89-
out << lbuff;
86+
87+
out << name << ",," << inflow_mean << "," << outflow_mean << ","
88+
<< inpres_mean << "," << outpres_mean << "," << d_inflow_mean << ","
89+
<< d_outflow_mean << "," << d_inpres_mean << "," << d_outpres_mean
90+
<< "\n";
9091
} else {
9192
for (size_t i = 0; i < num_steps; i++) {
92-
snprintf(lbuff, 236,
93-
"%s,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e,%.16e\n",
94-
name.c_str(), times[i], states[i].y[inflow_dof],
95-
states[i].y[outflow_dof], states[i].y[inpres_dof],
96-
states[i].y[outpres_dof], states[i].ydot[inflow_dof],
97-
states[i].ydot[outflow_dof], states[i].ydot[inpres_dof],
98-
states[i].ydot[outpres_dof]);
99-
out << lbuff;
93+
out << name << "," << times[i] << "," << states[i].y[inflow_dof]
94+
<< "," << states[i].y[outflow_dof] << ","
95+
<< states[i].y[inpres_dof] << "," << states[i].y[outpres_dof]
96+
<< "," << states[i].ydot[inflow_dof] << ","
97+
<< states[i].ydot[outflow_dof] << ","
98+
<< states[i].ydot[inpres_dof] << ","
99+
<< states[i].ydot[outpres_dof] << "\n";
100100
}
101101
}
102102
} else {
@@ -116,16 +116,15 @@ std::string to_vessel_csv(const std::vector<double> &times,
116116
outflow_mean /= num_steps;
117117
inpres_mean /= num_steps;
118118
outpres_mean /= num_steps;
119-
snprintf(sbuff, 140, "%s,,%.16e,%.16e,%.16e,%.16e\n", name.c_str(),
120-
inflow_mean, outflow_mean, inpres_mean, outpres_mean);
121-
out << sbuff;
119+
120+
out << name << ",," << inflow_mean << "," << outflow_mean << ","
121+
<< inpres_mean << "," << outpres_mean << "\n";
122122
} else {
123123
for (size_t i = 0; i < num_steps; i++) {
124-
snprintf(sbuff, 140, "%s,%.16e,%.16e,%.16e,%.16e,%.16e\n",
125-
name.c_str(), times[i], states[i].y[inflow_dof],
126-
states[i].y[outflow_dof], states[i].y[inpres_dof],
127-
states[i].y[outpres_dof]);
128-
out << sbuff;
124+
out << name << "," << times[i] << "," << states[i].y[inflow_dof]
125+
<< "," << states[i].y[outflow_dof] << ","
126+
<< states[i].y[inpres_dof] << "," << states[i].y[outpres_dof]
127+
<< "\n";
129128
}
130129
}
131130
}
@@ -151,9 +150,8 @@ std::string to_variable_csv(const std::vector<double> &times,
151150
// Create string stream to buffer output
152151
std::stringstream out;
153152

154-
// Create short and long buffer for lines
155-
char sbuff[87];
156-
char lbuff[110];
153+
// Set floating point format for the entire stream
154+
out << std::scientific << std::setprecision(16);
157155

158156
// Determine number of time steps
159157
int num_steps = times.size();
@@ -173,17 +171,15 @@ std::string to_variable_csv(const std::vector<double> &times,
173171
}
174172
mean_y /= num_steps;
175173
mean_ydot /= num_steps;
176-
snprintf(lbuff, 110, "%s,,%.16e,%.16e\n", name.c_str(), mean_y,
177-
mean_ydot);
178-
out << lbuff;
174+
175+
out << name << ",," << mean_y << "," << mean_ydot << "\n";
179176
}
180177
} else {
181178
for (size_t i = 0; i < model.dofhandler.size(); i++) {
182179
std::string name = model.dofhandler.variables[i];
183180
for (size_t j = 0; j < num_steps; j++) {
184-
snprintf(lbuff, 110, "%s,%.16e,%.16e,%.16e\n", name.c_str(), times[j],
185-
states[j].y[i], states[j].ydot[i]);
186-
out << lbuff;
181+
out << name << "," << times[j] << "," << states[j].y[i] << ","
182+
<< states[j].ydot[i] << "\n";
187183
}
188184
}
189185
}
@@ -197,20 +193,18 @@ std::string to_variable_csv(const std::vector<double> &times,
197193
mean_y += states[j].y[i];
198194
}
199195
mean_y /= num_steps;
200-
snprintf(sbuff, 87, "%s,,%.16e\n", name.c_str(), mean_y);
201-
out << sbuff;
196+
197+
out << name << ",," << mean_y << "\n";
202198
}
203199
} else {
204200
for (size_t i = 0; i < model.dofhandler.size(); i++) {
205201
std::string name = model.dofhandler.variables[i];
206202
for (size_t j = 0; j < num_steps; j++) {
207-
snprintf(sbuff, 87, "%s,%.16e,%.16e\n", name.c_str(), times[j],
208-
states[j].y[i]);
209-
out << sbuff;
203+
out << name << "," << times[j] << "," << states[j].y[i] << "\n";
210204
}
211205
}
212206
}
213207
}
214208

215209
return out.str();
216-
}
210+
}

0 commit comments

Comments
 (0)