Panzer  Version of the Day
Panzer_Integrator_GradBasisTimesScalar_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_EVALUATOR_GRADBASISTIMESSCALAR_IMPL_HPP
44 #define PANZER_EVALUATOR_GRADBASISTIMESSCALAR_IMPL_HPP
45 
46 #include "Intrepid2_FunctionSpaceTools.hpp"
48 #include "Panzer_BasisIRLayout.hpp"
50 #include "Kokkos_ViewFactory.hpp"
51 
52 namespace panzer {
53 
54 //**********************************************************************
55 PHX_EVALUATOR_CTOR(Integrator_GradBasisTimesScalar,p):
58  _basis_index(-1)
59 {
60 
61  // TODO: Get all of the panzer modules to read in const integration rules and basisIRlayouts
62  Teuchos::RCP<const BasisIRLayout> basis_layout = p.get< Teuchos::RCP<BasisIRLayout> >("Basis").getConst();
63  Teuchos::RCP<const panzer::IntegrationRule> ir = p.get< Teuchos::RCP<panzer::IntegrationRule> >("IR").getConst();
64  _num_dims = ir->dl_vector->dimension(2);
65 
66  Teuchos::RCP<const PureBasis> basis = basis_layout->getBasis();
67  _basis_name = basis_layout->name();
68 
69  // Verify that this basis supports the gradient operation
70  TEUCHOS_TEST_FOR_EXCEPTION(!basis->supportsGrad(),std::logic_error,"Integrator_GradBasisTimesScalar: Basis of type \"" << basis->name() << "\" does not support GRAD");
71 
72  // Setup residuals
73  _residuals.clear();
74  if (p.isType<Teuchos::RCP<const std::vector<std::string> > >("Residual Names")){
75  const std::vector<std::string> & names = *(p.get<Teuchos::RCP<const std::vector<std::string> > >("Residual Names"));
76  for(const auto & name : names){
77  PHX::MDField<ScalarT,Cell,BASIS> res(name, basis_layout->functional);
78  _residuals.push_back(res);
79  }
80  }
81 
82  // Currently we only allow for vectors of length 3
83  TEUCHOS_TEST_FOR_EXCEPTION(_num_dims != _residuals.size(),std::logic_error,"Number of residuals must equal number of gradient components (mesh dimensions).");
84 
85  // Setup vector
86  _scalar = PHX::MDField<const ScalarT,Cell,IP>(p.get<std::string>("Scalar Name"), ir->dl_scalar);
87 
88  // Read the scalar multiplier
89  _multiplier = ScalarT(p.get<double>("Multiplier"));
90 
91  // Setup field multipliers
92  if (p.isType<Teuchos::RCP<const std::vector<std::string> > >("Field Multipliers")){
93  const std::vector<std::string> & field_multiplier_names = *(p.get<Teuchos::RCP<const std::vector<std::string> > >("Field Multipliers"));
94  for (const std::string & name : field_multiplier_names){
95  _field_multipliers.push_back(PHX::MDField<const ScalarT,Cell,IP>(name, ir->dl_scalar));
96  }
97  }
98 
99  // Register evaluated fields
100  for (auto & residual : _residuals){
101  this->addEvaluatedField(residual);
102  }
103 
104  // Register dependent fields
105  this->addDependentField(_scalar);
106  for (auto & field : _field_multipliers){
107  this->addDependentField(field);
108  }
109 
110  // Name the module
111  this->setName("Integrator_GradBasisTimesScalar: " + _scalar.fieldTag().name());
112 }
113 
114 //**********************************************************************
115 PHX_POST_REGISTRATION_SETUP(Integrator_GradBasisTimesScalar,sd,fm)
116 {
117 
118  for (auto & residual : _residuals){
119  this->utils.setFieldData(residual,fm);
120  }
121 
122  this->utils.setFieldData(_scalar,fm);
123 
124  for (auto & field : _field_multipliers){
125  this->utils.setFieldData(field,fm);
126  }
127 
128  _num_basis_nodes = _residuals[0].dimension(1);
129  _num_quadrature_points = _scalar.dimension(1);
130 
131  _basis_index = panzer::getBasisIndex(_basis_name, (*sd.worksets_)[0], this->wda);
132 
133  // TODO: figure out a clean way of cloning _vector
134  _tmp = Kokkos::createDynRankView(_residuals[0].get_static_view(),"tmp",_scalar.dimension(0), _num_quadrature_points);
135 }
136 
137 //**********************************************************************
138 PHX_EVALUATE_FIELDS(Integrator_GradBasisTimesScalar,workset)
139 {
140 
141  // Zero the residuals
142  for(int i=0; i<_num_dims; ++i){
143  Kokkos::deep_copy(_residuals[i].get_static_view(), ScalarT(0.0));
144  }
145 
146  // do a scaled copy to initialize _tmp
147  for (int i=0; i < _scalar.extent_int(0); ++i)
148  for (int j=0; j < _scalar.extent_int(1); ++j)
149  _tmp(i,j) = _multiplier * _scalar(i,j);
150 
151  // Apply the field multipliers
152  for(auto & field_data : _field_multipliers){
153  for (index_t cell = 0; cell < workset.num_cells; ++cell) {
154  for (std::size_t qp = 0; qp < _num_quadrature_points; ++qp) {
155  _tmp(cell,qp) *= field_data(cell,qp);
156  }
157  }
158  }
159 
160  const BasisValues2<double> & bv = *this->wda(workset).bases[_basis_index];
161 
162  for (index_t cell = 0; cell < workset.num_cells; ++cell){
163  for (std::size_t basis = 0; basis < _num_basis_nodes; ++basis){
164  for (std::size_t qp = 0; qp < _num_quadrature_points; ++qp){
165  for(std::size_t dim = 0; dim < _num_dims; ++dim){
166  _residuals[dim](cell,basis) += _tmp(cell,qp)*bv.weighted_grad_basis(cell,basis,qp,dim);
167  }
168  }
169  }
170  }
171 
172 }
173 
174 //**********************************************************************
175 
176 }
177 
178 #endif
PHX::MDField< ScalarT > residual
Evaluates a Dirichlet BC residual corresponding to a field value.
std::vector< std::string >::size_type getBasisIndex(std::string basis_name, panzer::Workset &workset, WorksetDetailsAccessor &wda)
Returns the index in the workset bases for a particular BasisIRLayout name.
Kokkos::DynRankView< ScalarT, PHX::Device > _tmp
std::vector< PHX::MDField< ScalarT, Cell, BASIS > > _residuals
PHX::MDField< const ScalarT, Cell, IP > _scalar
Kokkos::DynRankView< typename InputArray::value_type, PHX::Device > createDynRankView(const InputArray &a, const std::string &name, const DimensionPack... dims)
Wrapper to simplify Panzer use of Sacado ViewFactory.
PHX_EVALUATOR_CTOR(BasisValues_Evaluator, p)
PHX::MDField< const ScalarT, Cell, IP > field
Array_CellBasisIPDim weighted_grad_basis
PHX_EVALUATE_FIELDS(BasisValues_Evaluator, workset)
Teuchos::RCP< const panzer::PureBasis > basis
Interpolates basis DOF values to IP DOF values.
PHX_POST_REGISTRATION_SETUP(BasisValues_Evaluator, sd, fm)
std::vector< PHX::MDField< const ScalarT, Cell, IP > > _field_multipliers