Salome HOME
Fix of reference connectivity of faces of PENTA15 cell from Roberto Da Via
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingRemapperCommon.i
1 // Copyright (C) 2017-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #define MEDCOUPLING_EXPORT
21 #define INTERPKERNEL_EXPORT
22 #define MEDCOUPLINGREMAPPER_EXPORT
23
24 %newobject MEDCoupling::MEDCouplingRemapper::transferField;
25 %newobject MEDCoupling::MEDCouplingRemapper::reverseTransferField;
26
27 %{
28 #include "MEDCouplingRemapper.hxx"
29 #include <memory>
30 %}
31
32 %include "InterpolationOptions.hxx"
33
34 namespace MEDCoupling
35 {
36   typedef enum
37     {
38       IK_ONLY_PREFERED = 0,
39       NOT_IK_ONLY_PREFERED = 1,
40       IK_ONLY_FORCED = 2,
41       NOT_IK_ONLY_FORCED =3
42     } InterpolationMatrixPolicy;
43
44   class MEDCouplingRemapper : public TimeLabel, public INTERP_KERNEL::InterpolationOptions
45     {
46     private:
47       void updateTime() const;
48     public:
49       MEDCouplingRemapper();
50       ~MEDCouplingRemapper();
51       int prepare(const MEDCouplingMesh *srcMesh, const MEDCouplingMesh *targetMesh, const std::string& method);
52       int prepareEx(const MEDCouplingFieldTemplate *src, const MEDCouplingFieldTemplate *target);
53       void transfer(const MEDCouplingFieldDouble *srcField, MEDCouplingFieldDouble *targetField, double dftValue);
54       void partialTransfer(const MEDCouplingFieldDouble *srcField, MEDCouplingFieldDouble *targetField);
55       void reverseTransfer(MEDCouplingFieldDouble *srcField, const MEDCouplingFieldDouble *targetField, double dftValue);
56       MEDCouplingFieldDouble *transferField(const MEDCouplingFieldDouble *srcField, double dftValue);
57       MEDCouplingFieldDouble *reverseTransferField(const MEDCouplingFieldDouble *targetField, double dftValue);
58       bool setOptionInt(const std::string& key, int value);
59       bool setOptionDouble(const std::string& key, double value);
60       bool setOptionString(const std::string& key, const std::string& value);
61       int getInterpolationMatrixPolicy() const;
62       void setInterpolationMatrixPolicy(int newInterpMatPol);
63       //
64       int nullifiedTinyCoeffInCrudeMatrixAbs(double maxValAbs);
65       int nullifiedTinyCoeffInCrudeMatrix(double scaleFactor);
66       double getMaxValueInCrudeMatrix() const;
67       int getNumberOfColsOfMatrix() const;
68       static std::string BuildMethodFrom(const std::string& meth1, const std::string& meth2);
69       %extend
70          {
71            PyObject *getCrudeMatrix() const
72            {
73              const std::vector<std::map<mcIdType,double> >& m=self->getCrudeMatrix();
74              std::size_t sz=m.size();
75              PyObject *ret=PyList_New(sz);
76              for(std::size_t i=0;i<sz;i++)
77                {
78                  const std::map<mcIdType,double>& row=m[i];
79                  PyObject *ret0=PyDict_New();
80                  for(std::map<mcIdType,double>::const_iterator it=row.begin();it!=row.end();it++)
81                    {
82                      std::unique_ptr<PyObject,std::function<void(PyObject*)>> k(PyInt_FromLong((*it).first),[](PyObject *obj) { Py_XDECREF(obj); } ),v(PyFloat_FromDouble((*it).second),[](PyObject *obj) { Py_XDECREF(obj); } );
83                      PyDict_SetItem(ret0,k.get(),v.get());
84                    }
85                  PyList_SetItem(ret,i,ret0);
86                }
87              return ret;
88            }
89 #if defined(WITH_NUMPY) && defined(WITH_SCIPY)
90            PyObject *getCrudeCSRMatrix() const
91            {
92              return ToCSRMatrix(self->getCrudeMatrix(),self->getNumberOfColsOfMatrix());
93            }
94            static PyObject *ToCSRMatrix(PyObject *m, mcIdType nbOfCols)
95            {
96               std::vector<std::map<mcIdType,double> > mCpp;
97               convertToVectMapIntDouble(m,mCpp);
98               return ToCSRMatrix(mCpp,nbOfCols);
99            }
100 #endif
101            void setCrudeMatrix(const MEDCouplingMesh *srcMesh, const MEDCouplingMesh *targetMesh, const std::string& method, PyObject *m)
102            {
103              std::vector<std::map<mcIdType,double> > mCpp;
104              if(isCSRMatrix(m))
105                {
106 #if defined(WITH_NUMPY) && defined(WITH_SCIPY)
107                  PyObject *indptr(PyObject_GetAttrString(m,"indptr"));
108                  PyObject *indices(PyObject_GetAttrString(m,"indices"));
109                  PyObject *data(PyObject_GetAttrString(m,"data"));
110                  MCAuto<DataArrayInt32> indptrPtr, indicesPtr;
111                  // csr_matrix.indptr and csr_matrix.indices are always dtype==int32
112 // #if defined(MEDCOUPLING_USE_64BIT_IDS)
113 //                  indptrPtr = MEDCoupling_DataArrayInt64_New__SWIG_1(indptr,NULL,NULL);
114 //                  indicesPtr = MEDCoupling_DataArrayInt64_New__SWIG_1(indices,NULL,NULL);
115 // #else
116                  indptrPtr = MEDCoupling_DataArrayInt32_New__SWIG_1(indptr,NULL,NULL);
117                  indicesPtr = MEDCoupling_DataArrayInt32_New__SWIG_1(indices,NULL,NULL);
118 //#endif
119                  MCAuto<DataArrayDouble> dataPtr(MEDCoupling_DataArrayDouble_New__SWIG_1(data,NULL,NULL));
120                  convertCSR_MCDataToVectMapIntDouble(indptrPtr,indicesPtr,dataPtr,mCpp);
121                  Py_XDECREF(data); Py_XDECREF(indptr); Py_XDECREF(indices);
122 #else
123                  throw INTERP_KERNEL::Exception("pywrap of MEDCouplingRemapper::setCrudeMatrix : unexpected situation regarding numpy/scipy !");
124 #endif
125                }
126              else
127                convertToVectMapIntDouble(m,mCpp);
128              self->setCrudeMatrix(srcMesh,targetMesh,method,mCpp);
129            }
130
131            void setCrudeMatrixEx(const MEDCouplingFieldTemplate *src, const MEDCouplingFieldTemplate *target, PyObject *m)
132            {
133              std::vector<std::map<mcIdType,double> > mCpp;
134              convertToVectMapIntDouble(m,mCpp);
135              self->setCrudeMatrixEx(src,target,mCpp);
136            }
137          }
138     };
139 }
140