deal2lkit: A ToolKit library for Deal.II
sacado_tools.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2016 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_sacado_tools_h
17 #define _d2k_sacado_tools_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal.II/base/config.h>
21 
22 
30 #ifdef DEAL_II_WITH_TRILINOS
31 
32 
33 #include <Sacado.hpp>
34 typedef Sacado::Fad::DFad<double> Sdouble;
35 typedef Sacado::Fad::DFad<Sdouble> SSdouble;
36 
37 #include <deal.II/base/tensor.h>
38 
39 using namespace dealii;
40 
41 
42 
43 D2K_NAMESPACE_OPEN
44 
45 namespace SacadoTools
46 {
52  template<int dim, typename Number>
53  Number scalar_product (const Tensor<1,dim,Number> &T1,
54  const Tensor<1,dim,double> &T2)
55  {
56  Number ret=0;
57  for (unsigned int i=0; i<dim; ++i)
58  ret += T1[i]*T2[i];
59  return ret;
60  }
61 
62 
63 
69  template<int dim, typename Number>
70  Number scalar_product (const Tensor<1,dim,double> &T1,
71  const Tensor<1,dim,Number> &T2)
72  {
73  return SacadoTools::scalar_product(T2,T1);
74  }
75 
76 
77 
81  template<int dim>
82  double scalar_product (const Tensor<1,dim,double> &T1,
83  const Tensor<1,dim,double> &T2)
84  {
85  return T1*T2;
86  }
87 
88 
89 
95  template<int dim, typename Number>
96  Number scalar_product (const Tensor<2,dim,Number> &T1,
97  const Tensor<2,dim,double> &T2)
98  {
99  Number ret=0;
100  for (unsigned int i=0; i<dim; ++i)
101  for (unsigned int j=0; j<dim; ++j)
102  ret += T1[i][j]*T2[i][j];
103  return ret;
104  }
105 
106 
107 
113  template<int dim, typename Number>
114  Number scalar_product (const Tensor<2,dim,double> &T1,
115  const Tensor<2,dim,Number> &T2)
116  {
117  return SacadoTools::scalar_product(T2,T1);
118  }
119 
120 
121 
125  template<int dim>
126  double scalar_product (const Tensor<2,dim,double> &T1,
127  const Tensor<2,dim,double> &T2)
128  {
129  return dealii::scalar_product(T1,T2);
130  }
131 
132 
133 
134 // Helper functions val() which recursively calls val() function of Sacado
135 
139  double val(const double &n)
140  {
141  return n;
142  }
143 
144 
145 
149  template <typename number>
150  number val(const Sacado::Fad::DFad<number> &n)
151  {
152  return n.val();
153  }
154 
155 
156 
160  template <int index, int dim, typename number>
161  Tensor<index,dim,number> val(const Tensor<index,dim,Sacado::Fad::DFad<number> > &T)
162  {
164  for (unsigned int i=0; i<dim; ++i)
165  ret[i] = val(T[i]);
166  return ret;
167  }
168 
169 
170 
175  template <int index, int dim, typename number>
176  std::vector<Tensor<index,dim,number> > val(const std::vector<Tensor<index,dim,Sacado::Fad::DFad<number> > > &T)
177  {
178  std::vector<Tensor<index,dim,number> > ret(T.size());
179  for (unsigned int q=0; q<T.size(); ++q)
180  ret[q] = val(T[q]);
181  return ret;
182  }
183 
184 
185 
189  template <typename number>
190  std::vector<number> val(const std::vector<Sacado::Fad::DFad<number> > v)
191  {
192  std::vector<number> ret(v.size());
193  for (unsigned int i=0; i<v.size(); ++i)
194  ret[i] = v[i].val();
195  return ret;
196  }
197 
198 
199 
200  // helper functions to_double wich downgrade Sacado type to double
201 
205  double to_double(const double &s)
206  {
207  return s;
208  }
209 
210 
211 
215  double to_double(const Sdouble &s)
216  {
217  return s.val();
218  }
219 
220 
221 
225  double to_double(const SSdouble &s)
226  {
227  return s.val().val();
228  }
229 
230 
231 
235  template <int index, int dim>
236  Tensor <index,dim> to_double(const Tensor<index,dim> &t)
237  {
238  return t;
239  }
240 
241 
242 
247  template <int index, int dim, typename number>
248  Tensor <index,dim> to_double(const Tensor<index,dim, Sacado::Fad::DFad<number> > &t)
249  {
250  Tensor<index,dim> ret;
251  for (unsigned int i=0; i<dim; ++i)
252  ret[i] = to_double(t[i]);
253  return ret;
254  }
255 
256 
257 
263  template <int index, int dim, typename number>
264  std::vector<Tensor<index,dim> > to_double(const std::vector<Tensor<index,dim,Sacado::Fad::DFad<number> > > &v)
265  {
266  std::vector<Tensor<index,dim> > ret(v.size());
267  for (unsigned int q=0; q<v.size(); ++q)
268  ret[q] = to_double(v[q]);
269  return ret;
270  }
271 
272 
273 
279  template <typename number>
280  std::vector<double> to_double(const std::vector<number> &v)
281  {
282  std::vector<double> ret(v.size());
283  for (unsigned int q=0; q<v.size(); ++q)
284  ret[q] = to_double(v[q]);
285  return ret;
286  }
287 
288 
289 }// end namespace
290 
291 
292 D2K_NAMESPACE_CLOSE
293 
294 #endif // DEAL_II_WITH_TRILINOS
295 
296 #endif
297