Skip to content

Commit

Permalink
Merge pull request #5945 from bangerth/avoid-stringstreams
Browse files Browse the repository at this point in the history
Avoid where possible to use std::ostringstream to generate filenames.
  • Loading branch information
tjhei committed Feb 22, 2018
2 parents 0149fea + da507ff commit ed953c6
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 154 deletions.
15 changes: 6 additions & 9 deletions examples/step-13/step-13.cc
Expand Up @@ -55,7 +55,6 @@
#include <iostream>
#include <fstream>
#include <list>
#include <sstream>

// The last step is as in all previous programs:
namespace Step13
Expand Down Expand Up @@ -392,13 +391,13 @@ namespace Step13
{}


// After the description above, the function generating the actual output
// Following the description above, the function generating the actual output
// is now relatively straightforward. The only particularly interesting
// feature over previous example programs is the use of the
// DataOutBase::default_suffix function, returning the usual
// suffix for files of a given format (e.g. ".eps" for encapsulated
// postscript files, ".gnuplot" for Gnuplot files), and of the generic
// <code>DataOut::write</code> function with a second argument, which
// DataOut::write() function with a second argument, which internally
// branches to the actual output functions for the different graphics
// formats, based on the value of the format descriptor passed as second
// argument.
Expand All @@ -418,12 +417,10 @@ namespace Step13
data_out.add_data_vector (solution, "solution");
data_out.build_patches ();

std::ostringstream filename;
filename << output_name_base << "-"
<< this->refinement_cycle
<< data_out.default_suffix (output_format)
<< std::ends;
std::ofstream out (filename.str().c_str());
std::ofstream out (output_name_base
+ "-"
+ std::to_string(this->refinement_cycle)
+ data_out.default_suffix (output_format));

data_out.write (out, output_format);
}
Expand Down
32 changes: 10 additions & 22 deletions examples/step-14/step-14.cc
Expand Up @@ -54,7 +54,6 @@
#include <list>
#include <algorithm>
#include <numeric>
#include <sstream>

// The last step is as in all previous programs:
namespace Step14
Expand Down Expand Up @@ -344,13 +343,10 @@ namespace Step14
GridOutput<dim>::operator () (const DoFHandler<dim> &dof_handler,
const Vector<double> &/*solution*/) const
{
std::ostringstream filename;
filename << output_name_base << "-"
<< this->refinement_cycle
<< ".eps"
<< std::ends;

std::ofstream out (filename.str().c_str());
std::ofstream out (output_name_base
+ "-"
+ std::to_string(this->refinement_cycle)
+ ".eps");
GridOut().write_eps (dof_handler.get_triangulation(), out);
}
}
Expand Down Expand Up @@ -802,13 +798,9 @@ namespace Step14
data_out.add_data_vector (this->solution, "solution");
data_out.build_patches ();

std::ostringstream filename;
filename << "solution-"
<< this->refinement_cycle
<< ".gnuplot"
<< std::ends;

std::ofstream out (filename.str().c_str());
std::ofstream out ("solution-"
+ std::to_string(this->refinement_cycle)
+ ".gnuplot");
data_out.write (out, DataOutBase::gnuplot);
}

Expand Down Expand Up @@ -2260,13 +2252,9 @@ namespace Step14

data_out.build_patches ();

std::ostringstream filename;
filename << "solution-"
<< this->refinement_cycle
<< ".gnuplot"
<< std::ends;

std::ofstream out (filename.str().c_str());
std::ofstream out ("solution-"
+ std::to_string(this->refinement_cycle)
+ ".gnuplot");
data_out.write (out, DataOutBase::gnuplot);
}

Expand Down
10 changes: 3 additions & 7 deletions examples/step-16/step-16.cc
Expand Up @@ -88,7 +88,6 @@
// This is C++:
#include <iostream>
#include <fstream>
#include <sstream>

using namespace dealii;

Expand Down Expand Up @@ -591,12 +590,9 @@ namespace Step16
data_out.add_data_vector (solution, "solution");
data_out.build_patches ();

