Panzer  Version of the Day
Panzer_Integrator_GradBasisDotVector_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef __Panzer_Integrator_GradBasisDotVector_impl_hpp__
44 #define __Panzer_Integrator_GradBasisDotVector_impl_hpp__
45 
47 //
48 // Include Files
49 //
51 
52 // Panzer
53 #include "Panzer_BasisIRLayout.hpp"
56 
57 namespace panzer
58 {
60  //
61  // Main Constructor
62  //
64  template<typename EvalT, typename Traits>
68  const std::string& resName,
69  const std::string& fluxName,
70  const panzer::BasisIRLayout& basis,
71  const panzer::IntegrationRule& ir,
72  const double& multiplier, /* = 1 */
73  const std::vector<std::string>& fmNames, /* =
74  std::vector<std::string>() */
75  const Teuchos::RCP<PHX::DataLayout>& vecDL /* = Teuchos::null */)
76  :
77  evalStyle_(evalStyle),
78  multiplier_(multiplier),
79  numDim_(static_cast<int>(ir.dl_vector->extent(2))),
80  basisName_(basis.name())
81  {
82  using Kokkos::View;
83  using panzer::BASIS;
84  using panzer::Cell;
86  using panzer::IP;
87  using PHX::DataLayout;
88  using PHX::MDField;
89  using PHX::typeAsString;
90  using std::invalid_argument;
91  using std::logic_error;
92  using std::string;
93  using Teuchos::RCP;
94 
95  // Ensure the input makes sense.
96  TEUCHOS_TEST_FOR_EXCEPTION(resName == "", invalid_argument, "Error: " \
97  "Integrator_GradBasisDotVector called with an empty residual name.")
98  TEUCHOS_TEST_FOR_EXCEPTION(fluxName == "", invalid_argument, "Error: " \
99  "Integrator_GradBasisDotVector called with an empty flux name.")
100  RCP<const PureBasis> tmpBasis = basis.getBasis();
101  TEUCHOS_TEST_FOR_EXCEPTION(not tmpBasis->supportsGrad(), logic_error,
102  "Integrator_GradBasisDotVector: Basis of type \"" << tmpBasis->name() <<
103  "\" does not support the gradient operator.");
104  RCP<DataLayout> tmpVecDL = ir.dl_vector;
105  if (not vecDL.is_null())
106  {
107  tmpVecDL = vecDL;
108  TEUCHOS_TEST_FOR_EXCEPTION(
109  static_cast<int>(tmpVecDL->extent(2)) < numDim_, logic_error,
110  "Integrator_GradBasisDotVector: Dimension of space exceeds " \
111  "dimension of Vector Data Layout.");
112  } // end if (not vecDL.is_null())
113 
114  // Create the field for the vector-valued function we're integrating.
115  vector_ = MDField<const ScalarT, Cell, IP, Dim>(fluxName, tmpVecDL);
116  this->addDependentField(vector_);
117 
118  // Create the field that we're either contributing to or evaluating
119  // (storing).
120  field_ = MDField<ScalarT, Cell, BASIS>(resName, basis.functional);
122  this->addContributedField(field_);
123  else // if (evalStyle_ == EvaluatorStyle::EVALUATES)
124  this->addEvaluatedField(field_);
125 
126  // Add the dependent field multipliers, if there are any.
127  int i(0);
128  fieldMults_.resize(fmNames.size());
130  View<View<const ScalarT**>*>("GradBasisDotVector::KokkosFieldMultipliers",
131  fmNames.size());
132  for (const auto& name : fmNames)
133  {
134  fieldMults_[i++] = MDField<const ScalarT, Cell, IP>(name, ir.dl_scalar);
135  this->addDependentField(fieldMults_[i - 1]);
136  } // end loop over the field multipliers
137 
138  // Set the name of this object.
139  string n("Integrator_GradBasisDotVector (");
141  n += "Cont";
142  else // if (evalStyle_ == EvaluatorStyle::EVALUATES)
143  n += "Eval";
144  n += ", " + typeAsString<EvalT>() + "): " + field_.fieldTag().name();
145  this->setName(n);
146  } // end of Main Constructor
147 
149  //
150  // ParameterList Constructor
151  //
153  template<typename EvalT, typename Traits>
156  const Teuchos::ParameterList& p)
157  :
160  p.get<std::string>("Residual Name"),
161  p.get<std::string>("Flux Name"),
162  (*p.get<Teuchos::RCP<panzer::BasisIRLayout>>("Basis")),
163  (*p.get<Teuchos::RCP<panzer::IntegrationRule>>("IR")),
164  p.get<double>("Multiplier"),
165  p.isType<Teuchos::RCP<const std::vector<std::string>>>
166  ("Field Multipliers") ?
167  (*p.get<Teuchos::RCP<const std::vector<std::string>>>
168  ("Field Multipliers")) : std::vector<std::string>(),
169  p.isType<Teuchos::RCP<PHX::DataLayout>>("Vector Data Layout") ?
170  p.get<Teuchos::RCP<PHX::DataLayout>>("Vector Data Layout") :
171  Teuchos::null)
172  {
173  using Teuchos::ParameterList;
174  using Teuchos::RCP;
175 
176  // Ensure that the input ParameterList didn't contain any bogus entries.
177  RCP<ParameterList> validParams = this->getValidParameters();
178  p.validateParameters(*validParams);
179  } // end of ParameterList Constructor
180 
182  //
183  // postRegistrationSetup()
184  //
186  template<typename EvalT, typename Traits>
187  void
190  typename Traits::SetupData sd,
191  PHX::FieldManager<Traits>& /* fm */)
192  {
193  using panzer::getBasisIndex;
194  using std::size_t;
195 
196  // Get the Kokkos::Views of the field multipliers.
197  for (size_t i(0); i < fieldMults_.size(); ++i)
198  kokkosFieldMults_(i) = fieldMults_[i].get_static_view();
199 
200  // Determine the number of quadrature points and the dimensionality of the
201  // vector that we're integrating.
202  numQP_ = vector_.extent(1);
203  numDim_ = vector_.extent(2);
204 
205  // Determine the index in the Workset bases for our particular basis name.
206  basisIndex_ = getBasisIndex(basisName_, (*sd.worksets_)[0], this->wda);
207  } // end of postRegistrationSetup()
208 
210  //
211  // operator()()
212  //
214  template<typename EvalT, typename Traits>
215  template<int NUM_FIELD_MULT>
216  KOKKOS_INLINE_FUNCTION
217  void
220  const FieldMultTag<NUM_FIELD_MULT>& /* tag */,
221  const size_t& cell) const
222  {
224 
225  // Initialize the evaluated field.
226  const int numBases(basis_.extent(1));
227  if (evalStyle_ == EvaluatorStyle::EVALUATES)
228  for (int basis(0); basis < numBases; ++basis)
229  field_(cell, basis) = 0.0;
230 
231  // The following if-block is for the sake of optimization depending on the
232  // number of field multipliers.
233  ScalarT tmp;
234  if (NUM_FIELD_MULT == 0)
235  {
236  // Loop over the quadrature points and dimensions of our vector fields,
237  // scale the integrand by the multiplier, and then perform the
238  // integration, looping over the bases.
239  for (int qp(0); qp < numQP_; ++qp)
240  {
241  for (int dim(0); dim < numDim_; ++dim)
242  {
243  tmp = multiplier_ * vector_(cell, qp, dim);
244  for (int basis(0); basis < numBases; ++basis)
245  field_(cell, basis) += basis_(cell, basis, qp, dim) * tmp;
246  } // end loop over the dimensions of the vector field
247  } // end loop over the quadrature points
248  }
249  else if (NUM_FIELD_MULT == 1)
250  {
251  // Loop over the quadrature points and dimensions of our vector fields,
252  // scale the integrand by the multiplier and the single field multiplier,
253  // and then perform the actual integration, looping over the bases.
254  for (int qp(0); qp < numQP_; ++qp)
255  {
256  for (int dim(0); dim < numDim_; ++dim)
257  {
258  tmp = multiplier_ * vector_(cell, qp, dim) *
259  kokkosFieldMults_(0)(cell, qp);
260  for (int basis(0); basis < numBases; ++basis)
261  field_(cell, basis) += basis_(cell, basis, qp, dim) * tmp;
262  } // end loop over the dimensions of the vector field
263  } // end loop over the quadrature points
264  }
265  else
266  {
267  // Loop over the quadrature points and pre-multiply all the field
268  // multipliers together. Then loop over the dimensions of our vector
269  // fields, scale the integrand by the multiplier and the combination of
270  // the field multipliers, and then perform the actual integration,
271  // looping over the bases.
272  const int numFieldMults(kokkosFieldMults_.extent(0));
273  for (int qp(0); qp < numQP_; ++qp)
274  {
275  ScalarT fieldMultsTotal(1);
276  for (int fm(0); fm < numFieldMults; ++fm)
277  fieldMultsTotal *= kokkosFieldMults_(fm)(cell, qp);
278  for (int dim(0); dim < numDim_; ++dim)
279  {
280  tmp = multiplier_ * vector_(cell, qp, dim) * fieldMultsTotal;
281  for (int basis(0); basis < numBases; ++basis)
282  field_(cell, basis) += basis_(cell, basis, qp, dim) * tmp;
283  } // end loop over the dimensions of the vector field
284  } // end loop over the quadrature points
285  } // end if (NUM_FIELD_MULT == something)
286  } // end of operator()()
287 
289  //
290  // evaluateFields()
291  //
293  template<typename EvalT, typename Traits>
294  void
297  typename Traits::EvalData workset)
298  {
299  using Kokkos::parallel_for;
300  using Kokkos::RangePolicy;
301 
302  // Grab the basis information.
303  basis_ = this->wda(workset).bases[basisIndex_]->weighted_grad_basis;
304 
305  // The following if-block is for the sake of optimization depending on the
306  // number of field multipliers. The parallel_fors will loop over the cells
307  // in the Workset and execute operator()() above.
308  if (fieldMults_.size() == 0)
309  parallel_for(RangePolicy<FieldMultTag<0>>(0, workset.num_cells), *this);
310  else if (fieldMults_.size() == 1)
311  parallel_for(RangePolicy<FieldMultTag<1>>(0, workset.num_cells), *this);
312  else
313  parallel_for(RangePolicy<FieldMultTag<-1>>(0, workset.num_cells), *this);
314  } // end of evaluateFields()
315 
317  //
318  // getValidParameters()
319  //
321  template<typename EvalT, typename TRAITS>
322  Teuchos::RCP<Teuchos::ParameterList>
325  {
326  using panzer::BasisIRLayout;
328  using PHX::DataLayout;
329  using std::string;
330  using std::vector;
331  using Teuchos::ParameterList;
332  using Teuchos::RCP;
333  using Teuchos::rcp;
334 
335  // Create a ParameterList with all the valid keys we support.
336  RCP<ParameterList> p = rcp(new ParameterList);
337  p->set<string>("Residual Name", "?");
338  p->set<string>("Flux Name", "?");
339  RCP<BasisIRLayout> basis;
340  p->set("Basis", basis);
341  RCP<IntegrationRule> ir;
342  p->set("IR", ir);
343  p->set<double>("Multiplier", 1.0);
344  RCP<const vector<string>> fms;
345  p->set("Field Multipliers", fms);
346  RCP<DataLayout> vecDL;
347  p->set("Vector Data Layout", vecDL);
348  return p;
349  } // end of getValidParameters()
350 
351 } // end of namespace panzer
352 
353 #endif // __Panzer_Integrator_GradBasisDotVector_impl_hpp__
PHX::MDField< const ScalarT, panzer::Cell, panzer::IP, panzer::Dim > vector_
A field representing the vector-valued function we&#39;re integrating ( ).
Integrator_GradBasisDotVector(const panzer::EvaluatorStyle &evalStyle, const std::string &resName, const std::string &fluxName, const panzer::BasisIRLayout &basis, const panzer::IntegrationRule &ir, const double &multiplier=1, const std::vector< std::string > &fmNames=std::vector< std::string >(), const Teuchos::RCP< PHX::DataLayout > &vecDL=Teuchos::null)
Main Constructor.
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field_
A field to which we&#39;ll contribute, or in which we&#39;ll store, the result of computing this integral...
Teuchos::RCP< Teuchos::ParameterList > getValidParameters() const
Get Valid Parameters.
Teuchos::RCP< const PureBasis > getBasis() const
Kokkos::View< Kokkos::View< const ScalarT ** > * > kokkosFieldMults_
The Kokkos::View representation of the (possibly empty) list of fields that are multipliers out in fr...
EvaluatorStyle
An indication of how an Evaluator will behave.
const panzer::EvaluatorStyle evalStyle_
An enum determining the behavior of this Evaluator.
std::vector< PHX::MDField< const ScalarT, panzer::Cell, panzer::IP > > fieldMults_
The (possibly empty) list of fields that are multipliers out in front of the integral ( ...
double multiplier
The scalar multiplier out in front of the integral ( ).
KOKKOS_INLINE_FUNCTION void operator()(const FieldMultTag< NUM_FIELD_MULT > &tag, const std::size_t &cell) const
Perform the integration.
Teuchos::RCP< PHX::DataLayout > dl_scalar
Data layout for scalar fields.
panzer::EvaluatorStyle evalStyle
The EvaluatorStyle of the parent Integrator_CurlBasisDotVector object.
int numDim_
The number of dimensions in the vector field .
std::vector< std::string >::size_type getBasisIndex(std::string basis_name, const panzer::Workset &workset, WorksetDetailsAccessor &wda)
Returns the index in the workset bases for a particular BasisIRLayout name.
void evaluateFields(typename Traits::EvalData d)
Evaluate Fields.
void postRegistrationSetup(typename Traits::SetupData d, PHX::FieldManager< Traits > &fm)
Post-Registration Setup.
Teuchos::RCP< PHX::DataLayout > dl_vector
Data layout for vector fields.
This empty struct allows us to optimize operator()() depending on the number of field multipliers...
Teuchos::RCP< PHX::DataLayout > functional
<Cell,Basis>
Teuchos::RCP< const std::vector< panzer::Workset > > worksets_