[593] | 1 | #ifndef MXDS_H |
---|
| 2 | #define MXDS_H |
---|
| 3 | |
---|
| 4 | |
---|
[565] | 5 | #include "../bdm/bdmerror.h" |
---|
[593] | 6 | #include "../bdm/base/datasources.h" |
---|
[529] | 7 | #include "mex_parser.h" |
---|
[477] | 8 | |
---|
[377] | 9 | namespace bdm { |
---|
| 10 | /*! |
---|
| 11 | * \brief Memory storage of off-line data column-wise |
---|
| 12 | |
---|
| 13 | The 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 | |
---|
| 15 | The data can be loaded from a file. |
---|
| 16 | */ |
---|
[593] | 17 | class mxArrayDS : public MemDS { |
---|
| 18 | public: |
---|
[477] | 19 | //!Default constructor |
---|
[593] | 20 | mxArrayDS ():MemDS() {}; |
---|
[377] | 21 | |
---|
[477] | 22 | /*! \brief Create memory data source from mxArray |
---|
| 23 | |
---|
| 24 | \code |
---|
| 25 | system={ |
---|
| 26 | type="mexDS"; |
---|
[593] | 27 | varname=""; // name of workspace variable |
---|
| 28 | row_rv = {class='RV',...} // definition of |
---|
[477] | 29 | }; |
---|
| 30 | \endcode |
---|
[377] | 31 | |
---|
[529] | 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"] ) ); |
---|
[593] | 37 | /* UI::get ( rowid, set, "rids" , UI::compulsory ); |
---|
[565] | 38 | bdm_assert_debug ( max ( rowid ) <= Data.rows(), "MemDS rowid is too high for given Dat." ); |
---|
[529] | 39 | |
---|
| 40 | UI::get ( delays, set, "tds", UI::compulsory ); |
---|
| 41 | time = max ( delays ); |
---|
[565] | 42 | bdm_assert_debug ( time < Data.cols(), "MemDS delays are too high." ); |
---|
[593] | 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 ); |
---|
[529] | 50 | RV ru = RV(); |
---|
[593] | 51 | if (r){ |
---|
[660] | 52 | set_drv ( *r, ru ); |
---|
[593] | 53 | } else { |
---|
| 54 | RV def((const char*)set["varname"],Data.rows()); |
---|
[660] | 55 | set_drv(def, ru); |
---|
[593] | 56 | } |
---|
[529] | 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
| 61 | }; |
---|
| 62 | |
---|
[593] | 63 | UIREGISTER ( mxArrayDS ); |
---|
| 64 | SHAREDPTR ( mxArrayDS ); |
---|
[377] | 65 | |
---|
[596] | 66 | /*! |
---|
| 67 | * \brief Matlab wrapper for DS mapping functions step() to a matlab function |
---|
| 68 | |
---|
| 69 | The identifier of matlab function is stored in attribute \c name. |
---|
| 70 | This 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 | */ |
---|
| 74 | class mexDS : public DS { |
---|
| 75 | protected: |
---|
| 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; |
---|
| 84 | public: |
---|
| 85 | //!Default constructor |
---|
| 86 | mexDS ():DS() {}; |
---|
| 87 | |
---|
| 88 | /*! \brief Create memory data source from mxArray |
---|
| 89 | |
---|
| 90 | \code |
---|
| 91 | system={ |
---|
| 92 | type="mexDS"; |
---|
| 93 | step_name=""; // name of function to call |
---|
| 94 | input_name=""; // name of workspace variable where inputs are written |
---|
| 95 | rv_out = {class='RV',...} // identification of outputs |
---|
| 96 | rv_in = {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> ry = UI::build<RV> ( set, "rv_out", UI::compulsory ); |
---|
| 108 | shared_ptr<RV> ru = UI::build<RV> ( set, "rv_in", UI::compulsory); |
---|
| 109 | |
---|
| 110 | dtsize=ry->_dsize(); |
---|
| 111 | utsize=ru->_dsize(); |
---|
| 112 | |
---|
| 113 | set_drv(*ry, *ru); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void step() { |
---|
| 117 | mxArray* tmp; |
---|
| 118 | mxArray* tmp2; |
---|
| 119 | // write inputs to variable input_name |
---|
| 120 | mxArray* mxinp= mexGetVariable ( "global", input_name.c_str()) ; |
---|
| 121 | vec2mxArray(ut, mxinp); |
---|
| 122 | // call function step_name |
---|
| 123 | mexCallMATLAB(1, &tmp, 0, (mxArray **) &tmp2, step_name.c_str()); |
---|
| 124 | // save its results |
---|
| 125 | dt=mxArray2vec(tmp); |
---|
| 126 | } |
---|
[660] | 127 | void write(const vec &ut0){ ut=ut0;} |
---|
[596] | 128 | void getdata(vec &dt_out){dt_out = dt; } |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | // TODO dodelat void to_setting( Setting &set ) const; |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | UIREGISTER ( mexDS ); |
---|
| 135 | SHAREDPTR ( mexDS ); |
---|
| 136 | |
---|
[529] | 137 | } |
---|
[593] | 138 | #endif //MXDS_H |
---|