1 // -*- C++ -*- 2 // Siconos is a program dedicated to modeling, simulation and control 3 // of non smooth dynamical systems. 4 // 5 // Copyright 2021 INRIA. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 // 19 // 20 21 // SWIG interface for Siconos Kernel 22 %module(package="siconos", directors="1", allprotected="1") kernel 23 24 %include start.i 25 26 // generated docstrings from doxygen xml output 27 %include kernel-docstrings.i 28 29 #ifdef WITH_SERIALIZATION 30 %{ 31 #define KERNEL_ONLY 32 %} 33 #endif 34 35 %include serialization.i 36 37 %include picklable.i 38 39 %{ 40 #include <SiconosKernel.hpp> 41 #include <SiconosAlgebra.hpp> 42 #include <boost/numeric/ublas/matrix_sparse.hpp> 43 #include <SiconosVisitor.hpp> 44 #include "addons.hpp" 45 #include <boost/type_traits/is_polymorphic.hpp> 46 #include <boost/mpl/eval_if.hpp> 47 #include <boost/typeof/typeof.hpp> 48 #include <RotationQuaternion.hpp> 49 #include <SiconosVectorIterator.hpp> 50 #include <vector> 51 #include <cstddef> 52 %} 53 54 55 // ignores 56 %ignore nullDeleter; 57 // remove the visitor stuff, I see no use of it in Python for now --xhub 58 %ignore *::acceptSerializer; 59 %ignore *::acceptType; 60 %ignore *::accept; 61 %ignore *::acceptSP; 62 // do not wrap visitor visit : this lead to a huge amount of wrapper 63 // code generation and this fail at compile time on shared_ptr freearg 64 %ignore SiconosVisitor; 65 66 %ignore visit; 67 68 %ignore Type::str; 69 70 // cannot compile wrapper 71 %ignore statOut; 72 73 // defined in SiconosVector.cpp 74 %ignore setBlock; 75 %ignore add; 76 %ignore sub; 77 %ignore axpby; 78 %ignore axpy; 79 %ignore inner_prod; 80 %ignore outer_prod; 81 %ignore scal; 82 %ignore subscal; 83 %ignore cross_product; 84 85 // defined in SimpleMatrix.cpp 86 %ignore private_addprod; 87 %ignore private_prod; 88 // %ignore prod; 89 %ignore axpy_prod; 90 %ignore subprod; 91 %ignore axpy_prod; 92 %ignore gemv; 93 %ignore gemm; 94 95 %ignore getWMap; 96 %ignore getWBoundaryConditionsMap; 97 %ignore getDSBlocks; 98 %ignore getInvMBlock; 99 100 // SiconosMemory 101 %ignore swap; 102 103 %warnfilter(509) quaternionRotate; 104 %warnfilter(509) changeFrameAbsToBody; 105 %warnfilter(509) changeFrameBodyToAbs; 106 %warnfilter(325) Change; 107 %warnfilter(325) ChangeLogIter; 108 109 110 // common declarations with upper modules : Mechanics, IO, ... 111 %include handleException.i 112 113 %include sharedPointers.i 114 115 // handle stl data types 116 %include stl.i 117 118 // 1. Vector and Matrix <=> numpy array (dense only) 119 %include SiconosAlgebra.i 120 121 122 // 2. try to hide SP::Type on python side 123 124 125 // boost namespace (can be fixed with a correct import) 126 namespace boost 127 { 128 namespace numeric 129 { 130 namespace ublas 131 { 132 } 133 namespace bindings 134 { 135 } 136 } 137 }; 138 139 // a std::size_t definition (otherwise swig complains about it) 140 //namespace std 141 //{ 142 // typedef size_t size_t; 143 //} 144 145 146 147 // swig see those classes as abstract => no wrappers for constructors 148 // note: this means something may be wrong in .hpp headers => use -Wall to 149 // detect it 150 151 // yes, undefined private copy constructors 152 %feature("notabstract") TimeStepping; 153 %feature("notabstract") TimeSteppingCombinedProjection; 154 %feature("notabstract") TimeSteppingDirectProjection; 155 %feature("notabstract") EventDriven; 156 157 // common declarations with Numerics 158 159 // note : solver_options_delete is call by ~LCP(), ~FrictionContact(), etc. 160 %shared_ptr(SolverOptions); 161 %shared_ptr(NumericsMatrix); 162 %shared_ptr(CSparseMatrix); 163 %shared_ptr(SparseBlockStructuredMatrix); 164 //%shared_ptr(GlobalFrictionContactProblem); 165 166 %include solverOptions.i 167 168 // access NumericsMatrix cf Numerics.i 169 %typemap(out) (std::shared_ptr<NumericsMatrix>) { 170 npy_intp dims[2]; 171 172 if (!$1) 173 { 174 Py_INCREF(Py_None); 175 $result = Py_None; 176 } 177 else 178 { 179 dims[0] = $1->size0; 180 dims[1] = $1->size1; 181 182 if ($1->matrix0) 183 { 184 PyObject *obj = PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, $1->matrix0); 185 PyArrayObject *array = (PyArrayObject*) obj; 186 if (!array || !require_fortran(array)) SWIG_fail; 187 $result = obj; 188 } 189 else if($1->matrix1) 190 { 191 // matrix is sparse : return opaque pointer 192 $result = SWIG_NewPointerObj(SWIG_as_voidptr($1->matrix1), $descriptor(SparseBlockStructuredMatrix *), 0); 193 } 194 else // failing silently does not seem a good option 195 { 196 Py_INCREF(Py_None); 197 $result = Py_None; 198 } 199 } 200 } 201 %import NumericsMatrix.h 202 %import CSparseMatrix.h 203 %import SparseBlockMatrix.h 204 205 // segfaults... 206 // we cannot share data struct 207 //%import Numerics.i 208 209 %include "SiconosConst.hpp" 210 211 %include "SiconosVisitables.hpp" 212 213 %import "SiconosVisitor.hpp" 214 %import "Question.hpp" 215 %import "TypeName.hpp" 216 217 %import "SiconosSerialization.hpp" 218 219 %import "SiconosProperties.hpp" 220 221 %include graph.i 222 223 %rename (ioMatrix_read) ioMatrix::read; 224 %rename (ioMatrix_write) ioMatrix::write; 225 %include "ioMatrix.hpp" 226 227 %include "SimulationTypeDef.hpp" 228 229 %import "boost/config.hpp" 230 %import "boost/graph/graph_utility.hpp" 231 232 %include "Tools.hpp" 233 234 %include "addons.hpp" 235 236 // fix : how to prevent swig to generate getter/setter for mpz_t ? 237 // on some distrib %import gmp.h is not sufficient as gmp-<arch>.h may be used 238 typedef struct 239 {} __mpz_struct; 240 typedef __mpz_struct mpz_t[1]; 241 242 %include "SiconosAlgebraTypeDef.hpp" 243 %include "SiconosAlgebra.hpp" 244 %include "RotationQuaternion.hpp" 245 246 %import "RelationNamespace.hpp"; 247 248 249 //namespace std { 250 251 %template (dspv) std::vector<std::pair<std::shared_ptr<DynamicalSystem>, 252 std::shared_ptr<DynamicalSystem> > >; 253 254 %template (dsiv) std::vector<std::pair<unsigned int, unsigned int > >; 255 256 257 %template (dsi) std::pair<unsigned int, unsigned int >; 258 259 %template (dsp) std::pair<std::shared_ptr<DynamicalSystem>, 260 std::shared_ptr<DynamicalSystem> >; 261 262 //BouncingBallNETS.py, attempt to reach DSlink as a vector... 263 //swig failure. 264 //%shared_ptr(VectorOfBlockVectors); 265 //%template (vectorOfBlockVectors) std::vector<std::shared_ptr<BlockVector> >; 266 /// 267 //} 268 269 270 %template(unsignedintv) std::shared_ptr<std::vector<unsigned int> >; 271 272 // not sufficient 273 %ignore Question<bool>; 274 %template (qbool) Question<bool>; 275 276 %ignore Question<unsigned int>; 277 %template (quint) Question<unsigned int>; 278 279 280 %ignore OSNSMatrix::updateSizeAndPositions; 281 282 %include std_vector.i 283 %template (MemoryContainer) std::vector<SiconosVector>; 284 285 // registered classes in KernelRegistration.i 286 287 %include KernelRegistration.i 288 %include pyRegister.i 289 KERNEL_REGISTRATION(); 290 291 %include pyInclude.i 292 293 KERNEL_REGISTRATION() 294 295 %fragment("StdSequenceTraits"); 296 297 %fragment("StdMapTraits"); 298 299 %inline 300 %{ 301 302 /* Note: without the PyCObject stuff the python 303 * wrapper fail on this, the numpy vector points on a deleted 304 * memory!*/ 305 getVector(SP::SiconosVector v)306 const SP::SiconosVector getVector(SP::SiconosVector v) 307 { 308 return v; 309 }; 310 getMatrix(SP::SiconosMatrix v)311 const SP::SiconosMatrix getMatrix(SP::SiconosMatrix v) 312 { 313 return v; 314 }; 315 getVector(SP::BlockVector v)316 const SP::BlockVector getVector(SP::BlockVector v) 317 { 318 return v; 319 }; 320 321 /* to make swig define SWIGTYPE_p_PyArrayObject */ getVector(PyArrayObject * v)322 const PyArrayObject* getVector(PyArrayObject* v) 323 { 324 return v; 325 }; 326 cast_NewtonImpactFrictionNSL(SP::NonSmoothLaw nslaw)327 SP::NewtonImpactFrictionNSL cast_NewtonImpactFrictionNSL(SP::NonSmoothLaw nslaw) 328 { 329 return std::dynamic_pointer_cast<NewtonImpactFrictionNSL>(nslaw); 330 } 331 cast_RelayNSL(SP::NonSmoothLaw nslaw)332 SP::RelayNSL cast_RelayNSL(SP::NonSmoothLaw nslaw) 333 { 334 return std::dynamic_pointer_cast<RelayNSL>(nslaw); 335 } 336 cast_NewtonImpactNSL(SP::NonSmoothLaw nslaw)337 SP::NewtonImpactNSL cast_NewtonImpactNSL(SP::NonSmoothLaw nslaw) 338 { 339 return std::dynamic_pointer_cast<NewtonImpactNSL>(nslaw); 340 } 341 cast_NewtonEulerDS(SP::DynamicalSystem ds)342 SP::NewtonEulerDS cast_NewtonEulerDS(SP::DynamicalSystem ds) 343 { 344 return std::dynamic_pointer_cast<NewtonEulerDS>(ds); 345 } 346 cast_LagrangianDS(SP::DynamicalSystem ds)347 SP::LagrangianDS cast_LagrangianDS(SP::DynamicalSystem ds) 348 { 349 return std::dynamic_pointer_cast<LagrangianDS>(ds); 350 } 351 cast_NewtonEuler1DR(SP::Relation r)352 SP::NewtonEuler1DR cast_NewtonEuler1DR(SP::Relation r) 353 { 354 return std::dynamic_pointer_cast<NewtonEuler1DR>(r); 355 } 356 cast_FrictionContact(SP::OneStepNSProblem osnpb)357 SP::FrictionContact cast_FrictionContact(SP::OneStepNSProblem osnpb) 358 { 359 return std::dynamic_pointer_cast<FrictionContact>(osnpb); 360 } 361 362 // Required to get size of a graph of interactions in python interp size_graph(const InteractionsGraph & index_set)363 size_t size_graph(const InteractionsGraph& index_set) 364 { 365 return index_set.size(); 366 } 367 368 %} 369