Skip to content

Commit

Permalink
[SolutionArray] Implement SolutionArray::normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 25, 2023
1 parent fe5b703 commit 525973a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/cantera/base/SolutionArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ class SolutionArray
//! Set the state vector for a given location.
void setState(size_t loc, const vector<double>& state);

//! Normalize mass/mole fractions
void normalize();

/*!
* Add auxiliary component to SolutionArray. Initialization requires a subsequent
* call of setComponent.
Expand Down
32 changes: 31 additions & 1 deletion src/base/SolutionArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ void SolutionArray::setLoc(size_t loc, bool restore)
m_loc = m_active[loc];
if (restore) {
size_t nState = m_sol->thermo()->stateSize();
m_sol->thermo()->restoreState(nState, m_data->data() + m_loc * m_stride);
m_sol->thermo()->restoreState(nState, m_data->data() + m_loc * m_stride);
}
}

Expand Down Expand Up @@ -456,6 +456,36 @@ void SolutionArray::setState(size_t loc, const vector<double>& state)
m_sol->thermo()->saveState(nState, m_data->data() + m_loc * m_stride);
}

void SolutionArray::normalize() {
auto phase = m_sol->thermo();
auto nativeState = phase->nativeState();
if (nativeState.size() < 3) {
return;
}
size_t nState = phase->stateSize();
vector<double> out(nState);
if (nativeState.count("Y")) {
size_t offset = nativeState["Y"];
for (size_t loc = 0; loc < m_size; loc++) {
setLoc(loc, true); // set location and restore state
phase->setMassFractions(m_data->data() + m_loc * m_stride + offset);
m_sol->thermo()->saveState(out);
setState(loc, out);
}
} else if (nativeState.count("X")) {
size_t offset = nativeState["X"];
for (size_t loc = 0; loc < m_size; loc++) {
setLoc(loc, true); // set location and restore state
phase->setMoleFractions(m_data->data() + m_loc * m_stride + offset);
m_sol->thermo()->saveState(out);
setState(loc, out);
}
} else {
throw NotImplementedError("SolutionArray::normalize",
"Not implemented for mode '{}'.", phase->nativeMode());
}
}

AnyMap SolutionArray::getAuxiliary(size_t loc)
{
setLoc(loc);
Expand Down

0 comments on commit 525973a

Please sign in to comment.