Differences From Artifact [24363b5ab0]:

To Artifact [4c433b0d7e]:


1
2
3
4
5
6
7
8
9
10
11



12
13
14
15
16
17
18
#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
    ######################################

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$



## Revision 1.89  2004/08/29 16:04:44  geraint
## Fixed ae for non-sorted code.
##
## Revision 1.88  2004/08/29 13:15:28  geraint
## Uses sys_sae instead of sys_ae if sorted equations are being used.
##
## Revision 1.87  2004/08/29 01:46:56  geraint











>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
    ######################################

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.90  2005/04/26 23:43:34  geraint
## Reset open switches after update to eliminate overshoots.
##
## Revision 1.89  2004/08/29 16:04:44  geraint
## Fixed ae for non-sorted code.
##
## Revision 1.88  2004/08/29 13:15:28  geraint
## Uses sys_sae instead of sys_ae if sorted equations are being used.
##
## Revision 1.87  2004/08/29 01:46:56  geraint
552
553
554
555
556
557
558

559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

cat <<EOF  > $filename

// Code generation directives
#define STANDALONE 0
#define OCTAVEDLD  1
#define MATLABMEX  2

#if (! defined (CODEGENTARGET))
#define CODEGENTARGET STANDALONE
#endif // (! defined (CODEGENTARGET))

#include <octave/oct.h>
#include <octave/lo-mappers.h>
#include <octave/variables.h>

#if (CODEGENTARGET == OCTAVEDLD)
#include <octave/${feval_header}>
#endif // (CODEGENTARGET == OCTAVEDLD)

#include "${sys}_def.h"
#include "${sys}_sympar.h"

#include "mtt_${algebraic_solver}.hh"

#include <fstream>
#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX))

extern ColumnVector ${sys}_${ae} (
	const ColumnVector &x,
	const ColumnVector &u,
	const double &t,
	const ColumnVector &par);








>


















|







555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

cat <<EOF  > $filename

// Code generation directives
#define STANDALONE 0
#define OCTAVEDLD  1
#define MATLABMEX  2
#define RTXI       3
#if (! defined (CODEGENTARGET))
#define CODEGENTARGET STANDALONE
#endif // (! defined (CODEGENTARGET))

#include <octave/oct.h>
#include <octave/lo-mappers.h>
#include <octave/variables.h>

#if (CODEGENTARGET == OCTAVEDLD)
#include <octave/${feval_header}>
#endif // (CODEGENTARGET == OCTAVEDLD)

#include "${sys}_def.h"
#include "${sys}_sympar.h"

#include "mtt_${algebraic_solver}.hh"

#include <fstream>
#if (CODEGENTARGET != OCTAVEDLD)

extern ColumnVector ${sys}_${ae} (
	const ColumnVector &x,
	const ColumnVector &u,
	const double &t,
	const ColumnVector &par);

667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
	const ColumnVector &open_switches);
 
EOF
    ;;
esac
cat <<EOF >> $filename

#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX))

ColumnVector
mtt_ae (const ColumnVector &x,
	const ColumnVector &u,
	const double &t,
	const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_input (const ColumnVector &x,
	   const ColumnVector &y,
	   const double &t,
	   const ColumnVector &par)







|







|









|







671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
	const ColumnVector &open_switches);
 
EOF
    ;;
esac
cat <<EOF >> $filename

#endif // (CODEGENTARGET != OCTAVEDLD)

ColumnVector
mtt_ae (const ColumnVector &x,
	const ColumnVector &u,
	const double &t,
	const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
    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 != OCTAVEDLD)
}

inline ColumnVector
mtt_input (const ColumnVector &x,
	   const ColumnVector &y,
	   const double &t,
	   const ColumnVector &par)
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823

