Skip to content

Commit

Permalink
Print temporals in CSV format (#410)
Browse files Browse the repository at this point in the history
* util for getting varname from state index

* print temporals in CSV format with headers

* remove redundant string initialization

* fix typo
  • Loading branch information
baperry2 authored Sep 10, 2024
1 parent 1371b7c commit a802fd9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Source/Efield/PeleLMeX_EFUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ PeleLM::ionsBalance()
}
}

tmpIonsFile << m_nstep << " " << m_cur_time; // Time info
tmpIonsFile << m_nstep << "," << m_cur_time; // Time info
for (int i = 0; i < 2 * AMREX_SPACEDIM; i++) {
tmpIonsFile << " " << ionsCurrent[i]; // ions current as xlo, xhi, ylo, ...
tmpIonsFile << "," << ionsCurrent[i]; // ions current as xlo, xhi, ylo, ...
}
tmpIonsFile << "\n";
tmpIonsFile.flush();
Expand Down
1 change: 1 addition & 0 deletions Source/PeleLMeX.H
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ public:
bool isReactVariable(std::string_view a_name);
int stateVariableIndex(std::string_view a_name);
int reactVariableIndex(std::string_view a_name);
std::string stateVariableName(int a_index);

// Typical values
void setTypicalValues(const PeleLM::TimeStamp& a_time, int is_init = 0);
Expand Down
81 changes: 52 additions & 29 deletions Source/PeleLMeX_Temporals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ PeleLM::massBalance()
+m_domainMassFlux[2] + m_domainMassFlux[3],
+m_domainMassFlux[4] + m_domainMassFlux[5]);

tmpMassFile << m_nstep << " " << m_cur_time // Time info
<< " " << m_massNew // mass
<< " " << dmdt // mass temporal derivative
<< " " << massFluxBalance // domain boundaries mass fluxes
<< " " << std::abs(dmdt - massFluxBalance) << " \n"; // balance
tmpMassFile << m_nstep << "," << m_cur_time // Time info
<< "," << m_massNew // mass
<< "," << dmdt // mass temporal derivative
<< "," << massFluxBalance // domain boundaries mass fluxes
<< "," << std::abs(dmdt - massFluxBalance) << "\n"; // balance
tmpMassFile.flush();
}

void
PeleLM::speciesBalancePatch()
{
tmppatchmfrFile << m_nstep << " " << m_cur_time; // Time info
tmppatchmfrFile << m_nstep << "," << m_cur_time; // Time info
for (int n = 0; n < m_bPatches.size(); n++) {
BPatch::BpatchDataContainer* bphost = m_bPatches[n]->getHostDataPtr();
for (int i = 0; i < bphost->num_species; i++) {
tmppatchmfrFile << " " << bphost->speciesFlux[i];
tmppatchmfrFile << "," << bphost->speciesFlux[i];
}
}
tmppatchmfrFile << "\n";
Expand All @@ -90,13 +90,13 @@ PeleLM::speciesBalance()
m_domainRhoYFlux[2 * n * AMREX_SPACEDIM + 5]);
}

tmpSpecFile << m_nstep << " " << m_cur_time; // Time info
tmpSpecFile << m_nstep << "," << m_cur_time; // Time info
for (int n = 0; n < NUM_SPECIES; n++) {
tmpSpecFile << " " << m_RhoYNew[n] // mass of Y
<< " " << dmYdt[n] // mass temporal derivative
<< " " << massYFluxBalance[n] // domain boundaries mass fluxes
<< " " << rhoYdots[n] // integrated consumption rate
<< " "
tmpSpecFile << "," << m_RhoYNew[n] // mass of Y
<< "," << dmYdt[n] // mass temporal derivative
<< "," << massYFluxBalance[n] // domain boundaries mass fluxes
<< "," << rhoYdots[n] // integrated consumption rate
<< ","
<< std::abs(
dmYdt[n] - massYFluxBalance[n] - rhoYdots[n]); // balance
}
Expand Down Expand Up @@ -310,11 +310,11 @@ PeleLM::rhoHBalance()
+m_domainRhoHFlux[2] + m_domainRhoHFlux[3],
+m_domainRhoHFlux[4] + m_domainRhoHFlux[5]);

tmpMassFile << m_nstep << " " << m_cur_time // Time info
<< " " << m_RhoHNew // RhoH
<< " " << dRhoHdt // RhoH temporal derivative
<< " " << rhoHFluxBalance // domain boundaries RhoH fluxes
<< " " << std::abs(dRhoHdt - rhoHFluxBalance) << " \n"; // balance
tmpMassFile << m_nstep << "," << m_cur_time // Time info
<< "," << m_RhoHNew // RhoH
<< "," << dRhoHdt // RhoH temporal derivative
<< "," << rhoHFluxBalance // domain boundaries RhoH fluxes
<< "," << std::abs(dRhoHdt - rhoHFluxBalance) << "\n"; // balance
tmpMassFile.flush();
}

Expand Down Expand Up @@ -670,13 +670,13 @@ PeleLM::writeTemporals()
heatReleaseRateInt = MFSum(GetVecOfConstPtrs(kinEnergy), 0);
}

