Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_GlobalStatistics_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_GLOBAL_STATISTICS_IMPL_HPP
12 #define PANZER_GLOBAL_STATISTICS_IMPL_HPP
13 
14 #include "Intrepid2_FunctionSpaceTools.hpp"
18 #include "Panzer_GlobalData.hpp"
19 #include "Panzer_IosAllSaver.hpp"
20 #include "Phalanx_DataLayout_MDALayout.hpp"
21 #include "Teuchos_ScalarTraits.hpp"
22 #include "Teuchos_CommHelpers.hpp"
23 #include <iomanip>
24 
25 namespace panzer {
26 
27 //**********************************************************************
28 template<typename EvalT, typename Traits>
31  const Teuchos::ParameterList& p)
32 {
33  comm = p.get< Teuchos::RCP<const Teuchos::Comm<int> > >("Comm");
34 
35  global_data = p.get<Teuchos::RCP<panzer::GlobalData> >("Global Data");
36 
37  // Expects a string that is a Colon separated list of field names to compute statistics on.
38  // for example the string "UX:UY:UZ:PRESSURE" would be separated into a vector with
39  // four fields, "UX", "UY", "UZ", and "PRESSURE".
40  std::string names_string = p.get<std::string>("Names");
41  std::vector<std::string> names;
42  panzer::StringTokenizer(names, names_string);
43 
45 
46  field_values.clear();
47  for (typename std::vector<std::string>::const_iterator name = names.begin(); name != names.end(); ++name)
48  field_values.push_back(PHX::MDField<const ScalarT,Cell,IP>(*name, ir->dl_scalar));
49 
51  volumes = PHX::MDField<ScalarT,Cell>("Cell Volumes",cell_dl);
52 
53  tmp = PHX::MDField<ScalarT,Cell>("GlobalStatistics:tmp:"+names_string,cell_dl);
54  ones = PHX::MDField<ScalarT,Cell,IP>("GlobalStatistics:ones:"+names_string,ir->dl_scalar);
55 
56  this->addEvaluatedField(volumes);
57  this->addEvaluatedField(tmp);
58  this->addEvaluatedField(ones);
59  for (typename std::vector<PHX::MDField<const ScalarT,Cell,IP> >::const_iterator field = field_values.begin();
60  field != field_values.end(); ++field) {
61  this->addDependentField(*field);
62  }
63 
64  averages.resize(field_values.size());
65  maxs.resize(field_values.size());
66  mins.resize(field_values.size());
67  global_maxs.resize(field_values.size());
68  global_mins.resize(field_values.size());
69  global_averages.resize(field_values.size());
70 
71  ir_order = ir->cubature_degree;
72 
73  std::string n = "GlobalStatistics: " + names_string;
74  this->setName(n);
75 }
76 
77 //**********************************************************************
78 template<typename EvalT, typename Traits>
79 void
82  typename Traits::SetupData sd,
83  PHX::FieldManager<Traits>& /* fm */)
84 {
85  ir_index = panzer::getIntegrationRuleIndex(ir_order,(*sd.worksets_)[0], this->wda);
86  auto l_ones = ones.get_static_view();
87  Kokkos::parallel_for("GlobalStatistics", l_ones.extent(0), KOKKOS_LAMBDA(int cell) {
88  for (std::size_t ip = 0; ip < l_ones.extent(1); ++ip)
89  l_ones(cell,ip) = 1.0;
90  });
91 }
92 
93 //**********************************************************************
94 template<typename EvalT, typename Traits>
95 void
98  typename Traits::EvalData workset)
99 {
100  if (workset.num_cells == 0)
101  return;
102 
103  Intrepid2::FunctionSpaceTools<PHX::Device::execution_space>::integrate(volumes.get_view(),
104  ones.get_view(),
105  (this->wda(workset).int_rules[ir_index])->weighted_measure.get_view());
106  auto volumes_h = Kokkos::create_mirror_view(as_view(volumes));
107  Kokkos::deep_copy(volumes_h, as_view(volumes));
108 
109  for (index_t cell = 0; cell < workset.num_cells; ++cell)
110  total_volume += volumes_h(cell);
111 
112  typename std::vector<PHX::MDField<ScalarT,Cell,IP> >::size_type field_index = 0;
113  for (typename std::vector<PHX::MDField<const ScalarT,Cell,IP> >::iterator field = field_values.begin();
114  field != field_values.end(); ++field,++field_index) {
115 
116  Intrepid2::FunctionSpaceTools<PHX::Device::execution_space>::integrate(tmp.get_view(),
117  field->get_view(),
118  (this->wda(workset).int_rules[ir_index])->weighted_measure.get_view());
119  auto tmp_h = Kokkos::create_mirror_view(tmp.get_static_view());
120  auto field_h = Kokkos::create_mirror_view( field->get_static_view());
121  Kokkos::deep_copy(tmp_h, tmp.get_static_view());
122  Kokkos::deep_copy(field_h, field->get_static_view());
123 
124 
125  for (index_t cell = 0; cell < workset.num_cells; ++cell) {
126  averages[field_index] += tmp_h(cell);
127 
128  for (typename PHX::MDField<ScalarT,Cell,IP>::size_type ip = 0; ip < (field->extent(1)); ++ip) {
129  maxs[field_index] = std::max( field_h(cell,ip), maxs[field_index]);
130  mins[field_index] = std::min( field_h(cell,ip), mins[field_index]);
131  }
132  }
133 
134  }
135 }
136 
137 //**********************************************************************
138 template<typename EvalT, typename Traits>
139 void
142  typename Traits::PreEvalData /* data */)
143 {
144  total_volume = Teuchos::ScalarTraits<ScalarT>::zero();
145 
146  for (typename std::vector<ScalarT>::iterator field = averages.begin(); field != averages.end(); ++field)
148 
149  for (typename std::vector<ScalarT>::iterator field = maxs.begin(); field != maxs.end(); ++field)
151 
152  for (typename std::vector<ScalarT>::iterator field = mins.begin(); field != mins.end(); ++field)
154 }
155 
156 //**********************************************************************
157 template<typename EvalT, typename Traits>
158 void
161  typename Traits::PostEvalData /* data */)
162 {
163  this->postprocess(*(global_data->os));
164 }
165 
166 //**********************************************************************
167 template<typename EvalT, typename TRAITS>
169 {
170  // throw unless specialized for residual evaluations
171  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"SHOULD NEVER BE CALLED!");
172 }
173 
174 //**********************************************************************
175 template<>
177 {
178  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, static_cast<int>(1), &total_volume, &global_total_volume);
179  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, static_cast<int>(averages.size()), &averages[0], &global_averages[0]);
180  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, static_cast<int>(maxs.size()), &maxs[0], &global_maxs[0]);
181  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MIN, static_cast<int>(mins.size()), &mins[0], &global_mins[0]);
182 
183  for (std::vector<ScalarT>::size_type i = 0; i < field_values.size(); ++i)
184  global_averages[i] /= global_total_volume;
185 
186  if (comm->getRank() == 0) {
187 
188  panzer::ios_all_saver saver(os);
189 
190  std::size_t precision = 8;
191  os << std::scientific << std::showpoint << std::setprecision(precision) << std::left;
192 
193  std::size_t name_width = 0;
194  for (std::vector<ScalarT>::size_type i = 0; i < field_values.size(); ++i)
195  name_width = std::max(name_width,field_values[i].fieldTag().name().size());
196 
197  std::size_t value_width = precision + 7;
198 
199  os << std::setw(name_width) << "Field"
200  << " " << std::setw(value_width) << "Average"
201  << " " << std::setw(value_width) << "Maximum (@IP)"
202  << " " << std::setw(value_width) << "Minimum (@IP)"
203  << std::endl;
204 
205  for (std::vector<ScalarT>::size_type i = 0; i < field_values.size(); ++i) {
206  os << std::setw(name_width) << field_values[i].fieldTag().name()
207  << " " << std::setw(value_width) << global_averages[i]
208  << " " << std::setw(value_width) << global_maxs[i]
209  << " " << std::setw(value_width) << global_mins[i] << std::endl;
210  }
211 
212  }
213 
214 }
215 
216 //**********************************************************************
217 template<typename EvalT, typename TRAITS>
219 {
220  return tmp.fieldTag();
221 }
222 
223 
224 } // namespace panzer
225 
226 
227 
228 #endif
229 
const PHX::FieldTag & getRequiredFieldTag()
int num_cells
DEPRECATED - use: numCells()
void evaluateFields(typename Traits::EvalData d)
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
std::vector< int >::size_type getIntegrationRuleIndex(int ir_degree, const panzer::Workset &workset, WorksetDetailsAccessor &wda)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
GlobalStatistics(const Teuchos::ParameterList &p)
Teuchos::RCP< PHX::DataLayout > dl_scalar
Data layout for scalar fields.
void preEvaluate(typename Traits::PreEvalData d)
void postEvaluate(typename Traits::PostEvalData d)
KOKKOS_FORCEINLINE_FUNCTION array_type get_static_view()
void postRegistrationSetup(typename Traits::SetupData d, PHX::FieldManager< Traits > &fm)
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...
void StringTokenizer(std::vector< std::string > &tokens, const std::string &str, const std::string delimiters, bool trim)
Tokenize a string, put tokens in a vector.
Teuchos::RCP< const std::vector< panzer::Workset > > worksets_