inline ColumnVector
mtt_logic (const ColumnVector &x,
	   const ColumnVector &u,
	   const double &t,
	   const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_numpar (void)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_simpar (void)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_state (const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_rate (const ColumnVector &x,
	  const ColumnVector &u,
	  const double &t,
	  const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_output (const ColumnVector &x,
	    const ColumnVector &u,
	    const double &t,
	    const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_zeroswitches (ColumnVector &x,
		  const int Nx,
		  const ColumnVector &openx)
{







|









|





|





|





|





|





|






|








|









|








|









|







728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827

inline ColumnVector
mtt_logic (const ColumnVector &x,
	   const ColumnVector &u,
	   const double &t,
	   const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_numpar (void)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_simpar (void)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_state (const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_rate (const ColumnVector &x,
	  const ColumnVector &u,
	  const double &t,
	  const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_output (const ColumnVector &x,
	    const ColumnVector &u,
	    const double &t,
	    const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_zeroswitches (ColumnVector &x,
		  const int Nx,
		  const ColumnVector &openx)
{
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
	      ColumnVector &dx,
	      Matrix &AA,
	      ColumnVector &AAx,
	      const double &ddt,
	      const int &nx,
	      const ColumnVector &open_switches)
{
#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);
  args (2) = octave_value (AA);
  args (3) = octave_value (AAx);
  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) || (CODEGENTARGET == MATLABMEX))
}

inline Matrix
mtt_smxa (const ColumnVector &x,
	  const ColumnVector &u,
	  const double &t,
	  const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

inline ColumnVector
mtt_smxax (const ColumnVector &x,
	   const ColumnVector &u,
	   const double &t,
	   const ColumnVector &par)
{
#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) || (CODEGENTARGET == MATLABMEX))
}

EOF
    ;;
    "dassl")
cat <<EOF >> $filename
inline ColumnVector
mtt_dassl (ColumnVector &x,
	   const ColumnVector &u,
	   const double &t,
	   const ColumnVector &par,
	   const ColumnVector &dx,
	   const double &ddt,
	   const int &nx,
	   const int &nyz,
	   const ColumnVector &open_switches)
{
#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);
    args (2) = octave_value (t);
    args (3) = octave_value (par);
    args (4) = octave_value (dx);
    args (5) = octave_value (ddt);
    args (6) = octave_value (static_cast<double> (nx));
    args (7) = octave_value (static_cast<double> (nyz));
    args (8) = octave_value (open_switches);
    f = feval ("mtt_dassl", args, 1);
    return f(0).${vector_value} ();
#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX))
}

#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX))
ColumnVector
Fmtt_residual (const ColumnVector &X,
	      const ColumnVector &DX,
	      double t,
	      int &ires)
{
#elif (CODEGENTARGET == OCTAVEDLD)
DEFUN_DLD (mtt_residual, args, , "mtt_residual")
{
    static ColumnVector	X  (MTTNX+MTTNYZ);
    static ColumnVector	DX (MTTNX+MTTNYZ);
    static double      	t;
    static int		ires;

    X  = args(0).${vector_value} ();
    DX = args(1).${vector_value} ();
    t  = args(2).double_value ();
    ires = static_cast<int>(args(3).double_value ());
#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);
    static ColumnVector par (MTTNPAR);
    static ColumnVector dx(MTTNX);
    static ColumnVector yz(MTTNYZ);

    static ColumnVector x (MTTNX);
    static ColumnVector ui (MTTNYZ);

    static octave_value_list new_args;

    x = X.extract (0,MTTNX-1);
    if (MTTNYZ > 0)
      ui = X.extract (MTTNX,MTTNX+MTTNYZ-1);    

#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) || (CODEGENTARGET == MATLABMEX))

    U.insert (u,0);
    if (MTTNYZ > 0)
      U.insert (ui,MTTNU);

#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) || (CODEGENTARGET == MATLABMEX))

    for (register int i = 0; i < MTTNX; i++)
      residual (i) = dx(i) - DX(i);
    
    if (MTTNYZ > 0)
      {
	residual.insert (yz,MTTNX);







|












|








|









|








|









|

















|














|


|


















|


















|









|





|






|







843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
	      ColumnVector &dx,
	      Matrix &AA,
	      ColumnVector &AAx,
	      const double &ddt,
	      const int &nx,
	      const ColumnVector &open_switches)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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);
  args (2) = octave_value (AA);
  args (3) = octave_value (AAx);
  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 != OCTAVEDLD)
}

inline Matrix
mtt_smxa (const ColumnVector &x,
	  const ColumnVector &u,
	  const double &t,
	  const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

inline ColumnVector
mtt_smxax (const ColumnVector &x,
	   const ColumnVector &u,
	   const double &t,
	   const ColumnVector &par)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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 != OCTAVEDLD)
}

EOF
    ;;
    "dassl")
cat <<EOF >> $filename
inline ColumnVector
mtt_dassl (ColumnVector &x,
	   const ColumnVector &u,
	   const double &t,
	   const ColumnVector &par,
	   const ColumnVector &dx,
	   const double &ddt,
	   const int &nx,
	   const int &nyz,
	   const ColumnVector &open_switches)
{
#if (CODEGENTARGET != OCTAVEDLD)
   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);
    args (2) = octave_value (t);
    args (3) = octave_value (par);
    args (4) = octave_value (dx);
    args (5) = octave_value (ddt);
    args (6) = octave_value (static_cast<double> (nx));
    args (7) = octave_value (static_cast<double> (nyz));
    args (8) = octave_value (open_switches);
    f = feval ("mtt_dassl", args, 1);
    return f(0).${vector_value} ();
#endif // (CODEGENTARGET != OCTAVEDLD)
}

#if (CODEGENTARGET != OCTAVEDLD)
ColumnVector
Fmtt_residual (const ColumnVector &X,
	      const ColumnVector &DX,
	      double t,
	      int &ires)
{
#elif (CODEGENTARGET == OCTAVEDLD)
DEFUN_DLD (mtt_residual, args, , "mtt_residual")
{
    static ColumnVector	X  (MTTNX+MTTNYZ);
    static ColumnVector	DX (MTTNX+MTTNYZ);
    static double      	t;
    static int		ires;

    X  = args(0).${vector_value} ();
    DX = args(1).${vector_value} ();
    t  = args(2).double_value ();
    ires = static_cast<int>(args(3).double_value ());
#endif // (CODEGENTARGET != OCTAVEDLD)

    static ColumnVector residual (MTTNX+MTTNYZ);
    static ColumnVector U (MTTNU+MTTNYZ);
    static ColumnVector u (MTTNU);
    static ColumnVector y (MTTNY,0.0);
    static ColumnVector par (MTTNPAR);
    static ColumnVector dx(MTTNX);
    static ColumnVector yz(MTTNYZ);

    static ColumnVector x (MTTNX);
    static ColumnVector ui (MTTNYZ);

    static octave_value_list new_args;

    x = X.extract (0,MTTNX-1);
    if (MTTNYZ > 0)
      ui = X.extract (MTTNX,MTTNX+MTTNYZ-1);    

#if (CODEGENTARGET != OCTAVEDLD)
    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 != OCTAVEDLD)

    U.insert (u,0);
    if (MTTNYZ > 0)
      U.insert (ui,MTTNU);

#if (CODEGENTARGET != OCTAVEDLD)
    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 != OCTAVEDLD)

    for (register int i = 0; i < MTTNX; i++)
      residual (i) = dx(i) - DX(i);
    
    if (MTTNYZ > 0)
      {
	residual.insert (yz,MTTNX);
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
	  }
	else
	  step = t;	
	for (register int i = MTTNX; i < MTTNX+MTTNYZ; i++)
	  residual(i) += X(i) - DX(i)*step;
      }

#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX))
    return residual;
#elif (CODEGENTARGET == OCTAVEDLD)
    return octave_value (residual);
#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == MATLABMEX))
}
    
EOF
    ;;
    "euler" | "rk4" | *)
