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, P.boundaries) ;
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 || P.timeaverage == AverageType::IntermediateAndPre5) avg=true ;
136  P.read_timestep(ts+P.skipT) ;
137  C->cT = ts ;
138  P.set_data (C->data) ;
139  if (P.window == Windows::RVE)
140  {
141  (static_cast<LibRVE*>(C->Window))->set_volume(P.get_volume()) ;
142  //printf("%g %g\n", P.get_volume(), (static_cast<LibRVE*>(C->Window))->scale) ;
143  }
144 
145  if (!hasdonefirstpass) if (pipeline & Pass::Pass1) C->pass_1() ;
146  if (pipeline & Pass::VelFluct) C->compute_fluc_vel (avg) ;
147  if (pipeline & Pass::RotFluct) C->compute_fluc_rot (avg) ;
148  if (pipeline & Pass::Pass2) C->pass_2() ;
149  if (pipeline & Pass::Pass3) C->pass_3() ;
150  if (pipeline & Pass::Pass4) C->pass_4() ;
151  if ( ! (P.timeaverage == AverageType::Pre5 || P.timeaverage==AverageType::IntermediateAndPre5))
152  if (pipeline & Pass::Pass5) C->pass_5() ;
153  return 0 ;
154 }
155 //------------------------------------------------------------
157 {
158  for (int ts=0 ; ts<P.maxT ; ts++)
159  {
160  P.read_timestep(ts+P.skipT, true) ;
161  C->cT = ts ;
162  P.set_data (C->data) ;
163  C->pass_1() ;
164  // printf("\r") ;
165  }
166  if (P.timeaverage == AverageType::Intermediate || P.timeaverage == AverageType::Both || P.timeaverage == AverageType::IntermediateAndPre5) //Should be automatically verified when the function is called
167  C->mean_time(true) ;
168 
169  return 0 ;
170 }
171 //----------------------------------------------------------
173 {
174  bool hasdonefirstpass = false ;
175  if ((P.timeaverage == AverageType::Intermediate || P.timeaverage == AverageType::Both || P.timeaverage == AverageType::IntermediateAndPre5) &&
176  ((pipeline & Pass::VelFluct) || (pipeline & Pass::RotFluct)))
177  {
178  process_fluct_from_avg() ;
179  hasdonefirstpass = true ;
180  }
181 
182  for (int ts=0 ; ts<P.maxT ; ts++)
183  {
184  printf("\rProcessing %d ", ts) ; fflush(stdout) ;
185  process_timestep(ts+P.skipT, hasdonefirstpass) ;
186  }
187  if (P.timeaverage == AverageType::Final || P.timeaverage == AverageType::Both || P.timeaverage == AverageType::Pre5 || P.timeaverage == AverageType::IntermediateAndPre5)
188  C->mean_time(false) ;
189 
190  if (P.timeaverage == AverageType::Pre5 || P.timeaverage == AverageType::IntermediateAndPre5)
191  {
192  C->cT=0 ;
193  C->pass_5() ;
194  }
195 }
196 //------------------------------------------------------
198 {
199  if (std::find(P.saveformat.begin(), P.saveformat.end(), "netCDF")!=P.saveformat.end()) C->write_netCDF(P.save) ;
200  if (std::find(P.saveformat.begin(), P.saveformat.end(), "vtk")!=P.saveformat.end()) C->write_vtk (P.save) ;
201  if (std::find(P.saveformat.begin(), P.saveformat.end(), "mat")!=P.saveformat.end()) C->write_matlab(P.save, true) ;
202  if (std::find(P.saveformat.begin(), P.saveformat.end(), "numpy")!=P.saveformat.end()) C->write_numpy(P.save, true) ;
203 }
204 //------------------------------------------------------
205 Vector1Djs CoarseGraining::get_result(int ts_abs, std::string field, int component)
206 {
207  int ts = ts_abs - P.skipT ;
208  if (ts != C->cT) {printf("The requested timestep has not been processed.\n"); return {} ; }
209  int idx = C->get_id(field) ;
210  idx += component ;
211  std::vector <double> res ;
212  res.resize(C->CGP.size()) ;
213  for (size_t i = 0 ; i<res.size() ; i++)
214  res[i]=C->CGP[C->idx_FastFirst2SlowFirst(i)].fields[C->cT][idx] ;
215  return to_js_array(res) ;
216 }
217 //-----------------------------------------------------
219 {
220  std::vector<double> res ;
221  //origin
222  res.push_back(C->CGP[0].location[0]) ;
223  res.push_back(C->CGP[0].location[1]) ;
224  res.push_back(C->CGP[0].location[2]) ;
225  //spacing
226  res.push_back(C->dx[0]) ;
227  res.push_back(C->dx[1]) ;
228  res.push_back(C->dx[2]) ;
229  //dimensions
230  res.push_back(C->npt[0]) ;
231  res.push_back(C->npt[1]) ;
232  res.push_back(C->npt[2]) ;
233  return to_js_array(res) ;
234 }
235 //-----------------------------------------------------
237 {
238  std::vector<std::vector<double>> res ; res.resize(4) ;
239  int ts = ts_abs - P.skipT ;
240  P.read_timestep(ts+P.skipT) ;
241 
242  int n = P.get_num_particles() ;
243  for (int i=0 ; i<4 ; i++) res[i].resize(n) ;
244 
245  double *p ;
246  for (int i=0 ; i<3 ; i++)
247  {
248  p = P.get_data(DataValue::pos, i) ;
249  for (int j=0 ; j<n ; j++)
250  res[i][j]=p[j] ;
251  }
252  p = P.get_data(DataValue::radius) ;
253  for (int j=0 ; j<n ; j++)
254  res[3][j]=p[j] ;
255 
256  return to_js_array(res) ;
257 }
258 
259 #ifdef EMSCRIPTEN
260 #ifdef USEBINDINGS
261  #include "em_bindings.h"
262 #endif
263 #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
Definition: CoarseGraining.h:67
Param P
Definition: CoarseGraining.h:70
CoarseGraining()
Definition: CoarseGraining.h:69
void process_all()
Definition: CoarseGraining.h:172
Vector1Djs get_result(int ts, std::string field, int component)
Definition: CoarseGraining.h:205
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:156
Pass pipeline
Definition: CoarseGraining.h:72
Vector2Djs get_spheres(int ts_abs)
Definition: CoarseGraining.h:236
void param_from_json_string(std::string param)
Definition: CoarseGraining.h:85
Vector1Djs get_gridinfo()
Definition: CoarseGraining.h:218
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:197
Main Coarse graining class.
Definition: Coarsing.h:151
Definition: WindowLibrary.h:416
namespace for Niels Lohmann
Definition: json.hpp:20203
Pass
Definition: Coarsing.h:66
@ Pre5
Definition: Coarsing.h:65
@ Final
Definition: Coarsing.h:65
@ IntermediateAndPre5
Definition: Coarsing.h:65
@ Both
Definition: Coarsing.h:65
@ Intermediate
Definition: Coarsing.h:65
@ Pass1
Definition: Coarsing.h:66
@ Pass3
Definition: Coarsing.h:66
@ Pass4
Definition: Coarsing.h:66
@ Pass2
Definition: Coarsing.h:66
@ VelFluct
Definition: Coarsing.h:67
@ RotFluct
Definition: Coarsing.h:67
@ Pass5
Definition: Coarsing.h:66
@ 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:124
std::vector< File > files
Definition: Parameters.h:42
void from_json(json &j)
Definition: IOLiggghts.h:107
void post_init()
Definition: IOLiggghts.h:188