From bb5c3a6ff6cbdf22530981a039d3da29dfb22bc0 Mon Sep 17 00:00:00 2001 From: Dwyer Deighan Date: Tue, 31 Oct 2023 16:54:38 -0400 Subject: [PATCH 1/9] commiting clarifying comments and & function definitions + some helpful utility functions for debugging --- src/eos/chemTab.cpp | 87 +++++++++++++++++++++++++++++++++++---------- src/eos/chemTab.hpp | 29 +++++++++++---- 2 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index 05863b4d6..e79206312 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -1,7 +1,6 @@ #include "chemTab.hpp" #include -#include #ifdef WITH_TENSORFLOW #include @@ -90,9 +89,9 @@ ablate::eos::ChemTab::~ChemTab() { TF_DeleteSession(session, status); TF_DeleteSessionOptions(sessionOpts); TF_DeleteStatus(status); + free(sourceEnergyScaler); for (std::size_t i = 0; i < speciesNames.size(); i++) free(Wmat[i]); - free(Wmat); } @@ -153,8 +152,8 @@ void ablate::eos::ChemTab::LoadBasisVectors(std::istream &inputStream, std::size #define safe_free(ptr) \ if (ptr != NULL) free(ptr) -void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const PetscReal densityProgressVariable[], PetscReal *predictedSourceEnergy, PetscReal *progressVariableSource, - PetscReal *densityMassFractions) const { +void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const PetscReal densityProgressVariables[], PetscReal *densityEnergySource, + PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions) const { //********* Get Input tensor const std::size_t numInputs = 1; @@ -185,7 +184,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const float data[ninputs]; for (std::size_t i = 0; i < ninputs; i++) { - data[i] = (float)(densityProgressVariable[i] / density); + data[i] = (float)(densityProgressVariables[i] / density); } std::size_t ndata = ninputs * sizeof(float); @@ -202,7 +201,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const float *outputArray; // Dwyer: as counter intuitive as it may be static dependents come second, it did pass its tests! outputArray = (float *)TF_TensorData(outputValues[1]); auto p = (PetscReal)outputArray[0]; - if (predictedSourceEnergy != nullptr) *predictedSourceEnergy += p * density; + if (densityEnergySource != nullptr) *densityEnergySource += p * density; // store inverted mass fractions if (densityMassFractions) { @@ -213,12 +212,12 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const // store CPV sources outputArray = (float *)TF_TensorData(outputValues[0]); - if (progressVariableSource != nullptr) { - // progressVariableSource[0] = 0; // Zmix source is always 0! We + if (densityProgressVariableSource != nullptr) { + //densityProgressVariableSource[0] = 0; // Zmix source is always 0! - // -1 b/c we don't want to go out of bounds with the +1 below, also int is to prevent integer overflow + // -1 b/c we don't want to go out of bounds with the +1 below, also to prevent integer overflow for (size_t i = 0; i < (progressVariablesNames.size() - 1); ++i) { - progressVariableSource[i + 1] += (PetscReal)outputArray[i] * density; // +1 b/c we are manually filling in Zmix source value (to 0) + densityProgressVariableSource[i + 1] += (PetscReal)outputArray[i] * density; // +1 b/c we are manually filling in Zmix source value (to 0) } } @@ -231,21 +230,25 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const } } -void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *progressVariables, PetscReal *densityMassFractions, PetscReal density) const { +void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *densityProgressVariables, PetscReal *densityMassFractions, const PetscReal density) const { // call model using generalized invocation method (usable for inversion & source computation) - ChemTabModelComputeFunction(density, progressVariables, nullptr, nullptr, densityMassFractions); + ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, nullptr, densityMassFractions); } -void ablate::eos::ChemTab::ComputeMassFractions(const std::vector &progressVariables, std::vector &massFractions, PetscReal density) const { +void ablate::eos::ChemTab::ComputeMassFractions(std::vector &progressVariables, std::vector &massFractions, PetscReal density) const { if (progressVariables.size() != progressVariablesNames.size()) { - throw std::invalid_argument("The Progress variable size is expected to be " + std::to_string(progressVariablesNames.size())); + throw std::invalid_argument( + "The Progress variable size is expected to be " + std::to_string(progressVariablesNames.size())); } if (massFractions.size() != speciesNames.size()) { throw std::invalid_argument("The Species names for massFractions is expected to be " + std::to_string(progressVariablesNames.size())); } + // the naming is wrong on purpose so that it will conform to tests. ComputeMassFractions(progressVariables.data(), massFractions.data(), density); + //ComputeProgressVariables(massFractions.data(), progressVariables.data()); } +// Apparently only used for tests! void ablate::eos::ChemTab::ComputeProgressVariables(const std::vector &massFractions, std::vector &progressVariables) const { if (progressVariables.size() != progressVariablesNames.size()) { throw std::invalid_argument("The Progress variable size is expected to be " + std::to_string(progressVariablesNames.size())); @@ -256,6 +259,7 @@ void ablate::eos::ChemTab::ComputeProgressVariables(const std::vector ComputeProgressVariables(massFractions.data(), progressVariables.data()); } +// This is real one used elsewhere probably because it is faster void ablate::eos::ChemTab::ComputeProgressVariables(const PetscReal *massFractions, PetscReal *progressVariables) const { // c = W'y for (size_t i = 0; i < progressVariablesNames.size(); i++) { @@ -267,13 +271,37 @@ void ablate::eos::ChemTab::ComputeProgressVariables(const PetscReal *massFractio } } -void ablate::eos::ChemTab::ChemistrySource(PetscReal density, const PetscReal densityProgressVariable[], PetscReal *densityEnergySource, PetscReal *progressVariableSource) const { +inline double L2_norm(PetscReal* array, int n) { + double norm=0; + for (int i=0; i ablate::eos::ChemTab::CreateSourceCalculator(const std::vector &fields, const ablate::domain::Range &cellRange) { // Look for the euler field auto eulerField = std::find_if(fields.begin(), fields.end(), [](const auto &field) { return field.name == ablate::finiteVolume::CompressibleFlowFields::EULER_FIELD; }); @@ -332,6 +360,7 @@ ablate::eos::EOSFunction ablate::eos::ChemTab::GetFieldFunctionFunction(const st // Compute the mass fractions from progress std::vector yi(speciesNames.size()); + // TODO: change for batch processing!! (ask Matt about it) // compute the progress variables and put into conserved for now ComputeMassFractions(progress, yi.data()); @@ -377,6 +406,7 @@ ablate::eos::EOSFunction ablate::eos::ChemTab::GetFieldFunctionFunction(const st // Compute the mass fractions from progress PetscReal yi[speciesNames.size()]; + // TODO: change for batch processing!! (ask Matt about it) // compute the progress variables and put into conserved for now ComputeMassFractions(progress, yi); @@ -426,6 +456,7 @@ PetscErrorCode ablate::eos::ChemTab::ComputeMassFractions(PetscReal time, PetscI // Get the density from euler PetscReal density = u[uOff[EULER] + finiteVolume::CompressibleFlowFields::RHO]; + // TODO: change for batch processing!! (ask Matt about it) // call the compute mass fractions chemTab->ComputeMassFractions(u + uOff[DENSITY_PROGRESS], u + uOff[DENSITY_YI], density); @@ -444,6 +475,8 @@ ablate::eos::ChemTab::ChemTabSourceCalculator::ChemTabSourceCalculator(PetscInt std::shared_ptr chemTabModel) : densityOffset(densityOffset), densityEnergyOffset(densityEnergyOffset), densityProgressVariableOffset(densityProgressVariableOffset), chemTabModel(std::move(chemTabModel)) {} +// NOTE: I'm not sure however I believe that this could be the ONLY place that needs updating for Batch processing?? +// Comments seem to indicate it is the case... void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::domain::Range &cellRange, Vec locX, Vec locFVec) { // get access to the xArray, fArray PetscScalar *fArray; @@ -452,22 +485,38 @@ void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::doma VecGetArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError; // Get the solution dm - DM dm; + DM dm; // NOTE: DM is topological space (i.e. grid) VecGetDM(locFVec, &dm) >> utilities::PetscUtilities::checkError; + // TODO: change this for batch processing!! // March over each cell in the range for (PetscInt c = cellRange.start; c < cellRange.end; ++c) { const PetscInt iCell = cellRange.points ? cellRange.points[c] : c; + // Dwyer: iCell is the "point" + + // Def: PetscErrorCode DMPlexPointLocalRef(DM dm, PetscInt point, PetscScalar *array, void *ptr) + // Help: return read/write access to a point in local array + // :param array: - array to index into + // :param ptr: output reference/return value - // Get the current state variables for this cell + // Get the current source variables for this cell PetscScalar *sourceAtCell = nullptr; DMPlexPointLocalRef(dm, iCell, fArray, &sourceAtCell) >> utilities::PetscUtilities::checkError; - // Get the current state variables for this cell + // Get the current state variables for this cell (CPVs) const PetscScalar *solutionAtCell = nullptr; DMPlexPointLocalRead(dm, iCell, xArray, &solutionAtCell) >> utilities::PetscUtilities::checkError; - chemTabModel->ChemistrySource(solutionAtCell[densityOffset], solutionAtCell + densityProgressVariableOffset, sourceAtCell + densityEnergyOffset, sourceAtCell + densityProgressVariableOffset); + // Def: PetscErrorCode DMPlexPointLocalRead(DM dm, PetscInt point, const PetscScalar *array, void *ptr) + // Help: return read access to a point in local array + // NOTE: The only difference is that DMPlexPointLocalRef gives read/write access + // & DMPlexPointLocalRead gives only ready access + + // Def: ChemTab::ChemistrySource(PetscReal density, const PetscReal densityProgressVariable[], PetscReal *densityEnergySource, PetscReal *progressVariableSource) + // Help: last 2 (Souener & CPV_source) are the "return values" + chemTabModel->ChemistrySource(solutionAtCell[densityOffset], solutionAtCell + densityProgressVariableOffset, + sourceAtCell + densityEnergyOffset, sourceAtCell + densityProgressVariableOffset); + // NOTE: These "offsets" are pointers since they are CONSTANT class attributes! } // cleanup VecRestoreArray(locFVec, &fArray) >> utilities::PetscUtilities::checkError; diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index d1771a032..1979bef8b 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -47,12 +47,12 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this& progressVariables, std::vector& massFractions, PetscReal density = 1.0) const; + void ComputeMassFractions(std::vector& progressVariables, std::vector& massFractions, PetscReal density = 1.0) const; + +// /** +// * helper function to compute the mass fractions = from the mass fractions progress variables +// * @param progressVariables is density*progress +// * @param massFractions +// * @param density allows for this function to be used with density*progress variables +// */ +// void ComputeMassFractions(PetscReal* progressVariables, PetscReal* massFractions, PetscReal density = 1.0) const; /** * helper function to compute the mass fractions = from the mass fractions progress variables From 24291fb15649356f5a95c614358a2a2ff23db1b8 Mon Sep 17 00:00:00 2001 From: Dwyer Deighan Date: Tue, 31 Oct 2023 18:53:38 -0400 Subject: [PATCH 2/9] fixed problem with previous commit where consts weren't being used where needed --- src/eos/chemTab.cpp | 2 +- src/eos/chemTab.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index e79206312..e94d24d9d 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -288,7 +288,7 @@ inline void print_array(std::string prefix, PetscReal* array, const int n) { std::cout << std::endl; } -void ablate::eos::ChemTab::ChemistrySource(const PetscReal density, PetscReal densityProgressVariables[], +void ablate::eos::ChemTab::ChemistrySource(const PetscReal density, const PetscReal densityProgressVariables[], PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource) const { // call model using generalized invocation method (usable for inversion & source computation) ChemTabModelComputeFunction(density, densityProgressVariables, densityEnergySource, densityProgressVariableSource, nullptr); diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index 1979bef8b..e39a419f6 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -146,7 +146,7 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this Date: Tue, 31 Oct 2023 20:08:09 -0400 Subject: [PATCH 3/9] first draft of dummy batch api changes essentially passing to dummy batch function which manually calls the unbatched version, but good test case. --- src/eos/chemTab.cpp | 51 ++++++++++++++++++++++++++++++++++++--------- src/eos/chemTab.hpp | 2 +- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index e94d24d9d..dc4a16fe3 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -294,9 +294,14 @@ void ablate::eos::ChemTab::ChemistrySource(const PetscReal density, const PetscR ChemTabModelComputeFunction(density, densityProgressVariables, densityEnergySource, densityProgressVariableSource, nullptr); } -void ablate::eos::ChemTab::ChemistrySourceBatch(PetscReal density, const PetscReal densityProgressVariable[][], PetscReal** densityEnergySource, PetscReal** progressVariableSource) const { - // call model using generalized invocation method (usable for inversion & source computation) - ChemTabModelComputeFunction(density, densityProgressVariable, densityEnergySource, progressVariableSource, nullptr); +void ablate::eos::ChemTab::ChemistrySourceBatch(const PetscReal*const density, const PetscReal*const*const densityProgressVariable, + PetscReal** densityEnergySource, PetscReal** progressVariableSource, int n) const { + // for now we are implementing batch in the same way that single calls happened + // but testing that this works prepares the api for the real thing! + for (int i=0; i> utilities::PetscUtilities::checkError; + // Here we store the batched pointers needed for multiple chemTabModel->ChemistrySource() calls + PetscInt buffer_len = cellRange.end - cellRange.start; + //PetscScalar* allSourceAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets + //const PetscScalar* allSolutionAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets + PetscScalar allDensity[buffer_len]; + const PetscScalar* allDensityCPV[buffer_len]; + PetscScalar* allDensityEnergySource[buffer_len]; + PetscScalar* allDensityCPVSource[buffer_len]; + // TODO: change this for batch processing!! // March over each cell in the range for (PetscInt c = cellRange.start; c < cellRange.end; ++c) { @@ -499,25 +513,42 @@ void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::doma // :param array: - array to index into // :param ptr: output reference/return value - // Get the current source variables for this cell - PetscScalar *sourceAtCell = nullptr; +// // Get the current source variables for this cell +// DMPlexPointLocalRef(dm, iCell, fArray, &allSourceAtCell[c-cellRange.start]) >> utilities::PetscUtilities::checkError; + + PetscScalar* sourceAtCell = nullptr; DMPlexPointLocalRef(dm, iCell, fArray, &sourceAtCell) >> utilities::PetscUtilities::checkError; + //assert(sourceAtCell==allSourceAtCell[c-cellRange.start]); // silly sanity check // Get the current state variables for this cell (CPVs) - const PetscScalar *solutionAtCell = nullptr; + const PetscScalar* solutionAtCell = nullptr; DMPlexPointLocalRead(dm, iCell, xArray, &solutionAtCell) >> utilities::PetscUtilities::checkError; + //allSolutionAtCell[c-cellRange.start]=solutionAtCell; + //DMPlexPointLocalRead(dm, iCell, xArray, &allSolutionAtCell[c-cellRange.start]) >> utilities::PetscUtilities::checkError; + //assert(solutionAtCell==allSolutionAtCell[c-cellRange.start]); // silly sanity check // Def: PetscErrorCode DMPlexPointLocalRead(DM dm, PetscInt point, const PetscScalar *array, void *ptr) // Help: return read access to a point in local array // NOTE: The only difference is that DMPlexPointLocalRef gives read/write access // & DMPlexPointLocalRead gives only ready access - // Def: ChemTab::ChemistrySource(PetscReal density, const PetscReal densityProgressVariable[], PetscReal *densityEnergySource, PetscReal *progressVariableSource) - // Help: last 2 (Souener & CPV_source) are the "return values" - chemTabModel->ChemistrySource(solutionAtCell[densityOffset], solutionAtCell + densityProgressVariableOffset, - sourceAtCell + densityEnergyOffset, sourceAtCell + densityProgressVariableOffset); + // store this cell's attributes into arg vectors + size_t index = c-cellRange.start; + allDensity[index]=solutionAtCell[densityOffset]; + allDensityCPV[index]=solutionAtCell + densityProgressVariableOffset; + allDensityEnergySource[index]=sourceAtCell + densityEnergyOffset; + allDensityCPVSource[index]=sourceAtCell + densityProgressVariableOffset; + +// // Def: ChemTab::ChemistrySource(PetscReal density, const PetscReal densityProgressVariable[], PetscReal *densityEnergySource, PetscReal *progressVariableSource) +// // Help: last 2 (Souener & CPV_source) are the "return values" +// chemTabModel->ChemistrySource(solutionAtCell[densityOffset], solutionAtCell + densityProgressVariableOffset, +// sourceAtCell + densityEnergyOffset, sourceAtCell + densityProgressVariableOffset); // NOTE: These "offsets" are pointers since they are CONSTANT class attributes! } + + chemTabModel->ChemistrySourceBatch(allDensity, allDensityCPV,allDensityEnergySource, + allDensityCPVSource, buffer_len); + // cleanup VecRestoreArray(locFVec, &fArray) >> utilities::PetscUtilities::checkError; VecRestoreArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError; diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index e39a419f6..220fe1346 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -154,7 +154,7 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this Date: Tue, 31 Oct 2023 21:41:43 -0400 Subject: [PATCH 4/9] added dummy batched version of every function, each of which relies on the single dummy batched ChemTabModelComputeFunction --- src/eos/chemTab.cpp | 62 ++++++++++++++++++++++++++++++++++----------- src/eos/chemTab.hpp | 27 ++++++++++++-------- 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index dc4a16fe3..c99310cda 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -198,7 +198,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const //********** Extract source predictions // store physical variables (e.g. souener & mass fractions) - float *outputArray; // Dwyer: as counter intuitive as it may be static dependents come second, it did pass its tests! + float *outputArray; // Dwyer: as counterintuitive as it may be static dependents come second, it did pass its tests! outputArray = (float *)TF_TensorData(outputValues[1]); auto p = (PetscReal)outputArray[0]; if (densityEnergySource != nullptr) *densityEnergySource += p * density; @@ -230,9 +230,19 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const } } -void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *densityProgressVariables, PetscReal *densityMassFractions, const PetscReal density) const { - // call model using generalized invocation method (usable for inversion & source computation) - ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, nullptr, densityMassFractions); +#define safe_id(array, i) (array ? array[i] : nullptr) + +void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal*const*const densityProgressVariables, + PetscReal** densityEnergySource, PetscReal** densityProgressVariableSource, + PetscReal** densityMassFractions, size_t n) const { + // for now we are implementing batch in the same way that single calls happened + // but testing that this works prepares the api for the real thing! + for (size_t i=0; i &progressVariables, std::vector &massFractions, PetscReal density) const { @@ -248,6 +258,28 @@ void ablate::eos::ChemTab::ComputeMassFractions(std::vector &progress //ComputeProgressVariables(massFractions.data(), progressVariables.data()); } +void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *densityProgressVariables, PetscReal *densityMassFractions, + const PetscReal density) const { + // call model using generalized invocation method (usable for inversion & source computation) + ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, + nullptr, densityMassFractions); +} + +void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal*const* densityProgressVariables, PetscReal** densityMassFractions, + const PetscReal density[], size_t n) const { + ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, + nullptr, densityMassFractions, n); +} + + +// Batched Version +void ablate::eos::ChemTab::ComputeProgressVariables(const PetscReal *const *massFractions, + PetscReal *const *progressVariables, size_t n) const { + for (size_t i = 0; i < n; i++) { + ComputeProgressVariables(massFractions[i], progressVariables[i]); + } +} + // Apparently only used for tests! void ablate::eos::ChemTab::ComputeProgressVariables(const std::vector &massFractions, std::vector &progressVariables) const { if (progressVariables.size() != progressVariablesNames.size()) { @@ -291,17 +323,16 @@ inline void print_array(std::string prefix, PetscReal* array, const int n) { void ablate::eos::ChemTab::ChemistrySource(const PetscReal density, const PetscReal densityProgressVariables[], PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource) const { // call model using generalized invocation method (usable for inversion & source computation) - ChemTabModelComputeFunction(density, densityProgressVariables, densityEnergySource, densityProgressVariableSource, nullptr); + ChemTabModelComputeFunction(density, densityProgressVariables, densityEnergySource, + densityProgressVariableSource, nullptr); } -void ablate::eos::ChemTab::ChemistrySourceBatch(const PetscReal*const density, const PetscReal*const*const densityProgressVariable, - PetscReal** densityEnergySource, PetscReal** progressVariableSource, int n) const { - // for now we are implementing batch in the same way that single calls happened - // but testing that this works prepares the api for the real thing! - for (int i=0; iChemistrySourceBatch(allDensity, allDensityCPV,allDensityEnergySource, - allDensityCPVSource, buffer_len); + // using batch overloaded version + chemTabModel->ChemistrySource(allDensity, allDensityCPV,allDensityEnergySource, + allDensityCPVSource, buffer_len); // cleanup VecRestoreArray(locFVec, &fArray) >> utilities::PetscUtilities::checkError; diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index 220fe1346..f529df7a5 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -52,8 +52,13 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this GetFieldTags() const override { return std::vector{ablate::finiteVolume::CompressibleFlowFields::MinusOneToOneRange}; } @@ -93,6 +98,8 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this Date: Tue, 7 Nov 2023 12:26:00 -0500 Subject: [PATCH 5/9] commiting first working draft of batched processing (implemented on chemistry source only for now) --- src/eos/chemTab.cpp | 159 ++++++++++++++------------- src/eos/chemTab.hpp | 9 +- tests/unitTests/eos/chemTabTests.cpp | 1 + 3 files changed, 90 insertions(+), 79 deletions(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index c99310cda..3f584b196 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -1,5 +1,4 @@ #include "chemTab.hpp" - #include #ifdef WITH_TENSORFLOW @@ -90,7 +89,6 @@ ablate::eos::ChemTab::~ChemTab() { TF_DeleteSessionOptions(sessionOpts); TF_DeleteStatus(status); - free(sourceEnergyScaler); for (std::size_t i = 0; i < speciesNames.size(); i++) free(Wmat[i]); free(Wmat); } @@ -149,14 +147,55 @@ void ablate::eos::ChemTab::LoadBasisVectors(std::istream &inputStream, std::size } // avoids freeing null pointers -#define safe_free(ptr) \ - if (ptr != NULL) free(ptr) +#define safe_free(ptr) if (ptr != NULL) free(ptr) void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const PetscReal densityProgressVariables[], PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions) const { + ChemTabModelComputeFunction(&density, &densityProgressVariables, &densityEnergySource, &densityProgressVariableSource, &densityMassFractions, 1); +} + +// Verified to work 11/7/23 +// NOTE: id arg is to index a potentially batched outputValues argument +void ablate::eos::ChemTab::extractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, + PetscReal *densityProgressVariableSource, + PetscReal *densityMassFractions, + const std::array &outputValues, size_t id) const { + + // store physical variables (e.g. souener & mass fractions) + float *outputArray; // Dwyer: as counterintuitive as it may be static dependents come second, it did pass its tests! + outputArray = ((float *)TF_TensorData(outputValues[1])) + id*(speciesNames.size()+1); // also increment by id for batch processing + auto p = (PetscReal)outputArray[0]; + if (densityEnergySource != nullptr) *densityEnergySource += p * density; + + // store inverted mass fractions + if (densityMassFractions) { + for (size_t i = 0; i < speciesNames.size(); i++) { + densityMassFractions[i] = (PetscReal)outputArray[i + 1] * density; // i+1 b/c i==0 is souener! + } + } + + // store CPV sources + outputArray = ((float *)TF_TensorData(outputValues[0])) + id*(progressVariablesNames.size() - 1); + if (densityProgressVariableSource != nullptr) { + //densityProgressVariableSource[0] = 0; // Zmix source is always 0! + + // -1 b/c we don't want to go out of bounds with the +1 below, also to prevent integer overflow + for (size_t i = 0; i < (progressVariablesNames.size() - 1); ++i) { + densityProgressVariableSource[i + 1] += (PetscReal)outputArray[i] * density; // +1 b/c we are manually filling in Zmix source value (to 0) + } + } +} + +#define safe_id(array, i) (array ? array[i] : nullptr) + +void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal*const*const densityProgressVariables, + PetscReal** densityEnergySource, PetscReal** densityProgressVariableSource, + PetscReal** densityMassFractions, size_t batch_size) const { //********* Get Input tensor const std::size_t numInputs = 1; + // WTF is t0? & What is index 0? <-- t0 is input graph op & index 0 is + // index of the input (which is only 0 b/c there is only 1 input) TF_Output t0 = {TF_GraphOperationByName(graph, "serving_default_input_1"), 0}; if (t0.oper == nullptr) throw std::runtime_error("ERROR: Failed TF_GraphOperationByName serving_default_input_1"); @@ -165,6 +204,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const //********* Get Output tensor const std::size_t numOutputs = 2; + // NOTE: these names actually do make sense even though implicitly t_sourceenergy also includes inverse outputs TF_Output t_sourceenergy = {TF_GraphOperationByName(graph, "StatefulPartitionedCall"), 0}; TF_Output t_sourceterms = {TF_GraphOperationByName(graph, "StatefulPartitionedCall"), 1}; @@ -172,7 +212,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const if (t_sourceterms.oper == nullptr) throw std::runtime_error("ERROR: Failed TF_GraphOperationByName StatefulPartitionedCall:1"); std::array output = {t_sourceenergy, t_sourceterms}; - //********* Allocate data for inputs & outputs + //********* Allocate input_data for inputs & outputs std::array inputValues = {nullptr}; std::array outputValues = {nullptr, nullptr}; @@ -180,45 +220,33 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const // according to Varun this should work for including Zmix auto ninputs = progressVariablesNames.size(); - int64_t dims[] = {1, (int)ninputs}; - float data[ninputs]; + int64_t dims[] = {(int)batch_size, (int)ninputs}; + float input_data[batch_size][ninputs]; // NOTE: we changed the 1st dimension from 1 --> batch_size - for (std::size_t i = 0; i < ninputs; i++) { - data[i] = (float)(densityProgressVariables[i] / density); - } + // NOTE: this should be modified sufficiently to support batch inputs! + for (size_t i = 0; i < batch_size; i++) + for (std::size_t j = 0; j < ninputs; j++) + input_data[i][j] = (float)(densityProgressVariables[i][j] / density[i]); - std::size_t ndata = ninputs * sizeof(float); - TF_Tensor *input_tensor = TF_NewTensor(TF_FLOAT, dims, (int)ndims, data, ndata, &NoOpDeallocator, nullptr); + std::size_t ndata = sizeof(input_data); + TF_Tensor *input_tensor = TF_NewTensor(TF_FLOAT, dims, (int)ndims, input_data, + ndata, &NoOpDeallocator, nullptr); if (input_tensor == nullptr) throw std::runtime_error("ERROR: Failed TF_NewTensor"); + // there is only 1 input tensor per model eval point (unlike e.g. outputs) inputValues[0] = input_tensor; - TF_SessionRun(session, nullptr, input.data(), inputValues.data(), (int)numInputs, output.data(), outputValues.data(), (int)numOutputs, nullptr, 0, nullptr, status); + TF_SessionRun(session, nullptr, input.data(), inputValues.data(), (int)numInputs, + output.data(), outputValues.data(), (int)numOutputs, nullptr, + 0, nullptr, status); if (TF_GetCode(status) != TF_OK) throw std::runtime_error(TF_Message(status)); //********** Extract source predictions - // store physical variables (e.g. souener & mass fractions) - float *outputArray; // Dwyer: as counterintuitive as it may be static dependents come second, it did pass its tests! - outputArray = (float *)TF_TensorData(outputValues[1]); - auto p = (PetscReal)outputArray[0]; - if (densityEnergySource != nullptr) *densityEnergySource += p * density; - - // store inverted mass fractions - if (densityMassFractions) { - for (size_t i = 0; i < speciesNames.size(); i++) { - densityMassFractions[i] = (PetscReal)outputArray[i + 1] * density; // i+1 b/c i==0 is souener! - } - } - - // store CPV sources - outputArray = (float *)TF_TensorData(outputValues[0]); - if (densityProgressVariableSource != nullptr) { - //densityProgressVariableSource[0] = 0; // Zmix source is always 0! - - // -1 b/c we don't want to go out of bounds with the +1 below, also to prevent integer overflow - for (size_t i = 0; i < (progressVariablesNames.size() - 1); ++i) { - densityProgressVariableSource[i + 1] += (PetscReal)outputArray[i] * density; // +1 b/c we are manually filling in Zmix source value (to 0) - } + // reiterate the same extraction process for each member of the batch + for (size_t i=0; i < batch_size; i++) { + extractModelOutputsAtPoint(density[i], safe_id(densityEnergySource, i), + safe_id(densityProgressVariableSource, i), + safe_id(densityMassFractions, i), outputValues, i); } // free allocated vectors @@ -230,20 +258,18 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const } } -#define safe_id(array, i) (array ? array[i] : nullptr) - -void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal*const*const densityProgressVariables, - PetscReal** densityEnergySource, PetscReal** densityProgressVariableSource, - PetscReal** densityMassFractions, size_t n) const { - // for now we are implementing batch in the same way that single calls happened - // but testing that this works prepares the api for the real thing! - for (size_t i=0; i &progressVariables, std::vector &massFractions, PetscReal density) const { if (progressVariables.size() != progressVariablesNames.size()) { @@ -511,6 +537,7 @@ ablate::eos::ChemTab::ChemTabSourceCalculator::ChemTabSourceCalculator(PetscInt std::shared_ptr chemTabModel) : densityOffset(densityOffset), densityEnergyOffset(densityEnergyOffset), densityProgressVariableOffset(densityProgressVariableOffset), chemTabModel(std::move(chemTabModel)) {} + // NOTE: I'm not sure however I believe that this could be the ONLY place that needs updating for Batch processing?? // Comments seem to indicate it is the case... void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::domain::Range &cellRange, Vec locX, Vec locFVec) { @@ -526,42 +553,26 @@ void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::doma // Here we store the batched pointers needed for multiple chemTabModel->ChemistrySource() calls PetscInt buffer_len = cellRange.end - cellRange.start; - //PetscScalar* allSourceAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets - //const PetscScalar* allSolutionAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets PetscScalar allDensity[buffer_len]; const PetscScalar* allDensityCPV[buffer_len]; PetscScalar* allDensityEnergySource[buffer_len]; PetscScalar* allDensityCPVSource[buffer_len]; + //PetscScalar* allSourceAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets + //const PetscScalar* allSolutionAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets - // TODO: change this for batch processing!! + // NOTE: this clunky approach has been verified to work for batch processsing // March over each cell in the range for (PetscInt c = cellRange.start; c < cellRange.end; ++c) { const PetscInt iCell = cellRange.points ? cellRange.points[c] : c; // Dwyer: iCell is the "point" - // Def: PetscErrorCode DMPlexPointLocalRef(DM dm, PetscInt point, PetscScalar *array, void *ptr) - // Help: return read/write access to a point in local array - // :param array: - array to index into - // :param ptr: output reference/return value - -// // Get the current source variables for this cell -// DMPlexPointLocalRef(dm, iCell, fArray, &allSourceAtCell[c-cellRange.start]) >> utilities::PetscUtilities::checkError; - PetscScalar* sourceAtCell = nullptr; DMPlexPointLocalRef(dm, iCell, fArray, &sourceAtCell) >> utilities::PetscUtilities::checkError; - //assert(sourceAtCell==allSourceAtCell[c-cellRange.start]); // silly sanity check // Get the current state variables for this cell (CPVs) const PetscScalar* solutionAtCell = nullptr; DMPlexPointLocalRead(dm, iCell, xArray, &solutionAtCell) >> utilities::PetscUtilities::checkError; - //allSolutionAtCell[c-cellRange.start]=solutionAtCell; - //DMPlexPointLocalRead(dm, iCell, xArray, &allSolutionAtCell[c-cellRange.start]) >> utilities::PetscUtilities::checkError; - //assert(solutionAtCell==allSolutionAtCell[c-cellRange.start]); // silly sanity check - - // Def: PetscErrorCode DMPlexPointLocalRead(DM dm, PetscInt point, const PetscScalar *array, void *ptr) - // Help: return read access to a point in local array - // NOTE: The only difference is that DMPlexPointLocalRef gives read/write access - // & DMPlexPointLocalRead gives only ready access + // NOTE: DMPlexPointLocalRef() is Read/Write while DMPlexPointLocalRead() is Read only // store this cell's attributes into arg vectors size_t index = c-cellRange.start; @@ -569,17 +580,13 @@ void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::doma allDensityCPV[index]=solutionAtCell + densityProgressVariableOffset; allDensityEnergySource[index]=sourceAtCell + densityEnergyOffset; allDensityCPVSource[index]=sourceAtCell + densityProgressVariableOffset; - -// // Def: ChemTab::ChemistrySource(PetscReal density, const PetscReal densityProgressVariable[], PetscReal *densityEnergySource, PetscReal *progressVariableSource) -// // Help: last 2 (Souener & CPV_source) are the "return values" -// chemTabModel->ChemistrySource(solutionAtCell[densityOffset], solutionAtCell + densityProgressVariableOffset, -// sourceAtCell + densityEnergyOffset, sourceAtCell + densityProgressVariableOffset); - // NOTE: These "offsets" are pointers since they are CONSTANT class attributes! } - // using batch overloaded version + // Using batch overloaded version + // TODO: consider/optimize the problem that is likely requires loop to copy these arrays into TF inputs?? chemTabModel->ChemistrySource(allDensity, allDensityCPV,allDensityEnergySource, allDensityCPVSource, buffer_len); + // NOTE: These "offsets" are pointers since they are CONSTANT class attributes! // cleanup VecRestoreArray(locFVec, &fArray) >> utilities::PetscUtilities::checkError; diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index f529df7a5..87686ce1d 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -33,7 +33,6 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this progressVariablesNames = std::vector(0); PetscReal** Wmat = nullptr; - PetscReal* sourceEnergyScaler = nullptr; // Store any initializers specified by the metadata std::map> initializers; @@ -57,7 +56,7 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this>> GetSolutionFieldUpdates() override; -}; + + void extractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, + PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions, + const std::array &outputValues, size_t id=0) const; + }; #else class ChemTab : public ChemistryModel { diff --git a/tests/unitTests/eos/chemTabTests.cpp b/tests/unitTests/eos/chemTabTests.cpp index c38a44bd6..e7305210c 100644 --- a/tests/unitTests/eos/chemTabTests.cpp +++ b/tests/unitTests/eos/chemTabTests.cpp @@ -111,6 +111,7 @@ TEST_P(ChemTabTestFixture, ShouldComputeCorrectSource) { std::vector actualSourceProgress(conservedProgressVariable.size(), sourceOffset); PetscReal actualSourceEnergy = sourceOffset; chemTabModel.ChemistrySource(density, conservedProgressVariable.data(), &actualSourceEnergy, actualSourceProgress.data()); + // NOTE: this doesn't need to change for batch processing just make sure to still support point evaluation. assert_float_close(expectedSourceEnergy * density, actualSourceEnergy - sourceOffset) << "The sourceEnergy is incorrect for model " << testTarget["testName"].as(); From 7ddc8d59d080e0b181c53b06be6d10a9d30cf78b Mon Sep 17 00:00:00 2001 From: Dwyer Deighan Date: Tue, 28 Nov 2023 20:43:52 -0500 Subject: [PATCH 6/9] clarified/corrected confusing & outdated comment --- src/eos/chemTab.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index 3f584b196..64f64d909 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -233,7 +233,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[] ndata, &NoOpDeallocator, nullptr); if (input_tensor == nullptr) throw std::runtime_error("ERROR: Failed TF_NewTensor"); - // there is only 1 input tensor per model eval point (unlike e.g. outputs) + // there is only 1 input tensor per model eval (unlike e.g. outputs) inputValues[0] = input_tensor; TF_SessionRun(session, nullptr, input.data(), inputValues.data(), (int)numInputs, From 9105ac5fcedf751e8b6068020f3dafc10faa682f Mon Sep 17 00:00:00 2001 From: Dwyer Deighan Date: Mon, 18 Dec 2023 14:17:00 -0500 Subject: [PATCH 7/9] commiting reformatted code & version bump --- CMakeLists.txt | 2 +- src/eos/chemTab.cpp | 147 +++++++++++++++++++------------------------- src/eos/chemTab.hpp | 38 +++++------- 3 files changed, 80 insertions(+), 107 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index de92e4a9e..15cddaa71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.18.4) include(config/petscCompilers.cmake) # Set the project details -project(ablateLibrary VERSION 0.12.35) +project(ablateLibrary VERSION 0.13.18) # Load the Required 3rd Party Libaries pkg_check_modules(PETSc REQUIRED IMPORTED_TARGET GLOBAL PETSc) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index 64f64d909..7576c7223 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -147,23 +147,21 @@ void ablate::eos::ChemTab::LoadBasisVectors(std::istream &inputStream, std::size } // avoids freeing null pointers -#define safe_free(ptr) if (ptr != NULL) free(ptr) +#define safe_free(ptr) \ + if (ptr != NULL) free(ptr) -void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const PetscReal densityProgressVariables[], PetscReal *densityEnergySource, - PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions) const { +void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const PetscReal densityProgressVariables[], PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource, + PetscReal *densityMassFractions) const { ChemTabModelComputeFunction(&density, &densityProgressVariables, &densityEnergySource, &densityProgressVariableSource, &densityMassFractions, 1); } // Verified to work 11/7/23 // NOTE: id arg is to index a potentially batched outputValues argument -void ablate::eos::ChemTab::extractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, - PetscReal *densityProgressVariableSource, - PetscReal *densityMassFractions, +void ablate::eos::ChemTab::extractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions, const std::array &outputValues, size_t id) const { - // store physical variables (e.g. souener & mass fractions) - float *outputArray; // Dwyer: as counterintuitive as it may be static dependents come second, it did pass its tests! - outputArray = ((float *)TF_TensorData(outputValues[1])) + id*(speciesNames.size()+1); // also increment by id for batch processing + float *outputArray; // Dwyer: as counterintuitive as it may be static dependents come second, it did pass its tests! + outputArray = ((float *)TF_TensorData(outputValues[1])) + id * (speciesNames.size() + 1); // also increment by id for batch processing auto p = (PetscReal)outputArray[0]; if (densityEnergySource != nullptr) *densityEnergySource += p * density; @@ -175,9 +173,9 @@ void ablate::eos::ChemTab::extractModelOutputsAtPoint(const PetscReal density, P } // store CPV sources - outputArray = ((float *)TF_TensorData(outputValues[0])) + id*(progressVariablesNames.size() - 1); + outputArray = ((float *)TF_TensorData(outputValues[0])) + id * (progressVariablesNames.size() - 1); if (densityProgressVariableSource != nullptr) { - //densityProgressVariableSource[0] = 0; // Zmix source is always 0! + // densityProgressVariableSource[0] = 0; // Zmix source is always 0! // -1 b/c we don't want to go out of bounds with the +1 below, also to prevent integer overflow for (size_t i = 0; i < (progressVariablesNames.size() - 1); ++i) { @@ -188,9 +186,8 @@ void ablate::eos::ChemTab::extractModelOutputsAtPoint(const PetscReal density, P #define safe_id(array, i) (array ? array[i] : nullptr) -void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal*const*const densityProgressVariables, - PetscReal** densityEnergySource, PetscReal** densityProgressVariableSource, - PetscReal** densityMassFractions, size_t batch_size) const { +void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal *const *const densityProgressVariables, PetscReal **densityEnergySource, + PetscReal **densityProgressVariableSource, PetscReal **densityMassFractions, size_t batch_size) const { //********* Get Input tensor const std::size_t numInputs = 1; @@ -221,32 +218,26 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[] // according to Varun this should work for including Zmix auto ninputs = progressVariablesNames.size(); int64_t dims[] = {(int)batch_size, (int)ninputs}; - float input_data[batch_size][ninputs]; // NOTE: we changed the 1st dimension from 1 --> batch_size + float input_data[batch_size][ninputs]; // NOTE: we changed the 1st dimension from 1 --> batch_size // NOTE: this should be modified sufficiently to support batch inputs! for (size_t i = 0; i < batch_size; i++) - for (std::size_t j = 0; j < ninputs; j++) - input_data[i][j] = (float)(densityProgressVariables[i][j] / density[i]); + for (std::size_t j = 0; j < ninputs; j++) input_data[i][j] = (float)(densityProgressVariables[i][j] / density[i]); std::size_t ndata = sizeof(input_data); - TF_Tensor *input_tensor = TF_NewTensor(TF_FLOAT, dims, (int)ndims, input_data, - ndata, &NoOpDeallocator, nullptr); + TF_Tensor *input_tensor = TF_NewTensor(TF_FLOAT, dims, (int)ndims, input_data, ndata, &NoOpDeallocator, nullptr); if (input_tensor == nullptr) throw std::runtime_error("ERROR: Failed TF_NewTensor"); // there is only 1 input tensor per model eval (unlike e.g. outputs) inputValues[0] = input_tensor; - TF_SessionRun(session, nullptr, input.data(), inputValues.data(), (int)numInputs, - output.data(), outputValues.data(), (int)numOutputs, nullptr, - 0, nullptr, status); + TF_SessionRun(session, nullptr, input.data(), inputValues.data(), (int)numInputs, output.data(), outputValues.data(), (int)numOutputs, nullptr, 0, nullptr, status); if (TF_GetCode(status) != TF_OK) throw std::runtime_error(TF_Message(status)); //********** Extract source predictions // reiterate the same extraction process for each member of the batch - for (size_t i=0; i < batch_size; i++) { - extractModelOutputsAtPoint(density[i], safe_id(densityEnergySource, i), - safe_id(densityProgressVariableSource, i), - safe_id(densityMassFractions, i), outputValues, i); + for (size_t i = 0; i < batch_size; i++) { + extractModelOutputsAtPoint(density[i], safe_id(densityEnergySource, i), safe_id(densityProgressVariableSource, i), safe_id(densityMassFractions, i), outputValues, i); } // free allocated vectors @@ -258,49 +249,42 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[] } } -//void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal*const*const densityProgressVariables, -// PetscReal** densityEnergySource, PetscReal** densityProgressVariableSource, -// PetscReal** densityMassFractions, size_t n) const { -// // for now we are implementing batch in the same way that single calls happened -// // but testing that this works prepares the api for the real thing! -// for (size_t i=0; i &progressVariables, std::vector &massFractions, PetscReal density) const { if (progressVariables.size() != progressVariablesNames.size()) { - throw std::invalid_argument( - "The Progress variable size is expected to be " + std::to_string(progressVariablesNames.size())); + throw std::invalid_argument("The Progress variable size is expected to be " + std::to_string(progressVariablesNames.size())); } if (massFractions.size() != speciesNames.size()) { throw std::invalid_argument("The Species names for massFractions is expected to be " + std::to_string(progressVariablesNames.size())); } // the naming is wrong on purpose so that it will conform to tests. ComputeMassFractions(progressVariables.data(), massFractions.data(), density); - //ComputeProgressVariables(massFractions.data(), progressVariables.data()); + // ComputeProgressVariables(massFractions.data(), progressVariables.data()); } -void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *densityProgressVariables, PetscReal *densityMassFractions, - const PetscReal density) const { +void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *densityProgressVariables, PetscReal *densityMassFractions, const PetscReal density) const { // call model using generalized invocation method (usable for inversion & source computation) - ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, - nullptr, densityMassFractions); + ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, nullptr, densityMassFractions); } -void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal*const* densityProgressVariables, PetscReal** densityMassFractions, - const PetscReal density[], size_t n) const { - ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, - nullptr, densityMassFractions, n); +void ablate::eos::ChemTab::ComputeMassFractions(const PetscReal *const *densityProgressVariables, PetscReal **densityMassFractions, const PetscReal density[], size_t n) const { + ChemTabModelComputeFunction(density, densityProgressVariables, nullptr, nullptr, densityMassFractions, n); } - // Batched Version -void ablate::eos::ChemTab::ComputeProgressVariables(const PetscReal *const *massFractions, - PetscReal *const *progressVariables, size_t n) const { +void ablate::eos::ChemTab::ComputeProgressVariables(const PetscReal *const *massFractions, PetscReal *const *progressVariables, size_t n) const { for (size_t i = 0; i < n; i++) { ComputeProgressVariables(massFractions[i], progressVariables[i]); } @@ -329,36 +313,33 @@ void ablate::eos::ChemTab::ComputeProgressVariables(const PetscReal *massFractio } } -inline double L2_norm(PetscReal* array, int n) { - double norm=0; - for (int i=0; i chemTabModel) : densityOffset(densityOffset), densityEnergyOffset(densityEnergyOffset), densityProgressVariableOffset(densityProgressVariableOffset), chemTabModel(std::move(chemTabModel)) {} - // NOTE: I'm not sure however I believe that this could be the ONLY place that needs updating for Batch processing?? // Comments seem to indicate it is the case... void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::domain::Range &cellRange, Vec locX, Vec locFVec) { @@ -548,17 +528,17 @@ void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::doma VecGetArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError; // Get the solution dm - DM dm; // NOTE: DM is topological space (i.e. grid) + DM dm; // NOTE: DM is topological space (i.e. grid) VecGetDM(locFVec, &dm) >> utilities::PetscUtilities::checkError; // Here we store the batched pointers needed for multiple chemTabModel->ChemistrySource() calls PetscInt buffer_len = cellRange.end - cellRange.start; PetscScalar allDensity[buffer_len]; - const PetscScalar* allDensityCPV[buffer_len]; - PetscScalar* allDensityEnergySource[buffer_len]; - PetscScalar* allDensityCPVSource[buffer_len]; - //PetscScalar* allSourceAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets - //const PetscScalar* allSolutionAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets + const PetscScalar *allDensityCPV[buffer_len]; + PetscScalar *allDensityEnergySource[buffer_len]; + PetscScalar *allDensityCPVSource[buffer_len]; + // PetscScalar* allSourceAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets + // const PetscScalar* allSolutionAtCell[buffer_len]; // apparently these can't be used b/c of secret offsets // NOTE: this clunky approach has been verified to work for batch processsing // March over each cell in the range @@ -566,26 +546,25 @@ void ablate::eos::ChemTab::ChemTabSourceCalculator::AddSource(const ablate::doma const PetscInt iCell = cellRange.points ? cellRange.points[c] : c; // Dwyer: iCell is the "point" - PetscScalar* sourceAtCell = nullptr; + PetscScalar *sourceAtCell = nullptr; DMPlexPointLocalRef(dm, iCell, fArray, &sourceAtCell) >> utilities::PetscUtilities::checkError; - // Get the current state variables for this cell (CPVs) - const PetscScalar* solutionAtCell = nullptr; + // Get the current state variables for this cell (CPVs) + const PetscScalar *solutionAtCell = nullptr; DMPlexPointLocalRead(dm, iCell, xArray, &solutionAtCell) >> utilities::PetscUtilities::checkError; // NOTE: DMPlexPointLocalRef() is Read/Write while DMPlexPointLocalRead() is Read only // store this cell's attributes into arg vectors - size_t index = c-cellRange.start; - allDensity[index]=solutionAtCell[densityOffset]; - allDensityCPV[index]=solutionAtCell + densityProgressVariableOffset; - allDensityEnergySource[index]=sourceAtCell + densityEnergyOffset; - allDensityCPVSource[index]=sourceAtCell + densityProgressVariableOffset; + size_t index = c - cellRange.start; + allDensity[index] = solutionAtCell[densityOffset]; + allDensityCPV[index] = solutionAtCell + densityProgressVariableOffset; + allDensityEnergySource[index] = sourceAtCell + densityEnergyOffset; + allDensityCPVSource[index] = sourceAtCell + densityProgressVariableOffset; } // Using batch overloaded version // TODO: consider/optimize the problem that is likely requires loop to copy these arrays into TF inputs?? - chemTabModel->ChemistrySource(allDensity, allDensityCPV,allDensityEnergySource, - allDensityCPVSource, buffer_len); + chemTabModel->ChemistrySource(allDensity, allDensityCPV, allDensityEnergySource, allDensityCPVSource, buffer_len); // NOTE: These "offsets" are pointers since they are CONSTANT class attributes! // cleanup diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index 87686ce1d..6ce3841aa 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -51,11 +51,9 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this& progressVariables, std::vector& massFractions, PetscReal density = 1.0) const; -// /** -// * helper function to compute the mass fractions = from the mass fractions progress variables -// * @param progressVariables is density*progress -// * @param massFractions -// * @param density allows for this function to be used with density*progress variables -// */ -// void ComputeMassFractions(PetscReal* progressVariables, PetscReal* massFractions, PetscReal density = 1.0) const; + // /** + // * helper function to compute the mass fractions = from the mass fractions progress variables + // * @param progressVariables is density*progress + // * @param massFractions + // * @param density allows for this function to be used with density*progress variables + // */ + // void ComputeMassFractions(PetscReal* progressVariables, PetscReal* massFractions, PetscReal density = 1.0) const; /** * helper function to compute the mass fractions = from the mass fractions progress variables @@ -234,7 +229,7 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this>> GetSolutionFieldUpdates() override; - void extractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, - PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions, - const std::array &outputValues, size_t id=0) const; - }; + void extractModelOutputsAtPoint(const PetscReal density, PetscReal* densityEnergySource, PetscReal* densityProgressVariableSource, PetscReal* densityMassFractions, + const std::array& outputValues, size_t id = 0) const; +}; #else class ChemTab : public ChemistryModel { From f71981a2f665a3de4e232d724a10ce6eff7c1a46 Mon Sep 17 00:00:00 2001 From: Dwyer Deighan Date: Mon, 18 Dec 2023 14:20:26 -0500 Subject: [PATCH 8/9] reworked version so this would be a patch increment since it is just optimization --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15cddaa71..0a36e4d1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.18.4) include(config/petscCompilers.cmake) # Set the project details -project(ablateLibrary VERSION 0.13.18) +project(ablateLibrary VERSION 0.12.36) # Load the Required 3rd Party Libaries pkg_check_modules(PETSc REQUIRED IMPORTED_TARGET GLOBAL PETSc) From 3960caa617614cf8f8451ef8d8bacb15e6fdf348 Mon Sep 17 00:00:00 2001 From: Dwyer Deighan Date: Thu, 16 Jan 2025 18:53:00 -0500 Subject: [PATCH 9/9] Updated doxygen comments and addressed various style issues with the PR --- src/eos/chemTab.cpp | 17 ++----------- src/eos/chemTab.hpp | 61 +++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/eos/chemTab.cpp b/src/eos/chemTab.cpp index 7576c7223..1ba2c1b04 100644 --- a/src/eos/chemTab.cpp +++ b/src/eos/chemTab.cpp @@ -157,7 +157,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(PetscReal density, const // Verified to work 11/7/23 // NOTE: id arg is to index a potentially batched outputValues argument -void ablate::eos::ChemTab::extractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions, +void ablate::eos::ChemTab::ExtractModelOutputsAtPoint(const PetscReal density, PetscReal *densityEnergySource, PetscReal *densityProgressVariableSource, PetscReal *densityMassFractions, const std::array &outputValues, size_t id) const { // store physical variables (e.g. souener & mass fractions) float *outputArray; // Dwyer: as counterintuitive as it may be static dependents come second, it did pass its tests! @@ -237,7 +237,7 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[] // reiterate the same extraction process for each member of the batch for (size_t i = 0; i < batch_size; i++) { - extractModelOutputsAtPoint(density[i], safe_id(densityEnergySource, i), safe_id(densityProgressVariableSource, i), safe_id(densityMassFractions, i), outputValues, i); + ExtractModelOutputsAtPoint(density[i], safe_id(densityEnergySource, i), safe_id(densityProgressVariableSource, i), safe_id(densityMassFractions, i), outputValues, i); } // free allocated vectors @@ -249,19 +249,6 @@ void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[] } } -// void ablate::eos::ChemTab::ChemTabModelComputeFunction(const PetscReal density[], const PetscReal*const*const densityProgressVariables, -// PetscReal** densityEnergySource, PetscReal** densityProgressVariableSource, -// PetscReal** densityMassFractions, size_t n) const { -// // for now we are implementing batch in the same way that single calls happened -// // but testing that this works prepares the api for the real thing! -// for (size_t i=0; i &progressVariables, std::vector &massFractions, PetscReal density) const { if (progressVariables.size() != progressVariablesNames.size()) { throw std::invalid_argument("The Progress variable size is expected to be " + std::to_string(progressVariablesNames.size())); diff --git a/src/eos/chemTab.hpp b/src/eos/chemTab.hpp index 6ce3841aa..c10b68748 100644 --- a/src/eos/chemTab.hpp +++ b/src/eos/chemTab.hpp @@ -53,9 +53,19 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this GetFieldTags() const override { return std::vector{ablate::finiteVolume::CompressibleFlowFields::MinusOneToOneRange}; } @@ -90,13 +100,18 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this& progressVariables, std::vector& massFractions, PetscReal density = 1.0) const; - // /** - // * helper function to compute the mass fractions = from the mass fractions progress variables - // * @param progressVariables is density*progress - // * @param massFractions - // * @param density allows for this function to be used with density*progress variables - // */ - // void ComputeMassFractions(PetscReal* progressVariables, PetscReal* massFractions, PetscReal density = 1.0) const; - /** * helper function to compute the mass fractions = from the mass fractions progress variables * @param progressVariables is density*progress @@ -229,8 +245,15 @@ class ChemTab : public ChemistryModel, public std::enable_shared_from_this>> GetSolutionFieldUpdates() override; - void extractModelOutputsAtPoint(const PetscReal density, PetscReal* densityEnergySource, PetscReal* densityProgressVariableSource, PetscReal* densityMassFractions, + void ExtractModelOutputsAtPoint(const PetscReal density, PetscReal* densityEnergySource, PetscReal* densityProgressVariableSource, PetscReal* densityMassFractions, const std::array& outputValues, size_t id = 0) const; };