Flow123d  master-e663071
equation.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 equation.cc
15  * @brief Abstract base class for equation clasess.
16  * @author Jan Brezina
17  */
18 
19 #include <petscmat.h>
20 #include "tools/time_governor.hh"
21 
22 
23 #include "equation.hh"
24 #include "system/system.hh"
25 #include "input/accessors.hh"
26 #include "fields/field_set.hh"
27 #include "fields/field_common.hh"
28 #include "fields/bc_field.hh"
29 #include "tools/unit_converter.hh"
30 #include "tools/unit_si.hh"
31 
32 
33 
34 
35 /*****************************************************************************************
36  * Implementation of EqBase
37  */
38 
40  return Input::Type::Record("EquationBase_AUX", "Auxiliary record with keys common for equations. Should not be used.")
42  "Time governor setting.")
43  .close();
44 }
45 
47  return Input::Type::Record("EquationBase_user_field_AUX", "Auxiliary record with common key user_field. Should not be used.")
48  .declare_key("user_fields", Input::Type::Array(
49  FieldSet::make_user_field_type(equation_name)),
51  "Input fields of the equation defined by user.")
52  .close();
53 }
54 
56 : equation_empty_(true),
57  mesh_(NULL),
58  time_(NULL),
59  input_record_(),
60  eq_fieldset_(nullptr)
61 {}
62 
63 
64 
66 : equation_empty_(false),
67  mesh_(&mesh),
68  time_(NULL),
69  input_record_(in_rec),
70  eq_fieldset_(nullptr)
71 {}
72 
73 
75 {
76  time_ = &time;
77 }
78 
80 {
81  return time_->t();
82 }
83 
84 void EquationBase::init_user_fields(Input::Array user_fields, FieldSet &output_fields) {
86  it != user_fields.end();
87  ++it) {
88  std::string field_name = it->val<std::string>("name");
89  bool is_bdr = it->val<bool>("is_boundary");
90 
91  // check if field of same name doesn't exist in FieldSet
92  auto * exist_field = eq_fieldset_->field(field_name);
93  if (exist_field!=nullptr) {
94  THROW(FieldSet::ExcFieldExists() << FieldCommon::EI_Field(field_name));
95  }
96 
97  UnitSI units = UnitSI::dimensionless();
98  Input::Record unit_record;
99  if ( it->opt_val("unit", unit_record) ) {
100  std::string unit_str = unit_record.val<std::string>("unit_formula");
101  try {
102  units.convert_unit_from(unit_str);
103  } catch (ExcInvalidUnit &e) {
104  e << it->ei_address();
105  throw;
106  } catch (ExcNoncorrespondingUnit &e) {
107  e << it->ei_address();
108  throw;
109  }
110  }
111 
112  Input::Iterator<Input::AbstractRecord> scalar_it = it->find<Input::AbstractRecord>("scalar_field");
113  if (scalar_it) {
114  Field<3, FieldValue<3>::Scalar> * scalar_field;
115  if (is_bdr)
116  scalar_field = new BCField<3, FieldValue<3>::Scalar>();
117  else
118  scalar_field = new Field<3, FieldValue<3>::Scalar>();
119  *eq_fieldset_+=scalar_field
120  ->name(field_name)
121  .description("")
122  .units( units )
124  scalar_field->set_mesh(*mesh_);
125  scalar_field->set( *scalar_it, time_->t());
126  scalar_field->set_default_fieldset(*eq_fieldset_);
127  output_fields+=*scalar_field;
128  } else {
129  Input::Iterator<Input::AbstractRecord> vector_it = it->find<Input::AbstractRecord>("vector_field");
130  if (vector_it) {
131  Field<3, FieldValue<3>::VectorFixed> * vector_field;
132  if (is_bdr)
133  vector_field = new BCField<3, FieldValue<3>::VectorFixed>();
134  else
135  vector_field = new Field<3, FieldValue<3>::VectorFixed>();
136  *eq_fieldset_+=vector_field
137  ->name(field_name)
138  .description("")
139  .units( units )
141  vector_field->set_mesh(*mesh_);
142  vector_field->set( *vector_it, time_->t());
143  vector_field->set_default_fieldset(*eq_fieldset_);
144  output_fields+=*vector_field;
145  } else {
146  Input::Iterator<Input::AbstractRecord> tensor_it = it->find<Input::AbstractRecord>("tensor_field");
147  if (tensor_it) {
148  Field<3, FieldValue<3>::TensorFixed> * tensor_field;
149  if (is_bdr)
150  tensor_field = new BCField<3, FieldValue<3>::TensorFixed>();
151  else
152  tensor_field = new Field<3, FieldValue<3>::TensorFixed>();
153  *eq_fieldset_+=tensor_field
154  ->name(field_name)
155  .description("")
156  .units( units )
158  tensor_field->set_mesh(*mesh_);
159  tensor_field->set( *tensor_it, time_->t());
160  tensor_field->set_default_fieldset(*eq_fieldset_);
161  output_fields+=*tensor_field;
162  } else {
163  THROW(FieldSet::ExcFieldNotSet() << FieldCommon::EI_Field(field_name));
164  }
165  }
166  }
167  }
168 }
FieldSet::make_user_field_type
static const Input::Type::Record & make_user_field_type(const std::string &equation_name)
Declare input record type of field defined by user.
Definition: field_set.cc:33
FieldCommon::units
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
Definition: field_common.hh:153
EquationBase::user_fields_template
static Input::Type::Record & user_fields_template(std::string equation_name)
Template Record with common key user_fields for derived equations.
Definition: equation.cc:46
bc_field.hh
EquationBase::mesh_
Mesh * mesh_
Definition: equation.hh:240
UnitSI::dimensionless
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:55
time_governor.hh
Basic time management class.
EquationBase::eq_fieldset_
std::shared_ptr< FieldSet > eq_fieldset_
Definition: equation.hh:249
EquationBase::time_
TimeGovernor * time_
Definition: equation.hh:241
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
EquationBase::time
TimeGovernor & time()
Definition: equation.hh:151
field_set.hh
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
system.hh
FieldCommon::flags
FieldCommon & flags(FieldFlag::Flags::Mask mask)
Definition: field_common.hh:192
UnitSI::convert_unit_from
double convert_unit_from(std::string actual_unit) const
Convert and check user-defined unit.
Definition: unit_si.cc:217
EquationBase::init_user_fields
void init_user_fields(Input::Array user_fields, FieldSet &output_fields)
Definition: equation.cc:84
Input::Array::begin
Iterator< ValueType > begin() const
Definition: accessors_impl.hh:145
Input::Iterator
Definition: accessors.hh:143
Input::Type::Default
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
FieldFlag::equation_result
static constexpr Mask equation_result
Match result fields. These are never given by input or copy of input.
Definition: field_flag.hh:55
accessors.hh
Field::set
void set(FieldBasePtr field, double time, std::vector< std::string > region_set_names={"ALL"})
Definition: field.impl.hh:244
TimeGovernor
Basic time management functionality for unsteady (and steady) solvers (class Equation).
Definition: time_governor.hh:317
Input::AbstractRecord
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
Input::Type::Record::declare_key
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:503
EquationBase::solved_time
virtual double solved_time()
Definition: equation.cc:79
FieldSet
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
equation.hh
Abstract base class for equation clasess.
EquationBase::EquationBase
EquationBase()
Definition: equation.cc:55
FieldCommon::set_default_fieldset
void set_default_fieldset(FieldSet &default_fs)
Definition: field_common.hh:529
TimeGovernor::get_input_type
static const Input::Type::Record & get_input_type()
Definition: time_governor.cc:63
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Input::Type
Definition: balance.hh:41
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
Mesh
Definition: mesh.h:362
Input::Type::Array
Class for declaration of inputs sequences.
Definition: type_base.hh:339
Field::set_mesh
void set_mesh(const Mesh &mesh) override
Definition: field.impl.hh:205
Input::Array
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
unit_si.hh
unit_converter.hh
EquationBase::record_template
static Input::Type::Record & record_template()
Template Record with common keys for derived equations.
Definition: equation.cc:39
BCField
Definition: bc_field.hh:32
FieldCommon::description
FieldCommon & description(const string &description)
Definition: field_common.hh:128
Field
Class template representing a field with values dependent on: point, element, and region.
Definition: field.hh:92
Input::Type::Default::optional
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:124
EquationBase::set_time_governor
virtual void set_time_governor(TimeGovernor &time)
Definition: equation.cc:74
Input::Array::end
IteratorBase end() const
Definition: accessors_impl.hh:157
field_common.hh
FieldCommon::name
FieldCommon & name(const string &name)
Definition: field_common.hh:121
TimeGovernor::t
double t() const
Definition: time_governor.hh:542