NDDEM
Reader-NDDEM.h
Go to the documentation of this file.
1 #include "../Dem/Xml.h"
2 #ifndef NDDEMREADER
3 #define NDDEMREADER
4 
5 class NDDEMReader : public Reader {
6 public:
7  NDDEMReader(std::string ppath): path(ppath) {
8  XML = new XMLReader(path) ;
9  dimension=atoi(XML->tags.second["dimensions"].c_str()) ;
12  Nparticles = radius.size() ;
13  reorganised_pos.resize(dimension, nullptr) ;
14  reorganised_vel.resize(dimension, nullptr) ;
15  reorganised_omega.resize((dimension*(dimension-1))/2, nullptr) ;
16  startingpoint = XML->fic.tellg() ;
17  }
18  ~NDDEMReader () {XML->close() ; delete(XML) ;}
19 
20  void post_init() {
21  for (int i=0 ; i<Nparticles ; i++)
22  {
23  mass.push_back(Volume(dimension,radius[i]) * get_default_density()) ;
25  }
26  }
27 
28  int get_dimension () {return dimension;}
29  std::vector<std::vector<double>> get_bounds() {return bounds ; }
30  int get_numts() {return build_index() ; }
31 
32  int read_timestep (int ts) ;
33  double * get_data(DataValue datavalue, int dd, std::string name="") ;
34  int get_num_particles () {return Nparticles;}
35  int get_num_contacts () {return Ncontacts;}
36 
37  int build_index() {index = XML->read_index() ; return index.size() ; }
38  double actualts=0 ;
39 
40 private:
42  std::string path ;
43  std::streampos startingpoint ;
44  int dimension ;
45  std::vector<std::vector<double>> bounds ;
46  std::vector <double> radius ;
47  std::vector <double> mass, Imom ;
48  int Nparticles=-1, Ncontacts=-1 ;
49  std::vector<std::string> names ;
50  std::vector<std::vector<std::vector<double>>> data ;
51  std::vector<std::pair<double,std::streampos>> index ;
52 
53  double InertiaMomentum (int d , double R, double rho) ;
54  void clear_tsdata () {names.clear() ; for (auto & v:data) for (auto & w: v) w.clear() ; for (auto & v:data) v.clear() ; data.clear() ; }
55 
56  std::vector<double*> reorganised_pos ;
57  std::vector<double*> reorganised_vel ;
58  std::vector<double*> reorganised_omega ;
59 } ;
60 
61 //=================================================================================
63 {
64  if (index.size()>0)
65  {
66  XML->fic.seekg(index[ts].second, ios_base::beg) ;
67  clear_tsdata() ;
69  curts = ts ;
70  if (static_cast<float>(actualts) != static_cast<float>(index[ts].first)) printf("WARN: the timestep read is not the one that was expected %.10g %.10g\n", actualts, index[ts].first) ;
71  }
72  else
73  {
74  if (curts>ts)
75  {
76  printf("WARN: this file can only read forward. Restarting if from scratch. \n") ;
77  XML->fic.seekg(startingpoint) ;
78  curts=-1 ;
79  }
80  while (curts<ts)
81  {
82  clear_tsdata() ;
84  curts ++ ;
85  }
86  }
87  return 0 ;
88 }
89 //------------------------------------------------------------------------
90  double * NDDEMReader::get_data(DataValue datavalue, int dd, std::string name)
91 {
92  unsigned int delta ;
93  switch (datavalue)
94  {
95  case DataValue::radius : return (radius.data()) ;
96  case DataValue::mass : return (mass.data()) ;
97  case DataValue::Imom : return (Imom.data()) ;
98  case DataValue::pos :
99  delta = find(names.begin(), names.end(), "Position")-names.begin() ;
100  if (reorganised_pos[dd]==nullptr) reorganised_pos[dd] = (double *) malloc (sizeof(double)*Nparticles) ;
101  for (int j=0 ; j<Nparticles ; j++) reorganised_pos[dd][j] = data[delta][j][dd] ;
102  return reorganised_pos[dd] ;
103  case DataValue::vel :
104  delta=find(names.begin(), names.end(), "Velocity")-names.begin() ;
105  if (reorganised_vel[dd]==nullptr) reorganised_vel[dd] = (double *) malloc (sizeof(double)*Nparticles) ;
106  for (int j=0 ; j<Nparticles ; j++) reorganised_vel[dd][j] = data[delta][j][dd] ;
107  return reorganised_vel[dd] ;
108  case DataValue::omega:
109  delta=find(names.begin(), names.end(), "Omega")-names.begin() ;
110  if (delta==names.size()) return nullptr ;
111  if (reorganised_omega[dd]==nullptr) reorganised_omega[dd] = (double *) malloc (sizeof(double)*Nparticles) ;
112  for (int j=0 ; j<Nparticles ; j++) reorganised_omega[dd][j] = data[delta][j][dd] ;
113  return reorganised_omega[dd] ;
114  default :
115  return nullptr ;
116  }
117 }
118 
119 
120 #define MAXDEFDIM 30
121 double NDDEMReader::InertiaMomentum (int d , double R, double rho)
122 {
123  if (d>MAXDEFDIM)
124  {
125  printf("[WARN] Inertia InertiaMomentum not guaranteed for dimension > %d\n", MAXDEFDIM) ;
126  }
127 
128  double res ;
129  if (d%2==0)
130  {
131  unsigned int k=d/2 ;
132  res=pow(boost::math::double_constants::pi,k)*pow(R, d+2) / boost::math::factorial<double>(k+1) ;
133  return (res*rho) ;
134  }
135  else
136  {
137  unsigned int k=(d-1)/2 ;
138  res=pow(2,k+2) * pow(boost::math::double_constants::pi, k) * pow(R, d+2) / boost::math::double_factorial<double> (d+2) ;
139  return (res*rho) ;
140  }
141 }
142 #endif
#define MAXDEFDIM
Definition: Reader-NDDEM.h:120
Definition: Reader-NDDEM.h:5
std::string path
Definition: Reader-NDDEM.h:42
std::vector< std::vector< double > > bounds
Definition: Reader-NDDEM.h:45
int build_index()
Definition: Reader-NDDEM.h:37
void clear_tsdata()
Definition: Reader-NDDEM.h:54
int get_dimension()
Definition: Reader-NDDEM.h:28
double actualts
Definition: Reader-NDDEM.h:38
std::vector< double * > reorganised_vel
Definition: Reader-NDDEM.h:57
int get_num_particles()
Definition: Reader-NDDEM.h:34
double * get_data(DataValue datavalue, int dd, std::string name="")
Definition: Reader-NDDEM.h:90
int dimension
Definition: Reader-NDDEM.h:44
std::vector< std::string > names
Definition: Reader-NDDEM.h:49
NDDEMReader(std::string ppath)
Definition: Reader-NDDEM.h:7
std::vector< std::pair< double, std::streampos > > index
Definition: Reader-NDDEM.h:51
std::vector< double * > reorganised_pos
Definition: Reader-NDDEM.h:56
int get_numts()
Definition: Reader-NDDEM.h:30
std::vector< double * > reorganised_omega
Definition: Reader-NDDEM.h:58
int read_timestep(int ts)
Definition: Reader-NDDEM.h:62
XMLReader * XML
Definition: Reader-NDDEM.h:41
int Nparticles
Definition: Reader-NDDEM.h:48
std::streampos startingpoint
Definition: Reader-NDDEM.h:43
std::vector< double > Imom
Definition: Reader-NDDEM.h:47
double InertiaMomentum(int d, double R, double rho)
Definition: Reader-NDDEM.h:121
int Ncontacts
Definition: Reader-NDDEM.h:48
std::vector< std::vector< std::vector< double > > > data
Definition: Reader-NDDEM.h:50
std::vector< double > radius
Definition: Reader-NDDEM.h:46
void post_init()
Definition: Reader-NDDEM.h:20
int get_num_contacts()
Definition: Reader-NDDEM.h:35
std::vector< std::vector< double > > get_bounds()
Definition: Reader-NDDEM.h:29
std::vector< double > mass
Definition: Reader-NDDEM.h:47
~NDDEMReader()
Definition: Reader-NDDEM.h:18
Definition: Reader.h:10
int curts
Definition: Reader.h:135
double get_default_density()
Definition: Reader.h:62
double delta
Definition: Reader.h:138
ifstream fic
Definition: Xml.h:66
void close()
Definition: Xml.h:70
Definition: Xml.h:74
int read_radius(vector< double > &radius)
Definition: Xml.cpp:273
int read_boundaries(vector< vector< double >> &boundaries)
Definition: Xml.cpp:258
std::pair< string, map< string, string > > tags
Definition: Xml.h:86
std::vector< std::pair< double, std::streampos > > read_index()
Definition: Xml.cpp:331
double read_nextts(vector< string > &names, vector< vector< vector< double >>> &data)
Definition: Xml.cpp:281
double Volume(int d, double R)
Compute a sphere volume in dimension D.
Definition: Coarsing.cpp:1785
DataValue
Definition: Typedefs.h:19
@ Imom
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
uint d