Flow123d  release_2.2.0-914-gf1a3a4f
equation_output.cc
Go to the documentation of this file.
1 /*
2  * equation_output.cc
3  *
4  * Created on: Jul 8, 2016
5  * Author: jb
6  */
7 
8 #include "tools/time_marks.hh"
9 #include "input/input_type.hh"
10 #include "input/accessors.hh"
12 #include "io/output_time_set.hh"
14 #include <memory>
15 
16 
17 namespace IT = Input::Type;
18 
19 
20 
22 
23  static const IT::Selection &interpolation_sel =
24  IT::Selection("Discrete_output", "Discrete type of output. Determines type of output data (element, node, native etc).")
25  .add_value(OutputTime::NODE_DATA, "P1_average", "Node data / point data.")
26  .add_value(OutputTime::CORNER_DATA, "D1_value", "Corner data.")
27  .add_value(OutputTime::ELEM_DATA, "P0_value", "Element data / cell data.")
28  .add_value(OutputTime::NATIVE_DATA, "Native", "Native data (Flow123D data).")
29  .close();
30 
31  static const IT::Record &field_output_setting =
32  IT::Record("FieldOutputSetting", "Setting of the field output. The field name, output times, output interpolation (future).")
33  .allow_auto_conversion("field")
34  .declare_key("field", IT::Parameter("output_field_selection"), IT::Default::obligatory(),
35  "The field name (from selection).")
37  "Output times specific to particular field.")
38  .declare_key("interpolation", interpolation_sel, IT::Default::read_time("Interpolation type of output data."),
39  "Optional value. Implicit value is given by field and can be changed.")
40  .close();
41 
42  return IT::Record("EquationOutput",
43  "Output of the equation's fields."
44  "The output is done through the output stream of the associated balance law equation."
45  "The stream defines output format for the full space information in selected times and "
46  "observe points for the full time information. The key 'fields' select the fields for the full spatial output."
47  "The set of output times may be specified per field otherwise common time set 'times' is used. If even this is not provided"
48  "the time set of the output_stream is used. The initial time of the equation is automatically added "
49  "to the time set of every selected field. The end time of the equation is automatically added "
50  "to the common output time set.")
53  "Output times used for the output fields without is own time series specification.")
54  .declare_key("add_input_times", IT::Bool(), IT::Default("false"),
55  "Add all input time points of the equation, mentioned in the 'input_fields' list, also as the output points.")
56  .declare_key("fields", IT::Array(field_output_setting), IT::Default("[]"),
57  "Array of output fields and their individual output settings.")
58  .declare_key("observe_fields", IT::Array( IT::Parameter("output_field_selection")), IT::Default("[]"),
59  "Array of the fields evaluated in the observe points of the associated output stream.")
60  .close();
61 }
62 
63 
64 
65 const IT::Instance &EquationOutput::make_output_type(const string &equation_name, const string &additional_description)
66 {
67  string selection_name = equation_name + ":OutputFields";
68  string description = "Selection of output fields for the " + equation_name + " model.\n" + additional_description;
69  IT::Selection sel(selection_name, description );
70  int i=0;
71  // add value for each field excluding boundary fields
72  for( FieldCommon * field : field_list)
73  {
74  //DebugOut().fmt("type for field: {}\n", field->name());
75  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
76  {
77  string desc = "(($[" + field->units().format_latex()+"]$)) "; + "Output of: the field " + field->name() + " ";
79  desc += "Input field: ";
80  if (field->description().length() > 0)
81  desc += field->description();
83  i++;
84  }
85  }
86 
87  const IT::Selection &output_field_selection = sel.close();
88 
90  param_vec.push_back( std::make_pair("output_field_selection", std::make_shared< IT::Selection >(output_field_selection) ) );
91  return IT::Instance(get_input_type(), param_vec).close();
92 
93 }
94 
95 
96 void EquationOutput::initialize(std::shared_ptr<OutputTime> stream, Mesh *mesh, Input::Record in_rec, const TimeGovernor & tg)
97 {
98  stream_ = stream;
99  mesh_ = mesh;
102  read_from_input(in_rec, tg);
103 }
104 
105 
106 
108 {
109  ASSERT(stream_).error("The 'set_stream' method must be called before the 'read_from_input'.");
110  auto &marks = TimeGovernor::marks();
111 
112  Input::Array times_array;
113  if (in_rec.opt_val("times", times_array) ) {
114  common_output_times_.read_from_input(times_array, tg);
115  } else {
116  // take times from the output_stream if key times is missing
117  auto times_array_it = stream_->get_time_set_array();
118  if (times_array_it) {
119  common_output_times_.read_from_input(*times_array_it, tg);
120  }
121  }
122  // always add the end time
124 
125  if (in_rec.val<bool>("add_input_times")) {
126  // copy time marks in order to prevent invalidation of the iterator
127  TimeMarks marks_copy = TimeGovernor::marks();
128  for(auto time_mark_it = marks_copy.begin(equation_type_ | marks.type_input());
129  time_mark_it != marks_copy.end(equation_type_ | marks.type_input());
130  ++time_mark_it) {
131  common_output_times_.add(time_mark_it->time(), equation_fixed_type_);
132  }
133  }
134  auto fields_array = in_rec.val<Input::Array>("fields");
135  for(auto it = fields_array.begin<Input::Record>(); it != fields_array.end(); ++it) {
136  string field_name = it -> val< Input::FullEnum >("field");
137  FieldCommon *found_field = field(field_name);
138  OutputTime::DiscreteSpace interpolation = it->val<OutputTime::DiscreteSpace>("interpolation", OutputTime::UNDEFINED);
139  found_field->output_type(interpolation);
140  Input::Array field_times_array;
141  if (it->opt_val("times", field_times_array)) {
142  OutputTimeSet field_times;
143  field_times.read_from_input(field_times_array, tg);
144  field_output_times_[field_name] = field_times;
145  } else {
147  }
148  // Add init time as the output time for every output field.
149  field_output_times_[field_name].add(tg.init_time(), equation_fixed_type_);
150  }
151  auto observe_fields_array = in_rec.val<Input::Array>("observe_fields");
152  for(auto it = observe_fields_array.begin<Input::FullEnum>(); it != observe_fields_array.end(); ++it) {
153  observe_fields_.insert(string(*it));
154  }
155 
156  // register interpolation type of fields to OutputStream
157  for(FieldCommon * field : this->field_list) {
159  }
160 }
161 
163 {
164  auto &marks = TimeGovernor::marks();
165  auto field_times_it = field_output_times_.find(field.name());
166  if (field_times_it == field_output_times_.end()) return false;
167  ASSERT( step.eq(field.time()) )(step.end())(field.time())(field.name()).error("Field is not set to the output time.");
168  auto current_mark_it = marks.current(step, equation_type_ | marks.type_output() );
169  if (current_mark_it == marks.end(equation_type_ | marks.type_output()) ) return false;
170  return (field_times_it->second.contains(*current_mark_it) );
171 }
172 
173 
175 {
176  ASSERT_PTR(mesh_).error();
177 
178  // make observe points if not already done
179  auto observe_ptr = stream_->observe(mesh_);
180 
181  int rank; bool parallel;
182  stream_->get_output_params(parallel, rank);
183 
184  if ( (rank == 0) || parallel ) {
185  this->make_output_mesh(parallel);
186  }
187 
188  for(FieldCommon * field : this->field_list) {
189 
190  if ( field->flags().match( FieldFlag::allow_output) ) {
191  if (is_field_output_time(*field, step)) {
193  }
194  // observe output
195  if (observe_fields_.find(field->name()) != observe_fields_.end()) {
196  field->observe_output( observe_ptr );
197  }
198  }
199  }
200 
201  // complete information about dummy fields
202  stream_->add_dummy_fields();
203 }
204 
205 
206 void EquationOutput::add_output_times(double begin, double step, double end)
207 {
208  common_output_times_.add(begin,step, end, equation_fixed_type_ );
209 }
210 
211 
213 {
214  // already computed
215  if (stream_->is_output_data_caches_init()) return;
216 
217  // Read optional error control field name
218  bool discont = stream_->get_output_mesh_record();
219 
220  if(stream_->enable_refinement()) {
221  if(discont) {
222  // create output meshes from input record
223  output_mesh_ = std::make_shared<OutputMeshDiscontinuous>(*mesh_, *stream_->get_output_mesh_record());
224 
225  // possibly set error control field for refinement
226  auto ecf = select_error_control_field();
227  output_mesh_->set_error_control_field(ecf);
228 
229  // actually compute refined mesh
230  output_mesh_->create_refined_mesh();
231  stream_->set_output_data_caches(output_mesh_);
232  return;
233  }
234  }
235  else
236  {
237  // skip creation of output mesh (use computational one)
238  if(discont)
239  WarningOut() << "Ignoring output mesh record.\n Output in GMSH format available only on computational mesh!";
240  }
241 
242  // create output mesh identical with the computational one
244  discont |= parallel;
245  if (discont) {
246  output_mesh_ = std::make_shared<OutputMeshDiscontinuous>(*mesh_);
247  } else {
248  output_mesh_ = std::make_shared<OutputMesh>(*mesh_);
249  }
250  if (parallel) output_mesh_->create_sub_mesh();
251  else output_mesh_->create_mesh();
252  stream_->set_output_data_caches(output_mesh_);
253 }
254 
255 
257 {
258  std::string error_control_field_name = "";
259  // Read optional error control field name
260  auto it = stream_->get_output_mesh_record()->find<std::string>("error_control_field");
261  if(it) error_control_field_name = *it;
262 
263  if(error_control_field_name!="")
264  {
265  FieldCommon* field = this->field(error_control_field_name);
266  // throw input exception if the field is unknown
267  if(field == nullptr){
268  THROW(FieldSet::ExcUnknownField()
269  << FieldCommon::EI_Field(error_control_field_name));
270  }
271 
272  // throw input exception if the field is not scalar
273  if( typeid(*field) == typeid(Field<3,FieldValue<3>::Scalar>) ) {
274 
275  Field<3,FieldValue<3>::Scalar>* error_control_field = static_cast<Field<3,FieldValue<3>::Scalar>*>(field);
276  DebugOut() << "Error control field for output mesh set: " << error_control_field_name << ".";
277  auto lambda_function =
278  [error_control_field](const std::vector< Space<OutputMeshBase::spacedim>::Point > &point_list, const ElementAccessor<OutputMeshBase::spacedim> &elm, std::vector<double> &value_list)->void
279  { error_control_field->value_list(point_list, elm, value_list); };
280 
281  OutputMeshBase::ErrorControlFieldFunc func = lambda_function;
282  return func;
283 
284  }
285  else{
286  THROW(ExcFieldNotScalar()
287  << FieldCommon::EI_Field(error_control_field_name));
288  }
289  }
291 }
virtual void field_output(std::shared_ptr< OutputTime > stream)=0
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:243
void add_output_times(double begin, double step, double end)
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:60
std::unordered_set< string > observe_fields_
Set of observed fields. The observe points are given within the observe stream.
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
double end_time() const
End time.
static constexpr Mask allow_output
The field can output. Is part of generated output selection. (default on)
Definition: field_flag.hh:37
std::function< void(const std::vector< Space< spacedim >::Point > &, const ElementAccessor< spacedim > &, std::vector< double > &)> ErrorControlFieldFunc
Definition: output_mesh.hh:74
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Class for declaration of the input of type Bool.
Definition: type_base.hh:458
OutputTime::DiscreteSpace get_output_type() const
void output(TimeStep step)
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:62
const Instance & close() const
Used for set Instance to TypeRepository.
TimeMarks::iterator begin(TimeMark::Type mask) const
Iterator for the begin mimics container-like of TimeMarks.
Definition: time_marks.cc:192
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
virtual std::string get_value_attribute() const =0
Class for representing parametric types in IST.
Definition: type_generic.hh:53
Definition: mesh.h:99
TimeMark::Type equation_fixed_type_
The fixed time mark type of the equation.
Helper class that stores data of generic types.
Definition: type_generic.hh:89
void read_from_input(Input::Array in_array, const TimeGovernor &tg)
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
Basic time management functionality for unsteady (and steady) solvers (class Equation).
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
void make_output_mesh(bool parallel)
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list) const
Definition: field.hh:382
static TimeMarks & marks()
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
Class for declaration of inputs sequences.
Definition: type_base.hh:345
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:124
double init_time() const
bool opt_val(const string &key, Ret &value) const
void read_from_input(Input::Record in_rec, const TimeGovernor &tg)
double time() const
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:140
virtual Record & allow_auto_conversion(const string &from_key)
Allows shorter input of the Record providing only value of the from_key given as the parameter...
Definition: type_record.cc:132
TimeMark::Type equation_fixed_mark_type() const
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
TimeMark::Type equation_mark_type() const
const Ret val(const string &key) const
virtual Record & root_of_generic_subtree()
Definition: type_record.cc:419
OutputMeshBase::ErrorControlFieldFunc select_error_control_field()
Selects the error control field out of output field set according to input record.
Selection & add_value(const int value, const std::string &key, const std::string &description="", TypeBase::attribute_map attributes=TypeBase::attribute_map())
Adds one new value with name given by key to the Selection.
static string field_value_shape()
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:490
std::shared_ptr< OutputTime > stream_
output stream (may be shared by more equation)
double end() const
This class is a collection of time marks to manage various events occurring during simulation time...
Definition: time_marks.hh:206
std::unordered_map< string, OutputTimeSet > field_output_times_
Time sets of individual fields.
static Default read_time(const std::string &description)
The factory function to make an default value that will be specified at the time when a key will be r...
Definition: type_record.hh:97
FieldCommon & description(const string &description)
void initialize(std::shared_ptr< OutputTime > stream, Mesh *mesh, Input::Record in_rec, const TimeGovernor &tg)
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:335
const Selection & close() const
Close the Selection, no more values can be added.
static Input::Type::Record & get_input_type()
static const Input::Type::Array get_input_type()
TimeMarks::iterator end(TimeMark::Type mask) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:206
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:246
FieldCommon & name(const string &name)
Definition: field_common.hh:97
virtual void observe_output(std::shared_ptr< Observe > observe)=0
Record type proxy class.
Definition: type_record.hh:182
bool is_field_output_time(const FieldCommon &field, TimeStep step) const
const Input::Type::Instance & make_output_type(const string &equation_name, const string &aditional_description="")
FieldCommon & flags(FieldFlag::Flags::Mask mask)
OutputTimeSet common_output_times_
The time set used for the fields without explicit time set.
static constexpr Mask equation_input
The field is data parameter of the owning equation. (default on)
Definition: field_flag.hh:33
bool is_bc() const
#define DebugOut()
Macro defining &#39;debug&#39; record of log.
Definition: logger.hh:252
TimeMark::Type equation_type_
The time mark type of the equation.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Representation of one time step..
Template for classes storing finite set of named values.
std::shared_ptr< OutputMeshBase > output_mesh_
Output mesh.
std::set< OutputTime::DiscreteSpace > used_interpolations_
bool eq(double other_time) const
void add(double begin, TimeMark::Type mark_type)