cat <<EOF >> $filename
inline ColumnVector
mtt_euler (ColumnVector &x,
	   const ColumnVector &dx,
	   const double &ddt,
	   const int &nx,
	   const ColumnVector &open_switches)
{
#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);
  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) || (CODEGENTARGET == MATLABMEX))
}

EOF
    ;;
esac
cat <<EOF >> $filename








|



|













|










|







1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
	  }
	else
	  step = t;	
	for (register int i = MTTNX; i < MTTNX+MTTNYZ; i++)
	  residual(i) += X(i) - DX(i)*step;
      }

#if (CODEGENTARGET != OCTAVEDLD)
    return residual;
#elif (CODEGENTARGET == OCTAVEDLD)
    return octave_value (residual);
#endif // (CODEGENTARGET != OCTAVEDLD)
}
    
EOF
    ;;
    "euler" | "rk4" | *)
cat <<EOF >> $filename
inline ColumnVector
mtt_euler (ColumnVector &x,
	   const ColumnVector &dx,
	   const double &ddt,
	   const int &nx,
	   const ColumnVector &open_switches)
{
#if (CODEGENTARGET != OCTAVEDLD)
  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);
  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 != OCTAVEDLD)
}

EOF
    ;;
esac
cat <<EOF >> $filename

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125













