v0.13.0
FieldBlas.hpp
Go to the documentation of this file.
1 /** \file FieldBlas.hpp
2  * \brief Field basic algebra
3  * \ingroup mofem_is_managers
4  *
5  * Managing problems, build and partitioning.
6  *
7  */
8 
9 /*
10  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>
17  */
18 
19 #ifndef __FIELD_BLAS_HPP__
20 #define __FIELD_BLAS_HPP__
21 
22 #include "UnknownInterface.hpp"
23 
24 namespace MoFEM {
25 
26 /**
27  * \brief Basic algebra on fields
28  * \ingroup mofem_field_algebra
29  *
30  */
31 struct FieldBlas : public UnknownInterface {
32 
33  MoFEMErrorCode query_interface(boost::typeindex::type_index type_index,
34  UnknownInterface **iface) const;
35 
37  bool dEbug;
38 
39  FieldBlas(const MoFEM::Core &core);
40 
41  ~FieldBlas() = default;
42 
43  /**
44  * @brief function to set a field value
45  *
46  */
48  boost::function<double(const double field_val)>;
49 
50  /**
51  * @brief
52  *
53  */
54  using OneFieldFunctionOnEntities = boost::function<MoFEMErrorCode(
55  boost::shared_ptr<FieldEntity> field_entity_ptr)>;
56 
57  /**
58  * @brief
59  * \param y_field_value_reference field "y_field" values
60  * \param x_field_value
61  */
62  using TwoFieldFunctionOnValues = boost::function<double(
63  const double y_field_value_reference, const double x_field_value)>;
64 
65  /**
66  * @brief
67  * \param y_field_entity_ptr pointer to second "y_field" field entities
68  * \param x_field_entity_ptr pointer to first "x_field" field entities
69  */
70  using TwoFieldFunctionOnEntities = boost::function<MoFEMErrorCode(
71  boost::shared_ptr<FieldEntity> y_field_entity_ptr,
72  const boost::shared_ptr<FieldEntity> x_field_entity_ptr)>;
73 
74  /** \brief filed lambda
75  * \ingroup mofem_field_algebra
76  *
77  * Do calculation on one field using lambda function with entity field input
78  *
79  * \code
80  auto field_abs = [&](
81  boost::shared_ptr<FieldEntity> ent_ptr) {
82  MoFEMFunctionBeginHot;
83  auto field_data = ent_ptr->getEntFieldData();
84  for (auto &v : field_data)
85  v = std::abs(v);
86  MoFEMFunctionReturnHot(0);
87  };
88 
89  CHKERR
90  m_field.getInterface<FieldBlas>()->fieldLambdaOnEntities(field_abs,
91  "U", block_ents_ptr);
92  * \endcode
93  *
94  * \param function f(double &x, double)
95  * \param field_name name of field
96  * \param ents_ptr execute lambda function only on entities given by pointer
97  to range
98  *
99  */
101  const std::string field_name,
102  Range *ents_ptr = nullptr);
103 
104  /** \brief filed lambda
105  * \ingroup mofem_field_algebra
106  *
107  * Do calculation on one field using lambda function with field value input
108  *
109  * \code
110  auto scale_field = [&](const double val) {
111  double time = ts_t; // global variable
112  return val * time;
113  };
114  CHKERR
115  m_field.getInterface<FieldBlas>()->fieldLambdaOnValues(scale_field,
116  "U", false, block_ents_ptr);
117  * \endcode
118  *
119  * \param function f(const double)
120  * \param field_name name of field
121  * \param ents_ptr execute lambda function only on entities given by
122  pointer to range
123  *
124  */
126  const std::string field_name,
127  Range *ents_ptr = nullptr);
128 
129  /** \brief filed lambda
130  * \ingroup mofem_field_algebra
131  *
132  * Do calculation on two fields and save result to field fy
133  * input function usees field values
134  * \code
135  auto field_axpy = [&](const double val_y,
136  const double val_x) {
137  // y = 2 * x + y
138  return 2 * val_x + val_y;
139  };
140  CHKERR
141  m_field.getInterface<FieldBlas>()->fieldLambdaOnValues(field_axpy,
142  "U", "P" false, block_ents_ptr);
143  * \endcode
144  *
145  * \param function f(double &x, double)
146  * \param field_name_x name of field_x
147  * \param field_name_y name of field_y
148  * \param error_if_missing throw error if missing entities of field y
149  * \param ents_ptr execute lambda function only on entities given by pointer
150  to range
151  *
152  */
154  const std::string field_name_x,
155  const std::string field_name_y,
156  bool error_if_missing = false,
157  Range *ents_ptr = nullptr);
158 
159  /** \brief filed lambda
160  * \ingroup mofem_field_algebra
161  *
162  * Do calculation on two fields and save result to field fy
163  * input function usees field entities
164  *
165  * \code
166  auto vector_times_scalar_field = [&](boost::shared_ptr<FieldEntity> ent_ptr_y,
167  boost::shared_ptr<FieldEntity> ent_ptr_x) {
168  MoFEMFunctionBeginHot;
169  auto x_data = ent_ptr_x->getEntFieldData();
170  auto y_data = ent_ptr_y->getEntFieldData();
171  const auto size_x = x_data.size(); // scalar
172  const auto size_y = y_data.size(); // vector
173 
174  for (size_t dd = 0; dd != size_y; ++dd)
175  y_data[dd] *= x_data[0];
176 
177  MoFEMFunctionReturnHot(0);
178  };
179 
180  CHKERR
181  m_field.getInterface<FieldBlas>()->fieldLambdaOnValues(vector_times_scalar_field,
182  "U", "P" false, block_ents_ptr);
183 
184  * \endcode
185  *
186  * \param function f(double &x, double)
187  * \param field_name_x name of field_x
188  * \param field_name_y name of field_y
189  * \param error_if_missing throw error if missing entities of field y
190  * \param ents_ptr execute lambda function only on entities given by pointer
191  to range
192  *
193  */
195  const std::string field_name_x,
196  const std::string field_name_y,
197  bool error_if_missing = false,
198  Range *ents_ptr = nullptr);
199 
200  /**
201  * @deprecated This is with obsolete last option
202  *
203  * @param lambda
204  * @param field_name_x
205  * @param field_name_y
206  * @param error_if_missing
207  * @param create_if_missing
208  * @return DEPRECATED
209  */
211  const std::string field_name_x,
212  const std::string field_name_y,
213  bool error_if_missing,
214  bool create_if_missing);
215 
216  /** \brief axpy fields
217  * \ingroup mofem_field_algebra
218  *
219  * field_y = field_y + alpha*field_x
220  *
221  * \param alpha
222  * \param field_name_x name of field_x
223  * \param field_name_y name of field_y
224  * \param error_if_missing throw error if entity/dof exist in field_x but not
225  * \param ents_ptr execute lambda function only on entities given by pointer
226  * on field_y
227  *
228  */
229  MoFEMErrorCode fieldAxpy(const double alpha, const std::string field_name_x,
230  const std::string field_name_y,
231  bool error_if_missing = false,
232  Range *ents_ptr = nullptr);
233 
234  /** \deprecated axpy fields
235  * \ingroup mofem_field_algebra
236  *
237  * field_y = field_y + alpha*field_x
238  *
239  * \param alpha
240  * \param field_name_x name of field_x
241  * \param field_name_y name of field_y
242  * \param error_if_missing throw error if entity/dof exist in field_x but not
243  * on field_y \param create_if_missing creat dof in field_y from field_x if it
244  * is not database
245  *
246  */
248  const std::string field_name_x,
249  const std::string field_name_y,
250  bool error_if_missing,
251  bool create_if_missing);
252 
253  /** \brief copy and scale fields
254  * \ingroup mofem_field_algebra
255  *
256  * field_y = alpha*field_x
257  *
258  * \param alpha
259  * \param field_name_x name of field_x
260  * \param field_name_y name of field_y
261  * \param error_if_missing throw error if entity/dof exist in field_x but not
262  * \param ents_ptr execute lambda function only on entities given by pointer
263  * on field_y \param create_if_missing creat dof in field_y from field_x if it
264  * is not database
265  *
266  */
267  MoFEMErrorCode fieldCopy(const double alpha, const std::string field_name_x,
268  const std::string field_name_y,
269  bool error_if_missing = false,
270  Range *ents_ptr = nullptr);
271 
273  const std::string field_name_x,
274  const std::string field_name_y,
275  bool error_if_missing,
276  bool create_if_missing);
277 
278  /** \brief field scale
279  * \ingroup mofem_field_algebra
280  *
281  * \param scale field by value
282  * \param field_name name of field
283  * \param error_if_missing throw error if missing entities of field y
284  * \param ents_ptr execute lambda function only on entities given by
285  pointer to range
286  */
287  MoFEMErrorCode fieldScale(const double alpha, const std::string field_name,
288  Range *ents_ptr = nullptr);
289 
291  boost::function<MoFEMErrorCode(VectorAdaptor &&field_data, double *xcoord,
292  double *ycoord, double *zcoord)>;
293 
294  /** \brief Set DOFs on vertices using user function
295  * \ingroup mofem_field_algebra
296  *
297  * Example:
298  *
299  * \code
300  * auto do_something = [&](VectorAdaptor &field_data, double *x,
301  * double *y, double *z) {
302  * MoFEMFunctionBegin;
303  * field_data[0] = (*x);
304  * field_data[1] = (*y);
305  * field_data[2] = (*z);
306  * MoFEMFunctionReturn(0);
307  * };
308  * CHKERR m_field.getInterface<FieldBlas>()->setVertexDofs(set_distance,
309  * "DISP"); \endcode
310  *
311  * \note Function works both ways, using it coordinates can be set from
312  * field.
313  *
314  * \param lambda function evaluating field at points
315  * \param field_name is a field name
316  * \param verts pointer to vertices if null all vertices in the field are
317  * evaluated)
318  *
319  */
321  const std::string field_name,
322  Range *verts = nullptr);
323 
324  /** \deprecated scale field
325  * \ingroup mofem_field_algebra
326  *
327  * \param val is a set parameter
328  * \field_name is a field name
329  *
330  */
331  MoFEMErrorCode setField(const double val, const EntityType type,
332  const std::string field_name);
333 
334  /** \brief set field
335  * \ingroup mofem_field_algebra
336  *
337  * field_y = val
338  *
339  * \param val
340  * \param entity type
341  * \param field_name
342  *
343  */
344  MoFEMErrorCode setField(const double val, const EntityType type,
345  const Range &ents, const std::string field_name);
346 
347  /** \brief set field
348  * \ingroup mofem_field_algebra
349  *
350  * field_y = val
351  *
352  * \param val
353  * \param field_name
354  *
355  */
356  MoFEMErrorCode setField(const double val, const std::string field_name);
357 
358 };
359 
360 } // namespace MoFEM
361 
362 #endif // __FIELD_BLAS_HPP__
MoFEM interface.
constexpr double alpha
#define DEPRECATED
Definition: definitions.h:30
constexpr double lambda
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:67
VectorShallowArrayAdaptor< double > VectorAdaptor
Definition: Types.hpp:126
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
Core (interface) class.
Definition: Core.hpp:92
Deprecated interface functions.
Basic algebra on fields.
Definition: FieldBlas.hpp:31
DEPRECATED MoFEMErrorCode fieldLambda(TwoFieldFunctionOnValues lambda, const std::string field_name_x, const std::string field_name_y, bool error_if_missing, bool create_if_missing)
Definition: FieldBlas.cpp:270
MoFEMErrorCode fieldAxpy(const double alpha, const std::string field_name_x, const std::string field_name_y, bool error_if_missing=false, Range *ents_ptr=nullptr)
axpy fields
Definition: FieldBlas.cpp:282
boost::function< MoFEMErrorCode(boost::shared_ptr< FieldEntity > field_entity_ptr)> OneFieldFunctionOnEntities
Definition: FieldBlas.hpp:55
MoFEMErrorCode fieldLambdaOnEntities(OneFieldFunctionOnEntities lambda, const std::string field_name, Range *ents_ptr=nullptr)
filed lambda
Definition: FieldBlas.cpp:63
boost::function< double(const double y_field_value_reference, const double x_field_value)> TwoFieldFunctionOnValues
Definition: FieldBlas.hpp:63
MoFEMErrorCode setVertexDofs(VertexCoordsFunction lambda, const std::string field_name, Range *verts=nullptr)
Set DOFs on vertices using user function.
Definition: FieldBlas.cpp:333
boost::function< MoFEMErrorCode(VectorAdaptor &&field_data, double *xcoord, double *ycoord, double *zcoord)> VertexCoordsFunction
Definition: FieldBlas.hpp:292
MoFEMErrorCode setField(const double val, const EntityType type, const std::string field_name)
Definition: FieldBlas.cpp:401
boost::function< double(const double field_val)> OneFieldFunctionOnValues
function to set a field value
Definition: FieldBlas.hpp:48
MoFEMErrorCode fieldScale(const double alpha, const std::string field_name, Range *ents_ptr=nullptr)
field scale
Definition: FieldBlas.cpp:322
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
Definition: FieldBlas.cpp:23
MoFEMErrorCode fieldLambdaOnValues(OneFieldFunctionOnValues lambda, const std::string field_name, Range *ents_ptr=nullptr)
filed lambda
Definition: FieldBlas.cpp:34
FieldBlas(const MoFEM::Core &core)
Definition: FieldBlas.cpp:30
const MoFEM::Interface & cOre
Definition: FieldBlas.hpp:36
~FieldBlas()=default
boost::function< MoFEMErrorCode(boost::shared_ptr< FieldEntity > y_field_entity_ptr, const boost::shared_ptr< FieldEntity > x_field_entity_ptr)> TwoFieldFunctionOnEntities
Definition: FieldBlas.hpp:72
MoFEMErrorCode fieldCopy(const double alpha, const std::string field_name_x, const std::string field_name_y, bool error_if_missing=false, Range *ents_ptr=nullptr)
copy and scale fields
Definition: FieldBlas.cpp:303
base class for all interface classes