Jpp  18.2.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGeometry.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JGEOMETRY__
2 #define __JACOUSTICS__JGEOMETRY__
3 
4 #include <ostream>
5 #include <iomanip>
6 #include <vector>
7 #include <map>
8 #include <algorithm>
9 #include <cmath>
10 
11 #include "JLang/JException.hh"
12 #include "JLang/JPredicate.hh"
13 #include "JLang/JComparator.hh"
14 #include "JLang/JManip.hh"
15 
17 
18 #include "JTools/JHashMap.hh"
19 
20 #include "JDetector/JLocation.hh"
21 #include "JDetector/JHydrophone.hh"
22 #include "JDetector/JDetector.hh"
24 
27 #include "JAcoustics/JMechanics.hh"
28 #include "JAcoustics/JModel.hh"
29 
30 
31 /**
32  * \file
33  *
34  * Acoustic geometries.
35  * \author mdejong
36  */
37 namespace JACOUSTICS {}
38 namespace JPP { using namespace JACOUSTICS; }
39 
40 namespace JACOUSTICS {
41 
43  using JLANG::JNoValue;
46  using JTOOLS::JHashMap;
49 
50 
51  /**
52  * Auxiliary namespace to encapsulate different geometries.\n
53  */
54  namespace JGEOMETRY {
55 
56  /**
57  * Floor geometry.
58  */
59  struct JFloor {
60  /**
61  * Default constructor.
62  */
63  JFloor() :
64  height(0.0)
65  {}
66 
67 
68  /**
69  * Constructor.\n
70  * The height refers to the maximal distance from the top of the T-bar of the parent string to the actual sensor.
71  *
72  * \param height height
73  */
74  JFloor(const double height) :
75  height(height)
76  {}
77 
78 
79  /**
80  * Get height of this floor.\n
81  * The height refers to the maximal distance from
82  * the fixed reference point of the parent string.
83  *
84  * \return height
85  */
86  double getHeight() const
87  {
88  return height;
89  }
90 
91 
92  /**
93  * Write floor parameters to output stream.
94  *
95  * \param out output stream
96  * \param floor floor
97  * \return output stream
98  */
99  friend inline std::ostream& operator<<(std::ostream& out, const JFloor& floor)
100  {
101  return out << FIXED(7,2) << floor.getHeight();
102  }
103 
104 
105  protected:
106  double height;
107  };
108 
109 
110  /**
111  * String geometry.
112  *
113  * This data structure provides for the implementation of the dynamical geometry of a detector string.\n
114  * In this,
115  * the position of floor > 0 corresponds to the piezo sensor which is located inside the optical module and \n
116  * the position of floor = 0 to the hydrophone which is optionally mounted on the anchor.\n
117  * The reference position of a string corresponds to the top of the T-bar located on the anchor.\n
118  * The position of a piezo sensor depends on the tilt of the string and the mechanical model.\n
119  * The position of the hydrophone is fixed.
120  * Its value is relative to the reference position of the string.
121  */
122  struct JString :
123  public JPosition3D,
124  public std::vector<JFloor>
125  {
129 
130  static constexpr double PRECISION_M = 1.0e-4; //!< precision of height evaluation [m]
131 
132  /**
133  * Get approximate length of string.
134  *
135  * \param parameters parameters
136  * \param mechanics mechanics
137  * \param height height
138  * \return length
139  */
140  static inline double getLength(const JMODEL::JString& parameters,
141  const JMechanics& mechanics,
142  const double height)
143  {
144  const double T2 = parameters.getLengthSquared();
145 
146  const double x = 1.0 - mechanics.a*height;
147 
148  return sqrt(1.0 + T2) * height + T2*mechanics.b * log(x) + 0.5*T2*mechanics.a*mechanics.b*mechanics.b * (1.0/x - 1.0);
149  }
150 
151 
152  /**
153  * Get approximate height of string.
154  *
155  * \param parameters parameters
156  * \param mechanics mechanics
157  * \param length length
158  * \param precision precision
159  * \return height
160  */
161  static inline double getHeight(const JMODEL::JString& parameters,
162  const JMechanics& mechanics,
163  const double length,
164  const double precision = PRECISION_M)
165  {
166  const size_t MAXIMUM_NUMBER_OF_ITERATIONS = 10;
167 
168  const double T2 = parameters.getLengthSquared();
169 
170  double z = length; // start value
171 
172  for (size_t i = 0; i != MAXIMUM_NUMBER_OF_ITERATIONS; ++i) {
173 
174  const double ls = getLength(parameters, mechanics, z) - length;
175 
176  if (fabs(ls) <= precision) {
177  break;
178  }
179 
180  const double vs = 1.0 - mechanics.a*mechanics.b / (1.0 - mechanics.a*z);
181 
182  z -= ls / (1.0 + 0.5*T2 * vs*vs);
183  }
184 
185  return z;
186  }
187 
188 
189  /**
190  * Get position at given height according to given string model parameters and mechanics.
191  *
192  * \param parameters parameters
193  * \param mechanics mechanics
194  * \param height height
195  * \return position
196  */
198  const JMechanics& mechanics,
199  const double height)
200  {
201  const double h1 = height * (1.0 + parameters.vs);
202  const double z1 = mechanics.getHeight(h1);
203 
204  return JPosition3D(parameters.tx * z1 + parameters.tx2 * h1*h1,
205  parameters.ty * z1 + parameters.ty2 * h1*h1,
206  getHeight(parameters, mechanics, h1));
207  }
208 
209 
210  /**
211  * Default constructor.
212  */
214  has_hydrophone(false)
215  {}
216 
217 
218  /**
219  * Constructor.
220  *
221  * The given position corresponds to the reference position of the string from
222  * which the positions of the piezo sensors and hydrophone are determined.
223  *
224  * \param position position
225  * \param mechanics mechanical model parameters
226  */
227  JString(const JVector3D& position,
228  const JMechanics& mechanics) :
229  JPosition3D(position),
230  has_hydrophone(false),
231  mechanics(mechanics)
232  {}
233 
234 
235  /**
236  * Constructor.
237  *
238  * The given position corresponds to the reference position of the string from
239  * which the positions of the piezo sensors and hydrophone are determined.
240  *
241  * The template parameter should correspond to a data type which implements the following policy methods.
242  * <pre>
243  * int %getFloor();
244  * JGEOMETRY3d::JPosition3D %getPosition();
245  * </pre>
246  * In this, the position should correspond to the center of the optical module.
247  *
248  * Note that the position of the piezo is offset by JDETECTOR::getPiezoPosition with respect to the center of the optical module.\n
249  * The position of the hydrophone should separately be set.
250  *
251  * \param position position
252  * \param mechanics mechanical model parameters
253  * \param __begin begin of optical modules
254  * \param __end end of optical modules
255  */
256  template<class T>
257  JString(const JVector3D& position,
258  const JMechanics& mechanics,
259  T __begin,
260  T __end) :
261  JPosition3D(position),
262  has_hydrophone(false),
263  mechanics(mechanics)
264  {
265  for (T i = __begin; i != __end; ++i) {
266  (*this)[i->getFloor()] = this->getDistance(i->getPosition() + JDETECTOR::getPiezoPosition());
267  }
268  }
269 
270 
271  /**
272  * Get mechanical model parameters.
273  *
274  * \return mechanical model parameters
275  */
276  const JMechanics& getMechanics() const
277  {
278  return mechanics;
279  }
280 
281 
282  /**
283  * Get floor data.
284  *
285  * \param floor floor number
286  * \return floor data
287  */
288  JFloor& operator[](size_t floor)
289  {
290  if (floor >= this->size()) {
291  this->resize(floor + 1);
292  }
293 
294  return static_cast<std::vector<JFloor>&>(*this)[floor];
295  }
296 
297 
298  /**
299  * Check if this string has receiver at given floor.
300  *
301  * \param floor floor
302  * \return true if receiver present; else false
303  */
304  bool hasFloor(size_t floor) const
305  {
306  if (floor == 0)
307  return has_hydrophone;
308  else
309  return (floor < this->size());
310  }
311 
312 
313  /**
314  * Get height of receiver at given floor with respect to reference position.
315  *
316  * \param floor floor
317  * \return height
318  */
319  double getHeight(const size_t floor) const
320  {
321  if (!hasFloor(floor)) {
322  THROW(JValueOutOfRange, "Invalid floor " << floor);
323  }
324 
325  if (floor == 0)
326  return hydrophone.getZ();
327  else
328  return (*this)[floor].getHeight();
329  }
330 
331 
332  /**
333  * Get position of receiver at given floor according to given string model parameters.
334  *
335  * \param parameters parameters
336  * \param floor floor
337  * \return position
338  */
340  const size_t floor) const
341  {
342  if (!hasFloor(floor)) {
343  THROW(JValueOutOfRange, "Invalid floor " << floor);
344  }
345 
346  if (floor == 0)
347  return this->getPosition() + hydrophone.getPosition();
348  else
349  return this->getPosition() + getPosition(parameters, this->mechanics, (*this)[floor].getHeight());
350  }
351 
352 
353  /**
354  * Get position of receiver at given floor.
355  *
356  * \param floor floor
357  * \return position
358  */
359  JPosition3D getPosition(const size_t floor) const
360  {
361  if (!hasFloor(floor)) {
362  THROW(JValueOutOfRange, "Invalid floor " << floor);
363  }
364 
365  if (floor == 0)
366  return this->getPosition() + hydrophone.getPosition();
367  else
368  return this->getPosition() + JPosition3D(0.0, 0.0, (*this)[floor].getHeight());
369  }
370 
371 
372  /**
373  * Get distance between given position and floor according to given string model parameters.
374  *
375  * \param parameters parameters
376  * \param position position
377  * \param floor floor
378  * \return distance
379  */
381  const JVector3D& position,
382  const size_t floor) const
383  {
384  return this->getPosition(parameters, floor).getDistance(position);
385  }
386 
387 
388  /**
389  * Get model gradient of distance between given position and floor according to given string model parameters.
390  *
391  * \param parameters parameters
392  * \param position position
393  * \param floor floor
394  * \return gradient
395  */
397  const JVector3D& position,
398  const size_t floor) const
399  {
400  if (floor == 0) {
401 
402  return JMODEL::JString();
403 
404  } else if (floor < this->size()) {
405 
406  const JPosition3D pos = this->getPosition(parameters, floor);
407  const double height = (*this)[floor].getHeight();
408  const double h1 = height * (1.0 + parameters.vs);
409  const double z1 = mechanics.getHeight(h1);
410 
411  const double tx = parameters.tx;
412  const double ty = parameters.ty;
413  const double tz = sqrt(1.0 - tx*tx - ty*ty);
414 
415  const double dx = pos.getX() - position.getX();
416  const double dy = pos.getY() - position.getY();
417  const double dz = pos.getZ() - position.getZ();
418 
419  const double D = sqrt(dx*dx + dy*dy + dz*dz);
420  const double vw = 1.0 - mechanics.a * mechanics.b / (1.0 - mechanics.a*h1);
421  const double vs = 1.0 + 0.5 * parameters.getLengthSquared() * vw;
422 
423  return JMODEL::JString(z1 * dx / D - height * (tx / tz) * dz / D,
424  z1 * dy / D - height * (ty / tz) * dz / D,
425  h1*h1 * dx / D,
426  h1*h1 * dy / D,
427  height * vw * (tx * dx + ty * dy) / D + h1 * (dz / vs) / D);
428 
429  } else {
430 
431  THROW(JValueOutOfRange, "Invalid floor " << floor);
432  }
433  }
434 
435 
436  /**
437  * Write string parameters to output stream.
438  *
439  * \param out output stream
440  * \param string string
441  * \return output stream
442  */
443  friend inline std::ostream& operator<<(std::ostream& out, const JString& string)
444  {
445  using namespace std;
446 
447  for (size_t i = 0; i != string.size(); ++i) {
448  if (string.hasFloor(i)) {
449  out << setw(2) << i << ' '
450  << FIXED(7,3) << string[i] << " | "
451  << string.getPosition(i) << ' '
452  << string.mechanics << endl;
453  }
454  }
455 
456  return out;
457  }
458 
459 
460  /**
461  * Hydrophone.
462  *
463  * The position of the hydrophone is relative to the reference position of the string.
464  */
467 
468  private:
469  /**
470  * Mechanical data.
471  */
473  };
474 
475 
476  /**
477  * Detector geometry.
478  */
479  struct JDetector :
480  public JHashMap<int, JString>
481  {
482  /**
483  * Default constructor.
484  */
486  {}
487 
488 
489  /**
490  * Constructor.
491  *
492  * Note that the positions of the base modules correspond to the reference position of the string.\n
493  * As a consequence, a base module (i.e.\ floor = 0) is required for each string.\n
494  * Missing base modules should therefore be added beforehand (e.g.\ using application JDetectorDB.cc).
495  *
496  * Note that if the position of a hydrophone is not available,
497  * it is assumed that there is no hydrophone on that string.\n
498  * If the position of the hydrophone is manually set,
499  * the corresponding parameter JString::has_hydrophone should be set to <tt>true</tt>.
500  *
501  * \param detector detector
502  * \param hydrophones container with data of hydrophones
503  */
505  const std::vector<JHydrophone>& hydrophones = std::vector<JHydrophone>())
506  {
507  using namespace std;
508  using namespace JPP;
509 
510  map<int, vector<module_type> > buffer; // string -> modules
511 
512  for (JDETECTOR::JDetector::const_iterator module = detector.begin(); module != detector.end(); ++module) {
513  buffer[module->getString()].push_back(module_type(module->getLocation(), module->getPosition()));
514  }
515 
516  for (map<int, vector<module_type> >::iterator i = buffer.begin(); i != buffer.end(); ++i) {
517 
518  sort(i->second.begin(), i->second.end(), make_comparator(&module_type::getFloor));
519 
520  if (i->second[0].getFloor() == 0) {
521 
522  vector<module_type>::iterator p = i->second.begin();
523 
524  ++p;
525 
526  (*this)[i->first] = JGEOMETRY::JString(i->second[0].getPosition(), getMechanics(i->first), p, i->second.end());
527 
528  try {
529 
530  (*this)[i->first].hydrophone = getPosition(hydrophones.begin(),
531  hydrophones.end(),
533 
534  (*this)[i->first].has_hydrophone = true;
535  }
536  catch(const exception&) {
537  (*this)[i->first].has_hydrophone = false;
538  }
539 
540  } else {
541 
542  THROW(JNoValue, "No floor 0 in string " << i->first << "; use e.g. JDetectorDB -W.");
543  }
544  }
545  }
546 
547 
548  /**
549  * Check if this detector has given string.
550  *
551  * \param string string
552  * \return true if string present; else false
553  */
554  bool hasString(int string) const
555  {
556  return this->has(string);
557  }
558 
559 
560  /**
561  * Check if this detector has given location.
562  *
563  * \param location location
564  * \return true if location present; else false
565  */
566  bool hasLocation(const JLocation& location) const
567  {
568  return this->hasString(location.getString()) && (*this)[location.getString()].hasFloor(location.getFloor());
569  }
570 
571 
572  /**
573  * Write detector parameters to output stream.
574  *
575  * \param out output stream
576  * \param detector detector
577  * \return output stream
578  */
579  friend inline std::ostream& operator<<(std::ostream& out, const JDetector& detector)
580  {
581  using namespace std;
582 
583  for (JDetector::const_iterator i = detector.begin(); i != detector.end(); ++i) {
584  out << setw(4) << i->first << endl << i->second;
585  }
586 
587  return out;
588  }
589 
590 
591  /**
592  * Auxiliary data structure for module location and position.
593  */
594  struct module_type :
595  public JLocation,
596  public JPosition3D
597  {
598  /**
599  * Constructor.
600  *
601  * \param location module location
602  * \param position module position
603  */
604  module_type(const JLocation& location,
605  const JPosition3D& position) :
606  JLocation (location),
607  JPosition3D(position)
608  {}
609 
610 
611  /**
612  * Less-than operator.
613  *
614  * \param first first module
615  * \param second second module
616  * \return true if floor of first module less than that of second; else false
617  */
618  friend inline bool operator<(const module_type& first, const module_type& second)
619  {
620  return first.getFloor() < second.getFloor();
621  }
622  };
623  };
624  }
625 
626 
627  /**
628  * Type definition of detector geometry.
629  */
631 }
632 
633 #endif
static double getHeight(const JMODEL::JString &parameters, const JMechanics &mechanics, const double length, const double precision=PRECISION_M)
Get approximate height of string.
Definition: JGeometry.hh:161
Mechanical modelling of string.
Module support kit.
Exceptions.
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
const JMechanics & getMechanics() const
Get mechanical model parameters.
Definition: JGeometry.hh:276
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
static JPosition3D getPosition(const JMODEL::JString &parameters, const JMechanics &mechanics, const double height)
Get position at given height according to given string model parameters and mechanics.
Definition: JGeometry.hh:197
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:72
friend std::ostream & operator<<(std::ostream &out, const JFloor &floor)
Write floor parameters to output stream.
Definition: JGeometry.hh:99
General purpose class for hash map of unique elements.
Detector data structure.
Definition: JDetector.hh:89
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
double getHeight(const size_t floor) const
Get height of receiver at given floor with respect to reference position.
Definition: JGeometry.hh:319
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
JString()
Default constructor.
Definition: JGeometry.hh:213
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
double getDistance(const JVector3D &pos) const
Get distance to point.
Definition: JVector3D.hh:270
JFloor()
Default constructor.
Definition: JGeometry.hh:63
Data structure for detector geometry and calibration.
JPosition3D getPosition(const size_t floor) const
Get position of receiver at given floor.
Definition: JGeometry.hh:359
Auxiliary data structure for module location and position.
Definition: JGeometry.hh:594
Type definition of hydrophone.
Definition: JHydrophone.hh:30
static constexpr double PRECISION_M
precision of height evaluation [m]
Definition: JGeometry.hh:130
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Data structure for hydrophone.
JFloor(const double height)
Constructor.
Definition: JGeometry.hh:74
friend std::ostream & operator<<(std::ostream &out, const JString &string)
Write string parameters to output stream.
Definition: JGeometry.hh:443
bool hasString(int string) const
Check if this detector has given string.
Definition: JGeometry.hh:554
JGEOMETRY::JDetector JGeometry
Type definition of detector geometry.
Definition: JGeometry.hh:630
Exception for missing value.
Definition: JException.hh:214
module_type(const JLocation &location, const JPosition3D &position)
Constructor.
Definition: JGeometry.hh:604
JString(const JVector3D &position, const JMechanics &mechanics)
Constructor.
Definition: JGeometry.hh:227
Detector file.
Definition: JHead.hh:226
Acoustics support kit.
std::vector< double > vs
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
Logical location of module.
Definition: JLocation.hh:37
double getDistance(const JMODEL::JString &parameters, const JVector3D &position, const size_t floor) const
Get distance between given position and floor according to given string model parameters.
Definition: JGeometry.hh:380
Acoustics toolkit.
JPosition3D getPosition(const JMODEL::JString &parameters, const size_t floor) const
Get position of receiver at given floor according to given string model parameters.
Definition: JGeometry.hh:339
JMODEL::JString getGradient(const JMODEL::JString &parameters, const JVector3D &position, const size_t floor) const
Get model gradient of distance between given position and floor according to given string model param...
Definition: JGeometry.hh:396
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static double getLength(const JMODEL::JString &parameters, const JMechanics &mechanics, const double height)
Get approximate length of string.
Definition: JGeometry.hh:140
double a
0 &lt;= a &lt; (maximal height)⁻1; [m^-1]
Definition: JMechanics.hh:102
double getHeight(const double height) const
Get effective height for given actual height.
Definition: JMechanics.hh:70
double getLengthSquared() const
Get length squared.
double getY() const
Get y position.
Definition: JVector3D.hh:104
const JPosition3D & getPosition() const
Get position.
Definition: JPosition3D.hh:130
JDetector(const JDETECTOR::JDetector &detector, const std::vector< JHydrophone > &hydrophones=std::vector< JHydrophone >())
Constructor.
Definition: JGeometry.hh:504
double getLength() const
Get length.
Definition: JVector3D.hh:246
Logical location of module.
I/O manipulators.
JString(const JVector3D &position, const JMechanics &mechanics, T __begin, T __end)
Constructor.
Definition: JGeometry.hh:257
double b
0 &lt;= b; [m]
Definition: JMechanics.hh:103
JFloor & operator[](size_t floor)
Get floor data.
Definition: JGeometry.hh:288
then set_variable DIR else fatal Wrong number of arguments fi for INPUT_FILE in ls rt $DIR stage * log
bool hasFloor(size_t floor) const
Check if this string has receiver at given floor.
Definition: JGeometry.hh:304
int getString() const
Get string number.
Definition: JLocation.hh:134
friend std::ostream & operator<<(std::ostream &out, const JDetector &detector)
Write detector parameters to output stream.
Definition: JGeometry.hh:579
JPosition3D hydrophone
Hydrophone.
Definition: JGeometry.hh:465
JMechanics mechanics
Mechanical data.
Definition: JGeometry.hh:472
JPosition3D getPiezoPosition()
Get relative position of piezo in optical module.
JPosition3D()
Default constructor.
Definition: JPosition3D.hh:48
friend bool operator<(const module_type &first, const module_type &second)
Less-than operator.
Definition: JGeometry.hh:618
JDetector()
Default constructor.
Definition: JGeometry.hh:485
double getX() const
Get x position.
Definition: JVector3D.hh:94
bool hasLocation(const JLocation &location) const
Check if this detector has given location.
Definition: JGeometry.hh:566
Auxiliary data structure to list files in directory.
Definition: JFilesystem.hh:18
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
double getHeight() const
Get height of this floor.
Definition: JGeometry.hh:86
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:178
container_type::const_iterator const_iterator
Definition: JHashMap.hh:86
Model for fit to acoutsics data.
do echo Generating $dir eval D
Definition: JDrawLED.sh:53
double getZ() const
Get z position.
Definition: JVector3D.hh:115
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:41
container_type::iterator iterator
Definition: JHashMap.hh:88