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