deal2lkit: A ToolKit library for Deal.II
dof_utilities.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2015 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
16 #ifndef _d2k_dof_utilities_h
17 #define _d2k_dof_utilities_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal.II/base/utilities.h>
22 #include <typeinfo>
24 #include <deal.II/fe/fe.h>
25 #include <deal.II/fe/fe_values.h>
26 
27 
28 #include <sstream>
29 
30 using namespace dealii;
31 
32 
33 
41 #ifdef DEAL_II_WITH_TRILINOS
42 
43 
44 #include <Sacado.hpp>
45 typedef Sacado::Fad::DFad<double> Sdouble;
46 typedef Sacado::Fad::DFad<Sdouble> SSdouble;
47 #endif
48 
49 D2K_NAMESPACE_OPEN
50 namespace DOFUtilities
51 {
57  template <typename Number, typename VEC>
58  void
59  extract_local_dofs (const VEC &global_vector,
60  const std::vector<types::global_dof_index> &local_dof_indices,
61  std::vector<Number> &independent_local_dofs)
62  {
63  AssertDimension(local_dof_indices.size(),independent_local_dofs.size());
64 
65  const unsigned int dofs_per_cell = local_dof_indices.size();
66 
67  for (unsigned int i=0; i < dofs_per_cell; ++i)
68  {
69  if (typeid(Number) == typeid(double))
70  {
71  independent_local_dofs[i] = global_vector (local_dof_indices[i]);
72  }
73 #ifdef DEAL_II_WITH_TRILINOS
74  else if (typeid(Number) == typeid(Sdouble))
75  {
76  Sdouble ildv(dofs_per_cell, i, global_vector (local_dof_indices[i]));
77  ((Sdouble &)independent_local_dofs[i]) = ildv;
78  }
79  else if (typeid(Number) == typeid(SSdouble))
80  {
81  SSdouble ildv(dofs_per_cell, i, global_vector(local_dof_indices[i]));
82  ildv.val() = Sdouble(dofs_per_cell, i, global_vector(local_dof_indices[i]));
83  ((SSdouble &)independent_local_dofs[i]) = ildv;
84  }
85 #endif
86  else
87  {
88  Assert (false, ExcNotImplemented());
89  }
90  }
91  }
92 
100  template <int dim, int spacedim, typename Number>
101  void
103  const std::vector<Number> &independent_local_dof_values,
104  std::vector <std::vector<Number> > &us)
105 
106  {
107  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
108  const unsigned int n_q_points = fe_values.n_quadrature_points;
109  const unsigned int n_components = fe_values.get_fe().n_components();
110 
111  AssertDimension(us.size(), n_q_points);
112  AssertDimension(us[0].size(), n_components);
113  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
114 
115  for (unsigned int q=0; q<n_q_points; ++q)
116  {
117  us[q] = std::vector<Number> (n_components,0.0);
118  for (unsigned int i=0; i<dofs_per_cell; ++i)
119  for (unsigned int j=0; j<n_components; ++j)
120  {
122  us[q][j] += independent_local_dof_values[i]*fe_values[s].value(i,q);
123  }
124  }
125  }
126 
134  template <int dim, int spacedim, typename Number>
135 
136  void
138  const std::vector<Number> &independent_local_dof_values,
139  const FEValuesExtractors::Vector &vector_variable,
140  std::vector <Tensor <1, spacedim, Number> > &us)
141 
142  {
143  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
144  const unsigned int n_q_points = fe_values.n_quadrature_points;
145 
146  AssertDimension(us.size(), n_q_points);
147  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
148 
149  for (unsigned int q=0; q<n_q_points; ++q)
150  {
151  us[q] = 0;
152  for (unsigned int i=0; i<dofs_per_cell; ++i)
153  if (fe_values[vector_variable].value(i,q).norm() > 0.0)
154  for (unsigned int j=0; j<spacedim; ++j)
155  us[q][j] += independent_local_dof_values[i]*fe_values[vector_variable].value(i,q)[j];
156  }
157  }
158 
166  template <int dim, int spacedim, typename Number>
167  void
169  const std::vector<Number> &independent_local_dof_values,
170  const FEValuesExtractors::Scalar &scalar_variable,
171  std::vector <Number> &us)
172 
173  {
174  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
175  const unsigned int n_q_points = fe_values.n_quadrature_points;
176 
177  AssertDimension(us.size(), n_q_points);
178  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
179 
180  for (unsigned int q=0; q<n_q_points; ++q)
181  {
182  us[q] = 0;
183  for (unsigned int i=0; i<dofs_per_cell; ++i)
184  us[q] += independent_local_dof_values[i]*fe_values[scalar_variable].value(i,q);
185  }
186  }
187 
188 
196  template <int dim, int spacedim, typename Number>
197  void
199  const std::vector<Number> &independent_local_dof_values,
200  const FEValuesExtractors::Vector &vector_variable,
201  std::vector <Number> &us)
202 
203  {
204  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
205  const unsigned int n_q_points = fe_values.n_quadrature_points;
206 
207  AssertDimension(us.size(), n_q_points);
208  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
209 
210  for (unsigned int q=0; q<n_q_points; ++q)
211  {
212  us[q] = 0;
213  for (unsigned int i=0; i<dofs_per_cell; ++i)
214  us[q] += independent_local_dof_values[i]*fe_values[vector_variable].divergence(i,q);
215  }
216  }
217 
218 
226  template <int dim, int spacedim, typename Number>
227  void
229  const std::vector<Number> &independent_local_dof_values,
230  const FEValuesExtractors::Vector &vector_variable,
231  std::vector <Tensor <2, spacedim, Number> > &grad_us)
232  {
233  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
234  const unsigned int n_q_points = fe_values.n_quadrature_points;
235 
236  AssertDimension(grad_us.size(), n_q_points);
237  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
238 
239 
240  for (unsigned int q=0; q<n_q_points; ++q)
241  {
242  grad_us[q] = 0;
243  for (unsigned int i=0; i<dofs_per_cell; ++i)
244  for (unsigned int d=0; d<spacedim; ++d)
245  for (unsigned int dd=0; dd<spacedim; ++dd)
246  grad_us[q][d][dd] += independent_local_dof_values[i]*fe_values[vector_variable].gradient(i,q)[d][dd];
247  }
248 
249  }
250 
251 
259  template <int dim, int spacedim, typename Number>
260  void
262  const std::vector<Number> &independent_local_dof_values,
263  const FEValuesExtractors::Vector &vector_variable,
264  std::vector <Tensor <1, (spacedim > 2 ? spacedim : 1), Number> > &curl_us)
265  {
266  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
267  const unsigned int n_q_points = fe_values.n_quadrature_points;
268 
269  AssertDimension(curl_us.size(), n_q_points);
270  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
271 
272 
273  for (unsigned int q=0; q<n_q_points; ++q)
274  {
275  curl_us[q] = 0;
276  for (unsigned int i=0; i<dofs_per_cell; ++i)
277  {
278  auto c = fe_values[vector_variable].curl(i,q);
279  for (unsigned int d=0; d<(spacedim > 2 ? spacedim : 1); ++d)
280  curl_us[q][d] += independent_local_dof_values[i]*c[d];
281  }
282  }
283 
284  }
285 
286 
294  template <int dim, int spacedim, typename Number>
295  void
297  const std::vector<Number> &independent_local_dof_values,
298  const FEValuesExtractors::Scalar &scalar_variable,
299  std::vector <Tensor <1, spacedim, Number> > &grad_us)
300  {
301  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
302  const unsigned int n_q_points = fe_values.n_quadrature_points;
303 
304  AssertDimension(grad_us.size(), n_q_points);
305  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
306 
307 
308  for (unsigned int q=0; q<n_q_points; ++q)
309  {
310  grad_us[q] = 0;
311  for (unsigned int i=0; i<dofs_per_cell; ++i)
312  for (unsigned int d=0; d<spacedim; ++d)
313  grad_us[q][d] += independent_local_dof_values[i]*fe_values[scalar_variable].gradient(i,q)[d];
314  }
315 
316  }
317 
318 
326  template <int dim, int spacedim, typename Number>
327  void
329  const std::vector<Number> &independent_local_dof_values,
330  const FEValuesExtractors::Vector &vector_variable,
331  std::vector <Tensor <2, spacedim, Number> > &Fs)
332  {
333  const unsigned int n_q_points = fe_values.n_quadrature_points;
334 
335  AssertDimension(Fs.size(), n_q_points);
336 
337  DOFUtilities::get_gradients (fe_values, independent_local_dof_values, vector_variable, Fs);
338 
339  for (unsigned int q=0; q<n_q_points; ++q)
340  for (unsigned int d=0; d<dim; ++d)
341  Fs[q][d][d] += 1.0; // I + grad(u)
342  }
343 
351  template <int dim, int spacedim, typename Number>
352  void
354  const std::vector<Number> &independent_local_dof_values,
355  const FEValuesExtractors::Vector &vector_variable,
356  std::vector <Tensor <2, spacedim, Number> > &grad_us)
357  {
358  const unsigned int n_q_points = fe_values.n_quadrature_points;
359 
360  AssertDimension(grad_us.size(), n_q_points);
361 
362  DOFUtilities::get_gradients (fe_values, independent_local_dof_values, vector_variable, grad_us);
363  for (unsigned int q=0; q<n_q_points; ++q)
364  {
365  grad_us[q] += transpose(grad_us[q]);
366  grad_us[q] *= 0.5;
367  }
368 
369  }
370 
371 
379  template <int dim, int spacedim, typename Number>
380 
381  void
383  const std::vector<Number> &independent_local_dof_values,
384  const FEValuesExtractors::Scalar &variable,
385  std::vector <Number> &us)
386 
387  {
388  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
389  const unsigned int n_q_points = fe_values.n_quadrature_points;
390 
391  AssertDimension(us.size(), n_q_points);
392  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
393 
394  for (unsigned int q=0; q<n_q_points; ++q)
395  {
396  us[q] = 0;
397  for (unsigned int i=0; i<dofs_per_cell; ++i)
398  us[q] += independent_local_dof_values[i]*trace(fe_values[variable].hessian(i,q));
399  }
400  }
401 
402 
410  template <int dim, int spacedim, typename Number>
411 
412  void
414  const std::vector<Number> &independent_local_dof_values,
415  const FEValuesExtractors::Vector &variable,
416  std::vector <Tensor<1,spacedim,Number> > &us)
417 
418  {
419  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
420  const unsigned int n_q_points = fe_values.n_quadrature_points;
421 
422  AssertDimension(us.size(), n_q_points);
423  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
424 
425  for (unsigned int d=0; d<spacedim; ++d)
426  for (unsigned int q=0; q<n_q_points; ++q)
427  {
428  us[q][d] = 0;
429  for (unsigned int i=0; i<dofs_per_cell; ++i)
430  us[q][d] += independent_local_dof_values[i]*trace(fe_values[variable].hessian(i,q)[d]);
431  }
432  }
433 
441  template <int dim, int spacedim, typename Number>
442 
443  void
445  const std::vector<Number> &independent_local_dof_values,
446  const FEValuesExtractors::Scalar &variable,
447  std::vector <Tensor<2,spacedim,Number> > &us)
448 
449  {
450  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
451  const unsigned int n_q_points = fe_values.n_quadrature_points;
452 
453  AssertDimension(us.size(), n_q_points);
454  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
455 
456  for (unsigned int d=0; d<spacedim; ++d)
457  for (unsigned int q=0; q<n_q_points; ++q)
458  {
459  us[q]= 0;
460  for (unsigned int i=0; i<dofs_per_cell; ++i)
461  us[q] += independent_local_dof_values[i]*fe_values[variable].hessian(i,q);
462  }
463  }
464 
465 
473  template <int dim, int spacedim, typename Number>
474 
475  void
477  const std::vector<Number> &independent_local_dof_values,
478  const FEValuesExtractors::Vector &variable,
479  std::vector <Tensor<3,spacedim,Number> > &us)
480 
481  {
482  const unsigned int dofs_per_cell = fe_values.dofs_per_cell;
483  const unsigned int n_q_points = fe_values.n_quadrature_points;
484 
485  AssertDimension(us.size(), n_q_points);
486  AssertDimension(independent_local_dof_values.size(), dofs_per_cell);
487 
488  for (unsigned int d=0; d<spacedim; ++d)
489  for (unsigned int q=0; q<n_q_points; ++q)
490  {
491  us[q]= 0;
492  for (unsigned int i=0; i<dofs_per_cell; ++i)
493  us[q] += independent_local_dof_values[i]*fe_values[variable].hessian(i,q);
494  }
495  }
496 
497 
498 
499 }// end namespace
500 
501 
502 D2K_NAMESPACE_CLOSE
503 
504 #endif
#define AssertDimension(dim1, dim2)
const unsigned int dofs_per_cell
Define namespace "DOFUtilities" some utilities aimed at solving non-linear problems are implemented...
Definition: dof_utilities.h:50
void get_gradients(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Scalar &scalar_variable, std::vector< Tensor< 1, spacedim, Number > > &grad_us)
Extract gradient values of a scalar_variable on face of a cell and store them in std::vector <Tensor ...
void extract_local_dofs(const VEC &global_vector, const std::vector< types::global_dof_index > &local_dof_indices, std::vector< Number > &independent_local_dofs)
Extract local dofs values and initialize the number of independent variables up to the second order d...
Definition: dof_utilities.h:59
void get_divergences(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &vector_variable, std::vector< Number > &us)
Extract divergence values of a vector_variable on face of a cell and store them in std::vector <Numbe...
void get_values(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Scalar &scalar_variable, std::vector< Number > &us)
Extract independent local dofs values of a scalar_variable in a cell and store them in std::vector <N...
#define Assert(cond, exc)
void get_deformation_gradients(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &vector_variable, std::vector< Tensor< 2, spacedim, Number > > &Fs)
Compute the deformation gradient in each quadrature point of a cell and it is stored in std::vector <...
void get_hessians(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &variable, std::vector< Tensor< 3, spacedim, Number > > &us)
Extract hessians local dofs values of a vector variable in a cell and store them in std::vector <Tens...
void get_symmetric_gradients(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &vector_variable, std::vector< Tensor< 2, spacedim, Number > > &grad_us)
Extract symmetric gradient values of a vector_variable on face of a cell and store them in std::vecto...
void get_curls(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &vector_variable, std::vector< Tensor< 1,(spacedim > 2 ? spacedim :1), Number > > &curl_us)
Extract curl values of a vector_variable on face or cell and store them in std::vector <Tensor <1...
static const bool value
void get_laplacians(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &variable, std::vector< Tensor< 1, spacedim, Number > > &us)
Extract laplacians local dofs values of a vector variable in a cell and store them in std::vector <Te...
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
const unsigned int n_quadrature_points
static::ExceptionBase & ExcNotImplemented()
const FiniteElement< dim, spacedim > & get_fe() const
void get_gradients(const FEValuesBase< dim, spacedim > &fe_values, const std::vector< Number > &independent_local_dof_values, const FEValuesExtractors::Vector &vector_variable, std::vector< Tensor< 2, spacedim, Number > > &grad_us)
Extract gradient values of a vector_variable on face of a cell and store them in std::vector <Tensor ...