SPHinXsys  alpha version
ensemble_averaged_method.hpp
1 
6 #pragma once
7 
9 
10  //=================================================================================================//
11 namespace SPH
12 {
13  //=================================================================================================//
14  template<class ObserveMethodType>
16  DoubleVec<Real> &meanvalue_new, DoubleVec<Real> &variance, DoubleVec<Real> &variance_new)
17  {
18  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
19  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
20  for (int run_index = 0; run_index != this->number_of_run_; ++run_index)
21  variance_new[snapshot_index][observation_index] = SMAX(variance[snapshot_index][observation_index], variance_new[snapshot_index][observation_index],
22  std::pow((result[run_index][snapshot_index][observation_index] - meanvalue_new[snapshot_index][observation_index]), 2), std::pow(meanvalue_new[snapshot_index][observation_index] * 1.0e-2, 2));
23  };
24  //=================================================================================================//
25  template<class ObserveMethodType>
27  DoubleVec<Vecd> &meanvalue_new, DoubleVec<Vecd> &variance, DoubleVec<Vecd> &variance_new)
28  {
29  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
30  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
31  for (int run_index = 0; run_index != this->number_of_run_; ++run_index)
32  for (int dimension_index = 0; dimension_index != variance[0][0].size(); ++dimension_index)
33  variance_new[snapshot_index][observation_index][dimension_index] = SMAX(variance[snapshot_index][observation_index][dimension_index], variance_new[snapshot_index][observation_index][dimension_index],
34  std::pow((result[run_index][snapshot_index][observation_index][dimension_index] - meanvalue_new[snapshot_index][observation_index][dimension_index]), 2),
35  std::pow(meanvalue_new[snapshot_index][observation_index][dimension_index] * 1.0e-2, 2));
36  };
37  //=================================================================================================//
38  template<class ObserveMethodType>
40  DoubleVec<Matd> &meanvalue_new, DoubleVec<Matd> &variance, DoubleVec<Matd> &variance_new)
41  {
42  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
43  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
44  for (int run_index = 0; run_index != this->number_of_run_; ++run_index)
45  for (size_t dimension_index_i = 0; dimension_index_i != variance[0][0].size(); ++dimension_index_i)
46  for (size_t dimension_index_j = 0; dimension_index_j != variance[0][0].size(); ++dimension_index_j)
47  variance_new[snapshot_index][observation_index][dimension_index_i][dimension_index_j] = SMAX(variance[snapshot_index][observation_index][dimension_index_i][dimension_index_j], variance_new[snapshot_index][observation_index][dimension_index_i][dimension_index_j],
48  std::pow((result[run_index][snapshot_index][observation_index][dimension_index_i][dimension_index_j] - meanvalue_new[snapshot_index][observation_index][dimension_index_i][dimension_index_j]), 2),
49  std::pow(meanvalue_new[snapshot_index][observation_index][dimension_index_i][dimension_index_j] * 1.0e-2, 2));
50  };
51  //=================================================================================================//
52  template<class ObserveMethodType>
54  DoubleVec<Real> &parameter, DoubleVec<Real> &parameter_new, Real &threshold)
55  {
56  int count = 0;
57  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
58  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
59  {
60  Real relative_value_ = ABS((parameter[snapshot_index][observation_index] - parameter_new[snapshot_index][observation_index]) / (parameter_new[snapshot_index][observation_index] + TinyReal));
61  if (relative_value_ > threshold)
62  {
63  std::cout << par_name << ": " << this->quantity_name_ << "[" << observation_index << "] in " << this->element_tag_[snapshot_index]
64  << " is not converged, and difference is " << relative_value_ << endl;
65  count++;
66  }
67  }
68  return count;
69  };
71  template<class ObserveMethodType>
73  DoubleVec<Vecd> &parameter, DoubleVec<Vecd> &parameter_new, Vecd &threshold)
74  {
75  int count = 0;
76  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
77  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
78  for (int dimension_index = 0; dimension_index != parameter[0][0].size(); ++dimension_index)
79  {
80  Real relative_value_ = ABS((parameter[snapshot_index][observation_index][dimension_index] - parameter_new[snapshot_index][observation_index][dimension_index]) / (parameter_new[snapshot_index][observation_index][dimension_index] + TinyReal));
81  if (relative_value_ > threshold[dimension_index])
82  {
83  std::cout << par_name << ": " << this->quantity_name_ << "[" << observation_index << "][" << dimension_index << "] in " << this->element_tag_[snapshot_index]
84  << " is not converged, and difference is " << relative_value_ << endl;
85  count++;
86  }
87  }
88  return count;
89  };
91  template<class ObserveMethodType>
93  DoubleVec<Matd> &parameter, DoubleVec<Matd> &parameter_new, Matd &threshold)
94  {
95  int count = 0;
96  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
97  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
98  for (int dimension_index_i = 0; dimension_index_i != parameter[0][0].size(); ++dimension_index_i)
99  for (int dimension_index_j = 0; dimension_index_j != parameter[0][0].size(); ++dimension_index_j)
100  {
101  Real relative_value_ = ABS(parameter[snapshot_index][observation_index][dimension_index_i][dimension_index_j] - parameter_new[snapshot_index][observation_index][dimension_index_i][dimension_index_j])
102  / (parameter_new[snapshot_index][observation_index][dimension_index_i][dimension_index_j] + TinyReal);
103  if (relative_value_ > threshold[dimension_index_i][dimension_index_j])
104  {
105  std::cout << par_name << ": " << this->quantity_name_ << "[" << observation_index << "][" << dimension_index_i << "][" << dimension_index_j << " ] in "
106  << this->element_tag_[snapshot_index] << " is not converged, and difference is " << relative_value_ << endl;
107  count++;
108  }
109  }
110  return count;
111  };
112  //=================================================================================================//
113  template<class ObserveMethodType>
114  int RegressionTestEnsembleAveraged<ObserveMethodType>::testNewResult(int diff, DoubleVec<Real> &current_result,
115  DoubleVec<Real> &meanvalue, DoubleVec<Real> &variance)
116  {
117  int count = 0;
118  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
119  {
120  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
121  {
122  Real relative_value_ = (std::pow(current_result[snapshot_index][observation_index] - meanvalue[snapshot_index + diff][observation_index], 2) - variance[snapshot_index + diff][observation_index]) / variance[snapshot_index + diff][observation_index];
123  if (relative_value_ > 0.01)
124  {
125  std::cout << this->quantity_name_ << "[" << observation_index << "] in " << this->element_tag_[snapshot_index] << " is beyond the exception, and difference is "
126  << relative_value_ << endl;
127  count++;
128  }
129  }
130  }
131  return count;
132  };
133  //=================================================================================================//
134  template<class ObserveMethodType>
135  int RegressionTestEnsembleAveraged<ObserveMethodType>::testNewResult(int diff, DoubleVec<Vecd> &current_result,
136  DoubleVec<Vecd> &meanvalue, DoubleVec<Vecd> &variance)
137  {
138  int count = 0;
139  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
140  {
141  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
142  {
143  for (int dimension_index = 0; dimension_index != meanvalue[0][0].size(); ++dimension_index)
144  {
145  Real relative_value_ = (std::pow(current_result[snapshot_index][observation_index][dimension_index] - meanvalue[snapshot_index + diff][observation_index][dimension_index], 2) - variance[snapshot_index + diff][observation_index][dimension_index]) / variance[snapshot_index + diff][observation_index][dimension_index];
146  if (relative_value_ > 0.01)
147  {
148  std::cout << this->quantity_name_ << "[" << observation_index << "][" << dimension_index << "] in " << this->element_tag_[snapshot_index] << " is beyond the exception, and difference is "
149  << relative_value_ << endl;
150  count++;
151  }
152  }
153  }
154  }
155  return count;
156  };
157  //=================================================================================================//
158  template<class ObserveMethodType>
159  int RegressionTestEnsembleAveraged<ObserveMethodType>::testNewResult(int diff, DoubleVec<Matd> &current_result,
160  DoubleVec<Matd> &meanvalue, DoubleVec<Matd> &variance)
161  {
162  int count = 0;
163  std::cout << "The current length difference is " << diff << "." << endl;
164  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
165  {
166  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
167  {
168  for (int dimension_index_i = 0; dimension_index_i != meanvalue[0][0].size(); ++dimension_index_i)
169  {
170  for (int dimension_index_j = 0; dimension_index_j != meanvalue[0][0].size(); ++dimension_index_j)
171  {
172  Real relative_value_ = (std::pow(current_result[snapshot_index][observation_index][dimension_index_i][dimension_index_j] - meanvalue[snapshot_index + diff][observation_index][dimension_index_i][dimension_index_j], 2) - variance[snapshot_index + diff][observation_index][dimension_index_i][dimension_index_j]) / variance[snapshot_index + diff][observation_index][dimension_index_i][dimension_index_j];
173  if (relative_value_ > 0.01)
174  {
175  std::cout << this->quantity_name_ << "[" << observation_index << "][" << dimension_index_i << "] in " << this->element_tag_[snapshot_index] << " is beyond the exception, and difference is "
176  << relative_value_ << endl;
177  count++;
178  }
179  }
180  }
181  }
182  }
183  return count;
184  };
185  //=================================================================================================//
186  template<class ObserveMethodType>
188  {
189  this->snapshot_ = this->current_result_.size();
190  this->observation_ = this->current_result_[0].size();
191 
192  if (this->number_of_run_ > 1)
193  {
194  if (this->converged == "false" ) /*< To identify the database generation or new result testing. */
195  {
196  if (!fs::exists(this->result_filefullpath_))
197  {
198  std::cout << "\n Error: the input file:" << this->result_filefullpath_ << " is not exists" << std::endl;
199  std::cout << __FILE__ << ':' << __LINE__ << std::endl;
200  exit(1);
201  }
202  else
203  this->result_xml_engine_in_.loadXmlFile(this->result_filefullpath_);
204  }
205 
206  if (!fs::exists(this->mean_variance_filefullpath_))
207  {
208  std::cout << "\n Error: the input file:" << this->mean_variance_filefullpath_ << " is not exists" << std::endl;
209  std::cout << __FILE__ << ':' << __LINE__ << std::endl;
210  exit(1);
211  }
212  else
213  {
214  this->mean_variance_xml_engine_in_.loadXmlFile(this->mean_variance_filefullpath_);
215  SimTK::Xml::Element mean_element_ = this->mean_variance_xml_engine_in_.getChildElement("Mean_Element");
216  this->number_of_snapshot_old_ = std::distance(mean_element_.element_begin(), mean_element_.element_end());
217 
218  DoubleVec<VariableType> temp(SMAX(this->snapshot_, this->number_of_snapshot_old_), StdVec<VariableType>(this->observation_));
219  meanvalue_ = temp;
220  variance_ = temp;
221 
223  if (this->number_of_snapshot_old_ < this->snapshot_)
224  {
225  this->difference_ = this->snapshot_ - this->number_of_snapshot_old_;
226  for (int delete_ = 0; delete_ != this->difference_; ++delete_)
227  this->current_result_.pop_back();
228  }
229  else if (this->number_of_snapshot_old_ > this->snapshot_)
230  this->difference_ = this->number_of_snapshot_old_ - this->snapshot_;
231  else
232  this->difference_ = 0;
233  }
234  }
235  else if (this->number_of_run_ == 1)
236  {
237  this->number_of_snapshot_old_ = this->snapshot_;
238  DoubleVec<VariableType> temp(this->snapshot_, StdVec<VariableType>(this->observation_));
239  this->result_.push_back(this->current_result_);
240  meanvalue_ = temp;
241  variance_ = temp;
242  }
243  };
244  //=================================================================================================//
245  template<class ObserveMethodType>
247  {
248  if (this->number_of_run_ > 1)
249  {
250  SimTK::Xml::Element mean_element_ = this->mean_variance_xml_engine_in_.getChildElement("Mean_Element");
251  SimTK::Xml::Element variance_element_ = this->mean_variance_xml_engine_in_.getChildElement("Variance_Element");
252  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
253  {
254  this->xmlmemory_io_.readDataFromXmlMemory(this->mean_variance_xml_engine_in_,
255  mean_element_, observation_index, this->meanvalue_, this->quantity_name_);
256  this->xmlmemory_io_.readDataFromXmlMemory(this->mean_variance_xml_engine_in_,
257  variance_element_, observation_index, this->variance_, this->quantity_name_);
258  }
259  }
260  };
261  //=================================================================================================//
262  template<class ObserveMethodType>
264  {
266  if (this->number_of_run_ > 1)
267  {
268  for (int delete_ = 0; delete_ != this->difference_; ++delete_)
269  {
270  meanvalue_.pop_back();
271  variance_.pop_back();
272  }
273  }
274  meanvalue_new_ = meanvalue_;
275  variance_new_ = variance_;
276 
278  for (int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
279  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
280  meanvalue_new_[snapshot_index][observation_index] = (meanvalue_[snapshot_index][observation_index] * (this->number_of_run_ - 1) + this->current_result_[snapshot_index][observation_index]) / this->number_of_run_;
282  calculateNewVariance(this->result_, meanvalue_new_, variance_, variance_new_);
283  };
284  //=================================================================================================//
285  template<class ObserveMethodType>
287  {
288  this->mean_variance_xml_engine_out_.addElementToXmlDoc("Mean_Element");
289  SimTK::Xml::Element mean_element_ = this->mean_variance_xml_engine_out_.getChildElement("Mean_Element");
290  this->xmlmemory_io_.writeDataToXmlMemory(this->mean_variance_xml_engine_out_, mean_element_, this->meanvalue_new_,
291  SMIN(this->snapshot_, this->number_of_snapshot_old_), this->observation_, this->quantity_name_, this->element_tag_);
292  this->mean_variance_xml_engine_out_.addElementToXmlDoc("Variance_Element");
293  SimTK::Xml::Element variance_element_ = this->mean_variance_xml_engine_out_.getChildElement("Variance_Element");
294  this->xmlmemory_io_.writeDataToXmlMemory(this->mean_variance_xml_engine_out_, variance_element_, this->variance_new_,
295  SMIN(this->snapshot_, this->number_of_snapshot_old_), this->observation_, this->quantity_name_, this->element_tag_);
296  this->mean_variance_xml_engine_out_.writeToXmlFile(this->mean_variance_filefullpath_);
297  };
298  //=================================================================================================//
299  template<class ObserveMethodType>
301  {
302  int count_not_converged_m = 0;
303  int count_not_converged_v = 0;
304  count_not_converged_m = compareParameter("meanvalue", meanvalue_, meanvalue_new_, this->threshold_mean_);
305  count_not_converged_v = compareParameter("variance", variance_, variance_new_, this->threshold_variance_);
306  if (count_not_converged_m == 0)
307  {
308  std::cout << "The meanvalue of " << this->quantity_name_ << " are converged now." << endl;
309  if (count_not_converged_v == 0)
310  {
311  if (this->label_for_repeat_ == 4)
312  {
313  this->converged = "true";
314  this->label_for_repeat_++;
315  std::cout << "The meanvalue and variance of " << this->quantity_name_ << " are converged enough times, and run will stop now." << endl;
316  return true;
317  }
318  else
319  {
320  this->converged = "false";
321  this->label_for_repeat_++;
322  std::cout << "The variance of " << this->quantity_name_ << " are also converged, and this is the " << this->label_for_repeat_
323  << " times. They should be converged more times to be stable." << endl;
324  return false;
325  }
326  }
327  else if (count_not_converged_v != 0)
328  {
329  this->converged = "false";
330  this->label_for_repeat_ = 0;
331  std::cout << "The variance of " << this->quantity_name_ << " are not converged " << count_not_converged_v << " times." << endl;
332  return false;
333  };
334  }
335  else if (count_not_converged_m != 0)
336  {
337  this->converged = "false";
338  this->label_for_repeat_ = 0;
339  std::cout << "The meanvalue of " << this->quantity_name_ << " are not converged " << count_not_converged_m << " times." << endl;
340  return false;
341  }
342  };
343  //=================================================================================================//
344  template<class ObserveMethodType>
346  {
347  /* compare the current result to the converged mean value and variance. */
348  int test_wrong = 0;
349  if (this->snapshot_ < this->number_of_snapshot_old_)
350  test_wrong = testNewResult(this->difference_, this->current_result_, meanvalue_, variance_);
351  else
352  {
354  for (int delete_ = 0; delete_ != this->difference_; ++delete_)
355  {
356  meanvalue_.pop_back();
357  variance_.pop_back();
358  }
359  test_wrong = testNewResult(0, this->current_result_, meanvalue_, variance_);
360  }
361  /* draw the conclusion. */
362  if (test_wrong == 0)
363  std::cout << "The result of " << this->quantity_name_ << " are correct based on the ensemble averaged regression test!" << endl;
364  else
365  {
366  std::cout << "There are " << test_wrong << " snapshots are not within the expected range." << endl;
367  std::cout << "Please try again. If it still post this conclusion, the result is not correct!" << endl;
368  exit(1);
369  }
370  };
371  //=================================================================================================//
372 }
void readMeanVarianceFromXml()
Definition: ensemble_averaged_method.hpp:246
Classes for the comparison between validated and tested results with ensemble-averaged meanvalue and ...
int testNewResult(int diff, DoubleVec< Real > &current_result, DoubleVec< Real > &meanvalue, DoubleVec< Real > &variance)
Definition: ensemble_averaged_method.hpp:114
void writeMeanVarianceToXml()
Definition: ensemble_averaged_method.hpp:286
bool compareMeanVariance()
Definition: ensemble_averaged_method.hpp:300
int compareParameter(string par_name, DoubleVec< Real > &parameter, DoubleVec< Real > &parameter_new, Real &threshold)
Definition: ensemble_averaged_method.hpp:53
void resultTest()
Definition: ensemble_averaged_method.hpp:345
void updateMeanVariance()
Definition: ensemble_averaged_method.hpp:263
void calculateNewVariance(TripleVec< Real > &result, DoubleVec< Real > &meanvalue_new, DoubleVec< Real > &variance, DoubleVec< Real > &variance_new)
Definition: ensemble_averaged_method.hpp:15
void settingupAndCorrection()
Definition: ensemble_averaged_method.hpp:187
Definition: solid_body_supplementary.cpp:9