Skip to content

Commit

Permalink
Kick out py_shared_ptr and instead keep the python instance alive man…
Browse files Browse the repository at this point in the history
…ually by holding a reference to it

See pybind/pybind11#1389 for why py_shared_ptr was needed in the first place, and the comment from May 27 why we may not want to use it (reference cycle)
  • Loading branch information
florianwechsung committed Sep 23, 2021
1 parent 4bf5b85 commit 2b40a3a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/simsopt/field/magneticfieldclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ def __init__(self, field, degree, rrange, phirange, zrange, extrapolate=True, nf
logger.warning(fr"Sure about phirange[1]={phirange[1]}? When exploiting rotational symmetry, the interpolant is never evaluated for phi>2\pi/nfp.")

sopp.InterpolatedField.__init__(self, field, degree, rrange, phirange, zrange, extrapolate, nfp, stellsym)
self.__field == field

def to_vtk(self, filename, h=0.1):
"""Export the field evaluated on a regular grid for visualisation with e.g. Paraview."""
Expand Down
30 changes: 0 additions & 30 deletions src/simsoptpp/py_shared_ptr.h

This file was deleted.

2 changes: 0 additions & 2 deletions src/simsoptpp/python.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include "pybind11/functional.h"
#include "py_shared_ptr.h"
PYBIND11_DECLARE_HOLDER_TYPE(T, py_shared_ptr<T>);
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp" // Numpy bindings
typedef xt::pyarray<double> PyArray;
Expand Down
8 changes: 3 additions & 5 deletions src/simsoptpp/python_curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "pybind11/stl.h"
#include "xtensor-python/pyarray.hpp" // Numpy bindings
typedef xt::pyarray<double> PyArray;
#include "py_shared_ptr.h"
PYBIND11_DECLARE_HOLDER_TYPE(T, py_shared_ptr<T>);
using std::shared_ptr;


Expand Down Expand Up @@ -90,16 +88,16 @@ template <typename T, typename S> void register_common_curve_methods(S &c) {
}

void init_curves(py::module_ &m) {
auto pycurve = py::class_<PyCurve, py_shared_ptr<PyCurve>, PyCurveTrampoline<PyCurve>>(m, "Curve")
auto pycurve = py::class_<PyCurve, shared_ptr<PyCurve>, PyCurveTrampoline<PyCurve>>(m, "Curve")
.def(py::init<vector<double>>());
register_common_curve_methods<PyCurve>(pycurve);

auto pycurvexyzfourier = py::class_<PyCurveXYZFourier, py_shared_ptr<PyCurveXYZFourier>, PyCurveXYZFourierTrampoline<PyCurveXYZFourier>, PyCurve>(m, "CurveXYZFourier")
auto pycurvexyzfourier = py::class_<PyCurveXYZFourier, shared_ptr<PyCurveXYZFourier>, PyCurveXYZFourierTrampoline<PyCurveXYZFourier>, PyCurve>(m, "CurveXYZFourier")
.def(py::init<vector<double>, int>())
.def_readonly("dofs", &PyCurveXYZFourier::dofs);
register_common_curve_methods<PyCurveXYZFourier>(pycurvexyzfourier);

auto pycurverzfourier = py::class_<PyCurveRZFourier, py_shared_ptr<PyCurveRZFourier>, PyCurveRZFourierTrampoline<PyCurveRZFourier>, PyCurve>(m, "CurveRZFourier")
auto pycurverzfourier = py::class_<PyCurveRZFourier, shared_ptr<PyCurveRZFourier>, PyCurveRZFourierTrampoline<PyCurveRZFourier>, PyCurve>(m, "CurveRZFourier")
//.def(py::init<int, int>())
.def(py::init<vector<double>, int, int, bool>())
.def_readwrite("rc", &PyCurveRZFourier::rc)
Expand Down
2 changes: 0 additions & 2 deletions src/simsoptpp/python_surfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "pybind11/stl.h"
#include "xtensor-python/pyarray.hpp" // Numpy bindings
typedef xt::pyarray<double> PyArray;
#include "py_shared_ptr.h"
PYBIND11_DECLARE_HOLDER_TYPE(T, py_shared_ptr<T>);
using std::shared_ptr;
using std::vector;

Expand Down

0 comments on commit 2b40a3a

Please sign in to comment.