diff --git a/src/dfChemistryModel/DNNInferencer/DNNInferencer.cpp b/src/dfChemistryModel/DNNInferencer/DNNInferencer.cpp index 0092bb6e..69aa784e 100644 --- a/src/dfChemistryModel/DNNInferencer/DNNInferencer.cpp +++ b/src/dfChemistryModel/DNNInferencer/DNNInferencer.cpp @@ -265,7 +265,7 @@ std::vector> DNNInferencer::Inference_multiDNNs(std::vector< std::chrono::steady_clock::time_point stop = std::chrono::steady_clock::now(); std::chrono::duration processingTime = std::chrono::duration_cast>(stop - start); - std::cout << "preInf time = " << processingTime.count() << std::endl; + // std::cout << "preInf time = " << processingTime.count() << std::endl; time_preInf += processingTime.count(); // inference @@ -288,7 +288,7 @@ std::vector> DNNInferencer::Inference_multiDNNs(std::vector< std::chrono::steady_clock::time_point stop1 = std::chrono::steady_clock::now(); std::chrono::duration processingTime1 = std::chrono::duration_cast>(stop1 - start1); - std::cout << "Inf time = " << processingTime1.count() << std::endl; + // std::cout << "Inf time = " << processingTime1.count() << std::endl; time_Inference += processingTime1.count(); // generate outputTensor @@ -322,7 +322,7 @@ std::vector> DNNInferencer::Inference_multiDNNs(std::vector< std::chrono::steady_clock::time_point stop3 = std::chrono::steady_clock::now(); std::chrono::duration processingTime3 = std::chrono::duration_cast>(stop3 - start3); - std::cout << "hot time = " << processingTime3.count() << std::endl; + // std::cout << "hot time = " << processingTime3.count() << std::endl; time_hot += processingTime3.count(); std::vector RRoutputs0(Youtputs0.data_ptr(), Youtputs0.data_ptr() + Youtputs0.numel()); @@ -333,13 +333,13 @@ std::vector> DNNInferencer::Inference_multiDNNs(std::vector< std::chrono::steady_clock::time_point stop2 = std::chrono::steady_clock::now(); std::chrono::duration processingTime2 = std::chrono::duration_cast>(stop2 - start2); - std::cout << "postInf time = " << processingTime2.count() << std::endl; + // std::cout << "postInf time = " << processingTime2.count() << std::endl; time_postInf += processingTime2.count(); - std::cout << "preInf sum time = " << time_preInf << std::endl; - std::cout << "Inf sum time = " << time_Inference << std::endl; - std::cout << "postInf sum time = " << time_postInf << std::endl; - std::cout << "hot sum time = " << time_hot << std::endl; + // std::cout << "preInf sum time = " << time_preInf << std::endl; + // std::cout << "Inf sum time = " << time_Inference << std::endl; + // std::cout << "postInf sum time = " << time_postInf << std::endl; + // std::cout << "hot sum time = " << time_hot << std::endl; return results; } diff --git a/src/dfChemistryModel/dfChemistryModel.C b/src/dfChemistryModel/dfChemistryModel.C index 334be4d0..a0b4a7c9 100644 --- a/src/dfChemistryModel/dfChemistryModel.C +++ b/src/dfChemistryModel/dfChemistryModel.C @@ -99,7 +99,14 @@ Foam::dfChemistryModel::dfChemistryModel ) { #ifdef USE_LIBTORCH + useDNN = true; + if (!Qdot_.typeHeaderOk()) + { + useDNN = false; + } + torchSwitch_ = this->lookupOrDefault("torch", false); + gpu_ = this->lookupOrDefault("GPU", false), torchModelName_ = this->lookupOrDefault("torchModel", word("")); torchModelName1_ = this->lookupOrDefault("torchModel1", word("")); @@ -125,14 +132,23 @@ Foam::dfChemistryModel::dfChemistryModel { if(!(Pstream::myProcNo() % coresPerGPU)) // Now is a master { - int CUDANo = (Pstream::myProcNo() / coresPerGPU) % GPUsPerNode; - std::string device_ = "cuda:" + std::to_string(CUDANo); + Info << "location 0" << endl; Info << "torchModelName1_ = " << torchModelName1_ << endl; torch::jit::script::Module torchModel1_ = torch::jit::load(torchModelName1_); torch::jit::script::Module torchModel2_ = torch::jit::load(torchModelName2_); torch::jit::script::Module torchModel3_ = torch::jit::load(torchModelName3_); Info << "location 1" << endl; + std::string device_; + if (gpu_) + { + int CUDANo = (Pstream::myProcNo() / coresPerGPU) % GPUsPerNode; + device_ = "cuda:" + std::to_string(CUDANo); + } + else + { + device_ = "cpu"; + } DNNInferencer DNNInferencer(torchModel1_, torchModel2_, torchModel3_, device_); DNNInferencer_ = DNNInferencer; } @@ -140,6 +156,13 @@ Foam::dfChemistryModel::dfChemistryModel #endif #ifdef USE_PYTORCH + useDNN = true; + if (!Qdot_.typeHeaderOk()) + { + useDNN = false; + } + gpu_ = this->lookupOrDefault("GPU", false), + torchSwitch_ = this->lookupOrDefault("torch", false); Tact1_ = this->subDict("torchParameters1").lookupOrDefault("Tact", 700); @@ -276,23 +299,39 @@ Foam::scalar Foam::dfChemistryModel::solve #ifdef USE_LIBTORCH if(torchSwitch_) { - result = solve_DNN_GPU(deltaT); + if (useDNN) + { + result = solve_DNN(deltaT); + } + else + { + result = solve_CVODE(deltaT); + useDNN = true; + } } else { - result = solve_loadBalance(deltaT); + result = solve_CVODE(deltaT); } #elif USE_PYTORCH if(torchSwitch_) { - result = solve_DNN_GPU(deltaT); + if (useDNN) + { + result = solve_DNN(deltaT); + } + else + { + result = solve_CVODE(deltaT); + useDNN = true; + } } else { - result = solve_loadBalance(deltaT); + result = solve_CVODE(deltaT); } #else - result = solve_loadBalance(deltaT); + result = solve_CVODE(deltaT); #endif return result; @@ -331,152 +370,6 @@ void Foam::dfChemistryModel::setNumerics(Cantera::ReactorNet &sim) #ifdef USE_LIBTORCH -template -template -Foam::scalar Foam::dfChemistryModel::solve_DNN -( - const DeltaTType& deltaT -) -{ - scalar deltaTMin = great; - - if (!this->chemistry_) - { - return deltaTMin; - } - - Info<<"=== begin solve_DNN === "< torch_cell; - label torch_cellname= 0; - - // obtain the number of DNN cells - forAll(T_, cellI) - { - if ((Qdot_[cellI] >= Qdotact_) && (T_[cellI] >= Tact_)) - { - torch_cell.push_back(cellI); - } - } - - torch::Tensor inputs(torch::zeros({torch_cell.size(),mixture_.nSpecies()+2})); - forAll(T_, cellI) - { - scalar Ti = T_[cellI]; - scalar pi = p_[cellI]; - scalar rhoi = rho_[cellI]; - - if ((Qdot_[cellI] >= Qdotact_) && (Ti >= Tact_)) - { - Qdot_[cellI] = 0.0; - // Info<<"Now is DNN "< inputs_; - inputs_.push_back((Ti - Xmu_[0])/Xstd_[0]); - inputs_.push_back((pi / 101325 - Xmu_[1])/Xstd_[1]); - for (size_t i=0; inSpecies(); ++i) - { - yPre_[i] = Y_[i][cellI]; - yBCT_[i] = (pow(yPre_[i],lambda) - 1) / lambda; // function BCT - } - for (size_t i=0; inSpecies(); ++i) - { - inputs_.push_back((yBCT_[i] - Xmu_[i+2]) / Xstd_[i+2]); - } - inputs[torch_cellname] = torch::tensor(inputs_); - torch_cellname ++; - // Info << "cell_name = "<< cellI <nSpecies(); ++i) - { - yPre_[i] = Y_[i][cellI]; - } - - CanteraGas_->setState_TPY(Ti, pi, yPre_.begin()); - react.insert(mixture_.CanteraSolution()); - react.setEnergy(0); - - Cantera::ReactorNet sim; - sim.addReactor(react); - setNumerics(sim); - sim.advance(deltaT[cellI]); - - CanteraGas_->getMassFractions(yTemp_.begin()); - - for (size_t i=0; inSpecies(); ++i) - { - RR_[i][cellI] = (yTemp_[i] - yPre_[i])*rhoi/deltaT[cellI]; - Qdot_[cellI] -= hc_[i]*RR_[i][cellI]; - } - } - } - // DNN - std::vector INPUTS{inputs}; - auto outputs_raw = torchModel_.forward(INPUTS); - auto outputs = outputs_raw.toTensor(); - - for(size_t cellI = 0; cellInSpecies(); ++i) - { - yPre_[i] = Y_[i][torch_cell[cellI]]; - yTemp_[i] = Y_[i][torch_cell[cellI]]; - yBCT_[i] = (pow(yPre_[i],lambda) - 1) / lambda; // function BCT - } - for (size_t i=0; i<(CanteraGas_->nSpecies()); ++i)// - { - u_[i+2] = outputs[cellI][i+2].item().to()*Ystd_[i+2]+Ymu_[i+2]; - yTemp_[i] = pow((yBCT_[i] + u_[i+2]*deltaT[cellI])*lambda+1,1/lambda); - Yt += yTemp_[i]; - } - for (size_t i=0; inSpecies(); ++i) - { - yTemp_[i] = yTemp_[i] / Yt; - RR_[i][torch_cell[cellI]] = (yTemp_[i] - Y_[i][torch_cell[cellI]])*rho_[torch_cell[cellI]]/deltaT[cellI]; - Qdot_[torch_cell[cellI]] -= hc_[i]*RR_[i][torch_cell[cellI]]; - } - } - - Info<<"=== end torch&ode-solve === "< template Foam::DynamicList @@ -520,34 +413,34 @@ Foam::dfChemistryModel::getGPUProblems // choose DNN module if ((Qdot_[cellI] < Qdotact2_) && (T_[cellI] < Tact2_) && ( T_[cellI] >= Tact1_))//choose1 { - problem.DNNid = 0; + problem.DNNid = 0; problemList.append(problem); continue; } if(((Qdot_[cellI] >= Qdotact2_) && (T_[cellI] < Tact2_)&&(T_[cellI] >= Tact1_))||((Qdot_[cellI] > Qdotact3_) && T_[cellI] > Tact2_)) //choose2 { - problem.DNNid = 1; + problem.DNNid = 1; problemList.append(problem); continue; } if ((Qdot_[cellI] < Qdotact3_) && (T_[cellI] >= Tact2_) && (Qdot_[cellI]!=0)) //choose3 { - problem.DNNid = 2; + problem.DNNid = 2; problemList.append(problem); continue; } } - return problemList; + return problemList; } template void Foam::dfChemistryModel::getDNNinputs ( - const Foam::DynamicBuffer& problemBuffer, + const Foam::DynamicBuffer& problemBuffer, std::vector