Flow123d  jenkins-Flow123d-linux-release-multijob-282
output_time.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id: output.cc 2505 2013-09-13 14:52:27Z jiri.hnidek $
21  * $Revision: 2505 $
22  * $LastChangedBy: jiri.hnidek $
23  * $LastChangedDate: 2013-09-13 16:52:27 +0200 (Pá, 13 IX 2013) $
24  *
25  * @file output.cc
26  * @brief The functions for all outputs (methods of classes: Output and OutputTime).
27  *
28  */
29 
30 #include <string>
31 
32 #include "system/sys_profiler.hh"
33 #include "mesh/mesh.h"
34 #include "input/accessors.hh"
35 #include "output_time.impl.hh"
36 #include "output_vtk.hh"
37 #include "output_msh.hh"
38 
39 
40 using namespace Input::Type;
41 
43  = Record("OutputStream", "Parameters of output.")
44  // The stream
46  "File path to the connected output file.")
47  // The format
49  "Format of output stream and possible parameters.")
50  .declare_key("time_step", Double(0.0),
51  "Time interval between outputs.\n"
52  "Regular grid of output time points starts at the initial time of the equation and ends at the end time which must be specified.\n"
53  "The start time and the end time are always added. ")
54  .declare_key("time_list", Array(Double(0.0)),
55  Default::read_time("List containing the initial time of the equation. \n You can prescribe an empty list to override this behavior."),
56  "Explicit array of output time points (can be combined with 'time_step'.")
57  .declare_key("add_input_times", Bool(), Default("false"),
58  "Add all input time points of the equation, mentioned in the 'input_fields' list, also as the output points.");
59 
60 
62  = AbstractRecord("OutputTime",
63  "Format of output stream and possible parameters.");
64 
65 
67 : _mesh(nullptr)
68 {
69 
70 }
71 
72 
73 
75 : input_record_(in_rec)
76 {
77 
78  this->_base_filename = in_rec.val<FilePath>("file");
79  this->current_step = 0;
80  this->_mesh = NULL;
81  this->time = -1.0;
82  this->write_time = -1.0;
83 
84 
86 
87 }
88 
89 
90 
92 {
93  /* It's possible now to do output to the file only in the first process */
94  //if(rank != 0) {
95  // /* TODO: do something, when support for Parallel VTK is added */
96  // return;
97  // }
98 
99  if (this->_base_file.is_open()) this->_base_file.close();
100 
101  xprintf(MsgLog, "O.K.\n");
102 }
103 
104 
105 
106 
107 
108 
109 
110 void OutputTime::fix_main_file_extension(std::string extension)
111 {
112  if(this->_base_filename.compare(this->_base_filename.size()-extension.size(), extension.size(), extension) != 0) {
113  string new_name = this->_base_filename + extension;
114  xprintf(Warn, "Renaming output file: %s to %s\n",
115  this->_base_filename.c_str(), new_name.c_str());
116  this->_base_filename = new_name;
117  }
118 }
119 
120 
121 /* Initialize static member of the class */
122 //std::vector<OutputTime*> OutputTime::output_streams;
123 
124 
125 /*
126 void OutputTime::destroy_all(void)
127 {
128  // Delete all objects
129  for(std::vector<OutputTime*>::iterator ot_iter = OutputTime::output_streams.begin();
130  ot_iter != OutputTime::output_streams.end();
131  ++ot_iter)
132  {
133  delete *ot_iter;
134  }
135 
136  OutputTime::output_streams.clear();
137 }
138  */
139 
140 
142 {
143  OutputTime* output_time;
144 
146 
147  if(format) {
148  if((*format).type() == OutputVTK::input_type) {
149  output_time = new OutputVTK(in_rec);
150  } else if ( (*format).type() == OutputMSH::input_type) {
151  output_time = new OutputMSH(in_rec);
152  } else {
153  xprintf(Warn, "Unsupported file format, using default VTK\n");
154  output_time = new OutputVTK(in_rec);
155  }
156  output_time->format_record_ = *format;
157  } else {
158  output_time = new OutputVTK(in_rec);
159  }
160 
161  return output_time;
162 }
163 
164 
166 {
167  vector<Input::FullEnum> field_ids;
168  in_array.copy_to(field_ids);
169 
170  for (auto field_full_enum: field_ids) {
171  /* Setting flags to zero means use just discrete space
172  * provided as default in the field.
173  */
174  DiscreteSpaceFlags flags = 0;
175  this->output_names[(std::string)field_full_enum]=flags;
176  }
177 }
178 
179 
180 
181 
183 {
184  TimeMark::Type output_mark_type = tg.equation_fixed_mark_type() | tg.marks().type_output();
185 
186  double time_step;
187  if (input_record_.opt_val("time_step", time_step)) {
188  tg.add_time_marks_grid(time_step, output_mark_type);
189  }
190 
191  Input::Array time_list;
192  if (input_record_.opt_val("time_list", time_list)) {
194  time_list.copy_to(list);
195  for( double time : list) tg.marks().add(TimeMark(time, output_mark_type));
196  } else {
197  tg.marks().add( TimeMark(tg.init_time(), output_mark_type) );
198  }
199 
200  bool add_flag;
201  if (input_record_.opt_val("add_input_times", add_flag) && add_flag) {
202  TimeMark::Type input_mark_type = tg.equation_mark_type() | tg.marks().type_input();
203  vector<double> mark_times;
204  // can not add marks while iterating through time marks
205  for(auto it = tg.marks().begin(input_mark_type); it != tg.marks().end(input_mark_type); ++it)
206  mark_times.push_back(it->time());
207  for(double time : mark_times)
208  tg.marks().add( TimeMark(time, output_mark_type) );
209 
210  }
211 
212 }
213 
214 
216 {
217  START_TIMER("write_time_frame");
218  /* TODO: do something, when support for Parallel VTK is added */
219  if (this->rank == 0) {
220  // Write data to output stream, when data registered to this output
221  // streams were changed
222  if(write_time < time) {
223  xprintf(MsgLog, "Write output to output stream: %s for time: %f\n",
224  this->_base_filename.c_str(), time);
225  write_data();
226  // Remember the last time of writing to output stream
227  write_time = time;
228  current_step++;
229  } else {
230  xprintf(MsgLog, "Skipping output stream: %s in time: %f\n",
231  this->_base_filename.c_str(), time);
232  }
233  }
234  clear_data();
235 }
236 
238 {
239  for(auto &map : output_data_vec_) map.clear();
240 }
241 
242 
243 
244 #define INSTANCE_register_field(spacedim, value) \
245  template void OutputTime::register_data<spacedim, value> \
246  (const DiscreteSpace ref_type, Field<spacedim, value> &field);
247 
248 #define INSTANCE_register_multifield(spacedim, value) \
249  template void OutputTime::register_data<spacedim, value> \
250  (const DiscreteSpace ref_type, MultiField<spacedim, value> &field);
251 
252 
253 #define INSTANCE_OutputData(spacedim, value) \
254  template class OutputData<value>;
255 
256 
257 #define INSTANCE_DIM_DEP_VALUES( MACRO, dim_from, dim_to) \
258  MACRO(dim_from, FieldValue<dim_to>::VectorFixed ) \
259  MACRO(dim_from, FieldValue<dim_to>::TensorFixed )
260 
261 #define INSTANCE_TO_ALL( MACRO, dim_from) \
262  MACRO(dim_from, FieldValue<0>::Enum ) \
263  MACRO(dim_from, FieldValue<0>::EnumVector) \
264  MACRO(dim_from, FieldValue<0>::Integer) \
265  MACRO(dim_from, FieldValue<0>::Scalar) \
266  MACRO(dim_from, FieldValue<0>::Vector) \
267  INSTANCE_DIM_DEP_VALUES(MACRO, dim_from, 2) \
268  INSTANCE_DIM_DEP_VALUES(MACRO, dim_from, 3) \
269 
270 #define INSTANCE_ALL(MACRO) \
271  INSTANCE_TO_ALL( MACRO, 3) \
272  INSTANCE_TO_ALL( MACRO, 2)
273 
274 
277 
278 //INSTANCE_TO_ALL( INSTANCE_OutputData, 0)
279 
280 
281 //INSTANCE_register_field(3, FieldValue<0>::Scalar)
282 //INSTANCE_register_multifield(3, FieldValue<0>::Scalar)
283 //INSTANCE_OutputData(3, FieldValue<0>::Scalar)
double time
Definition: output_time.hh:190
Mesh * _mesh
Definition: output_time.hh:222
unsigned long int Type
Definition: time_marks.hh:64
static Input::Type::AbstractRecord input_format_type
The specification of output file format.
Definition: output_time.hh:62
Input::Record input_record_
Definition: output_time.hh:207
#define INSTANCE_register_multifield(spacedim, value)
Definition: output_time.cc:248
Accessor to input data conforming to declared Array.
Definition: accessors.hh:558
void fix_main_file_extension(std::string extension)
Definition: output_time.cc:110
TimeMarks::iterator begin(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the begin mimics container-like of TimeMarks.
Definition: time_marks.cc:163
static Input::Type::Record input_type
The specification of output stream.
Definition: output_time.hh:57
void add_admissible_field_names(const Input::Array &in_array)
Registers names of output fields that can be written using this stream.
Definition: output_time.cc:165
#define INSTANCE_ALL(MACRO)
Definition: output_time.cc:270
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:39
Class for declaration of the input of type Bool.
Definition: type_base.hh:332
static Input::Type::Record input_type
The definition of input record for gmsh file format.
Definition: output_msh.hh:60
TimeMark::Type type_output()
Definition: time_marks.hh:203
static Default obligatory()
Definition: type_record.hh:89
???
Iterator< Ret > find(const string &key) const
OutputTime()
Default constructor. Only for testing.
Definition: output_time.cc:66
Basic time management functionality for unsteady (and steady) solvers (class Equation).
static TimeMarks & marks()
Definition: system.hh:72
Class for declaration of inputs sequences.
Definition: type_base.hh:239
static Input::Type::Record input_type
The definition of input record for vtk file format.
Definition: output_vtk.hh:65
std::map< std::string, DiscreteSpaceFlags > output_names
Definition: output_time.hh:202
unsigned int DiscreteSpaceFlags
Definition: output_time.hh:201
static Default optional()
Definition: type_record.hh:102
void add(const TimeMark &mark)
Definition: time_marks.cc:88
double init_time() const
bool opt_val(const string &key, Ret &value) const
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:392
static OutputTime * create_output_stream(const Input::Record &in_rec)
This method delete all object instances of class OutputTime stored in output_streams vector...
Definition: output_time.cc:141
Definition: system.hh:72
int current_step
Definition: output_time.hh:185
void add_time_marks_grid(double step, TimeMark::Type mark_type=TimeMark::none_type) const
TimeMark::Type equation_fixed_mark_type() const
Accessor to the data with type Type::Record.
Definition: accessors.hh:327
TimeMark::Type equation_mark_type() const
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:100
#define START_TIMER(tag)
Starts a timer with specified tag.
Class for declaration of polymorphic Record.
Definition: type_record.hh:487
This class is used for output data to VTK file format.
Definition: output_vtk.hh:41
Input::AbstractRecord format_record_
Definition: output_time.hh:143
virtual ~OutputTime()
Destructor of OutputTime. It doesn&#39;t do anything, because all necessary destructors will be called in...
Definition: output_time.cc:91
void clear_data(void)
Clear data for output computed by method compute_field_data.
Definition: output_time.cc:237
ofstream _base_file
Definition: output_time.hh:212
The class for outputting data during time.
Definition: output_time.hh:32
void mark_output_times(const TimeGovernor &tg)
Definition: output_time.cc:182
string _base_filename
Definition: output_time.hh:217
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:448
#define MPI_Comm_rank
Definition: mpi.h:236
Dedicated class for storing path to input and output files.
Definition: file_path.hh:32
void write_time_frame()
Definition: output_time.cc:215
static Default read_time(const std::string &description)
Definition: type_record.hh:77
void copy_to(Container &out) const
TimeMark::Type type_input()
Definition: time_marks.hh:208
static FileName output()
Definition: type_base.hh:470
virtual int write_data(void)=0
Virtual method for writing data to output file.
#define INSTANCE_register_field(spacedim, value)
Definition: output_time.cc:244
OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES]
Definition: output_time.hh:180
#define MPI_COMM_WORLD
Definition: mpi.h:123
Class used for marking specified times at which some events occur.
Definition: time_marks.hh:49
Record type proxy class.
Definition: type_record.hh:169
double write_time
Definition: output_time.hh:195
TimeMarks::iterator end(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:170
This class is used for output data to VTK file format.
Definition: output_msh.hh:39
Record & declare_key(const string &key, const KeyType &type, const Default &default_value, const string &description)
Definition: type_record.cc:430