Index: mttroot/mtt/bin/mtt ================================================================== --- mttroot/mtt/bin/mtt +++ mttroot/mtt/bin/mtt @@ -15,10 +15,14 @@ ############################################################### ## Version control history ############################################################### ## $Header$ ## $Log$ +## Revision 1.344 2002/05/11 01:14:17 geraint +## Fix for [ 553218 ] simpar.oct and simpar.m different. +## Translation added between ColumnVector in base .cc and Octave_map in .oct. +## ## Revision 1.343 2002/05/10 14:07:16 geraint ## Preserve .cc files. ## ## Revision 1.342 2002/05/10 13:24:58 geraint ## Added initial support for building Simulink S-functions. @@ -2158,11 +2162,11 @@ ## .cc files .PRECIOUS: %.cc # Don't let mtt delete them .PRECIOUS: $1_%.cc # Don't let mtt delete them $1_%.cc: $1_%.m - mtt_m2cc.sh $1 \$* cat + mtt_m2cc.sh $1 \$* cc cat mtt_%.cc: ${MTT_LIB}/cc/mtt_%.cc cp ${MTT_LIB}/cc/mtt_\$*.cc mtt_\$*.cc mtt_%.hh: ${MTT_LIB}/cc/mtt_%.hh Index: mttroot/mtt/bin/trans/mtt_header ================================================================== --- mttroot/mtt/bin/trans/mtt_header +++ mttroot/mtt/bin/trans/mtt_header @@ -10,10 +10,14 @@ ############################################################### ## Version control history ############################################################### ## $Id$ ## $Log$ +## Revision 1.49 2002/05/11 01:14:17 geraint +## Fix for [ 553218 ] simpar.oct and simpar.m different. +## Translation added between ColumnVector in base .cc and Octave_map in .oct. +## ## Revision 1.48 2002/05/08 14:51:03 geraint ## Moved matlab/octave data type conversion functions to a separate file. ## ## Revision 1.47 2002/05/07 23:50:34 geraint ## Preliminary support for Matlab dynamically linked shared objects: @@ -454,17 +458,31 @@ declareswitches=no ;; oct) modeline="// -*-c++-*- Put Emacs into c++-mode"; Lc='//'; + Lb='('; + Rb=')'; oct_header=yes; constant_declaration="const double " var_declaration="double " minusone="-1" map="_map" declaredummies=yes ;; + sfun) + modeline="// -*-c++-*- Put Emacs into c++-mode"; + Lc='//' + Rc='' + Lb='[' + Rb=']' + start="## BEGIN Code" + finish="## END Code" + constant_declaration="const double " + var_declaration="double " + minusone="-1" + ;; *) echo Language $language not supported - sorry; exit 1 esac if [ "$rep" = "simpar" ]; then @@ -584,11 +602,11 @@ if [ "$parameters" = "yes" ]; then cat < ${OUT} + echo "/* ${SYS}_def.h, generated by MTT on `date` */" > ${OUT} echo "" >> ${OUT} echo "const int MTTNU = `cat ${IN} | get_array_size nu`;" >> ${OUT} echo "const int MTTNX = `cat ${IN} | get_array_size nx`;" >> ${OUT} echo "const int MTTNY = `cat ${IN} | get_array_size ny`;" >> ${OUT} echo "const int MTTNZ = `cat ${IN} | get_array_size nz`;" >> ${OUT} echo "const int MTTNYZ = `cat ${IN} | get_array_size nyz`;" >> ${OUT} echo "const int MTTNPAR = `wc -l ${SYM} | gawk '{ print $1 }'`;" >> ${OUT} - -cat <> ${OUT} - -// typedefs won't work because it is illegal to initialise ColumnVector in typedef -// use "ColumnVector mttx (MTTNX);" until the proper classes are ready - - -#if 0 // NB: These classes are not ready for use yet! -class InputVector : public ColumnVector -{ -public: - InputVector (void) : ColumnVector (MTTNU) { ; } -}; -class StateVector : public ColumnVector -{ -public: - StateVector (void) : ColumnVector (MTTNX) { ; } -}; -class OutputVector : public ColumnVector -{ -public: - OutputVector (void) : ColumnVector (MTTNY) { ; } -}; -class ParameterVector : public ColumnVector -{ -public: - ParameterVector (void) : ColumnVector (MTTNPAR) { ; } -}; -class StateMatrix : public Matrix -{ -public: - StateMatrix (void) : Matrix (MTTNX, MTTNX) { ; } -}; -#endif - -EOF - Index: mttroot/mtt/cc/mtt_m2cc.sh ================================================================== --- mttroot/mtt/cc/mtt_m2cc.sh +++ mttroot/mtt/cc/mtt_m2cc.sh @@ -1,13 +1,14 @@ #! /bin/sh SYS=$1 REP=$2 -PARSER=${3:-indent} +TARGET=${3:-cc} +PARSER=${4:-indent} IN=${SYS}_${REP}.m -OUT=${SYS}_${REP}.cc +OUT=${SYS}_${REP}.${TARGET} TMP=${SYS}_${REP}_m2cc.tmp rep_declarations () { (case ${REP} in @@ -107,21 +108,21 @@ decrement_indices () { # first section appends '-1' to container indices # to convert from FORTRAN-type numbering to C-type numbering - sed 's/mtta(\([0-9][0-9]*\),\([0-9][0-9]*\))/mtta(\1-1,\2-1)/g' |\ - sed 's/mttax(\([0-9][0-9]*\))/mttax(\1-1)/g' |\ - sed 's/mttdx(\([0-9][0-9]*\))/mttdx(\1-1)/g' |\ - sed 's/mttedx(\([0-9][0-9]*\))/mttedx(\1-1)/g' |\ - sed 's/mttpar(\([0-9][0-9]*\))/mttpar(\1-1)/g' |\ - sed 's/mttu(\([0-9][0-9]*\))/mttu(\1-1)/g' |\ - sed 's/mttx(\([0-9][0-9]*\))/mttx(\1-1)/g' |\ - sed 's/mtty(\([0-9][0-9]*\))/mtty(\1-1)/g' |\ - sed 's/mttyz(\([0-9][0-9]*\))/mttyz(\1-1)/g' |\ - sed 's/mttz(\([0-9][0-9]*\))/mttz(\1-1)/g' |\ - sed 's/mttopen(\([0-9][0-9]*\))/mttopen(\1-1)/g' |\ + sed 's/mtta(\([0-9][0-9]*\),\([0-9][0-9]*\))/mtta[\1-1,\2-1]/g' |\ + sed 's/mttax(\([0-9][0-9]*\))/mttax[\1-1]/g' |\ + sed 's/mttdx(\([0-9][0-9]*\))/mttdx[\1-1]/g' |\ + sed 's/mttedx(\([0-9][0-9]*\))/mttedx[\1-1]/g' |\ + sed 's/mttpar(\([0-9][0-9]*\))/mttpar[\1-1]/g' |\ + sed 's/mttu(\([0-9][0-9]*\))/mttu[\1-1]/g' |\ + sed 's/mttx(\([0-9][0-9]*\))/mttx[\1-1]/g' |\ + sed 's/mtty(\([0-9][0-9]*\))/mtty[\1-1]/g' |\ + sed 's/mttyz(\([0-9][0-9]*\))/mttyz[\1-1]/g' |\ + sed 's/mttz(\([0-9][0-9]*\))/mttz[\1-1]/g' |\ + sed 's/mttopen(\([0-9][0-9]*\))/mttopen[\1-1]/g' |\ \ # next sections tidy the code up a bit, but are not necessary sed 's/1\-1\([\,\)]\)/0\1/g' |\ sed 's/2\-1\([\,\)]\)/1\1/g' |\ sed 's/3\-1\([\,\)]\)/2\1/g' |\ @@ -152,10 +153,16 @@ sed 's/800\-1\([\,\)]\)/799\1/g' |\ sed 's/900\-1\([\,\)]\)/899\1/g' |\ sed 's/\([(,]\)0\([0-9]\)/\1\2/g' }; +fortran_to_c_paren () +{ + # converts [i] to (i) + sed 's/\[\([^[]*\)\]/(\1)/g' +} + fix_pow () { # matches number^number where number is one or more digits and one or zero decimal points # converts to pow (number, number) sed 's/\([0-9]*\)\(\.\)\{0,1\}\([0-9]*\)\^\([0-9]*\)\(\.\)\{0,1\}\([0-9]*\)/pow \(\1\2\3,\4\5\6\)/g' @@ -162,18 +169,37 @@ }; echo Creating ${OUT} -mtt_header ${SYS} ${REP} "oct" > ${TMP} -find_code ${TMP} head > ${OUT} -rep_declarations >> ${OUT} -find_code ${IN} body |\ - decrement_indices |\ - fix_comment_delimiter |\ - fix_pow |\ - strip_junk |\ - ${PARSER} >> ${OUT} -rep_footer >> ${OUT} -find_code ${TMP} foot >> ${OUT} -rm ${TMP} +case ${TARGET} in + sfun) + mtt_header ${SYS} ${REP} "sfun" > ${TMP} + echo "## END Code" >> ${TMP} + find_code ${TMP} head > ${OUT} + find_code ${IN} body |\ + decrement_indices |\ + fix_comment_delimiter |\ + fix_pow |\ + strip_junk |\ + ${PARSER} >> ${OUT} + find_code ${TMP} foot >> ${OUT} + rm ${TMP} + ;; + cc | *) + mtt_header ${SYS} ${REP} "oct" > ${TMP} + find_code ${TMP} head > ${OUT} + rep_declarations >> ${OUT} + find_code ${IN} body |\ + decrement_indices |\ + fortran_to_c_paren |\ + fix_comment_delimiter |\ + fix_pow |\ + strip_junk |\ + ${PARSER} >> ${OUT} + rep_footer >> ${OUT} + find_code ${TMP} foot >> ${OUT} + rm ${TMP} + ;; +esac + Index: mttroot/mtt/lib/rep/sfun_rep/Makefile ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/Makefile +++ mttroot/mtt/lib/rep/sfun_rep/Makefile @@ -1,13 +1,29 @@ #! /usr/bin/make -f -$(SYS)_sfun.mexglx: $(SYS)_sfun.cc $(SYS)_def.h $(SYS)_state.mexglx $(SYS)_ode.mexglx $(SYS)_odeo.mexglx - cp -a $(SYS)_ode.mexglx .. - cp -a $(SYS)_odeo.mexglx .. - cp -a $(SYS)_state.mexglx .. +SRC = $(SYS)_sfun.cc $(SYS)_def.h $(SYS)_ae.sfun $(SYS)_ode.sfun $(SYS)_odeo.sfun $(SYS)_state.sfun + +all: $(SYS)_sfun.mexglx + +$(SYS)_sfun.mexglx: $(SRC) mex $(SYS)_sfun.cc $(SYS)_sfun.cc:: ${MTT_REP}/sfun_rep/sfun.cc.tmpl cat $^ | sed 's//$(SYS)/g' > $@ +$(SYS)_sfun.zip: $(SRC) + zip $@ $^ + +$(SYS)_ae.sfun: $(SYS)_ae.m + ${MTT_CC}/mtt_m2cc.sh $(SYS) ae sfun cat + +$(SYS)_ode.sfun: $(SYS)_ode.m + ${MTT_CC}/mtt_m2cc.sh $(SYS) ode sfun cat + +$(SYS)_odeo.sfun: $(SYS)_odeo.m + ${MTT_CC}/mtt_m2cc.sh $(SYS) odeo sfun cat + +$(SYS)_state.sfun: $(SYS)_state.m + ${MTT_CC}/mtt_m2cc.sh $(SYS) state sfun cat + %:: mtt -q $(OPTS) `echo $* | sed 's/\(.*\)_\(.*\)\.\(.*\)/\1 \2 \3/'` Index: mttroot/mtt/lib/rep/sfun_rep/sfun.cc.tmpl ================================================================== --- mttroot/mtt/lib/rep/sfun_rep/sfun.cc.tmpl +++ mttroot/mtt/lib/rep/sfun_rep/sfun.cc.tmpl @@ -1,56 +1,61 @@ // -*-c++-*- -// _sfun.cc: +// _sfun.c: // Matlab S-function simulation of #define S_FUNCTION_NAME _sfun #define S_FUNCTION_LEVEL 2 #include "simstruc.h" -#include #include "_def.h" +static mxArray *dX = mxCreateDoubleMatrix (MTTNX , 1, mxREAL); static mxArray *P = mxCreateDoubleMatrix (MTTNPAR , 1, mxREAL); static mxArray *T = mxCreateDoubleMatrix (1 , 1, mxREAL); static mxArray *U = mxCreateDoubleMatrix (MTTNU , 1, mxREAL); static mxArray *X = mxCreateDoubleMatrix (MTTNX , 1, mxREAL); +static mxArray *Y = mxCreateDoubleMatrix (MTTNY , 1, mxREAL); +static mxArray *YZ = mxCreateDoubleMatrix (MTTNYZ , 1, mxREAL); -static double *inputs = mxGetPr (U); -static double *parameters = mxGetPr (P); -static double *states = mxGetPr (X); -static double *simtime = mxGetPr (T); +static double *mttdx = mxGetPr (dX); +static double *mttu = mxGetPr (U); +static double *mttpar = mxGetPr (P); +static double *mttx = mxGetPr (X); +static double *mtty = mxGetPr (Y); +static double *mttyz = mxGetPr (YZ); +static double *mtttp = mxGetPr (T); void update_parameters_from_simulink (SimStruct *S) { for (int i = 0; i < MTTNPAR; i++) { - parameters [i] = *mxGetPr (ssGetSFcnParam (S, i)); + mttpar [i] = *mxGetPr (ssGetSFcnParam (S, i)); } } void update_states_from_simulink (SimStruct *S) { static double *x; x = ssGetContStates (S); for (int i = 0; i < MTTNX; i++) { - states [i] = x [i]; + mttx [i] = x [i]; } } void update_inputs_from_simulink (SimStruct *S) { for (int i = 0; i < MTTNU; i++) { - inputs [i] = *ssGetInputPortRealSignalPtrs (S, i)[0]; + mttu [i] = *ssGetInputPortRealSignalPtrs (S, i)[0]; } } void update_simtime_from_simulink (SimStruct *S) { - simtime [0] = ssGetT (S); + mtttp [0] = ssGetT (S); } // S-function methods static void mdlInitializeSizes(SimStruct *S) @@ -91,78 +96,48 @@ } #define MDL_INITIALIZE_CONDITIONS static void mdlInitializeConditions(SimStruct *S) { - static mxArray *plhs[1]; - static mxArray *prhs[1]; - - update_parameters_from_simulink (S); - - plhs[0] = X; - prhs[0] = P; - - mexCallMATLAB (1, plhs, 1, prhs, "_state"); - - for (int i = 0; i < MTTNX; i++) { - ssGetContStates (S)[i] = states [i]; - } - - mxDestroyArray (plhs[0]); -} - -static void mdlOutputs(SimStruct *S, int_T tid) -{ - static mxArray *plhs[1]; - static mxArray *prhs[4]; - - update_states_from_simulink (S); - update_inputs_from_simulink (S); - update_simtime_from_simulink (S); - update_parameters_from_simulink (S); - - prhs[0] = X; - prhs[1] = U; - prhs[2] = T; - prhs[3] = P; - - UNUSED_ARG(tid); // not used in single tasking mode - - mexCallMATLAB (1, plhs, 4, prhs, "_odeo"); - double *outputs = mxGetPr (plhs[0]); - - for (int i = 0; i < MTTNY; i++) { - ssGetOutputPortRealSignal (S,i)[0] = outputs [i]; - } - - mxDestroyArray (plhs[0]); -} - -#define MDL_DERIVATIVES -static void mdlDerivatives(SimStruct *S) -{ - static mxArray *plhs[1]; - static mxArray *prhs[4]; - - update_states_from_simulink (S); - update_inputs_from_simulink (S); - update_simtime_from_simulink (S); - update_parameters_from_simulink (S); - - prhs[0] = X; - prhs[1] = U; - prhs[2] = T; - prhs[3] = P; - - mexCallMATLAB (1, plhs, 4, prhs, "_ode"); - double *rates= mxGetPr (plhs[0]); - - for (int i = 0; i < MTTNX; i++) { - ssGetdX (S)[i] = rates [i]; - } - - mxDestroyArray (plhs[0]); + update_parameters_from_simulink (S); + +#include "_state.sfun" + + for (int i = 0; i < MTTNX; i++) { + ssGetContStates (S)[i] = mttx [i]; + } +} + +static void mdlOutputs(SimStruct *S, int_T tid) +{ + update_states_from_simulink (S); + update_inputs_from_simulink (S); + update_simtime_from_simulink (S); + update_parameters_from_simulink (S); + + UNUSED_ARG(tid); // not used in single tasking mode + +#include "_odeo.sfun" + + for (int i = 0; i < MTTNY; i++) { + ssGetOutputPortRealSignal (S,i)[0] = mtty [i]; + } +} + +#define MDL_DERIVATIVES +static void mdlDerivatives(SimStruct *S) +{ + update_states_from_simulink (S); + update_inputs_from_simulink (S); + update_simtime_from_simulink (S); + update_parameters_from_simulink (S); + +#include "_ode.sfun" + + for (int i = 0; i < MTTNX; i++) { + ssGetdX (S)[i] = mttdx [i]; + } } static void mdlTerminate(SimStruct *S) { UNUSED_ARG(S);