misc.hh

Go to the documentation of this file.
00001 #ifndef MISC_HH
00002 #define MISC_HH
00003 
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <sstream>
00011 #include <complex>
00012 #include "exceptions.hh"
00013 
00014 namespace Dune {
00015 
00021 template <int N>
00022 struct Int2Type {
00023   enum { value = N };
00024 };
00025 
00031 
00032 // conjugate complex does nothing for non-complex types
00033 template<class K>
00034 inline K conjugateComplex (const K& x)
00035 {
00036     return x;
00037 }
00038 
00039 #ifndef DOXYGEN
00040 // specialization for complex
00041 template<class K>
00042 inline std::complex<K> conjugateComplex (const std::complex<K>& c)
00043 {
00044     return std::complex<K>(c.real(),-c.imag());
00045 }
00046 #endif
00047 
00049 template <class T>
00050 int sign(const T& val) 
00051 {
00052   return (val < 0 ? -1 : 1);
00053 }
00054 
00061 template<class T>
00062 T SQR (T t)
00063 {
00064   return t*t;
00065 }
00066 
00068 template <int m, int p> 
00069 struct Power_m_p
00070 {
00071   // power stores m^p
00072   enum { power = (m * Power_m_p<m,p-1>::power ) };
00073 };
00074 
00076 template <int m> 
00077 struct Power_m_p< m , 0>
00078 {
00079   // m^0 = 1
00080   enum { power = 1 };
00081 };
00082 
00084 template <int m> 
00085 struct Factorial
00086 {
00088   enum { factorial = m * Factorial<m-1>::factorial };
00089 };
00090 
00092 template <> 
00093 struct Factorial<0>
00094 {
00095   // 0! = 1
00096   enum { factorial = 1 };
00097 };
00098 
00099 //********************************************************************
00100 //
00101 // generate filenames with timestep number in it 
00102 //
00103 //********************************************************************
00104 
00106 inline std::string genFilename(const std::string& path, 
00107                                const std::string& fn, 
00108                                int ntime, 
00109                                int precision = 6)
00110 {
00111   std::ostringstream name;
00112 
00113   if(path.size() > 0)
00114   {
00115     name << path; 
00116     name << "/"; 
00117   }
00118   name << fn << std::setw(precision) << std::setfill('0') << ntime;
00119  
00120   // Return the string corresponding to the stringstream
00121   return name.str();
00122 }
00123     
00126 }
00127 
00128 
00129 #endif
Generated on Mon Apr 26 10:45:21 2010 for dune-common by  doxygen 1.6.3