Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fe_jac_fill_funcs.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef FE_JAC_FILL_FUNCS_HPP
31 #define FE_JAC_FILL_FUNCS_HPP
32 
33 #include "Sacado_No_Kokkos.hpp"
34 #include "Sacado_Fad_SimpleFad.hpp"
35 
36 #include "Teuchos_Time.hpp"
38 
39 // ADOL-C includes
40 #ifdef HAVE_ADOLC
41 #ifdef PACKAGE
42 #undef PACKAGE
43 #endif
44 #ifdef PACKAGE_NAME
45 #undef PACKAGE_NAME
46 #endif
47 #ifdef PACKAGE_BUGREPORT
48 #undef PACKAGE_BUGREPORT
49 #endif
50 #ifdef PACKAGE_STRING
51 #undef PACKAGE_STRING
52 #endif
53 #ifdef PACKAGE_TARNAME
54 #undef PACKAGE_TARNAME
55 #endif
56 #ifdef PACKAGE_VERSION
57 #undef PACKAGE_VERSION
58 #endif
59 #ifdef VERSION
60 #undef VERSION
61 #endif
62 //#define ADOLC_TAPELESS
63 #define NUMBER_DIRECTIONS 100
64 #include "adolc/adouble.h"
65 #include "adolc/drivers/drivers.h"
66 #include "adolc/interfaces.h"
67 #endif
68 
69 #ifdef HAVE_ADIC
70 // We have included an ADIC differentiated version of the element fill
71 // routine to compare the speed of operator overloading to source
72 // transformation. To run this code, all that is necessary is to turn
73 // on the ADIC TPL. However to modify the code, it is necessary to
74 // re-run the ADIC source transformation tool. To do so, first update
75 // the changes to adic_element_fill.c. Then set the following environment
76 // variables:
77 // export ADIC_ARCH=linux
78 // export ADIC=/home/etphipp/AD_libs/adic
79 // Next run ADIC via in the tests/performance source directory:
80 // ${ADIC}/bin/linux/adiC -vd gradient -i adic_element_fill.init
81 // Finally, copy the resulting differentiated function in adic_element_fill.ad.c
82 // back into this file. The function will need to be edited by changing
83 // the allocation of s to a std::vector<DERIV_TYPE> (the compiler
84 // doesn't seem to like malloc), and commenting out the g_filenum lines.
85 #define ad_GRAD_MAX 130
86 #include "ad_deriv.h"
87 #endif
88 
89 
90 // A performance test that computes a finite-element-like Jacobian using
91 // several Fad variants
92 
93 struct ElemData {
94  static const unsigned int nqp = 2;
95  static const unsigned int nnode = 2;
96  double w[nqp], jac[nqp], phi[nqp][nnode], dphi[nqp][nnode];
97  unsigned int gid[nnode];
98 
99  ElemData(double mesh_spacing) {
100  // Quadrature points
101  double xi[nqp];
102  xi[0] = -1.0 / std::sqrt(3.0);
103  xi[1] = 1.0 / std::sqrt(3.0);
104 
105  for (unsigned int i=0; i<nqp; i++) {
106  // Weights
107  w[i] = 1.0;
108 
109  // Element transformation Jacobian
110  jac[i] = 0.5*mesh_spacing;
111 
112  // Shape functions & derivatives
113  phi[i][0] = 0.5*(1.0 - xi[i]);
114  phi[i][1] = 0.5*(1.0 + xi[i]);
115  dphi[i][0] = -0.5;
116  dphi[i][1] = 0.5;
117  }
118  }
119 };
120 
121 template <class FadType>
122 void fad_init_fill(const ElemData& e,
123  unsigned int neqn,
124  const std::vector<double>& x,
125  std::vector<FadType>& x_fad) {
126  for (unsigned int node=0; node<e.nnode; node++)
127  for (unsigned int eqn=0; eqn<neqn; eqn++)
128  x_fad[node*neqn+eqn] = FadType(e.nnode*neqn, node*neqn+eqn,
129  x[e.gid[node]*neqn+eqn]);
130 }
131 
132 template <class T>
134  unsigned int neqn,
135  const std::vector<T>& x,
136  std::vector<T>& u,
137  std::vector<T>& du,
138  std::vector<T>& f) {
139  // Construct element solution, derivative
140  for (unsigned int qp=0; qp<e.nqp; qp++) {
141  for (unsigned int eqn=0; eqn<neqn; eqn++) {
142  u[qp*neqn+eqn] = 0.0;
143  du[qp*neqn+eqn] = 0.0;
144  for (unsigned int node=0; node<e.nnode; node++) {
145  u[qp*neqn+eqn] += x[node*neqn+eqn] * e.phi[qp][node];
146  du[qp*neqn+eqn] += x[node*neqn+eqn] * e.dphi[qp][node];
147  }
148  }
149  }
150 
151  // Compute sum of equations for coupling
152  std::vector<T> s(e.nqp);
153  for (unsigned int qp=0; qp<e.nqp; qp++) {
154  s[qp] = 0.0;
155  for (unsigned int eqn=0; eqn<neqn; eqn++)
156  s[qp] += u[qp*neqn+eqn]*u[qp*neqn+eqn];
157  }
158 
159  // Evaluate element residual
160  for (unsigned int node=0; node<e.nnode; node++) {
161  for (unsigned int eqn=0; eqn<neqn; eqn++) {
162  unsigned int row = node*neqn+eqn;
163  f[row] = 0.0;
164  for (unsigned int qp=0; qp<e.nqp; qp++) {
165  double c1 = e.w[qp]*e.jac[qp];
166  double c2 = -e.dphi[qp][node]/(e.jac[qp]*e.jac[qp]);
167  f[row] +=
168  c1*(c2*du[qp*neqn+eqn] + e.phi[qp][node]*s[qp]*exp(u[qp*neqn+eqn]));
169  }
170  }
171  }
172 }
173 
174 template <class FadType>
176  unsigned int neqn,
177  const std::vector<FadType>& f_fad,
178  std::vector<double>& f,
179  std::vector< std::vector<double> >& jac) {
180  for (unsigned int eqn_row=0; eqn_row<neqn; eqn_row++) {
181  f[e.gid[0]*neqn+eqn_row] += f_fad[0*neqn+eqn_row].val();
182  f[e.gid[1]*neqn+eqn_row] += f_fad[1*neqn+eqn_row].val();
183  for (unsigned int node_col=0; node_col<e.nnode; node_col++) {
184  for (unsigned int eqn_col=0; eqn_col<neqn; eqn_col++) {
185  unsigned int col = node_col*neqn+eqn_col;
186  unsigned int next_col = (node_col+1)*neqn+eqn_col;
187  jac[e.gid[0]*neqn+eqn_row][next_col] +=
188  f_fad[0*neqn+eqn_row].fastAccessDx(col);
189  jac[e.gid[1]*neqn+eqn_row][col] +=
190  f_fad[1*neqn+eqn_row].fastAccessDx(col);
191  }
192  }
193  }
194 }
195 
196 template <class FadType>
197 double fad_jac_fill(unsigned int num_nodes, unsigned int num_eqns,
198  double mesh_spacing) {
199  ElemData e(mesh_spacing);
200 
201  // Solution vector, residual, jacobian
202  std::vector<double> x(num_nodes*num_eqns), f(num_nodes*num_eqns);
203  std::vector< std::vector<double> > jac(num_nodes*num_eqns);
204  for (unsigned int node_row=0; node_row<num_nodes; node_row++) {
205  for (unsigned int eqn_row=0; eqn_row<num_eqns; eqn_row++) {
206  unsigned int row = node_row*num_eqns + eqn_row;
207  x[row] = (mesh_spacing*node_row - 0.5)*(mesh_spacing*node_row - 0.5);
208  f[row] = 0.0;
209  jac[row] = std::vector<double>((e.nnode+1)*num_eqns);
210  for (unsigned int node_col=0; node_col<e.nnode+1; node_col++) {
211  for (unsigned int eqn_col=0; eqn_col<num_eqns; eqn_col++) {
212  unsigned int col = node_col*num_eqns + eqn_col;
213  jac[row][col] = 0.0;
214  }
215  }
216  }
217  }
218 
219  Teuchos::Time timer("FE Fad Jacobian Fill", false);
220  timer.start(true);
221  std::vector<FadType> x_fad(e.nnode*num_eqns), f_fad(e.nnode*num_eqns);
222  std::vector<FadType> u(e.nqp*num_eqns), du(e.nqp*num_eqns);
223  for (unsigned int i=0; i<num_nodes-1; i++) {
224  e.gid[0] = i;
225  e.gid[1] = i+1;
226 
227  fad_init_fill(e, num_eqns, x, x_fad);
228  template_element_fill(e, num_eqns, x_fad, u, du, f_fad);
229  fad_process_fill(e, num_eqns, f_fad, f, jac);
230  }
231  timer.stop();
232 
233  // std::cout << "Fad Residual = " << std::endl;
234  // for (unsigned int i=0; i<num_nodes*num_eqns; i++)
235  // std::cout << "\t" << f[i] << std::endl;
236 
237  // std::cout.precision(8);
238  // std::cout << "Fad Jacobian = " << std::endl;
239  // for (unsigned int i=0; i<num_nodes*num_eqns; i++) {
240  // std::cout << "\t";
241  // for (unsigned int j=0; j<(e.nnode+1)*num_eqns; j++)
242  // std::cout << jac[i][j] << "\t";
243  // std::cout << std::endl;
244  // }
245 
246  return timer.totalElapsedTime();
247 }
248 
249 double analytic_jac_fill(unsigned int num_nodes, unsigned int num_eqns,
250  double mesh_spacing);
251 double residual_fill(unsigned int num_nodes, unsigned int num_eqns,
252  double mesh_spacing);
253 
254 #ifdef HAVE_ADOLC
255 #ifndef ADOLC_TAPELESS
256 void adolc_init_fill(bool retape,
257  int tag,
258  const ElemData& e,
259  unsigned int neqn,
260  const std::vector<double>& x,
261  std::vector<double>& x_local,
262  std::vector<adouble>& x_ad);
263 
264 void adolc_process_fill(bool retape,
265  int tag,
266  const ElemData& e,
267  unsigned int neqn,
268  std::vector<double>& x_local,
269  std::vector<adouble>& f_ad,
270  std::vector<double>& f,
271  std::vector<double>& f_local,
272  std::vector< std::vector<double> >& jac,
273  double **seed,
274  double **jac_local);
275 
276 double adolc_jac_fill(unsigned int num_nodes, unsigned int num_eqns,
277  double mesh_spacing);
278 
279 double adolc_retape_jac_fill(unsigned int num_nodes, unsigned int num_eqns,
280  double mesh_spacing);
281 
282 #else
283 
284 void adolc_tapeless_init_fill(const ElemData& e,
285  unsigned int neqn,
286  const std::vector<double>& x,
287  std::vector<adtl::adouble>& x_ad);
288 
289 void adolc_tapeless_process_fill(const ElemData& e,
290  unsigned int neqn,
291  const std::vector<adtl::adouble>& f_ad,
292  std::vector<double>& f,
293  std::vector< std::vector<double> >& jac);
294 
295 double adolc_tapeless_jac_fill(unsigned int num_nodes, unsigned int num_eqns,
296  double mesh_spacing);
297 
298 #endif
299 #endif
300 
301 #ifdef HAVE_ADIC
302 void adic_init_fill(const ElemData& e,
303  unsigned int neqn,
304  const std::vector<double>& x,
305  std::vector<DERIV_TYPE>& x_fad);
306 void adic_element_fill(ElemData *e,unsigned int neqn,DERIV_TYPE *x,DERIV_TYPE *u,DERIV_TYPE *du,DERIV_TYPE *f);
307 void adic_process_fill(const ElemData& e,
308  unsigned int neqn,
309  const std::vector<DERIV_TYPE>& f_fad,
310  std::vector<double>& f,
311  std::vector< std::vector<double> >& jac);
312 double adic_jac_fill(unsigned int num_nodes, unsigned int num_eqns,
313  double mesh_spacing);
314 inline void AD_Init(int arg0) {
315  ad_AD_GradInit(arg0);
316 }
317 inline void AD_Final() {
318  ad_AD_GradFinal();
319 }
320 #endif
321 
322 #endif
void f()
void AD_Init(int)
double residual_fill(unsigned int num_nodes, unsigned int num_eqns, double mesh_spacing)
void template_element_fill(const ElemData &e, unsigned int neqn, const std::vector< T > &x, std::vector< T > &u, std::vector< T > &du, std::vector< T > &f)
InactiveDouble * jac
InactiveDouble * w
void AD_Final()
void adic_element_fill(ElemData *e, unsigned int neqn, const DERIV_TYPE *x, DERIV_TYPE *u, DERIV_TYPE *du, DERIV_TYPE *f)
double analytic_jac_fill(unsigned int num_nodes, unsigned int num_eqns, double mesh_spacing)
Sacado::Fad::DFad< double > FadType
ElemData(double mesh_spacing)
void fad_init_fill(const ElemData &e, unsigned int neqn, const std::vector< double > &x, std::vector< FadType > &x_fad)
void start(bool reset=false)
double stop()
sqrt(expr.val())
InactiveDouble ** phi
InactiveDouble ** dphi
void fad_process_fill(const ElemData &e, unsigned int neqn, const std::vector< FadType > &f_fad, std::vector< double > &f, std::vector< std::vector< double > > &jac)
double totalElapsedTime(bool readCurrentTime=false) const
double fad_jac_fill(unsigned int num_nodes, unsigned int num_eqns, double mesh_spacing)
exp(expr.val())