std::ostringstream filename;
filename << "solution-"
<< cycle
<< ".vtk";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ std::to_string(cycle)
+ ".vtk");
data_out.write_vtk (output);
}

Expand Down
14 changes: 4 additions & 10 deletions examples/step-17/step-17.cc
Expand Up @@ -88,7 +88,6 @@
// And this is simply C++ again:
#include <fstream>
#include <iostream>
#include <sstream>

// The last step is as in all previous programs:
namespace Step17
Expand Down Expand Up @@ -941,17 +940,12 @@ namespace Step17

// This being done, process zero goes ahead with setting up the
// output file as in step-8, and attaching the (localized)
// solution vector to the output object. (The code to generate the
// output file name is stolen and slightly modified from step-5,
// since we expect that we can do a number of cycles greater than
// 10, which is the maximum of what the code in step-8 could
// handle.)
// solution vector to the output object.
if (this_mpi_process == 0)
{
std::ostringstream filename;
filename << "solution-" << cycle << ".vtk";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ std::to_string(cycle)
+ ".vtk");

DataOut<dim> data_out;
data_out.attach_dof_handler (dof_handler);
Expand Down
19 changes: 11 additions & 8 deletions examples/step-21/step-21.cc
Expand Up @@ -59,7 +59,6 @@

#include <iostream>
#include <fstream>
#include <sstream>

// In this program, we use a tensor-valued coefficient. Since it may have a
// spatial dependence, we consider it a tensor-valued function. The following
Expand Down Expand Up @@ -1086,7 +1085,14 @@ namespace Step21
// @sect4{TwoPhaseFlowProblem::output_results}

// There is nothing surprising here. Since the program will do a lot of time
// steps, we create an output file only every fifth time step.
// steps, we create an output file only every fifth time step and skip all
// other time steps at the top of the file already.
//
// When creating file names for output close to the bottom of the function,
// we convert the number of the time step to a string representation that
// is padded by leading zeros to four digits. We do this because this way
// all output file names have the same length, and consequently sort well
// when creating a directory listing.
template <int dim>
void TwoPhaseFlowProblem<dim>::output_results () const
{
Expand Down Expand Up @@ -1122,12 +1128,9 @@ namespace Step21

data_out.build_patches (degree+1);

std::ostringstream filename;
filename << "solution-"
<< Utilities::int_to_string(timestep_number,4)
<< ".vtk";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ Utilities::int_to_string(timestep_number,4)
+ ".vtk");
data_out.write_vtk (output);
}

Expand Down
14 changes: 5 additions & 9 deletions examples/step-22/step-22.cc
Expand Up @@ -67,7 +67,6 @@
#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>

// As in all programs, the namespace dealii is included:
namespace Step22
Expand Down Expand Up @@ -845,9 +844,9 @@ namespace Step22
// <code>DataComponentInterpretation</code> namespace: as with the filename,
// we create a vector in which the first <code>dim</code> components refer
// to the velocities and are given the tag
// <code>DataComponentInterpretation::component_is_part_of_vector</code>; we
// DataComponentInterpretation::component_is_part_of_vector; we
// finally push one tag
// <code>DataComponentInterpretation::component_is_scalar</code> to describe
// DataComponentInterpretation::component_is_scalar to describe
// the grouping of the pressure variable.

// The rest of the function is then the same as in step-20.
Expand All @@ -871,12 +870,9 @@ namespace Step22
data_component_interpretation);
data_out.build_patches ();

std::ostringstream filename;
filename << "solution-"
<< Utilities::int_to_string (refinement_cycle, 2)
<< ".vtk";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ Utilities::int_to_string(refinement_cycle, 2)
+ ".vtk");
data_out.write_vtk (output);
}

Expand Down
12 changes: 5 additions & 7 deletions examples/step-31/step-31.cc
Expand Up @@ -70,7 +70,6 @@
#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>
#include <limits>


