Skip to content

Commit

Permalink
[base] Augment C++ SolutionArray API
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Sep 29, 2022
1 parent 48fbac9 commit 9c9d3e0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
41 changes: 33 additions & 8 deletions include/cantera/base/SolutionArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Cantera

class Solution;

/**
/*!
* A container class providing a convenient interface for representing many
* thermodynamic states using the same Solution object. C++ SolutionArray objects are
* one-dimensional by design; extensions to multi-dimensional arrays need to be
Expand All @@ -45,14 +45,14 @@ class SolutionArray
new SolutionArray(sol, size, meta));
}

/**
/*!
* Initialize SolutionArray with independent memory management
*
* @param extra Names of auxiliary data
*/
void initialize(const std::vector<std::string>& extra={});

/**
/*!
* Initialize SolutionArray object with mapped memory
*
* @param data Pointer to mapped memory address
Expand All @@ -65,22 +65,46 @@ class SolutionArray
size_t stride,
const std::vector<std::pair<std::string, size_t>>& offsets);

/**
/*!
* Size of SolutionArray (number of entries)
*/
int size() const {
return m_size;
}

/**
/*!
* SolutionArray meta data.
*/
AnyMap& meta() {
return m_meta;
}

/*!
* Update the buffered index used to access entries.
*/
void setIndex(size_t index);

/*!
* Retrieve the state vector for a single entry. If index is valid, it is updated;
* otherwise, the last previously used index is referenced.
*/
vector_fp getState(size_t index=npos);

/*!
* Retrieve auxiliary data for a single entry. If index is valid, it is updated;
* otherwise, the last previously used index is referenced.
*/
std::map<std::string, double> getAuxiliary(size_t index=npos);

/*!
* Save the current SolutionArray to a container file.
*
* @param fname Name of output container file
* @param id Identifier of SolutionArray within the container file
*/
void save(const std::string& fname, const std::string& id);

/**
/*!
* Restore SolutionArray from a container file.
*
* @param fname Name of container file
Expand All @@ -99,8 +123,9 @@ class SolutionArray
size_t m_size; //!< Number of entries in SolutionArray
size_t m_stride; //!< Stride between SolutionArray entries
AnyMap m_meta; //!< Metadata
bool m_managed = false; //!< Flag indicating whether memory is externally managed
size_t m_index = npos; //!< Buffered index

bool m_managed = false; //!< Flag indicating whether memory is externally managed
shared_ptr<vector_fp> m_work; //!< Work vector holding states (if not managed)
double* m_data; //!< Memory location holding state information (may be augmented)
std::map<std::string, shared_ptr<vector_fp>> m_other; //!< Auxiliary data
Expand All @@ -109,7 +134,7 @@ class SolutionArray
};


// /**
// /*!
// * Create a SolutionArray object with independent memory management
// *
// * @param sol The Solution object associated with state information
Expand Down
42 changes: 42 additions & 0 deletions src/base/SolutionArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,48 @@ void SolutionArray::initialize(
}
}

void SolutionArray::setIndex(size_t index)
{
if (m_size == 0) {
throw CanteraError("SolutionArray::setIndex",
"Unable to set index in empty SolutionArray.");
} else if (index == npos) {
if (m_index == npos) {
throw CanteraError("SolutionArray::setIndex",
"Both current and buffered indices are invalid.");
}
return;
} else if (index == m_index) {
return;
} else if (index >= m_size) {
throw IndexError("SolutionArray::setIndex", "entries", index, m_size - 1);
}
m_index = index;
size_t nState = m_sol->thermo()->stateSize();
m_sol->thermo()->restoreState(nState, &m_data[m_index * m_stride]);
}

vector_fp SolutionArray::getState(size_t index)
{
setIndex(index);
size_t nState = m_sol->thermo()->stateSize();
vector_fp out(nState);
m_sol->thermo()->saveState(out);
// std::copy(&m_data[offset], &m_data[offset + nState], out.begin());
return out;
}

std::map<std::string, double> SolutionArray::getAuxiliary(size_t index)
{
setIndex(index);
std::map<std::string, double> out;
for (auto& item : m_other) {
auto& extra = *item.second;
out[item.first] = extra[m_index];
}
return out;
}

void SolutionArray::save(const std::string& fname, const std::string& id)
{
throw CanteraError("SolutionArray::save", "Not implemented.");
Expand Down

0 comments on commit 9c9d3e0

Please sign in to comment.