root/library/bdm/mex/mex_datasource.h @ 904

Revision 904, 4.5 kB (checked in by smidl, 14 years ago)

fixes

  • Property svn:eol-style set to native
Line 
1#ifndef MXDS_H
2#define MXDS_H
3
4
5#include "../bdm/bdmerror.h"
6#include "../bdm/base/datasources.h"
7#include "mex_parser.h"
8
9namespace bdm {
10/*!
11* \brief Memory storage of off-line data column-wise
12
13The data are stored in an internal matrix \c Data . Each column of Data corresponds to one discrete time observation \f$t\f$. Access to this matrix is via indices \c rowid and \c delays.
14
15The data can be loaded from a file.
16*/
17class mxArrayDS : public MemDS {
18public:
19        //!Default constructor
20        mxArrayDS () : MemDS() {};
21
22        /*! \brief Create memory data source from mxArray
23
24        \code
25        system={
26                type="mexDS";
27                varname="";                // name of workspace variable
28                row_rv = {class='RV',...}  // definition of
29                };
30        \endcode
31
32        MemDS with the above fields will be created;
33
34        */
35        void from_setting ( const Setting &set ) {
36                Data = mxArray2mat ( mexGetVariable ( "base", set["varname"] ) );
37                /*              UI::get ( rowid, set, "rids" , UI::compulsory );
38                                bdm_assert_debug ( max ( rowid ) <= Data.rows(), "MemDS rowid is too high for given Dat." );
39
40                                UI::get ( delays, set, "tds", UI::compulsory );
41                                time = max ( delays );
42                                bdm_assert_debug ( time < Data.cols(), "MemDS delays are too high." );
43                */
44                //set MemDS
45                dtsize = Data.rows();
46                utsize = 0;
47
48                shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
49                RV ru = RV();
50                if ( r ) {
51                        set_drv ( *r, ru );
52                } else {
53                        RV def ( ( const char* ) set["varname"], Data.rows() );
54                        set_drv ( def, ru );
55                }
56        }
57
58
59        // TODO dodelat void to_setting( Setting &set ) const;
60};
61
62UIREGISTER ( mxArrayDS );
63SHAREDPTR ( mxArrayDS );
64
65/*!
66* \brief Matlab wrapper for DS mapping functions step() to a matlab function
67
68The identifier of matlab function is stored in attribute \c name.
69This identifier defines:
70\li function to call to do a step(): name_step.m
71\li workspace variable to write input to: name_input
72*/
73class mexDS : public DS {
74protected:
75        //! identifier of matlab function
76        string step_name;
77        //! identifier of matlab input variabel
78        string input_name;
79        //! cache of results from name_step
80        vec dt;
81        //! cache of inputs
82        vec ut;
83public:
84        //!Default constructor
85        mexDS () : DS() {};
86
87        /*! \brief Data source computed by matlab function
88
89        \code
90        system={
91                class="mexDS";
92                step_name="";              // name of function to call
93                input_name="";             // name of workspace variable where inputs are written
94                drv = {class='RV',...}  // identification of outputs
95                urv = {class='RV',...}   // identification of inputs
96                };
97        \endcode
98
99        MemDS with the above fields will be created;
100
101        */
102        void from_setting ( const Setting &set ) {
103                UI::get ( step_name, set, "step_name", UI::compulsory );
104                UI::get ( input_name, set, "input_name", UI::compulsory );
105
106                shared_ptr<RV> rd = UI::build<RV> ( set, "drv", UI::compulsory );
107                shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory );
108
109                dtsize = rd->_dsize();
110                utsize = ru->_dsize();
111
112                set_drv ( *rd, *ru );
113                validate();
114        }
115
116        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
117
118        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
119
120
121        void step() {
122                mxArray* tmp;
123                mxArray* dummy = NULL;
124                // write inputs to variable input_name
125                mxArray* mxinp = mexGetVariable ( "global", input_name.c_str() ) ;
126                if ( mxinp ) {
127                        if ( ( int ) mxGetM ( mxinp ) != utsize || ( int ) mxGetN ( mxinp ) != utsize ) {
128                                // mxinp is invalid - create new one
129                                mxDestroyArray ( mxinp );
130                                mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL );
131                        }
132
133                } else {
134                        mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL );
135                }
136                vec2mxArray ( ut, mxinp );
137                mexPutVariable ( "global", input_name.c_str(), mxinp );
138                // call function step_name
139                mexCallMATLAB ( 1, &tmp, 0, ( mxArray ** ) &dummy, step_name.c_str() );
140                // save its results
141                bdm_assert_debug ( ( int ) mxGetM ( tmp ) == dtsize || ( int ) mxGetN ( tmp ) == dtsize, "mexDS.step() expected return vector of length " + num2str ( dtsize ) +
142                                   "got vector " + num2str ( ( int ) mxGetM ( tmp ) ) + "x" + num2str ( ( int ) mxGetN ( tmp ) ) );
143                //write  y
144                dt.set_subvector ( 0, mxArray2vec ( tmp ) );
145                //write u
146        }
147        void write ( const vec &ut0 ) {
148                bdm_assert_debug ( ut0.length() == ut.length(), "mexDS: Incompatible input vector" );
149                ut = ut0;
150        }
151        void getdata ( vec &dt_out ) const {
152                bdm_assert_debug ( dt_out.length() == dt.length(), "mexDS: Incompatible output vector" );
153                dt_out = dt;
154        }
155
156        void validate() {
157                dt = zeros ( dtsize );
158                ut = zeros ( utsize );
159        }
160
161        // TODO dodelat void to_setting( Setting &set ) const;
162};
163
164UIREGISTER ( mexDS );
165SHAREDPTR ( mexDS );
166
167}
168#endif //MXDS_H
Note: See TracBrowser for help on using the browser.