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