Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Integrator_BasisTimesScalar_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_INTEGRATOR_BASISTIMESSCALAR_IMPL_HPP
12 #define PANZER_INTEGRATOR_BASISTIMESSCALAR_IMPL_HPP
13 
15 //
16 // Include Files
17 //
19 
20 // Panzer
21 #include "Panzer_BasisIRLayout.hpp"
24 
25 namespace panzer
26 {
28  //
29  // Main Constructor
30  //
32  template<typename EvalT, typename Traits>
36  const std::string& resName,
37  const std::string& valName,
38  const panzer::BasisIRLayout& basis,
39  const panzer::IntegrationRule& ir,
40  const double& multiplier, /* = 1 */
41  const std::vector<std::string>& fmNames /* =
42  std::vector<std::string>() */)
43  :
44  evalStyle_(evalStyle),
45  multiplier_(multiplier),
46  basisName_(basis.name())
47  {
48  using PHX::View;
49  using panzer::BASIS;
50  using panzer::Cell;
52  using panzer::IP;
53  using panzer::PureBasis;
54  using PHX::MDField;
55  using std::invalid_argument;
56  using std::logic_error;
57  using std::string;
58  using Teuchos::RCP;
59 
60  // Ensure the input makes sense.
61  TEUCHOS_TEST_FOR_EXCEPTION(resName == "", invalid_argument, "Error: " \
62  "Integrator_BasisTimesScalar called with an empty residual name.")
63  TEUCHOS_TEST_FOR_EXCEPTION(valName == "", invalid_argument, "Error: " \
64  "Integrator_BasisTimesScalar called with an empty value name.")
65  RCP<const PureBasis> tmpBasis = basis.getBasis();
66  TEUCHOS_TEST_FOR_EXCEPTION(not tmpBasis->isScalarBasis(), logic_error,
67  "Error: Integrator_BasisTimesScalar: Basis of type \""
68  << tmpBasis->name() << "\" is not a scalar basis.")
69 
70  // Create the field for the scalar quantity we're integrating.
71  scalar_ = MDField<const ScalarT, Cell, IP>(valName, ir.dl_scalar);
72  this->addDependentField(scalar_);
73 
74  // Create the field that we're either contributing to or evaluating
75  // (storing).
76  field_ = MDField<ScalarT, Cell, BASIS>(resName, basis.functional);
77  if (evalStyle == EvaluatorStyle::CONTRIBUTES)
78  this->addContributedField(field_);
79  else // if (evalStyle == EvaluatorStyle::EVALUATES)
80  this->addEvaluatedField(field_);
81 
82  // Add the dependent field multipliers, if there are any.
83  int i(0);
84  fieldMults_.resize(fmNames.size());
86  View<Kokkos::View<const ScalarT**, typename PHX::DevLayout<ScalarT>::type, Kokkos::MemoryUnmanaged>*>("BasisTimesScalar::KokkosFieldMultipliers",
87  fmNames.size());
88  for (const auto& name : fmNames)
89  {
90  fieldMults_[i++] = MDField<const ScalarT, Cell, IP>(name, ir.dl_scalar);
91  this->addDependentField(fieldMults_[i - 1]);
92  } // end loop over the field multipliers
93 
94  // Set the name of this object.
95  string n("Integrator_BasisTimesScalar (");
97  n += "CONTRIBUTES";
98  else // if (evalStyle_ == EvaluatorStyle::EVALUATES)
99  n += "EVALUATES";
100  n += "): " + field_.fieldTag().name();
101  this->setName(n);
102  } // end of Main Constructor
103 
105  //
106  // ParameterList Constructor
107  //
109  template<typename EvalT, typename Traits>
112  const Teuchos::ParameterList& p)
113  :
115  panzer::EvaluatorStyle::EVALUATES,
116  p.get<std::string>("Residual Name"),
117  p.get<std::string>("Value Name"),
118  (*p.get<Teuchos::RCP<panzer::BasisIRLayout>>("Basis")),
119  (*p.get<Teuchos::RCP<panzer::IntegrationRule>>("IR")),
120  p.get<double>("Multiplier"),
121  p.isType<Teuchos::RCP<const std::vector<std::string>>>
122  ("Field Multipliers") ?
123  (*p.get<Teuchos::RCP<const std::vector<std::string>>>
124  ("Field Multipliers")) : std::vector<std::string>())
125  {
127  using Teuchos::RCP;
128 
129  // Ensure that the input ParameterList didn't contain any bogus entries.
130  RCP<ParameterList> validParams = this->getValidParameters();
131  p.validateParameters(*validParams);
132  } // end of ParameterList Constructor
133 
135  //
136  // postRegistrationSetup()
137  //
139  template<typename EvalT, typename Traits>
140  void
143  typename Traits::SetupData sd,
144  PHX::FieldManager<Traits>& /* fm */)
145  {
146  using panzer::getBasisIndex;
147  using std::size_t;
148 
149  auto kokkosFieldMults_h = Kokkos::create_mirror_view(kokkosFieldMults_);
150 
151  // Get the PHX::Views of the field multipliers.
152  for (size_t i(0); i < fieldMults_.size(); ++i)
153  kokkosFieldMults_h(i) = fieldMults_[i].get_static_view();
154 
155  Kokkos::deep_copy(kokkosFieldMults_, kokkosFieldMults_h);
156 
157  // Determine the index in the Workset bases for our particular basis name.
158  basisIndex_ = getBasisIndex(basisName_, (*sd.worksets_)[0], this->wda);
159 
160  // Allocate temporary
161  if (Sacado::IsADType<ScalarT>::value) {
162  const auto fadSize = Kokkos::dimension_scalar(field_.get_view());
163  tmp_ = PHX::View<ScalarT*>("IntegratorBasisTimesScalar::tmp_",field_.extent(0),fadSize);
164  if (fieldMults_.size() > 1)
165  tmp2_ = PHX::View<ScalarT*>("IntegratorBasisTimesScalar::tmp_",field_.extent(0),fadSize);
166  } else {
167  tmp_ = PHX::View<ScalarT*>("IntegratorBasisTimesScalar::tmp_",field_.extent(0));
168  if (fieldMults_.size() > 1)
169  tmp2_ = PHX::View<ScalarT*>("IntegratorBasisTimesScalar::tmp_",field_.extent(0));
170  }
171  } // end of postRegistrationSetup()
172 
174  //
175  // operator()()
176  //
178  template<typename EvalT, typename Traits>
179  template<int NUM_FIELD_MULT>
180  KOKKOS_INLINE_FUNCTION
181  void
184  const FieldMultTag<NUM_FIELD_MULT>& /* tag */,
185  const size_t& cell) const
186  {
188 
189  // Initialize the evaluated field.
190  const int numQP(scalar_.extent(1)), numBases(basis_.extent(1));
191  if (evalStyle_ == EvaluatorStyle::EVALUATES)
192  for (int basis(0); basis < numBases; ++basis)
193  field_(cell, basis) = 0.0;
194 
195  // The following if-block is for the sake of optimization depending on the
196  // number of field multipliers.
197  if (NUM_FIELD_MULT == 0)
198  {
199  // Loop over the quadrature points, scale the integrand by the
200  // multiplier, and then perform the actual integration, looping over the
201  // bases.
202  for (int qp(0); qp < numQP; ++qp)
203  {
204  tmp_(cell) = multiplier_ * scalar_(cell, qp);
205  for (int basis(0); basis < numBases; ++basis)
206  field_(cell, basis) += basis_(cell, basis, qp) * tmp_(cell);
207  } // end loop over the quadrature points
208  }
209  else if (NUM_FIELD_MULT == 1)
210  {
211  // Loop over the quadrature points, scale the integrand by the multiplier
212  // and the single field multiplier, and then perform the actual
213  // integration, looping over the bases.
214  for (int qp(0); qp < numQP; ++qp)
215  {
216  tmp_(cell) = multiplier_ * scalar_(cell, qp) * kokkosFieldMults_(0)(cell, qp);
217  for (int basis(0); basis < numBases; ++basis)
218  field_(cell, basis) += basis_(cell, basis, qp) * tmp_(cell);
219  } // end loop over the quadrature points
220  }
221  else
222  {
223  // Loop over the quadrature points and pre-multiply all the field
224  // multipliers together, scale the integrand by the multiplier and
225  // the combination of the field multipliers, and then perform the actual
226  // integration, looping over the bases.
227  const int numFieldMults(kokkosFieldMults_.extent(0));
228  for (int qp(0); qp < numQP; ++qp)
229  {
230  tmp2_(cell) = 1.0;
231  for (int fm(0); fm < numFieldMults; ++fm)
232  tmp2_(cell) *= kokkosFieldMults_(fm)(cell, qp);
233  tmp_(cell) = multiplier_ * scalar_(cell, qp) * tmp2_(cell);
234  for (int basis(0); basis < numBases; ++basis)
235  field_(cell, basis) += basis_(cell, basis, qp) * tmp_(cell);
236  } // end loop over the quadrature points
237  } // end if (NUM_FIELD_MULT == something)
238  } // end of operator()()
239 
241  //
242  // evaluateFields()
243  //
245  template<typename EvalT, typename Traits>
246  void
249  typename Traits::EvalData workset)
250  {
251  using Kokkos::parallel_for;
252  using Kokkos::RangePolicy;
253 
254  // Grab the basis information.
255  basis_ = this->wda(workset).bases[basisIndex_]->weighted_basis_scalar;
256 
257  // The following if-block is for the sake of optimization depending on the
258  // number of field multipliers. The parallel_fors will loop over the cells
259  // in the Workset and execute operator()() above.
260  if (fieldMults_.size() == 0)
261  parallel_for(RangePolicy<FieldMultTag<0>>(0, workset.num_cells), *this);
262  else if (fieldMults_.size() == 1)
263  parallel_for(RangePolicy<FieldMultTag<1>>(0, workset.num_cells), *this);
264  else
265  parallel_for(RangePolicy<FieldMultTag<-1>>(0, workset.num_cells), *this);
266  } // end of evaluateFields()
267 
269  //
270  // getValidParameters()
271  //
273  template<typename EvalT, typename TRAITS>
277  {
278  using panzer::BasisIRLayout;
280  using std::string;
281  using std::vector;
283  using Teuchos::RCP;
284  using Teuchos::rcp;
285 
286  // Create a ParameterList with all the valid keys we support.
287  RCP<ParameterList> p = rcp(new ParameterList);
288  p->set<string>("Residual Name", "?");
289  p->set<string>("Value Name", "?");
290  RCP<BasisIRLayout> basis;
291  p->set("Basis", basis);
293  p->set("IR", ir);
294  p->set<double>("Multiplier", 1.0);
296  p->set("Field Multipliers", fms);
297  return p;
298  } // end of getValidParameters()
299 
300 } // end of namespace panzer
301 
302 #endif // PANZER_INTEGRATOR_BASISTIMESSCALAR_IMPL_HPP
int num_cells
DEPRECATED - use: numCells()
const panzer::EvaluatorStyle evalStyle_
An enum determining the behavior of this Evaluator.
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...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
This empty struct allows us to optimize operator()() depending on the number of field multipliers...
Integrator_BasisTimesScalar(const panzer::EvaluatorStyle &evalStyle, const std::string &resName, const std::string &valName, const panzer::BasisIRLayout &basis, const panzer::IntegrationRule &ir, const double &multiplier=1, const std::vector< std::string > &fmNames=std::vector< std::string >())
Main Constructor.
EvaluatorStyle
An indication of how an Evaluator will behave.
KOKKOS_INLINE_FUNCTION void operator()(const FieldMultTag< NUM_FIELD_MULT > &tag, const std::size_t &cell) const
Perform the integration.
Teuchos::RCP< const PureBasis > getBasis() const
double multiplier
The scalar multiplier out in front of the integral ( ).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< PHX::DataLayout > dl_scalar
Data layout for scalar fields.
panzer::EvaluatorStyle evalStyle
The EvaluatorStyle of the parent Integrator_CurlBasisDotVector object.
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.
PHX::MDField< const ScalarT, panzer::Cell, panzer::IP > scalar_
A field representing the scalar function we&#39;re integrating ( ).
void evaluateFields(typename Traits::EvalData workset)
Evaluate Fields.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
PHX::View< Kokkos::View< const ScalarT **, typename PHX::DevLayout< ScalarT >::type, Kokkos::MemoryUnmanaged > * > kokkosFieldMults_
The PHX::View representation of the (possibly empty) list of fields that are multipliers out in front...
void postRegistrationSetup(typename Traits::SetupData sd, PHX::FieldManager< Traits > &fm)
Post-Registration Setup.
const std::vector< std::pair< int, LocalOrdinal > > &pid_and_lid const
Description and data layouts associated with a particular basis.
Teuchos::RCP< PHX::DataLayout > functional
&lt;Cell,Basis&gt;
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 ( ...
Teuchos::RCP< Teuchos::ParameterList > getValidParameters() const
Get Valid Parameters.
Teuchos::RCP< const std::vector< panzer::Workset > > worksets_