Index: mttroot/mtt/bin/mtt ================================================================== --- mttroot/mtt/bin/mtt +++ mttroot/mtt/bin/mtt @@ -15,10 +15,13 @@ ############################################################### ## Version control history ############################################################### ## $Header$ ## $Log$ +## Revision 1.337 2002/05/08 14:51:03 geraint +## Moved matlab/octave data type conversion functions to a separate file. +## ## Revision 1.336 2002/05/07 23:50:34 geraint ## Preliminary support for Matlab dynamically linked shared objects: ## invoke with: mtt -cc sys rep mexglx ## ode2odes support is not yet included. ## @@ -2793,10 +2796,18 @@ $1_ode2odes.oct: $1_ode2odes.cc $1_ode2odes_common.oct $1_ode2odes_${integration_method}.oct $1_ode2odes_${algebraic_solver}.o touch $1_ode2odes.m echo Creating $1_ode2odes.oct $MKOCTFILE -DCODEGENTARGET=OCTAVEDLD $1_ode2odes.cc mtt_${algebraic_solver}.cc mtt_Solver.cc mtt_AlgebraicSolver.cc + +$1_ode2odes.mexglx: $1_ode2odes.cc $1_ode2odes_common.o $1_ode2odes_${integration_method}.o $1_ode2odes_${algebraic_solver}.o + touch $1_ode2odes.m + echo Creating $1_ode2odes.mexglx + ${MTT_CXX} -DCODEGENTARGET=MATLABMEX -o $1_ode2odes.mexglx $1_ode2odes.cc \ + $1_ode2odes_common.o $1_ode2odes_${integration_method}.o $1_ode2odes_${algebraic_solver}.o \ + ${MTT_MATLAB_FLAGS} ${MTT_CXXINCS} ${MTT_CXXLIBS} ${MTT_CXXFLAGS} \ + ${MTT_LIB}/cc/mtt_kpathsea.cc ${MTT_LIB}/cc/mtt_matlab_octave.cc $1_ode2odes.cc: $1_def.r $1_sympars.txt\ $1_ode2odes_common.m $1_ode2odes_common.cc\ $1_ode2odes_${integration_method}.m $1_ode2odes_${integration_method}.cc\ mtt_Solver.cc mtt_AlgebraicSolver.cc mtt_${algebraic_solver}.cc mtt_${algebraic_solver}.hh Index: mttroot/mtt/bin/trans/make_ode2odes ================================================================== --- mttroot/mtt/bin/trans/make_ode2odes +++ mttroot/mtt/bin/trans/make_ode2odes @@ -7,10 +7,13 @@ ############################################################### ## Version control history ############################################################### ## $Id$ ## $Log$ +## Revision 1.76 2002/05/08 14:14:55 geraint +## Tidied up ode2odes code - reduced interweaving of STANDALONE/OCTAVEDLD sections +## ## Revision 1.75 2002/05/07 13:48:42 geraint ## Improved clarity of code generated for -cc and -oct (except ode2odes). ## Octave DEFUN_DLDs now call (rather than replace) their .cc equivalents. ## ## Revision 1.74 2002/05/01 17:30:56 geraint @@ -440,10 +443,11 @@ cat < $filename // Code generation directives #define STANDALONE 0 #define OCTAVEDLD 1 +#define MATLABMEX 2 #if (! defined (CODEGENTARGET)) #define CODEGENTARGET STANDALONE #endif // (! defined (CODEGENTARGET)) #include @@ -458,11 +462,11 @@ #include "${sys}_sympar.h" #include "mtt_${algebraic_solver}.hh" #include -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) extern ColumnVector ${sys}_ae ( ColumnVector &x, ColumnVector &u, const double &t, @@ -555,29 +559,29 @@ EOF ;; esac cat <> $filename -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) ColumnVector mtt_ae (ColumnVector &x, ColumnVector &u, const double &t, ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_ae(x,u,t,par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_ae", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_input (ColumnVector &x, ColumnVector &y, @@ -585,21 +589,21 @@ ColumnVector &par) { static MTT::${algebraic_solver} ae(MTTNPAR,MTTNU,MTTNX,MTTNY,MTTNYZ); static ColumnVector u (MTTNU); -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) u = ${sys}_input (x, y, t, par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (y); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_input", args, 1); u = f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) if (MTTNYZ == 0) { return u; } else @@ -612,96 +616,96 @@ mtt_logic (ColumnVector &x, ColumnVector &u, const double &t, ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_logic (x, u, t, par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_logic", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_numpar (void) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_numpar (); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; f = feval ("${sys}_numpar", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_simpar (void) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_simpar (); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; f = feval ("${sys}_simpar", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_state (ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_state (par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (par); f = feval ("${sys}_state", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_rate (ColumnVector &x, ColumnVector &u, const double &t, ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_${ode} (x, u, t, par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_${ode}", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_output (ColumnVector &x, ColumnVector &u, const double &t, ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_${odeo} (x, u, t, par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_${odeo}", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } EOF case "$method" in "implicit") @@ -713,11 +717,11 @@ ColumnVector &AAx, const double &ddt, const int &nx, const ColumnVector &open_switches) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return Fmtt_implicit (x, dx, AA, AAx, ddt, nx, open_switches); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (dx); @@ -726,49 +730,49 @@ args (4) = octave_value (ddt); args (5) = octave_value ((double)nx); args (6) = octave_value (open_switches); f = feval ("mtt_implicit", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline Matrix mtt_smxa (ColumnVector &x, ColumnVector &u, const double &t, ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_smxa (x, u, t, par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_smxa", args, 1); return f(0).matrix_value (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } inline ColumnVector mtt_smxax (ColumnVector &x, ColumnVector &u, const double &t, ColumnVector &par) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return ${sys}_smxax (x, u, t, par); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); args (2) = octave_value (t); args (3) = octave_value (par); f = feval ("${sys}_smxax", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } EOF ;; "dassl") @@ -782,11 +786,11 @@ const double &ddt, const int &nx, const int &nyz, const ColumnVector &open_switches) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return Fmtt_dassl (x, u, t, par, dx, ddt, nx, nyz, open_switches); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (u); @@ -797,14 +801,14 @@ args (6) = octave_value (static_cast (nx)); args (7) = octave_value (static_cast (nyz)); args (8) = octave_value (open_switches); f = feval ("mtt_dassl", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) ColumnVector mtt_residual (const ColumnVector &X, const ColumnVector &DX, double t, int &ires) @@ -819,11 +823,11 @@ X = args(0).${vector_value} (); DX = args(1).${vector_value} (); t = args(2).double_value (); ires = static_cast(args(3).double_value ()); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) static ColumnVector residual (MTTNX+MTTNYZ); static ColumnVector U (MTTNU+MTTNYZ); static ColumnVector u (MTTNU); static ColumnVector y (MTTNY,0.0); @@ -838,34 +842,34 @@ x = X.extract (0,MTTNX-1); if (MTTNYZ > 0) ui = X.extract (MTTNX,MTTNX+MTTNYZ-1); -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) par = ${sys}_numpar(); u = ${sys}_input(x,y,t,par); #elif (CODEGENTARGET == OCTAVEDLD) par = feval ("${sys}_numpar", new_args, 1)(0).${vector_value} (); new_args(0) = octave_value (x); new_args(1) = octave_value (u); new_args(2) = octave_value (t); new_args(3) = octave_value (par); u = feval ("${sys}_input", new_args, 1)(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) U.insert (u,0); if (MTTNYZ > 0) U.insert (ui,MTTNU); -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) dx = ${sys}_${ode} (x,U,t,par); yz = ${sys}_ae (x,U,t,par); #elif (CODEGENTARGET == OCTAVEDLD) new_args(1) = octave_value (U); dx = feval ("${sys}_${ode}", new_args, 1)(0).${vector_value} (); yz = feval ("${sys}_ae", new_args, 1)(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) for (register int i = 0; i < MTTNX; i++) residual (i) = dx(i) - DX(i); if (MTTNYZ > 0) @@ -887,15 +891,15 @@ step = t; for (register int i = MTTNX; i < MTTNX+MTTNYZ; i++) residual(i) += X(i) - DX(i)*step; } -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return residual; #elif (CODEGENTARGET == OCTAVEDLD) return octave_value (residual); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } EOF ;; "euler" | "rk4" | *) @@ -905,11 +909,11 @@ const ColumnVector &dx, const double &ddt, const int &nx, const ColumnVector &open_switches) { -#if (CODEGENTARGET == STANDALONE) +#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) return Fmtt_euler (x, dx, ddt, nx, open_switches); #elif (CODEGENTARGET == OCTAVEDLD) static octave_value_list args, f; args (0) = octave_value (x); args (1) = octave_value (dx); @@ -916,11 +920,11 @@ args (2) = octave_value (ddt); args (3) = octave_value ((double)nx); args (4) = octave_value (open_switches); f = feval ("mtt_euler", args, 1); return f(0).${vector_value} (); -#endif // (CODEGENTARGET == STANDALONE) +#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX)) } EOF ;; esac @@ -983,11 +987,11 @@ << "# type: matrix" << std::endl << "# rows: " << data.rows () << std::endl << "# columns: " << data.columns () << std::endl << data; file.flush (); -#elif (CODEGENTARGET == OCTAVEDLD) +#elif ((CODEGENTARGET == OCTAVEDLD) || (CODEGENTARGET == MATLABMEX)) set_global_value ("MTT_data", data); #endif // (CODEGENTARGET == STANDALONE) row = 0; fcputime.close(); } @@ -1192,10 +1196,56 @@ ${sys}_ode2odes (state0, numpar, simpar); retval = octave_value (get_global_value ("MTT_data")); return (retval); +} +#elif (CODEGENTARGET == MATLABMEX) +#include + +extern "C" { + void + mexFunction (int nlhs, mxArray *plhs[], + int nrhs, const mxArray *prhs[]) + { + static ColumnVector state0 (MTTNX); + static ColumnVector numpar (MTTNPAR); + static ColumnVector simpar (8); + + initialize_symbol_tables (); + + switch (nrhs) + { + case 3: + simpar = mtt_ColumnVector (prhs[2]); + numpar = mtt_ColumnVector (prhs[1]); + state0 = mtt_ColumnVector (prhs[0]); + break; + case 2: + simpar = mtt_simpar (); + numpar = mtt_ColumnVector (prhs[1]); + state0 = mtt_ColumnVector (prhs[0]); + break; + case 1: + simpar = mtt_simpar (); + numpar = mtt_numpar (); + state0 = mtt_ColumnVector (prhs[0]); + break; + case 0: + simpar = mtt_simpar (); + numpar = mtt_numpar (); + state0 = mtt_state (numpar); + break; + default: + std::cerr << "usage: ${sys}_ode2odes (x par simpar)" << std::endl; + return; + } + + ${sys}_ode2odes (state0, numpar, simpar); + + plhs[0] = mtt_mxArray (get_global_value ("MTT_data").matrix_value ()); + } } #endif // (CODEGENTARGET == STANDALONE) EOF }