Skip to content

Commit

Permalink
Make SolutionArray::share a member function
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Mar 16, 2023
1 parent 26d73a3 commit 0c5fc1d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
9 changes: 3 additions & 6 deletions include/cantera/base/SolutionArray.h
Expand Up @@ -32,8 +32,7 @@ class SolutionArray
size_t size,
const AnyMap& meta);

SolutionArray(const shared_ptr<SolutionArray>& arr,
const vector<int>& indices);
SolutionArray(const SolutionArray& arr, const vector<int>& indices);

public:
virtual ~SolutionArray() {}
Expand All @@ -57,13 +56,11 @@ class SolutionArray
*
* Both SolutionArray object share common data. The method is used for slicing
* of SolutionArrays from high-level API's. Note that meta data are not inherited.
* @param other SolutionArray object containing shared data
* @param selected List of locations for shared entries
*/
static shared_ptr<SolutionArray> share(const shared_ptr<SolutionArray>& other,
const vector<int>& selected)
shared_ptr<SolutionArray> share(const vector<int>& selected)
{
return shared_ptr<SolutionArray>(new SolutionArray(other, selected));
return shared_ptr<SolutionArray>(new SolutionArray(*this, selected));
}

//! Reset all entries of the SolutionArray to the current Solution state
Expand Down
4 changes: 1 addition & 3 deletions interfaces/cython/cantera/solutionbase.pxd
Expand Up @@ -71,6 +71,7 @@ cdef extern from "cantera/base/Solution.h" namespace "Cantera":

cdef extern from "cantera/base/SolutionArray.h" namespace "Cantera":
cdef cppclass CxxSolutionArray "Cantera::SolutionArray":
shared_ptr[CxxSolutionArray] share(vector[int]&) except +translate_exception
void reset() except +translate_exception
size_t size()
void resize(size_t) except +translate_exception
Expand Down Expand Up @@ -99,9 +100,6 @@ cdef extern from "cantera/base/SolutionArray.h" namespace "Cantera":
cdef shared_ptr[CxxSolutionArray] CxxNewSolutionArray "Cantera::SolutionArray::create" (
shared_ptr[CxxSolution], size_t, CxxAnyMap&) except +translate_exception

cdef shared_ptr[CxxSolutionArray] CxxShareSolutionArray "Cantera::SolutionArray::share" (
shared_ptr[CxxSolutionArray], vector[int]&) except +translate_exception


ctypedef void (*transportMethod1d)(CxxTransport*, double*) except +translate_exception
ctypedef void (*transportMethod2d)(CxxTransport*, size_t, double*) except +translate_exception
Expand Down
3 changes: 1 addition & 2 deletions interfaces/cython/cantera/solutionbase.pyx
Expand Up @@ -482,7 +482,6 @@ cdef object _wrap_Solution(shared_ptr[CxxSolution] cxx_soln):
if isinstance(soln, Interface):
iface = soln
iface.surf = <CxxSurfPhase*>(soln.thermo)
# <InterfaceKinetics>(iface)._setup_phase_indices()
iface._setup_phase_indices()
return soln

Expand Down Expand Up @@ -515,7 +514,7 @@ cdef class SolutionArrayBase:
cdef vector[int] cxx_selected
for loc in selected:
cxx_selected.push_back(loc)
dest._base = CxxShareSolutionArray(self._base, cxx_selected)
dest._base = self.base.share(cxx_selected)
dest.base = dest._base.get()
return dest

Expand Down
14 changes: 7 additions & 7 deletions src/base/SolutionArray.cpp
Expand Up @@ -58,15 +58,15 @@ SolutionArray::SolutionArray(const shared_ptr<Solution>& sol,
m_apiShape[0] = m_dataSize;
}

SolutionArray::SolutionArray(const shared_ptr<SolutionArray>& other,
SolutionArray::SolutionArray(const SolutionArray& other,
const vector<int>& selected)
: m_sol(other->solution())
: m_sol(other.m_sol)
, m_size(selected.size())
, m_dataSize(other->m_data->size())
, m_stride(other->m_stride)
, m_data(other->m_data)
, m_extra(other->m_extra)
, m_order(other->m_order)
, m_dataSize(other.m_data->size())
, m_stride(other.m_stride)
, m_data(other.m_data)
, m_extra(other.m_extra)
, m_order(other.m_order)
, m_shared(true)
, m_active(selected)
{
Expand Down
12 changes: 6 additions & 6 deletions test/general/test_composite.cpp
Expand Up @@ -271,7 +271,7 @@ TEST(SolutionArray, extraSlicedDoubles) {
arr->setComponent("spam", any);

auto indices = vector<int>({1, 2, 3});
auto sliced = SolutionArray::share(arr, indices);
auto sliced = arr->share(indices);
testSingleCol<double>(*sliced, 2.71828, true);
}

Expand All @@ -290,7 +290,7 @@ TEST(SolutionArray, extraSlicedIntegers) {
ASSERT_EQ(arr->getAuxiliary(1)["spam"].asInt(), 3);

auto indices = vector<int>({1, 2, 3});
auto sliced = SolutionArray::share(arr, indices);
auto sliced = arr->share(indices);
ASSERT_EQ(sliced->getAuxiliary(0)["spam"].asInt(), 3);
ASSERT_EQ(sliced->getAuxiliary(1)["spam"].asInt(), 42);
testSingleCol<long int>(*sliced, 101, true);
Expand All @@ -307,7 +307,7 @@ TEST(SolutionArray, extraSlicedStrings) {
arr->setComponent("spam", any);

auto indices = vector<int>({1, 2, 3});
auto sliced = SolutionArray::share(arr, indices);
auto sliced = arr->share(indices);
testSingleCol<string>(*sliced, "bar", true);
}

Expand Down Expand Up @@ -418,7 +418,7 @@ TEST(SolutionArray, extraSlicedDoubleArrays) {
arr->setComponent("spam", any);

auto indices = vector<int>({1, 2, 3});
auto sliced = SolutionArray::share(arr, indices);
auto sliced = arr->share(indices);

testMultiCol<double>(*sliced, vector<double>({1.1, 2.2}), true);
}
Expand All @@ -432,7 +432,7 @@ TEST(SolutionArray, extraSlicedIntegerArrays) {
arr->setComponent("spam", any);

auto indices = vector<int>({1, 2, 3});
auto sliced = SolutionArray::share(arr, indices);
auto sliced = arr->share(indices);

testMultiCol<long int>(*sliced, vector<long int>({3, 4, 5}), true);
}
Expand All @@ -454,7 +454,7 @@ TEST(SolutionArray, extraSlicedStringArrays) {
ASSERT_EQ(arr->getAuxiliary(1)["spam"].asVector<string>()[0], "abc");

auto indices = vector<int>({1, 2, 3});
auto sliced = SolutionArray::share(arr, indices);
auto sliced = arr->share(indices);
ASSERT_EQ(sliced->getAuxiliary(0)["spam"].asVector<string>()[0], "abc");
ASSERT_EQ(sliced->getAuxiliary(1)["spam"].asVector<string>()[0], "a");
testMultiCol<string>(*sliced, vector<string>({"foo", "bar", "baz"}), true);
Expand Down

0 comments on commit 0c5fc1d

Please sign in to comment.