Hermes2D  3.0
mesh_function.cpp
1 // This file is part of Hermes2D.
2 //
3 // Hermes2D is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 
16 #include "solution.h"
17 
18 namespace Hermes
19 {
20  namespace Hermes2D
21  {
22  template<typename Scalar>
23  MeshFunctionSharedPtr<Scalar>::MeshFunctionSharedPtr(Hermes::Hermes2D::MeshFunction<Scalar> * ptr) : std::tr1::shared_ptr<Hermes::Hermes2D::MeshFunction<Scalar> >(ptr)
24  {
25  }
26 
27  template<typename Scalar>
28  MeshFunctionSharedPtr<Scalar>::MeshFunctionSharedPtr(const MeshFunctionSharedPtr& other) : std::tr1::shared_ptr<Hermes::Hermes2D::MeshFunction<Scalar> >(other)
29  {
30  }
31 
32  template<typename Scalar>
33  void MeshFunctionSharedPtr<Scalar>::operator=(const MeshFunctionSharedPtr& other)
34  {
35  std::tr1::shared_ptr<Hermes::Hermes2D::MeshFunction<Scalar> >::operator=(other);
36  }
37 
38  template<typename Scalar>
39  Hermes::Hermes2D::Solution<Scalar>* MeshFunctionSharedPtr<Scalar>::get_solution()
40  {
41  return dynamic_cast<Hermes::Hermes2D::Solution<Scalar>*>(this->get());
42  }
43 
44  template class HERMES_API MeshFunctionSharedPtr < double > ;
45  template class HERMES_API MeshFunctionSharedPtr < std::complex<double> > ;
46 
47  template<typename Scalar>
49  : Function<Scalar>()
50  {
51  this->element = nullptr;
52  }
53 
54  template<typename Scalar>
55  MeshFunction<Scalar>::MeshFunction(MeshSharedPtr mesh) :
56  Function<Scalar>()
57  {
58  this->mesh = mesh;
59  }
60 
61  template<typename Scalar>
63  {
64  free();
65  }
66 
67  template<typename Scalar>
69  {
70  return "MeshFunction";
71  }
72 
73  template<typename Scalar>
75  {
76  throw Exceptions::Exception("This instance is in fact not a Solution instance in copy().");
77  }
78 
79  template<typename Scalar>
81  {
82  copy(sln.get());
83  }
84 
85  template<typename Scalar>
87  {
88  bool okay = true;
89  if (this->mesh == nullptr)
90  okay = false;
91  try
92  {
93  if (this->mesh->get_max_element_id() < 0)
94  throw Hermes::Exceptions::Exception("Internal exception.");
95  this->mesh->get_element(this->mesh->get_max_element_id() - 1);
96  }
97  catch (std::exception& e)
98  {
99  std::cout << e.what();
100  okay = false;
101  }
102  return okay;
103  }
104 
105  template<typename Scalar>
107  {
108  }
109 
110  template<typename Scalar>
112  {
113  }
114 
115  template<typename Scalar>
117  {
118  this->free();
119  init();
120  }
121 
122  template<typename Scalar>
124  {
125  throw Exceptions::MethodNotOverridenException("MeshFunction<Scalar>::add");
126  }
127 
128  template<typename Scalar>
130  {
131  throw Exceptions::MethodNotOverridenException("MeshFunction<Scalar>::multiply");
132  }
133 
134  template<>
136  {
137  this->check();
138 
139  Quad2D *old_quad = this->get_quad_2d();
140  this->set_quad_2d(&g_quad_lin);
141 
142  double max = std::numeric_limits<double>::min();
143 
144  int component = 0;
145  int value_type = 0;
146  int item = item_;
147 
148  if (item >= 0x40)
149  {
150  component = 1;
151  item >>= 6;
152  }
153  while (!(item & 1))
154  {
155  item >>= 1;
156  value_type++;
157  }
158 
159  item = item_;
160 
161  Element* e;
162  for_all_active_elements(e, this->mesh)
163  {
164  this->set_active_element(e);
165  this->set_quad_order(1, item);
166  const double* val = this->get_values(component, value_type);
167  for (int i = 0; i < (e->is_triangle() ? 3 : 4); i++)
168  {
169  double v = val[i];
170  if (v > max)
171  max = v;
172  }
173  }
174 
175  this->set_quad_2d(old_quad);
176  return max;
177  }
178 
179  template<>
180  std::complex<double> MeshFunction<std::complex<double> >::get_approx_max_value(int item_)
181  {
182  this->check();
183  this->warn("Asked for a max value of a complex function.");
184  return std::numeric_limits<std::complex<double> >::min();
185  }
186 
187  template<>
189  {
190  this->check();
191 
192  Quad2D *old_quad = this->get_quad_2d();
193  this->set_quad_2d(&g_quad_lin);
194 
195  double min = std::numeric_limits<double>::max();
196 
197  int component = 0;
198  int value_type = 0;
199  int item = item_;
200 
201  if (item >= 0x40)
202  {
203  component = 1;
204  item >>= 6;
205  }
206  while (!(item & 1))
207  {
208  item >>= 1;
209  value_type++;
210  }
211 
212  item = item_;
213 
214  Element* e;
215  for_all_active_elements(e, this->mesh)
216  {
217  this->set_active_element(e);
218  this->set_quad_order(1, item);
219  const double* val = this->get_values(component, value_type);
220  for (int i = 0; i < (e->is_triangle() ? 3 : 4); i++)
221  {
222  double v = val[i];
223  if (v < min)
224  min = v;
225  }
226  }
227 
228  this->set_quad_2d(old_quad);
229  return min;
230  }
231 
232  template<>
233  std::complex<double> MeshFunction<std::complex<double> >::get_approx_min_value(int item_)
234  {
235  this->check();
236  this->warn("Asked for a min value of a complex function.");
237  return std::numeric_limits<std::complex<double> >::max();
238  }
239 
240  template<typename Scalar>
242  {
244  }
245 
246  template<typename Scalar>
247  MeshSharedPtr MeshFunction<Scalar>::get_mesh() const
248  {
249  return mesh;
250  }
251 
252  template<typename Scalar>
254  {
255  if (update)
256  this->update_refmap();
257  return &refmap;
258  }
259 
260  template<typename Scalar>
262  {
263  if (quad_2d == nullptr)
264  throw Exceptions::NullException(1);
266  refmap.set_quad_2d(quad_2d);
267  }
268 
269  template<typename Scalar>
271  {
272  if (e && !e->active)
273  throw Hermes::Exceptions::Exception("Cannot select inactive element. Wrong mesh?");
274 
276  mode = e->get_mode();
277  refmap.set_active_element(e);
278  }
279 
280  template<typename Scalar>
282  {
283  refmap.force_transform(this->sub_idx, this->ctm);
284  }
285 
286  template class HERMES_API MeshFunction < double > ;
287  template class HERMES_API MeshFunction < std::complex<double> > ;
288  }
289 }
Definition: adapt.h:24
MeshFunction()
Empty constructor.
virtual double get_approx_min_value(int item=H2D_FN_VAL_0)
Return the approximate minimum value of this instance.
Stores one element of a mesh.
Definition: element.h:107
virtual void init()
Internal.
virtual void copy(const MeshFunction< Scalar > *sln)
Copy from sln to this instance.
virtual bool isOkay() const
State querying helpers.
Represents a function defined on a mesh.
Definition: mesh_function.h:56
::xsd::cxx::tree::exception< char > exception
Root of the C++/Tree exception hierarchy.
Used to pass the instances of Space around.
Definition: space.h:34
std::string getClassName() const
Internal.
virtual void multiply(Scalar coef)
Multiplies the function represented by this class by the given coefficient.
virtual void force_transform(uint64_t sub_idx, Trf *ctm)
For internal use only.
Definition: function.cpp:141
Represents an arbitrary function defined on an element.
Definition: function.h:106
virtual void add(MeshFunctionSharedPtr< Scalar > &other_mesh_function, SpaceSharedPtr< Scalar > target_space)
Element * element
The active element.
virtual void reinit()
Internal.
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
Represents the reference mapping.
Definition: refmap.h:40
bool active
0 = active, no sons; 1 = inactive (refined), has sons
Definition: element.h:114
Represents the solution of a PDE.
Definition: api2d.h:35
virtual Scalar get_approx_max_value(int item=H2D_FN_VAL_0)
Return the approximate maximum value of this instance.
virtual void free()
Frees all precalculated tables.
virtual ~MeshFunction()
Destructor.