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 //
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_GLOBAL_STATISTICS_IMPL_HPP
44 #define PANZER_GLOBAL_STATISTICS_IMPL_HPP
45 
46 #include "Intrepid2_FunctionSpaceTools.hpp"
50 #include "Panzer_GlobalData.hpp"
51 #include "Panzer_IosAllSaver.hpp"
52 #include "Phalanx_DataLayout_MDALayout.hpp"
53 #include "Teuchos_ScalarTraits.hpp"
54 #include "Teuchos_CommHelpers.hpp"
55 #include <iomanip>
56 
57 namespace panzer {
58 
59 //**********************************************************************
60 template<typename EvalT, typename Traits>
63  const Teuchos::ParameterList& p)
64 {
65  comm = p.get< Teuchos::RCP<const Teuchos::Comm<int> > >("Comm");
66 
67  global_data = p.get<Teuchos::RCP<panzer::GlobalData> >("Global Data");
68 
69  // Expects a string that is a Colon separated list of field names to compute statistics on.
70  // for example the string "UX:UY:UZ:PRESSURE" would be separated into a vector with
71  // four fields, "UX", "UY", "UZ", and "PRESSURE".
72  std::string names_string = p.get<std::string>("Names");
73  std::vector<std::string> names;
74  panzer::StringTokenizer(names, names_string);
75 
77 
78  field_values.clear();
79  for (typename std::vector<std::string>::const_iterator name = names.begin(); name != names.end(); ++name)
80  field_values.push_back(PHX::MDField<const ScalarT,Cell,IP>(*name, ir->dl_scalar));
81 
82  Teuchos::RCP<PHX::MDALayout<Cell> > cell_dl = Teuchos::rcp(new PHX::MDALayout<Cell>(ir->dl_scalar->extent(0)));
83  volumes = PHX::MDField<ScalarT,Cell>("Cell Volumes",cell_dl);
84 
85  tmp = PHX::MDField<ScalarT,Cell>("GlobalStatistics:tmp:"+names_string,cell_dl);
86  ones = PHX::MDField<ScalarT,Cell,IP>("GlobalStatistics:ones:"+names_string,ir->dl_scalar);
87 
88  this->addEvaluatedField(volumes);
89  this->addEvaluatedField(tmp);
90  this->addEvaluatedField(ones);
91  for (typename std::vector<PHX::MDField<const ScalarT,Cell,IP> >::const_iterator field = field_values.begin();
92  field != field_values.end(); ++field) {
93  this->addDependentField(*field);
94  }
95 
96  averages.resize(field_values.size());
97  maxs.resize(field_values.size());
98  mins.resize(field_values.size());
99  global_maxs.resize(field_values.size());
100  global_mins.resize(field_values.size());
101  global_averages.resize(field_values.size());
102 
103  ir_order = ir->cubature_degree;
104 
105  std::string n = "GlobalStatistics: " + names_string;
106  this->setName(n);
107 }
108 
109 //**********************************************************************
110 template<typename EvalT, typename Traits>
111 void
114  typename Traits::SetupData sd,
115  PHX::FieldManager<Traits>& /* fm */)
116 {
117  ir_index = panzer::getIntegrationRuleIndex(ir_order,(*sd.worksets_)[0], this->wda);
118  for (typename PHX::MDField<ScalarT,Cell,IP>::size_type cell = 0; cell < ones.extent(0); ++cell)
119  for (typename PHX::MDField<ScalarT,Cell,IP>::size_type ip = 0; ip < ones.extent(1); ++ip)
120  ones(cell,ip) = 1.0;
121 }
122 
123 //**********************************************************************
124 template<typename EvalT, typename Traits>
125 void
128  typename Traits::EvalData workset)
129 {
130  if (workset.num_cells == 0)
131  return;
132 
133  Intrepid2::FunctionSpaceTools<PHX::Device::execution_space>::integrate(volumes.get_view(),
134  ones.get_view(),
135  (this->wda(workset).int_rules[ir_index])->weighted_measure.get_view());
136 
137  for (index_t cell = 0; cell < workset.num_cells; ++cell)
138  total_volume += volumes(cell);
139 
140  typename std::vector<PHX::MDField<ScalarT,Cell,IP> >::size_type field_index = 0;
141  for (typename std::vector<PHX::MDField<const ScalarT,Cell,IP> >::iterator field = field_values.begin();
142  field != field_values.end(); ++field,++field_index) {
143 
144  Intrepid2::FunctionSpaceTools<PHX::Device::execution_space>::integrate(tmp.get_view(),
145  field->get_view(),
146  (this->wda(workset).int_rules[ir_index])->weighted_measure.get_view());
147 
148  for (index_t cell = 0; cell < workset.num_cells; ++cell) {
149  averages[field_index] += tmp(cell);
150 
151  for (typename PHX::MDField<ScalarT,Cell,IP>::size_type ip = 0; ip < (field->extent(1)); ++ip) {
152  maxs[field_index] = std::max( (*field)(cell,ip), maxs[field_index]);
153  mins[field_index] = std::min( (*field)(cell,ip), mins[field_index]);
154  }
155  }
156 
157  }
158 }
159 
160 //**********************************************************************
161 template<typename EvalT, typename Traits>
162 void
165  typename Traits::PreEvalData /* data */)
166 {
167  total_volume = Teuchos::ScalarTraits<ScalarT>::zero();
168 
169  for (typename std::vector<ScalarT>::iterator field = averages.begin(); field != averages.end(); ++field)
171 
172  for (typename std::vector<ScalarT>::iterator field = maxs.begin(); field != maxs.end(); ++field)
174 
175  for (typename std::vector<ScalarT>::iterator field = mins.begin(); field != mins.end(); ++field)
177 }
178 
179 //**********************************************************************
180 template<typename EvalT, typename Traits>
181 void
184  typename Traits::PostEvalData /* data */)
185 {
186  this->postprocess(*(global_data->os));
187 }
188 
189 //**********************************************************************
190 template<typename EvalT, typename TRAITS>
192 {
193  // throw unless specialized for residual evaluations
194  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"SHOULD NEVER BE CALLED!");
195 }
196 
197 //**********************************************************************
198 template<>
200 {
201  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, static_cast<int>(1), &total_volume, &global_total_volume);
202  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, static_cast<int>(averages.size()), &averages[0], &global_averages[0]);
203  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, static_cast<int>(maxs.size()), &maxs[0], &global_maxs[0]);
204  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MIN, static_cast<int>(mins.size()), &mins[0], &global_mins[0]);
205 
206  for (std::vector<ScalarT>::size_type i = 0; i < field_values.size(); ++i)
207  global_averages[i] /= global_total_volume;
208 
209  if (comm->getRank() == 0) {
210 
211  panzer::ios_all_saver saver(os);
212 
213  std::size_t precision = 8;
214  os << std::scientific << std::showpoint << std::setprecision(precision) << std::left;
215 
216  std::size_t name_width = 0;
217  for (std::vector<ScalarT>::size_type i = 0; i < field_values.size(); ++i)
218  name_width = std::max(name_width,field_values[i].fieldTag().name().size());
219 
220  std::size_t value_width = precision + 7;
221 
222  os << std::setw(name_width) << "Field"
223  << " " << std::setw(value_width) << "Average"
224  << " " << std::setw(value_width) << "Maximum (@IP)"
225  << " " << std::setw(value_width) << "Minimum (@IP)"
226  << std::endl;
227 
228  for (std::vector<ScalarT>::size_type i = 0; i < field_values.size(); ++i) {
229  os << std::setw(name_width) << field_values[i].fieldTag().name()
230  << " " << std::setw(value_width) << global_averages[i]
231  << " " << std::setw(value_width) << global_maxs[i]
232  << " " << std::setw(value_width) << global_mins[i] << std::endl;
233  }
234 
235  }
236 
237 }
238 
239 //**********************************************************************
240 template<typename EvalT, typename TRAITS>
242 {
243  return tmp.fieldTag();
244 }
245 
246 
247 } // namespace panzer
248 
249 
250 
251 #endif
252 
const PHX::FieldTag & getRequiredFieldTag()
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< std::string >::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)
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
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_