NDDEM
CoarseGraining.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 #include <optional>
10 
11 #ifdef EMSCRIPTEN
12  #include <emscripten.h>
13  #include <emscripten/bind.h>
14  using namespace emscripten ;
15 
16  #ifndef JS_CONVERT_ARRAYS
17  #define JS_CONVERT_ARRAYS
18  using Vector2Djs = emscripten::val ;
19  using Vector1Djs = emscripten::val ;
20 
21  template <typename T>
22  emscripten::val to_js_array(const std::vector<std::vector<T>>& data) {
23  using namespace emscripten;
24  val outer = val::array();
25  for (size_t i = 0; i < data.size(); ++i) {
26  val inner = val::array();
27  for (size_t j = 0; j < data[i].size(); ++j) {
28  inner.set(j, data[i][j]);
29  }
30  outer.set(i, inner);
31  }
32  return outer;
33  }
34  template <typename T>
35  emscripten::val to_js_array(const std::vector<T>& data) {
36  using namespace emscripten;
37  val outer = val::array();
38  for (size_t i = 0; i < data.size(); ++i) {
39  outer.set(i, data[i]);
40  }
41  return outer;
42  }
43  #endif
44 #else
45  using Vector2Djs = std::vector<std::vector<double>> ;
46  using Vector1Djs = std::vector<double> ;
47  template <typename T> std::vector<std::vector<T>> to_js_array(std::vector<std::vector<T>>& data) {return data ; }
48  template <typename T> std::vector<T> to_js_array(std::vector<T>& data) {return data ; }
49 #endif
50 
51 #include "gzip.hpp"
52 //#include "termcolor.hpp"
53 #include <boost/iostreams/filtering_streambuf.hpp>
54 #include <boost/iostreams/stream.hpp>
55 //#include "json.hpp"
56 #include "json_parser.h"
57 #include "Typedefs.h"
58 #include "Coarsing.h"
59 #include "Reader.h"
60 #include "Reader-Liggghts.h"
61 #include "Reader-NDDEM.h"
62 #include "Reader-interactive.h"
63 #include "Reader-Mercury.h"
64 #include "Reader-Yade.h"
65 #include "Parameters.h"
66 
68 public:
70  Param P ;
71  Coarsing * C = nullptr;
73 
74  void setup_CG () ;
75 
76  int process_timestep (int ts, bool hasdonefirstpass=false) ;
77  int process_fluct_from_avg() ;
78  void process_all ();
79  void write () ;
80  Vector1Djs get_result(int ts, std::string field, int component) ;
81  Vector1Djs get_gridinfo() ;
82  Vector2Djs get_spheres(int ts_abs) ;
83 
84  // Expose functions from member classes for js access
85  void param_from_json_string (std::string param) { json jsonparam =json::parse(param) ; return P.from_json(jsonparam) ; }
86  Vector2Djs param_get_bounds (int file = 0) {auto tmp = P.files[file].reader->get_bounds() ; return to_js_array(tmp) ; }
87  Vector1Djs param_get_minmaxradius (int file = 0) {auto tmp = P.files[file].reader->get_minmaxradius() ; return to_js_array(tmp) ; }
88  int param_get_numts(int file = 0) {return P.files[file].reader->get_numts(); }
89  void debug () {/*char resc[5000] ; sprintf(resc, "%d %X", P.files.size(), P.files[0].reader) ; return resc ; */printf("BLAAAA\n") ; }
90  int param_read_timestep(int n) {return P.read_timestep(n) ; }
91  void param_post_init () {return P.post_init() ; }
92 
93  /*std::string testing(std::string input) {
94  FILE * in = fopen(input.c_str(), "r") ;
95  printf("[%s]", input.c_str()) ;
96  if (in != nullptr)
97  {
98  printf("Starting reading\n") ;
99  while (!feof(in))
100  fscanf(in,"%*c") ;
101  fclose(in) ;
102  printf("Finished reading\n") ;
103  }
104  return(input) ;
105  } */
106 
107 } ;
108 //=========================================================
109 
111 {
112  //P.verify() ;
113  if (C != nullptr)
114  delete C ;
115 
116  C = new Coarsing (P.dim, P.boxes, P.boundaries, P.maxT) ;
117  for (auto i : P.extrafields)
118  C->add_extra_field(i.name, i.order, i.type) ;
119  if (P.window == Windows::LucyND_Periodic)
120  C->setWindow(P.window, P.windowsize, P.periodicity, P.boxes, P.delta) ;
121  else
122  C->setWindow(P.window,P.windowsize) ;
123  pipeline = C->set_flags(P.flags) ;
124  auto extrafieldmap = C->grid_setfields() ;
125  C->grid_neighbour() ;
126 
127  C->cT=-1 ;
128 }
129 //-----------------------------------------
130 int CoarseGraining::process_timestep (int ts_abs, bool hasdonefirstpass)
131 {
132  int ts = ts_abs - P.skipT ;
133 
134  bool avg=false ;
135  if (P.timeaverage == AverageType::Intermediate || P.timeaverage == AverageType::Both) avg=true ;
136  P.read_timestep(ts+P.skipT) ;
137  C->cT = ts ;
138  P.set_data (C->data) ;
139 
140  if (!hasdonefirstpass) if (pipeline & Pass::Pass1) C->pass_1() ;
141  if (pipeline & Pass::VelFluct) C->compute_fluc_vel (avg) ;
142  if (pipeline & Pass::RotFluct) C->compute_fluc_rot (avg) ;
143  if (pipeline & Pass::Pass2) C->pass_2() ;
144  if (pipeline & Pass::Pass3) C->pass_3() ;
145  if (pipeline & Pass::Pass4) C->pass_4() ;
146  if (pipeline & Pass::Pass5) C->pass_5() ;
147  return 0 ;
148 }
149 //------------------------------------------------------------
151 {
152  for (int ts=0 ; ts<P.maxT ; ts++)
153  {
154  P.read_timestep(ts+P.skipT, true) ;
155  C->cT = ts ;
156  P.set_data (C->data) ;
157  C->pass_1() ;
158  // printf("\r") ;
159  }
160  if (P.timeaverage == AverageType::Intermediate || P.timeaverage == AverageType::Both) //Should be automatically verified when the function is called
161  C->mean_time(true) ;
162 
163  return 0 ;
164 }
165 //----------------------------------------------------------
167 {
168  bool hasdonefirstpass = false ;
169  if ((P.timeaverage == AverageType::Intermediate || P.timeaverage == AverageType::Both) &&
170  ((pipeline & Pass::VelFluct) || (pipeline & Pass::RotFluct)))
171  {
172  process_fluct_from_avg() ;
173  hasdonefirstpass = true ;
174  }
175 
176  for (int ts=0 ; ts<P.maxT ; ts++)
177  {
178  printf("\rProcessing %d ", ts) ; fflush(stdout) ;
179  process_timestep(ts+P.skipT, hasdonefirstpass) ;
180  }
181  if (P.timeaverage == AverageType::Final || P.timeaverage == AverageType::Both)
182  C->mean_time(false) ;
183 }
184 //------------------------------------------------------
186 {
187  if (std::find(P.saveformat.begin(), P.saveformat.end(), "netCDF")!=P.saveformat.end()) C->write_netCDF(P.save) ;
188  if (std::find(P.saveformat.begin(), P.saveformat.end(), "vtk")!=P.saveformat.end()) C->write_vtk (P.save) ;
189  if (std::find(P.saveformat.begin(), P.saveformat.end(), "mat")!=P.saveformat.end()) C->write_matlab(P.save, true) ;
190  if (std::find(P.saveformat.begin(), P.saveformat.end(), "numpy")!=P.saveformat.end()) C->write_numpy(P.save, true) ;
191 }
192 //------------------------------------------------------
193 Vector1Djs CoarseGraining::get_result(int ts_abs, std::string field, int component)
194 {
195  int ts = ts_abs - P.skipT ;
196  if (ts != C->cT) {printf("The requested timestep has not been processed.\n"); return {} ; }
197  int idx = C->get_id(field) ;
198  idx += component ;
199  std::vector <double> res ;
200  res.resize(C->CGP.size()) ;
201  for (size_t i = 0 ; i<res.size() ; i++)
202  res[i]=C->CGP[C->idx_FastFirst2SlowFirst(i)].fields[C->cT][idx] ;
203  return to_js_array(res) ;
204 }
205 //-----------------------------------------------------
207 {
208  std::vector<double> res ;
209  //origin
210  res.push_back(C->CGP[0].location[0]) ;
211  res.push_back(C->CGP[0].location[1]) ;
212  res.push_back(C->CGP[0].location[2]) ;
213  //spacing
214  res.push_back(C->dx[0]) ;
215  res.push_back(C->dx[1]) ;
216  res.push_back(C->dx[2]) ;
217  //dimensions
218  res.push_back(C->npt[0]) ;
219  res.push_back(C->npt[1]) ;
220  res.push_back(C->npt[2]) ;
221  return to_js_array(res) ;
222 }
223 //-----------------------------------------------------
225 {
226  std::vector<std::vector<double>> res ; res.resize(4) ;
227  int ts = ts_abs - P.skipT ;
228  P.read_timestep(ts+P.skipT) ;
229 
230  int n = P.get_num_particles() ;
231  for (int i=0 ; i<4 ; i++) res[i].resize(n) ;
232 
233  double *p ;
234  for (int i=0 ; i<3 ; i++)
235  {
236  p = P.get_data(DataValue::pos, i) ;
237  for (int j=0 ; j<n ; j++)
238  res[i][j]=p[j] ;
239  }
240  p = P.get_data(DataValue::radius) ;
241  for (int j=0 ; j<n ; j++)
242  res[3][j]=p[j] ;
243 
244  return to_js_array(res) ;
245 }
246 
247 #ifdef EMSCRIPTEN
248 #ifdef USEBINDINGS
249  #include "em_bindings.h"
250 #endif
251 #endif
std::vector< std::vector< T > > to_js_array(std::vector< std::vector< T >> &data)
Definition: CoarseGraining.h:47
std::vector< std::vector< double > > Vector2Djs
Definition: DEMND.h:78
std::vector< double > Vector1Djs
Definition: DEMND.h:79
@ LucyND_Periodic
Definition: CoarseGraining.h:67
Param P
Definition: CoarseGraining.h:70
CoarseGraining()
Definition: CoarseGraining.h:69
void process_all()
Definition: CoarseGraining.h:166
Vector1Djs get_result(int ts, std::string field, int component)
Definition: CoarseGraining.h:193
Vector2Djs param_get_bounds(int file=0)
Definition: CoarseGraining.h:86
int process_timestep(int ts, bool hasdonefirstpass=false)
Definition: CoarseGraining.h:130
void param_post_init()
Definition: CoarseGraining.h:91
Vector1Djs param_get_minmaxradius(int file=0)
Definition: CoarseGraining.h:87
int param_get_numts(int file=0)
Definition: CoarseGraining.h:88
int process_fluct_from_avg()
Definition: CoarseGraining.h:150
Pass pipeline
Definition: CoarseGraining.h:72
Vector2Djs get_spheres(int ts_abs)
Definition: CoarseGraining.h:224
void param_from_json_string(std::string param)
Definition: CoarseGraining.h:85
Vector1Djs get_gridinfo()
Definition: CoarseGraining.h:206
int param_read_timestep(int n)
Definition: CoarseGraining.h:90
void setup_CG()
Definition: CoarseGraining.h:110
void debug()
Definition: CoarseGraining.h:89
void write()
Definition: CoarseGraining.h:185
Main Coarse graining class.
Definition: Coarsing.h:141
namespace for Niels Lohmann
Definition: json.hpp:20203
Pass
Definition: Coarsing.h:61
@ Final
Definition: Coarsing.h:60
@ Both
Definition: Coarsing.h:60
@ Intermediate
Definition: Coarsing.h:60
@ Pass1
Definition: Coarsing.h:61
@ Pass3
Definition: Coarsing.h:61
@ Pass4
Definition: Coarsing.h:61
@ Pass2
Definition: Coarsing.h:61
@ VelFluct
Definition: Coarsing.h:62
@ RotFluct
Definition: Coarsing.h:62
@ Pass5
Definition: Coarsing.h:61
@ pos
Definition: Typedefs.h:19
@ radius
Definition: Typedefs.h:19
Contains the parameters of the simulation & CG.
Definition: IOLiggghts.h:67
int read_timestep(int ts, bool particleonly=false)
Definition: Parameters.h:116
std::vector< File > files
Definition: Parameters.h:41
void from_json(json &j)
Definition: IOLiggghts.h:107
void post_init()
Definition: IOLiggghts.h:188