NDDEM
Reader.h
Go to the documentation of this file.
1 #include <vector>
2 #include <string>
3 #include <cmath>
4 #include <optional>
5 #include "Typedefs.h"
6 
7 #ifndef READER
8 #define READER
9 
10 class Reader {
11 public:
12  virtual std::vector<std::vector<double>> get_bounds() {return {} ; }
13  virtual std::vector<double> get_minmaxradius() {return {} ; }
14  virtual int get_dimension () {return 3 ;}
15  virtual int get_numts()
16  {
17  if (filenumbering.numts != -1) return filenumbering.numts ;
18  if (filenumbering.ismultifile)
19  {
20  filenumbering.numts=0 ;
21  FILE * in ;
22  in=fopen(getpath(filenumbering.numts).c_str(), "r") ;
23  while ( in != nullptr )
24  {
25  printf(".") ;
26  filenumbering.numts++ ;
27  fclose(in) ;
28  //printf("%s\n", getpath(filenumbering.numts).c_str()) ;
29  in=fopen(getpath(filenumbering.numts).c_str(), "r") ;
30  }
31  printf("Found %d files\n", filenumbering.numts) ;
32  return filenumbering.numts ;
33  }
34  else
35  return -1;
36  }
37  virtual int get_num_particles () {return -1;}
38  virtual int get_num_contacts () {return -1;}
39  virtual double * get_data([[maybe_unused]] DataValue datavalue, [[maybe_unused]] int dd, [[maybe_unused]] std::string name="") {return nullptr ; }
40  virtual int build_index () {return -1 ;}
41  virtual int read_timestep ([[maybe_unused]] int ts) {return -1 ; }
42  virtual void post_init() {}
43 
44  void set_default_radiusdensity (double radius, double density) {Radius=radius ; Density=density ;}
46  void set_default_density (double density) {Density=density ; }
47  void set_default_superquadric (double a, double b, double c, double pa, double pb, double pc)
48  {is_superquadric = true ; SuperQuadric[0] = a ; SuperQuadric[1] = b ; SuperQuadric[2] = c ; SuperQuadric[3] = pa ; SuperQuadric[4] = pb ; SuperQuadric[5] = pc ;}
49  double get_default_superquadric(int component) {return SuperQuadric[component] ; }
51  {
52  static bool info=true ;
53  if (Radius==-1)
54  {
55  if (info)
56  {
57  printf("ERR: the default radius was requested but not set!!!!! set to 1\n") ;
58  info=false ;
59  }
60  return 1. ;
61  }
62  else
63  return Radius ;
64  }
66  {
67  static bool info=true ;
68  if (Density==-1)
69  {
70  if (info)
71  {
72  printf("ERR: the default density was requested but not set!!!!! set to 1\n") ;
73  info=false ;
74  }
75  return 1. ;
76  }
77  else
78  return Density;
79  }
80 
81  //virtual double * get_data(DataValue, int dd) {return nullptr;}
82 
83  bool is_seekable = false ;
84  bool is_fullymapped = false ;
85  bool is_superquadric = false ;
86  std::vector <std::optional<std::streampos>> mapped_ts ;
87 
88  void build_pospqlpq_from_ids (v2d & contactarray , int idx_id1, int idx_id2, int idx_pospq, int idx_lpq,
89  v2d & particlearray, int idx_pos, int idx_r=-1)
90  {
91  //size_t N = particlearray[idx_pos].size() ;
92  size_t Nc = contactarray[idx_id1].size() ;
93  int d=get_dimension() ;
94  for (int i=0 ; i<d ; i++)
95  {
96  contactarray[idx_pospq+i].resize(Nc) ;
97  contactarray[idx_lpq+i].resize(Nc) ;
98  }
99 
100  for (size_t i=0 ; i<Nc ; i++)
101  {
102  int id1=static_cast<int>(contactarray[idx_id1][i]) ;
103  int id2=static_cast<int>(contactarray[idx_id2][i]) ;
104  double fraction = 0.5 ;
105  for (int dd=0 ; dd<d ; dd++)
106  {
107  contactarray[idx_lpq+dd][i] = particlearray[idx_pos+dd][id1] - particlearray[idx_pos+dd][id2] ;
108  if (idx_r!=-1)
109  {
110  fraction = particlearray[idx_r][id2] / (particlearray[idx_r][id1]+particlearray[idx_r][id2]) ;
111  }
112  contactarray[idx_pospq+dd][i] = particlearray[idx_pos+dd][id2] + fraction * contactarray[idx_lpq+dd][i] ;
113  }
114  }
115  }
116 
117  int clean_contacts (v2d & contactarray, int id1, int id2, int idx_lpq, v2d & particlearray, int idx_r)
118  {
119  // WORK IN PROGRESS
120  printf("NEED TO WORK ON THAT ...") ; return 0 ;
121  /*size_t Nc = contactarray[0].size() ;
122  double dst, rr ;
123  std::vector<bool> rm (Nc,false) ;
124 
125  for (size_t i=0 ; i<Nc ; i++)
126  {
127  dst=0 ;
128  for (int dd=0 ; dd<get_dimension() ; dd++)
129  dst += (contactarray[i][idx_lpq+dd]*contactarray[i][idx_lpq+dd]) ;
130  dst=sqrt(dst) ;
131  rr=particlearray[idx_r][contactarray[i][id1]]+particlearray[idx_r][contactarray[i][id1]] ;
132 
133  }*/
134 
135  }
136 
137  // Filebuilder
138  std::string path ;
139  int curts=-1 ;
140  struct {
141  double initial = 0 ;
142  double delta = 1 ;
143  int numts = -1 ;
144  bool ismultifile = false ;
146  std::string getpath (int ts)
147  {
148  char * tmp = nullptr ;
149  size_t pos = path.find('%') ;
150  for ( ; !isalpha(path[pos]) ; pos++) ;
151  if (path[pos]=='e' || path[pos]=='f' || path[pos]=='g' || path[pos]=='E' || path[pos]=='F' || path[pos]=='G')
152  {
153  int len = snprintf(NULL, 0, path.c_str(), filenumbering.initial+filenumbering.delta*ts) ;
154  tmp = (char*) malloc(len) ;
155  sprintf(tmp, path.c_str(), filenumbering.initial+filenumbering.delta*ts) ;
156  }
157  else if (path[pos]=='d'|| path[pos]=='i' || path[pos]=='u' || path[pos]=='h' || path[pos]=='l' || path[pos]=='j' || path[pos]=='z')
158  {
159  int len = snprintf(NULL, 0, path.c_str(), static_cast<long long int>(filenumbering.initial+filenumbering.delta*ts)) ;
160  tmp = (char*) malloc(len+1) ;
161  sprintf(tmp, path.c_str(), static_cast<long long int>(filenumbering.initial+filenumbering.delta*ts)) ;
162  }
163  else printf("ERR: unknown format specifier in file path: %c.\n", path[pos]) ;
164  std::string res = tmp ;
165  free(tmp) ;
166  return res ;
167  }
168 
169 
170 
171 protected:
172  std::vector<std::pair<double,streampos>> index ;
173 
174 private:
175  double Radius=-1, Density=-1 ;
176  double SuperQuadric[6] = {1,1,1,2,2,2} ;
177 } ;
178 
179 #endif
Definition: Reader.h:10
std::string path
Definition: Reader.h:138
struct Reader::@886 filenumbering
void set_default_density(double density)
Definition: Reader.h:46
double get_default_superquadric(int component)
Definition: Reader.h:49
void set_default_radiusdensity(double radius, double density)
Definition: Reader.h:44
void set_default_radius(double radius)
Definition: Reader.h:45
bool is_fullymapped
Definition: Reader.h:84
void set_default_superquadric(double a, double b, double c, double pa, double pb, double pc)
Definition: Reader.h:47
virtual int get_num_particles()
Definition: Reader.h:37
int curts
Definition: Reader.h:139
double Radius
Definition: Reader.h:175
double SuperQuadric[6]
Definition: Reader.h:176
int numts
Definition: Reader.h:143
virtual int build_index()
Definition: Reader.h:40
std::vector< std::pair< double, streampos > > index
Definition: Reader.h:172
bool is_superquadric
Definition: Reader.h:85
virtual double * get_data([[maybe_unused]] DataValue datavalue, [[maybe_unused]] int dd, [[maybe_unused]] std::string name="")
Definition: Reader.h:39
std::string getpath(int ts)
Definition: Reader.h:146
virtual void post_init()
Definition: Reader.h:42
int clean_contacts(v2d &contactarray, int id1, int id2, int idx_lpq, v2d &particlearray, int idx_r)
Definition: Reader.h:117
void build_pospqlpq_from_ids(v2d &contactarray, int idx_id1, int idx_id2, int idx_pospq, int idx_lpq, v2d &particlearray, int idx_pos, int idx_r=-1)
Definition: Reader.h:88
virtual std::vector< std::vector< double > > get_bounds()
Definition: Reader.h:12
bool is_seekable
Definition: Reader.h:83
virtual int read_timestep([[maybe_unused]] int ts)
Definition: Reader.h:41
double get_default_density()
Definition: Reader.h:65
bool ismultifile
Definition: Reader.h:144
double initial
Definition: Reader.h:141
virtual int get_numts()
Definition: Reader.h:15
virtual int get_num_contacts()
Definition: Reader.h:38
std::vector< std::optional< std::streampos > > mapped_ts
Definition: Reader.h:86
virtual int get_dimension()
Definition: Reader.h:14
virtual std::vector< double > get_minmaxradius()
Definition: Reader.h:13
double delta
Definition: Reader.h:142
double get_default_radius()
Definition: Reader.h:50
double Density
Definition: Reader.h:175
vector< vector< double > > v2d
Definition: Typedefs.h:10
DataValue
Definition: Typedefs.h:19
@ id2
Definition: Typedefs.h:19
@ pos
Definition: Typedefs.h:19
@ id1
Definition: Typedefs.h:19
@ radius
Definition: Typedefs.h:19
uint d
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181