SPHinXsys  alpha version
in_output.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  * HU1527/12-1 and HU1527/12-4. *
14  * *
15  * Portions copyright (c) 2017-2022 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 #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
31 
32 #include "base_data_package.h"
33 #include "sph_data_containers.h"
34 #include "all_physical_dynamics.h"
35 #include "parameterization.h"
36 #include "xml_engine.h"
37 
38 #include <sstream>
39 #include <iomanip>
40 #include <fstream>
42 #ifdef __APPLE__
43 #include <boost/filesystem.hpp>
44 namespace fs = boost::filesystem;
45 #else
46 #include <experimental/filesystem>
47 namespace fs = std::experimental::filesystem;
48 #endif
49 
50 using VtuStringData = std::map<std::string, std::string>;
51 
52 namespace SPH
53 {
54 
58  class BaseLevelSet;
59 
65  class InOutput
66  {
67  private:
68  UniquePtrKeeper<ParameterizationIO> parameterization_io_ptr_keeper_;
69 
70  public:
71  explicit InOutput(SPHSystem &sph_system, bool delete_output = true);
72  virtual ~InOutput(){};
73 
74  SPHSystem &sph_system_;
75  std::string input_folder_;
76  std::string output_folder_;
77  std::string restart_folder_;
78  std::string reload_folder_;
79  std::string restart_step_;
80 
81  ParameterizationIO &defineParameterizationIO();
82  };
83 
88  class PltEngine
89  {
90  public:
91  PltEngine(){};
92  virtual ~PltEngine(){};
93 
94  void writeAQuantityHeader(std::ofstream &out_file, const Real &quantity, const std::string &quantity_name);
95  void writeAQuantityHeader(std::ofstream &out_file, const Vecd &quantity, const std::string &quantity_name);
96  void writeAQuantity(std::ofstream &out_file, const Real &quantity);
97  void writeAQuantity(std::ofstream &out_file, const Vecd &quantity);
98  };
99 
105  {
106  protected:
107  InOutput &in_output_;
108  SPHBody *body_;
109  SPHBodyVector bodies_;
110 
111  std::string convertPhysicalTimeToString(Real physical_time);
112 
113  public:
114  BodyStatesIO(InOutput &in_output, SPHBody &body)
115  : in_output_(in_output), body_(&body), bodies_({&body}){};
116  BodyStatesIO(InOutput &in_output, SPHBodyVector bodies)
117  : in_output_(in_output), body_(bodies[0]), bodies_(bodies){};
118  virtual ~BodyStatesIO(){};
119  };
120 
126  {
127  public:
128  BodyStatesRecording(InOutput &in_output, SPHBody &body)
129  : BodyStatesIO(in_output, body){};
130  BodyStatesRecording(InOutput &in_output, SPHBodyVector bodies)
131  : BodyStatesIO(in_output, bodies){};
132  virtual ~BodyStatesRecording(){};
133 
135  void writeToFile()
136  {
137  writeWithFileName(convertPhysicalTimeToString(GlobalStaticVariables::physical_time_));
138  };
139 
141  virtual void writeToFile(size_t iteration_step);
142 
143  protected:
144  virtual void writeWithFileName(const std::string &sequence) = 0;
145  };
146 
151  template <class MobilizedBodyType>
153  {
154  protected:
155  InOutput &in_output_;
156  SimTK::RungeKuttaMersonIntegrator &integ_;
157  MobilizedBodyType &mobody_;
158 
159  public:
160  SimBodyStatesIO(InOutput &in_output, SimTK::RungeKuttaMersonIntegrator &integ, MobilizedBodyType &mobody)
161  : in_output_(in_output), integ_(integ), mobody_(mobody){};
162  virtual ~SimBodyStatesIO(){};
163  };
164 
169  template <class MobilizedBodyType>
170  class WriteSimBodyStates : public SimBodyStatesIO<MobilizedBodyType>
171  {
172  public:
173  WriteSimBodyStates(InOutput &in_output, SimTK::RungeKuttaMersonIntegrator &integ, MobilizedBodyType &mobody)
174  : SimBodyStatesIO<MobilizedBodyType>(in_output, integ, mobody){};
175  virtual ~WriteSimBodyStates(){};
176 
177  virtual void writeToFile(size_t iteration_step) = 0;
178  };
179 
184  template <class MobilizedBodyType>
185  class ReadSimBodyStates : public SimBodyStatesIO<MobilizedBodyType>
186  {
187  public:
188  ReadSimBodyStates(InOutput &in_output, MobilizedBodyType *mobody)
189  : SimBodyStatesIO<MobilizedBodyType>(in_output, mobody){};
190  ReadSimBodyStates(InOutput &in_output, StdVec<MobilizedBodyType *> mobodies)
191  : SimBodyStatesIO<MobilizedBodyType>(in_output, mobodies){};
192  virtual ~ReadSimBodyStates(){};
193 
194  virtual void readFromFile(size_t iteration_step) = 0;
195  };
196 
203  {
204  public:
205  BodyStatesRecordingToVtp(InOutput &in_output, SPHBody &body)
206  : BodyStatesRecording(in_output, body){};
208  : BodyStatesRecording(in_output, bodies){};
209  virtual ~BodyStatesRecordingToVtp(){};
210 
211  protected:
212  virtual void writeWithFileName(const std::string &sequence) override;
213  };
214 
222  {
223  public:
225  : BodyStatesRecording(in_output, bodies){};
226  virtual ~BodyStatesRecordingToVtpString() = default;
227 
228  const VtuStringData &GetVtuData() const;
229  void clear()
230  {
231  _vtuData.clear();
232  }
233 
234  protected:
235  virtual void writeWithFileName(const std::string &sequence) override;
236  virtual void writeVtu(std::ostream &stream, SPHBody *body) const;
237 
238  private:
239  VtuStringData _vtuData;
240  };
241 
248  {
249  public:
250  BodyStatesRecordingToPlt(InOutput &in_output, SPHBody &body)
251  : BodyStatesRecording(in_output, body){};
253  : BodyStatesRecording(in_output, bodies){};
254  virtual ~BodyStatesRecordingToPlt(){};
255 
256  protected:
257  virtual void writeWithFileName(const std::string &sequence) override;
258  };
259 
266  : public BodyStatesRecordingToVtp
267  {
268  protected:
269  bool out_of_bound_;
270  StdVec<VelocityBoundCheck> check_bodies_;
271  virtual void writeWithFileName(const std::string &sequence) override;
272 
273  public:
275  SPHBodyVector bodies, Real velocity_bound);
276  virtual ~WriteToVtpIfVelocityOutOfBound(){};
277  };
278 
284  {
285  protected:
286  std::string filefullpath_;
287  BaseMeshField *mesh_field_;
288  virtual void writeWithFileName(const std::string &sequence) override;
289 
290  public:
291  MeshRecordingToPlt(InOutput &in_output, SPHBody &body, BaseMeshField *mesh_field);
292  virtual ~MeshRecordingToPlt(){};
293  };
294 
299  template <typename VariableType>
301  public observer_dynamics::ObservingAQuantity<VariableType>
302  {
303  protected:
304  SPHBody *observer_;
305  PltEngine plt_engine_;
306  BaseParticles *base_particles_;
307  std::string body_name_;
308  const std::string quantity_name_;
309  std::string filefullpath_output_;
310 
311  public:
312  VariableType type_indicator_; /*< this is an indicator to identify the variable type. */
313 
314  public:
315  ObservedQuantityRecording(const std::string &quantity_name, InOutput &in_output,
316  BaseBodyRelationContact &contact_relation)
317  : BodyStatesRecording(in_output, *contact_relation.sph_body_),
318  observer_dynamics::ObservingAQuantity<VariableType>(contact_relation, quantity_name),
319  observer_(contact_relation.sph_body_), plt_engine_(),
320  base_particles_(observer_->base_particles_), body_name_(contact_relation.sph_body_->getBodyName()),
321  quantity_name_(quantity_name)
322  {
324  filefullpath_output_ = in_output_.output_folder_ + "/" + body_name_ + "_" + quantity_name + "_" + in_output_.restart_step_ + ".dat";
325  std::ofstream out_file(filefullpath_output_.c_str(), std::ios::app);
326  out_file << "run_time"
327  << " ";
328  for (size_t i = 0; i != base_particles_->total_real_particles_; ++i)
329  {
330  std::string quantity_name_i = quantity_name + "[" + std::to_string(i) + "]";
331  plt_engine_.writeAQuantityHeader(out_file, (*this->interpolated_quantities_)[i], quantity_name_i);
332  }
333  out_file << "\n";
334  out_file.close();
335  };
336  virtual ~ObservedQuantityRecording(){};
337 
338  virtual void writeWithFileName(const std::string &sequence) override
339  {
340  this->parallel_exec();
341  std::ofstream out_file(filefullpath_output_.c_str(), std::ios::app);
342  out_file << GlobalStaticVariables::physical_time_ << " ";
343  for (size_t i = 0; i != base_particles_->total_real_particles_; ++i)
344  {
345  plt_engine_.writeAQuantity(out_file, (*this->interpolated_quantities_)[i]);
346  }
347  out_file << "\n";
348  out_file.close();
349  };
350  };
351 
356  template <class ReduceMethodType>
358  {
359  protected:
360  InOutput &in_output_;
361  PltEngine plt_engine_;
362  ReduceMethodType reduce_method_;
363  std::string body_name_;
364  const std::string quantity_name_;
365  std::string filefullpath_output_;
366 
367  public:
368  /*< deduce variable type from reduce method. */
369  using VariableType = decltype(reduce_method_.InitialReference());
370  VariableType type_indicator_; /*< this is an indicator to identify the variable type. */
371 
372  public:
373  template <typename... ConstructorArgs>
374  BodyReducedQuantityRecording(InOutput &in_output, ConstructorArgs &&...args)
375  : in_output_(in_output), plt_engine_(), reduce_method_(std::forward<ConstructorArgs>(args)...),
376  body_name_(reduce_method_.getSPHBody()->getBodyName()),
377  quantity_name_(reduce_method_.QuantityName())
378  {
380  filefullpath_output_ = in_output_.output_folder_ + "/" + body_name_ + "_" + quantity_name_ + "_" + in_output_.restart_step_ + ".dat";
381  std::ofstream out_file(filefullpath_output_.c_str(), std::ios::app);
382  out_file << "\"run_time\""
383  << " ";
384  plt_engine_.writeAQuantityHeader(out_file, reduce_method_.InitialReference(), quantity_name_);
385  out_file << "\n";
386  out_file.close();
387  };
388  virtual ~BodyReducedQuantityRecording(){};
389 
390  virtual void writeToFile(size_t iteration_step = 0)
391  {
392  std::ofstream out_file(filefullpath_output_.c_str(), std::ios::app);
393  out_file << GlobalStaticVariables::physical_time_ << " ";
394  plt_engine_.writeAQuantity(out_file, reduce_method_.parallel_exec());
395  out_file << "\n";
396  out_file.close();
397  };
398  };
399 
405  {
406  protected:
407  StdVec<std::string> file_paths_;
408 
409  public:
410  ReloadParticleIO(InOutput &in_output, SPHBodyVector bodies);
411  ReloadParticleIO(InOutput &in_output, SPHBodyVector bodies, const StdVec<std::string> &given_body_names);
412  virtual ~ReloadParticleIO(){};
413 
414  virtual void writeToFile(size_t iteration_step = 0);
415  virtual void readFromFile(size_t iteration_step = 0);
416  };
417 
422  class RestartIO : public BodyStatesIO
423  {
424  protected:
425  std::string overall_file_path_;
426  StdVec<std::string> file_paths_;
427 
428  Real readRestartTime(size_t restart_step);
429 
430  public:
431  RestartIO(InOutput &in_output, SPHBodyVector bodies);
432  virtual ~RestartIO(){};
433 
434  virtual void writeToFile(size_t iteration_step = 0);
435  virtual void readFromFile(size_t iteration_step = 0);
436  virtual Real readRestartFiles(size_t restart_step)
437  {
438  readFromFile(restart_step);
439  return readRestartTime(restart_step);
440  };
441  };
442 
447  class WriteSimBodyPinData : public WriteSimBodyStates<SimTK::MobilizedBody::Pin>
448  {
449  protected:
450  std::string filefullpath_;
451 
452  public:
453  WriteSimBodyPinData(InOutput &in_output, SimTK::RungeKuttaMersonIntegrator &integ, SimTK::MobilizedBody::Pin &pinbody);
454  virtual ~WriteSimBodyPinData(){};
455  virtual void writeToFile(size_t iteration_step = 0) override;
456  };
457 
463  {
464  protected:
465  InOutput &in_output_;
466  BaseMaterial *base_material_;
467  std::string file_path_;
468 
469  public:
470  ReloadMaterialParameterIO(InOutput &in_output, BaseMaterial *base_material);
471  ReloadMaterialParameterIO(InOutput &in_output, BaseMaterial *base_material, const std::string &given_parameters_name);
472  virtual ~ReloadMaterialParameterIO(){};
473 
474  virtual void writeToFile(size_t iteration_step = 0);
475  virtual void readFromFile(size_t iteration_step = 0);
476  };
477 }
Particles with essential (geometric and kinematic) data. There are three types of particles, all par...
Definition: base_particles.h:81
Write the restart file in XML format.
Definition: in_output.h:422
The base relation between a SPH body and its contact SPH bodies.
Definition: base_body_relation.h:136
For write and read material property.
Definition: in_output.h:462
Observing a variable from contact bodies.
Definition: observer_dynamics.h:119
Base of all materials.
Definition: base_material.h:52
base class for write SimBody states.
Definition: in_output.h:170
void writeToFile()
Definition: in_output.h:135
Write total force acting a solid body.
Definition: in_output.h:447
The base class which defines Tecplot file related operation.
Definition: in_output.h:88
XML class for xml input and output, this is GUI of simbody xml parser.
A wrapper to provide an ownership for a new derived object which previous often generated by new a ra...
Definition: ownership.h:90
Abstract base class for the field data saved on a mesh.
Definition: base_mesh.h:128
Write files for bodies the output file is VTK XML format can visualized by ParaView the data type vtk...
Definition: in_output.h:202
Write files for bodies the output file is dat format can visualized by TecPlot.
Definition: in_output.h:247
base class for write and read body states.
Definition: in_output.h:104
Write strings for bodies the output is map of strings with VTK XML format can visualized by ParaView ...
Definition: in_output.h:221
output body sates if particle velocity is out of a bound
Definition: in_output.h:265
std::map< std::string, std::string > VtuStringData
Definition: in_output.h:50
static Real physical_time_
Definition: base_particle_dynamics.h:132
This is the base classes for introducing the parameterization of a class or method.
ObservedQuantityRecording(const std::string &quantity_name, InOutput &in_output, BaseBodyRelationContact &contact_relation)
Definition: in_output.h:315
StdVec< SPHBody * > SPHBodyVector
Definition: sph_data_containers.h:30
base class for write and read SimBody states.
Definition: in_output.h:152
Set up of basic data structure.
base class for write body states.
Definition: in_output.h:125
write the background mesh data for relax body
Definition: in_output.h:283
SPHBody is a base body with basic data and functions. Its derived class can be a real fluid body...
Definition: base_body.h:61
Definition: parameterization.h:40
The base class which defines folders for output, restart and particle reload folders.
Definition: in_output.h:65
write files for observed quantity
Definition: in_output.h:300
BodyReducedQuantityRecording(InOutput &in_output, ConstructorArgs &&...args)
Definition: in_output.h:374
The SPH system managing objects in the system level.
Definition: sph_system.h:46
write reduced quantity of a body
Definition: in_output.h:357
Write the reload particles file in XML format.
Definition: in_output.h:404
Definition: solid_body_supplementary.cpp:9
base class for read SimBody states.
Definition: in_output.h:185