#ifndef __JACOUSTICS__JMECHANICS__ #define __JACOUSTICS__JMECHANICS__ #include #include #include #include #include #include #include #include "JSystem/JStat.hh" #include "JLang/JStringStream.hh" #include "JLang/JManip.hh" #include "JLang/JException.hh" #include "Jeep/JeepToolkit.hh" #include "Jeep/JComment.hh" /** * \file * * Mechanical modelling of string. * \author mdejong */ namespace JACOUSTICS {} namespace JPP { using namespace JACOUSTICS; } namespace JACOUSTICS { using JEEP::JComment; using JLANG::JFileOpenException; /** * Auxiliary data structure for parameters of mechanical model.\n * This data structure provides for the implementation of the effective height conform the mechanical model of string. */ struct JMechanics { /** * Default constructor. */ JMechanics() : a(0.0), b(0.0) {} /** * Constructor. * * \param a logarithmic term * \param b linear term */ JMechanics(const double a, const double b) : a(a), b(b) {} /** * Get effective height for given actual height. * * \param height height * \return height */ double getHeight(const double height) const { return height + this->b * log(1.0 - this->a * height); } /** * Read parameters from input stream. * * \param in input stream * \param parameters parameters * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JMechanics& parameters) { return in >> parameters.a >> parameters.b; } /** * Write parameters to output stream. * * \param out output stream * \param parameters parameters * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JMechanics& parameters) { return out << FIXED(7,5) << parameters.a << ' ' << FIXED(7,3) << parameters.b; } double a; //!< 0 <= a < (maximal height)⁻1; [m^-1] double b; //!< 0 <= b; [m] }; /** * Auxiliary data structure for mechanical model parameters of strings in a given detector. * * Note that the JDetectorMechanics::WILD_CARD acts as default value for the string number. */ struct JDetectorMechanics : public std::map { enum { WILD_CARD = -1 //!< wild card for string number. }; /** * Get file name with mechanical model parameters for given detector identifier. * * \param id detector identifier * \return file name */ static std::string getFilename(const int id) { return MAKE_STRING("mechanics_" << FILL(8,'0') << id << ".txt"); } /** * Load mechanical model parameters from file. * * \param file_name file name */ void load(const std::string& file_name) { std::ifstream in(file_name.c_str()); if (!in) { THROW(JFileOpenException, "File not opened for reading: " << file_name); } in >> *this; in.close(); } /** * Load mechanical model parameters for given detector identifier. * * \param id detector identifier */ void load(const int id) { load(getFilename(id)); } /** * Get mechanical parameters for given string. * * \param string string number * \return mechanical parameters */ const JMechanics& operator()(const int string) const { static const JMechanics mechanics; const_iterator p; if ((p = this->find(string)) != this->end()) return p->second; else if ((p = this->find(WILD_CARD)) != this->end()) return p->second; else return mechanics; } /** * Read detector mechanics from input. * * \param in input stream * \param object detector mechanics * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JDetectorMechanics& object) { using namespace JPP; JStringStream is(in); if (getFileStatus(is.str().c_str())) { is.load(); } object.clear(); is >> object.comment; int string; JMechanics mechanics; while (is >> string >> mechanics) { object[string] = mechanics; } return in; } /** * Write detector mechanics to output. * * \param out output stream * \param object detector mechanics * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JDetectorMechanics& object) { using namespace std; out << object.comment; for (JDetectorMechanics::const_iterator i = object.begin(); i != object.end(); ++i) { out << setw(4) << i->first << ' ' << i->second << endl; } return out; } JComment comment; }; /** * Function object to get string mechanics. */ static JDetectorMechanics getMechanics; } #endif