Expand Down Expand Up @@ -1942,8 +1941,8 @@ namespace Step31
// using the DataComponentInterpretation helper class. Next, we actually
// attach the data vectors with their DoFHandler objects, build patches
// according to the degree of freedom, which are (sub-) elements that
// describe the data for visualization programs. Finally, we set a file name
// (that includes the time step number) and write the vtk file.
// describe the data for visualization programs. Finally, we open a file
// (that includes the time step number) and write the vtk data into it.
template <int dim>
void BoussinesqFlowProblem<dim>::output_results () const
{
Expand All @@ -1966,10 +1965,9 @@ namespace Step31
"T");
data_out.build_patches (std::min(stokes_degree, temperature_degree));

std::ostringstream filename;
filename << "solution-" << Utilities::int_to_string(timestep_number, 4) << ".vtk";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ Utilities::int_to_string(timestep_number,4)
+ ".vtk");
data_out.write_vtk (output);
}

Expand Down
29 changes: 11 additions & 18 deletions examples/step-37/step-37.cc
Expand Up @@ -58,7 +58,6 @@

#include <iostream>
#include <fstream>
#include <sstream>


namespace Step37
Expand Down Expand Up @@ -1129,29 +1128,23 @@ namespace Step37
data_out.add_data_vector (solution, "solution");
data_out.build_patches ();

std::ostringstream filename;
filename << "solution-"
<< cycle
<< "." << Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
<< ".vtu";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ std::to_string(cycle)
+ "."
+ std::to_string(Utilities::MPI::this_mpi_process(MPI_COMM_WORLD))
+ ".vtu");
data_out.write_vtu (output);

if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
{
std::vector<std::string> filenames;
for (unsigned int i=0; i<Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD); ++i)
{
std::ostringstream filename;
filename << "solution-"
<< cycle
<< "."
<< i
<< ".vtu";

filenames.push_back(filename.str().c_str());
}
filenames.emplace_back("solution-"
+ std::to_string(cycle)
+ "."
+ std::to_string(i)
+ ".vtu");

std::string master_name = "solution-" + Utilities::to_string(cycle) + ".pvtu";
std::ofstream master_output (master_name.c_str());
data_out.write_pvtu_record (master_output, filenames);
Expand Down
9 changes: 5 additions & 4 deletions examples/step-44/step-44.cc
Expand Up @@ -3384,10 +3384,11 @@ namespace Step44
MappingQEulerian<dim> q_mapping(degree, dof_handler_ref, soln);
data_out.build_patches(q_mapping, degree);

std::ostringstream filename;
filename << "solution-" << dim << "d-" << time.get_timestep() << ".vtk";

std::ofstream output(filename.str().c_str());
std::ofstream output ("solution-"
+ std::to_string(dim)
+ "d-"
+ std::to_string(time.get_timestep())
+ ".vtk");
data_out.write_vtk(output);
}

Expand Down
13 changes: 5 additions & 8 deletions examples/step-45/step-45.cc
Expand Up @@ -676,14 +676,11 @@ namespace Step45
data_out.add_data_vector (subdomain, "subdomain");
data_out.build_patches (mapping, degree+1);

std::ostringstream filename;
filename << "solution-"
<< Utilities::int_to_string (refinement_cycle, 2)
<< "."
<< Utilities::int_to_string (triangulation.locally_owned_subdomain(),2)
<< ".vtu";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ Utilities::int_to_string(refinement_cycle, 2)
+ "."
+ Utilities::int_to_string (triangulation.locally_owned_subdomain(),2)
+ ".vtu");
data_out.write_vtu (output);

if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
Expand Down
10 changes: 3 additions & 7 deletions examples/step-46/step-46.cc
Expand Up @@ -60,7 +60,6 @@

#include <iostream>
#include <fstream>
#include <sstream>


namespace Step46
Expand Down Expand Up @@ -910,12 +909,9 @@ namespace Step46
data_component_interpretation);
data_out.build_patches ();

std::ostringstream filename;
filename << "solution-"
<< Utilities::int_to_string (refinement_cycle, 2)
<< ".vtk";

std::ofstream output (filename.str().c_str());
std::ofstream output ("solution-"
+ Utilities::int_to_string(refinement_cycle, 2)
+ ".vtk");
data_out.write_vtk (output);
}

Expand Down

0 comments on commit ed953c6

Please sign in to comment.