NDDEM
IOLiggghts.h
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstdio>
3 #include <vector>
4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 #include <math.h>
8 #include <sstream>
9 
10 #include "gzip.hpp"
11 //#include "termcolor.hpp"
12 #include <boost/iostreams/filtering_streambuf.hpp>
13 #include <boost/iostreams/stream.hpp>
14 #include "json.hpp"
15 #include "Typedefs.h"
16 #include "Coarsing.h"
17 
18 using namespace std;
20 
21 
22 class Datafile {
23 public:
24  Datafile() : Radius(0.00075), Rho(2500) {}
26  if (file_in != nullptr && file_in->is_open()) file_in->close() ;
27  if (file_incf != nullptr && file_incf->is_open()) file_incf->close() ;
28  }
29 
30 
31  istream *in = nullptr, *incf = nullptr ; //, *incf ;
32 
33  int curts ; int N, Ncf ;
34  double Radius, Rho ;
35 
36  vector <string> fields, fieldscf ;
37  v2d data, tdata, datacf ;
39 
40  // Functions
41  int open(string path) ;
42  int reopen(string path) ;
43  int opencf(string path) ;
44  int reopencf(string path) ;
45  int read_full_ts(bool keep) ;
46  int set_data(struct Data & D, std::map<string,size_t> extrafieldmap) ;
47  vector<vector<double>> get_bounds () ;
48  vector <vector <double> > boundaries ;
49  vector<bool> periodicity ;
50  vector<double> delta ;
51  int get_numts () ;
52 
53  std::map <std::string, std::string> cfmapping ;
54 
55 private:
56  int read_next_ts(istream *is, bool iscf, bool keep) ;
57  int do_post_atm() ;
58  int do_post_cf() ;
59  boost::iostreams::filtering_streambuf<boost::iostreams::input> filt_in;
60  boost::iostreams::filtering_streambuf<boost::iostreams::input> filt_in2;
61  boost::iostreams::filtering_streambuf<boost::iostreams::input> filt_incf;
62  boost::iostreams::filtering_streambuf<boost::iostreams::input> filt_incf2;
63  ifstream * file_in = nullptr , *file_incf = nullptr ;
64 } ;
65 
66 // ========================
67 struct Param {
68 public:
69  string atmdump="" ;
70  string cfdump="" ;
71  int skipT=0 ;
72  int maxT =1;
73  vector <string> flags = {} ;
74  const int dim=3 ;
75  vector <int> boxes= {3,3,3} ;
76  vector <vector <double> > boundaries={{0,0,0},{1,1,1}} ;
77  string save="" ;
78  vector<string> saveformat ;
79  std::map <std::string, std::string> cfmapping ;
80 
81  bool hascf = false ;
82  bool dotimeavg = false ;
83  uint8_t maxlevel = 3 ;
84 
85  void from_json (json & j) ;
86  string windowstr = "";
88  double windowsize ;
89  vector<bool> periodicity ;
90  vector<double>delta ;
91 
92  struct ExtaField {
93  string name ;
96  } ;
97  vector<ExtaField> extrafields ;
98 
99 private :
100  bool needmaxts = true ;
101  bool needbounds = true ;
102  void post_init () ;
103 
104 } ;
105 
106 //-------------------------------
108 {
109  auto v = j.find("particles") ;
110  if (v == j.end()) { printf("The key 'particles' is required\n") ; std::exit(3) ; }
111  else atmdump = v->get<string>();
112 
113  v = j.find("savefile") ;
114  if (v == j.end()) { printf("The key 'savefile' is required\n") ; std::exit(3) ; }
115  else save = v->get<string>();
116 
117  v = j.find("saveformat") ;
118  if (v == j.end()) { printf("The key 'saveformat' is required\n") ; std::exit(3) ; }
119  else
120  {
121  try {saveformat = v->get<vector<string>>();}
122  catch(...) {string a = v->get<string>(); saveformat.push_back(a) ; }
123  }
124 
125  v = j.find("window size") ;
126  if (v != j.end()) windowsize = v->get<double>();
127  else { printf("The key 'window size' is required\n") ; std::exit(3) ; }
128 
129  v = j.find("forces") ;
130  if (v != j.end()) {cfdump = v->get<string>(); hascf = true ; }
131 
132  v = j.find("skip") ;
133  if (v != j.end()) skipT = v->get<int>();
134 
135  v = j.find("max time") ;
136  if (v != j.end()) { maxT = v->get<int>(); needmaxts = false ; }
137  else printf("The 'max time' key is strongly recommended, the file will have to be processed twice otherwise.\n") ;
138 
139  v = j.find("fields") ;
140  if (v != j.end()) flags = v->get<vector<string>>();
141  else printf("The 'fields' key is strongly recommended, nothing will be processed otherwise.\n") ;
142 
143  v = j.find("boxes") ;
144  if (v != j.end()) boxes = v->get<decltype(boxes)>();
145 
146  v = j.find("mapping") ;
147  if (v != j.end()) cfmapping= v->get<decltype(cfmapping)>();
148 
149  v = j.find("boundaries") ;
150  if (v != j.end()) {boundaries = v->get<decltype(boundaries)>(); needbounds=false ; }
151  else {printf("'Boundaries' are not defined, the whole simulation box will be used.\n") ;}
152 
153  v = j.find("time average") ;
154  if (v != j.end()) {dotimeavg = v->get<bool>(); }
155 
156  v = j.find("window") ;
157  if (v != j.end()) windowstr = v->get<string>();
158 
159  v = j.find("periodicity") ;
160  if (v != j.end()) periodicity = v->get<decltype(periodicity)>();
161 
162  v = j.find("extra fields") ;
163  if (v != j.end())
164  {
165  auto v2=j["extra fields"] ;
166  extrafields.resize(v2.size()) ;
167  for (size_t i=0 ; i<v2.size() ; i++)
168  {
169  extrafields[i].name = v2[i]["name"] ;
170  extrafields[i].order = static_cast<TensorOrder>(v2[i]["tensor order"].get<int>()) ;
171 
172  if (v2[i]["type"]=="Particle")
173  extrafields[i].type=FieldType::Particle ;
174  else if (v2[i]["type"]=="Contact")
175  extrafields[i].type=FieldType::Contact ;
176  else if (v2[i]["type"]=="Fluctuation")
177  extrafields[i].type=FieldType::Fluctuation ;
178  else
179  printf("ERR: unknown extrafields type\n") ;
180  }
181  }
182 
183 
184  post_init() ;
185 }
186 
187 //-------------------------------
189 {
190  if (boundaries.size() != 2 || boundaries[0].size() != static_cast<size_t>(dim) || boundaries[1].size() != static_cast<size_t>(dim) )
191  { printf("Inconsistent boundaries\n") ; std::exit(3) ; }
192 
193  if (!hascf && (
194  std::find(flags.begin(), flags.end(), "TC") != flags.end() ||
195  std::find(flags.begin(), flags.end(), "MC") != flags.end() ||
196  std::find(flags.begin(), flags.end(), "mC") != flags.end() ||
197  std::find(flags.begin(), flags.end(), "qTC") != flags.end() ||
198  std::find(flags.begin(), flags.end(), "qRC") != flags.end()
199  ))
200  { printf("Contact force informations are required for one or more of the fields you requested\n") ; std::exit(3) ; }
201 
202  if (std::find(flags.begin(), flags.end(), "RHO" ) != flags.end() ||
203  std::find(flags.begin(), flags.end(), "VAVG" ) != flags.end() ||
204  std::find(flags.begin(), flags.end(), "ROT" ) != flags.end() ||
205  std::find(flags.begin(), flags.end(), "EKT" ) != flags.end() ||
206  std::find(flags.begin(), flags.end(), "EKR" ) != flags.end())
207  maxlevel=1 ;
208 
209  if (std::find(flags.begin(), flags.end(), "TK" ) != flags.end() ||
210  std::find(flags.begin(), flags.end(), "MK" ) != flags.end() ||
211  std::find(flags.begin(), flags.end(), "eKT" ) != flags.end() ||
212  std::find(flags.begin(), flags.end(), "eKR" ) != flags.end() ||
213  std::find(flags.begin(), flags.end(), "qTK" ) != flags.end() ||
214  std::find(flags.begin(), flags.end(), "qRK" ) != flags.end())
215  maxlevel = 2 ;
216 
217  if (std::find(flags.begin(), flags.end(), "TC" ) != flags.end() ||
218  std::find(flags.begin(), flags.end(), "MC" ) != flags.end() ||
219  std::find(flags.begin(), flags.end(), "mC" ) != flags.end() ||
220  std::find(flags.begin(), flags.end(), "qTC" ) != flags.end() ||
221  std::find(flags.begin(), flags.end(), "qRC" ) != flags.end() ||
222  std::find(flags.begin(), flags.end(), "zT" ) != flags.end() ||
223  std::find(flags.begin(), flags.end(), "zR" ) != flags.end())
224  maxlevel = 3 ;
225 
226  if (std::find(flags.begin(), flags.end(), "TotalStress" ) != flags.end() ||
227  std::find(flags.begin(), flags.end(), "Pressure" ) != flags.end() ||
228  std::find(flags.begin(), flags.end(), "KineticPressure" ) != flags.end() ||
229  std::find(flags.begin(), flags.end(), "ShearStress" ) != flags.end() ||
230  std::find(flags.begin(), flags.end(), "StrainRate" ) != flags.end() ||
231  std::find(flags.begin(), flags.end(), "VolumetricStrainRate" ) != flags.end() ||
232  std::find(flags.begin(), flags.end(), "ShearStrainRate" ) != flags.end())
233  maxlevel = 4 ;
234 
235  if (needbounds)
236  {
237  Datafile D ; // Temporary datafile in principle the destructor handles the file closing ...
238  D.open(atmdump) ;
239  boundaries = D.get_bounds() ;
240  }
241 
242  if (periodicity.size()>0)
243  {
244  bool hasper=false ;
245  for (auto v : periodicity)
246  if (v)
247  hasper=true ;
248  if (!hasper)
249  periodicity.resize(0) ; // A periodic vector was defined, but with no PBC ... removing it.
250  else
251  {
252  delta.resize(dim,0) ;
253  if (needbounds==false)
254  printf("WARN: you should really not provide the bounds if you are expecting to use periodicity.\n") ;
255 
256  for(int i=0 ; i<dim ; i++)
257  {
258  if (periodicity[i])
259  {
260  if (boxes[i]!=1)
261  printf("WARN: using more than 1 box in the periodic dimension does not make much sense\n");
262  delta[i] = boundaries[1][i]-boundaries[0][i] ;
263  }
264  }
265  }
266  }
267 
268  if (needmaxts)
269  {
270  Datafile D ; // Temporary datafile in principle the destructor handles the file closing ...
271  D.open(atmdump) ;
272  maxT = D.get_numts() ;
273  }
274 
275  if (windowstr != "")
276  {
277  if (windowstr=="Rect3D") window=Windows::Rect3D ;
278  else if ( windowstr=="Rect3DIntersect") window=Windows::Rect3DIntersect ;
279  else if ( windowstr=="Lucy3D") window=Windows::Lucy3D ;
280  else if ( windowstr=="Hann3D") window=Windows::Hann3D ;
281  else if ( windowstr=="RectND") window=Windows::RectND ;
282  else if ( windowstr=="LucyND") window=Windows::LucyND ;
283  else if ( windowstr=="LucyND_Periodic") window=Windows::LucyND_Periodic ;
284  else if ( windowstr=="Lucy3DFancyInt") window=Windows::Lucy3DFancyInt;
285  else {printf("Unknown windowing function.\n") ; std::exit(4) ; }
286  }
287 }
Windows
Definition: WindowLibrary.h:2
@ Lucy3DFancyInt
@ LucyND_Periodic
Definition: IOLiggghts.h:22
vector< bool > periodicity
Definition: IOLiggghts.h:49
Datafile()
Definition: IOLiggghts.h:24
v1d dataextra
Definition: IOLiggghts.h:38
vector< vector< double > > boundaries
Definition: IOLiggghts.h:48
double Radius
Definition: IOLiggghts.h:34
v2d data
Definition: IOLiggghts.h:37
boost::iostreams::filtering_streambuf< boost::iostreams::input > filt_incf2
Definition: IOLiggghts.h:62
boost::iostreams::filtering_streambuf< boost::iostreams::input > filt_incf
Definition: IOLiggghts.h:61
~Datafile()
Definition: IOLiggghts.h:25
vector< string > fields
Definition: IOLiggghts.h:36
boost::iostreams::filtering_streambuf< boost::iostreams::input > filt_in
Definition: IOLiggghts.h:59
int curts
Definition: IOLiggghts.h:33
boost::iostreams::filtering_streambuf< boost::iostreams::input > filt_in2
Definition: IOLiggghts.h:60
int open(string path)
Definition: IOLiggghts.cpp:131
std::map< std::string, std::string > cfmapping
Definition: IOLiggghts.h:53
vector< double > delta
Definition: IOLiggghts.h:50
vector< vector< double > > get_bounds()
Definition: IOLiggghts.cpp:174
int get_numts()
Definition: IOLiggghts.cpp:191
namespace for Niels Lohmann
Definition: json.hpp:20203
iterator find(const typename object_t::key_type &key)
find an element in a JSON object
Definition: json.hpp:22784
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:21425
TensorOrder
Definition: Coarsing.h:58
FieldType
Definition: Coarsing.h:59
@ Particle
Definition: Coarsing.h:59
@ Fluctuation
Definition: Coarsing.h:59
@ Contact
Definition: Coarsing.h:59
vector< vector< double > > v2d
Definition: Typedefs.h:10
vector< double > v1d
Definition: Typedefs.h:9
int N
constexpr JSON_INLINE_VARIABLE const auto & from_json
Definition: json.hpp:5394
void save(Archive &ar, SetT const &set)
Definition: set.hpp:42
basic_json<> json
default JSON class
Definition: json.hpp:3372
Definition: json.hpp:5678
FILE * open(char path[], int d)
Definition: Vtk.h:4
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
unsigned char uint8_t
Definition: stdint.h:124
Data structure handling point data and contact data.
Definition: Coarsing.h:93
Definition: IOLiggghts.h:92
TensorOrder order
Definition: IOLiggghts.h:94
string name
Definition: IOLiggghts.h:93
FieldType type
Definition: IOLiggghts.h:95
Contains the parameters of the simulation & CG.
Definition: IOLiggghts.h:67
double windowsize
Definition: IOLiggghts.h:88
std::map< std::string, std::string > cfmapping
Definition: IOLiggghts.h:79
vector< ExtaField > extrafields
Definition: IOLiggghts.h:97
vector< bool > periodicity
Definition: IOLiggghts.h:89
vector< string > saveformat
Definition: IOLiggghts.h:78
void from_json(json &j)
Definition: IOLiggghts.h:107
void post_init()
Definition: IOLiggghts.h:188
vector< double > delta
Definition: IOLiggghts.h:90