1126
1127
1128
1129
1130
1131

1132
1133
1134
1135
1136
1137
1138
  fcputime $insertor t $insertor '\t'
	   $insertor static_cast <double> (cputime - cputime0) / CLOCKS_PER_SEC $insertor '\t'
	   $insertor static_cast <double> (cputime - cputime1) / CLOCKS_PER_SEC $insertor std::endl;
  cputime1 = cputime;

  if (nrows == row)
    {
#if (CODEGENTARGET == STANDALONE)
      file $insertor "# name: mtt_dump" $insertor std::endl
	   $insertor "# type: matrix" $insertor std::endl
	   $insertor "# rows: " $insertor data.rows () $insertor std::endl
	   $insertor "# columns: " $insertor data.columns () $insertor std::endl
	   $insertor data;
      file.flush ();
#elif ((CODEGENTARGET == OCTAVEDLD) || (CODEGENTARGET == MATLABMEX))
  set_global_value ("MTT_data", data);
#endif // (CODEGENTARGET == STANDALONE)
    row = 0;
    fcputime.close();
    }
}














void
${sys}_ode2odes (const ColumnVector &state0,
		 const ColumnVector &par,
		 const ColumnVector &simpar)
{

  static double first, dt, last, stepfactor;
  first		= simpar (0);
  last		= simpar (1);
  dt		= simpar (2);
  stepfactor	= simpar (3);

  static ColumnVector	dx (MTTNX, 0.0);







|








|




>
>
>
>
>
>
>
>
>
>
>
>
>






>







1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
  fcputime $insertor t $insertor '\t'
	   $insertor static_cast <double> (cputime - cputime0) / CLOCKS_PER_SEC $insertor '\t'
	   $insertor static_cast <double> (cputime - cputime1) / CLOCKS_PER_SEC $insertor std::endl;
  cputime1 = cputime;

  if (nrows == row)
    {
#if ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == RTXI))
      file $insertor "# name: mtt_dump" $insertor std::endl
	   $insertor "# type: matrix" $insertor std::endl
	   $insertor "# rows: " $insertor data.rows () $insertor std::endl
	   $insertor "# columns: " $insertor data.columns () $insertor std::endl
	   $insertor data;
      file.flush ();
#elif ((CODEGENTARGET == OCTAVEDLD) || (CODEGENTARGET == MATLABMEX))
  set_global_value ("MTT_data", data);
#endif // ((CODEGENTARGET == STANDALONE) || (CODEGENTARGET == RTXI))
    row = 0;
    fcputime.close();
    }
}

