SPHinXsys  alpha version
time_averaged_method.hpp
1 
6 #pragma once
7 
8 #include "time_averaged_method.h"
9 
10  //=================================================================================================//
11 namespace SPH
12 {
13  //=================================================================================================//
14  template<class ObserveMethodType>
16  {
17  int scale = round(this->snapshot_ / 200);
18  std::cout << "The filter scale is " << scale * 2 << "." << endl;
19  for (int snapshot_index = 0; snapshot_index != this->snapshot_; ++snapshot_index)
20  {
21  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
22  {
23  Real filter_meanvalue = 0;
24  Real filter_variance = 0;
25  for (int index = SMAX(snapshot_index - scale, 0); index != SMIN(snapshot_index + scale, this->snapshot_); ++index)
26  {
27  filter_meanvalue += current_result[index][observation_index];
28  }
29  filter_meanvalue = (filter_meanvalue - current_result[snapshot_index][observation_index]) / (SMIN(snapshot_index + scale, this->snapshot_) - SMAX(snapshot_index - scale, 0));
30  for (int index = SMAX(snapshot_index - scale, 0); index != SMIN(snapshot_index + scale, this->snapshot_); ++index)
31  {
32  filter_variance += std::pow(current_result[index][observation_index] - filter_meanvalue, 2);
33  }
34  Real current_variance = std::pow(current_result[snapshot_index][observation_index] - filter_meanvalue, 2);
35  filter_variance = (filter_variance - current_variance) / (SMIN(snapshot_index + scale, this->snapshot_) - SMAX(snapshot_index - scale, 0));
36  if (current_variance > 4 * filter_variance)
37  {
38  current_result[snapshot_index][observation_index] = filter_meanvalue;
39  std::cout << "The current value of " << this->quantity_name_ << "[" << snapshot_index << "][" << observation_index << "] is " << current_result[snapshot_index][observation_index]
40  << ", but the neighbor averaged value is " << filter_meanvalue << ", and the rate is " << current_variance / filter_variance << endl;
41  }
42  }
43  }
44  };
45  //=================================================================================================//
46  template<class ObserveMethodType>
47  void RegressionTestTimeAveraged<ObserveMethodType>::filterLocalResult(DoubleVec<Vecd> &current_result)
48  {
49  int scale = round(this->snapshot_ / 200);
50  std::cout << "The filter scale is " << scale * 2 << "." << endl;
51  for (int snapshot_index = 0; snapshot_index != this->snapshot_; ++snapshot_index)
52  {
53  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
54  {
55  for (int dimension_index = 0; dimension_index != current_result[0][0].size(); ++dimension_index)
56  {
57  Real filter_meanvalue = 0;
58  Real filter_variance = 0;
59  for (int index = SMAX(snapshot_index - scale, 0); index != SMIN(snapshot_index + scale, this->snapshot_); ++index)
60  {
61  filter_meanvalue += current_result[index][observation_index][dimension_index];
62  }
63  filter_meanvalue = (filter_meanvalue - current_result[snapshot_index][observation_index][dimension_index]) / (SMIN(snapshot_index + scale, this->snapshot_) - SMAX(snapshot_index - scale, 0));
64  for (int index = SMAX(snapshot_index - scale, 0); index != SMIN(snapshot_index + scale, this->snapshot_); ++index)
65  {
66  filter_variance += std::pow(current_result[index][observation_index][dimension_index] - filter_meanvalue, 2);
67  }
68  Real current_variance = std::pow(current_result[snapshot_index][observation_index][dimension_index] - filter_meanvalue, 2);
69  filter_variance = (filter_variance - current_variance) / (SMIN(snapshot_index + scale, this->snapshot_) - SMAX(snapshot_index - scale, 0));
70  if (current_variance > 4 * filter_variance)
71  {
72  current_result[snapshot_index][observation_index][dimension_index] = filter_meanvalue;
73  std::cout << "The current value of " << this->quantity_name_ << "[" << snapshot_index << "][" << observation_index << "][" << dimension_index << "] is " << current_result[snapshot_index][observation_index][dimension_index]
74  << ", but the neighbor averaged value is " << filter_meanvalue << ", and the rate is " << current_variance / filter_variance << endl;
75  }
76  }
77  }
78  }
79  };
80  //=================================================================================================//
81  template<class ObserveMethodType>
82  void RegressionTestTimeAveraged<ObserveMethodType>::filterLocalResult(DoubleVec<Matd> &current_result)
83  {
84  int scale = round(this->snapshot_ / 200);
85  std::cout << "The filter scale is " << scale * 2 << "." << endl;
86  for (int snapshot_index = 0; snapshot_index != this->snapshot_; ++snapshot_index)
87  {
88  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
89  {
90  for (int dimension_index_i = 0; dimension_index_i != current_result[0][0].size(); ++dimension_index_i)
91  {
92  for (int dimension_index_j = 0; dimension_index_j != current_result[0][0].size(); ++dimension_index_j)
93  {
94  Real filter_meanvalue = 0;
95  Real filter_variance = 0;
96  for (int index = SMAX(snapshot_index - scale, 0); index != SMIN(snapshot_index + scale, this->snapshot_); ++index)
97  {
98  filter_meanvalue += current_result[index][observation_index][dimension_index_i][dimension_index_j];
99  }
100  filter_meanvalue = (filter_meanvalue - current_result[snapshot_index][observation_index][dimension_index_i][dimension_index_j]) / (SMIN(snapshot_index + scale, this->snapshot_) - SMAX(snapshot_index - scale, 0));
101  for (int index = SMAX(snapshot_index - scale, 0); index != SMIN(snapshot_index + scale, this->snapshot_); ++index)
102  {
103  filter_variance += std::pow(current_result[index][observation_index][dimension_index_i][dimension_index_j] - filter_meanvalue, 2);
104  }
105  Real current_variance = std::pow(current_result[snapshot_index][observation_index][dimension_index_i][dimension_index_j] - filter_meanvalue, 2);
106  filter_variance = (filter_variance - current_variance) / (SMIN(snapshot_index + scale, this->snapshot_) - SMAX(snapshot_index - scale, 0));
107  if (current_variance > 4 * filter_variance)
108  {
109  current_result[snapshot_index][observation_index][dimension_index_i][dimension_index_j] = filter_meanvalue;
110  std::cout << "The current value of " << this->quantity_name_ << "[" << snapshot_index << "][" << observation_index << "][" << dimension_index_i << "][" << dimension_index_j << "] is " << current_result[snapshot_index][observation_index][dimension_index_i][dimension_index_j]
111  << ", but the neighbor averaged value is " << filter_meanvalue << ", and the rate is " << current_variance / filter_variance << endl;
112  }
113  }
114  }
115  }
116  }
117  };
118  //=================================================================================================//
119  template<class ObserveMethodType>
121  {
122  /* the search is only for one value. */
123  int scale = round(this->snapshot_ / 20);
124  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
125  for (int snapshot_index = this->snapshot_ - 1; snapshot_index != 3 * scale; --snapshot_index)
126  {
127  Real value_one = 0, value_two = 0;
128  for (int index = snapshot_index; index != snapshot_index - scale; --index)
129  {
130  value_one += current_result[index][observation_index] / scale;
131  value_two += current_result[index - 2 * scale][observation_index] / scale;
132  }
133 
134  if (ABS(value_one - value_two) / ABS((value_one + value_two) / 2) > 0.1)
135  {
136  snapshot_for_converged_ = SMAX(snapshot_for_converged_, snapshot_index - scale);
137  break;
138  }
139  }
140  std::cout << "The scale is " << scale << "." << endl;
141  };
142  //=================================================================================================//
143  template<class ObserveMethodType>
145  {
146  /* the search is for each value within parameters. */
147  int scale = round(this->snapshot_ / 20);
148  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
149  for (int snapshot_index = this->snapshot_ - 1; snapshot_index != 3 * scale; --snapshot_index)
150  {
151  Real value_one = 0, value_two = 0;
152  for (int index = snapshot_index; index != snapshot_index - scale; --index)
153  {
154  value_one += current_result[index][observation_index][0] / scale;
155  value_two += current_result[index - 2 * scale][observation_index][0] / scale;
156  }
157 
158  if (ABS(value_one - value_two) / ABS((value_one + value_two) / 2) > 0.1)
159  {
160  snapshot_for_converged_ = SMAX(snapshot_for_converged_, snapshot_index - scale);
161  break;
162  }
163  }
164  std::cout << "The scale is " << scale << "." << endl;
165  };
166  //=================================================================================================//
167  template<class ObserveMethodType>
169  {
170  int scale = round(this->snapshot_ / 20);
171  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
172  for (int snapshot_index = this->snapshot_ - 1; snapshot_index != 3 * scale; --snapshot_index)
173  {
174  Real value_one = 0, value_two = 0;
175  for (int index = snapshot_index; index != snapshot_index - scale; --index)
176  {
177  value_one += current_result[index][observation_index][0][0];
178  value_two += current_result[index - 2 * scale][observation_index][0][0];
179  }
180 
181  if (ABS(value_one - value_two) / ABS((value_one + value_two) / 2) > 0.1)
182  {
183  snapshot_for_converged_ = SMAX(snapshot_for_converged_, snapshot_index - scale);
184  break;
185  }
186  }
187  std::cout << "The scale is " << scale << "." << endl;
188  };
189  //=================================================================================================//
190  template<class ObserveMethodType>
192  StdVec<Real> &local_meanvalue, StdVec<Real> &variance, StdVec<Real> &variance_new)
193  {
194  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
195  {
196  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
197  variance_new[observation_index] += std::pow((current_result[observation_index][snapshot_index] - local_meanvalue[observation_index]), 2);
198  variance_new[observation_index] = SMAX((variance_new[observation_index] / (this->snapshot_ - snapshot_for_converged_)), variance[observation_index], std::pow(local_meanvalue[observation_index] * 1.0e-2, 2));
199  }
200  };
201  //=================================================================================================//
202  template<class ObserveMethodType>
203  void RegressionTestTimeAveraged<ObserveMethodType>::calculateNewVariance(DoubleVec<Vecd> &current_result,
204  StdVec<Vecd> &local_meanvalue, StdVec<Vecd> &variance, StdVec<Vecd> &variance_new)
205  {
206  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
207  for (int dimension_index = 0; dimension_index != current_result[0][0].size(); ++dimension_index)
208  {
209  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
210  variance_new[observation_index][dimension_index] += std::pow((current_result[observation_index][snapshot_index][dimension_index] - local_meanvalue[observation_index][dimension_index]), 2);
211  variance_new[observation_index][dimension_index] = SMAX((variance_new[observation_index][dimension_index] / (this->snapshot_ - snapshot_for_converged_)), variance[observation_index][dimension_index], std::pow(local_meanvalue[observation_index][dimension_index] * 1.0e-2, 2));
212  }
213  };
214  //=================================================================================================//
215  template<class ObserveMethodType>
216  void RegressionTestTimeAveraged<ObserveMethodType>::calculateNewVariance(DoubleVec<Matd> &current_result,
217  StdVec<Matd> &local_meanvalue, StdVec<Matd> &variance, StdVec<Matd> &variance_new)
218  {
219  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
220  for (int dimension_index_i = 0; dimension_index_i != current_result[0][0].size(); ++dimension_index_i)
221  for (int dimension_index_j = 0; dimension_index_j != current_result[0][0].size(); ++dimension_index_j)
222  {
223  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
224  variance_new[observation_index][dimension_index_i][dimension_index_j] += std::pow((current_result[observation_index][snapshot_index][dimension_index_i][dimension_index_j] - local_meanvalue[observation_index][dimension_index_i][dimension_index_j]), 2);
225  variance_new[observation_index][dimension_index_i][dimension_index_j] = SMAX((variance_new[observation_index][dimension_index_i][dimension_index_j] / (this->snapshot_ - snapshot_for_converged_)), variance[observation_index][dimension_index_i][dimension_index_j], std::pow(local_meanvalue[observation_index][dimension_index_i][dimension_index_j] * 1.0e-2, 2));
226  }
227  };
228  //=================================================================================================//
229  template<class ObserveMethodType>
231  StdVec<Real> &parameter, StdVec<Real> &parameter_new, Real &threshold)
232  {
233  int count = 0;
234  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
235  {
236  if ((par_name == "meanvalue") && (ABS(parameter[observation_index]) < 0.005) && (ABS(parameter_new[observation_index]) < 0.005))
237  {
238  std::cout << "The old meanvalue is " << parameter[observation_index] << ", and the new meanvalue is " << parameter_new[observation_index]
239  << ". So this variable will be ignored due to its tiny effect." << endl;
240  continue;
241  }
242  Real relative_value_ = ABS((parameter[observation_index] - parameter_new[observation_index]) / (parameter_new[observation_index] + TinyReal));
243  if (relative_value_ > threshold)
244  {
245  std::cout << par_name << ": " << this->quantity_name_ << "[" << observation_index << "]"
246  << " is not converged, and difference is " << relative_value_ << endl;
247  count++;
248  }
249  }
250  return count;
251  };
252  //=================================================================================================//
253  template<class ObserveMethodType>
255  StdVec<Vecd> &parameter, StdVec<Vecd> &parameter_new, Vecd &threshold)
256  {
257  int count = 0;
258  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
259  for (int dimension_index = 0; dimension_index != parameter[0].size(); ++dimension_index)
260  {
261  if ((par_name == "meanvalue") && (ABS(parameter[observation_index][dimension_index]) < 0.001) && (ABS(parameter_new[observation_index][dimension_index]) < 0.001))
262  {
263  std::cout << "The old meanvalue is " << parameter[observation_index][dimension_index] << ", and the new meanvalue is " << parameter_new[observation_index][dimension_index]
264  << ". So this variable will be ignored due to its tiny effect." << endl;
265  continue;
266  }
267  Real relative_value_ = ABS((parameter[observation_index][dimension_index] - parameter_new[observation_index][dimension_index]) / (parameter_new[observation_index][dimension_index] + TinyReal));
268  if (relative_value_ > threshold[dimension_index])
269  {
270  std::cout << par_name << ": " << this->quantity_name_ << "[" << observation_index << "][" << dimension_index << "]"
271  << " is not converged, and difference is " << relative_value_ << endl;
272  count++;
273  }
274  }
275  return count;
276  };
277  //=================================================================================================//
278  template<class ObserveMethodType>
280  StdVec<Matd> &parameter, StdVec<Matd> &parameter_new, Matd &threshold)
281  {
282  int count = 0;
283  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
284  for (int dimension_index_i = 0; dimension_index_i != parameter[0].size(); ++dimension_index_i)
285  for (int dimension_index_j = 0; dimension_index_j != parameter[0].size(); ++dimension_index_i)
286  {
287  if ((par_name == "meanvalue") && (ABS(parameter[observation_index][dimension_index_i][dimension_index_j]) < 0.001) && (ABS(parameter_new[observation_index][dimension_index_i][dimension_index_j]) < 0.001))
288  {
289  std::cout << "The old meanvalue is " << parameter[observation_index][dimension_index_i][dimension_index_j] << ", and the new meanvalue is " << parameter_new[observation_index][dimension_index_i][dimension_index_j]
290  << ". So this variable will be ignored due to its tiny effect." << endl;
291  continue;
292  }
293  Real relative_value_ = ABS((parameter[observation_index][dimension_index_i][dimension_index_j] - parameter_new[observation_index][dimension_index_i][dimension_index_j]) / (parameter_new[observation_index][dimension_index_i][dimension_index_j] + TinyReal));
294  if (relative_value_ > threshold[dimension_index_i][dimension_index_j])
295  {
296  std::cout << par_name << ": " << this->quantity_name_ << "[" << observation_index << "][" << dimension_index_i << "][" << dimension_index_j << "]"
297  << " is not converged, and difference is " << relative_value_ << endl;
298  count++;
299  }
300  }
301  return count;
302  };
303  //=================================================================================================//
304  template<class ObserveMethodType>
306  StdVec<Real> &meanvalue, StdVec<Real> &local_meanvalue, StdVec<Real> &variance)
307  {
308  int count = 0;
309  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
310  {
311  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
312  {
313  variance_new_[observation_index] += std::pow((current_result[snapshot_index][observation_index] - local_meanvalue[observation_index]), 2);
314  }
315  variance_new_[observation_index] = variance_new_[observation_index] / (this->snapshot_ - snapshot_for_converged_);
316  if ((ABS(meanvalue[observation_index]) < 0.005) && (ABS(local_meanvalue[observation_index]) < 0.005))
317  {
318  std::cout << "The old meanvalue is " << meanvalue[observation_index] << ", and the current meanvalue is " << local_meanvalue[observation_index]
319  << ". So this variable will not be tested due to its tiny effect." << endl;
320  continue;
321  }
322  Real relative_value_ = ABS((meanvalue[observation_index] - local_meanvalue[observation_index]) / meanvalue[observation_index]);
323  if (relative_value_ > 0.1 || variance_new_[observation_index] > (1.01 * variance[observation_index]))
324  {
325  std::cout << this->quantity_name_ << "[" << observation_index << "] is beyond the exception !" << endl;
326  std::cout << "The meanvalue is " << meanvalue[observation_index] << ", and the current meanvalue is " << local_meanvalue[observation_index] << endl;
327  std::cout << "The variance is " << variance[observation_index] << ", and the current variance is " << variance_new_[observation_index] << endl;
328  count++;
329  }
330  }
331  return count;
332  };
333  //=================================================================================================//
334  template<class ObserveMethodType>
335  int RegressionTestTimeAveraged<ObserveMethodType>::testNewResult(DoubleVec<Vecd> &current_result,
336  StdVec<Vecd> &meanvalue, StdVec<Vecd> &local_meanvalue, StdVec<Vecd> &variance)
337  {
338  int count = 0;
339  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
340  {
341  for (int dimension_index_i = 0; dimension_index_i != meanvalue_[0].size(); ++dimension_index_i)
342  {
343  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
344  {
345  variance_new_[observation_index][dimension_index_i] += std::pow((current_result[snapshot_index][observation_index][dimension_index_i] - local_meanvalue[observation_index][dimension_index_i]), 2);
346  }
347  variance_new_[observation_index][dimension_index_i] = variance_new_[observation_index][dimension_index_i] / (this->snapshot_ - snapshot_for_converged_);
348  if ((ABS(meanvalue[observation_index][dimension_index_i]) < 0.005) && (ABS(local_meanvalue[observation_index][dimension_index_i]) < 0.005))
349  {
350  std::cout << "The old meanvalue is " << meanvalue[observation_index][dimension_index_i] << ", and the current meanvalue is " << local_meanvalue[observation_index][dimension_index_i]
351  << ". So this variable will not be tested due to its tiny effect." << endl;
352  continue;
353  }
354  Real relative_value_ = ABS((meanvalue[observation_index][dimension_index_i] - local_meanvalue[observation_index][dimension_index_i]) / meanvalue[observation_index][dimension_index_i]);
355  if (relative_value_ > 0.1 || (variance_new_[observation_index][dimension_index_i] > 1.01 * variance[observation_index][dimension_index_i]))
356  {
357  std::cout << this->quantity_name_ << "[" << observation_index << "][" << dimension_index_i << "] is beyond the exception !" << endl;
358  std::cout << "The meanvalue is " << meanvalue[observation_index][dimension_index_i] << ", and the current meanvalue is " << local_meanvalue[observation_index][dimension_index_i] << endl;
359  std::cout << "The variance is " << variance[observation_index][dimension_index_i] << ", and the new variance is " << variance_new_[observation_index][dimension_index_i] << endl;
360  count++;
361  }
362  }
363  }
364  return count;
365  };
366  //=================================================================================================//
367  template<class ObserveMethodType>
368  int RegressionTestTimeAveraged<ObserveMethodType>::testNewResult(DoubleVec<Matd> &current_result,
369  StdVec<Matd> &meanvalue, StdVec<Matd> &local_meanvalue, StdVec<Matd> &variance)
370  {
371  int count = 0;
372  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
373  {
374  for (int dimension_index_i = 0; dimension_index_i != meanvalue[0].size(); ++dimension_index_i)
375  {
376  for (int dimension_index_j = 0; dimension_index_j != meanvalue[0].size(); ++dimension_index_j)
377  {
378  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
379  {
380  variance_new_[observation_index][dimension_index_i][dimension_index_j] += std::pow((current_result[snapshot_index][observation_index][dimension_index_i][dimension_index_j] - local_meanvalue[observation_index][dimension_index_i][dimension_index_j]), 2);
381  }
382  variance_new_[observation_index][dimension_index_i][dimension_index_j] = variance_new_[observation_index][dimension_index_i][dimension_index_j] / (this->snapshot_ - snapshot_for_converged_);
383  if ((ABS(meanvalue[observation_index][dimension_index_i][dimension_index_j]) < 0.005) && (ABS(local_meanvalue[observation_index][dimension_index_i][dimension_index_j]) < 0.005))
384  {
385  std::cout << "The old meanvalue is " << meanvalue[observation_index][dimension_index_i][dimension_index_j] << ", and the new meanvalue is " << local_meanvalue[observation_index][dimension_index_i][dimension_index_j]
386  << ". So this variable will not be tested due to its tiny effect. " << endl;
387  continue;
388  }
389  Real relative_value_ = ABS((meanvalue_[observation_index][dimension_index_i][dimension_index_j] - local_meanvalue[observation_index][dimension_index_i][dimension_index_j]) / meanvalue[observation_index][dimension_index_i][dimension_index_j]);
390  if (relative_value_ > 0.1 || variance_new_[observation_index][dimension_index_i][dimension_index_j] > 1.01 * variance[observation_index][dimension_index_i][dimension_index_j])
391  {
392  std::cout << this->quantity_name_ << "[" << observation_index << "][" << dimension_index_i << "][" << dimension_index_j << "] is beyond the exception !" << endl;
393  std::cout << "The meanvalue is " << meanvalue[observation_index][dimension_index_i][dimension_index_j] << ", and the new meanvalue is " << local_meanvalue[observation_index][dimension_index_i][dimension_index_j] << endl;
394  std::cout << "The variance is " << variance[observation_index][dimension_index_i][dimension_index_j] << ", and the new variance is " << variance_new_[observation_index][dimension_index_i][dimension_index_j] << endl;
395  count++;
396  }
397  }
398  }
399  }
400  return count;
401  };
402  //=================================================================================================//
403  template<class ObserveMethodType>
404  void RegressionTestTimeAveraged<ObserveMethodType>::initializeThreshold(VariableType &threshold_mean, VariableType &threshold_variance)
405  {
406  threshold_mean_ = threshold_mean;
407  threshold_variance_ = threshold_variance;
408  };
409  //=================================================================================================//
410  template<class ObserveMethodType>
412  {
413  this->snapshot_ = this->current_result_.size();
414  this->observation_ = this->current_result_[0].size();
415  StdVec<VariableType> temp(this->observation_);
416  meanvalue_ = temp;
417  variance_ = temp;
418  local_meanvalue_ = temp;
419  meanvalue_new_ = meanvalue_;
420  variance_new_ = variance_;
421 
422  if ((this->number_of_run_ > 1) && (!fs::exists(mean_variance_filefullpath_)))
423  {
424  std::cout << "\n Error: the input file:" << mean_variance_filefullpath_ << " is not exists" << std::endl;
425  std::cout << __FILE__ << ':' << __LINE__ << std::endl;
426  exit(1);
427  }
428  };
429  //=================================================================================================//
430  template<class ObserveMethodType>
432  {
433  if (this->number_of_run_ > 1)
434  {
435  mean_variance_xml_engine_in_.loadXmlFile(mean_variance_filefullpath_);
436  SimTK::Xml::Element meanvalue_element_ = mean_variance_xml_engine_in_.getChildElement("MeanValue_Element");
437  SimTK::Xml::element_iterator ele_ite_mean_ = meanvalue_element_.element_begin();
438  for (; ele_ite_mean_ != meanvalue_element_.element_end(); ++ele_ite_mean_)
439  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
440  {
441  std::string attribute_name_ = this->quantity_name_ + "_" + std::to_string(observation_index);
442  mean_variance_xml_engine_in_.getRequiredAttributeValue(ele_ite_mean_, attribute_name_, meanvalue_[observation_index]);
443  }
444 
445  SimTK::Xml::Element variance_element_ = mean_variance_xml_engine_in_.getChildElement("Variance_Element");
446  SimTK::Xml::element_iterator ele_ite_variance_ = variance_element_.element_begin();
447  for (; ele_ite_variance_ != variance_element_.element_end(); ++ele_ite_variance_)
448  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
449  {
450  std::string attribute_name_ = this->quantity_name_ + "_" + std::to_string(observation_index);
451  mean_variance_xml_engine_in_.getRequiredAttributeValue(ele_ite_variance_, attribute_name_, variance_[observation_index]);
452  }
453  }
454  };
455  //=================================================================================================//
456  template<class ObserveMethodType>
458  {
459  snapshot_for_converged_ = 0;
460  searchSteadyStart(this->current_result_);
461  std::cout << "The snapshot for converged is " << snapshot_for_converged_ << endl;
462  };
463  //=================================================================================================//
464  template<class ObserveMethodType>
466  {
467  filterLocalResult(this->current_result_);
468  filefullpath_filter_output_ = this->input_folder_path_ + "/" + this->body_name_
469  + "_" + this->quantity_name_ + ".dat";
470  std::ofstream out_file(filefullpath_filter_output_.c_str(), std::ios::app);
471  out_file << "run_time" << " ";
472  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
473  {
474  std::string quantity_name_i = this->quantity_name_ + "[" + std::to_string(observation_index) + "]";
475  this->plt_engine_.writeAQuantityHeader(out_file, this->current_result_[0][0], quantity_name_i);
476  }
477  out_file << "\n";
478  out_file.close();
479 
480  for (int snapshot_index = 0; snapshot_index != this->snapshot_; ++snapshot_index)
481  {
482  std::ofstream out_file(filefullpath_filter_output_.c_str(), std::ios::app);
483  out_file << this->element_tag_[snapshot_index] << " ";
484  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
485  {
486  this->plt_engine_.writeAQuantity(out_file, this->current_result_[snapshot_index][observation_index]);
487  }
488  out_file << "\n";
489  out_file.close();
490  }
491  };
492  //=================================================================================================//
493  template<class ObserveMethodType>
495  {
496  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
497  {
498  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
499  {
500  local_meanvalue_[observation_index] += this->current_result_[snapshot_index][observation_index];
501  }
502  local_meanvalue_[observation_index] = local_meanvalue_[observation_index] / (this->snapshot_ - snapshot_for_converged_);
503  meanvalue_new_[observation_index] = (local_meanvalue_[observation_index] + meanvalue_[observation_index] * (this->number_of_run_ - 1)) / this->number_of_run_;
504  }
505  calculateNewVariance(this->current_result_trans_, local_meanvalue_, variance_, variance_new_);
506  };
507  //=================================================================================================//
508  template<class ObserveMethodType>
510  {
511  mean_variance_xml_engine_out_.addElementToXmlDoc("MeanValue_Element");
512  SimTK::Xml::Element meanvalue_element_ = mean_variance_xml_engine_out_.getChildElement("MeanValue_Element");
513  mean_variance_xml_engine_out_.addChildToElement(meanvalue_element_, "Snapshot_MeanValue");
514  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
515  {
516  SimTK::Xml::element_iterator ele_ite_mean = meanvalue_element_.element_begin();
517  std::string attribute_name_ = this->quantity_name_ + "_" + std::to_string(observation_index);
518  mean_variance_xml_engine_out_.setAttributeToElement(ele_ite_mean, attribute_name_, meanvalue_new_[observation_index]);
519  }
520  mean_variance_xml_engine_out_.addElementToXmlDoc("Variance_Element");
521  SimTK::Xml::Element variance_element_ = mean_variance_xml_engine_out_.getChildElement("Variance_Element");
522  mean_variance_xml_engine_out_.addChildToElement(variance_element_, "Snapshot_Variance");
523  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
524  {
525  SimTK::Xml::element_iterator ele_ite_variance = variance_element_.element_begin();
526  std::string attribute_name_ = this->quantity_name_ + "_" + std::to_string(observation_index);
527  mean_variance_xml_engine_out_.setAttributeToElement(ele_ite_variance, attribute_name_, variance_new_[observation_index]);
528  }
529  mean_variance_xml_engine_out_.writeToXmlFile(mean_variance_filefullpath_);
530  };
531  //=================================================================================================//
532  template<class ObserveMethodType>
534  {
535  int count_not_converged_m = 0;
536  int count_not_converged_v = 0;
537  count_not_converged_m = this->compareParameter("meanvalue", this->meanvalue_, this->meanvalue_new_, this->threshold_mean_);
538  count_not_converged_v = this->compareParameter("variance", this->variance_, this->variance_new_, this->threshold_variance_);
539  if (count_not_converged_m == 0)
540  {
541  std::cout << "The meanvalue of " << this->quantity_name_ << " are converged now." << endl;
542  if (count_not_converged_v == 0)
543  {
544  if (this->label_for_repeat_ == 4)
545  {
546  this->converged = "true";
547  this->label_for_repeat_++;
548  std::cout << "The meanvalue and variance of " << this->quantity_name_ << " are converged enough times, and run will stop now." << endl;
549  return true;
550  }
551  else
552  {
553  this->converged = "false";
554  this->label_for_repeat_++;
555  std::cout << "The variance of " << this->quantity_name_ << " are also converged, and this is the " << this->label_for_repeat_
556  << " times. They should be converged more times to be stable." << endl;
557  return false;
558  }
559  }
560  else if (count_not_converged_v != 0)
561  {
562  this->converged = "false";
563  this->label_for_repeat_ = 0;
564  std::cout << "The variance of " << this->quantity_name_ << " are not converged " << count_not_converged_v << " times." << endl;
565  return false;
566  };
567  }
568  else if (count_not_converged_m != 0)
569  {
570  this->converged = "false";
571  this->label_for_repeat_ = 0;
572  std::cout << "The meanvalue of " << this->quantity_name_ << " are not converged " << count_not_converged_m << " times." << endl;
573  return false;
574  }
575  };
576  //=================================================================================================//
577  template<class ObserveMethodType>
579  {
580  int test_wrong = 0;
581 
582  for (int observation_index = 0; observation_index != this->observation_; ++observation_index)
583  {
584  for (int snapshot_index = snapshot_for_converged_; snapshot_index != this->snapshot_; ++snapshot_index)
585  local_meanvalue_[observation_index] += this->current_result_[snapshot_index][observation_index];
586  local_meanvalue_[observation_index] = local_meanvalue_[observation_index] / (this->snapshot_-snapshot_for_converged_);
587  }
588 
589  test_wrong = testNewResult(this->current_result_, meanvalue_, local_meanvalue_, variance_);
590  if (test_wrong == 0)
591  std::cout << "The result of " << this->quantity_name_ << " is correct based on the time-averaged regression test!" << endl;
592  else
593  {
594  std::cout << "There are " << test_wrong << " particles are not within the expected range." << endl;
595  std::cout << "Please try again. If it still post this conclusion, the result is not correct!" << endl;
596  exit(1);
597  }
598  };
599  //=================================================================================================//
600 };
int testNewResult(DoubleVec< Real > &current_result, StdVec< Real > &meanvalue, StdVec< Real > &local_meanvalue, StdVec< Real > &variance)
Definition: time_averaged_method.hpp:305
void updateMeanVariance()
Definition: time_averaged_method.hpp:494
int compareParameter(string par_name, StdVec< Real > &parameter, StdVec< Real > &parameter_new, Real &threshold)
Definition: time_averaged_method.hpp:230
void filterLocalResult(DoubleVec< Real > &current_result)
Definition: time_averaged_method.hpp:15
void filterExtremeValues()
Definition: time_averaged_method.hpp:465
void writeMeanVarianceToXml()
Definition: time_averaged_method.hpp:509
void initializeThreshold(VariableType &threshold_mean, VariableType &threshold_variance)
Definition: time_averaged_method.hpp:404
void resultTest()
Definition: time_averaged_method.hpp:578
void searchForStartPoint()
Definition: time_averaged_method.hpp:457
bool compareMeanVariance()
Definition: time_averaged_method.hpp:533
void calculateNewVariance(DoubleVec< Real > &current_result, StdVec< Real > &local_meanvalue, StdVec< Real > &variance, StdVec< Real > &variance_new)
Definition: time_averaged_method.hpp:191
void readMeanVarianceFromXml()
Definition: time_averaged_method.hpp:431
Classes for the comparison between validated and tested results with time-averaged meanvalue and vari...
void searchSteadyStart(DoubleVec< Real > &current_result)
Definition: time_averaged_method.hpp:120
Definition: solid_body_supplementary.cpp:9
The regression test is based on the time-averaged meanvalue and variance.
Definition: time_averaged_method.h:40