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

Revision 896, 4.6 kB (checked in by mido, 14 years ago)

cleanup of MemDS and its descendants
bdmtoolbox/CMakeLists.txt slightly changed to avoid unnecessary MEX condition
"indeces" replaced by "indices"

  • 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                rowid = linspace ( 0, Data.rows() - 1 );
46                dtsize = rowid.length();
47                utsize = 0;
48
49                shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
50                RV ru = RV();
51                if ( r ) {
52                        set_drv ( *r, ru );
53                } else {
54                        RV def ( ( const char* ) set["varname"], Data.rows() );
55                        set_drv ( def, ru );
56                }
57        }
58
59
60        // TODO dodelat void to_setting( Setting &set ) const;
61};
62
63UIREGISTER ( mxArrayDS );
64SHAREDPTR ( mxArrayDS );
65
66/*!
67* \brief Matlab wrapper for DS mapping functions step() to a matlab function
68
69The identifier of matlab function is stored in attribute \c name.
70This identifier defines:
71\li function to call to do a step(): name_step.m
72\li workspace variable to write input to: name_input
73*/
74class mexDS : public DS {
75protected:
76        //! identifier of matlab function
77        string step_name;
78        //! identifier of matlab input variabel
79        string input_name;
80        //! cache of results from name_step
81        vec dt;
82        //! cache of inputs
83        vec ut;
84public:
85        //!Default constructor
86        mexDS () : DS() {};
87
88        /*! \brief Data source computed by matlab function
89
90        \code
91        system={
92                class="mexDS";
93                step_name="";              // name of function to call
94                input_name="";             // name of workspace variable where inputs are written
95                drv = {class='RV',...}  // identification of outputs
96                urv = {class='RV',...}   // identification of inputs
97                };
98        \endcode
99
100        MemDS with the above fields will be created;
101
102        */
103        void from_setting ( const Setting &set ) {
104                UI::get ( step_name, set, "step_name", UI::compulsory );
105                UI::get ( input_name, set, "input_name", UI::compulsory );
106
107                shared_ptr<RV> rd = UI::build<RV> ( set, "drv", UI::compulsory );
108                shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory );
109
110                dtsize = rd->_dsize();
111                utsize = ru->_dsize();
112
113                set_drv ( *rd, *ru );
114                validate();
115        }
116
117        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID;
118
119        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
120
121
122        void step() {
123                mxArray* tmp;
124                mxArray* dummy = NULL;
125                // write inputs to variable input_name
126                mxArray* mxinp = mexGetVariable ( "global", input_name.c_str() ) ;
127                if ( mxinp ) {
128                        if ( ( int ) mxGetM ( mxinp ) != utsize || ( int ) mxGetN ( mxinp ) != utsize ) {
129                                // mxinp is invalid - create new one
130                                mxDestroyArray ( mxinp );
131                                mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL );
132                        }
133
134                } else {
135                        mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL );
136                }
137                vec2mxArray ( ut, mxinp );
138                mexPutVariable ( "global", input_name.c_str(), mxinp );
139                // call function step_name
140                mexCallMATLAB ( 1, &tmp, 0, ( mxArray ** ) &dummy, step_name.c_str() );
141                // save its results
142                bdm_assert_debug ( ( int ) mxGetM ( tmp ) == dtsize || ( int ) mxGetN ( tmp ) == dtsize, "mexDS.step() expected return vector of length " + num2str ( dtsize ) +
143                                   "got vector " + num2str ( ( int ) mxGetM ( tmp ) ) + "x" + num2str ( ( int ) mxGetN ( tmp ) ) );
144                //write  y
145                dt.set_subvector ( 0, mxArray2vec ( tmp ) );
146                //write u
147        }
148        void write ( const vec &ut0 ) {
149                bdm_assert_debug ( ut0.length() == ut.length(), "mexDS: Incompatible input vector" );
150                ut = ut0;
151        }
152        void getdata ( vec &dt_out ) const {
153                bdm_assert_debug ( dt_out.length() == dt.length(), "mexDS: Incompatible output vector" );
154                dt_out = dt;
155        }
156
157        void validate() {
158                dt = zeros ( dtsize );
159                ut = zeros ( utsize );
160        }
161
162        // TODO dodelat void to_setting( Setting &set ) const;
163};
164
165UIREGISTER ( mexDS );
166SHAREDPTR ( mexDS );
167
168}
169#endif //MXDS_H
Note: See TracBrowser for help on using the browser.