NDDEM
Reader-Yade.h
Go to the documentation of this file.
1 #ifndef YADEREADER
2 #define YADEREADER
3 #include "Reader.h"
4 #include "Typedefs.h"
5 #include "pugixml.hpp"
6 #include <algorithm>
7 #include <vector>
8 #include <map>
9 #include "zlib.h"
10 
11 class YadeReader : public Reader {
12 public:
13  virtual std::vector<std::vector<double>> get_bounds()
14  {
15  if (curts==-1) read_timestep(0) ;
16  else read_timestep(curts) ;
17 
18  vector<vector<double>> res(2, vector<double>(3,0)) ;
19  auto x = std::minmax_element(data[0].begin(), data[0].end()) ;
20  auto y = std::minmax_element(data[1].begin(), data[1].end()) ;
21  auto z = std::minmax_element(data[2].begin(), data[2].end()) ;
22  res[0][0]=*(x.first) ; res[1][0]=*(x.second) ;
23  res[0][1]=*(y.first) ; res[1][1]=*(y.second) ;
24  res[0][2]=*(z.first) ; res[1][2]=*(z.second) ;
25 
26  return res ;
27  }
28  virtual std::vector<double> get_minmaxradius()
29  {
30  if (curts==-1) read_timestep(0) ;
31  else read_timestep(curts) ;
32  auto r = std::minmax_element(data[6].begin(), data[6].end()) ;
33  return {*r.first, *r.second} ;
34  }
35 
36  virtual int get_num_particles () {return N;}
37  virtual int get_num_contacts () {return -1;}
38 
39  std::map <DataValue, size_t> data_mapping = {{DataValue::pos, 0}, {DataValue::vel, 3}, {DataValue::radius, 6}, {DataValue::mass, 7}, {DataValue::omega,8}} ;
40  virtual double * get_data(DataValue datavalue, int dd, std::string name="")
41  {
42  for (const auto& [key, idx] : data_mapping)
43  if (key==datavalue)
44  return data[idx].size()==0 ? nullptr : &(data[idx+dd][0]) ;
45  return nullptr ;
46  /*try {
47  int idx = data_mapping.at(datavalue) ;
48  return data[idx].size()==0 ? nullptr : &(data[idx+dd][0]) ;
49  }
50  catch (const std::out_of_range &e) { return nullptr ; }*/
51  }
52 
53 
54  // Base64 functions
55  int read_timestep(int ts) ;
56  int getfield_from_data (std::string name, std::vector<std::string> &variables, std::vector<size_t> &var_offsets, std::string &raw64) ;
57  std::vector<uint8_t> base64decode (const std::vector<uint8_t> & base64) ;
58  size_t length_int2base64 (uint64_t bytes)
59  {
60  size_t res = ceil(bytes*4*8/6.) ;
61  if (res%4) res = res + (4-(res%4)) ;
62  return res ;
63  }
64  size_t length_byte2base64 (uint64_t bytes)
65  {
66  size_t res = ceil(bytes*8/6.) ;
67  if (res%4) res = res + (4-(res%4)) ;
68  return res ;
69  }
71  {
72  return ((*a) | *(a+1)<<8 | *(a+2)<<16 | *(a+3)<<24) ;
73  }
74 
75  double byte2double (const uint8_t * b, char n)
76  {
77  static_assert((sizeof(float)==4)) ;
78  static_assert((sizeof(double)==8)) ;
79 
80  #pragma GCC diagnostic push
81  #pragma GCC diagnostic ignored "-Wstrict-aliasing"
82  if (n==4)
83  {
84  uint32_t v = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24 ;
85  return static_cast<double>(*reinterpret_cast<float*>(&v)) ;
86  }
87  else if (n==8)
88  {
89  uint64_t v=b[7] ;
90  for (int i=1 ; i<8 ; i++) {v<<=8; v|=b[7-i] ;}
91  //printf("%lX %d %d %d %d %d %d %d %d %g\n", v, b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0], *reinterpret_cast<double *>(&v)) ;
92  return *reinterpret_cast<double *>(&v) ;
93  }
94  else return 0 ;
95  #pragma GCC diagnostic pop
96  }
97 
98  virtual int get_numts ()
99  {
100  return Reader::get_numts() ;
101  }
102 
103  std::string path ;
104  int dimension=3 ;
106  int N ;
107 
108 private:
109  std::vector<std::string> variables ;
110  std::vector<size_t> var_offsets ;
111  std::string raw64 ;
112 } ;
113 
114 #endif
Definition: Reader.h:10
int curts
Definition: Reader.h:135
virtual int get_numts()
Definition: Reader.h:15
Definition: Reader-Yade.h:11
virtual int get_num_contacts()
Definition: Reader-Yade.h:37
virtual std::vector< std::vector< double > > get_bounds()
Definition: Reader-Yade.h:13
virtual int get_num_particles()
Definition: Reader-Yade.h:36
double byte2double(const uint8_t *b, char n)
Definition: Reader-Yade.h:75
int read_timestep(int ts)
Definition: Reader-Yade.cpp:4
v2d data
Definition: Reader-Yade.h:105
uint32_t bytes2uint32(const uint8_t *a)
Definition: Reader-Yade.h:70
size_t length_int2base64(uint64_t bytes)
Definition: Reader-Yade.h:58
std::map< DataValue, size_t > data_mapping
Definition: Reader-Yade.h:39
virtual double * get_data(DataValue datavalue, int dd, std::string name="")
Definition: Reader-Yade.h:40
std::vector< size_t > var_offsets
Definition: Reader-Yade.h:110
int N
Definition: Reader-Yade.h:106
std::vector< uint8_t > base64decode(const std::vector< uint8_t > &base64)
Definition: Reader-Yade.cpp:113
virtual int get_numts()
Definition: Reader-Yade.h:98
std::string raw64
Definition: Reader-Yade.h:111
std::string path
Definition: Reader-Yade.h:103
int dimension
Definition: Reader-Yade.h:104
std::vector< std::string > variables
Definition: Reader-Yade.h:109
virtual std::vector< double > get_minmaxradius()
Definition: Reader-Yade.h:28
int getfield_from_data(std::string name, std::vector< std::string > &variables, std::vector< size_t > &var_offsets, std::string &raw64)
Definition: Reader-Yade.cpp:52
size_t length_byte2base64(uint64_t bytes)
Definition: Reader-Yade.h:64
vector< vector< double > > v2d
Definition: Typedefs.h:10
DataValue
Definition: Typedefs.h:19
@ pos
Definition: Typedefs.h:19
@ vel
Definition: Typedefs.h:19
@ omega
Definition: Typedefs.h:19
@ radius
Definition: Typedefs.h:19
@ mass
Definition: Typedefs.h:19
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
unsigned __int64 uint64_t
Definition: stdint.h:136