Return vectors by value

Return vectors by value instead of unique pointers to arrays.
This commit is contained in:
Håkon Hægland 2023-11-09 19:09:31 +01:00
parent 55aab60549
commit 757c67f284
5 changed files with 40 additions and 48 deletions

View File

@ -48,13 +48,10 @@ namespace Opm::Pybind
};
public:
PyFluidState(Simulator *ebos_simulator);
std::unique_ptr<double []> getFluidStateVariable(
const std::string &name, std::size_t *size ) const;
std::unique_ptr<int []> getPrimaryVarMeaning(
const std::string &variable, std::size_t *size) const;
std::vector<double> getFluidStateVariable(const std::string &name) const;
std::vector<int> getPrimaryVarMeaning(const std::string &variable) const;
std::map<std::string, int> getPrimaryVarMeaningMap(const std::string &variable) const;
std::unique_ptr<double []> getPrimaryVariable(
const std::string &idx_name, std::size_t *size ) const;
std::vector<double> getPrimaryVariable(const std::string &idx_name) const;
void setPrimaryVariable(const std::string &idx_name, const double *data, std::size_t size);
private:

View File

@ -31,14 +31,14 @@ PyFluidState(Simulator* ebos_simulator) : ebos_simulator_(ebos_simulator)
// ------------------------------------
template <class TypeTag>
std::unique_ptr<int []>
std::vector<int>
PyFluidState<TypeTag>::
getPrimaryVarMeaning(const std::string &variable, std::size_t *size) const {
getPrimaryVarMeaning(const std::string &variable) const {
Model &model = this->ebos_simulator_->model();
auto &sol = model.solution(/*timeIdx*/0);
*size = model.numGridDof();
auto array = std::make_unique<int []>(*size);
for (unsigned dof_idx = 0; dof_idx < *size; ++dof_idx) {
auto size = model.numGridDof();
std::vector<int> array(size);
for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
auto primary_vars = sol[dof_idx];
array[dof_idx] = getVariableMeaning_(primary_vars, variable);
}
@ -106,20 +106,20 @@ getPrimaryVarMeaningMap(const std::string &variable) const
kr_g = Gas relperm,
*/
template <class TypeTag>
std::unique_ptr<double []>
std::vector<double>
PyFluidState<TypeTag>::
getFluidStateVariable(const std::string &name, std::size_t *size ) const
getFluidStateVariable(const std::string &name) const
{
using ElementIterator = typename GridView::template Codim<0>::Iterator;
using Element = typename GridView::template Codim<0>::Entity;
Model &model = this->ebos_simulator_->model();
*size = model.numGridDof();
auto size = model.numGridDof();
std::vector<double> array(size);
const auto& grid_view = this->ebos_simulator_->vanguard().gridView();
/* NOTE: grid_view.size(0) should give the same value as
* model.numGridDof()
*/
auto array = std::make_unique<double []>(*size);
ElementContext elem_ctx(*this->ebos_simulator_);
ElementIterator elem_itr = grid_view.template begin</*codim=*/0>();
const ElementIterator& elem_end_itr = grid_view.template end</*codim=*/0>();
@ -128,7 +128,7 @@ getFluidStateVariable(const std::string &name, std::size_t *size ) const
const Element& elem = *elem_itr;
elem_ctx.updatePrimaryStencil(elem);
elem_ctx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
for (unsigned dof_idx = 0;dof_idx < elem_ctx.numPrimaryDof(/*timeIdx=*/0); ++dof_idx) {
for (unsigned dof_idx = 0; dof_idx < elem_ctx.numPrimaryDof(/*timeIdx=*/0); ++dof_idx) {
const auto& int_quants = elem_ctx.intensiveQuantities(dof_idx, /*timeIdx=*/0);
const auto& fs = int_quants.fluidState();
unsigned global_dof_idx = elem_ctx.globalSpaceIndex(dof_idx, /*timeIdx=*/0);
@ -139,16 +139,16 @@ getFluidStateVariable(const std::string &name, std::size_t *size ) const
}
template <class TypeTag>
std::unique_ptr<double []>
std::vector<double>
PyFluidState<TypeTag>::
getPrimaryVariable(const std::string &idx_name, std::size_t *size ) const
getPrimaryVariable(const std::string &idx_name) const
{
std::size_t primary_var_idx = getPrimaryVarIndex_(idx_name);
Model &model = this->ebos_simulator_->model();
auto &sol = model.solution(/*timeIdx*/0);
*size = model.numGridDof();
auto array = std::make_unique<double []>(*size);
for (unsigned dof_idx = 0; dof_idx < *size; ++dof_idx) {
auto size = model.numGridDof();
std::vector<double> array(size);
for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
auto primary_vars = sol[dof_idx];
array[dof_idx] = primary_vars[primary_var_idx];
}

View File

@ -45,8 +45,8 @@ namespace Opm::Pybind
PyMaterialState(Simulator *ebos_simulator)
: ebos_simulator_(ebos_simulator) { }
std::unique_ptr<double []> getCellVolumes( std::size_t *size);
std::unique_ptr<double []> getPorosity( std::size_t *size);
std::vector<double> getCellVolumes();
std::vector<double> getPorosity();
void setPorosity(const double *poro, std::size_t size);
private:
Simulator *ebos_simulator_;

View File

@ -22,29 +22,29 @@
namespace Opm::Pybind {
template <class TypeTag>
std::unique_ptr<double []>
std::vector<double>
PyMaterialState<TypeTag>::
getCellVolumes( std::size_t *size)
getCellVolumes()
{
Model &model = this->ebos_simulator_->model();
*size = model.numGridDof();
auto array = std::make_unique<double []>(*size);
for (unsigned dof_idx = 0; dof_idx < *size; ++dof_idx) {
auto size = model.numGridDof();
std::vector<double> array(size);
for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
array[dof_idx] = model.dofTotalVolume(dof_idx);
}
return array;
}
template <class TypeTag>
std::unique_ptr<double []>
std::vector<double>
PyMaterialState<TypeTag>::
getPorosity( std::size_t *size)
getPorosity()
{
Problem &problem = this->ebos_simulator_->problem();
Model &model = this->ebos_simulator_->model();
*size = model.numGridDof();
auto array = std::make_unique<double []>(*size);
for (unsigned dof_idx = 0; dof_idx < *size; ++dof_idx) {
auto size = model.numGridDof();
std::vector<double> array(size);
for (unsigned dof_idx = 0; dof_idx < size; ++dof_idx) {
array[dof_idx] = problem.referencePorosity(dof_idx, /*timeIdx*/0);
}
return array;

View File

@ -95,9 +95,8 @@ int PyBlackOilSimulator::currentStep()
}
py::array_t<double> PyBlackOilSimulator::getCellVolumes() {
std::size_t len;
auto array = getMaterialState().getCellVolumes(&len);
return py::array(len, array.get());
auto vector = getMaterialState().getCellVolumes();
return py::array(vector.size(), vector.data());
}
double PyBlackOilSimulator::getDT() {
@ -106,36 +105,32 @@ double PyBlackOilSimulator::getDT() {
py::array_t<double> PyBlackOilSimulator::getPorosity()
{
std::size_t len;
auto array = getMaterialState().getPorosity(&len);
return py::array(len, array.get());
auto vector = getMaterialState().getPorosity();
return py::array(vector.size(), vector.data());
}
py::array_t<double>
PyBlackOilSimulator::
getFluidStateVariable(const std::string &name) const
{
std::size_t len;
auto array = getFluidState().getFluidStateVariable(name, &len);
return py::array(len, array.get());
auto vector = getFluidState().getFluidStateVariable(name);
return py::array(vector.size(), vector.data());
}
py::array_t<double>
PyBlackOilSimulator::
getPrimaryVariable(const std::string &variable) const
{
std::size_t len;
auto array = getFluidState().getPrimaryVariable(variable, &len);
return py::array(len, array.get());
auto vector = getFluidState().getPrimaryVariable(variable);
return py::array(vector.size(), vector.data());
}
py::array_t<int>
PyBlackOilSimulator::
getPrimaryVarMeaning(const std::string &variable) const
{
std::size_t len;
auto array = getFluidState().getPrimaryVarMeaning(variable, &len);
return py::array(len, array.get());
auto vector = getFluidState().getPrimaryVarMeaning(variable);
return py::array(vector.size(), vector.data());
}
std::map<std::string, int>