AFEPack
|
00001 00011 #ifndef _Miscellaneous_h_ 00012 #define _Miscellaneous_h_ 00013 00014 #include <sys/types.h> 00015 #include <sys/stat.h> 00016 #include <fcntl.h> 00017 #include <unistd.h> 00018 #include <wordexp.h> 00019 #include <dlfcn.h> 00020 00021 #include <cstring> 00022 #include <cstdlib> 00023 #include <iostream> 00024 #include <fstream> 00025 #include <string> 00026 00027 #include <vector> 00028 00029 #include <boost/iostreams/filtering_stream.hpp> 00030 #include <lac/vector.h> 00031 00032 namespace dealii {}; 00033 using namespace dealii; 00034 00035 #ifndef __SERIALIZATION__ 00036 #define __SERIALIZATION__ 00037 #endif 00038 00039 #ifndef MULTITHREAD 00040 #define MULTITHREAD 00041 #endif 00042 00043 typedef void * dlhandle_t; 00044 typedef boost::iostreams::filtering_istream filtering_istream; 00045 00046 void ExpandString(std::string& str); 00047 void StringToWord(const std::string& str, const char& c, std::vector<std::string>& result); 00048 void CombineString(const std::vector<std::string>& prefix, 00049 const std::vector<std::string>& suffix, 00050 std::vector<std::string>& result); 00051 00057 std::string FindAFEPackLibraryFilePath(const std::string& filename); 00058 00063 dlhandle_t AFEPackDLOpen(const std::string& filename); 00064 00071 void OpenAFEPackLibraryFile(const std::string& filename, 00072 filtering_istream& is); 00073 00081 void LoadLibraryFunction(dlhandle_t& handle, 00082 const std::string& sym, 00083 dlhandle_t& fun_ptr); 00084 00093 void OpenFilteredStream(const std::string& filename, 00094 filtering_istream& is); 00095 00096 void hsfc_renumerate(int, double *, double *, double *, int *); 00097 void hsfc_renumerate(int, double *, double *, double *, int *, 00098 void (*)(double, double, double, 00099 double&,double&,double&)); 00100 00101 template <class V, template <class T> class C> 00102 V innerProduct(const C<V>& c0, const C<V>& c1) { 00103 V v = 0; 00104 typename C<V>::const_iterator 00105 the_v0 = c0.begin(), 00106 end_v0 = c0.end(), 00107 the_v1 = c1.begin(); 00108 for (;the_v0 != end_v0; ++ the_v0, ++ the_v1) { 00109 v += (*the_v0) * (*the_v1); 00110 } 00111 return v; 00112 } 00113 00121 template <class C> 00122 typename C::value_type innerProduct(const C& c0, const C& c1) { 00123 typename C::value_type v = 0; 00124 typename C::const_iterator 00125 the_v0 = c0.begin(), 00126 end_v0 = c0.end(), 00127 the_v1 = c1.begin(); 00128 for (;the_v0 != end_v0; ++ the_v0, ++ the_v1) { 00129 v += (*the_v0) * (*the_v1); 00130 } 00131 return v; 00132 } 00133 00140 template <class value_type> 00141 class Function 00142 { 00143 public: 00145 Function() {}; 00147 virtual ~Function() {}; 00148 public: 00150 virtual value_type value(const double * p) const {return value_type();}; 00152 virtual std::vector<value_type> gradient(const double * p) const 00153 {return std::vector<value_type>();}; 00154 }; 00155 00161 template <class value_type> 00162 class FunctionFunction : public Function<value_type> 00163 { 00164 public: 00165 typedef value_type (*ValuePrototype)(const double *); 00166 typedef std::vector<value_type> (*GradientPrototype)(const double *); 00167 private: 00169 ValuePrototype vf; 00171 GradientPrototype gf; 00172 public: 00174 FunctionFunction(value_type (*v)(const double *) = NULL, 00175 std::vector<value_type> (*g)(const double *) = NULL) : 00176 vf(v), gf(g) {}; 00177 FunctionFunction(const FunctionFunction<value_type>& f) : 00178 vf(f.vf), gf(f.gf) {}; 00180 virtual ~FunctionFunction() {}; 00181 public: 00183 ValuePrototype valueFunction() const {return vf;}; 00185 ValuePrototype& valueFunction() {return vf;}; 00187 GradientPrototype gradientFunction() const {return gf;}; 00189 GradientPrototype& gradientFunction() {return gf;}; 00190 operator ValuePrototype() const {return vf;}; 00191 operator ValuePrototype() {return vf;}; 00192 operator GradientPrototype() const {return gf;}; 00193 operator GradientPrototype() {return gf;}; 00194 public: 00196 virtual value_type value(const double * p) const {return (*vf)(p);}; 00198 virtual std::vector<value_type> gradient(const double * p) const {return (*gf)(p);}; 00199 }; 00200 00206 template <int n, class _Tp> 00207 class nVector : public std::vector<_Tp> 00208 { 00209 public: 00210 nVector() : std::vector<_Tp>(n, _Tp()) {}; 00211 explicit nVector(const _Tp& t) : std::vector<_Tp>(n, t) {}; 00212 nVector(const nVector<n,_Tp>& v) : std::vector<_Tp>(n) { 00213 for (int i = 0;i < n;i ++) 00214 (*this)[i] = v[i]; 00215 }; 00216 nVector<n,_Tp>& operator=(const nVector<n,_Tp>& v) { 00217 for (int i = 0;i < n;i ++) 00218 (*this)[i] = v[i]; 00219 return *this; 00220 }; 00221 }; 00222 00223 #endif 00224