Commit: bfdc42d9906a08ecc8033eeb9fe267e0a7e5265b Author: Sebastián Barschkis Date: Thu Apr 2 13:48:50 2020 +0200 Branches: master https://developer.blender.org/rBbfdc42d9906a08ecc8033eeb9fe267e0a7e5265b
Fluid: Refactored MANTA class Refactored the caching system so that return values are no longer ignored. The aim of this refactor was to make the caching more robust. =================================================================== M intern/mantaflow/intern/MANTA_main.cpp M intern/mantaflow/intern/MANTA_main.h =================================================================== diff --git a/intern/mantaflow/intern/MANTA_main.cpp b/intern/mantaflow/intern/MANTA_main.cpp index d14ab8d6dd3..3555e098caf 100644 --- a/intern/mantaflow/intern/MANTA_main.cpp +++ b/intern/mantaflow/intern/MANTA_main.cpp @@ -48,7 +48,6 @@ #include "MEM_guardedalloc.h" -std::atomic<bool> MANTA::mantaInitialized(false); std::atomic<int> MANTA::solverID(0); int MANTA::with_debug(0); @@ -523,6 +522,7 @@ MANTA::~MANTA() // Destruction string for Python std::string tmpString = ""; std::vector<std::string> pythonCommands; + bool result = false; tmpString += manta_import; tmpString += fluid_delete_all; @@ -530,11 +530,15 @@ MANTA::~MANTA() // Leave out mmd argument in parseScript since only looking up IDs std::string finalString = parseScript(tmpString); pythonCommands.push_back(finalString); - runPythonString(pythonCommands); + result = runPythonString(pythonCommands); + + assert(result); + (void)result; // not needed in release } -void MANTA::runPythonString(std::vector<std::string> commands) +bool MANTA::runPythonString(std::vector<std::string> commands) { + int success = -1; PyGILState_STATE gilstate = PyGILState_Ensure(); for (std::vector<std::string>::iterator it = commands.begin(); it != commands.end(); ++it) { std::string command = *it; @@ -546,19 +550,23 @@ void MANTA::runPythonString(std::vector<std::string> commands) memcpy(buffer, command.data(), cmdLength); buffer[cmdLength] = '\0'; - PyRun_SimpleString(buffer); + success = PyRun_SimpleString(buffer); delete[] buffer; #else - PyRun_SimpleString(command.c_str()); + success = PyRun_SimpleString(command.c_str()); #endif } PyGILState_Release(gilstate); + + /* PyRun_SimpleString returns 0 on success, -1 when an error occurred. */ + assert(success == 0); + return (success != -1); } void MANTA::initializeMantaflow() { if (with_debug) - std::cout << "Initializing Mantaflow" << std::endl; + std::cout << "Fluid: Initializing Mantaflow framework." << std::endl; std::string filename = "manta_scene_" + std::to_string(mCurrentID) + ".py"; std::vector<std::string> fill = std::vector<std::string>(); @@ -568,18 +576,16 @@ void MANTA::initializeMantaflow() PyGILState_STATE gilstate = PyGILState_Ensure(); Pb::setup(filename, fill); // Namespace from Mantaflow (registry) PyGILState_Release(gilstate); - mantaInitialized = true; } void MANTA::terminateMantaflow() { if (with_debug) - std::cout << "Terminating Mantaflow" << std::endl; + std::cout << "Fluid: Releasing Mantaflow framework." << std::endl; PyGILState_STATE gilstate = PyGILState_Ensure(); Pb::finalize(); // Namespace from Mantaflow (registry) PyGILState_Release(gilstate); - mantaInitialized = false; } static std::string getCacheFileEnding(char cache_format) @@ -599,8 +605,8 @@ static std::string getCacheFileEnding(char cache_format) case FLUID_DOMAIN_FILE_OBJECT: return ".obj"; default: - if (MANTA::with_debug) - std::cout << "Error: Could not find file extension" << std::endl; + std::cerr << "Fluid Error -- Could not find file extension. Using default file extension." + << std::endl; return ".uni"; } } @@ -618,8 +624,7 @@ std::string MANTA::getRealValue(const std::string &varName, FluidModifierData *m } if (!mmd) { - if (with_debug) - std::cout << "Invalid modifier data in getRealValue()" << std::endl; + std::cerr << "Fluid Error -- Invalid modifier data." << std::endl; ss << "ERROR - INVALID MODIFIER DATA"; return ss.str(); } @@ -994,7 +999,7 @@ std::string MANTA::getRealValue(const std::string &varName, FluidModifierData *m else if (varName == "USING_DIFFUSION") ss << (mmd->domain->flags & FLUID_DOMAIN_USE_DIFFUSION ? "True" : "False"); else - std::cout << "ERROR: Unknown option: " << varName << std::endl; + std::cerr << "Fluid Error -- Unknown option: " << varName << std::endl; return ss.str(); } @@ -1034,24 +1039,27 @@ std::string MANTA::parseScript(const std::string &setup_string, FluidModifierDat return res.str(); } -int MANTA::updateFlipStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateFlipStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateFlipStructures()" << std::endl; mFlipFromFile = false; + if (!mUsingLiquid) + return false; + if (BLI_path_is_rel(mmd->domain->cache_directory)) + return false; + + int result = 0; + int expected = 0; /* Expected number of read successes for this frame. */ + // Ensure empty data structures at start if (mFlipParticleData) mFlipParticleData->clear(); if (mFlipParticleVelocity) mFlipParticleVelocity->clear(); - if (!mUsingLiquid) - return 0; - if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; - std::ostringstream ss; char cacheDir[FILE_MAX], targetFile[FILE_MAX]; cacheDir[0] = '\0'; @@ -1061,27 +1069,31 @@ int MANTA::updateFlipStructures(FluidModifierData *mmd, int framenr) BLI_path_join( cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_DATA, nullptr); + expected += 1; + ss.str(""); ss << "pp_####" << pformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, false, false); + result += updateParticlesFromFile(targetFile, false, false); + assert(result == expected); } + expected += 1; ss.str(""); ss << "pVel_####" << pformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, false, true); + result += updateParticlesFromFile(targetFile, false, true); + assert(result == expected); } + mFlipFromFile = true; - return 1; + return (result == expected); } -int MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateMeshStructures()" << std::endl; @@ -1089,9 +1101,12 @@ int MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) mMeshFromFile = false; if (!mUsingMesh) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; + + int result = 0; + int expected = 0; /* Expected number of read successes for this frame. */ // Ensure empty data structures at start if (mMeshNodes) @@ -1111,29 +1126,33 @@ int MANTA::updateMeshStructures(FluidModifierData *mmd, int framenr) BLI_path_join( cacheDir, sizeof(cacheDir), mmd->domain->cache_directory, FLUID_DOMAIN_DIR_MESH, nullptr); + expected += 1; + ss.str(""); ss << "lMesh_####" << mformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateMeshFromFile(targetFile); + result += updateMeshFromFile(targetFile); + assert(result == expected); } if (mUsingMVel) { + expected += 1; ss.str(""); ss << "lVelMesh_####" << dformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateMeshFromFile(targetFile); + result += updateMeshFromFile(targetFile); + assert(result == expected); } } + mMeshFromFile = true; - return 1; + return (result == expected); } -int MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateParticleStructures()" << std::endl; @@ -1141,9 +1160,12 @@ int MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) mParticlesFromFile = false; if (!mUsingDrops && !mUsingBubbles && !mUsingFloats && !mUsingTracers) - return 0; + return false; if (BLI_path_is_rel(mmd->domain->cache_directory)) - return 0; + return false; + + int result = 0; + int expected = 0; /* Expected number of read successes for this frame. */ // Ensure empty data structures at start if (mSndParticleData) @@ -1165,36 +1187,41 @@ int MANTA::updateParticleStructures(FluidModifierData *mmd, int framenr) FLUID_DOMAIN_DIR_PARTICLES, nullptr); + expected += 1; + ss.str(""); ss << "ppSnd_####" << pformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, true, false); + result += updateParticlesFromFile(targetFile, true, false); + assert(result == expected); } + expected += 1; ss.str(""); ss << "pVelSnd_####" << pformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, true, true); + result += updateParticlesFromFile(targetFile, true, true); + assert(result == expected); } + expected += 1; ss.str(""); ss << "pLifeSnd_####" << pformat; BLI_join_dirfile(targetFile, sizeof(targetFile), cacheDir, ss.str().c_str()); BLI_path_frame(targetFile, framenr, 0); - if (BLI_exists(targetFile)) { - updateParticlesFromFile(targetFile, true, false); + result += updateParticlesFromFile(targetFile, true, false); + assert(result == expected); } + mParticlesFromFile = true; - return 1; + return (result == expected); } -int MANTA::updateSmokeStructures(FluidModifierData *mmd, int framenr) +bool MANTA::updateSmokeStructures(FluidModifierData *mmd, int framenr) { if (MANTA::with_debug) std::cout << "MANTA::updateGridStructures()" << std::endl; @@ -1202,9 +1229,9 @@ int MANTA::updateSmokeStructures(FluidModifierData * @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs