Flow123d  release_2.1.0-84-g6a13a75
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"
14 #include <memory>
15 
16 
17 namespace IT = Input::Type;
18 
19 
20 
22 
23  static const IT::Record &field_output_setting =
24  IT::Record("FieldOutputSetting", "Setting of the field output. The field name, output times, output interpolation (future).")
25  .allow_auto_conversion("field")
26  .declare_key("field", IT::Parameter("output_field_selection"), IT::Default::obligatory(),
27  "The field name (from selection).")
29  "Output times specific to particular field.")
30  //.declare_key("interpolation", ...)
31  .close();
32 
33  return IT::Record("EquationOutput",
34  "Output of the equation's fields."
35  "The output is done through the output stream of the associated balance law equation."
36  "The stream defines output format for the full space information in selected times and "
37  "observe points for the full time information. The key 'fields' select the fields for the full spatial output."
38  "The set of output times may be specified per field otherwise common time set 'times' is used. If even this is not provided"
39  "the time set of the output_stream is used. The initial time of the equation is automatically added "
40  "to the time set of every selected field. The end time of the equation is automatically added "
41  "to the common output time set.")
44  "Output times used for the output fields without is own time series specification.")
45  .declare_key("add_input_times", IT::Bool(), IT::Default("false"),
46  "Add all input time points of the equation, mentioned in the 'input_fields' list, also as the output points.")
47  .declare_key("fields", IT::Array(field_output_setting), IT::Default("[]"),
48  "Array of output fields and their individual output settings.")
49  .declare_key("observe_fields", IT::Array( IT::Parameter("output_field_selection")), IT::Default("[]"),
50  "Array of the fields evaluated in the observe points of the associated output stream.")
51  .close();
52 }
53 
54 
55 
56 const IT::Instance &EquationOutput::make_output_type(const string &equation_name, const string &additional_description)
57 {
58  string selection_name = equation_name + ":OutputFields";
59  string description = "Selection of output fields for the " + equation_name + " model.\n" + additional_description;
60  IT::Selection sel(selection_name, description );
61  int i=0;
62  // add value for each field excluding boundary fields
63  for( FieldCommon * field : field_list)
64  {
65  //DebugOut().fmt("type for field: {}\n", field->name());
66  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
67  {
68  string desc = "(($[" + field->units().format_latex()+"]$)) "; + "Output of: the field " + field->name() + " ";
70  desc += "Input field: ";
71  if (field->description().length() > 0)
72  desc += field->description();
74  i++;
75  }
76  }
77 
78  const IT::Selection &output_field_selection = sel.close();
79 
81  param_vec.push_back( std::make_pair("output_field_selection", std::make_shared< IT::Selection >(output_field_selection) ) );
82  return IT::Instance(get_input_type(), param_vec).close();
83 
84 }
85 
86 
87 void EquationOutput::initialize(std::shared_ptr<OutputTime> stream, Input::Record in_rec, const TimeGovernor & tg)
88 {
89  stream_ = stream;
92  read_from_input(in_rec, tg);
93 }
94 
95 
96 
98 {
99  ASSERT(stream_).error("The 'set_stream' method must be called before the 'read_from_input'.");
100  auto &marks = TimeGovernor::marks();
101 
102  Input::Array times_array;
103  if (in_rec.opt_val("times", times_array) ) {
104  common_output_times_.read_from_input(times_array, tg);
105  } else {
106  // take times from the output_stream if key times is missing
107  auto times_array_it = stream_->get_time_set_array();
108  if (times_array_it) {
109  common_output_times_.read_from_input(*times_array_it, tg);
110  }
111  }
112  // always add the end time
114 
115  if (in_rec.val<bool>("add_input_times")) {
116  // copy time marks in order to prevent invalidation of the iterator
117  TimeMarks marks_copy = TimeGovernor::marks();
118  for(auto time_mark_it = marks_copy.begin(equation_type_ | marks.type_input());
119  time_mark_it != marks_copy.end(equation_type_ | marks.type_input());
120  ++time_mark_it) {
121  common_output_times_.add(time_mark_it->time(), equation_fixed_type_);
122  }
123  }
124  auto fields_array = in_rec.val<Input::Array>("fields");
125  for(auto it = fields_array.begin<Input::Record>(); it != fields_array.end(); ++it) {
126  string field_name = it -> val< Input::FullEnum >("field");
127  Input::Array field_times_array;
128  if (it->opt_val("times", field_times_array)) {
129  OutputTimeSet field_times;
130  field_times.read_from_input(field_times_array, tg);
131  field_output_times_[field_name] = field_times;
132  } else {
134  }
135  // Add init time as the output time for every output field.
136  field_output_times_[field_name].add(tg.init_time(), equation_fixed_type_);
137  }
138  auto observe_fields_array = in_rec.val<Input::Array>("observe_fields");
139  for(auto it = observe_fields_array.begin<Input::FullEnum>(); it != observe_fields_array.end(); ++it) {
140  observe_fields_.insert(string(*it));
141  }
142 }
143 
145 {
146  auto &marks = TimeGovernor::marks();
147  auto field_times_it = field_output_times_.find(field.name());
148  if (field_times_it == field_output_times_.end()) return false;
149  ASSERT( step.eq(field.time()) )(step.end())(field.time())(field.name()).error("Field is not set to the output time.");
150  auto current_mark_it = marks.current(step, equation_type_ | marks.type_output() );
151  if (current_mark_it == marks.end(equation_type_ | marks.type_output()) ) return false;
152  return (field_times_it->second.contains(*current_mark_it) );
153 }
154 
156 {
157  // TODO: remove const_cast after resolving problems with const Mesh.
158  //Mesh *field_mesh = const_cast<Mesh *>(field_list[0]->mesh());
159  stream_->make_output_mesh(*this);
160 
161  for(FieldCommon * field : this->field_list) {
162 
163  if ( field->flags().match( FieldFlag::allow_output) ) {
164  if (is_field_output_time(*field, step)) {
165  field->output(stream_);
166  }
167  // observe output
168  if (observe_fields_.find(field->name()) != observe_fields_.end()) {
169  field->observe_output( stream_->observe() );
170  }
171  }
172  }
173 }
174 
175 
176 void EquationOutput::add_output_times(double begin, double step, double end)
177 {
178  common_output_times_.add(begin,step, end, equation_fixed_type_ );
179 }
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:243
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:561
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
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:56
Class for declaration of the input of type Bool.
Definition: type_base.hh:452
void output(TimeStep step)
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:193
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:105
virtual std::string get_value_attribute() const =0
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:301
Class for declaration of inputs sequences.
Definition: type_base.hh:339
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:119
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:132
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:130
TimeMark::Type equation_fixed_mark_type() const
Accessor to the data with type Type::Record.
Definition: accessors.hh:286
TimeMark::Type equation_mark_type() const
const Ret val(const string &key) const
virtual Record & root_of_generic_subtree()
Definition: type_record.cc:417
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:488
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:197
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()
TimeMarks::iterator end(TimeMark::Type mask) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:207
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:177
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
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)