tmpStateFile << m_nstep << " " << m_cur_time << " " << m_dt // Time
<< " " << kinenergy_int // Kinetic energy
<< " " << enstrophy_int // Enstrophy
<< " " << m_pNew // Thermo. pressure
<< " " << fuelConsumptionInt // Integ fuel burning rate
<< " " << heatReleaseRateInt // Integ heat release rate
<< " \n";
tmpStateFile << m_nstep << "," << m_cur_time << "," << m_dt // Time
<< "," << kinenergy_int // Kinetic energy
<< "," << enstrophy_int // Enstrophy
<< "," << m_pNew // Thermo. pressure
<< "," << fuelConsumptionInt // Integ fuel burning rate
<< "," << heatReleaseRateInt // Integ heat release rate
<< "\n";
tmpStateFile.flush();

// Get min/max for state components
Expand All @@ -689,12 +689,12 @@ PeleLM::writeTemporals()
? MLmin(GetVecOfConstPtrs(getStateVect(AmrNewTime)), 0, AMREX_SPACEDIM)
: MLmin(GetVecOfConstPtrs(getStateVect(AmrNewTime)), 0, NVAR);

tmpExtremasFile << m_nstep << " " << m_cur_time; // Time
tmpExtremasFile << m_nstep << "," << m_cur_time; // Time
for (int n = 0; n < stateMax.size();
++n) { // Min & max of each state variable
tmpExtremasFile << " " << stateMin[n] << " " << stateMax[n];
tmpExtremasFile << "," << stateMin[n] << "," << stateMax[n];
}
tmpExtremasFile << " \n";
tmpExtremasFile << "\n";
tmpExtremasFile.flush();

#ifdef PELE_USE_EFIELD
Expand All @@ -720,34 +720,52 @@ PeleLM::openTempFile()
tempFileName.c_str(),
std::ios::out | std::ios::app | std::ios_base::binary);
tmpStateFile.precision(12);
tmpStateFile << "iter,time,dt,kinEnergy,enstrophy,pressure,fuelConsumption,"
"heatRelease\n";
if (m_do_massBalance != 0) {
tempFileName = "temporals/tempMass";
tmpMassFile.open(
tempFileName.c_str(),
std::ios::out | std::ios::app | std::ios_base::binary);
tmpMassFile.precision(12);
tmpMassFile << "iter,time,massNew,dmdt,netMassFlux,balance\n";
}
if (m_do_speciesBalance != 0) {
tempFileName = "temporals/tempSpecies";
tmpSpecFile.open(
tempFileName.c_str(),
std::ios::out | std::ios::app | std::ios_base::binary);
tmpSpecFile.precision(12);
tmpSpecFile << "iter,time";
for (int n = 0; n < NUM_SPECIES; n++) {
tmpSpecFile << ",rhoYnew_" << PeleLM::stateVariableName(FIRSTSPEC + n);
tmpSpecFile << ",drhoYdt_" << PeleLM::stateVariableName(FIRSTSPEC + n);
tmpSpecFile << ",netFlux_" << PeleLM::stateVariableName(FIRSTSPEC + n);
tmpSpecFile << ",rxnCons_" << PeleLM::stateVariableName(FIRSTSPEC + n);
tmpSpecFile << ",balance_" << PeleLM::stateVariableName(FIRSTSPEC + n);
}
tmpSpecFile << "\n";
}
if (m_do_extremas != 0) {
tempFileName = "temporals/tempExtremas";
tmpExtremasFile.open(
tempFileName.c_str(),
std::ios::out | std::ios::app | std::ios_base::binary);
tmpExtremasFile.precision(12);
tmpExtremasFile << "iter,time";
for (int n = 0; n < NVAR; ++n) {
tmpExtremasFile << ",min_" << PeleLM::stateVariableName(n) << ",max_"
<< PeleLM::stateVariableName(n);
}
tmpExtremasFile << "\n";
}
if (m_do_patch_mfr != 0) {
tempFileName = "temporals/temppatchmfr";
tmppatchmfrFile.open(
tempFileName.c_str(),
std::ios::out | std::ios::app | std::ios_base::binary);
tmppatchmfrFile.precision(12);
tmppatchmfrFile << "#Variables=iter,time";
tmppatchmfrFile << "iter,time";
for (int n = 0; n < m_bPatches.size(); n++) {
BPatch* patch = m_bPatches[n].get();
BPatch::BpatchDataContainer bphost = patch->getHostData();
Expand All @@ -765,6 +783,11 @@ PeleLM::openTempFile()
tempFileName.c_str(),
std::ios::out | std::ios::app | std::ios_base::binary);
tmpIonsFile.precision(12);
tmpIonsFile << "iter,time";
for (int i = 0; i < AMREX_SPACEDIM; i++) {
tmpIonsFile << ",curr_" << i << "_low,curr_" << i << "_hi";
}
tmpIonsFile << "\n";
}
#endif
}
Expand Down
18 changes: 18 additions & 0 deletions Source/PeleLMeX_Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,24 @@ PeleLM::stateVariableIndex(std::string_view a_name)
return idx;
}

std::string
PeleLM::stateVariableName(int a_index)
{
if (a_index < 0 || a_index >= NVAR) {
amrex::Error(
"PeleLM::stateVariableName(): invalid state index: " +
std::to_string(a_index));
}

std::string var_name;
for (const auto& stateComponent : stateComponents) {
if (std::get<0>(stateComponent) == a_index) {
var_name = std::get<1>(stateComponent);
}
}
return var_name;
}

int
PeleLM::reactVariableIndex(std::string_view a_name)
{
Expand Down

0 comments on commit a802fd9

Please sign in to comment.