GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Sensor.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _LibUtil_Sensor_hpp_
31 #define _LibUtil_Sensor_hpp_
32 
33 #include <iostream>
34 #include <functional>
35 #include <fstream>
36 #include <string>
37 #include <vector>
38 #include <map>
39 #include <set>
40 
41 
42 #include "GranOO3/Geom/Vector.hpp"
44 #include "GranOO3/Geom/Tensor.hpp"
46 #include "GranOO3/Core/Problem.hpp"
47 #include "GranOO3/Math/Stat.hpp"
48 
49 #define CONSTRUCTOR_SPECIFICATION(OPT1, OPT2, OPT3, OPT4, OPT5) \
50  template <typename A, typename T, class R> \
51  inline void \
52  Sensor::new_object(OPT1 A &r, OPT2 T OPT3 (R::*f)() OPT4, OPT5 label) \
53  { \
54  std::function<T ()> bf(std::bind(f, std::ref(r))); \
55  Sensor::instanciate(bf, label); \
56  }
57 
58 namespace GranOO3
59 {
60  namespace Core
61  {
62  //
63  // A purely template class (don't look for Sensor.cpp !!) designed to implement
64  // the concept of "Numerical Sensor".
65  // TSensor constructor takes 2 arguments :
66  // - an object on wich some measurement will be taken (tanks to an adhoc method)
67  // - a pointer to a method of the object class, which is able to measure an return
68  // some parameter value.
69 
70 
71  // Class Sensor :
72  // base classe, with only static members.
73  // Has a table of all conctructed sensors.
74  //
75  // Sensor::acquire_all_sensor_and_write() : (static method)
76  // scans all sensors, takes a measurement and writes it in the outFile.
77  //
78  // Sensor::write_data_in_file_out(const std::string &) (static method)
79  // write some specific data (comment, values, ...) in the outFile
80 
81  // Class TSensor : purely templates (T) Sensor .
82  // template parameters are :
83  // - class T : the type of the object on which measurements will be taken,
84  // - class R : the return type of the measurement fucntion.
85  //
86  // Class TSensor constructor takes 2 arguments :
87  // - const T& : an object on wich some measurement will be taken
88  // - const R & (T::*f)() const : a pointer to a const method of class T
89  // that returns a const R & value.
90 
91 
93  {
94  public:
95  ArraySensor(Math::Stat&, const std::string & label);
96  ~ArraySensor();
97 
98  void write(std::ostream&);
99  static std::set<ArraySensor*> get_all() {return _all;}
100 
101  private:
102  static std::set<ArraySensor*> _all;
103 
104  private:
105  //Data
106  const std::string _label;
108  };
109 
110 
111  class Sensor
112  {
113  private:
114  static double val_from_std_vector(std::function<std::vector<double>()>& f, std::size_t);
115 
116  public:
117  //Methods
118  Sensor();
119  virtual ~Sensor();
120 
121  static void open_file_out(const std::string & file_name, bool force = false);
122  static void close_file_out();
123  static void flush_file_out();
124 
125 
126  static void acquire_all_sensor_and_write();
127  static void set_header_column_shape(const bool isColumn);
128  static void write_data_in_file_out(const std::string &);
129  static std::ofstream & get_output_stream() {return _fout;};
130  static bool is_file_out_open();
131  static std::string get_file_name() {return _file_name;};
132  static void reset();
133 
134  virtual const std::string& get_label() const = 0;
135 
136  //Static method to create new sensors with one label
137  template <typename A, typename T, class R> static void new_object( A & r, T (R::*f)(), const std::string & label);
138  template <typename A, typename T, class R> static void new_object(const A & r, T (R::*f)() const, const std::string & label);
139  template <typename A, typename T, class R> static void new_object( A &r, const T (R::*f)(), const std::string& label);
140  template <typename A, typename T, class R> static void new_object(const A &r, const T (R::*f)() const, const std::string& label);
141 
142  //Static method to create new sensors with one vector of labels
143  template <typename A, typename T, class R> static void new_object( A & r, T (R::*f)(), const std::vector<std::string>&labelVector);
144  template <typename A, typename T, class R> static void new_object(const A & r, T (R::*f)() const, const std::vector<std::string>&labelVector);
145  template <typename A, typename T, class R> static void new_object( A &r, const T (R::*f)(), const std::vector<std::string>& labelVector);
146  template <typename A, typename T, class R> static void new_object(const A &r, const T (R::*f)() const, const std::vector<std::string>& labelVector);
147 
148  template <typename T> static void new_object(const T (*f)(), const std::string& label);
149  template <typename T> static void new_object(T (*f)(), const std::string& label);
150  template <typename T> static void new_object(const T& (*f)(), const std::string& label);
151  template <typename T> static void new_object(T& (*f)(), const std::string& label);
152 
153  template <typename T> static void new_object(std::function<T ()> f, const std::string& label);
154 
155  template <typename T> static void instanciate(std::function<T ()>& f, const std::string& label);
156  template <typename T> static void instanciate(std::function<T ()>& f, const std::vector<std::string>& labelVector);
157 
158  protected:
159  static int add(Sensor* s) {_sensor_table.push_back(s); return _rank++;};
160  static void desactivate(Sensor* s) {_sensor_table[s->get_rank()] = 0;};
161  virtual void acquire_and_write() = 0;
162  virtual int get_rank() const = 0;
163  static void write_header();
164  static std::ofstream _fout;
165 
166  private:
167  static std::vector<Sensor* > _sensor_table;
168  static std::string _file_name;
169  static int _rank;
170  static bool _header_written;
171  static bool _header_column_shape;
172  };
173 
174 
175 
176 
177  template<typename T>
178  class SensorT : public Sensor
179  {
180  public:
181  SensorT(std::function<T ()>&, const std::string & label = std::string(""));
182  ~SensorT() {desactivate(this);};
183 
184  void acquire_and_write();
185  int get_rank() const {return _rank;};
186  const std::string& get_label() const {return _label;};
187 
188  private:
189  //Data
190  const std::string _label;
191  const int _rank;
192  std::function<T ()> _f;
193  };
194 
195  //Obsolete class, plz use SensorT
196  template<class T, class R>
197  class TSensor : public Sensor
198  {
199  public:
200  //Methods
201  TSensor(const T &t, const R (T::*f)() const, const std::string & label = std::string(""));
203  desactivate(this);
204  };
205 
206  void acquire_and_write();
207  int get_rank() const {
208  return _rank;
209  };
210  const std::string& get_label() const {
211  return _label;
212  };
213 
214  private:
215  private:
216  //Data
217  const std::string _label;
218  const T &_t;
219  const R (T::*_f)() const;
220  const int _rank;
221 
222  };
223 
224  CONSTRUCTOR_SPECIFICATION(, , , , const std::string&)
226  CONSTRUCTOR_SPECIFICATION(, const, , , const std::string&)
227  CONSTRUCTOR_SPECIFICATION(const, const, , const, const std::string&)
228 
229  CONSTRUCTOR_SPECIFICATION(, , , , const std::vector<std::string>&)
230  CONSTRUCTOR_SPECIFICATION(const, , , const, const std::vector<std::string>&)
231  CONSTRUCTOR_SPECIFICATION(, const, , , const std::vector<std::string>&)
232  CONSTRUCTOR_SPECIFICATION(const, const, , const, const std::vector<std::string>&)
233 
234 
235  inline double
236  Sensor::val_from_std_vector(std::function<std::vector<double>()>& f, std::size_t index) {
237  std::vector<double> vec = f();
238  return vec.at(index);
239  }
240 
241  template <typename T>
242  inline void
243  Sensor::new_object(const T (*f)(), const std::string& label) {
244  std::function<T ()> bf(f);
245  Sensor::instanciate(bf, label);
246  }
247 
248  template <typename T>
249  inline void
250  Sensor::new_object(T (*f)(), const std::string& label) {
251  std::function<T ()> bf(f);
252  Sensor::instanciate(bf, label);
253  }
254 
255  template <typename T>
256  inline void
257  Sensor::new_object(const T& (*f)(), const std::string& label) {
258  std::function<T ()> bf(f);
259  Sensor::instanciate(bf, label);
260  }
261 
262  template <typename T>
263  inline void
264  Sensor::new_object(T& (*f)(), const std::string& label) {
265  std::function<T ()> bf(f);
266  Sensor::instanciate(bf, label);
267  }
268 
269  template <typename T>
270  inline void
271  Sensor::new_object(std::function<T ()> f, const std::string& label) {
272  Sensor::instanciate(f, label);
273  }
274 
275  // specialization for methods which return a Geom::Vector
276  template<>
277  inline void
278  Sensor::instanciate<Geom::Vector >(std::function< Geom::Vector ()>& f, const std::string& label) {
279  const std::string label_x = label + "_X";
280  const std::string label_y = label + "_Y";
281  const std::string label_z = label + "_Z";
282 
283  const double& (Geom::Vector::*FctX)() const = &Geom::Vector::x;
284  const double& (Geom::Vector::*FctY)() const = &Geom::Vector::y;
285  const double& (Geom::Vector::*FctZ)() const = &Geom::Vector::z;
286  std::function<double ()> bf_x = std::bind(FctX, std::bind(f));
287  std::function<double ()> bf_y = std::bind(FctY, std::bind(f));
288  std::function<double ()> bf_z = std::bind(FctZ, std::bind(f));
289 
290  new SensorT<double>(bf_x, label_x);
291  new SensorT<double>(bf_y, label_y);
292  new SensorT<double>(bf_z, label_z);
293  }
294 
295  // specialization for methods which return a Geom::Vector&
296  template<>
297  inline void
298  Sensor::instanciate<Geom::Vector& >(std::function< Geom::Vector& ()>& f, const std::string& label) {
299  const std::string label_x = label + "_X";
300  const std::string label_y = label + "_Y";
301  const std::string label_z = label + "_Z";
302 
303  const double& (Geom::Vector::*FctX)() const = &Geom::Vector::x;
304  const double& (Geom::Vector::*FctY)() const = &Geom::Vector::y;
305  const double& (Geom::Vector::*FctZ)() const = &Geom::Vector::z;
306  std::function<double ()> bf_x = std::bind(FctX, std::bind(f));
307  std::function<double ()> bf_y = std::bind(FctY, std::bind(f));
308  std::function<double ()> bf_z = std::bind(FctZ, std::bind(f));
309 
310  new SensorT<double>(bf_x, label_x);
311  new SensorT<double>(bf_y, label_y);
312  new SensorT<double>(bf_z, label_z);
313  }
314 
315  // specialization for methods which return a Geom::Vector&
316  template<>
317  inline void
318  Sensor::instanciate<const Geom::Vector& >(std::function< const Geom::Vector& ()>& f, const std::string& label) {
319  const std::string label_x = label + "_X";
320  const std::string label_y = label + "_Y";
321  const std::string label_z = label + "_Z";
322 
323  const double& (Geom::Vector::*FctX)() const = &Geom::Vector::x;
324  const double& (Geom::Vector::*FctY)() const = &Geom::Vector::y;
325  const double& (Geom::Vector::*FctZ)() const = &Geom::Vector::z;
326  std::function<double ()> bf_x = std::bind(FctX, std::bind(f));
327  std::function<double ()> bf_y = std::bind(FctY, std::bind(f));
328  std::function<double ()> bf_z = std::bind(FctZ, std::bind(f));
329 
330  new SensorT<double>(bf_x, label_x);
331  new SensorT<double>(bf_y, label_y);
332  new SensorT<double>(bf_z, label_z);
333  }
334 
335  // specialization for methods which return a Geom::Quaternion
336  template<>
337  inline void
338  Sensor::instanciate<Geom::Quaternion >(std::function< Geom::Quaternion ()>& f, const std::string& label) {
339  const std::string label_x = label + "_X";
340  const std::string label_y = label + "_Y";
341  const std::string label_z = label + "_Z";
342  const std::string label_r = label + "_R";
343 
344  const double& (Geom::Quaternion::*FctX)() const = &Geom::Quaternion::x;
345  const double& (Geom::Quaternion::*FctY)() const = &Geom::Quaternion::y;
346  const double& (Geom::Quaternion::*FctZ)() const = &Geom::Quaternion::z;
347  const double& (Geom::Quaternion::*FctR)() const = &Geom::Quaternion::r;
348  std::function<double ()> bf_x = std::bind(FctX, std::bind(f));
349  std::function<double ()> bf_y = std::bind(FctY, std::bind(f));
350  std::function<double ()> bf_z = std::bind(FctZ, std::bind(f));
351  std::function<double ()> bf_r = std::bind(FctR, std::bind(f));
352 
353  new SensorT<double>(bf_x, label_x);
354  new SensorT<double>(bf_y, label_y);
355  new SensorT<double>(bf_z, label_z);
356  new SensorT<double>(bf_r, label_r);
357  }
358 
359  // specialization for methods which return a Geom::Quaternion&
360  template<>
361  inline void
362  Sensor::instanciate<Geom::Quaternion& >(std::function< Geom::Quaternion& ()>& f, const std::string& label) {
363  const std::string label_x = label + "_X";
364  const std::string label_y = label + "_Y";
365  const std::string label_z = label + "_Z";
366  const std::string label_r = label + "_R";
367 
368  const double& (Geom::Quaternion::*FctX)() const = &Geom::Quaternion::x;
369  const double& (Geom::Quaternion::*FctY)() const = &Geom::Quaternion::y;
370  const double& (Geom::Quaternion::*FctZ)() const = &Geom::Quaternion::z;
371  const double& (Geom::Quaternion::*FctR)() const = &Geom::Quaternion::r;
372  std::function<double ()> bf_x = std::bind(FctX, std::bind(f));
373  std::function<double ()> bf_y = std::bind(FctY, std::bind(f));
374  std::function<double ()> bf_z = std::bind(FctZ, std::bind(f));
375  std::function<double ()> bf_r = std::bind(FctR, std::bind(f));
376 
377  new SensorT<double>(bf_x, label_x);
378  new SensorT<double>(bf_y, label_y);
379  new SensorT<double>(bf_z, label_z);
380  new SensorT<double>(bf_r, label_r);
381  }
382 
383  // specialization for methods which return a Geom::Quaternion&
384  template<>
385  inline void
386  Sensor::instanciate<const Geom::Quaternion& >(std::function< const Geom::Quaternion& ()>& f, const std::string& label) {
387  const std::string label_x = label + "_X";
388  const std::string label_y = label + "_Y";
389  const std::string label_z = label + "_Z";
390  const std::string label_r = label + "_R";
391 
392  const double& (Geom::Quaternion::*FctX)() const = &Geom::Quaternion::x;
393  const double& (Geom::Quaternion::*FctY)() const = &Geom::Quaternion::y;
394  const double& (Geom::Quaternion::*FctZ)() const = &Geom::Quaternion::z;
395  const double& (Geom::Quaternion::*FctR)() const = &Geom::Quaternion::r;
396  std::function<double ()> bf_x = std::bind(FctX, std::bind(f));
397  std::function<double ()> bf_y = std::bind(FctY, std::bind(f));
398  std::function<double ()> bf_z = std::bind(FctZ, std::bind(f));
399  std::function<double ()> bf_r = std::bind(FctR, std::bind(f));
400 
401  new SensorT<double>(bf_x, label_x);
402  new SensorT<double>(bf_y, label_y);
403  new SensorT<double>(bf_z, label_z);
404  new SensorT<double>(bf_r, label_r);
405  }
406 
407  // specialization for methods which return a Geom::Tensor
408  template<>
409  inline void
410  Sensor::instanciate<Geom::Tensor >(std::function< Geom::Tensor ()>& f, const std::string& label) {
411  const std::string label_xx = label + "_XX";
412  const std::string label_yy = label + "_YY";
413  const std::string label_zz = label + "_ZZ";
414 
415  const std::string label_xy = label + "_XY";
416  const std::string label_yx = label + "_YX";
417 
418  const std::string label_xz = label + "_XZ";
419  const std::string label_zx = label + "_ZX";
420 
421  const std::string label_yz = label + "_YZ";
422  const std::string label_zy = label + "_ZY";
423 
424 
425 
426  const double& (Geom::Tensor::*FctXX)() const = &Geom::Tensor::xx;
427  const double& (Geom::Tensor::*FctYY)() const = &Geom::Tensor::yy;
428  const double& (Geom::Tensor::*FctZZ)() const = &Geom::Tensor::zz;
429 
430  const double& (Geom::Tensor::*FctXY)() const = &Geom::Tensor::xy;
431  const double& (Geom::Tensor::*FctYX)() const = &Geom::Tensor::yx;
432 
433  const double& (Geom::Tensor::*FctXZ)() const = &Geom::Tensor::xz;
434  const double& (Geom::Tensor::*FctZX)() const = &Geom::Tensor::zx;
435 
436  const double& (Geom::Tensor::*FctYZ)() const = &Geom::Tensor::yz;
437  const double& (Geom::Tensor::*FctZY)() const = &Geom::Tensor::zy;
438 
439 
440  std::function<double ()> bf_xx = std::bind(FctXX, std::bind(f));
441  std::function<double ()> bf_yy = std::bind(FctYY, std::bind(f));
442  std::function<double ()> bf_zz = std::bind(FctZZ, std::bind(f));
443 
444  std::function<double ()> bf_xy = std::bind(FctXY, std::bind(f));
445  std::function<double ()> bf_yx = std::bind(FctYX, std::bind(f));
446 
447  std::function<double ()> bf_xz = std::bind(FctXZ, std::bind(f));
448  std::function<double ()> bf_zx = std::bind(FctZX, std::bind(f));
449 
450  std::function<double ()> bf_yz = std::bind(FctYZ, std::bind(f));
451  std::function<double ()> bf_zy = std::bind(FctZY, std::bind(f));
452 
453  new SensorT<double>(bf_xx, label_xx);
454  new SensorT<double>(bf_yy, label_yy);
455  new SensorT<double>(bf_zz, label_zz);
456 
457  new SensorT<double>(bf_xy, label_xy);
458  new SensorT<double>(bf_yx, label_yx);
459 
460  new SensorT<double>(bf_xz, label_xz);
461  new SensorT<double>(bf_zx, label_zx);
462 
463  new SensorT<double>(bf_yz, label_yz);
464  new SensorT<double>(bf_zy, label_zy);
465 
466  }
467 
468  // specialization for methods which return a Geom::Tensor&
469  template<>
470  inline void
471  Sensor::instanciate<Geom::Tensor& >(std::function< Geom::Tensor& ()>& f, const std::string& label) {
472  const std::string label_xx = label + "_XX";
473  const std::string label_yy = label + "_YY";
474  const std::string label_zz = label + "_ZZ";
475 
476  const std::string label_xy = label + "_XY";
477  const std::string label_yx = label + "_YX";
478 
479  const std::string label_xz = label + "_XZ";
480  const std::string label_zx = label + "_ZX";
481 
482  const std::string label_yz = label + "_YZ";
483  const std::string label_zy = label + "_ZY";
484 
485 
486 
487  const double& (Geom::Tensor::*FctXX)() const = &Geom::Tensor::xx;
488  const double& (Geom::Tensor::*FctYY)() const = &Geom::Tensor::yy;
489  const double& (Geom::Tensor::*FctZZ)() const = &Geom::Tensor::zz;
490 
491  const double& (Geom::Tensor::*FctXY)() const = &Geom::Tensor::xy;
492  const double& (Geom::Tensor::*FctYX)() const = &Geom::Tensor::yx;
493 
494  const double& (Geom::Tensor::*FctXZ)() const = &Geom::Tensor::xz;
495  const double& (Geom::Tensor::*FctZX)() const = &Geom::Tensor::zx;
496 
497  const double& (Geom::Tensor::*FctYZ)() const = &Geom::Tensor::yz;
498  const double& (Geom::Tensor::*FctZY)() const = &Geom::Tensor::zy;
499 
500 
501  std::function<double ()> bf_xx = std::bind(FctXX, std::bind(f));
502  std::function<double ()> bf_yy = std::bind(FctYY, std::bind(f));
503  std::function<double ()> bf_zz = std::bind(FctZZ, std::bind(f));
504 
505  std::function<double ()> bf_xy = std::bind(FctXY, std::bind(f));
506  std::function<double ()> bf_yx = std::bind(FctYX, std::bind(f));
507 
508  std::function<double ()> bf_xz = std::bind(FctXZ, std::bind(f));
509  std::function<double ()> bf_zx = std::bind(FctZX, std::bind(f));
510 
511  std::function<double ()> bf_yz = std::bind(FctYZ, std::bind(f));
512  std::function<double ()> bf_zy = std::bind(FctZY, std::bind(f));
513 
514  new SensorT<double>(bf_xx, label_xx);
515  new SensorT<double>(bf_yy, label_yy);
516  new SensorT<double>(bf_zz, label_zz);
517 
518  new SensorT<double>(bf_xy, label_xy);
519  new SensorT<double>(bf_yx, label_yx);
520 
521  new SensorT<double>(bf_xz, label_xz);
522  new SensorT<double>(bf_zx, label_zx);
523 
524  new SensorT<double>(bf_yz, label_yz);
525  new SensorT<double>(bf_zy, label_zy);
526  }
527 
528 
529  // specialization for methods which return a const Geom::Tensor&
530  template<>
531  inline void
532  Sensor::instanciate<const Geom::Tensor& >(std::function< const Geom::Tensor& ()>& f, const std::string& label) {
533  const std::string label_xx = label + "_XX";
534  const std::string label_yy = label + "_YY";
535  const std::string label_zz = label + "_ZZ";
536 
537  const std::string label_xy = label + "_XY";
538  const std::string label_yx = label + "_YX";
539 
540  const std::string label_xz = label + "_XZ";
541  const std::string label_zx = label + "_ZX";
542 
543  const std::string label_yz = label + "_YZ";
544  const std::string label_zy = label + "_ZY";
545 
546 
547 
548  const double& (Geom::Tensor::*FctXX)() const = &Geom::Tensor::xx;
549  const double& (Geom::Tensor::*FctYY)() const = &Geom::Tensor::yy;
550  const double& (Geom::Tensor::*FctZZ)() const = &Geom::Tensor::zz;
551 
552  const double& (Geom::Tensor::*FctXY)() const = &Geom::Tensor::xy;
553  const double& (Geom::Tensor::*FctYX)() const = &Geom::Tensor::yx;
554 
555  const double& (Geom::Tensor::*FctXZ)() const = &Geom::Tensor::xz;
556  const double& (Geom::Tensor::*FctZX)() const = &Geom::Tensor::zx;
557 
558  const double& (Geom::Tensor::*FctYZ)() const = &Geom::Tensor::yz;
559  const double& (Geom::Tensor::*FctZY)() const = &Geom::Tensor::zy;
560 
561 
562  std::function<double ()> bf_xx = std::bind(FctXX, std::bind(f));
563  std::function<double ()> bf_yy = std::bind(FctYY, std::bind(f));
564  std::function<double ()> bf_zz = std::bind(FctZZ, std::bind(f));
565 
566  std::function<double ()> bf_xy = std::bind(FctXY, std::bind(f));
567  std::function<double ()> bf_yx = std::bind(FctYX, std::bind(f));
568 
569  std::function<double ()> bf_xz = std::bind(FctXZ, std::bind(f));
570  std::function<double ()> bf_zx = std::bind(FctZX, std::bind(f));
571 
572  std::function<double ()> bf_yz = std::bind(FctYZ, std::bind(f));
573  std::function<double ()> bf_zy = std::bind(FctZY, std::bind(f));
574 
575  new SensorT<double>(bf_xx, label_xx);
576  new SensorT<double>(bf_yy, label_yy);
577  new SensorT<double>(bf_zz, label_zz);
578 
579  new SensorT<double>(bf_xy, label_xy);
580  new SensorT<double>(bf_yx, label_yx);
581 
582  new SensorT<double>(bf_xz, label_xz);
583  new SensorT<double>(bf_zx, label_zx);
584 
585  new SensorT<double>(bf_yz, label_yz);
586  new SensorT<double>(bf_zy, label_zy);
587  }
588 
589  // specialization for methods which return a Geom::SymTensor
590  template<>
591  inline void
592  Sensor::instanciate<Geom::SymTensor >(std::function< Geom::SymTensor ()>& f, const std::string& label) {
593  const std::string label_xx = label + "_XX";
594  const std::string label_yy = label + "_YY";
595  const std::string label_zz = label + "_ZZ";
596 
597  const std::string label_xy = label + "_XY";
598  const std::string label_yx = label + "_YX";
599 
600  const std::string label_xz = label + "_XZ";
601  const std::string label_zx = label + "_ZX";
602 
603  const std::string label_yz = label + "_YZ";
604  const std::string label_zy = label + "_ZY";
605 
606 
607 
608  const double& (Geom::SymTensor::*FctXX)() const = &Geom::SymTensor::xx;
609  const double& (Geom::SymTensor::*FctYY)() const = &Geom::SymTensor::yy;
610  const double& (Geom::SymTensor::*FctZZ)() const = &Geom::SymTensor::zz;
611 
612  const double& (Geom::SymTensor::*FctXY)() const = &Geom::SymTensor::xy;
613  const double& (Geom::SymTensor::*FctYX)() const = &Geom::SymTensor::yx;
614 
615  const double& (Geom::SymTensor::*FctXZ)() const = &Geom::SymTensor::xz;
616  const double& (Geom::SymTensor::*FctZX)() const = &Geom::SymTensor::zx;
617 
618  const double& (Geom::SymTensor::*FctYZ)() const = &Geom::SymTensor::yz;
619  const double& (Geom::SymTensor::*FctZY)() const = &Geom::SymTensor::zy;
620 
621 
622  std::function<double ()> bf_xx = std::bind(FctXX, std::bind(f));
623  std::function<double ()> bf_yy = std::bind(FctYY, std::bind(f));
624  std::function<double ()> bf_zz = std::bind(FctZZ, std::bind(f));
625 
626  std::function<double ()> bf_xy = std::bind(FctXY, std::bind(f));
627  std::function<double ()> bf_yx = std::bind(FctYX, std::bind(f));
628 
629  std::function<double ()> bf_xz = std::bind(FctXZ, std::bind(f));
630  std::function<double ()> bf_zx = std::bind(FctZX, std::bind(f));
631 
632  std::function<double ()> bf_yz = std::bind(FctYZ, std::bind(f));
633  std::function<double ()> bf_zy = std::bind(FctZY, std::bind(f));
634 
635  new SensorT<double>(bf_xx, label_xx);
636  new SensorT<double>(bf_yy, label_yy);
637  new SensorT<double>(bf_zz, label_zz);
638 
639  new SensorT<double>(bf_xy, label_xy);
640  new SensorT<double>(bf_yx, label_yx);
641 
642  new SensorT<double>(bf_xz, label_xz);
643  new SensorT<double>(bf_zx, label_zx);
644 
645  new SensorT<double>(bf_yz, label_yz);
646  new SensorT<double>(bf_zy, label_zy);
647 
648  }
649 
650  // specialization for methods which return a Geom::SymTensor&
651  template<>
652  inline void
653  Sensor::instanciate<Geom::SymTensor& >(std::function< Geom::SymTensor& ()>& f, const std::string& label) {
654  const std::string label_xx = label + "_XX";
655  const std::string label_yy = label + "_YY";
656  const std::string label_zz = label + "_ZZ";
657 
658  const std::string label_xy = label + "_XY";
659  const std::string label_yx = label + "_YX";
660 
661  const std::string label_xz = label + "_XZ";
662  const std::string label_zx = label + "_ZX";
663 
664  const std::string label_yz = label + "_YZ";
665  const std::string label_zy = label + "_ZY";
666 
667 
668 
669  const double& (Geom::SymTensor::*FctXX)() const = &Geom::SymTensor::xx;
670  const double& (Geom::SymTensor::*FctYY)() const = &Geom::SymTensor::yy;
671  const double& (Geom::SymTensor::*FctZZ)() const = &Geom::SymTensor::zz;
672 
673  const double& (Geom::SymTensor::*FctXY)() const = &Geom::SymTensor::xy;
674  const double& (Geom::SymTensor::*FctYX)() const = &Geom::SymTensor::yx;
675 
676  const double& (Geom::SymTensor::*FctXZ)() const = &Geom::SymTensor::xz;
677  const double& (Geom::SymTensor::*FctZX)() const = &Geom::SymTensor::zx;
678 
679  const double& (Geom::SymTensor::*FctYZ)() const = &Geom::SymTensor::yz;
680  const double& (Geom::SymTensor::*FctZY)() const = &Geom::SymTensor::zy;
681 
682 
683  std::function<double ()> bf_xx = std::bind(FctXX, std::bind(f));
684  std::function<double ()> bf_yy = std::bind(FctYY, std::bind(f));
685  std::function<double ()> bf_zz = std::bind(FctZZ, std::bind(f));
686 
687  std::function<double ()> bf_xy = std::bind(FctXY, std::bind(f));
688  std::function<double ()> bf_yx = std::bind(FctYX, std::bind(f));
689 
690  std::function<double ()> bf_xz = std::bind(FctXZ, std::bind(f));
691  std::function<double ()> bf_zx = std::bind(FctZX, std::bind(f));
692 
693  std::function<double ()> bf_yz = std::bind(FctYZ, std::bind(f));
694  std::function<double ()> bf_zy = std::bind(FctZY, std::bind(f));
695 
696  new SensorT<double>(bf_xx, label_xx);
697  new SensorT<double>(bf_yy, label_yy);
698  new SensorT<double>(bf_zz, label_zz);
699 
700  new SensorT<double>(bf_xy, label_xy);
701  new SensorT<double>(bf_yx, label_yx);
702 
703  new SensorT<double>(bf_xz, label_xz);
704  new SensorT<double>(bf_zx, label_zx);
705 
706  new SensorT<double>(bf_yz, label_yz);
707  new SensorT<double>(bf_zy, label_zy);
708  }
709 
710 
711  // specialization for methods which return a const Geom::SymTensor&
712  template<>
713  inline void
714  Sensor::instanciate<const Geom::SymTensor& >(std::function< const Geom::SymTensor& ()>& f, const std::string& label) {
715  const std::string label_xx = label + "_XX";
716  const std::string label_yy = label + "_YY";
717  const std::string label_zz = label + "_ZZ";
718 
719  const std::string label_xy = label + "_XY";
720  const std::string label_yx = label + "_YX";
721 
722  const std::string label_xz = label + "_XZ";
723  const std::string label_zx = label + "_ZX";
724 
725  const std::string label_yz = label + "_YZ";
726  const std::string label_zy = label + "_ZY";
727 
728 
729 
730  const double& (Geom::SymTensor::*FctXX)() const = &Geom::SymTensor::xx;
731  const double& (Geom::SymTensor::*FctYY)() const = &Geom::SymTensor::yy;
732  const double& (Geom::SymTensor::*FctZZ)() const = &Geom::SymTensor::zz;
733 
734  const double& (Geom::SymTensor::*FctXY)() const = &Geom::SymTensor::xy;
735  const double& (Geom::SymTensor::*FctYX)() const = &Geom::SymTensor::yx;
736 
737  const double& (Geom::SymTensor::*FctXZ)() const = &Geom::SymTensor::xz;
738  const double& (Geom::SymTensor::*FctZX)() const = &Geom::SymTensor::zx;
739 
740  const double& (Geom::SymTensor::*FctYZ)() const = &Geom::SymTensor::yz;
741  const double& (Geom::SymTensor::*FctZY)() const = &Geom::SymTensor::zy;
742 
743 
744  std::function<double ()> bf_xx = std::bind(FctXX, std::bind(f));
745  std::function<double ()> bf_yy = std::bind(FctYY, std::bind(f));
746  std::function<double ()> bf_zz = std::bind(FctZZ, std::bind(f));
747 
748  std::function<double ()> bf_xy = std::bind(FctXY, std::bind(f));
749  std::function<double ()> bf_yx = std::bind(FctYX, std::bind(f));
750 
751  std::function<double ()> bf_xz = std::bind(FctXZ, std::bind(f));
752  std::function<double ()> bf_zx = std::bind(FctZX, std::bind(f));
753 
754  std::function<double ()> bf_yz = std::bind(FctYZ, std::bind(f));
755  std::function<double ()> bf_zy = std::bind(FctZY, std::bind(f));
756 
757  new SensorT<double>(bf_xx, label_xx);
758  new SensorT<double>(bf_yy, label_yy);
759  new SensorT<double>(bf_zz, label_zz);
760 
761  new SensorT<double>(bf_xy, label_xy);
762  new SensorT<double>(bf_yx, label_yx);
763 
764  new SensorT<double>(bf_xz, label_xz);
765  new SensorT<double>(bf_zx, label_zx);
766 
767  new SensorT<double>(bf_yz, label_yz);
768  new SensorT<double>(bf_zy, label_zy);
769  }
770 
771  // specialization for methods which return a Math::Stat
772  template<>
773  inline void
774  Sensor::instanciate<const Math::Stat&>(std::function<const Math::Stat&()>& f, const std::string& label) {
775  const std::string label_Ave = label + "_Ave";
776  const std::string label_Std = label + "_Std";
777  const std::string label_Min = label + "_Min";
778  const std::string label_Max = label + "_Max";
779 
780  const double& (Math::Stat::*Fct_Ave)() const = &Math::Stat::average;
781  const double& (Math::Stat::*Fct_Std)() const = &Math::Stat::std;
782  const double& (Math::Stat::*Fct_Max)() const = &Math::Stat::max;
783  const double& (Math::Stat::*Fct_Min)() const = &Math::Stat::min;
784  std::function<double ()> bf_Ave = std::bind(Fct_Ave, std::bind(f));
785  std::function<double ()> bf_Std = std::bind(Fct_Std, std::bind(f));
786  std::function<double ()> bf_Max = std::bind(Fct_Max, std::bind(f));
787  std::function<double ()> bf_Min = std::bind(Fct_Min, std::bind(f));
788 
789  new SensorT<double>(bf_Ave, label_Ave);
790  new SensorT<double>(bf_Std, label_Std);
791  new SensorT<double>(bf_Max, label_Max);
792  new SensorT<double>(bf_Min, label_Min);
793  }
794 
795  // specialization for SensorMethods which return a couple std::vector<double> and std::vector<std::string>
796  template<>
797  inline void
798  Sensor::instanciate<std::vector<double > >(std::function< std::vector<double > ()>& f, const std::vector<std::string>& labelVector) {
799  for (unsigned int i = 0; i!=labelVector.size(); i++) {
800  std::function<const double ()> Fun = std::bind(&Sensor::val_from_std_vector, f, i);
801  new SensorT<const double>(Fun, labelVector[i]);
802  }
803 
804 
805  }
806 
807  template<typename T>
808  inline void
809  Sensor::instanciate(std::function<T ()>& f, const std::string& label) {
810  new SensorT<T>(f, label);
811  }
812 
813  template<typename T>
814  inline
815  SensorT<T>::SensorT(std::function<T ()>& f, const std::string& label)
816  : Sensor(), _label(label), _rank(add(this)), _f(f) {
817  }
818 
819  template<typename T>
820  inline void
822  UserAssert(is_file_out_open(), "can't open sensor file");
823  _fout << _f()<<"\t";
824  }
825 
826  template<typename T, class R>
827  inline TSensor<T,R>::TSensor(const T &t, const R (T::*f)() const, const std::string & label)
828  : Sensor(), _label(label), _t(t),_f(f), _rank(add(this)) {
829  }
830 
831  template<typename T, class R>
832  inline void
834  UserAssert(is_file_out_open(), "can't open sensor file");
835  _fout << (_t.*_f)()<<"\t";
836  }
837 
838 
839  }
840 }
841 
842 #endif
#define UserAssert(condition, message)
Definition: Macro.hpp:54
Definition: Sensor.hpp:93
~ArraySensor()
Definition: Sensor.cpp:51
static std::set< ArraySensor * > _all
Definition: Sensor.hpp:102
void write(std::ostream &)
Definition: Sensor.cpp:56
Math::Stat & _data
Definition: Sensor.hpp:107
const std::string _label
Definition: Sensor.hpp:106
static std::set< ArraySensor * > get_all()
Definition: Sensor.hpp:99
ArraySensor(Math::Stat &, const std::string &label)
Definition: Sensor.cpp:46
Definition: Sensor.hpp:112
static void new_object(const A &r, T(R::*f)() const, const std::string &label)
static void close_file_out()
Definition: Sensor.cpp:94
static void new_object(A &r, T(R::*f)(), const std::string &label)
static void flush_file_out()
Definition: Sensor.cpp:108
static void new_object(A &r, const T(R::*f)(), const std::vector< std::string > &labelVector)
virtual ~Sensor()
Definition: Sensor.cpp:90
static std::string _file_name
Definition: Sensor.hpp:168
static bool is_file_out_open()
Definition: Sensor.cpp:103
static double val_from_std_vector(std::function< std::vector< double >()> &f, std::size_t)
Definition: Sensor.hpp:236
static int _rank
Definition: Sensor.hpp:169
static std::vector< Sensor * > _sensor_table
Definition: Sensor.hpp:167
static void acquire_all_sensor_and_write()
Definition: Sensor.cpp:148
static std::ofstream & get_output_stream()
Definition: Sensor.hpp:129
static void new_object(const A &r, const T(R::*f)() const, const std::vector< std::string > &labelVector)
static void new_object(A &r, T(R::*f)(), const std::vector< std::string > &labelVector)
static void new_object(A &r, const T(R::*f)(), const std::string &label)
static void new_object(const A &r, const T(R::*f)() const, const std::string &label)
static void instanciate(std::function< T()> &f, const std::vector< std::string > &labelVector)
static void instanciate(std::function< T()> &f, const std::string &label)
Definition: Sensor.hpp:809
static void desactivate(Sensor *s)
Definition: Sensor.hpp:160
virtual const std::string & get_label() const =0
static void write_header()
Definition: Sensor.cpp:125
static bool _header_column_shape
Definition: Sensor.hpp:171
static std::ofstream _fout
Definition: Sensor.hpp:164
static std::string get_file_name()
Definition: Sensor.hpp:131
static void set_header_column_shape(const bool isColumn)
Definition: Sensor.cpp:120
virtual void acquire_and_write()=0
static bool _header_written
Definition: Sensor.hpp:170
virtual int get_rank() const =0
static void reset()
Definition: Sensor.cpp:64
static void open_file_out(const std::string &file_name, bool force=false)
Definition: Sensor.cpp:75
Sensor()
Definition: Sensor.cpp:87
static void write_data_in_file_out(const std::string &)
Definition: Sensor.cpp:115
static int add(Sensor *s)
Definition: Sensor.hpp:159
static void new_object(const A &r, T(R::*f)() const, const std::vector< std::string > &labelVector)
Definition: Sensor.hpp:179
std::function< T()> _f
Definition: Sensor.hpp:192
~SensorT()
Definition: Sensor.hpp:182
const std::string & get_label() const
Definition: Sensor.hpp:186
int get_rank() const
Definition: Sensor.hpp:185
void acquire_and_write()
Definition: Sensor.hpp:821
const std::string _label
Definition: Sensor.hpp:186
SensorT(std::function< T()> &, const std::string &label=std::string(""))
Definition: Sensor.hpp:815
const int _rank
Definition: Sensor.hpp:191
Definition: Sensor.hpp:198
const std::string _label
Definition: Sensor.hpp:212
const R(T::* _f)() const
Definition: Sensor.hpp:219
TSensor(const T &t, const R(T::*f)() const, const std::string &label=std::string(""))
Definition: Sensor.hpp:827
const int _rank
Definition: Sensor.hpp:220
~TSensor()
Definition: Sensor.hpp:202
const T & _t
Definition: Sensor.hpp:218
int get_rank() const
Definition: Sensor.hpp:207
void acquire_and_write()
Definition: Sensor.hpp:833
const std::string & get_label() const
Definition: Sensor.hpp:210
Definition: Quaternion.hpp:54
Definition: SymTensor.hpp:68
Definition: Tensor.hpp:62
Definition: Vector.hpp:75
Definition: Stat.hpp:48
const
Definition: Sensor.hpp:225
CONSTRUCTOR_SPECIFICATION(,,,, const std::string &) CONSTRUCTOR_SPECIFICATION(const
Definition: Common.hpp:198
T min(const T v0, const T v1)
Definition: Exprtk.hpp:1456
T max(const T v0, const T v1)
Definition: Exprtk.hpp:1463
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t t(t+t)") define_sfop3(16