Hermes2D  2.0
essential_boundary_conditions.cpp
1 // This file is part of Hermes2D
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 #include "essential_boundary_conditions.h"
21 #include "exact_solution.h"
22 namespace Hermes
23 {
24  namespace Hermes2D
25  {
26  template<typename Scalar>
27  EssentialBoundaryCondition<Scalar>::EssentialBoundaryCondition(Hermes::vector<std::string> markers) : markers(markers)
28  {
29  current_time = 0.0;
30  value_const = 0.0;
31  }
32 
33  template<typename Scalar>
35  {
36  markers.push_back(marker);
37  current_time = 0.0;
38  value_const = 0.0;
39  }
40 
41  template<typename Scalar>
43  {
44  }
45 
46  template<typename Scalar>
48  {
49  this->current_time = time;
50  }
51 
52  template<typename Scalar>
54  {
55  return current_time;
56  }
57 
58  template<typename Scalar>
59  DefaultEssentialBCConst<Scalar>::DefaultEssentialBCConst(Hermes::vector<std::string> markers, Scalar value_const) : EssentialBoundaryCondition<Scalar>(markers)
60  {
61  this->value_const = value_const;
62  }
63 
64  template<typename Scalar>
65  DefaultEssentialBCConst<Scalar>::DefaultEssentialBCConst(std::string marker, Scalar value_const) : EssentialBoundaryCondition<Scalar>(Hermes::vector<std::string>())
66  {
67  this->value_const = value_const;
68  this->markers.push_back(marker);
69  }
70 
71  template<typename Scalar>
72  Scalar DefaultEssentialBCConst<Scalar>::value(double x, double y, double n_x, double n_y, double t_x, double t_y) const
73  {
74  this->warn("EssentialBoundaryCondition::Function used either for a constant condition, or not redefined for nonconstant condition.");
75  return 0.0;
76  }
77 
78  template<typename Scalar>
79  DefaultEssentialBCNonConst<Scalar>::DefaultEssentialBCNonConst(Hermes::vector<std::string> markers_,
80  ExactSolutionScalar<Scalar>* exact_solution)
81  : EssentialBoundaryCondition<Scalar>(Hermes::vector<std::string>()), exact_solution(exact_solution)
82  {
83  for (unsigned int i = 0; i < this->markers.size(); i++) this->markers.push_back(markers_[i]);
84  }
85 
86  template<typename Scalar>
87  DefaultEssentialBCNonConst<Scalar>::DefaultEssentialBCNonConst(std::string marker, ExactSolutionScalar<Scalar>* exact_solution)
88  : EssentialBoundaryCondition<Scalar>(Hermes::vector<std::string>()), exact_solution(exact_solution)
89  {
90  this->markers.push_back(marker);
91  }
92 
93  template<typename Scalar>
94  Scalar DefaultEssentialBCNonConst<Scalar>::value(double x, double y, double n_x, double n_y, double t_x, double t_y) const
95  {
96  return exact_solution->value(x, y);
97  }
98 
99  template<typename Scalar>
101  ExactSolutionVector<Scalar>* exact_solution2)
102  : EssentialBoundaryCondition<Scalar>(Hermes::vector<std::string>()), exact_solution2(exact_solution2)
103  {
104  for (unsigned int i = 0; i < this->markers.size(); i++)
105  this->markers.push_back(markers_[i]);
106  }
107 
108  template<typename Scalar>
109  DefaultEssentialBCNonConstHcurl<Scalar>::DefaultEssentialBCNonConstHcurl(std::string marker, ExactSolutionVector<Scalar>* exact_solution2)
110  : EssentialBoundaryCondition<Scalar>(Hermes::vector<std::string>()), exact_solution2(exact_solution2)
111  {
112  this->markers.push_back(marker);
113  }
114 
115  template<typename Scalar>
116  Scalar DefaultEssentialBCNonConstHcurl<Scalar>::value(double x, double y, double n_x, double n_y, double t_x, double t_y) const
117  {
118  Scalar2<Scalar> val = exact_solution2->value(x, y);
119  return val.val[0] * t_x + val.val[1] * t_y;
120  }
121 
122  template<typename Scalar>
124  {
125  }
126 
127  template<typename Scalar>
128  EssentialBCs<Scalar>::EssentialBCs(Hermes::vector<EssentialBoundaryCondition<Scalar> *> essential_bcs) : HermesAnyBC(NULL)
129  {
130  add_boundary_conditions(essential_bcs);
131  }
132 
133  template<typename Scalar>
134  EssentialBCs<Scalar>::EssentialBCs(EssentialBoundaryCondition<Scalar> * boundary_condition) : HermesAnyBC(NULL)
135  {
136  Hermes::vector<EssentialBoundaryCondition<Scalar> *> boundary_conditions;
137  boundary_conditions.push_back(boundary_condition);
138  add_boundary_conditions(boundary_conditions);
139  }
140 
141  template<typename Scalar>
143  {
144  for(typename Hermes::vector<EssentialBoundaryCondition<Scalar> *>::iterator it = boundary_conditions.begin(); it != boundary_conditions.end(); it++)
145  all.push_back(*it);
146 
147  this->markers.clear();
148  create_marker_cache();
149  }
150 
151  template<typename Scalar>
153  {
154  Hermes::vector<EssentialBoundaryCondition<Scalar> *> boundary_conditions;
155  boundary_conditions.push_back(boundary_condition);
156  add_boundary_conditions(boundary_conditions);
157  }
158 
159  template<typename Scalar>
160  typename Hermes::vector<EssentialBoundaryCondition<Scalar> *>::const_iterator EssentialBCs<Scalar>::begin() const
161  {
162  return all.begin();
163  }
164 
165  template<typename Scalar>
166  typename Hermes::vector<EssentialBoundaryCondition<Scalar> *>::const_iterator EssentialBCs<Scalar>::end() const
167  {
168  return all.end();
169  }
170 
171  template<typename Scalar>
173  {
174  }
175 
176  template<typename Scalar>
178  {
179  bool hermes_any_set = false;
180  this->markers.clear();
181  this->BCs.clear();
182  EssentialBoundaryCondition<Scalar>* any_set = NULL;
183  for(this->iterator = begin(); iterator != end(); iterator++)
184  for(Hermes::vector<std::string>::const_iterator it = (*iterator)->markers.begin(); it != (*iterator)->markers.end(); it++)
185  {
186  if(hermes_any_set)
187  throw Hermes::Exceptions::Exception("Attempt to define a BC on HERMES_ANY together with a BC on a specific part: '%s'.", it->c_str());
188  if((*it) == HERMES_ANY)
189  {
190  if(any_set != NULL)
191  throw Hermes::Exceptions::Exception("Attempt to define a BC on HERMES_ANY together with a BC on a specific part: '%s'.", any_set->markers.begin()->c_str());
192  hermes_any_set = true;
193  this->HermesAnyBC = *iterator;
194  }
195  else
196  {
197  any_set = *iterator;
198  for(int i = 0; i < this->markers.size(); i++)
199  if(this->markers[i] == *it)
200  throw Hermes::Exceptions::Exception("Attempt to define more than one description of the BC on the same part of the boundary with marker '%s'.", it->c_str());
201  this->markers.push_back(*it);
202  this->BCs.push_back(*iterator);
203  }
204  }
205  }
206 
207  template<typename Scalar>
208  EssentialBoundaryCondition<Scalar>* EssentialBCs<Scalar>::get_boundary_condition(std::string marker)
209  {
210  if(this->HermesAnyBC != NULL)
211  return this->HermesAnyBC;
212  for(int i = 0; i < this->markers.size(); i++)
213  if(this->markers[i] == marker)
214  return this->BCs[i];
215  return NULL;
216  }
217 
218  template<typename Scalar>
220  {
221  for(iterator = begin(); iterator != end(); iterator++)
222  (*iterator)->set_current_time(time);
223  }
224 
225  template HERMES_API class EssentialBoundaryCondition<double>;
226  template HERMES_API class EssentialBoundaryCondition<std::complex<double> >;
227  template HERMES_API class DefaultEssentialBCConst<double>;
228  template HERMES_API class DefaultEssentialBCConst<std::complex<double> >;
229  template HERMES_API class DefaultEssentialBCNonConst<double>;
230  template HERMES_API class DefaultEssentialBCNonConst<std::complex<double> >;
231  template HERMES_API class EssentialBCs<double>;
232  template HERMES_API class EssentialBCs<std::complex<double> >;
233  }
234 }