14 template<
class ObserveMethodType>
16 DoubleVec<Real> &meanvalue_new, DoubleVec<Real> &variance, DoubleVec<Real> &variance_new)
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));
25 template<
class ObserveMethodType>
27 DoubleVec<Vecd> &meanvalue_new, DoubleVec<Vecd> &variance, DoubleVec<Vecd> &variance_new)
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));
38 template<
class ObserveMethodType>
40 DoubleVec<Matd> &meanvalue_new, DoubleVec<Matd> &variance, DoubleVec<Matd> &variance_new)
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));
52 template<
class ObserveMethodType>
54 DoubleVec<Real> ¶meter, DoubleVec<Real> ¶meter_new, Real &threshold)
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)
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)
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;
71 template<
class ObserveMethodType>
73 DoubleVec<Vecd> ¶meter, DoubleVec<Vecd> ¶meter_new, Vecd &threshold)
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)
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])
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;
91 template<
class ObserveMethodType>
93 DoubleVec<Matd> ¶meter, DoubleVec<Matd> ¶meter_new, Matd &threshold)
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)
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])
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;
113 template<
class ObserveMethodType>
115 DoubleVec<Real> &meanvalue, DoubleVec<Real> &variance)
118 for (
int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
120 for (
int observation_index = 0; observation_index != this->observation_; ++observation_index)
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)
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;
134 template<
class ObserveMethodType>
136 DoubleVec<Vecd> &meanvalue, DoubleVec<Vecd> &variance)
139 for (
int snapshot_index = 0; snapshot_index != SMIN(this->snapshot_, this->number_of_snapshot_old_); ++snapshot_index)
141 for (
int observation_index = 0; observation_index != this->observation_; ++observation_index)
143 for (
int dimension_index = 0; dimension_index != meanvalue[0][0].size(); ++dimension_index)
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)
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;
158 template<
class ObserveMethodType>
160 DoubleVec<Matd> &meanvalue, DoubleVec<Matd> &variance)
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)
166 for (
int observation_index = 0; observation_index != this->observation_; ++observation_index)
168 for (
int dimension_index_i = 0; dimension_index_i != meanvalue[0][0].size(); ++dimension_index_i)
170 for (
int dimension_index_j = 0; dimension_index_j != meanvalue[0][0].size(); ++dimension_index_j)
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)
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;
186 template<
class ObserveMethodType>
189 this->snapshot_ = this->current_result_.size();
190 this->observation_ = this->current_result_[0].size();
192 if (this->number_of_run_ > 1)
194 if (this->converged ==
"false" )
196 if (!fs::exists(this->result_filefullpath_))
198 std::cout <<
"\n Error: the input file:" << this->result_filefullpath_ <<
" is not exists" << std::endl;
199 std::cout << __FILE__ <<
':' << __LINE__ << std::endl;
203 this->result_xml_engine_in_.loadXmlFile(this->result_filefullpath_);
206 if (!fs::exists(this->mean_variance_filefullpath_))
208 std::cout <<
"\n Error: the input file:" << this->mean_variance_filefullpath_ <<
" is not exists" << std::endl;
209 std::cout << __FILE__ <<
':' << __LINE__ << std::endl;
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());
218 DoubleVec<VariableType> temp(SMAX(this->snapshot_, this->number_of_snapshot_old_), StdVec<VariableType>(this->observation_));
223 if (this->number_of_snapshot_old_ < this->snapshot_)
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();
229 else if (this->number_of_snapshot_old_ > this->snapshot_)
230 this->difference_ = this->number_of_snapshot_old_ - this->snapshot_;
232 this->difference_ = 0;
235 else if (this->number_of_run_ == 1)
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_);
245 template<
class ObserveMethodType>
248 if (this->number_of_run_ > 1)
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)
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_);
262 template<
class ObserveMethodType>
266 if (this->number_of_run_ > 1)
268 for (
int delete_ = 0; delete_ != this->difference_; ++delete_)
270 meanvalue_.pop_back();
271 variance_.pop_back();
274 meanvalue_new_ = meanvalue_;
275 variance_new_ = variance_;
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_);
285 template<
class ObserveMethodType>
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_);
299 template<
class ObserveMethodType>
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)
308 std::cout <<
"The meanvalue of " << this->quantity_name_ <<
" are converged now." << endl;
309 if (count_not_converged_v == 0)
311 if (this->label_for_repeat_ == 4)
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;
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;
327 else if (count_not_converged_v != 0)
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;
335 else if (count_not_converged_m != 0)
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;
344 template<
class ObserveMethodType>
349 if (this->snapshot_ < this->number_of_snapshot_old_)
350 test_wrong = testNewResult(this->difference_, this->current_result_, meanvalue_, variance_);
354 for (
int delete_ = 0; delete_ != this->difference_; ++delete_)
356 meanvalue_.pop_back();
357 variance_.pop_back();
359 test_wrong = testNewResult(0, this->current_result_, meanvalue_, variance_);
363 std::cout <<
"The result of " << this->quantity_name_ <<
" are correct based on the ensemble averaged regression test!" << endl;
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;
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 > ¤t_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 > ¶meter, DoubleVec< Real > ¶meter_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