deal2lkit: A ToolKit library for Deal.II
parsed_data_out.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2015 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
17 
18 #include <deal.II/base/logstream.h>
20 #include <deal.II/base/utilities.h>
21 
23 
24 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
27 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
29 #include <deal.II/fe/mapping_q.h>
30 #include <deal.II/fe/fe.h>
31 #include <cstdio>
32 #include <iostream>
33 #include <fstream>
34 #include <vector>
35 #include <string>
36 
37 
38 #include <deal.II/base/utilities.h>
39 
40 
41 D2K_NAMESPACE_OPEN
42 
43 template <int dim, int spacedim>
45  const std::string &output_format,
46  const unsigned int &subdivisions,
47  const std::string &incremental_run_prefix,
48  const std::string &base_name_input,
49  const std::string &files_to_save,
50  const MPI_Comm &comm) :
51  ParameterAcceptor(name),
52  comm(comm),
55  output_format(output_format),
56  subdivisions(subdivisions),
57  base_name(base_name_input),
58  incremental_run_prefix(incremental_run_prefix),
59  files_to_save(files_to_save)
60 {
61  initialized = false;
62 }
63 
64 template <int dim, int spacedim>
66 {
67  add_parameter(prm, &base_name, "Problem base name", base_name, Patterns::Anything());
68 
70 
71  add_parameter(prm, &files_to_save, "Files to save in run directory", files_to_save, Patterns::Anything());
72 
73  add_parameter(prm, &output_partitioning, "Output partitioning", "false", Patterns::Bool());
74  add_parameter(prm, &solution_names, "Solution names", "u", Patterns::Anything(),
75  "Comma separated list of names for the components. If a "
76  "name is repeated, then the repeated names are grouped into "
77  "vectors.");
78 
79  add_parameter(prm, &output_format, "Output format", output_format,
81 
82  add_parameter(prm, &subdivisions, "Subdivisions", std::to_string(subdivisions),
84 
85 }
86 
87 template <int dim, int spacedim>
89 {
90  if ( incremental_run_prefix != "" )
91  {
93  // The use of the barrier is
94  // to avoid the case of a processor below the master node.
95 #ifdef DEAL_II_WITH_MPI
96  MPI_Barrier(comm);
97 #endif
98  if ( Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
100 #ifdef DEAL_II_WITH_MPI
101  MPI_Barrier(comm);
102 #endif
103  path_solution_dir += "/";
104  }
105  else
106  {
107  path_solution_dir = "./";
108  }
109 }
110 
111 template <int dim, int spacedim>
113  const std::string &suffix)
114 {
115  deallog.push("PrepareOutput");
116  data_out = SP(new DataOut<dim, DoFHandler<dim, spacedim> > ());
118 
120 
121  std::string fname = current_name;
122 
123  if (data_out->default_suffix() != "")
124  {
125 
126  // If the output is needed and we have many processes, just output
127  // the one we need *in intermediate format*.
128  if (n_mpi_processes > 1)
129  {
130  fname += ("." + Utilities::int_to_string(this_mpi_process, 2) +
132  data_out->default_suffix()) ;
133  }
134  else
135  {
136  fname += data_out->default_suffix();
137  }
138 
139  deallog << "Will write on file: " << fname.c_str() << std::endl;
140  output_file.open(fname.c_str());
142  data_out->attach_dof_handler (dh);
143 
144  if (n_mpi_processes > 1)
145  {
146  // Output the partitioning
148  {
149  deallog << "Writing partitioning" << std::endl;
150  Vector<float> partitioning(dh.get_triangulation().n_active_cells());
151  for (unsigned int i=0; i<partitioning.size(); ++i)
152  partitioning(i) = this_mpi_process;
153  static Vector<float> static_partitioning;
154  static_partitioning.swap(partitioning);
155  data_out->add_data_vector (static_partitioning, "partitioning");
156  }
157  }
158  }
159  initialized = true;
160  deallog.pop();
161 }
162 
163 
164 
165 template <int dim, int spacedim>
167 {
168  if (output_format=="none")
169  return;
170 
171  if (files_to_save!="")
172  {
173  std::vector<std::string> files;
175  for (unsigned int i = 0; i<files.size(); ++i)
176  {
177  if (this_mpi_process == 0)
178  copy_files(files[i], path_solution_dir);
179  }
180  }
183  deallog.push("WritingData");
184  if (data_out->default_suffix() != "")
185  {
186  data_out->build_patches(mapping, subdivisions,
187  DataOut<dim, DoFHandler<dim,spacedim> >::curved_inner_cells);
188  data_out->write(output_file);
189  deallog << "Wrote output file." << std::endl;
190 
191  if (this_mpi_process == 0 && n_mpi_processes > 1 && data_out->default_suffix() == ".vtu")
192  {
193  std::vector<std::string> filenames;
194  for (unsigned int i=0; i<n_mpi_processes; ++i)
195  filenames.push_back (current_name +
196  "." + Utilities::int_to_string(i, 2) +
197  "." + Utilities::int_to_string(n_mpi_processes, 2) +
198  data_out->default_suffix());
199 
200  std::ofstream master_output ((current_name + ".pvtu").c_str());
201  data_out->write_pvtu_record (master_output, filenames);
202  }
203  }
204  data_out = 0;
205  deallog << "Reset output." << std::endl;
206  initialized = false;
207  output_file.close();
208  deallog.pop();
209 }
210 
211 
212 D2K_NAMESPACE_CLOSE
213 
214 template class deal2lkit::ParsedDataOut<1,1>;
215 template class deal2lkit::ParsedDataOut<1,2>;
216 template class deal2lkit::ParsedDataOut<1,3>;
217 template class deal2lkit::ParsedDataOut<2,2>;
218 template class deal2lkit::ParsedDataOut<2,3>;
219 template class deal2lkit::ParsedDataOut<3,3>;
virtual void parse_parameters_call_back()
Prepare names for output directories.
std::string base_name
Base name for output files.
shared_ptr< DataOut< dim, DoFHandler< dim, spacedim > > > data_out
Outputs only the data that refers to this process.
std::string incremental_run_prefix
name of progressive directories.
std::string files_to_save
a list of file to save at each run in @ incremental_run_prefix directory.
A parameter acceptor base class.
static ParameterHandler prm
Static parameter.
static::ExceptionBase & ExcIO()
bool output_partitioning
Output the partitioning of the domain.
const unsigned int this_mpi_process
My mpi process.
std::string output_format
Output format.
const Triangulation< dim, spacedim > & get_triangulation() const
static::ExceptionBase & ExcNotInitialized()
#define AssertThrow(cond, exc)
shared_ptr< T > SP(T *t)
Construct a shared pointer to a non const class T.
Definition: utilities.h:483
const unsigned int n_mpi_processes
Number of processes.
unsigned int subdivisions
Number of subdivisions.
std::string get_next_available_directory_name(const std::string &base, int n_digits, unsigned int start, unsigned int index_max)
A function that return the name of the first non existing folder matching a pattern make by base and ...
Definition: utilities.cc:102
std::ofstream output_file
Output file.
bool initialized
Initialization flag.
std::string current_name
Current output name.
bool copy_files(const std::string &files, const std::string &destination)
A function to copy a list of file ( "file1 file2 file3" ) in the destination folder (destination) ...
Definition: utilities.cc:119
OutputFormat parse_output_format(const std::string &format_name)
std::string solution_names
Solution names.
std::string path_solution_dir
Folder where solutions are stored.
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
bool create_directory(const std::string &name)
A function to create directory.
Definition: utilities.cc:108
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
void write_data_and_clear(const Mapping< dim, spacedim > &mapping=StaticMappingQ1< dim, spacedim >::mapping)
Actually write the file.
virtual void swap(Vector< Number > &v)
void prepare_data_output(const DoFHandler< dim, spacedim > &dh, const std::string &suffix="")
Prepare to output data on the given file.
std::vector< std::string > split_string_list(const std::string &s, const char delimiter=',')
virtual void declare_parameters(ParameterHandler &prm)
Initialize the given values for the paramter file.
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
std::string get_output_format_names()
void add_parameter(ParameterHandler &prm, T *parameter, const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Add a parameter the given parameter list.
ParsedDataOut(const std::string &name="", const std::string &output_format="vtu", const unsigned int &subdivisions=1, const std::string &incremental_run_prefix="", const std::string &base_name_input="solution", const std::string &files_to_save="", const MPI_Comm &comm=MPI_COMM_WORLD)
Optional name for parameter section.
const MPI_Comm & comm
MPI communicator.