#if (CODEGENTARGET == RTXI)
#include "${sys}_rtxi.h"
void
${sys}::execute(void)
{
  static ColumnVector simpar (8), par (MTTNPAR), state0 (MTTNX);

  simpar	= mtt_simpar ();
  par		= mtt_numpar ();
  state0	= mtt_state (par);
  
#else

void
${sys}_ode2odes (const ColumnVector &state0,
		 const ColumnVector &par,
		 const ColumnVector &simpar)
{
#endif // (CODEGENTARGET == RTXI)
  static double first, dt, last, stepfactor;
  first		= simpar (0);
  last		= simpar (1);
  dt		= simpar (2);
  stepfactor	= simpar (3);

  static ColumnVector	dx (MTTNX, 0.0);
1150
1151
1152
1153
1154
1155
1156



1157
1158
1159
1160
1161
1162
1163
1164
1165

1166





1167
1168
1169

1170





1171
1172
1173
1174
1175
1176
1177
  const double	ddt	= dt / stepfactor;
  const int	ilast	= static_cast<int> (round ( last  / ddt)) + 1;
  const int	nrows	= static_cast<int> (round ((last - first) / dt)) + 1;

  for (register int i = 0; i < MTTNY; i++)
    {
      y (i) = 0.0;



    }

  for (register int i = 0; i < MTTNX; i++)
    {
      x (i) = state0 (i);
    }

  for (register int j = 0, i = 1; i <= ilast; i++)
    {

      u	= mtt_input (x, y, t, par);





      y	= mtt_output (x, u, t, par);
      if (0 == j)
	{

           mtt_write (t, x, y, first, nrows);





	}
EOF
case "$method" in
    "rk4")
	cat << EOF >> $filename
      {
        static ColumnVector







>
>
>









>

>
>
>
>
>



>
|
>
>
>
>
>







1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
  const double	ddt	= dt / stepfactor;
  const int	ilast	= static_cast<int> (round ( last  / ddt)) + 1;
  const int	nrows	= static_cast<int> (round ((last - first) / dt)) + 1;

  for (register int i = 0; i < MTTNY; i++)
    {
      y (i) = 0.0;
#if (CODEGENTARGET == RTXI)
      output(i) = y(i);
#endif // (CODEGENTARGET == RTXI)
    }

  for (register int i = 0; i < MTTNX; i++)
    {
      x (i) = state0 (i);
    }

  for (register int j = 0, i = 1; i <= ilast; i++)
    {
#if (CODEGENTARGET != RTXI)
      u	= mtt_input (x, y, t, par);
#else
      for (register int i = 0; i < MTTNU; i++) {
	u(i) = input(i);
      }
#endif // (CODEGENTARGET != RTXI)
      y	= mtt_output (x, u, t, par);
      if (0 == j)
	{
#if (CODEGENTARGET != RTXI)
	  mtt_write (t, x, y, first, nrows);
#else
	  for (register int i = 0; i < MTTNU; i++) {
	    output(i) = y(i);
	  }
#endif // (CODEGENTARGET != RTXI)
	}
EOF
case "$method" in
    "rk4")
	cat << EOF >> $filename
      {
        static ColumnVector
1374
1375
1376
1377
1378
1379
1380

1381
1382
1383
1384
1385
1386
1387
      }

    ${sys}_ode2odes (state0, numpar, simpar);

    plhs[0] = mtt_mxArray (get_global_value ("MTT_data").matrix_value ());
  }
}

#endif // (CODEGENTARGET == STANDALONE)

EOF
}

case ${lang} in
    m)







>







1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
      }

    ${sys}_ode2odes (state0, numpar, simpar);

    plhs[0] = mtt_mxArray (get_global_value ("MTT_data").matrix_value ());
  }
}

#endif // (CODEGENTARGET == STANDALONE)

EOF
}

case ${lang} in
    m)

MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]