Jpp  19.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMechanics.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JMECHANICS__
2 #define __JACOUSTICS__JMECHANICS__
3 
4 #include <istream>
5 #include <ostream>
6 #include <fstream>
7 #include <iomanip>
8 #include <string>
9 #include <map>
10 #include <cmath>
11 
12 #include "JSystem/JStat.hh"
13 
14 #include "JLang/JStringStream.hh"
15 #include "JLang/JManip.hh"
16 #include "JLang/JException.hh"
17 
18 #include "Jeep/JeepToolkit.hh"
19 #include "Jeep/JComment.hh"
20 
21 
22 /**
23  * \file
24  *
25  * Mechanical modelling of string.
26  * \author mdejong
27  */
28 namespace JACOUSTICS {}
29 namespace JPP { using namespace JACOUSTICS; }
30 
31 namespace JACOUSTICS {
32 
33  using JEEP::JComment;
35 
36 
37  /**
38  * Auxiliary data structure for parameters of mechanical model.\n
39  * This data structure provides for the implementation of the effective height conform the mechanical model of string.
40  */
41  struct JMechanics {
42  /**
43  * Default constructor.
44  */
46  a(0.0),
47  b(0.0)
48  {}
49 
50 
51  /**
52  * Constructor.
53  *
54  * \param a logarithmic term
55  * \param b linear term
56  */
57  JMechanics(const double a,
58  const double b) :
59  a(a),
60  b(b)
61  {}
62 
63 
64  /**
65  * Get effective height for given actual height.
66  *
67  * \param height height
68  * \return height
69  */
70  double getHeight(const double height) const
71  {
72  return height + this->b * log(1.0 - this->a * height);
73  }
74 
75 
76  /**
77  * Read parameters from input stream.
78  *
79  * \param in input stream
80  * \param parameters parameters
81  * \return input stream
82  */
83  friend inline std::istream& operator>>(std::istream& in, JMechanics& parameters)
84  {
85  return in >> parameters.a >> parameters.b;
86  }
87 
88 
89  /**
90  * Write parameters to output stream.
91  *
92  * \param out output stream
93  * \param parameters parameters
94  * \return output stream
95  */
96  friend inline std::ostream& operator<<(std::ostream& out, const JMechanics& parameters)
97  {
98  return out << FIXED(7,5) << parameters.a << ' '
99  << FIXED(7,3) << parameters.b;
100  }
101 
102  double a; //!< <tt>0 <= a < (maximal height)⁻1;</tt> [m^-1]
103  double b; //!< <tt>0 <= b;</tt> [m]
104  };
105 
106 
107  /**
108  * Auxiliary data structure for mechanical model parameters of strings in a given detector.
109  *
110  * Note that the JDetectorMechanics::WILDCARD acts as default value for the string number.
111  */
113  public std::map<int, JMechanics>
114  {
115  enum {
116  WILDCARD = -1 //!< wild card for string number.
117  };
118 
119 
120  /**
121  * Get file name with mechanical model parameters for given detector identifier.
122  *
123  * \param id detector identifier
124  * \return file name
125  */
126  static std::string getFilename(const int id)
127  {
128  return MAKE_STRING("mechanics_" << FILL(8,'0') << id << ".txt");
129  }
130 
131 
132  /**
133  * Load mechanical model parameters from file.
134  *
135  * \param file_name file name
136  */
137  void load(const std::string& file_name)
138  {
139  std::ifstream in(file_name.c_str());
140 
141  if (!in) {
142  THROW(JFileOpenException, "File not opened for reading: " << file_name);
143  }
144 
145  in >> *this;
146 
147  in.close();
148  }
149 
150 
151  /**
152  * Load mechanical model parameters for given detector identifier.
153  *
154  * \param id detector identifier
155  */
156  void load(const int id)
157  {
158  load(getFilename(id));
159  }
160 
161 
162  /**
163  * Get mechanical parameters for given string.
164  *
165  * \param string string number
166  * \return mechanical parameters
167  */
168  const JMechanics& operator()(const int string) const
169  {
170  static const JMechanics mechanics;
171 
172  const_iterator p;
173 
174  if ((p = this->find(string)) != this->end())
175  return p->second;
176  else if ((p = this->find(WILDCARD)) != this->end())
177  return p->second;
178  else
179  return mechanics;
180  }
181 
182 
183  /**
184  * Read detector mechanics from input.
185  *
186  * \param in input stream
187  * \param object detector mechanics
188  * \return input stream
189  */
190  friend inline std::istream& operator>>(std::istream& in, JDetectorMechanics& object)
191  {
192  using namespace JPP;
193 
194  JStringStream is(in);
195 
196  if (getFileStatus(is.str().c_str())) {
197  is.load();
198  }
199 
200  object.clear();
201 
202  is >> object.comment;
203 
204  int string;
205  JMechanics mechanics;
206 
207  while (is >> string >> mechanics) {
208  object[string] = mechanics;
209  }
210 
211  return in;
212  }
213 
214 
215  /**
216  * Write detector mechanics to output.
217  *
218  * \param out output stream
219  * \param object detector mechanics
220  * \return output stream
221  */
222  friend inline std::ostream& operator<<(std::ostream& out, const JDetectorMechanics& object)
223  {
224  using namespace std;
225 
226  out << object.comment;
227 
228  for (JDetectorMechanics::const_iterator i = object.begin(); i != object.end(); ++i) {
229  out << setw(4) << i->first << ' ' << i->second << endl;
230  }
231 
232  return out;
233  }
234 
236  };
237 
238 
239  /**
240  * Function object to get string mechanics.
241  */
243 }
244 
245 #endif
Exception for opening of file.
Definition: JException.hh:358
Exceptions.
Auxiliary data structure for mechanical model parameters of strings in a given detector.
Definition: JMechanics.hh:112
static JDetectorMechanics getMechanics
Function object to get string mechanics.
Definition: JMechanics.hh:242
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
const JMechanics & operator()(const int string) const
Get mechanical parameters for given string.
Definition: JMechanics.hh:168
*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
wild card for string number.
Definition: JMechanics.hh:116
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
is
Definition: JDAQCHSM.chsm:167
JMechanics(const double a, const double b)
Constructor.
Definition: JMechanics.hh:57
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
friend std::istream & operator>>(std::istream &in, JDetectorMechanics &object)
Read detector mechanics from input.
Definition: JMechanics.hh:190
friend std::istream & operator>>(std::istream &in, JMechanics &parameters)
Read parameters from input stream.
Definition: JMechanics.hh:83
Auxiliary methods for handling file names, type names and environment.
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
JMechanics()
Default constructor.
Definition: JMechanics.hh:45
void load(const std::string &file_name)
Load mechanical model parameters from file.
Definition: JMechanics.hh:137
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
I/O manipulators.
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
Auxiliary class for comment.
Definition: JComment.hh:41
void load(const int id)
Load mechanical model parameters for given detector identifier.
Definition: JMechanics.hh:156
double b
0 &lt;= b; [m]
Definition: JMechanics.hh:103
static std::string getFilename(const int id)
Get file name with mechanical model parameters for given detector identifier.
Definition: JMechanics.hh:126
then fatal The output file must have the wildcard in the e g root fi eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
friend std::ostream & operator<<(std::ostream &out, const JMechanics &parameters)
Write parameters to output stream.
Definition: JMechanics.hh:96
friend std::ostream & operator<<(std::ostream &out, const JDetectorMechanics &object)
Write detector mechanics to output.
Definition: JMechanics.hh:222
File status.
Auxiliary data structure for parameters of mechanical model.
Definition: JMechanics.hh:41