-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ublas vector wrapping support is broken #11
Comments
Also add support for
|
The existing E.g, to solve the example described in this issue,
and the line
|
The solution provided in the previous comment only appears to fix some of the failures. Further investigation shows that the |
Using the test script
The program throws a I have investigated the call stack to this point with gdb, and for both zero and non-zero values, the types involved are identical. The only difference is that in the non-zero ones, the base GDB trace for non-zero value given below:
|
Possible Workaround
|
Simplified example with typecaster A simplified example seems to work fine with the current typecaster: #include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <boost/numeric/ublas/vector.hpp>
#include <memory>
#include <string>
#include <vector>
using boost::numeric::ublas::c_vector;
// Typecaster for c_vector<double, 2>
namespace pybind11
{
namespace detail
{
template <>
struct type_caster<c_vector<double, 2>>
{
public:
typedef c_vector<double, 2> c_vector_double_2_t;
PYBIND11_TYPE_CASTER(c_vector_double_2_t, const_name("c_vector_double_2_t"));
bool load(handle src, bool convert)
{
if (!convert && !array_t<double>::check_(src)) { return false; }
auto buf = array_t<double, array::c_style | array::forcecast>::ensure(src);
if (!buf) { return false; }
if (buf.ndim() != 1 or buf.shape()[0] != 2) { return false; }
value.resize(2);
for (int i = 0; i < 2; i++)
{
value[i] = buf.data()[i];
}
return true;
}
static handle cast(const c_vector<double, 2> &src, return_value_policy policy, handle parent)
{
std::vector<size_t> shape(1, 2);
std::vector<size_t> strides(1, sizeof(double));
double *data = src.size() ? const_cast<double *>(&src[0]) : static_cast<double *>(NULL);
array a(std::move(shape), std::move(strides), data);
return a.release();
}
};
}
}
// Simplified CellPopulation
template <unsigned SPACE_DIM>
class CellPopulation
{
public:
unsigned getDim() { return SPACE_DIM; }
};
template class CellPopulation<2>;
// Simplified BoundaryCondition
template <unsigned ELEMENT_DIM, unsigned SPACE_DIM = ELEMENT_DIM>
class BoundaryCondition
{
public:
BoundaryCondition(CellPopulation<SPACE_DIM> *population, c_vector<double, 2> point, c_vector<double, 2> normal)
{
_total = point[0] + point[1] + normal[0] + normal[1] + population->getDim();
}
double getTotal() { return _total; }
private:
double _total;
};
template class BoundaryCondition<2, 2>;
// Pybind11 Bindings
namespace py = pybind11;
PYBIND11_MODULE(example, m)
{
py::class_<CellPopulation<2>>(m, "CellPopulation2")
.def(py::init<>())
.def("getDim", &CellPopulation<2>::getDim);
py::class_<BoundaryCondition<2, 2>>(m, "BoundaryCondition2_2")
.def(py::init<CellPopulation<2> *, c_vector<double, 2>, c_vector<double, 2>>())
.def("getTotal", &BoundaryCondition<2, 2>::getTotal);
}
Python: import numpy as np
from example import CellPopulation2, BoundaryCondition2_2
pop = CellPopulation2()
print(pop.getDim())
point = np.array([0.0,0.0])
normal = np.array([0.1,0.0])
bc = BoundaryCondition2_2(pop, point, normal)
print(bc.getTotal()) |
Passing Tests
|
TestVertexBasedCellSimulationsPythonTutorial Error
Trace OffLatticeSimulation::Solve(...);
OffLatticeSimulation::ApplyBoundaries(...);
PlaneBoundaryCondition::ImposeBoundaryCondition(...);
if (dynamic_cast<AbstractOffLatticeCellPopulation<ELEMENT_DIM, SPACE_DIM>*>(this->mpCellPopulation) == nullptr){...} |
TestPetscToolsPython Error
|
Closing this issue as the remaining failing tests are unrelated to |
It appears that the errors in constructors which contained |
Describe the bug
See PyChaste/issues/4
To Reproduce
Install PyChaste from conda:
In Python:
Error
The text was updated successfully, but these errors were encountered: