SPHinXsys  alpha version
time_averaged_method.h
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------*
2 * SPHinXsys *
3 * --------------------------------------------------------------------------*
4 * SPHinXsys (pronunciation: s'finksis) is an acronym from Smoothed Particle *
5 * Hydrodynamics for industrial compleX systems. It provides C++ APIs for *
6 * physical accurate simulation and aims to model coupled industrial dynamic *
7 * systems including fluid, solid, multi-body dynamics and beyond with SPH *
8 * (smoothed particle hydrodynamics), a meshless computational method using *
9 * particle discretization. *
10 * *
11 * SPHinXsys is partially funded by German Research Foundation *
12 * (Deutsche Forschungsgemeinschaft) DFG HU1527/6-1, HU1527/10-1 *
13 * and HU1527/12-1. *
14 * *
15 * Portions copyright (c) 2017-2020 Technical University of Munich and *
16 * the authors' affiliations. *
17 * *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may *
19 * not use this file except in compliance with the License. You may obtain a *
20 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
21 * *
22 * --------------------------------------------------------------------------*/
30 #pragma once
31 #include "regression_test_base.hpp"
32 
33 namespace SPH
34 {
39  template<class ObserveMethodType>
40  class RegressionTestTimeAveraged : public RegressionTestBase<ObserveMethodType>
41  {
42  /* identify the variable type from the parent class. */
43  using VariableType = decltype(ObserveMethodType::type_indicator_);
44 
45  protected:
46  int snapshot_for_converged_ ; /* the index of the steady converged starting point. */
47  std::string mean_variance_filefullpath_; /* the file path for mean and variance. (.xml) */
48  std::string filefullpath_filter_output_; /* the file path for filtered output. */
49  XmlEngine mean_variance_xml_engine_in_; /* xml engine for mean and variance input. */
50  XmlEngine mean_variance_xml_engine_out_; /* xml engine for mean and variance output. */
51 
52  VariableType threshold_mean_, threshold_variance_; /* the container of threshold value for mean and variance. */
53  StdVec<VariableType> meanvalue_, meanvalue_new_; /* the container of (new) meanvalue. */
54  StdVec<VariableType> variance_, variance_new_; /* the container of (new) variance. */
55  StdVec<VariableType> local_meanvalue_; /* the container of meanvalue for current result. */
56 
58  void filterLocalResult(DoubleVec<Real> &current_result);
59  void filterLocalResult(DoubleVec<Vecd> &current_result);
60  void filterLocalResult(DoubleVec<Matd> &current_result);
61 
63  void searchSteadyStart(DoubleVec<Real> &current_result);
64  void searchSteadyStart(DoubleVec<Vecd> &current_result);
65  void searchSteadyStart(DoubleVec<Matd> &current_result);
66 
68  void calculateNewVariance(DoubleVec<Real> &current_result, StdVec<Real> &local_meanvalue, StdVec<Real> &variance, StdVec<Real> &variance_new);
69  void calculateNewVariance(DoubleVec<Vecd> &current_result, StdVec<Vecd> &local_meanvalue, StdVec<Vecd> &variance, StdVec<Vecd> &variance_new);
70  void calculateNewVariance(DoubleVec<Matd> &current_result, StdVec<Matd> &local_meanvalue, StdVec<Matd> &variance, StdVec<Matd> &variance_new);
71 
73  int compareParameter(string par_name, StdVec<Real> &parameter, StdVec<Real> &parameter_new, Real &threshold);
74  int compareParameter(string par_name, StdVec<Vecd> &parameter, StdVec<Vecd> &parameter_new, Vecd &threshold);
75  int compareParameter(string par_name, StdVec<Matd> &parameter, StdVec<Matd> &parameter_new, Matd &threshold);
76 
78  int testNewResult(DoubleVec<Real> &current_result, StdVec<Real> &meanvalue, StdVec<Real> &local_meanvalue, StdVec<Real> &variance);
79  int testNewResult(DoubleVec<Vecd> &current_result, StdVec<Vecd> &meanvalue, StdVec<Vecd> &local_meanvalue, StdVec<Vecd> &variance);
80  int testNewResult(DoubleVec<Matd> &current_result, StdVec<Matd> &meanvalue, StdVec<Matd> &local_meanvalue, StdVec<Matd> &variance);
81 
82  public:
83  template<typename... ConstructorArgs>
84  explicit RegressionTestTimeAveraged(ConstructorArgs &&...args) :
85  RegressionTestBase<ObserveMethodType>(std::forward<ConstructorArgs>(args)...),
86  mean_variance_xml_engine_in_("mean_variance_xml_engine_in_", "meanvariance"),
87  mean_variance_xml_engine_out_("mean_variance_xml_engine_out_", "meanvariance")
88  {
89  mean_variance_filefullpath_ = this->input_folder_path_ + "/" + this->body_name_
90  + "_" + this->quantity_name_ + "_time_averaged_mean_variance.xml";
91  };
92  virtual ~RegressionTestTimeAveraged() {};
93 
95  void initializeThreshold(VariableType &threshold_mean, VariableType &threshold_variance);
96  void settingupTheTest();
98  void searchForStartPoint();
99  void filterExtremeValues();
100  void updateMeanVariance();
101  void writeMeanVarianceToXml();
102  bool compareMeanVariance();
103  void resultTest();
105  /* the interface for generating the priori converged result with time-averaged meanvalue and variance. */
106  void generateDataBase(VariableType threshold_mean, VariableType threshold_variance, string filter = "false")
107  {
108  this->writeXmlToXmlFile(); /* currently defined in in_output. */
109  this->readXmlFromXmlFile();/* currently defined in in_output. */
110  initializeThreshold(threshold_mean, threshold_variance);
111  if (this->converged == "false")
112  {
113  settingupTheTest();
114  if (filter == "true")
115  filterExtremeValues(); /* Pay attention to use this filter. */
116  searchForStartPoint(); /* searching starting point with snapshot*observation data structure, and it is dynamic varying. */
117  this->transposeTheIndex(); /* transpose the snapshot and observation, and it is defined in Base. */
120  this->writeResultToXml(this->number_of_run_ - 1); /* the result is output as separately. */
122  compareMeanVariance(); /* To identify whether the current mean and variance are converged or not.*/
123  }
124  else
125  std::cout << "The results have been converged." << endl;
126  };
127 
129  void newResultTest(string filter = "false")
130  {
131  this->writeXmlToXmlFile(); /* currently defined in in_output. */
132  this->readXmlFromXmlFile(); /* currently defined in in_output. */
133  settingupTheTest();
134  if (filter == "true")
138  resultTest();
139  }
140  };
141 }
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
void writeXmlToXmlFile()
Definition: regression_test_base.h:139
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 writeResultToXml()
Definition: regression_test_base.hpp:112
void filterExtremeValues()
Definition: time_averaged_method.hpp:465
void writeMeanVarianceToXml()
Definition: time_averaged_method.hpp:509
void generateDataBase(VariableType threshold_mean, VariableType threshold_variance, string filter="false")
Definition: time_averaged_method.h:106
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
The base of regression test for various method (time-averaged, ensemble-averaged, dynamic time warpin...
Definition: regression_test_base.h:45
void readXmlFromXmlFile()
Definition: regression_test_base.h:145
void newResultTest(string filter="false")
Definition: time_averaged_method.h:129
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
Definition: xml_engine.h:56
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