18#ifndef _GIMLI_GIMLI__H
19#define _GIMLI_GIMLI__H
21#ifdef HAVE_CONFIG_CMAKE_H
22 #include <config.cmake.h>
24 #if defined(HAVE_CONFIG_H)
26 #define PACKAGE_AUTHORS "carsten@gimli.org, thomas@gimli.org, florian@gimli.org"
38#if BOOST_THREAD_FOUND || defined(HAVE_BOOST_THREAD_HPP)
39 #define USE_BOOST_THREAD TRUE
42#if BOOST_BIND_FOUND || defined(HAVE_BOOST_BIND_HPP)
43 #define USE_BOOST_BIND TRUE
47 #define PACKAGE_NAME "libgimli"
48 #define PACKAGE_VERSION "untagt-win"
50 #define PACKAGE_BUGREPORT "carsten@gimli.org"
51 #define PACKAGE_AUTHORS "carsten@gimli.org, thomas@gimli.org, florian@gimli.org"
55 #pragma warning(disable: 4251)
79 typedef unsigned int uint;
83typedef uint16_t uint16;
84typedef uint32_t uint32;
85typedef uint64_t uint64;
100 typedef int32 SIndex;
101#elif defined(_MSC_VER)
103 typedef unsigned __int32 Index;
104 typedef __int32 SIndex;
107 #include <sys/types.h>
108 typedef size_t Index;
110 typedef ssize_t SIndex;
113#ifndef __ASSERT_FUNCTION
114 #if defined(_MSC_VER)
115 #define __ASSERT_FUNCTION __FUNCTION__
117 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
123DLLEXPORT std::string
replace(
const std::string & str,
124 const std::string & from,
125 const std::string & to);
133 #define __FILENAME__ GIMLI::replace(__FILE__, SRC_DIR, ".")
137 #define __FILENAME__ GIMLI::replace(__FILE__, SRC_DIR, ".")
142inline std::string str(){
return "";}
145template<
typename T >
inline std::string str(
const T & v){
146 std::ostringstream os;
150enum LogType {Verbose, Info, Warning, Error, Debug, Critical};
151DLLEXPORT
void log(LogType type,
const std::string & msg);
153template<
typename Value,
typename... Values>
154std::string str(Value v, Values... vs){
155 std::ostringstream os;
156 using expander =
int[];
158 (void) expander{ 0, (os <<
" " << vs, void(), 0)... };
162template<
typename... Values>
163void log(LogType type, Values... vs){
164 return log(type, str(vs...));
169 std::cout << std::endl;
173void print(std::ostream& s, Head&& head) {
174 s << std::forward<Head>(head) << std::endl;
177template<
class Head,
class... Tail>
178void print(std::ostream& s, Head&& head, Tail&&... tail) {
179 s << std::forward<Head>(head) <<
" ";
180 print(s, std::forward<Tail>(tail)...);
183template<
class... Args>
184void print(Args&&... args) {
185 print(std::cout, std::forward<Args>(args)...);
188#define WHERE GIMLI::str(__FILENAME__) + ":" + GIMLI::str(__LINE__) + "\t"
189#define WHERE_AM_I WHERE + "\t" + GIMLI::str(__ASSERT_FUNCTION) + " "
190#define TO_IMPL WHERE_AM_I + " not yet implemented\n " + GIMLI::versionStr() + "\nPlease send the messages above, the commandline and all necessary data to the author."
192#define __M std::cout << "*** " << WHERE << std::endl;
193#define __MS(str) std::cout << "*** " <<str << " " << WHERE << std::endl;
195#define __MSP(...) GIMLI::print("+++", WHERE, "\n", __VA_ARGS__, "\n---");
196#define __D if (debug()) std::cout << "Debug: " << WHERE << std::endl;
197#define __DS(str) if (debug()) std::cout << "Debug: " << str << " " << WHERE << std::endl;
199#define THROW_TO_IMPL throwToImplement(TO_IMPL);
200#define CERR_TO_IMPL std::cerr << TO_IMPL << std::endl;
201#define DEPRECATED std::cerr << WHERE_AM_I << " is deprecated " << std::endl;
202#define DEPR_STR(s) std::cerr << WHERE_AM_I << " is deprecated. Use: " << s " instead."<< std::endl;
203#define COUTMARKER std::cerr << WHERE_AM_I << std::endl;
204#define UNTESTED std::cerr << "WARNING!" << WHERE_AM_I << " " << "this function is untested" << std::endl;
206#define TOLERANCE 1e-12
207#define TOUCH_TOLERANCE 1e-12
208#define MAX_DOUBLE std::numeric_limits<double>::max()
209#define MAX_INT std::numeric_limits< int >::max()
211#define MESHBINSUFFIX ".bms"
212#define MATRIXBINSUFFIX ".bmat"
213#define MATRIXASCSUFFIX ".matrix"
214#define VECTORBINSUFFIX ".bvec"
215#define VECTORASCSUFFIX ".vector"
216#define NOT_DEFINED "notDefined"
218#define ASSERT_EQUAL_SIZE(m, n) if (m.size() != n.size()) \
219 throwLengthError(WHERE_AM_I + " " + str(m.size()) + " != " + str(n.size()));
220#define ASSERT_THIS_SIZE(n) if (n < 0 || n >= this->size()) \
221 throwLengthError(WHERE_AM_I + " " + str(this->size()) + " <= " + str(n));
222#define ASSERT_VEC_SIZE(vec, n) if (n != vec.size()) \
223 throwLengthError(WHERE_AM_I + " " + str(vec.size()) + " != " + str(n));
224#define ASSERT_SIZE(vec, n) if (n < 0 || n >= vec.size()) \
225 throwLengthError(WHERE_AM_I + " " + str(vec.size()) + " <= " + str(n));
226#define ASSERT_EQUAL(m, n) if (m != n) \
227 throwLengthError(WHERE_AM_I + " " + str(m) + " != " + str(n));
228#define ASSERT_RANGE(i, start, end) if (i < start || i >= end) \
229 throwRangeError(WHERE_AM_I, i, start, end);
230#define ASSERT_EMPTY(v) if (v.size()==0) \
231 throwLengthError(WHERE_AM_I + " array size is zero.");
232#define ASSERT_PTR(v) if (v == 0) \
233 throwError(WHERE_AM_I + " object not initialized.");
235static const int MARKER_BOUND_HOMOGEN_NEUMANN = -1;
236static const int MARKER_BOUND_MIXED = -2;
237static const int MARKER_BOUND_HOMOGEN_DIRICHLET = -3;
238static const int MARKER_BOUND_DIRICHLET = -4;
239static const int MARKER_CELL_PARAMETER = 2;
240static const int MARKER_NODE_SENSOR = -99;
241static const int MARKER_FIXEDVALUE_REGION = -1000000;
243static const uint8 MESH_BASEENTITY_RTTI = 00;
244static const uint8 MESH_MESHENTITY_RTTI = 01;
245static const uint8 MESH_NODE_RTTI = 10;
246static const uint8 MESH_BOUNDARY_RTTI = 20;
247static const uint8 MESH_BOUNDARY_NODE_RTTI = 21;
248static const uint8 MESH_EDGE_RTTI = 22;
249static const uint8 MESH_EDGE3_RTTI = 23;
250static const uint8 MESH_TRIANGLEFACE_RTTI = 24;
251static const uint8 MESH_TRIANGLEFACE6_RTTI = 25;
252static const uint8 MESH_QUADRANGLEFACE_RTTI = 26;
253static const uint8 MESH_QUADRANGLEFACE8_RTTI = 27;
254static const uint8 MESH_POLYGON_FACE_RTTI = 28;
255static const uint8 MESH_CELL_RTTI = 30;
256static const uint8 MESH_EDGE_CELL_RTTI = 31;
257static const uint8 MESH_EDGE3_CELL_RTTI = 32;
258static const uint8 MESH_TRIANGLE_RTTI = 33;
259static const uint8 MESH_TRIANGLE6_RTTI = 34;
260static const uint8 MESH_QUADRANGLE_RTTI = 35;
261static const uint8 MESH_QUADRANGLE8_RTTI = 36;
262static const uint8 MESH_QUADRANGLE9_RTTI = 37;
263static const uint8 MESH_TETRAHEDRON_RTTI = 41;
264static const uint8 MESH_TETRAHEDRON10_RTTI = 42;
265static const uint8 MESH_HEXAHEDRON_RTTI = 43;
266static const uint8 MESH_HEXAHEDRON20_RTTI = 44;
267static const uint8 MESH_TRIPRISM_RTTI = 45;
268static const uint8 MESH_TRIPRISM15_RTTI = 46;
269static const uint8 MESH_PYRAMID_RTTI = 47;
270static const uint8 MESH_PYRAMID13_RTTI = 48;
272static const uint8 MESH_SHAPE_NODE_RTTI = 210;
273static const uint8 MESH_SHAPE_EDGE_RTTI = 211;
274static const uint8 MESH_SHAPE_TRIANGLE_RTTI = 221;
275static const uint8 MESH_SHAPE_QUADRANGLE_RTTI = 222;
276static const uint8 MESH_SHAPE_POLYGON_FACE_RTTI = 223;
277static const uint8 MESH_SHAPE_TETRAHEDRON_RTTI = 231;
278static const uint8 MESH_SHAPE_HEXAHEDRON_RTTI = 232;
279static const uint8 MESH_SHAPE_TRIPRISM_RTTI = 233;
280static const uint8 MESH_SHAPE_PYRAMID_RTTI = 234;
282static const uint8 GIMLI_MATRIXBASE_RTTI = 0;
283static const uint8 GIMLI_MATRIX_RTTI = 1;
284static const uint8 GIMLI_SPARSE_MAP_MATRIX_RTTI = 2;
285static const uint8 GIMLI_SPARSE_CRS_MATRIX_RTTI = 3;
286static const uint8 GIMLI_BLOCKMATRIX_RTTI = 4;
310typedef std::complex < double > Complex;
312template <
class ValueType >
class SparseMatrix;
313typedef SparseMatrix< int > ISparseMatrix;
314typedef SparseMatrix< double > RSparseMatrix;
315typedef SparseMatrix< Complex > CSparseMatrix;
317template<
class ValueType,
class IndexType >
class SparseMapMatrix;
318typedef SparseMapMatrix< int, Index > ISparseMapMatrix;
319typedef SparseMapMatrix< double, Index > RSparseMapMatrix;
320typedef SparseMapMatrix< Complex, Index > CSparseMapMatrix;
322template <
class ValueType >
class Matrix;
323template <
class ValueType >
class BlockMatrix;
324template <
class ValueType >
class Matrix3;
325template <
class ValueType >
class Vector;
327typedef Vector < double > RVector;
328typedef Vector < Complex > CVector;
329typedef Vector < Pos > PosVector;
330typedef PosVector R3Vector;
331typedef Vector < bool > BVector;
332typedef Vector < SIndex > IVector;
333typedef Vector < Index > IndexArray;
335typedef std::vector < SIndex > SIndexArray;
337typedef Matrix < double > RMatrix;
338typedef Matrix3< double > RMatrix3;
339typedef Matrix < Complex > CMatrix;
340typedef BlockMatrix < double > RBlockMatrix;
348template <
class ValueType >
class PolynomialFunction;
349typedef PolynomialFunction< double > RPolynomialFunction;
351template <
class ValueType >
class ElementMatrix;
352typedef ElementMatrix < double > RElementMatrix;
354template <
class Vec >
class Trans;
357DLLEXPORT
void savePythonGIL(
bool s);
358DLLEXPORT
bool pythonGIL();
362DLLEXPORT
bool debug();
366DLLEXPORT
int deepDebug();
371DLLEXPORT Index threadCount();
373DLLEXPORT
void setUseOMP(
bool o);
374DLLEXPORT
bool useOMP();
379class DLLEXPORT PythonGILSave {
381 PythonGILSave() : saved_(
false) { save(); }
382 ~PythonGILSave() { restore(); }
388 PyThreadState *save_;
392#define ALLOW_PYTHON_THREADS PythonGILSave __pygil_t__;
393#define RESTORE_PYTHON_THREADS __pygil_t__.restore();
394#define SAVE_PYTHON_THREADS __pygil_t__.save();
397 #ifndef Py_BEGIN_ALLOW_THREADS
398 #define Py_BEGIN_ALLOW_THREADS
399 #define Py_END_ALLOW_THREADS
405DLLEXPORT std::string versionStr();
407DLLEXPORT std::string authors();
409template <
class T,
class U > T min(
const T & a,
const U & b){
return std::min(a, T(b)); }
410template <
class T,
class U > T max(
const T & a,
const U & b){
return std::max(a, T(b)); }
412DLLEXPORT
int openFile(
const std::string & fname, std::fstream * file,
413 std::ios_base::openmode farg,
bool terminate);
415inline int openInFileTerm(
const std::string & fname, std::fstream * file){
416 return openFile(fname, file, std::ios::in,
true);
418inline int openInFile(
const std::string & fname, std::fstream * file,
419 bool terminate=
true){
420 return openFile(fname, file, std::ios::in, terminate);
422inline int openOutFile(
const std::string & fname, std::fstream * file,
423 bool terminate=
true){
424 return openFile(fname, file, std::ios::out, terminate);
427DLLEXPORT
bool fileExist(
const std::string & filename);
428DLLEXPORT uint countColumnsInFile(
const std::string & fname, uint & columnCount);
429DLLEXPORT uint countColumnsInFile(
const std::string & fname);
430DLLEXPORT uint countRowsInFile(
const std::string & fname);
431DLLEXPORT uint fileLength(std::fstream & file);
432DLLEXPORT std::vector < std::string > getRowSubstrings(std::fstream & file,
char comment=
'#');
434inline std::vector < std::string > getRow(std::fstream & file,
char comment=
'#'){
435 return getRowSubstrings(file, comment);
438DLLEXPORT std::vector < std::string > getNonEmptyRow(std::fstream & file,
char comment=
'#');
439DLLEXPORT std::vector < std::string > getCommentLine(std::fstream & file,
char comment=
'#');
441DLLEXPORT std::vector < std::string > getSubstrings(
const std::string & str);
442DLLEXPORT std::vector < std::string > split(
const std::string & str,
char delimiter=
':');
444DLLEXPORT std::map < float, Complex > loadCFloatMap(
const std::string & filename);
445DLLEXPORT std::map < float, float > loadFloatMap(
const std::string & filename);
446DLLEXPORT std::map < int, int > loadIntMap(
const std::string & filename);
448inline void convert(
bool & var,
char * opt) { var =
true; }
449inline void convert(
int & var,
char * opt) {
if (!opt) var ++;
else var = atoi(opt); }
450inline void convert(uint & var,
char * opt) {
if (!opt) var ++;
else var = atoi(opt); }
451inline void convert(Index & var,
char * opt) {
if (!opt) var ++;
else var = atoi(opt); }
452inline void convert(
float & var,
char * opt) {
if (!opt) var = 0.0f;
else var = (float)atof(opt); }
453inline void convert(
double & var,
char * opt) {
if (!opt) var = 0.0;
else var = atof(opt); }
454inline void convert(std::string & var,
char * opt) {
if (!opt) var =
"";
else var = opt ; }
455inline void convert(std::vector < std::string > & var,
char * opt) {
if (opt) var.push_back(opt); }
457inline std::string type(
const bool & var) {
return "bool"; }
458inline std::string type(
const int32 & var) {
return "int32"; }
459inline std::string type(
const int64 & var) {
return "int64"; }
460inline std::string type(
const uint32 & var) {
return "uint32"; }
461inline std::string type(
const uint64 & var) {
return "uint64"; }
464inline std::string type(
const float & var) {
return "float"; }
465inline std::string type(
const double & var) {
return "double"; }
466inline std::string type(
const Complex & var) {
return "complex"; }
467inline std::string type(
const std::string & var) {
return "string"; }
468inline std::string type(
const std::vector < std::string > & var) {
return "string"; }
470inline std::string type(
const RVector & var) {
return "RVector"; }
471inline std::string type(
const RVector3 & var) {
return "RVector3"; }
472inline std::string type(
const R3Vector & var) {
return "R3Vector"; }
473inline std::string type(
const CVector & var) {
return "CVector"; }
474inline std::string type(
const RMatrix & var) {
return "RMatrix"; }
475inline std::string type(
const CMatrix & var) {
return "CMatrix"; }
477inline int toInt(
const std::string & str){
return std::atoi(str.c_str()); }
478inline float toFloat(
const std::string & str){
return (
float)std::atof(str.c_str()); }
479inline double toDouble(
const std::string & str){
return std::strtod(str.c_str(), NULL); }
485template <
typename ValueType > ValueType
getEnvironment(
const std::string & name,
490 char * cVar = getenv(name.c_str());
493 if (verbose) std::cout <<
"Found: export " << name <<
"=" << cVar << std::endl;
503 int ret = setenv(name.c_str(), str(val).c_str(), 1);
506 __MS(name <<
" " << val)
507 throwError(
"name is NULL, points to a string of length 0, or contains an '=' character.");
509 __MS(name <<
" " << val)
510 throwError(
"name is NULL, Insufficient memory to add a new variable to the environment.");
512 if (verbose) std::cout <<
"set: export " << name <<
"=" << val << std::endl;
516DLLEXPORT std::string
replace(
const std::string & str,
const char from,
const char to);
519DLLEXPORT std::string
lower(
const std::string & str);
532 template <
typename T >
void operator()(T * p) {
delete p; }
535 template <
typename T >
void operator() (
const T * p)
const { std::cerr << p <<
" " << std::endl; }
538 template <
typename T >
void operator() (
const T * p)
const { std::cerr << *p <<
" " << std::endl; }
541 template <
typename T >
void operator() (
const T * p)
const { std::cout << p <<
" " << std::endl; }
544 template <
typename T >
void operator() (
const T * p)
const { std::cout << *p <<
" " << std::endl; }
547template <
typename Set >
inline void intersectionSet(Set & dest,
551 set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::inserter(dest, dest.begin()));
553template <
typename Set >
inline void intersectionSet(Set & dest,
558 set_intersection(a.begin(), a.end(), b.begin(), b.end(),
559 std::inserter(dest, dest.begin()));
562 set_intersection(tmp.begin(), tmp.end(), c.begin(), c.end(),
563 std::inserter(dest, dest.begin()));
566template <
typename Set >
inline void intersectionSet(Set & dest,
572 set_intersection(a.begin(), a.end(), b.begin(), b.end(),
573 std::inserter(dest, dest.begin()));
576 set_intersection(tmp.begin(), tmp.end(), c.begin(), c.end(),
577 std::inserter(dest, dest.begin()));
580 set_intersection(tmp.begin(), tmp.end(), d.begin(), d.end(),
581 std::inserter(dest, dest.begin()));
584template <
typename Set >
inline void intersectionSet(Set & dest,
585 const std::vector < Set > & a){
587 intersectionSet(dest, a[0], a[1]);
588 for (
size_t i = 2; i < a.size(); i ++){
591 set_intersection(tmp.begin(), tmp.end(), a[i].begin(), a[i].end(),
592 std::inserter(dest, dest.begin()));
594 }
else if (a.size() == 1){
601template <
class T >
class IncrementSequence {
603 IncrementSequence(T initialValue = 0) : value_(initialValue) { }
604 inline T operator() () {
return value_++; }
610template <
typename Classname >
class DLLEXPORT
Singleton {
615 delete pInstance_; pInstance_ = NULL;
622 return pInstance_ ? pInstance_ : (pInstance_ =
new Classname());
641 static Classname * pInstance_;
645template <
typename T > Index hash_(T v){
647 return std::hash<T>()(v);
656 seed ^= hash_(val) + 0x9e3779b9 + (seed<<6) + (seed>>2);
658template <
typename T,
typename... Types>
659void hashCombine (Index & seed,
const T & val,
const Types&... args){
664template <
typename... Types>
665Index hash(
const Types&... args){
670template void hashCombine(Index & seed,
const Index & hash);
Singleton()
Definition gimli.h:634
static Classname & instance()
Definition gimli.h:628
static Classname * pInstance()
Definition gimli.h:620
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
void hashCombine(Index &seed, const T &val)
Definition gimli.h:655
ValueType getEnvironment(const std::string &name, ValueType def, bool verbose=false)
Definition gimli.h:485
void setEnvironment(const std::string &name, ValueType val, bool verbose=false)
Definition gimli.h:500
std::string lower(const std::string &str)
Definition gimli.cpp:305
void setDeepDebug(int level)
Definition gimli.cpp:131
std::string replace(const std::string &str, const std::string &from, const std::string &to)
Definition gimli.cpp:292
void showSizes()
Definition gimli.cpp:151
void setDebug(bool s)
Definition gimli.cpp:128
void setThreadCount(Index nThreads)
Definition gimli.cpp:73
IOFormat
Definition gimli.h:289