From 320e300c237b2026d35fe7236fd226b9e711eb53 Mon Sep 17 00:00:00 2001 From: 0Arian0 Date: Mon, 13 Jul 2026 16:52:43 +0330 Subject: [PATCH] DEMSystems For Thermal Equations --- DEMSystems/CMakeLists.txt | 5 +- DEMSystems/DEMSystem/DEMSystem.cpp | 147 ++++---- DEMSystems/DEMSystem/DEMSystem.hpp | 314 +++++++++--------- .../thermalSphereDEMSystem.cpp | 254 ++++++++++++++ .../thermalSphereDEMSystem.hpp | 121 +++++++ 5 files changed, 625 insertions(+), 216 deletions(-) create mode 100644 DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.cpp create mode 100644 DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.hpp diff --git a/DEMSystems/CMakeLists.txt b/DEMSystems/CMakeLists.txt index 6cc134e26..9a140f965 100644 --- a/DEMSystems/CMakeLists.txt +++ b/DEMSystems/CMakeLists.txt @@ -2,8 +2,11 @@ set(SourceFiles domainDistribute/domainDistribute.cpp DEMSystem/DEMSystem.cpp -sphereDEMSystem/sphereFluidParticles.cpp sphereDEMSystem/sphereDEMSystem.cpp + +sphereDEMSystem/thermalSphereDEMSystem.cpp +sphereDEMSystem/multiReactiveShrinkingDEMSystem.cpp + grainDEMSystem/grainFluidParticles.cpp grainDEMSystem/grainDEMSystem.cpp ) diff --git a/DEMSystems/DEMSystem/DEMSystem.cpp b/DEMSystems/DEMSystem/DEMSystem.cpp index 9df8447ec..f2b290675 100644 --- a/DEMSystems/DEMSystem/DEMSystem.cpp +++ b/DEMSystems/DEMSystem/DEMSystem.cpp @@ -2,7 +2,7 @@ O C enter of O O E ngineering and O O M ultiscale modeling of - OOOOOOO F luid flow + OOOOOOO F luid flow ------------------------------------------------------------------------------ Copyright (C): www.cemf.ir email: hamid.r.norouzi AT gmail.com @@ -11,80 +11,111 @@ This file is part of phasicFlow code. It is a free software for simulating granular and multiphase flows. You can redistribute it and/or modify it under the terms of GNU General Public License v3 or any other later versions. - - phasicFlow is distributed to help others in their research in the field of - granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the - implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -----------------------------------------------------------------------------*/ -// from phasicFlow #include "KokkosTypes.hpp" - #include "DEMSystem.hpp" - -pFlow::DEMSystem::DEMSystem( - word demSystemName, - const std::vector& domains, - int argc, - char* argv[]) -: - ControlDict_() +namespace pFlow { - - REPORT(0)<<"Initializing host/device execution spaces . . . \n"; - REPORT(1)<<"Host execution space is "<< Green_Text(DefaultHostExecutionSpace::name())<& domains, + int argc, + char* argv[] +) +: + ControlDict_() +{ + // ------------------------------------------------------------------------ + // Kokkos Bootstrapping + // Probes the hardware, allocates thread pools (CPU), and locks GPU resources. + // ------------------------------------------------------------------------ + REPORT(0) << "Initializing host/device execution spaces . . . \n"; + REPORT(1) << "Host execution space is " + << Green_Text(DefaultHostExecutionSpace::name()) << END_REPORT; + REPORT(1) << "Device execution space is " + << Green_Text(DefaultExecutionSpace::name()) << END_REPORT; - REPORT(0)<<"\nCreating Control repository . . ."<( - ControlDict_.startTime(), - ControlDict_.endTime(), - ControlDict_.saveInterval(), - ControlDict_.startTimeName()); + Kokkos::initialize(argc, argv); - timers_ = makeUnique(demSystemName, &Control_().timers()); + // ------------------------------------------------------------------------ + // System Control & Profiling Initialization + // ------------------------------------------------------------------------ + REPORT(0) << "\nCreating Control repository . . ." << END_REPORT; + Control_ = makeUnique + ( + ControlDict_.startTime(), + ControlDict_.endTime(), + ControlDict_.saveInterval(), + ControlDict_.startTimeName() + ); + timers_ = makeUnique(demSystemName, &Control_().timers()); } -pFlow::DEMSystem::~DEMSystem() +DEMSystem::~DEMSystem() { - - Control_.reset(); + // Explicitly reset the control repository to release its memory before + // shutting down the execution spaces. + Control_.reset(); - output<< "\nFinalizing host/device execution space ...."< - pFlow::DEMSystem::create( - word demSystemName, - const std::vector& domains, - int argc, - char* argv[], - bool requireRVel - ) +uniquePtr DEMSystem::create +( + word demSystemName, + const std::vector& domains, + int argc, + char* argv[], + bool requireRVel +) { - if( wordvCtorSelector_.search(demSystemName) ) - { - return wordvCtorSelector_[demSystemName] (demSystemName, domains, argc, argv, requireRVel); - } - else - { - printKeys - ( - fatalError << "Ctor Selector "<< demSystemName << " dose not exist. \n" - <<"Avaiable ones are: \n\n" - , - wordvCtorSelector_ - ); - return nullptr; - } + // Search the macro-generated hash map to see if a class matching + // the requested string has been registered. + if (wordvCtorSelector_.search(demSystemName)) + { + // Invoke the registered constructor function via function pointer + return wordvCtorSelector_[demSystemName] + ( + demSystemName, + domains, + argc, + argv, + requireRVel + ); + } + else + { + // Graceful error handling: List available options if a typo is made + printKeys + ( + fatalError << "Ctor Selector " << demSystemName << " does not exist. \n" + << "Available ones are: \n\n", + wordvCtorSelector_ + ); + return nullptr; + } - return nullptr; + return nullptr; } + +} // namespace pFlow + + diff --git a/DEMSystems/DEMSystem/DEMSystem.hpp b/DEMSystems/DEMSystem/DEMSystem.hpp index f51b91461..274c5b566 100644 --- a/DEMSystems/DEMSystem/DEMSystem.hpp +++ b/DEMSystems/DEMSystem/DEMSystem.hpp @@ -2,20 +2,15 @@ O C enter of O O E ngineering and O O M ultiscale modeling of - OOOOOOO F luid flow + OOOOOOO F luid flow ------------------------------------------------------------------------------ Copyright (C): www.cemf.ir email: hamid.r.norouzi AT gmail.com ------------------------------------------------------------------------------- +------------------------------------------------------------------------------ Licence: - This file is part of phasicFlow code. It is a free software for simulating + This file is part of phasicFlow code. It is a free software for simulating granular and multiphase flows. You can redistribute it and/or modify it under - the terms of GNU General Public License v3 or any other later versions. - - phasicFlow is distributed to help others in their research in the field of - granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the - implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - + the terms of GNU General Public License v3 or any other later versions. -----------------------------------------------------------------------------*/ #ifndef __DEMSystem_hpp__ @@ -31,166 +26,171 @@ #include "systemControl.hpp" #include "readControlDict.hpp" - namespace pFlow { - +/** + * @class DEMSystem + * @brief Base class defining the interface for all discrete element systems. + * * @details + * Provides the overarching API for mechanical, thermal, and chemical + * interactions between particles and coupling layers. Derived classes + * override these virtual methods to expose their specific physics arrays. + */ class DEMSystem { protected: - - readControlDict ControlDict_; - - uniquePtr Control_ = nullptr; - uniquePtr timers_; + // --- Section 1: System Control --- + readControlDict ControlDict_; + uniquePtr Control_ = nullptr; + uniquePtr timers_; public: - TypeInfo("DEMSystem"); - - DEMSystem( - word demSystemName, - const std::vector& domains, - int argc, - char* argv[]); - - virtual ~DEMSystem(); - - DEMSystem(const DEMSystem&)=delete; - - /// @brief no assignment - DEMSystem& operator = (const DEMSystem&)=delete; - - create_vCtor( - DEMSystem, - word, - ( - word demSystemName, - const std::vector& domains, - int argc, - char* argv[], - bool requireRVel - ), - ( - demSystemName, - domains, - argc, - argv, - requireRVel - )); - - realx3 g()const - { - return Control_->g(); - } - - // methods - auto& Control() - { - return Control_(); - } - - const auto& Control()const - { - return Control_(); - } - - auto inline constexpr usingDouble()const - { - return pFlow::usingDouble__; - } - - Timers& timers() - { - return Control_->timers(); - } - - virtual - bool updateParticleDistribution(real extentFraction, const std::vector domains) = 0; - - virtual - int32 numParInDomain(int32 di)const = 0; - - virtual - std::vector numParInDomains()const = 0; - - virtual - span parIndexInDomain(int32 domIndx)const = 0; - - /// return the largest diameter of avaiable shapes - virtual - std::vector shapeDiameters()const = 0; - - virtual - span diameter() = 0; - - virtual - span particleId() = 0; - - virtual - span courseGrainFactor() = 0; - - virtual - span acceleration()=0; - - virtual - span velocity() = 0; - - virtual - span position() = 0; - - virtual - span rAcceleration()=0; - - virtual - span rVelocity() = 0; - - virtual - span rPosition() = 0; - - virtual - span parFluidForce() = 0; - - virtual - span parFluidTorque() = 0; - - virtual - bool sendFluidForceToDEM() = 0; - - virtual - bool sendFluidTorqueToDEM() = 0; - - virtual - real maxBounndingSphereSize()const = 0; - - virtual - bool beforeIteration() = 0; - - virtual - bool iterate( - real upToTime, - real timeToWrite, - word timeName) = 0; - - virtual - bool iterate(real upToTime) = 0; - - - static - uniquePtr - create( - word demSystemName, - const std::vector& domains, - int argc, - char* argv[], - bool requireRVel=false); - + TypeInfo("DEMSystem"); + + // --- Section 2: Constructor / Destructor --- + + DEMSystem + ( + word demSystemName, + const std::vector& domains, + int argc, + char* argv[] + ); + + virtual ~DEMSystem(); + + DEMSystem(const DEMSystem&) = delete; + DEMSystem& operator=(const DEMSystem&) = delete; + + create_vCtor + ( + DEMSystem, + word, + ( + word demSystemName, + const std::vector& domains, + int argc, + char* argv[], + bool requireRVel + ), + ( + demSystemName, + domains, + argc, + argv, + requireRVel + ) + ); + + // --- Section 3: Global Accessors --- + + inline realx3 g() const { return Control_->g(); } + inline auto& Control() { return Control_(); } + inline const auto& Control() const { return Control_(); } + inline constexpr auto usingDouble() const { return pFlow::usingDouble__; } + inline Timers& timers() { return Control_->timers(); } + + // --- Section 4: Pure Virtual Interfaces (Mechanics) --- + + virtual bool updateParticleDistribution + ( + real extentFraction, + const std::vector domains + ) = 0; + + virtual int32 numParInDomain(int32 di) const = 0; + virtual std::vector numParInDomains() const = 0; + virtual span parIndexInDomain(int32 domIndx) const = 0; + virtual real maxBounndingSphereSize() const = 0; + + virtual std::vector shapeDiameters() const = 0; + virtual span diameter() = 0; + virtual span particleId() = 0; + virtual span courseGrainFactor() = 0; + + virtual span acceleration() = 0; + virtual span velocity() = 0; + virtual span position() = 0; + virtual span rAcceleration() = 0; + virtual span rVelocity() = 0; + virtual span rPosition() = 0; + + virtual span parFluidForce() = 0; + virtual span parFluidTorque() = 0; + + virtual bool sendFluidForceToDEM() = 0; + virtual bool sendFluidTorqueToDEM() = 0; + + virtual bool beforeIteration() = 0; + virtual bool iterate(real upToTime, real timeToWrite, word timeName) = 0; + virtual bool iterate(real upToTime) = 0; + + // --- Section 5: Virtual Interfaces (Thermal & Radiation) --- + // Default implementation returns empty spans or false for non-thermal systems. + + virtual span temperature() { return span(); } + virtual span emissivity() { return span(); } + virtual span radSumTemp() { return span(); } + virtual span radNumPrt() { return span(); } + + virtual span parFluidHeatSourceConv() { return span(); } + virtual span parFluidHeatSourceRad() { return span(); } + virtual bool sendFluidHeatSourcesToDEM(){ return false; } + virtual bool hasRadiation() const { return false; } + + virtual span parFluidKappa() { return span(); } + virtual span parFluidAlpha() { return span(); } + virtual bool sendFluidPropertiesToDEM() { return false; } + + // --- Section 6: Virtual Interfaces (Chemical Reactions) --- + // Default implementation returns empty spans or false for non-reactive systems. + + virtual span solidMassFractions() { return span(); } + virtual span gasMassSource() { return span(); } + virtual span gasMassSourceSp() { return span(); } + virtual span gasConcentrations() { return span(); } + + /** + * @brief Per-particle heat deposited in the solid: (1-η) * Q_rxn [W]. + * Drives DEM temperature integration and is available for post-processing. + */ + virtual span reactionHeat() { return span(); } + + /** + * @brief Per-particle heat routed to the CFD energy equation: η * Q_rxn [W]. + * Zero-filled when η = 0 (default behavior for surface reactions). + */ + virtual span reactionHeatFluid() { return span(); } + + /** + * @brief Names of the gas species in the DEM kinetics order. + */ + virtual std::vector gasSpeciesNames() const + { + return std::vector(); + } + + virtual bool sendGasConcentrationsToDEM() { return false; } + + // --- Section 7: Static Factory --- + + static uniquePtr create + ( + word demSystemName, + const std::vector& domains, + int argc, + char* argv[], + bool requireRVel = false + ); }; - -} // pFlow +} // namespace pFlow #endif // __DEMSystem_hpp__ + + + diff --git a/DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.cpp b/DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.cpp new file mode 100644 index 000000000..3cf487961 --- /dev/null +++ b/DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.cpp @@ -0,0 +1,254 @@ +/*------------------------------- phasicFlow --------------------------------- + O C enter of + O O E ngineering and + O O M ultiscale modeling of + OOOOOOO F luid flow +------------------------------------------------------------------------------ + Copyright (C): www.cemf.ir + email: hamid.r.norouzi AT gmail.com +------------------------------------------------------------------------------ +Licence: + This file is part of phasicFlow code. It is a free software for simulating + granular and multiphase flows. You can redistribute it and/or modify it under + the terms of GNU General Public License v3 or any other later versions. +-----------------------------------------------------------------------------*/ + +#include "thermalSphereDEMSystem.hpp" +#include "vocabs.hpp" + +namespace pFlow +{ + +// ========================================================================= // +// Section 1: Constructors +// ========================================================================= // + +pFlow::thermalSphereDEMSystem::thermalSphereDEMSystem +( + word demSystemName, + const std::vector& domains, + int argc, + char* argv[], + bool requireRVel +) +: + sphereDEMSystem(demSystemName, domains, argc, argv, requireRVel) +{ + REPORT(0) << "\nInitializing thermal DEM components..." << END_REPORT; + + // Reset base instances for thermal override + interaction_.reset(); + insertion_.reset(); + particles_.reset(); + spheres_.reset(); + + // 1.1 Load thermal properties + auto thermalProps = thermalProperty + ( + propertyFile__, + Control().caseSetup().path() + ); + + // 1.2 Initialize thermal shapes + auto* combinedShape = new thermalSphereShape + ( + pFlow::shapeFile__, + &Control().caseSetup(), + thermalProps + ); + + spheres_ = uniquePtr(combinedShape); + + // 1.3 Initialize thermal particles on GPU + auto* tp = new thermalSphereParticles + ( + Control(), + *combinedShape, + *combinedShape + ); + + particles_ = uniquePtr(tp); + thermalParticles_ = tp; + + // 1.4 Reconstruct insertion mechanism + insertion_ = makeUnique + ( + particles_(), + particles_().spheres() + ); + + if (!thermalParticles_->initializeThermalParticles()) + { + fatalError << "Failed to initialize thermal properties for particles.\n"; + } + + // 1.5 Reconstruct mechanical interactions + interaction_ = interaction::create + ( + Control(), + Particles(), + Geometry() + ); + + // 1.6 Initialize Unified Thermal Interaction Model + REPORT(0) << "Creating thermal interactions " + << "(Conduction, Radiation, PFP)..." << END_REPORT; + + box localDomain = domains.empty() ? box() : domains[0]; + + thermalInteraction_ = makeUnique + ( + Control(), + *thermalParticles_, + localDomain + ); + + // 1.7 Update distribution boundaries + real minD, maxD; + particles_->boundingSphereMinMax(minD, maxD); + particleDistribution_ = makeUnique(domains, maxD); +} + +// ========================================================================= // +// Section 2: Time Integration Constraints +// ========================================================================= // + +bool pFlow::thermalSphereDEMSystem::iterate +( + real upToTime, + real timeToWrite, + word timeName +) +{ + Control().time().setStopAt(upToTime); + Control().time().setOutputToFile(timeToWrite, timeName); + + return loop(); +} + +bool pFlow::thermalSphereDEMSystem::iterate(real upToTime) +{ + Control().time().setStopAt(upToTime); + + return loop(); +} + +// ========================================================================= // +// Section 3: Core Physics Loop +// ========================================================================= // + +bool pFlow::thermalSphereDEMSystem::loop() +{ + do + { + // 3.1 Handle particle injection triggers + if (!insertion_().insertParticles + ( + Control().time().currentIter(), + Control().time().currentTime(), + Control().time().dt() + )) + { + fatalError << "Particle insertion failed in thermalSphereDEMSystem.\n"; + return false; + } + + // 3.2 Initialize physics accumulators + geometry_->beforeIteration(); + interaction_->beforeIteration(); + particles_->beforeIteration(); + + // 3.3 Mechanical collision evaluation + interaction_->iterate(); + + // 3.4 Thermodynamic evaluation (Q_pp, Q_pfp, Q_rad) + if (thermalInteraction_) + { + thermalInteraction_->iterate(); + } + + // 3.5 Equations of motion and explicit energy integration + particles_->iterate(); + + // 3.6 Clean up and state finalization + geometry_->iterate(); + particles_->afterIteration(); + geometry_->afterIteration(); + + } while(Control()++); + + return true; +} + +// ========================================================================= // +// Section 4: Data Exchange Interfaces (CFD-DEM Coupling) +// ========================================================================= // + +pFlow::span pFlow::thermalSphereDEMSystem::temperature() +{ + auto& hVec = thermalParticles_->temperatureHost(); + return span(hVec.data(), hVec.size()); +} + +pFlow::span pFlow::thermalSphereDEMSystem::emissivity() +{ + auto& hVec = thermalParticles_->emissivityHost(); + return span(hVec.data(), hVec.size()); +} + +pFlow::span pFlow::thermalSphereDEMSystem::radSumTemp() +{ + auto& hVec = thermalParticles_->radSumTempHost(); + return span(hVec.data(), hVec.size()); +} + +pFlow::span pFlow::thermalSphereDEMSystem::radNumPrt() +{ + auto& hVec = thermalParticles_->radNumPrtHost(); + return span(hVec.data(), hVec.size()); +} + +pFlow::span pFlow::thermalSphereDEMSystem::parFluidHeatSourceConv() +{ + auto& hVec = thermalParticles_->heatSourceConvHost(); + return span(hVec.data(), hVec.size()); +} + +pFlow::span pFlow::thermalSphereDEMSystem::parFluidHeatSourceRad() +{ + auto& hVec = thermalParticles_->heatSourceRadHost(); + return span(hVec.data(), hVec.size()); +} + +bool pFlow::thermalSphereDEMSystem::sendFluidHeatSourcesToDEM() +{ + thermalParticles_->heatSourcesHostUpdatedSync(); + return true; +} + +// ========================================================================= // +// Section 5: PFP Pipeline Exchange +// ========================================================================= // + +pFlow::span pFlow::thermalSphereDEMSystem::parFluidKappa() +{ + auto& hVec = thermalParticles_->fluidKappaHost(); + return span(hVec.data(), hVec.size()); +} + +pFlow::span pFlow::thermalSphereDEMSystem::parFluidAlpha() +{ + auto& hVec = thermalParticles_->fluidAlphaHost(); + return span(hVec.data(), hVec.size()); +} + +bool pFlow::thermalSphereDEMSystem::sendFluidPropertiesToDEM() +{ + thermalParticles_->fluidPropertiesHostUpdatedSync(); + return true; +} + +} // namespace pFlow + + + diff --git a/DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.hpp b/DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.hpp new file mode 100644 index 000000000..b72a45eff --- /dev/null +++ b/DEMSystems/sphereDEMSystem/thermalSphereDEMSystem.hpp @@ -0,0 +1,121 @@ +/*------------------------------- phasicFlow --------------------------------- + O C enter of + O O E ngineering and + O O M ultiscale modeling of + OOOOOOO F luid flow +------------------------------------------------------------------------------ + Copyright (C): www.cemf.ir + email: hamid.r.norouzi AT gmail.com +------------------------------------------------------------------------------ +Licence: + This file is part of phasicFlow code. It is a free software for simulating + granular and multiphase flows. You can redistribute it and/or modify it under + the terms of GNU General Public License v3 or any other later versions. +-----------------------------------------------------------------------------*/ + +#ifndef __thermalSphereDEMSystem_hpp__ +#define __thermalSphereDEMSystem_hpp__ + +#include "sphereDEMSystem.hpp" +#include "thermalSphereParticles.hpp" +#include "thermalInteraction.hpp" +#include "thermalProperty.hpp" +#include "thermalSphereShape.hpp" + +namespace pFlow +{ + +/** + * @brief Extends the base mechanical DEM solver to handle thermodynamic + * physics (Conduction, PFP, and Radiation). + */ +class thermalSphereDEMSystem : public sphereDEMSystem +{ +protected: + + // --- Section 1: Core Physics Components --- + + /// @brief Direct host-view access for thermal particles. + thermalSphereParticles* thermalParticles_ = nullptr; + + /// @brief Manages inter-particle conduction, fluid-bridge heat, and radiation. + uniquePtr thermalInteraction_ = nullptr; + + /** + * @brief Core integration loop encompassing mechanical and thermal updates. + * @return True upon successful execution. + */ + bool loop(); + +public: + + /// @brief Runtime type identifier for factory allocation. + TypeInfo("thermalSphereDEMSystem"); + + // --- Section 2: Constructors and Destructors --- + + thermalSphereDEMSystem + ( + word demSystemName, + const std::vector& domains, + int argc, + char* argv[], + bool requireRVel = false + ); + + ~thermalSphereDEMSystem() override = default; + + add_vCtor + ( + DEMSystem, + thermalSphereDEMSystem, + word + ); + + // --- Section 3: Time Integration Controls --- + + bool iterate + ( + real upToTime, + real timeToWrite, + word timeName + ) override; + + bool iterate(real upToTime) override; + + // --- Section 4: Thermodynamic Data Exchange Spans --- + + span temperature() override; + span emissivity() override; + span radSumTemp() override; + span radNumPrt() override; + span parFluidHeatSourceConv() override; + span parFluidHeatSourceRad() override; + + bool sendFluidHeatSourcesToDEM() override; + + // --- Section 5: PFP Sub-grid Model Pipeline --- + + span parFluidKappa() override; + span parFluidAlpha() override; + bool sendFluidPropertiesToDEM() override; + + // --- Section 6: Logic Flags --- + + /** + * @brief Evaluates radiation module availability. + * @return True if the module is active and enabled by user dictionary. + */ + bool hasRadiation() const override + { + return thermalInteraction_ != nullptr && + thermalInteraction_->isRadiationEnabled(); + } +}; + +} // namespace pFlow + +#endif // __thermalSphereDEMSystem_hpp__ + + +