/** \file elbeem/intern/utilities.cpp * \ingroup elbeem */ /****************************************************************************** * * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method * Copyright 2003-2006 Nils Thuerey * * Global C style utility funcions * *****************************************************************************/ #include #include #ifdef WIN32 // for timing #include #else #include #include #include #endif #include "utilities.h" #ifndef NOPNG #ifdef WIN32 #include "png.h" #else #include #endif #endif // NOPNG #include // global debug level #ifdef DEBUG int gDebugLevel = DEBUG; #else // DEBUG int gDebugLevel = 0; #endif // DEBUG // global world state, acces with get/setElbeemState int gElbeemState = SIMWORLD_INVALID; // access global state of elbeem simulator void setElbeemState(int set) { gElbeemState = set; } int getElbeemState(void) { return gElbeemState; } int isSimworldOk(void) { return (getElbeemState() >=0); } // last error as string, acces with get/setElbeemErrorString char gElbeemErrorString[256] = {'-','\0' }; // access elbeem simulator error string void setElbeemErrorString(const char* set) { strncpy(gElbeemErrorString, set, 256); } char* getElbeemErrorString(void) { return gElbeemErrorString; } //! for interval debugging output myTime_t globalIntervalTime = 0; //! color output setting for messages (0==off, else on) #ifdef WIN32 // switch off first call #define DEF_globalColorSetting -1 #else // WIN32 // linux etc., on by default #define DEF_globalColorSetting 1 #endif // WIN32 int globalColorSetting = DEF_globalColorSetting; // linux etc., on by default int globalFirstEnvCheck = 0; void resetGlobalColorSetting() { globalColorSetting = DEF_globalColorSetting; } // global string for formatting vector output, TODO test!? const char *globVecFormatStr = "V[%f,%f,%f]"; // global mp on/off switch bool glob_mpactive = false; // global access to mpi index, for debugging (e.g. in utilities.cpp) int glob_mpnum = -1; int glob_mpindex = -1; int glob_mppn = -1; //----------------------------------------------------------------------------- // helper function that converts a string to integer, // and returns an alternative value if the conversion fails int convertString2Int(const char *str, int alt) { int val; char *endptr; bool success=true; val = strtol(str, &endptr, 10); if( (str==endptr) || ((str!=endptr) && (*endptr != '\0')) ) success = false; if(!success) { return alt; } return val; } //----------------------------------------------------------------------------- //! helper function that converts a flag field to a readable integer string convertFlags2String(int flags) { std::ostringstream ret; ret <<"("; int max = sizeof(int)*8; for(int i=0; i4) && (filentemp[filentemp.length()-4]=='.')) { filentemp[filentemp.length()-4] = '\0'; } std::ostringstream filennew; filennew << filentemp.c_str(); filennew << ".ppm.gz"; gzf = gzopen(filennew.str().c_str(), "wb9"); if(!gzf) goto fail; gzprintf(gzf,"P6\n%d %d\n255\n",w,h); // output binary pixels for(int j=0;j0) { ret << ms<<"m"<< ss<<"s" ; } else { if(ps>0) { ret << ss<<"."; if(ps<10) { ret <<"0"; } ret <e[0]) || (s[1]>e[1]) || (s[2]>e[2]) ) { errFatal("checkBoundingBox","Check by '"<0) { myTime_t currTime = getTime(); if((currTime - globalIntervalTime)>interval) { globalIntervalTime = getTime(); } else { return; } } // colors off? if( (globalColorSetting == -1) || // off for e.g. win32 ((globalColorSetting==1) && ((id==DM_FATAL)||( getenv("ELBEEM_NOCOLOROUT") )) ) ) { // only reset once col_std = col_black = col_dark_gray = col_bright_gray = col_red = col_bright_red = col_green = col_bright_green = col_bright_yellow = col_yellow = col_cyan = col_bright_cyan = col_purple = col_bright_purple = col_neutral = ""; globalColorSetting = 0; } std::ostringstream sout; if(id==DM_DIRECT) { sout << msg; } else { sout << col_cyan<< from; switch(id) { case DM_MSG: sout << col_std << " message:"; break; case DM_NOTIFY: sout << col_bright_cyan << " note:" << col_std; break; case DM_IMPORTANT: sout << col_yellow << " important:" << col_std; break; case DM_WARNING: sout << col_bright_red << " warning:" << col_std; break; case DM_ERROR: sout << col_red << " error:" << col_red; break; case DM_FATAL: sout << col_red << " fatal("<=0) { mpin << "elbeem_log_"<< glob_mpindex <<".txt"; } else { mpin << "elbeem_log_ini.txt"; } fileout = 1; strncpy(filen, mpin.str().c_str(),255); filen[255]='\0'; #else strncpy(filen, "elbeem_debug_log.txt",255); #endif #ifdef WIN32 // windows causes trouble with direct output fileout = 1; #endif // WIN32 #if PARALLEL==1 fileout = 2;// buffer out, switch off again... if(globOutstrForce) fileout=1; #endif if(getenv("ELBEEM_FORCESTDOUT")) { fileout = 0;// always direct out } //fprintf(stdout,"out deb %d, %d, '%s',l%d \n",globOutstrForce,fileout, filen, globOutstr.str().size() ); #if PARALLEL==1 #pragma omp critical #endif // PARALLEL==1 { if(fileout==1) { // debug level is >0 anyway, so write to file... FILE *logf = fopen(filen,"a+"); // dont complain anymore here... if(logf) { if(globOutstrForce) { fprintf(logf, "%s",globOutstr.str().c_str() ); globOutstr.str(""); // reset } fprintf(logf, "%s",sout.str().c_str() ); fclose(logf); } } else if(fileout==2) { globOutstr << sout.str(); } else { // normal stdout output fprintf(stdout, "%s",sout.str().c_str() ); if(id!=DM_DIRECT) fflush(stdout); } } // omp crit } // helper functions from external program using elbeem lib (e.g. Blender) /* set gDebugLevel according to env. var */ extern "C" void elbeemCheckDebugEnv(void) { const char *strEnvName = "BLENDER_ELBEEMDEBUG"; const char *strEnvName2 = "ELBEEM_DEBUGLEVEL"; if(globalFirstEnvCheck) return; if(getenv(strEnvName)) { gDebugLevel = atoi(getenv(strEnvName)); if(gDebugLevel>0) debMsgStd("performElbeemSimulation",DM_NOTIFY,"Using envvar '"<0) debMsgStd("performElbeemSimulation",DM_NOTIFY,"Using envvar '"<10) gDebugLevel = 0; // only use valid values globalFirstEnvCheck = 1; } /* elbeem debug output function */ extern "C" void elbeemDebugOut(char *msg) { elbeemCheckDebugEnv(); // external messages default to debug level 5... if(gDebugLevel<5) return; // delegate to messageOutputFunc messageOutputFunc("[External]",DM_MSG,msg,0); } /* set elbeem debug output level (0=off to 10=full on) */ extern "C" void elbeemSetDebugLevel(int level) { if(level<0) level=0; if(level>10) level=10; gDebugLevel=level; } /* estimate how much memory a given setup will require */ #include "solver_interface.h" extern "C" double elbeemEstimateMemreq(int res, float sx, float sy, float sz, int refine, char *retstr) { int resx = res, resy = res, resz = res; // dont use real coords, just place from 0.0 to sizeXYZ ntlVec3Gfx vgs(0.0), vge(sx,sy,sz); initGridSizes( resx,resy,resz, vgs,vge, refine, 0); double memreq = -1.0; string memreqStr(""); // ignore farfield for now... calculateMemreqEstimate(resx,resy,resz, refine, 0., &memreq, NULL, &memreqStr ); if(retstr) { // copy at max. 32 characters strncpy(retstr, memreqStr.c_str(), 32 ); retstr[31] = '\0'; } return memreq; }