Flow123d  last_with_con_2.0.0-4-g42e6930
field_set.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file field_set.cc
15  * @brief
16  */
17 
18 #include "fields/field_set.hh"
19 #include "system/sys_profiler.hh"
21 
22 
23 
24 FieldSet &FieldSet::operator +=(FieldCommon &add_field) {
25  FieldCommon *found_field = field(add_field.name());
26  if (found_field) {
27  OLD_ASSERT(&add_field==found_field, "Another field of the same name exists when adding field: %s\n",
28  add_field.name().c_str());
29  } else {
30  field_list.push_back(&add_field);
31  }
32  return *this;
33 }
34 
35 
36 
37 FieldSet &FieldSet::operator +=(const FieldSet &other) {
38  for(auto field_ptr : other.field_list) this->operator +=(*field_ptr);
39  return *this;
40 }
41 
42 
43 
44 FieldSet FieldSet::subset(std::vector<std::string> names) const {
45  FieldSet set;
46  for(auto name : names) set += (*this)[name];
47  return set;
48 }
49 
50 
51 
52 FieldSet FieldSet::subset( FieldFlag::Flags::Mask mask) const {
53  FieldSet set;
54  for(auto field : field_list)
55  if (field->flags().match(mask)) set += *field;
56  return set;
57 }
58 
59 
60 
61 Input::Type::Record FieldSet::make_field_descriptor_type(const std::string &equation_name) const {
62  Input::Type::Record rec = Input::Type::Record(equation_name + "_Data",
63  FieldCommon::field_descriptor_record_description(equation_name + "_Data"))
64  .copy_keys(FieldCommon::field_descriptor_record(equation_name + "_Data_aux"));
65 
66  for(auto field : field_list) {
67  if ( field->flags().match(FieldFlag::declare_input) ) {
68  string description = field->description() + " (($[" + field->units().format_latex() + "]$))";
69 
70  // Adding units is not so simple.
71  // 1) It must be correct for Latex.
72  // 2) It should be consistent with rest of documentation.
73  // 3) Should be specified for all fields.
74  //if (units != "") description+= " [" +field->units() + "]";
75 
76  // TODO: temporary solution, see FieldCommon::multifield_
77 
78  std::shared_ptr<Input::Type::TypeBase> field_type_ptr;
79  if (field->is_multifield()) {
80  field_type_ptr = std::make_shared<Input::Type::Array>(field->get_multifield_input_type());
81  } else {
82  field_type_ptr = std::make_shared<Input::Type::Instance>(field->get_input_type());
83  }
84  OLD_ASSERT( field->units().is_def() , "units not def.");
85  rec.declare_key(field->input_name(), field_type_ptr, Input::Type::Default::optional(), description,
86  {{FlowAttribute::field_unit(), field->units().json() }});
87  }
88 
89  }
90  return rec.close();
91 }
92 
93 
94 
95 Input::Type::Selection FieldSet::make_output_field_selection(const string &name, const string &desc)
96 {
97  namespace IT=Input::Type;
98  IT::Selection sel(name, desc);
99  int i=0;
100  // add value for each field excluding boundary fields
101  for( auto field : field_list)
102  {
103  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
104  {
105  string desc = "Output of the field " + field->name() + " (($[" + field->units().format_latex()+"]$))";
106  if (field->description().length() > 0)
107  desc += " (" + field->description() + ").";
108  else
109  desc += ".";
110  sel.add_value(i, field->name(), desc);
111  i++;
112  }
113  }
114 
115  return sel;
116 }
117 
118 
119 
120 void FieldSet::set_field(const std::string &dest_field_name, FieldCommon &source)
121 {
122  auto &field = (*this)[dest_field_name];
123  field.copy_from(source);
124 }
125 
126 
127 
128 FieldCommon *FieldSet::field(const std::string &field_name) const {
129  for(auto field : field_list)
130  if (field->name() ==field_name) return field;
131  return nullptr;
132 }
133 
134 
135 
136 FieldCommon &FieldSet::operator[](const std::string &field_name) const {
137  FieldCommon *found_field=field(field_name);
138  if (found_field) return *found_field;
139 
140  THROW(ExcUnknownField() << FieldCommon::EI_Field(field_name));
141  return *field_list[0]; // formal to prevent compiler warning
142 }
143 
144 
145 
146 bool FieldSet::changed() const {
147  bool changed_all=false;
148  for(auto field : field_list) changed_all = changed_all || field->changed();
149  return changed_all;
150 }
151 
152 
153 
154 bool FieldSet::is_constant(Region reg) const {
155  bool const_all=true;
156  for(auto field : field_list) const_all = const_all && field->is_constant(reg);
157  return const_all;
158 }
159 
160 
162  bool is_jump = false;
163  for(auto field : field_list) is_jump = is_jump || field->is_jump_time();
164  return is_jump;
165 }
166 
167 
168 
169 // OBSOLETE method
171  const string &desc, const string & d_val) {
172  *this += field->name(name).description(desc).input_default(d_val);
173  return *field;
174 }
175 
176 
177 
178 std::ostream &operator<<(std::ostream &stream, const FieldSet &set) {
179  for(FieldCommon * field : set.field_list) {
180  stream << *field
181  << std::endl;
182  }
183  return stream;
184 }
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:245
bool is_jump_time() const
Definition: field_set.cc:161
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:60
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:61
virtual void copy_from(const FieldCommon &other)=0
FieldCommon & operator[](const std::string &field_name) const
Definition: field_set.cc:136
static constexpr Mask allow_output
The field can output. Is part of generated output selection. (default on)
Definition: field_flag.hh:37
virtual bool is_constant(Region reg)=0
virtual IT::Instance get_input_type()=0
static const std::string field_descriptor_record_description(const string &record_name)
Definition: field_common.cc:67
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:286
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:113
#define OLD_ASSERT(...)
Definition: global_defs.h:131
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:128
FieldCommon & add_field(FieldCommon *field, const string &name, const string &desc, const string &d_val="")
Definition: field_set.cc:170
FieldCommon & input_default(const string &input_default)
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.
static IT::Record field_descriptor_record(const string &record_name)
Definition: field_common.cc:54
void set_field(const std::string &dest_field_name, FieldCommon &source)
Definition: field_set.cc:120
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
bool is_multifield() const
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:212
bool is_jump_time()
FieldCommon & description(const string &description)
Definition: field_common.hh:93
virtual IT::Array get_multifield_input_type()=0
bool is_constant(Region reg) const
Definition: field_set.cc:154
FieldCommon & name(const string &name)
Definition: field_common.hh:86
static string field_unit()
bool changed() const
friend std::ostream & operator<<(std::ostream &stream, const FieldSet &set)
Definition: field_set.cc:178
Record type proxy class.
Definition: type_record.hh:171
FieldCommon & flags(FieldFlag::Flags::Mask mask)
const std::string & input_name() const
bool is_bc() const
Input::Type::Selection make_output_field_selection(const string &name, const string &desc)
Definition: field_set.cc:95
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
bool changed() const
Definition: field_set.cc:146
Template for classes storing finite set of named values.
Input::Type::Record make_field_descriptor_type(const std::string &equation_name) const
Definition: field_set.cc:61
static constexpr Mask declare_input
The field can be set from input. The key in input field descriptor is declared. (default on) ...
Definition: field_flag.hh:35