1 /* Siconos is a program dedicated to modeling, simulation and control
2  * of non smooth dynamical systems.
3  *
4  * Copyright 2021 INRIA.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17 */
18 
19 #ifndef SubPluggedObject_H
20 #define SubPluggedObject_H
21 
22 #include "PluggedObject.hpp"
23 
24 /*! \file SubPluggedObject.hpp
25   \brief utilities for plugin definition to compute extract a column from a matrix computed by an user-defined function.
26 */
27 
28 typedef void (*matrixPlugin)(double, unsigned int, unsigned int, double*, unsigned int, double*);
29 
30 class SubPluggedObject : public PluggedObject
31 {
32 private:
33   /** serialization hooks
34   */
35   ACCEPT_SERIALIZATION(SubPluggedObject);
36   /** function pointer to the parent PluggedObject*/
37   void * _parentfPtr;
38   /** Matrix to temporary storage of the output */
39   SP::SiconosMatrix _tmpMat;
40   /** Column index */
41   unsigned int _indx;
42   /** Number of columns */
43   unsigned int _p;
44 
45 public:
46 
47   /** Default Constructor
48    */
SubPluggedObject()49   SubPluggedObject(): PluggedObject(), _parentfPtr(nullptr), _indx(0), _p(0)  { };
50 
51   /** Constructor with the plugin name
52    * \param PO a PluggedObject
53    * \param n the number of rows of the matrix
54    * \param p the number of column of the matrix
55    * \param indx the column index (optional)
56    */
SubPluggedObject(const PluggedObject & PO,const unsigned int n,const unsigned int p,const unsigned int indx=0)57   SubPluggedObject(const PluggedObject& PO, const unsigned int n, const unsigned int p, const unsigned int indx = 0):  _indx(indx), _p(p)
58   {
59     _pluginName = "Sub" + PO.pluginName();
60     _tmpMat.reset(new SimpleMatrix(n, p));
61 #if (__GNUG__ && !( __clang__ || __INTEL_COMPILER || __APPLE__ ) && (((__GNUC__ > 5) && (__GNUC_MINOR__ > 0))))
62 #pragma GCC diagnostic ignored "-Wpmf-conversions"
63     fPtr = (void *)&SubPluggedObject::computeAndExtract;
64     _parentfPtr = PO.fPtr;
65 #else
66     THROW_EXCEPTION("SubPluggedObject must be compiled with GCC !");
67 #endif
68   };
69 
70   /** Copy constructor
71    * \param SPO a PluggedObject we are going to copy
72   */
SubPluggedObject(const SubPluggedObject & SPO)73   SubPluggedObject(const SubPluggedObject& SPO): PluggedObject(SPO), _indx(SPO.getIndex()), _p(SPO.getp())
74   {
75     _parentfPtr = SPO.getParentfPtr();
76     _tmpMat.reset(new SimpleMatrix(SPO.getTmpMat()));
77   }
78 
79   /** destructor
80    */
~SubPluggedObject()81   ~SubPluggedObject() {};
82 
83   /** Intermediate function to compute the column of a plugged matrix
84    * \param time current time
85    * \param n the length of the vector
86    * \param M the vector used for storage
87    * \param sizez the size of z
88    * \param z a vector used as a parameter
89    */
computeAndExtract(double time,unsigned int n,double * M,unsigned int sizez,double * z)90   void computeAndExtract(double time, unsigned int n, double* M, unsigned int sizez, double* z)
91   {
92     ((matrixPlugin)_parentfPtr)(time, n, _p, &(*_tmpMat)(0, 0), sizez, z);
93     for (unsigned int i = 0; i < n; i++)
94     {
95       M[i] = (*_tmpMat)(i, _indx);
96     }
97   };
98 
99   /* Set column index
100    * \param newIndx the new column index
101    */
setIndex(unsigned int newIndx)102   inline void setIndex(unsigned int newIndx)
103   {
104     _indx = newIndx;
105   };
106 
107   /* Get column index
108    * \return the column index
109    */
getIndex() const110   inline unsigned int getIndex() const
111   {
112     return _indx;
113   };
114 
115   /** Get the number of row
116    * \return the number of row
117    */
getp() const118   inline unsigned int getp() const
119   {
120     return _p;
121   };
122 
123   /** Get the user defined plugin
124    * \return the user defined plugin
125    */
getParentfPtr() const126   inline void * getParentfPtr() const
127   {
128     return _parentfPtr;
129   };
130 
131   /** Get the user defined plugin
132    * \return the user defined plugin
133    */
getTmpMat() const134   inline const SiconosMatrix& getTmpMat() const
135   {
136     return *_tmpMat;
137   };
138 
139 };
140 #endif
141