SPHinXsys  alpha version
regression_test_base.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 * --------------------------------------------------------------------------*/
29 #pragma once
30 
31 #include "in_output.h"
32 #include "xml_engine.h"
33 #include "all_physical_dynamics.h"
34 
35 namespace SPH
36 {
44  template<class ObserveMethodType>
46  {
47  /*identify the variable type from the parent class. */
48  using VariableType = decltype(ObserveMethodType::type_indicator_);
49 
50  protected:
51  std::string input_folder_path_; /* the folder path for the input folder. (folder) */
52  std::string in_output_filefullpath_; /* the file path for current result from xml memory to xml file. */
53  std::string result_filefullpath_; /* the file path for all run results. (.xml)*/
54  std::string runtimes_filefullpath_; /* the file path for run times information. (.dat)*/
55  std::string converged; /* the tag for result converged, default false. */
56 
57  XmlMemoryIO xmlmemory_io_; /* xml memory in_output operator, which has defined several
58  methods to read and write data from and into xml memory,
59  including one by one, or all result in the same time. */
60 
61  XmlEngine observe_xml_engine_; /* xml engine for current result in_output. */
62  XmlEngine result_xml_engine_in_; /* xml engine for input result. */
63  XmlEngine result_xml_engine_out_; /* xml engine for output result. */
64  /* the XmlEngine can operate the node name and elements in xml memory. */
65 
66  StdVec<std::string> element_tag_; /* the container of the tag of current result. */
67  DoubleVec<VariableType> current_result_; /* the container of current run result stored as snapshot * observation. */
68  DoubleVec<VariableType> current_result_trans_; /* the container of current run result with snapshot & observations transposed,
69  because this data structure is required in TA and DTW method. */
70  DoubleVec<VariableType> result_in_; /* the temporary container of each result when reading from .xml memory, and
71  it may be snapshot * observations (reading all result for averaged methods),
72  or observations * snapshot (reading specified result for TA and DTW method.) */
73  TripleVec<VariableType> result_; /* the container of results in all runs (run * snapshot * observation) */
74 
75  int snapshot_, observation_; /* the size of each layer of current result vector. */
76  int difference_; /* the length difference of snapshot between old and new result,
77  which is used to trim each new run result to be a unified length. */
78  int number_of_run_; /* the times of run. */
79  int label_for_repeat_; /* the label used stable convergence (several convergence). */
80  int number_of_snapshot_old_; /* the snapshot size of last trimmed result. */
81 
82  public:
83  template<typename... ConstructorArgs>
84  explicit RegressionTestBase(ConstructorArgs &&...args) :
85  ObserveMethodType(std::forward<ConstructorArgs>(args)...), xmlmemory_io_(),
86  observe_xml_engine_("xml_observe_reduce", this->quantity_name_),
87  result_xml_engine_in_("result_xml_engine_in", "result"),
88  result_xml_engine_out_("result_xml_engine_out", "result")
89  {
90  input_folder_path_ = this->in_output_.input_folder_;
91  in_output_filefullpath_ = input_folder_path_ + "/" + this->body_name_
92  + "_" + this->quantity_name_ + "_" + this->in_output_.restart_step_ + ".xml";
93  result_filefullpath_ = input_folder_path_ + "/" + this->body_name_
94  + "_" + this->quantity_name_ + "_result.xml";
95  runtimes_filefullpath_ = input_folder_path_ + "/" + this->body_name_
96  + "_" + this->quantity_name_ + "_runtimes.dat";
97 
98  if (!fs::exists(runtimes_filefullpath_))
99  {
100  converged = "false";
101  number_of_run_ = 1;
102  label_for_repeat_ = 0;
103  }
104  else
105  {
106  std::ifstream in_file(runtimes_filefullpath_.c_str());
107  in_file >> converged;
108  in_file >> number_of_run_;
109  in_file >> label_for_repeat_;
110  in_file.close();
111  };
112  };
113  virtual ~RegressionTestBase();
114 
115  void writeToXml(ObservedQuantityRecording<VariableType>* observe_method, size_t iteration = 0);
116  template <typename ReduceType>
117  void writeToXml(BodyReducedQuantityRecording<ReduceType>* reduce_method, size_t iteration = 0);
118  /* read current result from xml file into xml memory. */
119  void readFromXml(ObservedQuantityRecording<VariableType>* observe_method);
120  template <typename ReduceType>
121  void readFromXml(BodyReducedQuantityRecording<ReduceType>* reduce_method);
122 
123  void transposeTheIndex();
124  void readResultFromXml();
125  void writeResultToXml();
126  void readResultFromXml(int index_of_run_); /* read the result from the .xml file with the specified index. (DTW method, TA method) */
127  void writeResultToXml(int index_of_run_); /* write the result to the .xml file with the specified index. (DTW method, TA method) */
128 
130  void writeToFile(size_t iteration = 0) override
131  {
132  ObserveMethodType::writeToFile(iteration); /* used for visualization (.dat)*/
133  writeToXml(this, iteration); /* used for regression test. (.xml) */
134  };
135 
136  /*The new run result is stored in the xml memory, and can't be copied and used directly
137  *so they should be output as .xml file and reloaded from .xml file into the memory.*/
140  {
141  observe_xml_engine_.writeToXmlFile(in_output_filefullpath_); /* two method are same.*/
142  };
143 
146  {
147  readFromXml(this);
148  };
149  };
150 };
void writeXmlToXmlFile()
Definition: regression_test_base.h:139
Definition: xml_engine.h:116
void writeResultToXml()
Definition: regression_test_base.hpp:112
XML class for xml input and output, this is GUI of simbody xml parser.
void readResultFromXml()
Definition: regression_test_base.hpp:90
The base of regression test for various method (time-averaged, ensemble-averaged, dynamic time warpin...
Definition: regression_test_base.h:45
void writeToFile(size_t iteration=0) override
Definition: regression_test_base.h:130
void readXmlFromXmlFile()
Definition: regression_test_base.h:145
void writeToXmlFile(const std::string &filefullpath)
Definition: xml_engine.cpp:67
write files for observed quantity
Definition: in_output.h:300
Definition: xml_engine.h:56
Classes for input and output functions.
write reduced quantity of a body
Definition: in_output.h:357
Definition: solid_body_supplementary.cpp:9