Flow123d  last_with_con_2.0.0-4-g42e6930
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"
11 #include "io/equation_output.hh"
12 #include "io/output_time_set.hh"
13 #include <memory>
14 
15 
16 namespace IT = Input::Type;
17 
18 
19 
21 
22  static const IT::Record &field_output_setting =
23  IT::Record("FieldOutputSetting", "Setting of the field output. The field name, output times, output interpolation (future).")
24  .allow_auto_conversion("field")
25  .declare_key("field", IT::Parameter("output_field_selection"), IT::Default::obligatory(),
26  "The field name (from selection).")
28  "Output times specific to particular field.")
29  //.declare_key("interpolation", ...)
30  .close();
31 
32  return IT::Record("EquationOutput",
33  "Output of the equation's fields."
34  "The output is done through the output stream of the associated balance law equation."
35  "The stream defines output format for the full space information in selected times and "
36  "observe points for the full time information. The key 'fields' select the fields for the full spatial output."
37  "The set of output times may be specified per field otherwise common time set 'times' is used. If even this is not provided"
38  "the time set of the output_stream is used. The initial time of the equation is automatically added "
39  "to the time set of every selected field. The end time of the equation is automatically added "
40  "to the common output time set.")
43  "Output times used for the output fields without is own time series specification.")
44  .declare_key("add_input_times", IT::Bool(), IT::Default("false"),
45  "Add all input time points of the equation, mentioned in the 'input_fields' list, also as the output points.")
46  .declare_key("fields", IT::Array(field_output_setting), IT::Default("[]"),
47  "Array of output fields and their individual output settings.")
48  .declare_key("observe_fields", IT::Array( IT::Parameter("output_field_selection")), IT::Default("[]"),
49  "Array of the fields evaluated in the observe points of the associated output stream.")
50  .close();
51 }
52 
53 
54 
55 const IT::Instance &EquationOutput::make_output_type(const string &equation_name, const string &additional_description)
56 {
57  string selection_name = equation_name + "_output_fields";
58  string description = "Selection of output fields for the " + equation_name + " model.\n" + additional_description;
59  IT::Selection sel(selection_name, description );
60  int i=0;
61  // add value for each field excluding boundary fields
62  for( FieldCommon * field : field_list)
63  {
64  //DebugOut().fmt("type for field: {}\n", field->name());
65  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
66  {
67  string desc = "Output of the field " + field->name() + " (($[" + field->units().format_latex()+"]$))";
68  if (field->description().length() > 0)
69  desc += " (" + field->description() + ").";
70  else
71  desc += ".";
72  sel.add_value(i, field->name(), desc);
73  i++;
74  }
75  }
76 
77  const IT::Selection &output_field_selection = sel.close();
78 
80  param_vec.push_back( std::make_pair("output_field_selection", std::make_shared< IT::Selection >(output_field_selection) ) );
81  return IT::Instance(get_input_type(), param_vec).close();
82 
83 }
84 
85 
86 void EquationOutput::initialize(std::shared_ptr<OutputTime> stream, Input::Record in_rec, const TimeGovernor & tg)
87 {
88  stream_ = stream;
91  read_from_input(in_rec, tg);
92 }
93 
94 
95 
97 {
98  ASSERT(stream_).error("The 'set_stream' method must be called before the 'read_from_input'.");
99  auto &marks = TimeGovernor::marks();
100 
101  Input::Array times_array;
102  if (in_rec.opt_val("times", times_array) ) {
103  common_output_times_.read_from_input(times_array, tg);
104  } else {
105  // take times from the output_stream if key times is missing
106  auto times_array_it = stream_->get_time_set_array();
107  if (times_array_it) {
108  common_output_times_.read_from_input(*times_array_it, tg);
109  }
110  }
111  // always add the end time
113 
114  if (in_rec.val<bool>("add_input_times")) {
115  // copy time marks in order to prevent invalidation of the iterator
116  TimeMarks marks_copy = TimeGovernor::marks();
117  for(auto time_mark_it = marks_copy.begin(equation_type_ | marks.type_input());
118  time_mark_it != marks_copy.end(equation_type_ | marks.type_input());
119  ++time_mark_it) {
120  common_output_times_.add(time_mark_it->time(), equation_fixed_type_);
121  }
122  }
123  auto fields_array = in_rec.val<Input::Array>("fields");
124  for(auto it = fields_array.begin<Input::Record>(); it != fields_array.end(); ++it) {
125  string field_name = it -> val< Input::FullEnum >("field");
126  Input::Array field_times_array;
127  if (it->opt_val("times", field_times_array)) {
128  OutputTimeSet field_times;
129  field_times.read_from_input(field_times_array, tg);
130  field_output_times_[field_name] = field_times;
131  } else {
133  }
134  // Add init time as the output time for every output field.
135  field_output_times_[field_name].add(tg.init_time(), equation_fixed_type_);
136  }
137  auto observe_fields_array = in_rec.val<Input::Array>("observe_fields");
138  for(auto it = observe_fields_array.begin<Input::FullEnum>(); it != observe_fields_array.end(); ++it) {
139  observe_fields_.insert(string(*it));
140  }
141 }
142 
144 {
145  auto &marks = TimeGovernor::marks();
146  auto field_times_it = field_output_times_.find(field.name());
147  if (field_times_it == field_output_times_.end()) return false;
148  ASSERT( step.eq(field.time()) )(step.end())(field.time())(field.name()).error("Field is not set to the output time.");
149  auto current_mark_it = marks.current(step, equation_type_ | marks.type_output() );
150  if (current_mark_it == marks.end(equation_type_ | marks.type_output()) ) return false;
151  return (field_times_it->second.contains(*current_mark_it) );
152 }
153 
155 {
156  // TODO: remove const_cast after resolving problems with const Mesh.
157  //Mesh *field_mesh = const_cast<Mesh *>(field_list[0]->mesh());
158  stream_->make_output_mesh(*this);
159 
160  for(FieldCommon * field : this->field_list) {
161 
162  if ( field->flags().match( FieldFlag::allow_output) ) {
163  if (is_field_output_time(*field, step)) {
164  field->output(stream_);
165  }
166  // observe output
167  if (observe_fields_.find(field->name()) != observe_fields_.end()) {
168  field->observe_output( stream_->observe() );
169  }
170  }
171  }
172 }
173 
174 
175 void EquationOutput::add_output_times(double begin, double step, double end)
176 {
177  common_output_times_.add(begin,step, end, equation_fixed_type_ );
178 }
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:245
virtual void output(std::shared_ptr< OutputTime > stream)=0
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:552
double end_time() const
End time.
TimeMarks::iterator begin(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the begin mimics container-like of TimeMarks.
Definition: time_marks.cc:176
static constexpr Mask allow_output
The field can output. Is part of generated output selection. (default on)
Definition: field_flag.hh:37
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:50
Class for declaration of the input of type Bool.
Definition: type_base.hh:434
void output(TimeStep step)
const Instance & close() const
Used for set Instance to TypeRepository.
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:99
Class for representing parametric types in IST.
Definition: type_generic.hh:52
void initialize(std::shared_ptr< OutputTime > stream, Input::Record in_rec, const TimeGovernor &tg)
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:88
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:347
Basic time management functionality for unsteady (and steady) solvers (class Equation).
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
static TimeMarks & marks()
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:286
Class for declaration of inputs sequences.
Definition: type_base.hh:321
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:113
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:128
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
Selection & add_value(const int value, const std::string &key, const std::string &description="")
Adds one new value with name given by key to the Selection.
Accessor to the data with type Type::Record.
Definition: accessors.hh:277
TimeMark::Type equation_mark_type() const
const Ret val(const string &key) const
virtual Record & root_of_generic_subtree()
Definition: type_record.cc:397
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:468
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:163
std::unordered_map< string, OutputTimeSet > field_output_times_
Time sets of individual fields.
FieldCommon & description(const string &description)
Definition: field_common.hh:93
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()
FieldCommon & name(const string &name)
Definition: field_common.hh:86
virtual void observe_output(std::shared_ptr< Observe > observe)=0
Record type proxy class.
Definition: type_record.hh:171
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="")
TimeMarks::iterator end(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:183
FieldCommon & flags(FieldFlag::Flags::Mask mask)
OutputTimeSet common_output_times_
The time set used for the fields without explicit time set.
bool is_bc() const
TimeMark::Type equation_type_
The time mark type of the equation.
Representation of one time step..
Template for classes storing finite set of named values.
bool eq(double other_time) const
void add(double begin, TimeMark::Type mark_type)