Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Sum_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #ifndef PANZER_SUM_IMPL_HPP
12 #define PANZER_SUM_IMPL_HPP
13 
14 #include <cstddef>
15 #include <string>
16 #include <vector>
17 
18 namespace panzer {
19 
20 //**********************************************************************
21 template<typename EvalT, typename Traits>
24  const Teuchos::ParameterList& p)
25 {
26  std::string sum_name = p.get<std::string>("Sum Name");
28  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
29  Teuchos::RCP<PHX::DataLayout> data_layout =
30  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
31 
32  TEUCHOS_ASSERT(static_cast<int>(value_names->size()) < MAX_VALUES);
33 
34  // check if the user wants to scale each term independently
35  auto local_scalars = PHX::View<double *>("scalars",value_names->size());
36  auto local_scalars_host = Kokkos::create_mirror_view(local_scalars);
37  if(p.isType<Teuchos::RCP<const std::vector<double> > >("Scalars")) {
38  auto scalars_v = *p.get<Teuchos::RCP<const std::vector<double> > >("Scalars");
39 
40  // safety/sanity check
41  TEUCHOS_ASSERT(scalars_v.size()==value_names->size());
42 
43  for (std::size_t i=0; i < value_names->size(); ++i)
44  local_scalars_host(i) = scalars_v[i];
45  }
46  else {
47  for (std::size_t i=0; i < value_names->size(); ++i)
48  local_scalars_host(i) = 1.0;
49  }
50  Kokkos::deep_copy(local_scalars,local_scalars_host);
51 
52  scalars = local_scalars;
53 
54  sum = PHX::MDField<ScalarT>(sum_name, data_layout);
55 
56  this->addEvaluatedField(sum);
57 
58  for (std::size_t i=0; i < value_names->size(); ++i) {
59  values[i] = PHX::MDField<const ScalarT>( (*value_names)[i], data_layout);
60  this->addDependentField(values[i]);
61  }
62  /*
63  values.resize(value_names->size());
64  for (std::size_t i=0; i < value_names->size(); ++i) {
65  values[i] = PHX::MDField<const ScalarT>( (*value_names)[i], data_layout);
66  this->addDependentField(values[i]);
67  }
68  */
69 
70  std::string n = "Sum Evaluator";
71  this->setName(n);
72 }
73 
74 //**********************************************************************
75 template<typename EvalT, typename Traits>
76 void
79  typename Traits::SetupData /* worksets */,
80  PHX::FieldManager<Traits>& /* fm */)
81 {
82  cell_data_size = sum.size() / sum.fieldTag().dataLayout().extent(0);
83 }
84 
85 
86 //**********************************************************************
87 template<typename EvalT, typename TRAITS>
88 template<unsigned int RANK>
89 KOKKOS_INLINE_FUNCTION
91  auto num_vals = scalars.extent(0);
92 
93 
94  if (RANK == 1 )
95  {
96  for (std::size_t iv = 0; iv < num_vals; ++iv)
97  sum(i) += scalars(iv)*(values[iv](i));
98  }
99  else if (RANK == 2)
100  {
101  const size_t dim_1 = sum.extent(1);
102  for (std::size_t j = 0; j < dim_1; ++j)
103  for (std::size_t iv = 0; iv < num_vals; ++iv)
104  sum(i,j) += scalars(iv)*(values[iv](i,j));
105  }
106  else if (RANK == 3)
107  {
108  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2);
109  for (std::size_t j = 0; j < dim_1; ++j)
110  for (std::size_t k = 0; k < dim_2; ++k)
111  for (std::size_t iv = 0; iv < num_vals; ++iv)
112  sum(i,j,k) += scalars(iv)*(values[iv](i,j,k));
113  }
114  else if (RANK == 4)
115  {
116  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3);
117  for (std::size_t j = 0; j < dim_1; ++j)
118  for (std::size_t k = 0; k < dim_2; ++k)
119  for (std::size_t l = 0; l < dim_3; ++l)
120  for (std::size_t iv = 0; iv < num_vals; ++iv)
121  sum(i,j,k,l) += scalars(iv)*(values[iv](i,j,k,l));
122  }
123  else if (RANK == 5)
124  {
125  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3),dim_4 = sum.extent(4);
126  for (std::size_t j = 0; j < dim_1; ++j)
127  for (std::size_t k = 0; k < dim_2; ++k)
128  for (std::size_t l = 0; l < dim_3; ++l)
129  for (std::size_t m = 0; m < dim_4; ++m)
130  for (std::size_t iv = 0; iv < num_vals; ++iv)
131  sum(i,j,k,l,m) += scalars(iv)*(values[iv](i,j,k,l,m));
132  }
133  else if (RANK == 6)
134  {
135  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3),dim_4 = sum.extent(4),dim_5 = sum.extent(5);
136  for (std::size_t j = 0; j < dim_1; ++j)
137  for (std::size_t k = 0; k < dim_2; ++k)
138  for (std::size_t l = 0; l < dim_3; ++l)
139  for (std::size_t m = 0; m < dim_4; ++m)
140  for (std::size_t n = 0; n < dim_5; ++n)
141  for (std::size_t iv = 0; iv < num_vals; ++iv)
142  sum(i,j,k,l,m,n) += scalars(iv)*(values[iv](i,j,k,l,m,n));
143  }
144 }
145 
146 //**********************************************************************
147 template<typename EvalT, typename Traits>
148 void
151  typename Traits::EvalData /* workset */)
152 {
153 
154  sum.deep_copy(ScalarT(0.0));
155 
156  size_t rank = sum.rank();
157  const size_t length = sum.extent(0);
158  if (rank == 1 )
159  {
160  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<1> >(0, length), *this);
161  }
162  else if (rank == 2)
163  {
164  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<2> >(0, length), *this);
165  }
166  else if (rank == 3)
167  {
168  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<3> >(0, length), *this);
169  }
170  else if (rank == 4)
171  {
172  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<4> >(0, length), *this);
173  }
174  else if (rank == 5)
175  {
176  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<5> >(0, length), *this);
177  }
178  else if (rank == 6)
179  {
180  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<6> >(0, length), *this);
181  }
182  else
183  {
184  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "ERROR: rank of sum is higher than supported");
185  }
186 }
187 
188 
189 //**********************************************************************
190 //**********************************************************************
191 // Sum Static
192 //**********************************************************************
193 //**********************************************************************
194 
195 template<typename EvalT, typename TRAITS,typename Tag0>
198 {
199  std::string sum_name = p.get<std::string>("Sum Name");
201  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
202  Teuchos::RCP<PHX::DataLayout> data_layout =
203  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
204 
205  // sanity check
206  TEUCHOS_ASSERT(data_layout->rank()==1);
207 
208  sum = PHX::MDField<ScalarT,Tag0>(sum_name, data_layout);
209 
210  this->addEvaluatedField(sum);
211 
212  values.resize(value_names->size());
213  for (std::size_t i=0; i < value_names->size(); ++i) {
214  values[i] = PHX::MDField<const ScalarT,Tag0>( (*value_names)[i], data_layout);
215  this->addDependentField(values[i]);
216  }
217 
218  std::string n = "SumStatic Rank 1 Evaluator";
219  this->setName(n);
220 }
221 
222 //**********************************************************************
223 
224 template<typename EvalT, typename TRAITS,typename Tag0>
226 evaluateFields(typename TRAITS::EvalData /* d */)
227 {
228  sum.deep_copy(ScalarT(0.0));
229  for (std::size_t i = 0; i < sum.extent(0); ++i)
230  for (std::size_t d = 0; d < values.size(); ++d)
231  sum(i) += (values[d])(i);
232 }
233 
234 //**********************************************************************
235 //**********************************************************************
236 
237 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
240 {
241  std::string sum_name = p.get<std::string>("Sum Name");
243  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
244  Teuchos::RCP<PHX::DataLayout> data_layout =
245  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
246 
247  // check if the user wants to scale each term independently
248  if(p.isType<Teuchos::RCP<const std::vector<double> > >("Scalars")) {
250  = p.get<Teuchos::RCP<const std::vector<double> > >("Scalars");
251 
252  // safety/sanity check
253  TEUCHOS_ASSERT(scalar_values->size()==value_names->size());
254  useScalars = true;
255 
256  PHX::View<double*> scalars_nc = PHX::View<double*>("scalars",scalar_values->size());
257 
258  for(std::size_t i=0;i<scalar_values->size();i++)
259  scalars_nc(i) = (*scalar_values)[i];
260 
261  scalars = scalars_nc;
262  }
263  else {
264  useScalars = false;
265  }
266 
267  // sanity check
268  TEUCHOS_ASSERT(data_layout->rank()==2);
269 
270  sum = PHX::MDField<ScalarT,Tag0,Tag1>(sum_name, data_layout);
271 
272  this->addEvaluatedField(sum);
273 
274  values.resize(value_names->size());
275  for (std::size_t i=0; i < value_names->size(); ++i) {
276  values[i] = PHX::MDField<const ScalarT,Tag0,Tag1>( (*value_names)[i], data_layout);
277  this->addDependentField(values[i]);
278  }
279  numValues = value_names->size();
280  TEUCHOS_ASSERT(numValues<=MAX_VALUES);
281 
282  std::string n = "SumStatic Rank 2 Evaluator";
283  this->setName(n);
284 }
285 
286 //**********************************************************************
287 
288 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
290 SumStatic(const std::vector<PHX::Tag<typename EvalT::ScalarT>> & inputs,
291  const std::vector<double> & scalar_values,
292  const PHX::Tag<typename EvalT::ScalarT> & output)
293 {
294  TEUCHOS_ASSERT(scalar_values.size()==inputs.size());
295 
296  // check if the user wants to scale each term independently
297  if(scalars.size()==0) {
298  useScalars = false;
299  }
300  else {
301  useScalars = true;
302 
303  PHX::View<double*> scalars_nc = PHX::View<double*>("scalars",scalar_values.size());
304 
305  for(std::size_t i=0;i<scalar_values.size();i++)
306  scalars_nc(i) = scalar_values[i];
307 
308  scalars = scalars_nc;
309  }
310 
311  // sanity check
312  TEUCHOS_ASSERT(inputs.size()<=MAX_VALUES);
313 
314  sum = output;
315  this->addEvaluatedField(sum);
316 
317  values.resize(inputs.size());
318  for (std::size_t i=0; i < inputs.size(); ++i) {
319  values[i] = inputs[i];
320  this->addDependentField(values[i]);
321  }
322 
323  numValues = inputs.size();
324 
325  std::string n = "SumStatic Rank 2 Evaluator";
326  this->setName(n);
327 }
328 
329 //**********************************************************************
330 
331 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
333 postRegistrationSetup(typename TRAITS::SetupData /* d */,
334  PHX::FieldManager<TRAITS>& /* fm */)
335 {
336  for (std::size_t i=0; i < values.size(); ++i)
337  value_views[i] = values[i].get_static_view();
338 }
339 
340 //**********************************************************************
341 
342 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
344 evaluateFields(typename TRAITS::EvalData /* d */)
345 {
346  sum.deep_copy(ScalarT(0.0));
347 
348  // Kokkos::parallel_for(sum.extent(0), *this);
349  if(useScalars)
350  Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device,ScalarsTag>(0,sum.extent(0)), *this);
351  else
352  Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device,NoScalarsTag>(0,sum.extent(0)), *this);
353 }
354 
355 //**********************************************************************
356 
357 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
358 KOKKOS_INLINE_FUNCTION
360 operator()(const ScalarsTag, const unsigned c ) const
361 {
362  for (int i=0;i<numValues;i++) {
363  for (int j = 0; j < sum.extent_int(1); ++j)
364  sum(c,j) += scalars(i)*value_views[i](c,j);
365  }
366 }
367 
368 //**********************************************************************
369 
370 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
371 KOKKOS_INLINE_FUNCTION
373 operator()(const NoScalarsTag, const unsigned c ) const
374 {
375  for (int i=0;i<numValues;i++) {
376  for (int j = 0; j < sum.extent_int(1); ++j)
377  sum(c,j) += value_views[i](c,j);
378  }
379 }
380 
381 
382 //**********************************************************************
383 //**********************************************************************
384 
385 /*
386 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1,typename Tag2>
387 SumStatic<EvalT,TRAITS,Tag0,Tag1,Tag2>::
388 SumStatic(const Teuchos::ParameterList& p)
389 {
390  std::string sum_name = p.get<std::string>("Sum Name");
391  Teuchos::RCP<std::vector<std::string> > value_names =
392  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
393  Teuchos::RCP<PHX::DataLayout> data_layout =
394  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
395 
396  // sanity check
397  TEUCHOS_ASSERT(data_layout->rank()==3);
398 
399  sum = PHX::MDField<ScalarT,Tag0,Tag1,Tag2>(sum_name, data_layout);
400 
401  this->addEvaluatedField(sum);
402 
403  values.resize(value_names->size());
404  for (std::size_t i=0; i < value_names->size(); ++i) {
405  values[i] = PHX::MDField<ScalarT,Tag0,Tag1,Tag2>( (*value_names)[i], data_layout);
406  this->addDependentField(values[i]);
407  }
408 
409  std::string n = "Sum Evaluator";
410  this->setName(n);
411 }
412 */
413 
414 //**********************************************************************
415 
416 /*
417 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1,typename Tag2>
418 void SumStatic<EvalT,TRAITS,Tag0,Tag1,Tag2>::
419 evaluateFields(typename TRAITS::EvalData d)
420 {
421  sum.deep_copy(ScalarT(0.0));
422 
423  for (std::size_t d = 0; d < values.size(); ++d)
424  for (std::size_t i = 0; i < sum.extent(0); ++i)
425  for (std::size_t j = 0; j < sum.extent(1); ++j)
426  for (std::size_t k = 0; k < sum.extent(2); ++k)
427  sum(i,j,k) += (values[d])(i);
428 }
429 */
430 
431 //**********************************************************************
432 //**********************************************************************
433 
434 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1,typename Tag2>
436 buildStaticSumEvaluator(const std::string & sum_name,
437  const std::vector<std::string> & value_names,
438  const Teuchos::RCP<PHX::DataLayout> & data_layout)
439 {
441  p.set<std::string>("Sum Name",sum_name);
442  p.set<Teuchos::RCP<std::vector<std::string> > >("Values Names",Teuchos::rcp(new std::vector<std::string>(value_names)));
443  p.set< Teuchos::RCP<PHX::DataLayout> >("Data Layout",data_layout);
444 
446 }
447 
448 }
449 
450 #endif
Teuchos::RCP< PHX::Evaluator< TRAITS > > buildStaticSumEvaluator(const std::string &sum_name, const std::vector< std::string > &value_names, const Teuchos::RCP< PHX::DataLayout > &data_layout)
Sum(const Teuchos::ParameterList &p)
T & get(const std::string &name, T def_value)
EvalT::ScalarT ScalarT
Definition: Panzer_Sum.hpp:88
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void postRegistrationSetup(typename Traits::SetupData d, PHX::FieldManager< Traits > &fm)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
KOKKOS_INLINE_FUNCTION void operator()(PanzerSumTag< RANK >, const int &i) const
SumStatic(const Teuchos::ParameterList &p)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void postRegistrationSetup(typename Traits::SetupData d, PHX::FieldManager< Traits > &vm)=0
void evaluateFields(typename Traits::EvalData d)
typename EvalT::ScalarT ScalarT
Definition: Panzer_Sum.hpp:56
bool isType(const std::string &name) const
void evaluateFields(typename TRAITS::EvalData d)
#define TEUCHOS_ASSERT(assertion_test)