From bc92fcac102f6f75305058a186a9558606e8c371 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Thu, 29 Jun 2023 08:42:58 -0600 Subject: [PATCH] [base] Use 'int' for SolutionArray API Fixes MSVC compiler warnings --- include/cantera/base/SolutionArray.h | 22 ++++---- src/base/SolutionArray.cpp | 77 +++++++++++++++------------- src/oneD/StFlow.cpp | 3 +- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/include/cantera/base/SolutionArray.h b/include/cantera/base/SolutionArray.h index 1dbc3242ab..b51216d0be 100644 --- a/include/cantera/base/SolutionArray.h +++ b/include/cantera/base/SolutionArray.h @@ -27,7 +27,7 @@ class SolutionArray { private: SolutionArray(const shared_ptr& sol, - size_t size, + int size, const AnyMap& meta); SolutionArray(const SolutionArray& arr, const vector& indices); @@ -43,7 +43,7 @@ class SolutionArray * @param meta AnyMap holding SolutionArray meta data */ static shared_ptr create(const shared_ptr& sol, - size_t size=0, + int size=0, const AnyMap& meta={}) { return shared_ptr(new SolutionArray(sol, size, meta)); @@ -70,7 +70,7 @@ class SolutionArray } //! Resize SolutionArray objects with a single dimension (default). - void resize(size_t size); + void resize(int size); //! SolutionArray shape information used by high-level API's. vector apiShape() const { @@ -82,8 +82,8 @@ class SolutionArray void setApiShape(const vector& shape); //! Number of SolutionArray dimensions used by high-level API's. - size_t apiNdim() const { - return m_apiShape.size(); + int apiNdim() const { + return static_cast(m_apiShape.size()); } /*! @@ -142,18 +142,18 @@ class SolutionArray /*! * Update the buffered location used to access SolutionArray entries. */ - void setLoc(size_t loc, bool restore=true); + void setLoc(int loc, bool restore=true); /*! * Update state at given location to state of associated Solution object. */ - void updateState(size_t loc); + void updateState(int loc); //! Retrieve the state vector for a given location. - vector getState(size_t loc); + vector getState(int loc); //! Set the state vector for a given location. - void setState(size_t loc, const vector& state); + void setState(int loc, const vector& state); //! Normalize mass/mole fractions void normalize(); @@ -178,10 +178,10 @@ class SolutionArray vector listExtra(bool all=true) const; //! Retrieve auxiliary data for a given location. - AnyMap getAuxiliary(size_t loc); + AnyMap getAuxiliary(int loc); //! Set auxiliary data for a given location. - void setAuxiliary(size_t loc, const AnyMap& data); + void setAuxiliary(int loc, const AnyMap& data); //! Append location entry at end of SolutionArray. void append(const vector& state, const AnyMap& extra); diff --git a/src/base/SolutionArray.cpp b/src/base/SolutionArray.cpp index 0d199ceb0e..56437ce3dd 100644 --- a/src/base/SolutionArray.cpp +++ b/src/base/SolutionArray.cpp @@ -37,7 +37,7 @@ namespace Cantera { SolutionArray::SolutionArray(const shared_ptr& sol, - size_t size, const AnyMap& meta) + int size, const AnyMap& meta) : m_sol(sol) , m_size(size) , m_dataSize(size) @@ -52,11 +52,11 @@ SolutionArray::SolutionArray(const shared_ptr& sol, m_extra = make_shared>(); m_order = make_shared>(); for (size_t i = 0; i < m_dataSize; ++i) { - m_active.push_back(i); + m_active.push_back(static_cast(i)); } reset(); m_apiShape.resize(1); - m_apiShape[0] = m_dataSize; + m_apiShape[0] = static_cast(m_dataSize); } SolutionArray::SolutionArray(const SolutionArray& other, @@ -150,7 +150,7 @@ void SolutionArray::reset() } } -void SolutionArray::resize(size_t size) +void SolutionArray::resize(int size) { if (apiNdim() > 1) { throw CanteraError("SolutionArray::resize", @@ -161,8 +161,8 @@ void SolutionArray::resize(size_t size) throw CanteraError("SolutionArray::resize", "Unable to resize as data are shared by multiple objects."); } - _resize(size); - m_apiShape[0] = size; + _resize(static_cast(size)); + m_apiShape[0] = static_cast(size); } void SolutionArray::setApiShape(const vector& shape) @@ -196,7 +196,7 @@ void SolutionArray::_resize(size_t size) } m_active.clear(); for (size_t i = 0; i < m_dataSize; ++i) { - m_active.push_back(i); + m_active.push_back(static_cast(i)); } } @@ -209,8 +209,9 @@ vector doubleColumn(string name, const vector& comp, vector data; vector raw; string notation = fmt::format("{{:{}.{}g}}", width, (width - 1) / 2); - int dots = comp.size() + 1; - if (comp.size() <= rows) { + int csize = static_cast(comp.size()); + int dots = csize + 1; + if (csize <= rows) { for (const auto& val : comp) { data.push_back(val); raw.push_back(boost::trim_copy(fmt::format(notation, val))); @@ -221,7 +222,7 @@ vector doubleColumn(string name, const vector& comp, data.push_back(comp[row]); raw.push_back(boost::trim_copy(fmt::format(notation, comp[row]))); } - for (int row = comp.size() - rows / 2; row < comp.size(); row++) { + for (int row = csize - rows / 2; row < csize; row++) { data.push_back(comp[row]); raw.push_back(boost::trim_copy(fmt::format(notation, comp[row]))); } @@ -296,8 +297,9 @@ vector integerColumn(string name, const vector& comp, vector data; string notation = fmt::format("{{:{}}}", width); size_t maxLen = 2; // minimum column width is 2 - int dots = comp.size() + 1; - if (comp.size() <= rows) { + int csize = static_cast(comp.size()); + int dots = csize + 1; + if (csize <= rows) { for (const auto& val : comp) { data.push_back(val); string name = boost::trim_copy(fmt::format(notation, val)); @@ -316,7 +318,7 @@ vector integerColumn(string name, const vector& comp, } maxLen = std::max(maxLen, name.size()); } - for (int row = comp.size() - rows / 2; row < comp.size(); row++) { + for (int row = csize - rows / 2; row < csize; row++) { data.push_back(comp[row]); string name = boost::trim_copy(fmt::format(notation, comp[row])); if (name[0] == '-') { @@ -355,8 +357,9 @@ vector stringColumn(string name, const vector& comp, vector data; string notation = fmt::format("{{:{}}}", width); size_t maxLen = 3; // minimum column width is 3 - int dots = comp.size() + 1; - if (comp.size() <= rows) { + int csize = static_cast(comp.size()); + int dots = csize + 1; + if (csize <= rows) { for (const auto& val : comp) { data.push_back(val); maxLen = std::max(maxLen, @@ -369,7 +372,7 @@ vector stringColumn(string name, const vector& comp, maxLen = std::max(maxLen, boost::trim_copy(fmt::format(notation, comp[row])).size()); } - for (int row = comp.size() - rows / 2; row < comp.size(); row++) { + for (int row = csize - rows / 2; row < csize; row++) { data.push_back(comp[row]); maxLen = std::max(maxLen, boost::trim_copy(fmt::format(notation, comp[row])).size()); @@ -434,7 +437,8 @@ vector formatColumn(string name, const AnyValue& comp, int rows, int wid col.push_back(repr); } col.push_back(fmt::format(notation, "...")); - for (int row = size - rows / 2; row < size; row++) { + int size_ = static_cast(size); + for (int row = size_ - rows / 2; row < size_; row++) { col.push_back(repr); } } @@ -732,37 +736,38 @@ void SolutionArray::setComponent(const string& name, const AnyValue& data) } } -void SolutionArray::setLoc(size_t loc, bool restore) +void SolutionArray::setLoc(int loc, bool restore) { + size_t loc_ = static_cast(loc); if (m_size == 0) { throw CanteraError("SolutionArray::setLoc", "Unable to set location in empty SolutionArray."); - } else if (loc == npos) { + } else if (loc < 0) { if (m_loc == npos) { throw CanteraError("SolutionArray::setLoc", "Both current and buffered indices are invalid."); } return; - } else if (m_active[loc] == (int)m_loc) { + } else if (m_active[loc_] == m_loc) { return; - } else if (loc >= m_size) { - throw IndexError("SolutionArray::setLoc", "indices", loc, m_size - 1); + } else if (loc_ >= m_size) { + throw IndexError("SolutionArray::setLoc", "indices", loc_, m_size - 1); } - m_loc = m_active[loc]; + 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); } } -void SolutionArray::updateState(size_t loc) +void SolutionArray::updateState(int loc) { setLoc(loc, false); size_t nState = m_sol->thermo()->stateSize(); m_sol->thermo()->saveState(nState, m_data->data() + m_loc * m_stride); } -vector SolutionArray::getState(size_t loc) +vector SolutionArray::getState(int loc) { setLoc(loc); size_t nState = m_sol->thermo()->stateSize(); @@ -771,7 +776,7 @@ vector SolutionArray::getState(size_t loc) return out; } -void SolutionArray::setState(size_t loc, const vector& state) +void SolutionArray::setState(int loc, const vector& state) { size_t nState = m_sol->thermo()->stateSize(); if (state.size() != nState) { @@ -794,7 +799,7 @@ void SolutionArray::normalize() { vector out(nState); if (nativeState.count("Y")) { size_t offset = nativeState["Y"]; - for (size_t loc = 0; loc < m_size; loc++) { + for (int loc = 0; loc < static_cast(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); @@ -802,7 +807,7 @@ void SolutionArray::normalize() { } } else if (nativeState.count("X")) { size_t offset = nativeState["X"]; - for (size_t loc = 0; loc < m_size; loc++) { + for (int loc = 0; loc < static_cast(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); @@ -814,7 +819,7 @@ void SolutionArray::normalize() { } } -AnyMap SolutionArray::getAuxiliary(size_t loc) +AnyMap SolutionArray::getAuxiliary(int loc) { setLoc(loc); AnyMap out; @@ -842,7 +847,7 @@ AnyMap SolutionArray::getAuxiliary(size_t loc) return out; } -void SolutionArray::setAuxiliary(size_t loc, const AnyMap& data) +void SolutionArray::setAuxiliary(int loc, const AnyMap& data) { setLoc(loc, false); for (const auto& [name, value] : data) { @@ -1051,7 +1056,7 @@ void SolutionArray::writeEntry(const string& fname, bool overwrite, const string output << header.str() << std::endl << std::setprecision(9); vector buf(speciesNames.size(), 0.); - for (size_t row = 0; row < m_size; row++) { + for (int row = 0; row < static_cast(m_size); row++) { setLoc(row); if (mole) { m_sol->thermo()->getMoleFractions(buf.data()); @@ -1254,7 +1259,7 @@ void SolutionArray::append(const vector& state, const AnyMap& extra) "Unable to append multi-dimensional arrays."); } - size_t pos = size(); + int pos = size(); resize(pos + 1); try { setState(pos, state); @@ -1643,7 +1648,7 @@ void SolutionArray::readEntry(const string& fname, const string& name, m_meta.erase("api-shape"); } else { // legacy format; size needs to be detected - resize(size); + resize(static_cast(size)); } if (m_size == 0) { @@ -1793,7 +1798,7 @@ void SolutionArray::readEntry(const AnyMap& root, const string& name, const stri } // set size and initialize - size_t size = 0; + long size = 0; if (path.hasKey("size")) { // one-dimensional array resize(path["size"].asInt()); @@ -1806,9 +1811,9 @@ void SolutionArray::readEntry(const AnyMap& root, const string& name, const stri size = path.getInt("points", 0); if (!path.hasKey("T") && !path.hasKey("temperature")) { // overwrite size - Sim1D erroneously assigns '1' - size = 0; + size = (long)0; } - resize(size); + resize(static_cast(size)); } m_extra->clear(); diff --git a/src/oneD/StFlow.cpp b/src/oneD/StFlow.cpp index bc624b889d..90f2fefa70 100644 --- a/src/oneD/StFlow.cpp +++ b/src/oneD/StFlow.cpp @@ -789,7 +789,8 @@ AnyMap StFlow::getMeta() const shared_ptr StFlow::asArray(const double* soln) const { - auto arr = SolutionArray::create(m_solution, nPoints(), getMeta()); + auto arr = SolutionArray::create( + m_solution, static_cast(nPoints()), getMeta()); arr->addExtra("grid", false); // leading entry AnyValue value; value = m_z;