VertexCFD  0.0-dev
VertexCFD_Closure_VariableOldValue_impl.hpp
1 #ifndef VERTEXCFD_CLOSURE_VARIABLEOLDVALUE_IMPL_HPP
2 #define VERTEXCFD_CLOSURE_VARIABLEOLDVALUE_IMPL_HPP
3 
4 #include "utils/VertexCFD_Utils_GetValue.hpp"
5 
6 #include <Panzer_HierarchicParallelism.hpp>
7 
8 #include <Teuchos_StandardParameterEntryValidators.hpp>
9 
10 namespace VertexCFD
11 {
12 namespace ClosureModel
13 {
14 //---------------------------------------------------------------------------//
15 template<class EvalType, class Traits>
16 VariableOldValue<EvalType, Traits>::VariableOldValue(
17  const panzer::IntegrationRule& ir,
18  const Teuchos::ParameterList& closure_params)
19  : _old_value_type(OldValueType::LastTime)
20  , _make_grad(true)
21  , _field_name(closure_params.get<std::string>("Field Name"))
22  , _prefix("OLD_")
23  , _nl_iter(-1)
24  , _stage(0)
25  , _time(0.0)
26  , _var(_field_name, ir.dl_scalar)
27  , _grad_var("GRAD_" + _field_name, ir.dl_vector)
28 {
29  // Check old value type
30  if (closure_params.isType<std::string>("Old Value Type"))
31  {
32  const auto type_validator = Teuchos::rcp(
33  new Teuchos::StringToIntegralParameterEntryValidator<OldValueType>(
34  Teuchos::tuple<std::string>("LastTime", "LastStage"),
35  "LastTime"));
36 
37  _old_value_type = type_validator->getIntegralValue(
38  closure_params.get<std::string>("Old Value Type"));
39  }
40 
41  // Set suffix
42  if (_old_value_type == OldValueType::LastStage)
43  _prefix = "STAR_";
44 
45  // Check for gradient
46  if (closure_params.isType<bool>("Save Old Gradient"))
47  _make_grad = closure_params.get<bool>("Save Old Gradient");
48 
49  // Initialize old fields
50  _var_old_val = PHX::MDField<double, panzer::Cell, panzer::Point>(
51  _prefix + _field_name, ir.dl_scalar);
52  if (_make_grad)
53  _grad_var_old_val
54  = PHX::MDField<double, panzer::Cell, panzer::Point, panzer::Dim>(
55  _prefix + "GRAD_" + _field_name, ir.dl_vector);
56 
57  // Add evaludated fields
58  this->addEvaluatedField(_var_old_val);
59  if (_make_grad)
60  this->addEvaluatedField(_grad_var_old_val);
61 
62  // Add dependent fields
63  this->addDependentField(_var);
64  if (_make_grad)
65  this->addDependentField(_grad_var);
66 
67  // Closure model name
68  this->setName(_field_name + " Old Value");
69 }
70 
71 //---------------------------------------------------------------------------//
72 template<class EvalType, class Traits>
73 void VariableOldValue<EvalType, Traits>::evaluateFields(
74  typename Traits::EvalData workset)
75 {
76  const double curr_time = workset.time;
77  const double curr_stage = workset.stage_number;
78 
79  // If new time or new stage, reset non-linear iteration counter and update
80  if ((curr_stage > _stage) || (curr_time != _time))
81  {
82  _nl_iter = -1;
83  _stage = curr_stage;
84  _time = curr_time;
85  }
86 
87  ++_nl_iter;
88 
89  auto policy = panzer::HP::inst().teamPolicy<scalar_type, PHX::Device>(
90  workset.num_cells);
91  Kokkos::parallel_for(this->getName(), policy, *this);
92 }
93 
94 //---------------------------------------------------------------------------//
95 template<class EvalType, class Traits>
96 KOKKOS_INLINE_FUNCTION void VariableOldValue<EvalType, Traits>::operator()(
97  const Kokkos::TeamPolicy<PHX::exec_space>::member_type& team) const
98 {
99  const int cell = team.league_rank();
100  const int num_point = _var.extent(1);
101  const int num_space_dim = _make_grad ? _grad_var.extent(2) : 0;
102 
103  // Default: no update
104  bool update = false;
105 
106  // Update last time values on 0th NL iteration of 0th stage
107  if ((_old_value_type == OldValueType::LastTime) && (_stage == 0)
108  && (_nl_iter == 0))
109  {
110  update = true;
111  }
112  // Update last stage values on 0th NL iteration
113  else if ((_old_value_type == OldValueType::LastStage) && (_nl_iter == 0))
114  {
115  update = true;
116  }
117 
118  if (update)
119  {
120  using Utils::getValue;
121 
122  Kokkos::parallel_for(
123  Kokkos::TeamThreadRange(team, 0, num_point), [&](const int point) {
124  _var_old_val(cell, point) = getValue(_var(cell, point));
125 
126  for (int dim = 0; dim < num_space_dim; ++dim)
127  {
128  _grad_var_old_val(cell, point, dim)
129  = getValue(_grad_var(cell, point, dim));
130  }
131  });
132  }
133 }
134 
135 //---------------------------------------------------------------------------//
136 
137 } // end namespace ClosureModel
138 } // end namespace VertexCFD
139 
140 #endif // end VERTEXCFD_CLOSURE_VARIABLEOLDVALUE_IMPL_HPP
VertexCFD
Definition: tstMethodManufacturedSolutionBC.cpp:23