diff --git a/sdpm/_src/sdpmw.i b/sdpm/_src/sdpmw.i index ecfd4a16fd02228256522fc6cc442a9ef86441ae..2ce749bd24dd8e35413329a7a2f1d7f448c44dd4 100644 --- a/sdpm/_src/sdpmw.i +++ b/sdpm/_src/sdpmw.i @@ -90,6 +90,9 @@ threads="1" // SDPM typemaps %include "sdpm_def.h" // needed to access USE_CODI in SWIG interface +#ifndef USE_CODI +typedef double sdpmDouble; // needed so that SWIG does not convert sdpmDouble to double +#endif %typemap(out) sdpmDouble { #ifndef USE_CODI $result = PyFloat_FromDouble($1); @@ -106,6 +109,14 @@ threads="1" %template(std_vector_Elementp) std::vector<sdpm::Element *>; %template(std_vector_Tagp) std::vector<sdpm::Tag *>; %template(std_map_string_Tagp) std::map<std::string, sdpm::Tag *>; +%template(std_vector_vector3d) std::vector<sdpmVector3d>; +#ifndef USE_CODI +%rename(std_vector_double) std::vector<sdpmDouble>; +%rename(std_vector_vector_double) std::vector<std::vector<sdpmDouble>>; +#else +%template(std_vector_sdpmdouble) std::vector<sdpmDouble>; +%template(std_vector_vector_sdpmdouble) std::vector<std::vector<sdpmDouble>>; +#endif // SDPM %include "sdpm.h" @@ -134,26 +145,40 @@ threads="1" // Interface to SDPM types class sdpmVector3d {}; - %extend sdpmVector3d { - double getitem(int i) + double __getitem__(int i) { + if (i > 2) + throw std::runtime_error("sdpmVector3d: index out of range (size 3)!\n"); #ifndef USE_CODI return (*self)(i); #else return (*self)(i).getValue(); #endif } - std::string __str__() + // use repr instead of str so that iterables also print nicely + std::string __repr__() { - std::ostringstream out; out << *self; return out.str(); + std::ostringstream out; out << *self; + return out.str(); } +} - %pythoncode { -def __getitem__(self, idx): - return self.getitem(idx) +#ifdef USE_CODI +class sdpmDouble {}; +%extend sdpmDouble { + double __float__() + { + return (*self).getValue(); + } + // use repr instead of str so that iterables also print nicely + std::string __repr__() + { + std::ostringstream out; out << *self; + return out.str(); } } +#endif // Interface to SDPM classes namespace sdpm { diff --git a/sdpm/api/om.py b/sdpm/api/om.py index e052755b954b14a03eac0835fdce327ab701265a..4e75025d5620f735bb256089324ef9a9e3247dfe 100644 --- a/sdpm/api/om.py +++ b/sdpm/api/om.py @@ -80,8 +80,8 @@ class SdpmSolver(om.ExplicitComponent): self.adj.solve() # Set outputs for i in range(self.nf): - outputs['Q_re'][i, :, :] = np.array(self.sol.getGafMatrixRe(i)) - outputs['Q_im'][i, :, :] = np.array(self.sol.getGafMatrixIm(i)) + outputs['Q_re'][i, :, :] = np.array(self.sol.getGafMatrixRe(i), dtype=float) + outputs['Q_im'][i, :, :] = np.array(self.sol.getGafMatrixIm(i), dtype=float) class SdpmBuilder(Builder): """SDPM builder for OpenMDAO diff --git a/sdpm/src/sdpm.h b/sdpm/src/sdpm.h index eca4d3ef8b5efc02ece8d8d23907dc8fc12e8cf0..180f34c59609b94b636c2dc3afed16c2b0bed13a 100644 --- a/sdpm/src/sdpm.h +++ b/sdpm/src/sdpm.h @@ -35,6 +35,7 @@ #include <Eigen/Dense> #include <complex> +#ifndef SWIG #ifndef USE_CODI using sdpmDouble = double; #else // USE_CODI @@ -51,6 +52,7 @@ using sdpmVector3d = Eigen::Matrix<sdpmDouble, 3, 1>; using sdpmMatrixXcd = Eigen::Matrix<sdpmComplex, Eigen::Dynamic, Eigen::Dynamic>; using sdpmVectorXcd = Eigen::Matrix<sdpmComplex, Eigen::Dynamic, 1>; using sdpmVector3cd = Eigen::Matrix<sdpmComplex, 3, 1>; +#endif // SWIG namespace sdpm {