SPHinXsys  alpha version
dynamic_time_warping_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>
41  {
42  /*identify the variable type from the parent class. */
43  using VariableType = decltype(ObserveMethodType::type_indicator_);
44 
45  protected:
46  std::string dtw_distance_filefullpath_; /* the path for DTW distance. */
47  XmlEngine dtw_distance_xml_engine_in_; /* xml engine for dtw distance input. */
48  XmlEngine dtw_distance_xml_engine_out_; /* xml engine for dtw distance output. */
49 
50  StdVec<Real> dtw_distance_, dtw_distance_new_; /* the container of DTW distance between each pairs. */
51 
53  Real calculatePNorm(Real variable_a, Real variable_b);
54  Real calculatePNorm(Vecd variable_a, Vecd variable_b);
55  Real calculatePNorm(Matd variable_a, Matd variable_b);
56 
58  StdVec<Real> calculateDTWDistance(DoubleVec<VariableType> dataset_a_, DoubleVec<VariableType> dataset_b_);
59 
60  public:
61  template<typename... ConstructorArgs>
62  explicit RegressionTestDynamicTimeWarping(ConstructorArgs &&...args) :
63  RegressionTestTimeAveraged<ObserveMethodType>(std::forward<ConstructorArgs>(args)...),
64  dtw_distance_xml_engine_in_("dtw_distance_xml_engine_in", "dtw_distance"),
65  dtw_distance_xml_engine_out_("dtw_distance_xml_engine_out", "dtw_distance")
66  {
67  dtw_distance_filefullpath_ = this->input_folder_path_ + "/" + this->body_name_ + "_"
68  + this->quantity_name_ + "_dtwdistance.xml";
69  };
71 
72  void settingupTheTest();
73  void readDTWDistanceFromXml();
74  void updateDTWDistance();
75  void writeDTWDistanceToXml(); /* write the updated DTWDistance to .xml file.*/
76  bool compareDTWDistance(Real threshold_value); /* compare the DTWDistance if converged. */
77  void resultTest();
80  void generateDataBase(Real threshold_value, string filter = "false")
81  {
82  this->writeXmlToXmlFile();
83  this->readXmlFromXmlFile();
84  this->transposeTheIndex();
85  if (this->converged == "false")
86  {
87  settingupTheTest();
88  if (filter == "true")
89  this->filterExtremeValues();
91  /* loop all existed result to get maximum dtw distance. */
92  for (int n = 0; n != (this->number_of_run_ - 1); ++n)
93  {
94  this->readResultFromXml(n);
96  }
97  this->writeResultToXml(this->number_of_run_ - 1);
99  compareDTWDistance(threshold_value);
100  }
101  else
102  std::cout << "The results have been converged." << endl;
103  };
104 
106  void newResultTest(string filter = "false")
107  {
108  this->writeXmlToXmlFile();
109  this->readXmlFromXmlFile();
110  this->transposeTheIndex();
111  settingupTheTest();
112  if (filter == "true")
113  this->filterExtremeValues();
115  for (int n = 0; n != this->number_of_run_; ++n)
116  {
117  this->result_filefullpath_ = this->input_folder_path_ + "/" + this->body_name_
118  + "_" + this->quantity_name_ + "_Run_" + std::to_string(n) + "_result.xml";
119  if (!fs::exists(this->result_filefullpath_))
120  {
121  std::cout << "This result has not been preserved and will not be compared." << endl;
122  continue;
123  }
124  this->readResultFromXml(n);
125  resultTest();
126  }
127  std::cout << "The result of " << this->quantity_name_
128  << " is correct based on the dynamic time warping regression test!" << endl;
129  };
130  };
131 }
void readDTWDistanceFromXml()
Definition: dynamic_time_warping_method.hpp:89
void writeXmlToXmlFile()
Definition: regression_test_base.h:139
void generateDataBase(Real threshold_value, string filter="false")
Definition: dynamic_time_warping_method.h:80
void writeResultToXml()
Definition: regression_test_base.hpp:112
void filterExtremeValues()
Definition: time_averaged_method.hpp:465
StdVec< Real > calculateDTWDistance(DoubleVec< VariableType > dataset_a_, DoubleVec< VariableType > dataset_b_)
Definition: dynamic_time_warping_method.hpp:42
void readResultFromXml()
Definition: regression_test_base.hpp:90
void updateDTWDistance()
Definition: dynamic_time_warping_method.hpp:106
void writeDTWDistanceToXml()
Definition: dynamic_time_warping_method.hpp:121
void readXmlFromXmlFile()
Definition: regression_test_base.h:145
the regression test is based on the dynamic time warping.
Definition: dynamic_time_warping_method.h:40
Real calculatePNorm(Real variable_a, Real variable_b)
Definition: dynamic_time_warping_method.hpp:15
Definition: xml_engine.h:56
void newResultTest(string filter="false")
Definition: dynamic_time_warping_method.h:106
Definition: solid_body_supplementary.cpp:9
The regression test is based on the time-averaged meanvalue and variance.
Definition: time_averaged_method.h:40