Hermes2D  3.0
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 "function.h"
17 #include "../mesh/element.h"
18 
19 namespace Hermes
20 {
21  namespace Hermes2D
22  {
23  template<typename Scalar>
24  int Function<Scalar>::idx2mask[H2D_NUM_FUNCTION_VALUES][2] =
25  {
26  { H2D_FN_VAL_0, H2D_FN_VAL_1 }, { H2D_FN_DX_0, H2D_FN_DX_1 }, { H2D_FN_DY_0, H2D_FN_DY_1 },
27 #ifdef H2D_USE_SECOND_DERIVATIVES
28  { H2D_FN_DXX_0, H2D_FN_DXX_1 }, { H2D_FN_DYY_0, H2D_FN_DYY_1 }, { H2D_FN_DXY_0, H2D_FN_DXY_1 }
29 #endif
30  };
31 
32  template<typename Scalar>
34  : Transformable()
35  {
36  order = -1;
37  memset(quads, 0, H2D_MAX_QUADRATURES*sizeof(Quad2D*));
38  this->values_valid = false;
39  }
40 
41  template<typename Scalar>
43  {
44  }
45 
46  template<typename Scalar>
48  {
49  return order;
50  }
51 
52  template<typename Scalar>
53  int Function<Scalar>::get_edge_fn_order(unsigned char) const
54  {
55  return order;
56  }
57 
58  template<typename Scalar>
59  void Function<Scalar>::set_quad_order(unsigned short order, unsigned short mask)
60  {
61  precalculate(order, mask);
62  this->order = order;
63  }
64 
65  template<typename Scalar>
67  {
69  this->invalidate_values();
70  }
71 
72  template<typename Scalar>
73  const Scalar* Function<Scalar>::get_values(int component, int item) const
74  {
75  return values[component][item];
76  }
77 
78  template<typename Scalar>
80  {
81  int i;
82  this->invalidate_values();
83 
84  // check to see if we already have the quadrature
85  for (i = 0; i < H2D_MAX_QUADRATURES; i++)
86  {
87  if (quads[i] == quad_2d)
88  {
89  cur_quad = i;
90  return;
91  }
92  }
93 
94  // if not, add the quadrature to a free slot
95  for (i = 0; i < H2D_MAX_QUADRATURES; i++)
96  {
97  if (quads[i] == nullptr)
98  {
99  quads[i] = quad_2d;
100  cur_quad = i;
101  return;
102  }
103  }
104 
105  throw Hermes::Exceptions::Exception("too many quadratures.");
106  }
107 
108  template<typename Scalar>
109  void Function<Scalar>::precalculate(unsigned short order, unsigned short mask)
110  {
111  this->values_valid = true;
112  }
113 
114  template<typename Scalar>
116  {
118  this->invalidate_values();
119  }
120 
121  template<typename Scalar>
123  {
125  this->invalidate_values();
126  }
127 
128  template<typename Scalar>
130  {
131  return quads[cur_quad];
132  }
133 
134  template<typename Scalar>
136  {
137  this->values_valid = false;
138  }
139 
140  template<typename Scalar>
141  void Function<Scalar>::force_transform(uint64_t sub_idx, Trf* ctm)
142  {
143  Transformable::force_transform(sub_idx, ctm);
144  this->invalidate_values();
145  }
146 
147  template<typename Scalar>
149  {
151  this->invalidate_values();
152  }
153 
154  template<typename Scalar>
156  {
158  this->invalidate_values();
159  }
160 
161  template<typename Scalar>
163  {
164  return num_components;
165  }
166 
167  template<typename Scalar>
168  const Scalar* Function<Scalar>::get_fn_values(int component) const
169  {
170  assert(this->values_valid);
171  return &values[component][0][0];
172  }
173 
174  template<typename Scalar>
175  const Scalar* Function<Scalar>::get_dx_values(int component) const
176  {
177  assert(this->values_valid);
178  return &values[component][1][0];
179  }
180 
181  template<typename Scalar>
182  const Scalar* Function<Scalar>::get_dy_values(int component) const
183  {
184  assert(this->values_valid);
185  return &values[component][2][0];
186  }
187 
188 #ifdef H2D_USE_SECOND_DERIVATIVES
189  template<typename Scalar>
190  const Scalar* Function<Scalar>::get_dxx_values(int component) const
191  {
192  assert(this->values_valid);
193  return &values[component][3][0];
194  }
195 
196  template<typename Scalar>
197  const Scalar* Function<Scalar>::get_dyy_values(int component) const
198  {
199  assert(this->values_valid);
200  return &values[component][4][0];
201  }
202 
203  template<typename Scalar>
204  const Scalar* Function<Scalar>::get_dxy_values(int component) const
205  {
206  assert(this->values_valid);
207  return &values[component][5][0];
208  }
209 #endif
210 
211  template<typename Scalar>
212  Scalar* Function<Scalar>::deep_copy_array(int component, int item) const
213  {
214  assert(this->values_valid);
215  unsigned char np = this->quads[this->cur_quad]->get_num_points(this->order, this->element->get_mode());
216  Scalar* toReturn = malloc_with_check<Scalar>(np);
217  memcpy(toReturn, this->get_values(component, item), sizeof(Scalar)* np);
218  return toReturn;
219  }
220 
221  template class HERMES_API Function < double > ;
222  template class HERMES_API Function < std::complex<double> > ;
223  }
224 }
virtual void set_active_element(Element *e)
Definition: adapt.h:24
virtual void set_active_element(Element *e)
Sets the active element.
Definition: function.cpp:66
virtual void push_transform(int son)
Definition: function.cpp:115
virtual void push_transform(int son)
Stores one element of a mesh.
Definition: element.h:107
virtual void reset_transform()
Empties the stack, loads identity transform.
virtual int get_edge_fn_order(unsigned char edge) const
Returns the polynomial degree of the function at given edge. To be overridden in derived classes...
Definition: function.cpp:53
Quad2D * get_quad_2d() const
Returns the current quadrature points.
Definition: function.cpp:129
void set_quad_order(unsigned short order, unsigned short mask=H2D_FN_DEFAULT)
Definition: function.cpp:59
unsigned char get_num_components() const
Returns the number of components of the function being represented by the class.
Definition: function.cpp:162
virtual const Scalar * get_dx_values(int component=0) const
Returns the x partial derivative.
Definition: function.cpp:175
virtual const Scalar * get_dy_values(int component=0) const
Returns the y partial derivative.
Definition: function.cpp:182
virtual void force_transform(uint64_t sub_idx, Trf *ctm)
For internal use only.
virtual int get_fn_order() const
Returns the polynomial degree of the function being represented by the class.
Definition: function.cpp:47
virtual void precalculate(unsigned short order, unsigned short mask)
precalculates the current function at the current integration points.
Definition: function.cpp:109
virtual void force_transform(uint64_t sub_idx, Trf *ctm)
For internal use only.
Definition: function.cpp:141
virtual void set_transform(uint64_t idx)
virtual void pop_transform()
Definition: function.cpp:122
virtual void set_transform(uint64_t idx)
Definition: function.cpp:155
2D transformation.
Definition: transformable.h:29
Represents an arbitrary function defined on an element.
Definition: function.h:106
bool values_valid
Flag that the data are not 'dirty'.
Definition: function.h:205
Function()
Default constructor.
Definition: function.cpp:33
Scalar * deep_copy_array(int component=0, int item=0) const
Returns function values.
Definition: function.cpp:212
virtual void set_quad_2d(Quad2D *quad_2d)
Selects the quadrature points in which the function will be evaluated.
Definition: function.cpp:79
virtual const Scalar * get_fn_values(int component=0) const
Returns function values.
Definition: function.cpp:168
Quad2D * quads[H2D_MAX_QUADRATURES]
List of available quadratures.
Definition: function.h:226
int order
Current function polynomial order.
Definition: function.h:215
virtual void reset_transform()
Empties the stack, loads identity transform.
Definition: function.cpp:148